]>
Commit | Line | Data |
---|---|---|
20ed3cee AJ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org> | |
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> |
20ed3cee | 21 | #include <string.h> |
c1aae900 | 22 | #include <libsigrok/libsigrok.h> |
20ed3cee | 23 | #include "libsigrok-internal.h" |
5a1afc09 | 24 | #include "scpi.h" |
20ed3cee AJ |
25 | |
26 | #define LOG_PREFIX "scpi_usbtmc" | |
27 | ||
28 | #define MAX_TRANSFER_LENGTH 2048 | |
29 | #define TRANSFER_TIMEOUT 1000 | |
30 | ||
31 | struct scpi_usbtmc_libusb { | |
32 | struct sr_context *ctx; | |
33 | struct sr_usb_dev_inst *usb; | |
34 | int detached_kernel_driver; | |
35 | uint8_t interface; | |
36 | uint8_t bulk_in_ep; | |
37 | uint8_t bulk_out_ep; | |
38 | uint8_t interrupt_ep; | |
39 | uint8_t usbtmc_int_cap; | |
40 | uint8_t usbtmc_dev_cap; | |
41 | uint8_t usb488_dev_cap; | |
42 | uint8_t bTag; | |
43 | uint8_t bulkin_attributes; | |
44 | uint8_t buffer[MAX_TRANSFER_LENGTH]; | |
45 | int response_length; | |
46 | int response_bytes_read; | |
47 | int remaining_length; | |
48 | }; | |
49 | ||
50 | /* Some USBTMC-specific enums, as defined in the USBTMC standard. */ | |
d9251a2c UH |
51 | #define SUBCLASS_USBTMC 0x03 |
52 | #define USBTMC_USB488 0x01 | |
20ed3cee AJ |
53 | |
54 | enum { | |
55 | /* USBTMC control requests */ | |
56 | INITIATE_ABORT_BULK_OUT = 1, | |
57 | CHECK_ABORT_BULK_OUT_STATUS = 2, | |
58 | INITIATE_ABORT_BULK_IN = 3, | |
59 | CHECK_ABORT_BULK_IN_STATUS = 4, | |
60 | INITIATE_CLEAR = 5, | |
61 | CHECK_CLEAR_STATUS = 6, | |
62 | GET_CAPABILITIES = 7, | |
63 | INDICATOR_PULSE = 64, | |
64 | ||
65 | /* USB488 control requests */ | |
66 | READ_STATUS_BYTE = 128, | |
67 | REN_CONTROL = 160, | |
68 | GO_TO_LOCAL = 161, | |
69 | LOCAL_LOCKOUT = 162, | |
70 | }; | |
71 | ||
baed0211 BV |
72 | /* USBTMC status codes */ |
73 | #define USBTMC_STATUS_SUCCESS 0x01 | |
74 | ||
20ed3cee AJ |
75 | /* USBTMC capabilities */ |
76 | #define USBTMC_INT_CAP_LISTEN_ONLY 0x01 | |
77 | #define USBTMC_INT_CAP_TALK_ONLY 0x02 | |
78 | #define USBTMC_INT_CAP_INDICATOR 0x04 | |
79 | ||
80 | #define USBTMC_DEV_CAP_TERMCHAR 0x01 | |
81 | ||
82 | #define USB488_DEV_CAP_DT1 0x01 | |
83 | #define USB488_DEV_CAP_RL1 0x02 | |
84 | #define USB488_DEV_CAP_SR1 0x04 | |
85 | #define USB488_DEV_CAP_SCPI 0x08 | |
86 | ||
87 | /* Bulk messages constants */ | |
d9251a2c | 88 | #define USBTMC_BULK_HEADER_SIZE 12 |
20ed3cee AJ |
89 | |
90 | /* Bulk MsgID values */ | |
d9251a2c UH |
91 | #define DEV_DEP_MSG_OUT 1 |
92 | #define REQUEST_DEV_DEP_MSG_IN 2 | |
93 | #define DEV_DEP_MSG_IN 2 | |
20ed3cee AJ |
94 | |
95 | /* bmTransferAttributes */ | |
d9251a2c UH |
96 | #define EOM 0x01 |
97 | #define TERM_CHAR_ENABLED 0x02 | |
20ed3cee | 98 | |
11582652 BV |
99 | struct usbtmc_blacklist { |
100 | uint16_t vid; | |
101 | uint16_t pid; | |
102 | }; | |
103 | ||
e40e9ca2 | 104 | /* Devices that publish RL1 support, but don't support it. */ |
11582652 | 105 | static struct usbtmc_blacklist blacklist_remote[] = { |
d9251a2c UH |
106 | { 0x1ab1, 0x0588 }, /* Rigol DS1000 series */ |
107 | { 0x1ab1, 0x04b0 }, /* Rigol DS2000 series */ | |
108 | { 0x0957, 0x0588 }, /* Agilent DSO1000 series (rebadged Rigol DS1000) */ | |
109 | { 0x0b21, 0xffff }, /* All Yokogawa devices */ | |
11582652 BV |
110 | ALL_ZERO |
111 | }; | |
112 | ||
20ed3cee AJ |
113 | static GSList *scpi_usbtmc_libusb_scan(struct drv_context *drvc) |
114 | { | |
115 | struct libusb_device **devlist; | |
116 | struct libusb_device_descriptor des; | |
117 | struct libusb_config_descriptor *confdes; | |
118 | const struct libusb_interface_descriptor *intfdes; | |
119 | GSList *resources = NULL; | |
120 | int confidx, intfidx, ret, i; | |
121 | char *res; | |
122 | ||
52f6951c UH |
123 | ret = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
124 | if (ret < 0) { | |
125 | sr_err("Failed to get device list: %s.", | |
126 | libusb_error_name(ret)); | |
127 | return NULL; | |
128 | } | |
20ed3cee | 129 | for (i = 0; devlist[i]; i++) { |
2a8f2d41 | 130 | libusb_get_device_descriptor(devlist[i], &des); |
20ed3cee AJ |
131 | |
132 | for (confidx = 0; confidx < des.bNumConfigurations; confidx++) { | |
9ad05e6c UH |
133 | if ((ret = libusb_get_config_descriptor(devlist[i], confidx, &confdes)) < 0) { |
134 | sr_dbg("Failed to get configuration descriptor: %s, " | |
135 | "ignoring device.", libusb_error_name(ret)); | |
20ed3cee AJ |
136 | break; |
137 | } | |
138 | for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) { | |
139 | intfdes = confdes->interface[intfidx].altsetting; | |
140 | if (intfdes->bInterfaceClass != LIBUSB_CLASS_APPLICATION || | |
141 | intfdes->bInterfaceSubClass != SUBCLASS_USBTMC || | |
142 | intfdes->bInterfaceProtocol != USBTMC_USB488) | |
143 | continue; | |
144 | sr_dbg("Found USBTMC device (VID:PID = %04x:%04x, " | |
145 | "bus.address = %d.%d).", des.idVendor, des.idProduct, | |
146 | libusb_get_bus_number(devlist[i]), | |
147 | libusb_get_device_address(devlist[i])); | |
148 | res = g_strdup_printf("usbtmc/%d.%d", | |
149 | libusb_get_bus_number(devlist[i]), | |
150 | libusb_get_device_address(devlist[i])); | |
151 | resources = g_slist_append(resources, res); | |
152 | } | |
153 | libusb_free_config_descriptor(confdes); | |
154 | } | |
155 | } | |
156 | libusb_free_device_list(devlist, 1); | |
157 | ||
91057d2f | 158 | /* No log message for #devices found (caller will log that). */ |
20ed3cee AJ |
159 | |
160 | return resources; | |
161 | } | |
162 | ||
163 | static int scpi_usbtmc_libusb_dev_inst_new(void *priv, struct drv_context *drvc, | |
164 | const char *resource, char **params, const char *serialcomm) | |
165 | { | |
166 | struct scpi_usbtmc_libusb *uscpi = priv; | |
167 | GSList *devices; | |
168 | ||
169 | (void)resource; | |
170 | (void)serialcomm; | |
171 | ||
172 | if (!params || !params[1]) { | |
173 | sr_err("Invalid parameters."); | |
174 | return SR_ERR; | |
175 | } | |
176 | ||
177 | uscpi->ctx = drvc->sr_ctx; | |
178 | devices = sr_usb_find(uscpi->ctx->libusb_ctx, params[1]); | |
179 | if (g_slist_length(devices) != 1) { | |
180 | sr_err("Failed to find USB device '%s'.", params[1]); | |
181 | g_slist_free_full(devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
182 | return SR_ERR; | |
183 | } | |
184 | uscpi->usb = devices->data; | |
185 | g_slist_free(devices); | |
186 | ||
187 | return SR_OK; | |
188 | } | |
189 | ||
11582652 BV |
190 | static int check_usbtmc_blacklist(struct usbtmc_blacklist *blacklist, |
191 | uint16_t vid, uint16_t pid) | |
192 | { | |
193 | int i; | |
194 | ||
195 | for (i = 0; blacklist[i].vid; i++) { | |
a084a8f2 SA |
196 | if ((blacklist[i].vid == vid && blacklist[i].pid == 0xFFFF) || |
197 | (blacklist[i].vid == vid && blacklist[i].pid == pid)) | |
11582652 BV |
198 | return TRUE; |
199 | } | |
200 | ||
201 | return FALSE; | |
202 | } | |
203 | ||
204 | static int scpi_usbtmc_remote(struct scpi_usbtmc_libusb *uscpi) | |
205 | { | |
206 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
207 | struct libusb_device *dev; | |
208 | struct libusb_device_descriptor des; | |
209 | int ret; | |
210 | uint8_t status; | |
211 | ||
212 | if (!(uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1)) | |
213 | return SR_OK; | |
214 | ||
215 | dev = libusb_get_device(usb->devhdl); | |
216 | libusb_get_device_descriptor(dev, &des); | |
217 | if (check_usbtmc_blacklist(blacklist_remote, des.idVendor, des.idProduct)) | |
218 | return SR_OK; | |
219 | ||
220 | sr_dbg("Locking out local control."); | |
d9251a2c UH |
221 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | |
222 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, | |
223 | REN_CONTROL, 1, uscpi->interface, &status, 1, TRANSFER_TIMEOUT); | |
11582652 BV |
224 | if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { |
225 | if (ret < 0) | |
226 | sr_dbg("Failed to enter REN state: %s.", libusb_error_name(ret)); | |
227 | else | |
228 | sr_dbg("Failed to enter REN state: USBTMC status %d.", status); | |
229 | return SR_ERR; | |
230 | } | |
231 | ||
d9251a2c UH |
232 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | |
233 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, | |
234 | LOCAL_LOCKOUT, 0, uscpi->interface, &status, 1, | |
235 | TRANSFER_TIMEOUT); | |
11582652 BV |
236 | if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { |
237 | if (ret < 0) | |
238 | sr_dbg("Failed to enter local lockout state: %s.", | |
239 | libusb_error_name(ret)); | |
240 | else | |
241 | sr_dbg("Failed to enter local lockout state: USBTMC " | |
242 | "status %d.", status); | |
243 | return SR_ERR; | |
244 | } | |
245 | ||
246 | return SR_OK; | |
247 | } | |
248 | ||
249 | static void scpi_usbtmc_local(struct scpi_usbtmc_libusb *uscpi) | |
250 | { | |
251 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
252 | struct libusb_device *dev; | |
253 | struct libusb_device_descriptor des; | |
254 | int ret; | |
255 | uint8_t status; | |
256 | ||
257 | if (!(uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1)) | |
258 | return; | |
259 | ||
260 | dev = libusb_get_device(usb->devhdl); | |
261 | libusb_get_device_descriptor(dev, &des); | |
262 | if (check_usbtmc_blacklist(blacklist_remote, des.idVendor, des.idProduct)) | |
263 | return; | |
264 | ||
265 | sr_dbg("Returning local control."); | |
d9251a2c UH |
266 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | |
267 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, | |
268 | GO_TO_LOCAL, 0, uscpi->interface, &status, 1, TRANSFER_TIMEOUT); | |
11582652 BV |
269 | if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { |
270 | if (ret < 0) | |
271 | sr_dbg("Failed to clear local lockout state: %s.", | |
272 | libusb_error_name(ret)); | |
273 | else | |
274 | sr_dbg("Failed to clear local lockout state: USBTMC " | |
275 | "status %d.", status); | |
276 | } | |
277 | ||
278 | return; | |
279 | } | |
280 | ||
04229f7b | 281 | static int scpi_usbtmc_libusb_open(struct sr_scpi_dev_inst *scpi) |
20ed3cee | 282 | { |
04229f7b | 283 | struct scpi_usbtmc_libusb *uscpi = scpi->priv; |
20ed3cee AJ |
284 | struct sr_usb_dev_inst *usb = uscpi->usb; |
285 | struct libusb_device *dev; | |
286 | struct libusb_device_descriptor des; | |
287 | struct libusb_config_descriptor *confdes; | |
288 | const struct libusb_interface_descriptor *intfdes; | |
289 | const struct libusb_endpoint_descriptor *ep; | |
938bdc25 | 290 | int confidx, intfidx, epidx, config = 0, current_config; |
11582652 | 291 | uint8_t capabilities[24]; |
20ed3cee AJ |
292 | int ret, found = 0; |
293 | ||
294 | if (usb->devhdl) | |
295 | return SR_OK; | |
296 | ||
297 | if (sr_usb_open(uscpi->ctx->libusb_ctx, usb) != SR_OK) | |
298 | return SR_ERR; | |
299 | ||
300 | dev = libusb_get_device(usb->devhdl); | |
2a8f2d41 | 301 | libusb_get_device_descriptor(dev, &des); |
20ed3cee AJ |
302 | |
303 | for (confidx = 0; confidx < des.bNumConfigurations; confidx++) { | |
9ad05e6c UH |
304 | if ((ret = libusb_get_config_descriptor(dev, confidx, &confdes)) < 0) { |
305 | sr_dbg("Failed to get configuration descriptor: %s, " | |
306 | "ignoring device.", libusb_error_name(ret)); | |
20ed3cee AJ |
307 | continue; |
308 | } | |
309 | for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) { | |
310 | intfdes = confdes->interface[intfidx].altsetting; | |
311 | if (intfdes->bInterfaceClass != LIBUSB_CLASS_APPLICATION || | |
d9251a2c | 312 | intfdes->bInterfaceSubClass != SUBCLASS_USBTMC || |
20ed3cee AJ |
313 | intfdes->bInterfaceProtocol != USBTMC_USB488) |
314 | continue; | |
315 | uscpi->interface = intfdes->bInterfaceNumber; | |
20ed3cee | 316 | config = confdes->bConfigurationValue; |
e82d34a9 | 317 | sr_dbg("Interface %d configuration %d.", uscpi->interface, config); |
20ed3cee AJ |
318 | for (epidx = 0; epidx < intfdes->bNumEndpoints; epidx++) { |
319 | ep = &intfdes->endpoint[epidx]; | |
320 | if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK && | |
321 | !(ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK))) { | |
322 | uscpi->bulk_out_ep = ep->bEndpointAddress; | |
323 | sr_dbg("Bulk OUT EP %d", uscpi->bulk_out_ep); | |
324 | } | |
325 | if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK && | |
326 | ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) { | |
327 | uscpi->bulk_in_ep = ep->bEndpointAddress; | |
e82d34a9 | 328 | sr_dbg("Bulk IN EP %d", uscpi->bulk_in_ep & 0x7f); |
20ed3cee AJ |
329 | } |
330 | if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT && | |
331 | ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) { | |
332 | uscpi->interrupt_ep = ep->bEndpointAddress; | |
e82d34a9 | 333 | sr_dbg("Interrupt EP %d", uscpi->interrupt_ep & 0x7f); |
20ed3cee AJ |
334 | } |
335 | } | |
336 | found = 1; | |
337 | } | |
338 | libusb_free_config_descriptor(confdes); | |
339 | if (found) | |
340 | break; | |
341 | } | |
342 | ||
343 | if (!found) { | |
52f6951c | 344 | sr_err("Failed to find USBTMC interface."); |
20ed3cee AJ |
345 | return SR_ERR; |
346 | } | |
347 | ||
348 | if (libusb_kernel_driver_active(usb->devhdl, uscpi->interface) == 1) { | |
349 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, | |
350 | uscpi->interface)) < 0) { | |
52f6951c UH |
351 | sr_err("Failed to detach kernel driver: %s.", |
352 | libusb_error_name(ret)); | |
20ed3cee AJ |
353 | return SR_ERR; |
354 | } | |
355 | uscpi->detached_kernel_driver = 1; | |
356 | } | |
357 | ||
938bdc25 AJ |
358 | if (libusb_get_configuration(usb->devhdl, ¤t_config) == 0 |
359 | && current_config != config) { | |
360 | if ((ret = libusb_set_configuration(usb->devhdl, config)) < 0) { | |
361 | sr_err("Failed to set configuration: %s.", | |
362 | libusb_error_name(ret)); | |
363 | return SR_ERR; | |
364 | } | |
20ed3cee AJ |
365 | } |
366 | ||
d8cbd659 | 367 | if ((ret = libusb_claim_interface(usb->devhdl, uscpi->interface)) < 0) { |
52f6951c UH |
368 | sr_err("Failed to claim interface: %s.", |
369 | libusb_error_name(ret)); | |
20ed3cee AJ |
370 | return SR_ERR; |
371 | } | |
372 | ||
52f6951c | 373 | /* Get capabilities. */ |
d9251a2c UH |
374 | ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | |
375 | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, | |
376 | GET_CAPABILITIES, 0, uscpi->interface, capabilities, | |
377 | sizeof(capabilities), TRANSFER_TIMEOUT); | |
20ed3cee AJ |
378 | if (ret == sizeof(capabilities)) { |
379 | uscpi->usbtmc_int_cap = capabilities[ 4]; | |
380 | uscpi->usbtmc_dev_cap = capabilities[ 5]; | |
381 | uscpi->usb488_dev_cap = capabilities[15]; | |
382 | } | |
52f6951c | 383 | sr_dbg("Device capabilities: %s%s%s%s%s, %s, %s", |
d9251a2c UH |
384 | uscpi->usb488_dev_cap & USB488_DEV_CAP_SCPI ? "SCPI, " : "", |
385 | uscpi->usbtmc_dev_cap & USBTMC_DEV_CAP_TERMCHAR ? "TermChar, ": "", | |
386 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY ? "L3, " : | |
387 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "" : "L4, ", | |
388 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "T5, " : | |
389 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY ? "" : "T6, ", | |
390 | uscpi->usb488_dev_cap & USB488_DEV_CAP_SR1 ? "SR1" : "SR0", | |
391 | uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1 ? "RL1" : "RL0", | |
392 | uscpi->usb488_dev_cap & USB488_DEV_CAP_DT1 ? "DT1" : "DT0"); | |
20ed3cee | 393 | |
11582652 | 394 | scpi_usbtmc_remote(uscpi); |
baed0211 | 395 | |
20ed3cee AJ |
396 | return SR_OK; |
397 | } | |
398 | ||
102f1239 BV |
399 | static int scpi_usbtmc_libusb_source_add(struct sr_session *session, |
400 | void *priv, int events, int timeout, sr_receive_data_callback cb, | |
401 | void *cb_data) | |
20ed3cee AJ |
402 | { |
403 | struct scpi_usbtmc_libusb *uscpi = priv; | |
404 | (void)events; | |
102f1239 | 405 | return usb_source_add(session, uscpi->ctx, timeout, cb, cb_data); |
20ed3cee AJ |
406 | } |
407 | ||
102f1239 BV |
408 | static int scpi_usbtmc_libusb_source_remove(struct sr_session *session, |
409 | void *priv) | |
20ed3cee AJ |
410 | { |
411 | struct scpi_usbtmc_libusb *uscpi = priv; | |
102f1239 | 412 | return usb_source_remove(session, uscpi->ctx); |
20ed3cee AJ |
413 | } |
414 | ||
415 | static void usbtmc_bulk_out_header_write(void *header, uint8_t MsgID, | |
416 | uint8_t bTag, | |
417 | uint32_t TransferSize, | |
418 | uint8_t bmTransferAttributes, | |
419 | char TermChar) | |
420 | { | |
d9251a2c UH |
421 | W8(header + 0, MsgID); |
422 | W8(header + 1, bTag); | |
423 | W8(header + 2, ~bTag); | |
424 | W8(header + 3, 0); | |
425 | WL32(header + 4, TransferSize); | |
426 | W8(header + 8, bmTransferAttributes); | |
427 | W8(header + 9, TermChar); | |
428 | WL16(header + 10, 0); | |
20ed3cee AJ |
429 | } |
430 | ||
431 | static int usbtmc_bulk_in_header_read(void *header, uint8_t MsgID, | |
432 | unsigned char bTag, | |
433 | int32_t *TransferSize, | |
434 | uint8_t *bmTransferAttributes) | |
435 | { | |
d9251a2c UH |
436 | if (R8(header + 0) != MsgID || |
437 | R8(header + 1) != bTag || | |
438 | R8(header + 2) != (unsigned char)~bTag) | |
20ed3cee AJ |
439 | return SR_ERR; |
440 | if (TransferSize) | |
d9251a2c | 441 | *TransferSize = RL32(header + 4); |
20ed3cee | 442 | if (bmTransferAttributes) |
d9251a2c UH |
443 | *bmTransferAttributes = R8(header + 8); |
444 | ||
20ed3cee AJ |
445 | return SR_OK; |
446 | } | |
447 | ||
448 | static int scpi_usbtmc_bulkout(struct scpi_usbtmc_libusb *uscpi, | |
449 | uint8_t msg_id, const void *data, int32_t size, | |
450 | uint8_t transfer_attributes) | |
451 | { | |
452 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
453 | int padded_size, ret, transferred; | |
454 | ||
d9251a2c | 455 | if (data && (size + USBTMC_BULK_HEADER_SIZE + 3) > (int)sizeof(uscpi->buffer)) { |
52f6951c | 456 | sr_err("USBTMC bulk out transfer is too big."); |
20ed3cee AJ |
457 | return SR_ERR; |
458 | } | |
459 | ||
460 | uscpi->bTag++; | |
d9251a2c | 461 | uscpi->bTag += !uscpi->bTag; /* bTag == 0 is invalid so avoid it. */ |
20ed3cee AJ |
462 | |
463 | usbtmc_bulk_out_header_write(uscpi->buffer, msg_id, uscpi->bTag, | |
464 | size, transfer_attributes, 0); | |
465 | if (data) | |
d9251a2c | 466 | memcpy(uscpi->buffer + USBTMC_BULK_HEADER_SIZE, data, size); |
20ed3cee AJ |
467 | else |
468 | size = 0; | |
469 | size += USBTMC_BULK_HEADER_SIZE; | |
470 | padded_size = (size + 3) & ~0x3; | |
d9251a2c | 471 | memset(uscpi->buffer + size, 0, padded_size - size); |
20ed3cee AJ |
472 | |
473 | ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_out_ep, | |
474 | uscpi->buffer, padded_size, &transferred, | |
475 | TRANSFER_TIMEOUT); | |
d8cbd659 | 476 | if (ret < 0) { |
52f6951c UH |
477 | sr_err("USBTMC bulk out transfer error: %s.", |
478 | libusb_error_name(ret)); | |
20ed3cee AJ |
479 | return SR_ERR; |
480 | } | |
481 | ||
482 | if (transferred < padded_size) { | |
52f6951c | 483 | sr_dbg("USBTMC bulk out partial transfer (%d/%d bytes).", |
20ed3cee AJ |
484 | transferred, padded_size); |
485 | return SR_ERR; | |
486 | } | |
487 | ||
488 | return transferred - USBTMC_BULK_HEADER_SIZE; | |
489 | } | |
490 | ||
491 | static int scpi_usbtmc_bulkin_start(struct scpi_usbtmc_libusb *uscpi, | |
492 | uint8_t msg_id, void *data, int32_t size, | |
493 | uint8_t *transfer_attributes) | |
494 | { | |
495 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
496 | int ret, transferred, message_size; | |
497 | ||
498 | ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size, | |
499 | &transferred, TRANSFER_TIMEOUT); | |
d8cbd659 | 500 | if (ret < 0) { |
52f6951c UH |
501 | sr_err("USBTMC bulk in transfer error: %s.", |
502 | libusb_error_name(ret)); | |
20ed3cee AJ |
503 | return SR_ERR; |
504 | } | |
505 | ||
506 | if (usbtmc_bulk_in_header_read(data, msg_id, uscpi->bTag, &message_size, | |
507 | transfer_attributes) != SR_OK) { | |
52f6951c | 508 | sr_err("USBTMC invalid bulk in header."); |
20ed3cee AJ |
509 | return SR_ERR; |
510 | } | |
511 | ||
512 | message_size += USBTMC_BULK_HEADER_SIZE; | |
513 | uscpi->response_length = MIN(transferred, message_size); | |
514 | uscpi->response_bytes_read = USBTMC_BULK_HEADER_SIZE; | |
515 | uscpi->remaining_length = message_size - uscpi->response_length; | |
516 | ||
517 | return transferred - USBTMC_BULK_HEADER_SIZE; | |
518 | } | |
519 | ||
520 | static int scpi_usbtmc_bulkin_continue(struct scpi_usbtmc_libusb *uscpi, | |
521 | void *data, int size) | |
522 | { | |
523 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
524 | int ret, transferred; | |
525 | ||
526 | ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size, | |
527 | &transferred, TRANSFER_TIMEOUT); | |
d8cbd659 | 528 | if (ret < 0) { |
52f6951c UH |
529 | sr_err("USBTMC bulk in transfer error: %s.", |
530 | libusb_error_name(ret)); | |
20ed3cee AJ |
531 | return SR_ERR; |
532 | } | |
533 | ||
534 | uscpi->response_length = MIN(transferred, uscpi->remaining_length); | |
535 | uscpi->response_bytes_read = 0; | |
536 | uscpi->remaining_length -= uscpi->response_length; | |
537 | ||
538 | return transferred; | |
539 | } | |
540 | ||
541 | static int scpi_usbtmc_libusb_send(void *priv, const char *command) | |
542 | { | |
543 | struct scpi_usbtmc_libusb *uscpi = priv; | |
544 | ||
545 | if (scpi_usbtmc_bulkout(uscpi, DEV_DEP_MSG_OUT, | |
546 | command, strlen(command), EOM) <= 0) | |
547 | return SR_ERR; | |
548 | ||
549 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
550 | ||
551 | return SR_OK; | |
552 | } | |
553 | ||
554 | static int scpi_usbtmc_libusb_read_begin(void *priv) | |
555 | { | |
556 | struct scpi_usbtmc_libusb *uscpi = priv; | |
557 | ||
558 | uscpi->remaining_length = 0; | |
559 | ||
560 | if (scpi_usbtmc_bulkout(uscpi, REQUEST_DEV_DEP_MSG_IN, | |
561 | NULL, INT32_MAX, 0) < 0) | |
562 | return SR_ERR; | |
563 | if (scpi_usbtmc_bulkin_start(uscpi, DEV_DEP_MSG_IN, | |
564 | uscpi->buffer, sizeof(uscpi->buffer), | |
565 | &uscpi->bulkin_attributes) < 0) | |
566 | return SR_ERR; | |
567 | ||
568 | return SR_OK; | |
569 | } | |
570 | ||
571 | static int scpi_usbtmc_libusb_read_data(void *priv, char *buf, int maxlen) | |
572 | { | |
573 | struct scpi_usbtmc_libusb *uscpi = priv; | |
574 | int read_length; | |
575 | ||
576 | if (uscpi->response_bytes_read >= uscpi->response_length) { | |
577 | if (uscpi->remaining_length > 0) { | |
578 | if (scpi_usbtmc_bulkin_continue(uscpi, uscpi->buffer, | |
579 | sizeof(uscpi->buffer)) <= 0) | |
580 | return SR_ERR; | |
581 | } else { | |
582 | if (uscpi->bulkin_attributes & EOM) | |
583 | return SR_ERR; | |
584 | if (scpi_usbtmc_libusb_read_begin(uscpi) < 0) | |
585 | return SR_ERR; | |
586 | } | |
587 | } | |
588 | ||
589 | read_length = MIN(uscpi->response_length - uscpi->response_bytes_read, maxlen); | |
590 | ||
591 | memcpy(buf, uscpi->buffer + uscpi->response_bytes_read, read_length); | |
592 | ||
593 | uscpi->response_bytes_read += read_length; | |
594 | ||
595 | return read_length; | |
596 | } | |
597 | ||
598 | static int scpi_usbtmc_libusb_read_complete(void *priv) | |
599 | { | |
600 | struct scpi_usbtmc_libusb *uscpi = priv; | |
601 | return uscpi->response_bytes_read >= uscpi->response_length && | |
602 | uscpi->remaining_length <= 0 && | |
603 | uscpi->bulkin_attributes & EOM; | |
604 | } | |
605 | ||
04229f7b | 606 | static int scpi_usbtmc_libusb_close(struct sr_scpi_dev_inst *scpi) |
20ed3cee | 607 | { |
04229f7b | 608 | struct scpi_usbtmc_libusb *uscpi = scpi->priv; |
20ed3cee | 609 | struct sr_usb_dev_inst *usb = uscpi->usb; |
04229f7b | 610 | int ret; |
20ed3cee AJ |
611 | |
612 | if (!usb->devhdl) | |
613 | return SR_ERR; | |
614 | ||
11582652 | 615 | scpi_usbtmc_local(uscpi); |
baed0211 | 616 | |
52f6951c UH |
617 | if ((ret = libusb_release_interface(usb->devhdl, uscpi->interface)) < 0) |
618 | sr_err("Failed to release interface: %s.", | |
619 | libusb_error_name(ret)); | |
176d785d | 620 | |
20ed3cee | 621 | if (uscpi->detached_kernel_driver) { |
52f6951c UH |
622 | if ((ret = libusb_attach_kernel_driver(usb->devhdl, |
623 | uscpi->interface)) < 0) | |
624 | sr_err("Failed to re-attach kernel driver: %s.", | |
625 | libusb_error_name(ret)); | |
626 | ||
20ed3cee AJ |
627 | uscpi->detached_kernel_driver = 0; |
628 | } | |
0b2b92f6 | 629 | sr_usb_close(usb); |
20ed3cee AJ |
630 | |
631 | return SR_OK; | |
632 | } | |
633 | ||
634 | static void scpi_usbtmc_libusb_free(void *priv) | |
635 | { | |
636 | struct scpi_usbtmc_libusb *uscpi = priv; | |
637 | sr_usb_dev_inst_free(uscpi->usb); | |
638 | } | |
639 | ||
20ed3cee AJ |
640 | SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev = { |
641 | .name = "USBTMC", | |
642 | .prefix = "usbtmc", | |
643 | .priv_size = sizeof(struct scpi_usbtmc_libusb), | |
644 | .scan = scpi_usbtmc_libusb_scan, | |
645 | .dev_inst_new = scpi_usbtmc_libusb_dev_inst_new, | |
646 | .open = scpi_usbtmc_libusb_open, | |
647 | .source_add = scpi_usbtmc_libusb_source_add, | |
648 | .source_remove = scpi_usbtmc_libusb_source_remove, | |
649 | .send = scpi_usbtmc_libusb_send, | |
650 | .read_begin = scpi_usbtmc_libusb_read_begin, | |
651 | .read_data = scpi_usbtmc_libusb_read_data, | |
652 | .read_complete = scpi_usbtmc_libusb_read_complete, | |
653 | .close = scpi_usbtmc_libusb_close, | |
654 | .free = scpi_usbtmc_libusb_free, | |
655 | }; |