| 1 |
/* |
| 2 |
* Copyright © 2011 Martin Pieuchot <[email protected]> |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
|
| 21 |
#include <sys/time.h> |
| 22 |
#include <sys/types.h> |
| 23 |
|
| 24 |
#include <errno.h> |
| 25 |
#include <fcntl.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <unistd.h> |
| 30 |
|
| 31 |
#include <dev/usb/usb.h> |
| 32 |
|
| 33 |
#include "libusbi.h" |
| 34 |
|
| 35 |
struct device_priv { |
| 36 |
char devnode[16]; |
| 37 |
int fd; |
| 38 |
|
| 39 |
usb_config_descriptor_t *cdesc; /* active config descriptor */ |
| 40 |
}; |
| 41 |
|
| 42 |
struct handle_priv { |
| 43 |
int endpoints[USB_MAX_ENDPOINTS]; |
| 44 |
}; |
| 45 |
|
| 46 |
/* |
| 47 |
* Backend functions |
| 48 |
*/ |
| 49 |
static int netbsd_get_device_list(struct libusb_context *, |
| 50 |
struct discovered_devs **); |
| 51 |
static int netbsd_open(struct libusb_device_handle *); |
| 52 |
static void netbsd_close(struct libusb_device_handle *); |
| 53 |
|
| 54 |
static int netbsd_get_active_config_descriptor(struct libusb_device *, |
| 55 |
void *, size_t); |
| 56 |
static int netbsd_get_config_descriptor(struct libusb_device *, uint8_t, |
| 57 |
void *, size_t); |
| 58 |
|
| 59 |
static int netbsd_get_configuration(struct libusb_device_handle *, uint8_t *); |
| 60 |
static int netbsd_set_configuration(struct libusb_device_handle *, int); |
| 61 |
|
| 62 |
static int netbsd_claim_interface(struct libusb_device_handle *, uint8_t); |
| 63 |
static int netbsd_release_interface(struct libusb_device_handle *, uint8_t); |
| 64 |
|
| 65 |
static int netbsd_set_interface_altsetting(struct libusb_device_handle *, |
| 66 |
uint8_t, uint8_t); |
| 67 |
static int netbsd_clear_halt(struct libusb_device_handle *, unsigned char); |
| 68 |
static void netbsd_destroy_device(struct libusb_device *); |
| 69 |
|
| 70 |
static int netbsd_submit_transfer(struct usbi_transfer *); |
| 71 |
static int netbsd_cancel_transfer(struct usbi_transfer *); |
| 72 |
static int netbsd_handle_transfer_completion(struct usbi_transfer *); |
| 73 |
|
| 74 |
/* |
| 75 |
* Private functions |
| 76 |
*/ |
| 77 |
static int _errno_to_libusb(int); |
| 78 |
static int _cache_active_config_descriptor(struct libusb_device *, int); |
| 79 |
static int _sync_control_transfer(struct usbi_transfer *); |
| 80 |
static int _sync_gen_transfer(struct usbi_transfer *); |
| 81 |
static int _access_endpoint(struct libusb_transfer *); |
| 82 |
|
| 83 |
const struct usbi_os_backend usbi_backend = { |
| 84 |
.name = "Synchronous NetBSD backend", |
| 85 |
.caps = 0, |
| 86 |
.get_device_list = netbsd_get_device_list, |
| 87 |
.open = netbsd_open, |
| 88 |
.close = netbsd_close, |
| 89 |
|
| 90 |
.get_active_config_descriptor = netbsd_get_active_config_descriptor, |
| 91 |
.get_config_descriptor = netbsd_get_config_descriptor, |
| 92 |
|
| 93 |
.get_configuration = netbsd_get_configuration, |
| 94 |
.set_configuration = netbsd_set_configuration, |
| 95 |
|
| 96 |
.claim_interface = netbsd_claim_interface, |
| 97 |
.release_interface = netbsd_release_interface, |
| 98 |
|
| 99 |
.set_interface_altsetting = netbsd_set_interface_altsetting, |
| 100 |
.clear_halt = netbsd_clear_halt, |
| 101 |
|
| 102 |
.destroy_device = netbsd_destroy_device, |
| 103 |
|
| 104 |
.submit_transfer = netbsd_submit_transfer, |
| 105 |
.cancel_transfer = netbsd_cancel_transfer, |
| 106 |
|
| 107 |
.handle_transfer_completion = netbsd_handle_transfer_completion, |
| 108 |
|
| 109 |
.device_priv_size = sizeof(struct device_priv), |
| 110 |
.device_handle_priv_size = sizeof(struct handle_priv), |
| 111 |
}; |
| 112 |
|
| 113 |
int |
| 114 |
netbsd_get_device_list(struct libusb_context * ctx, |
| 115 |
struct discovered_devs **discdevs) |
| 116 |
{ |
| 117 |
struct libusb_device *dev; |
| 118 |
struct device_priv *dpriv; |
| 119 |
struct usb_device_info di; |
| 120 |
usb_device_descriptor_t ddesc; |
| 121 |
unsigned long session_id; |
| 122 |
char devnode[16]; |
| 123 |
int fd, err, i; |
| 124 |
|
| 125 |
usbi_dbg(ctx, " "); |
| 126 |
|
| 127 |
/* Only ugen(4) is supported */ |
| 128 |
for (i = 0; i < USB_MAX_DEVICES; i++) { |
| 129 |
/* Control endpoint is always .00 */ |
| 130 |
snprintf(devnode, sizeof(devnode), "/dev/ugen%d.00", i); |
| 131 |
|
| 132 |
if ((fd = open(devnode, O_RDONLY)) < 0) { |
| 133 |
if (errno != ENOENT && errno != ENXIO) |
| 134 |
usbi_err(ctx, "could not open %s", devnode); |
| 135 |
continue; |
| 136 |
} |
| 137 |
|
| 138 |
if (ioctl(fd, USB_GET_DEVICEINFO, &di) < 0) |
| 139 |
continue; |
| 140 |
|
| 141 |
session_id = (di.udi_bus << 8 | di.udi_addr); |
| 142 |
dev = usbi_get_device_by_session_id(ctx, session_id); |
| 143 |
|
| 144 |
if (dev == NULL) { |
| 145 |
dev = usbi_alloc_device(ctx, session_id); |
| 146 |
if (dev == NULL) |
| 147 |
return LIBUSB_ERROR_NO_MEM; |
| 148 |
|
| 149 |
dev->bus_number = 1 + di.udi_bus; |
| 150 |
dev->device_address = 1 + di.udi_addr; |
| 151 |
dev->speed = di.udi_speed; /* NetBSD #define's happen to match libusb enum */ |
| 152 |
|
| 153 |
dpriv = usbi_get_device_priv(dev); |
| 154 |
strlcpy(dpriv->devnode, devnode, sizeof(devnode)); |
| 155 |
dpriv->fd = -1; |
| 156 |
|
| 157 |
if (ioctl(fd, USB_GET_DEVICE_DESC, &ddesc) < 0) { |
| 158 |
err = errno; |
| 159 |
goto error; |
| 160 |
} |
| 161 |
|
| 162 |
static_assert(sizeof(dev->device_descriptor) == sizeof(ddesc), |
| 163 |
"mismatch between libusb and OS device descriptor sizes"); |
| 164 |
memcpy(&dev->device_descriptor, &ddesc, LIBUSB_DT_DEVICE_SIZE); |
| 165 |
usbi_localize_device_descriptor(&dev->device_descriptor); |
| 166 |
|
| 167 |
if (_cache_active_config_descriptor(dev, fd)) { |
| 168 |
err = errno; |
| 169 |
goto error; |
| 170 |
} |
| 171 |
|
| 172 |
if ((err = usbi_sanitize_device(dev))) |
| 173 |
goto error; |
| 174 |
|
| 175 |
usbi_connect_device(dev); |
| 176 |
} |
| 177 |
close(fd); |
| 178 |
|
| 179 |
if (discovered_devs_append(*discdevs, dev) == NULL) |
| 180 |
return LIBUSB_ERROR_NO_MEM; |
| 181 |
|
| 182 |
libusb_unref_device(dev); |
| 183 |
} |
| 184 |
|
| 185 |
return LIBUSB_SUCCESS; |
| 186 |
|
| 187 |
error: |
| 188 |
close(fd); |
| 189 |
libusb_unref_device(dev); |
| 190 |
return _errno_to_libusb(err); |
| 191 |
} |
| 192 |
|
| 193 |
int |
| 194 |
netbsd_open(struct libusb_device_handle *handle) |
| 195 |
{ |
| 196 |
struct device_priv *dpriv = usbi_get_device_priv(handle->dev); |
| 197 |
struct handle_priv *hpriv = usbi_get_device_handle_priv(handle); |
| 198 |
int i; |
| 199 |
|
| 200 |
dpriv->fd = open(dpriv->devnode, O_RDWR); |
| 201 |
if (dpriv->fd < 0) { |
| 202 |
dpriv->fd = open(dpriv->devnode, O_RDONLY); |
| 203 |
if (dpriv->fd < 0) |
| 204 |
return _errno_to_libusb(errno); |
| 205 |
} |
| 206 |
|
| 207 |
for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
| 208 |
hpriv->endpoints[i] = -1; |
| 209 |
|
| 210 |
usbi_dbg(HANDLE_CTX(handle), "open %s: fd %d", dpriv->devnode, dpriv->fd); |
| 211 |
|
| 212 |
return LIBUSB_SUCCESS; |
| 213 |
} |
| 214 |
|
| 215 |
void |
| 216 |
netbsd_close(struct libusb_device_handle *handle) |
| 217 |
{ |
| 218 |
struct device_priv *dpriv = usbi_get_device_priv(handle->dev); |
| 219 |
|
| 220 |
usbi_dbg(HANDLE_CTX(handle), "close: fd %d", dpriv->fd); |
| 221 |
|
| 222 |
close(dpriv->fd); |
| 223 |
dpriv->fd = -1; |
| 224 |
} |
| 225 |
|
| 226 |
int |
| 227 |
netbsd_get_active_config_descriptor(struct libusb_device *dev, |
| 228 |
void *buf, size_t len) |
| 229 |
{ |
| 230 |
struct device_priv *dpriv = usbi_get_device_priv(dev); |
| 231 |
|
| 232 |
len = MIN(len, (size_t)UGETW(dpriv->cdesc->wTotalLength)); |
| 233 |
|
| 234 |
usbi_dbg(DEVICE_CTX(dev), "len %zu", len); |
| 235 |
|
| 236 |
memcpy(buf, dpriv->cdesc, len); |
| 237 |
|
| 238 |
return (int)len; |
| 239 |
} |
| 240 |
|
| 241 |
int |
| 242 |
netbsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx, |
| 243 |
void *buf, size_t len) |
| 244 |
{ |
| 245 |
struct device_priv *dpriv = usbi_get_device_priv(dev); |
| 246 |
struct usb_full_desc ufd; |
| 247 |
int fd, err; |
| 248 |
|
| 249 |
usbi_dbg(DEVICE_CTX(dev), "index %u, len %zu", idx, len); |
| 250 |
|
| 251 |
/* A config descriptor may be requested before opening the device */ |
| 252 |
if (dpriv->fd >= 0) { |
| 253 |
fd = dpriv->fd; |
| 254 |
} else { |
| 255 |
fd = open(dpriv->devnode, O_RDONLY); |
| 256 |
if (fd < 0) |
| 257 |
return _errno_to_libusb(errno); |
| 258 |
} |
| 259 |
|
| 260 |
ufd.ufd_config_index = idx; |
| 261 |
ufd.ufd_size = len; |
| 262 |
ufd.ufd_data = buf; |
| 263 |
|
| 264 |
if (ioctl(fd, USB_GET_FULL_DESC, &ufd) < 0) { |
| 265 |
err = errno; |
| 266 |
if (dpriv->fd < 0) |
| 267 |
close(fd); |
| 268 |
return _errno_to_libusb(err); |
| 269 |
} |
| 270 |
|
| 271 |
if (dpriv->fd < 0) |
| 272 |
close(fd); |
| 273 |
|
| 274 |
return (int)len; |
| 275 |
} |
| 276 |
|
| 277 |
int |
| 278 |
netbsd_get_configuration(struct libusb_device_handle *handle, uint8_t *config) |
| 279 |
{ |
| 280 |
struct device_priv *dpriv = usbi_get_device_priv(handle->dev); |
| 281 |
int tmp; |
| 282 |
|
| 283 |
usbi_dbg(HANDLE_CTX(handle), " "); |
| 284 |
|
| 285 |
if (ioctl(dpriv->fd, USB_GET_CONFIG, &tmp) < 0) |
| 286 |
return _errno_to_libusb(errno); |
| 287 |
|
| 288 |
usbi_dbg(HANDLE_CTX(handle), "configuration %d", tmp); |
| 289 |
*config = (uint8_t)tmp; |
| 290 |
|
| 291 |
return LIBUSB_SUCCESS; |
| 292 |
} |
| 293 |
|
| 294 |
int |
| 295 |
netbsd_set_configuration(struct libusb_device_handle *handle, int config) |
| 296 |
{ |
| 297 |
struct device_priv *dpriv = usbi_get_device_priv(handle->dev); |
| 298 |
|
| 299 |
usbi_dbg(HANDLE_CTX(handle), "configuration %d", config); |
| 300 |
|
| 301 |
if (ioctl(dpriv->fd, USB_SET_CONFIG, &config) < 0) |
| 302 |
return _errno_to_libusb(errno); |
| 303 |
|
| 304 |
return _cache_active_config_descriptor(handle->dev, dpriv->fd); |
| 305 |
} |
| 306 |
|
| 307 |
int |
| 308 |
netbsd_claim_interface(struct libusb_device_handle *handle, uint8_t iface) |
| 309 |
{ |
| 310 |
struct handle_priv *hpriv = usbi_get_device_handle_priv(handle); |
| 311 |
int i; |
| 312 |
|
| 313 |
UNUSED(iface); |
| 314 |
|
| 315 |
for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
| 316 |
hpriv->endpoints[i] = -1; |
| 317 |
|
| 318 |
return LIBUSB_SUCCESS; |
| 319 |
} |
| 320 |
|
| 321 |
int |
| 322 |
netbsd_release_interface(struct libusb_device_handle *handle, uint8_t iface) |
| 323 |
{ |
| 324 |
struct handle_priv *hpriv = usbi_get_device_handle_priv(handle); |
| 325 |
int i; |
| 326 |
|
| 327 |
UNUSED(iface); |
| 328 |
|
| 329 |
for (i = 0; i < USB_MAX_ENDPOINTS; i++) |
| 330 |
if (hpriv->endpoints[i] >= 0) |
| 331 |
close(hpriv->endpoints[i]); |
| 332 |
|
| 333 |
return LIBUSB_SUCCESS; |
| 334 |
} |
| 335 |
|
| 336 |
int |
| 337 |
netbsd_set_interface_altsetting(struct libusb_device_handle *handle, uint8_t iface, |
| 338 |
uint8_t altsetting) |
| 339 |
{ |
| 340 |
struct device_priv *dpriv = usbi_get_device_priv(handle->dev); |
| 341 |
struct usb_alt_interface intf; |
| 342 |
|
| 343 |
usbi_dbg(HANDLE_CTX(handle), "iface %u, setting %u", iface, altsetting); |
| 344 |
|
| 345 |
memset(&intf, 0, sizeof(intf)); |
| 346 |
|
| 347 |
intf.uai_interface_index = iface; |
| 348 |
intf.uai_alt_no = altsetting; |
| 349 |
|
| 350 |
if (ioctl(dpriv->fd, USB_SET_ALTINTERFACE, &intf) < 0) |
| 351 |
return _errno_to_libusb(errno); |
| 352 |
|
| 353 |
return LIBUSB_SUCCESS; |
| 354 |
} |
| 355 |
|
| 356 |
int |
| 357 |
netbsd_clear_halt(struct libusb_device_handle *handle, unsigned char endpoint) |
| 358 |
{ |
| 359 |
struct device_priv *dpriv = usbi_get_device_priv(handle->dev); |
| 360 |
struct usb_ctl_request req; |
| 361 |
|
| 362 |
usbi_dbg(HANDLE_CTX(handle), " "); |
| 363 |
|
| 364 |
req.ucr_request.bmRequestType = UT_WRITE_ENDPOINT; |
| 365 |
req.ucr_request.bRequest = UR_CLEAR_FEATURE; |
| 366 |
USETW(req.ucr_request.wValue, UF_ENDPOINT_HALT); |
| 367 |
USETW(req.ucr_request.wIndex, endpoint); |
| 368 |
USETW(req.ucr_request.wLength, 0); |
| 369 |
|
| 370 |
if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0) |
| 371 |
return _errno_to_libusb(errno); |
| 372 |
|
| 373 |
return LIBUSB_SUCCESS; |
| 374 |
} |
| 375 |
|
| 376 |
void |
| 377 |
netbsd_destroy_device(struct libusb_device *dev) |
| 378 |
{ |
| 379 |
struct device_priv *dpriv = usbi_get_device_priv(dev); |
| 380 |
|
| 381 |
usbi_dbg(DEVICE_CTX(dev), " "); |
| 382 |
|
| 383 |
free(dpriv->cdesc); |
| 384 |
} |
| 385 |
|
| 386 |
int |
| 387 |
netbsd_submit_transfer(struct usbi_transfer *itransfer) |
| 388 |
{ |
| 389 |
struct libusb_transfer *transfer; |
| 390 |
int err = 0; |
| 391 |
|
| 392 |
usbi_dbg(ITRANSFER_CTX(itransfer), " "); |
| 393 |
|
| 394 |
transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 395 |
|
| 396 |
switch (transfer->type) { |
| 397 |
case LIBUSB_TRANSFER_TYPE_CONTROL: |
| 398 |
err = _sync_control_transfer(itransfer); |
| 399 |
break; |
| 400 |
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: |
| 401 |
if (IS_XFEROUT(transfer)) { |
| 402 |
/* Isochronous write is not supported */ |
| 403 |
err = LIBUSB_ERROR_NOT_SUPPORTED; |
| 404 |
break; |
| 405 |
} |
| 406 |
err = _sync_gen_transfer(itransfer); |
| 407 |
break; |
| 408 |
case LIBUSB_TRANSFER_TYPE_BULK: |
| 409 |
case LIBUSB_TRANSFER_TYPE_INTERRUPT: |
| 410 |
if (IS_XFEROUT(transfer) && |
| 411 |
transfer->flags & LIBUSB_TRANSFER_ADD_ZERO_PACKET) { |
| 412 |
err = LIBUSB_ERROR_NOT_SUPPORTED; |
| 413 |
break; |
| 414 |
} |
| 415 |
err = _sync_gen_transfer(itransfer); |
| 416 |
break; |
| 417 |
case LIBUSB_TRANSFER_TYPE_BULK_STREAM: |
| 418 |
err = LIBUSB_ERROR_NOT_SUPPORTED; |
| 419 |
break; |
| 420 |
} |
| 421 |
|
| 422 |
if (err) |
| 423 |
return err; |
| 424 |
|
| 425 |
usbi_signal_transfer_completion(itransfer); |
| 426 |
|
| 427 |
return LIBUSB_SUCCESS; |
| 428 |
} |
| 429 |
|
| 430 |
int |
| 431 |
netbsd_cancel_transfer(struct usbi_transfer *itransfer) |
| 432 |
{ |
| 433 |
UNUSED(itransfer); |
| 434 |
|
| 435 |
usbi_dbg(ITRANSFER_CTX(itransfer), " "); |
| 436 |
|
| 437 |
return LIBUSB_ERROR_NOT_SUPPORTED; |
| 438 |
} |
| 439 |
|
| 440 |
int |
| 441 |
netbsd_handle_transfer_completion(struct usbi_transfer *itransfer) |
| 442 |
{ |
| 443 |
return usbi_handle_transfer_completion(itransfer, LIBUSB_TRANSFER_COMPLETED); |
| 444 |
} |
| 445 |
|
| 446 |
int |
| 447 |
_errno_to_libusb(int err) |
| 448 |
{ |
| 449 |
usbi_dbg(NULL, "error: %s (%d)", strerror(err), err); |
| 450 |
|
| 451 |
switch (err) { |
| 452 |
case EIO: |
| 453 |
return LIBUSB_ERROR_IO; |
| 454 |
case EACCES: |
| 455 |
return LIBUSB_ERROR_ACCESS; |
| 456 |
case ENOENT: |
| 457 |
return LIBUSB_ERROR_NO_DEVICE; |
| 458 |
case ENOMEM: |
| 459 |
return LIBUSB_ERROR_NO_MEM; |
| 460 |
case EWOULDBLOCK: |
| 461 |
case ETIMEDOUT: |
| 462 |
return LIBUSB_ERROR_TIMEOUT; |
| 463 |
default: |
| 464 |
return LIBUSB_ERROR_OTHER; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
int |
| 469 |
_cache_active_config_descriptor(struct libusb_device *dev, int fd) |
| 470 |
{ |
| 471 |
struct device_priv *dpriv = usbi_get_device_priv(dev); |
| 472 |
struct usb_config_desc ucd; |
| 473 |
struct usb_full_desc ufd; |
| 474 |
void *buf; |
| 475 |
int len; |
| 476 |
|
| 477 |
usbi_dbg(DEVICE_CTX(dev), "fd %d", fd); |
| 478 |
|
| 479 |
ucd.ucd_config_index = USB_CURRENT_CONFIG_INDEX; |
| 480 |
|
| 481 |
if (ioctl(fd, USB_GET_CONFIG_DESC, &ucd) < 0) |
| 482 |
return _errno_to_libusb(errno); |
| 483 |
|
| 484 |
usbi_dbg(DEVICE_CTX(dev), "active bLength %d", ucd.ucd_desc.bLength); |
| 485 |
|
| 486 |
len = UGETW(ucd.ucd_desc.wTotalLength); |
| 487 |
buf = malloc((size_t)len); |
| 488 |
if (buf == NULL) |
| 489 |
return LIBUSB_ERROR_NO_MEM; |
| 490 |
|
| 491 |
ufd.ufd_config_index = ucd.ucd_config_index; |
| 492 |
ufd.ufd_size = len; |
| 493 |
ufd.ufd_data = buf; |
| 494 |
|
| 495 |
usbi_dbg(DEVICE_CTX(dev), "index %d, len %d", ufd.ufd_config_index, len); |
| 496 |
|
| 497 |
if (ioctl(fd, USB_GET_FULL_DESC, &ufd) < 0) { |
| 498 |
free(buf); |
| 499 |
return _errno_to_libusb(errno); |
| 500 |
} |
| 501 |
|
| 502 |
if (dpriv->cdesc) |
| 503 |
free(dpriv->cdesc); |
| 504 |
dpriv->cdesc = buf; |
| 505 |
|
| 506 |
return 0; |
| 507 |
} |
| 508 |
|
| 509 |
int |
| 510 |
_sync_control_transfer(struct usbi_transfer *itransfer) |
| 511 |
{ |
| 512 |
struct libusb_transfer *transfer; |
| 513 |
struct libusb_control_setup *setup; |
| 514 |
struct device_priv *dpriv; |
| 515 |
struct usb_ctl_request req; |
| 516 |
|
| 517 |
transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 518 |
dpriv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 519 |
setup = (struct libusb_control_setup *)transfer->buffer; |
| 520 |
|
| 521 |
usbi_dbg(ITRANSFER_CTX(itransfer), "type 0x%x request 0x%x value 0x%x index %d length %d timeout %d", |
| 522 |
setup->bmRequestType, setup->bRequest, |
| 523 |
libusb_le16_to_cpu(setup->wValue), |
| 524 |
libusb_le16_to_cpu(setup->wIndex), |
| 525 |
libusb_le16_to_cpu(setup->wLength), transfer->timeout); |
| 526 |
|
| 527 |
req.ucr_request.bmRequestType = setup->bmRequestType; |
| 528 |
req.ucr_request.bRequest = setup->bRequest; |
| 529 |
/* Don't use USETW, libusb already deals with the endianness */ |
| 530 |
(*(uint16_t *)req.ucr_request.wValue) = setup->wValue; |
| 531 |
(*(uint16_t *)req.ucr_request.wIndex) = setup->wIndex; |
| 532 |
(*(uint16_t *)req.ucr_request.wLength) = setup->wLength; |
| 533 |
req.ucr_data = transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; |
| 534 |
|
| 535 |
if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0) |
| 536 |
req.ucr_flags = USBD_SHORT_XFER_OK; |
| 537 |
|
| 538 |
if (ioctl(dpriv->fd, USB_SET_TIMEOUT, &transfer->timeout) < 0) |
| 539 |
return _errno_to_libusb(errno); |
| 540 |
|
| 541 |
if (ioctl(dpriv->fd, USB_DO_REQUEST, &req) < 0) |
| 542 |
return _errno_to_libusb(errno); |
| 543 |
|
| 544 |
itransfer->transferred = req.ucr_actlen; |
| 545 |
|
| 546 |
usbi_dbg(ITRANSFER_CTX(itransfer), "transferred %d", itransfer->transferred); |
| 547 |
|
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
int |
| 552 |
_access_endpoint(struct libusb_transfer *transfer) |
| 553 |
{ |
| 554 |
struct handle_priv *hpriv; |
| 555 |
struct device_priv *dpriv; |
| 556 |
char *s, devnode[16]; |
| 557 |
int fd, endpt; |
| 558 |
mode_t mode; |
| 559 |
|
| 560 |
hpriv = usbi_get_device_handle_priv(transfer->dev_handle); |
| 561 |
dpriv = usbi_get_device_priv(transfer->dev_handle->dev); |
| 562 |
|
| 563 |
endpt = UE_GET_ADDR(transfer->endpoint); |
| 564 |
mode = IS_XFERIN(transfer) ? O_RDONLY : O_WRONLY; |
| 565 |
|
| 566 |
usbi_dbg(TRANSFER_CTX(transfer), "endpoint %d mode %d", endpt, mode); |
| 567 |
|
| 568 |
if (hpriv->endpoints[endpt] < 0) { |
| 569 |
/* Pick the right node given the control one */ |
| 570 |
strlcpy(devnode, dpriv->devnode, sizeof(devnode)); |
| 571 |
s = strchr(devnode, '.'); |
| 572 |
snprintf(s, 4, ".%02d", endpt); |
| 573 |
|
| 574 |
/* We may need to read/write to the same endpoint later. */ |
| 575 |
if (((fd = open(devnode, O_RDWR)) < 0) && (errno == ENXIO)) |
| 576 |
if ((fd = open(devnode, mode)) < 0) |
| 577 |
return -1; |
| 578 |
|
| 579 |
hpriv->endpoints[endpt] = fd; |
| 580 |
} |
| 581 |
|
| 582 |
return hpriv->endpoints[endpt]; |
| 583 |
} |
| 584 |
|
| 585 |
int |
| 586 |
_sync_gen_transfer(struct usbi_transfer *itransfer) |
| 587 |
{ |
| 588 |
struct libusb_transfer *transfer; |
| 589 |
int fd, nr = 1; |
| 590 |
|
| 591 |
transfer = USBI_TRANSFER_TO_LIBUSB_TRANSFER(itransfer); |
| 592 |
|
| 593 |
/* |
| 594 |
* Bulk, Interrupt or Isochronous transfer depends on the |
| 595 |
* endpoint and thus the node to open. |
| 596 |
*/ |
| 597 |
if ((fd = _access_endpoint(transfer)) < 0) |
| 598 |
return _errno_to_libusb(errno); |
| 599 |
|
| 600 |
if (ioctl(fd, USB_SET_TIMEOUT, &transfer->timeout) < 0) |
| 601 |
return _errno_to_libusb(errno); |
| 602 |
|
| 603 |
if (IS_XFERIN(transfer)) { |
| 604 |
if ((transfer->flags & LIBUSB_TRANSFER_SHORT_NOT_OK) == 0) |
| 605 |
if (ioctl(fd, USB_SET_SHORT_XFER, &nr) < 0) |
| 606 |
return _errno_to_libusb(errno); |
| 607 |
|
| 608 |
nr = read(fd, transfer->buffer, transfer->length); |
| 609 |
} else { |
| 610 |
nr = write(fd, transfer->buffer, transfer->length); |
| 611 |
} |
| 612 |
|
| 613 |
if (nr < 0) |
| 614 |
return _errno_to_libusb(errno); |
| 615 |
|
| 616 |
itransfer->transferred = nr; |
| 617 |
|
| 618 |
return 0; |
| 619 |
} |