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