]>
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 | |
ac046ef8 BV |
38 | static const int32_t hwopts[] = { |
39 | SR_CONF_CONN, | |
40 | }; | |
41 | ||
a59b4eef | 42 | static const int32_t hwcaps[] = { |
1953564a BV |
43 | SR_CONF_MULTIMETER, |
44 | SR_CONF_LIMIT_MSEC, | |
45 | SR_CONF_LIMIT_SAMPLES, | |
46 | SR_CONF_CONTINUOUS, | |
7a360375 BV |
47 | }; |
48 | ||
6078d2c9 | 49 | static int init(struct sr_context *sr_ctx) |
ac3898d2 | 50 | { |
f6beaac5 | 51 | return std_init(sr_ctx, di, LOG_PREFIX); |
ac3898d2 BV |
52 | } |
53 | ||
6078d2c9 | 54 | static GSList *scan(GSList *options) |
ac3898d2 BV |
55 | { |
56 | struct drv_context *drvc; | |
7a360375 BV |
57 | struct dev_context *devc; |
58 | struct sr_dev_inst *sdi; | |
ba7dd8bb | 59 | struct sr_channel *ch; |
7a360375 BV |
60 | struct libusb_device_descriptor des; |
61 | libusb_device **devlist; | |
ac3898d2 | 62 | GSList *devices; |
7a360375 | 63 | int ret, devcnt, i; |
ac3898d2 BV |
64 | |
65 | (void)options; | |
66 | ||
4b97c74e | 67 | drvc = di->priv; |
7a360375 | 68 | |
ac3898d2 | 69 | devices = NULL; |
d4abb463 | 70 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 BV |
71 | for (i = 0; devlist[i]; i++) { |
72 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { | |
73 | sr_warn("Failed to get device descriptor: %s", | |
74 | libusb_error_name(ret)); | |
75 | continue; | |
76 | } | |
ac3898d2 | 77 | |
7a360375 BV |
78 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) |
79 | continue; | |
80 | ||
81 | devcnt = g_slist_length(drvc->instances); | |
fa773062 UH |
82 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, |
83 | VICTOR_VENDOR, NULL, NULL))) | |
7a360375 BV |
84 | return NULL; |
85 | sdi->driver = di; | |
86 | ||
87 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) | |
88 | return NULL; | |
89 | sdi->priv = devc; | |
90 | ||
3f239f08 | 91 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"))) |
7a360375 | 92 | return NULL; |
ba7dd8bb | 93 | sdi->channels = g_slist_append(NULL, ch); |
7a360375 | 94 | |
ac046ef8 | 95 | if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), |
7a360375 BV |
96 | libusb_get_device_address(devlist[i]), NULL))) |
97 | return NULL; | |
ac046ef8 | 98 | sdi->inst_type = SR_INST_USB; |
7a360375 BV |
99 | |
100 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
101 | devices = g_slist_append(devices, sdi); | |
102 | } | |
103 | libusb_free_device_list(devlist, 1); | |
ac3898d2 BV |
104 | |
105 | return devices; | |
106 | } | |
107 | ||
6078d2c9 | 108 | static GSList *dev_list(void) |
ac3898d2 | 109 | { |
0e94d524 | 110 | return ((struct drv_context *)(di->priv))->instances; |
ac3898d2 BV |
111 | } |
112 | ||
6078d2c9 | 113 | static int dev_open(struct sr_dev_inst *sdi) |
ac3898d2 | 114 | { |
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; | |
119 | ||
120 | if (!di->priv) { | |
121 | sr_err("Driver was not initialized."); | |
122 | return SR_ERR; | |
123 | } | |
124 | ||
ac046ef8 BV |
125 | usb = sdi->conn; |
126 | ||
d4abb463 | 127 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 | 128 | for (i = 0; devlist[i]; i++) { |
ac046ef8 BV |
129 | if (libusb_get_bus_number(devlist[i]) != usb->bus |
130 | || libusb_get_device_address(devlist[i]) != usb->address) | |
7a360375 | 131 | continue; |
ac046ef8 | 132 | if ((ret = libusb_open(devlist[i], &usb->devhdl))) { |
fa773062 | 133 | sr_err("Failed to open device: %s.", libusb_error_name(ret)); |
7a360375 BV |
134 | return SR_ERR; |
135 | } | |
136 | break; | |
137 | } | |
138 | libusb_free_device_list(devlist, 1); | |
139 | if (!devlist[i]) { | |
140 | sr_err("Device not found."); | |
141 | return SR_ERR; | |
142 | } | |
143 | ||
144 | /* The device reports as HID class, so the kernel would have | |
145 | * claimed it. */ | |
ac046ef8 BV |
146 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { |
147 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { | |
d4928d71 PS |
148 | sr_err("Failed to detach kernel driver: %s.", |
149 | libusb_error_name(ret)); | |
7a360375 BV |
150 | return SR_ERR; |
151 | } | |
152 | } | |
153 | ||
ac046ef8 | 154 | if ((ret = libusb_claim_interface(usb->devhdl, |
7a360375 | 155 | VICTOR_INTERFACE))) { |
fa773062 | 156 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); |
7a360375 BV |
157 | return SR_ERR; |
158 | } | |
159 | sdi->status = SR_ST_ACTIVE; | |
ac3898d2 BV |
160 | |
161 | return SR_OK; | |
162 | } | |
163 | ||
6078d2c9 | 164 | static int dev_close(struct sr_dev_inst *sdi) |
ac3898d2 | 165 | { |
ac046ef8 | 166 | struct sr_usb_dev_inst *usb; |
7a360375 BV |
167 | |
168 | if (!di->priv) { | |
169 | sr_err("Driver was not initialized."); | |
170 | return SR_ERR; | |
171 | } | |
172 | ||
ac046ef8 BV |
173 | usb = sdi->conn; |
174 | ||
175 | if (!usb->devhdl) | |
7a360375 BV |
176 | /* Nothing to do. */ |
177 | return SR_OK; | |
178 | ||
ac046ef8 BV |
179 | libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); |
180 | libusb_close(usb->devhdl); | |
181 | usb->devhdl = NULL; | |
7a360375 | 182 | sdi->status = SR_ST_INACTIVE; |
ac3898d2 BV |
183 | |
184 | return SR_OK; | |
185 | } | |
186 | ||
6078d2c9 | 187 | static int cleanup(void) |
ac3898d2 | 188 | { |
8d18d266 | 189 | int ret; |
7a360375 BV |
190 | struct drv_context *drvc; |
191 | ||
192 | if (!(drvc = di->priv)) | |
193 | /* Can get called on an unused driver, doesn't matter. */ | |
194 | return SR_OK; | |
ac3898d2 | 195 | |
a6630742 BV |
196 | |
197 | ret = std_dev_clear(di, NULL); | |
7a360375 BV |
198 | g_free(drvc); |
199 | di->priv = NULL; | |
ac3898d2 | 200 | |
8d18d266 | 201 | return ret; |
ac3898d2 BV |
202 | } |
203 | ||
8f996b89 | 204 | static int config_get(int id, 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 | |
ac046ef8 BV |
212 | switch (id) { |
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 | ||
8f996b89 | 227 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 228 | const struct sr_channel_group *cg) |
ac3898d2 | 229 | { |
7a360375 BV |
230 | struct dev_context *devc; |
231 | gint64 now; | |
ac3898d2 BV |
232 | int ret; |
233 | ||
53b4680f | 234 | (void)cg; |
8f996b89 | 235 | |
e73ffd42 BV |
236 | if (sdi->status != SR_ST_ACTIVE) |
237 | return SR_ERR_DEV_CLOSED; | |
238 | ||
7a360375 BV |
239 | if (!di->priv) { |
240 | sr_err("Driver was not initialized."); | |
241 | return SR_ERR; | |
242 | } | |
243 | ||
7a360375 | 244 | devc = sdi->priv; |
ac3898d2 | 245 | ret = SR_OK; |
035a1078 | 246 | switch (id) { |
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; | |
251 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
252 | devc->limit_msec); | |
253 | break; | |
1953564a | 254 | case SR_CONF_LIMIT_SAMPLES: |
a59b4eef | 255 | devc->limit_samples = g_variant_get_uint64(data); |
fa773062 UH |
256 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
257 | devc->limit_samples); | |
258 | break; | |
ac3898d2 | 259 | default: |
bd6fbf62 | 260 | ret = SR_ERR_NA; |
ac3898d2 BV |
261 | } |
262 | ||
263 | return ret; | |
264 | } | |
265 | ||
8f996b89 | 266 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 267 | const struct sr_channel_group *cg) |
a1c743fc | 268 | { |
a1c743fc | 269 | (void)sdi; |
53b4680f | 270 | (void)cg; |
a1c743fc BV |
271 | |
272 | switch (key) { | |
ac046ef8 BV |
273 | case SR_CONF_SCAN_OPTIONS: |
274 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
275 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
276 | break; | |
9a6517d1 | 277 | case SR_CONF_DEVICE_OPTIONS: |
a59b4eef BV |
278 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
279 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 280 | break; |
a1c743fc | 281 | default: |
bd6fbf62 | 282 | return SR_ERR_NA; |
a1c743fc BV |
283 | } |
284 | ||
285 | return SR_OK; | |
286 | } | |
287 | ||
7a360375 BV |
288 | static void receive_transfer(struct libusb_transfer *transfer) |
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; | |
d4abb463 | 332 | struct drv_context *drvc = di->priv; |
7a360375 BV |
333 | struct sr_datafeed_packet packet; |
334 | struct sr_dev_inst *sdi; | |
335 | struct timeval tv; | |
336 | gint64 now; | |
7a360375 BV |
337 | |
338 | (void)fd; | |
339 | (void)revents; | |
340 | ||
341 | sdi = cb_data; | |
342 | devc = sdi->priv; | |
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 | { |
7a360375 | 368 | struct dev_context *devc; |
d4abb463 | 369 | struct drv_context *drvc = di->priv; |
ac046ef8 | 370 | struct sr_usb_dev_inst *usb; |
7a360375 | 371 | struct libusb_transfer *transfer; |
6c60facc | 372 | int ret; |
7a360375 BV |
373 | unsigned char *buf; |
374 | ||
e73ffd42 BV |
375 | if (sdi->status != SR_ST_ACTIVE) |
376 | return SR_ERR_DEV_CLOSED; | |
377 | ||
7a360375 BV |
378 | if (!di->priv) { |
379 | sr_err("Driver was not initialized."); | |
380 | return SR_ERR; | |
381 | } | |
382 | ||
7a360375 | 383 | devc = sdi->priv; |
ac046ef8 | 384 | usb = sdi->conn; |
7a360375 BV |
385 | devc->cb_data = cb_data; |
386 | ||
387 | /* Send header packet to the session bus. */ | |
29a27196 | 388 | std_session_send_df_header(cb_data, LOG_PREFIX); |
7a360375 | 389 | |
102f1239 BV |
390 | usb_source_add(sdi->session, drvc->sr_ctx, 100, |
391 | handle_events, (void *)sdi); | |
7a360375 | 392 | |
ff945683 | 393 | buf = g_try_malloc(DMM_DATA_SIZE); |
7a360375 BV |
394 | transfer = libusb_alloc_transfer(0); |
395 | /* Each transfer request gets 100ms to arrive before it's restarted. | |
396 | * The device only sends 1 transfer/second no matter how many | |
397 | * times you ask, but we want to keep step with the USB events | |
398 | * handling above. */ | |
ac046ef8 | 399 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, |
fa773062 UH |
400 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, |
401 | cb_data, 100); | |
7a360375 | 402 | if ((ret = libusb_submit_transfer(transfer) != 0)) { |
fa773062 | 403 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); |
7a360375 BV |
404 | libusb_free_transfer(transfer); |
405 | g_free(buf); | |
406 | return SR_ERR; | |
407 | } | |
ac3898d2 BV |
408 | |
409 | return SR_OK; | |
410 | } | |
411 | ||
6078d2c9 | 412 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
ac3898d2 BV |
413 | { |
414 | (void)cb_data; | |
415 | ||
7a360375 BV |
416 | if (!di->priv) { |
417 | sr_err("Driver was not initialized."); | |
418 | return SR_ERR; | |
419 | } | |
420 | ||
ac3898d2 | 421 | if (sdi->status != SR_ST_ACTIVE) { |
7a360375 | 422 | sr_err("Device not active, can't stop acquisition."); |
ac3898d2 BV |
423 | return SR_ERR; |
424 | } | |
425 | ||
7a360375 | 426 | sdi->status = SR_ST_STOPPING; |
ac3898d2 BV |
427 | |
428 | return SR_OK; | |
429 | } | |
430 | ||
431 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info = { | |
432 | .name = "victor-dmm", | |
7a360375 | 433 | .longname = "Victor DMMs", |
ac3898d2 | 434 | .api_version = 1, |
6078d2c9 UH |
435 | .init = init, |
436 | .cleanup = cleanup, | |
437 | .scan = scan, | |
438 | .dev_list = dev_list, | |
a6630742 | 439 | .dev_clear = NULL, |
ac046ef8 | 440 | .config_get = config_get, |
035a1078 | 441 | .config_set = config_set, |
a1c743fc | 442 | .config_list = config_list, |
6078d2c9 UH |
443 | .dev_open = dev_open, |
444 | .dev_close = dev_close, | |
445 | .dev_acquisition_start = dev_acquisition_start, | |
446 | .dev_acquisition_stop = dev_acquisition_stop, | |
ac3898d2 BV |
447 | .priv = NULL, |
448 | }; |