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