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