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 int hw_init(void)
193 if (libusb_init(&genericdmm_usb_context) != 0) {
194 sr_err("genericdmm: Failed to initialize USB.");
202 static int hw_scan(void)
204 struct sr_dev_inst *sdi;
208 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
209 sr_err("genericdmm: ctx malloc failed.");
213 devcnt = g_slist_length(genericdmm_dev_insts);
214 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, "Generic DMM",
216 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
220 genericdmm_dev_insts = g_slist_append(genericdmm_dev_insts, sdi);
222 /* Always initialized just one device instance. */
226 static int hw_dev_open(int dev_index)
228 struct sr_dev_inst *sdi;
231 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
232 sr_err("genericdmm: sdi was NULL.");
236 if (!(ctx = sdi->priv)) {
237 sr_err("genericdmm: sdi->priv was NULL.");
241 sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
243 switch (ctx->profile->transport) {
244 case DMM_TRANSPORT_USBHID:
247 case DMM_TRANSPORT_SERIAL:
248 /* TODO: O_NONBLOCK? */
249 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
250 if (ctx->serial->fd == -1) {
251 sr_err("genericdmm: Couldn't open serial port '%s'.",
255 // serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
258 sr_err("No transport set.");
264 static int hw_dev_close(int dev_index)
266 struct sr_dev_inst *sdi;
269 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
270 sr_err("genericdmm: %s: sdi was NULL.", __func__);
274 if (!(ctx = sdi->priv)) {
275 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
279 /* TODO: Check for != NULL. */
281 switch (ctx->profile->transport) {
282 case DMM_TRANSPORT_USBHID:
285 case DMM_TRANSPORT_SERIAL:
286 if (ctx->serial && ctx->serial->fd != -1) {
287 serial_close(ctx->serial->fd);
288 ctx->serial->fd = -1;
289 sdi->status = SR_ST_INACTIVE;
297 static int hw_cleanup(void)
300 struct sr_dev_inst *sdi;
303 /* Properly close and free all devices. */
304 for (l = gdi->instances; l; l = l->next) {
305 if (!(sdi = l->data)) {
306 /* Log error, but continue cleaning up the rest. */
307 sr_err("genericdmm: sdi was NULL, continuing.");
310 if (!(ctx = sdi->priv)) {
311 /* Log error, but continue cleaning up the rest. */
312 sr_err("genericdmm: sdi->priv was NULL, continuing.");
317 switch (ctx->profile->transport) {
318 case DMM_TRANSPORT_USBHID:
321 case DMM_TRANSPORT_SERIAL:
322 if (ctx->serial && ctx->serial->fd != -1)
323 serial_close(ctx->serial->fd);
324 sr_serial_dev_inst_free(ctx->serial);
329 sr_dev_inst_free(sdi);
332 g_slist_free(gdi->instances);
333 gdi->instances = NULL;
335 if (genericdmm_usb_context)
336 libusb_exit(genericdmm_usb_context);
341 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
343 struct sr_dev_inst *sdi;
347 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
348 sr_err("genericdmm: sdi was NULL.");
352 if (!(ctx = sdi->priv)) {
353 sr_err("genericdmm: sdi->priv was NULL.");
357 sr_spew("genericdmm: dev_index %d, dev_info_id %d.",
358 dev_index, dev_info_id);
360 switch (dev_info_id) {
363 sr_spew("genericdmm: Returning sdi.");
365 case SR_DI_NUM_PROBES:
366 info = GINT_TO_POINTER(1);
367 sr_spew("genericdmm: Returning number of probes: 1.");
369 case SR_DI_PROBE_NAMES:
371 sr_spew("genericdmm: Returning probenames.");
373 case SR_DI_CUR_SAMPLERATE:
374 /* TODO get rid of this */
376 sr_spew("genericdmm: Returning samplerate: 0.");
379 /* Unknown device info ID. */
380 sr_err("genericdmm: Unknown device info ID: %d.", dev_info_id);
388 static int hw_dev_status_get(int dev_index)
390 struct sr_dev_inst *sdi;
392 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
393 sr_err("genericdmm: sdi was NULL, device not found.");
394 return SR_ST_NOT_FOUND;
397 sr_dbg("genericdmm: Returning status: %d.", sdi->status);
402 static const int *hw_hwcap_get_all(void)
404 sr_spew("genericdmm: Returning list of device capabilities.");
409 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
411 struct sr_dev_inst *sdi;
414 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
415 sr_err("genericdmm: sdi was NULL.");
419 if (!(ctx = sdi->priv)) {
420 sr_err("genericdmm: sdi->priv was NULL.");
424 sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
427 case SR_HWCAP_LIMIT_MSEC:
428 if (*(const uint64_t *)value == 0) {
429 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
432 ctx->limit_msec = *(const uint64_t *)value;
433 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
436 case SR_HWCAP_LIMIT_SAMPLES:
437 ctx->limit_samples = *(const uint64_t *)value;
438 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
442 sr_err("genericdmm: Unknown capability: %d.", hwcap);
450 static int receive_data(int fd, int revents, void *cb_data)
452 struct sr_dev_inst *sdi;
455 if (!(sdi = cb_data))
458 if (!(ctx = sdi->priv))
461 if (revents != G_IO_IN) {
462 sr_err("genericdmm: No data?");
466 switch (ctx->profile->transport) {
467 case DMM_TRANSPORT_USBHID:
470 case DMM_TRANSPORT_SERIAL:
478 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
480 struct sr_datafeed_packet packet;
481 struct sr_datafeed_header header;
482 struct sr_datafeed_meta_analog meta;
483 struct sr_dev_inst *sdi;
486 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
487 sr_err("genericdmm: sdi was NULL.");
491 if (!(ctx = sdi->priv)) {
492 sr_err("genericdmm: sdi->priv was NULL.");
496 sr_dbg("genericdmm: Starting acquisition.");
498 ctx->cb_data = cb_data;
500 /* Send header packet to the session bus. */
501 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
502 packet.type = SR_DF_HEADER;
503 packet.payload = (uint8_t *)&header;
504 header.feed_version = 1;
505 gettimeofday(&header.starttime, NULL);
506 sr_session_send(ctx->cb_data, &packet);
508 /* Send metadata about the SR_DF_ANALOG packets to come. */
509 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
510 packet.type = SR_DF_META_ANALOG;
511 packet.payload = &meta;
513 sr_session_send(ctx->cb_data, &packet);
515 /* Hook up a proxy handler to receive data from the device. */
516 switch (ctx->profile->transport) {
517 case DMM_TRANSPORT_USBHID:
518 /* TODO libusb FD setup */
520 case DMM_TRANSPORT_SERIAL:
521 /* TODO serial FD setup */
522 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
529 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
531 struct sr_datafeed_packet packet;
533 /* Avoid compiler warnings. */
536 sr_dbg("genericdmm: Stopping acquisition.");
538 /* Send end packet to the session bus. */
539 sr_dbg("genericdmm: Sending SR_DF_END.");
540 packet.type = SR_DF_END;
541 sr_session_send(cb_data, &packet);
546 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
547 .name = "genericdmm",
548 .longname = "Generic DMM",
551 .cleanup = hw_cleanup,
553 .dev_open = hw_dev_open,
554 .dev_close = hw_dev_close,
555 .dev_info_get = hw_dev_info_get,
556 .dev_status_get = hw_dev_status_get,
557 .hwcap_get_all = hw_hwcap_get_all,
558 .dev_config_set = hw_dev_config_set,
559 .dev_acquisition_start = hw_dev_acquisition_start,
560 .dev_acquisition_stop = hw_dev_acquisition_stop,