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