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