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