]>
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) |
d9251a2c | 34 | |
695dc859 | 35 | static int dev_acquisition_stop(struct sr_dev_inst *sdi); |
7a360375 | 36 | |
55333928 BV |
37 | static const uint32_t drvopts[] = { |
38 | SR_CONF_MULTIMETER, | |
39 | }; | |
1beccaed | 40 | |
a0e0bb41 | 41 | static const uint32_t scanopts[] = { |
ac046ef8 BV |
42 | SR_CONF_CONN, |
43 | }; | |
44 | ||
f254bc4b | 45 | static const uint32_t devopts[] = { |
1953564a | 46 | SR_CONF_CONTINUOUS, |
5827f61b BV |
47 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
48 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
55333928 | 49 | SR_CONF_CONN | SR_CONF_GET, |
7a360375 BV |
50 | }; |
51 | ||
4f840ce9 | 52 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
ac3898d2 BV |
53 | { |
54 | struct drv_context *drvc; | |
7a360375 BV |
55 | struct dev_context *devc; |
56 | struct sr_dev_inst *sdi; | |
7a360375 BV |
57 | struct libusb_device_descriptor des; |
58 | libusb_device **devlist; | |
ac3898d2 | 59 | GSList *devices; |
2a8f2d41 | 60 | int i; |
66c91e25 | 61 | char connection_id[64]; |
ac3898d2 BV |
62 | |
63 | (void)options; | |
64 | ||
41812aca | 65 | drvc = di->context; |
7a360375 | 66 | |
ac3898d2 | 67 | devices = NULL; |
d4abb463 | 68 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
7a360375 | 69 | for (i = 0; devlist[i]; i++) { |
2a8f2d41 | 70 | libusb_get_device_descriptor(devlist[i], &des); |
ac3898d2 | 71 | |
7a360375 BV |
72 | if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID) |
73 | continue; | |
74 | ||
66c91e25 SA |
75 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); |
76 | ||
aac29cc1 | 77 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
78 | sdi->status = SR_ST_INACTIVE; |
79 | sdi->vendor = g_strdup(VICTOR_VENDOR); | |
66c91e25 | 80 | sdi->connection_id = g_strdup(connection_id); |
f57d8ffe | 81 | devc = g_malloc0(sizeof(struct dev_context)); |
cf9e86bc | 82 | sr_sw_limits_init(&devc->limits); |
7a360375 BV |
83 | sdi->priv = devc; |
84 | ||
5e23fcab | 85 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
91219afc UH |
86 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]), |
87 | libusb_get_device_address(devlist[i]), NULL); | |
ac046ef8 | 88 | sdi->inst_type = SR_INST_USB; |
7a360375 | 89 | |
7a360375 BV |
90 | devices = g_slist_append(devices, sdi); |
91 | } | |
92 | libusb_free_device_list(devlist, 1); | |
ac3898d2 | 93 | |
15a5bfe4 | 94 | return std_scan_complete(di, devices); |
ac3898d2 BV |
95 | } |
96 | ||
6078d2c9 | 97 | static int dev_open(struct sr_dev_inst *sdi) |
ac3898d2 | 98 | { |
4f840ce9 | 99 | struct sr_dev_driver *di = sdi->driver; |
41812aca | 100 | struct drv_context *drvc = di->context; |
ac046ef8 | 101 | struct sr_usb_dev_inst *usb; |
4af1f68f | 102 | int ret; |
7a360375 | 103 | |
ac046ef8 BV |
104 | usb = sdi->conn; |
105 | ||
4af1f68f LPC |
106 | ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); |
107 | if (ret != SR_OK) | |
108 | return ret; | |
7a360375 BV |
109 | |
110 | /* The device reports as HID class, so the kernel would have | |
111 | * claimed it. */ | |
ac046ef8 BV |
112 | if (libusb_kernel_driver_active(usb->devhdl, 0) == 1) { |
113 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, 0)) < 0) { | |
d4928d71 PS |
114 | sr_err("Failed to detach kernel driver: %s.", |
115 | libusb_error_name(ret)); | |
7a360375 BV |
116 | return SR_ERR; |
117 | } | |
118 | } | |
119 | ||
ac046ef8 | 120 | if ((ret = libusb_claim_interface(usb->devhdl, |
7a360375 | 121 | VICTOR_INTERFACE))) { |
fa773062 | 122 | sr_err("Failed to claim interface: %s.", libusb_error_name(ret)); |
7a360375 BV |
123 | return SR_ERR; |
124 | } | |
125 | sdi->status = SR_ST_ACTIVE; | |
ac3898d2 BV |
126 | |
127 | return SR_OK; | |
128 | } | |
129 | ||
6078d2c9 | 130 | static int dev_close(struct sr_dev_inst *sdi) |
ac3898d2 | 131 | { |
ac046ef8 | 132 | struct sr_usb_dev_inst *usb; |
7a360375 | 133 | |
ac046ef8 BV |
134 | usb = sdi->conn; |
135 | ||
136 | if (!usb->devhdl) | |
d9251a2c | 137 | /* Nothing to do. */ |
7a360375 BV |
138 | return SR_OK; |
139 | ||
ac046ef8 BV |
140 | libusb_release_interface(usb->devhdl, VICTOR_INTERFACE); |
141 | libusb_close(usb->devhdl); | |
142 | usb->devhdl = NULL; | |
7a360375 | 143 | sdi->status = SR_ST_INACTIVE; |
ac3898d2 BV |
144 | |
145 | return SR_OK; | |
146 | } | |
147 | ||
584560f1 | 148 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 149 | const struct sr_channel_group *cg) |
ac046ef8 | 150 | { |
cf9e86bc | 151 | struct dev_context *devc = sdi->priv; |
ac046ef8 BV |
152 | struct sr_usb_dev_inst *usb; |
153 | char str[128]; | |
154 | ||
53b4680f | 155 | (void)cg; |
8f996b89 | 156 | |
584560f1 | 157 | switch (key) { |
ac046ef8 BV |
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; | |
cf9e86bc LPC |
165 | case SR_CONF_LIMIT_SAMPLES: |
166 | case SR_CONF_LIMIT_MSEC: | |
167 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
ac046ef8 BV |
168 | default: |
169 | return SR_ERR_NA; | |
170 | } | |
171 | ||
172 | return SR_OK; | |
173 | } | |
174 | ||
584560f1 | 175 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 176 | const struct sr_channel_group *cg) |
ac3898d2 | 177 | { |
7a360375 | 178 | struct dev_context *devc; |
ac3898d2 | 179 | |
53b4680f | 180 | (void)cg; |
8f996b89 | 181 | |
e73ffd42 BV |
182 | if (sdi->status != SR_ST_ACTIVE) |
183 | return SR_ERR_DEV_CLOSED; | |
184 | ||
7a360375 | 185 | devc = sdi->priv; |
dcd438ee | 186 | |
cf9e86bc | 187 | return sr_sw_limits_config_set(&devc->limits, key, data); |
ac3898d2 BV |
188 | } |
189 | ||
584560f1 | 190 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 191 | const struct sr_channel_group *cg) |
a1c743fc | 192 | { |
a1c743fc | 193 | (void)sdi; |
53b4680f | 194 | (void)cg; |
a1c743fc BV |
195 | |
196 | switch (key) { | |
ac046ef8 | 197 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 198 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 199 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
ac046ef8 | 200 | break; |
9a6517d1 | 201 | case SR_CONF_DEVICE_OPTIONS: |
55333928 BV |
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)); | |
9a6517d1 | 208 | break; |
a1c743fc | 209 | default: |
bd6fbf62 | 210 | return SR_ERR_NA; |
a1c743fc BV |
211 | } |
212 | ||
213 | return SR_OK; | |
214 | } | |
215 | ||
55462b8b | 216 | static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer) |
7a360375 BV |
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. */ | |
695dc859 | 226 | dev_acquisition_stop(sdi); |
7a360375 | 227 | } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) { |
fa773062 | 228 | sr_dbg("Got %d-byte packet.", transfer->actual_length); |
ff945683 BV |
229 | if (transfer->actual_length == DMM_DATA_SIZE) { |
230 | victor_dmm_receive_data(sdi, transfer->buffer); | |
cf9e86bc LPC |
231 | if (sr_sw_limits_check(&devc->limits)) |
232 | dev_acquisition_stop(sdi); | |
7a360375 BV |
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)) { | |
fa773062 UH |
241 | sr_err("Unable to resubmit transfer: %s.", |
242 | libusb_error_name(ret)); | |
7a360375 | 243 | g_free(transfer->buffer); |
9ec7ff94 | 244 | libusb_free_transfer(transfer); |
695dc859 | 245 | dev_acquisition_stop(sdi); |
7a360375 BV |
246 | } |
247 | } else { | |
248 | /* This was the last transfer we're going to receive, so | |
249 | * clean up now. */ | |
7a360375 | 250 | g_free(transfer->buffer); |
9ec7ff94 | 251 | libusb_free_transfer(transfer); |
7a360375 | 252 | } |
7a360375 BV |
253 | } |
254 | ||
255 | static int handle_events(int fd, int revents, void *cb_data) | |
256 | { | |
257 | struct dev_context *devc; | |
4f840ce9 | 258 | struct drv_context *drvc; |
7a360375 | 259 | struct sr_dev_inst *sdi; |
4f840ce9 | 260 | struct sr_dev_driver *di; |
7a360375 | 261 | struct timeval tv; |
7a360375 BV |
262 | |
263 | (void)fd; | |
264 | (void)revents; | |
265 | ||
266 | sdi = cb_data; | |
267 | devc = sdi->priv; | |
4f840ce9 | 268 | di = sdi->driver; |
41812aca | 269 | drvc = di->context; |
7a360375 | 270 | |
cf9e86bc LPC |
271 | if (sr_sw_limits_check(&devc->limits)) |
272 | dev_acquisition_stop(sdi); | |
7a360375 BV |
273 | |
274 | if (sdi->status == SR_ST_STOPPING) { | |
102f1239 | 275 | usb_source_remove(sdi->session, drvc->sr_ctx); |
6078d2c9 | 276 | dev_close(sdi); |
bee2b016 | 277 | std_session_send_df_end(sdi); |
7a360375 BV |
278 | } |
279 | ||
280 | memset(&tv, 0, sizeof(struct timeval)); | |
d4abb463 PS |
281 | libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv, |
282 | NULL); | |
7a360375 BV |
283 | |
284 | return TRUE; | |
285 | } | |
286 | ||
695dc859 | 287 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
ac3898d2 | 288 | { |
4f840ce9 | 289 | struct sr_dev_driver *di = sdi->driver; |
41812aca | 290 | struct drv_context *drvc = di->context; |
ac046ef8 | 291 | struct sr_usb_dev_inst *usb; |
7a360375 | 292 | struct libusb_transfer *transfer; |
6c60facc | 293 | int ret; |
7a360375 BV |
294 | unsigned char *buf; |
295 | ||
e73ffd42 BV |
296 | if (sdi->status != SR_ST_ACTIVE) |
297 | return SR_ERR_DEV_CLOSED; | |
298 | ||
ac046ef8 | 299 | usb = sdi->conn; |
7a360375 | 300 | |
bee2b016 | 301 | std_session_send_df_header(sdi); |
7a360375 | 302 | |
102f1239 BV |
303 | usb_source_add(sdi->session, drvc->sr_ctx, 100, |
304 | handle_events, (void *)sdi); | |
7a360375 | 305 | |
a95f142e | 306 | buf = g_malloc(DMM_DATA_SIZE); |
7a360375 BV |
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. */ | |
ac046ef8 | 312 | libusb_fill_interrupt_transfer(transfer, usb->devhdl, |
fa773062 | 313 | VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer, |
695dc859 | 314 | (struct sr_dev_inst *)sdi, 100); |
7a360375 | 315 | if ((ret = libusb_submit_transfer(transfer) != 0)) { |
fa773062 | 316 | sr_err("Unable to submit transfer: %s.", libusb_error_name(ret)); |
7a360375 BV |
317 | libusb_free_transfer(transfer); |
318 | g_free(buf); | |
319 | return SR_ERR; | |
320 | } | |
ac3898d2 BV |
321 | |
322 | return SR_OK; | |
323 | } | |
324 | ||
695dc859 | 325 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
ac3898d2 | 326 | { |
ac3898d2 | 327 | if (sdi->status != SR_ST_ACTIVE) { |
7a360375 | 328 | sr_err("Device not active, can't stop acquisition."); |
ac3898d2 BV |
329 | return SR_ERR; |
330 | } | |
331 | ||
7a360375 | 332 | sdi->status = SR_ST_STOPPING; |
ac3898d2 BV |
333 | |
334 | return SR_OK; | |
335 | } | |
336 | ||
dd5c48a6 | 337 | static struct sr_dev_driver victor_dmm_driver_info = { |
ac3898d2 | 338 | .name = "victor-dmm", |
7a360375 | 339 | .longname = "Victor DMMs", |
ac3898d2 | 340 | .api_version = 1, |
c2fdcc25 | 341 | .init = std_init, |
700d6b64 | 342 | .cleanup = std_cleanup, |
6078d2c9 | 343 | .scan = scan, |
c01bf34c | 344 | .dev_list = std_dev_list, |
a6630742 | 345 | .dev_clear = NULL, |
ac046ef8 | 346 | .config_get = config_get, |
035a1078 | 347 | .config_set = config_set, |
a1c743fc | 348 | .config_list = config_list, |
6078d2c9 UH |
349 | .dev_open = dev_open, |
350 | .dev_close = dev_close, | |
351 | .dev_acquisition_start = dev_acquisition_start, | |
352 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 353 | .context = NULL, |
ac3898d2 | 354 | }; |
dd5c48a6 | 355 | SR_REGISTER_DEV_DRIVER(victor_dmm_driver_info); |