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
;
52 void ClearHalt(uint8 endpoint
, const ResultCallback
& callback
) override
;
54 void ControlTransfer(UsbEndpointDirection direction
,
55 TransferRequestType request_type
,
56 TransferRecipient recipient
,
60 scoped_refptr
<net::IOBuffer
> buffer
,
63 const TransferCallback
& callback
) override
;
65 void BulkTransfer(UsbEndpointDirection direction
,
67 scoped_refptr
<net::IOBuffer
> buffer
,
70 const TransferCallback
& callback
) override
;
72 void InterruptTransfer(UsbEndpointDirection direction
,
74 scoped_refptr
<net::IOBuffer
> buffer
,
77 const TransferCallback
& callback
) override
;
79 void IsochronousTransfer(UsbEndpointDirection direction
,
81 scoped_refptr
<net::IOBuffer
> buffer
,
84 unsigned int packet_length
,
86 const TransferCallback
& callback
) override
;
89 friend class UsbDeviceImpl
;
91 // This constructor is called by UsbDeviceImpl.
93 scoped_refptr
<UsbContext
> context
,
94 scoped_refptr
<UsbDeviceImpl
> device
,
95 PlatformUsbDeviceHandle handle
,
96 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
);
98 ~UsbDeviceHandleImpl() override
;
100 PlatformUsbDeviceHandle
handle() const { return handle_
; }
103 class InterfaceClaimer
;
106 void SetConfigurationOnBlockingThread(int configuration_value
,
107 const ResultCallback
& callback
);
108 void SetConfigurationComplete(bool success
, const ResultCallback
& callback
);
109 void ClaimInterfaceOnBlockingThread(int interface_number
,
110 const ResultCallback
& callback
);
111 void ClaimInterfaceComplete(int interface_number
,
113 const ResultCallback
& callback
);
114 void SetInterfaceAlternateSettingOnBlockingThread(
115 int interface_number
,
116 int alternate_setting
,
117 const ResultCallback
& callback
);
118 void SetInterfaceAlternateSettingComplete(int interface_number
,
119 int alternate_setting
,
121 const ResultCallback
& callback
);
122 void ResetDeviceOnBlockingThread(const ResultCallback
& callback
);
123 void ClearHaltOnBlockingThread(uint8 endpoint
,
124 const ResultCallback
& callback
);
126 // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
127 // SetInterfaceAlternateSetting.
128 void RefreshEndpointMap();
130 // Look up the claimed interface by endpoint. Return NULL if the interface
131 // of the endpoint is not found.
132 scoped_refptr
<InterfaceClaimer
> GetClaimedInterfaceForEndpoint(
135 void ControlTransferInternal(
136 UsbEndpointDirection direction
,
137 TransferRequestType request_type
,
138 TransferRecipient recipient
,
142 scoped_refptr
<net::IOBuffer
> buffer
,
144 unsigned int timeout
,
145 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
146 const TransferCallback
& callback
);
148 void BulkTransferInternal(
149 UsbEndpointDirection direction
,
151 scoped_refptr
<net::IOBuffer
> buffer
,
153 unsigned int timeout
,
154 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
155 const TransferCallback
& callback
);
157 void InterruptTransferInternal(
158 UsbEndpointDirection direction
,
160 scoped_refptr
<net::IOBuffer
> buffer
,
162 unsigned int timeout
,
163 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
164 const TransferCallback
& callback
);
166 void IsochronousTransferInternal(
167 UsbEndpointDirection direction
,
169 scoped_refptr
<net::IOBuffer
> buffer
,
171 unsigned int packets
,
172 unsigned int packet_length
,
173 unsigned int timeout
,
174 scoped_refptr
<base::TaskRunner
> callback_task_runner
,
175 const TransferCallback
& callback
);
177 // Submits a transfer and starts tracking it. Retains the buffer and copies
178 // the completion callback until the transfer finishes, whereupon it invokes
179 // the callback then releases the buffer.
180 void SubmitTransfer(scoped_ptr
<Transfer
> transfer
);
182 // Removes the transfer from the in-flight transfer set and invokes the
183 // completion callback.
184 void TransferComplete(Transfer
* transfer
, const base::Closure
& callback
);
186 // Informs the object to drop internal references.
187 void InternalClose();
189 scoped_refptr
<UsbDeviceImpl
> device_
;
191 PlatformUsbDeviceHandle handle_
;
193 typedef std::map
<int, scoped_refptr
<InterfaceClaimer
>> ClaimedInterfaceMap
;
194 ClaimedInterfaceMap claimed_interfaces_
;
196 // This set holds weak pointers to pending transfers.
197 std::set
<Transfer
*> transfers_
;
199 // A map from endpoints to interfaces
200 typedef std::map
<int, int> EndpointMap
;
201 EndpointMap endpoint_map_
;
203 // Retain the UsbContext so that the platform context will not be destroyed
204 // before this handle.
205 scoped_refptr
<UsbContext
> context_
;
207 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
208 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner_
;
210 base::ThreadChecker thread_checker_
;
212 DISALLOW_COPY_AND_ASSIGN(UsbDeviceHandleImpl
);
215 } // namespace device
217 #endif // DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_