]> sigrok.org Git - libsigrok.git/blob - hwdriver.c
sr: fx2lafw: Add basic support for USBee DX and clones
[libsigrok.git] / hwdriver.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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>
26 #include "libsigrok.h"
27 #include "libsigrok-internal.h"
28
29 /*
30  * This enumerates which driver capabilities correspond to user-settable
31  * options.
32  */
33 /* TODO: This shouldn't be a global. */
34 SR_API struct sr_hwcap_option sr_hwcap_options[] = {
35         {SR_HWCAP_SAMPLERATE, SR_T_UINT64, "Sample rate", "samplerate"},
36         {SR_HWCAP_CAPTURE_RATIO, SR_T_UINT64, "Pre-trigger capture ratio", "captureratio"},
37         {SR_HWCAP_PATTERN_MODE, SR_T_CHAR, "Pattern generator mode", "pattern"},
38         {SR_HWCAP_RLE, SR_T_BOOL, "Run Length Encoding", "rle"},
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"},
44         {SR_HWCAP_TIMEBASE, SR_T_RATIONAL_PERIOD, "Time base", "timebase"},
45         {SR_HWCAP_FILTER, SR_T_CHAR, "Filter targets", "filter"},
46         {SR_HWCAP_VDIV, SR_T_RATIONAL_VOLT, "Volts/div", "vdiv"},
47         {SR_HWCAP_COUPLING, SR_T_CHAR, "Coupling", "coupling"},
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"},
51         {0, 0, NULL, NULL},
52 };
53
54 #ifdef HAVE_LA_DEMO
55 extern SR_PRIV struct sr_dev_driver demo_driver_info;
56 #endif
57 #ifdef HAVE_LA_OLS
58 extern SR_PRIV struct sr_dev_driver ols_driver_info;
59 #endif
60 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
61 extern SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
62 #endif
63 #ifdef HAVE_LA_ASIX_SIGMA
64 extern SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
65 #endif
66 #ifdef HAVE_LA_CHRONOVU_LA8
67 extern SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
68 #endif
69 #ifdef HAVE_LA_LINK_MSO19
70 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
71 #endif
72 #ifdef HAVE_LA_ALSA
73 extern SR_PRIV struct sr_dev_driver alsa_driver_info;
74 #endif
75 #ifdef HAVE_LA_FX2LAFW
76 extern SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
77 #endif
78 #ifdef HAVE_HW_HANTEK_DSO
79 extern SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
80 #endif
81 #ifdef HAVE_HW_GENERICDMM
82 extern SR_PRIV struct sr_dev_driver genericdmm_driver_info;
83 #endif
84
85 static struct sr_dev_driver *drivers_list[] = {
86 #ifdef HAVE_LA_DEMO
87         &demo_driver_info,
88 #endif
89 #ifdef HAVE_LA_OLS
90         &ols_driver_info,
91 #endif
92 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
93         &zeroplus_logic_cube_driver_info,
94 #endif
95 #ifdef HAVE_LA_ASIX_SIGMA
96         &asix_sigma_driver_info,
97 #endif
98 #ifdef HAVE_LA_CHRONOVU_LA8
99         &chronovu_la8_driver_info,
100 #endif
101 #ifdef HAVE_LA_LINK_MSO19
102         &link_mso19_driver_info,
103 #endif
104 #ifdef HAVE_LA_ALSA
105         &alsa_driver_info,
106 #endif
107 #ifdef HAVE_LA_FX2LAFW
108         &fx2lafw_driver_info,
109 #endif
110 #ifdef HAVE_HW_HANTEK_DSO
111         &hantek_dso_driver_info,
112 #endif
113 #ifdef HAVE_HW_GENERICDMM
114         &genericdmm_driver_info,
115 #endif
116         NULL,
117 };
118
119 /**
120  * Return the list of supported hardware drivers.
121  *
122  * @return Pointer to the NULL-terminated list of hardware driver pointers.
123  */
124 SR_API struct sr_dev_driver **sr_driver_list(void)
125 {
126         return drivers_list;
127 }
128
129 /**
130  * Initialize a hardware driver.
131  *
132  * The specified driver is initialized, and all devices discovered by the
133  * driver are instantiated.
134  *
135  * @param driver The driver to initialize.
136  *
137  * @return The number of devices found and instantiated by the driver.
138  */
139 SR_API int sr_driver_init(struct sr_dev_driver *driver)
140 {
141         int num_devs, num_probes, i, j;
142         int num_initialized_devs = 0;
143         struct sr_dev *dev;
144         char **probe_names;
145
146         sr_dbg("initializing %s driver", driver->name);
147         num_devs = driver->init(NULL);
148         for (i = 0; i < num_devs; i++) {
149                 num_probes = GPOINTER_TO_INT(
150                                 driver->dev_info_get(i, SR_DI_NUM_PROBES));
151                 probe_names = (char **)driver->dev_info_get(i,
152                                                         SR_DI_PROBE_NAMES);
153
154                 if (!probe_names) {
155                         sr_warn("hwdriver: %s: driver %s does not return a "
156                                 "list of probe names", __func__, driver->name);
157                         continue;
158                 }
159
160                 dev = sr_dev_new(driver, i);
161                 for (j = 0; j < num_probes; j++)
162                         sr_dev_probe_add(dev, probe_names[j]);
163                 num_initialized_devs++;
164         }
165
166         return num_initialized_devs;
167 }
168
169 SR_PRIV void sr_hw_cleanup_all(void)
170 {
171         int i;
172         struct sr_dev_driver **drivers;
173
174         drivers = sr_driver_list();
175         for (i = 0; drivers[i]; i++) {
176                 if (drivers[i]->cleanup)
177                         drivers[i]->cleanup();
178         }
179 }
180
181 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
182                 const char *vendor, const char *model, const char *version)
183 {
184         struct sr_dev_inst *sdi;
185
186         if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
187                 sr_err("hwdriver: %s: sdi malloc failed", __func__);
188                 return NULL;
189         }
190
191         sdi->index = index;
192         sdi->status = status;
193         sdi->inst_type = -1;
194         sdi->vendor = vendor ? g_strdup(vendor) : NULL;
195         sdi->model = model ? g_strdup(model) : NULL;
196         sdi->version = version ? g_strdup(version) : NULL;
197         sdi->priv = NULL;
198
199         return sdi;
200 }
201
202 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
203 {
204         struct sr_dev_inst *sdi;
205         GSList *l;
206
207         for (l = dev_insts; l; l = l->next) {
208                 sdi = (struct sr_dev_inst *)(l->data);
209                 if (sdi->index == dev_index)
210                         return sdi;
211         }
212         sr_warn("could not find device index %d instance", dev_index);
213
214         return NULL;
215 }
216
217 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
218 {
219         g_free(sdi->priv);
220         g_free(sdi->vendor);
221         g_free(sdi->model);
222         g_free(sdi->version);
223         g_free(sdi);
224 }
225
226 #ifdef HAVE_LIBUSB_1_0
227
228 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
229                         uint8_t address, struct libusb_device_handle *hdl)
230 {
231         struct sr_usb_dev_inst *udi;
232
233         if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
234                 sr_err("hwdriver: %s: udi malloc failed", __func__);
235                 return NULL;
236         }
237
238         udi->bus = bus;
239         udi->address = address;
240         udi->devhdl = hdl;
241
242         return udi;
243 }
244
245 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
246 {
247         /* Avoid compiler warnings. */
248         (void)usb;
249
250         /* Nothing to do for this device instance type. */
251 }
252
253 #endif
254
255 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
256                                                           int fd)
257 {
258         struct sr_serial_dev_inst *serial;
259
260         if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
261                 sr_err("hwdriver: %s: serial malloc failed", __func__);
262                 return NULL;
263         }
264
265         serial->port = g_strdup(port);
266         serial->fd = fd;
267
268         return serial;
269 }
270
271 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
272 {
273         g_free(serial->port);
274 }
275
276 /**
277  * Find out if a hardware driver has a specific capability.
278  *
279  * @param driver The hardware driver in which to search for the capability.
280  * @param hwcap The capability to find in the list.
281  *
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.
285  */
286 SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
287 {
288         const int *hwcaps;
289         int i;
290
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
301         for (i = 0; hwcaps[i]; i++) {
302                 if (hwcaps[i] == hwcap)
303                         return TRUE;
304         }
305
306         return FALSE;
307 }
308
309 /**
310  * Get a hardware driver capability option.
311  *
312  * @param hwcap The capability to get.
313  *
314  * @return A pointer to a struct with information about the parameter, or NULL
315  *         if the capability was not found.
316  */
317 SR_API const struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
318 {
319         int i;
320
321         for (i = 0; sr_hwcap_options[i].hwcap; i++) {
322                 if (sr_hwcap_options[i].hwcap == hwcap)
323                         return &sr_hwcap_options[i];
324         }
325
326         return NULL;
327 }
328
329 /* Unnecessary level of indirection follows. */
330
331 SR_PRIV int sr_source_remove(int fd)
332 {
333         return sr_session_source_remove(fd);
334 }
335
336 SR_PRIV int sr_source_add(int fd, int events, int timeout,
337                           sr_receive_data_callback_t cb, void *cb_data)
338 {
339         return sr_session_source_add(fd, events, timeout, cb, cb_data);
340 }