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