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