Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / usb_service / usb_interface.cc
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.h"
6
7 #include "base/logging.h"
8 #include "third_party/libusb/src/libusb/libusb.h"
9
10 namespace usb_service {
11
12 UsbEndpointDescriptor::UsbEndpointDescriptor(
13     scoped_refptr<const UsbConfigDescriptor> config,
14     PlatformUsbEndpointDescriptor descriptor)
15     : config_(config), descriptor_(descriptor) {
16 }
17
18 UsbEndpointDescriptor::~UsbEndpointDescriptor() {
19 }
20
21 int UsbEndpointDescriptor::GetAddress() const {
22   return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
23 }
24
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;
31     default:
32       NOTREACHED();
33       return USB_DIRECTION_INBOUND;
34   }
35 }
36
37 int UsbEndpointDescriptor::GetMaximumPacketSize() const {
38   return descriptor_->wMaxPacketSize;
39 }
40
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;
51     default:
52       NOTREACHED();
53       return USB_SYNCHRONIZATION_NONE;
54   }
55 }
56
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;
67     default:
68       NOTREACHED();
69       return USB_TRANSFER_CONTROL;
70   }
71 }
72
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;
81     default:
82       NOTREACHED();
83       return USB_USAGE_DATA;
84   }
85 }
86
87 int UsbEndpointDescriptor::GetPollingInterval() const {
88   return descriptor_->bInterval;
89 }
90
91 UsbInterfaceAltSettingDescriptor::UsbInterfaceAltSettingDescriptor(
92     scoped_refptr<const UsbConfigDescriptor> config,
93     PlatformUsbInterfaceDescriptor descriptor)
94     : config_(config), descriptor_(descriptor) {
95 }
96
97 UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() {
98 }
99
100 size_t UsbInterfaceAltSettingDescriptor::GetNumEndpoints() const {
101   return descriptor_->bNumEndpoints;
102 }
103
104 scoped_refptr<const UsbEndpointDescriptor>
105 UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index) const {
106   return new UsbEndpointDescriptor(config_, &descriptor_->endpoint[index]);
107 }
108
109 int UsbInterfaceAltSettingDescriptor::GetInterfaceNumber() const {
110   return descriptor_->bInterfaceNumber;
111 }
112
113 int UsbInterfaceAltSettingDescriptor::GetAlternateSetting() const {
114   return descriptor_->bAlternateSetting;
115 }
116
117 int UsbInterfaceAltSettingDescriptor::GetInterfaceClass() const {
118   return descriptor_->bInterfaceClass;
119 }
120
121 int UsbInterfaceAltSettingDescriptor::GetInterfaceSubclass() const {
122   return descriptor_->bInterfaceSubClass;
123 }
124
125 int UsbInterfaceAltSettingDescriptor::GetInterfaceProtocol() const {
126   return descriptor_->bInterfaceProtocol;
127 }
128
129 UsbInterfaceDescriptor::UsbInterfaceDescriptor(
130     scoped_refptr<const UsbConfigDescriptor> config,
131     PlatformUsbInterface usbInterface)
132     : config_(config), interface_(usbInterface) {
133 }
134
135 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() {
136 }
137
138 size_t UsbInterfaceDescriptor::GetNumAltSettings() const {
139   return interface_->num_altsetting;
140 }
141
142 scoped_refptr<const UsbInterfaceAltSettingDescriptor>
143 UsbInterfaceDescriptor::GetAltSetting(size_t index) const {
144   return new UsbInterfaceAltSettingDescriptor(config_,
145                                               &interface_->altsetting[index]);
146 }
147
148 UsbConfigDescriptor::UsbConfigDescriptor(PlatformUsbConfigDescriptor config)
149     : config_(config) {
150 }
151
152 // TODO(zvorygin): Used for tests only. Should be removed when
153 // all interfaces are extracted properly.
154 UsbConfigDescriptor::UsbConfigDescriptor() {
155   config_ = NULL;
156 }
157
158 UsbConfigDescriptor::~UsbConfigDescriptor() {
159   if (config_ != NULL) {
160     libusb_free_config_descriptor(config_);
161     config_ = NULL;
162   }
163 }
164
165 size_t UsbConfigDescriptor::GetNumInterfaces() const {
166   return config_->bNumInterfaces;
167 }
168
169 scoped_refptr<const UsbInterfaceDescriptor> UsbConfigDescriptor::GetInterface(
170     size_t index) const {
171   return new UsbInterfaceDescriptor(this, &config_->interface[index]);
172 }
173
174 }  // namespace usb_service