usb_device_handle_impl.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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
  3. // found in the LICENSE file.
  4. #ifndef SERVICES_DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
  5. #define SERVICES_DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <map>
  9. #include <memory>
  10. #include <set>
  11. #include <vector>
  12. #include "base/callback.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/ref_counted.h"
  15. #include "base/sequence_checker.h"
  16. #include "services/device/public/mojom/usb_device.mojom.h"
  17. #include "services/device/usb/scoped_libusb_device_handle.h"
  18. #include "services/device/usb/usb_device_handle.h"
  19. #include "third_party/libusb/src/libusb/libusb.h"
  20. namespace base {
  21. class RefCountedBytes;
  22. class SequencedTaskRunner;
  23. } // namespace base
  24. namespace device {
  25. struct EndpointMapValue {
  26. raw_ptr<const mojom::UsbInterfaceInfo> interface;
  27. raw_ptr<const mojom::UsbEndpointInfo> endpoint;
  28. };
  29. class UsbDeviceImpl;
  30. typedef libusb_iso_packet_descriptor* PlatformUsbIsoPacketDescriptor;
  31. typedef libusb_transfer* PlatformUsbTransferHandle;
  32. // UsbDeviceHandle class provides basic I/O related functionalities.
  33. class UsbDeviceHandleImpl : public UsbDeviceHandle {
  34. public:
  35. UsbDeviceHandleImpl(const UsbDeviceHandleImpl&) = delete;
  36. UsbDeviceHandleImpl& operator=(const UsbDeviceHandleImpl&) = delete;
  37. scoped_refptr<UsbDevice> GetDevice() const override;
  38. void Close() override;
  39. void SetConfiguration(int configuration_value,
  40. ResultCallback callback) override;
  41. void ClaimInterface(int interface_number, ResultCallback callback) override;
  42. void ReleaseInterface(int interface_number, ResultCallback callback) override;
  43. void SetInterfaceAlternateSetting(int interface_number,
  44. int alternate_setting,
  45. ResultCallback callback) override;
  46. void ResetDevice(ResultCallback callback) override;
  47. void ClearHalt(mojom::UsbTransferDirection direction,
  48. uint8_t endpoint_number,
  49. ResultCallback callback) override;
  50. void ControlTransfer(mojom::UsbTransferDirection direction,
  51. mojom::UsbControlTransferType request_type,
  52. mojom::UsbControlTransferRecipient recipient,
  53. uint8_t request,
  54. uint16_t value,
  55. uint16_t index,
  56. scoped_refptr<base::RefCountedBytes> buffer,
  57. unsigned int timeout,
  58. TransferCallback callback) override;
  59. void IsochronousTransferIn(uint8_t endpoint,
  60. const std::vector<uint32_t>& packet_lengths,
  61. unsigned int timeout,
  62. IsochronousTransferCallback callback) override;
  63. void IsochronousTransferOut(uint8_t endpoint,
  64. scoped_refptr<base::RefCountedBytes> buffer,
  65. const std::vector<uint32_t>& packet_lengths,
  66. unsigned int timeout,
  67. IsochronousTransferCallback callback) override;
  68. void GenericTransfer(mojom::UsbTransferDirection direction,
  69. uint8_t endpoint_number,
  70. scoped_refptr<base::RefCountedBytes> buffer,
  71. unsigned int timeout,
  72. TransferCallback callback) override;
  73. const mojom::UsbInterfaceInfo* FindInterfaceByEndpoint(
  74. uint8_t endpoint_address) override;
  75. protected:
  76. friend class UsbDeviceImpl;
  77. // This constructor is called by UsbDeviceImpl.
  78. UsbDeviceHandleImpl(
  79. scoped_refptr<UsbDeviceImpl> device,
  80. ScopedLibusbDeviceHandle handle,
  81. scoped_refptr<base::SequencedTaskRunner> blocking_task_runner);
  82. ~UsbDeviceHandleImpl() override;
  83. libusb_device_handle* handle() const { return handle_.get(); }
  84. private:
  85. class InterfaceClaimer;
  86. class Transfer;
  87. void SetConfigurationBlocking(int configuration_value,
  88. ResultCallback callback);
  89. void SetConfigurationComplete(bool success, ResultCallback callback);
  90. void ClaimInterfaceBlocking(int interface_number, ResultCallback callback);
  91. void ClaimInterfaceComplete(scoped_refptr<InterfaceClaimer> interface_claimer,
  92. ResultCallback callback);
  93. void SetInterfaceAlternateSettingBlocking(int interface_number,
  94. int alternate_setting,
  95. ResultCallback callback);
  96. void SetInterfaceAlternateSettingComplete(int interface_number,
  97. int alternate_setting,
  98. bool success,
  99. ResultCallback callback);
  100. void ResetDeviceBlocking(ResultCallback callback);
  101. void ClearHaltBlocking(uint8_t endpoint_address, ResultCallback callback);
  102. // Refresh endpoint_map_ after ClaimInterface, ReleaseInterface and
  103. // SetInterfaceAlternateSetting.
  104. void RefreshEndpointMap();
  105. // Look up the claimed interface by endpoint address. Returns nullptr if an
  106. // interface containing the endpoint is not found.
  107. scoped_refptr<InterfaceClaimer> GetClaimedInterfaceForEndpoint(
  108. uint8_t endpoint_address);
  109. void ReportIsochronousTransferError(
  110. UsbDeviceHandle::IsochronousTransferCallback callback,
  111. const std::vector<uint32_t> packet_lengths,
  112. mojom::UsbTransferStatus status);
  113. // Submits a transfer and starts tracking it. Retains the buffer and copies
  114. // the completion callback until the transfer finishes, whereupon it invokes
  115. // the callback then releases the buffer.
  116. void SubmitTransfer(std::unique_ptr<Transfer> transfer);
  117. // Removes the transfer from the in-flight transfer set and invokes the
  118. // completion callback.
  119. void TransferComplete(Transfer* transfer, base::OnceClosure callback);
  120. scoped_refptr<UsbDeviceImpl> device_;
  121. ScopedLibusbDeviceHandle handle_;
  122. typedef std::map<int, scoped_refptr<InterfaceClaimer>> ClaimedInterfaceMap;
  123. ClaimedInterfaceMap claimed_interfaces_;
  124. // This set holds weak pointers to pending transfers.
  125. std::set<Transfer*> transfers_;
  126. // A map from endpoints to EndpointMapValue
  127. typedef std::map<int, EndpointMapValue> EndpointMap;
  128. EndpointMap endpoint_map_;
  129. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  130. scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
  131. SEQUENCE_CHECKER(sequence_checker_);
  132. };
  133. } // namespace device
  134. #endif // SERVICES_DEVICE_USB_USB_DEVICE_HANDLE_IMPL_H_