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
5 #include "components/usb_service/usb_interface_impl.h"
6
7 #include "base/logging.h"
8 #include "third_party/libusb/src/libusb/libusb.h"
9
10 namespace usb_service {
11
UsbEndpointDescriptorImpl(scoped_refptr<const UsbConfigDescriptor> config,PlatformUsbEndpointDescriptor descriptor)12 UsbEndpointDescriptorImpl::UsbEndpointDescriptorImpl(
13 scoped_refptr<const UsbConfigDescriptor> config,
14 PlatformUsbEndpointDescriptor descriptor)
15 : config_(config), descriptor_(descriptor) {
16 }
17
~UsbEndpointDescriptorImpl()18 UsbEndpointDescriptorImpl::~UsbEndpointDescriptorImpl() {
19 }
20
GetAddress() const21 int UsbEndpointDescriptorImpl::GetAddress() const {
22 return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
23 }
24
GetDirection() const25 UsbEndpointDirection UsbEndpointDescriptorImpl::GetDirection() const {
26 switch (descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
27 case LIBUSB_ENDPOINT_IN:
28 return USB_DIRECTION_INBOUND;
29 case LIBUSB_ENDPOINT_OUT:
30 return USB_DIRECTION_OUTBOUND;
31 default:
32 NOTREACHED();
33 return USB_DIRECTION_INBOUND;
34 }
35 }
36
GetMaximumPacketSize() const37 int UsbEndpointDescriptorImpl::GetMaximumPacketSize() const {
38 return descriptor_->wMaxPacketSize;
39 }
40
41 UsbSynchronizationType
GetSynchronizationType() const42 UsbEndpointDescriptorImpl::GetSynchronizationType() const {
43 switch (descriptor_->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
44 case LIBUSB_ISO_SYNC_TYPE_NONE:
45 return USB_SYNCHRONIZATION_NONE;
46 case LIBUSB_ISO_SYNC_TYPE_ASYNC:
47 return USB_SYNCHRONIZATION_ASYNCHRONOUS;
48 case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
49 return USB_SYNCHRONIZATION_ADAPTIVE;
50 case LIBUSB_ISO_SYNC_TYPE_SYNC:
51 return USB_SYNCHRONIZATION_SYNCHRONOUS;
52 default:
53 NOTREACHED();
54 return USB_SYNCHRONIZATION_NONE;
55 }
56 }
57
GetTransferType() const58 UsbTransferType UsbEndpointDescriptorImpl::GetTransferType() const {
59 switch (descriptor_->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
60 case LIBUSB_TRANSFER_TYPE_CONTROL:
61 return USB_TRANSFER_CONTROL;
62 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
63 return USB_TRANSFER_ISOCHRONOUS;
64 case LIBUSB_TRANSFER_TYPE_BULK:
65 return USB_TRANSFER_BULK;
66 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
67 return USB_TRANSFER_INTERRUPT;
68 default:
69 NOTREACHED();
70 return USB_TRANSFER_CONTROL;
71 }
72 }
73
GetUsageType() const74 UsbUsageType UsbEndpointDescriptorImpl::GetUsageType() const {
75 switch (descriptor_->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
76 case LIBUSB_ISO_USAGE_TYPE_DATA:
77 return USB_USAGE_DATA;
78 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
79 return USB_USAGE_FEEDBACK;
80 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
81 return USB_USAGE_EXPLICIT_FEEDBACK;
82 default:
83 NOTREACHED();
84 return USB_USAGE_DATA;
85 }
86 }
87
GetPollingInterval() const88 int UsbEndpointDescriptorImpl::GetPollingInterval() const {
89 return descriptor_->bInterval;
90 }
91
UsbInterfaceAltSettingDescriptorImpl(scoped_refptr<const UsbConfigDescriptor> config,PlatformUsbInterfaceDescriptor descriptor)92 UsbInterfaceAltSettingDescriptorImpl::UsbInterfaceAltSettingDescriptorImpl(
93 scoped_refptr<const UsbConfigDescriptor> config,
94 PlatformUsbInterfaceDescriptor descriptor)
95 : config_(config), descriptor_(descriptor) {
96 }
97
~UsbInterfaceAltSettingDescriptorImpl()98 UsbInterfaceAltSettingDescriptorImpl::~UsbInterfaceAltSettingDescriptorImpl() {
99 }
100
GetNumEndpoints() const101 size_t UsbInterfaceAltSettingDescriptorImpl::GetNumEndpoints() const {
102 return descriptor_->bNumEndpoints;
103 }
104
105 scoped_refptr<const UsbEndpointDescriptor>
GetEndpoint(size_t index) const106 UsbInterfaceAltSettingDescriptorImpl::GetEndpoint(size_t index) const {
107 return new UsbEndpointDescriptorImpl(config_, &descriptor_->endpoint[index]);
108 }
109
GetInterfaceNumber() const110 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceNumber() const {
111 return descriptor_->bInterfaceNumber;
112 }
113
GetAlternateSetting() const114 int UsbInterfaceAltSettingDescriptorImpl::GetAlternateSetting() const {
115 return descriptor_->bAlternateSetting;
116 }
117
GetInterfaceClass() const118 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceClass() const {
119 return descriptor_->bInterfaceClass;
120 }
121
GetInterfaceSubclass() const122 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceSubclass() const {
123 return descriptor_->bInterfaceSubClass;
124 }
125
GetInterfaceProtocol() const126 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceProtocol() const {
127 return descriptor_->bInterfaceProtocol;
128 }
129
UsbInterfaceDescriptorImpl(scoped_refptr<const UsbConfigDescriptor> config,PlatformUsbInterface usbInterface)130 UsbInterfaceDescriptorImpl::UsbInterfaceDescriptorImpl(
131 scoped_refptr<const UsbConfigDescriptor> config,
132 PlatformUsbInterface usbInterface)
133 : config_(config), interface_(usbInterface) {
134 }
135
~UsbInterfaceDescriptorImpl()136 UsbInterfaceDescriptorImpl::~UsbInterfaceDescriptorImpl() {
137 }
138
GetNumAltSettings() const139 size_t UsbInterfaceDescriptorImpl::GetNumAltSettings() const {
140 return interface_->num_altsetting;
141 }
142
143 scoped_refptr<const UsbInterfaceAltSettingDescriptor>
GetAltSetting(size_t index) const144 UsbInterfaceDescriptorImpl::GetAltSetting(size_t index) const {
145 return new UsbInterfaceAltSettingDescriptorImpl(
146 config_, &interface_->altsetting[index]);
147 }
148
UsbConfigDescriptorImpl(PlatformUsbConfigDescriptor config)149 UsbConfigDescriptorImpl::UsbConfigDescriptorImpl(
150 PlatformUsbConfigDescriptor config)
151 : config_(config) {
152 DCHECK(config);
153 }
154
~UsbConfigDescriptorImpl()155 UsbConfigDescriptorImpl::~UsbConfigDescriptorImpl() {
156 libusb_free_config_descriptor(config_);
157 }
158
GetNumInterfaces() const159 size_t UsbConfigDescriptorImpl::GetNumInterfaces() const {
160 return config_->bNumInterfaces;
161 }
162
163 scoped_refptr<const UsbInterfaceDescriptor>
GetInterface(size_t index) const164 UsbConfigDescriptorImpl::GetInterface(size_t index) const {
165 return new UsbInterfaceDescriptorImpl(this, &config_->interface[index]);
166 }
167
168 } // namespace usb_service
169