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