2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
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.
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.
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
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27 #include "genericdmm.h"
29 extern SR_PRIV struct dmmchip dmmchip_victor70c;
31 static struct sr_hwopt victor_70c_vidpid[] = {
32 { SR_HWOPT_CONN, "1244.d237" },
35 static struct dev_profile dev_profiles[] = {
36 { "victor-70c", "Victor", "70C", &dmmchip_victor70c,
37 DMM_TRANSPORT_USBHID, 1000, victor_70c_vidpid
39 { NULL, NULL, NULL, NULL, 0, 0, NULL }
42 static const int hwopts[] = {
49 static const int hwcaps[] = {
51 SR_HWCAP_LIMIT_SAMPLES,
57 static const char *probe_names[] = {
62 SR_PRIV struct sr_dev_driver genericdmm_driver_info;
63 static struct sr_dev_driver *gdi = &genericdmm_driver_info;
64 /* TODO need a way to keep this local to the static library */
65 static libusb_context *genericdmm_usb_context = NULL;
66 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
68 static GSList *connect_serial(const char *conn, const char *serialcomm)
75 sr_dbg("Not yet implemented.");
80 GSList *genericdmm_connect(const char *conn, const char *serialcomm)
83 struct sr_dev_inst *sdi;
84 struct dev_context *devc;
85 struct drv_context *drvc;
86 struct sr_probe *probe;
87 struct libusb_device *ldev;
95 /* Must be a serial port. */
96 return connect_serial(conn, serialcomm);
98 if (!(l = sr_usb_connect(genericdmm_usb_context, conn))) {
102 for (i = 0; i < (int)g_slist_length(l); i++) {
103 ldev = (struct libusb_device *)l->data;
105 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
106 sr_err("Device context malloc failed.");
110 devcnt = g_slist_length(drvc->instances);
111 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
112 NULL, NULL, NULL))) {
113 sr_err("sr_dev_inst_new returned NULL.");
117 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
119 sdi->probes = g_slist_append(sdi->probes, probe);
120 devc->usb = sr_usb_dev_inst_new(
121 libusb_get_bus_number(ldev),
122 libusb_get_device_address(ldev), NULL);
123 devices = g_slist_append(devices, sdi);
131 static GSList *default_scan(GSList *options)
134 struct sr_hwopt *opt;
135 const char *conn, *serialcomm;
138 conn = serialcomm = NULL;
139 for (l = options; l; l = l->next) {
141 switch (opt->hwopt) {
145 case SR_HWOPT_SERIALCOMM:
146 serialcomm = opt->value;
151 devices = genericdmm_connect(conn, serialcomm);
156 static int clear_instances(void)
159 struct sr_dev_inst *sdi;
160 struct dev_context *devc;
161 struct drv_context *drvc;
163 if (!(drvc = gdi->priv))
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. */
170 sr_err("sdi was NULL, continuing.");
173 if (!(devc = sdi->priv)) {
174 /* Log error, but continue cleaning up the rest. */
175 sr_err("sdi->priv was NULL, continuing.");
180 switch (devc->profile->transport) {
181 case DMM_TRANSPORT_USBHID:
182 sr_usb_dev_inst_free(devc->usb);
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);
192 sr_dev_inst_free(sdi);
195 g_slist_free(drvc->instances);
196 drvc->instances = NULL;
201 static int hw_init(void)
203 struct drv_context *drvc;
205 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
206 sr_err("Driver context malloc failed.");
207 return SR_ERR_MALLOC;
210 if (libusb_init(&genericdmm_usb_context) != 0) {
211 sr_err("Failed to initialize USB.");
220 static GSList *hw_scan(GSList *options)
222 GSList *l, *ldef, *defopts, *newopts, *devices;
223 struct sr_hwopt *opt, *defopt;
224 struct dev_profile *pr, *profile;
225 struct sr_dev_inst *sdi;
226 struct drv_context *drvc;
227 struct dev_context *devc;
232 /* Separate model from the options list. */
235 for (l = options; l; l = l->next) {
237 if (opt->hwopt == SR_HWOPT_MODEL)
240 /* New list with references to the original data. */
241 newopts = g_slist_append(newopts, opt);
244 /* This driver only works when a model is specified. */
248 /* Find a profile with this model name. */
250 for (pr = dev_profiles; pr->modelid; pr++) {
251 if (!strcmp(pr->modelid, model)) {
257 sr_err("Unknown model %s.", model);
261 /* Initialize the DMM chip driver. */
262 if (profile->chip->init)
263 profile->chip->init();
265 /* Convert the profile's default options list to a GSList. */
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);
272 /* Options given as argument to this function override the
273 * profile's default options.
275 for (ldef = defopts; ldef; ldef = ldef->next) {
277 for (l = newopts; l; l = l->next) {
279 if (opt->hwopt == defopt->hwopt) {
280 /* Override the default, and drop it from the
283 ldef->data = l->data;
284 newopts = g_slist_remove(newopts, opt);
289 /* Whatever is left in newopts wasn't in the default options. */
290 defopts = g_slist_concat(defopts, newopts);
291 g_slist_free(newopts);
293 if (profile->chip->scan)
294 /* The DMM chip driver wants to do its own scanning. */
295 devices = profile->chip->scan(defopts);
297 devices = default_scan(defopts);
298 g_slist_free(defopts);
301 /* TODO: need to fix up sdi->index fields */
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.
309 devc->profile = profile;
312 sdi->vendor = g_strdup(profile->vendor);
314 sdi->model = g_strdup(profile->model);
315 /* Add a copy of these new devices to the driver instances. */
316 drvc->instances = g_slist_append(drvc->instances, l->data);
323 static GSList *hw_dev_list(void)
325 struct drv_context *drvc;
329 return drvc->instances;
332 static int hw_dev_open(struct sr_dev_inst *sdi)
334 struct dev_context *devc;
337 if (!(devc = sdi->priv)) {
338 sr_err("sdi->priv was NULL.");
343 switch (devc->profile->transport) {
344 case DMM_TRANSPORT_USBHID:
345 if (sdi->status == SR_ST_ACTIVE) {
346 sr_err("Device already in use.");
349 ret = sr_usb_open(genericdmm_usb_context, devc->usb);
352 case DMM_TRANSPORT_SERIAL:
353 sr_dbg("Opening serial port '%s'.", devc->serial->port);
354 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
355 if (devc->serial->fd == -1) {
356 sr_err("Couldn't open serial port '%s'.",
360 // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
363 sr_err("No transport set.");
370 static int hw_dev_close(struct sr_dev_inst *sdi)
372 struct dev_context *devc;
374 if (!(devc = sdi->priv)) {
375 sr_err("%s: sdi->priv was NULL.", __func__);
379 switch (devc->profile->transport) {
380 case DMM_TRANSPORT_USBHID:
383 case DMM_TRANSPORT_SERIAL:
384 if (devc->serial && devc->serial->fd != -1) {
385 serial_close(devc->serial->fd);
386 devc->serial->fd = -1;
387 sdi->status = SR_ST_INACTIVE;
395 static int hw_cleanup(void)
400 if (genericdmm_usb_context)
401 libusb_exit(genericdmm_usb_context);
406 static int hw_info_get(int info_id, const void **data,
407 const struct sr_dev_inst *sdi)
409 struct dev_context *devc;
421 case SR_DI_NUM_PROBES:
422 *data = GINT_TO_POINTER(1);
424 case SR_DI_PROBE_NAMES:
427 case SR_DI_CUR_SAMPLERATE:
428 /* TODO get rid of this */
433 /* Unknown device info ID. */
440 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
443 struct dev_context *devc;
445 if (!(devc = sdi->priv)) {
446 sr_err("sdi->priv was NULL.");
451 case SR_HWCAP_LIMIT_MSEC:
452 /* TODO: not yet implemented */
453 if (*(const uint64_t *)value == 0) {
454 sr_err("LIMIT_MSEC can't be 0.");
457 devc->limit_msec = *(const uint64_t *)value;
458 sr_dbg("Setting time limit to %" PRIu64 "ms.",
461 case SR_HWCAP_LIMIT_SAMPLES:
462 devc->limit_samples = *(const uint64_t *)value;
463 sr_dbg("Setting sample limit to %" PRIu64 ".",
464 devc->limit_samples);
467 sr_err("Unknown capability: %d.", hwcap);
475 static int receive_data(int fd, int revents, void *cb_data)
477 struct sr_dev_inst *sdi;
478 struct dev_context *devc;
482 if (!(sdi = cb_data))
485 if (!(devc = sdi->priv))
488 switch (devc->profile->transport) {
489 case DMM_TRANSPORT_USBHID:
490 if (devc->profile->chip->data)
491 devc->profile->chip->data(sdi);
493 case DMM_TRANSPORT_SERIAL:
499 if (devc->num_samples >= devc->limit_samples)
500 hw_dev_acquisition_stop(sdi, cb_data);
505 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
508 struct sr_datafeed_packet packet;
509 struct sr_datafeed_header header;
510 struct sr_datafeed_meta_analog meta;
511 struct dev_context *devc;
513 if (!(devc = sdi->priv)) {
514 sr_err("sdi->priv was NULL.");
518 sr_dbg("Starting acquisition.");
520 devc->cb_data = cb_data;
522 /* Send header packet to the session bus. */
523 sr_dbg("Sending SR_DF_HEADER.");
524 packet.type = SR_DF_HEADER;
525 packet.payload = (uint8_t *)&header;
526 header.feed_version = 1;
527 gettimeofday(&header.starttime, NULL);
528 sr_session_send(devc->cb_data, &packet);
530 /* Send metadata about the SR_DF_ANALOG packets to come. */
531 sr_dbg("Sending SR_DF_META_ANALOG.");
532 packet.type = SR_DF_META_ANALOG;
533 packet.payload = &meta;
535 sr_session_send(devc->cb_data, &packet);
537 /* Hook up a proxy handler to receive data from the device. */
538 switch (devc->profile->transport) {
539 case DMM_TRANSPORT_USBHID:
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.
546 sr_source_add(0, 0, devc->profile->poll_timeout,
547 receive_data, (void *)sdi);
549 case DMM_TRANSPORT_SERIAL:
550 /* TODO serial FD setup */
551 // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
558 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
560 struct sr_datafeed_packet packet;
562 /* Avoid compiler warnings. */
565 sr_dbg("Stopping acquisition.");
567 /* Send end packet to the session bus. */
568 sr_dbg("Sending SR_DF_END.");
569 packet.type = SR_DF_END;
570 sr_session_send(cb_data, &packet);
577 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
578 .name = "genericdmm",
579 .longname = "Generic DMM",
582 .cleanup = hw_cleanup,
584 .dev_list = hw_dev_list,
585 .dev_clear = clear_instances,
586 .dev_open = hw_dev_open,
587 .dev_close = hw_dev_close,
588 .info_get = hw_info_get,
589 .dev_config_set = hw_dev_config_set,
590 .dev_acquisition_start = hw_dev_acquisition_start,
591 .dev_acquisition_stop = hw_dev_acquisition_stop,