Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_USB_USB_DEVICE_H_ | 5 #ifndef CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ | 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | |
| 9 | 10 |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "chrome/browser/usb/usb_interface.h" | |
| 12 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
| 13 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 14 | 16 |
| 15 struct libusb_device; | 17 struct libusb_device; |
| 16 struct libusb_device_handle; | 18 struct libusb_device_handle; |
| 17 struct libusb_iso_packet_descriptor; | 19 struct libusb_iso_packet_descriptor; |
| 18 struct libusb_transfer; | 20 struct libusb_transfer; |
| 19 | 21 |
| 20 typedef libusb_device* PlatformUsbDevice; | 22 typedef libusb_device* PlatformUsbDevice; |
| 21 typedef libusb_device_handle* PlatformUsbDeviceHandle; | 23 typedef libusb_device_handle* PlatformUsbDeviceHandle; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 32 USB_TRANSFER_COMPLETED = 0, | 34 USB_TRANSFER_COMPLETED = 0, |
| 33 USB_TRANSFER_ERROR, | 35 USB_TRANSFER_ERROR, |
| 34 USB_TRANSFER_TIMEOUT, | 36 USB_TRANSFER_TIMEOUT, |
| 35 USB_TRANSFER_CANCELLED, | 37 USB_TRANSFER_CANCELLED, |
| 36 USB_TRANSFER_STALLED, | 38 USB_TRANSFER_STALLED, |
| 37 USB_TRANSFER_DISCONNECT, | 39 USB_TRANSFER_DISCONNECT, |
| 38 USB_TRANSFER_OVERFLOW, | 40 USB_TRANSFER_OVERFLOW, |
| 39 USB_TRANSFER_LENGTH_SHORT, | 41 USB_TRANSFER_LENGTH_SHORT, |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 enum UsbTransferType { | |
| 43 USB_TRANSFER_CONTROL = 0, | |
| 44 USB_TRANSFER_BULK, | |
| 45 USB_TRANSFER_INTERRUPT, | |
| 46 USB_TRANSFER_ISOCHRONOUS, | |
| 47 }; | |
| 48 | |
| 49 typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, | 44 typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, |
| 50 size_t)> UsbTransferCallback; | 45 size_t)> UsbTransferCallback; |
| 51 typedef base::Callback<void(bool)> UsbInterfaceCallback; | 46 typedef base::Callback<void(bool)> UsbInterfaceCallback; |
| 52 | 47 |
| 53 // A UsbDevice wraps the platform's underlying representation of what a USB | 48 // A UsbDevice wraps the platform's underlying representation of what a USB |
| 54 // device actually is, and provides accessors for performing many of the | 49 // device actually is, and provides accessors for performing many of the |
| 55 // standard USB operations. | 50 // standard USB operations. |
| 56 class UsbDevice : public base::RefCounted<UsbDevice> { | 51 class UsbDevice : public base::RefCounted<UsbDevice> { |
| 57 public: | 52 public: |
| 58 enum TransferDirection { INBOUND, OUTBOUND }; | |
| 59 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; | 53 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; |
| 60 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | 54 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; |
| 61 | 55 |
| 62 // Usually you will not want to directly create a UsbDevice, favoring to let | 56 // Usually you will not want to directly create a UsbDevice, favoring to let |
| 63 // the UsbService take care of the logistics of getting a platform device | 57 // the UsbService take care of the logistics of getting a platform device |
| 64 // handle and handling events for it. | 58 // handle and handling events for it. |
| 65 UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); | 59 UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); |
| 66 | 60 |
| 67 PlatformUsbDeviceHandle handle() { return handle_; } | 61 PlatformUsbDeviceHandle handle() { return handle_; } |
| 68 | 62 |
| 69 // Close the USB device and release the underlying platform device. |callback| | 63 // Close the USB device and release the underlying platform device. |callback| |
| 70 // is invoked after the device has been closed. | 64 // is invoked after the device has been closed. |
| 71 virtual void Close(const base::Callback<void()>& callback); | 65 virtual void Close(const base::Callback<void()>& callback); |
| 72 | 66 |
| 67 virtual void ListInterfaces(PlatformUsbConfigDescriptor* config, | |
|
Bei Zhang
2013/04/26 22:38:01
This is actually GetConfigDescriptor.
Kenny Root (Google)
2013/05/08 01:15:08
You mean change the name?
| |
| 68 const UsbInterfaceCallback& callback); | |
| 69 | |
| 73 virtual void ClaimInterface(const int interface_number, | 70 virtual void ClaimInterface(const int interface_number, |
| 74 const UsbInterfaceCallback& callback); | 71 const UsbInterfaceCallback& callback); |
| 75 | 72 |
| 76 virtual void ReleaseInterface(const int interface_number, | 73 virtual void ReleaseInterface(const int interface_number, |
| 77 const UsbInterfaceCallback& callback); | 74 const UsbInterfaceCallback& callback); |
| 78 | 75 |
| 79 virtual void SetInterfaceAlternateSetting( | 76 virtual void SetInterfaceAlternateSetting( |
| 80 const int interface_number, | 77 const int interface_number, |
| 81 const int alternate_setting, | 78 const int alternate_setting, |
| 82 const UsbInterfaceCallback& callback); | 79 const UsbInterfaceCallback& callback); |
| 83 | 80 |
| 84 virtual void ControlTransfer(const TransferDirection direction, | 81 virtual void ControlTransfer(const UsbInterfaceDirection direction, |
| 85 const TransferRequestType request_type, | 82 const TransferRequestType request_type, |
| 86 const TransferRecipient recipient, | 83 const TransferRecipient recipient, |
| 87 const uint8 request, | 84 const uint8 request, |
| 88 const uint16 value, | 85 const uint16 value, |
| 89 const uint16 index, | 86 const uint16 index, |
| 90 net::IOBuffer* buffer, | 87 net::IOBuffer* buffer, |
| 91 const size_t length, | 88 const size_t length, |
| 92 const unsigned int timeout, | 89 const unsigned int timeout, |
| 93 const UsbTransferCallback& callback); | 90 const UsbTransferCallback& callback); |
| 94 | 91 |
| 95 virtual void BulkTransfer(const TransferDirection direction, | 92 virtual void BulkTransfer(const UsbInterfaceDirection direction, |
| 96 const uint8 endpoint, | 93 const uint8 endpoint, |
| 97 net::IOBuffer* buffer, | 94 net::IOBuffer* buffer, |
| 98 const size_t length, | 95 const size_t length, |
| 99 const unsigned int timeout, | 96 const unsigned int timeout, |
| 100 const UsbTransferCallback& callback); | 97 const UsbTransferCallback& callback); |
| 101 | 98 |
| 102 virtual void InterruptTransfer(const TransferDirection direction, | 99 virtual void InterruptTransfer(const UsbInterfaceDirection direction, |
| 103 const uint8 endpoint, | 100 const uint8 endpoint, |
| 104 net::IOBuffer* buffer, | 101 net::IOBuffer* buffer, |
| 105 const size_t length, | 102 const size_t length, |
| 106 const unsigned int timeout, | 103 const unsigned int timeout, |
| 107 const UsbTransferCallback& callback); | 104 const UsbTransferCallback& callback); |
| 108 | 105 |
| 109 virtual void IsochronousTransfer(const TransferDirection direction, | 106 virtual void IsochronousTransfer(const UsbInterfaceDirection direction, |
| 110 const uint8 endpoint, | 107 const uint8 endpoint, |
| 111 net::IOBuffer* buffer, | 108 net::IOBuffer* buffer, |
| 112 const size_t length, | 109 const size_t length, |
| 113 const unsigned int packets, | 110 const unsigned int packets, |
| 114 const unsigned int packet_length, | 111 const unsigned int packet_length, |
| 115 const unsigned int timeout, | 112 const unsigned int timeout, |
| 116 const UsbTransferCallback& callback); | 113 const UsbTransferCallback& callback); |
| 117 | 114 |
| 118 // Normal code should not call this function. It is called by the platform's | 115 // Normal code should not call this function. It is called by the platform's |
| 119 // callback mechanism in such a way that it cannot be made private. Invokes | 116 // callback mechanism in such a way that it cannot be made private. Invokes |
| 120 // the callbacks associated with a given transfer, and removes it from the | 117 // the callbacks associated with a given transfer, and removes it from the |
| 121 // in-flight transfer set. | 118 // in-flight transfer set. |
| 122 void TransferComplete(PlatformUsbTransferHandle transfer); | 119 void TransferComplete(PlatformUsbTransferHandle transfer); |
| 123 | 120 |
| 124 protected: | 121 protected: |
| 125 // This constructor variant is for use in testing only. | 122 // This constructor variant is for use in testing only. |
| 126 UsbDevice(); | 123 UsbDevice(); |
| 127 | 124 |
| 128 friend class base::RefCounted<UsbDevice>; | 125 friend class base::RefCounted<UsbDevice>; |
| 129 virtual ~UsbDevice(); | 126 virtual ~UsbDevice(); |
| 130 | 127 |
| 131 private: | 128 private: |
| 132 struct Transfer { | 129 struct Transfer { |
| 133 Transfer(); | 130 Transfer(); |
| 134 ~Transfer(); | 131 ~Transfer(); |
| 135 | 132 |
| 136 UsbTransferType transfer_type; | 133 UsbEndpointType transfer_type; |
| 137 scoped_refptr<net::IOBuffer> buffer; | 134 scoped_refptr<net::IOBuffer> buffer; |
| 138 size_t length; | 135 size_t length; |
| 139 UsbTransferCallback callback; | 136 UsbTransferCallback callback; |
| 140 }; | 137 }; |
| 141 | 138 |
| 142 // Checks that the device has not yet been closed. | 139 // Checks that the device has not yet been closed. |
| 143 void CheckDevice(); | 140 void CheckDevice(); |
| 144 | 141 |
| 145 // Submits a transfer and starts tracking it. Retains the buffer and copies | 142 // Submits a transfer and starts tracking it. Retains the buffer and copies |
| 146 // the completion callback until the transfer finishes, whereupon it invokes | 143 // the completion callback until the transfer finishes, whereupon it invokes |
| 147 // the callback then releases the buffer. | 144 // the callback then releases the buffer. |
| 148 void SubmitTransfer(PlatformUsbTransferHandle handle, | 145 void SubmitTransfer(PlatformUsbTransferHandle handle, |
| 149 UsbTransferType transfer_type, | 146 UsbEndpointType interface_type, |
|
Bei Zhang
2013/04/26 22:38:01
Isn't this endpoint_type?
Kenny Root (Google)
2013/05/08 01:15:08
Done.
| |
| 150 net::IOBuffer* buffer, | 147 net::IOBuffer* buffer, |
| 151 const size_t length, | 148 const size_t length, |
| 152 const UsbTransferCallback& callback); | 149 const UsbTransferCallback& callback); |
| 153 | 150 |
| 154 // The UsbService isn't referenced here to prevent a dependency cycle between | 151 // The UsbService isn't referenced here to prevent a dependency cycle between |
| 155 // the service and the devices. Since a service owns every device, and is | 152 // the service and the devices. Since a service owns every device, and is |
| 156 // responsible for its destruction, there is no case where a UsbDevice can | 153 // responsible for its destruction, there is no case where a UsbDevice can |
| 157 // have outlived its originating UsbService. | 154 // have outlived its originating UsbService. |
| 158 UsbService* const service_; | 155 UsbService* const service_; |
| 159 PlatformUsbDeviceHandle handle_; | 156 PlatformUsbDeviceHandle handle_; |
| 160 | 157 |
| 161 // transfers_ tracks all in-flight transfers associated with this device, | 158 // transfers_ tracks all in-flight transfers associated with this device, |
| 162 // allowing the device to retain the buffer and callback associated with a | 159 // allowing the device to retain the buffer and callback associated with a |
| 163 // transfer until such time that it completes. It is protected by lock_. | 160 // transfer until such time that it completes. It is protected by lock_. |
| 164 base::Lock lock_; | 161 base::Lock lock_; |
| 165 std::map<PlatformUsbTransferHandle, Transfer> transfers_; | 162 std::map<PlatformUsbTransferHandle, Transfer> transfers_; |
| 166 | 163 |
| 167 DISALLOW_COPY_AND_ASSIGN(UsbDevice); | 164 DISALLOW_COPY_AND_ASSIGN(UsbDevice); |
| 168 }; | 165 }; |
| 169 | 166 |
| 170 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ | 167 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ |
| OLD | NEW |