1 // Copyright (c) 2013 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.
5 #ifndef CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_
6 #define CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/strings/string16.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h"
15 #include "chrome/browser/usb/usb_interface.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/io_buffer.h"
19 struct libusb_device_handle;
20 struct libusb_iso_packet_descriptor;
21 struct libusb_transfer;
23 typedef libusb_device_handle* PlatformUsbDeviceHandle;
24 typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
25 typedef libusb_transfer* PlatformUsbTransferHandle;
28 class UsbConfigDescriptor;
30 class UsbInterfaceDescriptor;
33 class MessageLoopProxy;
40 enum UsbTransferStatus {
41 USB_TRANSFER_COMPLETED = 0,
44 USB_TRANSFER_CANCELLED,
46 USB_TRANSFER_DISCONNECT,
47 USB_TRANSFER_OVERFLOW,
48 USB_TRANSFER_LENGTH_SHORT,
51 typedef base::Callback<void(UsbTransferStatus, scoped_refptr<net::IOBuffer>,
52 size_t)> UsbTransferCallback;
54 // UsbDeviceHandle class provides basic I/O related functionalities.
55 class UsbDeviceHandle : public base::RefCountedThreadSafe<UsbDeviceHandle> {
57 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED };
58 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER };
60 scoped_refptr<UsbDevice> device() const;
61 PlatformUsbDeviceHandle handle() const { return handle_; }
63 // Notifies UsbDevice to drop the reference of this object; cancels all the
65 // It is possible that the object has no other reference after this call. So
66 // if it is called using a raw pointer, it could be invalidated.
67 // The platform device handle will be closed when UsbDeviceHandle destructs.
70 // Device manipulation operations. These methods are blocking and must be
71 // called on FILE thread.
72 virtual bool ClaimInterface(const int interface_number);
73 virtual bool ReleaseInterface(const int interface_number);
74 virtual bool SetInterfaceAlternateSetting(
75 const int interface_number,
76 const int alternate_setting);
77 virtual bool ResetDevice();
78 virtual bool GetSerial(base::string16* serial);
80 // Async IO. Can be called on any thread.
81 virtual void ControlTransfer(const UsbEndpointDirection direction,
82 const TransferRequestType request_type,
83 const TransferRecipient recipient,
87 net::IOBuffer* buffer,
89 const unsigned int timeout,
90 const UsbTransferCallback& callback);
92 virtual void BulkTransfer(const UsbEndpointDirection direction,
94 net::IOBuffer* buffer,
96 const unsigned int timeout,
97 const UsbTransferCallback& callback);
99 virtual void InterruptTransfer(const UsbEndpointDirection direction,
100 const uint8 endpoint,
101 net::IOBuffer* buffer,
103 const unsigned int timeout,
104 const UsbTransferCallback& callback);
106 virtual void IsochronousTransfer(const UsbEndpointDirection direction,
107 const uint8 endpoint,
108 net::IOBuffer* buffer,
110 const unsigned int packets,
111 const unsigned int packet_length,
112 const unsigned int timeout,
113 const UsbTransferCallback& callback);
116 friend class base::RefCountedThreadSafe<UsbDeviceHandle>;
117 friend class UsbDevice;
119 // This constructor is called by UsbDevice.
120 UsbDeviceHandle(scoped_refptr<UsbContext> context,
121 UsbDevice* device, PlatformUsbDeviceHandle handle,
122 scoped_refptr<UsbConfigDescriptor> interfaces);
124 // This constructor variant is for use in testing only.
126 virtual ~UsbDeviceHandle();
131 friend void HandleTransferCompletion(PlatformUsbTransferHandle handle);
133 class InterfaceClaimer;
136 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
137 // SetInterfaceAlternateSetting.
138 void RefreshEndpointMap();
140 // Look up the claimed interface by endpoint. Return NULL if the interface
141 // of the endpoint is not found.
142 scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
143 unsigned char endpoint);
145 // Submits a transfer and starts tracking it. Retains the buffer and copies
146 // the completion callback until the transfer finishes, whereupon it invokes
147 // the callback then releases the buffer.
148 void SubmitTransfer(PlatformUsbTransferHandle handle,
149 UsbTransferType transfer_type,
150 net::IOBuffer* buffer,
152 scoped_refptr<base::MessageLoopProxy> message_loop_proxy,
153 const UsbTransferCallback& callback);
155 // Invokes the callbacks associated with a given transfer, and removes it from
156 // the in-flight transfer set.
157 void TransferComplete(PlatformUsbTransferHandle transfer);
159 // Informs the object to drop internal references.
160 void InternalClose();
162 PlatformUsbDeviceHandle handle_;
164 scoped_refptr<UsbConfigDescriptor> interfaces_;
166 typedef std::map<int, scoped_refptr<InterfaceClaimer> > ClaimedInterfaceMap;
167 ClaimedInterfaceMap claimed_interfaces_;
169 typedef std::map<PlatformUsbTransferHandle, Transfer> TransferMap;
170 TransferMap transfers_;
172 // A map from endpoints to interfaces
173 typedef std::map<int, int> EndpointMap;
174 EndpointMap endpoint_map_;
176 // Retain the UsbContext so that the platform context will not be destroyed
177 // before this handle.
178 scoped_refptr<UsbContext> context_;
180 base::ThreadChecker thread_checker_;
182 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandle);
185 #endif // CHROME_BROWSER_USB_USB_DEVICE_HANDLE_H_