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