]> sigrok.org Git - libsigrok.git/blob - src/scpi/scpi_usbtmc_libusb.c
Build: Set local include directories in Makefile.am
[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 <string.h>
21 #include <libsigrok/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         int rigol_ds1000;
47 };
48
49 /* Some USBTMC-specific enums, as defined in the USBTMC standard. */
50 #define SUBCLASS_USBTMC  0x03
51 #define USBTMC_USB488    0x01
52
53 enum {
54         /* USBTMC control requests */
55         INITIATE_ABORT_BULK_OUT     =   1,
56         CHECK_ABORT_BULK_OUT_STATUS =   2,
57         INITIATE_ABORT_BULK_IN      =   3,
58         CHECK_ABORT_BULK_IN_STATUS  =   4,
59         INITIATE_CLEAR              =   5,
60         CHECK_CLEAR_STATUS          =   6,
61         GET_CAPABILITIES            =   7,
62         INDICATOR_PULSE             =  64,
63
64         /* USB488 control requests */
65         READ_STATUS_BYTE            = 128,
66         REN_CONTROL                 = 160,
67         GO_TO_LOCAL                 = 161,
68         LOCAL_LOCKOUT               = 162,
69 };
70
71 /* USBTMC capabilities */
72 #define USBTMC_INT_CAP_LISTEN_ONLY 0x01
73 #define USBTMC_INT_CAP_TALK_ONLY   0x02
74 #define USBTMC_INT_CAP_INDICATOR   0x04
75
76 #define USBTMC_DEV_CAP_TERMCHAR    0x01
77
78 #define USB488_DEV_CAP_DT1         0x01
79 #define USB488_DEV_CAP_RL1         0x02
80 #define USB488_DEV_CAP_SR1         0x04
81 #define USB488_DEV_CAP_SCPI        0x08
82
83 /* Bulk messages constants */
84 #define USBTMC_BULK_HEADER_SIZE  12
85
86 /* Bulk MsgID values */
87 #define DEV_DEP_MSG_OUT         1
88 #define REQUEST_DEV_DEP_MSG_IN  2
89 #define DEV_DEP_MSG_IN          2
90
91 /* bmTransferAttributes */
92 #define EOM                0x01
93 #define TERM_CHAR_ENABLED  0x02
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
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         }
111         for (i = 0; devlist[i]; i++) {
112                 if ((ret = libusb_get_device_descriptor(devlist[i], &des)) < 0) {
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++) {
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));
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);
196         if ((ret = libusb_get_device_descriptor(dev, &des)) < 0) {
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++) {
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));
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                         config = confdes->bConfigurationValue;
216                         sr_dbg("Interface %d configuration %d.", uscpi->interface, config);
217                         for (epidx = 0; epidx < intfdes->bNumEndpoints; epidx++) {
218                                 ep = &intfdes->endpoint[epidx];
219                                 if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK &&
220                                     !(ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK))) {
221                                         uscpi->bulk_out_ep = ep->bEndpointAddress;
222                                         sr_dbg("Bulk OUT EP %d", uscpi->bulk_out_ep);
223                                 }
224                                 if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK &&
225                                     ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) {
226                                         uscpi->bulk_in_ep = ep->bEndpointAddress;
227                                         sr_dbg("Bulk IN EP %d", uscpi->bulk_in_ep & 0x7f);
228                                 }
229                                 if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT &&
230                                     ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) {
231                                         uscpi->interrupt_ep = ep->bEndpointAddress;
232                                         sr_dbg("Interrupt EP %d", uscpi->interrupt_ep & 0x7f);
233                                 }
234                         }
235                         found = 1;
236                         uscpi->rigol_ds1000 = des.idVendor  == 0x1ab1 &&
237                                               des.idProduct == 0x0588;
238                 }
239                 libusb_free_config_descriptor(confdes);
240                 if (found)
241                         break;
242         }
243
244         if (!found) {
245                 sr_err("Failed to find USBTMC interface.");
246                 return SR_ERR;
247         }
248
249         if (libusb_kernel_driver_active(usb->devhdl, uscpi->interface) == 1) {
250                 if ((ret = libusb_detach_kernel_driver(usb->devhdl,
251                                                        uscpi->interface)) < 0) {
252                         sr_err("Failed to detach kernel driver: %s.",
253                                libusb_error_name(ret));
254                         return SR_ERR;
255                 }
256                 uscpi->detached_kernel_driver = 1;
257         }
258
259         if ((ret = libusb_set_configuration(usb->devhdl, config)) < 0) {
260                 sr_err("Failed to set configuration: %s.",
261                        libusb_error_name(ret));
262                 return SR_ERR;
263         }
264
265         if ((ret = libusb_claim_interface(usb->devhdl, uscpi->interface)) < 0) {
266                 sr_err("Failed to claim interface: %s.",
267                        libusb_error_name(ret));
268                 return SR_ERR;
269         }
270
271         if (!uscpi->rigol_ds1000) {
272         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0) {
273                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
274                        uscpi->bulk_in_ep, libusb_error_name(ret));
275                 return SR_ERR;
276         }
277         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0) {
278                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
279                        uscpi->bulk_out_ep, libusb_error_name(ret));
280                 return SR_ERR;
281         }
282         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0) {
283                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
284                        uscpi->interrupt_ep, libusb_error_name(ret));
285                 return SR_ERR;
286         }
287         }
288
289         /* Get capabilities. */
290         ret = libusb_control_transfer(usb->devhdl,
291                                       LIBUSB_ENDPOINT_IN         |
292                                       LIBUSB_REQUEST_TYPE_CLASS  |
293                                       LIBUSB_RECIPIENT_INTERFACE,
294                                       GET_CAPABILITIES, 0,
295                                       uscpi->interface,
296                                       capabilities, sizeof(capabilities),
297                                       TRANSFER_TIMEOUT);
298         if (ret == sizeof(capabilities)) {
299                 uscpi->usbtmc_int_cap = capabilities[ 4];
300                 uscpi->usbtmc_dev_cap = capabilities[ 5];
301                 uscpi->usb488_dev_cap = capabilities[15];
302         }
303         sr_dbg("Device capabilities: %s%s%s%s%s, %s, %s",
304                uscpi->usb488_dev_cap & USB488_DEV_CAP_SCPI       ? "SCPI, "    : "",
305                uscpi->usbtmc_dev_cap & USBTMC_DEV_CAP_TERMCHAR   ? "TermChar, ": "",
306                uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? "L3, " :
307                uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY  ? ""     : "L4, ",
308                uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY  ? "T5, " :
309                uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? ""     : "T6, ",
310                uscpi->usb488_dev_cap & USB488_DEV_CAP_SR1        ? "SR1"  : "SR0",
311                uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1        ? "RL1"  : "RL0",
312                uscpi->usb488_dev_cap & USB488_DEV_CAP_DT1        ? "DT1"  : "DT0");
313
314         return SR_OK;
315 }
316
317 static int scpi_usbtmc_libusb_source_add(struct sr_session *session,
318                 void *priv, int events, int timeout, sr_receive_data_callback cb,
319                 void *cb_data)
320 {
321         struct scpi_usbtmc_libusb *uscpi = priv;
322         (void)events;
323         return usb_source_add(session, uscpi->ctx, timeout, cb, cb_data);
324 }
325
326 static int scpi_usbtmc_libusb_source_remove(struct sr_session *session,
327                 void *priv)
328 {
329         struct scpi_usbtmc_libusb *uscpi = priv;
330         return usb_source_remove(session, uscpi->ctx);
331 }
332
333 static void usbtmc_bulk_out_header_write(void *header, uint8_t MsgID,
334                                          uint8_t bTag,
335                                          uint32_t TransferSize,
336                                          uint8_t bmTransferAttributes,
337                                          char TermChar)
338 {
339           W8(header+ 0, MsgID);
340           W8(header+ 1, bTag);
341           W8(header+ 2, ~bTag);
342           W8(header+ 3, 0);
343         WL32(header+ 4, TransferSize);
344           W8(header+ 8, bmTransferAttributes);
345           W8(header+ 9, TermChar);
346         WL16(header+10, 0);
347 }
348
349 static int usbtmc_bulk_in_header_read(void *header, uint8_t MsgID,
350                                       unsigned char bTag,
351                                       int32_t *TransferSize,
352                                       uint8_t *bmTransferAttributes)
353 {
354         if (R8(header+0) != MsgID ||
355             R8(header+1) != bTag  ||
356             R8(header+2) != (unsigned char)~bTag)
357                 return SR_ERR;
358         if (TransferSize)
359                 *TransferSize = RL32(header+4);
360         if (bmTransferAttributes)
361                 *bmTransferAttributes = R8(header+8);
362         return SR_OK;
363 }
364
365 static int scpi_usbtmc_bulkout(struct scpi_usbtmc_libusb *uscpi,
366                                uint8_t msg_id, const void *data, int32_t size,
367                                uint8_t transfer_attributes)
368 {
369         struct sr_usb_dev_inst *usb = uscpi->usb;
370         int padded_size, ret, transferred;
371
372         if (data && size+USBTMC_BULK_HEADER_SIZE+3 > (int)sizeof(uscpi->buffer)) {
373                 sr_err("USBTMC bulk out transfer is too big.");
374                 return SR_ERR;
375         }
376
377         uscpi->bTag++;
378         uscpi->bTag += !uscpi->bTag;  /* bTag == 0 is invalid so avoid it. */
379
380         usbtmc_bulk_out_header_write(uscpi->buffer, msg_id, uscpi->bTag,
381                                      size, transfer_attributes, 0);
382         if (data)
383                 memcpy(uscpi->buffer+USBTMC_BULK_HEADER_SIZE, data, size);
384         else
385                 size = 0;
386         size += USBTMC_BULK_HEADER_SIZE;
387         padded_size = (size + 3) & ~0x3;
388         memset(uscpi->buffer+size, 0, padded_size - size);
389
390         ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_out_ep,
391                                    uscpi->buffer, padded_size, &transferred,
392                                    TRANSFER_TIMEOUT);
393         if (ret < 0) {
394                 sr_err("USBTMC bulk out transfer error: %s.",
395                        libusb_error_name(ret));
396                 return SR_ERR;
397         }
398
399         if (transferred < padded_size) {
400                 sr_dbg("USBTMC bulk out partial transfer (%d/%d bytes).",
401                        transferred, padded_size);
402                 return SR_ERR;
403         }
404
405         return transferred - USBTMC_BULK_HEADER_SIZE;
406 }
407
408 static int scpi_usbtmc_bulkin_start(struct scpi_usbtmc_libusb *uscpi,
409                                     uint8_t msg_id, void *data, int32_t size,
410                                     uint8_t *transfer_attributes)
411 {
412         struct sr_usb_dev_inst *usb = uscpi->usb;
413         int ret, transferred, message_size;
414
415         ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size,
416                                    &transferred, TRANSFER_TIMEOUT);
417         if (ret < 0) {
418                 sr_err("USBTMC bulk in transfer error: %s.",
419                        libusb_error_name(ret));
420                 return SR_ERR;
421         }
422
423         if (usbtmc_bulk_in_header_read(data, msg_id, uscpi->bTag, &message_size,
424                                        transfer_attributes) != SR_OK) {
425                 sr_err("USBTMC invalid bulk in header.");
426                 return SR_ERR;
427         }
428
429         message_size += USBTMC_BULK_HEADER_SIZE;
430         uscpi->response_length = MIN(transferred, message_size);
431         uscpi->response_bytes_read = USBTMC_BULK_HEADER_SIZE;
432         uscpi->remaining_length = message_size - uscpi->response_length;
433
434         return transferred - USBTMC_BULK_HEADER_SIZE;
435 }
436
437 static int scpi_usbtmc_bulkin_continue(struct scpi_usbtmc_libusb *uscpi,
438                                        void *data, int size)
439 {
440         struct sr_usb_dev_inst *usb = uscpi->usb;
441         int ret, transferred;
442
443         ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size,
444                                    &transferred, TRANSFER_TIMEOUT);
445         if (ret < 0) {
446                 sr_err("USBTMC bulk in transfer error: %s.",
447                        libusb_error_name(ret));
448                 return SR_ERR;
449         }
450
451         uscpi->response_length = MIN(transferred, uscpi->remaining_length);
452         uscpi->response_bytes_read = 0;
453         uscpi->remaining_length -= uscpi->response_length;
454
455         return transferred;
456 }
457
458 static int scpi_usbtmc_libusb_send(void *priv, const char *command)
459 {
460         struct scpi_usbtmc_libusb *uscpi = priv;
461
462         if (scpi_usbtmc_bulkout(uscpi, DEV_DEP_MSG_OUT,
463                                 command, strlen(command), EOM) <= 0)
464                 return SR_ERR;
465
466         sr_spew("Successfully sent SCPI command: '%s'.", command);
467
468         return SR_OK;
469 }
470
471 static int scpi_usbtmc_libusb_read_begin(void *priv)
472 {
473         struct scpi_usbtmc_libusb *uscpi = priv;
474
475         uscpi->remaining_length = 0;
476
477         if (scpi_usbtmc_bulkout(uscpi, REQUEST_DEV_DEP_MSG_IN,
478             NULL, INT32_MAX, 0) < 0)
479                 return SR_ERR;
480         if (scpi_usbtmc_bulkin_start(uscpi, DEV_DEP_MSG_IN,
481                                      uscpi->buffer, sizeof(uscpi->buffer),
482                                      &uscpi->bulkin_attributes) < 0)
483                 return SR_ERR;
484
485         return SR_OK;
486 }
487
488 static int scpi_usbtmc_libusb_read_data(void *priv, char *buf, int maxlen)
489 {
490         struct scpi_usbtmc_libusb *uscpi = priv;
491         int read_length;
492
493         if (uscpi->response_bytes_read >= uscpi->response_length) {
494                 if (uscpi->remaining_length > 0) {
495                         if (scpi_usbtmc_bulkin_continue(uscpi, uscpi->buffer,
496                                                         sizeof(uscpi->buffer)) <= 0)
497                                 return SR_ERR;
498                 } else {
499                         if (uscpi->bulkin_attributes & EOM)
500                                 return SR_ERR;
501                         if (scpi_usbtmc_libusb_read_begin(uscpi) < 0)
502                                 return SR_ERR;
503                 }
504         }
505
506         read_length = MIN(uscpi->response_length - uscpi->response_bytes_read, maxlen);
507
508         memcpy(buf, uscpi->buffer + uscpi->response_bytes_read, read_length);
509
510         uscpi->response_bytes_read += read_length;
511
512         return read_length;
513 }
514
515 static int scpi_usbtmc_libusb_read_complete(void *priv)
516 {
517         struct scpi_usbtmc_libusb *uscpi = priv;
518         return uscpi->response_bytes_read >= uscpi->response_length &&
519                uscpi->remaining_length <= 0 &&
520                uscpi->bulkin_attributes & EOM;
521 }
522
523 static int scpi_usbtmc_libusb_close(void *priv)
524 {
525         int ret;
526         struct scpi_usbtmc_libusb *uscpi = priv;
527         struct sr_usb_dev_inst *usb = uscpi->usb;
528
529         if (!usb->devhdl)
530                 return SR_ERR;
531
532         if (!uscpi->rigol_ds1000) {
533         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0)
534                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
535                        uscpi->bulk_in_ep, libusb_error_name(ret));
536         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0)
537                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
538                        uscpi->bulk_out_ep, libusb_error_name(ret));
539         if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0)
540                 sr_err("Failed to clear halt/stall condition for EP %d: %s.",
541                        uscpi->interrupt_ep, libusb_error_name(ret));
542         }
543
544         if ((ret = libusb_release_interface(usb->devhdl, uscpi->interface)) < 0)
545                 sr_err("Failed to release interface: %s.",
546                        libusb_error_name(ret));
547         
548         if (uscpi->detached_kernel_driver) {
549                 if ((ret = libusb_attach_kernel_driver(usb->devhdl,
550                                                 uscpi->interface)) < 0)
551                         sr_err("Failed to re-attach kernel driver: %s.",
552                                libusb_error_name(ret));
553
554                 uscpi->detached_kernel_driver = 0;
555         }
556         libusb_close(usb->devhdl);
557         usb->devhdl = NULL;
558
559         return SR_OK;
560 }
561
562 static 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
568 SR_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 };