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