| 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 <utility> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/location.h" | 12 #include "base/location.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 15 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 17 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 } | 258 } |
| 258 | 259 |
| 259 libusb_fill_control_setup(reinterpret_cast<uint8_t*>(buffer->data()), type, | 260 libusb_fill_control_setup(reinterpret_cast<uint8_t*>(buffer->data()), type, |
| 260 request, value, index, length); | 261 request, value, index, length); |
| 261 libusb_fill_control_transfer(transfer->platform_transfer_, | 262 libusb_fill_control_transfer(transfer->platform_transfer_, |
| 262 device_handle->handle_, | 263 device_handle->handle_, |
| 263 reinterpret_cast<uint8_t*>(buffer->data()), | 264 reinterpret_cast<uint8_t*>(buffer->data()), |
| 264 &UsbDeviceHandleImpl::Transfer::PlatformCallback, | 265 &UsbDeviceHandleImpl::Transfer::PlatformCallback, |
| 265 transfer.get(), timeout); | 266 transfer.get(), timeout); |
| 266 | 267 |
| 267 return transfer.Pass(); | 268 return transfer; |
| 268 } | 269 } |
| 269 | 270 |
| 270 // static | 271 // static |
| 271 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 272 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
| 272 UsbDeviceHandleImpl::Transfer::CreateBulkTransfer( | 273 UsbDeviceHandleImpl::Transfer::CreateBulkTransfer( |
| 273 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 274 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
| 274 uint8_t endpoint, | 275 uint8_t endpoint, |
| 275 scoped_refptr<net::IOBuffer> buffer, | 276 scoped_refptr<net::IOBuffer> buffer, |
| 276 int length, | 277 int length, |
| 277 unsigned int timeout, | 278 unsigned int timeout, |
| 278 scoped_refptr<base::TaskRunner> callback_task_runner, | 279 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 279 const TransferCallback& callback) { | 280 const TransferCallback& callback) { |
| 280 scoped_ptr<Transfer> transfer(new Transfer( | 281 scoped_ptr<Transfer> transfer(new Transfer( |
| 281 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), | 282 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), |
| 282 USB_TRANSFER_BULK, buffer, length, callback_task_runner, callback)); | 283 USB_TRANSFER_BULK, buffer, length, callback_task_runner, callback)); |
| 283 | 284 |
| 284 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 285 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
| 285 if (!transfer->platform_transfer_) { | 286 if (!transfer->platform_transfer_) { |
| 286 USB_LOG(ERROR) << "Failed to allocate bulk transfer."; | 287 USB_LOG(ERROR) << "Failed to allocate bulk transfer."; |
| 287 return nullptr; | 288 return nullptr; |
| 288 } | 289 } |
| 289 | 290 |
| 290 libusb_fill_bulk_transfer( | 291 libusb_fill_bulk_transfer( |
| 291 transfer->platform_transfer_, device_handle->handle_, endpoint, | 292 transfer->platform_transfer_, device_handle->handle_, endpoint, |
| 292 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), | 293 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), |
| 293 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), | 294 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), |
| 294 timeout); | 295 timeout); |
| 295 | 296 |
| 296 return transfer.Pass(); | 297 return transfer; |
| 297 } | 298 } |
| 298 | 299 |
| 299 // static | 300 // static |
| 300 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 301 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
| 301 UsbDeviceHandleImpl::Transfer::CreateInterruptTransfer( | 302 UsbDeviceHandleImpl::Transfer::CreateInterruptTransfer( |
| 302 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 303 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
| 303 uint8_t endpoint, | 304 uint8_t endpoint, |
| 304 scoped_refptr<net::IOBuffer> buffer, | 305 scoped_refptr<net::IOBuffer> buffer, |
| 305 int length, | 306 int length, |
| 306 unsigned int timeout, | 307 unsigned int timeout, |
| 307 scoped_refptr<base::TaskRunner> callback_task_runner, | 308 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 308 const TransferCallback& callback) { | 309 const TransferCallback& callback) { |
| 309 scoped_ptr<Transfer> transfer(new Transfer( | 310 scoped_ptr<Transfer> transfer(new Transfer( |
| 310 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), | 311 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), |
| 311 USB_TRANSFER_INTERRUPT, buffer, length, callback_task_runner, callback)); | 312 USB_TRANSFER_INTERRUPT, buffer, length, callback_task_runner, callback)); |
| 312 | 313 |
| 313 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 314 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
| 314 if (!transfer->platform_transfer_) { | 315 if (!transfer->platform_transfer_) { |
| 315 USB_LOG(ERROR) << "Failed to allocate interrupt transfer."; | 316 USB_LOG(ERROR) << "Failed to allocate interrupt transfer."; |
| 316 return nullptr; | 317 return nullptr; |
| 317 } | 318 } |
| 318 | 319 |
| 319 libusb_fill_interrupt_transfer( | 320 libusb_fill_interrupt_transfer( |
| 320 transfer->platform_transfer_, device_handle->handle_, endpoint, | 321 transfer->platform_transfer_, device_handle->handle_, endpoint, |
| 321 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), | 322 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), |
| 322 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), | 323 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), |
| 323 timeout); | 324 timeout); |
| 324 | 325 |
| 325 return transfer.Pass(); | 326 return transfer; |
| 326 } | 327 } |
| 327 | 328 |
| 328 // static | 329 // static |
| 329 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 330 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
| 330 UsbDeviceHandleImpl::Transfer::CreateIsochronousTransfer( | 331 UsbDeviceHandleImpl::Transfer::CreateIsochronousTransfer( |
| 331 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 332 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
| 332 uint8_t endpoint, | 333 uint8_t endpoint, |
| 333 scoped_refptr<net::IOBuffer> buffer, | 334 scoped_refptr<net::IOBuffer> buffer, |
| 334 size_t length, | 335 size_t length, |
| 335 unsigned int packets, | 336 unsigned int packets, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 350 USB_LOG(ERROR) << "Failed to allocate isochronous transfer."; | 351 USB_LOG(ERROR) << "Failed to allocate isochronous transfer."; |
| 351 return nullptr; | 352 return nullptr; |
| 352 } | 353 } |
| 353 | 354 |
| 354 libusb_fill_iso_transfer( | 355 libusb_fill_iso_transfer( |
| 355 transfer->platform_transfer_, device_handle->handle_, endpoint, | 356 transfer->platform_transfer_, device_handle->handle_, endpoint, |
| 356 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), | 357 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), |
| 357 packets, &Transfer::PlatformCallback, transfer.get(), timeout); | 358 packets, &Transfer::PlatformCallback, transfer.get(), timeout); |
| 358 libusb_set_iso_packet_lengths(transfer->platform_transfer_, packet_length); | 359 libusb_set_iso_packet_lengths(transfer->platform_transfer_, packet_length); |
| 359 | 360 |
| 360 return transfer.Pass(); | 361 return transfer; |
| 361 } | 362 } |
| 362 | 363 |
| 363 UsbDeviceHandleImpl::Transfer::Transfer( | 364 UsbDeviceHandleImpl::Transfer::Transfer( |
| 364 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 365 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
| 365 scoped_refptr<InterfaceClaimer> claimed_interface, | 366 scoped_refptr<InterfaceClaimer> claimed_interface, |
| 366 UsbTransferType transfer_type, | 367 UsbTransferType transfer_type, |
| 367 scoped_refptr<net::IOBuffer> buffer, | 368 scoped_refptr<net::IOBuffer> buffer, |
| 368 size_t length, | 369 size_t length, |
| 369 scoped_refptr<base::TaskRunner> callback_task_runner, | 370 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 370 const TransferCallback& callback) | 371 const TransferCallback& callback) |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 874 scoped_ptr<Transfer> transfer = Transfer::CreateControlTransfer( | 875 scoped_ptr<Transfer> transfer = Transfer::CreateControlTransfer( |
| 875 this, CreateRequestType(direction, request_type, recipient), request, | 876 this, CreateRequestType(direction, request_type, recipient), request, |
| 876 value, index, static_cast<uint16_t>(length), resized_buffer, timeout, | 877 value, index, static_cast<uint16_t>(length), resized_buffer, timeout, |
| 877 callback_task_runner, callback); | 878 callback_task_runner, callback); |
| 878 if (!transfer) { | 879 if (!transfer) { |
| 879 RunTransferCallback(callback_task_runner, callback, USB_TRANSFER_ERROR, | 880 RunTransferCallback(callback_task_runner, callback, USB_TRANSFER_ERROR, |
| 880 buffer, 0); | 881 buffer, 0); |
| 881 return; | 882 return; |
| 882 } | 883 } |
| 883 | 884 |
| 884 SubmitTransfer(transfer.Pass()); | 885 SubmitTransfer(std::move(transfer)); |
| 885 } | 886 } |
| 886 | 887 |
| 887 void UsbDeviceHandleImpl::IsochronousTransferInternal( | 888 void UsbDeviceHandleImpl::IsochronousTransferInternal( |
| 888 uint8_t endpoint_address, | 889 uint8_t endpoint_address, |
| 889 scoped_refptr<net::IOBuffer> buffer, | 890 scoped_refptr<net::IOBuffer> buffer, |
| 890 size_t length, | 891 size_t length, |
| 891 unsigned int packets, | 892 unsigned int packets, |
| 892 unsigned int packet_length, | 893 unsigned int packet_length, |
| 893 unsigned int timeout, | 894 unsigned int timeout, |
| 894 scoped_refptr<base::TaskRunner> callback_task_runner, | 895 scoped_refptr<base::TaskRunner> callback_task_runner, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 905 USB_LOG(USER) << "Transfer too long."; | 906 USB_LOG(USER) << "Transfer too long."; |
| 906 RunTransferCallback(callback_task_runner, callback, USB_TRANSFER_ERROR, | 907 RunTransferCallback(callback_task_runner, callback, USB_TRANSFER_ERROR, |
| 907 buffer, 0); | 908 buffer, 0); |
| 908 return; | 909 return; |
| 909 } | 910 } |
| 910 | 911 |
| 911 scoped_ptr<Transfer> transfer = Transfer::CreateIsochronousTransfer( | 912 scoped_ptr<Transfer> transfer = Transfer::CreateIsochronousTransfer( |
| 912 this, endpoint_address, buffer, static_cast<int>(length), packets, | 913 this, endpoint_address, buffer, static_cast<int>(length), packets, |
| 913 packet_length, timeout, callback_task_runner, callback); | 914 packet_length, timeout, callback_task_runner, callback); |
| 914 | 915 |
| 915 SubmitTransfer(transfer.Pass()); | 916 SubmitTransfer(std::move(transfer)); |
| 916 } | 917 } |
| 917 | 918 |
| 918 void UsbDeviceHandleImpl::GenericTransferInternal( | 919 void UsbDeviceHandleImpl::GenericTransferInternal( |
| 919 uint8_t endpoint_address, | 920 uint8_t endpoint_address, |
| 920 scoped_refptr<net::IOBuffer> buffer, | 921 scoped_refptr<net::IOBuffer> buffer, |
| 921 size_t length, | 922 size_t length, |
| 922 unsigned int timeout, | 923 unsigned int timeout, |
| 923 scoped_refptr<base::TaskRunner> callback_task_runner, | 924 scoped_refptr<base::TaskRunner> callback_task_runner, |
| 924 const TransferCallback& callback) { | 925 const TransferCallback& callback) { |
| 925 DCHECK(thread_checker_.CalledOnValidThread()); | 926 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 958 this, endpoint_address, buffer, static_cast<int>(length), timeout, | 959 this, endpoint_address, buffer, static_cast<int>(length), timeout, |
| 959 callback_task_runner, callback); | 960 callback_task_runner, callback); |
| 960 } else { | 961 } else { |
| 961 USB_LOG(DEBUG) << "Endpoint " << static_cast<int>(endpoint_address) | 962 USB_LOG(DEBUG) << "Endpoint " << static_cast<int>(endpoint_address) |
| 962 << " is not a bulk or interrupt endpoint."; | 963 << " is not a bulk or interrupt endpoint."; |
| 963 RunTransferCallback(callback_task_runner, callback, USB_TRANSFER_ERROR, | 964 RunTransferCallback(callback_task_runner, callback, USB_TRANSFER_ERROR, |
| 964 buffer, 0); | 965 buffer, 0); |
| 965 return; | 966 return; |
| 966 } | 967 } |
| 967 | 968 |
| 968 SubmitTransfer(transfer.Pass()); | 969 SubmitTransfer(std::move(transfer)); |
| 969 } | 970 } |
| 970 | 971 |
| 971 void UsbDeviceHandleImpl::SubmitTransfer(scoped_ptr<Transfer> transfer) { | 972 void UsbDeviceHandleImpl::SubmitTransfer(scoped_ptr<Transfer> transfer) { |
| 972 DCHECK(thread_checker_.CalledOnValidThread()); | 973 DCHECK(thread_checker_.CalledOnValidThread()); |
| 973 | 974 |
| 974 // Transfer is owned by libusb until its completion callback is run. This | 975 // Transfer is owned by libusb until its completion callback is run. This |
| 975 // object holds a weak reference. | 976 // object holds a weak reference. |
| 976 transfers_.insert(transfer.get()); | 977 transfers_.insert(transfer.get()); |
| 977 blocking_task_runner_->PostTask( | 978 blocking_task_runner_->PostTask( |
| 978 FROM_HERE, | 979 FROM_HERE, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1010 // Attempt-release all the interfaces. | 1011 // Attempt-release all the interfaces. |
| 1011 // It will be retained until the transfer cancellation is finished. | 1012 // It will be retained until the transfer cancellation is finished. |
| 1012 claimed_interfaces_.clear(); | 1013 claimed_interfaces_.clear(); |
| 1013 | 1014 |
| 1014 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 1015 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
| 1015 // finish. | 1016 // finish. |
| 1016 device_ = NULL; | 1017 device_ = NULL; |
| 1017 } | 1018 } |
| 1018 | 1019 |
| 1019 } // namespace device | 1020 } // namespace device |
| OLD | NEW |