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