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