]>
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 <config.h> | |
21 | #include <glib.h> | |
22 | #include <libusb.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <libsigrok/libsigrok.h> | |
26 | #include "libsigrok-internal.h" | |
27 | #include "protocol.h" | |
28 | ||
29 | #define VICTOR_VID 0x1244 | |
30 | #define VICTOR_PID 0xd237 | |
31 | #define VICTOR_VENDOR "Victor" | |
32 | #define VICTOR_INTERFACE 0 | |
33 | #define VICTOR_ENDPOINT (LIBUSB_ENDPOINT_IN | 1) | |
34 | ||
35 | static int dev_acquisition_stop(struct sr_dev_inst *sdi); | |
36 | ||
37 | static const uint32_t drvopts[] = { | |
38 | SR_CONF_MULTIMETER, | |
39 | }; | |
40 | ||
41 | static const uint32_t scanopts[] = { | |
42 | SR_CONF_CONN, | |
43 | }; | |
44 | ||
45 | static const uint32_t devopts[] = { | |
46 | SR_CONF_CONTINUOUS, | |
47 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
48 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
49 | SR_CONF_CONN | SR_CONF_GET, | |
50 | }; | |
51 | ||
52 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
53 | { | |
54 | struct drv_context *drvc; | |
55 | struct dev_context *devc; | |
56 | struct sr_dev_inst *sdi; | |
57 | struct libusb_device_descriptor des; | |
58 | libusb_device **devlist; | |
59 | GSList *devices; | |
60 | int i; | |
61 | char connection_id[64]; | |
62 | ||
63 | (void)options; | |
64 | ||
65 | drvc = di->context; | |
66 | ||
67 | devices = NULL; | |
68 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
69 | for (i = 0; devlist[i]; i++) { | |
70 | libusb_get_device_descriptor(devlist[i], &des); | |
71 | ||
72 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) | |
73 | continue; | |
74 | ||
75 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
76 | ||
77 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
78 | sdi->status = SR_ST_INACTIVE; | |
79 | sdi->vendor = g_strdup(VICTOR_VENDOR); | |
80 | sdi->connection_id = g_strdup(connection_id); | |
81 | devc = g_malloc0(sizeof(struct dev_context)); | |
82 | sr_sw_limits_init(&devc->limits); | |
83 | sdi->priv = devc; | |
84 | ||
85 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
86 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), | |
87 | libusb_get_device_address(devlist[i]), NULL); | |
88 | sdi->inst_type = SR_INST_USB; | |
89 | ||
90 | devices = g_slist_append(devices, sdi); | |
91 | } | |
92 | libusb_free_device_list(devlist, 1); | |
93 | ||
94 | return std_scan_complete(di, devices); | |
95 | } | |
96 | ||
97 | static int dev_open(struct sr_dev_inst *sdi) | |
98 | { | |
99 | struct sr_dev_driver *di = sdi->driver; | |
100 | struct drv_context *drvc = di->context; | |
101 | struct sr_usb_dev_inst *usb; | |
102 | int ret; | |
103 | ||
104 | usb = sdi->conn; | |
105 | ||
106 | ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); | |
107 | if (ret != SR_OK) | |
108 | return ret; | |
109 | ||
110 | /* The device reports as HID class, so the kernel would have | |
111 | * claimed it. */ | |
112 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { | |
113 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { | |
114 | sr_err("Failed to detach kernel driver: %s.", | |
115 | libusb_error_name(ret)); | |
116 | return SR_ERR; | |
117 | } | |
118 | } | |
119 | ||
120 | if ((ret = libusb_claim_interface(usb->devhdl, | |
121 | VICTOR_INTERFACE))) { | |
122 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); | |
123 | return SR_ERR; | |
124 | } | |
125 | sdi->status = SR_ST_ACTIVE; | |
126 | ||
127 | return SR_OK; | |
128 | } | |
129 | ||
130 | static int dev_close(struct sr_dev_inst *sdi) | |
131 | { | |
132 | struct sr_usb_dev_inst *usb; | |
133 | ||
134 | usb = sdi->conn; | |
135 | ||
136 | if (!usb->devhdl) | |
137 | /* Nothing to do. */ | |
138 | return SR_OK; | |
139 | ||
140 | libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); | |
141 | libusb_close(usb->devhdl); | |
142 | usb->devhdl = NULL; | |
143 | sdi->status = SR_ST_INACTIVE; | |
144 | ||
145 | return SR_OK; | |
146 | } | |
147 | ||
148 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
149 | const struct sr_channel_group *cg) | |
150 | { | |
151 | struct dev_context *devc = sdi->priv; | |
152 | struct sr_usb_dev_inst *usb; | |
153 | char str[128]; | |
154 | ||
155 | (void)cg; | |
156 | ||
157 | switch (key) { | |
158 | case SR_CONF_CONN: | |
159 | if (!sdi || !sdi->conn) | |
160 | return SR_ERR_ARG; | |
161 | usb = sdi->conn; | |
162 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
163 | *data = g_variant_new_string(str); | |
164 | break; | |
165 | case SR_CONF_LIMIT_SAMPLES: | |
166 | case SR_CONF_LIMIT_MSEC: | |
167 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
168 | default: | |
169 | return SR_ERR_NA; | |
170 | } | |
171 | ||
172 | return SR_OK; | |
173 | } | |
174 | ||
175 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
176 | const struct sr_channel_group *cg) | |
177 | { | |
178 | struct dev_context *devc; | |
179 | ||
180 | (void)cg; | |
181 | ||
182 | if (sdi->status != SR_ST_ACTIVE) | |
183 | return SR_ERR_DEV_CLOSED; | |
184 | ||
185 | devc = sdi->priv; | |
186 | ||
187 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
188 | } | |
189 | ||
190 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
191 | const struct sr_channel_group *cg) | |
192 | { | |
193 | (void)sdi; | |
194 | (void)cg; | |
195 | ||
196 | switch (key) { | |
197 | case SR_CONF_SCAN_OPTIONS: | |
198 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
199 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
200 | break; | |
201 | case SR_CONF_DEVICE_OPTIONS: | |
202 | if (!sdi) | |
203 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
204 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
205 | else | |
206 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
207 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
208 | break; | |
209 | default: | |
210 | return SR_ERR_NA; | |
211 | } | |
212 | ||
213 | return SR_OK; | |
214 | } | |
215 | ||
216 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) | |
217 | { | |
218 | struct dev_context *devc; | |
219 | struct sr_dev_inst *sdi; | |
220 | int ret; | |
221 | ||
222 | sdi = transfer->user_data; | |
223 | devc = sdi->priv; | |
224 | if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) { | |
225 | /* USB device was unplugged. */ | |
226 | dev_acquisition_stop(sdi); | |
227 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { | |
228 | sr_dbg("Got %d-byte packet.", transfer->actual_length); | |
229 | if (transfer->actual_length == DMM_DATA_SIZE) { | |
230 | victor_dmm_receive_data(sdi, transfer->buffer); | |
231 | if (sr_sw_limits_check(&devc->limits)) | |
232 | dev_acquisition_stop(sdi); | |
233 | } | |
234 | } | |
235 | /* Anything else is either an error or a timeout, which is fine: | |
236 | * we were just going to send another transfer request anyway. */ | |
237 | ||
238 | if (sdi->status == SR_ST_ACTIVE) { | |
239 | /* Send the same request again. */ | |
240 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
241 | sr_err("Unable to resubmit transfer: %s.", | |
242 | libusb_error_name(ret)); | |
243 | g_free(transfer->buffer); | |
244 | libusb_free_transfer(transfer); | |
245 | dev_acquisition_stop(sdi); | |
246 | } | |
247 | } else { | |
248 | /* This was the last transfer we're going to receive, so | |
249 | * clean up now. */ | |
250 | g_free(transfer->buffer); | |
251 | libusb_free_transfer(transfer); | |
252 | } | |
253 | } | |
254 | ||
255 | static int handle_events(int fd, int revents, void *cb_data) | |
256 | { | |
257 | struct dev_context *devc; | |
258 | struct drv_context *drvc; | |
259 | struct sr_dev_inst *sdi; | |
260 | struct sr_dev_driver *di; | |
261 | struct timeval tv; | |
262 | ||
263 | (void)fd; | |
264 | (void)revents; | |
265 | ||
266 | sdi = cb_data; | |
267 | devc = sdi->priv; | |
268 | di = sdi->driver; | |
269 | drvc = di->context; | |
270 | ||
271 | if (sr_sw_limits_check(&devc->limits)) | |
272 | dev_acquisition_stop(sdi); | |
273 | ||
274 | if (sdi->status == SR_ST_STOPPING) { | |
275 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
276 | dev_close(sdi); | |
277 | std_session_send_df_end(sdi); | |
278 | } | |
279 | ||
280 | memset(&tv, 0, sizeof(struct timeval)); | |
281 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, | |
282 | NULL); | |
283 | ||
284 | return TRUE; | |
285 | } | |
286 | ||
287 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
288 | { | |
289 | struct sr_dev_driver *di = sdi->driver; | |
290 | struct drv_context *drvc = di->context; | |
291 | struct sr_usb_dev_inst *usb; | |
292 | struct libusb_transfer *transfer; | |
293 | int ret; | |
294 | unsigned char *buf; | |
295 | ||
296 | if (sdi->status != SR_ST_ACTIVE) | |
297 | return SR_ERR_DEV_CLOSED; | |
298 | ||
299 | usb = sdi->conn; | |
300 | ||
301 | std_session_send_df_header(sdi); | |
302 | ||
303 | usb_source_add(sdi->session, drvc->sr_ctx, 100, | |
304 | handle_events, (void *)sdi); | |
305 | ||
306 | buf = g_malloc(DMM_DATA_SIZE); | |
307 | transfer = libusb_alloc_transfer(0); | |
308 | /* Each transfer request gets 100ms to arrive before it's restarted. | |
309 | * The device only sends 1 transfer/second no matter how many | |
310 | * times you ask, but we want to keep step with the USB events | |
311 | * handling above. */ | |
312 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, | |
313 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, | |
314 | (struct sr_dev_inst *)sdi, 100); | |
315 | if ((ret = libusb_submit_transfer(transfer) != 0)) { | |
316 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); | |
317 | libusb_free_transfer(transfer); | |
318 | g_free(buf); | |
319 | return SR_ERR; | |
320 | } | |
321 | ||
322 | return SR_OK; | |
323 | } | |
324 | ||
325 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
326 | { | |
327 | if (sdi->status != SR_ST_ACTIVE) { | |
328 | sr_err("Device not active, can't stop acquisition."); | |
329 | return SR_ERR; | |
330 | } | |
331 | ||
332 | sdi->status = SR_ST_STOPPING; | |
333 | ||
334 | return SR_OK; | |
335 | } | |
336 | ||
337 | static struct sr_dev_driver victor_dmm_driver_info = { | |
338 | .name = "victor-dmm", | |
339 | .longname = "Victor DMMs", | |
340 | .api_version = 1, | |
341 | .init = std_init, | |
342 | .cleanup = std_cleanup, | |
343 | .scan = scan, | |
344 | .dev_list = std_dev_list, | |
345 | .dev_clear = NULL, | |
346 | .config_get = config_get, | |
347 | .config_set = config_set, | |
348 | .config_list = config_list, | |
349 | .dev_open = dev_open, | |
350 | .dev_close = dev_close, | |
351 | .dev_acquisition_start = dev_acquisition_start, | |
352 | .dev_acquisition_stop = dev_acquisition_stop, | |
353 | .context = NULL, | |
354 | }; | |
355 | SR_REGISTER_DEV_DRIVER(victor_dmm_driver_info); |