]>
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> | |
c1aae900 | 24 | #include <libsigrok/libsigrok.h> |
fa773062 UH |
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 | |
1a46cc62 | 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 | }; | |
1beccaed | 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 | ||
4f840ce9 | 52 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
ac3898d2 | 53 | { |
f6beaac5 | 54 | return std_init(sr_ctx, di, LOG_PREFIX); |
ac3898d2 BV |
55 | } |
56 | ||
4f840ce9 | 57 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
ac3898d2 BV |
58 | { |
59 | struct drv_context *drvc; | |
7a360375 BV |
60 | struct dev_context *devc; |
61 | struct sr_dev_inst *sdi; | |
7a360375 BV |
62 | struct libusb_device_descriptor des; |
63 | libusb_device **devlist; | |
ac3898d2 | 64 | GSList *devices; |
66c91e25 SA |
65 | int ret, i; |
66 | char connection_id[64]; | |
ac3898d2 BV |
67 | |
68 | (void)options; | |
69 | ||
41812aca | 70 | drvc = di->context; |
7a360375 | 71 | |
ac3898d2 | 72 | devices = NULL; |
d4abb463 | 73 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 BV |
74 | for (i = 0; devlist[i]; i++) { |
75 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { | |
76 | sr_warn("Failed to get device descriptor: %s", | |
77 | libusb_error_name(ret)); | |
78 | continue; | |
79 | } | |
ac3898d2 | 80 | |
7a360375 BV |
81 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) |
82 | continue; | |
83 | ||
66c91e25 SA |
84 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); |
85 | ||
aac29cc1 | 86 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
87 | sdi->status = SR_ST_INACTIVE; |
88 | sdi->vendor = g_strdup(VICTOR_VENDOR); | |
7a360375 | 89 | sdi->driver = di; |
66c91e25 | 90 | sdi->connection_id = g_strdup(connection_id); |
f57d8ffe | 91 | devc = g_malloc0(sizeof(struct dev_context)); |
7a360375 BV |
92 | sdi->priv = devc; |
93 | ||
5e23fcab | 94 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
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 | { |
41812aca | 109 | return ((struct drv_context *)(di->context))->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; |
41812aca | 115 | struct drv_context *drvc = di->context; |
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 | 120 | |
41812aca | 121 | if (!di->context) { |
7a360375 BV |
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 | 169 | |
41812aca | 170 | if (!di->context) { |
7a360375 BV |
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 | ||
41812aca | 194 | if (!(drvc = di->context)) |
7a360375 BV |
195 | /* Can get called on an unused driver, doesn't matter. */ |
196 | return SR_OK; | |
ac3898d2 | 197 | |
a6630742 | 198 | ret = std_dev_clear(di, NULL); |
7a360375 | 199 | g_free(drvc); |
ac3898d2 | 200 | |
8d18d266 | 201 | return ret; |
ac3898d2 BV |
202 | } |
203 | ||
584560f1 | 204 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 205 | const struct sr_channel_group *cg) |
ac046ef8 BV |
206 | { |
207 | struct sr_usb_dev_inst *usb; | |
208 | char str[128]; | |
209 | ||
53b4680f | 210 | (void)cg; |
8f996b89 | 211 | |
584560f1 | 212 | switch (key) { |
ac046ef8 BV |
213 | case SR_CONF_CONN: |
214 | if (!sdi || !sdi->conn) | |
215 | return SR_ERR_ARG; | |
216 | usb = sdi->conn; | |
217 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
218 | *data = g_variant_new_string(str); | |
219 | break; | |
220 | default: | |
221 | return SR_ERR_NA; | |
222 | } | |
223 | ||
224 | return SR_OK; | |
225 | } | |
226 | ||
584560f1 | 227 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 228 | const struct sr_channel_group *cg) |
ac3898d2 | 229 | { |
4f840ce9 | 230 | struct sr_dev_driver *di = sdi->driver; |
7a360375 BV |
231 | struct dev_context *devc; |
232 | gint64 now; | |
ac3898d2 | 233 | |
53b4680f | 234 | (void)cg; |
8f996b89 | 235 | |
e73ffd42 BV |
236 | if (sdi->status != SR_ST_ACTIVE) |
237 | return SR_ERR_DEV_CLOSED; | |
238 | ||
41812aca | 239 | if (!di->context) { |
7a360375 BV |
240 | sr_err("Driver was not initialized."); |
241 | return SR_ERR; | |
242 | } | |
243 | ||
7a360375 | 244 | devc = sdi->priv; |
dcd438ee | 245 | |
584560f1 | 246 | switch (key) { |
1953564a | 247 | case SR_CONF_LIMIT_MSEC: |
a59b4eef | 248 | devc->limit_msec = g_variant_get_uint64(data); |
fa773062 UH |
249 | now = g_get_monotonic_time() / 1000; |
250 | devc->end_time = now + devc->limit_msec; | |
fa773062 | 251 | break; |
1953564a | 252 | case SR_CONF_LIMIT_SAMPLES: |
a59b4eef | 253 | devc->limit_samples = g_variant_get_uint64(data); |
fa773062 | 254 | break; |
ac3898d2 | 255 | default: |
dcd438ee | 256 | return SR_ERR_NA; |
ac3898d2 BV |
257 | } |
258 | ||
dcd438ee | 259 | return SR_OK; |
ac3898d2 BV |
260 | } |
261 | ||
584560f1 | 262 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 263 | const struct sr_channel_group *cg) |
a1c743fc | 264 | { |
a1c743fc | 265 | (void)sdi; |
53b4680f | 266 | (void)cg; |
a1c743fc BV |
267 | |
268 | switch (key) { | |
ac046ef8 | 269 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 270 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 271 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
ac046ef8 | 272 | break; |
9a6517d1 | 273 | case SR_CONF_DEVICE_OPTIONS: |
55333928 BV |
274 | if (!sdi) |
275 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
276 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
277 | else | |
278 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
279 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
9a6517d1 | 280 | break; |
a1c743fc | 281 | default: |
bd6fbf62 | 282 | return SR_ERR_NA; |
a1c743fc BV |
283 | } |
284 | ||
285 | return SR_OK; | |
286 | } | |
287 | ||
55462b8b | 288 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) |
7a360375 BV |
289 | { |
290 | struct dev_context *devc; | |
291 | struct sr_dev_inst *sdi; | |
292 | int ret; | |
293 | ||
294 | sdi = transfer->user_data; | |
295 | devc = sdi->priv; | |
296 | if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) { | |
297 | /* USB device was unplugged. */ | |
6078d2c9 | 298 | dev_acquisition_stop(sdi, sdi); |
7a360375 | 299 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { |
fa773062 | 300 | sr_dbg("Got %d-byte packet.", transfer->actual_length); |
ff945683 BV |
301 | if (transfer->actual_length == DMM_DATA_SIZE) { |
302 | victor_dmm_receive_data(sdi, transfer->buffer); | |
7a360375 BV |
303 | if (devc->limit_samples) { |
304 | if (devc->num_samples >= devc->limit_samples) | |
6078d2c9 | 305 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
306 | } |
307 | } | |
308 | } | |
309 | /* Anything else is either an error or a timeout, which is fine: | |
310 | * we were just going to send another transfer request anyway. */ | |
311 | ||
312 | if (sdi->status == SR_ST_ACTIVE) { | |
313 | /* Send the same request again. */ | |
314 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
fa773062 UH |
315 | sr_err("Unable to resubmit transfer: %s.", |
316 | libusb_error_name(ret)); | |
7a360375 | 317 | g_free(transfer->buffer); |
9ec7ff94 | 318 | libusb_free_transfer(transfer); |
6078d2c9 | 319 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
320 | } |
321 | } else { | |
322 | /* This was the last transfer we're going to receive, so | |
323 | * clean up now. */ | |
7a360375 | 324 | g_free(transfer->buffer); |
9ec7ff94 | 325 | libusb_free_transfer(transfer); |
7a360375 | 326 | } |
7a360375 BV |
327 | } |
328 | ||
329 | static int handle_events(int fd, int revents, void *cb_data) | |
330 | { | |
331 | struct dev_context *devc; | |
4f840ce9 | 332 | struct drv_context *drvc; |
7a360375 BV |
333 | struct sr_datafeed_packet packet; |
334 | struct sr_dev_inst *sdi; | |
4f840ce9 | 335 | struct sr_dev_driver *di; |
7a360375 BV |
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; | |
4f840ce9 | 344 | di = sdi->driver; |
41812aca | 345 | drvc = di->context; |
7a360375 BV |
346 | |
347 | if (devc->limit_msec) { | |
348 | now = g_get_monotonic_time() / 1000; | |
349 | if (now > devc->end_time) | |
6078d2c9 | 350 | dev_acquisition_stop(sdi, sdi); |
7a360375 BV |
351 | } |
352 | ||
353 | if (sdi->status == SR_ST_STOPPING) { | |
102f1239 | 354 | usb_source_remove(sdi->session, drvc->sr_ctx); |
7a360375 | 355 | |
6078d2c9 | 356 | dev_close(sdi); |
7a360375 BV |
357 | |
358 | packet.type = SR_DF_END; | |
359 | sr_session_send(cb_data, &packet); | |
360 | } | |
361 | ||
362 | memset(&tv, 0, sizeof(struct timeval)); | |
d4abb463 PS |
363 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, |
364 | NULL); | |
7a360375 BV |
365 | |
366 | return TRUE; | |
367 | } | |
368 | ||
6078d2c9 | 369 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
ac3898d2 | 370 | { |
4f840ce9 | 371 | struct sr_dev_driver *di = sdi->driver; |
7a360375 | 372 | struct dev_context *devc; |
41812aca | 373 | struct drv_context *drvc = di->context; |
ac046ef8 | 374 | struct sr_usb_dev_inst *usb; |
7a360375 | 375 | struct libusb_transfer *transfer; |
6c60facc | 376 | int ret; |
7a360375 BV |
377 | unsigned char *buf; |
378 | ||
e73ffd42 BV |
379 | if (sdi->status != SR_ST_ACTIVE) |
380 | return SR_ERR_DEV_CLOSED; | |
381 | ||
41812aca | 382 | if (!di->context) { |
7a360375 BV |
383 | sr_err("Driver was not initialized."); |
384 | return SR_ERR; | |
385 | } | |
386 | ||
7a360375 | 387 | devc = sdi->priv; |
ac046ef8 | 388 | usb = sdi->conn; |
7a360375 BV |
389 | devc->cb_data = cb_data; |
390 | ||
391 | /* Send header packet to the session bus. */ | |
29a27196 | 392 | std_session_send_df_header(cb_data, LOG_PREFIX); |
7a360375 | 393 | |
102f1239 BV |
394 | usb_source_add(sdi->session, drvc->sr_ctx, 100, |
395 | handle_events, (void *)sdi); | |
7a360375 | 396 | |
a95f142e | 397 | buf = g_malloc(DMM_DATA_SIZE); |
7a360375 BV |
398 | transfer = libusb_alloc_transfer(0); |
399 | /* Each transfer request gets 100ms to arrive before it's restarted. | |
400 | * The device only sends 1 transfer/second no matter how many | |
401 | * times you ask, but we want to keep step with the USB events | |
402 | * handling above. */ | |
ac046ef8 | 403 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, |
fa773062 UH |
404 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, |
405 | cb_data, 100); | |
7a360375 | 406 | if ((ret = libusb_submit_transfer(transfer) != 0)) { |
fa773062 | 407 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); |
7a360375 BV |
408 | libusb_free_transfer(transfer); |
409 | g_free(buf); | |
410 | return SR_ERR; | |
411 | } | |
ac3898d2 BV |
412 | |
413 | return SR_OK; | |
414 | } | |
415 | ||
6078d2c9 | 416 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
ac3898d2 | 417 | { |
4f840ce9 | 418 | struct sr_dev_driver *di = sdi->driver; |
ac3898d2 BV |
419 | (void)cb_data; |
420 | ||
41812aca | 421 | if (!di->context) { |
7a360375 BV |
422 | sr_err("Driver was not initialized."); |
423 | return SR_ERR; | |
424 | } | |
425 | ||
ac3898d2 | 426 | if (sdi->status != SR_ST_ACTIVE) { |
7a360375 | 427 | sr_err("Device not active, can't stop acquisition."); |
ac3898d2 BV |
428 | return SR_ERR; |
429 | } | |
430 | ||
7a360375 | 431 | sdi->status = SR_ST_STOPPING; |
ac3898d2 BV |
432 | |
433 | return SR_OK; | |
434 | } | |
435 | ||
436 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info = { | |
437 | .name = "victor-dmm", | |
7a360375 | 438 | .longname = "Victor DMMs", |
ac3898d2 | 439 | .api_version = 1, |
6078d2c9 UH |
440 | .init = init, |
441 | .cleanup = cleanup, | |
442 | .scan = scan, | |
443 | .dev_list = dev_list, | |
a6630742 | 444 | .dev_clear = NULL, |
ac046ef8 | 445 | .config_get = config_get, |
035a1078 | 446 | .config_set = config_set, |
a1c743fc | 447 | .config_list = config_list, |
6078d2c9 UH |
448 | .dev_open = dev_open, |
449 | .dev_close = dev_close, | |
450 | .dev_acquisition_start = dev_acquisition_start, | |
451 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 452 | .context = NULL, |
ac3898d2 | 453 | }; |