]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_usbtmc_libusb.c
No need to check return value of libusb_get_device_descriptor().
[libsigrok.git] / src / scpi / scpi_usbtmc_libusb.c
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 <config.h>
21 #include <string.h>
22 #include <libsigrok/libsigrok.h>
23 #include "libsigrok-internal.h"
24 #include "scpi.h"
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         int rigol_ds1000;
49 };
50
51 /* Some USBTMC-specific enums, as defined in the USBTMC standard. */
52 #define SUBCLASS_USBTMC  0x03
53 #define USBTMC_USB488    0x01
54
55 enum {
56         /* USBTMC control requests */
57         INITIATE_ABORT_BULK_OUT     =   1,
58         CHECK_ABORT_BULK_OUT_STATUS =   2,
59         INITIATE_ABORT_BULK_IN      =   3,
60         CHECK_ABORT_BULK_IN_STATUS  =   4,
61         INITIATE_CLEAR              =   5,
62         CHECK_CLEAR_STATUS          =   6,
63         GET_CAPABILITIES            =   7,
64         INDICATOR_PULSE             =  64,
65
66         /* USB488 control requests */
67         READ_STATUS_BYTE            = 128,
68         REN_CONTROL                 = 160,
69         GO_TO_LOCAL                 = 161,
70         LOCAL_LOCKOUT               = 162,
71 };
72
73 /* USBTMC status codes */
74 #define USBTMC_STATUS_SUCCESS      0x01
75
76 /* USBTMC capabilities */
77 #define USBTMC_INT_CAP_LISTEN_ONLY 0x01
78 #define USBTMC_INT_CAP_TALK_ONLY   0x02
79 #define USBTMC_INT_CAP_INDICATOR   0x04
80
81 #define USBTMC_DEV_CAP_TERMCHAR    0x01
82
83 #define USB488_DEV_CAP_DT1         0x01
84 #define USB488_DEV_CAP_RL1         0x02
85 #define USB488_DEV_CAP_SR1         0x04
86 #define USB488_DEV_CAP_SCPI        0x08
87
88 /* Bulk messages constants */
89 #define USBTMC_BULK_HEADER_SIZE  12
90
91 /* Bulk MsgID values */
92 #define DEV_DEP_MSG_OUT         1
93 #define REQUEST_DEV_DEP_MSG_IN  2
94 #define DEV_DEP_MSG_IN          2
95
96 /* bmTransferAttributes */
97 #define EOM                0x01
98 #define TERM_CHAR_ENABLED  0x02
99
100 static GSList *scpi_usbtmc_libusb_scan(struct drv_context *drvc)
101 {
102         struct libusb_device **devlist;
103         struct libusb_device_descriptor des;
104         struct libusb_config_descriptor *confdes;
105         const struct libusb_interface_descriptor *intfdes;
106         GSList *resources = NULL;
107         int confidx, intfidx, ret, i;
108         char *res;
109
110         ret = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
111         if (ret < 0) {
112                 sr_err("Failed to get device list: %s.",
113                        libusb_error_name(ret));
114                 return NULL;
115         }
116         for (i = 0; devlist[i]; i++) {
117                 libusb_get_device_descriptor(devlist[i], &des);
118
119                 for (confidx = 0; confidx < des.bNumConfigurations; confidx++) {
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));
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
150 static 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
177 static 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], status;
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);
197         libusb_get_device_descriptor(dev, &des);
198
199         for (confidx = 0; confidx < des.bNumConfigurations; confidx++) {
200                 if ((ret = libusb_get_config_descriptor(dev, confidx, &confdes)) < 0) {
201                         sr_dbg("Failed to get configuration descriptor: %s, "
202                                "ignoring device.", libusb_error_name(ret));
203                         continue;
204                 }
205                 for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) {
206                         intfdes = confdes->interface[intfidx].altsetting;
207                         if (intfdes->bInterfaceClass    != LIBUSB_CLASS_APPLICATION ||
208                             intfdes->bInterfaceSubClass != SUBCLASS_USBTMC          ||
209                             intfdes->bInterfaceProtocol != USBTMC_USB488)
210                                 continue;
211                         uscpi->interface = intfdes->bInterfaceNumber;
212                         config = confdes->bConfigurationValue;
213                         sr_dbg("Interface %d configuration %d.", uscpi->interface, config);
214                         for (epidx = 0; epidx < intfdes->bNumEndpoints; epidx++) {
215                                 ep = &intfdes->endpoint[epidx];
216                                 if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK &&
217                                     !(ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK))) {
218                                         uscpi->bulk_out_ep = ep->bEndpointAddress;
219                                         sr_dbg("Bulk OUT EP %d", uscpi->bulk_out_ep);
220                                 }
221                                 if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK &&
222                                     ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) {
223                                         uscpi->bulk_in_ep = ep->bEndpointAddress;
224                                         sr_dbg("Bulk IN EP %d", uscpi->bulk_in_ep & 0x7f);
225                                 }
226                                 if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT &&
227                                     ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) {
228                                         uscpi->interrupt_ep = ep->bEndpointAddress;
229                                         sr_dbg("Interrupt EP %d", uscpi->interrupt_ep & 0x7f);
230                                 }
231                         }
232                         found = 1;
233                         uscpi->rigol_ds1000 = des.idVendor  == 0x1ab1 &&
234                                               des.idProduct == 0x0588;
235                 }
236                 libusb_free_config_descriptor(confdes);
237                 if (found)
238                         break;
239         }
240
241         if (!found) {
242                 sr_err("Failed to find USBTMC interface.");
243                 return SR_ERR;
244         }
245
246         if (libusb_kernel_driver_active(usb->devhdl, uscpi->interface) == 1) {
247                 if ((ret = libusb_detach_kernel_driver(usb->devhdl,
248                                                        uscpi->interface)) < 0) {
249                         sr_err("Failed to detach kernel driver: %s.",
250                                libusb_error_name(ret));
251                         return SR_ERR;
252                 }
253                 uscpi->detached_kernel_driver = 1;
254         }
255
256         if ((ret = libusb_set_configuration(usb->devhdl, config)) < 0) {
257                 sr_err("Failed to set configuration: %s.",
258                        libusb_error_name(ret));
259                 return SR_ERR;
260         }
261
262         if ((ret = libusb_claim_interface(usb->devhdl, uscpi->interface)) < 0) {
263                 sr_err("Failed to claim interface: %s.",
264                        libusb_error_name(ret));
265                 return SR_ERR;
266         }
267
268         if (!uscpi->rigol_ds1000) {
269         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0) {
270                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
271                        uscpi->bulk_in_ep, libusb_error_name(ret));
272                 return SR_ERR;
273         }
274         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0) {
275                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
276                        uscpi->bulk_out_ep, libusb_error_name(ret));
277                 return SR_ERR;
278         }
279         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0) {
280                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
281                        uscpi->interrupt_ep, libusb_error_name(ret));
282                 return SR_ERR;
283         }
284         }
285
286         /* Get capabilities. */
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         }
300         sr_dbg("Device capabilities: %s%s%s%s%s, %s, %s",
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         if (uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1) {
312                 sr_dbg("Locking out local control.");
313                 ret = libusb_control_transfer(usb->devhdl,
314                                 LIBUSB_ENDPOINT_IN         |
315                                 LIBUSB_REQUEST_TYPE_CLASS  |
316                                 LIBUSB_RECIPIENT_INTERFACE,
317                                 REN_CONTROL, 1,
318                                 uscpi->interface,
319                                 &status, 1,
320                                 TRANSFER_TIMEOUT);
321                 if (ret < 0 || status != USBTMC_STATUS_SUCCESS) {
322                         sr_dbg("Failed to enter REN state.");
323                         return SR_OK;
324                 }
325                 ret = libusb_control_transfer(usb->devhdl,
326                                 LIBUSB_ENDPOINT_IN         |
327                                 LIBUSB_REQUEST_TYPE_CLASS  |
328                                 LIBUSB_RECIPIENT_INTERFACE,
329                                 LOCAL_LOCKOUT, 1,
330                                 uscpi->interface,
331                                 &status, 1,
332                                 TRANSFER_TIMEOUT);
333                 if (ret < 0 || status != USBTMC_STATUS_SUCCESS)
334                         sr_dbg("Failed to enter local lockout state.");
335         }
336
337         return SR_OK;
338 }
339
340 static int scpi_usbtmc_libusb_source_add(struct sr_session *session,
341                 void *priv, int events, int timeout, sr_receive_data_callback cb,
342                 void *cb_data)
343 {
344         struct scpi_usbtmc_libusb *uscpi = priv;
345         (void)events;
346         return usb_source_add(session, uscpi->ctx, timeout, cb, cb_data);
347 }
348
349 static int scpi_usbtmc_libusb_source_remove(struct sr_session *session,
350                 void *priv)
351 {
352         struct scpi_usbtmc_libusb *uscpi = priv;
353         return usb_source_remove(session, uscpi->ctx);
354 }
355
356 static void usbtmc_bulk_out_header_write(void *header, uint8_t MsgID,
357                                          uint8_t bTag,
358                                          uint32_t TransferSize,
359                                          uint8_t bmTransferAttributes,
360                                          char TermChar)
361 {
362           W8(header+ 0, MsgID);
363           W8(header+ 1, bTag);
364           W8(header+ 2, ~bTag);
365           W8(header+ 3, 0);
366         WL32(header+ 4, TransferSize);
367           W8(header+ 8, bmTransferAttributes);
368           W8(header+ 9, TermChar);
369         WL16(header+10, 0);
370 }
371
372 static int usbtmc_bulk_in_header_read(void *header, uint8_t MsgID,
373                                       unsigned char bTag,
374                                       int32_t *TransferSize,
375                                       uint8_t *bmTransferAttributes)
376 {
377         if (R8(header+0) != MsgID ||
378             R8(header+1) != bTag  ||
379             R8(header+2) != (unsigned char)~bTag)
380                 return SR_ERR;
381         if (TransferSize)
382                 *TransferSize = RL32(header+4);
383         if (bmTransferAttributes)
384                 *bmTransferAttributes = R8(header+8);
385         return SR_OK;
386 }
387
388 static int scpi_usbtmc_bulkout(struct scpi_usbtmc_libusb *uscpi,
389                                uint8_t msg_id, const void *data, int32_t size,
390                                uint8_t transfer_attributes)
391 {
392         struct sr_usb_dev_inst *usb = uscpi->usb;
393         int padded_size, ret, transferred;
394
395         if (data && size+USBTMC_BULK_HEADER_SIZE+3 > (int)sizeof(uscpi->buffer)) {
396                 sr_err("USBTMC bulk out transfer is too big.");
397                 return SR_ERR;
398         }
399
400         uscpi->bTag++;
401         uscpi->bTag += !uscpi->bTag;  /* bTag == 0 is invalid so avoid it. */
402
403         usbtmc_bulk_out_header_write(uscpi->buffer, msg_id, uscpi->bTag,
404                                      size, transfer_attributes, 0);
405         if (data)
406                 memcpy(uscpi->buffer+USBTMC_BULK_HEADER_SIZE, data, size);
407         else
408                 size = 0;
409         size += USBTMC_BULK_HEADER_SIZE;
410         padded_size = (size + 3) & ~0x3;
411         memset(uscpi->buffer+size, 0, padded_size - size);
412
413         ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_out_ep,
414                                    uscpi->buffer, padded_size, &transferred,
415                                    TRANSFER_TIMEOUT);
416         if (ret < 0) {
417                 sr_err("USBTMC bulk out transfer error: %s.",
418                        libusb_error_name(ret));
419                 return SR_ERR;
420         }
421
422         if (transferred < padded_size) {
423                 sr_dbg("USBTMC bulk out partial transfer (%d/%d bytes).",
424                        transferred, padded_size);
425                 return SR_ERR;
426         }
427
428         return transferred - USBTMC_BULK_HEADER_SIZE;
429 }
430
431 static int scpi_usbtmc_bulkin_start(struct scpi_usbtmc_libusb *uscpi,
432                                     uint8_t msg_id, void *data, int32_t size,
433                                     uint8_t *transfer_attributes)
434 {
435         struct sr_usb_dev_inst *usb = uscpi->usb;
436         int ret, transferred, message_size;
437
438         ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size,
439                                    &transferred, TRANSFER_TIMEOUT);
440         if (ret < 0) {
441                 sr_err("USBTMC bulk in transfer error: %s.",
442                        libusb_error_name(ret));
443                 return SR_ERR;
444         }
445
446         if (usbtmc_bulk_in_header_read(data, msg_id, uscpi->bTag, &message_size,
447                                        transfer_attributes) != SR_OK) {
448                 sr_err("USBTMC invalid bulk in header.");
449                 return SR_ERR;
450         }
451
452         message_size += USBTMC_BULK_HEADER_SIZE;
453         uscpi->response_length = MIN(transferred, message_size);
454         uscpi->response_bytes_read = USBTMC_BULK_HEADER_SIZE;
455         uscpi->remaining_length = message_size - uscpi->response_length;
456
457         return transferred - USBTMC_BULK_HEADER_SIZE;
458 }
459
460 static int scpi_usbtmc_bulkin_continue(struct scpi_usbtmc_libusb *uscpi,
461                                        void *data, int size)
462 {
463         struct sr_usb_dev_inst *usb = uscpi->usb;
464         int ret, transferred;
465
466         ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size,
467                                    &transferred, TRANSFER_TIMEOUT);
468         if (ret < 0) {
469                 sr_err("USBTMC bulk in transfer error: %s.",
470                        libusb_error_name(ret));
471                 return SR_ERR;
472         }
473
474         uscpi->response_length = MIN(transferred, uscpi->remaining_length);
475         uscpi->response_bytes_read = 0;
476         uscpi->remaining_length -= uscpi->response_length;
477
478         return transferred;
479 }
480
481 static int scpi_usbtmc_libusb_send(void *priv, const char *command)
482 {
483         struct scpi_usbtmc_libusb *uscpi = priv;
484
485         if (scpi_usbtmc_bulkout(uscpi, DEV_DEP_MSG_OUT,
486                                 command, strlen(command), EOM) <= 0)
487                 return SR_ERR;
488
489         sr_spew("Successfully sent SCPI command: '%s'.", command);
490
491         return SR_OK;
492 }
493
494 static int scpi_usbtmc_libusb_read_begin(void *priv)
495 {
496         struct scpi_usbtmc_libusb *uscpi = priv;
497
498         uscpi->remaining_length = 0;
499
500         if (scpi_usbtmc_bulkout(uscpi, REQUEST_DEV_DEP_MSG_IN,
501             NULL, INT32_MAX, 0) < 0)
502                 return SR_ERR;
503         if (scpi_usbtmc_bulkin_start(uscpi, DEV_DEP_MSG_IN,
504                                      uscpi->buffer, sizeof(uscpi->buffer),
505                                      &uscpi->bulkin_attributes) < 0)
506                 return SR_ERR;
507
508         return SR_OK;
509 }
510
511 static int scpi_usbtmc_libusb_read_data(void *priv, char *buf, int maxlen)
512 {
513         struct scpi_usbtmc_libusb *uscpi = priv;
514         int read_length;
515
516         if (uscpi->response_bytes_read >= uscpi->response_length) {
517                 if (uscpi->remaining_length > 0) {
518                         if (scpi_usbtmc_bulkin_continue(uscpi, uscpi->buffer,
519                                                         sizeof(uscpi->buffer)) <= 0)
520                                 return SR_ERR;
521                 } else {
522                         if (uscpi->bulkin_attributes & EOM)
523                                 return SR_ERR;
524                         if (scpi_usbtmc_libusb_read_begin(uscpi) < 0)
525                                 return SR_ERR;
526                 }
527         }
528
529         read_length = MIN(uscpi->response_length - uscpi->response_bytes_read, maxlen);
530
531         memcpy(buf, uscpi->buffer + uscpi->response_bytes_read, read_length);
532
533         uscpi->response_bytes_read += read_length;
534
535         return read_length;
536 }
537
538 static int scpi_usbtmc_libusb_read_complete(void *priv)
539 {
540         struct scpi_usbtmc_libusb *uscpi = priv;
541         return uscpi->response_bytes_read >= uscpi->response_length &&
542                uscpi->remaining_length <= 0 &&
543                uscpi->bulkin_attributes & EOM;
544 }
545
546 static int scpi_usbtmc_libusb_close(void *priv)
547 {
548         int ret;
549         struct scpi_usbtmc_libusb *uscpi = priv;
550         struct sr_usb_dev_inst *usb = uscpi->usb;
551         uint8_t status;
552
553         if (!usb->devhdl)
554                 return SR_ERR;
555
556         if (!uscpi->rigol_ds1000) {
557         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0)
558                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
559                        uscpi->bulk_in_ep, libusb_error_name(ret));
560         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0)
561                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
562                        uscpi->bulk_out_ep, libusb_error_name(ret));
563         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0)
564                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
565                        uscpi->interrupt_ep, libusb_error_name(ret));
566         }
567
568         if (uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1) {
569                 sr_dbg("Returning local control.");
570                 ret = libusb_control_transfer(usb->devhdl,
571                                 LIBUSB_ENDPOINT_IN         |
572                                 LIBUSB_REQUEST_TYPE_CLASS  |
573                                 LIBUSB_RECIPIENT_INTERFACE,
574                                 GO_TO_LOCAL, 1,
575                                 uscpi->interface,
576                                 &status, 1,
577                                 TRANSFER_TIMEOUT);
578                 if (ret < 0 || status != USBTMC_STATUS_SUCCESS)
579                         sr_dbg("Failed to clear local lockout state.");
580         }
581
582         if ((ret = libusb_release_interface(usb->devhdl, uscpi->interface)) < 0)
583                 sr_err("Failed to release interface: %s.",
584                        libusb_error_name(ret));
585         
586         if (uscpi->detached_kernel_driver) {
587                 if ((ret = libusb_attach_kernel_driver(usb->devhdl,
588                                                 uscpi->interface)) < 0)
589                         sr_err("Failed to re-attach kernel driver: %s.",
590                                libusb_error_name(ret));
591
592                 uscpi->detached_kernel_driver = 0;
593         }
594         sr_usb_close(usb);
595
596         return SR_OK;
597 }
598
599 static void scpi_usbtmc_libusb_free(void *priv)
600 {
601         struct scpi_usbtmc_libusb *uscpi = priv;
602         sr_usb_dev_inst_free(uscpi->usb);
603 }
604
605 SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev = {
606         .name          = "USBTMC",
607         .prefix        = "usbtmc",
608         .priv_size     = sizeof(struct scpi_usbtmc_libusb),
609         .scan          = scpi_usbtmc_libusb_scan,
610         .dev_inst_new  = scpi_usbtmc_libusb_dev_inst_new,
611         .open          = scpi_usbtmc_libusb_open,
612         .source_add    = scpi_usbtmc_libusb_source_add,
613         .source_remove = scpi_usbtmc_libusb_source_remove,
614         .send          = scpi_usbtmc_libusb_send,
615         .read_begin    = scpi_usbtmc_libusb_read_begin,
616         .read_data     = scpi_usbtmc_libusb_read_data,
617         .read_complete = scpi_usbtmc_libusb_read_complete,
618         .close         = scpi_usbtmc_libusb_close,
619         .free          = scpi_usbtmc_libusb_free,
620 };