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;
32 static struct sr_hwopt victor_70c_vidpid[] = {
33 { SR_HWOPT_CONN, "1244.d237" },
36 static struct dev_profile dev_profiles[] = {
37 { "victor-70c", "Victor", "70C", &dmmchip_fs9922,
38 DMM_TRANSPORT_USBHID, victor_70c_vidpid
40 { "mastech-va18b", "Mastech", "VA18B", NULL, DMM_TRANSPORT_SERIAL, 0, NULL},
41 { NULL, NULL, NULL, NULL, 0, 0, NULL }
44 static const int hwopts[] = {
51 static const int hwcaps[] = {
53 SR_HWCAP_LIMIT_SAMPLES,
59 static const char *probe_names[] = {
64 SR_PRIV struct sr_dev_driver genericdmm_driver_info;
65 static struct sr_dev_driver *gdi = &genericdmm_driver_info;
66 /* TODO need a way to keep this local to the static library */
67 static libusb_context *genericdmm_usb_context = NULL;
68 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
72 static GSList *connect_usb(const char *conn)
74 struct sr_dev_inst *sdi;
75 struct drv_context *drvc;
76 struct dev_context *devc;
77 struct sr_probe *probe;
78 libusb_device **devlist;
79 struct libusb_device_descriptor des;
83 int vid, pid, bus, addr, devcnt, err, i;
88 vid = pid = bus = addr = 0;
89 reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
90 if (g_regex_match(reg, conn, 0, &match)) {
92 if ((mstr = g_match_info_fetch(match, 1)))
93 vid = strtoul(mstr, NULL, 16);
97 if ((mstr = g_match_info_fetch(match, 2)))
98 pid = strtoul(mstr, NULL, 16);
101 g_match_info_unref(match);
103 reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL);
104 if (g_regex_match(reg, conn, 0, &match)) {
106 if ((mstr = g_match_info_fetch(match, 0)))
107 bus = strtoul(mstr, NULL, 16);
110 /* Extract address. */
111 if ((mstr = g_match_info_fetch(match, 0)))
112 addr = strtoul(mstr, NULL, 16);
116 g_match_info_unref(match);
119 if (vid + pid + bus + addr == 0)
123 sr_err("invalid bus");
128 sr_err("invalid address");
132 /* Looks like a valid USB device specification, but is it connected? */
134 libusb_get_device_list(genericdmm_usb_context, &devlist);
135 for (i = 0; devlist[i]; i++) {
136 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
137 sr_err("genericdmm: failed to get device descriptor: %d", err);
141 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
142 /* VID/PID specified, but no match. */
146 libusb_get_bus_number(devlist[i]) != bus
147 || libusb_get_device_address(devlist[i]) != addr))
148 /* Bus/address specified, but no match. */
152 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
153 sr_err("genericdmm: devc malloc failed.");
157 devcnt = g_slist_length(drvc->instances);
158 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
159 NULL, NULL, NULL))) {
160 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
164 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
166 sdi->probes = g_slist_append(sdi->probes, probe);
167 devc->usb = sr_usb_dev_inst_new(
168 libusb_get_bus_number(devlist[i]),
169 libusb_get_device_address(devlist[i]), NULL);
170 devices = g_slist_append(devices, sdi);
172 libusb_free_device_list(devlist, 1);
177 static GSList *connect_serial(const char *conn, const char *serialcomm)
184 sr_dbg("not yet implemented");
189 GSList *genericdmm_connect(const char *conn, const char *serialcomm)
194 /* Must be a serial port. */
195 return connect_serial(conn, serialcomm);
197 if ((devices = connect_usb(conn)))
203 static GSList *default_scan(GSList *options)
206 struct sr_hwopt *opt;
207 const char *conn, *serialcomm;
210 conn = serialcomm = NULL;
211 for (l = options; l; l = l->next) {
213 switch (opt->hwopt) {
217 case SR_HWOPT_SERIALCOMM:
218 serialcomm = opt->value;
223 devices = genericdmm_connect(conn, serialcomm);
228 static int open_usb(struct sr_dev_inst *sdi)
230 libusb_device **devlist;
231 struct libusb_device_descriptor des;
232 struct dev_context *devc;
233 int ret, tmp, cnt, i;
237 if (sdi->status == SR_ST_ACTIVE)
241 cnt = libusb_get_device_list(genericdmm_usb_context, &devlist);
243 sr_err("genericdmm: Failed to retrieve device list (%d)", cnt);
248 for (i = 0; i < cnt; i++) {
249 if ((tmp = libusb_get_device_descriptor(devlist[i], &des))) {
250 sr_err("genericdmm: Failed to get device descriptor: %d.", tmp);
254 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
255 || libusb_get_device_address(devlist[i]) != devc->usb->address)
256 /* this is not the one */
259 if ((tmp = libusb_open(devlist[i], &devc->usb->devhdl))) {
260 sr_err("genericdmm: Failed to open device: %d.", tmp);
264 sr_info("genericdmm: Opened device %s on %d.%d ", devc->profile->modelid,
265 devc->usb->bus, devc->usb->address);
269 libusb_free_device_list(devlist, 1);
274 static int clear_instances(void)
277 struct sr_dev_inst *sdi;
278 struct dev_context *devc;
279 struct drv_context *drvc;
281 if (!(drvc = gdi->priv))
284 /* Properly close and free all devices. */
285 for (l = drvc->instances; l; l = l->next) {
286 if (!(sdi = l->data)) {
287 /* Log error, but continue cleaning up the rest. */
288 sr_err("genericdmm: sdi was NULL, continuing.");
291 if (!(devc = sdi->priv)) {
292 /* Log error, but continue cleaning up the rest. */
293 sr_err("genericdmm: sdi->priv was NULL, continuing.");
298 switch (devc->profile->transport) {
299 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 /* TODO: O_NONBLOCK? */
468 sr_dbg("genericdmm: Opening serial port '%s'.", devc->serial->port);
469 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
470 if (devc->serial->fd == -1) {
471 sr_err("genericdmm: Couldn't open serial port '%s'.",
475 // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
478 sr_err("No transport set.");
485 static int hw_dev_close(struct sr_dev_inst *sdi)
487 struct dev_context *devc;
489 if (!(devc = sdi->priv)) {
490 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
494 switch (devc->profile->transport) {
495 case DMM_TRANSPORT_USBHID:
498 case DMM_TRANSPORT_SERIAL:
499 if (devc->serial && devc->serial->fd != -1) {
500 serial_close(devc->serial->fd);
501 devc->serial->fd = -1;
502 sdi->status = SR_ST_INACTIVE;
510 static int hw_cleanup(void)
515 if (genericdmm_usb_context)
516 libusb_exit(genericdmm_usb_context);
521 static int hw_info_get(int info_id, const void **data,
522 const struct sr_dev_inst *sdi)
524 struct dev_context *devc;
536 case SR_DI_NUM_PROBES:
537 *data = GINT_TO_POINTER(1);
539 case SR_DI_PROBE_NAMES:
542 case SR_DI_CUR_SAMPLERATE:
543 /* TODO get rid of this */
548 /* Unknown device info ID. */
555 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
558 struct dev_context *devc;
560 if (!(devc = sdi->priv)) {
561 sr_err("genericdmm: sdi->priv was NULL.");
566 case SR_HWCAP_LIMIT_MSEC:
567 /* TODO: not yet implemented */
568 if (*(const uint64_t *)value == 0) {
569 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
572 devc->limit_msec = *(const uint64_t *)value;
573 sr_dbg("genericdmm: Setting time limit to %" PRIu64 "ms.",
576 case SR_HWCAP_LIMIT_SAMPLES:
577 devc->limit_samples = *(const uint64_t *)value;
578 sr_dbg("genericdmm: Setting sample limit to %" PRIu64 ".",
579 devc->limit_samples);
582 sr_err("genericdmm: Unknown capability: %d.", hwcap);
590 static int receive_data(int fd, int revents, void *cb_data)
592 struct sr_dev_inst *sdi;
593 struct dev_context *devc;
597 if (!(sdi = cb_data))
600 if (!(devc = sdi->priv))
603 switch (devc->profile->transport) {
604 case DMM_TRANSPORT_USBHID:
605 if (devc->profile->chip->data)
606 devc->profile->chip->data(sdi);
608 case DMM_TRANSPORT_SERIAL:
614 if (devc->num_samples >= devc->limit_samples)
615 hw_dev_acquisition_stop(sdi, cb_data);
620 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
623 struct sr_datafeed_packet packet;
624 struct sr_datafeed_header header;
625 struct sr_datafeed_meta_analog meta;
626 struct dev_context *devc;
628 if (!(devc = sdi->priv)) {
629 sr_err("genericdmm: sdi->priv was NULL.");
633 sr_dbg("genericdmm: Starting acquisition.");
635 devc->cb_data = cb_data;
637 /* Send header packet to the session bus. */
638 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
639 packet.type = SR_DF_HEADER;
640 packet.payload = (uint8_t *)&header;
641 header.feed_version = 1;
642 gettimeofday(&header.starttime, NULL);
643 sr_session_send(devc->cb_data, &packet);
645 /* Send metadata about the SR_DF_ANALOG packets to come. */
646 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
647 packet.type = SR_DF_META_ANALOG;
648 packet.payload = &meta;
650 sr_session_send(devc->cb_data, &packet);
652 /* Hook up a proxy handler to receive data from the device. */
653 switch (devc->profile->transport) {
654 case DMM_TRANSPORT_USBHID:
655 /* Callously using stdin here. This works because no G_IO_* flags
656 * are set, but will certainly break when any other driver does
657 * this, and runs at the same time as genericdmm.
658 * We'll need a timeout-only source when revamping the whole
659 * driver source system.
661 sr_source_add(0, 0, devc->profile->poll_timeout,
662 receive_data, (void *)sdi);
664 case DMM_TRANSPORT_SERIAL:
665 /* TODO serial FD setup */
666 // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
673 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
676 struct sr_datafeed_packet packet;
678 /* Avoid compiler warnings. */
681 sr_dbg("genericdmm: Stopping acquisition.");
683 /* Send end packet to the session bus. */
684 sr_dbg("genericdmm: Sending SR_DF_END.");
685 packet.type = SR_DF_END;
686 sr_session_send(cb_data, &packet);
693 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
694 .name = "genericdmm",
695 .longname = "Generic DMM",
698 .cleanup = hw_cleanup,
700 .dev_list = hw_dev_list,
701 .dev_clear = clear_instances,
702 .dev_open = hw_dev_open,
703 .dev_close = hw_dev_close,
704 .info_get = hw_info_get,
705 .dev_config_set = hw_dev_config_set,
706 .dev_acquisition_start = hw_dev_acquisition_start,
707 .dev_acquisition_stop = hw_dev_acquisition_stop,