1 // Copyright 2014 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 DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
6 #define DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/thread_checker.h"
15 #include "device/usb/usb_device_handle.h"
16 #include "third_party/libusb/src/libusb/libusb.h"
19 class SequencedTaskRunner
;
20 class SingleThreadTaskRunner
;
30 struct EndpointMapValue
{
32 UsbTransferType transfer_type
;
36 struct UsbConfigDescriptor
;
39 typedef libusb_device_handle
* PlatformUsbDeviceHandle
;
40 typedef libusb_iso_packet_descriptor
* PlatformUsbIsoPacketDescriptor
;
41 typedef libusb_transfer
* PlatformUsbTransferHandle
;
43 // UsbDeviceHandle class provides basic I/O related functionalities.
44 class UsbDeviceHandleImpl
: public UsbDeviceHandle
{
46 scoped_refptr
<UsbDevice
> GetDevice() const override
;
47 void Close() override
;
48 void SetConfiguration(int configuration_value
,
49 const ResultCallback
& callback
) override
;
50 void ClaimInterface(int interface_number
,
51 const ResultCallback
& callback
) override
;
52 bool ReleaseInterface(int interface_number
) override
;
53 void SetInterfaceAlternateSetting(int interface_number
,
54 int alternate_setting
,
55 const ResultCallback
& callback
) override
;
56 void ResetDevice(const ResultCallback
& callback
) override
;
57 void ClearHalt(uint8 endpoint
, const ResultCallback
& callback
) override
;
59 void ControlTransfer(UsbEndpointDirection direction
,
60 TransferRequestType request_type
,
61 TransferRecipient recipient
,
65 scoped_refptr
<net::IOBuffer
> buffer
,
68 const TransferCallback
& callback
) override
;
70 void IsochronousTransfer(UsbEndpointDirection direction
,
72 scoped_refptr
<net::IOBuffer
> buffer
,
75 unsigned int packet_length
,
77 const TransferCallback
& callback
) override
;
79 void GenericTransfer(UsbEndpointDirection direction
,
81 scoped_refptr
<net::IOBuffer
> buffer
,
84 const TransferCallback
& callback
) override
;
87 friend class UsbDeviceImpl
;
89 // This constructor is called by UsbDeviceImpl.
91 scoped_refptr
<UsbContext
> context
,
92 scoped_refptr
<UsbDeviceImpl
> device
,
93 PlatformUsbDeviceHandle handle
,
94 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
96 ~UsbDeviceHandleImpl() override
;
98 PlatformUsbDeviceHandle
handle() const { return handle_
; }
101 class InterfaceClaimer
;
104 void SetConfigurationOnBlockingThread(int configuration_value
,
105 const ResultCallback
& callback
);
106 void SetConfigurationComplete(bool success
, const ResultCallback
& callback
);
107 void ClaimInterfaceOnBlockingThread(int interface_number
,
108 const ResultCallback
& callback
);
109 void ClaimInterfaceComplete(int interface_number
,
111 const ResultCallback
& callback
);
112 void SetInterfaceAlternateSettingOnBlockingThread(
113 int interface_number
,
114 int alternate_setting
,
115 const ResultCallback
& callback
);
116 void SetInterfaceAlternateSettingComplete(int interface_number
,
117 int alternate_setting
,
119 const ResultCallback
& callback
);
120 void ResetDeviceOnBlockingThread(const ResultCallback
& callback
);
121 void ClearHaltOnBlockingThread(uint8 endpoint
,
122 const ResultCallback
& callback
);
124 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
125 // SetInterfaceAlternateSetting.
126 void RefreshEndpointMap();
128 // Look up the claimed interface by endpoint. Return NULL if the interface
129 // of the endpoint is not found.
130 scoped_refptr
<InterfaceClaimer
> GetClaimedInterfaceForEndpoint(
133 void ControlTransferInternal(
134 UsbEndpointDirection direction
,
135 TransferRequestType request_type
,
136 TransferRecipient recipient
,
140 scoped_refptr
<net::IOBuffer
> buffer
,
142 unsigned int timeout
,
143 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
144 const TransferCallback
& callback
);
146 void BulkTransferInternal(
147 UsbEndpointDirection direction
,
149 scoped_refptr
<net::IOBuffer
> buffer
,
151 unsigned int timeout
,
152 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
153 const TransferCallback
& callback
);
155 void InterruptTransferInternal(
156 UsbEndpointDirection direction
,
158 scoped_refptr
<net::IOBuffer
> buffer
,
160 unsigned int timeout
,
161 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
162 const TransferCallback
& callback
);
164 void IsochronousTransferInternal(
165 UsbEndpointDirection direction
,
167 scoped_refptr
<net::IOBuffer
> buffer
,
169 unsigned int packets
,
170 unsigned int packet_length
,
171 unsigned int timeout
,
172 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
173 const TransferCallback
& callback
);
175 void GenericTransferInternal(
176 UsbEndpointDirection direction
,
178 scoped_refptr
<net::IOBuffer
> buffer
,
180 unsigned int timeout
,
181 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
182 const TransferCallback
& callback
);
184 // Submits a transfer and starts tracking it. Retains the buffer and copies
185 // the completion callback until the transfer finishes, whereupon it invokes
186 // the callback then releases the buffer.
187 void SubmitTransfer(scoped_ptr
<Transfer
> transfer
);
189 // Removes the transfer from the in-flight transfer set and invokes the
190 // completion callback.
191 void TransferComplete(Transfer
* transfer
, const base::Closure
& callback
);
193 // Informs the object to drop internal references.
194 void InternalClose();
196 scoped_refptr
<UsbDeviceImpl
> device_
;
198 PlatformUsbDeviceHandle handle_
;
200 typedef std::map
<int, scoped_refptr
<InterfaceClaimer
>> ClaimedInterfaceMap
;
201 ClaimedInterfaceMap claimed_interfaces_
;
203 // This set holds weak pointers to pending transfers.
204 std::set
<Transfer
*> transfers_
;
206 // A map from endpoints to EndpointMapValue
207 typedef std::map
<int, EndpointMapValue
> EndpointMap
;
208 EndpointMap endpoint_map_
;
210 // Retain the UsbContext so that the platform context will not be destroyed
211 // before this handle.
212 scoped_refptr
<UsbContext
> context_
;
214 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
215 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
217 base::ThreadChecker thread_checker_
;
219 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl
);
222 } // namespace device
224 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_