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