PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
usbdpriv.h
Go to the documentation of this file.
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
16#ifndef __USBDPRIV_H__
17#define __USBDPRIV_H__
18
19#include "irx_imports.h"
20
21#include "defs.h"
22#include "types.h"
23#include "usbd.h"
24
25#define OHCI_REG_BASE 0xBF801600
26
27#ifdef DEBUG
28#define dbg_printf(a...) printf("usbd: " a)
29#else
30#define dbg_printf(a...) (void)0
31#endif
32
33#define READ_UINT16(a) (((u8 *)a)[0] | (((u8 *)a)[1] << 8))
34
35typedef struct _argOption
36{
37 const char *param;
38 int *value;
39 int *value2;
41
42typedef struct _usbdConfig
43{
44 int m_maxDevices;
45 int m_maxEndpoints;
46 int m_maxTransfDesc;
47 int m_maxIsoTransfDesc;
48 int m_maxIoReqs;
49 int m_maxStaticDescSize;
50 int m_maxHubDevices;
51 int m_maxPortsPerHub;
52 int m_allocatedSize_unused;
53 int m_hcdThreadPrio;
54 int m_cbThreadPrio;
55 int m_curDescNum;
57
58extern UsbdConfig_t usbConfig;
59
60struct _device;
61struct _ioRequest;
62struct _hcTd;
63struct _hcIsoTd;
64struct _hcEd;
65struct _endpoint;
66struct _UsbDriver;
67struct _ioRequest;
68
69typedef void (*TimerCallback)(void *arg);
70typedef void (*InternCallback)(struct _ioRequest *arg);
71
72typedef struct _timerCbStruct
73{
74 u32 m_isActive;
75 struct _timerCbStruct *m_prev;
76 struct _timerCbStruct *m_next;
77 TimerCallback m_callbackProc;
78 void *m_callbackArg;
79 u32 m_delayCount;
81
82typedef struct _ioRequest
83{
84 u32 m_id;
85 u32 m_busyFlag;
86 struct _ioRequest *m_next;
87 struct _ioRequest *m_prev;
88 struct _endpoint *m_correspEndpoint;
89 UsbDeviceRequest m_devReq;
90 void *m_destPtr;
91 u32 m_length; // length of m_destPtr buffer
92 InternCallback m_callbackProc;
93 u32 m_resultCode;
94 u32 m_transferedBytes;
95 u32 m_waitFrames; // number of frames to wait for isochronous transfers
96 void *m_userCallbackArg;
97 union
98 {
99 void *m_userCallbackProc;
100 sceUsbdDoneCallback m_userCallbackProcRegular;
101 sceUsbdMultiIsochronousDoneCallback m_userCallbackProcMultiIsochronous;
102 };
103 void *m_gpSeg;
106
107typedef struct _device
108{
109 u32 m_id;
110 struct _device *m_next;
111 struct _device *m_prev;
112 struct _endpoint *m_endpointListStart;
113 struct _endpoint *m_endpointListEnd;
114 sceUsbdLddOps *m_devDriver;
115 u8 m_deviceStatus;
116 u8 m_functionAddress;
117 u8 m_isLowSpeedDevice;
118 u8 m_resetFlag;
119 u32 m_magicPowerValue;
120 struct _device *m_childListStart;
121 struct _device *m_childListEnd;
122 struct _device *m_parent;
123 u32 m_attachedToPortNo;
124 void *m_privDataField;
125 UsbdTimerCbStruct_t m_timer;
126 UsbdIoRequest_t m_ioRequest;
127 u32 m_functionDelay;
128 void *m_staticDeviceDescPtr;
129 void *m_staticDeviceDescEndPtr;
130 u32 m_fetchDescriptorCounter;
131 struct _usbdReportDescriptor *m_reportDescriptorCurForFetch;
132 struct _usbdReportDescriptor *m_reportDescriptorStart;
133 struct _usbdReportDescriptor *m_reportDescriptorEnd;
135
136typedef struct _hcTd
137{
138 u32 m_hcArea;
139 void *m_curBufPtr;
140 struct _hcTd *m_next;
141 void *m_bufferEnd;
142} UsbdHcTD_t;
143
144typedef struct _hcIsoTd
145{
146 u32 m_hcArea;
147 void *m_bufferPage0;
148 struct _hcIsoTd *m_next;
149 void *m_bufferEnd;
150 u16 m_psw[8];
152
154{
155 u16 m_hcArea;
156 u16 m_maxPacketSize;
157};
158
160{
161 struct _hcEdHcArea stru;
162 u32 asu32;
163};
164
165typedef struct _hcEd
166{
167 union _hcEdHcAreaU m_hcArea;
168 UsbdHcTD_t *m_tdTail;
169 UsbdHcTD_t *m_tdHead;
170 struct _hcEd *m_next;
171} UsbdHcED_t;
172
173typedef struct _endpoint
174{
175 u32 m_id;
176 u32 m_inTdQueue;
177 struct _endpoint *m_next;
178 struct _endpoint *m_prev;
179 struct _endpoint *m_busyNext;
180 struct _endpoint *m_busyPrev;
181 UsbdDevice_t *m_correspDevice;
182 UsbdIoRequest_t *m_ioReqListStart;
183 UsbdIoRequest_t *m_ioReqListEnd;
184 UsbdHcED_t *m_hcEd;
185 u32 m_endpointType;
186 u32 m_isochronLastFrameNum; // 40
187 UsbdTimerCbStruct_t m_timer; // sizeof(UsbdTimerCbStruct_t) => 24 bytes
188 u32 m_alignFlag;
189 u8 m_schedulingIndex;
190 u8 m_waitHigh;
191 u8 m_waitLow;
192 u8 m_packetSizeForScheduling;
194
195typedef struct _usbHub
196{
197 struct _usbHub *m_next;
198 u32 m_pad1[1];
199 u32 m_curAllocatedCount;
200 UsbdDevice_t *m_dev;
201 UsbdEndpoint_t *m_controlEp;
202 UsbdEndpoint_t *m_statusChangeEp;
203 UsbdIoRequest_t m_controlIoReq;
204 UsbdIoRequest_t m_statusIoReq;
205 u32 m_maxPower;
206 u32 m_isSelfPowered;
207 UsbHubDescriptor m_desc;
208 u32 m_pad2[6];
209 u32 m_numChildDevices;
210 u32 m_portCounter;
211 u32 m_hubStatusCounter;
212 u16 m_hubStatus;
213 u16 m_hubStatusChange; // unite to u32 to make it match portStatusChange
214 u32 m_portStatusChange;
215 u8 m_statusChangeInfo[8]; // depends on number of ports
216 u32 m_pad3[6];
218
219typedef struct _hcCA
220{
221 volatile UsbdHcED_t *InterruptTable[32];
222 volatile u16 FrameNumber;
223 volatile u16 pad;
224 volatile UsbdHcTD_t *DoneHead;
225 volatile u8 reserved[116];
226 volatile u32 pad2; // expand struct to 256 bytes for alignment
227} HcCA;
228
229typedef struct _ohciRegs
230{
231 volatile u32 HcRevision;
232 volatile u32 HcControl;
233 volatile u32 HcCommandStatus;
234 volatile u32 HcInterruptStatus;
235 volatile u32 HcInterruptEnable;
236 volatile u32 HcInterruptDisable;
237 volatile HcCA *HcHCCA;
238 volatile UsbdHcED_t *HcPeriodCurrentEd;
239 volatile UsbdHcED_t *HcControlHeadEd;
240 volatile UsbdHcED_t *HcControlCurrentEd;
241 volatile UsbdHcED_t *HcBulkHeadEd;
242 volatile UsbdHcED_t *HcBulkCurrentEd;
243 volatile u32 HcDoneHead;
244 volatile u32 HcFmInterval;
245 volatile u32 HcFmRemaining;
246 volatile u32 HcFmNumber;
247 volatile u32 HcPeriodicStart;
248 volatile u32 HcLsThreshold;
249 volatile u32 HcRhDescriptorA;
250 volatile u32 HcRhDescriptorB;
251 volatile u32 HcRhStatus;
252 volatile u32 HcRhPortStatus[2];
253} OhciRegs;
254
255typedef struct _memPool
256{
257 volatile OhciRegs *m_ohciRegs;
258 volatile HcCA *m_hcHCCA;
259
260 struct _hcEd *m_hcEdBuf;
261
262 struct _hcTd *m_freeHcTdList;
263 struct _hcTd *m_hcTdBuf;
264 struct _hcTd *m_hcTdBufEnd;
265
266 struct _hcIsoTd *m_freeHcIsoTdList;
267 struct _hcIsoTd *m_hcIsoTdBuf;
268 struct _hcIsoTd *m_hcIsoTdBufEnd;
269
270 struct _ioRequest **m_hcTdToIoReqLUT;
271 struct _ioRequest **m_hcIsoTdToIoReqLUT;
272
273 struct _ioRequest *m_ioReqBufPtr;
274 struct _ioRequest *m_freeIoReqList;
275 struct _ioRequest *m_freeIoReqListEnd;
276
277 struct _device *m_deviceTreeBuf;
278 struct _device *m_freeDeviceListStart;
279 struct _device *m_freeDeviceListEnd;
280
281 struct _endpoint *m_endpointBuf;
282 struct _endpoint *m_freeEpListStart;
283 struct _endpoint *m_freeEpListEnd;
284
285 struct _endpoint *m_tdQueueStart;
286 struct _endpoint *m_tdQueueEnd;
287
288 u32 m_interruptBandwidthSchedulingValues[32];
289 u32 m_delayResets;
290 int m_interruptCounters[9];
291
292 struct _timerCbStruct *m_timerListStart;
293 struct _timerCbStruct *m_timerListEnd;
294
295 struct _device *m_deviceTreeRoot;
297
299{
300 struct _usbdReportDescriptor *m_next;
301 struct _usbdReportDescriptor *m_prev;
302 u32 m_cfgNum;
303 u32 m_ifNum;
304 u32 m_length;
305 u8 m_data[];
307
309{
310 int m_usbdSema;
311 int m_hcdTid;
312 int m_hcdIrqEvent;
313 int m_callbackTid;
314 int m_callbackEvent;
316
317#define NOTIN_QUEUE 0
318#define GENTD_QUEUE 1
319#define ISOTD_QUEUE 2
320
321#define TYPE_CONTROL 0x3F
322#define TYPE_BULK 0x40
323#define TYPE_ISOCHRON 0x41
324
325#define DEVICE_NOTCONNECTED 1
326#define DEVICE_CONNECTED 3
327#define DEVICE_RESETDELAYED 4
328#define DEVICE_RESETPENDING 5
329#define DEVICE_RESETCOMPLETE 6
330#define DEVICE_FETCHINGDESCRIPTOR 7
331#define DEVICE_READY 8
332
333#define PORT_CONNECTION 0
334#define PORT_ENABLE 1
335#define PORT_SUSPEND 2
336#define PORT_OVER_CURRENT 3
337#define PORT_RESET 4
338#define PORT_POWER 8
339#define PORT_LOW_SPEED 9
340
341#define C_HUB_LOCAL_POWER 0
342#define C_HUB_OVER_CURRENT 1
343
344#define C_PORT_CONNECTION 16
345#define C_PORT_ENABLE 17
346#define C_PORT_SUSPEND 18
347#define C_PORT_OVER_CURRENT 19
348#define C_PORT_RESET 20
349
350#define BIT(x) (((u32)1) << (x))
351
352#define C_PORT_FLAGS \
353 (BIT(C_PORT_CONNECTION) | BIT(C_PORT_ENABLE) | BIT(C_PORT_SUSPEND) | BIT(C_PORT_OVER_CURRENT) | BIT(C_PORT_RESET))
354
355#define HCED_DIR_OUT BIT(11) // Direction field
356#define HCED_DIR_IN BIT(12) // Direction field
357#define HCED_SPEED BIT(13) // Speed bit
358#define HCED_SKIP BIT(14) // Skip bit
359#define HCED_ISOC BIT(15) // Format bit
360#define HCED_DIR_MASK (HCED_DIR_OUT | HCED_DIR_IN)
361
362#define ED_HALTED(a) ((u32)((a).m_tdHead) & 1)
363#define ED_SKIPPED(a) ((u32)((a).m_hcArea.stru.m_hcArea) & HCED_SKIP)
364
365#define TD_HCAREA(CC, T, DI, DP, R) (u32)((((CC) << 12) | ((T) << 8) | ((DI) << 5) | ((DP) << 3) | ((R) << 2)))
366
367#define TD_SETUP 0
368#define TD_OUT 1
369#define TD_IN 2
370
371#define OHCI_INT_SO BIT(0)
372#define OHCI_INT_WDH BIT(1)
373#define OHCI_INT_SF BIT(2)
374#define OHCI_INT_RD BIT(3)
375#define OHCI_INT_UE BIT(4)
376#define OHCI_INT_FNO BIT(5)
377#define OHCI_INT_RHSC BIT(6)
378#define OHCI_INT_OC BIT(30)
379#define OHCI_INT_MIE BIT(31)
380
381#define OHCI_COM_HCR BIT(0)
382#define OHCI_COM_CLF BIT(1)
383#define OHCI_COM_BLF BIT(2)
384
385#define OHCI_CTR_CBSR (3 << 0) // Control / Bulk Service Ratio
386#define OHCI_CTR_PLE BIT(2) // Periodic List Enable
387#define OHCI_CTR_IE BIT(3) // Isochronous Enable
388#define OHCI_CTR_CLE BIT(4) // Control List Enable
389#define OHCI_CTR_BLE BIT(5) // Bulk List Enable
390#define OHCI_CTR_USB_RESET (0 << 6)
391#define OHCI_CTR_USB_RESUME (1 << 6)
392#define OHCI_CTR_USB_OPERATIONAL (2 << 6)
393#define OHCI_CTR_USB_SUSPEND (3 << 6)
394
395// The following is defined in hub.c
396extern void hubResetDevicePort(UsbdDevice_t *dev);
397extern int initHubDriver(void);
398extern void deinitHubDriver(void);
399
400// The following is defined in mem.c
401extern UsbdDevice_t *fetchDeviceById(int devId);
402extern UsbdEndpoint_t *fetchEndpointById(int id);
403extern UsbdDevice_t *getDeviceTreeRoot(void);
404extern UsbdDevice_t *attachChildDevice(UsbdDevice_t *parent, u32 portNum);
405extern void freeDevice(UsbdDevice_t *dev);
406extern UsbdIoRequest_t *allocIoRequest(void);
407extern void freeIoRequest(UsbdIoRequest_t *req);
408extern UsbdEndpoint_t *allocEndpointForDevice(UsbdDevice_t *dev, u32 align);
409extern int cleanUpFunc(UsbdDevice_t *dev, UsbdEndpoint_t *ep);
410extern UsbdHcTD_t *allocTd(void);
411extern void freeTd(UsbdHcTD_t *argTd);
412extern UsbdHcIsoTD_t *allocIsoTd(void);
413extern void freeIsoTd(UsbdHcIsoTD_t *argTd);
414
415// The following is defined in timer.c
416extern int addTimerCallback(UsbdTimerCbStruct_t *arg, TimerCallback func, void *cbArg, int delay);
417extern int cancelTimerCallback(UsbdTimerCbStruct_t *arg);
418extern void handleTimerList(void);
419
420// The following is defined in endpoint.c
421extern UsbdEndpoint_t *openDeviceEndpoint(UsbdDevice_t *dev, const UsbEndpointDescriptor *endpDesc, u32 alignFlag);
422extern int removeEndpointFromDevice(UsbdDevice_t *dev, UsbdEndpoint_t *ep);
423
424// The following is defined in io_request.c
425extern void handleIoReqList(UsbdEndpoint_t *ep);
426
427// The following is defined in hub_resets.c
428extern void usbdRebootInner(void);
429extern void hubResetDevice(UsbdDevice_t *dev);
430extern int checkDelayedResets(UsbdDevice_t *dev);
431extern void handleRhsc(void);
432
433// The following is defined in td_queue.c
434extern void processDoneQueue_GenTd(UsbdHcTD_t *arg);
435extern void processDoneQueue_IsoTd(UsbdHcIsoTD_t *arg);
436
437// The following is defined in hcd.c
438extern void hcdProcessIntr(void);
439extern void PostIntrEnableFunction(void);
440extern int initHcdStructs(void);
441extern void deinitHcd(void);
442
443// The following is defined in usbd_sys.c
444extern void *AllocSysMemoryWrap(int size);
445extern int FreeSysMemoryWrap(void *ptr);
446extern int usbdLock(void);
447extern int usbdUnlock(void);
448
449// The following is defined in usbd_main.c
450extern void usbdReboot(int ac);
451
452// The following is defined in report_descriptor_init.c
453extern int
454handleStaticDeviceDescriptor(UsbdDevice_t *dev, UsbDeviceDescriptor *devDescStart, UsbDeviceDescriptor *devDescEnd);
455
456// The following is defined in device_driver.c
457extern int callUsbDriverFunc(int (*func)(int devId), int devId, void *gpSeg);
458extern int doRegisterDriver(sceUsbdLddOps *drv, void *drvGpSeg);
459extern int doRegisterAutoLoader(sceUsbdLddOps *drv, void *drvGpSeg);
460extern int doUnregisterDriver(sceUsbdLddOps *drv);
461extern int doUnregisterAutoLoader(void);
462
463// The following is defined in device.c
464extern void *doGetDeviceStaticDescriptor(int devId, void *data, u8 type);
465extern int doGetDeviceLocation(UsbdDevice_t *dev, u8 *path);
466extern UsbdEndpoint_t *doOpenEndpoint(UsbdDevice_t *dev, const UsbEndpointDescriptor *endpDesc, u32 alignFlag);
467extern int doCloseEndpoint(UsbdEndpoint_t *ep);
468extern int attachIoReqToEndpoint(UsbdEndpoint_t *ep, UsbdIoRequest_t *req, void *destdata, u16 length, void *callback);
469extern int doControlTransfer(
470 UsbdEndpoint_t *ep,
471 UsbdIoRequest_t *req,
472 u8 requestType,
473 u8 request,
474 u16 value,
475 u16 index,
476 u16 length,
477 void *destdata,
478 void *callback);
479extern int hubTimedSetFuncAddress(UsbdDevice_t *dev);
480extern void flushPort(UsbdDevice_t *dev);
481extern int usbdInitInner(void);
482
483// The following is defined in usbd_main.c
484extern UsbdMemoryPool_t *memPool;
485extern UsbdKernelResources_t usbKernelResources;
486extern UsbdIoRequest_t *cbListStart;
487extern UsbdIoRequest_t *cbListEnd;
488extern UsbdConfig_t usbConfig;
489extern sceUsbdLddOps *drvListStart;
490extern sceUsbdLddOps *drvListEnd;
491extern sceUsbdLddOps *drvAutoLoader;
492
493#endif // __USBDPRIV_H__