2 Copyright 2009, jimmikaelkael
3 Licenced under Academic Free License version 3.0
4 Review Open-ps2-loader README & LICENSE files for further details.
10 #include <io_common.h>
15 #define MODNAME "cddev"
16 IRX_ID(MODNAME
, 1, 1);
34 int sceCdInit(int init_mode
); // #4
35 int sceCdRead(u32 lsn
, u32 sectors
, void *buf
, cd_read_mode_t
*mode
); // #6
36 int sceCdSync(int mode
); // #11
37 int sceCdDiskReady(int mode
); // #13
38 int sceCdLayerSearchFile(cdl_file_t
*fp
, const char *name
, int layer
); // #84
40 // driver ops protypes
41 int cddev_dummy(void);
42 int cddev_init(iop_device_t
*dev
);
43 int cddev_deinit(iop_device_t
*dev
);
44 int cddev_open(iop_file_t
*f
, char *filename
, int mode
);
45 int cddev_close(iop_file_t
*f
);
46 int cddev_read(iop_file_t
*f
, void *buf
, u32 size
);
47 int cddev_lseek(iop_file_t
*f
, u32 offset
, int where
);
49 // driver ops func tab
50 void *cddev_ops
[17] = {
71 static iop_device_t cddev_dev
= {
76 (struct _iop_device_ops
*)&cddev_ops
86 #define MAX_FDHANDLES 64
87 FHANDLE cddev_fdhandles
[MAX_FDHANDLES
];
89 static int cddev_io_sema
;
91 #define CDDEV_FS_SECTORS 30
92 #define CDDEV_FS_BUFSIZE CDDEV_FS_SECTORS * 2048
93 static u8 cddev_fs_buf
[CDDEV_FS_BUFSIZE
+ 2*2048] __attribute__((aligned(64)));
95 //--------------------------------------------------------------
96 int _start(int argc
, char** argv
)
99 AddDrv((iop_device_t
*)&cddev_dev
);
103 return MODULE_RESIDENT_END
;
106 //--------------------------------------------------------------
107 int cddev_dummy(void)
112 //--------------------------------------------------------------
113 int cddev_init(iop_device_t
*dev
)
122 cddev_io_sema
= CreateSema(&smp
);
127 //--------------------------------------------------------------
128 int cddev_deinit(iop_device_t
*dev
)
130 DeleteSema(cddev_io_sema
);
135 //--------------------------------------------------------------
136 FHANDLE
*cddev_getfilefreeslot(void)
141 for (i
=0; i
<MAX_FDHANDLES
; i
++) {
142 fh
= (FHANDLE
*)&cddev_fdhandles
[i
];
150 //--------------------------------------------------------------
151 int cddev_open(iop_file_t
*f
, char *filename
, int mode
)
160 WaitSema(cddev_io_sema
);
162 fh
= cddev_getfilefreeslot();
165 r
= sceCdLayerSearchFile(&cdfile
, filename
, 0);
167 r
= sceCdLayerSearchFile(&cdfile
, filename
, 1);
170 SignalSema(cddev_io_sema
);
176 fh
->lsn
= cdfile
.lsn
;
177 fh
->filesize
= cdfile
.size
;
184 SignalSema(cddev_io_sema
);
189 //--------------------------------------------------------------
190 int cddev_close(iop_file_t
*f
)
192 FHANDLE
*fh
= (FHANDLE
*)f
->privdata
;
194 WaitSema(cddev_io_sema
);
197 memset(fh
, 0, sizeof(FHANDLE
));
199 SignalSema(cddev_io_sema
);
204 //--------------------------------------------------------------
205 int cddev_read(iop_file_t
*f
, void *buf
, u32 size
)
207 FHANDLE
*fh
= (FHANDLE
*)f
->privdata
;
208 register int rpos
, sectorpos
;
209 register u32 nsectors
, nbytes
;
213 if ((fh
->position
+ size
) > fh
->filesize
)
214 size
= fh
->filesize
- fh
->position
;
216 WaitSema(cddev_io_sema
);
221 nbytes
= CDDEV_FS_BUFSIZE
;
225 nsectors
= nbytes
>> 11;
226 sectorpos
= fh
->position
& 2047;
234 sceCdRead(fh
->lsn
+ ((fh
->position
& -2048) >> 11), nsectors
, cddev_fs_buf
, NULL
);
236 memcpy(buf
, &cddev_fs_buf
[sectorpos
], nbytes
);
241 fh
->position
+= nbytes
;
244 SignalSema(cddev_io_sema
);
249 //--------------------------------------------------------------
250 int cddev_lseek(iop_file_t
*f
, u32 offset
, int where
)
253 FHANDLE
*fh
= (FHANDLE
*)f
->privdata
;
255 WaitSema(cddev_io_sema
);
259 r
= fh
->position
+ offset
;
260 if (r
> fh
->filesize
) {
267 if (fh
->filesize
< offset
) {
283 SignalSema(cddev_io_sema
);
288 //-------------------------------------------------------------------------
289 DECLARE_IMPORT_TABLE(cdvdman
, 1, 1)
290 DECLARE_IMPORT(4, sceCdInit
)
291 DECLARE_IMPORT(6, sceCdRead
)
292 DECLARE_IMPORT(11, sceCdSync
)
293 DECLARE_IMPORT(13, sceCdDiskReady
)
294 DECLARE_IMPORT(84, sceCdLayerSearchFile
)