]> sigrok.org Git - libsigrok.git/blame - hardware/genericdmm/api.c
genericdmm: Factor out USB functions.
[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
b84c13d7 29extern SR_PRIV struct dmmchip dmmchip_victor70c;
ca3d84cc 30
7fc754a0
BV
31static struct sr_hwopt victor_70c_vidpid[] = {
32 { SR_HWOPT_CONN, "1244.d237" },
33 { 0, NULL }
34};
ca3d84cc 35static struct dev_profile dev_profiles[] = {
b84c13d7
BV
36 { "victor-70c", "Victor", "70C", &dmmchip_victor70c,
37 DMM_TRANSPORT_USBHID, 1000, victor_70c_vidpid
7fc754a0 38 },
3c6ce226 39 { NULL, NULL, NULL, NULL, 0, 0, NULL }
ca3d84cc
BV
40};
41
f1a14ea7
BV
42static const int hwopts[] = {
43 SR_HWOPT_MODEL,
44 SR_HWOPT_CONN,
45 SR_HWOPT_SERIALCOMM,
46 0,
47};
48
ca3d84cc
BV
49static const int hwcaps[] = {
50 SR_HWCAP_MULTIMETER,
51 SR_HWCAP_LIMIT_SAMPLES,
52 SR_HWCAP_LIMIT_MSEC,
53 SR_HWCAP_CONTINUOUS,
ca3d84cc
BV
54 0,
55};
56
57static const char *probe_names[] = {
58 "Probe",
59 NULL,
60};
61
75337758
BV
62SR_PRIV struct sr_dev_driver genericdmm_driver_info;
63static struct sr_dev_driver *gdi = &genericdmm_driver_info;
64/* TODO need a way to keep this local to the static library */
3c6ce226
BV
65static libusb_context *genericdmm_usb_context = NULL;
66static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
67 void *cb_data);
ca3d84cc 68
0c632d36
UH
69static GSList *connect_serial(const char *conn, const char *serialcomm)
70{
71 GSList *devices;
72
73 devices = NULL;
74
75 /* TODO */
76 sr_dbg("Not yet implemented.");
77
78 return devices;
79}
ca3d84cc 80
0c632d36 81GSList *genericdmm_connect(const char *conn, const char *serialcomm)
bbb40871 82{
0c632d36 83 GSList *devices, *l;
bbb40871 84 struct sr_dev_inst *sdi;
301a5e4c 85 struct dev_context *devc;
0c632d36 86 struct drv_context *drvc;
3c6ce226 87 struct sr_probe *probe;
0c632d36
UH
88 struct libusb_device *ldev;
89 int i, devcnt;
bbb40871 90
301a5e4c
BV
91 drvc = gdi->priv;
92
0c632d36 93 devices = NULL;
bbb40871 94
0c632d36
UH
95 if (serialcomm)
96 /* Must be a serial port. */
97 return connect_serial(conn, serialcomm);
bbb40871 98
0c632d36 99 if (!(l = sr_usb_connect(genericdmm_usb_context, conn))) {
bbb40871
BV
100 return NULL;
101 }
102
0c632d36
UH
103 for (i = 0; i < (int)g_slist_length(l); i++) {
104 ldev = (struct libusb_device *)l->data;
bbb40871 105
301a5e4c 106 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
2980cc24 107 sr_err("Device context malloc failed.");
886a52b6 108 return NULL;
bbb40871
BV
109 }
110
301a5e4c 111 devcnt = g_slist_length(drvc->instances);
3c6ce226 112 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
d6db79a4 113 NULL, NULL, NULL))) {
2980cc24 114 sr_err("sr_dev_inst_new returned NULL.");
bbb40871
BV
115 return NULL;
116 }
301a5e4c 117 sdi->priv = devc;
3c6ce226
BV
118 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
119 return NULL;
120 sdi->probes = g_slist_append(sdi->probes, probe);
301a5e4c 121 devc->usb = sr_usb_dev_inst_new(
0c632d36
UH
122 libusb_get_bus_number(ldev),
123 libusb_get_device_address(ldev), NULL);
bbb40871 124 devices = g_slist_append(devices, sdi);
bbb40871 125
bbb40871 126 return devices;
0c632d36 127 }
bbb40871
BV
128
129 return NULL;
130}
131
3a0fe402
BV
132static GSList *default_scan(GSList *options)
133{
134 GSList *l, *devices;
135 struct sr_hwopt *opt;
136 const char *conn, *serialcomm;
137
138 devices = NULL;
139 conn = serialcomm = NULL;
140 for (l = options; l; l = l->next) {
141 opt = l->data;
142 switch (opt->hwopt) {
143 case SR_HWOPT_CONN:
144 conn = opt->value;
145 break;
146 case SR_HWOPT_SERIALCOMM:
147 serialcomm = opt->value;
148 break;
149 }
150 }
151 if (conn)
152 devices = genericdmm_connect(conn, serialcomm);
153
154 return devices;
155}
156
811deee4
BV
157static int clear_instances(void)
158{
159 GSList *l;
160 struct sr_dev_inst *sdi;
161 struct dev_context *devc;
162 struct drv_context *drvc;
163
164 if (!(drvc = gdi->priv))
165 return SR_OK;
166
167 /* Properly close and free all devices. */
168 for (l = drvc->instances; l; l = l->next) {
169 if (!(sdi = l->data)) {
170 /* Log error, but continue cleaning up the rest. */
2980cc24 171 sr_err("sdi was NULL, continuing.");
811deee4
BV
172 continue;
173 }
174 if (!(devc = sdi->priv)) {
175 /* Log error, but continue cleaning up the rest. */
2980cc24 176 sr_err("sdi->priv was NULL, continuing.");
811deee4
BV
177 continue;
178 }
179
180 if (devc->profile) {
181 switch (devc->profile->transport) {
182 case DMM_TRANSPORT_USBHID:
811deee4
BV
183 sr_usb_dev_inst_free(devc->usb);
184 break;
185 case DMM_TRANSPORT_SERIAL:
186 if (devc->serial && devc->serial->fd != -1)
187 serial_close(devc->serial->fd);
188 sr_serial_dev_inst_free(devc->serial);
189 break;
190 }
191 }
192
193 sr_dev_inst_free(sdi);
194 }
195
196 g_slist_free(drvc->instances);
197 drvc->instances = NULL;
198
199 return SR_OK;
200}
201
40dda2c3 202static int hw_init(void)
ca3d84cc 203{
301a5e4c
BV
204 struct drv_context *drvc;
205
206 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
2980cc24 207 sr_err("Driver context malloc failed.");
886a52b6 208 return SR_ERR_MALLOC;
301a5e4c 209 }
ca3d84cc 210
ca3d84cc 211 if (libusb_init(&genericdmm_usb_context) != 0) {
2980cc24 212 sr_err("Failed to initialize USB.");
61136ea6 213 return SR_ERR;
ca3d84cc
BV
214 }
215
301a5e4c 216 gdi->priv = drvc;
61136ea6
BV
217
218 return SR_OK;
219}
220
3a0fe402 221static GSList *hw_scan(GSList *options)
61136ea6 222{
3a0fe402
BV
223 GSList *l, *ldef, *defopts, *newopts, *devices;
224 struct sr_hwopt *opt, *defopt;
225 struct dev_profile *pr, *profile;
d6db79a4 226 struct sr_dev_inst *sdi;
301a5e4c 227 struct drv_context *drvc;
3c6ce226 228 struct dev_context *devc;
3a0fe402
BV
229 const char *model;
230
301a5e4c
BV
231 drvc = gdi->priv;
232
3a0fe402
BV
233 /* Separate model from the options list. */
234 model = NULL;
235 newopts = NULL;
236 for (l = options; l; l = l->next) {
237 opt = l->data;
238 if (opt->hwopt == SR_HWOPT_MODEL)
239 model = opt->value;
240 else
241 /* New list with references to the original data. */
242 newopts = g_slist_append(newopts, opt);
243 }
244 if (!model) {
d6db79a4 245 /* This driver only works when a model is specified. */
3a0fe402
BV
246 return NULL;
247 }
61136ea6 248
3a0fe402
BV
249 /* Find a profile with this model name. */
250 profile = NULL;
251 for (pr = dev_profiles; pr->modelid; pr++) {
252 if (!strcmp(pr->modelid, model)) {
253 profile = pr;
254 break;
255 }
ca3d84cc 256 }
3a0fe402
BV
257 if (!profile) {
258 sr_err("Unknown model %s.", model);
259 return NULL;
260 }
261
262 /* Initialize the DMM chip driver. */
263 if (profile->chip->init)
264 profile->chip->init();
ca3d84cc 265
3a0fe402
BV
266 /* Convert the profile's default options list to a GSList. */
267 defopts = NULL;
268 for (opt = profile->defaults_opts; opt->hwopt; opt++) {
269 /* New list with references to const data in the profile. */
270 defopts = g_slist_append(defopts, opt);
ca3d84cc 271 }
ca3d84cc 272
3a0fe402
BV
273 /* Options given as argument to this function override the
274 * profile's default options.
275 */
276 for (ldef = defopts; ldef; ldef = ldef->next) {
277 defopt = ldef->data;
278 for (l = newopts; l; l = l->next) {
279 opt = l->data;
280 if (opt->hwopt == defopt->hwopt) {
281 /* Override the default, and drop it from the
282 * options list.
283 */
284 ldef->data = l->data;
285 newopts = g_slist_remove(newopts, opt);
286 break;
287 }
288 }
289 }
290 /* Whatever is left in newopts wasn't in the default options. */
291 defopts = g_slist_concat(defopts, newopts);
292 g_slist_free(newopts);
293
294 if (profile->chip->scan)
295 /* The DMM chip driver wants to do its own scanning. */
296 devices = profile->chip->scan(defopts);
297 else
298 devices = default_scan(defopts);
299 g_slist_free(defopts);
300
301 if (devices) {
302 /* TODO: need to fix up sdi->index fields */
d6db79a4
BV
303 for (l = devices; l; l = l->next) {
304 /* The default connection-based scanner doesn't really
305 * know about profiles, so it never filled in the vendor
306 * or model. Do that now.
307 */
308 sdi = l->data;
3c6ce226
BV
309 devc = sdi->priv;
310 devc->profile = profile;
d6db79a4
BV
311 sdi->driver = gdi;
312 if (!sdi->vendor)
313 sdi->vendor = g_strdup(profile->vendor);
314 if (!sdi->model)
315 sdi->model = g_strdup(profile->model);
316 /* Add a copy of these new devices to the driver instances. */
301a5e4c 317 drvc->instances = g_slist_append(drvc->instances, l->data);
d6db79a4 318 }
3a0fe402
BV
319 }
320
321 return devices;
ca3d84cc
BV
322}
323
811deee4
BV
324static GSList *hw_dev_list(void)
325{
326 struct drv_context *drvc;
327
328 drvc = gdi->priv;
329
330 return drvc->instances;
331}
332
25a0f108 333static int hw_dev_open(struct sr_dev_inst *sdi)
ca3d84cc 334{
301a5e4c 335 struct dev_context *devc;
3c6ce226 336 int ret;
ca3d84cc 337
301a5e4c 338 if (!(devc = sdi->priv)) {
2980cc24 339 sr_err("sdi->priv was NULL.");
ca3d84cc
BV
340 return SR_ERR_BUG;
341 }
342
3c6ce226 343 ret = SR_OK;
301a5e4c 344 switch (devc->profile->transport) {
ca3d84cc 345 case DMM_TRANSPORT_USBHID:
0c632d36
UH
346 if (sdi->status == SR_ST_ACTIVE) {
347 sr_err("Device already in use.");
348 ret = SR_ERR;
349 } else {
350 ret = sr_usb_open(genericdmm_usb_context, devc->usb);
351 }
ca3d84cc
BV
352 break;
353 case DMM_TRANSPORT_SERIAL:
2980cc24 354 sr_dbg("Opening serial port '%s'.", devc->serial->port);
301a5e4c
BV
355 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
356 if (devc->serial->fd == -1) {
2980cc24 357 sr_err("Couldn't open serial port '%s'.",
301a5e4c 358 devc->serial->port);
3c6ce226 359 ret = SR_ERR;
ca3d84cc 360 }
301a5e4c 361 // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
ca3d84cc
BV
362 break;
363 default:
364 sr_err("No transport set.");
3c6ce226 365 ret = SR_ERR;
ca3d84cc
BV
366 }
367
3c6ce226 368 return ret;
ca3d84cc
BV
369}
370
25a0f108 371static int hw_dev_close(struct sr_dev_inst *sdi)
ca3d84cc 372{
301a5e4c 373 struct dev_context *devc;
ca3d84cc 374
301a5e4c 375 if (!(devc = sdi->priv)) {
2980cc24 376 sr_err("%s: sdi->priv was NULL.", __func__);
ca3d84cc
BV
377 return SR_ERR_BUG;
378 }
379
301a5e4c 380 switch (devc->profile->transport) {
ca3d84cc
BV
381 case DMM_TRANSPORT_USBHID:
382 /* TODO */
383 break;
384 case DMM_TRANSPORT_SERIAL:
301a5e4c
BV
385 if (devc->serial && devc->serial->fd != -1) {
386 serial_close(devc->serial->fd);
387 devc->serial->fd = -1;
ca3d84cc
BV
388 sdi->status = SR_ST_INACTIVE;
389 }
390 break;
391 }
392
393 return SR_OK;
394}
395
396static int hw_cleanup(void)
397{
ca3d84cc 398
811deee4 399 clear_instances();
ca3d84cc
BV
400
401 if (genericdmm_usb_context)
402 libusb_exit(genericdmm_usb_context);
403
404 return SR_OK;
405}
406
c4a1de59 407static int hw_info_get(int info_id, const void **data,
6910bf6b 408 const struct sr_dev_inst *sdi)
ca3d84cc 409{
301a5e4c 410 struct dev_context *devc;
ca3d84cc 411
6f57fd96 412 (void)sdi;
301a5e4c 413 (void)devc;
6f57fd96 414
c4a1de59 415 switch (info_id) {
f1a14ea7
BV
416 case SR_DI_HWOPTS:
417 *data = hwopts;
418 break;
419 case SR_DI_HWCAPS:
420 *data = hwcaps;
421 break;
ca3d84cc 422 case SR_DI_NUM_PROBES:
6910bf6b 423 *data = GINT_TO_POINTER(1);
ca3d84cc
BV
424 break;
425 case SR_DI_PROBE_NAMES:
6910bf6b 426 *data = probe_names;
ca3d84cc
BV
427 break;
428 case SR_DI_CUR_SAMPLERATE:
429 /* TODO get rid of this */
6910bf6b 430 *data = NULL;
3c6ce226 431 return SR_ERR_ARG;
ca3d84cc
BV
432 break;
433 default:
434 /* Unknown device info ID. */
6910bf6b 435 return SR_ERR_ARG;
ca3d84cc
BV
436 }
437
6910bf6b 438 return SR_OK;
ca3d84cc
BV
439}
440
6f4b1868
BV
441static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
442 const void *value)
ca3d84cc 443{
301a5e4c 444 struct dev_context *devc;
ca3d84cc 445
301a5e4c 446 if (!(devc = sdi->priv)) {
2980cc24 447 sr_err("sdi->priv was NULL.");
ca3d84cc
BV
448 return SR_ERR_BUG;
449 }
450
ca3d84cc
BV
451 switch (hwcap) {
452 case SR_HWCAP_LIMIT_MSEC:
3c6ce226 453 /* TODO: not yet implemented */
ca3d84cc 454 if (*(const uint64_t *)value == 0) {
2980cc24 455 sr_err("LIMIT_MSEC can't be 0.");
ca3d84cc
BV
456 return SR_ERR;
457 }
301a5e4c 458 devc->limit_msec = *(const uint64_t *)value;
2980cc24 459 sr_dbg("Setting time limit to %" PRIu64 "ms.",
301a5e4c 460 devc->limit_msec);
ca3d84cc
BV
461 break;
462 case SR_HWCAP_LIMIT_SAMPLES:
301a5e4c 463 devc->limit_samples = *(const uint64_t *)value;
2980cc24 464 sr_dbg("Setting sample limit to %" PRIu64 ".",
301a5e4c 465 devc->limit_samples);
ca3d84cc 466 break;
ca3d84cc 467 default:
2980cc24 468 sr_err("Unknown capability: %d.", hwcap);
ca3d84cc
BV
469 return SR_ERR;
470 break;
471 }
472
473 return SR_OK;
474}
475
476static int receive_data(int fd, int revents, void *cb_data)
477{
478 struct sr_dev_inst *sdi;
301a5e4c 479 struct dev_context *devc;
ca3d84cc 480
3c6ce226
BV
481 (void)revents;
482
ca3d84cc 483 if (!(sdi = cb_data))
3c6ce226 484 return TRUE;
ca3d84cc 485
301a5e4c 486 if (!(devc = sdi->priv))
3c6ce226 487 return TRUE;
ca3d84cc 488
301a5e4c 489 switch (devc->profile->transport) {
ca3d84cc 490 case DMM_TRANSPORT_USBHID:
3c6ce226
BV
491 if (devc->profile->chip->data)
492 devc->profile->chip->data(sdi);
ca3d84cc
BV
493 break;
494 case DMM_TRANSPORT_SERIAL:
495 /* TODO */
3c6ce226 496 fd = fd;
ca3d84cc
BV
497 break;
498 }
499
3c6ce226
BV
500 if (devc->num_samples >= devc->limit_samples)
501 hw_dev_acquisition_stop(sdi, cb_data);
502
ca3d84cc
BV
503 return TRUE;
504}
505
3ffb6964
BV
506static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
507 void *cb_data)
ca3d84cc
BV
508{
509 struct sr_datafeed_packet packet;
510 struct sr_datafeed_header header;
511 struct sr_datafeed_meta_analog meta;
301a5e4c 512 struct dev_context *devc;
ca3d84cc 513
301a5e4c 514 if (!(devc = sdi->priv)) {
2980cc24 515 sr_err("sdi->priv was NULL.");
ca3d84cc
BV
516 return SR_ERR_BUG;
517 }
518
2980cc24 519 sr_dbg("Starting acquisition.");
ca3d84cc 520
301a5e4c 521 devc->cb_data = cb_data;
ca3d84cc
BV
522
523 /* Send header packet to the session bus. */
2980cc24 524 sr_dbg("Sending SR_DF_HEADER.");
ca3d84cc
BV
525 packet.type = SR_DF_HEADER;
526 packet.payload = (uint8_t *)&header;
527 header.feed_version = 1;
528 gettimeofday(&header.starttime, NULL);
301a5e4c 529 sr_session_send(devc->cb_data, &packet);
ca3d84cc
BV
530
531 /* Send metadata about the SR_DF_ANALOG packets to come. */
2980cc24 532 sr_dbg("Sending SR_DF_META_ANALOG.");
ca3d84cc
BV
533 packet.type = SR_DF_META_ANALOG;
534 packet.payload = &meta;
535 meta.num_probes = 1;
301a5e4c 536 sr_session_send(devc->cb_data, &packet);
ca3d84cc
BV
537
538 /* Hook up a proxy handler to receive data from the device. */
301a5e4c 539 switch (devc->profile->transport) {
ca3d84cc 540 case DMM_TRANSPORT_USBHID:
3c6ce226
BV
541 /* Callously using stdin here. This works because no G_IO_* flags
542 * are set, but will certainly break when any other driver does
543 * this, and runs at the same time as genericdmm.
544 * We'll need a timeout-only source when revamping the whole
545 * driver source system.
546 */
547 sr_source_add(0, 0, devc->profile->poll_timeout,
548 receive_data, (void *)sdi);
ca3d84cc
BV
549 break;
550 case DMM_TRANSPORT_SERIAL:
551 /* TODO serial FD setup */
301a5e4c 552 // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
ca3d84cc
BV
553 break;
554 }
555
556 return SR_OK;
557}
558
3ffb6964
BV
559static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
560 void *cb_data)
ca3d84cc
BV
561{
562 struct sr_datafeed_packet packet;
563
564 /* Avoid compiler warnings. */
3ffb6964 565 (void)sdi;
ca3d84cc 566
2980cc24 567 sr_dbg("Stopping acquisition.");
ca3d84cc
BV
568
569 /* Send end packet to the session bus. */
2980cc24 570 sr_dbg("Sending SR_DF_END.");
ca3d84cc
BV
571 packet.type = SR_DF_END;
572 sr_session_send(cb_data, &packet);
573
3c6ce226
BV
574 sr_source_remove(0);
575
ca3d84cc
BV
576 return SR_OK;
577}
578
579SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
580 .name = "genericdmm",
581 .longname = "Generic DMM",
582 .api_version = 1,
583 .init = hw_init,
584 .cleanup = hw_cleanup,
61136ea6 585 .scan = hw_scan,
811deee4
BV
586 .dev_list = hw_dev_list,
587 .dev_clear = clear_instances,
ca3d84cc
BV
588 .dev_open = hw_dev_open,
589 .dev_close = hw_dev_close,
6910bf6b 590 .info_get = hw_info_get,
ca3d84cc
BV
591 .dev_config_set = hw_dev_config_set,
592 .dev_acquisition_start = hw_dev_acquisition_start,
593 .dev_acquisition_stop = hw_dev_acquisition_stop,
301a5e4c 594 .priv = NULL,
ca3d84cc 595};