Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / usb / usb_interface.cc
blobd5a1e0fcdff74a96e098d89560452a9b66934289
1 // Copyright (c) 2012 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 "chrome/browser/usb/usb_interface.h"
7 #include "base/logging.h"
8 #include "third_party/libusb/src/libusb/libusb.h"
10 UsbEndpointDescriptor::UsbEndpointDescriptor(
11 scoped_refptr<const UsbConfigDescriptor> config,
12 PlatformUsbEndpointDescriptor descriptor)
13 : config_(config), descriptor_(descriptor) {
16 UsbEndpointDescriptor::~UsbEndpointDescriptor() {}
18 int UsbEndpointDescriptor::GetAddress() const {
19 return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
22 UsbEndpointDirection UsbEndpointDescriptor::GetDirection() const {
23 switch (descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
24 case LIBUSB_ENDPOINT_IN:
25 return USB_DIRECTION_INBOUND;
26 case LIBUSB_ENDPOINT_OUT:
27 return USB_DIRECTION_OUTBOUND;
28 default:
29 NOTREACHED();
30 return USB_DIRECTION_INBOUND;
34 int UsbEndpointDescriptor::GetMaximumPacketSize() const {
35 return descriptor_->wMaxPacketSize;
38 UsbSynchronizationType UsbEndpointDescriptor::GetSynchronizationType() const {
39 switch (descriptor_->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
40 case LIBUSB_ISO_SYNC_TYPE_NONE:
41 return USB_SYNCHRONIZATION_NONE;
42 case LIBUSB_ISO_SYNC_TYPE_ASYNC:
43 return USB_SYNCHRONIZATION_ASYNCHRONOUS;
44 case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
45 return USB_SYNCHRONIZATION_ADAPTIVE;
46 case LIBUSB_ISO_SYNC_TYPE_SYNC:
47 return USB_SYNCHRONIZATION_SYNCHRONOUS;
48 default:
49 NOTREACHED();
50 return USB_SYNCHRONIZATION_NONE;
54 UsbTransferType UsbEndpointDescriptor::GetTransferType() const {
55 switch (descriptor_->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
56 case LIBUSB_TRANSFER_TYPE_CONTROL:
57 return USB_TRANSFER_CONTROL;
58 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
59 return USB_TRANSFER_ISOCHRONOUS;
60 case LIBUSB_TRANSFER_TYPE_BULK:
61 return USB_TRANSFER_BULK;
62 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
63 return USB_TRANSFER_INTERRUPT;
64 default:
65 NOTREACHED();
66 return USB_TRANSFER_CONTROL;
70 UsbUsageType UsbEndpointDescriptor::GetUsageType() const {
71 switch (descriptor_->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
72 case LIBUSB_ISO_USAGE_TYPE_DATA:
73 return USB_USAGE_DATA;
74 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
75 return USB_USAGE_FEEDBACK;
76 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
77 return USB_USAGE_EXPLICIT_FEEDBACK;
78 default:
79 NOTREACHED();
80 return USB_USAGE_DATA;
84 int UsbEndpointDescriptor::GetPollingInterval() const {
85 return descriptor_->bInterval;
88 UsbInterfaceAltSettingDescriptor::UsbInterfaceAltSettingDescriptor(
89 scoped_refptr<const UsbConfigDescriptor> config,
90 PlatformUsbInterfaceDescriptor descriptor)
91 : config_(config), descriptor_(descriptor) {
94 UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() {}
96 size_t UsbInterfaceAltSettingDescriptor::GetNumEndpoints() const {
97 return descriptor_->bNumEndpoints;
100 scoped_refptr<const UsbEndpointDescriptor>
101 UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index) const {
102 return new UsbEndpointDescriptor(config_, &descriptor_->endpoint[index]);
105 int UsbInterfaceAltSettingDescriptor::GetInterfaceNumber() const {
106 return descriptor_->bInterfaceNumber;
109 int UsbInterfaceAltSettingDescriptor::GetAlternateSetting() const {
110 return descriptor_->bAlternateSetting;
113 int UsbInterfaceAltSettingDescriptor::GetInterfaceClass() const {
114 return descriptor_->bInterfaceClass;
117 int UsbInterfaceAltSettingDescriptor::GetInterfaceSubclass() const {
118 return descriptor_->bInterfaceSubClass;
121 int UsbInterfaceAltSettingDescriptor::GetInterfaceProtocol() const {
122 return descriptor_->bInterfaceProtocol;
125 UsbInterfaceDescriptor::UsbInterfaceDescriptor(
126 scoped_refptr<const UsbConfigDescriptor> config,
127 PlatformUsbInterface usbInterface)
128 : config_(config), interface_(usbInterface) {
131 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() {}
133 size_t UsbInterfaceDescriptor::GetNumAltSettings() const {
134 return interface_->num_altsetting;
137 scoped_refptr<const UsbInterfaceAltSettingDescriptor>
138 UsbInterfaceDescriptor::GetAltSetting(size_t index) const {
139 return new UsbInterfaceAltSettingDescriptor(config_,
140 &interface_->altsetting[index]);
143 UsbConfigDescriptor::UsbConfigDescriptor(PlatformUsbConfigDescriptor config)
144 : config_(config) {
147 UsbConfigDescriptor::~UsbConfigDescriptor() {
148 if (config_ != NULL) {
149 libusb_free_config_descriptor(config_);
150 config_ = NULL;
154 size_t UsbConfigDescriptor::GetNumInterfaces() const {
155 return config_->bNumInterfaces;
158 scoped_refptr<const UsbInterfaceDescriptor>
159 UsbConfigDescriptor::GetInterface(size_t index) const {
160 return new UsbInterfaceDescriptor(this, &config_->interface[index]);