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(const struct sr_dev_inst *sdi,
69 static GSList *connect_serial(const char *conn, const char *serialcomm)
76 sr_dbg("Not yet implemented.");
81 GSList *genericdmm_connect(const char *conn, const char *serialcomm)
84 struct sr_dev_inst *sdi;
85 struct dev_context *devc;
86 struct drv_context *drvc;
87 struct sr_probe *probe;
88 struct libusb_device *ldev;
96 /* Must be a serial port. */
97 return connect_serial(conn, serialcomm);
99 if (!(l = sr_usb_connect(genericdmm_usb_context, conn))) {
103 for (i = 0; i < (int)g_slist_length(l); i++) {
104 ldev = (struct libusb_device *)l->data;
106 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
107 sr_err("Device context malloc failed.");
111 devcnt = g_slist_length(drvc->instances);
112 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
113 NULL, NULL, NULL))) {
114 sr_err("sr_dev_inst_new returned NULL.");
118 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
120 sdi->probes = g_slist_append(sdi->probes, probe);
121 devc->usb = sr_usb_dev_inst_new(
122 libusb_get_bus_number(ldev),
123 libusb_get_device_address(ldev), NULL);
124 devices = g_slist_append(devices, sdi);
132 static GSList *default_scan(GSList *options)
135 struct sr_hwopt *opt;
136 const char *conn, *serialcomm;
139 conn = serialcomm = NULL;
140 for (l = options; l; l = l->next) {
142 switch (opt->hwopt) {
146 case SR_HWOPT_SERIALCOMM:
147 serialcomm = opt->value;
152 devices = genericdmm_connect(conn, serialcomm);
157 static int clear_instances(void)
160 struct sr_dev_inst *sdi;
161 struct dev_context *devc;
162 struct drv_context *drvc;
164 if (!(drvc = gdi->priv))
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. */
171 sr_err("sdi was NULL, continuing.");
174 if (!(devc = sdi->priv)) {
175 /* Log error, but continue cleaning up the rest. */
176 sr_err("sdi->priv was NULL, continuing.");
181 switch (devc->profile->transport) {
182 case DMM_TRANSPORT_USBHID:
183 sr_usb_dev_inst_free(devc->usb);
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);
193 sr_dev_inst_free(sdi);
196 g_slist_free(drvc->instances);
197 drvc->instances = NULL;
202 static int hw_init(void)
204 struct drv_context *drvc;
206 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
207 sr_err("Driver context malloc failed.");
208 return SR_ERR_MALLOC;
211 if (libusb_init(&genericdmm_usb_context) != 0) {
212 sr_err("Failed to initialize USB.");
221 static GSList *hw_scan(GSList *options)
223 GSList *l, *ldef, *defopts, *newopts, *devices;
224 struct sr_hwopt *opt, *defopt;
225 struct dev_profile *pr, *profile;
226 struct sr_dev_inst *sdi;
227 struct drv_context *drvc;
228 struct dev_context *devc;
233 /* Separate model from the options list. */
236 for (l = options; l; l = l->next) {
238 if (opt->hwopt == SR_HWOPT_MODEL)
241 /* New list with references to the original data. */
242 newopts = g_slist_append(newopts, opt);
245 /* This driver only works when a model is specified. */
249 /* Find a profile with this model name. */
251 for (pr = dev_profiles; pr->modelid; pr++) {
252 if (!strcmp(pr->modelid, model)) {
258 sr_err("Unknown model %s.", model);
262 /* Initialize the DMM chip driver. */
263 if (profile->chip->init)
264 profile->chip->init();
266 /* Convert the profile's default options list to a GSList. */
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);
273 /* Options given as argument to this function override the
274 * profile's default options.
276 for (ldef = defopts; ldef; ldef = ldef->next) {
278 for (l = newopts; l; l = l->next) {
280 if (opt->hwopt == defopt->hwopt) {
281 /* Override the default, and drop it from the
284 ldef->data = l->data;
285 newopts = g_slist_remove(newopts, opt);
290 /* Whatever is left in newopts wasn't in the default options. */
291 defopts = g_slist_concat(defopts, newopts);
292 g_slist_free(newopts);
294 if (profile->chip->scan)
295 /* The DMM chip driver wants to do its own scanning. */
296 devices = profile->chip->scan(defopts);
298 devices = default_scan(defopts);
299 g_slist_free(defopts);
302 /* TODO: need to fix up sdi->index fields */
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.
310 devc->profile = profile;
313 sdi->vendor = g_strdup(profile->vendor);
315 sdi->model = g_strdup(profile->model);
316 /* Add a copy of these new devices to the driver instances. */
317 drvc->instances = g_slist_append(drvc->instances, l->data);
324 static GSList *hw_dev_list(void)
326 struct drv_context *drvc;
330 return drvc->instances;
333 static int hw_dev_open(struct sr_dev_inst *sdi)
335 struct dev_context *devc;
338 if (!(devc = sdi->priv)) {
339 sr_err("sdi->priv was NULL.");
344 switch (devc->profile->transport) {
345 case DMM_TRANSPORT_USBHID:
346 if (sdi->status == SR_ST_ACTIVE) {
347 sr_err("Device already in use.");
350 ret = sr_usb_open(genericdmm_usb_context, devc->usb);
353 case DMM_TRANSPORT_SERIAL:
354 sr_dbg("Opening serial port '%s'.", devc->serial->port);
355 devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
356 if (devc->serial->fd == -1) {
357 sr_err("Couldn't open serial port '%s'.",
361 // serial_set_params(devc->serial->fd, 2400, 8, 0, 1, 2);
364 sr_err("No transport set.");
371 static int hw_dev_close(struct sr_dev_inst *sdi)
373 struct dev_context *devc;
375 if (!(devc = sdi->priv)) {
376 sr_err("%s: sdi->priv was NULL.", __func__);
380 switch (devc->profile->transport) {
381 case DMM_TRANSPORT_USBHID:
384 case DMM_TRANSPORT_SERIAL:
385 if (devc->serial && devc->serial->fd != -1) {
386 serial_close(devc->serial->fd);
387 devc->serial->fd = -1;
388 sdi->status = SR_ST_INACTIVE;
396 static int hw_cleanup(void)
401 if (genericdmm_usb_context)
402 libusb_exit(genericdmm_usb_context);
407 static int hw_info_get(int info_id, const void **data,
408 const struct sr_dev_inst *sdi)
410 struct dev_context *devc;
422 case SR_DI_NUM_PROBES:
423 *data = GINT_TO_POINTER(1);
425 case SR_DI_PROBE_NAMES:
428 case SR_DI_CUR_SAMPLERATE:
429 /* TODO get rid of this */
434 /* Unknown device info ID. */
441 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
444 struct dev_context *devc;
446 if (!(devc = sdi->priv)) {
447 sr_err("sdi->priv was NULL.");
452 case SR_HWCAP_LIMIT_MSEC:
453 /* TODO: not yet implemented */
454 if (*(const uint64_t *)value == 0) {
455 sr_err("LIMIT_MSEC can't be 0.");
458 devc->limit_msec = *(const uint64_t *)value;
459 sr_dbg("Setting time limit to %" PRIu64 "ms.",
462 case SR_HWCAP_LIMIT_SAMPLES:
463 devc->limit_samples = *(const uint64_t *)value;
464 sr_dbg("Setting sample limit to %" PRIu64 ".",
465 devc->limit_samples);
468 sr_err("Unknown capability: %d.", hwcap);
476 static int receive_data(int fd, int revents, void *cb_data)
478 struct sr_dev_inst *sdi;
479 struct dev_context *devc;
483 if (!(sdi = cb_data))
486 if (!(devc = sdi->priv))
489 switch (devc->profile->transport) {
490 case DMM_TRANSPORT_USBHID:
491 if (devc->profile->chip->data)
492 devc->profile->chip->data(sdi);
494 case DMM_TRANSPORT_SERIAL:
500 if (devc->num_samples >= devc->limit_samples)
501 hw_dev_acquisition_stop(sdi, cb_data);
506 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
509 struct sr_datafeed_packet packet;
510 struct sr_datafeed_header header;
511 struct sr_datafeed_meta_analog meta;
512 struct dev_context *devc;
514 if (!(devc = sdi->priv)) {
515 sr_err("sdi->priv was NULL.");
519 sr_dbg("Starting acquisition.");
521 devc->cb_data = cb_data;
523 /* Send header packet to the session bus. */
524 sr_dbg("Sending SR_DF_HEADER.");
525 packet.type = SR_DF_HEADER;
526 packet.payload = (uint8_t *)&header;
527 header.feed_version = 1;
528 gettimeofday(&header.starttime, NULL);
529 sr_session_send(devc->cb_data, &packet);
531 /* Send metadata about the SR_DF_ANALOG packets to come. */
532 sr_dbg("Sending SR_DF_META_ANALOG.");
533 packet.type = SR_DF_META_ANALOG;
534 packet.payload = &meta;
536 sr_session_send(devc->cb_data, &packet);
538 /* Hook up a proxy handler to receive data from the device. */
539 switch (devc->profile->transport) {
540 case DMM_TRANSPORT_USBHID:
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.
547 sr_source_add(0, 0, devc->profile->poll_timeout,
548 receive_data, (void *)sdi);
550 case DMM_TRANSPORT_SERIAL:
551 /* TODO serial FD setup */
552 // sr_source_add(devc->serial->fd, G_IO_IN, -1, receive_data, sdi);
559 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
562 struct sr_datafeed_packet packet;
564 /* Avoid compiler warnings. */
567 sr_dbg("Stopping acquisition.");
569 /* Send end packet to the session bus. */
570 sr_dbg("Sending SR_DF_END.");
571 packet.type = SR_DF_END;
572 sr_session_send(cb_data, &packet);
579 SR_PRIV struct sr_dev_driver genericdmm_driver_info = {
580 .name = "genericdmm",
581 .longname = "Generic DMM",
584 .cleanup = hw_cleanup,
586 .dev_list = hw_dev_list,
587 .dev_clear = clear_instances,
588 .dev_open = hw_dev_open,
589 .dev_close = hw_dev_close,
590 .info_get = hw_info_get,
591 .dev_config_set = hw_dev_config_set,
592 .dev_acquisition_start = hw_dev_acquisition_start,
593 .dev_acquisition_stop = hw_dev_acquisition_stop,