2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27 #include "genericdmm.h"
30 extern SR_PRIV struct dmmchip dmmchip_fs9922;
31 extern SR_PRIV struct dmmchip dmmchip_victor70c;
33 static struct sr_hwopt victor_70c_vidpid[] = {
34 { SR_HWOPT_CONN, "1244.d237" },
37 static struct dev_profile dev_profiles[] = {
38 { "victor-70c", "Victor", "70C", &dmmchip_victor70c,
39 DMM_TRANSPORT_USBHID, 1000, victor_70c_vidpid
41 { "mastech-va18b", "Mastech", "VA18B", NULL, DMM_TRANSPORT_SERIAL, 0, NULL},
42 { NULL, NULL, NULL, NULL, 0, 0, NULL }
45 static const int hwopts[] = {
52 static const int hwcaps[] = {
54 SR_HWCAP_LIMIT_SAMPLES,
60 static const char *probe_names[] = {
65 SR_PRIV struct sr_dev_driver genericdmm_driver_info;
66 static struct sr_dev_driver *gdi = &genericdmm_driver_info;
67 /* TODO need a way to keep this local to the static library */
68 static libusb_context *genericdmm_usb_context = NULL;
69 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
73 static GSList *connect_usb(const char *conn)
75 struct sr_dev_inst *sdi;
76 struct drv_context *drvc;
77 struct dev_context *devc;
78 struct sr_probe *probe;
79 libusb_device **devlist;
80 struct libusb_device_descriptor des;
84 int vid, pid, bus, addr, devcnt, err, i;
89 vid = pid = bus = addr = 0;
90 reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
91 if (g_regex_match(reg, conn, 0, &match)) {
93 if ((mstr = g_match_info_fetch(match, 1)))
94 vid = strtoul(mstr, NULL, 16);
98 if ((mstr = g_match_info_fetch(match, 2)))
99 pid = strtoul(mstr, NULL, 16);
102 g_match_info_unref(match);
104 reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL);
105 if (g_regex_match(reg, conn, 0, &match)) {
107 if ((mstr = g_match_info_fetch(match, 0)))
108 bus = strtoul(mstr, NULL, 16);
111 /* Extract address. */
112 if ((mstr = g_match_info_fetch(match, 0)))
113 addr = strtoul(mstr, NULL, 16);
117 g_match_info_unref(match);
120 if (vid + pid + bus + addr == 0)
124 sr_err("invalid bus");
129 sr_err("invalid address");
133 /* Looks like a valid USB device specification, but is it connected? */
135 libusb_get_device_list(genericdmm_usb_context, &devlist);
136 for (i = 0; devlist[i]; i++) {
137 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
138 sr_err("genericdmm: failed to get device descriptor: %d", err);
142 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
143 /* VID/PID specified, but no match. */
147 libusb_get_bus_number(devlist[i]) != bus
148 || libusb_get_device_address(devlist[i]) != addr))
149 /* Bus/address specified, but no match. */
153 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
154 sr_err("genericdmm: devc malloc failed.");
158 devcnt = g_slist_length(drvc->instances);
159 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
160 NULL, NULL, NULL))) {
161 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
165 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
167 sdi->probes = g_slist_append(sdi->probes, probe);
168 devc->usb = sr_usb_dev_inst_new(
169 libusb_get_bus_number(devlist[i]),
170 libusb_get_device_address(devlist[i]), NULL);
171 devices = g_slist_append(devices, sdi);
173 libusb_free_device_list(devlist, 1);
178 static GSList *connect_serial(const char *conn, const char *serialcomm)
185 sr_dbg("not yet implemented");
190 GSList *genericdmm_connect(const char *conn, const char *serialcomm)
195 /* Must be a serial port. */
196 return connect_serial(conn, serialcomm);
198 if ((devices = connect_usb(conn)))
204 static GSList *default_scan(GSList *options)
207 struct sr_hwopt *opt;
208 const char *conn, *serialcomm;
211 conn = serialcomm = NULL;
212 for (l = options; l; l = l->next) {
214 switch (opt->hwopt) {
218 case SR_HWOPT_SERIALCOMM:
219 serialcomm = opt->value;
224 devices = genericdmm_connect(conn, serialcomm);
229 static int open_usb(struct sr_dev_inst *sdi)
231 libusb_device **devlist;
232 struct libusb_device_descriptor des;
233 struct dev_context *devc;
234 int ret, tmp, cnt, i;
238 if (sdi->status == SR_ST_ACTIVE)
242 cnt = libusb_get_device_list(genericdmm_usb_context, &devlist);
244 sr_err("genericdmm: Failed to retrieve device list (%d)", cnt);
249 for (i = 0; i < cnt; i++) {
250 if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) {
251 sr_err("genericdmm: Failed to get device descriptor: %d.", tmp);
255 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
256 || libusb_get_device_address(devlist[i]) != devc->usb->address)
257 /* this is not the one */
260 if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) {
261 sr_err("genericdmm: Failed to open device: %d.", tmp);
265 sr_info("genericdmm: Opened device %s on %d.%d ", devc->profile->modelid,
266 devc->usb->bus, devc->usb->address);
270 libusb_free_device_list(devlist, 1);
275 static int clear_instances(void)
278 struct sr_dev_inst *sdi;
279 struct dev_context *devc;
280 struct drv_context *drvc;
282 if (!(drvc = gdi->priv))
285 /* Properly close and free all devices. */
286 for (l = drvc->instances; l; l = l->next) {
287 if (!(sdi = l->data)) {
288 /* Log error, but continue cleaning up the rest. */
289 sr_err("genericdmm: sdi was NULL, continuing.");
292 if (!(devc = sdi->priv)) {
293 /* Log error, but continue cleaning up the rest. */
294 sr_err("genericdmm: sdi->priv was NULL, continuing.");
299 switch (devc->profile->transport) {
300 case DMM_TRANSPORT_USBHID:
301 sr_usb_dev_inst_free(devc->usb);
303 case DMM_TRANSPORT_SERIAL:
304 if (devc->serial && devc->serial->fd != -1)
305 serial_close(devc->serial->fd);
306 sr_serial_dev_inst_free(devc->serial);
311 sr_dev_inst_free(sdi);
314 g_slist_free(drvc->instances);
315 drvc->instances = NULL;
320 static int hw_init(void)
322 struct drv_context *drvc;
324 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
325 sr_err("genericdmm: driver context malloc failed.");
329 if (libusb_init(&genericdmm_usb_context) != 0) {
330 sr_err("genericdmm: Failed to initialize USB.");
339 static GSList *hw_scan(GSList *options)
341 GSList *l, *ldef, *defopts, *newopts, *devices;
342 struct sr_hwopt *opt, *defopt;
343 struct dev_profile *pr, *profile;
344 struct sr_dev_inst *sdi;
345 struct drv_context *drvc;
346 struct dev_context *devc;
351 /* Separate model from the options list. */
354 for (l = options; l; l = l->next) {
356 if (opt->hwopt == SR_HWOPT_MODEL)
359 /* New list with references to the original data. */
360 newopts = g_slist_append(newopts, opt);
363 /* This driver only works when a model is specified. */
367 /* Find a profile with this model name. */
369 for (pr = dev_profiles; pr->modelid; pr++) {
370 if (!strcmp(pr->modelid, model)) {
376 sr_err("Unknown model %s.", model);
380 /* Initialize the DMM chip driver. */
381 if (profile->chip->init)
382 profile->chip->init();
384 /* Convert the profile's default options list to a GSList. */
386 for (opt = profile->defaults_opts; opt->hwopt; opt++) {
387 /* New list with references to const data in the profile. */
388 defopts = g_slist_append(defopts, opt);
391 /* Options given as argument to this function override the
392 * profile's default options.
394 for (ldef = defopts; ldef; ldef = ldef->next) {
396 for (l = newopts; l; l = l->next) {
398 if (opt->hwopt == defopt->hwopt) {
399 /* Override the default, and drop it from the
402 ldef->data = l->data;
403 newopts = g_slist_remove(newopts, opt);
408 /* Whatever is left in newopts wasn't in the default options. */
409 defopts = g_slist_concat(defopts, newopts);
410 g_slist_free(newopts);
412 if (profile->chip->scan)
413 /* The DMM chip driver wants to do its own scanning. */
414 devices = profile->chip->scan(defopts);
416 devices = default_scan(defopts);
417 g_slist_free(defopts);
420 /* TODO: need to fix up sdi->index fields */
421 for (l = devices; l; l = l->next) {
422 /* The default connection-based scanner doesn't really
423 * know about profiles, so it never filled in the vendor
424 * or model. Do that now.
428 devc->profile = profile;
431 sdi->vendor = g_strdup(profile->vendor);
433 sdi->model = g_strdup(profile->model);
434 /* Add a copy of these new devices to the driver instances. */
435 drvc->instances = g_slist_append(drvc->instances, l->data);
442 static GSList *hw_dev_list(void)
444 struct drv_context *drvc;
448 return drvc->instances;
451 static int hw_dev_open(struct sr_dev_inst *sdi)
453 struct dev_context *devc;
456 if (!(devc = sdi->priv)) {
457 sr_err("genericdmm: sdi->priv was NULL.");
462 switch (devc->profile->transport) {
463 case DMM_TRANSPORT_USBHID:
466 case DMM_TRANSPORT_SERIAL:
467 sr_dbg("genericdmm: Opening serial port '%s'.", devc->serial->port);
468 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
469 if (devc->serial->fd == -1) {
470 sr_err("genericdmm: Couldn't open serial port '%s'.",
474 // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
477 sr_err("No transport set.");
484 static int hw_dev_close(struct sr_dev_inst *sdi)
486 struct dev_context *devc;
488 if (!(devc = sdi->priv)) {
489 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
493 switch (devc->profile->transport) {
494 case DMM_TRANSPORT_USBHID:
497 case DMM_TRANSPORT_SERIAL:
498 if (devc->serial && devc->serial->fd != -1) {
499 serial_close(devc->serial->fd);
500 devc->serial->fd = -1;
501 sdi->status = SR_ST_INACTIVE;
509 static int hw_cleanup(void)
514 if (genericdmm_usb_context)
515 libusb_exit(genericdmm_usb_context);
520 static int hw_info_get(int info_id, const void **data,
521 const struct sr_dev_inst *sdi)
523 struct dev_context *devc;
535 case SR_DI_NUM_PROBES:
536 *data = GINT_TO_POINTER(1);
538 case SR_DI_PROBE_NAMES:
541 case SR_DI_CUR_SAMPLERATE:
542 /* TODO get rid of this */
547 /* Unknown device info ID. */
554 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
557 struct dev_context *devc;
559 if (!(devc = sdi->priv)) {
560 sr_err("genericdmm: sdi->priv was NULL.");
565 case SR_HWCAP_LIMIT_MSEC:
566 /* TODO: not yet implemented */
567 if (*(const uint64_t *)value == 0) {
568 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
571 devc->limit_msec = *(const uint64_t *)value;
572 sr_dbg("genericdmm: Setting time limit to %" PRIu64 "ms.",
575 case SR_HWCAP_LIMIT_SAMPLES:
576 devc->limit_samples = *(const uint64_t *)value;
577 sr_dbg("genericdmm: Setting sample limit to %" PRIu64 ".",
578 devc->limit_samples);
581 sr_err("genericdmm: Unknown capability: %d.", hwcap);
589 static int receive_data(int fd, int revents, void *cb_data)
591 struct sr_dev_inst *sdi;
592 struct dev_context *devc;
596 if (!(sdi = cb_data))
599 if (!(devc = sdi->priv))
602 switch (devc->profile->transport) {
603 case DMM_TRANSPORT_USBHID:
604 if (devc->profile->chip->data)
605 devc->profile->chip->data(sdi);
607 case DMM_TRANSPORT_SERIAL:
613 if (devc->num_samples >= devc->limit_samples)
614 hw_dev_acquisition_stop(sdi, cb_data);
619 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
622 struct sr_datafeed_packet packet;
623 struct sr_datafeed_header header;
624 struct sr_datafeed_meta_analog meta;
625 struct dev_context *devc;
627 if (!(devc = sdi->priv)) {
628 sr_err("genericdmm: sdi->priv was NULL.");
632 sr_dbg("genericdmm: Starting acquisition.");
634 devc->cb_data = cb_data;
636 /* Send header packet to the session bus. */
637 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
638 packet.type = SR_DF_HEADER;
639 packet.payload = (uint8_t *)&header;
640 header.feed_version = 1;
641 gettimeofday(&header.starttime, NULL);
642 sr_session_send(devc->cb_data, &packet);
644 /* Send metadata about the SR_DF_ANALOG packets to come. */
645 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
646 packet.type = SR_DF_META_ANALOG;
647 packet.payload = &meta;
649 sr_session_send(devc->cb_data, &packet);
651 /* Hook up a proxy handler to receive data from the device. */
652 switch (devc->profile->transport) {
653 case DMM_TRANSPORT_USBHID:
654 /* Callously using stdin here. This works because no G_IO_* flags
655 * are set, but will certainly break when any other driver does
656 * this, and runs at the same time as genericdmm.
657 * We'll need a timeout-only source when revamping the whole
658 * driver source system.
660 sr_source_add(0, 0, devc->profile->poll_timeout,
661 receive_data, (void *)sdi);
663 case DMM_TRANSPORT_SERIAL:
664 /* TODO serial FD setup */
665 // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
672 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
675 struct sr_datafeed_packet packet;
677 /* Avoid compiler warnings. */
680 sr_dbg("genericdmm: Stopping acquisition.");
682 /* Send end packet to the session bus. */
683 sr_dbg("genericdmm: Sending SR_DF_END.");
684 packet.type = SR_DF_END;
685 sr_session_send(cb_data, &packet);
692 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
693 .name = "genericdmm",
694 .longname = "Generic DMM",
697 .cleanup = hw_cleanup,
699 .dev_list = hw_dev_list,
700 .dev_clear = clear_instances,
701 .dev_open = hw_dev_open,
702 .dev_close = hw_dev_close,
703 .info_get = hw_info_get,
704 .dev_config_set = hw_dev_config_set,
705 .dev_acquisition_start = hw_dev_acquisition_start,
706 .dev_acquisition_stop = hw_dev_acquisition_stop,