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 /* Note: The order here must match the DMM/device enum in protocol.h. */
28 static const char *dev_names[] = {
33 static const int hwcaps[] = {
35 SR_HWCAP_LIMIT_SAMPLES,
41 static const char *probe_names[] = {
46 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info;
47 static struct sr_dev_driver *di_ut61d = &uni_t_ut61d_driver_info;
49 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info;
50 static struct sr_dev_driver *di_vc820 = &voltcraft_vc820_driver_info;
52 static int open_usb(struct sr_dev_inst *sdi)
54 libusb_device **devlist;
55 struct libusb_device_descriptor des;
56 struct dev_context *devc;
59 /* TODO: Use common code later, refactor. */
63 if ((cnt = libusb_get_device_list(NULL, &devlist)) < 0) {
64 sr_err("Error getting USB device list: %d.", cnt);
69 for (i = 0; i < cnt; i++) {
70 if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) {
71 sr_err("Failed to get device descriptor: %d.", tmp);
75 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
76 || libusb_get_device_address(devlist[i]) != devc->usb->address)
79 if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) {
80 sr_err("Failed to open device: %d.", tmp);
84 sr_info("Opened USB device on %d.%d.",
85 devc->usb->bus, devc->usb->address);
89 libusb_free_device_list(devlist, 1);
94 static GSList *connect_usb(const char *conn, int dmm)
96 struct sr_dev_inst *sdi;
97 struct drv_context *drvc;
98 struct dev_context *devc;
99 struct sr_probe *probe;
100 libusb_device **devlist;
101 struct libusb_device_descriptor des;
103 int vid, pid, devcnt, err, i;
107 /* TODO: Use common code later, refactor. */
109 if (dmm == UNI_T_UT61D)
110 drvc = di_ut61d->priv;
111 else if (dmm == VOLTCRAFT_VC820)
112 drvc = di_vc820->priv;
114 /* Hardcoded for now. */
115 vid = UT_D04_CABLE_USB_VID;
116 pid = UT_D04_CABLE_USB_DID;
119 libusb_get_device_list(NULL, &devlist);
120 for (i = 0; devlist[i]; i++) {
121 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
122 sr_err("Failed to get device descriptor: %d", err);
126 if (des.idVendor != vid || des.idProduct != pid)
129 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
130 sr_err("Device context malloc failed.");
134 devcnt = g_slist_length(drvc->instances);
135 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
136 dev_names[dmm], NULL, NULL))) {
137 sr_err("sr_dev_inst_new returned NULL.");
141 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
143 sdi->probes = g_slist_append(sdi->probes, probe);
144 devc->usb = sr_usb_dev_inst_new(
145 libusb_get_bus_number(devlist[i]),
146 libusb_get_device_address(devlist[i]), NULL);
147 devices = g_slist_append(devices, sdi);
149 libusb_free_device_list(devlist, 1);
154 static int clear_instances(void)
156 /* TODO: Use common code later. */
161 static int hw_init(int dmm)
164 struct drv_context *drvc;
166 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
167 sr_err("Driver context malloc failed.");
168 return SR_ERR_MALLOC;
171 if ((ret = libusb_init(NULL)) < 0) {
172 sr_err("Failed to initialize libusb: %s.",
173 libusb_error_name(ret));
177 if (dmm == UNI_T_UT61D)
178 di_ut61d->priv = drvc;
179 else if (dmm == VOLTCRAFT_VC820)
180 di_vc820->priv = drvc;
185 static int hw_init_ut61d(void)
187 return hw_init(UNI_T_UT61D);
190 static int hw_init_vc820(void)
192 return hw_init(VOLTCRAFT_VC820);
195 static GSList *hw_scan(GSList *options, int dmm)
198 struct sr_dev_inst *sdi;
199 struct drv_context *drvc;
203 if (dmm == UNI_T_UT61D)
204 drvc = di_ut61d->priv;
205 else if (dmm == VOLTCRAFT_VC820)
206 drvc = di_vc820->priv;
208 if (!(devices = connect_usb(NULL, dmm)))
211 for (l = devices; l; l = l->next) {
214 if (dmm == UNI_T_UT61D)
215 sdi->driver = di_ut61d;
216 else if (dmm == VOLTCRAFT_VC820)
217 sdi->driver = di_vc820;
219 drvc->instances = g_slist_append(drvc->instances, l->data);
225 static GSList *hw_scan_ut61d(GSList *options)
227 return hw_scan(options, UNI_T_UT61D);
230 static GSList *hw_scan_vc820(GSList *options)
232 return hw_scan(options, VOLTCRAFT_VC820);
235 static GSList *hw_dev_list(int dmm)
237 struct drv_context *drvc;
239 if (dmm == UNI_T_UT61D)
240 drvc = di_ut61d->priv;
241 else if (dmm == VOLTCRAFT_VC820)
242 drvc = di_vc820->priv;
244 return drvc->instances;
247 static GSList *hw_dev_list_ut61d(void)
249 return hw_dev_list(UNI_T_UT61D);
252 static GSList *hw_dev_list_vc820(void)
254 return hw_dev_list(VOLTCRAFT_VC820);
257 static int hw_dev_open(struct sr_dev_inst *sdi)
259 return open_usb(sdi);
262 static int hw_dev_close(struct sr_dev_inst *sdi)
271 static int hw_cleanup(void)
275 // libusb_exit(NULL);
280 static int hw_info_get(int info_id, const void **data,
281 const struct sr_dev_inst *sdi)
285 sr_spew("Backend requested info_id %d.", info_id);
290 sr_spew("%s: Returning hwcaps.", __func__);
292 case SR_DI_NUM_PROBES:
293 *data = GINT_TO_POINTER(1);
294 sr_spew("%s: Returning number of probes.", __func__);
296 case SR_DI_PROBE_NAMES:
298 sr_spew("%s: Returning probe names.", __func__);
300 case SR_DI_SAMPLERATES:
301 /* TODO: Get rid of this. */
303 sr_spew("%s: Returning samplerates.", __func__);
306 case SR_DI_CUR_SAMPLERATE:
307 /* TODO: Get rid of this. */
309 sr_spew("%s: Returning current samplerate.", __func__);
313 sr_err("%s: Unknown info_id %d.", __func__, info_id);
321 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
324 struct dev_context *devc;
329 case SR_HWCAP_LIMIT_MSEC:
330 /* TODO: Not yet implemented. */
331 if (*(const uint64_t *)value == 0) {
332 sr_err("Time limit cannot be 0.");
335 devc->limit_msec = *(const uint64_t *)value;
336 sr_dbg("Setting time limit to %" PRIu64 "ms.",
339 case SR_HWCAP_LIMIT_SAMPLES:
340 if (*(const uint64_t *)value == 0) {
341 sr_err("Sample limit cannot be 0.");
344 devc->limit_samples = *(const uint64_t *)value;
345 sr_dbg("Setting sample limit to %" PRIu64 ".",
346 devc->limit_samples);
349 sr_err("Unknown capability: %d.", hwcap);
357 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
358 int dmm, void *cb_data)
360 struct sr_datafeed_packet packet;
361 struct sr_datafeed_header header;
362 struct sr_datafeed_meta_analog meta;
363 struct dev_context *devc;
367 sr_dbg("Starting acquisition.");
369 devc->cb_data = cb_data;
371 /* Send header packet to the session bus. */
372 sr_dbg("Sending SR_DF_HEADER.");
373 packet.type = SR_DF_HEADER;
374 packet.payload = (uint8_t *)&header;
375 header.feed_version = 1;
376 gettimeofday(&header.starttime, NULL);
377 sr_session_send(devc->cb_data, &packet);
379 /* Send metadata about the SR_DF_ANALOG packets to come. */
380 sr_dbg("Sending SR_DF_META_ANALOG.");
381 packet.type = SR_DF_META_ANALOG;
382 packet.payload = &meta;
384 sr_session_send(devc->cb_data, &packet);
386 if (dmm == UNI_T_UT61D) {
387 sr_source_add(0, 0, 10 /* poll_timeout */,
388 uni_t_ut61d_receive_data, (void *)sdi);
389 } else if (dmm == VOLTCRAFT_VC820) {
390 sr_source_add(0, 0, 10 /* poll_timeout */,
391 voltcraft_vc820_receive_data, (void *)sdi);
397 static int hw_dev_acquisition_start_ut61d(const struct sr_dev_inst *sdi,
400 return hw_dev_acquisition_start(sdi, UNI_T_UT61D, cb_data);
403 static int hw_dev_acquisition_start_vc820(const struct sr_dev_inst *sdi,
406 return hw_dev_acquisition_start(sdi, VOLTCRAFT_VC820, cb_data);
409 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
412 struct sr_datafeed_packet packet;
416 sr_dbg("Stopping acquisition.");
418 /* Send end packet to the session bus. */
419 sr_dbg("Sending SR_DF_END.");
420 packet.type = SR_DF_END;
421 sr_session_send(cb_data, &packet);
429 SR_PRIV struct sr_dev_driver uni_t_ut61d_driver_info = {
430 .name = "uni-t-ut61d",
431 .longname = "UNI-T UT61D",
433 .init = hw_init_ut61d,
434 .cleanup = hw_cleanup,
435 .scan = hw_scan_ut61d,
436 .dev_list = hw_dev_list_ut61d,
437 .dev_clear = clear_instances,
438 .dev_open = hw_dev_open,
439 .dev_close = hw_dev_close,
440 .info_get = hw_info_get,
441 .dev_config_set = hw_dev_config_set,
442 .dev_acquisition_start = hw_dev_acquisition_start_ut61d,
443 .dev_acquisition_stop = hw_dev_acquisition_stop,
447 SR_PRIV struct sr_dev_driver voltcraft_vc820_driver_info = {
448 .name = "voltcraft-vc820",
449 .longname = "Voltcraft VC-820",
451 .init = hw_init_vc820,
452 .cleanup = hw_cleanup,
453 .scan = hw_scan_vc820,
454 .dev_list = hw_dev_list_vc820,
455 .dev_clear = clear_instances,
456 .dev_open = hw_dev_open,
457 .dev_close = hw_dev_close,
458 .info_get = hw_info_get,
459 .dev_config_set = hw_dev_config_set,
460 .dev_acquisition_start = hw_dev_acquisition_start_vc820,
461 .dev_acquisition_stop = hw_dev_acquisition_stop,