OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "device/usb/usb_device_handle_impl.h" | 5 #include "device/usb/usb_device_handle_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
17 #include "device/usb/usb_context.h" | 17 #include "device/usb/usb_context.h" |
| 18 #include "device/usb/usb_descriptors.h" |
18 #include "device/usb/usb_device_impl.h" | 19 #include "device/usb/usb_device_impl.h" |
19 #include "device/usb/usb_error.h" | 20 #include "device/usb/usb_error.h" |
20 #include "device/usb/usb_interface.h" | |
21 #include "device/usb/usb_service.h" | 21 #include "device/usb/usb_service.h" |
22 #include "third_party/libusb/src/libusb/libusb.h" | 22 #include "third_party/libusb/src/libusb/libusb.h" |
23 | 23 |
24 namespace device { | 24 namespace device { |
25 | 25 |
26 typedef libusb_device* PlatformUsbDevice; | 26 typedef libusb_device* PlatformUsbDevice; |
27 | 27 |
28 void HandleTransferCompletion(PlatformUsbTransferHandle transfer); | 28 void HandleTransferCompletion(PlatformUsbTransferHandle transfer); |
29 | 29 |
30 namespace { | 30 namespace { |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 void UsbDeviceHandleImpl::Transfer::Complete(UsbTransferStatus status, | 175 void UsbDeviceHandleImpl::Transfer::Complete(UsbTransferStatus status, |
176 size_t bytes_transferred) { | 176 size_t bytes_transferred) { |
177 if (task_runner->RunsTasksOnCurrentThread()) { | 177 if (task_runner->RunsTasksOnCurrentThread()) { |
178 callback.Run(status, buffer, bytes_transferred); | 178 callback.Run(status, buffer, bytes_transferred); |
179 } else { | 179 } else { |
180 task_runner->PostTask( | 180 task_runner->PostTask( |
181 FROM_HERE, base::Bind(callback, status, buffer, bytes_transferred)); | 181 FROM_HERE, base::Bind(callback, status, buffer, bytes_transferred)); |
182 } | 182 } |
183 } | 183 } |
184 | 184 |
185 UsbDeviceHandleImpl::UsbDeviceHandleImpl( | 185 UsbDeviceHandleImpl::UsbDeviceHandleImpl(scoped_refptr<UsbContext> context, |
186 scoped_refptr<UsbContext> context, | 186 UsbDeviceImpl* device, |
187 UsbDeviceImpl* device, | 187 PlatformUsbDeviceHandle handle, |
188 PlatformUsbDeviceHandle handle, | 188 const UsbConfigDescriptor& config) |
189 scoped_refptr<UsbConfigDescriptor> interfaces) | |
190 : device_(device), | 189 : device_(device), |
191 handle_(handle), | 190 handle_(handle), |
192 interfaces_(interfaces), | 191 config_(config), |
193 context_(context), | 192 context_(context), |
194 task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 193 task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
195 DCHECK(handle) << "Cannot create device with NULL handle."; | 194 DCHECK(handle) << "Cannot create device with NULL handle."; |
196 DCHECK(interfaces_.get()) << "Unable to list interfaces"; | |
197 } | 195 } |
198 | 196 |
199 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { | 197 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { |
200 DCHECK(thread_checker_.CalledOnValidThread()); | 198 DCHECK(thread_checker_.CalledOnValidThread()); |
201 | 199 |
202 libusb_close(handle_); | 200 libusb_close(handle_); |
203 handle_ = NULL; | 201 handle_ = NULL; |
204 } | 202 } |
205 | 203 |
206 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { | 204 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 timeout); | 652 timeout); |
655 libusb_set_iso_packet_lengths(transfer, packet_length); | 653 libusb_set_iso_packet_lengths(transfer, packet_length); |
656 | 654 |
657 PostOrSubmitTransfer( | 655 PostOrSubmitTransfer( |
658 transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); | 656 transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); |
659 } | 657 } |
660 | 658 |
661 void UsbDeviceHandleImpl::RefreshEndpointMap() { | 659 void UsbDeviceHandleImpl::RefreshEndpointMap() { |
662 DCHECK(thread_checker_.CalledOnValidThread()); | 660 DCHECK(thread_checker_.CalledOnValidThread()); |
663 endpoint_map_.clear(); | 661 endpoint_map_.clear(); |
664 for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); | 662 for (ClaimedInterfaceMap::iterator claimedIt = claimed_interfaces_.begin(); |
665 it != claimed_interfaces_.end(); | 663 claimedIt != claimed_interfaces_.end(); |
666 ++it) { | 664 ++claimedIt) { |
667 scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = | 665 for (UsbInterfaceDescriptor::Iterator ifaceIt = config_.interfaces.begin(); |
668 interfaces_->GetInterface(it->first) | 666 ifaceIt != config_.interfaces.end(); |
669 ->GetAltSetting(it->second->alternate_setting()); | 667 ++ifaceIt) { |
670 for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { | 668 if (ifaceIt->interface_number == claimedIt->first && |
671 scoped_refptr<const UsbEndpointDescriptor> endpoint = | 669 ifaceIt->alternate_setting == |
672 interface_desc->GetEndpoint(i); | 670 claimedIt->second->alternate_setting()) { |
673 endpoint_map_[endpoint->GetAddress()] = it->first; | 671 for (UsbEndpointDescriptor::Iterator endpointIt = |
| 672 ifaceIt->endpoints.begin(); |
| 673 endpointIt != ifaceIt->endpoints.end(); |
| 674 ++endpointIt) { |
| 675 endpoint_map_[endpointIt->address] = claimedIt->first; |
| 676 } |
| 677 break; |
| 678 } |
674 } | 679 } |
675 } | 680 } |
676 } | 681 } |
677 | 682 |
678 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> | 683 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> |
679 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { | 684 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { |
680 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; | 685 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; |
681 if (ContainsKey(endpoint_map_, address)) | 686 if (ContainsKey(endpoint_map_, address)) |
682 return claimed_interfaces_[endpoint_map_[address]]; | 687 return claimed_interfaces_[endpoint_map_[address]]; |
683 return NULL; | 688 return NULL; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 // Attempt-release all the interfaces. | 764 // Attempt-release all the interfaces. |
760 // It will be retained until the transfer cancellation is finished. | 765 // It will be retained until the transfer cancellation is finished. |
761 claimed_interfaces_.clear(); | 766 claimed_interfaces_.clear(); |
762 | 767 |
763 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 768 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
764 // finish. | 769 // finish. |
765 device_ = NULL; | 770 device_ = NULL; |
766 } | 771 } |
767 | 772 |
768 } // namespace device | 773 } // namespace device |
OLD | NEW |