| 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 return rv == LIBUSB_SUCCESS; | 151 return rv == LIBUSB_SUCCESS; |
| 152 } | 152 } |
| 153 | 153 |
| 154 // This inner class owns the underlying libusb_transfer and may outlast | 154 // This inner class owns the underlying libusb_transfer and may outlast |
| 155 // the UsbDeviceHandle that created it. | 155 // the UsbDeviceHandle that created it. |
| 156 class UsbDeviceHandleImpl::Transfer { | 156 class UsbDeviceHandleImpl::Transfer { |
| 157 public: | 157 public: |
| 158 static scoped_ptr<Transfer> CreateControlTransfer( | 158 static scoped_ptr<Transfer> CreateControlTransfer( |
| 159 uint8 type, | 159 uint8 type, |
| 160 uint8 request, | 160 uint8 request, |
| 161 uint8 value, | 161 uint16 value, |
| 162 uint8 index, | 162 uint16 index, |
| 163 uint16 length, | 163 uint16 length, |
| 164 scoped_refptr<net::IOBuffer> buffer, | 164 scoped_refptr<net::IOBuffer> buffer, |
| 165 unsigned int timeout, | 165 unsigned int timeout, |
| 166 const UsbTransferCallback& callback); | 166 const UsbTransferCallback& callback); |
| 167 static scoped_ptr<Transfer> CreateBulkTransfer( | 167 static scoped_ptr<Transfer> CreateBulkTransfer( |
| 168 uint8 endpoint, | 168 uint8 endpoint, |
| 169 scoped_refptr<net::IOBuffer> buffer, | 169 scoped_refptr<net::IOBuffer> buffer, |
| 170 int length, | 170 int length, |
| 171 unsigned int timeout, | 171 unsigned int timeout, |
| 172 const UsbTransferCallback& callback); | 172 const UsbTransferCallback& callback); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 size_t length_; | 213 size_t length_; |
| 214 UsbTransferCallback callback_; | 214 UsbTransferCallback callback_; |
| 215 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; | 215 scoped_refptr<base::SingleThreadTaskRunner> callback_task_runner_; |
| 216 }; | 216 }; |
| 217 | 217 |
| 218 // static | 218 // static |
| 219 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 219 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
| 220 UsbDeviceHandleImpl::Transfer::CreateControlTransfer( | 220 UsbDeviceHandleImpl::Transfer::CreateControlTransfer( |
| 221 uint8 type, | 221 uint8 type, |
| 222 uint8 request, | 222 uint8 request, |
| 223 uint8 value, | 223 uint16 value, |
| 224 uint8 index, | 224 uint16 index, |
| 225 uint16 length, | 225 uint16 length, |
| 226 scoped_refptr<net::IOBuffer> buffer, | 226 scoped_refptr<net::IOBuffer> buffer, |
| 227 unsigned int timeout, | 227 unsigned int timeout, |
| 228 const UsbTransferCallback& callback) { | 228 const UsbTransferCallback& callback) { |
| 229 scoped_ptr<Transfer> transfer(new Transfer(USB_TRANSFER_CONTROL, buffer, | 229 scoped_ptr<Transfer> transfer(new Transfer(USB_TRANSFER_CONTROL, buffer, |
| 230 length + LIBUSB_CONTROL_SETUP_SIZE, | 230 length + LIBUSB_CONTROL_SETUP_SIZE, |
| 231 callback)); | 231 callback)); |
| 232 | 232 |
| 233 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 233 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
| 234 if (!transfer->platform_transfer_) { | 234 if (!transfer->platform_transfer_) { |
| 235 LOG(ERROR) << "Failed to allocate control transfer."; |
| 235 return nullptr; | 236 return nullptr; |
| 236 } | 237 } |
| 237 | 238 |
| 238 libusb_fill_control_setup(reinterpret_cast<uint8*>(buffer->data()), type, | 239 libusb_fill_control_setup(reinterpret_cast<uint8*>(buffer->data()), type, |
| 239 request, value, index, length); | 240 request, value, index, length); |
| 240 libusb_fill_control_transfer(transfer->platform_transfer_, | 241 libusb_fill_control_transfer(transfer->platform_transfer_, |
| 241 nullptr, /* filled in by Submit() */ | 242 nullptr, /* filled in by Submit() */ |
| 242 reinterpret_cast<uint8*>(buffer->data()), | 243 reinterpret_cast<uint8*>(buffer->data()), |
| 243 &UsbDeviceHandleImpl::Transfer::PlatformCallback, | 244 &UsbDeviceHandleImpl::Transfer::PlatformCallback, |
| 244 transfer.get(), timeout); | 245 transfer.get(), timeout); |
| 245 | 246 |
| 246 return transfer.Pass(); | 247 return transfer.Pass(); |
| 247 } | 248 } |
| 248 | 249 |
| 249 // static | 250 // static |
| 250 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 251 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
| 251 UsbDeviceHandleImpl::Transfer::CreateBulkTransfer( | 252 UsbDeviceHandleImpl::Transfer::CreateBulkTransfer( |
| 252 uint8 endpoint, | 253 uint8 endpoint, |
| 253 scoped_refptr<net::IOBuffer> buffer, | 254 scoped_refptr<net::IOBuffer> buffer, |
| 254 int length, | 255 int length, |
| 255 unsigned int timeout, | 256 unsigned int timeout, |
| 256 const UsbTransferCallback& callback) { | 257 const UsbTransferCallback& callback) { |
| 257 scoped_ptr<Transfer> transfer( | 258 scoped_ptr<Transfer> transfer( |
| 258 new Transfer(USB_TRANSFER_BULK, buffer, length, callback)); | 259 new Transfer(USB_TRANSFER_BULK, buffer, length, callback)); |
| 259 | 260 |
| 260 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 261 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
| 261 if (!transfer->platform_transfer_) { | 262 if (!transfer->platform_transfer_) { |
| 263 LOG(ERROR) << "Failed to allocate bulk transfer."; |
| 262 return nullptr; | 264 return nullptr; |
| 263 } | 265 } |
| 264 | 266 |
| 265 libusb_fill_bulk_transfer(transfer->platform_transfer_, | 267 libusb_fill_bulk_transfer(transfer->platform_transfer_, |
| 266 nullptr, /* filled in by Submit() */ | 268 nullptr, /* filled in by Submit() */ |
| 267 endpoint, reinterpret_cast<uint8*>(buffer->data()), | 269 endpoint, reinterpret_cast<uint8*>(buffer->data()), |
| 268 static_cast<int>(length), | 270 static_cast<int>(length), |
| 269 &UsbDeviceHandleImpl::Transfer::PlatformCallback, | 271 &UsbDeviceHandleImpl::Transfer::PlatformCallback, |
| 270 transfer.get(), timeout); | 272 transfer.get(), timeout); |
| 271 | 273 |
| 272 return transfer.Pass(); | 274 return transfer.Pass(); |
| 273 } | 275 } |
| 274 | 276 |
| 275 // static | 277 // static |
| 276 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 278 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
| 277 UsbDeviceHandleImpl::Transfer::CreateInterruptTransfer( | 279 UsbDeviceHandleImpl::Transfer::CreateInterruptTransfer( |
| 278 uint8 endpoint, | 280 uint8 endpoint, |
| 279 scoped_refptr<net::IOBuffer> buffer, | 281 scoped_refptr<net::IOBuffer> buffer, |
| 280 int length, | 282 int length, |
| 281 unsigned int timeout, | 283 unsigned int timeout, |
| 282 const UsbTransferCallback& callback) { | 284 const UsbTransferCallback& callback) { |
| 283 scoped_ptr<Transfer> transfer( | 285 scoped_ptr<Transfer> transfer( |
| 284 new Transfer(USB_TRANSFER_INTERRUPT, buffer, length, callback)); | 286 new Transfer(USB_TRANSFER_INTERRUPT, buffer, length, callback)); |
| 285 | 287 |
| 286 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 288 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
| 287 if (!transfer->platform_transfer_) { | 289 if (!transfer->platform_transfer_) { |
| 290 LOG(ERROR) << "Failed to allocate interrupt transfer."; |
| 288 return nullptr; | 291 return nullptr; |
| 289 } | 292 } |
| 290 | 293 |
| 291 libusb_fill_interrupt_transfer( | 294 libusb_fill_interrupt_transfer( |
| 292 transfer->platform_transfer_, nullptr, /* filled in by Submit() */ | 295 transfer->platform_transfer_, nullptr, /* filled in by Submit() */ |
| 293 endpoint, reinterpret_cast<uint8*>(buffer->data()), | 296 endpoint, reinterpret_cast<uint8*>(buffer->data()), |
| 294 static_cast<int>(length), | 297 static_cast<int>(length), |
| 295 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), | 298 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), |
| 296 timeout); | 299 timeout); |
| 297 | 300 |
| 298 return transfer.Pass(); | 301 return transfer.Pass(); |
| 299 } | 302 } |
| 300 | 303 |
| 301 // static | 304 // static |
| 302 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 305 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
| 303 UsbDeviceHandleImpl::Transfer::CreateIsochronousTransfer( | 306 UsbDeviceHandleImpl::Transfer::CreateIsochronousTransfer( |
| 304 uint8 endpoint, | 307 uint8 endpoint, |
| 305 scoped_refptr<net::IOBuffer> buffer, | 308 scoped_refptr<net::IOBuffer> buffer, |
| 306 size_t length, | 309 size_t length, |
| 307 unsigned int packets, | 310 unsigned int packets, |
| 308 unsigned int packet_length, | 311 unsigned int packet_length, |
| 309 unsigned int timeout, | 312 unsigned int timeout, |
| 310 const UsbTransferCallback& callback) { | 313 const UsbTransferCallback& callback) { |
| 311 DCHECK(packets <= length && (packets * packet_length) <= length) | 314 DCHECK(packets <= length && (packets * packet_length) <= length) |
| 312 << "transfer length is too small"; | 315 << "transfer length is too small"; |
| 313 | 316 |
| 314 scoped_ptr<Transfer> transfer( | 317 scoped_ptr<Transfer> transfer( |
| 315 new Transfer(USB_TRANSFER_ISOCHRONOUS, buffer, length, callback)); | 318 new Transfer(USB_TRANSFER_ISOCHRONOUS, buffer, length, callback)); |
| 316 | 319 |
| 317 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 320 transfer->platform_transfer_ = libusb_alloc_transfer(packets); |
| 318 if (!transfer->platform_transfer_) { | 321 if (!transfer->platform_transfer_) { |
| 322 LOG(ERROR) << "Failed to allocate isochronous transfer."; |
| 319 return nullptr; | 323 return nullptr; |
| 320 } | 324 } |
| 321 | 325 |
| 322 libusb_fill_iso_transfer( | 326 libusb_fill_iso_transfer( |
| 323 transfer->platform_transfer_, nullptr, /* filled in by Submit() */ | 327 transfer->platform_transfer_, nullptr, /* filled in by Submit() */ |
| 324 endpoint, reinterpret_cast<uint8*>(buffer->data()), | 328 endpoint, reinterpret_cast<uint8*>(buffer->data()), |
| 325 static_cast<int>(length), packets, &Transfer::PlatformCallback, | 329 static_cast<int>(length), packets, &Transfer::PlatformCallback, |
| 326 transfer.get(), timeout); | 330 transfer.get(), timeout); |
| 327 libusb_set_iso_packet_lengths(transfer->platform_transfer_, packet_length); | 331 libusb_set_iso_packet_lengths(transfer->platform_transfer_, packet_length); |
| 328 | 332 |
| (...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 // Attempt-release all the interfaces. | 837 // Attempt-release all the interfaces. |
| 834 // It will be retained until the transfer cancellation is finished. | 838 // It will be retained until the transfer cancellation is finished. |
| 835 claimed_interfaces_.clear(); | 839 claimed_interfaces_.clear(); |
| 836 | 840 |
| 837 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 841 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
| 838 // finish. | 842 // finish. |
| 839 device_ = NULL; | 843 device_ = NULL; |
| 840 } | 844 } |
| 841 | 845 |
| 842 } // namespace device | 846 } // namespace device |
| OLD | NEW |