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