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