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