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