2 Copyright 2009-2010, jimmikaelkael
3 Licenced under Academic Free License version 3.0
4 Review Open PS2 Loader README & LICENSE files for further details.
10 static int mcman_mc_port
= 0;
11 static int mcman_mc_slot
= 0;
12 static int mcman_io_sema
= 0;
14 // mc driver ops functions prototypes
15 int mc_init(iop_device_t
*iop_dev
);
16 int mc_deinit(iop_device_t
*dev
);
17 int mc_open(iop_file_t
*f
, char *filename
, int mode
, int flags
);
18 int mc_close(iop_file_t
*f
);
19 int mc_lseek(iop_file_t
*f
, int pos
, int where
);
20 int mc_read(iop_file_t
*f
, void *buf
, int size
);
21 int mc_write(iop_file_t
*f
, void *buf
, int size
);
22 int mc_format(iop_file_t
*f
, char *a1
, char *a2
, void *a3
, int a4
);
23 int mc_remove(iop_file_t
*f
, char *filename
);
24 int mc_mkdir(iop_file_t
*f
, char *dirname
);
25 int mc_rmdir(iop_file_t
*f
, char *dirname
);
26 int mc_dopen(iop_file_t
*f
, char *dirname
);
27 int mc_dclose(iop_file_t
*f
);
28 int mc_dread(iop_file_t
*f
, fio_dirent_t
*dirent
);
29 int mc_getstat(iop_file_t
*f
, char *filename
, fio_stat_t
*stat
);
30 int mc_chstat(iop_file_t
*f
, char *filename
, fio_stat_t
*stat
, u32 statmask
);
31 int mc_ioctl(iop_file_t
*f
, int a1
, void* a2
);
33 // driver ops func tab
34 void *mcman_mcops
[17] = {
55 static iop_device_t mcman_mcdev
= {
60 (struct _iop_device_ops
*)&mcman_mcops
63 //--------------------------------------------------------------
64 int mc_init(iop_device_t
*dev
)
69 //--------------------------------------------------------------
70 int mcman_ioerrcode(int errcode
)
72 register int r
= errcode
;
105 //--------------------------------------------------------------
106 int mcman_modloadcb(char *filename
, int *unit
, u8
*arg3
)
108 register char *path
= filename
;
113 while (*path
== 0x20)
117 if (((u8
)path
[0] | 0x20) != mcman_mcdev
.name
[0])
119 if (((u8
)path
[1] | 0x20) != mcman_mcdev
.name
[1])
122 if ((u32
)strlen(path
) < 2) return 2;
124 upos
= mcman_chrpos(path
, ':');
131 if (((u8
)path
[upos
] - 0x30) < 10)
132 *unit
= (u8
)path
[upos
] - 0x30;
143 for (m
= 1; ((u8
)path
[upos
--] - 0x30) < 10; m
= v
<< 1) {
145 *arg3
+= m
* (path
[upos
] - 0x30);
153 //--------------------------------------------------------------
154 void mcman_unit2card(u32 unit
)
156 mcman_mc_port
= unit
& 1;
157 mcman_mc_slot
= (unit
>> 1) & (MCMAN_MAXSLOT
- 1);
159 // original mcman/xmcman code below is silly and I doubt it
160 // can support more than 2 units anyway...
162 register u32 mask = 0xF0000000;
164 while (!(unit & mask)) {
170 mcman_mc_port = unit & 0xf;
175 mcman_mc_slot = ((u32)(unit & mask)) / ((u32)(mask & (mask >> 0x3))) \
176 + (((mcman_mc_slot << 2) + mcman_mc_slot) << 1);
183 //--------------------------------------------------------------
184 int mcman_initdev(void)
188 SetCheckKelfPathCallback(mcman_modloadcb
);
190 DelDrv(mcman_mcdev
.name
);
191 if (AddDrv(&mcman_mcdev
)) {
200 mcman_io_sema
= CreateSema(&smp
);
205 //--------------------------------------------------------------
206 int mc_deinit(iop_device_t
*dev
)
208 DeleteSema(mcman_io_sema
);
214 //--------------------------------------------------------------
215 int mc_open(iop_file_t
*f
, char *filename
, int mode
, int flags
)
219 WaitSema(mcman_io_sema
);
220 mcman_unit2card(f
->unit
);
222 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
224 r
= McOpen(mcman_mc_port
, mcman_mc_slot
, filename
, mode
);
226 f
->privdata
= (void*)r
;
228 SignalSema(mcman_io_sema
);
230 return mcman_ioerrcode(r
);
233 //--------------------------------------------------------------
234 int mc_close(iop_file_t
*f
)
238 WaitSema(mcman_io_sema
);
239 r
= McClose((int)f
->privdata
);
240 SignalSema(mcman_io_sema
);
242 return mcman_ioerrcode(r
);
245 //--------------------------------------------------------------
246 int mc_lseek(iop_file_t
*f
, int pos
, int where
)
250 WaitSema(mcman_io_sema
);
251 r
= McSeek((int)f
->privdata
, pos
, where
);
252 SignalSema(mcman_io_sema
);
254 return mcman_ioerrcode(r
);
257 //--------------------------------------------------------------
258 int mc_read(iop_file_t
*f
, void *buf
, int size
)
262 WaitSema(mcman_io_sema
);
263 r
= McRead((int)f
->privdata
, buf
, size
);
264 SignalSema(mcman_io_sema
);
266 return mcman_ioerrcode(r
);
269 //--------------------------------------------------------------
270 int mc_write(iop_file_t
*f
, void *buf
, int size
)
274 WaitSema(mcman_io_sema
);
275 r
= McWrite((int)f
->privdata
, buf
, size
);
276 SignalSema(mcman_io_sema
);
278 return mcman_ioerrcode(r
);
281 //--------------------------------------------------------------
282 int mc_format(iop_file_t
*f
, char *a1
, char *a2
, void *a3
, int a4
)
286 WaitSema(mcman_io_sema
);
287 mcman_unit2card(f
->unit
);
289 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
291 r
= McFormat(mcman_mc_port
, mcman_mc_slot
);
293 SignalSema(mcman_io_sema
);
295 return mcman_ioerrcode(r
);
298 //--------------------------------------------------------------
299 int mc_remove(iop_file_t
*f
, char *filename
)
303 WaitSema(mcman_io_sema
);
304 mcman_unit2card(f
->unit
);
306 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
308 r
= McDelete(mcman_mc_port
, mcman_mc_slot
, filename
, 0);
310 SignalSema(mcman_io_sema
);
312 return mcman_ioerrcode(r
);
315 //--------------------------------------------------------------
316 int mc_mkdir(iop_file_t
*f
, char *dirname
)
320 WaitSema(mcman_io_sema
);
321 mcman_unit2card(f
->unit
);
323 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
325 r
= McOpen(mcman_mc_port
, mcman_mc_slot
, dirname
, 0x40);
327 SignalSema(mcman_io_sema
);
329 return mcman_ioerrcode(r
);
332 //--------------------------------------------------------------
333 int mc_rmdir(iop_file_t
*f
, char *dirname
)
337 WaitSema(mcman_io_sema
);
338 mcman_unit2card(f
->unit
);
340 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
342 r
= McDelete(mcman_mc_port
, mcman_mc_slot
, dirname
, 0);
344 SignalSema(mcman_io_sema
);
346 return mcman_ioerrcode(r
);
349 //--------------------------------------------------------------
350 int mc_dopen(iop_file_t
*f
, char *dirname
)
354 WaitSema(mcman_io_sema
);
355 mcman_unit2card(f
->unit
);
357 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
359 r
= McOpen(mcman_mc_port
, mcman_mc_slot
, dirname
, 0);
361 f
->privdata
= (void*)r
;
364 SignalSema(mcman_io_sema
);
366 return mcman_ioerrcode(r
);
369 //--------------------------------------------------------------
370 int mc_dclose(iop_file_t
*f
)
374 WaitSema(mcman_io_sema
);
375 r
= McClose((int)f
->privdata
);
376 SignalSema(mcman_io_sema
);
378 return mcman_ioerrcode(r
);
381 //--------------------------------------------------------------
382 int mc_dread(iop_file_t
*f
, fio_dirent_t
*dirent
)
386 WaitSema(mcman_io_sema
);
387 r
= mcman_dread((int)f
->privdata
, dirent
);
388 SignalSema(mcman_io_sema
);
390 return mcman_ioerrcode(r
);
393 //--------------------------------------------------------------
394 int mc_getstat(iop_file_t
*f
, char *filename
, fio_stat_t
*stat
)
398 WaitSema(mcman_io_sema
);
399 mcman_unit2card(f
->unit
);
401 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
403 r
= mcman_getstat(mcman_mc_port
, mcman_mc_slot
, filename
, stat
);
406 SignalSema(mcman_io_sema
);
408 return mcman_ioerrcode(r
);
411 //--------------------------------------------------------------
412 int mc_chstat(iop_file_t
*f
, char *filename
, fio_stat_t
*stat
, u32 statmask
)
414 register int r
, flags
;
415 sceMcTblGetDir mctbl
;
417 WaitSema(mcman_io_sema
);
418 mcman_unit2card(f
->unit
);
420 r
= McDetectCard2(mcman_mc_port
, mcman_mc_slot
);
422 if (statmask
& SCE_CST_ATTR
) {
424 mctbl
.Reserve2
= stat
->attr
;
428 if (statmask
& SCE_CST_MODE
) {
430 if (stat
->mode
& SCE_STM_R
) mctbl
.AttrFile
|= sceMcFileAttrReadable
;
431 else mctbl
.AttrFile
&= (unsigned short)~sceMcFileAttrReadable
;
433 if (stat
->mode
& SCE_STM_W
) mctbl
.AttrFile
|= sceMcFileAttrWriteable
;
434 else mctbl
.AttrFile
&= (unsigned short)~sceMcFileAttrWriteable
;
436 if (stat
->mode
& SCE_STM_X
) mctbl
.AttrFile
|= sceMcFileAttrExecutable
;
437 else mctbl
.AttrFile
&= (unsigned short)~sceMcFileAttrExecutable
;
439 if (stat
->mode
& SCE_STM_C
) mctbl
.AttrFile
|= sceMcFileAttrDupProhibit
;
440 else mctbl
.AttrFile
&= (unsigned short)~sceMcFileAttrDupProhibit
;
442 if (stat
->mode
& sceMcFileAttrPS1
) mctbl
.AttrFile
|= sceMcFileAttrPS1
;
443 else mctbl
.AttrFile
&= (unsigned short)~sceMcFileAttrPS1
;
445 if (stat
->mode
& sceMcFileAttrPDAExec
) mctbl
.AttrFile
|= sceMcFileAttrPDAExec
;
446 else mctbl
.AttrFile
&= (unsigned short)~sceMcFileAttrPDAExec
;
449 if (statmask
& SCE_CST_CT
) {
451 mctbl
._Create
= *((sceMcStDateTime
*)&stat
->ctime
[0]);
454 if (statmask
& SCE_CST_MT
) {
456 mctbl
._Modify
= *((sceMcStDateTime
*)&stat
->mtime
[0]);
459 r
= McSetFileInfo(mcman_mc_port
, mcman_mc_slot
, filename
, &mctbl
, flags
);
461 SignalSema(mcman_io_sema
);
463 return mcman_ioerrcode(r
);
466 //--------------------------------------------------------------
467 int mc_ioctl(iop_file_t
*f
, int a1
, void* a2
)
469 WaitSema(mcman_io_sema
);
473 //--------------------------------------------------------------