2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #include <sys/types.h>
27 #include "sigrok-internal.h"
30 * This enumerates which driver capabilities correspond to user-settable
33 /* TODO: This shouldn't be a global. */
34 SR_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",
43 {SR_HWCAP_BUFFERSIZE, SR_T_UINT64, "Buffer size", "buffersize"},
44 {SR_HWCAP_TIMEBASE, SR_T_RATIONAL, "Time base", "timebase"},
45 {SR_HWCAP_FILTER, SR_T_CHAR, "Filter targets", "filter"},
50 extern SR_PRIV struct sr_dev_driver demo_driver_info;
53 extern SR_PRIV struct sr_dev_driver ols_driver_info;
55 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
56 extern SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
58 #ifdef HAVE_LA_ASIX_SIGMA
59 extern SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
61 #ifdef HAVE_LA_CHRONOVU_LA8
62 extern SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
64 #ifdef HAVE_LA_LINK_MSO19
65 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
68 extern SR_PRIV struct sr_dev_driver alsa_driver_info;
70 #ifdef HAVE_LA_FX2LAFW
71 extern SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
73 #ifdef HAVE_HW_HANTEK_DSO
74 extern SR_PRIV struct sr_dev_driver hantek_dso_plugin_info;
77 static struct sr_dev_driver *drivers_list[] = {
84 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
85 &zeroplus_logic_cube_driver_info,
87 #ifdef HAVE_LA_ASIX_SIGMA
88 &asix_sigma_driver_info,
90 #ifdef HAVE_LA_CHRONOVU_LA8
91 &chronovu_la8_driver_info,
93 #ifdef HAVE_LA_LINK_MSO19
94 &link_mso19_driver_info,
99 #ifdef HAVE_LA_FX2LAFW
100 &fx2lafw_driver_info,
102 #ifdef HAVE_HW_HANTEK_DSO
103 &hantek_dso_plugin_info,
109 * Return the list of supported hardware drivers.
111 * @return Pointer to the NULL-terminated list of hardware driver pointers.
113 SR_API struct sr_dev_driver **sr_driver_list(void)
119 * Initialize a hardware driver.
121 * The specified driver is initialized, and all devices discovered by the
122 * driver are instantiated.
124 * @param driver The driver to initialize.
126 * @return The number of devices found and instantiated by the driver.
128 SR_API int sr_driver_init(struct sr_dev_driver *driver)
130 int num_devs, num_probes, i, j;
131 int num_initialized_devs = 0;
135 sr_dbg("initializing %s driver", driver->name);
136 num_devs = driver->init(NULL);
137 for (i = 0; i < num_devs; i++) {
138 num_probes = GPOINTER_TO_INT(
139 driver->dev_info_get(i, SR_DI_NUM_PROBES));
140 probe_names = (char **)driver->dev_info_get(i,
144 sr_warn("hwdriver: %s: driver %s does not return a "
145 "list of probe names", __func__, driver->name);
149 dev = sr_dev_new(driver, i);
150 for (j = 0; j < num_probes; j++)
151 sr_dev_probe_add(dev, probe_names[j]);
152 num_initialized_devs++;
155 return num_initialized_devs;
158 SR_PRIV void sr_hw_cleanup_all(void)
161 struct sr_dev_driver **drivers;
163 drivers = sr_driver_list();
164 for (i = 0; drivers[i]; i++) {
165 if (drivers[i]->cleanup)
166 drivers[i]->cleanup();
170 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
171 const char *vendor, const char *model, const char *version)
173 struct sr_dev_inst *sdi;
175 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
176 sr_err("hwdriver: %s: sdi malloc failed", __func__);
181 sdi->status = status;
183 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
184 sdi->model = model ? g_strdup(model) : NULL;
185 sdi->version = version ? g_strdup(version) : NULL;
191 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
193 struct sr_dev_inst *sdi;
196 for (l = dev_insts; l; l = l->next) {
197 sdi = (struct sr_dev_inst *)(l->data);
198 if (sdi->index == dev_index)
201 sr_warn("could not find device index %d instance", dev_index);
206 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
211 g_free(sdi->version);
215 #ifdef HAVE_LIBUSB_1_0
217 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
218 uint8_t address, struct libusb_device_handle *hdl)
220 struct sr_usb_dev_inst *udi;
222 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
223 sr_err("hwdriver: %s: udi malloc failed", __func__);
228 udi->address = address;
229 udi->devhdl = hdl; /* TODO: Check if this is NULL? */
234 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
236 /* Avoid compiler warnings. */
239 /* Nothing to do for this device instance type. */
244 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
247 struct sr_serial_dev_inst *serial;
249 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
250 sr_err("hwdriver: %s: serial malloc failed", __func__);
254 serial->port = g_strdup(port);
260 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
262 g_free(serial->port);
266 * Find out if a hardware driver has a specific capability.
268 * @param driver The hardware driver in which to search for the capability.
269 * @param hwcap The capability to find in the list.
271 * @return TRUE if the specified capability exists in the specified driver,
272 * FALSE otherwise. Also, if 'driver' is NULL or the respective driver
273 * returns an invalid capability list, FALSE is returned.
275 SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
280 sr_err("hwdriver: %s: driver was NULL", __func__);
284 if (!(hwcaps = driver->hwcap_get_all())) {
285 sr_err("hwdriver: %s: hwcap_get_all() returned NULL", __func__);
289 for (i = 0; hwcaps[i]; i++) {
290 if (hwcaps[i] == hwcap)
298 * Get a hardware driver capability option.
300 * @param hwcap The capability to get.
302 * @return A pointer to a struct with information about the parameter, or NULL
303 * if the capability was not found.
305 SR_API struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
309 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
310 if (sr_hwcap_options[i].hwcap == hwcap)
311 return &sr_hwcap_options[i];
317 /* Unnecessary level of indirection follows. */
319 SR_PRIV int sr_source_remove(int fd)
321 return sr_session_source_remove(fd);
324 SR_PRIV int sr_source_add(int fd, int events, int timeout,
325 sr_receive_data_callback_t cb, void *cb_data)
327 return sr_session_source_add(fd, events, timeout, cb, cb_data);