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