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