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