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/macros.h" |
12 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
15 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
17 #include "components/device_event_log/device_event_log.h" | 18 #include "components/device_event_log/device_event_log.h" |
18 #include "device/usb/usb_context.h" | 19 #include "device/usb/usb_context.h" |
19 #include "device/usb/usb_descriptors.h" | 20 #include "device/usb/usb_descriptors.h" |
20 #include "device/usb/usb_device_impl.h" | 21 #include "device/usb/usb_device_impl.h" |
21 #include "device/usb/usb_error.h" | 22 #include "device/usb/usb_error.h" |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 scoped_ptr<Transfer> transfer(new Transfer( | 249 scoped_ptr<Transfer> transfer(new Transfer( |
249 device_handle, nullptr, USB_TRANSFER_CONTROL, buffer, | 250 device_handle, nullptr, USB_TRANSFER_CONTROL, buffer, |
250 length + LIBUSB_CONTROL_SETUP_SIZE, callback_task_runner, callback)); | 251 length + LIBUSB_CONTROL_SETUP_SIZE, callback_task_runner, callback)); |
251 | 252 |
252 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 253 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
253 if (!transfer->platform_transfer_) { | 254 if (!transfer->platform_transfer_) { |
254 USB_LOG(ERROR) << "Failed to allocate control transfer."; | 255 USB_LOG(ERROR) << "Failed to allocate control transfer."; |
255 return nullptr; | 256 return nullptr; |
256 } | 257 } |
257 | 258 |
258 libusb_fill_control_setup(reinterpret_cast<uint8*>(buffer->data()), type, | 259 libusb_fill_control_setup(reinterpret_cast<uint8_t*>(buffer->data()), type, |
259 request, value, index, length); | 260 request, value, index, length); |
260 libusb_fill_control_transfer(transfer->platform_transfer_, | 261 libusb_fill_control_transfer(transfer->platform_transfer_, |
261 device_handle->handle_, | 262 device_handle->handle_, |
262 reinterpret_cast<uint8*>(buffer->data()), | 263 reinterpret_cast<uint8_t*>(buffer->data()), |
263 &UsbDeviceHandleImpl::Transfer::PlatformCallback, | 264 &UsbDeviceHandleImpl::Transfer::PlatformCallback, |
264 transfer.get(), timeout); | 265 transfer.get(), timeout); |
265 | 266 |
266 return transfer.Pass(); | 267 return transfer.Pass(); |
267 } | 268 } |
268 | 269 |
269 // static | 270 // static |
270 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 271 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
271 UsbDeviceHandleImpl::Transfer::CreateBulkTransfer( | 272 UsbDeviceHandleImpl::Transfer::CreateBulkTransfer( |
272 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 273 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
273 uint8_t endpoint, | 274 uint8_t endpoint, |
274 scoped_refptr<net::IOBuffer> buffer, | 275 scoped_refptr<net::IOBuffer> buffer, |
275 int length, | 276 int length, |
276 unsigned int timeout, | 277 unsigned int timeout, |
277 scoped_refptr<base::TaskRunner> callback_task_runner, | 278 scoped_refptr<base::TaskRunner> callback_task_runner, |
278 const TransferCallback& callback) { | 279 const TransferCallback& callback) { |
279 scoped_ptr<Transfer> transfer(new Transfer( | 280 scoped_ptr<Transfer> transfer(new Transfer( |
280 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), | 281 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), |
281 USB_TRANSFER_BULK, buffer, length, callback_task_runner, callback)); | 282 USB_TRANSFER_BULK, buffer, length, callback_task_runner, callback)); |
282 | 283 |
283 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 284 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
284 if (!transfer->platform_transfer_) { | 285 if (!transfer->platform_transfer_) { |
285 USB_LOG(ERROR) << "Failed to allocate bulk transfer."; | 286 USB_LOG(ERROR) << "Failed to allocate bulk transfer."; |
286 return nullptr; | 287 return nullptr; |
287 } | 288 } |
288 | 289 |
289 libusb_fill_bulk_transfer( | 290 libusb_fill_bulk_transfer( |
290 transfer->platform_transfer_, device_handle->handle_, endpoint, | 291 transfer->platform_transfer_, device_handle->handle_, endpoint, |
291 reinterpret_cast<uint8*>(buffer->data()), static_cast<int>(length), | 292 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), |
292 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), | 293 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), |
293 timeout); | 294 timeout); |
294 | 295 |
295 return transfer.Pass(); | 296 return transfer.Pass(); |
296 } | 297 } |
297 | 298 |
298 // static | 299 // static |
299 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 300 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
300 UsbDeviceHandleImpl::Transfer::CreateInterruptTransfer( | 301 UsbDeviceHandleImpl::Transfer::CreateInterruptTransfer( |
301 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 302 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
302 uint8_t endpoint, | 303 uint8_t endpoint, |
303 scoped_refptr<net::IOBuffer> buffer, | 304 scoped_refptr<net::IOBuffer> buffer, |
304 int length, | 305 int length, |
305 unsigned int timeout, | 306 unsigned int timeout, |
306 scoped_refptr<base::TaskRunner> callback_task_runner, | 307 scoped_refptr<base::TaskRunner> callback_task_runner, |
307 const TransferCallback& callback) { | 308 const TransferCallback& callback) { |
308 scoped_ptr<Transfer> transfer(new Transfer( | 309 scoped_ptr<Transfer> transfer(new Transfer( |
309 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), | 310 device_handle, device_handle->GetClaimedInterfaceForEndpoint(endpoint), |
310 USB_TRANSFER_INTERRUPT, buffer, length, callback_task_runner, callback)); | 311 USB_TRANSFER_INTERRUPT, buffer, length, callback_task_runner, callback)); |
311 | 312 |
312 transfer->platform_transfer_ = libusb_alloc_transfer(0); | 313 transfer->platform_transfer_ = libusb_alloc_transfer(0); |
313 if (!transfer->platform_transfer_) { | 314 if (!transfer->platform_transfer_) { |
314 USB_LOG(ERROR) << "Failed to allocate interrupt transfer."; | 315 USB_LOG(ERROR) << "Failed to allocate interrupt transfer."; |
315 return nullptr; | 316 return nullptr; |
316 } | 317 } |
317 | 318 |
318 libusb_fill_interrupt_transfer( | 319 libusb_fill_interrupt_transfer( |
319 transfer->platform_transfer_, device_handle->handle_, endpoint, | 320 transfer->platform_transfer_, device_handle->handle_, endpoint, |
320 reinterpret_cast<uint8*>(buffer->data()), static_cast<int>(length), | 321 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), |
321 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), | 322 &UsbDeviceHandleImpl::Transfer::PlatformCallback, transfer.get(), |
322 timeout); | 323 timeout); |
323 | 324 |
324 return transfer.Pass(); | 325 return transfer.Pass(); |
325 } | 326 } |
326 | 327 |
327 // static | 328 // static |
328 scoped_ptr<UsbDeviceHandleImpl::Transfer> | 329 scoped_ptr<UsbDeviceHandleImpl::Transfer> |
329 UsbDeviceHandleImpl::Transfer::CreateIsochronousTransfer( | 330 UsbDeviceHandleImpl::Transfer::CreateIsochronousTransfer( |
330 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 331 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
(...skipping 14 matching lines...) Expand all Loading... |
345 callback)); | 346 callback)); |
346 | 347 |
347 transfer->platform_transfer_ = libusb_alloc_transfer(packets); | 348 transfer->platform_transfer_ = libusb_alloc_transfer(packets); |
348 if (!transfer->platform_transfer_) { | 349 if (!transfer->platform_transfer_) { |
349 USB_LOG(ERROR) << "Failed to allocate isochronous transfer."; | 350 USB_LOG(ERROR) << "Failed to allocate isochronous transfer."; |
350 return nullptr; | 351 return nullptr; |
351 } | 352 } |
352 | 353 |
353 libusb_fill_iso_transfer( | 354 libusb_fill_iso_transfer( |
354 transfer->platform_transfer_, device_handle->handle_, endpoint, | 355 transfer->platform_transfer_, device_handle->handle_, endpoint, |
355 reinterpret_cast<uint8*>(buffer->data()), static_cast<int>(length), | 356 reinterpret_cast<uint8_t*>(buffer->data()), static_cast<int>(length), |
356 packets, &Transfer::PlatformCallback, transfer.get(), timeout); | 357 packets, &Transfer::PlatformCallback, transfer.get(), timeout); |
357 libusb_set_iso_packet_lengths(transfer->platform_transfer_, packet_length); | 358 libusb_set_iso_packet_lengths(transfer->platform_transfer_, packet_length); |
358 | 359 |
359 return transfer.Pass(); | 360 return transfer.Pass(); |
360 } | 361 } |
361 | 362 |
362 UsbDeviceHandleImpl::Transfer::Transfer( | 363 UsbDeviceHandleImpl::Transfer::Transfer( |
363 scoped_refptr<UsbDeviceHandleImpl> device_handle, | 364 scoped_refptr<UsbDeviceHandleImpl> device_handle, |
364 scoped_refptr<InterfaceClaimer> claimed_interface, | 365 scoped_refptr<InterfaceClaimer> claimed_interface, |
365 UsbTransferType transfer_type, | 366 UsbTransferType transfer_type, |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1009 // Attempt-release all the interfaces. | 1010 // Attempt-release all the interfaces. |
1010 // It will be retained until the transfer cancellation is finished. | 1011 // It will be retained until the transfer cancellation is finished. |
1011 claimed_interfaces_.clear(); | 1012 claimed_interfaces_.clear(); |
1012 | 1013 |
1013 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 1014 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
1014 // finish. | 1015 // finish. |
1015 device_ = NULL; | 1016 device_ = NULL; |
1016 } | 1017 } |
1017 | 1018 |
1018 } // namespace device | 1019 } // namespace device |
OLD | NEW |