2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
27 static const int hwcaps[] = {
29 SR_HWCAP_LIMIT_SAMPLES,
35 static const char *probe_names[] = {
40 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
41 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
43 static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
44 static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
46 /* After hw_init() this will point to a device-specific entry (see above). */
47 static struct sr_dev_driver *di = NULL;
49 static int open_usb(struct sr_dev_inst *sdi)
51 libusb_device **devlist;
52 struct libusb_device_descriptor des;
53 struct dev_context *devc;
56 /* TODO: Use common code later, refactor. */
60 if ((cnt = libusb_get_device_list(NULL, &devlist)) < 0) {
61 sr_err("Error getting USB device list: %d.", cnt);
66 for (i = 0; i < cnt; i++) {
67 if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) {
68 sr_err("Failed to get device descriptor: %d.", tmp);
72 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
73 || libusb_get_device_address(devlist[i]) != devc->usb->address)
76 if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) {
77 sr_err("Failed to open device: %d.", tmp);
81 sr_info("Opened USB device on %d.%d.",
82 devc->usb->bus, devc->usb->address);
86 libusb_free_device_list(devlist, 1);
91 static GSList *connect_usb(const char *conn)
93 struct sr_dev_inst *sdi;
94 struct drv_context *drvc;
95 struct dev_context *devc;
96 struct sr_probe *probe;
97 libusb_device **devlist;
98 struct libusb_device_descriptor des;
100 int vid, pid, devcnt, err, i;
104 /* TODO: Use common code later, refactor. */
108 /* Hardcoded for now. */
109 vid = UT_D04_CABLE_USB_VID;
110 pid = UT_D04_CABLE_USB_DID;
113 libusb_get_device_list(NULL, &devlist);
114 for (i = 0; devlist[i]; i++) {
115 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
116 sr_err("Failed to get device descriptor: %d", err);
120 if (des.idVendor != vid || des.idProduct != pid)
123 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
124 sr_err("Device context malloc failed.");
128 devcnt = g_slist_length(drvc->instances);
129 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
130 di->longname, NULL, NULL))) {
131 sr_err("sr_dev_inst_new returned NULL.");
135 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
137 sdi->probes = g_slist_append(sdi->probes, probe);
138 devc->usb = sr_usb_dev_inst_new(
139 libusb_get_bus_number(devlist[i]),
140 libusb_get_device_address(devlist[i]), NULL);
141 devices = g_slist_append(devices, sdi);
143 libusb_free_device_list(devlist, 1);
148 static int clear_instances(void)
150 /* TODO: Use common code later. */
155 static int hw_init(int dmm)
158 struct drv_context *drvc;
160 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
161 sr_err("Driver context malloc failed.");
162 return SR_ERR_MALLOC;
165 if ((ret = libusb_init(NULL)) < 0) {
166 sr_err("Failed to initialize libusb: %s.",
167 libusb_error_name(ret));
171 if (dmm == UNI_T_UT61D)
173 else if (dmm == VOLTCRAFT_VC820)
175 sr_dbg("Selected '%s' subdriver.", di->name);
182 static int hw_init_ut61d(void)
184 return hw_init(UNI_T_UT61D);
187 static int hw_init_vc820(void)
189 return hw_init(VOLTCRAFT_VC820);
192 static GSList *hw_scan(GSList *options)
195 struct sr_dev_inst *sdi;
196 struct drv_context *drvc;
202 if (!(devices = connect_usb(NULL)))
205 for (l = devices; l; l = l->next) {
208 drvc->instances = g_slist_append(drvc->instances, l->data);
214 static GSList *hw_dev_list(void)
216 struct drv_context *drvc;
220 return drvc->instances;
223 static int hw_dev_open(struct sr_dev_inst *sdi)
225 return open_usb(sdi);
228 static int hw_dev_close(struct sr_dev_inst *sdi)
237 static int hw_cleanup(void)
241 // libusb_exit(NULL);
246 static int hw_info_get(int info_id, const void **data,
247 const struct sr_dev_inst *sdi)
251 sr_spew("Backend requested info_id %d.", info_id);
256 sr_spew("%s: Returning hwcaps.", __func__);
258 case SR_DI_NUM_PROBES:
259 *data = GINT_TO_POINTER(1);
260 sr_spew("%s: Returning number of probes.", __func__);
262 case SR_DI_PROBE_NAMES:
264 sr_spew("%s: Returning probe names.", __func__);
266 case SR_DI_SAMPLERATES:
267 /* TODO: Get rid of this. */
269 sr_spew("%s: Returning samplerates.", __func__);
272 case SR_DI_CUR_SAMPLERATE:
273 /* TODO: Get rid of this. */
275 sr_spew("%s: Returning current samplerate.", __func__);
279 sr_err("%s: Unknown info_id %d.", __func__, info_id);
287 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
290 struct dev_context *devc;
295 case SR_HWCAP_LIMIT_MSEC:
296 /* TODO: Not yet implemented. */
297 if (*(const uint64_t *)value == 0) {
298 sr_err("Time limit cannot be 0.");
301 devc->limit_msec = *(const uint64_t *)value;
302 sr_dbg("Setting time limit to %" PRIu64 "ms.",
305 case SR_HWCAP_LIMIT_SAMPLES:
306 if (*(const uint64_t *)value == 0) {
307 sr_err("Sample limit cannot be 0.");
310 devc->limit_samples = *(const uint64_t *)value;
311 sr_dbg("Setting sample limit to %" PRIu64 ".",
312 devc->limit_samples);
315 sr_err("Unknown capability: %d.", hwcap);
323 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
326 struct sr_datafeed_packet packet;
327 struct sr_datafeed_header header;
328 struct sr_datafeed_meta_analog meta;
329 struct dev_context *devc;
333 sr_dbg("Starting acquisition.");
335 devc->cb_data = cb_data;
337 /* Send header packet to the session bus. */
338 sr_dbg("Sending SR_DF_HEADER.");
339 packet.type = SR_DF_HEADER;
340 packet.payload = (uint8_t *)&header;
341 header.feed_version = 1;
342 gettimeofday(&header.starttime, NULL);
343 sr_session_send(devc->cb_data, &packet);
345 /* Send metadata about the SR_DF_ANALOG packets to come. */
346 sr_dbg("Sending SR_DF_META_ANALOG.");
347 packet.type = SR_DF_META_ANALOG;
348 packet.payload = &meta;
350 sr_session_send(devc->cb_data, &packet);
352 if (!strcmp(di->name, "uni-t-ut61d")) {
353 sr_source_add(0, 0, 10 /* poll_timeout */,
354 uni_t_ut61d_receive_data, (void *)sdi);
355 } else if (!strcmp(di->name, "voltcraft-vc820")) {
356 sr_source_add(0, 0, 10 /* poll_timeout */,
357 voltcraft_vc820_receive_data, (void *)sdi);
363 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
366 struct sr_datafeed_packet packet;
370 sr_dbg("Stopping acquisition.");
372 /* Send end packet to the session bus. */
373 sr_dbg("Sending SR_DF_END.");
374 packet.type = SR_DF_END;
375 sr_session_send(cb_data, &packet);
383 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
384 .name = "uni-t-ut61d",
385 .longname = "UNI-T UT61D",
387 .init = hw_init_ut61d,
388 .cleanup = hw_cleanup,
390 .dev_list = hw_dev_list,
391 .dev_clear = clear_instances,
392 .dev_open = hw_dev_open,
393 .dev_close = hw_dev_close,
394 .info_get = hw_info_get,
395 .dev_config_set = hw_dev_config_set,
396 .dev_acquisition_start = hw_dev_acquisition_start,
397 .dev_acquisition_stop = hw_dev_acquisition_stop,
401 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
402 .name = "voltcraft-vc820",
403 .longname = "Voltcraft VC-820",
405 .init = hw_init_vc820,
406 .cleanup = hw_cleanup,
408 .dev_list = hw_dev_list,
409 .dev_clear = clear_instances,
410 .dev_open = hw_dev_open,
411 .dev_close = hw_dev_close,
412 .info_get = hw_info_get,
413 .dev_config_set = hw_dev_config_set,
414 .dev_acquisition_start = hw_dev_acquisition_start,
415 .dev_acquisition_stop = hw_dev_acquisition_stop,