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