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.
5 #include "components/usb_service/usb_interface.h"
7 #include "base/logging.h"
8 #include "third_party/libusb/src/libusb/libusb.h"
10 namespace usb_service
{
12 UsbEndpointDescriptor::UsbEndpointDescriptor(
13 scoped_refptr
<const UsbConfigDescriptor
> config
,
14 PlatformUsbEndpointDescriptor descriptor
)
15 : config_(config
), descriptor_(descriptor
) {
18 UsbEndpointDescriptor::~UsbEndpointDescriptor() {
21 int UsbEndpointDescriptor::GetAddress() const {
22 return descriptor_
->bEndpointAddress
& LIBUSB_ENDPOINT_ADDRESS_MASK
;
25 UsbEndpointDirection
UsbEndpointDescriptor::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
;
33 return USB_DIRECTION_INBOUND
;
37 int UsbEndpointDescriptor::GetMaximumPacketSize() const {
38 return descriptor_
->wMaxPacketSize
;
41 UsbSynchronizationType
UsbEndpointDescriptor::GetSynchronizationType() const {
42 switch (descriptor_
->bmAttributes
& LIBUSB_ISO_SYNC_TYPE_MASK
) {
43 case LIBUSB_ISO_SYNC_TYPE_NONE
:
44 return USB_SYNCHRONIZATION_NONE
;
45 case LIBUSB_ISO_SYNC_TYPE_ASYNC
:
46 return USB_SYNCHRONIZATION_ASYNCHRONOUS
;
47 case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE
:
48 return USB_SYNCHRONIZATION_ADAPTIVE
;
49 case LIBUSB_ISO_SYNC_TYPE_SYNC
:
50 return USB_SYNCHRONIZATION_SYNCHRONOUS
;
53 return USB_SYNCHRONIZATION_NONE
;
57 UsbTransferType
UsbEndpointDescriptor::GetTransferType() const {
58 switch (descriptor_
->bmAttributes
& LIBUSB_TRANSFER_TYPE_MASK
) {
59 case LIBUSB_TRANSFER_TYPE_CONTROL
:
60 return USB_TRANSFER_CONTROL
;
61 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS
:
62 return USB_TRANSFER_ISOCHRONOUS
;
63 case LIBUSB_TRANSFER_TYPE_BULK
:
64 return USB_TRANSFER_BULK
;
65 case LIBUSB_TRANSFER_TYPE_INTERRUPT
:
66 return USB_TRANSFER_INTERRUPT
;
69 return USB_TRANSFER_CONTROL
;
73 UsbUsageType
UsbEndpointDescriptor::GetUsageType() const {
74 switch (descriptor_
->bmAttributes
& LIBUSB_ISO_USAGE_TYPE_MASK
) {
75 case LIBUSB_ISO_USAGE_TYPE_DATA
:
76 return USB_USAGE_DATA
;
77 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK
:
78 return USB_USAGE_FEEDBACK
;
79 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT
:
80 return USB_USAGE_EXPLICIT_FEEDBACK
;
83 return USB_USAGE_DATA
;
87 int UsbEndpointDescriptor::GetPollingInterval() const {
88 return descriptor_
->bInterval
;
91 UsbInterfaceAltSettingDescriptor::UsbInterfaceAltSettingDescriptor(
92 scoped_refptr
<const UsbConfigDescriptor
> config
,
93 PlatformUsbInterfaceDescriptor descriptor
)
94 : config_(config
), descriptor_(descriptor
) {
97 UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() {
100 size_t UsbInterfaceAltSettingDescriptor::GetNumEndpoints() const {
101 return descriptor_
->bNumEndpoints
;
104 scoped_refptr
<const UsbEndpointDescriptor
>
105 UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index
) const {
106 return new UsbEndpointDescriptor(config_
, &descriptor_
->endpoint
[index
]);
109 int UsbInterfaceAltSettingDescriptor::GetInterfaceNumber() const {
110 return descriptor_
->bInterfaceNumber
;
113 int UsbInterfaceAltSettingDescriptor::GetAlternateSetting() const {
114 return descriptor_
->bAlternateSetting
;
117 int UsbInterfaceAltSettingDescriptor::GetInterfaceClass() const {
118 return descriptor_
->bInterfaceClass
;
121 int UsbInterfaceAltSettingDescriptor::GetInterfaceSubclass() const {
122 return descriptor_
->bInterfaceSubClass
;
125 int UsbInterfaceAltSettingDescriptor::GetInterfaceProtocol() const {
126 return descriptor_
->bInterfaceProtocol
;
129 UsbInterfaceDescriptor::UsbInterfaceDescriptor(
130 scoped_refptr
<const UsbConfigDescriptor
> config
,
131 PlatformUsbInterface usbInterface
)
132 : config_(config
), interface_(usbInterface
) {
135 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() {
138 size_t UsbInterfaceDescriptor::GetNumAltSettings() const {
139 return interface_
->num_altsetting
;
142 scoped_refptr
<const UsbInterfaceAltSettingDescriptor
>
143 UsbInterfaceDescriptor::GetAltSetting(size_t index
) const {
144 return new UsbInterfaceAltSettingDescriptor(config_
,
145 &interface_
->altsetting
[index
]);
148 UsbConfigDescriptor::UsbConfigDescriptor(PlatformUsbConfigDescriptor config
)
152 UsbConfigDescriptor::~UsbConfigDescriptor() {
153 if (config_
!= NULL
) {
154 libusb_free_config_descriptor(config_
);
159 size_t UsbConfigDescriptor::GetNumInterfaces() const {
160 return config_
->bNumInterfaces
;
163 scoped_refptr
<const UsbInterfaceDescriptor
> UsbConfigDescriptor::GetInterface(
164 size_t index
) const {
165 return new UsbInterfaceDescriptor(this, &config_
->interface
[index
]);
168 } // namespace usb_service