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