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