]> sigrok.org Git - libsigrok.git/blame - hardware/genericdmm/api.c
sr/drivers: change driver dev_open/dev_close calls to use sdi
[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
25a0f108 328static int hw_dev_open(struct sr_dev_inst *sdi)
ca3d84cc 329{
ca3d84cc
BV
330 struct context *ctx;
331
ca3d84cc
BV
332 if (!(ctx = sdi->priv)) {
333 sr_err("genericdmm: sdi->priv was NULL.");
334 return SR_ERR_BUG;
335 }
336
337 sr_dbg("genericdmm: Opening serial port '%s'.", ctx->serial->port);
338
339 switch (ctx->profile->transport) {
340 case DMM_TRANSPORT_USBHID:
341 /* TODO */
342 break;
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'.",
348 ctx->serial->port);
349 return SR_ERR;
350 }
351 // serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2);
352 break;
353 default:
354 sr_err("No transport set.");
355 }
356
357 return SR_OK;
358}
359
25a0f108 360static int hw_dev_close(struct sr_dev_inst *sdi)
ca3d84cc 361{
ca3d84cc
BV
362 struct context *ctx;
363
ca3d84cc
BV
364 if (!(ctx = sdi->priv)) {
365 sr_err("genericdmm: %s: sdi->priv was NULL.", __func__);
366 return SR_ERR_BUG;
367 }
368
ca3d84cc
BV
369 switch (ctx->profile->transport) {
370 case DMM_TRANSPORT_USBHID:
371 /* TODO */
372 break;
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;
378 }
379 break;
380 }
381
382 return SR_OK;
383}
384
385static int hw_cleanup(void)
386{
387 GSList *l;
388 struct sr_dev_inst *sdi;
389 struct context *ctx;
390
391 /* Properly close and free all devices. */
75337758 392 for (l = gdi->instances; l; l = l->next) {
ca3d84cc
BV
393 if (!(sdi = l->data)) {
394 /* Log error, but continue cleaning up the rest. */
395 sr_err("genericdmm: sdi was NULL, continuing.");
396 continue;
397 }
398 if (!(ctx = sdi->priv)) {
399 /* Log error, but continue cleaning up the rest. */
400 sr_err("genericdmm: sdi->priv was NULL, continuing.");
401 continue;
402 }
403
404 if (ctx->profile) {
405 switch (ctx->profile->transport) {
406 case DMM_TRANSPORT_USBHID:
407 /* TODO */
408 break;
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);
413 break;
414 }
415 }
416
417 sr_dev_inst_free(sdi);
418 }
419
75337758
BV
420 g_slist_free(gdi->instances);
421 gdi->instances = NULL;
ca3d84cc
BV
422
423 if (genericdmm_usb_context)
424 libusb_exit(genericdmm_usb_context);
425
426 return SR_OK;
427}
428
6910bf6b
BV
429static int hw_info_get(int dev_info_id, const void **data,
430 const struct sr_dev_inst *sdi)
ca3d84cc 431{
ca3d84cc 432 struct context *ctx;
ca3d84cc
BV
433
434 switch (dev_info_id) {
435 case SR_DI_INST:
6910bf6b 436 *data = sdi;
ca3d84cc
BV
437 sr_spew("genericdmm: Returning sdi.");
438 break;
f1a14ea7
BV
439 case SR_DI_HWOPTS:
440 *data = hwopts;
441 break;
442 case SR_DI_HWCAPS:
443 *data = hwcaps;
444 break;
ca3d84cc 445 case SR_DI_NUM_PROBES:
6910bf6b 446 *data = GINT_TO_POINTER(1);
ca3d84cc
BV
447 break;
448 case SR_DI_PROBE_NAMES:
6910bf6b 449 *data = probe_names;
ca3d84cc
BV
450 break;
451 case SR_DI_CUR_SAMPLERATE:
452 /* TODO get rid of this */
6910bf6b 453 *data = NULL;
ca3d84cc
BV
454 break;
455 default:
456 /* Unknown device info ID. */
6910bf6b 457 return SR_ERR_ARG;
ca3d84cc
BV
458 }
459
6910bf6b 460 return SR_OK;
ca3d84cc
BV
461}
462
463static int hw_dev_status_get(int dev_index)
464{
465 struct sr_dev_inst *sdi;
466
75337758 467 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
468 sr_err("genericdmm: sdi was NULL, device not found.");
469 return SR_ST_NOT_FOUND;
470 }
471
472 sr_dbg("genericdmm: Returning status: %d.", sdi->status);
473
474 return sdi->status;
475}
476
6f4b1868
BV
477static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
478 const void *value)
ca3d84cc 479{
ca3d84cc 480 struct context *ctx;
ca3d84cc 481
ca3d84cc
BV
482 if (!(ctx = sdi->priv)) {
483 sr_err("genericdmm: sdi->priv was NULL.");
484 return SR_ERR_BUG;
485 }
486
ca3d84cc
BV
487 switch (hwcap) {
488 case SR_HWCAP_LIMIT_MSEC:
489 if (*(const uint64_t *)value == 0) {
490 sr_err("genericdmm: LIMIT_MSEC can't be 0.");
491 return SR_ERR;
492 }
493 ctx->limit_msec = *(const uint64_t *)value;
494 sr_dbg("genericdmm: Setting LIMIT_MSEC to %" PRIu64 ".",
495 ctx->limit_msec);
496 break;
497 case SR_HWCAP_LIMIT_SAMPLES:
498 ctx->limit_samples = *(const uint64_t *)value;
499 sr_dbg("genericdmm: Setting LIMIT_SAMPLES to %" PRIu64 ".",
500 ctx->limit_samples);
501 break;
ca3d84cc
BV
502 default:
503 sr_err("genericdmm: Unknown capability: %d.", hwcap);
504 return SR_ERR;
505 break;
506 }
507
508 return SR_OK;
509}
510
511static int receive_data(int fd, int revents, void *cb_data)
512{
513 struct sr_dev_inst *sdi;
514 struct context *ctx;
515
516 if (!(sdi = cb_data))
517 return FALSE;
518
519 if (!(ctx = sdi->priv))
520 return FALSE;
521
522 if (revents != G_IO_IN) {
523 sr_err("genericdmm: No data?");
524 return FALSE;
525 }
526
527 switch (ctx->profile->transport) {
528 case DMM_TRANSPORT_USBHID:
529 /* TODO */
530 break;
531 case DMM_TRANSPORT_SERIAL:
532 /* TODO */
533 break;
534 }
535
536 return TRUE;
537}
538
539static int hw_dev_acquisition_start(int dev_index, void *cb_data)
540{
541 struct sr_datafeed_packet packet;
542 struct sr_datafeed_header header;
543 struct sr_datafeed_meta_analog meta;
544 struct sr_dev_inst *sdi;
545 struct context *ctx;
546
75337758 547 if (!(sdi = sr_dev_inst_get(gdi->instances, dev_index))) {
ca3d84cc
BV
548 sr_err("genericdmm: sdi was NULL.");
549 return SR_ERR_BUG;
550 }
551
552 if (!(ctx = sdi->priv)) {
553 sr_err("genericdmm: sdi->priv was NULL.");
554 return SR_ERR_BUG;
555 }
556
557 sr_dbg("genericdmm: Starting acquisition.");
558
559 ctx->cb_data = cb_data;
560
561 /* Send header packet to the session bus. */
562 sr_dbg("genericdmm: Sending SR_DF_HEADER.");
563 packet.type = SR_DF_HEADER;
564 packet.payload = (uint8_t *)&header;
565 header.feed_version = 1;
566 gettimeofday(&header.starttime, NULL);
567 sr_session_send(ctx->cb_data, &packet);
568
569 /* Send metadata about the SR_DF_ANALOG packets to come. */
570 sr_dbg("genericdmm: Sending SR_DF_META_ANALOG.");
571 packet.type = SR_DF_META_ANALOG;
572 packet.payload = &meta;
573 meta.num_probes = 1;
574 sr_session_send(ctx->cb_data, &packet);
575
576 /* Hook up a proxy handler to receive data from the device. */
577 switch (ctx->profile->transport) {
578 case DMM_TRANSPORT_USBHID:
579 /* TODO libusb FD setup */
580 break;
581 case DMM_TRANSPORT_SERIAL:
582 /* TODO serial FD setup */
583 // sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
584 break;
585 }
586
587 return SR_OK;
588}
589
590static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
591{
592 struct sr_datafeed_packet packet;
593
594 /* Avoid compiler warnings. */
595 (void)dev_index;
596
597 sr_dbg("genericdmm: Stopping acquisition.");
598
599 /* Send end packet to the session bus. */
600 sr_dbg("genericdmm: Sending SR_DF_END.");
601 packet.type = SR_DF_END;
602 sr_session_send(cb_data, &packet);
603
604 return SR_OK;
605}
606
607SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
608 .name = "genericdmm",
609 .longname = "Generic DMM",
610 .api_version = 1,
611 .init = hw_init,
612 .cleanup = hw_cleanup,
61136ea6 613 .scan = hw_scan,
ca3d84cc
BV
614 .dev_open = hw_dev_open,
615 .dev_close = hw_dev_close,
6910bf6b 616 .info_get = hw_info_get,
ca3d84cc 617 .dev_status_get = hw_dev_status_get,
ca3d84cc
BV
618 .dev_config_set = hw_dev_config_set,
619 .dev_acquisition_start = hw_dev_acquisition_start,
620 .dev_acquisition_stop = hw_dev_acquisition_stop,
75337758 621 .instances = NULL,
ca3d84cc 622};