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