]> sigrok.org Git - libsigrok.git/blame - hardware/genericdmm/api.c
chronovu-la8: adjust to multi-vid/pid patch
[libsigrok.git] / hardware / genericdmm / api.c
CommitLineData
ca3d84cc
BV
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include <stdlib.h>
23#include <string.h>
24#include <fcntl.h>
45c59c8b
BV
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
ca3d84cc
BV
27#include "genericdmm.h"
28
29
30extern SR_PRIV struct dmmchip dmmchip_fs9922;
31
7fc754a0
BV
32static struct sr_hwopt victor_70c_vidpid[] = {
33 { SR_HWOPT_CONN, "1244.d237" },
34 { 0, NULL }
35};
ca3d84cc
BV
36static struct dev_profile dev_profiles[] = {
37 { "victor-70c", "Victor", "70C", &dmmchip_fs9922,
7fc754a0
BV
38 DMM_TRANSPORT_USBHID, victor_70c_vidpid
39 },
40 { "mastech-va18b", "Mastech", "VA18B", NULL, DMM_TRANSPORT_SERIAL, NULL},
41 { NULL, NULL, NULL, NULL, 0, NULL }
ca3d84cc
BV
42};
43
f1a14ea7
BV
44static const int hwopts[] = {
45 SR_HWOPT_MODEL,
46 SR_HWOPT_CONN,
47 SR_HWOPT_SERIALCOMM,
48 0,
49};
50
ca3d84cc
BV
51static const int hwcaps[] = {
52 SR_HWCAP_MULTIMETER,
53 SR_HWCAP_LIMIT_SAMPLES,
54 SR_HWCAP_LIMIT_MSEC,
55 SR_HWCAP_CONTINUOUS,
ca3d84cc
BV
56 0,
57};
58
59static const char *probe_names[] = {
60 "Probe",
61 NULL,
62};
63
75337758
BV
64SR_PRIV struct sr_dev_driver genericdmm_driver_info;
65static struct sr_dev_driver *gdi = &genericdmm_driver_info;
66/* TODO need a way to keep this local to the static library */
ca3d84cc
BV
67SR_PRIV libusb_context *genericdmm_usb_context = NULL;
68
69
bbb40871
BV
70static GSList *connect_usb(const char *conn)
71{
72 struct sr_dev_inst *sdi;
73 struct context *ctx;
74 libusb_device **devlist;
75 struct libusb_device_descriptor des;
76 GSList *devices;
77 GRegex *reg;
78 GMatchInfo *match;
79 int vid, pid, bus, addr, devcnt, err, i;
80 char *mstr;
81
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)) {
85 /* Extract VID. */
86 if ((mstr = g_match_info_fetch(match, 1)))
87 vid = strtoul(mstr, NULL, 16);
88 g_free(mstr);
89
90 /* Extract PID. */
91 if ((mstr = g_match_info_fetch(match, 2)))
92 pid = strtoul(mstr, NULL, 16);
93 g_free(mstr);
94 } else {
95 g_match_info_unref(match);
96 g_regex_unref(reg);
97 reg = g_regex_new(DMM_CONN_USB_BUSADDR, 0, 0, NULL);
98 if (g_regex_match(reg, conn, 0, &match)) {
99 /* Extract bus. */
100 if ((mstr = g_match_info_fetch(match, 0)))
101 bus = strtoul(mstr, NULL, 16);
102 g_free(mstr);
103
104 /* Extract address. */
105 if ((mstr = g_match_info_fetch(match, 0)))
106 addr = strtoul(mstr, NULL, 16);
107 g_free(mstr);
108 }
109 }
110 g_match_info_unref(match);
111 g_regex_unref(reg);
112
113 if (vid + pid + bus + addr == 0)
114 return NULL;
115
116 if (bus > 64) {
117 sr_err("invalid bus");
118 return NULL;
119 }
120
121 if (addr > 127) {
122 sr_err("invalid address");
123 return NULL;
124 }
125
126 /* Looks like a valid USB device specification, but is it connected? */
127 devices = NULL;
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);
132 continue;
133 }
134
135 if (vid + pid && (des.idVendor != vid || des.idProduct != pid))
136 /* VID/PID specified, but no match. */
137 continue;
138
139 if (bus + addr && (
140 libusb_get_bus_number(devlist[i]) != bus
141 || libusb_get_device_address(devlist[i]) != addr))
142 /* Bus/address specified, but no match. */
143 continue;
144
145 /* Found one. */
146 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
147 sr_err("genericdmm: ctx malloc failed.");
148 return 0;
149 }
150
151 devcnt = g_slist_length(gdi->instances);
152 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
d6db79a4 153 NULL, NULL, NULL))) {
bbb40871
BV
154 sr_err("genericdmm: sr_dev_inst_new returned NULL.");
155 return NULL;
156 }
157 sdi->priv = ctx;
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);
162 }
163 libusb_free_device_list(devlist, 1);
164
165 return devices;
166}
167
168static GSList *connect_serial(const char *conn, const char *serialcomm)
169{
170 GSList *devices;
171
172 devices = NULL;
173
174 /* TODO */
175 sr_dbg("not yet implemented");
176
177 return devices;
178}
179
180GSList *genericdmm_connect(const char *conn, const char *serialcomm)
181{
182 GSList *devices;
183
184 if (serialcomm)
185 /* Must be a serial port. */
186 return connect_serial(conn, serialcomm);
187
188 if ((devices = connect_usb(conn)))
189 return devices;
190
191 return NULL;
192}
193
3a0fe402
BV
194static GSList *default_scan(GSList *options)
195{
196 GSList *l, *devices;
197 struct sr_hwopt *opt;
198 const char *conn, *serialcomm;
199
200 devices = NULL;
201 conn = serialcomm = NULL;
202 for (l = options; l; l = l->next) {
203 opt = l->data;
204 switch (opt->hwopt) {
205 case SR_HWOPT_CONN:
206 conn = opt->value;
207 break;
208 case SR_HWOPT_SERIALCOMM:
209 serialcomm = opt->value;
210 break;
211 }
212 }
213 if (conn)
214 devices = genericdmm_connect(conn, serialcomm);
215
216 return devices;
217}
218
40dda2c3 219static int hw_init(void)
ca3d84cc 220{
ca3d84cc 221
ca3d84cc
BV
222 if (libusb_init(&genericdmm_usb_context) != 0) {
223 sr_err("genericdmm: Failed to initialize USB.");
61136ea6 224 return SR_ERR;
ca3d84cc
BV
225 }
226
61136ea6
BV
227
228 return SR_OK;
229}
230
3a0fe402 231static GSList *hw_scan(GSList *options)
61136ea6 232{
3a0fe402
BV
233 GSList *l, *ldef, *defopts, *newopts, *devices;
234 struct sr_hwopt *opt, *defopt;
235 struct dev_profile *pr, *profile;
d6db79a4 236 struct sr_dev_inst *sdi;
3a0fe402
BV
237 const char *model;
238
239 /* Separate model from the options list. */
240 model = NULL;
241 newopts = NULL;
242 for (l = options; l; l = l->next) {
243 opt = l->data;
244 if (opt->hwopt == SR_HWOPT_MODEL)
245 model = opt->value;
246 else
247 /* New list with references to the original data. */
248 newopts = g_slist_append(newopts, opt);
249 }
250 if (!model) {
d6db79a4 251 /* This driver only works when a model is specified. */
3a0fe402
BV
252 return NULL;
253 }
61136ea6 254
3a0fe402
BV
255 /* Find a profile with this model name. */
256 profile = NULL;
257 for (pr = dev_profiles; pr->modelid; pr++) {
258 if (!strcmp(pr->modelid, model)) {
259 profile = pr;
260 break;
261 }
ca3d84cc 262 }
3a0fe402
BV
263 if (!profile) {
264 sr_err("Unknown model %s.", model);
265 return NULL;
266 }
267
268 /* Initialize the DMM chip driver. */
269 if (profile->chip->init)
270 profile->chip->init();
ca3d84cc 271
3a0fe402
BV
272 /* Convert the profile's default options list to a GSList. */
273 defopts = NULL;
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);
ca3d84cc 277 }
ca3d84cc 278
3a0fe402
BV
279 /* Options given as argument to this function override the
280 * profile's default options.
281 */
282 for (ldef = defopts; ldef; ldef = ldef->next) {
283 defopt = ldef->data;
284 for (l = newopts; l; l = l->next) {
285 opt = l->data;
286 if (opt->hwopt == defopt->hwopt) {
287 /* Override the default, and drop it from the
288 * options list.
289 */
290 ldef->data = l->data;
291 newopts = g_slist_remove(newopts, opt);
292 break;
293 }
294 }
295 }
296 /* Whatever is left in newopts wasn't in the default options. */
297 defopts = g_slist_concat(defopts, newopts);
298 g_slist_free(newopts);
299
300 if (profile->chip->scan)
301 /* The DMM chip driver wants to do its own scanning. */
302 devices = profile->chip->scan(defopts);
303 else
304 devices = default_scan(defopts);
305 g_slist_free(defopts);
306
307 if (devices) {
308 /* TODO: need to fix up sdi->index fields */
d6db79a4
BV
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.
313 */
314 sdi = l->data;
315 sdi->driver = gdi;
316 if (!sdi->vendor)
317 sdi->vendor = g_strdup(profile->vendor);
318 if (!sdi->model)
319 sdi->model = g_strdup(profile->model);
320 /* Add a copy of these new devices to the driver instances. */
3a0fe402 321 gdi->instances = g_slist_append(gdi->instances, l->data);
d6db79a4 322 }
3a0fe402
BV
323 }
324
325 return devices;
ca3d84cc
BV
326}
327
328static int hw_dev_open(int dev_index)
329{
330 struct sr_dev_inst *sdi;
331 struct context *ctx;
332
75337758 333 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
334 sr_err("genericdmm: sdi was NULL.");
335 return SR_ERR_BUG;
336 }
337
338 if (!(ctx = sdi->priv)) {
339 sr_err("genericdmm: sdi->priv was NULL.");
340 return SR_ERR_BUG;
341 }
342
343 sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
344
345 switch (ctx->profile->transport) {
346 case DMM_TRANSPORT_USBHID:
347 /* TODO */
348 break;
349 case DMM_TRANSPORT_SERIAL:
350 /* TODO: O_NONBLOCK? */
351 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
352 if (ctx->serial->fd == -1) {
353 sr_err("genericdmm: Couldn't open serial port '%s'.",
354 ctx->serial->port);
355 return SR_ERR;
356 }
357 // serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
358 break;
359 default:
360 sr_err("No transport set.");
361 }
362
363 return SR_OK;
364}
365
366static int hw_dev_close(int dev_index)
367{
368 struct sr_dev_inst *sdi;
369 struct context *ctx;
370
75337758 371 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
372 sr_err("genericdmm: %s: sdi was NULL.", __func__);
373 return SR_ERR_BUG;
374 }
375
376 if (!(ctx = sdi->priv)) {
377 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
378 return SR_ERR_BUG;
379 }
380
381 /* TODO: Check for != NULL. */
382
383 switch (ctx->profile->transport) {
384 case DMM_TRANSPORT_USBHID:
385 /* TODO */
386 break;
387 case DMM_TRANSPORT_SERIAL:
388 if (ctx->serial && ctx->serial->fd != -1) {
389 serial_close(ctx->serial->fd);
390 ctx->serial->fd = -1;
391 sdi->status = SR_ST_INACTIVE;
392 }
393 break;
394 }
395
396 return SR_OK;
397}
398
399static int hw_cleanup(void)
400{
401 GSList *l;
402 struct sr_dev_inst *sdi;
403 struct context *ctx;
404
405 /* Properly close and free all devices. */
75337758 406 for (l = gdi->instances; l; l = l->next) {
ca3d84cc
BV
407 if (!(sdi = l->data)) {
408 /* Log error, but continue cleaning up the rest. */
409 sr_err("genericdmm: sdi was NULL, continuing.");
410 continue;
411 }
412 if (!(ctx = sdi->priv)) {
413 /* Log error, but continue cleaning up the rest. */
414 sr_err("genericdmm: sdi->priv was NULL, continuing.");
415 continue;
416 }
417
418 if (ctx->profile) {
419 switch (ctx->profile->transport) {
420 case DMM_TRANSPORT_USBHID:
421 /* TODO */
422 break;
423 case DMM_TRANSPORT_SERIAL:
424 if (ctx->serial && ctx->serial->fd != -1)
425 serial_close(ctx->serial->fd);
426 sr_serial_dev_inst_free(ctx->serial);
427 break;
428 }
429 }
430
431 sr_dev_inst_free(sdi);
432 }
433
75337758
BV
434 g_slist_free(gdi->instances);
435 gdi->instances = NULL;
ca3d84cc
BV
436
437 if (genericdmm_usb_context)
438 libusb_exit(genericdmm_usb_context);
439
440 return SR_OK;
441}
442
6910bf6b
BV
443static int hw_info_get(int dev_info_id, const void **data,
444 const struct sr_dev_inst *sdi)
ca3d84cc 445{
ca3d84cc 446 struct context *ctx;
ca3d84cc
BV
447
448 switch (dev_info_id) {
449 case SR_DI_INST:
6910bf6b 450 *data = sdi;
ca3d84cc
BV
451 sr_spew("genericdmm: Returning sdi.");
452 break;
f1a14ea7
BV
453 case SR_DI_HWOPTS:
454 *data = hwopts;
455 break;
456 case SR_DI_HWCAPS:
457 *data = hwcaps;
458 break;
ca3d84cc 459 case SR_DI_NUM_PROBES:
6910bf6b 460 *data = GINT_TO_POINTER(1);
ca3d84cc
BV
461 break;
462 case SR_DI_PROBE_NAMES:
6910bf6b 463 *data = probe_names;
ca3d84cc
BV
464 break;
465 case SR_DI_CUR_SAMPLERATE:
466 /* TODO get rid of this */
6910bf6b 467 *data = NULL;
ca3d84cc
BV
468 break;
469 default:
470 /* Unknown device info ID. */
6910bf6b 471 return SR_ERR_ARG;
ca3d84cc
BV
472 }
473
6910bf6b 474 return SR_OK;
ca3d84cc
BV
475}
476
477static int hw_dev_status_get(int dev_index)
478{
479 struct sr_dev_inst *sdi;
480
75337758 481 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
482 sr_err("genericdmm: sdi was NULL, device not found.");
483 return SR_ST_NOT_FOUND;
484 }
485
486 sr_dbg("genericdmm: Returning status: %d.", sdi->status);
487
488 return sdi->status;
489}
490
ca3d84cc
BV
491static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
492{
493 struct sr_dev_inst *sdi;
494 struct context *ctx;
ca3d84cc 495
75337758 496 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
497 sr_err("genericdmm: sdi was NULL.");
498 return SR_ERR_BUG;
499 }
500
501 if (!(ctx = sdi->priv)) {
502 sr_err("genericdmm: sdi->priv was NULL.");
503 return SR_ERR_BUG;
504 }
505
506 sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
507
508 switch (hwcap) {
509 case SR_HWCAP_LIMIT_MSEC:
510 if (*(const uint64_t *)value == 0) {
511 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
512 return SR_ERR;
513 }
514 ctx->limit_msec = *(const uint64_t *)value;
515 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
516 ctx->limit_msec);
517 break;
518 case SR_HWCAP_LIMIT_SAMPLES:
519 ctx->limit_samples = *(const uint64_t *)value;
520 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
521 ctx->limit_samples);
522 break;
ca3d84cc
BV
523 default:
524 sr_err("genericdmm: Unknown capability: %d.", hwcap);
525 return SR_ERR;
526 break;
527 }
528
529 return SR_OK;
530}
531
532static int receive_data(int fd, int revents, void *cb_data)
533{
534 struct sr_dev_inst *sdi;
535 struct context *ctx;
536
537 if (!(sdi = cb_data))
538 return FALSE;
539
540 if (!(ctx = sdi->priv))
541 return FALSE;
542
543 if (revents != G_IO_IN) {
544 sr_err("genericdmm: No data?");
545 return FALSE;
546 }
547
548 switch (ctx->profile->transport) {
549 case DMM_TRANSPORT_USBHID:
550 /* TODO */
551 break;
552 case DMM_TRANSPORT_SERIAL:
553 /* TODO */
554 break;
555 }
556
557 return TRUE;
558}
559
560static int hw_dev_acquisition_start(int dev_index, void *cb_data)
561{
562 struct sr_datafeed_packet packet;
563 struct sr_datafeed_header header;
564 struct sr_datafeed_meta_analog meta;
565 struct sr_dev_inst *sdi;
566 struct context *ctx;
567
75337758 568 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
569 sr_err("genericdmm: sdi was NULL.");
570 return SR_ERR_BUG;
571 }
572
573 if (!(ctx = sdi->priv)) {
574 sr_err("genericdmm: sdi->priv was NULL.");
575 return SR_ERR_BUG;
576 }
577
578 sr_dbg("genericdmm: Starting acquisition.");
579
580 ctx->cb_data = cb_data;
581
582 /* Send header packet to the session bus. */
583 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
584 packet.type = SR_DF_HEADER;
585 packet.payload = (uint8_t *)&header;
586 header.feed_version = 1;
587 gettimeofday(&header.starttime, NULL);
588 sr_session_send(ctx->cb_data, &packet);
589
590 /* Send metadata about the SR_DF_ANALOG packets to come. */
591 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
592 packet.type = SR_DF_META_ANALOG;
593 packet.payload = &meta;
594 meta.num_probes = 1;
595 sr_session_send(ctx->cb_data, &packet);
596
597 /* Hook up a proxy handler to receive data from the device. */
598 switch (ctx->profile->transport) {
599 case DMM_TRANSPORT_USBHID:
600 /* TODO libusb FD setup */
601 break;
602 case DMM_TRANSPORT_SERIAL:
603 /* TODO serial FD setup */
604 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
605 break;
606 }
607
608 return SR_OK;
609}
610
611static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
612{
613 struct sr_datafeed_packet packet;
614
615 /* Avoid compiler warnings. */
616 (void)dev_index;
617
618 sr_dbg("genericdmm: Stopping acquisition.");
619
620 /* Send end packet to the session bus. */
621 sr_dbg("genericdmm: Sending SR_DF_END.");
622 packet.type = SR_DF_END;
623 sr_session_send(cb_data, &packet);
624
625 return SR_OK;
626}
627
628SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
629 .name = "genericdmm",
630 .longname = "Generic DMM",
631 .api_version = 1,
632 .init = hw_init,
633 .cleanup = hw_cleanup,
61136ea6 634 .scan = hw_scan,
ca3d84cc
BV
635 .dev_open = hw_dev_open,
636 .dev_close = hw_dev_close,
6910bf6b 637 .info_get = hw_info_get,
ca3d84cc 638 .dev_status_get = hw_dev_status_get,
ca3d84cc
BV
639 .dev_config_set = hw_dev_config_set,
640 .dev_acquisition_start = hw_dev_acquisition_start,
641 .dev_acquisition_stop = hw_dev_acquisition_stop,
75337758 642 .instances = NULL,
ca3d84cc 643};