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