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