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