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