OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "chrome/browser/usb/usb_device_handle.h" | 5 #include "chrome/browser/usb/usb_device_handle.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 UsbService* service, | 106 UsbService* service, |
107 PlatformUsbDeviceHandle handle) | 107 PlatformUsbDeviceHandle handle) |
108 : service_(service), handle_(handle) { | 108 : service_(service), handle_(handle) { |
109 DCHECK(handle) << "Cannot create device with NULL handle."; | 109 DCHECK(handle) << "Cannot create device with NULL handle."; |
110 } | 110 } |
111 | 111 |
112 UsbDeviceHandle::UsbDeviceHandle() : service_(NULL), handle_(NULL) {} | 112 UsbDeviceHandle::UsbDeviceHandle() : service_(NULL), handle_(NULL) {} |
113 | 113 |
114 UsbDeviceHandle::~UsbDeviceHandle() {} | 114 UsbDeviceHandle::~UsbDeviceHandle() {} |
115 | 115 |
116 void UsbDeviceHandle::Close(const base::Callback<void()>& callback) { | 116 void UsbDeviceHandle::Close() { |
117 CheckDevice(); | 117 CheckDevice(); |
118 service_->CloseDevice(this); | 118 service_->CloseDevice(this); |
119 handle_ = NULL; | 119 handle_ = NULL; |
120 callback.Run(); | |
121 } | 120 } |
122 | 121 |
123 void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { | 122 void UsbDeviceHandle::TransferComplete(PlatformUsbTransferHandle handle) { |
124 base::AutoLock lock(lock_); | 123 base::AutoLock lock(lock_); |
125 | 124 |
126 // TODO(gdk): Handle device disconnect. | 125 // TODO(gdk): Handle device disconnect. |
127 DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed"; | 126 DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed"; |
128 Transfer* const transfer = &transfers_[handle]; | 127 Transfer* const transfer = &transfers_[handle]; |
129 | 128 |
130 DCHECK(handle->actual_length >= 0) << "Negative actual length received"; | 129 DCHECK(handle->actual_length >= 0) << "Negative actual length received"; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 NOTREACHED() << "Invalid usb transfer type"; | 192 NOTREACHED() << "Invalid usb transfer type"; |
194 } | 193 } |
195 | 194 |
196 transfer->callback.Run(ConvertTransferStatus(handle->status), buffer, | 195 transfer->callback.Run(ConvertTransferStatus(handle->status), buffer, |
197 actual_length); | 196 actual_length); |
198 | 197 |
199 transfers_.erase(handle); | 198 transfers_.erase(handle); |
200 libusb_free_transfer(handle); | 199 libusb_free_transfer(handle); |
201 } | 200 } |
202 | 201 |
203 void UsbDeviceHandle::ListInterfaces(UsbConfigDescriptor* config, | 202 bool UsbDeviceHandle::ListInterfaces(UsbConfigDescriptor* config) { |
204 const UsbInterfaceCallback& callback) { | |
205 CheckDevice(); | 203 CheckDevice(); |
206 | 204 |
207 PlatformUsbDevice device = libusb_get_device(handle_); | 205 PlatformUsbDevice device = libusb_get_device(handle_); |
208 | 206 |
209 PlatformUsbConfigDescriptor platform_config; | 207 PlatformUsbConfigDescriptor platform_config; |
210 const int list_result = libusb_get_active_config_descriptor(device, | 208 const int list_result = libusb_get_active_config_descriptor(device, |
211 &platform_config); | 209 &platform_config); |
212 if (list_result == 0) { | 210 if (list_result == 0) { |
213 config->Reset(platform_config); | 211 config->Reset(platform_config); |
214 } | 212 } |
215 callback.Run(list_result == 0); | 213 return list_result == 0; |
216 } | 214 } |
217 | 215 |
218 void UsbDeviceHandle::ClaimInterface(const int interface_number, | 216 bool UsbDeviceHandle::ClaimInterface(const int interface_number) { |
219 const UsbInterfaceCallback& callback) { | |
220 CheckDevice(); | 217 CheckDevice(); |
221 | 218 |
222 const int claim_result = libusb_claim_interface(handle_, interface_number); | 219 const int claim_result = libusb_claim_interface(handle_, interface_number); |
223 callback.Run(claim_result == 0); | 220 return claim_result == 0; |
224 } | 221 } |
225 | 222 |
226 void UsbDeviceHandle::ReleaseInterface(const int interface_number, | 223 bool UsbDeviceHandle::ReleaseInterface(const int interface_number) { |
227 const UsbInterfaceCallback& callback) { | |
228 CheckDevice(); | 224 CheckDevice(); |
229 | 225 |
230 const int release_result = libusb_release_interface(handle_, | 226 const int release_result = libusb_release_interface(handle_, |
231 interface_number); | 227 interface_number); |
232 callback.Run(release_result == 0); | 228 return release_result == 0; |
233 } | 229 } |
234 | 230 |
235 void UsbDeviceHandle::SetInterfaceAlternateSetting( | 231 bool UsbDeviceHandle::SetInterfaceAlternateSetting( |
236 const int interface_number, | 232 const int interface_number, |
237 const int alternate_setting, | 233 const int alternate_setting) { |
238 const UsbInterfaceCallback& callback) { | |
239 CheckDevice(); | 234 CheckDevice(); |
240 | 235 |
241 const int setting_result = libusb_set_interface_alt_setting(handle_, | 236 const int setting_result = libusb_set_interface_alt_setting(handle_, |
242 interface_number, alternate_setting); | 237 interface_number, alternate_setting); |
243 | 238 |
244 callback.Run(setting_result == 0); | 239 return setting_result == 0; |
| 240 } |
| 241 |
| 242 bool UsbDeviceHandle::ResetDevice() { |
| 243 CheckDevice(); |
| 244 return libusb_reset_device(handle_) == 0; |
245 } | 245 } |
246 | 246 |
247 void UsbDeviceHandle::ControlTransfer(const UsbEndpointDirection direction, | 247 void UsbDeviceHandle::ControlTransfer(const UsbEndpointDirection direction, |
248 const TransferRequestType request_type, const TransferRecipient recipient, | 248 const TransferRequestType request_type, const TransferRecipient recipient, |
249 const uint8 request, const uint16 value, const uint16 index, | 249 const uint8 request, const uint16 value, const uint16 index, |
250 net::IOBuffer* buffer, const size_t length, const unsigned int timeout, | 250 net::IOBuffer* buffer, const size_t length, const unsigned int timeout, |
251 const UsbTransferCallback& callback) { | 251 const UsbTransferCallback& callback) { |
252 CheckDevice(); | 252 CheckDevice(); |
253 | 253 |
254 const size_t resized_length = LIBUSB_CONTROL_SETUP_SIZE + length; | 254 const size_t resized_length = LIBUSB_CONTROL_SETUP_SIZE + length; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 struct libusb_transfer* const transfer = libusb_alloc_transfer(packets); | 310 struct libusb_transfer* const transfer = libusb_alloc_transfer(packets); |
311 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 311 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
312 libusb_fill_iso_transfer(transfer, handle_, new_endpoint, | 312 libusb_fill_iso_transfer(transfer, handle_, new_endpoint, |
313 reinterpret_cast<uint8*>(buffer->data()), length, packets, | 313 reinterpret_cast<uint8*>(buffer->data()), length, packets, |
314 HandleTransferCompletion, this, timeout); | 314 HandleTransferCompletion, this, timeout); |
315 libusb_set_iso_packet_lengths(transfer, packet_length); | 315 libusb_set_iso_packet_lengths(transfer, packet_length); |
316 | 316 |
317 SubmitTransfer(transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); | 317 SubmitTransfer(transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); |
318 } | 318 } |
319 | 319 |
320 void UsbDeviceHandle::ResetDevice(const base::Callback<void(bool)>& callback) { | |
321 CheckDevice(); | |
322 callback.Run(libusb_reset_device(handle_) == 0); | |
323 } | |
324 | |
325 void UsbDeviceHandle::CheckDevice() { | 320 void UsbDeviceHandle::CheckDevice() { |
326 DCHECK(handle_) << "Device is already closed."; | 321 DCHECK(handle_) << "Device is already closed."; |
327 } | 322 } |
328 | 323 |
329 void UsbDeviceHandle::SubmitTransfer(PlatformUsbTransferHandle handle, | 324 void UsbDeviceHandle::SubmitTransfer(PlatformUsbTransferHandle handle, |
330 UsbTransferType transfer_type, | 325 UsbTransferType transfer_type, |
331 net::IOBuffer* buffer, | 326 net::IOBuffer* buffer, |
332 const size_t length, | 327 const size_t length, |
333 const UsbTransferCallback& callback) { | 328 const UsbTransferCallback& callback) { |
334 Transfer transfer; | 329 Transfer transfer; |
335 transfer.transfer_type = transfer_type; | 330 transfer.transfer_type = transfer_type; |
336 transfer.buffer = buffer; | 331 transfer.buffer = buffer; |
337 transfer.length = length; | 332 transfer.length = length; |
338 transfer.callback = callback; | 333 transfer.callback = callback; |
339 | 334 |
340 { | 335 { |
341 base::AutoLock lock(lock_); | 336 base::AutoLock lock(lock_); |
342 transfers_[handle] = transfer; | 337 transfers_[handle] = transfer; |
343 libusb_submit_transfer(handle); | 338 libusb_submit_transfer(handle); |
344 } | 339 } |
345 } | 340 } |
OLD | NEW |