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