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