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 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 SR_PRIV libusb_context *genericdmm_usb_context = NULL;
70 static GSList *connect_usb(const char *conn)
72 struct sr_dev_inst *sdi;
74 libusb_device **devlist;
75 struct libusb_device_descriptor des;
79 int vid, pid, bus, addr, devcnt, err, i;
82 vid = pid = bus = addr = 0;
83 reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
84 if (g_regex_match(reg, conn, 0, &match)) {
86 if ((mstr = g_match_info_fetch(match, 1)))
87 vid = strtoul(mstr, NULL, 16);
91 if ((mstr = g_match_info_fetch(match, 2)))
92 pid = strtoul(mstr, NULL, 16);
95 g_match_info_unref(match);
97 reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL);
98 if (g_regex_match(reg, conn, 0, &match)) {
100 if ((mstr = g_match_info_fetch(match, 0)))
101 bus = strtoul(mstr, NULL, 16);
104 /* Extract address. */
105 if ((mstr = g_match_info_fetch(match, 0)))
106 addr = strtoul(mstr, NULL, 16);
110 g_match_info_unref(match);
113 if (vid + pid + bus + addr == 0)
117 sr_err("invalid bus");
122 sr_err("invalid address");
126 /* Looks like a valid USB device specification, but is it connected? */
128 libusb_get_device_list(genericdmm_usb_context, &devlist);
129 for (i = 0; devlist[i]; i++) {
130 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
131 sr_err("genericdmm: failed to get device descriptor: %d", err);
135 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
136 /* VID/PID specified, but no match. */
140 libusb_get_bus_number(devlist[i]) != bus
141 || libusb_get_device_address(devlist[i]) != addr))
142 /* Bus/address specified, but no match. */
146 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
147 sr_err("genericdmm: ctx malloc failed.");
151 devcnt = g_slist_length(gdi->instances);
152 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
153 NULL, NULL, NULL))) {
154 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
158 ctx->usb = sr_usb_dev_inst_new(
159 libusb_get_bus_number(devlist[i]),
160 libusb_get_device_address(devlist[i]), NULL);
161 devices = g_slist_append(devices, sdi);
163 libusb_free_device_list(devlist, 1);
168 static GSList *connect_serial(const char *conn, const char *serialcomm)
175 sr_dbg("not yet implemented");
180 GSList *genericdmm_connect(const char *conn, const char *serialcomm)
185 /* Must be a serial port. */
186 return connect_serial(conn, serialcomm);
188 if ((devices = connect_usb(conn)))
194 static GSList *default_scan(GSList *options)
197 struct sr_hwopt *opt;
198 const char *conn, *serialcomm;
201 conn = serialcomm = NULL;
202 for (l = options; l; l = l->next) {
204 switch (opt->hwopt) {
208 case SR_HWOPT_SERIALCOMM:
209 serialcomm = opt->value;
214 devices = genericdmm_connect(conn, serialcomm);
219 static int hw_init(void)
222 if (libusb_init(&genericdmm_usb_context) != 0) {
223 sr_err("genericdmm: Failed to initialize USB.");
231 static GSList *hw_scan(GSList *options)
233 GSList *l, *ldef, *defopts, *newopts, *devices;
234 struct sr_hwopt *opt, *defopt;
235 struct dev_profile *pr, *profile;
236 struct sr_dev_inst *sdi;
239 /* Separate model from the options list. */
242 for (l = options; l; l = l->next) {
244 if (opt->hwopt == SR_HWOPT_MODEL)
247 /* New list with references to the original data. */
248 newopts = g_slist_append(newopts, opt);
251 /* This driver only works when a model is specified. */
255 /* Find a profile with this model name. */
257 for (pr = dev_profiles; pr->modelid; pr++) {
258 if (!strcmp(pr->modelid, model)) {
264 sr_err("Unknown model %s.", model);
268 /* Initialize the DMM chip driver. */
269 if (profile->chip->init)
270 profile->chip->init();
272 /* Convert the profile's default options list to a GSList. */
274 for (opt = profile->defaults_opts; opt->hwopt; opt++) {
275 /* New list with references to const data in the profile. */
276 defopts = g_slist_append(defopts, opt);
279 /* Options given as argument to this function override the
280 * profile's default options.
282 for (ldef = defopts; ldef; ldef = ldef->next) {
284 for (l = newopts; l; l = l->next) {
286 if (opt->hwopt == defopt->hwopt) {
287 /* Override the default, and drop it from the
290 ldef->data = l->data;
291 newopts = g_slist_remove(newopts, opt);
296 /* Whatever is left in newopts wasn't in the default options. */
297 defopts = g_slist_concat(defopts, newopts);
298 g_slist_free(newopts);
300 if (profile->chip->scan)
301 /* The DMM chip driver wants to do its own scanning. */
302 devices = profile->chip->scan(defopts);
304 devices = default_scan(defopts);
305 g_slist_free(defopts);
308 /* TODO: need to fix up sdi->index fields */
309 for (l = devices; l; l = l->next) {
310 /* The default connection-based scanner doesn't really
311 * know about profiles, so it never filled in the vendor
312 * or model. Do that now.
317 sdi->vendor = g_strdup(profile->vendor);
319 sdi->model = g_strdup(profile->model);
320 /* Add a copy of these new devices to the driver instances. */
321 gdi->instances = g_slist_append(gdi->instances, l->data);
328 static int hw_dev_open(struct sr_dev_inst *sdi)
332 if (!(ctx = sdi->priv)) {
333 sr_err("genericdmm: sdi->priv was NULL.");
337 sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
339 switch (ctx->profile->transport) {
340 case DMM_TRANSPORT_USBHID:
343 case DMM_TRANSPORT_SERIAL:
344 /* TODO: O_NONBLOCK? */
345 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
346 if (ctx->serial->fd == -1) {
347 sr_err("genericdmm: Couldn't open serial port '%s'.",
351 // serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
354 sr_err("No transport set.");
360 static int hw_dev_close(struct sr_dev_inst *sdi)
364 if (!(ctx = sdi->priv)) {
365 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
369 switch (ctx->profile->transport) {
370 case DMM_TRANSPORT_USBHID:
373 case DMM_TRANSPORT_SERIAL:
374 if (ctx->serial && ctx->serial->fd != -1) {
375 serial_close(ctx->serial->fd);
376 ctx->serial->fd = -1;
377 sdi->status = SR_ST_INACTIVE;
385 static int hw_cleanup(void)
388 struct sr_dev_inst *sdi;
391 /* Properly close and free all devices. */
392 for (l = gdi->instances; l; l = l->next) {
393 if (!(sdi = l->data)) {
394 /* Log error, but continue cleaning up the rest. */
395 sr_err("genericdmm: sdi was NULL, continuing.");
398 if (!(ctx = sdi->priv)) {
399 /* Log error, but continue cleaning up the rest. */
400 sr_err("genericdmm: sdi->priv was NULL, continuing.");
405 switch (ctx->profile->transport) {
406 case DMM_TRANSPORT_USBHID:
409 case DMM_TRANSPORT_SERIAL:
410 if (ctx->serial && ctx->serial->fd != -1)
411 serial_close(ctx->serial->fd);
412 sr_serial_dev_inst_free(ctx->serial);
417 sr_dev_inst_free(sdi);
420 g_slist_free(gdi->instances);
421 gdi->instances = NULL;
423 if (genericdmm_usb_context)
424 libusb_exit(genericdmm_usb_context);
429 static int hw_info_get(int dev_info_id, const void **data,
430 const struct sr_dev_inst *sdi)
434 switch (dev_info_id) {
437 sr_spew("genericdmm: Returning sdi.");
445 case SR_DI_NUM_PROBES:
446 *data = GINT_TO_POINTER(1);
448 case SR_DI_PROBE_NAMES:
451 case SR_DI_CUR_SAMPLERATE:
452 /* TODO get rid of this */
456 /* Unknown device info ID. */
463 static int hw_dev_status_get(int dev_index)
465 struct sr_dev_inst *sdi;
467 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
468 sr_err("genericdmm: sdi was NULL, device not found.");
469 return SR_ST_NOT_FOUND;
472 sr_dbg("genericdmm: Returning status: %d.", sdi->status);
477 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
482 if (!(ctx = sdi->priv)) {
483 sr_err("genericdmm: sdi->priv was NULL.");
488 case SR_HWCAP_LIMIT_MSEC:
489 if (*(const uint64_t *)value == 0) {
490 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
493 ctx->limit_msec = *(const uint64_t *)value;
494 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
497 case SR_HWCAP_LIMIT_SAMPLES:
498 ctx->limit_samples = *(const uint64_t *)value;
499 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
503 sr_err("genericdmm: Unknown capability: %d.", hwcap);
511 static int receive_data(int fd, int revents, void *cb_data)
513 struct sr_dev_inst *sdi;
516 if (!(sdi = cb_data))
519 if (!(ctx = sdi->priv))
522 if (revents != G_IO_IN) {
523 sr_err("genericdmm: No data?");
527 switch (ctx->profile->transport) {
528 case DMM_TRANSPORT_USBHID:
531 case DMM_TRANSPORT_SERIAL:
539 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
542 struct sr_datafeed_packet packet;
543 struct sr_datafeed_header header;
544 struct sr_datafeed_meta_analog meta;
547 if (!(ctx = sdi->priv)) {
548 sr_err("genericdmm: sdi->priv was NULL.");
552 sr_dbg("genericdmm: Starting acquisition.");
554 ctx->cb_data = cb_data;
556 /* Send header packet to the session bus. */
557 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
558 packet.type = SR_DF_HEADER;
559 packet.payload = (uint8_t *)&header;
560 header.feed_version = 1;
561 gettimeofday(&header.starttime, NULL);
562 sr_session_send(ctx->cb_data, &packet);
564 /* Send metadata about the SR_DF_ANALOG packets to come. */
565 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
566 packet.type = SR_DF_META_ANALOG;
567 packet.payload = &meta;
569 sr_session_send(ctx->cb_data, &packet);
571 /* Hook up a proxy handler to receive data from the device. */
572 switch (ctx->profile->transport) {
573 case DMM_TRANSPORT_USBHID:
574 /* TODO libusb FD setup */
576 case DMM_TRANSPORT_SERIAL:
577 /* TODO serial FD setup */
578 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
585 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
588 struct sr_datafeed_packet packet;
590 /* Avoid compiler warnings. */
593 sr_dbg("genericdmm: Stopping acquisition.");
595 /* Send end packet to the session bus. */
596 sr_dbg("genericdmm: Sending SR_DF_END.");
597 packet.type = SR_DF_END;
598 sr_session_send(cb_data, &packet);
603 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
604 .name = "genericdmm",
605 .longname = "Generic DMM",
608 .cleanup = hw_cleanup,
610 .dev_open = hw_dev_open,
611 .dev_close = hw_dev_close,
612 .info_get = hw_info_get,
613 .dev_status_get = hw_dev_status_get,
614 .dev_config_set = hw_dev_config_set,
615 .dev_acquisition_start = hw_dev_acquisition_start,
616 .dev_acquisition_stop = hw_dev_acquisition_stop,