]>
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 uint32_t scanopts[] = { | |
39 | SR_CONF_CONN, | |
40 | }; | |
41 | ||
42 | static const uint32_t devopts[] = { | |
43 | SR_CONF_MULTIMETER, | |
44 | SR_CONF_CONTINUOUS, | |
45 | SR_CONF_CONN | SR_CONF_GET, | |
46 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
47 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
48 | }; | |
49 | ||
50 | static int init(struct sr_context *sr_ctx) | |
51 | { | |
52 | return std_init(sr_ctx, di, LOG_PREFIX); | |
53 | } | |
54 | ||
55 | static GSList *scan(GSList *options) | |
56 | { | |
57 | struct drv_context *drvc; | |
58 | struct dev_context *devc; | |
59 | struct sr_dev_inst *sdi; | |
60 | struct sr_channel *ch; | |
61 | struct libusb_device_descriptor des; | |
62 | libusb_device **devlist; | |
63 | GSList *devices; | |
64 | int ret, i; | |
65 | char connection_id[64]; | |
66 | ||
67 | (void)options; | |
68 | ||
69 | drvc = di->priv; | |
70 | ||
71 | devices = NULL; | |
72 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
73 | for (i = 0; devlist[i]; i++) { | |
74 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) { | |
75 | sr_warn("Failed to get device descriptor: %s", | |
76 | libusb_error_name(ret)); | |
77 | continue; | |
78 | } | |
79 | ||
80 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) | |
81 | continue; | |
82 | ||
83 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
84 | ||
85 | if (!(sdi = sr_dev_inst_new(SR_ST_INACTIVE, | |
86 | VICTOR_VENDOR, NULL, NULL))) | |
87 | return NULL; | |
88 | sdi->driver = di; | |
89 | sdi->connection_id = g_strdup(connection_id); | |
90 | ||
91 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) | |
92 | return NULL; | |
93 | sdi->priv = devc; | |
94 | ||
95 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"))) | |
96 | return NULL; | |
97 | sdi->channels = g_slist_append(NULL, ch); | |
98 | ||
99 | if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
100 | libusb_get_device_address(devlist[i]), NULL))) | |
101 | return NULL; | |
102 | sdi->inst_type = SR_INST_USB; | |
103 | ||
104 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
105 | devices = g_slist_append(devices, sdi); | |
106 | } | |
107 | libusb_free_device_list(devlist, 1); | |
108 | ||
109 | return devices; | |
110 | } | |
111 | ||
112 | static GSList *dev_list(void) | |
113 | { | |
114 | return ((struct drv_context *)(di->priv))->instances; | |
115 | } | |
116 | ||
117 | static int dev_open(struct sr_dev_inst *sdi) | |
118 | { | |
119 | struct drv_context *drvc = di->priv; | |
120 | struct sr_usb_dev_inst *usb; | |
121 | libusb_device **devlist; | |
122 | int ret, i; | |
123 | char connection_id[64]; | |
124 | ||
125 | if (!di->priv) { | |
126 | sr_err("Driver was not initialized."); | |
127 | return SR_ERR; | |
128 | } | |
129 | ||
130 | usb = sdi->conn; | |
131 | ||
132 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
133 | for (i = 0; devlist[i]; i++) { | |
134 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
135 | if (strcmp(sdi->connection_id, connection_id)) | |
136 | continue; | |
137 | if ((ret = libusb_open(devlist[i], &usb->devhdl))) { | |
138 | sr_err("Failed to open device: %s.", libusb_error_name(ret)); | |
139 | return SR_ERR; | |
140 | } | |
141 | break; | |
142 | } | |
143 | libusb_free_device_list(devlist, 1); | |
144 | if (!devlist[i]) { | |
145 | sr_err("Device not found."); | |
146 | return SR_ERR; | |
147 | } | |
148 | ||
149 | /* The device reports as HID class, so the kernel would have | |
150 | * claimed it. */ | |
151 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { | |
152 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { | |
153 | sr_err("Failed to detach kernel driver: %s.", | |
154 | libusb_error_name(ret)); | |
155 | return SR_ERR; | |
156 | } | |
157 | } | |
158 | ||
159 | if ((ret = libusb_claim_interface(usb->devhdl, | |
160 | VICTOR_INTERFACE))) { | |
161 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); | |
162 | return SR_ERR; | |
163 | } | |
164 | sdi->status = SR_ST_ACTIVE; | |
165 | ||
166 | return SR_OK; | |
167 | } | |
168 | ||
169 | static int dev_close(struct sr_dev_inst *sdi) | |
170 | { | |
171 | struct sr_usb_dev_inst *usb; | |
172 | ||
173 | if (!di->priv) { | |
174 | sr_err("Driver was not initialized."); | |
175 | return SR_ERR; | |
176 | } | |
177 | ||
178 | usb = sdi->conn; | |
179 | ||
180 | if (!usb->devhdl) | |
181 | /* Nothing to do. */ | |
182 | return SR_OK; | |
183 | ||
184 | libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); | |
185 | libusb_close(usb->devhdl); | |
186 | usb->devhdl = NULL; | |
187 | sdi->status = SR_ST_INACTIVE; | |
188 | ||
189 | return SR_OK; | |
190 | } | |
191 | ||
192 | static int cleanup(void) | |
193 | { | |
194 | int ret; | |
195 | struct drv_context *drvc; | |
196 | ||
197 | if (!(drvc = di->priv)) | |
198 | /* Can get called on an unused driver, doesn't matter. */ | |
199 | return SR_OK; | |
200 | ||
201 | ||
202 | ret = std_dev_clear(di, NULL); | |
203 | g_free(drvc); | |
204 | di->priv = NULL; | |
205 | ||
206 | return ret; | |
207 | } | |
208 | ||
209 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
210 | const struct sr_channel_group *cg) | |
211 | { | |
212 | struct sr_usb_dev_inst *usb; | |
213 | char str[128]; | |
214 | ||
215 | (void)cg; | |
216 | ||
217 | switch (key) { | |
218 | case SR_CONF_CONN: | |
219 | if (!sdi || !sdi->conn) | |
220 | return SR_ERR_ARG; | |
221 | usb = sdi->conn; | |
222 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
223 | *data = g_variant_new_string(str); | |
224 | break; | |
225 | default: | |
226 | return SR_ERR_NA; | |
227 | } | |
228 | ||
229 | return SR_OK; | |
230 | } | |
231 | ||
232 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
233 | const struct sr_channel_group *cg) | |
234 | { | |
235 | struct dev_context *devc; | |
236 | gint64 now; | |
237 | int ret; | |
238 | ||
239 | (void)cg; | |
240 | ||
241 | if (sdi->status != SR_ST_ACTIVE) | |
242 | return SR_ERR_DEV_CLOSED; | |
243 | ||
244 | if (!di->priv) { | |
245 | sr_err("Driver was not initialized."); | |
246 | return SR_ERR; | |
247 | } | |
248 | ||
249 | devc = sdi->priv; | |
250 | ret = SR_OK; | |
251 | switch (key) { | |
252 | case SR_CONF_LIMIT_MSEC: | |
253 | devc->limit_msec = g_variant_get_uint64(data); | |
254 | now = g_get_monotonic_time() / 1000; | |
255 | devc->end_time = now + devc->limit_msec; | |
256 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
257 | devc->limit_msec); | |
258 | break; | |
259 | case SR_CONF_LIMIT_SAMPLES: | |
260 | devc->limit_samples = g_variant_get_uint64(data); | |
261 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
262 | devc->limit_samples); | |
263 | break; | |
264 | default: | |
265 | ret = SR_ERR_NA; | |
266 | } | |
267 | ||
268 | return ret; | |
269 | } | |
270 | ||
271 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
272 | const struct sr_channel_group *cg) | |
273 | { | |
274 | (void)sdi; | |
275 | (void)cg; | |
276 | ||
277 | switch (key) { | |
278 | case SR_CONF_SCAN_OPTIONS: | |
279 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
280 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
281 | break; | |
282 | case SR_CONF_DEVICE_OPTIONS: | |
283 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
284 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
285 | break; | |
286 | default: | |
287 | return SR_ERR_NA; | |
288 | } | |
289 | ||
290 | return SR_OK; | |
291 | } | |
292 | ||
293 | static void receive_transfer(struct libusb_transfer *transfer) | |
294 | { | |
295 | struct dev_context *devc; | |
296 | struct sr_dev_inst *sdi; | |
297 | int ret; | |
298 | ||
299 | sdi = transfer->user_data; | |
300 | devc = sdi->priv; | |
301 | if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) { | |
302 | /* USB device was unplugged. */ | |
303 | dev_acquisition_stop(sdi, sdi); | |
304 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { | |
305 | sr_dbg("Got %d-byte packet.", transfer->actual_length); | |
306 | if (transfer->actual_length == DMM_DATA_SIZE) { | |
307 | victor_dmm_receive_data(sdi, transfer->buffer); | |
308 | if (devc->limit_samples) { | |
309 | if (devc->num_samples >= devc->limit_samples) | |
310 | dev_acquisition_stop(sdi, sdi); | |
311 | } | |
312 | } | |
313 | } | |
314 | /* Anything else is either an error or a timeout, which is fine: | |
315 | * we were just going to send another transfer request anyway. */ | |
316 | ||
317 | if (sdi->status == SR_ST_ACTIVE) { | |
318 | /* Send the same request again. */ | |
319 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
320 | sr_err("Unable to resubmit transfer: %s.", | |
321 | libusb_error_name(ret)); | |
322 | g_free(transfer->buffer); | |
323 | libusb_free_transfer(transfer); | |
324 | dev_acquisition_stop(sdi, sdi); | |
325 | } | |
326 | } else { | |
327 | /* This was the last transfer we're going to receive, so | |
328 | * clean up now. */ | |
329 | g_free(transfer->buffer); | |
330 | libusb_free_transfer(transfer); | |
331 | } | |
332 | } | |
333 | ||
334 | static int handle_events(int fd, int revents, void *cb_data) | |
335 | { | |
336 | struct dev_context *devc; | |
337 | struct drv_context *drvc = di->priv; | |
338 | struct sr_datafeed_packet packet; | |
339 | struct sr_dev_inst *sdi; | |
340 | struct timeval tv; | |
341 | gint64 now; | |
342 | ||
343 | (void)fd; | |
344 | (void)revents; | |
345 | ||
346 | sdi = cb_data; | |
347 | devc = sdi->priv; | |
348 | ||
349 | if (devc->limit_msec) { | |
350 | now = g_get_monotonic_time() / 1000; | |
351 | if (now > devc->end_time) | |
352 | dev_acquisition_stop(sdi, sdi); | |
353 | } | |
354 | ||
355 | if (sdi->status == SR_ST_STOPPING) { | |
356 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
357 | ||
358 | dev_close(sdi); | |
359 | ||
360 | packet.type = SR_DF_END; | |
361 | sr_session_send(cb_data, &packet); | |
362 | } | |
363 | ||
364 | memset(&tv, 0, sizeof(struct timeval)); | |
365 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, | |
366 | NULL); | |
367 | ||
368 | return TRUE; | |
369 | } | |
370 | ||
371 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
372 | { | |
373 | struct dev_context *devc; | |
374 | struct drv_context *drvc = di->priv; | |
375 | struct sr_usb_dev_inst *usb; | |
376 | struct libusb_transfer *transfer; | |
377 | int ret; | |
378 | unsigned char *buf; | |
379 | ||
380 | if (sdi->status != SR_ST_ACTIVE) | |
381 | return SR_ERR_DEV_CLOSED; | |
382 | ||
383 | if (!di->priv) { | |
384 | sr_err("Driver was not initialized."); | |
385 | return SR_ERR; | |
386 | } | |
387 | ||
388 | devc = sdi->priv; | |
389 | usb = sdi->conn; | |
390 | devc->cb_data = cb_data; | |
391 | ||
392 | /* Send header packet to the session bus. */ | |
393 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
394 | ||
395 | usb_source_add(sdi->session, drvc->sr_ctx, 100, | |
396 | handle_events, (void *)sdi); | |
397 | ||
398 | buf = g_try_malloc(DMM_DATA_SIZE); | |
399 | transfer = libusb_alloc_transfer(0); | |
400 | /* Each transfer request gets 100ms to arrive before it's restarted. | |
401 | * The device only sends 1 transfer/second no matter how many | |
402 | * times you ask, but we want to keep step with the USB events | |
403 | * handling above. */ | |
404 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, | |
405 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, | |
406 | cb_data, 100); | |
407 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
408 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); | |
409 | libusb_free_transfer(transfer); | |
410 | g_free(buf); | |
411 | return SR_ERR; | |
412 | } | |
413 | ||
414 | return SR_OK; | |
415 | } | |
416 | ||
417 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
418 | { | |
419 | (void)cb_data; | |
420 | ||
421 | if (!di->priv) { | |
422 | sr_err("Driver was not initialized."); | |
423 | return SR_ERR; | |
424 | } | |
425 | ||
426 | if (sdi->status != SR_ST_ACTIVE) { | |
427 | sr_err("Device not active, can't stop acquisition."); | |
428 | return SR_ERR; | |
429 | } | |
430 | ||
431 | sdi->status = SR_ST_STOPPING; | |
432 | ||
433 | return SR_OK; | |
434 | } | |
435 | ||
436 | SR_PRIV struct sr_dev_driver victor_dmm_driver_info = { | |
437 | .name = "victor-dmm", | |
438 | .longname = "Victor DMMs", | |
439 | .api_version = 1, | |
440 | .init = init, | |
441 | .cleanup = cleanup, | |
442 | .scan = scan, | |
443 | .dev_list = dev_list, | |
444 | .dev_clear = NULL, | |
445 | .config_get = config_get, | |
446 | .config_set = config_set, | |
447 | .config_list = config_list, | |
448 | .dev_open = dev_open, | |
449 | .dev_close = dev_close, | |
450 | .dev_acquisition_start = dev_acquisition_start, | |
451 | .dev_acquisition_stop = dev_acquisition_stop, | |
452 | .priv = NULL, | |
453 | }; |