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 /* TODO need a way to keep these local to the static library */
61 SR_PRIV GSList *genericdmm_dev_insts = NULL;
62 SR_PRIV libusb_context *genericdmm_usb_context = NULL;
65 static int hw_init(void)
68 if (libusb_init(&genericdmm_usb_context) != 0) {
69 sr_err("genericdmm: Failed to initialize USB.");
77 static int hw_scan(void)
79 struct sr_dev_inst *sdi;
83 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
84 sr_err("genericdmm: ctx malloc failed.");
88 devcnt = g_slist_length(genericdmm_dev_insts);
89 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, "Generic DMM",
91 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
95 genericdmm_dev_insts = g_slist_append(genericdmm_dev_insts, sdi);
97 /* Always initialized just one device instance. */
101 static int hw_dev_open(int dev_index)
103 struct sr_dev_inst *sdi;
106 if (!(sdi = sr_dev_inst_get(genericdmm_dev_insts, dev_index))) {
107 sr_err("genericdmm: sdi was NULL.");
111 if (!(ctx = sdi->priv)) {
112 sr_err("genericdmm: sdi->priv was NULL.");
116 sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
118 switch (ctx->profile->transport) {
119 case DMM_TRANSPORT_USBHID:
122 case DMM_TRANSPORT_SERIAL:
123 /* TODO: O_NONBLOCK? */
124 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
125 if (ctx->serial->fd == -1) {
126 sr_err("genericdmm: Couldn't open serial port '%s'.",
130 // serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
133 sr_err("No transport set.");
139 static int hw_dev_close(int dev_index)
141 struct sr_dev_inst *sdi;
144 if (!(sdi = sr_dev_inst_get(genericdmm_dev_insts, dev_index))) {
145 sr_err("genericdmm: %s: sdi was NULL.", __func__);
149 if (!(ctx = sdi->priv)) {
150 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
154 /* TODO: Check for != NULL. */
156 switch (ctx->profile->transport) {
157 case DMM_TRANSPORT_USBHID:
160 case DMM_TRANSPORT_SERIAL:
161 if (ctx->serial && ctx->serial->fd != -1) {
162 serial_close(ctx->serial->fd);
163 ctx->serial->fd = -1;
164 sdi->status = SR_ST_INACTIVE;
172 static int hw_cleanup(void)
175 struct sr_dev_inst *sdi;
178 /* Properly close and free all devices. */
179 for (l = genericdmm_dev_insts; l; l = l->next) {
180 if (!(sdi = l->data)) {
181 /* Log error, but continue cleaning up the rest. */
182 sr_err("genericdmm: sdi was NULL, continuing.");
185 if (!(ctx = sdi->priv)) {
186 /* Log error, but continue cleaning up the rest. */
187 sr_err("genericdmm: sdi->priv was NULL, continuing.");
192 switch (ctx->profile->transport) {
193 case DMM_TRANSPORT_USBHID:
196 case DMM_TRANSPORT_SERIAL:
197 if (ctx->serial && ctx->serial->fd != -1)
198 serial_close(ctx->serial->fd);
199 sr_serial_dev_inst_free(ctx->serial);
204 sr_dev_inst_free(sdi);
207 g_slist_free(genericdmm_dev_insts);
208 genericdmm_dev_insts = NULL;
210 if (genericdmm_usb_context)
211 libusb_exit(genericdmm_usb_context);
216 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
218 struct sr_dev_inst *sdi;
222 if (!(sdi = sr_dev_inst_get(genericdmm_dev_insts, dev_index))) {
223 sr_err("genericdmm: sdi was NULL.");
227 if (!(ctx = sdi->priv)) {
228 sr_err("genericdmm: sdi->priv was NULL.");
232 sr_spew("genericdmm: dev_index %d, dev_info_id %d.",
233 dev_index, dev_info_id);
235 switch (dev_info_id) {
238 sr_spew("genericdmm: Returning sdi.");
240 case SR_DI_NUM_PROBES:
241 info = GINT_TO_POINTER(1);
242 sr_spew("genericdmm: Returning number of probes: 1.");
244 case SR_DI_PROBE_NAMES:
246 sr_spew("genericdmm: Returning probenames.");
248 case SR_DI_CUR_SAMPLERATE:
249 /* TODO get rid of this */
251 sr_spew("genericdmm: Returning samplerate: 0.");
254 /* Unknown device info ID. */
255 sr_err("genericdmm: Unknown device info ID: %d.", dev_info_id);
263 static int hw_dev_status_get(int dev_index)
265 struct sr_dev_inst *sdi;
267 if (!(sdi = sr_dev_inst_get(genericdmm_dev_insts, dev_index))) {
268 sr_err("genericdmm: sdi was NULL, device not found.");
269 return SR_ST_NOT_FOUND;
272 sr_dbg("genericdmm: Returning status: %d.", sdi->status);
277 static const int *hw_hwcap_get_all(void)
279 sr_spew("genericdmm: Returning list of device capabilities.");
284 static int parse_conn_vidpid(struct sr_dev_inst *sdi, const char *conn)
287 libusb_device **devlist;
288 struct libusb_device_descriptor des;
291 int vid, pid, found, err, i;
292 char *vidstr, *pidstr;
296 reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
297 if (g_regex_match(reg, conn, 0, &match)) {
299 if (!(vidstr = g_match_info_fetch(match, 0))) {
300 sr_err("failed to fetch VID from regex");
303 vid = strtoul(vidstr, NULL, 16);
306 sr_err("invalid VID");
311 if (!(pidstr = g_match_info_fetch(match, 0))) {
312 sr_err("failed to fetch PID from regex");
315 pid = strtoul(pidstr, NULL, 16);
318 sr_err("invalid PID");
322 /* Looks like a valid VID:PID, but is it connected? */
323 libusb_get_device_list(genericdmm_usb_context, &devlist);
324 for (i = 0; devlist[i]; i++) {
325 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
326 sr_err("genericdmm: failed to get device descriptor: %d", err);
330 if (des.idVendor == vid && des.idProduct == pid) {
332 ctx->usb = sr_usb_dev_inst_new(
333 libusb_get_bus_number(devlist[i]),
334 libusb_get_device_address(devlist[i]), NULL);
339 libusb_free_device_list(devlist, 1);
344 g_match_info_unref(match);
350 static int parse_conn_busaddr(struct sr_dev_inst *sdi, const char *conn)
353 libusb_device **devlist;
354 struct libusb_device_descriptor des;
357 int bus, addr, found, err, i;
358 char *busstr, *addrstr;
362 reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL);
363 if (g_regex_match(reg, conn, 0, &match)) {
365 if (!(busstr = g_match_info_fetch(match, 0))) {
366 sr_err("failed to fetch bus from regex");
369 bus = strtoul(busstr, NULL, 16);
372 sr_err("invalid bus");
376 /* Extract address. */
377 if (!(addrstr = g_match_info_fetch(match, 0))) {
378 sr_err("failed to fetch address from regex");
381 addr = strtoul(addrstr, NULL, 16);
384 sr_err("invalid address");
388 /* Looks like a valid bus/address, but is it connected? */
389 libusb_get_device_list(genericdmm_usb_context, &devlist);
390 for (i = 0; devlist[i]; i++) {
391 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
392 sr_err("genericdmm: failed to get device descriptor: %d", err);
396 if (libusb_get_bus_number(devlist[i]) == bus
397 && libusb_get_device_address(devlist[i]) == addr) {
399 ctx->usb = sr_usb_dev_inst_new(bus, addr, NULL);
404 libusb_free_device_list(devlist, 1);
409 g_match_info_unref(match);
415 static int parse_conn_serial(struct sr_dev_inst *sdi, const char *conn)
426 static int parse_conn(struct sr_dev_inst *sdi, const char *conn)
429 if (parse_conn_vidpid(sdi, conn))
432 if (parse_conn_busaddr(sdi, conn))
435 if (parse_conn_serial(sdi, conn))
438 sr_err("Invalid connection specification");
443 static int parse_serialcomm(struct sr_dev_inst *sdi, const char *conn)
447 /* set ctx->serial_* */
452 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
454 struct sr_dev_inst *sdi;
458 if (!(sdi = sr_dev_inst_get(genericdmm_dev_insts, dev_index))) {
459 sr_err("genericdmm: sdi was NULL.");
463 if (!(ctx = sdi->priv)) {
464 sr_err("genericdmm: sdi->priv was NULL.");
468 sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
471 case SR_HWCAP_LIMIT_MSEC:
472 if (*(const uint64_t *)value == 0) {
473 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
476 ctx->limit_msec = *(const uint64_t *)value;
477 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
480 case SR_HWCAP_LIMIT_SAMPLES:
481 ctx->limit_samples = *(const uint64_t *)value;
482 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
486 for (i = 0; dev_profiles[i].model; i++) {
487 if (!strcasecmp(dev_profiles[i].model, value)) {
488 ctx->profile = &dev_profiles[i];
489 /* Frontends access these fields directly, so we
490 * need to copy them over. */
491 sdi->vendor = g_strdup(dev_profiles[i].vendor);
492 sdi->model = g_strdup(dev_profiles[i].model);
493 /* This is the first time we actually know which
494 * DMM chip we're talking to, so let's init
495 * anything specific to it now */
496 if (ctx->profile->chip->init)
497 if (ctx->profile->chip->init(ctx) != SR_OK)
503 sr_err("unknown model %s", value);
508 if (parse_conn(sdi, value) != SR_OK)
511 case SR_HWCAP_SERIALCOMM:
512 if (parse_serialcomm(sdi, value) != SR_OK)
516 sr_err("genericdmm: Unknown capability: %d.", hwcap);
524 static int receive_data(int fd, int revents, void *cb_data)
526 struct sr_dev_inst *sdi;
529 if (!(sdi = cb_data))
532 if (!(ctx = sdi->priv))
535 if (revents != G_IO_IN) {
536 sr_err("genericdmm: No data?");
540 switch (ctx->profile->transport) {
541 case DMM_TRANSPORT_USBHID:
544 case DMM_TRANSPORT_SERIAL:
552 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
554 struct sr_datafeed_packet packet;
555 struct sr_datafeed_header header;
556 struct sr_datafeed_meta_analog meta;
557 struct sr_dev_inst *sdi;
560 if (!(sdi = sr_dev_inst_get(genericdmm_dev_insts, dev_index))) {
561 sr_err("genericdmm: sdi was NULL.");
565 if (!(ctx = sdi->priv)) {
566 sr_err("genericdmm: sdi->priv was NULL.");
570 sr_dbg("genericdmm: Starting acquisition.");
572 ctx->cb_data = cb_data;
574 /* Send header packet to the session bus. */
575 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
576 packet.type = SR_DF_HEADER;
577 packet.payload = (uint8_t *)&header;
578 header.feed_version = 1;
579 gettimeofday(&header.starttime, NULL);
580 sr_session_send(ctx->cb_data, &packet);
582 /* Send metadata about the SR_DF_ANALOG packets to come. */
583 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
584 packet.type = SR_DF_META_ANALOG;
585 packet.payload = &meta;
587 sr_session_send(ctx->cb_data, &packet);
589 /* Hook up a proxy handler to receive data from the device. */
590 switch (ctx->profile->transport) {
591 case DMM_TRANSPORT_USBHID:
592 /* TODO libusb FD setup */
594 case DMM_TRANSPORT_SERIAL:
595 /* TODO serial FD setup */
596 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
603 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
605 struct sr_datafeed_packet packet;
607 /* Avoid compiler warnings. */
610 sr_dbg("genericdmm: Stopping acquisition.");
612 /* Send end packet to the session bus. */
613 sr_dbg("genericdmm: Sending SR_DF_END.");
614 packet.type = SR_DF_END;
615 sr_session_send(cb_data, &packet);
620 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
621 .name = "genericdmm",
622 .longname = "Generic DMM",
625 .cleanup = hw_cleanup,
627 .dev_open = hw_dev_open,
628 .dev_close = hw_dev_close,
629 .dev_info_get = hw_dev_info_get,
630 .dev_status_get = hw_dev_status_get,
631 .hwcap_get_all = hw_hwcap_get_all,
632 .dev_config_set = hw_dev_config_set,
633 .dev_acquisition_start = hw_dev_acquisition_start,
634 .dev_acquisition_stop = hw_dev_acquisition_stop,