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, NULL},
41 { NULL, NULL, NULL, NULL, 0, NULL }
44 static const int hwcaps[] = {
46 SR_HWCAP_LIMIT_SAMPLES,
55 static const char *probe_names[] = {
60 SR_PRIV struct sr_dev_driver genericdmm_driver_info;
61 static struct sr_dev_driver *gdi = &genericdmm_driver_info;
62 /* TODO need a way to keep this local to the static library */
63 SR_PRIV libusb_context *genericdmm_usb_context = NULL;
66 static GSList *connect_usb(const char *conn)
68 struct sr_dev_inst *sdi;
70 libusb_device **devlist;
71 struct libusb_device_descriptor des;
75 int vid, pid, bus, addr, devcnt, err, i;
78 vid = pid = bus = addr = 0;
79 reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
80 if (g_regex_match(reg, conn, 0, &match)) {
82 if ((mstr = g_match_info_fetch(match, 1)))
83 vid = strtoul(mstr, NULL, 16);
87 if ((mstr = g_match_info_fetch(match, 2)))
88 pid = strtoul(mstr, NULL, 16);
91 g_match_info_unref(match);
93 reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL);
94 if (g_regex_match(reg, conn, 0, &match)) {
96 if ((mstr = g_match_info_fetch(match, 0)))
97 bus = strtoul(mstr, NULL, 16);
100 /* Extract address. */
101 if ((mstr = g_match_info_fetch(match, 0)))
102 addr = strtoul(mstr, NULL, 16);
106 g_match_info_unref(match);
109 if (vid + pid + bus + addr == 0)
113 sr_err("invalid bus");
118 sr_err("invalid address");
122 /* Looks like a valid USB device specification, but is it connected? */
124 libusb_get_device_list(genericdmm_usb_context, &devlist);
125 for (i = 0; devlist[i]; i++) {
126 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
127 sr_err("genericdmm: failed to get device descriptor: %d", err);
131 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
132 /* VID/PID specified, but no match. */
136 libusb_get_bus_number(devlist[i]) != bus
137 || libusb_get_device_address(devlist[i]) != addr))
138 /* Bus/address specified, but no match. */
142 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
143 sr_err("genericdmm: ctx malloc failed.");
147 devcnt = g_slist_length(gdi->instances);
148 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
149 "Generic DMM", NULL, NULL))) {
150 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
154 ctx->usb = sr_usb_dev_inst_new(
155 libusb_get_bus_number(devlist[i]),
156 libusb_get_device_address(devlist[i]), NULL);
157 devices = g_slist_append(devices, sdi);
159 libusb_free_device_list(devlist, 1);
164 static GSList *connect_serial(const char *conn, const char *serialcomm)
171 sr_dbg("not yet implemented");
176 GSList *genericdmm_connect(const char *conn, const char *serialcomm)
181 /* Must be a serial port. */
182 return connect_serial(conn, serialcomm);
184 if ((devices = connect_usb(conn)))
190 static GSList *default_scan(GSList *options)
193 struct sr_hwopt *opt;
194 const char *conn, *serialcomm;
197 conn = serialcomm = NULL;
198 for (l = options; l; l = l->next) {
200 switch (opt->hwopt) {
204 case SR_HWOPT_SERIALCOMM:
205 serialcomm = opt->value;
210 devices = genericdmm_connect(conn, serialcomm);
215 static int hw_init(void)
218 if (libusb_init(&genericdmm_usb_context) != 0) {
219 sr_err("genericdmm: Failed to initialize USB.");
227 static GSList *hw_scan(GSList *options)
229 GSList *l, *ldef, *defopts, *newopts, *devices;
230 struct sr_hwopt *opt, *defopt;
231 struct dev_profile *pr, *profile;
234 /* Separate model from the options list. */
237 for (l = options; l; l = l->next) {
239 if (opt->hwopt == SR_HWOPT_MODEL)
242 /* New list with references to the original data. */
243 newopts = g_slist_append(newopts, opt);
246 sr_err("Need a model to scan for.");
250 /* Find a profile with this model name. */
252 for (pr = dev_profiles; pr->modelid; pr++) {
253 if (!strcmp(pr->modelid, model)) {
259 sr_err("Unknown model %s.", model);
263 /* Initialize the DMM chip driver. */
264 if (profile->chip->init)
265 profile->chip->init();
267 /* Convert the profile's default options list to a GSList. */
269 for (opt = profile->defaults_opts; opt->hwopt; opt++) {
270 /* New list with references to const data in the profile. */
271 defopts = g_slist_append(defopts, opt);
274 /* Options given as argument to this function override the
275 * profile's default options.
277 for (ldef = defopts; ldef; ldef = ldef->next) {
279 for (l = newopts; l; l = l->next) {
281 if (opt->hwopt == defopt->hwopt) {
282 /* Override the default, and drop it from the
285 ldef->data = l->data;
286 newopts = g_slist_remove(newopts, opt);
291 /* Whatever is left in newopts wasn't in the default options. */
292 defopts = g_slist_concat(defopts, newopts);
293 g_slist_free(newopts);
295 if (profile->chip->scan)
296 /* The DMM chip driver wants to do its own scanning. */
297 devices = profile->chip->scan(defopts);
299 devices = default_scan(defopts);
300 g_slist_free(defopts);
303 /* TODO: need to fix up sdi->index fields */
304 /* Add a copy of these new devices to the driver instances. */
305 for (l = devices; l; l = l->next)
306 gdi->instances = g_slist_append(gdi->instances, l->data);
312 static int hw_dev_open(int dev_index)
314 struct sr_dev_inst *sdi;
317 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
318 sr_err("genericdmm: sdi was NULL.");
322 if (!(ctx = sdi->priv)) {
323 sr_err("genericdmm: sdi->priv was NULL.");
327 sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
329 switch (ctx->profile->transport) {
330 case DMM_TRANSPORT_USBHID:
333 case DMM_TRANSPORT_SERIAL:
334 /* TODO: O_NONBLOCK? */
335 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
336 if (ctx->serial->fd == -1) {
337 sr_err("genericdmm: Couldn't open serial port '%s'.",
341 // serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
344 sr_err("No transport set.");
350 static int hw_dev_close(int dev_index)
352 struct sr_dev_inst *sdi;
355 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
356 sr_err("genericdmm: %s: sdi was NULL.", __func__);
360 if (!(ctx = sdi->priv)) {
361 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
365 /* TODO: Check for != NULL. */
367 switch (ctx->profile->transport) {
368 case DMM_TRANSPORT_USBHID:
371 case DMM_TRANSPORT_SERIAL:
372 if (ctx->serial && ctx->serial->fd != -1) {
373 serial_close(ctx->serial->fd);
374 ctx->serial->fd = -1;
375 sdi->status = SR_ST_INACTIVE;
383 static int hw_cleanup(void)
386 struct sr_dev_inst *sdi;
389 /* Properly close and free all devices. */
390 for (l = gdi->instances; l; l = l->next) {
391 if (!(sdi = l->data)) {
392 /* Log error, but continue cleaning up the rest. */
393 sr_err("genericdmm: sdi was NULL, continuing.");
396 if (!(ctx = sdi->priv)) {
397 /* Log error, but continue cleaning up the rest. */
398 sr_err("genericdmm: sdi->priv was NULL, continuing.");
403 switch (ctx->profile->transport) {
404 case DMM_TRANSPORT_USBHID:
407 case DMM_TRANSPORT_SERIAL:
408 if (ctx->serial && ctx->serial->fd != -1)
409 serial_close(ctx->serial->fd);
410 sr_serial_dev_inst_free(ctx->serial);
415 sr_dev_inst_free(sdi);
418 g_slist_free(gdi->instances);
419 gdi->instances = NULL;
421 if (genericdmm_usb_context)
422 libusb_exit(genericdmm_usb_context);
427 static int hw_info_get(int dev_info_id, const void **data,
428 const struct sr_dev_inst *sdi)
432 switch (dev_info_id) {
435 sr_spew("genericdmm: Returning sdi.");
437 case SR_DI_NUM_PROBES:
438 *data = GINT_TO_POINTER(1);
440 case SR_DI_PROBE_NAMES:
443 case SR_DI_CUR_SAMPLERATE:
444 /* TODO get rid of this */
448 /* Unknown device info ID. */
455 static int hw_dev_status_get(int dev_index)
457 struct sr_dev_inst *sdi;
459 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
460 sr_err("genericdmm: sdi was NULL, device not found.");
461 return SR_ST_NOT_FOUND;
464 sr_dbg("genericdmm: Returning status: %d.", sdi->status);
469 static const int *hw_hwcap_get_all(void)
471 sr_spew("genericdmm: Returning list of device capabilities.");
476 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
478 struct sr_dev_inst *sdi;
481 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
482 sr_err("genericdmm: sdi was NULL.");
486 if (!(ctx = sdi->priv)) {
487 sr_err("genericdmm: sdi->priv was NULL.");
491 sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
494 case SR_HWCAP_LIMIT_MSEC:
495 if (*(const uint64_t *)value == 0) {
496 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
499 ctx->limit_msec = *(const uint64_t *)value;
500 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
503 case SR_HWCAP_LIMIT_SAMPLES:
504 ctx->limit_samples = *(const uint64_t *)value;
505 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
509 sr_err("genericdmm: Unknown capability: %d.", hwcap);
517 static int receive_data(int fd, int revents, void *cb_data)
519 struct sr_dev_inst *sdi;
522 if (!(sdi = cb_data))
525 if (!(ctx = sdi->priv))
528 if (revents != G_IO_IN) {
529 sr_err("genericdmm: No data?");
533 switch (ctx->profile->transport) {
534 case DMM_TRANSPORT_USBHID:
537 case DMM_TRANSPORT_SERIAL:
545 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
547 struct sr_datafeed_packet packet;
548 struct sr_datafeed_header header;
549 struct sr_datafeed_meta_analog meta;
550 struct sr_dev_inst *sdi;
553 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
554 sr_err("genericdmm: sdi was NULL.");
558 if (!(ctx = sdi->priv)) {
559 sr_err("genericdmm: sdi->priv was NULL.");
563 sr_dbg("genericdmm: Starting acquisition.");
565 ctx->cb_data = cb_data;
567 /* Send header packet to the session bus. */
568 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
569 packet.type = SR_DF_HEADER;
570 packet.payload = (uint8_t *)&header;
571 header.feed_version = 1;
572 gettimeofday(&header.starttime, NULL);
573 sr_session_send(ctx->cb_data, &packet);
575 /* Send metadata about the SR_DF_ANALOG packets to come. */
576 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
577 packet.type = SR_DF_META_ANALOG;
578 packet.payload = &meta;
580 sr_session_send(ctx->cb_data, &packet);
582 /* Hook up a proxy handler to receive data from the device. */
583 switch (ctx->profile->transport) {
584 case DMM_TRANSPORT_USBHID:
585 /* TODO libusb FD setup */
587 case DMM_TRANSPORT_SERIAL:
588 /* TODO serial FD setup */
589 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
596 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
598 struct sr_datafeed_packet packet;
600 /* Avoid compiler warnings. */
603 sr_dbg("genericdmm: Stopping acquisition.");
605 /* Send end packet to the session bus. */
606 sr_dbg("genericdmm: Sending SR_DF_END.");
607 packet.type = SR_DF_END;
608 sr_session_send(cb_data, &packet);
613 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
614 .name = "genericdmm",
615 .longname = "Generic DMM",
618 .cleanup = hw_cleanup,
620 .dev_open = hw_dev_open,
621 .dev_close = hw_dev_close,
622 .info_get = hw_info_get,
623 .dev_status_get = hw_dev_status_get,
624 .hwcap_get_all = hw_hwcap_get_all,
625 .dev_config_set = hw_dev_config_set,
626 .dev_acquisition_start = hw_dev_acquisition_start,
627 .dev_acquisition_stop = hw_dev_acquisition_stop,