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_dmm_driver_info;
41 static struct sr_dev_driver *di = &uni_t_dmm_driver_info;
43 static int open_usb(struct sr_dev_inst *sdi)
45 libusb_device **devlist;
46 struct libusb_device_descriptor des;
47 struct dev_context *devc;
50 /* TODO: Use common code later, refactor. */
54 if ((cnt = libusb_get_device_list(NULL, &devlist)) < 0) {
55 sr_err("Error getting USB device list: %d.", cnt);
60 for (i = 0; i < cnt; i++) {
61 if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) {
62 sr_err("Failed to get device descriptor: %d.", tmp);
66 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
67 || libusb_get_device_address(devlist[i]) != devc->usb->address)
70 if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) {
71 sr_err("Failed to open device: %d.", tmp);
75 sr_info("Opened USB device on %d.%d.",
76 devc->usb->bus, devc->usb->address);
80 libusb_free_device_list(devlist, 1);
85 static GSList *connect_usb(const char *conn)
87 struct sr_dev_inst *sdi;
88 struct drv_context *drvc;
89 struct dev_context *devc;
90 struct sr_probe *probe;
91 libusb_device **devlist;
92 struct libusb_device_descriptor des;
94 int vid, pid, devcnt, err, i;
98 /* TODO: Use common code later, refactor. */
102 /* Hardcoded for now. */
103 vid = UT_D04_CABLE_USB_VID;
104 pid = UT_D04_CABLE_USB_DID;
107 libusb_get_device_list(NULL, &devlist);
108 for (i = 0; devlist[i]; i++) {
109 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
110 sr_err("Failed to get device descriptor: %d", err);
114 if (des.idVendor != vid || des.idProduct != pid)
117 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
118 sr_err("Device context malloc failed.");
122 devcnt = g_slist_length(drvc->instances);
123 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
124 "UNI-T DMM", NULL, NULL))) {
125 sr_err("sr_dev_inst_new returned NULL.");
129 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
131 sdi->probes = g_slist_append(sdi->probes, probe);
132 devc->usb = sr_usb_dev_inst_new(
133 libusb_get_bus_number(devlist[i]),
134 libusb_get_device_address(devlist[i]), NULL);
135 devices = g_slist_append(devices, sdi);
137 libusb_free_device_list(devlist, 1);
142 static int clear_instances(void)
144 /* TODO: Use common code later. */
149 static int hw_init(void)
152 struct drv_context *drvc;
154 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
155 sr_err("Driver context malloc failed.");
156 return SR_ERR_MALLOC;
159 if ((ret = libusb_init(NULL)) < 0) {
160 sr_err("Failed to initialize libusb: %s.",
161 libusb_error_name(ret));
170 static GSList *hw_scan(GSList *options)
173 struct sr_dev_inst *sdi;
174 struct drv_context *drvc;
180 if (!(devices = connect_usb(NULL)))
183 for (l = devices; l; l = l->next) {
186 drvc->instances = g_slist_append(drvc->instances, l->data);
192 static GSList *hw_dev_list(void)
194 struct drv_context *drvc;
198 return drvc->instances;
201 static int hw_dev_open(struct sr_dev_inst *sdi)
203 return open_usb(sdi);
206 static int hw_dev_close(struct sr_dev_inst *sdi)
215 static int hw_cleanup(void)
219 // libusb_exit(NULL);
224 static int hw_info_get(int info_id, const void **data,
225 const struct sr_dev_inst *sdi)
229 sr_spew("Backend requested info_id %d.", info_id);
234 sr_spew("%s: Returning hwcaps.", __func__);
236 case SR_DI_NUM_PROBES:
237 *data = GINT_TO_POINTER(1);
238 sr_spew("%s: Returning number of probes.", __func__);
240 case SR_DI_PROBE_NAMES:
242 sr_spew("%s: Returning probe names.", __func__);
244 case SR_DI_SAMPLERATES:
245 /* TODO: Get rid of this. */
247 sr_spew("%s: Returning samplerates.", __func__);
250 case SR_DI_CUR_SAMPLERATE:
251 /* TODO: Get rid of this. */
253 sr_spew("%s: Returning current samplerate.", __func__);
257 sr_err("%s: Unknown info_id %d.", __func__, info_id);
265 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
268 struct dev_context *devc;
273 case SR_HWCAP_LIMIT_MSEC:
274 /* TODO: Not yet implemented. */
275 if (*(const uint64_t *)value == 0) {
276 sr_err("Time limit cannot be 0.");
279 devc->limit_msec = *(const uint64_t *)value;
280 sr_dbg("Setting time limit to %" PRIu64 "ms.",
283 case SR_HWCAP_LIMIT_SAMPLES:
284 if (*(const uint64_t *)value == 0) {
285 sr_err("Sample limit cannot be 0.");
288 devc->limit_samples = *(const uint64_t *)value;
289 sr_dbg("Setting sample limit to %" PRIu64 ".",
290 devc->limit_samples);
293 sr_err("Unknown capability: %d.", hwcap);
301 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
304 struct sr_datafeed_packet packet;
305 struct sr_datafeed_header header;
306 struct sr_datafeed_meta_analog meta;
307 struct dev_context *devc;
311 sr_dbg("Starting acquisition.");
313 devc->cb_data = cb_data;
315 /* Send header packet to the session bus. */
316 sr_dbg("Sending SR_DF_HEADER.");
317 packet.type = SR_DF_HEADER;
318 packet.payload = (uint8_t *)&header;
319 header.feed_version = 1;
320 gettimeofday(&header.starttime, NULL);
321 sr_session_send(devc->cb_data, &packet);
323 /* Send metadata about the SR_DF_ANALOG packets to come. */
324 sr_dbg("Sending SR_DF_META_ANALOG.");
325 packet.type = SR_DF_META_ANALOG;
326 packet.payload = &meta;
328 sr_session_send(devc->cb_data, &packet);
330 sr_source_add(0, 0, 10 /* poll_timeout */,
331 uni_t_dmm_receive_data, (void *)sdi);
336 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
339 struct sr_datafeed_packet packet;
343 sr_dbg("Stopping acquisition.");
345 /* Send end packet to the session bus. */
346 sr_dbg("Sending SR_DF_END.");
347 packet.type = SR_DF_END;
348 sr_session_send(cb_data, &packet);
356 SR_PRIV struct sr_dev_driver uni_t_dmm_driver_info = {
358 .longname = "UNI-T DMM series",
361 .cleanup = hw_cleanup,
363 .dev_list = hw_dev_list,
364 .dev_clear = clear_instances,
365 .dev_open = hw_dev_open,
366 .dev_close = hw_dev_close,
367 .info_get = hw_info_get,
368 .dev_config_set = hw_dev_config_set,
369 .dev_acquisition_start = hw_dev_acquisition_start,
370 .dev_acquisition_stop = hw_dev_acquisition_stop,