| 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 "components/usb_service/usb_device_handle_impl.h" | 5 #include "components/usb_service/usb_device_handle_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 11 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 12 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 13 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/thread_task_runner_handle.h" |
| 14 #include "components/usb_service/usb_context.h" | 17 #include "components/usb_service/usb_context.h" |
| 15 #include "components/usb_service/usb_device_impl.h" | 18 #include "components/usb_service/usb_device_impl.h" |
| 16 #include "components/usb_service/usb_error.h" | 19 #include "components/usb_service/usb_error.h" |
| 17 #include "components/usb_service/usb_interface.h" | 20 #include "components/usb_service/usb_interface.h" |
| 18 #include "components/usb_service/usb_service.h" | 21 #include "components/usb_service/usb_service.h" |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "third_party/libusb/src/libusb/libusb.h" | 22 #include "third_party/libusb/src/libusb/libusb.h" |
| 21 | 23 |
| 22 using content::BrowserThread; | |
| 23 | |
| 24 namespace usb_service { | 24 namespace usb_service { |
| 25 | 25 |
| 26 typedef libusb_device* PlatformUsbDevice; | 26 typedef libusb_device* PlatformUsbDevice; |
| 27 | 27 |
| 28 void HandleTransferCompletion(usb_service::PlatformUsbTransferHandle transfer); | 28 void HandleTransferCompletion(usb_service::PlatformUsbTransferHandle transfer); |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 static uint8 ConvertTransferDirection(const UsbEndpointDirection direction) { | 32 static uint8 ConvertTransferDirection(const UsbEndpointDirection direction) { |
| 33 switch (direction) { | 33 switch (direction) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 case LIBUSB_TRANSFER_OVERFLOW: | 96 case LIBUSB_TRANSFER_OVERFLOW: |
| 97 return USB_TRANSFER_OVERFLOW; | 97 return USB_TRANSFER_OVERFLOW; |
| 98 case LIBUSB_TRANSFER_CANCELLED: | 98 case LIBUSB_TRANSFER_CANCELLED: |
| 99 return USB_TRANSFER_CANCELLED; | 99 return USB_TRANSFER_CANCELLED; |
| 100 default: | 100 default: |
| 101 NOTREACHED(); | 101 NOTREACHED(); |
| 102 return USB_TRANSFER_ERROR; | 102 return USB_TRANSFER_ERROR; |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 static void LIBUSB_CALL | |
| 107 PlatformTransferCompletionCallback(PlatformUsbTransferHandle transfer) { | |
| 108 BrowserThread::PostTask(BrowserThread::FILE, | |
| 109 FROM_HERE, | |
| 110 base::Bind(HandleTransferCompletion, transfer)); | |
| 111 } | |
| 112 | |
| 113 } // namespace | 106 } // namespace |
| 114 | 107 |
| 115 void HandleTransferCompletion(PlatformUsbTransferHandle transfer) { | |
| 116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 117 UsbDeviceHandleImpl* const device_handle = | |
| 118 reinterpret_cast<UsbDeviceHandleImpl*>(transfer->user_data); | |
| 119 CHECK(device_handle) << "Device handle is closed before transfer finishes."; | |
| 120 device_handle->TransferComplete(transfer); | |
| 121 libusb_free_transfer(transfer); | |
| 122 } | |
| 123 | |
| 124 class UsbDeviceHandleImpl::InterfaceClaimer | 108 class UsbDeviceHandleImpl::InterfaceClaimer |
| 125 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { | 109 : public base::RefCountedThreadSafe<UsbDeviceHandleImpl::InterfaceClaimer> { |
| 126 public: | 110 public: |
| 127 InterfaceClaimer(const scoped_refptr<UsbDeviceHandleImpl> handle, | 111 InterfaceClaimer(const scoped_refptr<UsbDeviceHandleImpl> handle, |
| 128 const int interface_number); | 112 const int interface_number); |
| 129 | 113 |
| 130 bool Claim() const; | 114 bool Claim() const; |
| 131 | 115 |
| 132 int alternate_setting() const { return alternate_setting_; } | 116 int alternate_setting() const { return alternate_setting_; } |
| 133 void set_alternate_setting(const int alternate_setting) { | 117 void set_alternate_setting(const int alternate_setting) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 163 if (rv != LIBUSB_SUCCESS) { | 147 if (rv != LIBUSB_SUCCESS) { |
| 164 VLOG(1) << "Failed to claim interface: " << ConvertErrorToString(rv); | 148 VLOG(1) << "Failed to claim interface: " << ConvertErrorToString(rv); |
| 165 } | 149 } |
| 166 return rv == LIBUSB_SUCCESS; | 150 return rv == LIBUSB_SUCCESS; |
| 167 } | 151 } |
| 168 | 152 |
| 169 struct UsbDeviceHandleImpl::Transfer { | 153 struct UsbDeviceHandleImpl::Transfer { |
| 170 Transfer(); | 154 Transfer(); |
| 171 ~Transfer(); | 155 ~Transfer(); |
| 172 | 156 |
| 157 void Complete(UsbTransferStatus status, size_t bytes_transferred); |
| 158 |
| 173 UsbTransferType transfer_type; | 159 UsbTransferType transfer_type; |
| 174 scoped_refptr<net::IOBuffer> buffer; | 160 scoped_refptr<net::IOBuffer> buffer; |
| 175 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> claimed_interface; | 161 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> claimed_interface; |
| 176 scoped_refptr<base::MessageLoopProxy> message_loop_proxy; | 162 scoped_refptr<base::SingleThreadTaskRunner> task_runner; |
| 177 size_t length; | 163 size_t length; |
| 178 UsbTransferCallback callback; | 164 UsbTransferCallback callback; |
| 179 }; | 165 }; |
| 180 | 166 |
| 181 UsbDeviceHandleImpl::Transfer::Transfer() | 167 UsbDeviceHandleImpl::Transfer::Transfer() |
| 182 : transfer_type(USB_TRANSFER_CONTROL), length(0) { | 168 : transfer_type(USB_TRANSFER_CONTROL), length(0) { |
| 183 } | 169 } |
| 184 | 170 |
| 185 UsbDeviceHandleImpl::Transfer::~Transfer() { | 171 UsbDeviceHandleImpl::Transfer::~Transfer() { |
| 186 } | 172 } |
| 187 | 173 |
| 174 void UsbDeviceHandleImpl::Transfer::Complete(UsbTransferStatus status, |
| 175 size_t bytes_transferred) { |
| 176 if (task_runner->RunsTasksOnCurrentThread()) { |
| 177 callback.Run(status, buffer, bytes_transferred); |
| 178 } else { |
| 179 task_runner->PostTask( |
| 180 FROM_HERE, base::Bind(callback, status, buffer, bytes_transferred)); |
| 181 } |
| 182 } |
| 183 |
| 188 UsbDeviceHandleImpl::UsbDeviceHandleImpl( | 184 UsbDeviceHandleImpl::UsbDeviceHandleImpl( |
| 189 scoped_refptr<UsbContext> context, | 185 scoped_refptr<UsbContext> context, |
| 190 UsbDeviceImpl* device, | 186 UsbDeviceImpl* device, |
| 191 PlatformUsbDeviceHandle handle, | 187 PlatformUsbDeviceHandle handle, |
| 192 scoped_refptr<UsbConfigDescriptor> interfaces) | 188 scoped_refptr<UsbConfigDescriptor> interfaces) |
| 193 : device_(device), | 189 : device_(device), |
| 194 handle_(handle), | 190 handle_(handle), |
| 195 interfaces_(interfaces), | 191 interfaces_(interfaces), |
| 196 context_(context) { | 192 context_(context), |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); | 193 task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 198 DCHECK(handle) << "Cannot create device with NULL handle."; | 194 DCHECK(handle) << "Cannot create device with NULL handle."; |
| 199 DCHECK(interfaces_.get()) << "Unable to list interfaces"; | 195 DCHECK(interfaces_.get()) << "Unable to list interfaces"; |
| 200 } | 196 } |
| 201 | 197 |
| 202 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { | 198 UsbDeviceHandleImpl::~UsbDeviceHandleImpl() { |
| 203 DCHECK(thread_checker_.CalledOnValidThread()); | 199 DCHECK(thread_checker_.CalledOnValidThread()); |
| 204 | 200 |
| 205 libusb_close(handle_); | 201 libusb_close(handle_); |
| 206 handle_ = NULL; | 202 handle_ = NULL; |
| 207 } | 203 } |
| 208 | 204 |
| 209 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { | 205 scoped_refptr<UsbDevice> UsbDeviceHandleImpl::GetDevice() const { |
| 210 return static_cast<UsbDevice*>(device_); | 206 return static_cast<UsbDevice*>(device_); |
| 211 } | 207 } |
| 212 | 208 |
| 213 void UsbDeviceHandleImpl::Close() { | 209 void UsbDeviceHandleImpl::Close() { |
| 214 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 215 if (device_) | 211 if (device_) |
| 216 device_->Close(this); | 212 device_->Close(this); |
| 217 } | 213 } |
| 218 | 214 |
| 219 void UsbDeviceHandleImpl::TransferComplete(PlatformUsbTransferHandle handle) { | 215 /* static */ void LIBUSB_CALL UsbDeviceHandleImpl::PlatformTransferCallback( |
| 216 PlatformUsbTransferHandle transfer) { |
| 217 UsbDeviceHandleImpl* device_handle = |
| 218 reinterpret_cast<UsbDeviceHandleImpl*>(transfer->user_data); |
| 219 device_handle->task_runner_->PostTask( |
| 220 FROM_HERE, |
| 221 base::Bind( |
| 222 &UsbDeviceHandleImpl::CompleteTransfer, device_handle, transfer)); |
| 223 } |
| 224 |
| 225 void UsbDeviceHandleImpl::CompleteTransfer(PlatformUsbTransferHandle handle) { |
| 220 DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed"; | 226 DCHECK(ContainsKey(transfers_, handle)) << "Missing transfer completed"; |
| 221 | 227 |
| 222 Transfer transfer = transfers_[handle]; | 228 Transfer transfer = transfers_[handle]; |
| 223 transfers_.erase(handle); | 229 transfers_.erase(handle); |
| 224 | 230 |
| 225 DCHECK_GE(handle->actual_length, 0) << "Negative actual length received"; | 231 DCHECK_GE(handle->actual_length, 0) << "Negative actual length received"; |
| 226 size_t actual_length = | 232 size_t actual_length = |
| 227 static_cast<size_t>(std::max(handle->actual_length, 0)); | 233 static_cast<size_t>(std::max(handle->actual_length, 0)); |
| 228 | 234 |
| 229 DCHECK(transfer.length >= actual_length) | 235 DCHECK(transfer.length >= actual_length) |
| 230 << "data too big for our buffer (libusb failure?)"; | 236 << "data too big for our buffer (libusb failure?)"; |
| 231 | 237 |
| 232 scoped_refptr<net::IOBuffer> buffer = transfer.buffer; | |
| 233 switch (transfer.transfer_type) { | 238 switch (transfer.transfer_type) { |
| 234 case USB_TRANSFER_CONTROL: | 239 case USB_TRANSFER_CONTROL: |
| 235 // If the transfer is a control transfer we do not expose the control | 240 // If the transfer is a control transfer we do not expose the control |
| 236 // setup header to the caller. This logic strips off the header if | 241 // setup header to the caller. This logic strips off the header if |
| 237 // present before invoking the callback provided with the transfer. | 242 // present before invoking the callback provided with the transfer. |
| 238 if (actual_length > 0) { | 243 if (actual_length > 0) { |
| 239 CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE) | 244 CHECK(transfer.length >= LIBUSB_CONTROL_SETUP_SIZE) |
| 240 << "buffer was not correctly set: too small for the control header"; | 245 << "buffer was not correctly set: too small for the control header"; |
| 241 | 246 |
| 242 if (transfer.length >= (LIBUSB_CONTROL_SETUP_SIZE + actual_length)) { | 247 if (transfer.length >= (LIBUSB_CONTROL_SETUP_SIZE + actual_length)) { |
| 243 // If the payload is zero bytes long, pad out the allocated buffer | 248 // If the payload is zero bytes long, pad out the allocated buffer |
| 244 // size to one byte so that an IOBuffer of that size can be allocated. | 249 // size to one byte so that an IOBuffer of that size can be allocated. |
| 245 scoped_refptr<net::IOBuffer> resized_buffer = | 250 scoped_refptr<net::IOBuffer> resized_buffer = |
| 246 new net::IOBuffer(static_cast<int>( | 251 new net::IOBuffer(static_cast<int>( |
| 247 std::max(actual_length, static_cast<size_t>(1)))); | 252 std::max(actual_length, static_cast<size_t>(1)))); |
| 248 memcpy(resized_buffer->data(), | 253 memcpy(resized_buffer->data(), |
| 249 buffer->data() + LIBUSB_CONTROL_SETUP_SIZE, | 254 transfer.buffer->data() + LIBUSB_CONTROL_SETUP_SIZE, |
| 250 actual_length); | 255 actual_length); |
| 251 buffer = resized_buffer; | 256 transfer.buffer = resized_buffer; |
| 252 } | 257 } |
| 253 } | 258 } |
| 254 break; | 259 break; |
| 255 | 260 |
| 256 case USB_TRANSFER_ISOCHRONOUS: | 261 case USB_TRANSFER_ISOCHRONOUS: |
| 257 // Isochronous replies might carry data in the different isoc packets even | 262 // Isochronous replies might carry data in the different isoc packets even |
| 258 // if the transfer actual_data value is zero. Furthermore, not all of the | 263 // if the transfer actual_data value is zero. Furthermore, not all of the |
| 259 // received packets might contain data, so we need to calculate how many | 264 // received packets might contain data, so we need to calculate how many |
| 260 // data bytes we are effectively providing and pack the results. | 265 // data bytes we are effectively providing and pack the results. |
| 261 if (actual_length == 0) { | 266 if (actual_length == 0) { |
| 262 size_t packet_buffer_start = 0; | 267 size_t packet_buffer_start = 0; |
| 263 for (int i = 0; i < handle->num_iso_packets; ++i) { | 268 for (int i = 0; i < handle->num_iso_packets; ++i) { |
| 264 PlatformUsbIsoPacketDescriptor packet = &handle->iso_packet_desc[i]; | 269 PlatformUsbIsoPacketDescriptor packet = &handle->iso_packet_desc[i]; |
| 265 if (packet->actual_length > 0) { | 270 if (packet->actual_length > 0) { |
| 266 // We don't need to copy as long as all packets until now provide | 271 // We don't need to copy as long as all packets until now provide |
| 267 // all the data the packet can hold. | 272 // all the data the packet can hold. |
| 268 if (actual_length < packet_buffer_start) { | 273 if (actual_length < packet_buffer_start) { |
| 269 CHECK(packet_buffer_start + packet->actual_length <= | 274 CHECK(packet_buffer_start + packet->actual_length <= |
| 270 transfer.length); | 275 transfer.length); |
| 271 memmove(buffer->data() + actual_length, | 276 memmove(transfer.buffer->data() + actual_length, |
| 272 buffer->data() + packet_buffer_start, | 277 transfer.buffer->data() + packet_buffer_start, |
| 273 packet->actual_length); | 278 packet->actual_length); |
| 274 } | 279 } |
| 275 actual_length += packet->actual_length; | 280 actual_length += packet->actual_length; |
| 276 } | 281 } |
| 277 | 282 |
| 278 packet_buffer_start += packet->length; | 283 packet_buffer_start += packet->length; |
| 279 } | 284 } |
| 280 } | 285 } |
| 281 break; | 286 break; |
| 282 | 287 |
| 283 case USB_TRANSFER_BULK: | 288 case USB_TRANSFER_BULK: |
| 284 case USB_TRANSFER_INTERRUPT: | 289 case USB_TRANSFER_INTERRUPT: |
| 285 break; | 290 break; |
| 286 | 291 |
| 287 default: | 292 default: |
| 288 NOTREACHED() << "Invalid usb transfer type"; | 293 NOTREACHED() << "Invalid usb transfer type"; |
| 289 break; | 294 break; |
| 290 } | 295 } |
| 291 | 296 |
| 292 transfer.message_loop_proxy->PostTask( | 297 transfer.Complete(ConvertTransferStatus(handle->status), actual_length); |
| 293 FROM_HERE, | 298 libusb_free_transfer(handle); |
| 294 base::Bind(transfer.callback, | |
| 295 ConvertTransferStatus(handle->status), | |
| 296 buffer, | |
| 297 actual_length)); | |
| 298 | 299 |
| 299 // Must release interface first before actually delete this. | 300 // Must release interface first before actually delete this. |
| 300 transfer.claimed_interface = NULL; | 301 transfer.claimed_interface = NULL; |
| 301 } | 302 } |
| 302 | 303 |
| 303 bool UsbDeviceHandleImpl::ClaimInterface(const int interface_number) { | 304 bool UsbDeviceHandleImpl::ClaimInterface(const int interface_number) { |
| 304 DCHECK(thread_checker_.CalledOnValidThread()); | 305 DCHECK(thread_checker_.CalledOnValidThread()); |
| 305 if (!device_) | 306 if (!device_) |
| 306 return false; | 307 return false; |
| 307 if (ContainsKey(claimed_interfaces_, interface_number)) | 308 if (ContainsKey(claimed_interfaces_, interface_number)) |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 CreateRequestType(direction, request_type, recipient); | 546 CreateRequestType(direction, request_type, recipient); |
| 546 libusb_fill_control_setup(reinterpret_cast<uint8*>(resized_buffer->data()), | 547 libusb_fill_control_setup(reinterpret_cast<uint8*>(resized_buffer->data()), |
| 547 converted_type, | 548 converted_type, |
| 548 request, | 549 request, |
| 549 value, | 550 value, |
| 550 index, | 551 index, |
| 551 static_cast<int16>(length)); | 552 static_cast<int16>(length)); |
| 552 libusb_fill_control_transfer(transfer, | 553 libusb_fill_control_transfer(transfer, |
| 553 handle_, | 554 handle_, |
| 554 reinterpret_cast<uint8*>(resized_buffer->data()), | 555 reinterpret_cast<uint8*>(resized_buffer->data()), |
| 555 PlatformTransferCompletionCallback, | 556 &UsbDeviceHandleImpl::PlatformTransferCallback, |
| 556 this, | 557 this, |
| 557 timeout); | 558 timeout); |
| 558 | 559 |
| 559 BrowserThread::PostTask(BrowserThread::FILE, | 560 PostOrSubmitTransfer( |
| 560 FROM_HERE, | 561 transfer, USB_TRANSFER_CONTROL, resized_buffer, resized_length, callback); |
| 561 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | |
| 562 this, | |
| 563 transfer, | |
| 564 USB_TRANSFER_CONTROL, | |
| 565 resized_buffer, | |
| 566 resized_length, | |
| 567 base::MessageLoopProxy::current(), | |
| 568 callback)); | |
| 569 } | 562 } |
| 570 | 563 |
| 571 void UsbDeviceHandleImpl::BulkTransfer(const UsbEndpointDirection direction, | 564 void UsbDeviceHandleImpl::BulkTransfer(const UsbEndpointDirection direction, |
| 572 const uint8 endpoint, | 565 const uint8 endpoint, |
| 573 net::IOBuffer* buffer, | 566 net::IOBuffer* buffer, |
| 574 const size_t length, | 567 const size_t length, |
| 575 const unsigned int timeout, | 568 const unsigned int timeout, |
| 576 const UsbTransferCallback& callback) { | 569 const UsbTransferCallback& callback) { |
| 577 if (!device_) { | 570 if (!device_) { |
| 578 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 571 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
| 579 return; | 572 return; |
| 580 } | 573 } |
| 581 | 574 |
| 582 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); | 575 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); |
| 583 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 576 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
| 584 libusb_fill_bulk_transfer(transfer, | 577 libusb_fill_bulk_transfer(transfer, |
| 585 handle_, | 578 handle_, |
| 586 new_endpoint, | 579 new_endpoint, |
| 587 reinterpret_cast<uint8*>(buffer->data()), | 580 reinterpret_cast<uint8*>(buffer->data()), |
| 588 static_cast<int>(length), | 581 static_cast<int>(length), |
| 589 PlatformTransferCompletionCallback, | 582 &UsbDeviceHandleImpl::PlatformTransferCallback, |
| 590 this, | 583 this, |
| 591 timeout); | 584 timeout); |
| 592 | 585 |
| 593 BrowserThread::PostTask(BrowserThread::FILE, | 586 PostOrSubmitTransfer(transfer, USB_TRANSFER_BULK, buffer, length, callback); |
| 594 FROM_HERE, | |
| 595 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | |
| 596 this, | |
| 597 transfer, | |
| 598 USB_TRANSFER_BULK, | |
| 599 make_scoped_refptr(buffer), | |
| 600 length, | |
| 601 base::MessageLoopProxy::current(), | |
| 602 callback)); | |
| 603 } | 587 } |
| 604 | 588 |
| 605 void UsbDeviceHandleImpl::InterruptTransfer( | 589 void UsbDeviceHandleImpl::InterruptTransfer( |
| 606 const UsbEndpointDirection direction, | 590 const UsbEndpointDirection direction, |
| 607 const uint8 endpoint, | 591 const uint8 endpoint, |
| 608 net::IOBuffer* buffer, | 592 net::IOBuffer* buffer, |
| 609 const size_t length, | 593 const size_t length, |
| 610 const unsigned int timeout, | 594 const unsigned int timeout, |
| 611 const UsbTransferCallback& callback) { | 595 const UsbTransferCallback& callback) { |
| 612 if (!device_) { | 596 if (!device_) { |
| 613 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 597 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
| 614 return; | 598 return; |
| 615 } | 599 } |
| 616 | 600 |
| 617 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); | 601 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); |
| 618 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 602 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
| 619 libusb_fill_interrupt_transfer(transfer, | 603 libusb_fill_interrupt_transfer(transfer, |
| 620 handle_, | 604 handle_, |
| 621 new_endpoint, | 605 new_endpoint, |
| 622 reinterpret_cast<uint8*>(buffer->data()), | 606 reinterpret_cast<uint8*>(buffer->data()), |
| 623 static_cast<int>(length), | 607 static_cast<int>(length), |
| 624 PlatformTransferCompletionCallback, | 608 &UsbDeviceHandleImpl::PlatformTransferCallback, |
| 625 this, | 609 this, |
| 626 timeout); | 610 timeout); |
| 627 BrowserThread::PostTask(BrowserThread::FILE, | 611 |
| 628 FROM_HERE, | 612 PostOrSubmitTransfer( |
| 629 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | 613 transfer, USB_TRANSFER_INTERRUPT, buffer, length, callback); |
| 630 this, | |
| 631 transfer, | |
| 632 USB_TRANSFER_INTERRUPT, | |
| 633 make_scoped_refptr(buffer), | |
| 634 length, | |
| 635 base::MessageLoopProxy::current(), | |
| 636 callback)); | |
| 637 } | 614 } |
| 638 | 615 |
| 639 void UsbDeviceHandleImpl::IsochronousTransfer( | 616 void UsbDeviceHandleImpl::IsochronousTransfer( |
| 640 const UsbEndpointDirection direction, | 617 const UsbEndpointDirection direction, |
| 641 const uint8 endpoint, | 618 const uint8 endpoint, |
| 642 net::IOBuffer* buffer, | 619 net::IOBuffer* buffer, |
| 643 const size_t length, | 620 const size_t length, |
| 644 const unsigned int packets, | 621 const unsigned int packets, |
| 645 const unsigned int packet_length, | 622 const unsigned int packet_length, |
| 646 const unsigned int timeout, | 623 const unsigned int timeout, |
| 647 const UsbTransferCallback& callback) { | 624 const UsbTransferCallback& callback) { |
| 648 if (!device_) { | 625 if (!device_) { |
| 649 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 626 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
| 650 return; | 627 return; |
| 651 } | 628 } |
| 652 | 629 |
| 653 const uint64 total_length = packets * packet_length; | 630 const uint64 total_length = packets * packet_length; |
| 654 CHECK(packets <= length && total_length <= length) | 631 CHECK(packets <= length && total_length <= length) |
| 655 << "transfer length is too small"; | 632 << "transfer length is too small"; |
| 656 | 633 |
| 657 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(packets); | 634 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(packets); |
| 658 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 635 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
| 659 libusb_fill_iso_transfer(transfer, | 636 libusb_fill_iso_transfer(transfer, |
| 660 handle_, | 637 handle_, |
| 661 new_endpoint, | 638 new_endpoint, |
| 662 reinterpret_cast<uint8*>(buffer->data()), | 639 reinterpret_cast<uint8*>(buffer->data()), |
| 663 static_cast<int>(length), | 640 static_cast<int>(length), |
| 664 packets, | 641 packets, |
| 665 PlatformTransferCompletionCallback, | 642 &UsbDeviceHandleImpl::PlatformTransferCallback, |
| 666 this, | 643 this, |
| 667 timeout); | 644 timeout); |
| 668 libusb_set_iso_packet_lengths(transfer, packet_length); | 645 libusb_set_iso_packet_lengths(transfer, packet_length); |
| 669 | 646 |
| 670 BrowserThread::PostTask(BrowserThread::FILE, | 647 PostOrSubmitTransfer( |
| 671 FROM_HERE, | 648 transfer, USB_TRANSFER_ISOCHRONOUS, buffer, length, callback); |
| 672 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, | |
| 673 this, | |
| 674 transfer, | |
| 675 USB_TRANSFER_ISOCHRONOUS, | |
| 676 make_scoped_refptr(buffer), | |
| 677 length, | |
| 678 base::MessageLoopProxy::current(), | |
| 679 callback)); | |
| 680 } | 649 } |
| 681 | 650 |
| 682 void UsbDeviceHandleImpl::RefreshEndpointMap() { | 651 void UsbDeviceHandleImpl::RefreshEndpointMap() { |
| 683 DCHECK(thread_checker_.CalledOnValidThread()); | 652 DCHECK(thread_checker_.CalledOnValidThread()); |
| 684 endpoint_map_.clear(); | 653 endpoint_map_.clear(); |
| 685 for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); | 654 for (ClaimedInterfaceMap::iterator it = claimed_interfaces_.begin(); |
| 686 it != claimed_interfaces_.end(); | 655 it != claimed_interfaces_.end(); |
| 687 ++it) { | 656 ++it) { |
| 688 scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = | 657 scoped_refptr<const UsbInterfaceAltSettingDescriptor> interface_desc = |
| 689 interfaces_->GetInterface(it->first) | 658 interfaces_->GetInterface(it->first) |
| 690 ->GetAltSetting(it->second->alternate_setting()); | 659 ->GetAltSetting(it->second->alternate_setting()); |
| 691 for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { | 660 for (size_t i = 0; i < interface_desc->GetNumEndpoints(); i++) { |
| 692 scoped_refptr<const UsbEndpointDescriptor> endpoint = | 661 scoped_refptr<const UsbEndpointDescriptor> endpoint = |
| 693 interface_desc->GetEndpoint(i); | 662 interface_desc->GetEndpoint(i); |
| 694 endpoint_map_[endpoint->GetAddress()] = it->first; | 663 endpoint_map_[endpoint->GetAddress()] = it->first; |
| 695 } | 664 } |
| 696 } | 665 } |
| 697 } | 666 } |
| 698 | 667 |
| 699 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> | 668 scoped_refptr<UsbDeviceHandleImpl::InterfaceClaimer> |
| 700 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { | 669 UsbDeviceHandleImpl::GetClaimedInterfaceForEndpoint(unsigned char endpoint) { |
| 701 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; | 670 unsigned char address = endpoint & LIBUSB_ENDPOINT_ADDRESS_MASK; |
| 702 if (ContainsKey(endpoint_map_, address)) | 671 if (ContainsKey(endpoint_map_, address)) |
| 703 return claimed_interfaces_[endpoint_map_[address]]; | 672 return claimed_interfaces_[endpoint_map_[address]]; |
| 704 return NULL; | 673 return NULL; |
| 705 } | 674 } |
| 706 | 675 |
| 676 void UsbDeviceHandleImpl::PostOrSubmitTransfer( |
| 677 PlatformUsbTransferHandle transfer, |
| 678 UsbTransferType transfer_type, |
| 679 net::IOBuffer* buffer, |
| 680 size_t length, |
| 681 const UsbTransferCallback& callback) { |
| 682 if (task_runner_->RunsTasksOnCurrentThread()) { |
| 683 SubmitTransfer(transfer, |
| 684 transfer_type, |
| 685 buffer, |
| 686 length, |
| 687 base::ThreadTaskRunnerHandle::Get(), |
| 688 callback); |
| 689 } else { |
| 690 task_runner_->PostTask(FROM_HERE, |
| 691 base::Bind(&UsbDeviceHandleImpl::SubmitTransfer, |
| 692 this, |
| 693 transfer, |
| 694 transfer_type, |
| 695 make_scoped_refptr(buffer), |
| 696 length, |
| 697 base::ThreadTaskRunnerHandle::Get(), |
| 698 callback)); |
| 699 } |
| 700 } |
| 701 |
| 707 void UsbDeviceHandleImpl::SubmitTransfer( | 702 void UsbDeviceHandleImpl::SubmitTransfer( |
| 708 PlatformUsbTransferHandle handle, | 703 PlatformUsbTransferHandle handle, |
| 709 UsbTransferType transfer_type, | 704 UsbTransferType transfer_type, |
| 710 net::IOBuffer* buffer, | 705 net::IOBuffer* buffer, |
| 711 const size_t length, | 706 const size_t length, |
| 712 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 707 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 713 const UsbTransferCallback& callback) { | 708 const UsbTransferCallback& callback) { |
| 714 DCHECK(thread_checker_.CalledOnValidThread()); | 709 DCHECK(thread_checker_.CalledOnValidThread()); |
| 715 if (!device_) { | |
| 716 message_loop_proxy->PostTask( | |
| 717 FROM_HERE, | |
| 718 base::Bind( | |
| 719 callback, USB_TRANSFER_DISCONNECT, make_scoped_refptr(buffer), 0)); | |
| 720 } | |
| 721 | 710 |
| 722 Transfer transfer; | 711 Transfer transfer; |
| 723 transfer.transfer_type = transfer_type; | 712 transfer.transfer_type = transfer_type; |
| 724 transfer.buffer = buffer; | 713 transfer.buffer = buffer; |
| 725 transfer.length = length; | 714 transfer.length = length; |
| 726 transfer.callback = callback; | 715 transfer.callback = callback; |
| 727 transfer.message_loop_proxy = message_loop_proxy; | 716 transfer.task_runner = task_runner; |
| 717 |
| 718 if (!device_) { |
| 719 transfer.Complete(USB_TRANSFER_DISCONNECT, 0); |
| 720 return; |
| 721 } |
| 728 | 722 |
| 729 // It's OK for this method to return NULL. libusb_submit_transfer will fail if | 723 // It's OK for this method to return NULL. libusb_submit_transfer will fail if |
| 730 // it requires an interface we didn't claim. | 724 // it requires an interface we didn't claim. |
| 731 transfer.claimed_interface = GetClaimedInterfaceForEndpoint(handle->endpoint); | 725 transfer.claimed_interface = GetClaimedInterfaceForEndpoint(handle->endpoint); |
| 732 | 726 |
| 733 const int rv = libusb_submit_transfer(handle); | 727 const int rv = libusb_submit_transfer(handle); |
| 734 if (rv == LIBUSB_SUCCESS) { | 728 if (rv == LIBUSB_SUCCESS) { |
| 735 transfers_[handle] = transfer; | 729 transfers_[handle] = transfer; |
| 736 } else { | 730 } else { |
| 737 VLOG(1) << "Failed to submit transfer: " << ConvertErrorToString(rv); | 731 VLOG(1) << "Failed to submit transfer: " << ConvertErrorToString(rv); |
| 738 message_loop_proxy->PostTask( | 732 transfer.Complete(USB_TRANSFER_ERROR, 0); |
| 739 FROM_HERE, | |
| 740 base::Bind( | |
| 741 callback, USB_TRANSFER_ERROR, make_scoped_refptr(buffer), 0)); | |
| 742 } | 733 } |
| 743 } | 734 } |
| 744 | 735 |
| 745 void UsbDeviceHandleImpl::InternalClose() { | 736 void UsbDeviceHandleImpl::InternalClose() { |
| 746 DCHECK(thread_checker_.CalledOnValidThread()); | 737 DCHECK(thread_checker_.CalledOnValidThread()); |
| 747 if (!device_) | 738 if (!device_) |
| 748 return; | 739 return; |
| 749 | 740 |
| 750 // Cancel all the transfers. | 741 // Cancel all the transfers. |
| 751 for (TransferMap::iterator it = transfers_.begin(); it != transfers_.end(); | 742 for (TransferMap::iterator it = transfers_.begin(); it != transfers_.end(); |
| 752 ++it) { | 743 ++it) { |
| 753 // The callback will be called some time later. | 744 // The callback will be called some time later. |
| 754 libusb_cancel_transfer(it->first); | 745 libusb_cancel_transfer(it->first); |
| 755 } | 746 } |
| 756 | 747 |
| 757 // Attempt-release all the interfaces. | 748 // Attempt-release all the interfaces. |
| 758 // It will be retained until the transfer cancellation is finished. | 749 // It will be retained until the transfer cancellation is finished. |
| 759 claimed_interfaces_.clear(); | 750 claimed_interfaces_.clear(); |
| 760 | 751 |
| 761 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 752 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
| 762 // finish. | 753 // finish. |
| 763 device_ = NULL; | 754 device_ = NULL; |
| 764 } | 755 } |
| 765 | 756 |
| 766 } // namespace usb_service | 757 } // namespace usb_service |
| OLD | NEW |