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
;
31 struct UsbConfigDescriptor
;
34 typedef libusb_device_handle
* PlatformUsbDeviceHandle
;
35 typedef libusb_iso_packet_descriptor
* PlatformUsbIsoPacketDescriptor
;
36 typedef libusb_transfer
* PlatformUsbTransferHandle
;
38 // UsbDeviceHandle class provides basic I/O related functionalities.
39 class UsbDeviceHandleImpl
: public UsbDeviceHandle
{
41 scoped_refptr
<UsbDevice
> GetDevice() const override
;
42 void Close() override
;
43 void SetConfiguration(int configuration_value
,
44 const ResultCallback
& callback
) override
;
45 void ClaimInterface(int interface_number
,
46 const ResultCallback
& callback
) override
;
47 bool ReleaseInterface(int interface_number
) override
;
48 void SetInterfaceAlternateSetting(int interface_number
,
49 int alternate_setting
,
50 const ResultCallback
& callback
) override
;
51 void ResetDevice(const ResultCallback
& callback
) override
;
53 void ControlTransfer(UsbEndpointDirection direction
,
54 TransferRequestType request_type
,
55 TransferRecipient recipient
,
59 scoped_refptr
<net::IOBuffer
> buffer
,
62 const TransferCallback
& callback
) override
;
64 void BulkTransfer(UsbEndpointDirection direction
,
66 scoped_refptr
<net::IOBuffer
> buffer
,
69 const TransferCallback
& callback
) override
;
71 void InterruptTransfer(UsbEndpointDirection direction
,
73 scoped_refptr
<net::IOBuffer
> buffer
,
76 const TransferCallback
& callback
) override
;
78 void IsochronousTransfer(UsbEndpointDirection direction
,
80 scoped_refptr
<net::IOBuffer
> buffer
,
83 unsigned int packet_length
,
85 const TransferCallback
& callback
) override
;
88 friend class UsbDeviceImpl
;
90 // This constructor is called by UsbDeviceImpl.
92 scoped_refptr
<UsbContext
> context
,
93 scoped_refptr
<UsbDeviceImpl
> device
,
94 PlatformUsbDeviceHandle handle
,
95 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
97 ~UsbDeviceHandleImpl() override
;
99 PlatformUsbDeviceHandle
handle() const { return handle_
; }
102 class InterfaceClaimer
;
105 void SetConfigurationOnBlockingThread(PlatformUsbDeviceHandle handle
,
106 int configuration_value
,
107 const ResultCallback
& callback
);
108 void SetConfigurationComplete(bool success
, const ResultCallback
& callback
);
109 void ClaimInterfaceOnBlockingThread(PlatformUsbDeviceHandle handle
,
110 int interface_number
,
111 const ResultCallback
& callback
);
112 void ClaimInterfaceComplete(int interface_number
,
114 const ResultCallback
& callback
);
115 void SetInterfaceAlternateSettingOnBlockingThread(
116 PlatformUsbDeviceHandle handle
,
117 int interface_number
,
118 int alternate_setting
,
119 const ResultCallback
& callback
);
120 void SetInterfaceAlternateSettingComplete(int interface_number
,
121 int alternate_setting
,
123 const ResultCallback
& callback
);
124 void ResetDeviceOnBlockingThread(PlatformUsbDeviceHandle handle
,
125 const ResultCallback
& callback
);
126 void ResetDeviceComplete(bool success
, const ResultCallback
& callback
);
128 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
129 // SetInterfaceAlternateSetting.
130 void RefreshEndpointMap();
132 // Look up the claimed interface by endpoint. Return NULL if the interface
133 // of the endpoint is not found.
134 scoped_refptr
<InterfaceClaimer
> GetClaimedInterfaceForEndpoint(
135 unsigned char endpoint
);
137 void ControlTransferInternal(
138 UsbEndpointDirection direction
,
139 TransferRequestType request_type
,
140 TransferRecipient recipient
,
144 scoped_refptr
<net::IOBuffer
> buffer
,
146 unsigned int timeout
,
147 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
148 const TransferCallback
& callback
);
150 void BulkTransferInternal(
151 UsbEndpointDirection direction
,
153 scoped_refptr
<net::IOBuffer
> buffer
,
155 unsigned int timeout
,
156 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
157 const TransferCallback
& callback
);
159 void InterruptTransferInternal(
160 UsbEndpointDirection direction
,
162 scoped_refptr
<net::IOBuffer
> buffer
,
164 unsigned int timeout
,
165 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
166 const TransferCallback
& callback
);
168 void IsochronousTransferInternal(
169 UsbEndpointDirection direction
,
171 scoped_refptr
<net::IOBuffer
> buffer
,
173 unsigned int packets
,
174 unsigned int packet_length
,
175 unsigned int timeout
,
176 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
177 const TransferCallback
& callback
);
179 // Submits a transfer and starts tracking it. Retains the buffer and copies
180 // the completion callback until the transfer finishes, whereupon it invokes
181 // the callback then releases the buffer.
182 void SubmitTransfer(scoped_ptr
<Transfer
> transfer
);
184 // Removes the transfer from the in-flight transfer set and invokes the
185 // completion callback.
186 void TransferComplete(Transfer
* transfer
, const base::Closure
& callback
);
188 // Informs the object to drop internal references.
189 void InternalClose();
191 scoped_refptr
<UsbDeviceImpl
> device_
;
193 PlatformUsbDeviceHandle handle_
;
195 typedef std::map
<int, scoped_refptr
<InterfaceClaimer
>> ClaimedInterfaceMap
;
196 ClaimedInterfaceMap claimed_interfaces_
;
198 // This set holds weak pointers to pending transfers.
199 std::set
<Transfer
*> transfers_
;
201 // A map from endpoints to interfaces
202 typedef std::map
<int, int> EndpointMap
;
203 EndpointMap endpoint_map_
;
205 // Retain the UsbContext so that the platform context will not be destroyed
206 // before this handle.
207 scoped_refptr
<UsbContext
> context_
;
209 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
210 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
212 base::ThreadChecker thread_checker_
;
214 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl
);
217 } // namespace device
219 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_