PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
device_driver.c
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
11#include "usbdpriv.h"
12
13int callUsbDriverFunc(int (*func)(int devId), int devId, void *gpSeg)
14{
15 int res;
16
17 if ( !func )
18 return 0;
19 usbdUnlock();
20#if USE_GP_REGISTER
21 SetGP(gpSeg);
22#endif
23 res = func(devId);
24#if USE_GP_REGISTER
25 SetGP(_gp);
26#endif
27 usbdLock();
28 return res;
29}
30
31static void probeDeviceTree(UsbdDevice_t *tree, sceUsbdLddOps *drv)
32{
33 UsbdDevice_t *curDevice;
34
35 for ( curDevice = tree->m_childListStart; curDevice; curDevice = curDevice->m_next )
36 {
37 if ( curDevice->m_deviceStatus == DEVICE_READY )
38 {
39 if ( curDevice->m_devDriver )
40 {
41 if ( curDevice->m_childListStart )
42 probeDeviceTree(curDevice, drv);
43 }
44 else
45 {
46 curDevice->m_privDataField = NULL;
47 if ( callUsbDriverFunc(drv->probe, curDevice->m_id, drv->gp) )
48 {
49 curDevice->m_devDriver = drv;
50 callUsbDriverFunc(drv->connect, curDevice->m_id, drv->gp);
51 }
52 }
53 }
54 }
55}
56
57int doRegisterDriver(sceUsbdLddOps *drv, void *drvGpSeg)
58{
59 if ( drv->next || drv->prev || !drv->name || drv->reserved1 || drv->reserved2 )
60 {
61 return USB_RC_BADDRIVER;
62 }
63 if ( drvListStart == drv )
64 {
65 return USB_RC_BUSY;
66 }
67 drv->gp = drvGpSeg;
68 drv->prev = drvListEnd;
69 if ( drvListEnd )
70 drvListEnd->next = drv;
71 else
72 drvListStart = drv;
73 drv->next = NULL;
74 drvListEnd = drv;
75 if ( drv->probe )
76 {
77 probeDeviceTree(getDeviceTreeRoot(), drv);
78 }
79 return USB_RC_OK;
80}
81
82int doRegisterAutoLoader(sceUsbdLddOps *drv, void *drvGpSeg)
83{
84 if ( drv->next || drv->prev || !drv->name || drv->reserved1 || drv->reserved2 )
85 {
86 return USB_RC_BADDRIVER;
87 }
88 if ( drvAutoLoader )
89 {
90 return USB_RC_BUSY;
91 }
92 drv->gp = drvGpSeg;
93 drvAutoLoader = drv;
94 if ( drv->probe )
95 {
96 probeDeviceTree(getDeviceTreeRoot(), drv);
97 }
98 return USB_RC_OK;
99}
100
101static void disconnectDriver(UsbdDevice_t *tree, sceUsbdLddOps *drv)
102{
103 UsbdEndpoint_t *ep;
104 UsbdEndpoint_t *nextEp;
105 UsbdDevice_t *tree_tmp1;
106
107 if ( drv == tree->m_devDriver )
108 {
109 if ( tree->m_endpointListStart )
110 {
111 ep = tree->m_endpointListStart->m_next;
112 for ( nextEp = ep; nextEp; ep = nextEp )
113 {
114 nextEp = ep->m_next;
115 removeEndpointFromDevice(tree, ep);
116 }
117 }
118 tree->m_devDriver = NULL;
119 tree->m_privDataField = NULL;
120 }
121 for ( tree_tmp1 = tree->m_childListStart; tree_tmp1; tree_tmp1 = tree_tmp1->m_next )
122 {
123 disconnectDriver(tree_tmp1, drv);
124 }
125}
126
127int doUnregisterDriver(sceUsbdLddOps *drv)
128{
129 sceUsbdLddOps *pos;
130
131 for ( pos = drvListStart; pos && pos != drv; pos = pos->next )
132 {
133 }
134 if ( !pos )
135 return USB_RC_BADDRIVER;
136 if ( drv->next )
137 drv->next->prev = drv->prev;
138 else
139 drvListEnd = drv->prev;
140 if ( drv->prev )
141 drv->prev->next = drv->next;
142 else
143 drvListStart = drv->next;
144 disconnectDriver(getDeviceTreeRoot(), drv);
145 return USB_RC_OK;
146}
147
148int doUnregisterAutoLoader(void)
149{
150 drvAutoLoader = NULL;
151 return USB_RC_OK;
152}
char * name
Definition usbd.h:51
#define USB_RC_BADDRIVER
Definition usbd.h:273
#define USB_RC_OK
Definition usbd.h:238
#define USB_RC_BUSY
Definition usbd.h:287