]>
Commit | Line | Data |
---|---|---|
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> | |
21 | #include <libusb.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include "libsigrok.h" | |
25 | #include "libsigrok-internal.h" | |
26 | #include "protocol.h" | |
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 | |
33 | ||
34 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info; | |
35 | static struct sr_dev_driver *di = &victor_dmm_driver_info; | |
36 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); | |
37 | ||
38 | static const int32_t hwopts[] = { | |
39 | SR_CONF_CONN, | |
40 | }; | |
41 | ||
42 | static const int32_t hwcaps[] = { | |
43 | SR_CONF_MULTIMETER, | |
44 | SR_CONF_LIMIT_MSEC, | |
45 | SR_CONF_LIMIT_SAMPLES, | |
46 | SR_CONF_CONTINUOUS, | |
47 | }; | |
48 | ||
49 | static int init(struct sr_context *sr_ctx) | |
50 | { | |
51 | return std_init(sr_ctx, di, LOG_PREFIX); | |
52 | } | |
53 | ||
54 | static GSList *scan(GSList *options) | |
55 | { | |
56 | struct drv_context *drvc; | |
57 | struct dev_context *devc; | |
58 | struct sr_dev_inst *sdi; | |
59 | struct sr_channel *ch; | |
60 | struct libusb_device_descriptor des; | |
61 | libusb_device **devlist; | |
62 | GSList *devices; | |
63 | int ret, devcnt, i; | |
64 | ||
65 | (void)options; | |
66 | ||
67 | drvc = di->priv; | |
68 | ||
69 | devices = NULL; | |
70 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
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 | } | |
77 | ||
78 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) | |
79 | continue; | |
80 | ||
81 | devcnt = g_slist_length(drvc->instances); | |
82 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, | |
83 | VICTOR_VENDOR, NULL, NULL))) | |
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 | ||
91 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"))) | |
92 | return NULL; | |
93 | sdi->channels = g_slist_append(NULL, ch); | |
94 | ||
95 | if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
96 | libusb_get_device_address(devlist[i]), NULL))) | |
97 | return NULL; | |
98 | sdi->inst_type = SR_INST_USB; | |
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); | |
104 | ||
105 | return devices; | |
106 | } | |
107 | ||
108 | static GSList *dev_list(void) | |
109 | { | |
110 | return ((struct drv_context *)(di->priv))->instances; | |
111 | } | |
112 | ||
113 | static int dev_open(struct sr_dev_inst *sdi) | |
114 | { | |
115 | struct drv_context *drvc = di->priv; | |
116 | struct sr_usb_dev_inst *usb; | |
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 | ||
125 | usb = sdi->conn; | |
126 | ||
127 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
128 | for (i = 0; devlist[i]; i++) { | |
129 | if (libusb_get_bus_number(devlist[i]) != usb->bus | |
130 | || libusb_get_device_address(devlist[i]) != usb->address) | |
131 | continue; | |
132 | if ((ret = libusb_open(devlist[i], &usb->devhdl))) { | |
133 | sr_err("Failed to open device: %s.", libusb_error_name(ret)); | |
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. */ | |
146 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { | |
147 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { | |
148 | sr_err("Failed to detach kernel driver: %s.", | |
149 | libusb_error_name(ret)); | |
150 | return SR_ERR; | |
151 | } | |
152 | } | |
153 | ||
154 | if ((ret = libusb_claim_interface(usb->devhdl, | |
155 | VICTOR_INTERFACE))) { | |
156 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); | |
157 | return SR_ERR; | |
158 | } | |
159 | sdi->status = SR_ST_ACTIVE; | |
160 | ||
161 | return SR_OK; | |
162 | } | |
163 | ||
164 | static int dev_close(struct sr_dev_inst *sdi) | |
165 | { | |
166 | struct sr_usb_dev_inst *usb; | |
167 | ||
168 | if (!di->priv) { | |
169 | sr_err("Driver was not initialized."); | |
170 | return SR_ERR; | |
171 | } | |
172 | ||
173 | usb = sdi->conn; | |
174 | ||
175 | if (!usb->devhdl) | |
176 | /* Nothing to do. */ | |
177 | return SR_OK; | |
178 | ||
179 | libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); | |
180 | libusb_close(usb->devhdl); | |
181 | usb->devhdl = NULL; | |
182 | sdi->status = SR_ST_INACTIVE; | |
183 | ||
184 | return SR_OK; | |
185 | } | |
186 | ||
187 | static int cleanup(void) | |
188 | { | |
189 | int ret; | |
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; | |
195 | ||
196 | ||
197 | ret = std_dev_clear(di, NULL); | |
198 | g_free(drvc); | |
199 | di->priv = NULL; | |
200 | ||
201 | return ret; | |
202 | } | |
203 | ||
204 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi, | |
205 | const struct sr_channel_group *cg) | |
206 | { | |
207 | struct sr_usb_dev_inst *usb; | |
208 | char str[128]; | |
209 | ||
210 | (void)cg; | |
211 | ||
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 | ||
227 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, | |
228 | const struct sr_channel_group *cg) | |
229 | { | |
230 | struct dev_context *devc; | |
231 | gint64 now; | |
232 | int ret; | |
233 | ||
234 | (void)cg; | |
235 | ||
236 | if (sdi->status != SR_ST_ACTIVE) | |
237 | return SR_ERR_DEV_CLOSED; | |
238 | ||
239 | if (!di->priv) { | |
240 | sr_err("Driver was not initialized."); | |
241 | return SR_ERR; | |
242 | } | |
243 | ||
244 | devc = sdi->priv; | |
245 | ret = SR_OK; | |
246 | switch (id) { | |
247 | case SR_CONF_LIMIT_MSEC: | |
248 | devc->limit_msec = g_variant_get_uint64(data); | |
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; | |
254 | case SR_CONF_LIMIT_SAMPLES: | |
255 | devc->limit_samples = g_variant_get_uint64(data); | |
256 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
257 | devc->limit_samples); | |
258 | break; | |
259 | default: | |
260 | ret = SR_ERR_NA; | |
261 | } | |
262 | ||
263 | return ret; | |
264 | } | |
265 | ||
266 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
267 | const struct sr_channel_group *cg) | |
268 | { | |
269 | (void)sdi; | |
270 | (void)cg; | |
271 | ||
272 | switch (key) { | |
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; | |
277 | case SR_CONF_DEVICE_OPTIONS: | |
278 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
279 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
280 | break; | |
281 | default: | |
282 | return SR_ERR_NA; | |
283 | } | |
284 | ||
285 | return SR_OK; | |
286 | } | |
287 | ||
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. */ | |
298 | dev_acquisition_stop(sdi, sdi); | |
299 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { | |
300 | sr_dbg("Got %d-byte packet.", transfer->actual_length); | |
301 | if (transfer->actual_length == DMM_DATA_SIZE) { | |
302 | victor_dmm_receive_data(sdi, transfer->buffer); | |
303 | if (devc->limit_samples) { | |
304 | if (devc->num_samples >= devc->limit_samples) | |
305 | dev_acquisition_stop(sdi, sdi); | |
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)) { | |
315 | sr_err("Unable to resubmit transfer: %s.", | |
316 | libusb_error_name(ret)); | |
317 | g_free(transfer->buffer); | |
318 | libusb_free_transfer(transfer); | |
319 | dev_acquisition_stop(sdi, sdi); | |
320 | } | |
321 | } else { | |
322 | /* This was the last transfer we're going to receive, so | |
323 | * clean up now. */ | |
324 | g_free(transfer->buffer); | |
325 | libusb_free_transfer(transfer); | |
326 | } | |
327 | } | |
328 | ||
329 | static int handle_events(int fd, int revents, void *cb_data) | |
330 | { | |
331 | struct dev_context *devc; | |
332 | struct drv_context *drvc = di->priv; | |
333 | struct sr_datafeed_packet packet; | |
334 | struct sr_dev_inst *sdi; | |
335 | struct timeval tv; | |
336 | gint64 now; | |
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) | |
347 | dev_acquisition_stop(sdi, sdi); | |
348 | } | |
349 | ||
350 | if (sdi->status == SR_ST_STOPPING) { | |
351 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
352 | ||
353 | dev_close(sdi); | |
354 | ||
355 | packet.type = SR_DF_END; | |
356 | sr_session_send(cb_data, &packet); | |
357 | } | |
358 | ||
359 | memset(&tv, 0, sizeof(struct timeval)); | |
360 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, | |
361 | NULL); | |
362 | ||
363 | return TRUE; | |
364 | } | |
365 | ||
366 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
367 | { | |
368 | struct dev_context *devc; | |
369 | struct drv_context *drvc = di->priv; | |
370 | struct sr_usb_dev_inst *usb; | |
371 | struct libusb_transfer *transfer; | |
372 | int ret; | |
373 | unsigned char *buf; | |
374 | ||
375 | if (sdi->status != SR_ST_ACTIVE) | |
376 | return SR_ERR_DEV_CLOSED; | |
377 | ||
378 | if (!di->priv) { | |
379 | sr_err("Driver was not initialized."); | |
380 | return SR_ERR; | |
381 | } | |
382 | ||
383 | devc = sdi->priv; | |
384 | usb = sdi->conn; | |
385 | devc->cb_data = cb_data; | |
386 | ||
387 | /* Send header packet to the session bus. */ | |
388 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
389 | ||
390 | usb_source_add(sdi->session, drvc->sr_ctx, 100, | |
391 | handle_events, (void *)sdi); | |
392 | ||
393 | buf = g_try_malloc(DMM_DATA_SIZE); | |
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. */ | |
399 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, | |
400 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, | |
401 | cb_data, 100); | |
402 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
403 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); | |
404 | libusb_free_transfer(transfer); | |
405 | g_free(buf); | |
406 | return SR_ERR; | |
407 | } | |
408 | ||
409 | return SR_OK; | |
410 | } | |
411 | ||
412 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
413 | { | |
414 | (void)cb_data; | |
415 | ||
416 | if (!di->priv) { | |
417 | sr_err("Driver was not initialized."); | |
418 | return SR_ERR; | |
419 | } | |
420 | ||
421 | if (sdi->status != SR_ST_ACTIVE) { | |
422 | sr_err("Device not active, can't stop acquisition."); | |
423 | return SR_ERR; | |
424 | } | |
425 | ||
426 | sdi->status = SR_ST_STOPPING; | |
427 | ||
428 | return SR_OK; | |
429 | } | |
430 | ||
431 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info = { | |
432 | .name = "victor-dmm", | |
433 | .longname = "Victor DMMs", | |
434 | .api_version = 1, | |
435 | .init = init, | |
436 | .cleanup = cleanup, | |
437 | .scan = scan, | |
438 | .dev_list = dev_list, | |
439 | .dev_clear = NULL, | |
440 | .config_get = config_get, | |
441 | .config_set = config_set, | |
442 | .config_list = config_list, | |
443 | .dev_open = dev_open, | |
444 | .dev_close = dev_close, | |
445 | .dev_acquisition_start = dev_acquisition_start, | |
446 | .dev_acquisition_stop = dev_acquisition_stop, | |
447 | .priv = NULL, | |
448 | }; |