| 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_HANDLE_H_ |
| 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ | 6 #define CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "chrome/browser/usb/usb_interface.h" | 13 #include "chrome/browser/usb/usb_interface.h" |
| 14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
| 15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 16 | 16 |
| 17 struct libusb_device; | 17 typedef struct libusb_device_handle* PlatformUsbDeviceHandle; |
| 18 struct libusb_device_handle; | 18 typedef struct libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; |
| 19 struct libusb_iso_packet_descriptor; | 19 typedef struct libusb_transfer* PlatformUsbTransferHandle; |
| 20 struct libusb_transfer; | |
| 21 | |
| 22 typedef libusb_device* PlatformUsbDevice; | |
| 23 typedef libusb_device_handle* PlatformUsbDeviceHandle; | |
| 24 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor; | |
| 25 typedef libusb_transfer* PlatformUsbTransferHandle; | |
| 26 | 20 |
| 27 class UsbService; | 21 class UsbService; |
| 28 | 22 |
| 29 namespace net { | 23 namespace net { |
| 30 class IOBuffer; | 24 class IOBuffer; |
| 31 } // namespace net | 25 } // namespace net |
| 32 | 26 |
| 33 enum UsbTransferStatus { | 27 enum UsbTransferStatus { |
| 34 USB_TRANSFER_COMPLETED = 0, | 28 USB_TRANSFER_COMPLETED = 0, |
| 35 USB_TRANSFER_ERROR, | 29 USB_TRANSFER_ERROR, |
| 36 USB_TRANSFER_TIMEOUT, | 30 USB_TRANSFER_TIMEOUT, |
| 37 USB_TRANSFER_CANCELLED, | 31 USB_TRANSFER_CANCELLED, |
| 38 USB_TRANSFER_STALLED, | 32 USB_TRANSFER_STALLED, |
| 39 USB_TRANSFER_DISCONNECT, | 33 USB_TRANSFER_DISCONNECT, |
| 40 USB_TRANSFER_OVERFLOW, | 34 USB_TRANSFER_OVERFLOW, |
| 41 USB_TRANSFER_LENGTH_SHORT, | 35 USB_TRANSFER_LENGTH_SHORT, |
| 42 }; | 36 }; |
| 43 | 37 |
| 44 typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, | 38 typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>, |
| 45 size_t)> UsbTransferCallback; | 39 size_t)> UsbTransferCallback; |
| 46 typedef base::Callback<void(bool)> UsbInterfaceCallback; | 40 typedef base::Callback<void(bool)> UsbInterfaceCallback; |
| 47 | 41 |
| 48 // A UsbDevice wraps the platform's underlying representation of what a USB | 42 // A UsbDevice wraps the platform's underlying representation of what a USB |
| 49 // device actually is, and provides accessors for performing many of the | 43 // device actually is, and provides accessors for performing many of the |
| 50 // standard USB operations. | 44 // standard USB operations. |
| 51 class UsbDevice : public base::RefCounted<UsbDevice> { | 45 class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> { |
| 52 public: | 46 public: |
| 53 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; | 47 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; |
| 54 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | 48 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; |
| 55 | 49 |
| 56 // Usually you will not want to directly create a UsbDevice, favoring to let | 50 // Usually you will not want to directly create a UsbDevice, favoring to let |
| 57 // the UsbService take care of the logistics of getting a platform device | 51 // the UsbService take care of the logistics of getting a platform device |
| 58 // handle and handling events for it. | 52 // handle and handling events for it. |
| 59 UsbDevice(UsbService* service, PlatformUsbDeviceHandle handle); | 53 UsbDeviceHandle(UsbService* service, int device, |
| 54 PlatformUsbDeviceHandle handle); |
| 60 | 55 |
| 61 PlatformUsbDeviceHandle handle() { return handle_; } | 56 PlatformUsbDeviceHandle handle() { return handle_; } |
| 57 int device() { return device_; } |
| 62 | 58 |
| 63 // Close the USB device and release the underlying platform device. |callback| | 59 // Close the USB device and release the underlying platform device. |callback| |
| 64 // is invoked after the device has been closed. | 60 // is invoked after the device has been closed. |
| 65 virtual void Close(const base::Callback<void()>& callback); | 61 virtual void Close(const base::Callback<void()>& callback); |
| 66 | 62 |
| 67 virtual void ListInterfaces(UsbConfigDescriptor* config, | 63 virtual void ListInterfaces(UsbConfigDescriptor* config, |
| 68 const UsbInterfaceCallback& callback); | 64 const UsbInterfaceCallback& callback); |
| 69 | 65 |
| 70 virtual void ClaimInterface(const int interface_number, | 66 virtual void ClaimInterface(const int interface_number, |
| 71 const UsbInterfaceCallback& callback); | 67 const UsbInterfaceCallback& callback); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 virtual void ResetDevice(const base::Callback<void(bool)>& callback); | 111 virtual void ResetDevice(const base::Callback<void(bool)>& callback); |
| 116 | 112 |
| 117 // Normal code should not call this function. It is called by the platform's | 113 // Normal code should not call this function. It is called by the platform's |
| 118 // callback mechanism in such a way that it cannot be made private. Invokes | 114 // callback mechanism in such a way that it cannot be made private. Invokes |
| 119 // the callbacks associated with a given transfer, and removes it from the | 115 // the callbacks associated with a given transfer, and removes it from the |
| 120 // in-flight transfer set. | 116 // in-flight transfer set. |
| 121 void TransferComplete(PlatformUsbTransferHandle transfer); | 117 void TransferComplete(PlatformUsbTransferHandle transfer); |
| 122 | 118 |
| 123 protected: | 119 protected: |
| 124 // This constructor variant is for use in testing only. | 120 // This constructor variant is for use in testing only. |
| 125 UsbDevice(); | 121 UsbDeviceHandle(); |
| 126 | 122 |
| 127 friend class base::RefCounted<UsbDevice>; | 123 friend class base::RefCountedThreadSafe<UsbDeviceHandle>; |
| 128 virtual ~UsbDevice(); | 124 virtual ~UsbDeviceHandle(); |
| 129 | 125 |
| 130 private: | 126 private: |
| 131 struct Transfer { | 127 struct Transfer { |
| 132 Transfer(); | 128 Transfer(); |
| 133 ~Transfer(); | 129 ~Transfer(); |
| 134 | 130 |
| 135 UsbTransferType transfer_type; | 131 UsbTransferType transfer_type; |
| 136 scoped_refptr<net::IOBuffer> buffer; | 132 scoped_refptr<net::IOBuffer> buffer; |
| 137 size_t length; | 133 size_t length; |
| 138 UsbTransferCallback callback; | 134 UsbTransferCallback callback; |
| 139 }; | 135 }; |
| 140 | 136 |
| 141 // Checks that the device has not yet been closed. | 137 friend class UsbDevice; |
| 142 void CheckDevice(); | 138 |
| 139 // This only called from UsbDevice, thus always from FILE thread. |
| 140 void InternalClose(); |
| 143 | 141 |
| 144 // 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 |
| 145 // the completion callback until the transfer finishes, whereupon it invokes | 143 // the completion callback until the transfer finishes, whereupon it invokes |
| 146 // the callback then releases the buffer. | 144 // the callback then releases the buffer. |
| 147 void SubmitTransfer(PlatformUsbTransferHandle handle, | 145 void SubmitTransfer(PlatformUsbTransferHandle handle, |
| 148 UsbTransferType transfer_type, | 146 UsbTransferType transfer_type, |
| 149 net::IOBuffer* buffer, | 147 net::IOBuffer* buffer, |
| 150 const size_t length, | 148 const size_t length, |
| 151 const UsbTransferCallback& callback); | 149 const UsbTransferCallback& callback); |
| 152 | 150 |
| 153 // 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 |
| 154 // 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 |
| 155 // 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 |
| 156 // have outlived its originating UsbService. | 154 // have outlived its originating UsbService. |
| 157 UsbService* const service_; | 155 UsbService* const service_; |
| 156 int const device_; |
| 157 |
| 158 // Lock the handle for the race condition of InternalClose and |
| 159 // TransferComplete. |
| 160 base::Lock handle_lock_; |
| 158 PlatformUsbDeviceHandle handle_; | 161 PlatformUsbDeviceHandle handle_; |
| 159 | 162 |
| 160 // transfers_ tracks all in-flight transfers associated with this device, | 163 // transfers_ tracks all in-flight transfers associated with this device, |
| 161 // allowing the device to retain the buffer and callback associated with a | 164 // allowing the device to retain the buffer and callback associated with a |
| 162 // transfer until such time that it completes. It is protected by lock_. | 165 // transfer until such time that it completes. It is protected by |
| 163 base::Lock lock_; | 166 // transfer_lock_. |
| 167 // |
| 168 // If both locks are used, the locking order must be first handle_lock_, then |
| 169 // transfer_lock_. |
| 170 base::Lock transfer_lock_; |
| 164 std::map<PlatformUsbTransferHandle, Transfer> transfers_; | 171 std::map<PlatformUsbTransferHandle, Transfer> transfers_; |
| 165 | 172 |
| 166 DISALLOW_COPY_AND_ASSIGN(UsbDevice); | 173 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle); |
| 167 }; | 174 }; |
| 168 | 175 |
| 169 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ | 176 #endif // CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_ |
| OLD | NEW |