]> sigrok.org Git - libsigrok.git/blob - hwdriver.c
return SR_ERR_ARG if a driver doesn't implement sr_config_*()
[libsigrok.git] / hwdriver.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2013 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 "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
29
30 /* Message logging helpers with driver-specific prefix string. */
31 #define DRIVER_LOG_DOMAIN "hwdriver: "
32 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
36 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
37 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
38
39 /**
40  * @file
41  *
42  * Hardware driver handling in libsigrok.
43  */
44
45 /**
46  * @defgroup grp_driver Hardware drivers
47  *
48  * Hardware driver handling in libsigrok.
49  *
50  * @{
51  */
52
53 static struct sr_config_info sr_config_info_data[] = {
54         {SR_CONF_CONN, SR_T_CHAR, "conn",
55                 "Connection", NULL},
56         {SR_CONF_SERIALCOMM, SR_T_CHAR, "serialcomm",
57                 "Serial communication", NULL},
58         {SR_CONF_SAMPLERATE, SR_T_UINT64, "samplerate",
59                 "Sample rate", NULL},
60         {SR_CONF_CAPTURE_RATIO, SR_T_UINT64, "captureratio",
61                 "Pre-trigger capture ratio", NULL},
62         {SR_CONF_PATTERN_MODE, SR_T_CHAR, "pattern",
63                 "Pattern generator mode", NULL},
64         {SR_CONF_TRIGGER_TYPE, SR_T_CHAR, "triggertype",
65                 "Trigger types", NULL},
66         {SR_CONF_RLE, SR_T_BOOL, "rle",
67                 "Run Length Encoding", NULL},
68         {SR_CONF_TRIGGER_SLOPE, SR_T_UINT64, "triggerslope",
69                 "Trigger slope", NULL},
70         {SR_CONF_TRIGGER_SOURCE, SR_T_CHAR, "triggersource",
71                 "Trigger source", NULL},
72         {SR_CONF_HORIZ_TRIGGERPOS, SR_T_FLOAT, "horiz_triggerpos",
73                 "Horizontal trigger position", NULL},
74         {SR_CONF_BUFFERSIZE, SR_T_UINT64, "buffersize",
75                 "Buffer size", NULL},
76         {SR_CONF_TIMEBASE, SR_T_RATIONAL_PERIOD, "timebase",
77                 "Time base", NULL},
78         {SR_CONF_FILTER, SR_T_CHAR, "filter",
79                 "Filter targets", NULL},
80         {SR_CONF_VDIV, SR_T_RATIONAL_VOLT, "vdiv",
81                 "Volts/div", NULL},
82         {SR_CONF_COUPLING, SR_T_CHAR, "coupling",
83                 "Coupling", NULL},
84         {SR_CONF_DATALOG, SR_T_BOOL, "datalog",
85                 "Datalog", NULL},
86         {0, 0, NULL, NULL, NULL},
87 };
88
89 /** @cond PRIVATE */
90 #ifdef HAVE_HW_BRYMEN_DMM
91 extern SR_PRIV struct sr_dev_driver brymen_bm857_driver_info;
92 #endif
93 #ifdef HAVE_HW_COLEAD_SLM
94 extern SR_PRIV struct sr_dev_driver colead_slm_driver_info;
95 #endif
96 #ifdef HAVE_LA_DEMO
97 extern SR_PRIV struct sr_dev_driver demo_driver_info;
98 #endif
99 #ifdef HAVE_HW_LASCAR_EL_USB
100 extern SR_PRIV struct sr_dev_driver lascar_el_usb_driver_info;
101 #endif
102 #ifdef HAVE_HW_MIC_985XX
103 extern SR_PRIV struct sr_dev_driver mic_98581_driver_info;
104 extern SR_PRIV struct sr_dev_driver mic_98583_driver_info;
105 #endif
106 #ifdef HAVE_HW_NEXUS_OSCIPRIME
107 extern SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info;
108 #endif
109 #ifdef HAVE_LA_OLS
110 extern SR_PRIV struct sr_dev_driver ols_driver_info;
111 #endif
112 #ifdef HAVE_HW_RIGOL_DS1XX2
113 extern SR_PRIV struct sr_dev_driver rigol_ds1xx2_driver_info;
114 #endif
115 #ifdef HAVE_HW_TONDAJ_SL_814
116 extern SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info;
117 #endif
118 #ifdef HAVE_HW_VICTOR_DMM
119 extern SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
120 #endif
121 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
122 extern SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
123 #endif
124 #ifdef HAVE_LA_ASIX_SIGMA
125 extern SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
126 #endif
127 #ifdef HAVE_LA_CHRONOVU_LA8
128 extern SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
129 #endif
130 #ifdef HAVE_LA_LINK_MSO19
131 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
132 #endif
133 #ifdef HAVE_HW_ALSA
134 extern SR_PRIV struct sr_dev_driver alsa_driver_info;
135 #endif
136 #ifdef HAVE_LA_FX2LAFW
137 extern SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
138 #endif
139 #ifdef HAVE_HW_HANTEK_DSO
140 extern SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
141 #endif
142 #ifdef HAVE_HW_AGILENT_DMM
143 extern SR_PRIV struct sr_dev_driver agdmm_driver_info;
144 #endif
145 #ifdef HAVE_HW_FLUKE_DMM
146 extern SR_PRIV struct sr_dev_driver flukedmm_driver_info;
147 #endif
148 #ifdef HAVE_HW_SERIAL_DMM
149 extern SR_PRIV struct sr_dev_driver digitek_dt4000zc_driver_info;
150 extern SR_PRIV struct sr_dev_driver tekpower_tp4000zc_driver_info;
151 extern SR_PRIV struct sr_dev_driver metex_me31_driver_info;
152 extern SR_PRIV struct sr_dev_driver peaktech_3410_driver_info;
153 extern SR_PRIV struct sr_dev_driver mastech_mas345_driver_info;
154 extern SR_PRIV struct sr_dev_driver va_va18b_driver_info;
155 extern SR_PRIV struct sr_dev_driver metex_m3640d_driver_info;
156 extern SR_PRIV struct sr_dev_driver peaktech_4370_driver_info;
157 extern SR_PRIV struct sr_dev_driver pce_pce_dm32_driver_info;
158 extern SR_PRIV struct sr_dev_driver radioshack_22_168_driver_info;
159 extern SR_PRIV struct sr_dev_driver radioshack_22_805_driver_info;
160 extern SR_PRIV struct sr_dev_driver radioshack_22_812_driver_info;
161 extern SR_PRIV struct sr_dev_driver voltcraft_vc820_ser_driver_info;
162 extern SR_PRIV struct sr_dev_driver voltcraft_vc840_ser_driver_info;
163 extern SR_PRIV struct sr_dev_driver uni_t_ut61e_ser_driver_info;
164 #endif
165 #ifdef HAVE_HW_UNI_T_DMM
166 extern SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
167 extern SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
168 #endif
169 /** @endcond */
170
171 static struct sr_dev_driver *drivers_list[] = {
172 #ifdef HAVE_HW_BRYMEN_DMM
173         &brymen_bm857_driver_info,
174 #endif
175 #ifdef HAVE_HW_COLEAD_SLM
176         &colead_slm_driver_info,
177 #endif
178 #ifdef HAVE_LA_DEMO
179         &demo_driver_info,
180 #endif
181 #ifdef HAVE_HW_LASCAR_EL_USB
182         &lascar_el_usb_driver_info,
183 #endif
184 #ifdef HAVE_HW_MIC_985XX
185         &mic_98581_driver_info,
186         &mic_98583_driver_info,
187 #endif
188 #ifdef HAVE_HW_NEXUS_OSCIPRIME
189         &nexus_osciprime_driver_info,
190 #endif
191 #ifdef HAVE_LA_OLS
192         &ols_driver_info,
193 #endif
194 #ifdef HAVE_HW_RIGOL_DS1XX2
195         &rigol_ds1xx2_driver_info,
196 #endif
197 #ifdef HAVE_HW_TONDAJ_SL_814
198         &tondaj_sl_814_driver_info,
199 #endif
200 #ifdef HAVE_HW_VICTOR_DMM
201         &victor_dmm_driver_info,
202 #endif
203 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
204         &zeroplus_logic_cube_driver_info,
205 #endif
206 #ifdef HAVE_LA_ASIX_SIGMA
207         &asix_sigma_driver_info,
208 #endif
209 #ifdef HAVE_LA_CHRONOVU_LA8
210         &chronovu_la8_driver_info,
211 #endif
212 #ifdef HAVE_LA_LINK_MSO19
213         &link_mso19_driver_info,
214 #endif
215 #ifdef HAVE_HW_ALSA
216         &alsa_driver_info,
217 #endif
218 #ifdef HAVE_LA_FX2LAFW
219         &fx2lafw_driver_info,
220 #endif
221 #ifdef HAVE_HW_HANTEK_DSO
222         &hantek_dso_driver_info,
223 #endif
224 #ifdef HAVE_HW_AGILENT_DMM
225         &agdmm_driver_info,
226 #endif
227 #ifdef HAVE_HW_FLUKE_DMM
228         &flukedmm_driver_info,
229 #endif
230 #ifdef HAVE_HW_SERIAL_DMM
231         &digitek_dt4000zc_driver_info,
232         &tekpower_tp4000zc_driver_info,
233         &metex_me31_driver_info,
234         &peaktech_3410_driver_info,
235         &mastech_mas345_driver_info,
236         &va_va18b_driver_info,
237         &metex_m3640d_driver_info,
238         &peaktech_4370_driver_info,
239         &pce_pce_dm32_driver_info,
240         &radioshack_22_168_driver_info,
241         &radioshack_22_805_driver_info,
242         &radioshack_22_812_driver_info,
243         &voltcraft_vc820_ser_driver_info,
244         &voltcraft_vc840_ser_driver_info,
245         &uni_t_ut61e_ser_driver_info,
246 #endif
247 #ifdef HAVE_HW_UNI_T_DMM
248         &uni_t_ut61d_driver_info,
249         &voltcraft_vc820_driver_info,
250 #endif
251         NULL,
252 };
253
254 /**
255  * Return the list of supported hardware drivers.
256  *
257  * @return Pointer to the NULL-terminated list of hardware driver pointers.
258  */
259 SR_API struct sr_dev_driver **sr_driver_list(void)
260 {
261
262         return drivers_list;
263 }
264
265 /**
266  * Initialize a hardware driver.
267  *
268  * This usually involves memory allocations and variable initializations
269  * within the driver, but _not_ scanning for attached devices.
270  * The API call sr_driver_scan() is used for that.
271  *
272  * @param ctx A libsigrok context object allocated by a previous call to
273  *            sr_init(). Must not be NULL.
274  * @param driver The driver to initialize. This must be a pointer to one of
275  *               the entries returned by sr_driver_list(). Must not be NULL.
276  *
277  * @return SR_OK upon success, SR_ERR_ARG upon invalid parameters,
278  *         SR_ERR_BUG upon internal errors, or another negative error code
279  *         upon other errors.
280  */
281 SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver)
282 {
283         int ret;
284
285         if (!ctx) {
286                 sr_err("Invalid libsigrok context, can't initialize.");
287                 return SR_ERR_ARG;
288         }
289
290         if (!driver) {
291                 sr_err("Invalid driver, can't initialize.");
292                 return SR_ERR_ARG;
293         }
294
295         sr_spew("Initializing driver '%s'.", driver->name);
296         if ((ret = driver->init(ctx)) < 0)
297                 sr_err("Failed to initialize the driver: %d.", ret);
298
299         return ret;
300 }
301
302 /**
303  * Tell a hardware driver to scan for devices.
304  *
305  * In addition to the detection, the devices that are found are also
306  * initialized automatically. On some devices, this involves a firmware upload,
307  * or other such measures.
308  *
309  * The order in which the system is scanned for devices is not specified. The
310  * caller should not assume or rely on any specific order.
311  *
312  * Before calling sr_driver_scan(), the user must have previously initialized
313  * the driver by calling sr_driver_init().
314  *
315  * @param driver The driver that should scan. This must be a pointer to one of
316  *               the entries returned by sr_driver_list(). Must not be NULL.
317  * @param options A list of 'struct sr_hwopt' options to pass to the driver's
318  *                scanner. Can be NULL/empty.
319  *
320  * @return A GSList * of 'struct sr_dev_inst', or NULL if no devices were
321  *         found (or errors were encountered). This list must be freed by the
322  *         caller using g_slist_free(), but without freeing the data pointed
323  *         to in the list.
324  */
325 SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options)
326 {
327         GSList *l;
328
329         if (!driver) {
330                 sr_err("Invalid driver, can't scan for devices.");
331                 return NULL;
332         }
333
334         if (!driver->priv) {
335                 sr_err("Driver not initialized, can't scan for devices.");
336                 return NULL;
337         }
338
339         l = driver->scan(options);
340
341         sr_spew("Scan of '%s' found %d devices.", driver->name,
342                 g_slist_length(l));
343
344         return l;
345 }
346
347 /** @private */
348 SR_PRIV void sr_hw_cleanup_all(void)
349 {
350         int i;
351         struct sr_dev_driver **drivers;
352
353         drivers = sr_driver_list();
354         for (i = 0; drivers[i]; i++) {
355                 if (drivers[i]->cleanup)
356                         drivers[i]->cleanup();
357         }
358 }
359
360 /** A floating reference can be passed in for data. */
361 SR_PRIV struct sr_config *sr_config_new(int key, GVariant *data)
362 {
363         struct sr_config *src;
364
365         if (!(src = g_try_malloc(sizeof(struct sr_config))))
366                 return NULL;
367         src->key = key;
368         src->data = g_variant_ref_sink(data);
369
370         return src;
371 }
372
373 SR_PRIV void sr_config_free(struct sr_config *src)
374 {
375
376         if (!src || !src->data) {
377                 sr_err("%s: invalid data!", __func__);
378                 return;
379         }
380
381         g_variant_unref(src->data);
382         g_free(src);
383
384 }
385
386 /**
387  * Returns information about the given driver or device instance.
388  *
389  * @param driver The sr_dev_driver struct to query.
390  * @param key The configuration key (SR_CONF_*).
391  * @param data Pointer to a GVariant where the value will be stored. Must
392  *             not be NULL. The caller is given ownership of the GVariant
393  *             and must thus decrease the refcount after use. However if
394  *             this function returns an error code, the field should be
395  *             considered unused, and should not be unreferenced.
396  * @param sdi (optional) If the key is specific to a device, this must
397  *            contain a pointer to the struct sr_dev_inst to be checked.
398  *            Otherwise it must be NULL.
399  *
400  * @return SR_OK upon success or SR_ERR in case of error. Note SR_ERR_ARG
401  *         may be returned by the driver indicating it doesn't know that key,
402  *         but this is not to be flagged as an error by the caller; merely
403  *         as an indication that it's not applicable.
404  */
405 SR_API int sr_config_get(const struct sr_dev_driver *driver, int key,
406                 GVariant **data, const struct sr_dev_inst *sdi)
407 {
408         int ret;
409
410         if (!driver || !data)
411                 return SR_ERR;
412
413         if (!driver->config_get)
414                 return SR_ERR_ARG;
415
416         if ((ret = driver->config_get(key, data, sdi)) == SR_OK) {
417                 /* Got a floating reference from the driver. Sink it here,
418                  * caller will need to unref when done with it. */
419                 g_variant_ref_sink(*data);
420         }
421
422         return ret;
423 }
424
425 /**
426  * Set a configuration key in a device instance.
427  *
428  * @param sdi The device instance.
429  * @param key The configuration key (SR_CONF_*).
430  * @param data The new value for the key, as a GVariant with GVariantType
431  *        appropriate to that key. A floating reference can be passed
432  *        in; its refcount will be sunk and unreferenced after use.
433  *
434  * @return SR_OK upon success or SR_ERR in case of error. Note SR_ERR_ARG
435  *         may be returned by the driver indicating it doesn't know that key,
436  *         but this is not to be flagged as an error by the caller; merely
437  *         as an indication that it's not applicable.
438  */
439 SR_API int sr_config_set(const struct sr_dev_inst *sdi, int key, GVariant *data)
440 {
441         int ret;
442
443         g_variant_ref_sink(data);
444
445         if (!sdi || !sdi->driver || !data)
446                 ret = SR_ERR;
447         else if (!sdi->driver->config_set)
448                 ret = SR_ERR_ARG;
449         else
450                 ret = sdi->driver->config_set(key, data, sdi);
451
452         g_variant_unref(data);
453
454         return ret;
455 }
456
457 /**
458  * List all possible values for a configuration key.
459  *
460  * @param driver The sr_dev_driver struct to query.
461  * @param key The configuration key (SR_CONF_*).
462  * @param data A pointer to a GVariant where the list will be stored. The
463  *             caller is given ownership of the GVariant and must thus
464  *             unref the GVariant after use. However if this function
465  *             returns an error code, the field should be considered
466  *             unused, and should not be unreferenced.
467  * @param sdi (optional) If the key is specific to a device, this must
468  *            contain a pointer to the struct sr_dev_inst to be checked.
469  *
470  * @return SR_OK upon success or SR_ERR in case of error. Note SR_ERR_ARG
471  *         may be returned by the driver indicating it doesn't know that key,
472  *         but this is not to be flagged as an error by the caller; merely
473  *         as an indication that it's not applicable.
474  */
475 SR_API int sr_config_list(const struct sr_dev_driver *driver, int key,
476                 GVariant **data, const struct sr_dev_inst *sdi)
477 {
478         int ret;
479
480         if (!driver || !data)
481                 ret = SR_ERR;
482         else if (!sdi->driver->config_list)
483                 ret = SR_ERR_ARG;
484         else if ((ret = driver->config_list(key, data, sdi)) == SR_OK)
485                 g_variant_ref_sink(*data);
486
487         return ret;
488 }
489
490 /**
491  * Get information about a configuration key.
492  *
493  * @param key The configuration key.
494  *
495  * @return A pointer to a struct sr_config_info, or NULL if the key
496  *         was not found.
497  */
498 SR_API const struct sr_config_info *sr_config_info_get(int key)
499 {
500         int i;
501
502         for (i = 0; sr_config_info_data[i].key; i++) {
503                 if (sr_config_info_data[i].key == key)
504                         return &sr_config_info_data[i];
505         }
506
507         return NULL;
508 }
509
510 /**
511  * Get information about an configuration key, by name.
512  *
513  * @param optname The configuration key.
514  *
515  * @return A pointer to a struct sr_config_info, or NULL if the key
516  *         was not found.
517  */
518 SR_API const struct sr_config_info *sr_config_info_name_get(const char *optname)
519 {
520         int i;
521
522         for (i = 0; sr_config_info_data[i].key; i++) {
523                 if (!strcmp(sr_config_info_data[i].id, optname))
524                         return &sr_config_info_data[i];
525         }
526
527         return NULL;
528 }
529
530 /* Unnecessary level of indirection follows. */
531
532 /** @private */
533 SR_PRIV int sr_source_remove(int fd)
534 {
535         return sr_session_source_remove(fd);
536 }
537
538 /** @private */
539 SR_PRIV int sr_source_add(int fd, int events, int timeout,
540                           sr_receive_data_callback_t cb, void *cb_data)
541 {
542         return sr_session_source_add(fd, events, timeout, cb, cb_data);
543 }
544
545 /** @} */