mgh: fix for default HDD DMA mode, that wasn't correctly set
[open-ps2-loader.git] / modules / cdvd / discID / discID.c
bloba1b6447765377eb6f7c9fac40be82165e0cc6176
1 /*
2 Copyright 2009, jimmikaelkael
3 Licenced under Academic Free License version 3.0
4 Review OpenUsbLd README & LICENSE files for further details.
5 */
7 #include <loadcore.h>
8 #include <stdio.h>
9 #include <sifman.h>
10 #include <intrman.h>
11 #include <sysclib.h>
12 #include <ioman.h>
13 #include <io_common.h>
14 #include <thsemap.h>
15 #include <errno.h>
17 #define MODNAME "discID"
18 IRX_ID(MODNAME, 1, 1);
20 #define CDVDreg_30 (*(volatile unsigned char *)0xBF402030)
21 #define CDVDreg_31 (*(volatile unsigned char *)0xBF402031)
22 #define CDVDreg_32 (*(volatile unsigned char *)0xBF402032)
23 #define CDVDreg_33 (*(volatile unsigned char *)0xBF402033)
24 #define CDVDreg_34 (*(volatile unsigned char *)0xBF402034)
25 #define CDVDreg_KEYSTATE (*(volatile unsigned char *)0xBF402038)
26 #define CDVDreg_KEYXOR (*(volatile unsigned char *)0xBF402039)
28 typedef struct {
29 u8 trycount;
30 u8 spindlctrl;
31 u8 datapattern;
32 u8 pad;
33 } cd_read_mode_t;
35 // cdvdman imports
36 int sceCdInit(int init_mode); // #4
37 int sceCdStandby(void); // #5
38 int sceCdSync(int mode); // #11
39 int sceCdGetDiskType(void); // #12
40 int sceCdDiskReady(int mode); // #13
41 int sceCdStop(void); // #15
42 int sceCdReadKey(int arg1, int arg2, u32 lsn, char *key); // #35
44 // driver ops protypes
45 int discID_dummy(void);
46 int discID_init(iop_device_t *dev);
47 int discID_deinit(iop_device_t *dev);
48 int discID_open(iop_file_t *f, char *filename, int mode);
49 int discID_read(iop_file_t *f, void *buf, u32 size);
50 int discID_close(iop_file_t *f);
52 // driver ops func tab
53 void *discID_ops[17] = {
54 (void*)discID_init,
55 (void*)discID_deinit,
56 (void*)discID_dummy,
57 (void*)discID_open,
58 (void*)discID_close,
59 (void*)discID_read,
60 (void*)discID_dummy,
61 (void*)discID_dummy,
62 (void*)discID_dummy,
63 (void*)discID_dummy,
64 (void*)discID_dummy,
65 (void*)discID_dummy,
66 (void*)discID_dummy,
67 (void*)discID_dummy,
68 (void*)discID_dummy,
69 (void*)discID_dummy
72 // driver descriptor
73 static iop_device_t discID_dev = {
74 "discID",
75 IOP_DT_FS,
77 "discID",
78 (struct _iop_device_ops *)&discID_ops
81 iop_file_t discID_fhandle;
82 static int discID_io_sema = -1;
84 //--------------------------------------------------------------
85 int discID_dummy(void)
87 return -EPERM;
90 //--------------------------------------------------------------
91 int discID_init(iop_device_t *dev)
93 iop_sema_t smp;
95 smp.initial = 1;
96 smp.max = 1;
97 smp.attr = 1;
98 smp.option = 0;
100 discID_io_sema = CreateSema(&smp);
102 return 0;
105 //--------------------------------------------------------------
106 int discID_deinit(iop_device_t *dev)
108 DeleteSema(discID_io_sema);
110 return 0;
113 //--------------------------------------------------------------
114 int discID_open(iop_file_t *f, char *filename, int mode)
116 register int r = 0;
117 iop_file_t *fh = &discID_fhandle;
119 if (!filename)
120 return -ENOENT;
122 WaitSema(discID_io_sema);
124 if (!fh->privdata) {
125 f->privdata = fh;
126 memcpy(fh, f, sizeof(iop_file_t));
128 sceCdInit(0);
129 sceCdDiskReady(0);
130 sceCdStandby();
131 sceCdSync(0);
133 r = 0;
135 else
136 r = -EMFILE;
138 SignalSema(discID_io_sema);
140 return r;
143 //--------------------------------------------------------------
144 int discID_close(iop_file_t *f)
146 iop_file_t *fh = (iop_file_t *)f->privdata;
148 WaitSema(discID_io_sema);
150 if (fh)
151 memset(fh, 0, sizeof(iop_file_t));
153 sceCdStop();
154 sceCdSync(0);
156 SignalSema(discID_io_sema);
158 return 0;
161 //--------------------------------------------------------------
162 int discID_read(iop_file_t *f, void *buf, u32 size)
164 register int r = 0;
165 u8 key[16];
166 u8 discID[16];
168 WaitSema(discID_io_sema);
170 memset(key, 0, 16);
171 memset(discID, 0, 16);
173 r = sceCdGetDiskType();
174 if ((r >= 0x12) && (r < 0x15)) { // check it's PS2 Disc
176 sceCdReadKey(0, 0, 0x4b, key);
177 sceCdSync(0);
179 if (CDVDreg_KEYSTATE & 4) {
180 discID[0] = CDVDreg_30;
181 discID[0] ^= CDVDreg_KEYXOR;
182 discID[1] = CDVDreg_31;
183 discID[1] ^= CDVDreg_KEYXOR;
184 discID[2] = CDVDreg_32;
185 discID[2] ^= CDVDreg_KEYXOR;
186 discID[3] = CDVDreg_33;
187 discID[3] ^= CDVDreg_KEYXOR;
188 discID[4] = CDVDreg_34;
189 discID[4] ^= CDVDreg_KEYXOR;
191 memcpy(buf, discID, 5);
193 r = 5;
195 else
196 r = -EIO;
198 else
199 r = -ENODEV;
201 SignalSema(discID_io_sema);
203 return r;
206 //-------------------------------------------------------------------------
207 int _start(int argc, char** argv)
209 DelDrv("discID");
210 AddDrv((iop_device_t *)&discID_dev);
212 return MODULE_RESIDENT_END;
215 //-------------------------------------------------------------------------
216 DECLARE_IMPORT_TABLE(cdvdman, 1, 1)
217 DECLARE_IMPORT(4, sceCdInit)
218 DECLARE_IMPORT(5, sceCdStandby)
219 DECLARE_IMPORT(11, sceCdSync)
220 DECLARE_IMPORT(12, sceCdGetDiskType)
221 DECLARE_IMPORT(13, sceCdDiskReady)
222 DECLARE_IMPORT(15, sceCdStop)
223 DECLARE_IMPORT(35, sceCdReadKey)
224 END_IMPORT_TABLE