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