Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / usb_service / usb_interface_impl.cc
blob1eb1b2fcdabdb30eafeb348c586971406923dbd4
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_impl.h"
7 #include "base/logging.h"
8 #include "third_party/libusb/src/libusb/libusb.h"
10 namespace usb_service {
12 UsbEndpointDescriptorImpl::UsbEndpointDescriptorImpl(
13 scoped_refptr<const UsbConfigDescriptor> config,
14 PlatformUsbEndpointDescriptor descriptor)
15 : config_(config), descriptor_(descriptor) {
18 UsbEndpointDescriptorImpl::~UsbEndpointDescriptorImpl() {
21 int UsbEndpointDescriptorImpl::GetAddress() const {
22 return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
25 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;
37 int UsbEndpointDescriptorImpl::GetMaximumPacketSize() const {
38 return descriptor_->wMaxPacketSize;
41 UsbSynchronizationType
42 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;
58 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;
74 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;
88 int UsbEndpointDescriptorImpl::GetPollingInterval() const {
89 return descriptor_->bInterval;
92 UsbInterfaceAltSettingDescriptorImpl::UsbInterfaceAltSettingDescriptorImpl(
93 scoped_refptr<const UsbConfigDescriptor> config,
94 PlatformUsbInterfaceDescriptor descriptor)
95 : config_(config), descriptor_(descriptor) {
98 UsbInterfaceAltSettingDescriptorImpl::~UsbInterfaceAltSettingDescriptorImpl() {
101 size_t UsbInterfaceAltSettingDescriptorImpl::GetNumEndpoints() const {
102 return descriptor_->bNumEndpoints;
105 scoped_refptr<const UsbEndpointDescriptor>
106 UsbInterfaceAltSettingDescriptorImpl::GetEndpoint(size_t index) const {
107 return new UsbEndpointDescriptorImpl(config_, &descriptor_->endpoint[index]);
110 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceNumber() const {
111 return descriptor_->bInterfaceNumber;
114 int UsbInterfaceAltSettingDescriptorImpl::GetAlternateSetting() const {
115 return descriptor_->bAlternateSetting;
118 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceClass() const {
119 return descriptor_->bInterfaceClass;
122 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceSubclass() const {
123 return descriptor_->bInterfaceSubClass;
126 int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceProtocol() const {
127 return descriptor_->bInterfaceProtocol;
130 UsbInterfaceDescriptorImpl::UsbInterfaceDescriptorImpl(
131 scoped_refptr<const UsbConfigDescriptor> config,
132 PlatformUsbInterface usbInterface)
133 : config_(config), interface_(usbInterface) {
136 UsbInterfaceDescriptorImpl::~UsbInterfaceDescriptorImpl() {
139 size_t UsbInterfaceDescriptorImpl::GetNumAltSettings() const {
140 return interface_->num_altsetting;
143 scoped_refptr<const UsbInterfaceAltSettingDescriptor>
144 UsbInterfaceDescriptorImpl::GetAltSetting(size_t index) const {
145 return new UsbInterfaceAltSettingDescriptorImpl(
146 config_, &interface_->altsetting[index]);
149 UsbConfigDescriptorImpl::UsbConfigDescriptorImpl(
150 PlatformUsbConfigDescriptor config)
151 : config_(config) {
152 DCHECK(config);
155 UsbConfigDescriptorImpl::~UsbConfigDescriptorImpl() {
156 libusb_free_config_descriptor(config_);
159 size_t UsbConfigDescriptorImpl::GetNumInterfaces() const {
160 return config_->bNumInterfaces;
163 scoped_refptr<const UsbInterfaceDescriptor>
164 UsbConfigDescriptorImpl::GetInterface(size_t index) const {
165 return new UsbInterfaceDescriptorImpl(this, &config_->interface[index]);
168 } // namespace usb_service