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