PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
mem.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
13UsbdDevice_t *fetchDeviceById(int devId)
14{
15 UsbdDevice_t *dev;
16
17 if ( devId <= 0 )
18 {
19 return NULL;
20 }
21 if ( devId >= usbConfig.m_maxDevices )
22 {
23 return NULL;
24 }
25 dev = &memPool->m_deviceTreeBuf[devId];
26 if ( !dev->m_parent )
27 return NULL;
28 return dev;
29}
30
31UsbdEndpoint_t *fetchEndpointById(int id)
32{
34
35 if ( id < 0 )
36 {
37 return NULL;
38 }
39 if ( id >= usbConfig.m_maxEndpoints )
40 {
41 return NULL;
42 }
43 ep = &memPool->m_endpointBuf[id];
44 if ( !ep->m_correspDevice )
45 return NULL;
46 return ep;
47}
48
49UsbdDevice_t *getDeviceTreeRoot(void)
50{
51 return memPool->m_deviceTreeRoot;
52}
53
54UsbdDevice_t *attachChildDevice(UsbdDevice_t *parent, u32 portNum)
55{
56 UsbdDevice_t *newDev;
57
58 newDev = memPool->m_freeDeviceListStart;
59 if ( !newDev )
60 {
61 dbg_printf("Ran out of device handles\n");
62 return NULL;
63 }
64 if ( newDev->m_next )
65 newDev->m_next->m_prev = newDev->m_prev;
66 else
67 memPool->m_freeDeviceListEnd = newDev->m_prev;
68 if ( newDev->m_prev )
69 newDev->m_prev->m_next = newDev->m_next;
70 else
71 memPool->m_freeDeviceListStart = newDev->m_next;
72 newDev->m_endpointListEnd = NULL;
73 newDev->m_endpointListStart = NULL;
74 newDev->m_devDriver = NULL;
75 newDev->m_deviceStatus = DEVICE_NOTCONNECTED;
76 newDev->m_resetFlag = 0;
77 newDev->m_magicPowerValue = 0;
78 newDev->m_childListEnd = NULL;
79 newDev->m_childListStart = NULL;
80 newDev->m_parent = parent;
81 newDev->m_attachedToPortNo = portNum;
82 newDev->m_privDataField = NULL;
83 if ( parent )
84 {
85 newDev->m_prev = parent->m_childListEnd;
86 if ( parent->m_childListEnd )
87 parent->m_childListEnd->m_next = newDev;
88 else
89 parent->m_childListStart = newDev;
90 newDev->m_next = NULL;
91 parent->m_childListEnd = newDev;
92 }
93 return newDev;
94}
95
96void freeDevice(UsbdDevice_t *dev)
97{
98 if ( !dev || dev < memPool->m_deviceTreeBuf || dev >= memPool->m_deviceTreeBuf + usbConfig.m_maxDevices )
99 {
100 dbg_printf("freeDevice %p: Arg is not part of dev buffer\n", dev);
101 return;
102 }
103 dev->m_prev = memPool->m_freeDeviceListEnd;
104 if ( memPool->m_freeDeviceListEnd )
105 memPool->m_freeDeviceListEnd->m_next = dev;
106 else
107 memPool->m_freeDeviceListStart = dev;
108 dev->m_next = NULL;
109 memPool->m_freeDeviceListEnd = dev;
110 dev->m_parent = NULL;
111}
112
113UsbdIoRequest_t *allocIoRequest(void)
114{
115 UsbdIoRequest_t *res;
116
117 res = memPool->m_freeIoReqList;
118 if ( !res )
119 {
120 dbg_printf("ran out of IoReqs\n");
121 return NULL;
122 }
123 if ( res->m_next )
124 res->m_next->m_prev = res->m_prev;
125 else
126 memPool->m_freeIoReqListEnd = res->m_prev;
127 if ( res->m_prev )
128 res->m_prev->m_next = res->m_next;
129 else
130 memPool->m_freeIoReqList = res->m_next;
131 return res;
132}
133
134void freeIoRequest(UsbdIoRequest_t *req)
135{
136 UsbdIoRequest_t *pos;
137
138 if ( !req )
139 {
140 return;
141 }
142 if ( req < memPool->m_ioReqBufPtr || req >= memPool->m_ioReqBufPtr + usbConfig.m_maxIoReqs )
143 {
144 req->m_busyFlag = 0;
145 return;
146 }
147 for ( pos = memPool->m_freeIoReqList; pos && pos != req; pos = pos->m_next )
148 {
149 }
150 if ( pos )
151 {
152 dbg_printf("freeIoRequest %p: already free.\n", req);
153 return;
154 }
155 req->m_busyFlag = 0;
156 req->m_prev = memPool->m_freeIoReqListEnd;
157 if ( memPool->m_freeIoReqListEnd )
158 memPool->m_freeIoReqListEnd->m_next = req;
159 else
160 memPool->m_freeIoReqList = req;
161 req->m_next = NULL;
162 memPool->m_freeIoReqListEnd = req;
163}
164
165UsbdEndpoint_t *allocEndpointForDevice(UsbdDevice_t *dev, u32 align)
166{
167 UsbdEndpoint_t *newEp;
168
169 newEp = memPool->m_freeEpListStart;
170 if ( !newEp )
171 {
172 return NULL;
173 }
174 if ( newEp->m_next )
175 newEp->m_next->m_prev = newEp->m_prev;
176 else
177 memPool->m_freeEpListEnd = newEp->m_prev;
178 if ( newEp->m_prev )
179 newEp->m_prev->m_next = newEp->m_next;
180 else
181 memPool->m_freeEpListStart = newEp->m_next;
182 newEp->m_correspDevice = dev;
183 newEp->m_ioReqListEnd = NULL;
184 newEp->m_ioReqListStart = NULL;
185 newEp->m_busyPrev = NULL;
186 newEp->m_busyNext = NULL;
187 newEp->m_inTdQueue = 0;
188 newEp->m_alignFlag = align;
189 newEp->m_prev = dev->m_endpointListEnd;
190 if ( dev->m_endpointListEnd )
191 dev->m_endpointListEnd->m_next = newEp;
192 else
193 dev->m_endpointListStart = newEp;
194 newEp->m_next = NULL;
195 dev->m_endpointListEnd = newEp;
196 return newEp;
197}
198
199int cleanUpFunc(UsbdDevice_t *dev, UsbdEndpoint_t *ep)
200{
201 if ( !ep || ep < memPool->m_endpointBuf || ep >= memPool->m_endpointBuf + usbConfig.m_maxEndpoints )
202 {
203 return 0;
204 }
205 if ( ep->m_inTdQueue != NOTIN_QUEUE )
206 {
207 if ( ep->m_busyNext )
208 ep->m_busyNext->m_busyPrev = ep->m_busyPrev;
209 else
210 memPool->m_tdQueueEnd = ep->m_busyPrev;
211 if ( ep->m_busyPrev )
212 ep->m_busyPrev->m_busyNext = ep->m_busyNext;
213 else
214 memPool->m_tdQueueStart = ep->m_busyNext;
215 ep->m_inTdQueue = NOTIN_QUEUE;
216 }
217 if ( ep->m_next )
218 ep->m_next->m_prev = ep->m_prev;
219 else
220 dev->m_endpointListEnd = ep->m_prev;
221 if ( ep->m_prev )
222 ep->m_prev->m_next = ep->m_next;
223 else
224 dev->m_endpointListStart = ep->m_next;
225 ep->m_prev = memPool->m_freeEpListEnd;
226 if ( memPool->m_freeEpListEnd )
227 memPool->m_freeEpListEnd->m_next = ep;
228 else
229 memPool->m_freeEpListStart = ep;
230 ep->m_next = NULL;
231 memPool->m_freeEpListEnd = ep;
232 ep->m_correspDevice = NULL;
233 return 0;
234}
235
236UsbdHcTD_t *allocTd(void)
237{
238 UsbdHcTD_t *res;
239
240 res = memPool->m_freeHcTdList;
241 if ( !res )
242 {
243 return NULL;
244 }
245 memPool->m_freeHcTdList = res->m_next;
246 res->m_next = NULL;
247 return res;
248}
249
250void freeTd(UsbdHcTD_t *argTd)
251{
252 UsbdHcTD_t *pos;
253
254 if ( !argTd )
255 {
256 return;
257 }
258 for ( pos = memPool->m_freeHcTdList; pos && argTd != pos; pos = pos->m_next )
259 {
260 }
261 if ( pos )
262 {
263 dbg_printf("FreeTD %p: already free\n", argTd);
264 return;
265 }
266 argTd->m_next = memPool->m_freeHcTdList;
267 memPool->m_freeHcTdList = argTd;
268}
269
270UsbdHcIsoTD_t *allocIsoTd(void)
271{
272 UsbdHcIsoTD_t *newTd;
273
274 newTd = memPool->m_freeHcIsoTdList;
275 if ( !newTd )
276 {
277 return NULL;
278 }
279 memPool->m_freeHcIsoTdList = newTd->m_next;
280 newTd->m_next = NULL;
281 return newTd;
282}
283
284void freeIsoTd(UsbdHcIsoTD_t *argTd)
285{
286 UsbdHcIsoTD_t *pos;
287
288 if ( !argTd )
289 {
290 return;
291 }
292 for ( pos = memPool->m_freeHcIsoTdList; pos && argTd != pos; pos = pos->m_next )
293 {
294 }
295 if ( pos )
296 {
297 dbg_printf("freeIsoTd %p: already free\n", argTd);
298 return;
299 }
300 argTd->m_next = memPool->m_freeHcIsoTdList;
301 memPool->m_freeHcIsoTdList = argTd;
302}