XenevaOS
Loading...
Searching...
No Matches
usb3.h
Go to the documentation of this file.
1
30#ifndef __USB3_H__
31#define __USB3_H__
32
33#include <stdint.h>
34#include "xhci.h"
35#include <Hal\x86_64_sched.h>
36#include <list.h>
37#include <Sync\spinlock.h>
38
39#pragma pack(push,1)
72#pragma pack(pop)
73
74/* USBMessage structure */
75#pragma pack(push,1)
88#pragma pack(pop)
89
90/* Standard USB Requests */
91#define USB_BM_REQUEST_OUTPUT 0
92#define USB_BM_REQUEST_INPUT 0x80
93#define USB_BM_REQUEST_STANDARD 0
94#define USB_BM_REQUEST_CLASS 0x20
95#define USB_BM_REQUEST_VENDOR 0x40
96#define USB_BM_REQUEST_DEVICE 0
97#define USB_BM_REQUEST_INTERFACE 1
98#define USB_BM_REQUEST_ENDPOINT 2
99#define USB_BM_REQUEST_OTHER 3
100#define USB_BM_REQUEST_VENDORSPEC 0x1F
101
102#define USB_BREQUEST_GET_STATUS 0
103#define USB_BREQUEST_CLEAR_FEATURE 1
104#define USB_BREQUEST_SET_FEATURE 3
105#define USB_BREQUEST_SET_ADDRESS 5
106#define USB_BREQUEST_GET_DESCRIPTOR 6
107#define USB_BREQUEST_SET_DESCRIPTOR 7
108#define USB_BREQUEST_GET_CONFIGURATION 8
109#define USB_BREQUEST_SET_CONFIGURATION 9
110#define USB_BREQUEST_GET_INTERFACE 10
111#define USB_BREQUEST_SET_INTERFACE 11
112
113#define USB_DESCRIPTOR_DEVICE 1
114#define USB_DESCRIPTOR_CONFIGURATION 2
115#define USB_DESCRIPTOR_STRING 3
116#define USB_DESCRIPTOR_INTERFACE 4
117#define USB_DESCRIPTOR_ENDPOINT 5
118#define USB_DESCRIPTOR_SUPERSPEED_ENDP_CMP 48
119
120#define USB_DESCRIPTOR_WVALUE(type,index) ((type << 8) | index)
121
122#define USB_AUDIO_DEV_CLASS 0x01
123#define USB_COMMUNICATION_CDC_CTRL_CLASS 0x02
124#define USB_HID_DEV_CLASS 0x03
125#define USB_PHYSICAL_DEV_CLASS 0x05
126#define USB_IMAGE_DEV_CLASS 0x06
127#define USB_PRINTER_DEV_CLASS 0x07
128#define USB_MASS_STORAGE_DEV_CLASS 0x08
129#define USB_HUB_DEV_CLASS 0x09
130#define USB_CDC_DATA_DEV_CLASS 0x0A
131#define USB_SMART_CARD_DEV_CLASS 0x0B
132#define USB_CONTENT_SECURITY_DEV_CLASS 0x0D
133#define USB_VIDEO_DEV_CLASS 0x0E
134#define USB_PERSONAL_HEALTHCARE_DEV_CLASS 0x0F
135#define USB_AUDIO_VIDEO_DEV_CLASS 0x10
136#define USB_BILLBOARD_DEV_CLASS 0x11
137#define USB_TYPE_C_BRIDGE_CLASS 0x12
138#define USB_I3C_DEV_CLASS 0x3C
139#define USB_DIAGNOSTIC_DEV_CLASS 0xDC
140#define USB_WIRELESS_CONTROLLER_DEV_CLASS 0xE0
141#define USB_MISCELLANEOUS_DEV_CLASS 0xEF
142
143/* transfer type defined in endpoint descriptor*/
144#define ENDPOINT_TRANSFER_TYPE_CONTROL 0
145#define ENDPOINT_TRANSFER_TYPE_ISOCH 1
146#define ENDPOINT_TRANSFER_TYPE_BULK 2
147#define ENDPOINT_TRANSFER_TYPE_INT 3
148
149#pragma pack(push,1)
157#pragma pack(pop)
158
159#pragma pack(push,1)
176#pragma pack(pop)
177
178#pragma pack(push,1)
190#pragma pack(pop)
191
192#pragma pack(push,1)
204#pragma pack(pop)
205
206#pragma pack(push,1)
217#pragma pack(pop)
218
219#pragma pack(push,1)
225#pragma pack(pop)
226
227#pragma pack(push,1)
236#pragma pack(pop)
237
238#pragma pack(push,1)
243#pragma pack(pop)
244/*
245* USBGetDeviceDesc -- sends USB_GET_DESCRIPTOR request to specific device
246* @param dev -- Pointer to usb device structure
247* @param slot -- Pointer to usb slot data structure
248* @param slot_id -- Slot id of the device
249* @param buffer -- address of the buffer where device descriptor will be written
250* @param len -- number of bytes needs to be requested
251*/
252extern void USBGetDeviceDesc(USBDevice *dev, XHCISlot *slot, uint8_t slot_id, uint64_t buffer, uint16_t len);
253
254/*
255* USBGetStringDesc -- request a string descriptor to specific device
256* @param dev -- Pointer to usb device structure
257* @param slot -- Pointer to usb slot data structure
258* @param slot_id -- Slot id of the device
259* @param buffer -- address of the buffer where device descriptor will be written
260* @param id-- type of the string needs to be requested
261*/
262extern void USBGetStringDesc(USBDevice *dev, XHCISlot *slot, uint8_t slot_id, uint64_t buffer, uint16_t id);
263
264/*
265* USBGetConfigDesc -- get configuration descriptor
266* @param dev -- Pointer to usb device structure
267* @param slot -- Pointer to usb slot data structure
268* @param slot_id -- Slot id of the device
269* @param buffer -- address of the buffer where device descriptor will be written
270*/
271void USBGetConfigDesc(USBDevice* dev, XHCISlot* slot, uint8_t slot_id, uint64_t buffer, uint16_t len, uint8_t id);
272
273/*
274* USBGetConfigDesc -- get configuration descriptor
275* @param dev -- Pointer to usb device structure
276* @param slot -- Pointer to usb slot data structure
277* @param slot_id -- Slot id of the device
278* @param configval -- config value returned in configuration descriptor
279*/
280void USBSetConfigDesc(USBDevice* dev, XHCISlot* slot, uint8_t slot_id, uint8_t configval);
281
282/*
283* XHCIPollEvent -- waits for an event to occur on interrupts
284* @param usb_device -- pointer to usb device structure
285* @param trb_type -- type of trb to look
286* @return trb_event_index -- index of the trb on event_ring_segment
287*/
288extern int XHCIPollEvent(USBDevice* usb_device, int trb_type);
289
290/*
291* XHCISendCommand -- Sends command to USB device through XHCI
292* host
293* @param dev -- Pointer to usb device structure
294* @param param1 -- parameter 1 of TRB
295* @param param2 -- parameter 2 of TRB
296* @param status -- status of TRB
297* @param ctrl -- control for TRB
298*/
299extern void XHCISendCommand(USBDevice *dev, uint32_t param1, uint32_t param2, uint32_t status, uint32_t ctrl);
300
301/*
302* XHCISendCommandSlot -- sends command to slot trb
303* @param slot -- pointer to slot data structure
304* @param param1 -- first parameter of trb structure
305* @param param2 -- 2nd parameter of trb structure
306* @param status -- status field of trb structure
307* @param ctrl -- control field of trb structure
308*/
309extern void XHCISendCommandSlot(XHCISlot* slot, uint32_t param1, uint32_t param2, uint32_t status, uint32_t ctrl);
310
311/*
312* XHCISendCommandEndpoint -- sends command to endpoint trb
313* @param slot -- pointer to slot data structure
314* @param endp_num -- endpoint number
315* @param param1 -- first parameter of trb structure
316* @param param2 -- 2nd parameter of trb structure
317* @param status -- status field of trb structure
318* @param ctrl -- control field of trb structure
319*/
320void XHCISendCommandEndpoint(XHCISlot* slot, uint8_t endp_num, uint32_t param1, uint32_t param2, uint32_t status, uint32_t ctrl);
321
322/*
323* XHCIRingDoorbell -- rings the host doorbell
324* @param dev -- Pointer to usb structure
325*/
326extern void XHCIRingDoorbell(USBDevice* dev);
327
328/*
329* XHCIRingDoorbellSlot -- rings the doorbell by slot
330* @param dev -- Pointer to usb structure
331* @param slot -- slot id
332* @param endpoint -- endpoint number, it should be 0 if
333* the slot is 0, else values 1-255 should be placed
334*
335*/
337
338/*
339* XHCISendCommandMultiple -- sends multiple commands to the command ring
340* @param dev -- Pointer to usb structure
341* @param trb -- TRB address containing multiple TRBs
342* @param num_count -- counts of TRB to send
343*/
344extern void XHCISendCommandMultiple(USBDevice* dev, xhci_trb_t* trb, int num_count);
345
346/*
347* XHCIReset -- reset the xhci controller
348* @param dev -- pointer to USB device
349* structure
350*/
351extern void XHCIReset(USBDevice *dev);
352
353/*
354* XHCIDeviceContextInit -- initialise the
355* device context base array pointer (DCBAAP)
356* @param dev -- Pointer to USB device
357* structure
358*/
359extern void XHCIDeviceContextInit(USBDevice *dev);
360
361/*
362* XHCICommandRingInit -- initialise the command ring
363* buffer
364* @param dev -- Pointer to usb device
365*/
366extern void XHCICommandRingInit(USBDevice *dev);
367
368/*
369* XHCIProtocolInit -- Initialize XHCI Extended Protocol
370* @param dev -- Pointer to usb device
371*/
372extern void XHCIProtocolInit(USBDevice *dev);
373
374/*
375* xhci_event_ring_init -- initialize the
376* default event ring
377* @param dev -- Pointer to usb device
378*/
379extern void XHCIEventRingInit(USBDevice *dev);
380
381/*
382* XHCIStartDefaultPorts -- initializes all powered ports
383* @param dev -- Pointer to USB device structures
384*/
385extern void XHCIStartDefaultPorts(USBDevice *dev);
386
387/*
388* XHCIPortInitialize -- initializes a specific port
389* @param dev -- Pointer to USB device structures
390* @param port -- number of the port
391*/
392extern void XHCIPortInitialize(USBDevice *dev, unsigned int port);
393
394/*
395* XHCIGetSlotByID -- returns a slot from slot list
396* @param dev -- Pointer to USB device
397* @param slot_id -- slot id
398*/
399extern XHCISlot* XHCIGetSlotByID(USBDevice* dev, uint8_t slot_id);
400
401
402/*
403* XHCISlotGetEP_DCI -- returns an endpoint by its endpoint number
404*/
405extern XHCIEndpoint *XHCISlotGetEP_DCI(XHCISlot* slot, uint8_t endp_num);
406
407/*
408* USBGetMainDevice -- returns the
409* main usb device structure
410*/
412
413/*
414* XHCISlotGetEP -- returns an endpoint by its endpoint number
415*/
416extern XHCIEndpoint *XHCISlotGetEP(XHCISlot* slot, uint8_t endp_num);
417
418/*
419 * USBGetDescriptor -- checks and return a descriptor of given
420 * type
421 * @param slot -- XHCI Provided slot
422 * @param type -- type of the descriptor
423 */
425
426/*
427 * USBScheduleInterrupt -- schedules an interrupt for a device
428 * @param dev -- Pointer to USB Device
429 * @param slot -- Controller slot
430 * @param ep -- Endpoint data
431 * @param physdata -- Physical memory area
432 */
433extern void USBScheduleInterrupt(USBDevice* dev, XHCISlot* slot, XHCIEndpoint* ep, uint64_t physdata);
434#endif
void * endpoint
Definition main.cpp:149
USBDevice * usb_device
Definition main.cpp:47
unsigned int uint32_t
Definition acefiex.h:163
unsigned char uint8_t
Definition acefiex.h:161
COMPILER_DEPENDENT_UINT64 uint64_t
Definition acefiex.h:165
unsigned short int uint16_t
Definition acefiex.h:162
Definition usb3.h:150
uint8_t request
Definition usb3.h:152
uint16_t index
Definition usb3.h:154
uint16_t length
Definition usb3.h:155
uint8_t request_type
Definition usb3.h:151
uint16_t value
Definition usb3.h:153
Definition usb3.h:76
uint32_t dword1
Definition usb3.h:85
uint16_t ushort2
Definition usb3.h:84
uint16_t ushort1
Definition usb3.h:83
uint32_t dword2
Definition usb3.h:86
uint8_t uchar4
Definition usb3.h:81
uint8_t uchar2
Definition usb3.h:79
uint8_t uchar3
Definition usb3.h:80
uint8_t uchar1
Definition usb3.h:78
uint8_t type
Definition usb3.h:77
uint8_t uchar5
Definition usb3.h:82
Definition x86_64_sched.h:118
Definition usb3.h:207
uint8_t bMaxPower
Definition usb3.h:215
uint8_t iConfiguration
Definition usb3.h:213
uint8_t bNumInterfaces
Definition usb3.h:211
uint8_t bLength
Definition usb3.h:208
uint16_t wTotalLength
Definition usb3.h:210
uint8_t bDescriptorType
Definition usb3.h:209
uint8_t bmAttributes
Definition usb3.h:214
uint8_t bConfigurationValue
Definition usb3.h:212
Definition usb3.h:160
uint16_t idVendor
Definition usb3.h:168
uint8_t bDescriptorType
Definition usb3.h:162
uint16_t idProduct
Definition usb3.h:169
uint8_t bDeviceSubClass
Definition usb3.h:165
uint8_t bLength
Definition usb3.h:161
uint8_t iManufacturer
Definition usb3.h:171
uint8_t bMaxPacketSize0
Definition usb3.h:167
uint8_t iSerialNumber
Definition usb3.h:173
uint16_t bcdDevice
Definition usb3.h:170
uint8_t bDeviceClass
Definition usb3.h:164
uint8_t iProduct
Definition usb3.h:172
uint8_t bDeviceProtocol
Definition usb3.h:166
uint8_t bNumConfigurations
Definition usb3.h:174
uint16_t bcdUSB
Definition usb3.h:163
Definition xhci.h:183
Definition usb3.h:228
uint8_t bEndpointAddress
Definition usb3.h:231
uint8_t bmAttributes
Definition usb3.h:232
uint8_t bLength
Definition usb3.h:229
uint16_t wMaxPacketSize
Definition usb3.h:233
uint8_t bInterval
Definition usb3.h:234
uint8_t bDescriptorType
Definition usb3.h:230
Definition usb3.h:179
uint8_t bInterfaceSubClass
Definition usb3.h:186
uint8_t bAlternateSetting
Definition usb3.h:183
uint8_t iInterface
Definition usb3.h:188
uint8_t bLength
Definition usb3.h:180
uint8_t bDescriptorType
Definition usb3.h:181
uint8_t bNumEndpoints
Definition usb3.h:184
uint8_t bInterfaceClass
Definition usb3.h:185
uint8_t bInterfaceProtocol
Definition usb3.h:187
uint8_t bInterfaceNumber
Definition usb3.h:182
Definition list.h:46
Definition usb3.h:193
uint8_t bMaxPacketSize0
Definition usb3.h:200
uint16_t bcdUSB
Definition usb3.h:196
uint8_t bNumConfigurations
Definition usb3.h:201
uint8_t bDeviceProtocol
Definition usb3.h:199
uint8_t bDeviceSubClass
Definition usb3.h:198
uint8_t bLength
Definition usb3.h:194
uint8_t bDeviceClass
Definition usb3.h:197
uint8_t bDescriptorType
Definition usb3.h:195
uint8_t bReserved
Definition usb3.h:202
Definition spinlock.h:37
Definition usb3.h:220
uint8_t bDescriptorType
Definition usb3.h:222
uint8_t bLength
Definition usb3.h:221
Definition usb3.h:239
uint8_t bDescriptorType
Definition usb3.h:241
uint8_t bLength
Definition usb3.h:240
Definition usb3.h:40
uint64_t cmd_ring_phys
Definition usb3.h:57
Spinlock * usb_lock
Definition usb3.h:70
unsigned evnt_ring_cycle
Definition usb3.h:64
uint32_t num_slots
Definition usb3.h:51
xhci_port_regs_t * ports
Definition usb3.h:50
uint16_t max_intrs
Definition usb3.h:53
unsigned cmd_ring_cycle
Definition usb3.h:60
xhci_trb_t * cmd_ring
Definition usb3.h:56
unsigned cmd_ring_max
Definition usb3.h:59
xhci_cap_regs_t * cap_regs
Definition usb3.h:45
unsigned cmd_ring_index
Definition usb3.h:58
xhci_doorbell_regs_t * db_regs
Definition usb3.h:47
uint32_t num_ports
Definition usb3.h:52
int poll_event_for_trb
Definition usb3.h:66
list_t * slot_list
Definition usb3.h:44
xhci_runtime_regs_t * rt_regs
Definition usb3.h:48
xhci_ext_cap_t * ext_cap
Definition usb3.h:49
AuThread * usb_thread
Definition usb3.h:43
uint8_t irq
Definition usb3.h:54
xhci_op_regs_t * op_regs
Definition usb3.h:46
int trb_event_index
Definition usb3.h:69
bool initialised
Definition usb3.h:41
uint64_t * event_ring_seg_phys
Definition usb3.h:62
uint64_t * dev_ctx_base_array
Definition usb3.h:55
unsigned evnt_ring_index
Definition usb3.h:63
bool is_csz_64
Definition usb3.h:42
uint64_t * event_ring_segment
Definition usb3.h:61
unsigned evnt_ring_max
Definition usb3.h:65
bool event_available
Definition usb3.h:67
int poll_return_trb_type
Definition usb3.h:68
Definition xhci.h:98
Definition xhci.h:148
Definition xhci.h:163
Definition xhci.h:112
Definition xhci.h:174
Definition xhci.h:140
Definition xhci.h:202
Definition xhci.h:154
void XHCIEventRingInit(USBDevice *dev)
Definition usb3.cpp:343
void XHCICommandRingInit(USBDevice *dev)
Definition usb3.cpp:111
void USBGetDeviceDesc(USBDevice *dev, XHCISlot *slot, uint8_t slot_id, uint64_t buffer, uint16_t len)
Definition usbstd.cpp:43
int XHCIPollEvent(USBDevice *usb_device, int trb_type)
Definition usb3.cpp:533
void XHCIRingDoorbellSlot(USBDevice *dev, uint8_t slot, uint32_t endpoint)
Definition usb3.cpp:491
struct _string_desc_ usb_string_desc_t
void XHCIDeviceContextInit(USBDevice *dev)
Definition usb3.cpp:84
struct _endpoint_desc_ usb_endpoint_desc_t
void XHCISendCommandSlot(XHCISlot *slot, uint32_t param1, uint32_t param2, uint32_t status, uint32_t ctrl)
Definition usb3.cpp:419
struct _config_desc_ usb_config_desc_t
usb_descriptor_t * USBGetDescriptor(XHCISlot *slot, uint8_t type)
Definition usbstd.cpp:109
XHCIEndpoint * XHCISlotGetEP_DCI(XHCISlot *slot, uint8_t endp_num)
Definition usb3.cpp:221
XHCISlot * XHCIGetSlotByID(USBDevice *dev, uint8_t slot_id)
Definition usb3.cpp:197
void XHCIReset(USBDevice *dev)
Definition usb3.cpp:72
struct __usb_msg__ USBMessage
void XHCIProtocolInit(USBDevice *dev)
Definition usb3.cpp:296
void USBScheduleInterrupt(USBDevice *dev, XHCISlot *slot, XHCIEndpoint *ep, uint64_t physdata)
Definition usbstd.cpp:128
void USBSetConfigDesc(USBDevice *dev, XHCISlot *slot, uint8_t slot_id, uint8_t configval)
Definition usbstd.cpp:98
void XHCIStartDefaultPorts(USBDevice *dev)
Definition usb3.cpp:906
struct _interface_desc_ usb_if_desc_t
XHCIEndpoint * XHCISlotGetEP(XHCISlot *slot, uint8_t endp_num)
Definition usb3.cpp:210
void XHCISendCommandEndpoint(XHCISlot *slot, uint8_t endp_num, uint32_t param1, uint32_t param2, uint32_t status, uint32_t ctrl)
Definition usb3.cpp:449
USBDevice * USBGetMainDevice()
Definition main.cpp:325
void XHCISendCommandMultiple(USBDevice *dev, xhci_trb_t *trb, int num_count)
Definition usb3.cpp:502
struct _usb_desc_ usb_descriptor_t
void XHCIRingDoorbell(USBDevice *dev)
Definition usb3.cpp:479
void USBGetConfigDesc(USBDevice *dev, XHCISlot *slot, uint8_t slot_id, uint64_t buffer, uint16_t len, uint8_t id)
Definition usbstd.cpp:80
void USBGetStringDesc(USBDevice *dev, XHCISlot *slot, uint8_t slot_id, uint64_t buffer, uint16_t id)
Definition usbstd.cpp:62
struct _dev_desc_ usb_dev_desc_t
struct _usb_dev_ USBDevice
void XHCISendCommand(USBDevice *dev, uint32_t param1, uint32_t param2, uint32_t status, uint32_t ctrl)
Definition usb3.cpp:389
struct _qualifier_desc_ usb_qualifier_desc_t
void XHCIPortInitialize(USBDevice *dev, unsigned int port)
Definition usb3.cpp:599