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