]> Shamusworld >> Repos - virtualjaguar/blob - src/cdintf_linux.cpp
d8d73d00e9df18e0eaec477431ab5ff4d7904700
[virtualjaguar] / src / cdintf_linux.cpp
1 //
2 // OS specific CDROM interface (linux)
3 //
4 // by James L. Hammons and Niels Wagenaar
5 //
6 // NOTE : This CD-ROM code *could* work with other UN*X related OS. However,
7 //        we/I are not sure on this matter.
8 //
9 // This is very experimental and I have the feeling that this won't even compile.
10 // Hell, I don't even have a Linux dev system (broken) or Jaguar CD releases to
11 // test this code :(
12 //
13 // Big thanks to the DOSBOX team which provided us with great knowlegde about
14 // CD-ROM access and Linux/UN*X.
15
16 #ifdef LINUX_JAGUAR_CDROM
17
18 // *** OS dependent CDROM stuffola ***
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <linux/cdrom.h>
22 #include <sys/ioctl.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 // *** End OS dependent ***
27 #include "log.h"
28 #include "string.h"
29
30 // *** Virtual Jaguar dependent ***
31 //#include "SDL.h"                // Yes, we use SDL for initializing the CD-ROM and
32 //                                // give us access to certain CD-ROM states. But not just yet.
33
34 // *** SDL CD-ROM dependent ***        // Not yet needed!
35 // SDL_CD      *cdrom;                 // Our variable for SDL CD-ROM access.
36 // CDstatus     status;                // Let us get our status.
37 // char        *status_str;
38
39 // *** Local variables ***
40 char                   device_name[512] ; // Devicename, for example /dev/cdrom
41
42 //
43 // Linux support functions
44 // OS specific implementation of OS agnostic functions
45 //
46
47 bool CDIntfInit(void)
48 {
49     // Setting device_name to /deb/cdrom. /dev/cdrom is the default CD-ROM
50     // drive on most UN*X systems. Well, I think it is.
51     //
52     // In the future we can probably use SDL for getting CDROM states and
53     // CD-ROM specific information.
54     strcpy(device_name, "/dev/cdrom");
55
56     // Let us open the device_name and check if we can open the CD-ROM.
57     int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK);
58     
59         if (cdrom_fd <= 0) 
60     {
61         // CD-ROM isn't accessable.
62         // Write the error in the log file and return false.
63         WriteLog("CDINTF: CDIntfInit - Unable to open CDROM!\n");
64         return false;
65     }    
66     else
67     {
68         // CD-ROM is accessable.
69         // Write the success in the log file and return true.
70         WriteLog("CDINTF: CDIntfInit - Succesfully opened CDROM!\n"); 
71         close(device_name);
72         return true;
73     }
74         
75 }
76
77 void CDIntfDone(void)
78 {
79     // Just in case : closing device_name.
80     WriteLog("CDINTF: CDIntfDone - Closing CDROM!\n");
81     close(device_name);
82 }
83
84 bool CDIntfReadBlock(uint32 sector, uint8 * buffer)
85 {
86         unsigned int   buflen = CD_FRAMESIZE_RAW;          // Raw read, 2352 bytes per sector
87         //unsigned char *buf    = new unsigned int[buflen];     // DOSBOX, do we need this?
88         int ret;
89         struct cdrom_read cdrom_read;
90
91         // Let us open the device_name and check if we can open the CD-ROM.
92     int cdrom_fd = open(device_name, O_RDONLY | O_NONBLOCK);
93         if (cdrom_fd <= 0)
94     {
95         // CD-ROM isn't accessable.
96         // Write the error in the log file and return false.
97         WriteLog("CDINTF: CDIntfReadBlock - Unable to open CDROM!\n");
98         return false;
99     }    
100         
101         // Setting up the cdrom_read struct :
102         cdrom_read.cdread_lba = sector;             // Which sector to read.
103         cdrom_read.cdread_bufaddr = (char*)buffer;  // Where to put the data (?)
104         cdrom_read.cdread_buflen = buflen;          // 2352 bytes/sector -> RAW read
105                                           
106         // Let us read the content we want.     -1 (false) when it didn't work.
107         ret = ioctl(cdrom_fd, CDROMREADRAW, &cdrom_read);               
108
109         // Close the CD-ROM.
110         close(cdrom_fd);
111
112         // The following was taken from DOSBOX. After reading the content, they write
113         // back the information from buf (based upon the size of buflen) to buffer.
114         // I think that this is not needed. *fingers crossed*
115         //
116         // MEM_BlockWrite(buffer, buf, buflen);
117         // delete[] buf;
118         
119         // Uncomment the following for debug reasons.
120         //
121     // WriteLog("CDINTF: CDIntfReadBlock - Reading sector %d!\n", sector);
122     
123         return (ret > 0);
124
125 }
126
127 uint32 CDIntfGetNumSessions(void)
128 {
129         // Still need relevant code here...
130         return 2;
131 }
132
133 void CDIntfSelectDrive(uint32 driveNum)
134 {
135         WriteLog("CDINTF: SelectDrive unimplemented!\n");
136 }
137
138 uint32 CDIntfGetCurrentDrive(void)
139 {
140         WriteLog("CDINTF: GetCurrentDrive unimplemented!\n");
141         return 0;
142 }
143
144 const uint8 * CDIntfGetDriveName(uint32)
145 {
146         WriteLog("CDINTF: GetDriveName unimplemented!\n");
147         return NULL;
148 }
149
150 uint8 CDIntfGetSessionInfo(uint32 session, uint32 offset)
151 {
152         WriteLog("CDINTF: GetSessionInfo unimplemented!\n");
153         return 0xFF;
154 }
155
156 uint8 CDIntfGetTrackInfo(uint32 track, uint32 offset)
157 {
158         WriteLog("CDINTF: GetTrackInfo unimplemented!\n");
159         return 0xFF;
160 }
161
162 #else
163
164 #include "log.h"
165
166 //
167 // Linux support functions
168 // OS specific implementation of OS agnostic functions
169 //
170
171 bool CDIntfInit(void)
172 {
173         WriteLog("CDINTF: Init unimplemented!\n");
174         return false;
175 }
176
177 void CDIntfDone(void)
178 {
179 }
180
181 bool CDIntfReadBlock(uint32 sector, uint8 * buffer)
182 {
183         WriteLog("CDINTF: ReadBlock unimplemented!\n");
184         return false;
185 }
186
187 uint32 CDIntfGetNumSessions(void)
188 {
189         // Still need relevant code here... !!! FIX !!!
190         return 2;
191 }
192
193 void CDIntfSelectDrive(uint32 driveNum)
194 {
195         WriteLog("CDINTF: SelectDrive unimplemented!\n");
196 }
197
198 uint32 CDIntfGetCurrentDrive(void)
199 {
200         WriteLog("CDINTF: GetCurrentDrive unimplemented!\n");
201         return 0;
202 }
203
204 const uint8 * CDIntfGetDriveName(uint32)
205 {
206         WriteLog("CDINTF: GetDriveName unimplemented!\n");
207         return NULL;
208 }
209
210 uint8 CDIntfGetSessionInfo(uint32 session, uint32 offset)
211 {
212         WriteLog("CDINTF: GetSessionInfo unimplemented!\n");
213         return 0xFF;
214 }
215
216 uint8 CDIntfGetTrackInfo(uint32 track, uint32 offset)
217 {
218         WriteLog("CDINTF: GetTrackInfo unimplemented!\n");
219         return 0xFF;
220 }
221 #endif