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