1 |
/* |
2 |
* PearPC |
3 |
* cd.h |
4 |
* |
5 |
* Copyright (C) 2003 Sebastian Biallas (sb@biallas.net) |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License version 2 as |
9 |
* published by the Free Software Foundation. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#ifndef __CD_H__ |
22 |
#define __CD_H__ |
23 |
|
24 |
#include "idedevice.h" |
25 |
|
26 |
struct MSF |
27 |
{ |
28 |
uint8 m, s, f; |
29 |
}; |
30 |
|
31 |
typedef uint32 LBA; |
32 |
|
33 |
// Flags for IDEDevice::mMode |
34 |
#define IDE_ATAPI_TRANSFER_HDR_SYNC 0x80 |
35 |
#define IDE_ATAPI_TRANSFER_HDR_SECTOR_SUB 0x40 |
36 |
#define IDE_ATAPI_TRANSFER_HDR_SECTOR 0x20 |
37 |
#define IDE_ATAPI_TRANSFER_DATA 0x10 |
38 |
#define IDE_ATAPI_TRANSFER_ECC 0x08 |
39 |
|
40 |
struct Sense { |
41 |
uint8 sense_key; |
42 |
uint8 info[4]; |
43 |
uint8 spec_info[4]; |
44 |
uint8 key_spec[3]; |
45 |
uint8 fruc; |
46 |
uint8 asc; |
47 |
uint8 ascq; |
48 |
}; |
49 |
|
50 |
enum CDROMReceiveResult { |
51 |
CDROMReceiveOK, |
52 |
CDROMReceiveNop, |
53 |
CDROMReceiveError |
54 |
}; |
55 |
|
56 |
class CDROMDevice: public IDEDevice { |
57 |
protected: |
58 |
bool mLocked; |
59 |
bool mReady; |
60 |
Container *mFeatures, *mProfiles; |
61 |
int curProfile; |
62 |
bool is_dvd; |
63 |
public: |
64 |
CDROMDevice(const char *name); |
65 |
virtual ~CDROMDevice (); |
66 |
|
67 |
virtual bool isReady(); |
68 |
bool isLocked(); |
69 |
virtual bool setLock(bool aLocked); |
70 |
bool toggleLock(); |
71 |
static void MSFfromLBA(MSF & msf, LBA lba); |
72 |
static void LBAfromMSF(LBA & lba, MSF msf); |
73 |
virtual bool validLBA(LBA lba); |
74 |
virtual int getConfig(byte *buf, int len, byte RT, int first); |
75 |
virtual uint32 getCapacity() = 0; |
76 |
virtual int modeSense(byte *buf, int len, int pc, int page); |
77 |
virtual uint getBlockSize(); |
78 |
virtual uint getBlockCount(); |
79 |
virtual bool setReady(bool aReady); |
80 |
virtual int readTOC(byte *buf, bool msf, uint8 starttrack, int len, |
81 |
int format) = 0; |
82 |
virtual void eject() = 0; |
83 |
virtual int writeBlock(byte *buf); |
84 |
|
85 |
virtual int readDVDStructure(byte *buf, int len, uint8 subcommand, uint32 address, uint8 layer, uint8 format, uint8 AGID, uint8 control); |
86 |
virtual void activateDVD(bool onoff); |
87 |
virtual bool isDVD(void); |
88 |
|
89 |
protected: |
90 |
void addFeature(int feature); |
91 |
void addProfile(int profile); |
92 |
virtual int getFeature(byte *buf, int len, int feature); |
93 |
int createFeature(byte *buf, int len, int feature, int version, |
94 |
bool pp, bool cur, byte *add, int size); |
95 |
int put(byte *buf, int len, byte *src, int size); |
96 |
}; |
97 |
|
98 |
class CDROMDeviceFile: public CDROMDevice { |
99 |
SYS_FILE *mFile; |
100 |
LBA curLBA; |
101 |
uint32 mCapacity; |
102 |
public: |
103 |
CDROMDeviceFile(const char *name); |
104 |
virtual ~CDROMDeviceFile(); |
105 |
|
106 |
virtual uint32 getCapacity(); |
107 |
bool changeDataSource(const char *file); |
108 |
virtual bool seek(uint64 blockno); |
109 |
virtual void flush(); |
110 |
virtual int readBlock(byte *buf); |
111 |
virtual int readTOC(byte *buf, bool msf, uint8 starttrack, int len, |
112 |
int format); |
113 |
virtual void eject(); |
114 |
|
115 |
virtual bool promSeek(uint64 pos); |
116 |
virtual uint promRead(byte *buf, uint size); |
117 |
}; |
118 |
|
119 |
/// Generic interface for SCSI based implementations of a CD drive |
120 |
class CDROMDeviceSCSI: public CDROMDevice { |
121 |
private: |
122 |
/// Current seek position |
123 |
LBA curLBA; |
124 |
|
125 |
/// Seek position for PROM (byte) |
126 |
uint64 prompos; |
127 |
|
128 |
/// Read ahead buffer |
129 |
byte *data_buffer; |
130 |
|
131 |
/// First buffered sector |
132 |
LBA buffer_base; |
133 |
|
134 |
/// Size of read ahead buffer in sectors |
135 |
uint buffer_size; |
136 |
|
137 |
////////////////////////////////// |
138 |
// Internal utility functions |
139 |
////////////////////////////////// |
140 |
private: |
141 |
/// Set's the simulated drive's ready state based on the physical one's |
142 |
void checkReady(); |
143 |
|
144 |
/// Support function for byte-wise reading from CD (for PROM) |
145 |
uint readData(byte *buf, uint64 pos, uint count); |
146 |
|
147 |
/// Actual sector reading function with read-ahead buffer |
148 |
bool readBufferedData(byte *buf, uint sector); |
149 |
|
150 |
|
151 |
////////////////////////////////// |
152 |
// Abstract interface for impls. |
153 |
////////////////////////////////// |
154 |
protected: |
155 |
/// Handle SRB composition / sending / waiting |
156 |
virtual byte SCSI_ExecCmd(byte command, byte dir, byte params[8], |
157 |
byte *buffer = 0, uint buffer_len = 0) = 0; |
158 |
|
159 |
////////////////////////////////// |
160 |
// SCSI helper functions |
161 |
////////////////////////////////// |
162 |
private: |
163 |
/// Reads a number of sectors from the physical media |
164 |
bool SCSI_ReadSectors(uint32 start, byte *buffer, |
165 |
uint buffer_bytes, |
166 |
uint sectors); |
167 |
|
168 |
////////////////////////////////// |
169 |
// CDROMDevice interface |
170 |
////////////////////////////////// |
171 |
public: |
172 |
/// Returns the ready state |
173 |
virtual bool isReady(); |
174 |
|
175 |
/// Sets the tray lock state |
176 |
virtual bool setLock(bool lock); |
177 |
|
178 |
/// Returns the size of the inserted media in sectors |
179 |
virtual uint32 getCapacity(); |
180 |
|
181 |
/// Sets the current seek position to the specified block |
182 |
virtual bool seek(uint64 blockno); |
183 |
|
184 |
/// Flushes the write buffer (empty function here) |
185 |
virtual void flush(); |
186 |
|
187 |
/// Reads a block from the media |
188 |
virtual int readBlock(byte *buf); |
189 |
|
190 |
/// Reads the CDs TOC |
191 |
virtual int readTOC(byte *buffer, bool msf, uint8 starttrack, |
192 |
int len, int format); |
193 |
|
194 |
virtual int getConfig(byte *buf, int len, byte RT, int first); |
195 |
virtual int readDVDStructure(byte *buf, int len, uint8 subcommand, uint32 address, uint8 layer, uint8 format, uint8 AGID, uint8 control); |
196 |
|
197 |
/// Ejects the tray |
198 |
virtual void eject(); |
199 |
|
200 |
/// Seek function for PROM |
201 |
virtual bool promSeek(uint64 pos); |
202 |
|
203 |
/// Read function for PROM |
204 |
virtual uint promRead(byte *buf, uint size); |
205 |
|
206 |
|
207 |
////////////////////////////////// |
208 |
// Member functions |
209 |
////////////////////////////////// |
210 |
protected: |
211 |
/// Constructor |
212 |
CDROMDeviceSCSI(const char *name); |
213 |
|
214 |
/// Destructor |
215 |
virtual ~CDROMDeviceSCSI(); |
216 |
}; |
217 |
|
218 |
#endif |