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 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 return false; | 512 return false; |
513 } | 513 } |
514 | 514 |
515 if (desc.iSerialNumber == 0) { | 515 if (desc.iSerialNumber == 0) { |
516 return false; | 516 return false; |
517 } | 517 } |
518 | 518 |
519 return GetStringDescriptor(desc.iSerialNumber, serial); | 519 return GetStringDescriptor(desc.iSerialNumber, serial); |
520 } | 520 } |
521 | 521 |
522 void UsbDeviceHandleImpl::ControlTransfer( | 522 void UsbDeviceHandleImpl::ControlTransfer(UsbEndpointDirection direction, |
523 const UsbEndpointDirection direction, | 523 TransferRequestType request_type, |
524 const TransferRequestType request_type, | 524 TransferRecipient recipient, |
525 const TransferRecipient recipient, | 525 uint8 request, |
526 const uint8 request, | 526 uint16 value, |
527 const uint16 value, | 527 uint16 index, |
528 const uint16 index, | 528 net::IOBuffer* buffer, |
529 net::IOBuffer* buffer, | 529 size_t length, |
530 const size_t length, | 530 unsigned int timeout, |
531 const unsigned int timeout, | 531 const UsbTransferCallback& callback) { |
532 const UsbTransferCallback& callback) { | |
533 if (!device_) { | 532 if (!device_) { |
534 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 533 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
535 return; | 534 return; |
536 } | 535 } |
537 | 536 |
538 const size_t resized_length = LIBUSB_CONTROL_SETUP_SIZE + length; | 537 const size_t resized_length = LIBUSB_CONTROL_SETUP_SIZE + length; |
539 scoped_refptr<net::IOBuffer> resized_buffer( | 538 scoped_refptr<net::IOBuffer> resized_buffer( |
540 new net::IOBufferWithSize(static_cast<int>(resized_length))); | 539 new net::IOBufferWithSize(static_cast<int>(resized_length))); |
541 if (!resized_buffer.get()) { | 540 if (!resized_buffer.get()) { |
542 callback.Run(USB_TRANSFER_ERROR, buffer, 0); | 541 callback.Run(USB_TRANSFER_ERROR, buffer, 0); |
(...skipping 19 matching lines...) Expand all Loading... |
562 this, | 561 this, |
563 timeout); | 562 timeout); |
564 | 563 |
565 PostOrSubmitTransfer(transfer, | 564 PostOrSubmitTransfer(transfer, |
566 USB_TRANSFER_CONTROL, | 565 USB_TRANSFER_CONTROL, |
567 resized_buffer.get(), | 566 resized_buffer.get(), |
568 resized_length, | 567 resized_length, |
569 callback); | 568 callback); |
570 } | 569 } |
571 | 570 |
572 void UsbDeviceHandleImpl::BulkTransfer(const UsbEndpointDirection direction, | 571 void UsbDeviceHandleImpl::BulkTransfer(UsbEndpointDirection direction, |
573 const uint8 endpoint, | 572 uint8 endpoint, |
574 net::IOBuffer* buffer, | 573 net::IOBuffer* buffer, |
575 const size_t length, | 574 size_t length, |
576 const unsigned int timeout, | 575 unsigned int timeout, |
577 const UsbTransferCallback& callback) { | 576 const UsbTransferCallback& callback) { |
578 if (!device_) { | 577 if (!device_) { |
579 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 578 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
580 return; | 579 return; |
581 } | 580 } |
582 | 581 |
583 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); | 582 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); |
584 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 583 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
585 libusb_fill_bulk_transfer(transfer, | 584 libusb_fill_bulk_transfer(transfer, |
586 handle_, | 585 handle_, |
587 new_endpoint, | 586 new_endpoint, |
588 reinterpret_cast<uint8*>(buffer->data()), | 587 reinterpret_cast<uint8*>(buffer->data()), |
589 static_cast<int>(length), | 588 static_cast<int>(length), |
590 &UsbDeviceHandleImpl::PlatformTransferCallback, | 589 &UsbDeviceHandleImpl::PlatformTransferCallback, |
591 this, | 590 this, |
592 timeout); | 591 timeout); |
593 | 592 |
594 PostOrSubmitTransfer(transfer, USB_TRANSFER_BULK, buffer, length, callback); | 593 PostOrSubmitTransfer(transfer, USB_TRANSFER_BULK, buffer, length, callback); |
595 } | 594 } |
596 | 595 |
597 void UsbDeviceHandleImpl::InterruptTransfer( | 596 void UsbDeviceHandleImpl::InterruptTransfer( |
598 const UsbEndpointDirection direction, | 597 UsbEndpointDirection direction, |
599 const uint8 endpoint, | 598 uint8 endpoint, |
600 net::IOBuffer* buffer, | 599 net::IOBuffer* buffer, |
601 const size_t length, | 600 size_t length, |
602 const unsigned int timeout, | 601 unsigned int timeout, |
603 const UsbTransferCallback& callback) { | 602 const UsbTransferCallback& callback) { |
604 if (!device_) { | 603 if (!device_) { |
605 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 604 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
606 return; | 605 return; |
607 } | 606 } |
608 | 607 |
609 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); | 608 PlatformUsbTransferHandle const transfer = libusb_alloc_transfer(0); |
610 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; | 609 const uint8 new_endpoint = ConvertTransferDirection(direction) | endpoint; |
611 libusb_fill_interrupt_transfer(transfer, | 610 libusb_fill_interrupt_transfer(transfer, |
612 handle_, | 611 handle_, |
613 new_endpoint, | 612 new_endpoint, |
614 reinterpret_cast<uint8*>(buffer->data()), | 613 reinterpret_cast<uint8*>(buffer->data()), |
615 static_cast<int>(length), | 614 static_cast<int>(length), |
616 &UsbDeviceHandleImpl::PlatformTransferCallback, | 615 &UsbDeviceHandleImpl::PlatformTransferCallback, |
617 this, | 616 this, |
618 timeout); | 617 timeout); |
619 | 618 |
620 PostOrSubmitTransfer( | 619 PostOrSubmitTransfer( |
621 transfer, USB_TRANSFER_INTERRUPT, buffer, length, callback); | 620 transfer, USB_TRANSFER_INTERRUPT, buffer, length, callback); |
622 } | 621 } |
623 | 622 |
624 void UsbDeviceHandleImpl::IsochronousTransfer( | 623 void UsbDeviceHandleImpl::IsochronousTransfer( |
625 const UsbEndpointDirection direction, | 624 UsbEndpointDirection direction, |
626 const uint8 endpoint, | 625 uint8 endpoint, |
627 net::IOBuffer* buffer, | 626 net::IOBuffer* buffer, |
628 const size_t length, | 627 size_t length, |
629 const unsigned int packets, | 628 unsigned int packets, |
630 const unsigned int packet_length, | 629 unsigned int packet_length, |
631 const unsigned int timeout, | 630 unsigned int timeout, |
632 const UsbTransferCallback& callback) { | 631 const UsbTransferCallback& callback) { |
633 if (!device_) { | 632 if (!device_) { |
634 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); | 633 callback.Run(USB_TRANSFER_DISCONNECT, buffer, 0); |
635 return; | 634 return; |
636 } | 635 } |
637 | 636 |
638 const uint64 total_length = packets * packet_length; | 637 const uint64 total_length = packets * packet_length; |
639 CHECK(packets <= length && total_length <= length) | 638 CHECK(packets <= length && total_length <= length) |
640 << "transfer length is too small"; | 639 << "transfer length is too small"; |
641 | 640 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 // Attempt-release all the interfaces. | 763 // Attempt-release all the interfaces. |
765 // It will be retained until the transfer cancellation is finished. | 764 // It will be retained until the transfer cancellation is finished. |
766 claimed_interfaces_.clear(); | 765 claimed_interfaces_.clear(); |
767 | 766 |
768 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to | 767 // Cannot close device handle here. Need to wait for libusb_cancel_transfer to |
769 // finish. | 768 // finish. |
770 device_ = NULL; | 769 device_ = NULL; |
771 } | 770 } |
772 | 771 |
773 } // namespace device | 772 } // namespace device |
OLD | NEW |