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