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" | |
19 #include "device/usb/usb_device_impl.h" | 18 #include "device/usb/usb_device_impl.h" |
20 #include "device/usb/usb_error.h" | 19 #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(scoped_refptr<UsbContext> context, | 185 UsbDeviceHandleImpl::UsbDeviceHandleImpl( |
186 UsbDeviceImpl* device, | 186 scoped_refptr<UsbContext> context, |
187 PlatformUsbDeviceHandle handle, | 187 UsbDeviceImpl* device, |
188 const UsbConfigDescriptor& config) | 188 PlatformUsbDeviceHandle handle, |
| 189 scoped_refptr<UsbConfigDescriptor> interfaces) |
189 : device_(device), | 190 : device_(device), |
190 handle_(handle), | 191 handle_(handle), |
191 config_(config), | 192 interfaces_(interfaces), |
192 context_(context), | 193 context_(context), |
193 task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 194 task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
194 DCHECK(handle) << "Cannot create device with NULL handle."; | 195 DCHECK(handle) << "Cannot create device with NULL handle."; |
| 196 DCHECK(interfaces_.get()) << "Unable to list interfaces"; |
195 } | 197 } |
196 | 198 |
197 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { | 199 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { |
198 DCHECK(thread_checker_.CalledOnValidThread()); | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
199 | 201 |
200 libusb_close(handle_); | 202 libusb_close(handle_); |
201 handle_ = NULL; | 203 handle_ = NULL; |
202 } | 204 } |
203 | 205 |
204 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { | 206 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 timeout); | 654 timeout); |
653 libusb_set_iso_packet_lengths(transfer, packet_length); | 655 libusb_set_iso_packet_lengths(transfer, packet_length); |
654 | 656 |
655 PostOrSubmitTransfer( | 657 PostOrSubmitTransfer( |
656 transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); | 658 transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); |
657 } | 659 } |
658 | 660 |
659 void UsbDeviceHandleImpl::RefreshEndpointMap() { | 661 void UsbDeviceHandleImpl::RefreshEndpointMap() { |
660 DCHECK(thread_checker_.CalledOnValidThread()); | 662 DCHECK(thread_checker_.CalledOnValidThread()); |
661 endpoint_map_.clear(); | 663 endpoint_map_.clear(); |
662 for (ClaimedInterfaceMap::iterator claimedIt = claimed_interfaces_.begin(); | 664 for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); |
663 claimedIt != claimed_interfaces_.end(); | 665 it != claimed_interfaces_.end(); |
664 ++claimedIt) { | 666 ++it) { |
665 for (UsbInterfaceDescriptor::Iterator ifaceIt = config_.interfaces.begin(); | 667 scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = |
666 ifaceIt != config_.interfaces.end(); | 668 interfaces_->GetInterface(it->first) |
667 ++ifaceIt) { | 669 ->GetAltSetting(it->second->alternate_setting()); |
668 if (ifaceIt->interface_number == claimedIt->first && | 670 for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { |
669 ifaceIt->alternate_setting == | 671 scoped_refptr<const UsbEndpointDescriptor> endpoint = |
670 claimedIt->second->alternate_setting()) { | 672 interface_desc->GetEndpoint(i); |
671 for (UsbEndpointDescriptor::Iterator endpointIt = | 673 endpoint_map_[endpoint->GetAddress()] = it->first; |
672 ifaceIt->endpoints.begin(); | |
673 endpointIt != ifaceIt->endpoints.end(); | |
674 ++endpointIt) { | |
675 endpoint_map_[endpointIt->address] = claimedIt->first; | |
676 } | |
677 break; | |
678 } | |
679 } | 674 } |
680 } | 675 } |
681 } | 676 } |
682 | 677 |
683 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> | 678 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> |
684 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { | 679 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { |
685 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; | 680 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; |
686 if (ContainsKey(endpoint_map_, address)) | 681 if (ContainsKey(endpoint_map_, address)) |
687 return claimed_interfaces_[endpoint_map_[address]]; | 682 return claimed_interfaces_[endpoint_map_[address]]; |
688 return NULL; | 683 return NULL; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 // Attempt-release all the interfaces. | 759 // Attempt-release all the interfaces. |
765 // It will be retained until the transfer cancellation is finished. | 760 // It will be retained until the transfer cancellation is finished. |
766 claimed_interfaces_.clear(); | 761 claimed_interfaces_.clear(); |
767 | 762 |
768 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 763 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
769 // finish. | 764 // finish. |
770 device_ = NULL; | 765 device_ = NULL; |
771 } | 766 } |
772 | 767 |
773 } // namespace device | 768 } // namespace device |
OLD | NEW |