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 
23 #include "modules/system/usb/Usb.h"
24 #include "pedigree/kernel/compiler.h"
25 #include "pedigree/kernel/machine/Device.h"
26 #include "pedigree/kernel/processor/types.h"
27 #include "pedigree/kernel/utilities/String.h"
28 #include "pedigree/kernel/utilities/Vector.h"
29 #include "pedigree/kernel/utilities/utility.h"
30 
31 class UsbDeviceContainer;
32 class UsbHub;
33 struct UsbDeviceDescriptor;
36 
38 {
39  friend class UsbDeviceContainer;
40 
41  public:
42  struct Setup
43  {
44  inline Setup(
45  uint8_t requestType, uint8_t request, uint16_t value,
46  uint16_t index, uint16_t length)
47  : nRequestType(requestType), nRequest(request), nValue(value),
48  nIndex(index), nLength(length)
49  {
50  }
51 
52  uint8_t nRequestType;
53  uint8_t nRequest;
54  uint16_t nValue;
55  uint16_t nIndex;
56  uint16_t nLength;
57  } PACKED;
58 
60  {
61  inline UnknownDescriptor(uint8_t *pBuffer, uint8_t type, size_t length)
62  : nType(type), nLength(length)
63  {
64  pDescriptor = new uint8_t[nLength];
65  MemoryCopy(pDescriptor, pBuffer, nLength);
66  }
67 
68  void *pDescriptor;
69  uint8_t nType;
70  size_t nLength;
71  };
72 
73  struct Endpoint
74  {
75  Endpoint(UsbEndpointDescriptor *pDescriptor, UsbSpeed speed);
76 
77  enum TransferTypes
78  {
79  Control = 0,
80  Isochronus = 1,
81  Bulk = 2,
82  Interrupt = 3
83  };
84 
85  uint8_t nEndpoint;
86  bool bIn;
87  bool bOut;
88  uint8_t nTransferType;
89  uint16_t nMaxPacketSize;
90 
91  bool bDataToggle;
92  };
93 
94  struct Interface
95  {
96  Interface(UsbInterfaceDescriptor *pDescriptor);
97  ~Interface();
98 
99  uint8_t nInterface;
100  uint8_t nAlternateSetting;
101  uint8_t nClass;
102  uint8_t nSubclass;
103  uint8_t nProtocol;
104  uint8_t nString;
105 
106  Vector<Endpoint *> endpointList;
107  Vector<UnknownDescriptor *> otherDescriptorList;
108  String sString;
109  };
110 
112  {
114  void *pConfigBuffer, size_t nConfigLength, UsbSpeed speed);
115  ~ConfigDescriptor();
116 
117  uint8_t nConfig;
118  uint8_t nString;
119 
120  Vector<Interface *> interfaceList;
121  Vector<UnknownDescriptor *> otherDescriptorList;
122  String sString;
123  };
124 
126  {
128  ~DeviceDescriptor();
129 
130  uint16_t nBcdUsbRelease;
131  uint8_t nClass;
132  uint8_t nSubclass;
133  uint8_t nProtocol;
134  uint8_t nMaxControlPacketSize;
135  uint16_t nVendorId;
136  uint16_t nProductId;
137  uint16_t nBcdDeviceRelease;
138  uint8_t nVendorString;
139  uint8_t nProductString;
140  uint8_t nSerialString;
141  uint8_t nConfigurations;
142 
143  Vector<ConfigDescriptor *> configList;
144  String sVendor;
145  String sProduct;
146  String sSerial;
147  };
148 
150  {
151  uint16_t nVersion;
152  uint8_t nClass;
153  uint8_t nSubclass;
154  uint8_t nProtocol;
155  uint8_t nMaxControlPacketSize;
156  uint8_t nConfigurations;
157  uint8_t nRsvd;
158  } PACKED;
159 
161  enum UsbState
162  {
163  Connected = 0,
164  Addressed,
165  HasDescriptors,
166  Configured,
167  HasInterface,
168  HasDriver
169  };
170 
172  UsbDevice(UsbHub *pHub, uint8_t nPort, UsbSpeed speed);
173 
175  UsbDevice(UsbDevice *pDev);
176 
178  virtual ~UsbDevice();
179 
181  void initialise(uint8_t nAddress);
182 
184  virtual void initialiseDriver()
185  {
186  }
187 
188  virtual void getUsbDeviceName(String &str)
189  {
190  str = "Generic USB Device";
191  }
192 
194  inline uint8_t getAddress()
195  {
196  return m_nAddress;
197  }
198 
200  inline uint8_t getPort()
201  {
202  return m_nPort;
203  }
204 
206  inline UsbSpeed getSpeed()
207  {
208  return m_Speed;
209  }
210 
213  {
214  return m_UsbState;
215  }
216 
219  {
220  return m_pDescriptor;
221  }
222 
225  {
226  return m_pConfiguration;
227  }
228 
231  {
232  return m_pInterface;
233  }
234 
236  void useConfiguration(uint8_t nConfig);
237 
239  void useInterface(uint8_t nInterface);
240 
243  {
244  return m_pContainer;
245  }
246 
248  virtual bool hasSubtree() const
249  {
250  return false;
251  }
252 
255  virtual Device *getDevice()
256  {
257  return 0;
258  }
259 
260  protected:
261  // Sync transfer methods
262  ssize_t doSync(
263  Endpoint *pEndpoint, UsbPid pid, uintptr_t pBuffer, size_t nBytes,
264  size_t timeout);
265  ssize_t syncIn(
266  Endpoint *pEndpoint, uintptr_t pBuffer, size_t nBytes,
267  size_t timeout = 5000);
268  ssize_t syncOut(
269  Endpoint *pEndpoint, uintptr_t pBuffer, size_t nBytes,
270  size_t timeout = 5000);
271 
272  void addInterruptInHandler(
273  Endpoint *pEndpoint, uintptr_t pBuffer, uint16_t nBytes,
274  void (*pCallback)(uintptr_t, ssize_t), uintptr_t pParam = 0);
275 
277  bool controlRequest(
278  uint8_t nRequestType, uint8_t nRequest, uint16_t nValue,
279  uint16_t nIndex, uint16_t nLength = 0, uintptr_t pBuffer = 0);
280 
282  uint16_t getStatus();
283 
285  bool clearEndpointHalt(Endpoint *pEndpoint);
286 
288  void *getDescriptor(
289  uint8_t nDescriptorType, uint8_t nDescriptorIndex, uint16_t nBytes,
290  uint8_t requestType = 0);
291 
293  uint8_t getDescriptorLength(
294  uint8_t nDescriptorType, uint8_t nDescriptorIndex,
295  uint8_t requestType = 0);
296 
298  String getString(uint8_t nString);
299 
301  uint8_t m_nAddress;
302 
304  uint8_t m_nPort;
305 
307  UsbSpeed m_Speed;
308 
311 
314 
317 
320 
323 
326 
327  private:
328  UsbDevice(const UsbDevice &d);
329  const UsbDevice &operator=(const UsbDevice &d);
330 };
331 
333 {
334  public:
336  virtual ~UsbDeviceContainer();
337 
338  UsbDevice *getUsbDevice() const;
339 
340  virtual void getName(String &str);
341 
342  virtual Type getType();
343 
344  virtual void dump(String &str);
345 
346  private:
347  UsbDevice *m_pUsbDevice;
348 };
349 
350 #endif
UsbSpeed getSpeed()
Returns the speed at which the device operates.
Definition: UsbDevice.h:206
uint8_t m_nPort
The number of the port on which the device is connected.
Definition: UsbDevice.h:304
ConfigDescriptor * m_pConfiguration
Configuration in use.
Definition: UsbDevice.h:316
A vector / dynamic array.
ConfigDescriptor * getConfiguration()
Returns the configuration in use.
Definition: UsbDevice.h:224
virtual bool hasSubtree() const
Do we expose our own Device tree?
Definition: UsbDevice.h:248
Definition: String.h:49
Interface * getInterface()
Returns the interface in use.
Definition: UsbDevice.h:230
UsbDeviceContainer * getContainer() const
Gets our Device container, for replacing parents on hubs etc.
Definition: UsbDevice.h:242
UsbHub * m_pHub
Parent USB hub.
Definition: UsbDevice.h:322
Definition: Device.h:43
UsbSpeed m_Speed
The speed at which the device operates.
Definition: UsbDevice.h:307
virtual void initialiseDriver()
Implemented by the driver class, initialises driver-specific stuff.
Definition: UsbDevice.h:184
virtual Device * getDevice()
Definition: UsbDevice.h:255
UsbState m_UsbState
The current state of the device.
Definition: UsbDevice.h:310
uint8_t m_nAddress
The current address of the device.
Definition: UsbDevice.h:301
UsbState
Possible states for an USB device.
Definition: UsbDevice.h:161
DeviceDescriptor * m_pDescriptor
Device descriptor for this device.
Definition: UsbDevice.h:313
Definition: UsbHub.h:30
uint8_t getAddress()
Returns the current address of the device.
Definition: UsbDevice.h:194
DeviceDescriptor * getDescriptor()
Returns the device descriptor of the device.
Definition: UsbDevice.h:218
Type
Definition: Device.h:50
uint8_t getPort()
Returns the number of the port on which the device is connected.
Definition: UsbDevice.h:200
UsbState getUsbState()
Returns the current state of the device.
Definition: UsbDevice.h:212
Interface * m_pInterface
Interface in use.
Definition: UsbDevice.h:319
UsbDeviceContainer * m_pContainer
Our current container.
Definition: UsbDevice.h:325