| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef DEVICE_USB_USB_DEVICE_HANDLE_USBFS_H_ | |
| 6 #define DEVICE_USB_USB_DEVICE_HANDLE_USBFS_H_ | |
| 7 | |
| 8 #include <list> | |
| 9 #include <map> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/files/scoped_file.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "device/usb/usb_device_handle.h" | |
| 15 | |
| 16 struct usbdevfs_urb; | |
| 17 | |
| 18 namespace base { | |
| 19 class SequencedTaskRunner; | |
| 20 } | |
| 21 | |
| 22 namespace device { | |
| 23 | |
| 24 // Implementation of a USB device handle on top of the Linux USBFS ioctl | |
| 25 // interface available on Linux, Chrome OS and Android. | |
| 26 class UsbDeviceHandleUsbfs : public UsbDeviceHandle { | |
| 27 public: | |
| 28 // Constructs a new device handle from an existing |device| and open file | |
| 29 // descriptor to that device. |blocking_task_runner| must have a | |
| 30 // MessageLoopForIO. | |
| 31 UsbDeviceHandleUsbfs( | |
| 32 scoped_refptr<UsbDevice> device, | |
| 33 base::ScopedFD fd, | |
| 34 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); | |
| 35 | |
| 36 // UsbDeviceHandle implementation. | |
| 37 scoped_refptr<UsbDevice> GetDevice() const override; | |
| 38 void Close() override; | |
| 39 void SetConfiguration(int configuration_value, | |
| 40 const ResultCallback& callback) override; | |
| 41 void ClaimInterface(int interface_number, | |
| 42 const ResultCallback& callback) override; | |
| 43 void ReleaseInterface(int interface_number, | |
| 44 const ResultCallback& callback) override; | |
| 45 void SetInterfaceAlternateSetting(int interface_number, | |
| 46 int alternate_setting, | |
| 47 const ResultCallback& callback) override; | |
| 48 void ResetDevice(const ResultCallback& callback) override; | |
| 49 void ClearHalt(uint8_t endpoint, const ResultCallback& callback) override; | |
| 50 void ControlTransfer(UsbEndpointDirection direction, | |
| 51 TransferRequestType request_type, | |
| 52 TransferRecipient recipient, | |
| 53 uint8_t request, | |
| 54 uint16_t value, | |
| 55 uint16_t index, | |
| 56 scoped_refptr<net::IOBuffer> buffer, | |
| 57 size_t length, | |
| 58 unsigned int timeout, | |
| 59 const TransferCallback& callback) override; | |
| 60 void IsochronousTransferIn( | |
| 61 uint8_t endpoint_number, | |
| 62 const std::vector<uint32_t>& packet_lengths, | |
| 63 unsigned int timeout, | |
| 64 const IsochronousTransferCallback& callback) override; | |
| 65 | |
| 66 void IsochronousTransferOut( | |
| 67 uint8_t endpoint_number, | |
| 68 scoped_refptr<net::IOBuffer> buffer, | |
| 69 const std::vector<uint32_t>& packet_lengths, | |
| 70 unsigned int timeout, | |
| 71 const IsochronousTransferCallback& callback) override; | |
| 72 void GenericTransfer(UsbEndpointDirection direction, | |
| 73 uint8_t endpoint_number, | |
| 74 scoped_refptr<net::IOBuffer> buffer, | |
| 75 size_t length, | |
| 76 unsigned int timeout, | |
| 77 const TransferCallback& callback) override; | |
| 78 const UsbInterfaceDescriptor* FindInterfaceByEndpoint( | |
| 79 uint8_t endpoint_address) override; | |
| 80 | |
| 81 private: | |
| 82 class FileThreadHelper; | |
| 83 struct Transfer; | |
| 84 struct InterfaceInfo { | |
| 85 uint8_t alternate_setting; | |
| 86 }; | |
| 87 struct EndpointInfo { | |
| 88 UsbTransferType type; | |
| 89 const UsbInterfaceDescriptor* interface; | |
| 90 }; | |
| 91 | |
| 92 ~UsbDeviceHandleUsbfs() override; | |
| 93 void CloseBlocking(); | |
| 94 void SetConfigurationBlocking(int configuration_value, | |
| 95 const ResultCallback& callback); | |
| 96 void SetConfigurationComplete(int configuration_value, | |
| 97 bool success, | |
| 98 const ResultCallback& callback); | |
| 99 void ReleaseInterfaceBlocking(int interface_number, | |
| 100 const ResultCallback& callback); | |
| 101 void ReleaseInterfaceComplete(int interface_number, | |
| 102 const ResultCallback& callback); | |
| 103 void SetInterfaceBlocking(int interface_number, | |
| 104 int alternate_setting, | |
| 105 const ResultCallback& callback); | |
| 106 void ResetDeviceBlocking(const ResultCallback& callback); | |
| 107 void ClearHaltBlocking(uint8_t endpoint_address, | |
| 108 const ResultCallback& callback); | |
| 109 void IsochronousTransferInternal(uint8_t endpoint_address, | |
| 110 scoped_refptr<net::IOBuffer> buffer, | |
| 111 size_t total_length, | |
| 112 const std::vector<uint32_t>& packet_lengths, | |
| 113 unsigned int timeout, | |
| 114 const IsochronousTransferCallback& callback); | |
| 115 void ReapedUrbs(const std::vector<usbdevfs_urb*>& urbs); | |
| 116 void TransferComplete(scoped_ptr<Transfer> transfer); | |
| 117 void RefreshEndpointInfo(); | |
| 118 void ReportIsochronousError( | |
| 119 const std::vector<uint32_t>& packet_lengths, | |
| 120 const UsbDeviceHandle::IsochronousTransferCallback& callback, | |
| 121 UsbTransferStatus status); | |
| 122 void SetUpTimeoutCallback(Transfer* transfer, unsigned int timeout); | |
| 123 | |
| 124 static void TransferTimedOut(Transfer* transfer); | |
| 125 | |
| 126 scoped_refptr<UsbDevice> device_; | |
| 127 base::ScopedFD fd_; | |
| 128 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 129 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | |
| 130 base::ThreadChecker thread_checker_; | |
| 131 | |
| 132 // Maps claimed interfaces by interface number to their current alternate | |
| 133 // setting. | |
| 134 std::map<uint8_t, InterfaceInfo> interfaces_; | |
| 135 | |
| 136 // Maps endpoints (by endpoint address) to the interface they are a part of. | |
| 137 // Only endpoints of currently claimed and selected interface alternates are | |
| 138 // included in the map. | |
| 139 std::map<uint8_t, EndpointInfo> endpoints_; | |
| 140 | |
| 141 // Helper object owned by the blocking task thread. It will be freed if that | |
| 142 // thread's message loop is destroyed but can also be freed by this class on | |
| 143 // destruction. | |
| 144 FileThreadHelper* helper_; | |
| 145 | |
| 146 std::list<scoped_ptr<Transfer>> transfers_; | |
| 147 }; | |
| 148 | |
| 149 } // namespace device | |
| 150 | |
| 151 #endif // DEVICE_USB_USB_DEVICE_HANDLE_USBFS_H_ | |
| OLD | NEW |