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:
302 sr_usb_dev_inst_free(devc->usb);
304 case DMM_TRANSPORT_SERIAL:
305 if (devc->serial && devc->serial->fd != -1)
306 serial_close(devc->serial->fd);
307 sr_serial_dev_inst_free(devc->serial);
312 sr_dev_inst_free(sdi);
315 g_slist_free(drvc->instances);
316 drvc->instances = NULL;
321 static int hw_init(void)
323 struct drv_context *drvc;
325 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
326 sr_err("genericdmm: driver context malloc failed.");
330 if (libusb_init(&genericdmm_usb_context) != 0) {
331 sr_err("genericdmm: Failed to initialize USB.");
340 static GSList *hw_scan(GSList *options)
342 GSList *l, *ldef, *defopts, *newopts, *devices;
343 struct sr_hwopt *opt, *defopt;
344 struct dev_profile *pr, *profile;
345 struct sr_dev_inst *sdi;
346 struct drv_context *drvc;
347 struct dev_context *devc;
352 /* Separate model from the options list. */
355 for (l = options; l; l = l->next) {
357 if (opt->hwopt == SR_HWOPT_MODEL)
360 /* New list with references to the original data. */
361 newopts = g_slist_append(newopts, opt);
364 /* This driver only works when a model is specified. */
368 /* Find a profile with this model name. */
370 for (pr = dev_profiles; pr->modelid; pr++) {
371 if (!strcmp(pr->modelid, model)) {
377 sr_err("Unknown model %s.", model);
381 /* Initialize the DMM chip driver. */
382 if (profile->chip->init)
383 profile->chip->init();
385 /* Convert the profile's default options list to a GSList. */
387 for (opt = profile->defaults_opts; opt->hwopt; opt++) {
388 /* New list with references to const data in the profile. */
389 defopts = g_slist_append(defopts, opt);
392 /* Options given as argument to this function override the
393 * profile's default options.
395 for (ldef = defopts; ldef; ldef = ldef->next) {
397 for (l = newopts; l; l = l->next) {
399 if (opt->hwopt == defopt->hwopt) {
400 /* Override the default, and drop it from the
403 ldef->data = l->data;
404 newopts = g_slist_remove(newopts, opt);
409 /* Whatever is left in newopts wasn't in the default options. */
410 defopts = g_slist_concat(defopts, newopts);
411 g_slist_free(newopts);
413 if (profile->chip->scan)
414 /* The DMM chip driver wants to do its own scanning. */
415 devices = profile->chip->scan(defopts);
417 devices = default_scan(defopts);
418 g_slist_free(defopts);
421 /* TODO: need to fix up sdi->index fields */
422 for (l = devices; l; l = l->next) {
423 /* The default connection-based scanner doesn't really
424 * know about profiles, so it never filled in the vendor
425 * or model. Do that now.
429 devc->profile = profile;
432 sdi->vendor = g_strdup(profile->vendor);
434 sdi->model = g_strdup(profile->model);
435 /* Add a copy of these new devices to the driver instances. */
436 drvc->instances = g_slist_append(drvc->instances, l->data);
443 static GSList *hw_dev_list(void)
445 struct drv_context *drvc;
449 return drvc->instances;
452 static int hw_dev_open(struct sr_dev_inst *sdi)
454 struct dev_context *devc;
457 if (!(devc = sdi->priv)) {
458 sr_err("genericdmm: sdi->priv was NULL.");
463 switch (devc->profile->transport) {
464 case DMM_TRANSPORT_USBHID:
467 case DMM_TRANSPORT_SERIAL:
468 /* TODO: O_NONBLOCK? */
469 sr_dbg("genericdmm: Opening serial port '%s'.", devc->serial->port);
470 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
471 if (devc->serial->fd == -1) {
472 sr_err("genericdmm: Couldn't open serial port '%s'.",
476 // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
479 sr_err("No transport set.");
486 static int hw_dev_close(struct sr_dev_inst *sdi)
488 struct dev_context *devc;
490 if (!(devc = sdi->priv)) {
491 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
495 switch (devc->profile->transport) {
496 case DMM_TRANSPORT_USBHID:
499 case DMM_TRANSPORT_SERIAL:
500 if (devc->serial && devc->serial->fd != -1) {
501 serial_close(devc->serial->fd);
502 devc->serial->fd = -1;
503 sdi->status = SR_ST_INACTIVE;
511 static int hw_cleanup(void)
516 if (genericdmm_usb_context)
517 libusb_exit(genericdmm_usb_context);
522 static int hw_info_get(int info_id, const void **data,
523 const struct sr_dev_inst *sdi)
525 struct dev_context *devc;
537 case SR_DI_NUM_PROBES:
538 *data = GINT_TO_POINTER(1);
540 case SR_DI_PROBE_NAMES:
543 case SR_DI_CUR_SAMPLERATE:
544 /* TODO get rid of this */
549 /* Unknown device info ID. */
556 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
559 struct dev_context *devc;
561 if (!(devc = sdi->priv)) {
562 sr_err("genericdmm: sdi->priv was NULL.");
567 case SR_HWCAP_LIMIT_MSEC:
568 /* TODO: not yet implemented */
569 if (*(const uint64_t *)value == 0) {
570 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
573 devc->limit_msec = *(const uint64_t *)value;
574 sr_dbg("genericdmm: Setting time limit to %" PRIu64 "ms.",
577 case SR_HWCAP_LIMIT_SAMPLES:
578 devc->limit_samples = *(const uint64_t *)value;
579 sr_dbg("genericdmm: Setting sample limit to %" PRIu64 ".",
580 devc->limit_samples);
583 sr_err("genericdmm: Unknown capability: %d.", hwcap);
591 static int receive_data(int fd, int revents, void *cb_data)
593 struct sr_dev_inst *sdi;
594 struct dev_context *devc;
598 if (!(sdi = cb_data))
601 if (!(devc = sdi->priv))
604 switch (devc->profile->transport) {
605 case DMM_TRANSPORT_USBHID:
606 if (devc->profile->chip->data)
607 devc->profile->chip->data(sdi);
609 case DMM_TRANSPORT_SERIAL:
615 if (devc->num_samples >= devc->limit_samples)
616 hw_dev_acquisition_stop(sdi, cb_data);
621 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
624 struct sr_datafeed_packet packet;
625 struct sr_datafeed_header header;
626 struct sr_datafeed_meta_analog meta;
627 struct dev_context *devc;
629 if (!(devc = sdi->priv)) {
630 sr_err("genericdmm: sdi->priv was NULL.");
634 sr_dbg("genericdmm: Starting acquisition.");
636 devc->cb_data = cb_data;
638 /* Send header packet to the session bus. */
639 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
640 packet.type = SR_DF_HEADER;
641 packet.payload = (uint8_t *)&header;
642 header.feed_version = 1;
643 gettimeofday(&header.starttime, NULL);
644 sr_session_send(devc->cb_data, &packet);
646 /* Send metadata about the SR_DF_ANALOG packets to come. */
647 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
648 packet.type = SR_DF_META_ANALOG;
649 packet.payload = &meta;
651 sr_session_send(devc->cb_data, &packet);
653 /* Hook up a proxy handler to receive data from the device. */
654 switch (devc->profile->transport) {
655 case DMM_TRANSPORT_USBHID:
656 /* Callously using stdin here. This works because no G_IO_* flags
657 * are set, but will certainly break when any other driver does
658 * this, and runs at the same time as genericdmm.
659 * We'll need a timeout-only source when revamping the whole
660 * driver source system.
662 sr_source_add(0, 0, devc->profile->poll_timeout,
663 receive_data, (void *)sdi);
665 case DMM_TRANSPORT_SERIAL:
666 /* TODO serial FD setup */
667 // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
674 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
677 struct sr_datafeed_packet packet;
679 /* Avoid compiler warnings. */
682 sr_dbg("genericdmm: Stopping acquisition.");
684 /* Send end packet to the session bus. */
685 sr_dbg("genericdmm: Sending SR_DF_END.");
686 packet.type = SR_DF_END;
687 sr_session_send(cb_data, &packet);
694 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
695 .name = "genericdmm",
696 .longname = "Generic DMM",
699 .cleanup = hw_cleanup,
701 .dev_list = hw_dev_list,
702 .dev_clear = clear_instances,
703 .dev_open = hw_dev_open,
704 .dev_close = hw_dev_close,
705 .info_get = hw_info_get,
706 .dev_config_set = hw_dev_config_set,
707 .dev_acquisition_start = hw_dev_acquisition_start,
708 .dev_acquisition_stop = hw_dev_acquisition_stop,