| 1 |
/* |
| 2 |
* Written by Oron Peled <[email protected]> |
| 3 |
* Copyright (C) 2012, Xorcom |
| 4 |
* |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#include <assert.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <stdarg.h> |
| 29 |
#include <syslog.h> |
| 30 |
#include <arpa/inet.h> |
| 31 |
#include <libusb.h> |
| 32 |
#include <xtalk/debug.h> |
| 33 |
#include <xtalk/xusb.h> |
| 34 |
#include <autoconfig.h> |
| 35 |
#include "xusb_common.h" |
| 36 |
|
| 37 |
#define DBG_MASK 0x01 |
| 38 |
#define TIMEOUT 500 |
| 39 |
|
| 40 |
#define EP_IS_IN(ep) (((ep) & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN) |
| 41 |
#define EP_IS_OUT(ep) (((ep) & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT) |
| 42 |
|
| 43 |
struct libusb_implementation { |
| 44 |
struct libusb_device *dev; |
| 45 |
struct libusb_device_handle *handle; |
| 46 |
struct libusb_config_descriptor *config_desc; |
| 47 |
}; |
| 48 |
|
| 49 |
void __attribute__((constructor)) xusb_init(void) |
| 50 |
{ |
| 51 |
int ret; |
| 52 |
|
| 53 |
xtalk_parse_options(); |
| 54 |
ret = libusb_init(NULL); |
| 55 |
if (ret < 0) { |
| 56 |
ERR("libusb_init() failed: ret=%d\n", ret); |
| 57 |
abort(); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void __attribute__((destructor)) xusb_fini(void) |
| 62 |
{ |
| 63 |
libusb_exit(NULL); |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
* Translate libusbx error codes to regular errno |
| 68 |
*/ |
| 69 |
static int errno_map(int libusb_err) |
| 70 |
{ |
| 71 |
int ret = -libusb_err; |
| 72 |
#define ERRNO_MAP(libusb, errno) [-(libusb)] = errno |
| 73 |
static const int error_codes[] = { |
| 74 |
ERRNO_MAP(LIBUSB_SUCCESS, 0), |
| 75 |
ERRNO_MAP(LIBUSB_ERROR_IO, EIO), |
| 76 |
ERRNO_MAP(LIBUSB_ERROR_INVALID_PARAM, EINVAL), |
| 77 |
ERRNO_MAP(LIBUSB_ERROR_ACCESS, EACCES), |
| 78 |
ERRNO_MAP(LIBUSB_ERROR_NO_DEVICE, ENODEV), |
| 79 |
ERRNO_MAP(LIBUSB_ERROR_NOT_FOUND, ENOENT), |
| 80 |
ERRNO_MAP(LIBUSB_ERROR_BUSY, EBUSY), |
| 81 |
ERRNO_MAP(LIBUSB_ERROR_TIMEOUT, ETIMEDOUT), |
| 82 |
ERRNO_MAP(LIBUSB_ERROR_OVERFLOW, EOVERFLOW), |
| 83 |
ERRNO_MAP(LIBUSB_ERROR_PIPE, EPIPE), |
| 84 |
ERRNO_MAP(LIBUSB_ERROR_INTERRUPTED, EINTR), |
| 85 |
ERRNO_MAP(LIBUSB_ERROR_NO_MEM, ENOMSG), |
| 86 |
ERRNO_MAP(LIBUSB_ERROR_NOT_SUPPORTED, ENOTSUP), |
| 87 |
ERRNO_MAP(LIBUSB_ERROR_OTHER, EPROTO), |
| 88 |
}; |
| 89 |
#undef ERRNO_MAP |
| 90 |
if (ret < 0 || ret > sizeof(error_codes)/sizeof(error_codes[0])) { |
| 91 |
ERR("%s: Bad return code %d\n", __func__, -ret); |
| 92 |
return -EPROTO; |
| 93 |
} |
| 94 |
return -(error_codes[ret]); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/* |
| 99 |
* USB handling |
| 100 |
*/ |
| 101 |
|
| 102 |
static int get_usb_string(struct xusb_device *xusb_device, uint8_t item, char *buf) |
| 103 |
{ |
| 104 |
unsigned char tmp[BUFSIZ]; |
| 105 |
int ret; |
| 106 |
|
| 107 |
if (!xusb_device->impl->handle) { |
| 108 |
ERR("%s: device closed\n", xusb_device->devpath_tail); |
| 109 |
return -ENXIO; |
| 110 |
} |
| 111 |
if (!item) |
| 112 |
return 0; |
| 113 |
ret = libusb_get_string_descriptor_ascii(xusb_device->impl->handle, item, |
| 114 |
tmp, BUFSIZ); |
| 115 |
if (ret <= 0) |
| 116 |
return errno_map(ret); |
| 117 |
return snprintf(buf, BUFSIZ, "%s", tmp); |
| 118 |
} |
| 119 |
|
| 120 |
static const struct libusb_interface_descriptor *get_iface_descriptor( |
| 121 |
const struct xusb_device *xusb_device, int i) |
| 122 |
{ |
| 123 |
const struct libusb_config_descriptor *config_desc; |
| 124 |
const struct libusb_interface *interface; |
| 125 |
const struct libusb_interface_descriptor *iface_desc; |
| 126 |
|
| 127 |
assert(xusb_device); |
| 128 |
assert(xusb_device->impl); |
| 129 |
config_desc = xusb_device->impl->config_desc; |
| 130 |
assert(config_desc); |
| 131 |
assert(config_desc->bNumInterfaces < XUSB_MAX_INTERFACES); |
| 132 |
if (i >= XUSB_MAX_INTERFACES) |
| 133 |
return NULL; |
| 134 |
interface = &config_desc->interface[i]; |
| 135 |
iface_desc = interface->altsetting; |
| 136 |
return iface_desc; |
| 137 |
} |
| 138 |
|
| 139 |
#define GET_USB_STRING(xusb_device, from, item) \ |
| 140 |
get_usb_string((xusb_device), (from)->item, (xusb_device)->item) |
| 141 |
|
| 142 |
static int xusb_fill_strings(struct xusb_device *xusb_device, int interface_num) |
| 143 |
{ |
| 144 |
struct libusb_device_descriptor dev_desc; |
| 145 |
const struct libusb_interface_descriptor *iface_desc; |
| 146 |
struct xusb_iface *iface = xusb_device->interfaces[interface_num]; |
| 147 |
int ret; |
| 148 |
|
| 149 |
assert(xusb_device); |
| 150 |
assert(xusb_device->impl); |
| 151 |
ret = libusb_get_device_descriptor(xusb_device->impl->dev, &dev_desc); |
| 152 |
if (ret) { |
| 153 |
XUSB_ERR(iface, "libusb_get_device_descriptor() failed: %s\n", |
| 154 |
libusb_error_name(ret)); |
| 155 |
return errno_map(ret); |
| 156 |
} |
| 157 |
ret = GET_USB_STRING(xusb_device, &dev_desc, iManufacturer); |
| 158 |
if (ret < 0) { |
| 159 |
XUSB_ERR(iface, "Failed reading iManufacturer string: %s\n", |
| 160 |
libusb_error_name(ret)); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
ret = GET_USB_STRING(xusb_device, &dev_desc, iProduct); |
| 164 |
if (ret < 0) { |
| 165 |
XUSB_ERR(iface, "Failed reading iProduct string: %s\n", |
| 166 |
libusb_error_name(ret)); |
| 167 |
return 0; |
| 168 |
} |
| 169 |
ret = GET_USB_STRING(xusb_device, &dev_desc, iSerialNumber); |
| 170 |
if (ret < 0) { |
| 171 |
XUSB_ERR(iface, "Failed reading iSerialNumber string: %s\n", |
| 172 |
libusb_error_name(ret)); |
| 173 |
return 0; |
| 174 |
} |
| 175 |
iface_desc = get_iface_descriptor(xusb_device, interface_num); |
| 176 |
if (!iface_desc) { |
| 177 |
XUSB_ERR(iface, "Could not get interface descriptor of device\n"); |
| 178 |
return 0; |
| 179 |
} |
| 180 |
ret = get_usb_string(xusb_device, iface_desc->iInterface, iface->iInterface); |
| 181 |
if (ret < 0) { |
| 182 |
XUSB_ERR(iface, "Failed reading iInterface string: %s\n", |
| 183 |
libusb_error_name(ret)); |
| 184 |
return 0; |
| 185 |
} |
| 186 |
return 1; |
| 187 |
} |
| 188 |
|
| 189 |
static int xusb_open(struct xusb_device *xusb_device) |
| 190 |
{ |
| 191 |
int ret = 0; |
| 192 |
|
| 193 |
DBG("%s\n", xusb_device->devpath_tail); |
| 194 |
if (xusb_device->impl->handle) { |
| 195 |
ERR("%s: already open\n", xusb_device->devpath_tail); |
| 196 |
ret = -EBUSY; |
| 197 |
goto out; |
| 198 |
} |
| 199 |
ret = libusb_open(xusb_device->impl->dev, &(xusb_device->impl->handle)); |
| 200 |
if (ret < 0) { |
| 201 |
ERR("%s: Failed to open usb device: %s\n", |
| 202 |
xusb_device->devpath_tail, |
| 203 |
libusb_error_name(ret)); |
| 204 |
ret = errno_map(ret); |
| 205 |
xusb_device->impl->handle = NULL; |
| 206 |
goto out; |
| 207 |
} |
| 208 |
out: |
| 209 |
return ret; |
| 210 |
} |
| 211 |
|
| 212 |
void xusb_release(struct xusb_iface *iface) |
| 213 |
{ |
| 214 |
if (iface && iface->is_claimed) { |
| 215 |
struct libusb_device_handle *handle; |
| 216 |
|
| 217 |
assert(iface->xusb_device); |
| 218 |
handle = iface->xusb_device->impl->handle; |
| 219 |
XUSB_DBG(iface, "Releasing interface\n"); |
| 220 |
if (!handle) { |
| 221 |
XUSB_ERR(iface, "device closed\n"); |
| 222 |
iface->is_claimed = 0; |
| 223 |
return; |
| 224 |
} |
| 225 |
int ret = libusb_release_interface(handle, iface->interface_num); |
| 226 |
if (ret < 0) |
| 227 |
XUSB_ERR(iface, "Releasing interface: %s\n", |
| 228 |
libusb_error_name(ret)); |
| 229 |
iface->is_claimed = 0; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
static int xusb_clear_halt(struct xusb_iface *xusb_iface) |
| 234 |
{ |
| 235 |
struct xusb_device *xusb_device; |
| 236 |
int ret = 0; |
| 237 |
int ep; |
| 238 |
|
| 239 |
xusb_device = xusb_iface->xusb_device; |
| 240 |
/* |
| 241 |
* WE DO NOT CALL HALT for problematic devices: |
| 242 |
* - It cause problem with our usb-dongle (cypress CY7C63803, interrupt driven) |
| 243 |
*/ |
| 244 |
if (xusb_device->idVendor == 0xe4e4 && xusb_device->idProduct == 0x11a3) { |
| 245 |
XUSB_DBG(xusb_iface, "Skipping clear_halt()\n"); |
| 246 |
goto out; |
| 247 |
} |
| 248 |
if (!xtalk_option_use_clear_halt()) { |
| 249 |
XUSB_DBG(xusb_iface, "Don't use clear_halt()\n"); |
| 250 |
goto out; |
| 251 |
} |
| 252 |
ep = EP_OUT(xusb_iface); |
| 253 |
ret = libusb_clear_halt(xusb_device->impl->handle, ep); |
| 254 |
if (ret < 0) { |
| 255 |
XUSB_ERR(xusb_iface, "Clearing output endpoint 0x%02X: %s\n", |
| 256 |
ep, libusb_error_name(ret)); |
| 257 |
ret = errno_map(ret); |
| 258 |
goto out; |
| 259 |
} |
| 260 |
ep = EP_IN(xusb_iface); |
| 261 |
ret = libusb_clear_halt(xusb_device->impl->handle, ep); |
| 262 |
if (ret < 0) { |
| 263 |
XUSB_ERR(xusb_iface, "Clearing input endpoint 0x%02X: %s\n", |
| 264 |
ep, libusb_error_name(ret)); |
| 265 |
ret = errno_map(ret); |
| 266 |
goto out; |
| 267 |
} |
| 268 |
out: |
| 269 |
return ret; |
| 270 |
} |
| 271 |
|
| 272 |
int xusb_claim(struct xusb_device *xusb_device, unsigned int interface_num, |
| 273 |
struct xusb_iface **xusb_iface) |
| 274 |
{ |
| 275 |
struct xusb_iface *iface = NULL; |
| 276 |
enum xusb_transfer_type iface_tt = XUSB_TT_ILLEGAL; |
| 277 |
int ret = 0; |
| 278 |
|
| 279 |
*xusb_iface = NULL; |
| 280 |
assert(xusb_device); |
| 281 |
if (!xusb_device->impl->handle) { |
| 282 |
ERR("%s: device closed\n", xusb_device->devpath_tail); |
| 283 |
return -ENXIO; |
| 284 |
} |
| 285 |
if (interface_num >= XUSB_MAX_INTERFACES) { |
| 286 |
ERR("%s: interface number %d is too big\n", |
| 287 |
xusb_device->devpath_tail, interface_num); |
| 288 |
ret = -EINVAL; |
| 289 |
goto failed; |
| 290 |
} |
| 291 |
iface = xusb_device->interfaces[interface_num]; |
| 292 |
if (!iface) { |
| 293 |
ERR("%s: No interface number %d\n", |
| 294 |
xusb_device->devpath_tail, interface_num); |
| 295 |
ret = -EINVAL; |
| 296 |
goto failed; |
| 297 |
} |
| 298 |
if (iface->is_claimed) { |
| 299 |
XUSB_ERR(iface, "Already claimed\n"); |
| 300 |
ret = -EBUSY; |
| 301 |
goto failed; |
| 302 |
} |
| 303 |
ret = libusb_claim_interface(xusb_device->impl->handle, iface->interface_num); |
| 304 |
if (ret < 0) { |
| 305 |
XUSB_ERR(iface, "libusb_claim_interface: %s\n", |
| 306 |
libusb_error_name(ret)); |
| 307 |
ret = errno_map(ret); |
| 308 |
goto failed; |
| 309 |
} |
| 310 |
iface->is_claimed = 1; |
| 311 |
iface_tt = xusb_transfer_type(iface); |
| 312 |
if (iface_tt == XUSB_TT_ILLEGAL) { |
| 313 |
ret = -ENOTSUP; |
| 314 |
goto failed; |
| 315 |
} |
| 316 |
iface->transfer_type = iface_tt; |
| 317 |
xusb_fill_strings(xusb_device, interface_num); |
| 318 |
XUSB_DBG(iface, "ID=%04X:%04X Manufacturer=[%s] Product=[%s] " |
| 319 |
"SerialNumber=[%s] Interface=[%s] TT=%s\n", |
| 320 |
xusb_device->idVendor, |
| 321 |
xusb_device->idProduct, |
| 322 |
xusb_device->iManufacturer, |
| 323 |
xusb_device->iProduct, |
| 324 |
xusb_device->iSerialNumber, |
| 325 |
iface->iInterface, |
| 326 |
xusb_tt_name(iface->transfer_type)); |
| 327 |
ret = xusb_clear_halt(iface); |
| 328 |
if (ret < 0) |
| 329 |
goto failed; |
| 330 |
ret = xusb_flushread(iface); |
| 331 |
if (ret < 0) { |
| 332 |
XUSB_ERR(iface, "xusb_flushread failed: %d\n", ret); |
| 333 |
goto failed; |
| 334 |
} |
| 335 |
*xusb_iface = iface; |
| 336 |
return 0; |
| 337 |
failed: |
| 338 |
if (iface) |
| 339 |
xusb_release(iface); |
| 340 |
return ret; |
| 341 |
} |
| 342 |
|
| 343 |
void xusb_destroy(struct xusb_device *xusb_device) |
| 344 |
{ |
| 345 |
if (xusb_device) { |
| 346 |
struct xusb_iface **piface; |
| 347 |
struct libusb_implementation *impl; |
| 348 |
|
| 349 |
for (piface = xusb_device->interfaces; *piface; piface++) { |
| 350 |
xusb_destroy_interface(*piface); |
| 351 |
*piface = NULL; |
| 352 |
} |
| 353 |
impl = xusb_device->impl; |
| 354 |
if (impl) { |
| 355 |
if (impl->handle) { |
| 356 |
xusb_close(xusb_device); |
| 357 |
impl->handle = NULL; |
| 358 |
} |
| 359 |
if (impl->config_desc) |
| 360 |
libusb_free_config_descriptor(impl->config_desc); |
| 361 |
} |
| 362 |
DBG("%s: MEM: FREE device\n", xusb_device->devpath_tail); |
| 363 |
memset(xusb_device, 0, sizeof(*xusb_device)); |
| 364 |
free(xusb_device); |
| 365 |
xusb_device = NULL; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
static int init_interfaces(struct xusb_device *xusb_device) |
| 370 |
{ |
| 371 |
const struct libusb_config_descriptor *config_desc; |
| 372 |
const struct libusb_interface_descriptor *iface_desc; |
| 373 |
struct xusb_iface *iface; |
| 374 |
int max_packet_size = 0; |
| 375 |
int packet_size; |
| 376 |
int if_idx; |
| 377 |
|
| 378 |
assert(xusb_device); |
| 379 |
assert(xusb_device->impl); |
| 380 |
config_desc = xusb_device->impl->config_desc; |
| 381 |
assert(config_desc->bNumInterfaces < XUSB_MAX_INTERFACES); |
| 382 |
for (if_idx = 0; if_idx < config_desc->bNumInterfaces; if_idx++) { |
| 383 |
int ep_idx; |
| 384 |
|
| 385 |
iface_desc = get_iface_descriptor(xusb_device, if_idx); |
| 386 |
if (iface_desc->bInterfaceNumber != if_idx) { |
| 387 |
ERR("%s: interface %d is number %d\n", |
| 388 |
xusb_device->devpath_tail, |
| 389 |
if_idx, iface_desc->bInterfaceNumber); |
| 390 |
return -EINVAL; |
| 391 |
} |
| 392 |
if (iface_desc->bNumEndpoints != 2) { |
| 393 |
ERR("%s: interface %d has %d endpoints\n", |
| 394 |
xusb_device->devpath_tail, |
| 395 |
if_idx, iface_desc->bNumEndpoints); |
| 396 |
return -EINVAL; |
| 397 |
} |
| 398 |
iface = calloc(sizeof(*iface), 1); |
| 399 |
if (!iface) { |
| 400 |
ERR("%s: interface %d -- out of memory\n", |
| 401 |
xusb_device->devpath_tail, if_idx); |
| 402 |
return -ENOMEM; |
| 403 |
} |
| 404 |
DBG("MEM: ALLOC interface: %p\n", iface); |
| 405 |
xusb_device->interfaces[if_idx] = iface; |
| 406 |
iface->xusb_device = xusb_device; |
| 407 |
iface->interface_num = iface_desc->bInterfaceNumber; |
| 408 |
|
| 409 |
/* Search Endpoints */ |
| 410 |
iface->ep_in = 0; |
| 411 |
iface->ep_out = 0; |
| 412 |
for (ep_idx = 0; ep_idx < iface_desc->bNumEndpoints; ep_idx++) { |
| 413 |
int ep_num; |
| 414 |
|
| 415 |
ep_num = iface_desc->endpoint[ep_idx].bEndpointAddress; |
| 416 |
packet_size = libusb_get_max_packet_size(xusb_device->impl->dev, ep_num); |
| 417 |
if (!max_packet_size || packet_size < max_packet_size) |
| 418 |
max_packet_size = packet_size; |
| 419 |
if (EP_IS_OUT(ep_num)) |
| 420 |
iface->ep_out = ep_num; |
| 421 |
if (EP_IS_IN(ep_num)) |
| 422 |
iface->ep_in = ep_num; |
| 423 |
} |
| 424 |
/* Validation */ |
| 425 |
if (!iface->ep_out) { |
| 426 |
ERR("%s[%d]: Missing output endpoint\n", |
| 427 |
xusb_device->devpath_tail, if_idx); |
| 428 |
return -EINVAL; |
| 429 |
} |
| 430 |
if (!iface->ep_in) { |
| 431 |
ERR("%s[%d]: Missing input endpoint\n", |
| 432 |
xusb_device->devpath_tail, if_idx); |
| 433 |
return -EINVAL; |
| 434 |
} |
| 435 |
|
| 436 |
iface->is_claimed = 0; |
| 437 |
XUSB_DBG(iface, "ep_out=0x%X ep_in=0x%X packet_size=%d\n", |
| 438 |
iface->ep_out, iface->ep_in, max_packet_size); |
| 439 |
} |
| 440 |
if (xusb_device->packet_size < max_packet_size) |
| 441 |
xusb_device->packet_size = max_packet_size; |
| 442 |
xusb_device->is_usb2 = (xusb_device->packet_size == 512); |
| 443 |
return 0; |
| 444 |
} |
| 445 |
|
| 446 |
static struct xusb_device *xusb_new(struct libusb_device *dev, |
| 447 |
const struct xusb_spec *spec) |
| 448 |
{ |
| 449 |
struct libusb_device_descriptor dev_desc; |
| 450 |
int ret; |
| 451 |
struct xusb_device *xusb_device = NULL; |
| 452 |
|
| 453 |
xusb_device = calloc(sizeof(*xusb_device) + sizeof(struct libusb_implementation), 1); |
| 454 |
if (!xusb_device) { |
| 455 |
ERR("Out of memory"); |
| 456 |
goto fail; |
| 457 |
} |
| 458 |
DBG("MEM: ALLOC device: %p\n", xusb_device); |
| 459 |
/* Fill xusb_device */ |
| 460 |
xusb_device->impl = (void *)xusb_device + sizeof(*xusb_device); |
| 461 |
xusb_device->impl->dev = dev; |
| 462 |
xusb_device->spec = spec; |
| 463 |
xusb_device->bus_num = libusb_get_bus_number(dev); |
| 464 |
xusb_device->device_num = libusb_get_device_address(dev); |
| 465 |
snprintf(xusb_device->devpath_tail, PATH_MAX, "%03d/%03d", |
| 466 |
xusb_device->bus_num, xusb_device->device_num); |
| 467 |
/* |
| 468 |
* Opening the device |
| 469 |
*/ |
| 470 |
ret = xusb_open(xusb_device); |
| 471 |
if (ret < 0) { |
| 472 |
ERR("%s: Failed to open usb device: %s\n", |
| 473 |
xusb_device->devpath_tail, |
| 474 |
strerror(-ret)); |
| 475 |
goto fail; |
| 476 |
} |
| 477 |
/* |
| 478 |
* Get information from the usb_device |
| 479 |
*/ |
| 480 |
ret = libusb_get_device_descriptor(dev, &dev_desc); |
| 481 |
if (ret) { |
| 482 |
ERR("%s: libusb_get_device_descriptor() failed: %s\n", |
| 483 |
xusb_device->devpath_tail, |
| 484 |
libusb_error_name(ret)); |
| 485 |
goto fail; |
| 486 |
} |
| 487 |
xusb_device->idVendor = dev_desc.idVendor; |
| 488 |
xusb_device->idProduct = dev_desc.idProduct; |
| 489 |
if (!match_device(xusb_device, spec)) { |
| 490 |
DBG("[%04X:%04X] did not match\n", |
| 491 |
xusb_device->idVendor, xusb_device->idProduct); |
| 492 |
goto fail; |
| 493 |
} |
| 494 |
DBG("%s: process [%X:%X]\n", |
| 495 |
xusb_device->devpath_tail, |
| 496 |
xusb_device->idVendor, |
| 497 |
xusb_device->idProduct); |
| 498 |
ret = libusb_get_config_descriptor(dev, 0, &xusb_device->impl->config_desc); |
| 499 |
if (ret) { |
| 500 |
ERR("%s: libusb_get_config_descriptor() failed: %s\n", |
| 501 |
xusb_device->devpath_tail, |
| 502 |
libusb_error_name(ret)); |
| 503 |
goto fail; |
| 504 |
} |
| 505 |
ret = init_interfaces(xusb_device); |
| 506 |
if (ret < 0) { |
| 507 |
ERR("%s: init_interfaces() failed (ret = %d)\n", |
| 508 |
xusb_device->devpath_tail, ret); |
| 509 |
goto fail; |
| 510 |
} |
| 511 |
DBG("%s: Created %04X:%04X\n", |
| 512 |
xusb_device->devpath_tail, |
| 513 |
xusb_device->idVendor, |
| 514 |
xusb_device->idProduct); |
| 515 |
return xusb_device; |
| 516 |
fail: |
| 517 |
xusb_destroy(xusb_device); |
| 518 |
return NULL; |
| 519 |
} |
| 520 |
|
| 521 |
struct xusb_iface *xusb_find_iface(const char *devpath, |
| 522 |
int iface_num, |
| 523 |
int ep_out, |
| 524 |
int ep_in, |
| 525 |
struct xusb_spec *dummy_spec) |
| 526 |
{ |
| 527 |
ERR("FIXME: Unimplemented yet\n"); |
| 528 |
return NULL; |
| 529 |
} |
| 530 |
|
| 531 |
struct xusb_device *xusb_find_bypath(const char *path) |
| 532 |
{ |
| 533 |
struct xusb_spec *spec; |
| 534 |
libusb_device **list; |
| 535 |
ssize_t cnt; |
| 536 |
int i; |
| 537 |
|
| 538 |
DBG("path='%s'\n", path); |
| 539 |
spec = calloc(sizeof(*spec), 1); |
| 540 |
if (!spec) { |
| 541 |
ERR("Failed allocating spec\n"); |
| 542 |
goto failed; |
| 543 |
} |
| 544 |
cnt = libusb_get_device_list(NULL, &list); |
| 545 |
if (cnt < 0) { |
| 546 |
ERR("libusb_get_device_list() failed"); |
| 547 |
goto failed; |
| 548 |
} |
| 549 |
for (i = 0; i < cnt; i++) { |
| 550 |
struct libusb_device_descriptor dev_desc; |
| 551 |
libusb_device *dev = list[i]; |
| 552 |
struct xusb_device *xusb_device; |
| 553 |
char devpath_tail[PATH_MAX]; |
| 554 |
int bus_num; |
| 555 |
int device_num; |
| 556 |
int ret; |
| 557 |
|
| 558 |
bus_num = libusb_get_bus_number(dev); |
| 559 |
device_num = libusb_get_device_address(dev); |
| 560 |
snprintf(devpath_tail, PATH_MAX, "%03d/%03d", |
| 561 |
bus_num, device_num); |
| 562 |
if (!match_devpath(path, devpath_tail)) |
| 563 |
continue; |
| 564 |
ret = libusb_get_device_descriptor(dev, &dev_desc); |
| 565 |
if (ret < 0) { |
| 566 |
ERR("usb device without a device descriptor\n"); |
| 567 |
continue; |
| 568 |
} |
| 569 |
DBG("Found: %04x:%04x %s\n", |
| 570 |
dev_desc.idVendor, dev_desc.idProduct, devpath_tail); |
| 571 |
xusb_init_spec(spec, "<BYPATH>", |
| 572 |
dev_desc.idVendor, dev_desc.idProduct); |
| 573 |
xusb_device = xusb_new(dev, spec); |
| 574 |
if (!xusb_device) { |
| 575 |
ERR("Failed creating xusb for %s\n", |
| 576 |
devpath_tail); |
| 577 |
xusb_init_spec(spec, "<EMPTY>", 0, 0); |
| 578 |
continue; |
| 579 |
} |
| 580 |
return xusb_device; |
| 581 |
} |
| 582 |
failed: |
| 583 |
if (spec) |
| 584 |
free(spec); |
| 585 |
return NULL; |
| 586 |
} |
| 587 |
|
| 588 |
struct xlist_node *xusb_find_byproduct(const struct xusb_spec *specs, |
| 589 |
int numspecs, xusb_filter_t filterfunc, void *data) |
| 590 |
{ |
| 591 |
struct xlist_node *xlist; |
| 592 |
libusb_device **list; |
| 593 |
ssize_t cnt; |
| 594 |
int i; |
| 595 |
|
| 596 |
DBG("specs(%d)\n", numspecs); |
| 597 |
xlist = xlist_new(NULL); |
| 598 |
if (!xlist) { |
| 599 |
ERR("Failed allocation new xlist"); |
| 600 |
goto cleanup; |
| 601 |
} |
| 602 |
cnt = libusb_get_device_list(NULL, &list); |
| 603 |
if (cnt < 0) { |
| 604 |
ERR("libusb_get_device_list() failed"); |
| 605 |
goto cleanup; |
| 606 |
} |
| 607 |
for (i = 0; i < cnt; i++) { |
| 608 |
struct libusb_device_descriptor dev_desc; |
| 609 |
libusb_device *dev = list[i]; |
| 610 |
struct xlist_node *item; |
| 611 |
int ret; |
| 612 |
int j; |
| 613 |
|
| 614 |
ret = libusb_get_device_descriptor(dev, &dev_desc); |
| 615 |
if (ret < 0) { |
| 616 |
ERR("usb device without a device descriptor\n"); |
| 617 |
continue; |
| 618 |
} |
| 619 |
for (j = 0; j < numspecs; j++) { |
| 620 |
struct xusb_device *xusb_device; |
| 621 |
const struct xusb_spec *sp = &specs[j]; |
| 622 |
|
| 623 |
xusb_device = xusb_new(dev, sp); |
| 624 |
if (!xusb_device) |
| 625 |
continue; |
| 626 |
if (filterfunc && !filterfunc(xusb_device, data)) { |
| 627 |
DBG("%s: %04X:%04X filtered out\n", |
| 628 |
xusb_device->devpath_tail, |
| 629 |
dev_desc.idVendor, |
| 630 |
dev_desc.idProduct); |
| 631 |
xusb_destroy(xusb_device); |
| 632 |
continue; |
| 633 |
} |
| 634 |
item = xlist_new(xusb_device); |
| 635 |
xlist_append_item(xlist, item); |
| 636 |
break; |
| 637 |
} |
| 638 |
} |
| 639 |
xusb_list_dump(xlist); |
| 640 |
return xlist; |
| 641 |
cleanup: |
| 642 |
if (xlist) |
| 643 |
xlist_destroy(xlist, NULL); |
| 644 |
return NULL; |
| 645 |
} |
| 646 |
|
| 647 |
void xusb_showinfo(const struct xusb_device *xusb_device) |
| 648 |
{ |
| 649 |
struct libusb_device_descriptor dev_desc; |
| 650 |
struct libusb_device *dev; |
| 651 |
const struct xusb_iface **piface; |
| 652 |
int ret; |
| 653 |
|
| 654 |
assert(xusb_device); |
| 655 |
dev = xusb_device->impl->dev; |
| 656 |
ret = libusb_get_device_descriptor(dev, &dev_desc); |
| 657 |
if (ret < 0) { |
| 658 |
ERR("%s: usb device without a device descriptor\n", |
| 659 |
xusb_device->devpath_tail); |
| 660 |
return; |
| 661 |
} |
| 662 |
if (verbose <= LOG_INFO) { |
| 663 |
INFO("%s: [%04X:%04X] [%s / %s / %s]\n", |
| 664 |
xusb_device->devpath_tail, |
| 665 |
dev_desc.idVendor, |
| 666 |
dev_desc.idProduct, |
| 667 |
xusb_device->iManufacturer, |
| 668 |
xusb_device->iProduct, |
| 669 |
xusb_device->iSerialNumber); |
| 670 |
} else { |
| 671 |
printf("USB Bus/Device: [%03d/%03d] (%s)\n", |
| 672 |
xusb_device->bus_num, |
| 673 |
xusb_device->device_num, |
| 674 |
(xusb_device->impl->handle) ? "open" : "closed"); |
| 675 |
printf("USB Spec name: [%s]\n", xusb_device->spec->name); |
| 676 |
printf("USB iManufacturer: [%s]\n", xusb_device->iManufacturer); |
| 677 |
printf("USB iProduct: [%s]\n", xusb_device->iProduct); |
| 678 |
printf("USB iSerialNumber: [%s]\n", xusb_device->iSerialNumber); |
| 679 |
piface = (const struct xusb_iface **)xusb_device->interfaces; |
| 680 |
for (; *piface; piface++) { |
| 681 |
printf("USB Interface[%d]: ep_out=0x%02X ep_in=0x%02X claimed=%d [%s]\n", |
| 682 |
(*piface)->interface_num, |
| 683 |
(*piface)->ep_out, |
| 684 |
(*piface)->ep_in, |
| 685 |
(*piface)->is_claimed, |
| 686 |
(*piface)->iInterface); |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
int xusb_close(struct xusb_device *xusb_device) |
| 692 |
{ |
| 693 |
if (xusb_device && xusb_device->impl && xusb_device->impl->handle) { |
| 694 |
assert(xusb_device->spec); |
| 695 |
assert(xusb_device->spec->name); |
| 696 |
DBG("%s: Closing device \"%s\"\n", |
| 697 |
xusb_device->devpath_tail, |
| 698 |
xusb_device->spec->name); |
| 699 |
libusb_close(xusb_device->impl->handle); |
| 700 |
xusb_device->impl->handle = NULL; |
| 701 |
} |
| 702 |
return 0; |
| 703 |
} |
| 704 |
|
| 705 |
enum xusb_transfer_type xusb_transfer_type(const struct xusb_iface *iface) |
| 706 |
{ |
| 707 |
const struct xusb_device *xusb_device; |
| 708 |
const struct libusb_interface_descriptor *iface_desc; |
| 709 |
const struct libusb_endpoint_descriptor *ep; |
| 710 |
enum xusb_transfer_type ret = XUSB_TT_ILLEGAL; |
| 711 |
|
| 712 |
assert(iface); |
| 713 |
xusb_device = iface->xusb_device; |
| 714 |
assert(xusb_device); |
| 715 |
iface_desc = get_iface_descriptor(xusb_device, iface->interface_num); |
| 716 |
assert(iface_desc); |
| 717 |
ep = iface_desc->endpoint; |
| 718 |
assert(ep); |
| 719 |
switch (ep->bmAttributes) { |
| 720 |
case LIBUSB_TRANSFER_TYPE_CONTROL: |
| 721 |
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: |
| 722 |
break; |
| 723 |
case LIBUSB_TRANSFER_TYPE_BULK: |
| 724 |
ret = XUSB_TT_BULK; |
| 725 |
break; |
| 726 |
case LIBUSB_TRANSFER_TYPE_INTERRUPT: |
| 727 |
ret = XUSB_TT_INTERRUPT; |
| 728 |
break; |
| 729 |
} |
| 730 |
return ret; |
| 731 |
} |
| 732 |
|
| 733 |
int xusb_send(struct xusb_iface *iface, const char *buf, int len, int timeout) |
| 734 |
{ |
| 735 |
struct xusb_device *xusb_device = iface->xusb_device; |
| 736 |
int actual_len; |
| 737 |
int ep_out = EP_OUT(iface); |
| 738 |
int ret; |
| 739 |
|
| 740 |
if (!xusb_device->impl->handle) { |
| 741 |
ERR("%s: device closed\n", xusb_device->devpath_tail); |
| 742 |
return -ENXIO; |
| 743 |
} |
| 744 |
dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, len); |
| 745 |
if (!EP_IS_OUT(ep_out)) { |
| 746 |
XUSB_ERR(iface, "%s called with an input endpoint 0x%x\n", |
| 747 |
__func__, ep_out); |
| 748 |
return -EINVAL; |
| 749 |
} |
| 750 |
switch (iface->transfer_type) { |
| 751 |
case XUSB_TT_BULK: |
| 752 |
ret = libusb_bulk_transfer(xusb_device->impl->handle, ep_out, |
| 753 |
(unsigned char *)buf, len, &actual_len, timeout); |
| 754 |
break; |
| 755 |
case XUSB_TT_INTERRUPT: |
| 756 |
ret = libusb_interrupt_transfer(xusb_device->impl->handle, ep_out, |
| 757 |
(unsigned char *)buf, len, &actual_len, timeout); |
| 758 |
break; |
| 759 |
default: |
| 760 |
ret = -EAFNOSUPPORT; |
| 761 |
break; |
| 762 |
} |
| 763 |
if (ret) { |
| 764 |
/* |
| 765 |
* If the device was gone, it may be the |
| 766 |
* result of renumeration. Ignore it. |
| 767 |
*/ |
| 768 |
if (ret != LIBUSB_ERROR_NO_DEVICE) { |
| 769 |
XUSB_ERR(iface, "write to endpoint 0x%x failed: (%d) %s\n", |
| 770 |
ep_out, ret, libusb_error_name(ret)); |
| 771 |
dump_packet(LOG_ERR, DBG_MASK, "xbus_send[ERR]", |
| 772 |
buf, len); |
| 773 |
/*exit(2);*/ |
| 774 |
} else { |
| 775 |
XUSB_DBG(iface, "write to endpoint 0x%x got ENODEV\n", |
| 776 |
ep_out); |
| 777 |
xusb_close(xusb_device); |
| 778 |
} |
| 779 |
return errno_map(ret); |
| 780 |
} else if (actual_len != len) { |
| 781 |
XUSB_ERR(iface, "write to endpoint 0x%x short write: (%d) %s\n", |
| 782 |
ep_out, ret, libusb_error_name(ret)); |
| 783 |
dump_packet(LOG_ERR, DBG_MASK, "xbus_send[ERR]", buf, len); |
| 784 |
return -EFAULT; |
| 785 |
} |
| 786 |
return actual_len; |
| 787 |
} |
| 788 |
|
| 789 |
int xusb_recv(struct xusb_iface *iface, char *buf, size_t len, int timeout) |
| 790 |
{ |
| 791 |
struct xusb_device *xusb_device = iface->xusb_device; |
| 792 |
int actual_len; |
| 793 |
int ep_in = EP_IN(iface); |
| 794 |
int ret; |
| 795 |
|
| 796 |
if (!xusb_device->impl->handle) { |
| 797 |
ERR("%s: device closed\n", xusb_device->devpath_tail); |
| 798 |
return -ENXIO; |
| 799 |
} |
| 800 |
if (!EP_IS_IN(ep_in)) { |
| 801 |
XUSB_ERR(iface, "%s called with an output endpoint 0x%x\n", |
| 802 |
__func__, ep_in); |
| 803 |
return -EINVAL; |
| 804 |
} |
| 805 |
switch (iface->transfer_type) { |
| 806 |
case XUSB_TT_BULK: |
| 807 |
ret = libusb_bulk_transfer(xusb_device->impl->handle, ep_in, |
| 808 |
(unsigned char *)buf, len, &actual_len, timeout); |
| 809 |
break; |
| 810 |
case XUSB_TT_INTERRUPT: |
| 811 |
ret = libusb_interrupt_transfer(xusb_device->impl->handle, ep_in, |
| 812 |
(unsigned char *)buf, len, &actual_len, timeout); |
| 813 |
break; |
| 814 |
default: |
| 815 |
ret = -EAFNOSUPPORT; |
| 816 |
break; |
| 817 |
} |
| 818 |
if (ret < 0) { |
| 819 |
XUSB_DBG(iface, "read from endpoint 0x%x failed: (%d) %s\n", |
| 820 |
ep_in, ret, libusb_error_name(ret)); |
| 821 |
memset(buf, 0, len); |
| 822 |
return errno_map(ret); |
| 823 |
} |
| 824 |
dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, actual_len); |
| 825 |
return actual_len; |
| 826 |
} |