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