]> sigrok.org Git - libsigrok.git/blob - hardware/victor-dmm/api.c
07c61faf3056f2a149dfe6dd1c85de0a808700a9
[libsigrok.git] / hardware / victor-dmm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <glib.h>
21 #include <libusb.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
26 #include "protocol.h"
27
28 #define VICTOR_VID 0x1244
29 #define VICTOR_PID 0xd237
30 #define VICTOR_VENDOR "Victor"
31 #define VICTOR_INTERFACE 0
32 #define VICTOR_ENDPOINT LIBUSB_ENDPOINT_IN | 1
33
34 SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
35 static struct sr_dev_driver *di = &victor_dmm_driver_info;
36 static int hw_dev_close(struct sr_dev_inst *sdi);
37 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
38
39 static const int hwcaps[] = {
40         SR_HWCAP_MULTIMETER,
41         SR_HWCAP_LIMIT_MSEC,
42         SR_HWCAP_LIMIT_SAMPLES,
43         SR_HWCAP_CONTINUOUS,
44         0
45 };
46
47 static const char *probe_names[] = {
48         "P1",
49 };
50
51 /* Properly close and free all devices. */
52 static int clear_instances(void)
53 {
54         struct sr_dev_inst *sdi;
55         struct drv_context *drvc;
56         struct dev_context *devc;
57         GSList *l;
58
59         if (!(drvc = di->priv))
60                 /* Can get called on an unused driver, doesn't matter. */
61                 return SR_OK;
62
63         for (l = drvc->instances; l; l = l->next) {
64                 if (!(sdi = l->data))
65                         continue;
66                 if (!(devc = sdi->priv))
67                         continue;
68                 hw_dev_close(sdi);
69                 sr_usb_dev_inst_free(devc->usb);
70                 sr_dev_inst_free(sdi);
71         }
72
73         g_slist_free(drvc->instances);
74         drvc->instances = NULL;
75
76         return SR_OK;
77 }
78
79 static int hw_init(struct sr_context *sr_ctx)
80 {
81         struct drv_context *drvc;
82
83         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
84                 sr_err("Driver context malloc failed.");
85                 return SR_ERR_MALLOC;
86         }
87         drvc->sr_ctx = sr_ctx;
88         di->priv = drvc;
89
90         return SR_OK;
91 }
92
93 static GSList *hw_scan(GSList *options)
94 {
95         struct drv_context *drvc;
96         struct dev_context *devc;
97         struct sr_dev_inst *sdi;
98         struct sr_probe *probe;
99         struct libusb_device_descriptor des;
100         libusb_device **devlist;
101         GSList *devices;
102         int ret, devcnt, i;
103
104         (void)options;
105
106         if (!(drvc = di->priv)) {
107                 sr_err("Driver was not initialized.");
108                 return NULL;
109         }
110
111         /* USB scan is always authoritative. */
112         clear_instances();
113
114         devices = NULL;
115         libusb_get_device_list(NULL, &devlist);
116         for (i = 0; devlist[i]; i++) {
117                 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
118                         sr_warn("Failed to get device descriptor: %s",
119                                         libusb_error_name(ret));
120                         continue;
121                 }
122
123                 if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
124                         continue;
125
126                 devcnt = g_slist_length(drvc->instances);
127                 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
128                                 VICTOR_VENDOR, NULL, NULL)))
129                         return NULL;
130                 sdi->driver = di;
131
132                 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
133                         return NULL;
134                 sdi->priv = devc;
135
136                 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
137                         return NULL;
138                 sdi->probes = g_slist_append(NULL, probe);
139
140                 if (!(devc->usb = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
141                                 libusb_get_device_address(devlist[i]), NULL)))
142                         return NULL;
143
144                 drvc->instances = g_slist_append(drvc->instances, sdi);
145                 devices = g_slist_append(devices, sdi);
146         }
147         libusb_free_device_list(devlist, 1);
148
149         return devices;
150 }
151
152 static GSList *hw_dev_list(void)
153 {
154         struct drv_context *drvc;
155
156         if (!(drvc = di->priv)) {
157                 sr_err("Driver was not initialized.");
158                 return NULL;
159         }
160
161         return drvc->instances;
162 }
163
164 static int hw_dev_open(struct sr_dev_inst *sdi)
165 {
166         struct dev_context *devc;
167         libusb_device **devlist;
168         int ret, i;
169
170         if (!di->priv) {
171                 sr_err("Driver was not initialized.");
172                 return SR_ERR;
173         }
174
175         devc = sdi->priv;
176         libusb_get_device_list(NULL, &devlist);
177         for (i = 0; devlist[i]; i++) {
178                 if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
179                                 || libusb_get_device_address(devlist[i]) != devc->usb->address)
180                         continue;
181                 if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) {
182                         sr_err("Failed to open device: %s.", libusb_error_name(ret));
183                         return SR_ERR;
184                 }
185                 break;
186         }
187         libusb_free_device_list(devlist, 1);
188         if (!devlist[i]) {
189                 sr_err("Device not found.");
190                 return SR_ERR;
191         }
192
193         /* The device reports as HID class, so the kernel would have
194          * claimed it. */
195         if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
196                 if (libusb_detach_kernel_driver(devc->usb->devhdl, 0) < 0) {
197                         sr_err("Failed to detach kernel driver.");
198                         return SR_ERR;
199                 }
200         }
201
202         if ((ret = libusb_claim_interface(devc->usb->devhdl,
203                         VICTOR_INTERFACE))) {
204                 sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
205                 return SR_ERR;
206         }
207         sdi->status = SR_ST_ACTIVE;
208
209         return SR_OK;
210 }
211
212 static int hw_dev_close(struct sr_dev_inst *sdi)
213 {
214         struct dev_context *devc;
215
216         if (!di->priv) {
217                 sr_err("Driver was not initialized.");
218                 return SR_ERR;
219         }
220
221         devc = sdi->priv;
222         if (!devc->usb->devhdl)
223                 /*  Nothing to do. */
224                 return SR_OK;
225
226         libusb_release_interface(devc->usb->devhdl, VICTOR_INTERFACE);
227         libusb_close(devc->usb->devhdl);
228         devc->usb->devhdl = NULL;
229         sdi->status = SR_ST_INACTIVE;
230
231         return SR_OK;
232 }
233
234 static int hw_cleanup(void)
235 {
236         struct drv_context *drvc;
237
238         if (!(drvc = di->priv))
239                 /* Can get called on an unused driver, doesn't matter. */
240                 return SR_OK;
241
242         clear_instances();
243         g_free(drvc);
244         di->priv = NULL;
245
246         return SR_OK;
247 }
248
249 static int hw_info_get(int info_id, const void **data,
250                        const struct sr_dev_inst *sdi)
251 {
252         (void)sdi;
253
254         switch (info_id) {
255         case SR_DI_HWCAPS:
256                 *data = hwcaps;
257                 break;
258         case SR_DI_NUM_PROBES:
259                 *data = GINT_TO_POINTER(1);
260                 break;
261         case SR_DI_PROBE_NAMES:
262                 *data = probe_names;
263                 break;
264         default:
265                 sr_err("Unknown info_id: %d.", info_id);
266                 return SR_ERR_ARG;
267         }
268
269         return SR_OK;
270 }
271
272 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
273                              const void *value)
274 {
275         struct dev_context *devc;
276         gint64 now;
277         int ret;
278
279         if (!di->priv) {
280                 sr_err("Driver was not initialized.");
281                 return SR_ERR;
282         }
283
284         if (sdi->status != SR_ST_ACTIVE) {
285                 sr_err("Device inactive, can't set config options.");
286                 return SR_ERR;
287         }
288
289         devc = sdi->priv;
290         ret = SR_OK;
291         switch (hwcap) {
292         case SR_HWCAP_LIMIT_MSEC:
293                 devc->limit_msec = *(const int64_t *)value;
294                 now = g_get_monotonic_time() / 1000;
295                 devc->end_time = now + devc->limit_msec;
296                 sr_dbg("Setting time limit to %" PRIu64 "ms.",
297                        devc->limit_msec);
298                 break;
299         case SR_HWCAP_LIMIT_SAMPLES:
300                 devc->limit_samples = *(const uint64_t *)value;
301                 sr_dbg("Setting sample limit to %" PRIu64 ".",
302                        devc->limit_samples);
303                 break;
304         default:
305                 sr_err("Unknown hardware capability: %d.", hwcap);
306                 ret = SR_ERR_ARG;
307         }
308
309         return ret;
310 }
311
312 static void receive_transfer(struct libusb_transfer *transfer)
313 {
314         struct dev_context *devc;
315         struct sr_dev_inst *sdi;
316         int ret;
317
318         sdi = transfer->user_data;
319         devc = sdi->priv;
320         if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
321                 /* USB device was unplugged. */
322                 hw_dev_acquisition_stop(sdi, sdi);
323         } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
324                 sr_dbg("Got %d-byte packet.", transfer->actual_length);
325                 if (transfer->actual_length == DMM_DATA_SIZE) {
326                         victor_dmm_receive_data(sdi, transfer->buffer);
327                         if (devc->limit_samples) {
328                                 if (devc->num_samples >= devc->limit_samples)
329                                         hw_dev_acquisition_stop(sdi, sdi);
330                         }
331                 }
332         }
333         /* Anything else is either an error or a timeout, which is fine:
334          * we were just going to send another transfer request anyway. */
335
336         if (sdi->status == SR_ST_ACTIVE) {
337                 /* Send the same request again. */
338                 if ((ret = libusb_submit_transfer(transfer) != 0)) {
339                         sr_err("Unable to resubmit transfer: %s.",
340                                libusb_error_name(ret));
341                         libusb_free_transfer(transfer);
342                         g_free(transfer->buffer);
343                         hw_dev_acquisition_stop(sdi, sdi);
344                 }
345         } else {
346                 /* This was the last transfer we're going to receive, so
347                  * clean up now. */
348                 libusb_free_transfer(transfer);
349                 g_free(transfer->buffer);
350         }
351 }
352
353 static int handle_events(int fd, int revents, void *cb_data)
354 {
355         struct dev_context *devc;
356         struct sr_datafeed_packet packet;
357         struct sr_dev_inst *sdi;
358         struct timeval tv;
359         gint64 now;
360         int i;
361
362         (void)fd;
363         (void)revents;
364
365         sdi = cb_data;
366         devc = sdi->priv;
367
368         if (devc->limit_msec) {
369                 now = g_get_monotonic_time() / 1000;
370                 if (now > devc->end_time)
371                         hw_dev_acquisition_stop(sdi, sdi);
372         }
373
374         if (sdi->status == SR_ST_STOPPING) {
375                 for (i = 0; devc->usbfd[i] != -1; i++)
376                         sr_source_remove(devc->usbfd[i]);
377
378                 hw_dev_close(sdi);
379
380                 packet.type = SR_DF_END;
381                 sr_session_send(cb_data, &packet);
382         }
383
384         memset(&tv, 0, sizeof(struct timeval));
385         libusb_handle_events_timeout_completed(NULL, &tv, NULL);
386
387         return TRUE;
388 }
389
390 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
391                                     void *cb_data)
392 {
393         struct sr_datafeed_packet packet;
394         struct sr_datafeed_header header;
395         struct sr_datafeed_meta_analog meta;
396         struct dev_context *devc;
397         const struct libusb_pollfd **pfd;
398         struct libusb_transfer *transfer;
399         int ret, i;
400         unsigned char *buf;
401
402         if (!di->priv) {
403                 sr_err("Driver was not initialized.");
404                 return SR_ERR;
405         }
406
407         sr_dbg("Starting acquisition.");
408
409         devc = sdi->priv;
410         devc->cb_data = cb_data;
411
412         /* Send header packet to the session bus. */
413         sr_dbg("Sending SR_DF_HEADER.");
414         packet.type = SR_DF_HEADER;
415         packet.payload = (uint8_t *)&header;
416         header.feed_version = 1;
417         sr_session_send(devc->cb_data, &packet);
418
419         /* Send metadata about the SR_DF_ANALOG packets to come. */
420         packet.type = SR_DF_META_ANALOG;
421         packet.payload = &meta;
422         meta.num_probes = 1;
423         sr_session_send(devc->cb_data, &packet);
424
425         pfd = libusb_get_pollfds(NULL);
426         for (i = 0; pfd[i]; i++) {
427                 /* Handle USB events every 100ms, for decent latency. */
428                 sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
429                                 handle_events, (void *)sdi);
430                 /* We'll need to remove this fd later. */
431                 devc->usbfd[i] = pfd[i]->fd;
432         }
433         devc->usbfd[i] = -1;
434
435         buf = g_try_malloc(DMM_DATA_SIZE);
436         transfer = libusb_alloc_transfer(0);
437         /* Each transfer request gets 100ms to arrive before it's restarted.
438          * The device only sends 1 transfer/second no matter how many
439          * times you ask, but we want to keep step with the USB events
440          * handling above. */
441         libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl,
442                         VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
443                         cb_data, 100);
444         if ((ret = libusb_submit_transfer(transfer) != 0)) {
445                 sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
446                 libusb_free_transfer(transfer);
447                 g_free(buf);
448                 return SR_ERR;
449         }
450
451         return SR_OK;
452 }
453
454 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
455 {
456         (void)cb_data;
457
458         if (!di->priv) {
459                 sr_err("Driver was not initialized.");
460                 return SR_ERR;
461         }
462
463         if (sdi->status != SR_ST_ACTIVE) {
464                 sr_err("Device not active, can't stop acquisition.");
465                 return SR_ERR;
466         }
467
468         sdi->status = SR_ST_STOPPING;
469
470         return SR_OK;
471 }
472
473 SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
474         .name = "victor-dmm",
475         .longname = "Victor DMMs",
476         .api_version = 1,
477         .init = hw_init,
478         .cleanup = hw_cleanup,
479         .scan = hw_scan,
480         .dev_list = hw_dev_list,
481         .dev_clear = clear_instances,
482         .dev_open = hw_dev_open,
483         .dev_close = hw_dev_close,
484         .info_get = hw_info_get,
485         .dev_config_set = hw_dev_config_set,
486         .dev_acquisition_start = hw_dev_acquisition_start,
487         .dev_acquisition_stop = hw_dev_acquisition_stop,
488         .priv = NULL,
489 };