]>
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 | ||
20 | #include <string.h> | |
21 | #include "libsigrok.h" | |
22 | #include "libsigrok-internal.h" | |
23 | ||
24 | #define LOG_PREFIX "scpi_usbtmc" | |
25 | ||
26 | #define MAX_TRANSFER_LENGTH 2048 | |
27 | #define TRANSFER_TIMEOUT 1000 | |
28 | ||
29 | struct scpi_usbtmc_libusb { | |
30 | struct sr_context *ctx; | |
31 | struct sr_usb_dev_inst *usb; | |
32 | int detached_kernel_driver; | |
33 | uint8_t interface; | |
34 | uint8_t bulk_in_ep; | |
35 | uint8_t bulk_out_ep; | |
36 | uint8_t interrupt_ep; | |
37 | uint8_t usbtmc_int_cap; | |
38 | uint8_t usbtmc_dev_cap; | |
39 | uint8_t usb488_dev_cap; | |
40 | uint8_t bTag; | |
41 | uint8_t bulkin_attributes; | |
42 | uint8_t buffer[MAX_TRANSFER_LENGTH]; | |
43 | int response_length; | |
44 | int response_bytes_read; | |
45 | int remaining_length; | |
50e6311a | 46 | int rigol_ds1000; |
20ed3cee AJ |
47 | }; |
48 | ||
49 | /* Some USBTMC-specific enums, as defined in the USBTMC standard. */ | |
50 | #define SUBCLASS_USBTMC 0x03 | |
51 | #define USBTMC_USB488 0x01 | |
52 | ||
53 | enum { | |
54 | /* USBTMC control requests */ | |
55 | INITIATE_ABORT_BULK_OUT = 1, | |
56 | CHECK_ABORT_BULK_OUT_STATUS = 2, | |
57 | INITIATE_ABORT_BULK_IN = 3, | |
58 | CHECK_ABORT_BULK_IN_STATUS = 4, | |
59 | INITIATE_CLEAR = 5, | |
60 | CHECK_CLEAR_STATUS = 6, | |
61 | GET_CAPABILITIES = 7, | |
62 | INDICATOR_PULSE = 64, | |
63 | ||
64 | /* USB488 control requests */ | |
65 | READ_STATUS_BYTE = 128, | |
66 | REN_CONTROL = 160, | |
67 | GO_TO_LOCAL = 161, | |
68 | LOCAL_LOCKOUT = 162, | |
69 | }; | |
70 | ||
71 | /* USBTMC capabilities */ | |
72 | #define USBTMC_INT_CAP_LISTEN_ONLY 0x01 | |
73 | #define USBTMC_INT_CAP_TALK_ONLY 0x02 | |
74 | #define USBTMC_INT_CAP_INDICATOR 0x04 | |
75 | ||
76 | #define USBTMC_DEV_CAP_TERMCHAR 0x01 | |
77 | ||
78 | #define USB488_DEV_CAP_DT1 0x01 | |
79 | #define USB488_DEV_CAP_RL1 0x02 | |
80 | #define USB488_DEV_CAP_SR1 0x04 | |
81 | #define USB488_DEV_CAP_SCPI 0x08 | |
82 | ||
83 | /* Bulk messages constants */ | |
84 | #define USBTMC_BULK_HEADER_SIZE 12 | |
85 | ||
86 | /* Bulk MsgID values */ | |
87 | #define DEV_DEP_MSG_OUT 1 | |
88 | #define REQUEST_DEV_DEP_MSG_IN 2 | |
89 | #define DEV_DEP_MSG_IN 2 | |
90 | ||
91 | /* bmTransferAttributes */ | |
92 | #define EOM 0x01 | |
93 | #define TERM_CHAR_ENABLED 0x02 | |
94 | ||
95 | ||
96 | static GSList *scpi_usbtmc_libusb_scan(struct drv_context *drvc) | |
97 | { | |
98 | struct libusb_device **devlist; | |
99 | struct libusb_device_descriptor des; | |
100 | struct libusb_config_descriptor *confdes; | |
101 | const struct libusb_interface_descriptor *intfdes; | |
102 | GSList *resources = NULL; | |
103 | int confidx, intfidx, ret, i; | |
104 | char *res; | |
105 | ||
52f6951c UH |
106 | ret = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); |
107 | if (ret < 0) { | |
108 | sr_err("Failed to get device list: %s.", | |
109 | libusb_error_name(ret)); | |
110 | return NULL; | |
111 | } | |
20ed3cee | 112 | for (i = 0; devlist[i]; i++) { |
d8cbd659 | 113 | if ((ret = libusb_get_device_descriptor(devlist[i], &des)) < 0) { |
20ed3cee AJ |
114 | sr_err("Failed to get device descriptor: %s.", |
115 | libusb_error_name(ret)); | |
116 | continue; | |
117 | } | |
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; | |
187 | uint8_t capabilities[24]; | |
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); | |
d8cbd659 | 197 | if ((ret = libusb_get_device_descriptor(dev, &des)) < 0) { |
20ed3cee AJ |
198 | sr_err("Failed to get device descriptor: %s.", |
199 | libusb_error_name(ret)); | |
200 | return SR_ERR; | |
201 | } | |
202 | ||
203 | for (confidx = 0; confidx < des.bNumConfigurations; confidx++) { | |
9ad05e6c UH |
204 | if ((ret = libusb_get_config_descriptor(dev, confidx, &confdes)) < 0) { |
205 | sr_dbg("Failed to get configuration descriptor: %s, " | |
206 | "ignoring device.", libusb_error_name(ret)); | |
20ed3cee AJ |
207 | continue; |
208 | } | |
209 | for (intfidx = 0; intfidx < confdes->bNumInterfaces; intfidx++) { | |
210 | intfdes = confdes->interface[intfidx].altsetting; | |
211 | if (intfdes->bInterfaceClass != LIBUSB_CLASS_APPLICATION || | |
212 | intfdes->bInterfaceSubClass != SUBCLASS_USBTMC || | |
213 | intfdes->bInterfaceProtocol != USBTMC_USB488) | |
214 | continue; | |
215 | uscpi->interface = intfdes->bInterfaceNumber; | |
216 | sr_dbg("Interface %d", uscpi->interface); | |
217 | config = confdes->bConfigurationValue; | |
218 | sr_dbg("Configuration %d", config); | |
219 | for (epidx = 0; epidx < intfdes->bNumEndpoints; epidx++) { | |
220 | ep = &intfdes->endpoint[epidx]; | |
221 | if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK && | |
222 | !(ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK))) { | |
223 | uscpi->bulk_out_ep = ep->bEndpointAddress; | |
224 | sr_dbg("Bulk OUT EP %d", uscpi->bulk_out_ep); | |
225 | } | |
226 | if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_BULK && | |
227 | ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) { | |
228 | uscpi->bulk_in_ep = ep->bEndpointAddress; | |
229 | sr_dbg("Bulk IN EP %d", uscpi->bulk_in_ep); | |
230 | } | |
231 | if (ep->bmAttributes == LIBUSB_TRANSFER_TYPE_INTERRUPT && | |
232 | ep->bEndpointAddress & (LIBUSB_ENDPOINT_DIR_MASK)) { | |
233 | uscpi->interrupt_ep = ep->bEndpointAddress; | |
234 | sr_dbg("Interrupt EP %d", uscpi->interrupt_ep); | |
235 | } | |
236 | } | |
237 | found = 1; | |
50e6311a AJ |
238 | uscpi->rigol_ds1000 = des.idVendor == 0x1ab1 && |
239 | des.idProduct == 0x0588; | |
20ed3cee AJ |
240 | } |
241 | libusb_free_config_descriptor(confdes); | |
242 | if (found) | |
243 | break; | |
244 | } | |
245 | ||
246 | if (!found) { | |
52f6951c | 247 | sr_err("Failed to find USBTMC interface."); |
20ed3cee AJ |
248 | return SR_ERR; |
249 | } | |
250 | ||
251 | if (libusb_kernel_driver_active(usb->devhdl, uscpi->interface) == 1) { | |
252 | if ((ret = libusb_detach_kernel_driver(usb->devhdl, | |
253 | uscpi->interface)) < 0) { | |
52f6951c UH |
254 | sr_err("Failed to detach kernel driver: %s.", |
255 | libusb_error_name(ret)); | |
20ed3cee AJ |
256 | return SR_ERR; |
257 | } | |
258 | uscpi->detached_kernel_driver = 1; | |
259 | } | |
260 | ||
d8cbd659 | 261 | if ((ret = libusb_set_configuration(usb->devhdl, config)) < 0) { |
52f6951c UH |
262 | sr_err("Failed to set configuration: %s.", |
263 | libusb_error_name(ret)); | |
20ed3cee AJ |
264 | return SR_ERR; |
265 | } | |
266 | ||
d8cbd659 | 267 | if ((ret = libusb_claim_interface(usb->devhdl, uscpi->interface)) < 0) { |
52f6951c UH |
268 | sr_err("Failed to claim interface: %s.", |
269 | libusb_error_name(ret)); | |
20ed3cee AJ |
270 | return SR_ERR; |
271 | } | |
272 | ||
52f6951c UH |
273 | if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0) { |
274 | sr_err("Failed to clear halt/stall condition for EP %d: %s.", | |
275 | uscpi->bulk_in_ep, libusb_error_name(ret)); | |
276 | return SR_ERR; | |
277 | } | |
50e6311a | 278 | if (!uscpi->rigol_ds1000) |
52f6951c UH |
279 | if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0) { |
280 | sr_err("Failed to clear halt/stall condition for EP %d: %s.", | |
281 | uscpi->bulk_out_ep, libusb_error_name(ret)); | |
282 | return SR_ERR; | |
283 | } | |
284 | if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0) { | |
285 | sr_err("Failed to clear halt/stall condition for EP %d: %s.", | |
286 | uscpi->interrupt_ep, libusb_error_name(ret)); | |
287 | return SR_ERR; | |
288 | } | |
20ed3cee | 289 | |
52f6951c | 290 | /* Get capabilities. */ |
20ed3cee AJ |
291 | ret = libusb_control_transfer(usb->devhdl, |
292 | LIBUSB_ENDPOINT_IN | | |
293 | LIBUSB_REQUEST_TYPE_CLASS | | |
294 | LIBUSB_RECIPIENT_INTERFACE, | |
295 | GET_CAPABILITIES, 0, | |
296 | uscpi->interface, | |
297 | capabilities, sizeof(capabilities), | |
298 | TRANSFER_TIMEOUT); | |
299 | if (ret == sizeof(capabilities)) { | |
300 | uscpi->usbtmc_int_cap = capabilities[ 4]; | |
301 | uscpi->usbtmc_dev_cap = capabilities[ 5]; | |
302 | uscpi->usb488_dev_cap = capabilities[15]; | |
303 | } | |
52f6951c | 304 | sr_dbg("Device capabilities: %s%s%s%s%s, %s, %s", |
20ed3cee AJ |
305 | uscpi->usb488_dev_cap & USB488_DEV_CAP_SCPI ? "SCPI, " : "", |
306 | uscpi->usbtmc_dev_cap & USBTMC_DEV_CAP_TERMCHAR ? "TermChar, ": "", | |
307 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? "L3, " : | |
308 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "" : "L4, ", | |
309 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_TALK_ONLY ? "T5, " : | |
310 | uscpi->usbtmc_int_cap & USBTMC_INT_CAP_LISTEN_ONLY? "" : "T6, ", | |
311 | uscpi->usb488_dev_cap & USB488_DEV_CAP_SR1 ? "SR1" : "SR0", | |
312 | uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1 ? "RL1" : "RL0", | |
313 | uscpi->usb488_dev_cap & USB488_DEV_CAP_DT1 ? "DT1" : "DT0"); | |
314 | ||
315 | return SR_OK; | |
316 | } | |
317 | ||
318 | static int scpi_usbtmc_libusb_source_add(void *priv, int events, int timeout, | |
319 | sr_receive_data_callback_t cb, void *cb_data) | |
320 | { | |
321 | struct scpi_usbtmc_libusb *uscpi = priv; | |
322 | (void)events; | |
323 | return usb_source_add(uscpi->ctx, timeout, cb, cb_data); | |
324 | } | |
325 | ||
326 | static int scpi_usbtmc_libusb_source_remove(void *priv) | |
327 | { | |
328 | struct scpi_usbtmc_libusb *uscpi = priv; | |
329 | return usb_source_remove(uscpi->ctx); | |
330 | } | |
331 | ||
332 | static void usbtmc_bulk_out_header_write(void *header, uint8_t MsgID, | |
333 | uint8_t bTag, | |
334 | uint32_t TransferSize, | |
335 | uint8_t bmTransferAttributes, | |
336 | char TermChar) | |
337 | { | |
338 | W8(header+ 0, MsgID); | |
339 | W8(header+ 1, bTag); | |
340 | W8(header+ 2, ~bTag); | |
341 | W8(header+ 3, 0); | |
342 | WL32(header+ 4, TransferSize); | |
343 | W8(header+ 8, bmTransferAttributes); | |
344 | W8(header+ 9, TermChar); | |
345 | WL16(header+10, 0); | |
346 | } | |
347 | ||
348 | static int usbtmc_bulk_in_header_read(void *header, uint8_t MsgID, | |
349 | unsigned char bTag, | |
350 | int32_t *TransferSize, | |
351 | uint8_t *bmTransferAttributes) | |
352 | { | |
353 | if (R8(header+0) != MsgID || | |
354 | R8(header+1) != bTag || | |
355 | R8(header+2) != (unsigned char)~bTag) | |
356 | return SR_ERR; | |
357 | if (TransferSize) | |
358 | *TransferSize = RL32(header+4); | |
359 | if (bmTransferAttributes) | |
360 | *bmTransferAttributes = R8(header+8); | |
361 | return SR_OK; | |
362 | } | |
363 | ||
364 | static int scpi_usbtmc_bulkout(struct scpi_usbtmc_libusb *uscpi, | |
365 | uint8_t msg_id, const void *data, int32_t size, | |
366 | uint8_t transfer_attributes) | |
367 | { | |
368 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
369 | int padded_size, ret, transferred; | |
370 | ||
371 | if (data && size+USBTMC_BULK_HEADER_SIZE+3 > (int)sizeof(uscpi->buffer)) { | |
52f6951c | 372 | sr_err("USBTMC bulk out transfer is too big."); |
20ed3cee AJ |
373 | return SR_ERR; |
374 | } | |
375 | ||
376 | uscpi->bTag++; | |
52f6951c | 377 | uscpi->bTag += !uscpi->bTag; /* bTag == 0 is invalid so avoid it. */ |
20ed3cee AJ |
378 | |
379 | usbtmc_bulk_out_header_write(uscpi->buffer, msg_id, uscpi->bTag, | |
380 | size, transfer_attributes, 0); | |
381 | if (data) | |
382 | memcpy(uscpi->buffer+USBTMC_BULK_HEADER_SIZE, data, size); | |
383 | else | |
384 | size = 0; | |
385 | size += USBTMC_BULK_HEADER_SIZE; | |
386 | padded_size = (size + 3) & ~0x3; | |
387 | memset(uscpi->buffer+size, 0, padded_size - size); | |
388 | ||
389 | ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_out_ep, | |
390 | uscpi->buffer, padded_size, &transferred, | |
391 | TRANSFER_TIMEOUT); | |
d8cbd659 | 392 | if (ret < 0) { |
52f6951c UH |
393 | sr_err("USBTMC bulk out transfer error: %s.", |
394 | libusb_error_name(ret)); | |
20ed3cee AJ |
395 | return SR_ERR; |
396 | } | |
397 | ||
398 | if (transferred < padded_size) { | |
52f6951c | 399 | sr_dbg("USBTMC bulk out partial transfer (%d/%d bytes).", |
20ed3cee AJ |
400 | transferred, padded_size); |
401 | return SR_ERR; | |
402 | } | |
403 | ||
404 | return transferred - USBTMC_BULK_HEADER_SIZE; | |
405 | } | |
406 | ||
407 | static int scpi_usbtmc_bulkin_start(struct scpi_usbtmc_libusb *uscpi, | |
408 | uint8_t msg_id, void *data, int32_t size, | |
409 | uint8_t *transfer_attributes) | |
410 | { | |
411 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
412 | int ret, transferred, message_size; | |
413 | ||
414 | ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size, | |
415 | &transferred, TRANSFER_TIMEOUT); | |
d8cbd659 | 416 | if (ret < 0) { |
52f6951c UH |
417 | sr_err("USBTMC bulk in transfer error: %s.", |
418 | libusb_error_name(ret)); | |
20ed3cee AJ |
419 | return SR_ERR; |
420 | } | |
421 | ||
422 | if (usbtmc_bulk_in_header_read(data, msg_id, uscpi->bTag, &message_size, | |
423 | transfer_attributes) != SR_OK) { | |
52f6951c | 424 | sr_err("USBTMC invalid bulk in header."); |
20ed3cee AJ |
425 | return SR_ERR; |
426 | } | |
427 | ||
428 | message_size += USBTMC_BULK_HEADER_SIZE; | |
429 | uscpi->response_length = MIN(transferred, message_size); | |
430 | uscpi->response_bytes_read = USBTMC_BULK_HEADER_SIZE; | |
431 | uscpi->remaining_length = message_size - uscpi->response_length; | |
432 | ||
433 | return transferred - USBTMC_BULK_HEADER_SIZE; | |
434 | } | |
435 | ||
436 | static int scpi_usbtmc_bulkin_continue(struct scpi_usbtmc_libusb *uscpi, | |
437 | void *data, int size) | |
438 | { | |
439 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
440 | int ret, transferred; | |
441 | ||
442 | ret = libusb_bulk_transfer(usb->devhdl, uscpi->bulk_in_ep, data, size, | |
443 | &transferred, TRANSFER_TIMEOUT); | |
d8cbd659 | 444 | if (ret < 0) { |
52f6951c UH |
445 | sr_err("USBTMC bulk in transfer error: %s.", |
446 | libusb_error_name(ret)); | |
20ed3cee AJ |
447 | return SR_ERR; |
448 | } | |
449 | ||
450 | uscpi->response_length = MIN(transferred, uscpi->remaining_length); | |
451 | uscpi->response_bytes_read = 0; | |
452 | uscpi->remaining_length -= uscpi->response_length; | |
453 | ||
454 | return transferred; | |
455 | } | |
456 | ||
457 | static int scpi_usbtmc_libusb_send(void *priv, const char *command) | |
458 | { | |
459 | struct scpi_usbtmc_libusb *uscpi = priv; | |
460 | ||
461 | if (scpi_usbtmc_bulkout(uscpi, DEV_DEP_MSG_OUT, | |
462 | command, strlen(command), EOM) <= 0) | |
463 | return SR_ERR; | |
464 | ||
465 | sr_spew("Successfully sent SCPI command: '%s'.", command); | |
466 | ||
467 | return SR_OK; | |
468 | } | |
469 | ||
470 | static int scpi_usbtmc_libusb_read_begin(void *priv) | |
471 | { | |
472 | struct scpi_usbtmc_libusb *uscpi = priv; | |
473 | ||
474 | uscpi->remaining_length = 0; | |
475 | ||
476 | if (scpi_usbtmc_bulkout(uscpi, REQUEST_DEV_DEP_MSG_IN, | |
477 | NULL, INT32_MAX, 0) < 0) | |
478 | return SR_ERR; | |
479 | if (scpi_usbtmc_bulkin_start(uscpi, DEV_DEP_MSG_IN, | |
480 | uscpi->buffer, sizeof(uscpi->buffer), | |
481 | &uscpi->bulkin_attributes) < 0) | |
482 | return SR_ERR; | |
483 | ||
484 | return SR_OK; | |
485 | } | |
486 | ||
487 | static int scpi_usbtmc_libusb_read_data(void *priv, char *buf, int maxlen) | |
488 | { | |
489 | struct scpi_usbtmc_libusb *uscpi = priv; | |
490 | int read_length; | |
491 | ||
492 | if (uscpi->response_bytes_read >= uscpi->response_length) { | |
493 | if (uscpi->remaining_length > 0) { | |
494 | if (scpi_usbtmc_bulkin_continue(uscpi, uscpi->buffer, | |
495 | sizeof(uscpi->buffer)) <= 0) | |
496 | return SR_ERR; | |
497 | } else { | |
498 | if (uscpi->bulkin_attributes & EOM) | |
499 | return SR_ERR; | |
500 | if (scpi_usbtmc_libusb_read_begin(uscpi) < 0) | |
501 | return SR_ERR; | |
502 | } | |
503 | } | |
504 | ||
505 | read_length = MIN(uscpi->response_length - uscpi->response_bytes_read, maxlen); | |
506 | ||
507 | memcpy(buf, uscpi->buffer + uscpi->response_bytes_read, read_length); | |
508 | ||
509 | uscpi->response_bytes_read += read_length; | |
510 | ||
511 | return read_length; | |
512 | } | |
513 | ||
514 | static int scpi_usbtmc_libusb_read_complete(void *priv) | |
515 | { | |
516 | struct scpi_usbtmc_libusb *uscpi = priv; | |
517 | return uscpi->response_bytes_read >= uscpi->response_length && | |
518 | uscpi->remaining_length <= 0 && | |
519 | uscpi->bulkin_attributes & EOM; | |
520 | } | |
521 | ||
522 | static int scpi_usbtmc_libusb_close(void *priv) | |
523 | { | |
52f6951c | 524 | int ret; |
20ed3cee AJ |
525 | struct scpi_usbtmc_libusb *uscpi = priv; |
526 | struct sr_usb_dev_inst *usb = uscpi->usb; | |
527 | ||
528 | if (!usb->devhdl) | |
529 | return SR_ERR; | |
530 | ||
52f6951c UH |
531 | if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_in_ep)) < 0) |
532 | sr_err("Failed to clear halt/stall condition for EP %d: %s.", | |
533 | uscpi->bulk_in_ep, libusb_error_name(ret)); | |
50e6311a | 534 | if (!uscpi->rigol_ds1000) |
52f6951c UH |
535 | if ((ret = libusb_clear_halt(usb->devhdl, uscpi->bulk_out_ep)) < 0) |
536 | sr_err("Failed to clear halt/stall condition for EP %d: %s.", | |
537 | uscpi->bulk_out_ep, libusb_error_name(ret)); | |
538 | if ((ret = libusb_clear_halt(usb->devhdl, uscpi->interrupt_ep)) < 0) | |
539 | sr_err("Failed to clear halt/stall condition for EP %d: %s.", | |
540 | uscpi->interrupt_ep, libusb_error_name(ret)); | |
541 | ||
542 | if ((ret = libusb_release_interface(usb->devhdl, uscpi->interface)) < 0) | |
543 | sr_err("Failed to release interface: %s.", | |
544 | libusb_error_name(ret)); | |
545 | ||
20ed3cee | 546 | if (uscpi->detached_kernel_driver) { |
52f6951c UH |
547 | if ((ret = libusb_attach_kernel_driver(usb->devhdl, |
548 | uscpi->interface)) < 0) | |
549 | sr_err("Failed to re-attach kernel driver: %s.", | |
550 | libusb_error_name(ret)); | |
551 | ||
20ed3cee AJ |
552 | uscpi->detached_kernel_driver = 0; |
553 | } | |
554 | libusb_close(usb->devhdl); | |
555 | usb->devhdl = NULL; | |
556 | ||
557 | return SR_OK; | |
558 | } | |
559 | ||
560 | static void scpi_usbtmc_libusb_free(void *priv) | |
561 | { | |
562 | struct scpi_usbtmc_libusb *uscpi = priv; | |
563 | sr_usb_dev_inst_free(uscpi->usb); | |
564 | } | |
565 | ||
20ed3cee AJ |
566 | SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_libusb_dev = { |
567 | .name = "USBTMC", | |
568 | .prefix = "usbtmc", | |
569 | .priv_size = sizeof(struct scpi_usbtmc_libusb), | |
570 | .scan = scpi_usbtmc_libusb_scan, | |
571 | .dev_inst_new = scpi_usbtmc_libusb_dev_inst_new, | |
572 | .open = scpi_usbtmc_libusb_open, | |
573 | .source_add = scpi_usbtmc_libusb_source_add, | |
574 | .source_remove = scpi_usbtmc_libusb_source_remove, | |
575 | .send = scpi_usbtmc_libusb_send, | |
576 | .read_begin = scpi_usbtmc_libusb_read_begin, | |
577 | .read_data = scpi_usbtmc_libusb_read_data, | |
578 | .read_complete = scpi_usbtmc_libusb_read_complete, | |
579 | .close = scpi_usbtmc_libusb_close, | |
580 | .free = scpi_usbtmc_libusb_free, | |
581 | }; |