ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
libcdvd.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # (C) 2002 Nicholas Van Veen (nickvv@xtra.co.nz)
7 # 2003 loser (loser@internalreality.com)
8 # (c) 2004 Marcus R. Brown <mrbrown@0xd6.org>
9 # Licenced under Academic Free License version 2.0
10 # Review ps2sdk README & LICENSE files for further details.
11 */
12 
23 #include <stdio.h>
24 #include <tamtypes.h>
25 #include <kernel.h>
26 #include <sifrpc.h>
27 #include <string.h>
28 #include <libcdvd.h>
29 #include <stdarg.h>
30 
31 #include "internal.h"
32 
33 // rpc bind values
34 #define CD_SERVER_INIT 0x80000592
35 #define CD_SERVER_SEARCHFILE 0x80000597
36 #define CD_SERVER_DISKREADY 0x8000059A
38 #define CD_SERVER_POFF 0x80000596
39 
41 extern void *_gp;
42 
43 // prototypes
44 void _CdSemaExit(void);
45 
47 typedef struct {
48  u8 padding[32];
49  char name[256];
50  void *dest;
52 
53 #ifdef F__libcdvd_internals
54 // bind variables
55 int bindInit = -1;
56 int bindDiskReady = -1;
57 int bindSearchFile = -1;
58 // rpc binded client data
60 SifRpcClientData_t clientInit __attribute__ ((aligned(64)));
62 SifRpcClientData_t clientDiskReady __attribute__ ((aligned(64)));
64 SifRpcClientData_t clientSearchFile __attribute__ ((aligned(64)));
65 
67 int CdDebug = 0;
68 
69 // semaphore ids
71 int callbackSemaId = -1;
73 volatile int cbSema = 0;
74 
75 // callbacks
76 volatile int CdCallbackNum;
78 
79 // threads
80 int CdThreadId = 0;
82 int callbackThreadId = 0;
84 
85 // current command variables
86 
87 s32 diskReadyMode __attribute__ ((aligned(64)));
88 s32 trayReqData __attribute__ ((aligned(64)));
89 u32 initMode __attribute__ ((aligned(64)));
90 
91 // searchfile stuff
92 SearchFilePkt searchFileSendBuff __attribute__ ((aligned(64)));
93 u32 searchFileRecvBuff __attribute__ ((aligned(64)));
94 #endif
95 
96 // Prototypes for multimodule
97 extern int bindInit;
98 extern int bindDiskReady;
99 extern int bindSearchFile;
103 extern int CdDebug;
104 extern int callbackSemaId;
105 extern volatile int cbSema;
106 extern volatile int CdCallbackNum;
107 extern volatile sceCdCBFunc sceCdCallbackFunc;
108 extern int CdThreadId;
110 extern int callbackThreadId;
112 extern s32 diskReadyMode;
113 extern s32 trayReqData;
114 extern u32 initMode;
116 extern u32 searchFileRecvBuff;
117 
118 /* Other Functions */
119 
120 #ifdef F_sceCdInit
122 {
123  if (_CdSyncS(1))
124  return 0;
125  SifInitRpc(0);
127  bindSearchFile = -1;
128  bindNCmd = -1;
129  bindSCmd = -1;
130  bindDiskReady = -1;
131  bindInit = -1;
132 
133  while (1) {
134  if (SifBindRpc(&clientInit, CD_SERVER_INIT, 0) < 0) {
135  if (CdDebug > 0)
136  printf("Libcdvd bind err CD_Init\n");
137  } else if (clientInit.server != 0)
138  break;
139 
140  nopdelay();
141  }
142 
143  bindInit = 0;
144  initMode = mode;
145  if (SifCallRpc(&clientInit, 0, 0, &initMode, 4, 0, 0, 0, 0) < 0)
146  return 0;
147  if (mode == SCECdEXIT) {
148  if (CdDebug > 0)
149  printf("Libcdvd Exit\n");
150  _CdSemaExit();
151  nCmdSemaId = -1;
152  sCmdSemaId = -1;
153  callbackSemaId = -1;
154  } else {
155  _CdSemaInit();
156  }
157 
158  return 1;
159 }
160 #endif
161 
162 #ifdef F_sceCdIntToPos
164 {
165  p->minute = (((((i + 150) / 75) / 60) / 10) * 16) + ((((i + 150) / 75) / 60) % 10);
166  p->second = (((((i + 150) / 75) % 60) / 10) * 16) + ((((i + 150) / 75) % 60) % 10);
167  p->sector = ((((i + 150) % 75) / 10) * 16) + (((i + 150) % 75) % 10);
168  return p;
169 }
170 #endif
171 
172 #ifdef F_sceCdPosToInt
174 {
175  return ((((p->minute / 16) * 10) + (p->minute & 0xF)) * 60 + ((p->second / 16) * 10) + (p->second & 0xF)
176  ) * 75 + (p->sector / 16) * 10 + (p->sector & 0xF) - 150;
177 }
178 #endif
179 
180 #ifdef F_sceCdSearchFile
181 s32 sceCdSearchFile(sceCdlFILE * file, const char *name)
182 {
183  _CdSemaInit();
185  return 0;
188  if (sceCdSync(1)) {
190  return 0;
191  }
192  SifInitRpc(0);
193  if (bindSearchFile < 0) {
194  while (1) {
196  if (CdDebug > 0)
197  printf("libsceCdvd bind err sceCdSearchFile\n");
198  }
199  if (clientSearchFile.server != 0)
200  break;
201 
202  nopdelay();
203  }
204  bindSearchFile = 0;
205  }
206 
207  strncpy(searchFileSendBuff.name, name, 255);
208  searchFileSendBuff.name[255] = '\0';
210 
211  if (CdDebug > 0)
212  printf("ee call cmd search %s\n", searchFileSendBuff.name);
215  return 0;
216  }
217 
218  memcpy(file, UNCACHED_SEG(&searchFileSendBuff), 32);
219 
220  if (CdDebug > 0) {
221  printf("search name %s\n", file->name);
222  printf("search size %d\n", file->size);
223  printf("search loc lnn %d\n", file->lsn);
224  printf("search loc date %02X %02X %02X %02X %02X %02X %02X %02X\n",
225  file->date[0], file->date[1], file->date[2], file->date[3],
226  file->date[4], file->date[5], file->date[6], file->date[7]);
227  printf("search loc date %02d %02d %02d %02d %02d %02d %02d %02d\n",
228  file->date[0], file->date[1], file->date[2], file->date[3],
229  file->date[4], file->date[5], file->date[6], file->date[7]);
230  }
231 
233  return *(s32*)UNCACHED_SEG(nCmdRecvBuff);
234 }
235 #endif
236 
237 #ifdef F_sceCdDiskReady
239 {
240  if (CdDebug > 0)
241  printf("DiskReady 0\n");
242 
243  _CdSemaInit();
245  return SCECdNotReady;
246  if (_CdSyncS(1)) {
248  return SCECdNotReady;
249  }
250 
251  SifInitRpc(0);
252  if (bindDiskReady < 0) {
253  while (1) {
255  if (CdDebug > 0)
256  printf("Libcdvd bind err CdDiskReady\n");
257  }
258  if (clientDiskReady.server != 0)
259  break;
260 
261  nopdelay();
262  }
263  }
264  bindDiskReady = 0;
266 
267  if (SifCallRpc(&clientDiskReady, 0, 0, &diskReadyMode, 4, sCmdRecvBuff, 4, NULL, NULL) < 0) {
269  return 6;
270  }
271  if (CdDebug > 0)
272  printf("DiskReady ended\n");
273 
275  return *(s32 *) UNCACHED_SEG(sCmdRecvBuff);
276 }
277 #endif
278 
279 #ifdef F__CdSemaInit
280 void _CdSemaInit(void)
281 {
282  struct t_ee_sema semaParam;
283 
284  // return if both semaphores are already inited
285  if (nCmdSemaId != -1 && sCmdSemaId != -1)
286  return;
287 
288  semaParam.init_count = 1;
289  semaParam.max_count = 1;
290  semaParam.option = 0;
291  nCmdSemaId = CreateSema(&semaParam);
292  sCmdSemaId = CreateSema(&semaParam);
293 
294  semaParam.init_count = 0;
295  callbackSemaId = CreateSema(&semaParam);
296 
297  cbSema = 0;
298 }
299 #endif
300 
301 #ifdef F__CdSemaExit
302 void _CdSemaExit(void)
303 {
304  if (callbackThreadId) {
305  CdCallbackNum = -1;
307  }
311 }
312 #endif
313 
314 #ifdef F_sceCdInitEeCB
315 static void _CdCallbackLoop(void);
316 s32 sceCdInitEeCB(s32 priority, void *stackAddr, s32 stackSize)
317 {
318  // if callback thread has already been initialised, just change its priority
319  if (callbackThreadId != 0) {
321  return 0;
322  }
323  // initialise callback thread
326  callbackThreadParam.stack_size = stackSize;
328  callbackThreadParam.func = &_CdCallbackLoop;
329  callbackThreadParam.stack = stackAddr;
333 
334  return 1;
335 }
336 #endif
337 
338 #ifdef F_sceCdCallback
340 {
341  sceCdCBFunc oldFunc = sceCdCallbackFunc;
342 
343  if (sceCdSync(1))
344  return 0;
345 
346  sceCdCallbackFunc = newFunc;
347  return oldFunc;
348 }
349 #endif
350 
351 /* Util Functions */
352 
353 
359 #ifdef F_sceCdInitEeCB
360 static void _CdCallbackLoop(void)
361 {
362  while (1) {
364 
365  // if callback number if -1, stop callbck thread loop
366  if (CdCallbackNum == -1)
367  ExitThread();
368 
369  if (CdDebug > 0)
370  printf("sceCdCallbackFunc = %08X CdCallbackNum = %d\n", (u32) sceCdCallbackFunc, CdCallbackNum);
371 
372  // if callback function number and 'custom callback function' pointer are valid, do callback
375 
376  cbSema = 0;
377  }
378 }
379 #endif
380 
382 #ifdef F__CdGenericCallbackFunction
383 void _CdGenericCallbackFunction(void *funcNum)
384 {
385  // set the currently executing function num
386  CdCallbackNum = *(s32*) funcNum;
388 
389  // check if user callback is registered
390  if (callbackThreadId)
391  {
392  if (sceCdCallbackFunc)
393  {
395  return;
396  }
397  }
398 
399  // clear the currently executing function num
400  cbSema = 0;
401  CdCallbackNum = 0;
402 }
403 #endif
s32 CreateSema(ee_sema_t *sema)
s32 SignalSema(s32 sema_id)
s32 DeleteSema(s32 sema_id)
s32 iSignalSema(s32 sema_id)
static void nopdelay(void)
Definition: kernel.h:141
s32 ReferThreadStatus(s32 thread_id, ee_thread_status_t *info)
s32 CreateThread(ee_thread_t *thread)
void ExitThread(void)
s32 PollSema(s32 sema_id)
s32 WaitSema(s32 sema_id)
s32 StartThread(s32 thread_id, void *args)
#define UNCACHED_SEG(x)
Definition: kernel.h:35
s32 ChangeThreadPriority(s32 thread_id, s32 priority)
s32 GetThreadId(void)
int sceCdSearchFile(sceCdlFILE *file, const char *name)
@ SCECdNotReady
int sceCdInitEeCB(int priority, void *stackAddr, int stackSize)
int sceCdInit(int mode)
sceCdCBFunc sceCdCallback(sceCdCBFunc function)
int sceCdDiskReady(int mode)
sceCdlLOCCD * sceCdIntToPos(u32 i, sceCdlLOCCD *p)
u32 sceCdPosToInt(sceCdlLOCCD *p)
int sceCdSync(int mode)
void(* sceCdCBFunc)(int reason)
@ SCECdEXIT
SearchFilePkt searchFileSendBuff
int bindDiskReady
void _CdSemaExit(void)
int bindSearchFile
u32 searchFileRecvBuff
u32 initMode
SifRpcClientData_t clientDiskReady
int bindInit
volatile int cbSema
int CdDebug
int callbackSemaId
volatile int CdCallbackNum
SifRpcClientData_t clientSearchFile
void * _gp
#define CD_SERVER_DISKREADY
Definition: libcdvd.c:36
ee_thread_status_t CdThreadParam
ee_thread_t callbackThreadParam
#define CD_SERVER_INIT
Definition: libcdvd.c:34
int CdThreadId
#define CD_SERVER_SEARCHFILE
Definition: libcdvd.c:35
s32 diskReadyMode
SifRpcClientData_t clientInit
s32 trayReqData
volatile sceCdCBFunc sceCdCallbackFunc
int callbackThreadId
u8 nCmdRecvBuff[]
int sCmdSemaId
void _CdSemaInit(void)
void _CdGenericCallbackFunction(void *funcNum)
int bindNCmd
u8 sCmdRecvBuff[]
int nCmdNum
int nCmdSemaId
int _CdSyncS(int mode)
int bindSCmd
s32 mode
Definition: rpc_client.c:15
void SifInitRpc(int mode)
int SifBindRpc(SifRpcClientData_t *client, int rpc_number, int mode)
int SifCallRpc(SifRpcClientData_t *client, int rpc_number, int mode, void *send, int ssize, void *receive, int rsize, SifRpcEndFunc_t end_function, void *end_param)
void * dest
Definition: libcdvd.c:50
char name[256]
Definition: libcdvd.c:49
struct t_SifRpcServerData * server
Definition: sifrpc.h:142
void * gp_reg
Definition: kernel.h:230
int stack_size
Definition: kernel.h:229
void * func
Definition: kernel.h:227
void * stack
Definition: kernel.h:228
int initial_priority
Definition: kernel.h:231
char name[16]
#define NULL
Definition: tamtypes.h:91
signed int s32
Definition: tamtypes.h:58
unsigned int u32
Definition: tamtypes.h:30
unsigned char u8
Definition: tamtypes.h:23