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