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