]>
Commit | Line | Data |
---|---|---|
ac3898d2 BV |
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 <glib.h> | |
7a360375 BV |
21 | #include <libusb.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
fa773062 UH |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
26 | #include "protocol.h" | |
7a360375 BV |
27 | |
28 | #define VICTOR_VID 0x1244 | |
29 | #define VICTOR_PID 0xd237 | |
30 | #define VICTOR_VENDOR "Victor" | |
31 | #define VICTOR_INTERFACE 0 | |
32 | #define VICTOR_ENDPOINT LIBUSB_ENDPOINT_IN | 1 | |
ac3898d2 BV |
33 | |
34 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info; | |
35 | static struct sr_dev_driver *di = &victor_dmm_driver_info; | |
6078d2c9 | 36 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); |
7a360375 | 37 | |
ac046ef8 BV |
38 | static const int32_t hwopts[] = { |
39 | SR_CONF_CONN, | |
40 | }; | |
41 | ||
a59b4eef | 42 | static const int32_t hwcaps[] = { |
1953564a BV |
43 | SR_CONF_MULTIMETER, |
44 | SR_CONF_LIMIT_MSEC, | |
45 | SR_CONF_LIMIT_SAMPLES, | |
46 | SR_CONF_CONTINUOUS, | |
7a360375 BV |
47 | }; |
48 | ||
ac3898d2 BV |
49 | static int clear_instances(void) |
50 | { | |
8d18d266 | 51 | return std_dev_clear(di, NULL); |
ac3898d2 BV |
52 | } |
53 | ||
6078d2c9 | 54 | static int init(struct sr_context *sr_ctx) |
ac3898d2 | 55 | { |
f6beaac5 | 56 | return std_init(sr_ctx, di, LOG_PREFIX); |
ac3898d2 BV |
57 | } |
58 | ||
6078d2c9 | 59 | static GSList *scan(GSList *options) |
ac3898d2 BV |
60 | { |
61 | struct drv_context *drvc; | |
7a360375 BV |
62 | struct dev_context *devc; |
63 | struct sr_dev_inst *sdi; | |
64 | struct sr_probe *probe; | |
65 | struct libusb_device_descriptor des; | |
66 | libusb_device **devlist; | |
ac3898d2 | 67 | GSList *devices; |
7a360375 | 68 | int ret, devcnt, i; |
ac3898d2 BV |
69 | |
70 | (void)options; | |
71 | ||
4b97c74e | 72 | drvc = di->priv; |
7a360375 | 73 | |
ac3898d2 | 74 | devices = NULL; |
d4abb463 | 75 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 BV |
76 | for (i = 0; devlist[i]; i++) { |
77 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { | |
78 | sr_warn("Failed to get device descriptor: %s", | |
79 | libusb_error_name(ret)); | |
80 | continue; | |
81 | } | |
ac3898d2 | 82 | |
7a360375 BV |
83 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) |
84 | continue; | |
85 | ||
86 | devcnt = g_slist_length(drvc->instances); | |
fa773062 UH |
87 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, |
88 | VICTOR_VENDOR, NULL, NULL))) | |
7a360375 BV |
89 | return NULL; |
90 | sdi->driver = di; | |
91 | ||
92 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) | |
93 | return NULL; | |
94 | sdi->priv = devc; | |
95 | ||
96 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
97 | return NULL; | |
98 | sdi->probes = g_slist_append(NULL, probe); | |
99 | ||
ac046ef8 | 100 | if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), |
7a360375 BV |
101 | libusb_get_device_address(devlist[i]), NULL))) |
102 | return NULL; | |
ac046ef8 | 103 | sdi->inst_type = SR_INST_USB; |
7a360375 BV |
104 | |
105 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
106 | devices = g_slist_append(devices, sdi); | |
107 | } | |
108 | libusb_free_device_list(devlist, 1); | |
ac3898d2 BV |
109 | |
110 | return devices; | |
111 | } | |
112 | ||
6078d2c9 | 113 | static GSList *dev_list(void) |
ac3898d2 | 114 | { |
0e94d524 | 115 | return ((struct drv_context *)(di->priv))->instances; |
ac3898d2 BV |
116 | } |
117 | ||
6078d2c9 | 118 | static int dev_open(struct sr_dev_inst *sdi) |
ac3898d2 | 119 | { |
d4abb463 | 120 | struct drv_context *drvc = di->priv; |
ac046ef8 | 121 | struct sr_usb_dev_inst *usb; |
7a360375 BV |
122 | libusb_device **devlist; |
123 | int ret, i; | |
124 | ||
125 | if (!di->priv) { | |
126 | sr_err("Driver was not initialized."); | |
127 | return SR_ERR; | |
128 | } | |
129 | ||
ac046ef8 BV |
130 | usb = sdi->conn; |
131 | ||
d4abb463 | 132 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 | 133 | for (i = 0; devlist[i]; i++) { |
ac046ef8 BV |
134 | if (libusb_get_bus_number(devlist[i]) != usb->bus |
135 | || libusb_get_device_address(devlist[i]) != usb->address) | |
7a360375 | 136 | continue; |
ac046ef8 | 137 | if ((ret = libusb_open(devlist[i], &usb->devhdl))) { |
fa773062 | 138 | sr_err("Failed to open device: %s.", libusb_error_name(ret)); |
7a360375 BV |
139 | return SR_ERR; |
140 | } | |
141 | break; | |
142 | } | |
143 | libusb_free_device_list(devlist, 1); | |
144 | if (!devlist[i]) { | |
145 | sr_err("Device not found."); | |
146 | return SR_ERR; | |
147 | } | |
148 | ||
149 | /* The device reports as HID class, so the kernel would have | |
150 | * claimed it. */ | |
ac046ef8 BV |
151 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { |
152 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { | |
d4928d71 PS |
153 | sr_err("Failed to detach kernel driver: %s.", |
154 | libusb_error_name(ret)); | |
7a360375 BV |
155 | return SR_ERR; |
156 | } | |
157 | } | |
158 | ||
ac046ef8 | 159 | if ((ret = libusb_claim_interface(usb->devhdl, |
7a360375 | 160 | VICTOR_INTERFACE))) { |
fa773062 | 161 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); |
7a360375 BV |
162 | return SR_ERR; |
163 | } | |
164 | sdi->status = SR_ST_ACTIVE; | |
ac3898d2 BV |
165 | |
166 | return SR_OK; | |
167 | } | |
168 | ||
6078d2c9 | 169 | static int dev_close(struct sr_dev_inst *sdi) |
ac3898d2 | 170 | { |
ac046ef8 | 171 | struct sr_usb_dev_inst *usb; |
7a360375 BV |
172 | |
173 | if (!di->priv) { | |
174 | sr_err("Driver was not initialized."); | |
175 | return SR_ERR; | |
176 | } | |
177 | ||
ac046ef8 BV |
178 | usb = sdi->conn; |
179 | ||
180 | if (!usb->devhdl) | |
7a360375 BV |
181 | /* Nothing to do. */ |
182 | return SR_OK; | |
183 | ||
ac046ef8 BV |
184 | libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); |
185 | libusb_close(usb->devhdl); | |
186 | usb->devhdl = NULL; | |
7a360375 | 187 | sdi->status = SR_ST_INACTIVE; |
ac3898d2 BV |
188 | |
189 | return SR_OK; | |
190 | } | |
191 | ||
6078d2c9 | 192 | static int cleanup(void) |
ac3898d2 | 193 | { |
8d18d266 | 194 | int ret; |
7a360375 BV |
195 | struct drv_context *drvc; |
196 | ||
197 | if (!(drvc = di->priv)) | |
198 | /* Can get called on an unused driver, doesn't matter. */ | |
199 | return SR_OK; | |
ac3898d2 | 200 | |
8d18d266 | 201 | ret = clear_instances(); |
7a360375 BV |
202 | g_free(drvc); |
203 | di->priv = NULL; | |
ac3898d2 | 204 | |
8d18d266 | 205 | return ret; |
ac3898d2 BV |
206 | } |
207 | ||
ac046ef8 BV |
208 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi) |
209 | { | |
210 | struct sr_usb_dev_inst *usb; | |
211 | char str[128]; | |
212 | ||
213 | switch (id) { | |
214 | case SR_CONF_CONN: | |
215 | if (!sdi || !sdi->conn) | |
216 | return SR_ERR_ARG; | |
217 | usb = sdi->conn; | |
218 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
219 | *data = g_variant_new_string(str); | |
220 | break; | |
221 | default: | |
222 | return SR_ERR_NA; | |
223 | } | |
224 | ||
225 | return SR_OK; | |
226 | } | |
227 | ||
a59b4eef | 228 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) |
ac3898d2 | 229 | { |
7a360375 BV |
230 | struct dev_context *devc; |
231 | gint64 now; | |
ac3898d2 BV |
232 | int ret; |
233 | ||
e73ffd42 BV |
234 | if (sdi->status != SR_ST_ACTIVE) |
235 | return SR_ERR_DEV_CLOSED; | |
236 | ||
7a360375 BV |
237 | if (!di->priv) { |
238 | sr_err("Driver was not initialized."); | |
239 | return SR_ERR; | |
240 | } | |
241 | ||
7a360375 | 242 | devc = sdi->priv; |
ac3898d2 | 243 | ret = SR_OK; |
035a1078 | 244 | switch (id) { |
1953564a | 245 | case SR_CONF_LIMIT_MSEC: |
a59b4eef | 246 | devc->limit_msec = g_variant_get_uint64(data); |
fa773062 UH |
247 | now = g_get_monotonic_time() / 1000; |
248 | devc->end_time = now + devc->limit_msec; | |
249 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
250 | devc->limit_msec); | |
251 | break; | |
1953564a | 252 | case SR_CONF_LIMIT_SAMPLES: |
a59b4eef | 253 | devc->limit_samples = g_variant_get_uint64(data); |
fa773062 UH |
254 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
255 | devc->limit_samples); | |
256 | break; | |
ac3898d2 | 257 | default: |
bd6fbf62 | 258 | ret = SR_ERR_NA; |
ac3898d2 BV |
259 | } |
260 | ||
261 | return ret; | |
262 | } | |
263 | ||
a59b4eef | 264 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc BV |
265 | { |
266 | ||
267 | (void)sdi; | |
268 | ||
269 | switch (key) { | |
ac046ef8 BV |
270 | case SR_CONF_SCAN_OPTIONS: |
271 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
272 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
273 | break; | |
9a6517d1 | 274 | case SR_CONF_DEVICE_OPTIONS: |
a59b4eef BV |
275 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
276 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 277 | break; |
a1c743fc | 278 | default: |
bd6fbf62 | 279 | return SR_ERR_NA; |
a1c743fc BV |
280 | } |
281 | ||
282 | return SR_OK; | |
283 | } | |
284 | ||
7a360375 BV |
285 | static void receive_transfer(struct libusb_transfer *transfer) |
286 | { | |
287 | struct dev_context *devc; | |
288 | struct sr_dev_inst *sdi; | |
289 | int ret; | |
290 | ||
291 | sdi = transfer->user_data; | |
292 | devc = sdi->priv; | |
293 | if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) { | |
294 | /* USB device was unplugged. */ | |
6078d2c9 | 295 | dev_acquisition_stop(sdi, sdi); |
7a360375 | 296 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { |
fa773062 | 297 | sr_dbg("Got %d-byte packet.", transfer->actual_length); |
ff945683 BV |
298 | if (transfer->actual_length == DMM_DATA_SIZE) { |
299 | victor_dmm_receive_data(sdi, transfer->buffer); | |
7a360375 BV |
300 | if (devc->limit_samples) { |
301 | if (devc->num_samples >= devc->limit_samples) | |
6078d2c9 | 302 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
303 | } |
304 | } | |
305 | } | |
306 | /* Anything else is either an error or a timeout, which is fine: | |
307 | * we were just going to send another transfer request anyway. */ | |
308 | ||
309 | if (sdi->status == SR_ST_ACTIVE) { | |
310 | /* Send the same request again. */ | |
311 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
fa773062 UH |
312 | sr_err("Unable to resubmit transfer: %s.", |
313 | libusb_error_name(ret)); | |
7a360375 | 314 | g_free(transfer->buffer); |
9ec7ff94 | 315 | libusb_free_transfer(transfer); |
6078d2c9 | 316 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
317 | } |
318 | } else { | |
319 | /* This was the last transfer we're going to receive, so | |
320 | * clean up now. */ | |
7a360375 | 321 | g_free(transfer->buffer); |
9ec7ff94 | 322 | libusb_free_transfer(transfer); |
7a360375 | 323 | } |
7a360375 BV |
324 | } |
325 | ||
326 | static int handle_events(int fd, int revents, void *cb_data) | |
327 | { | |
328 | struct dev_context *devc; | |
d4abb463 | 329 | struct drv_context *drvc = di->priv; |
7a360375 BV |
330 | struct sr_datafeed_packet packet; |
331 | struct sr_dev_inst *sdi; | |
332 | struct timeval tv; | |
333 | gint64 now; | |
334 | int i; | |
335 | ||
336 | (void)fd; | |
337 | (void)revents; | |
338 | ||
339 | sdi = cb_data; | |
340 | devc = sdi->priv; | |
341 | ||
342 | if (devc->limit_msec) { | |
343 | now = g_get_monotonic_time() / 1000; | |
344 | if (now > devc->end_time) | |
6078d2c9 | 345 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
346 | } |
347 | ||
348 | if (sdi->status == SR_ST_STOPPING) { | |
349 | for (i = 0; devc->usbfd[i] != -1; i++) | |
350 | sr_source_remove(devc->usbfd[i]); | |
351 | ||
6078d2c9 | 352 | dev_close(sdi); |
7a360375 BV |
353 | |
354 | packet.type = SR_DF_END; | |
355 | sr_session_send(cb_data, &packet); | |
356 | } | |
357 | ||
358 | memset(&tv, 0, sizeof(struct timeval)); | |
d4abb463 PS |
359 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, |
360 | NULL); | |
7a360375 BV |
361 | |
362 | return TRUE; | |
363 | } | |
364 | ||
6078d2c9 | 365 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
ac3898d2 | 366 | { |
7a360375 | 367 | struct dev_context *devc; |
d4abb463 | 368 | struct drv_context *drvc = di->priv; |
7a360375 | 369 | const struct libusb_pollfd **pfd; |
ac046ef8 | 370 | struct sr_usb_dev_inst *usb; |
7a360375 BV |
371 | struct libusb_transfer *transfer; |
372 | int ret, i; | |
373 | unsigned char *buf; | |
374 | ||
e73ffd42 BV |
375 | if (sdi->status != SR_ST_ACTIVE) |
376 | return SR_ERR_DEV_CLOSED; | |
377 | ||
7a360375 BV |
378 | if (!di->priv) { |
379 | sr_err("Driver was not initialized."); | |
380 | return SR_ERR; | |
381 | } | |
382 | ||
7a360375 | 383 | devc = sdi->priv; |
ac046ef8 | 384 | usb = sdi->conn; |
7a360375 BV |
385 | devc->cb_data = cb_data; |
386 | ||
387 | /* Send header packet to the session bus. */ | |
29a27196 | 388 | std_session_send_df_header(cb_data, LOG_PREFIX); |
7a360375 | 389 | |
d4abb463 | 390 | pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx); |
7a360375 BV |
391 | for (i = 0; pfd[i]; i++) { |
392 | /* Handle USB events every 100ms, for decent latency. */ | |
393 | sr_source_add(pfd[i]->fd, pfd[i]->events, 100, | |
394 | handle_events, (void *)sdi); | |
395 | /* We'll need to remove this fd later. */ | |
396 | devc->usbfd[i] = pfd[i]->fd; | |
397 | } | |
398 | devc->usbfd[i] = -1; | |
399 | ||
ff945683 | 400 | buf = g_try_malloc(DMM_DATA_SIZE); |
7a360375 BV |
401 | transfer = libusb_alloc_transfer(0); |
402 | /* Each transfer request gets 100ms to arrive before it's restarted. | |
403 | * The device only sends 1 transfer/second no matter how many | |
404 | * times you ask, but we want to keep step with the USB events | |
405 | * handling above. */ | |
ac046ef8 | 406 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, |
fa773062 UH |
407 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, |
408 | cb_data, 100); | |
7a360375 | 409 | if ((ret = libusb_submit_transfer(transfer) != 0)) { |
fa773062 | 410 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); |
7a360375 BV |
411 | libusb_free_transfer(transfer); |
412 | g_free(buf); | |
413 | return SR_ERR; | |
414 | } | |
ac3898d2 BV |
415 | |
416 | return SR_OK; | |
417 | } | |
418 | ||
6078d2c9 | 419 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
ac3898d2 BV |
420 | { |
421 | (void)cb_data; | |
422 | ||
7a360375 BV |
423 | if (!di->priv) { |
424 | sr_err("Driver was not initialized."); | |
425 | return SR_ERR; | |
426 | } | |
427 | ||
ac3898d2 | 428 | if (sdi->status != SR_ST_ACTIVE) { |
7a360375 | 429 | sr_err("Device not active, can't stop acquisition."); |
ac3898d2 BV |
430 | return SR_ERR; |
431 | } | |
432 | ||
7a360375 | 433 | sdi->status = SR_ST_STOPPING; |
ac3898d2 BV |
434 | |
435 | return SR_OK; | |
436 | } | |
437 | ||
438 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info = { | |
439 | .name = "victor-dmm", | |
7a360375 | 440 | .longname = "Victor DMMs", |
ac3898d2 | 441 | .api_version = 1, |
6078d2c9 UH |
442 | .init = init, |
443 | .cleanup = cleanup, | |
444 | .scan = scan, | |
445 | .dev_list = dev_list, | |
ac3898d2 | 446 | .dev_clear = clear_instances, |
ac046ef8 | 447 | .config_get = config_get, |
035a1078 | 448 | .config_set = config_set, |
a1c743fc | 449 | .config_list = config_list, |
6078d2c9 UH |
450 | .dev_open = dev_open, |
451 | .dev_close = dev_close, | |
452 | .dev_acquisition_start = dev_acquisition_start, | |
453 | .dev_acquisition_stop = dev_acquisition_stop, | |
ac3898d2 BV |
454 | .priv = NULL, |
455 | }; |