Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 1 | #include "libusb_wrap.h" |
| 2 | |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 3 | #include <iostream> |
| 4 | |
| 5 | #include "aos/common/logging/logging.h" |
| 6 | |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 7 | LibUSB::LibUSB() { |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 8 | if (libusb_init(&ctx_) < 0) { |
| 9 | LOG(FATAL, "libusb_init(%p) failed\n", &ctx_); |
| 10 | } |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 11 | libusb_set_debug(ctx_, 3); |
| 12 | } |
| 13 | |
| 14 | LibUSBDeviceHandle *LibUSB::FindDeviceWithVIDPID(int vid, int pid) { |
| 15 | int r; |
| 16 | libusb_device **devs; |
| 17 | libusb_device_handle *dev_handle; |
| 18 | |
| 19 | ssize_t cnt; |
| 20 | cnt = libusb_get_device_list(ctx_, &devs); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 21 | if (cnt < 0) { |
| 22 | LOG(ERROR, "Get Device Error\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 23 | return NULL; |
| 24 | } |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 25 | LOG(INFO, "%d Devices in list.\n", cnt); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 26 | bool found = false; |
| 27 | for (int i = 0; i < cnt; ++i) { |
| 28 | struct libusb_device_descriptor desc; |
| 29 | r = libusb_get_device_descriptor(devs[i], &desc); |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 30 | if (r < 0) { |
| 31 | LOG(FATAL, "lib_usb_get_device_descriptor(%p, %p) failed\n", |
| 32 | devs[i], &desc); |
| 33 | } |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 34 | if (desc.idVendor == vid && desc.idProduct == pid) { |
Brian Silverman | 798c778 | 2013-03-28 16:48:02 -0700 | [diff] [blame] | 35 | LOG(INFO, "Device %d:%d matches\n", |
| 36 | (int)libusb_get_bus_number(devs[i]), |
| 37 | (int)libusb_get_device_address(devs[i])); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 38 | r = libusb_open(devs[i], &dev_handle); |
| 39 | if (libusb_kernel_driver_active(dev_handle, 0) == 1) { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 40 | LOG(INFO, "Device already in use, trying next one.\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 41 | continue; |
| 42 | } |
| 43 | if (r < 0) { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 44 | LOG(WARNING, "Failed to open device.\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 45 | } else { |
| 46 | found = true; |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | libusb_free_device_list(devs, 1); |
| 52 | if (!found) { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 53 | LOG(ERROR, "Couldn't open device.\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | if (libusb_kernel_driver_active(dev_handle, 0) == 1) { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 58 | LOG(INFO, "Kernel Driver Active\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 59 | if (libusb_detach_kernel_driver(dev_handle, 0) == 0) { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 60 | LOG(INFO, "Kernel Driver Detached!\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 61 | } else { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 62 | LOG(ERROR, "Couldn't detach kernel driver.\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | r = libusb_claim_interface(dev_handle, 0); |
| 68 | if (r < 0) { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 69 | LOG(ERROR, "Cannot Claim Interface\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 70 | return 0; |
| 71 | } |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 72 | LOG(INFO, "Claimed Interface\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 73 | return new LibUSBDeviceHandle(dev_handle); |
| 74 | } |
| 75 | |
| 76 | LibUSB::~LibUSB() { |
| 77 | libusb_exit(ctx_); |
| 78 | } |
| 79 | |
| 80 | LibUSBDeviceHandle::LibUSBDeviceHandle( |
| 81 | libusb_device_handle *dev_handle) : dev_handle_(dev_handle) { } |
| 82 | |
| 83 | LibUSBDeviceHandle::~LibUSBDeviceHandle() { |
| 84 | int r; |
| 85 | r = libusb_release_interface(dev_handle_, 0); |
| 86 | if (r != 0) { |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 87 | LOG(FATAL, "Cannot Release Interface\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 88 | } |
Brian Silverman | a4f9ef2 | 2013-03-30 14:31:16 -0700 | [diff] [blame^] | 89 | LOG(INFO, "Released Interface\n"); |
Austin Schuh | 9b360e9 | 2013-03-27 04:47:04 +0000 | [diff] [blame] | 90 | |
| 91 | libusb_close(dev_handle_); |
| 92 | } |
| 93 | |
| 94 | int LibUSBDeviceHandle::interrupt_transfer( |
| 95 | unsigned char endpoint, unsigned char *data, int length, |
| 96 | int *transferred, unsigned int timeout) { |
| 97 | return libusb_interrupt_transfer(dev_handle_, endpoint, data, length, |
| 98 | transferred, timeout); |
| 99 | } |
| 100 | |
| 101 | int LibUSBDeviceHandle::bulk_transfer( |
| 102 | unsigned char endpoint, unsigned char *data, |
| 103 | int length, int *transferred, unsigned int timeout) { |
| 104 | return libusb_bulk_transfer(dev_handle_, endpoint, data, length, |
| 105 | transferred, timeout); |
| 106 | } |