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;
73 struct drv_context *drvc;
74 struct dev_context *devc;
75 libusb_device **devlist;
76 struct libusb_device_descriptor des;
80 int vid, pid, bus, addr, devcnt, err, i;
85 vid = pid = bus = addr = 0;
86 reg = g_regex_new(DMM_CONN_USB_VIDPID, 0, 0, NULL);
87 if (g_regex_match(reg, conn, 0, &match)) {
89 if ((mstr = g_match_info_fetch(match, 1)))
90 vid = strtoul(mstr, NULL, 16);
94 if ((mstr = g_match_info_fetch(match, 2)))
95 pid = strtoul(mstr, NULL, 16);
98 g_match_info_unref(match);
100 reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL);
101 if (g_regex_match(reg, conn, 0, &match)) {
103 if ((mstr = g_match_info_fetch(match, 0)))
104 bus = strtoul(mstr, NULL, 16);
107 /* Extract address. */
108 if ((mstr = g_match_info_fetch(match, 0)))
109 addr = strtoul(mstr, NULL, 16);
113 g_match_info_unref(match);
116 if (vid + pid + bus + addr == 0)
120 sr_err("invalid bus");
125 sr_err("invalid address");
129 /* Looks like a valid USB device specification, but is it connected? */
131 libusb_get_device_list(genericdmm_usb_context, &devlist);
132 for (i = 0; devlist[i]; i++) {
133 if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
134 sr_err("genericdmm: failed to get device descriptor: %d", err);
138 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
139 /* VID/PID specified, but no match. */
143 libusb_get_bus_number(devlist[i]) != bus
144 || libusb_get_device_address(devlist[i]) != addr))
145 /* Bus/address specified, but no match. */
149 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
150 sr_err("genericdmm: devc malloc failed.");
154 devcnt = g_slist_length(drvc->instances);
155 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
156 NULL, NULL, NULL))) {
157 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
161 devc->usb = sr_usb_dev_inst_new(
162 libusb_get_bus_number(devlist[i]),
163 libusb_get_device_address(devlist[i]), NULL);
164 devices = g_slist_append(devices, sdi);
166 libusb_free_device_list(devlist, 1);
171 static GSList *connect_serial(const char *conn, const char *serialcomm)
178 sr_dbg("not yet implemented");
183 GSList *genericdmm_connect(const char *conn, const char *serialcomm)
188 /* Must be a serial port. */
189 return connect_serial(conn, serialcomm);
191 if ((devices = connect_usb(conn)))
197 static GSList *default_scan(GSList *options)
200 struct sr_hwopt *opt;
201 const char *conn, *serialcomm;
204 conn = serialcomm = NULL;
205 for (l = options; l; l = l->next) {
207 switch (opt->hwopt) {
211 case SR_HWOPT_SERIALCOMM:
212 serialcomm = opt->value;
217 devices = genericdmm_connect(conn, serialcomm);
222 static int hw_init(void)
224 struct drv_context *drvc;
226 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
227 sr_err("genericdmm: driver context malloc failed.");
231 if (libusb_init(&genericdmm_usb_context) != 0) {
232 sr_err("genericdmm: Failed to initialize USB.");
241 static GSList *hw_scan(GSList *options)
243 GSList *l, *ldef, *defopts, *newopts, *devices;
244 struct sr_hwopt *opt, *defopt;
245 struct dev_profile *pr, *profile;
246 struct sr_dev_inst *sdi;
247 struct drv_context *drvc;
252 /* Separate model from the options list. */
255 for (l = options; l; l = l->next) {
257 if (opt->hwopt == SR_HWOPT_MODEL)
260 /* New list with references to the original data. */
261 newopts = g_slist_append(newopts, opt);
264 /* This driver only works when a model is specified. */
268 /* Find a profile with this model name. */
270 for (pr = dev_profiles; pr->modelid; pr++) {
271 if (!strcmp(pr->modelid, model)) {
277 sr_err("Unknown model %s.", model);
281 /* Initialize the DMM chip driver. */
282 if (profile->chip->init)
283 profile->chip->init();
285 /* Convert the profile's default options list to a GSList. */
287 for (opt = profile->defaults_opts; opt->hwopt; opt++) {
288 /* New list with references to const data in the profile. */
289 defopts = g_slist_append(defopts, opt);
292 /* Options given as argument to this function override the
293 * profile's default options.
295 for (ldef = defopts; ldef; ldef = ldef->next) {
297 for (l = newopts; l; l = l->next) {
299 if (opt->hwopt == defopt->hwopt) {
300 /* Override the default, and drop it from the
303 ldef->data = l->data;
304 newopts = g_slist_remove(newopts, opt);
309 /* Whatever is left in newopts wasn't in the default options. */
310 defopts = g_slist_concat(defopts, newopts);
311 g_slist_free(newopts);
313 if (profile->chip->scan)
314 /* The DMM chip driver wants to do its own scanning. */
315 devices = profile->chip->scan(defopts);
317 devices = default_scan(defopts);
318 g_slist_free(defopts);
321 /* TODO: need to fix up sdi->index fields */
322 for (l = devices; l; l = l->next) {
323 /* The default connection-based scanner doesn't really
324 * know about profiles, so it never filled in the vendor
325 * or model. Do that now.
330 sdi->vendor = g_strdup(profile->vendor);
332 sdi->model = g_strdup(profile->model);
333 /* Add a copy of these new devices to the driver instances. */
334 drvc->instances = g_slist_append(drvc->instances, l->data);
341 static int hw_dev_open(struct sr_dev_inst *sdi)
343 struct dev_context *devc;
345 if (!(devc = sdi->priv)) {
346 sr_err("genericdmm: sdi->priv was NULL.");
350 sr_dbg("genericdmm: Opening serial port '%s'.", devc->serial->port);
352 switch (devc->profile->transport) {
353 case DMM_TRANSPORT_USBHID:
356 case DMM_TRANSPORT_SERIAL:
357 /* TODO: O_NONBLOCK? */
358 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
359 if (devc->serial->fd == -1) {
360 sr_err("genericdmm: Couldn't open serial port '%s'.",
364 // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
367 sr_err("No transport set.");
373 static int hw_dev_close(struct sr_dev_inst *sdi)
375 struct dev_context *devc;
377 if (!(devc = sdi->priv)) {
378 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
382 switch (devc->profile->transport) {
383 case DMM_TRANSPORT_USBHID:
386 case DMM_TRANSPORT_SERIAL:
387 if (devc->serial && devc->serial->fd != -1) {
388 serial_close(devc->serial->fd);
389 devc->serial->fd = -1;
390 sdi->status = SR_ST_INACTIVE;
398 static int hw_cleanup(void)
401 struct sr_dev_inst *sdi;
402 struct dev_context *devc;
403 struct drv_context *drvc;
405 if (!(drvc = gdi->priv))
408 /* Properly close and free all devices. */
409 for (l = drvc->instances; l; l = l->next) {
410 if (!(sdi = l->data)) {
411 /* Log error, but continue cleaning up the rest. */
412 sr_err("genericdmm: sdi was NULL, continuing.");
415 if (!(devc = sdi->priv)) {
416 /* Log error, but continue cleaning up the rest. */
417 sr_err("genericdmm: sdi->priv was NULL, continuing.");
422 switch (devc->profile->transport) {
423 case DMM_TRANSPORT_USBHID:
426 case DMM_TRANSPORT_SERIAL:
427 if (devc->serial && devc->serial->fd != -1)
428 serial_close(devc->serial->fd);
429 sr_serial_dev_inst_free(devc->serial);
434 sr_dev_inst_free(sdi);
437 g_slist_free(drvc->instances);
438 drvc->instances = NULL;
440 if (genericdmm_usb_context)
441 libusb_exit(genericdmm_usb_context);
446 static int hw_info_get(int info_id, const void **data,
447 const struct sr_dev_inst *sdi)
449 struct dev_context *devc;
461 case SR_DI_NUM_PROBES:
462 *data = GINT_TO_POINTER(1);
464 case SR_DI_PROBE_NAMES:
467 case SR_DI_CUR_SAMPLERATE:
468 /* TODO get rid of this */
472 /* Unknown device info ID. */
479 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
482 struct dev_context *devc;
484 if (!(devc = sdi->priv)) {
485 sr_err("genericdmm: sdi->priv was NULL.");
490 case SR_HWCAP_LIMIT_MSEC:
491 if (*(const uint64_t *)value == 0) {
492 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
495 devc->limit_msec = *(const uint64_t *)value;
496 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
499 case SR_HWCAP_LIMIT_SAMPLES:
500 devc->limit_samples = *(const uint64_t *)value;
501 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
502 devc->limit_samples);
505 sr_err("genericdmm: Unknown capability: %d.", hwcap);
513 static int receive_data(int fd, int revents, void *cb_data)
515 struct sr_dev_inst *sdi;
516 struct dev_context *devc;
518 if (!(sdi = cb_data))
521 if (!(devc = sdi->priv))
524 if (revents != G_IO_IN) {
525 sr_err("genericdmm: No data?");
529 switch (devc->profile->transport) {
530 case DMM_TRANSPORT_USBHID:
533 case DMM_TRANSPORT_SERIAL:
541 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
544 struct sr_datafeed_packet packet;
545 struct sr_datafeed_header header;
546 struct sr_datafeed_meta_analog meta;
547 struct dev_context *devc;
549 if (!(devc = sdi->priv)) {
550 sr_err("genericdmm: sdi->priv was NULL.");
554 sr_dbg("genericdmm: Starting acquisition.");
556 devc->cb_data = cb_data;
558 /* Send header packet to the session bus. */
559 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
560 packet.type = SR_DF_HEADER;
561 packet.payload = (uint8_t *)&header;
562 header.feed_version = 1;
563 gettimeofday(&header.starttime, NULL);
564 sr_session_send(devc->cb_data, &packet);
566 /* Send metadata about the SR_DF_ANALOG packets to come. */
567 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
568 packet.type = SR_DF_META_ANALOG;
569 packet.payload = &meta;
571 sr_session_send(devc->cb_data, &packet);
573 /* Hook up a proxy handler to receive data from the device. */
574 switch (devc->profile->transport) {
575 case DMM_TRANSPORT_USBHID:
576 /* TODO libusb FD setup */
578 case DMM_TRANSPORT_SERIAL:
579 /* TODO serial FD setup */
580 // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
587 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
590 struct sr_datafeed_packet packet;
592 /* Avoid compiler warnings. */
595 sr_dbg("genericdmm: Stopping acquisition.");
597 /* Send end packet to the session bus. */
598 sr_dbg("genericdmm: Sending SR_DF_END.");
599 packet.type = SR_DF_END;
600 sr_session_send(cb_data, &packet);
605 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
606 .name = "genericdmm",
607 .longname = "Generic DMM",
610 .cleanup = hw_cleanup,
612 .dev_open = hw_dev_open,
613 .dev_close = hw_dev_close,
614 .info_get = hw_info_get,
615 .dev_config_set = hw_dev_config_set,
616 .dev_acquisition_start = hw_dev_acquisition_start,
617 .dev_acquisition_stop = hw_dev_acquisition_stop,