| 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" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 callback_task_runner->PostTask( | 115 callback_task_runner->PostTask( |
| 116 FROM_HERE, base::Bind(callback, status, buffer, result)); | 116 FROM_HERE, base::Bind(callback, status, buffer, result)); |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 } // namespace | 120 } // namespace |
| 121 | 121 |
| 122 class UsbDeviceHandleImpl::InterfaceClaimer | 122 class UsbDeviceHandleImpl::InterfaceClaimer |
| 123 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { | 123 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { |
| 124 public: | 124 public: |
| 125 InterfaceClaimer(const scoped_refptr<UsbDeviceHandleImpl> handle, | 125 InterfaceClaimer(scoped_refptr<UsbDeviceHandleImpl> handle, |
| 126 const int interface_number); | 126 int interface_number); |
| 127 | |
| 128 bool Claim() const; | |
| 129 | 127 |
| 130 int alternate_setting() const { return alternate_setting_; } | 128 int alternate_setting() const { return alternate_setting_; } |
| 131 void set_alternate_setting(const int alternate_setting) { | 129 void set_alternate_setting(const int alternate_setting) { |
| 132 alternate_setting_ = alternate_setting; | 130 alternate_setting_ = alternate_setting; |
| 133 } | 131 } |
| 134 | 132 |
| 135 private: | 133 private: |
| 136 friend class UsbDevice; | |
| 137 friend class base::RefCountedThreadSafe<InterfaceClaimer>; | 134 friend class base::RefCountedThreadSafe<InterfaceClaimer>; |
| 138 ~InterfaceClaimer(); | 135 ~InterfaceClaimer(); |
| 139 | 136 |
| 140 const scoped_refptr<UsbDeviceHandleImpl> handle_; | 137 const scoped_refptr<UsbDeviceHandleImpl> handle_; |
| 141 const int interface_number_; | 138 const int interface_number_; |
| 142 int alternate_setting_; | 139 int alternate_setting_; |
| 143 | 140 |
| 144 DISALLOW_COPY_AND_ASSIGN(InterfaceClaimer); | 141 DISALLOW_COPY_AND_ASSIGN(InterfaceClaimer); |
| 145 }; | 142 }; |
| 146 | 143 |
| 147 UsbDeviceHandleImpl::InterfaceClaimer::InterfaceClaimer( | 144 UsbDeviceHandleImpl::InterfaceClaimer::InterfaceClaimer( |
| 148 const scoped_refptr<UsbDeviceHandleImpl> handle, | 145 scoped_refptr<UsbDeviceHandleImpl> handle, |
| 149 const int interface_number) | 146 int interface_number) |
| 150 : handle_(handle), | 147 : handle_(handle), |
| 151 interface_number_(interface_number), | 148 interface_number_(interface_number), |
| 152 alternate_setting_(0) { | 149 alternate_setting_(0) {} |
| 153 } | |
| 154 | 150 |
| 155 UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() { | 151 UsbDeviceHandleImpl::InterfaceClaimer::~InterfaceClaimer() { |
| 156 libusb_release_interface(handle_->handle(), interface_number_); | 152 libusb_release_interface(handle_->handle(), interface_number_); |
| 157 } | 153 } |
| 158 | 154 |
| 159 bool UsbDeviceHandleImpl::InterfaceClaimer::Claim() const { | |
| 160 const int rv = libusb_claim_interface(handle_->handle(), interface_number_); | |
| 161 if (rv != LIBUSB_SUCCESS) { | |
| 162 USB_LOG(EVENT) << "Failed to claim interface " << interface_number_ << ": " | |
| 163 << ConvertPlatformUsbErrorToString(rv); | |
| 164 } | |
| 165 return rv == LIBUSB_SUCCESS; | |
| 166 } | |
| 167 | |
| 168 // This inner class owns the underlying libusb_transfer and may outlast | 155 // This inner class owns the underlying libusb_transfer and may outlast |
| 169 // the UsbDeviceHandle that created it. | 156 // the UsbDeviceHandle that created it. |
| 170 class UsbDeviceHandleImpl::Transfer { | 157 class UsbDeviceHandleImpl::Transfer { |
| 171 public: | 158 public: |
| 172 static scoped_ptr<Transfer> CreateControlTransfer( | 159 static scoped_ptr<Transfer> CreateControlTransfer( |
| 173 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 160 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
| 174 uint8_t type, | 161 uint8_t type, |
| 175 uint8_t request, | 162 uint8_t request, |
| 176 uint16_t value, | 163 uint16_t value, |
| 177 uint16_t index, | 164 uint16_t index, |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 RefreshEndpointMap(); | 711 RefreshEndpointMap(); |
| 725 } | 712 } |
| 726 callback.Run(success); | 713 callback.Run(success); |
| 727 } | 714 } |
| 728 | 715 |
| 729 void UsbDeviceHandleImpl::ClaimInterfaceOnBlockingThread( | 716 void UsbDeviceHandleImpl::ClaimInterfaceOnBlockingThread( |
| 730 int interface_number, | 717 int interface_number, |
| 731 const ResultCallback& callback) { | 718 const ResultCallback& callback) { |
| 732 int rv = libusb_claim_interface(handle_, interface_number); | 719 int rv = libusb_claim_interface(handle_, interface_number); |
| 733 if (rv != LIBUSB_SUCCESS) { | 720 if (rv != LIBUSB_SUCCESS) { |
| 734 VLOG(1) << "Failed to claim interface: " | 721 USB_LOG(EVENT) << "Failed to claim interface: " |
| 735 << ConvertPlatformUsbErrorToString(rv); | 722 << ConvertPlatformUsbErrorToString(rv); |
| 736 } | 723 } |
| 737 task_runner_->PostTask( | 724 task_runner_->PostTask( |
| 738 FROM_HERE, base::Bind(&UsbDeviceHandleImpl::ClaimInterfaceComplete, this, | 725 FROM_HERE, base::Bind(&UsbDeviceHandleImpl::ClaimInterfaceComplete, this, |
| 739 interface_number, rv == LIBUSB_SUCCESS, callback)); | 726 interface_number, rv == LIBUSB_SUCCESS, callback)); |
| 740 } | 727 } |
| 741 | 728 |
| 742 void UsbDeviceHandleImpl::ClaimInterfaceComplete( | 729 void UsbDeviceHandleImpl::ClaimInterfaceComplete( |
| 743 int interface_number, | 730 int interface_number, |
| 744 bool success, | 731 bool success, |
| 745 const ResultCallback& callback) { | 732 const ResultCallback& callback) { |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 // Attempt-release all the interfaces. | 997 // Attempt-release all the interfaces. |
| 1011 // It will be retained until the transfer cancellation is finished. | 998 // It will be retained until the transfer cancellation is finished. |
| 1012 claimed_interfaces_.clear(); | 999 claimed_interfaces_.clear(); |
| 1013 | 1000 |
| 1014 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 1001 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
| 1015 // finish. | 1002 // finish. |
| 1016 device_ = NULL; | 1003 device_ = NULL; |
| 1017 } | 1004 } |
| 1018 | 1005 |
| 1019 } // namespace device | 1006 } // namespace device |
| OLD | NEW |