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