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>
26 #include "libsigrok.h"
27 #include "libsigrok-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_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 {SR_HWCAP_MODEL, SR_T_KEYVALUE, "Model", "model"},
49 {SR_HWCAP_CONN, SR_T_CHAR, "Connection", "connect"},
50 {SR_HWCAP_SERIALCOMM, SR_T_CHAR, "Serial communication", "serialcomm"},
55 extern SR_PRIV struct sr_dev_driver demo_driver_info;
58 extern SR_PRIV struct sr_dev_driver ols_driver_info;
60 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
61 extern SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info;
63 #ifdef HAVE_LA_ASIX_SIGMA
64 extern SR_PRIV struct sr_dev_driver asix_sigma_driver_info;
66 #ifdef HAVE_LA_CHRONOVU_LA8
67 extern SR_PRIV struct sr_dev_driver chronovu_la8_driver_info;
69 #ifdef HAVE_LA_LINK_MSO19
70 extern SR_PRIV struct sr_dev_driver link_mso19_driver_info;
73 extern SR_PRIV struct sr_dev_driver alsa_driver_info;
75 #ifdef HAVE_LA_FX2LAFW
76 extern SR_PRIV struct sr_dev_driver fx2lafw_driver_info;
78 #ifdef HAVE_HW_HANTEK_DSO
79 extern SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
81 #ifdef HAVE_HW_MASTECH_VA18B
82 extern SR_PRIV struct sr_dev_driver mastech_va18b_driver_info;
84 #ifdef HAVE_HW_GENERICDMM
85 extern SR_PRIV struct sr_dev_driver genericdmm_driver_info;
88 static struct sr_dev_driver *drivers_list[] = {
95 #ifdef HAVE_LA_ZEROPLUS_LOGIC_CUBE
96 &zeroplus_logic_cube_driver_info,
98 #ifdef HAVE_LA_ASIX_SIGMA
99 &asix_sigma_driver_info,
101 #ifdef HAVE_LA_CHRONOVU_LA8
102 &chronovu_la8_driver_info,
104 #ifdef HAVE_LA_LINK_MSO19
105 &link_mso19_driver_info,
110 #ifdef HAVE_LA_FX2LAFW
111 &fx2lafw_driver_info,
113 #ifdef HAVE_HW_HANTEK_DSO
114 &hantek_dso_driver_info,
116 #ifdef HAVE_HW_MASTECH_VA18B
117 &mastech_va18b_driver_info,
119 #ifdef HAVE_HW_GENERICDMM
120 &genericdmm_driver_info,
126 * Return the list of supported hardware drivers.
128 * @return Pointer to the NULL-terminated list of hardware driver pointers.
130 SR_API struct sr_dev_driver **sr_driver_list(void)
136 * Initialize a hardware driver.
138 * The specified driver is initialized, and all devices discovered by the
139 * driver are instantiated.
141 * @param driver The driver to initialize.
143 * @return The number of devices found and instantiated by the driver.
145 SR_API int sr_driver_init(struct sr_dev_driver *driver)
147 int num_devs, num_probes, i, j;
148 int num_initialized_devs = 0;
152 sr_dbg("initializing %s driver", driver->name);
153 num_devs = driver->init(NULL);
154 for (i = 0; i < num_devs; i++) {
155 num_probes = GPOINTER_TO_INT(
156 driver->dev_info_get(i, SR_DI_NUM_PROBES));
157 probe_names = (char **)driver->dev_info_get(i,
161 sr_warn("hwdriver: %s: driver %s does not return a "
162 "list of probe names", __func__, driver->name);
166 dev = sr_dev_new(driver, i);
167 for (j = 0; j < num_probes; j++)
168 sr_dev_probe_add(dev, probe_names[j]);
169 num_initialized_devs++;
172 return num_initialized_devs;
175 SR_PRIV void sr_hw_cleanup_all(void)
178 struct sr_dev_driver **drivers;
180 drivers = sr_driver_list();
181 for (i = 0; drivers[i]; i++) {
182 if (drivers[i]->cleanup)
183 drivers[i]->cleanup();
187 SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
188 const char *vendor, const char *model, const char *version)
190 struct sr_dev_inst *sdi;
192 if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
193 sr_err("hwdriver: %s: sdi malloc failed", __func__);
198 sdi->status = status;
200 sdi->vendor = vendor ? g_strdup(vendor) : NULL;
201 sdi->model = model ? g_strdup(model) : NULL;
202 sdi->version = version ? g_strdup(version) : NULL;
208 SR_PRIV struct sr_dev_inst *sr_dev_inst_get(GSList *dev_insts, int dev_index)
210 struct sr_dev_inst *sdi;
213 for (l = dev_insts; l; l = l->next) {
214 sdi = (struct sr_dev_inst *)(l->data);
215 if (sdi->index == dev_index)
218 sr_warn("could not find device index %d instance", dev_index);
223 SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
228 g_free(sdi->version);
232 #ifdef HAVE_LIBUSB_1_0
234 SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
235 uint8_t address, struct libusb_device_handle *hdl)
237 struct sr_usb_dev_inst *udi;
239 if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
240 sr_err("hwdriver: %s: udi malloc failed", __func__);
245 udi->address = address;
251 SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
253 /* Avoid compiler warnings. */
256 /* Nothing to do for this device instance type. */
261 SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
264 struct sr_serial_dev_inst *serial;
266 if (!(serial = g_try_malloc(sizeof(struct sr_serial_dev_inst)))) {
267 sr_err("hwdriver: %s: serial malloc failed", __func__);
271 serial->port = g_strdup(port);
277 SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
279 g_free(serial->port);
283 * Find out if a hardware driver has a specific capability.
285 * @param driver The hardware driver in which to search for the capability.
286 * @param hwcap The capability to find in the list.
288 * @return TRUE if the specified capability exists in the specified driver,
289 * FALSE otherwise. Also, if 'driver' is NULL or the respective driver
290 * returns an invalid capability list, FALSE is returned.
292 SR_API gboolean sr_driver_hwcap_exists(struct sr_dev_driver *driver, int hwcap)
298 sr_err("hwdriver: %s: driver was NULL", __func__);
302 if (!(hwcaps = driver->hwcap_get_all())) {
303 sr_err("hwdriver: %s: hwcap_get_all() returned NULL", __func__);
307 for (i = 0; hwcaps[i]; i++) {
308 if (hwcaps[i] == hwcap)
316 * Get a hardware driver capability option.
318 * @param hwcap The capability to get.
320 * @return A pointer to a struct with information about the parameter, or NULL
321 * if the capability was not found.
323 SR_API const struct sr_hwcap_option *sr_hw_hwcap_get(int hwcap)
327 for (i = 0; sr_hwcap_options[i].hwcap; i++) {
328 if (sr_hwcap_options[i].hwcap == hwcap)
329 return &sr_hwcap_options[i];
335 /* Unnecessary level of indirection follows. */
337 SR_PRIV int sr_source_remove(int fd)
339 return sr_session_source_remove(fd);
342 SR_PRIV int sr_source_add(int fd, int events, int timeout,
343 sr_receive_data_callback_t cb, void *cb_data)
345 return sr_session_source_add(fd, events, timeout, cb, cb_data);