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