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