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