]> sigrok.org Git - libsigrok.git/blame - hardware/genericdmm/api.c
genericdmm: don't use deprecated hwcap_get_all() driver API call
[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,
153 "Generic DMM", NULL, NULL))) {
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;
236 const char *model;
237
238 /* Separate model from the options list. */
239 model = NULL;
240 newopts = NULL;
241 for (l = options; l; l = l->next) {
242 opt = l->data;
243 if (opt->hwopt == SR_HWOPT_MODEL)
244 model = opt->value;
245 else
246 /* New list with references to the original data. */
247 newopts = g_slist_append(newopts, opt);
248 }
249 if (!model) {
250 sr_err("Need a model to scan for.");
251 return NULL;
252 }
61136ea6 253
3a0fe402
BV
254 /* Find a profile with this model name. */
255 profile = NULL;
256 for (pr = dev_profiles; pr->modelid; pr++) {
257 if (!strcmp(pr->modelid, model)) {
258 profile = pr;
259 break;
260 }
ca3d84cc 261 }
3a0fe402
BV
262 if (!profile) {
263 sr_err("Unknown model %s.", model);
264 return NULL;
265 }
266
267 /* Initialize the DMM chip driver. */
268 if (profile->chip->init)
269 profile->chip->init();
ca3d84cc 270
3a0fe402
BV
271 /* Convert the profile's default options list to a GSList. */
272 defopts = NULL;
273 for (opt = profile->defaults_opts; opt->hwopt; opt++) {
274 /* New list with references to const data in the profile. */
275 defopts = g_slist_append(defopts, opt);
ca3d84cc 276 }
ca3d84cc 277
3a0fe402
BV
278 /* Options given as argument to this function override the
279 * profile's default options.
280 */
281 for (ldef = defopts; ldef; ldef = ldef->next) {
282 defopt = ldef->data;
283 for (l = newopts; l; l = l->next) {
284 opt = l->data;
285 if (opt->hwopt == defopt->hwopt) {
286 /* Override the default, and drop it from the
287 * options list.
288 */
289 ldef->data = l->data;
290 newopts = g_slist_remove(newopts, opt);
291 break;
292 }
293 }
294 }
295 /* Whatever is left in newopts wasn't in the default options. */
296 defopts = g_slist_concat(defopts, newopts);
297 g_slist_free(newopts);
298
299 if (profile->chip->scan)
300 /* The DMM chip driver wants to do its own scanning. */
301 devices = profile->chip->scan(defopts);
302 else
303 devices = default_scan(defopts);
304 g_slist_free(defopts);
305
306 if (devices) {
307 /* TODO: need to fix up sdi->index fields */
308 /* Add a copy of these new devices to the driver instances. */
309 for (l = devices; l; l = l->next)
310 gdi->instances = g_slist_append(gdi->instances, l->data);
311 }
312
313 return devices;
ca3d84cc
BV
314}
315
316static int hw_dev_open(int dev_index)
317{
318 struct sr_dev_inst *sdi;
319 struct context *ctx;
320
75337758 321 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
322 sr_err("genericdmm: sdi was NULL.");
323 return SR_ERR_BUG;
324 }
325
326 if (!(ctx = sdi->priv)) {
327 sr_err("genericdmm: sdi->priv was NULL.");
328 return SR_ERR_BUG;
329 }
330
331 sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
332
333 switch (ctx->profile->transport) {
334 case DMM_TRANSPORT_USBHID:
335 /* TODO */
336 break;
337 case DMM_TRANSPORT_SERIAL:
338 /* TODO: O_NONBLOCK? */
339 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
340 if (ctx->serial->fd == -1) {
341 sr_err("genericdmm: Couldn't open serial port '%s'.",
342 ctx->serial->port);
343 return SR_ERR;
344 }
345 // serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
346 break;
347 default:
348 sr_err("No transport set.");
349 }
350
351 return SR_OK;
352}
353
354static int hw_dev_close(int dev_index)
355{
356 struct sr_dev_inst *sdi;
357 struct context *ctx;
358
75337758 359 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
360 sr_err("genericdmm: %s: sdi was NULL.", __func__);
361 return SR_ERR_BUG;
362 }
363
364 if (!(ctx = sdi->priv)) {
365 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
366 return SR_ERR_BUG;
367 }
368
369 /* TODO: Check for != NULL. */
370
371 switch (ctx->profile->transport) {
372 case DMM_TRANSPORT_USBHID:
373 /* TODO */
374 break;
375 case DMM_TRANSPORT_SERIAL:
376 if (ctx->serial && ctx->serial->fd != -1) {
377 serial_close(ctx->serial->fd);
378 ctx->serial->fd = -1;
379 sdi->status = SR_ST_INACTIVE;
380 }
381 break;
382 }
383
384 return SR_OK;
385}
386
387static int hw_cleanup(void)
388{
389 GSList *l;
390 struct sr_dev_inst *sdi;
391 struct context *ctx;
392
393 /* Properly close and free all devices. */
75337758 394 for (l = gdi->instances; l; l = l->next) {
ca3d84cc
BV
395 if (!(sdi = l->data)) {
396 /* Log error, but continue cleaning up the rest. */
397 sr_err("genericdmm: sdi was NULL, continuing.");
398 continue;
399 }
400 if (!(ctx = sdi->priv)) {
401 /* Log error, but continue cleaning up the rest. */
402 sr_err("genericdmm: sdi->priv was NULL, continuing.");
403 continue;
404 }
405
406 if (ctx->profile) {
407 switch (ctx->profile->transport) {
408 case DMM_TRANSPORT_USBHID:
409 /* TODO */
410 break;
411 case DMM_TRANSPORT_SERIAL:
412 if (ctx->serial && ctx->serial->fd != -1)
413 serial_close(ctx->serial->fd);
414 sr_serial_dev_inst_free(ctx->serial);
415 break;
416 }
417 }
418
419 sr_dev_inst_free(sdi);
420 }
421
75337758
BV
422 g_slist_free(gdi->instances);
423 gdi->instances = NULL;
ca3d84cc
BV
424
425 if (genericdmm_usb_context)
426 libusb_exit(genericdmm_usb_context);
427
428 return SR_OK;
429}
430
6910bf6b
BV
431static int hw_info_get(int dev_info_id, const void **data,
432 const struct sr_dev_inst *sdi)
ca3d84cc 433{
ca3d84cc 434 struct context *ctx;
ca3d84cc
BV
435
436 switch (dev_info_id) {
437 case SR_DI_INST:
6910bf6b 438 *data = sdi;
ca3d84cc
BV
439 sr_spew("genericdmm: Returning sdi.");
440 break;
f1a14ea7
BV
441 case SR_DI_HWOPTS:
442 *data = hwopts;
443 break;
444 case SR_DI_HWCAPS:
445 *data = hwcaps;
446 break;
ca3d84cc 447 case SR_DI_NUM_PROBES:
6910bf6b 448 *data = GINT_TO_POINTER(1);
ca3d84cc
BV
449 break;
450 case SR_DI_PROBE_NAMES:
6910bf6b 451 *data = probe_names;
ca3d84cc
BV
452 break;
453 case SR_DI_CUR_SAMPLERATE:
454 /* TODO get rid of this */
6910bf6b 455 *data = NULL;
ca3d84cc
BV
456 break;
457 default:
458 /* Unknown device info ID. */
6910bf6b 459 return SR_ERR_ARG;
ca3d84cc
BV
460 }
461
6910bf6b 462 return SR_OK;
ca3d84cc
BV
463}
464
465static int hw_dev_status_get(int dev_index)
466{
467 struct sr_dev_inst *sdi;
468
75337758 469 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
470 sr_err("genericdmm: sdi was NULL, device not found.");
471 return SR_ST_NOT_FOUND;
472 }
473
474 sr_dbg("genericdmm: Returning status: %d.", sdi->status);
475
476 return sdi->status;
477}
478
ca3d84cc
BV
479static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
480{
481 struct sr_dev_inst *sdi;
482 struct context *ctx;
ca3d84cc 483
75337758 484 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
485 sr_err("genericdmm: sdi was NULL.");
486 return SR_ERR_BUG;
487 }
488
489 if (!(ctx = sdi->priv)) {
490 sr_err("genericdmm: sdi->priv was NULL.");
491 return SR_ERR_BUG;
492 }
493
494 sr_spew("genericdmm: dev_index %d, hwcap %d.", dev_index, hwcap);
495
496 switch (hwcap) {
497 case SR_HWCAP_LIMIT_MSEC:
498 if (*(const uint64_t *)value == 0) {
499 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
500 return SR_ERR;
501 }
502 ctx->limit_msec = *(const uint64_t *)value;
503 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
504 ctx->limit_msec);
505 break;
506 case SR_HWCAP_LIMIT_SAMPLES:
507 ctx->limit_samples = *(const uint64_t *)value;
508 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
509 ctx->limit_samples);
510 break;
ca3d84cc
BV
511 default:
512 sr_err("genericdmm: Unknown capability: %d.", hwcap);
513 return SR_ERR;
514 break;
515 }
516
517 return SR_OK;
518}
519
520static int receive_data(int fd, int revents, void *cb_data)
521{
522 struct sr_dev_inst *sdi;
523 struct context *ctx;
524
525 if (!(sdi = cb_data))
526 return FALSE;
527
528 if (!(ctx = sdi->priv))
529 return FALSE;
530
531 if (revents != G_IO_IN) {
532 sr_err("genericdmm: No data?");
533 return FALSE;
534 }
535
536 switch (ctx->profile->transport) {
537 case DMM_TRANSPORT_USBHID:
538 /* TODO */
539 break;
540 case DMM_TRANSPORT_SERIAL:
541 /* TODO */
542 break;
543 }
544
545 return TRUE;
546}
547
548static int hw_dev_acquisition_start(int dev_index, void *cb_data)
549{
550 struct sr_datafeed_packet packet;
551 struct sr_datafeed_header header;
552 struct sr_datafeed_meta_analog meta;
553 struct sr_dev_inst *sdi;
554 struct context *ctx;
555
75337758 556 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
557 sr_err("genericdmm: sdi was NULL.");
558 return SR_ERR_BUG;
559 }
560
561 if (!(ctx = sdi->priv)) {
562 sr_err("genericdmm: sdi->priv was NULL.");
563 return SR_ERR_BUG;
564 }
565
566 sr_dbg("genericdmm: Starting acquisition.");
567
568 ctx->cb_data = cb_data;
569
570 /* Send header packet to the session bus. */
571 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
572 packet.type = SR_DF_HEADER;
573 packet.payload = (uint8_t *)&header;
574 header.feed_version = 1;
575 gettimeofday(&header.starttime, NULL);
576 sr_session_send(ctx->cb_data, &packet);
577
578 /* Send metadata about the SR_DF_ANALOG packets to come. */
579 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
580 packet.type = SR_DF_META_ANALOG;
581 packet.payload = &meta;
582 meta.num_probes = 1;
583 sr_session_send(ctx->cb_data, &packet);
584
585 /* Hook up a proxy handler to receive data from the device. */
586 switch (ctx->profile->transport) {
587 case DMM_TRANSPORT_USBHID:
588 /* TODO libusb FD setup */
589 break;
590 case DMM_TRANSPORT_SERIAL:
591 /* TODO serial FD setup */
592 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
593 break;
594 }
595
596 return SR_OK;
597}
598
599static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
600{
601 struct sr_datafeed_packet packet;
602
603 /* Avoid compiler warnings. */
604 (void)dev_index;
605
606 sr_dbg("genericdmm: Stopping acquisition.");
607
608 /* Send end packet to the session bus. */
609 sr_dbg("genericdmm: Sending SR_DF_END.");
610 packet.type = SR_DF_END;
611 sr_session_send(cb_data, &packet);
612
613 return SR_OK;
614}
615
616SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
617 .name = "genericdmm",
618 .longname = "Generic DMM",
619 .api_version = 1,
620 .init = hw_init,
621 .cleanup = hw_cleanup,
61136ea6 622 .scan = hw_scan,
ca3d84cc
BV
623 .dev_open = hw_dev_open,
624 .dev_close = hw_dev_close,
6910bf6b 625 .info_get = hw_info_get,
ca3d84cc 626 .dev_status_get = hw_dev_status_get,
ca3d84cc
BV
627 .dev_config_set = hw_dev_config_set,
628 .dev_acquisition_start = hw_dev_acquisition_start,
629 .dev_acquisition_stop = hw_dev_acquisition_stop,
75337758 630 .instances = NULL,
ca3d84cc 631};