HaikuDepot: notify work status from main window
[haiku.git] / src / kits / device / USBInterface.cpp
blobb2d447294e01687fe0c4271e83c22f6cf08ec2a5
1 /*
2 * Copyright 2007-2008, Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Michael Lotz <mmlr@mlotz.ch>
7 * Salvatore Benedetto <salvatore.benedetto@gmail.com>
8 */
10 #include <USBKit.h>
11 #include <usb_raw.h>
13 #include <new>
14 #include <string.h>
15 #include <unistd.h>
18 BUSBInterface::BUSBInterface(BUSBConfiguration *config, uint32 index,
19 uint32 alternate, int rawFD)
20 : fConfiguration(config),
21 fIndex(index),
22 fAlternate(alternate),
23 fRawFD(rawFD),
24 fEndpoints(NULL),
25 fAlternateCount(0),
26 fAlternates(NULL),
27 fInterfaceString(NULL)
29 _UpdateDescriptorAndEndpoints();
33 BUSBInterface::~BUSBInterface()
35 delete[] fInterfaceString;
37 if (fEndpoints != NULL) {
38 for (int32 i = 0; i < fDescriptor.num_endpoints; i++)
39 delete fEndpoints[i];
40 delete[] fEndpoints;
43 if (fAlternates != NULL) {
44 for (uint32 i = 0; i < fAlternateCount; i++)
45 delete fAlternates[i];
46 delete[] fAlternates;
51 uint32
52 BUSBInterface::Index() const
54 return fIndex;
58 uint32
59 BUSBInterface::AlternateIndex() const
61 if (fAlternate == B_USB_RAW_ACTIVE_ALTERNATE)
62 return ActiveAlternateIndex();
63 return fAlternate;
67 const BUSBConfiguration *
68 BUSBInterface::Configuration() const
70 return fConfiguration;
74 const BUSBDevice *
75 BUSBInterface::Device() const
77 return fConfiguration->Device();
81 uint8
82 BUSBInterface::Class() const
84 return fDescriptor.interface_class;
88 uint8
89 BUSBInterface::Subclass() const
91 return fDescriptor.interface_subclass;
95 uint8
96 BUSBInterface::Protocol() const
98 return fDescriptor.interface_protocol;
102 const char *
103 BUSBInterface::InterfaceString() const
105 if (fDescriptor.interface == 0)
106 return "";
108 if (fInterfaceString)
109 return fInterfaceString;
111 fInterfaceString = Device()->DecodeStringDescriptor(fDescriptor.interface);
112 if (fInterfaceString == NULL)
113 return "";
115 return fInterfaceString;
119 const usb_interface_descriptor *
120 BUSBInterface::Descriptor() const
122 return &fDescriptor;
126 status_t
127 BUSBInterface::OtherDescriptorAt(uint32 index, usb_descriptor *descriptor,
128 size_t length) const
130 if (length <= 0 || descriptor == NULL)
131 return B_BAD_VALUE;
133 usb_raw_command command;
134 command.generic_etc.descriptor = descriptor;
135 command.generic_etc.config_index = fConfiguration->Index();
136 command.generic_etc.interface_index = fIndex;
137 command.generic_etc.alternate_index = fAlternate;
138 command.generic_etc.generic_index = index;
139 command.generic_etc.length = length;
140 if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_GENERIC_DESCRIPTOR_ETC, &command,
141 sizeof(command)) || command.generic.status != B_USB_RAW_STATUS_SUCCESS)
142 return B_ERROR;
144 return B_OK;
148 uint32
149 BUSBInterface::CountEndpoints() const
151 return fDescriptor.num_endpoints;
155 const BUSBEndpoint *
156 BUSBInterface::EndpointAt(uint32 index) const
158 if (index >= fDescriptor.num_endpoints || fEndpoints == NULL)
159 return NULL;
161 return fEndpoints[index];
165 uint32
166 BUSBInterface::CountAlternates() const
168 usb_raw_command command;
169 command.alternate.config_index = fConfiguration->Index();
170 command.alternate.interface_index = fIndex;
171 if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_ALT_INTERFACE_COUNT, &command,
172 sizeof(command)) || command.alternate.status != B_USB_RAW_STATUS_SUCCESS)
173 return 1;
175 return command.alternate.alternate_info;
179 const BUSBInterface *
180 BUSBInterface::AlternateAt(uint32 alternateIndex) const
182 if (fAlternateCount > 0 && fAlternates != NULL) {
183 if (alternateIndex >= fAlternateCount)
184 return NULL;
186 return fAlternates[alternateIndex];
189 if (fAlternateCount == 0)
190 fAlternateCount = CountAlternates();
191 if (alternateIndex >= fAlternateCount)
192 return NULL;
194 fAlternates = new(std::nothrow) BUSBInterface *[fAlternateCount];
195 if (fAlternates == NULL)
196 return NULL;
198 for (uint32 i = 0; i < fAlternateCount; i++) {
199 fAlternates[i] = new(std::nothrow) BUSBInterface(fConfiguration, fIndex,
200 i, fRawFD);
203 return fAlternates[alternateIndex];
207 uint32
208 BUSBInterface::ActiveAlternateIndex() const
210 usb_raw_command command;
211 command.alternate.config_index = fConfiguration->Index();
212 command.alternate.interface_index = fIndex;
213 if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_ACTIVE_ALT_INTERFACE_INDEX, &command,
214 sizeof(command)) || command.alternate.status != B_USB_RAW_STATUS_SUCCESS)
215 return 0;
217 return command.alternate.alternate_info;
221 status_t
222 BUSBInterface::SetAlternate(uint32 alternateIndex)
224 usb_raw_command command;
225 command.alternate.alternate_info = alternateIndex;
226 command.alternate.config_index = fConfiguration->Index();
227 command.alternate.interface_index = fIndex;
228 if (ioctl(fRawFD, B_USB_RAW_COMMAND_SET_ALT_INTERFACE, &command,
229 sizeof(command)) || command.alternate.status != B_USB_RAW_STATUS_SUCCESS)
230 return B_ERROR;
232 _UpdateDescriptorAndEndpoints();
233 return B_OK;
237 void
238 BUSBInterface::_UpdateDescriptorAndEndpoints()
240 usb_raw_command command;
241 command.interface_etc.descriptor = &fDescriptor;
242 command.interface_etc.config_index = fConfiguration->Index();
243 command.interface_etc.interface_index = fIndex;
244 command.interface_etc.alternate_index = fAlternate;
245 if (ioctl(fRawFD, B_USB_RAW_COMMAND_GET_INTERFACE_DESCRIPTOR_ETC, &command,
246 sizeof(command)) || command.interface.status != B_USB_RAW_STATUS_SUCCESS)
247 memset(&fDescriptor, 0, sizeof(fDescriptor));
249 if (fEndpoints != NULL) {
250 // Delete old endpoints
251 for (int32 i = 0; i < fDescriptor.num_endpoints; i++)
252 delete fEndpoints[i];
253 delete[] fEndpoints;
256 fEndpoints = new(std::nothrow) BUSBEndpoint *[fDescriptor.num_endpoints];
257 if (fEndpoints == NULL)
258 return;
260 for (int32 i = 0; i < fDescriptor.num_endpoints; i++)
261 fEndpoints[i] = new(std::nothrow) BUSBEndpoint(this, i, fRawFD);