Changeset 1138 for trunk/src/ifd/sys-bsd.c
- Timestamp:
- 03/30/09 17:39:47 (3 years ago)
- File:
-
- 1 edited
-
trunk/src/ifd/sys-bsd.c (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/ifd/sys-bsd.c
r1137 r1138 14 14 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) 15 15 #include <sys/types.h> 16 #ifndef ENABLE_LIBUSB 16 17 #if defined(__DragonFly__) 17 18 #include <bus/usb/usb.h> … … 19 20 #include <dev/usb/usb.h> 20 21 #endif 22 #endif /* !ENABLE_LIBUSB */ 21 23 #include <sys/stat.h> 22 24 #include <sys/ioctl.h> … … 31 33 #include <errno.h> 32 34 #include <openct/driver.h> 35 #ifdef ENABLE_LIBUSB 36 #include <usb.h> 37 #endif 33 38 34 39 #include "usb-descriptors.h" 40 41 #ifdef ENABLE_LIBUSB 42 struct usb_dev_handle *devices[128]; 43 #endif 35 44 36 45 /* … … 46 55 } 47 56 57 #ifndef ENABLE_LIBUSB 48 58 typedef struct ep { 49 59 int ep_fd; … … 91 101 } 92 102 } 103 #endif /* !ENABLE_LIBUSB */ 93 104 94 105 int ifd_sysdep_usb_bulk(ifd_device_t * dev, int ep, void *buffer, size_t len, … … 104 115 direction); 105 116 if (direction) { 117 #ifdef ENABLE_LIBUSB 118 if ((bytes_to_process = 119 usb_bulk_read(devices[dev->fd], ep, buffer, len, 120 timeout)) < 0) { 121 ifd_debug(6, "ifd_sysdep_usb_bulk: read failed: %s", 122 strerror(errno)); 123 ct_error("usb_bulk read failed: %s", strerror(errno)); 124 return IFD_ERROR_COMM_ERROR; 125 } 126 #else 106 127 int one = 1; 107 128 … … 124 145 return IFD_ERROR_COMM_ERROR; 125 146 } 147 #endif /* ENABLE_LIBUSB */ 126 148 ct_debug("ifd_sysdep_usb_bulk: read %d bytes", 127 149 bytes_to_process); 128 150 return bytes_to_process; 129 151 } else { 152 #ifndef ENABLE_LIBUSB 130 153 if (open_ep(dev->name, 0, endpoint, O_WRONLY | O_NONBLOCK)) { 131 154 ct_debug("ifd_sysdep_usb_bulk: opening endpoint failed"); 132 155 return -1; 133 156 } 157 #endif /* !ENABLE_LIBUSB */ 134 158 135 159 bytes_to_process = len; 136 160 if ((bytes_processed = 161 #ifdef ENABLE_LIBUSB 162 usb_bulk_write(devices[dev->fd], ep, buffer, 163 bytes_to_process, timeout) 164 #else 137 165 write(interfaces[0][endpoint].ep_fd, buffer, 138 bytes_to_process)) != bytes_to_process) { 166 bytes_to_process) 167 #endif /* ENABLE_LIBUSB */ 168 ) != bytes_to_process) { 139 169 ifd_debug(6, "ifd_sysdep_usb_bulk: write failed: %s", 140 170 strerror(errno)); … … 167 197 { 168 198 ifd_usb_capture_t *cap; 199 #ifndef ENABLE_LIBUSB 169 200 int direction = 170 201 (ep & IFD_USB_ENDPOINT_DIR_MASK) == IFD_USB_ENDPOINT_IN ? 1 : 0; 171 202 int endpoint = (ep & ~IFD_USB_ENDPOINT_DIR_MASK); 203 #endif /* !ENABLE_LIBUSB */ 172 204 173 205 if (!(cap = (ifd_usb_capture_t *) calloc(1, sizeof(*cap) + maxpacket))) { … … 179 211 cap->maxpacket = maxpacket; 180 212 213 #ifndef ENABLE_LIBUSB 181 214 if (!interfaces[0][endpoint].ep_fd) { 182 215 if (open_ep(dev->name, 0, endpoint, O_RDONLY | O_NONBLOCK)) { … … 186 219 } 187 220 } 221 #endif /* !ENABLE_LIBUSB */ 188 222 *capret = cap; 189 223 return 0; … … 199 233 void *buffer, size_t len, long timeout) 200 234 { 235 int bytes_to_process = 0; 236 #ifdef ENABLE_LIBUSB 237 if ((bytes_to_process = 238 usb_interrupt_read(devices[dev->fd], cap->endpoint, buffer, len, 239 timeout)) < 0) { 240 ifd_debug(6, 241 "ifd_sysdep_usb_capture: usb_interrupt_read failed: %s", 242 strerror(errno)); 243 ct_error("usb_bulk read failed: %s", strerror(errno)); 244 return IFD_ERROR_COMM_ERROR; 245 } 246 #else 201 247 struct timeval begin; 202 int bytes_to_process = 0;203 248 int direction = 204 249 (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == … … 227 272 } 228 273 } while (!bytes_to_process); 274 #endif /* ENABLE_LIBUSB */ 229 275 ct_debug("ifd_sysdep_usb_capture: read buffer[%d]=%s", bytes_to_process, 230 276 ct_hexdump(buffer, bytes_to_process)); … … 234 280 int ifd_sysdep_usb_end_capture(ifd_device_t * dev, ifd_usb_capture_t * cap) 235 281 { 282 #ifndef ENABLE_LIBUSB 236 283 int direction = 237 284 (cap->endpoint & IFD_USB_ENDPOINT_DIR_MASK) == … … 239 286 int endpoint = (cap->endpoint & ~IFD_USB_ENDPOINT_DIR_MASK); 240 287 close_ep(0, endpoint); 288 #endif /* !ENABLE_LIBUSB */ 241 289 if (cap) 242 290 free(cap); … … 252 300 long timeout) 253 301 { 302 int rc, val; 303 304 #ifdef ENABLE_LIBUSB 305 ct_debug("ifd_sysdep_usb_control: dev->fd=%d handle=0x%x", dev->fd, 306 devices[dev->fd]); 307 if ((rc = 308 usb_control_msg(devices[dev->fd], requesttype, request, value, 309 index, data, len, timeout)) < 0) { 310 ifd_debug(1, "usb_control_msg failed: %d", rc); 311 ct_error("usb_control_msg failed: %s(%d)", 312 strerror(errno), errno); 313 return IFD_ERROR_COMM_ERROR; 314 } 315 316 ct_debug("ifd_sysdep_usb_control: return rc=%d", rc); 317 return rc; 318 #else 254 319 struct usb_ctl_request ctrl; 255 int rc, val;256 320 257 321 ifd_debug(1, "BSD: ifd_sysdep_usb_control(0x%x)", request); … … 294 358 ct_hexdump(ctrl.ucr_data, ctrl.ucr_actlen)); 295 359 return ctrl.ucr_actlen; 360 #endif /* ENABLE_LIBUSB */ 296 361 } 297 362 298 363 int ifd_sysdep_usb_set_configuration(ifd_device_t * dev, int config) 299 364 { 300 int value, rc; 365 int rc; 366 #ifdef ENABLE_LIBUSB 367 if ((rc = usb_set_configuration(devices[dev->fd], config)) < 0) { 368 ifd_debug(1, "usb_set_configuration failed: %d", rc); 369 #else 370 int value; 301 371 value = config; 302 372 if ((rc = ioctl(dev->fd, USB_SET_CONFIG, &value)) < 0) { 303 373 ifd_debug(1, "USB_SET_CONFIG failed: %d", rc); 374 #endif /* ENABLE_LIBUSB */ 304 375 ct_error("usb_set_configuration failed: %s(%d)", 305 376 strerror(errno), errno); … … 312 383 { 313 384 int rc; 385 #ifdef ENABLE_LIBUSB 386 if ((rc = usb_set_altinterface(devices[dev->fd], alt)) < 0) { 387 ifd_debug(1, "usb_set_altinterface failed: %d", rc); 388 #else 314 389 struct usb_alt_interface { 315 390 int uai_config_index; … … 323 398 if ((rc = ioctl(dev->fd, USB_SET_ALTINTERFACE, &value)) < 0) { 324 399 ifd_debug(1, "USB_SET_ALTINTERFACE failed: %d", rc); 400 #endif /* ENABLE_LIBUSB */ 325 401 ct_error("usb_set_interface failed: %s(%d)", 326 402 strerror(errno), errno); … … 332 408 int ifd_sysdep_usb_claim_interface(ifd_device_t * dev, int interface) 333 409 { 410 #ifdef ENABLE_LIBUSB 411 int rc; 412 413 ct_debug("ifd_sysdep_usb_claim_interface: interface=%d", interface); 414 if ((rc = usb_claim_interface(devices[dev->fd], interface)) < 0) { 415 ifd_debug(1, "usb_clain_interface failed: %d", rc); 416 ct_error("usb_release_interface failed: %s(%d)", 417 strerror(errno), errno); 418 return IFD_ERROR_COMM_ERROR; 419 } 420 #else 334 421 ct_debug 335 422 ("ifd_sysdep_usb_claim_interface: interface=%d (not yet implemented)", 336 423 interface); 424 #endif /* ENABLE_LIBUSB */ 337 425 return 0; 338 426 } … … 340 428 int ifd_sysdep_usb_release_interface(ifd_device_t * dev, int interface) 341 429 { 430 #ifdef ENABLE_LIBUSB 431 int rc; 432 433 ct_debug("ifd_sysdep_usb_release_interface: interface=%d", interface); 434 if ((rc = usb_release_interface(devices[dev->fd], interface)) < 0) { 435 ifd_debug(1, "usb_release_interface failed: %d", rc); 436 ct_error("usb_release_interface failed: %s(%d)", 437 strerror(errno), errno); 438 return IFD_ERROR_COMM_ERROR; 439 } 440 #else 342 441 ct_debug 343 442 ("ifd_sysdep_usb_release_interface: interface=%d (not yet implemented)", 344 443 interface); 444 #endif /* ENABLE_LIBUSB */ 345 445 return 0; 346 446 } … … 348 448 int ifd_sysdep_usb_open(const char *device) 349 449 { 450 #ifdef ENABLE_LIBUSB 451 struct usb_bus *bus; 452 struct usb_device *dev; 453 454 ct_debug("ifd_sysdep_usb_open: name=%s", device); 455 ct_debug("ifd_sysdep_usb_open: usb_init()"); 456 usb_init(); 457 ct_debug("ifd_sysdep_usb_open: usb_find_busses()"); 458 usb_find_busses(); 459 ct_debug("ifd_sysdep_usb_open: usb_find_devices()"); 460 usb_find_devices(); 461 462 ct_debug("ifd_sysdep_usb_open: walk devices"); 463 for (bus = usb_busses; bus; bus = bus->next) { 464 for (dev = bus->devices; dev; dev = dev->next) { 465 int i; 466 467 if (strcmp(dev->filename, device) != 0) 468 continue; 469 470 ct_debug 471 ("ifd_sysdep_usb_open: found match name=%s device=%s", 472 device, dev->filename); 473 for (i = 0; i < 128; i++) { 474 if (devices[i] == NULL) { 475 devices[i] = usb_open(dev); 476 ct_debug 477 ("ifd_sysdep_usb_open: usb_open index=%d handle=0x%x", 478 i, devices[i]); 479 return i; 480 } 481 } 482 } 483 } 484 return -1; 485 #else 350 486 #ifdef __OpenBSD__ 351 487 char path[256]; … … 357 493 return open(device, O_RDWR); 358 494 #endif /* __OpenBSD__ */ 495 #endif /* ENABLE_LIBUSB */ 359 496 } 360 497 … … 370 507 int ifd_scan_usb(void) 371 508 { 509 #ifdef ENABLE_LIBUSB 510 struct usb_bus *bus; 511 struct usb_device *dev; 512 ifd_devid_t id; 513 514 usb_init(); 515 usb_find_busses(); 516 usb_find_devices(); 517 518 id.type = IFD_DEVICE_TYPE_USB; 519 id.num = 2; 520 for (bus = usb_busses; bus; bus = bus->next) { 521 for (dev = bus->devices; dev; dev = dev->next) { 522 const char *driver; 523 char typedev[PATH_MAX]; 524 525 id.val[0] = dev->descriptor.idVendor; 526 id.val[1] = dev->descriptor.idProduct; 527 528 /* FIXME: if we don't find a driver with vendor/product 529 * then check for the interface type (ccid) and use 530 * driver ccid... */ 531 532 if (!(driver = ifd_driver_for_id(&id))) 533 continue; 534 535 snprintf(typedev, sizeof(typedev), 536 "usb:%s", dev->filename); 537 538 ifd_spawn_handler(driver, typedev, -1); 539 } 540 } 541 #else 372 542 int i, controller_fd; 373 543 char controller_devname[10]; … … 433 603 close(controller_fd); 434 604 } 605 #endif /* ENABLE_LIBUSB */ 435 606 return 0; 436 607 }
Note: See TracChangeset
for help on using the changeset viewer.
