The Pedigree Project 0.1
UsbDevice.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef USBDEVICE_H
21#define USBDEVICE_H
22#include "pedigree/kernel/Atomic.h"
23#include "pedigree/kernel/compiler.h"
24#include "pedigree/kernel/machine/Device.h"
25#include "pedigree/kernel/process/Mutex.h"
26#include "pedigree/kernel/process/OperationBarrier.h"
27#include "pedigree/kernel/processor/types.h"
28#include "pedigree/kernel/utilities/String.h"
29#include "pedigree/kernel/utilities/Vector.h"
30#include "pedigree/kernel/utilities/utility.h"
31
32#include <config.h>
33
34#include "modules/system/usb/Usb.h"
35
37class UsbHub;
39class UsbPnP;
43
44class EXPORTED_PUBLIC UsbDevice {
45 friend class UsbDeviceContainer;
46 friend class UsbPnP;
47
48 public:
49 struct Setup {
50 inline Setup(uint8_t requestType, uint8_t request, uint16_t value, uint16_t index,
51 uint16_t length)
52 : nRequestType(requestType),
53 nRequest(request),
54 nValue(value),
55 nIndex(index),
56 nLength(length) {}
57
58 uint8_t nRequestType;
59 uint8_t nRequest;
60 uint16_t nValue;
61 uint16_t nIndex;
62 uint16_t nLength;
63 } PACKED;
64
66 inline UnknownDescriptor(uint8_t* pBuffer, uint8_t type, size_t length)
67 : nType(type), nLength(length) {
68 pDescriptor = new uint8_t[nLength];
69 MemoryCopy(pDescriptor, pBuffer, nLength);
70 }
71
73 delete[] static_cast<uint8_t*>(pDescriptor);
74 }
75
76 void* pDescriptor;
77 uint8_t nType;
78 size_t nLength;
79 };
80
81 struct Endpoint {
82 Endpoint(UsbEndpointDescriptor* pDescriptor, UsbSpeed speed);
83
84 enum TransferTypes { Control = 0, Isochronus = 1, Bulk = 2, Interrupt = 3 };
85
86 uint8_t nEndpoint;
87 bool bIn;
88 bool bOut;
89 uint8_t nTransferType;
90 uint16_t nMaxPacketSize;
91 uint8_t nInterval;
92 uint8_t nTransactions;
93 uint8_t nMaxBurst = 0;
94 uint8_t nStreams = 0;
95 uint16_t nBytesPerInterval = 0;
96 bool hasCompanion = false;
97
98 bool bDataToggle;
99 };
100
101 struct Interface {
102 Interface(UsbInterfaceDescriptor* pDescriptor);
103 ~Interface();
104
105 uint8_t nInterface;
106 uint8_t nAlternateSetting;
107 uint8_t nClass;
108 uint8_t nSubclass;
109 uint8_t nProtocol;
110 uint8_t nString;
111
112 Vector<Endpoint*> endpointList;
113 Vector<UnknownDescriptor*> otherDescriptorList;
114 String sString;
115 };
116
118 ConfigDescriptor(void* pConfigBuffer, size_t nConfigLength, UsbSpeed speed);
120
121 uint8_t nConfig;
122 uint8_t nString;
123 bool valid;
124
125 Vector<Interface*> interfaceList;
126 Vector<UnknownDescriptor*> otherDescriptorList;
127 String sString;
128 };
129
133
134 void retain();
135 void release();
136
137 uint16_t nBcdUsbRelease;
138 uint8_t nClass;
139 uint8_t nSubclass;
140 uint8_t nProtocol;
141 uint8_t nMaxControlPacketSize;
142 uint16_t nVendorId;
143 uint16_t nProductId;
144 uint16_t nBcdDeviceRelease;
145 uint8_t nVendorString;
146 uint8_t nProductString;
147 uint8_t nSerialString;
148 uint8_t nConfigurations;
149
150 Vector<ConfigDescriptor*> configList;
151 String sVendor;
152 String sProduct;
153 String sSerial;
154
155 private:
156 Atomic<size_t> m_References;
157 };
158
160 uint16_t nVersion;
161 uint8_t nClass;
162 uint8_t nSubclass;
163 uint8_t nProtocol;
164 uint8_t nMaxControlPacketSize;
165 uint8_t nConfigurations;
166 uint8_t nRsvd;
167 } PACKED;
168
170 enum UsbState { Connected = 0, Addressed, HasDescriptors, Configured, HasInterface, HasDriver };
171
173 UsbDevice(UsbHub* pHub, uint8_t nPort, UsbSpeed speed);
174
176 UsbDevice(UsbDevice* pDev);
177
179 virtual ~UsbDevice();
180
182 void initialise(uint8_t nAddress);
183
185 virtual void initialiseDriver() {}
186
187 virtual void getUsbDeviceName(String& str) {
188 str.assign("Generic USB Device", 19);
189 }
190
192 inline uint8_t getAddress() {
193 return m_nAddress;
194 }
195
197 inline uint8_t getPort() {
198 return m_nPort;
199 }
200
201 UsbHub* getHub() const {
202 return m_pHub;
203 }
204
206 inline UsbSpeed getSpeed() {
207 return m_Speed;
208 }
209
212 return m_UsbState;
213 }
214
217 return m_pDescriptor;
218 }
219
222 return m_pConfiguration;
223 }
224
227 return m_pInterface;
228 }
229
231 void useConfiguration(uint8_t nConfig);
232
234 void useInterface(uint8_t nInterface);
235
238 return m_pContainer;
239 }
240
242 virtual bool hasSubtree() const {
243 return false;
244 }
245
248 virtual Device* getDevice() {
249 return 0;
250 }
251
254
256 virtual void quiesceForRetirement() {}
257
259 virtual void prepareForDisconnection() {}
260
261 protected:
262 // Sync transfer methods
263 ssize_t doSync(Endpoint* pEndpoint, UsbPid pid, uintptr_t pBuffer, size_t nBytes, size_t timeout);
264 ssize_t syncIn(Endpoint* pEndpoint, uintptr_t pBuffer, size_t nBytes, size_t timeout = 5000);
265 ssize_t syncOut(Endpoint* pEndpoint, uintptr_t pBuffer, size_t nBytes, size_t timeout = 5000);
266
267 MUST_USE_RESULT bool addInterruptInHandler(Endpoint* pEndpoint, uintptr_t pBuffer,
268 uint16_t nBytes, void (*pCallback)(uintptr_t, ssize_t),
269 UsbInterruptInHandle& handle, uintptr_t pParam = 0);
270
272 bool controlRequest(uint8_t nRequestType, uint8_t nRequest, uint16_t nValue, uint16_t nIndex,
273 uint16_t nLength = 0, uintptr_t pBuffer = 0, uint32_t timeout = 5000);
275 ssize_t controlRequestResult(uint8_t nRequestType, uint8_t nRequest, uint16_t nValue,
276 uint16_t nIndex, uint16_t nLength = 0, uintptr_t pBuffer = 0,
277 uint32_t timeout = 5000);
278
280 uint16_t getStatus();
281
283 bool clearEndpointHalt(Endpoint* pEndpoint);
284
286 void* getDescriptor(uint8_t nDescriptorType, uint8_t nDescriptorIndex, uint16_t nBytes,
287 uint8_t requestType = 0);
288
290 uint8_t getDescriptorLength(uint8_t nDescriptorType, uint8_t nDescriptorIndex,
291 uint8_t requestType = 0);
292
294 String getString(uint8_t nString);
295
297 uint8_t m_nAddress;
298 uint16_t m_ControlPacketSize;
299
301 uint8_t m_nPort;
302
304 uint8_t m_nRootPort;
305 size_t m_nRootPortGeneration;
306
308 UsbSpeed m_Speed;
309
312
315
318
321
324
327
328 private:
329 UsbDevice(const UsbDevice& d);
330 const UsbDevice& operator=(const UsbDevice& d);
331};
332
334 public:
336 virtual ~UsbDeviceContainer();
337
338 EXPORTED_PUBLIC UsbDevice* getUsbDevice() const;
339
342
343 virtual void getName(String& str);
344
345 virtual Type getType();
346
347 virtual void dump(String& str);
348
349 private:
350 friend class UsbHub;
351 friend class UsbPnP;
352
353 bool tryAcquireProbe(OperationBarrier::Lease& lease);
354 void closeProbeAdmission();
355 void waitForProbes();
356
357 void attachSubtree(UsbDevice* pDev);
358 void destroyUsbDevice(UsbDevice* pDev);
359
360 UsbDevice* m_pUsbDevice;
361 OperationBarrier m_ProbeOperations;
362 Mutex m_ProbeLock;
363 Atomic<UsbPnP*> m_BindingRegistry;
364 void* m_BindingOwner;
365 OperationBarrier::Lease m_BindingLease;
366 UsbDeviceContainer* m_PreviousBinding;
367 UsbDeviceContainer* m_NextBinding;
368};
369
370#if HOSTED && PEDIGREE_HOSTED_SMOKE_TESTS
371EXPORTED_PUBLIC bool runHostedUsbContainerOwnershipRegression();
372#endif
373
374#endif
Definition Mutex.h:56
MUST_USE_RESULT bool replaceUsbDevice(UsbDevice *pDev)
Definition UsbDevice.cc:748
virtual void getName(String &str)
Definition UsbDevice.cc:810
virtual Type getType()
Definition UsbDevice.cc:814
virtual void dump(String &str)
Definition UsbDevice.cc:818
UsbDeviceContainer * getContainer() const
Gets our Device container, for replacing parents on hubs etc.
Definition UsbDevice.h:237
UsbState getUsbState()
Returns the current state of the device.
Definition UsbDevice.h:211
virtual bool hasSubtree() const
Do we expose our own Device tree?
Definition UsbDevice.h:242
UsbHub * m_pHub
Parent USB hub.
Definition UsbDevice.h:323
ConfigDescriptor * getConfiguration()
Returns the configuration in use.
Definition UsbDevice.h:221
uint8_t m_nRootPort
Root-controller connection which owns this device generation.
Definition UsbDevice.h:304
UsbDeviceContainer * m_pContainer
Our current container.
Definition UsbDevice.h:326
virtual void initialiseDriver()
Implemented by the driver class, initialises driver-specific stuff.
Definition UsbDevice.h:185
virtual void quiesceForRetirement()
Definition UsbDevice.h:256
virtual Device * getDevice()
Definition UsbDevice.h:248
uint8_t m_nAddress
The current address of the device.
Definition UsbDevice.h:297
Interface * m_pInterface
Interface in use.
Definition UsbDevice.h:320
virtual void prepareForDriverRetirement()
Definition UsbDevice.h:253
UsbSpeed getSpeed()
Returns the speed at which the device operates.
Definition UsbDevice.h:206
UsbState m_UsbState
The current state of the device.
Definition UsbDevice.h:311
UsbSpeed m_Speed
The speed at which the device operates.
Definition UsbDevice.h:308
uint8_t m_nPort
The number of the port on which the device is connected.
Definition UsbDevice.h:301
ConfigDescriptor * m_pConfiguration
Configuration in use.
Definition UsbDevice.h:317
uint8_t getPort()
Returns the number of the port on which the device is connected.
Definition UsbDevice.h:197
uint8_t getAddress()
Returns the current address of the device.
Definition UsbDevice.h:192
DeviceDescriptor * m_pDescriptor
Device descriptor for this device.
Definition UsbDevice.h:314
DeviceDescriptor * getDescriptor()
Returns the device descriptor of the device.
Definition UsbDevice.h:216
virtual void prepareForDisconnection()
Definition UsbDevice.h:259
Interface * getInterface()
Returns the interface in use.
Definition UsbDevice.h:226
UsbState
Possible states for an USB device.
Definition UsbDevice.h:170
A vector / dynamic array.
Definition Vector.h:33