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