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