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 450 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 it = claimed_interfaces_.begin(); |
665 it != claimed_interfaces_.end(); | 663 it != claimed_interfaces_.end(); |
666 ++it) { | 664 ++it) { |
667 scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = | 665 for (size_t i = 0; i < config_.interfaces.size(); ++i) { |
668 interfaces_->GetInterface(it->first) | 666 const UsbInterfaceDescriptor& interface_desc = config_.interfaces[i]; |
669 ->GetAltSetting(it->second->alternate_setting()); | 667 if (interface_desc.interface_number == it->first && |
670 for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { | 668 interface_desc.alternate_setting == it->second->alternate_setting()) { |
671 scoped_refptr<const UsbEndpointDescriptor> endpoint = | 669 for (size_t j = 0; j < interface_desc.endpoints.size(); ++j) { |
672 interface_desc->GetEndpoint(i); | 670 const UsbEndpointDescriptor& endpoint = interface_desc.endpoints[j]; |
673 endpoint_map_[endpoint->GetAddress()] = it->first; | 671 endpoint_map_[endpoint.address] = it->first; |
| 672 } |
| 673 break; |
| 674 } |
674 } | 675 } |
675 } | 676 } |
676 } | 677 } |
677 | 678 |
678 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> | 679 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> |
679 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { | 680 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { |
680 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; | 681 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; |
681 if (ContainsKey(endpoint_map_, address)) | 682 if (ContainsKey(endpoint_map_, address)) |
682 return claimed_interfaces_[endpoint_map_[address]]; | 683 return claimed_interfaces_[endpoint_map_[address]]; |
683 return NULL; | 684 return NULL; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
759 // Attempt-release all the interfaces. | 760 // Attempt-release all the interfaces. |
760 // It will be retained until the transfer cancellation is finished. | 761 // It will be retained until the transfer cancellation is finished. |
761 claimed_interfaces_.clear(); | 762 claimed_interfaces_.clear(); |
762 | 763 |
763 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 764 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
764 // finish. | 765 // finish. |
765 device_ = NULL; | 766 device_ = NULL; |
766 } | 767 } |
767 | 768 |
768 } // namespace device | 769 } // namespace device |
OLD | NEW |