]> sigrok.org Git - libsigrok.git/blame - hwdriver.c
sr: fx2lafw: Add basic support for USBee DX and clones
[libsigrok.git] / hwdriver.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <stdlib.h>
21#include <stdio.h>
22#include <sys/types.h>
23#include <dirent.h>
24#include <string.h>
25#include <glib.h>
45c59c8b
BV
26#include "libsigrok.h"
27#include "libsigrok-internal.h"
a1bb33af 28
62c82025 29/*
c09f0b57 30 * This enumerates which driver capabilities correspond to user-settable
62c82025
UH
31 * options.
32 */
a65de030 33/* TODO: This shouldn't be a global. */
1a081ca6 34SR_API struct sr_hwcap_option sr_hwcap_options[] = {
5a2326a7
UH
35 {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
36 {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
eb0a3731 37 {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "pattern"},
4d436e71 38 {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
0fe11789
BV
39 {SR_HWCAP_TRIGGER_SLOPE, SR_T_UINT64, "Trigger slope", "triggerslope"},
40 {SR_HWCAP_TRIGGER_SOURCE, SR_T_CHAR, "Trigger source", "triggersource"},
41 {SR_HWCAP_HORIZ_TRIGGERPOS, SR_T_FLOAT, "Horizontal trigger position",
42 "horiz_triggerpos"},
43 {SR_HWCAP_BUFFERSIZE, SR_T_UINT64, "Buffer size", "buffersize"},
c1e48618 44 {SR_HWCAP_TIMEBASE, SR_T_RATIONAL_PERIOD, "Time base", "timebase"},
3c4976c9 45 {SR_HWCAP_FILTER, SR_T_CHAR, "Filter targets", "filter"},
bd8db307 46 {SR_HWCAP_VDIV, SR_T_RATIONAL_VOLT, "Volts/div", "vdiv"},
e1c8b2ab 47 {SR_HWCAP_COUPLING, SR_T_CHAR, "Coupling", "coupling"},
ca3d84cc
BV
48 {SR_HWCAP_MODEL, SR_T_KEYVALUE, "Model", "model"},
49 {SR_HWCAP_CONN, SR_T_CHAR, "Connection", "connect"},
50 {SR_HWCAP_SERIALCOMM, SR_T_CHAR, "Serial communication", "serialcomm"},
49d0ce50 51 {0, 0, NULL, NULL},
a1bb33af
UH
52};
53
a61b0e6a 54#ifdef HAVE_LA_DEMO
c09f0b57 55extern SR_PRIV struct sr_dev_driver demo_driver_info;
a61b0e6a 56#endif
960a75e4 57#ifdef HAVE_LA_OLS
c09f0b57 58extern SR_PRIV struct sr_dev_driver ols_driver_info;
960a75e4
UH
59#endif
60#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
c09f0b57 61extern SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
960a75e4 62#endif
5b907f9b 63#ifdef HAVE_LA_ASIX_SIGMA
c09f0b57 64extern SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
5b907f9b 65#endif
f4314d7e 66#ifdef HAVE_LA_CHRONOVU_LA8
c09f0b57 67extern SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
f4314d7e 68#endif
bffed4fc 69#ifdef HAVE_LA_LINK_MSO19
c09f0b57 70extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
bffed4fc 71#endif
6ed4f044 72#ifdef HAVE_LA_ALSA
c09f0b57 73extern SR_PRIV struct sr_dev_driver alsa_driver_info;
6ed4f044 74#endif
f302a082 75#ifdef HAVE_LA_FX2LAFW
c09f0b57 76extern SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
f302a082 77#endif
3b533202 78#ifdef HAVE_HW_HANTEK_DSO
62bb8840 79extern SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
3b533202 80#endif
ca3d84cc
BV
81#ifdef HAVE_HW_GENERICDMM
82extern SR_PRIV struct sr_dev_driver genericdmm_driver_info;
83#endif
6ed4f044 84
c09f0b57 85static struct sr_dev_driver *drivers_list[] = {
a61b0e6a 86#ifdef HAVE_LA_DEMO
c09f0b57 87 &demo_driver_info,
a61b0e6a 88#endif
960a75e4 89#ifdef HAVE_LA_OLS
c09f0b57 90 &ols_driver_info,
960a75e4
UH
91#endif
92#ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
c09f0b57 93 &zeroplus_logic_cube_driver_info,
960a75e4 94#endif
5b907f9b 95#ifdef HAVE_LA_ASIX_SIGMA
c09f0b57 96 &asix_sigma_driver_info,
5b907f9b 97#endif
f4314d7e 98#ifdef HAVE_LA_CHRONOVU_LA8
c09f0b57 99 &chronovu_la8_driver_info,
f4314d7e 100#endif
bffed4fc 101#ifdef HAVE_LA_LINK_MSO19
c09f0b57 102 &link_mso19_driver_info,
bffed4fc 103#endif
6ed4f044 104#ifdef HAVE_LA_ALSA
c09f0b57 105 &alsa_driver_info,
f302a082
JH
106#endif
107#ifdef HAVE_LA_FX2LAFW
c09f0b57 108 &fx2lafw_driver_info,
3b533202
BV
109#endif
110#ifdef HAVE_HW_HANTEK_DSO
62bb8840 111 &hantek_dso_driver_info,
6ed4f044 112#endif
ca3d84cc
BV
113#ifdef HAVE_HW_GENERICDMM
114 &genericdmm_driver_info,
eb1f1eb4 115#endif
050e9219
UH
116 NULL,
117};
a1bb33af 118
a1645fcd 119/**
cfe064d8 120 * Return the list of supported hardware drivers.
a1645fcd 121 *
c09f0b57 122 * @return Pointer to the NULL-terminated list of hardware driver pointers.
a1645fcd 123 */
cfe064d8 124SR_API struct sr_dev_driver **sr_driver_list(void)
a1bb33af 125{
c09f0b57 126 return drivers_list;
a1bb33af
UH
127}
128
a1645fcd 129/**
c09f0b57 130 * Initialize a hardware driver.
a1645fcd 131 *
c09f0b57
UH
132 * The specified driver is initialized, and all devices discovered by the
133 * driver are instantiated.
a1645fcd 134 *
c09f0b57 135 * @param driver The driver to initialize.
a1645fcd 136 *
c09f0b57 137 * @return The number of devices found and instantiated by the driver.
a1645fcd 138 */
cfe064d8 139SR_API int sr_driver_init(struct sr_dev_driver *driver)
8722c31e 140{
bb7ef793
UH
141 int num_devs, num_probes, i, j;
142 int num_initialized_devs = 0;
143 struct sr_dev *dev;
464d12c7 144 char **probe_names;
8722c31e 145
c09f0b57
UH
146 sr_dbg("initializing %s driver", driver->name);
147 num_devs = driver->init(NULL);
bb7ef793 148 for (i = 0; i < num_devs; i++) {
4d436e71 149 num_probes = GPOINTER_TO_INT(
c09f0b57
UH
150 driver->dev_info_get(i, SR_DI_NUM_PROBES));
151 probe_names = (char **)driver->dev_info_get(i,
c37d2b1b 152 SR_DI_PROBE_NAMES);
464d12c7
KS
153
154 if (!probe_names) {
c09f0b57
UH
155 sr_warn("hwdriver: %s: driver %s does not return a "
156 "list of probe names", __func__, driver->name);
464d12c7
KS
157 continue;
158 }
159
c09f0b57 160 dev = sr_dev_new(driver, i);
c37d2b1b 161 for (j = 0; j < num_probes; j++)
bb7ef793
UH
162 sr_dev_probe_add(dev, probe_names[j]);
163 num_initialized_devs++;
8722c31e
BV
164 }
165
bb7ef793 166 return num_initialized_devs;
8722c31e
BV
167}
168
93a04e3b 169SR_PRIV void sr_hw_cleanup_all(void)
8722c31e 170{
050e9219 171 int i;
c09f0b57 172 struct sr_dev_driver **drivers;
8722c31e 173
cfe064d8 174 drivers = sr_driver_list();
c09f0b57
UH
175 for (i = 0; drivers[i]; i++) {
176 if (drivers[i]->cleanup)
177 drivers[i]->cleanup();
8722c31e 178 }
8722c31e
BV
179}
180
d68e2d1a 181SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
49d0ce50 182 const char *vendor, const char *model, const char *version)
a1bb33af 183{
d68e2d1a 184 struct sr_dev_inst *sdi;
a1bb33af 185
d68e2d1a 186 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
c09f0b57 187 sr_err("hwdriver: %s: sdi malloc failed", __func__);
a1bb33af 188 return NULL;
133a37bf 189 }
a1bb33af
UH
190
191 sdi->index = index;
192 sdi->status = status;
1d9a8a5f 193 sdi->inst_type = -1;
8722c31e
BV
194 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
195 sdi->model = model ? g_strdup(model) : NULL;
196 sdi->version = version ? g_strdup(version) : NULL;
01cf8814 197 sdi->priv = NULL;
a1bb33af
UH
198
199 return sdi;
200}
201
bb7ef793 202SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
a1bb33af 203{
d68e2d1a 204 struct sr_dev_inst *sdi;
a1bb33af
UH
205 GSList *l;
206
d68e2d1a
UH
207 for (l = dev_insts; l; l = l->next) {
208 sdi = (struct sr_dev_inst *)(l->data);
bb7ef793 209 if (sdi->index == dev_index)
a1bb33af
UH
210 return sdi;
211 }
bb7ef793 212 sr_warn("could not find device index %d instance", dev_index);
a1bb33af
UH
213
214 return NULL;
215}
216
d68e2d1a 217SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
a1bb33af 218{
66410a86 219 g_free(sdi->priv);
8722c31e
BV
220 g_free(sdi->vendor);
221 g_free(sdi->model);
222 g_free(sdi->version);
223 g_free(sdi);
a1bb33af
UH
224}
225
22b02383
UH
226#ifdef HAVE_LIBUSB_1_0
227
d68e2d1a 228SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
62c82025 229 uint8_t address, struct libusb_device_handle *hdl)
a1bb33af 230{
d68e2d1a 231 struct sr_usb_dev_inst *udi;
a1bb33af 232
d68e2d1a 233 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
c09f0b57 234 sr_err("hwdriver: %s: udi malloc failed", __func__);
a1bb33af 235 return NULL;
133a37bf 236 }
a1bb33af
UH
237
238 udi->bus = bus;
239 udi->address = address;
ca3d84cc 240 udi->devhdl = hdl;
a1bb33af
UH
241
242 return udi;
243}
244
d68e2d1a 245SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
a1bb33af 246{
17e1afcb 247 /* Avoid compiler warnings. */
cb93f8a9 248 (void)usb;
a1bb33af 249
62c82025 250 /* Nothing to do for this device instance type. */
a1bb33af
UH
251}
252
22b02383
UH
253#endif
254
d68e2d1a
UH
255SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
256 int fd)
a1bb33af 257{
d68e2d1a 258 struct sr_serial_dev_inst *serial;
a1bb33af 259
d68e2d1a 260 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
c09f0b57 261 sr_err("hwdriver: %s: serial malloc failed", __func__);
a1bb33af 262 return NULL;
133a37bf 263 }
a1bb33af 264
133a37bf 265 serial->port = g_strdup(port);
a1bb33af
UH
266 serial->fd = fd;
267
268 return serial;
269}
270
d68e2d1a 271SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
a1bb33af 272{
133a37bf 273 g_free(serial->port);
a1bb33af
UH
274}
275
a1645fcd 276/**
c09f0b57 277 * Find out if a hardware driver has a specific capability.
a1645fcd 278 *
c09f0b57 279 * @param driver The hardware driver in which to search for the capability.
a1645fcd
BV
280 * @param hwcap The capability to find in the list.
281 *
c496ac97
UH
282 * @return TRUE if the specified capability exists in the specified driver,
283 * FALSE otherwise. Also, if 'driver' is NULL or the respective driver
284 * returns an invalid capability list, FALSE is returned.
a1645fcd 285 */
cfe064d8 286SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
a1bb33af 287{
915f7cc8
JH
288 const int *hwcaps;
289 int i;
a1bb33af 290
c496ac97
UH
291 if (!driver) {
292 sr_err("hwdriver: %s: driver was NULL", __func__);
293 return FALSE;
294 }
295
296 if (!(hwcaps = driver->hwcap_get_all())) {
297 sr_err("hwdriver: %s: hwcap_get_all() returned NULL", __func__);
298 return FALSE;
299 }
300
ffedd0bf
UH
301 for (i = 0; hwcaps[i]; i++) {
302 if (hwcaps[i] == hwcap)
a1bb33af 303 return TRUE;
49d0ce50 304 }
a1bb33af
UH
305
306 return FALSE;
307}
308
a1645fcd 309/**
c09f0b57 310 * Get a hardware driver capability option.
a1645fcd 311 *
93a04e3b 312 * @param hwcap The capability to get.
a1645fcd 313 *
44dae539
UH
314 * @return A pointer to a struct with information about the parameter, or NULL
315 * if the capability was not found.
a1645fcd 316 */
915f7cc8 317SR_API const struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
a1bb33af 318{
a1bb33af
UH
319 int i;
320
ffedd0bf
UH
321 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
322 if (sr_hwcap_options[i].hwcap == hwcap)
a65de030 323 return &sr_hwcap_options[i];
a1bb33af
UH
324 }
325
49d0ce50 326 return NULL;
a1bb33af
UH
327}
328
69cfcfc8 329/* Unnecessary level of indirection follows. */
544a4582 330
69cfcfc8 331SR_PRIV int sr_source_remove(int fd)
a1bb33af 332{
69cfcfc8 333 return sr_session_source_remove(fd);
a1bb33af
UH
334}
335
69cfcfc8
UH
336SR_PRIV int sr_source_add(int fd, int events, int timeout,
337 sr_receive_data_callback_t cb, void *cb_data)
a1bb33af 338{
69cfcfc8 339 return sr_session_source_add(fd, events, timeout, cb, cb_data);
a1bb33af 340}