/[pcsx2_0.9.7]/trunk/plugins/CDVDpeops/generic.c
ViewVC logotype

Contents of /trunk/plugins/CDVDpeops/generic.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File MIME type: text/plain
File size: 11608 byte(s)
committing r3113 initial commit again...
1 /***************************************************************************
2 generic.c - description
3 -------------------
4 begin : Sun Nov 16 2003
5 copyright : (C) 2003 by Pete Bernert
6 email : BlackDove@addcom.de
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. See also the license.txt file for *
15 * additional informations. *
16 * *
17 ***************************************************************************/
18
19 //*************************************************************************//
20 // History of changes:
21 //
22 // 2004/12/25 - Pete
23 // - repaired time2addr/addr2time
24 //
25 // 2003/11/16 - Pete
26 // - generic cleanup for the Peops release
27 //
28 //*************************************************************************//
29
30 /////////////////////////////////////////////////////////
31
32 #include "stdafx.h"
33 #define _IN_GENERIC
34 #include "externals.h"
35
36 /////////////////////////////////////////////////////////
37
38 int iCD_AD=-1; // drive address
39 int iCD_TA=-1;
40 int iCD_LU=-1;
41 int iRType=0; // read mode
42 int iUseSpeedLimit=0; // speed limit use
43 int iSpeedLimit=2; // speed 2x
44 int iNoWait=0; // wait
45 int iMaxRetry=5; // retry on error
46 int iShowReadErr=0; // show msg on error
47 HANDLE hEvent=NULL; // global event
48 HINSTANCE hASPI=NULL; // aspi lib
49 READFUNC pReadFunc=NULL; // read func
50 DEINITFUNC pDeInitFunc=NULL; // deinit func
51 int iInterfaceMode=1; // interface (aspi/ioctrlscsi/ioctrlraw)
52 int iWantedBlockSize=2352;
53 int iUsedBlockSize=2352;
54 int iUsedMode=CDVD_MODE_2352;
55 int iBlockDump=0;
56
57 DWORD (*pGetASPI32SupportInfo)(void); // ptrs to aspi funcs
58 DWORD (*pSendASPI32Command)(LPSRB);
59
60 /////////////////////////////////////////////////////////
61
62 void addr2time(unsigned long addr, unsigned char *time)
63 {
64 addr+=150;
65 time[3] = (unsigned char)(addr%75);
66 addr/=75;
67 time[2]=(unsigned char)(addr%60);
68 addr/=60;
69 time[1]=(unsigned char)(addr%100);
70 time[0]=(unsigned char)(addr/100);
71 }
72
73 void addr2timeB(unsigned long addr, unsigned char *time)
74 {
75 time[3] = itob((unsigned char)(addr%75));
76 addr/=75;
77 time[2]=itob((unsigned char)(addr%60));
78 addr/=60;
79 time[1]=itob((unsigned char)(addr%100));
80 time[0]=itob((unsigned char)(addr/100));
81 }
82
83 unsigned long time2addr(unsigned char *time)
84 {
85 unsigned long addr;
86
87 addr = time[0]*100;
88 addr += time[1];
89 addr *= 60;
90
91 addr = (addr + time[2])*75;
92 addr += time[3];
93 addr -= 150;
94 return addr;
95 }
96
97 unsigned long time2addrB(unsigned char *time)
98 {
99 unsigned long addr;
100
101 addr = (btoi(time[0]))*100;
102 addr += btoi(time[1]);
103 addr *= 60;
104 addr = (addr + btoi(time[2]))*75;
105 addr += btoi(time[3]);
106 addr -= 150;
107 return addr;
108 }
109
110 #ifndef _GCC
111
112 #ifdef __x86_64__
113 unsigned long reOrder(unsigned long value)
114 {
115 return ((value&0xff)<<24)|((value&0xff00)<<8)|((value&0xff0000)>>8)|(value>>24);
116 }
117 #else
118 unsigned long reOrder(unsigned long value)
119 {
120 #pragma warning (disable: 4035)
121 __asm
122 {
123 mov eax,value
124 bswap eax
125 }
126 }
127 #endif
128 #endif
129
130 /////////////////////////////////////////////////////////
131
132 void CreateGenEvent(void)
133 {
134 hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
135 }
136
137 /////////////////////////////////////////////////////////
138
139 void FreeGenEvent(void)
140 {
141 if(hEvent) CloseHandle(hEvent);
142 hEvent=0;
143 }
144
145 /////////////////////////////////////////////////////////
146
147 DWORD WaitGenEvent(DWORD dwMS)
148 {
149 if(hASPI) // aspi event
150 return WaitForSingleObject(hEvent,dwMS);
151 else
152 { // ioctl overlapped (always waiting til finished, dwMS not used)
153 DWORD dwR=0;
154 GetOverlappedResult(hIOCTL,&ovcIOCTL,&dwR,TRUE);
155 return 0;
156 }
157 }
158
159 /////////////////////////////////////////////////////////
160
161 void LockGenCDAccess(void)
162 {
163 if(hReadThread) // thread mode?
164 WaitForSingleObject(hThreadMutex[0],INFINITE); // -> wait until all reading is done
165 else // async prefetch?
166 if(bDoWaiting) // -> async operation has to finish first
167 {WaitGenEvent(0xFFFFFFFF);bDoWaiting=FALSE;}
168 }
169
170 /////////////////////////////////////////////////////////
171
172 void UnlockGenCDAccess(void)
173 {
174 if(hReadThread) // thread mode?
175 ReleaseMutex(hThreadMutex[0]); // -> we are finished with our special command, now reading can go on
176 }
177
178 /////////////////////////////////////////////////////////
179
180 void WaitUntilDriveIsReady(void)
181 {
182 if(iNoWait==0)
183 {
184 while(TestSCSIUnitReady()==0) Sleep(500);
185 }
186 }
187
188 /////////////////////////////////////////////////////////
189
190 void SetGenCDSpeed(int iReset)
191 {
192 if(iUseSpeedLimit)
193 {
194 if(bDoWaiting) // still a command running? wait
195 {WaitGenEvent(0xFFFFFFFF);bDoWaiting=FALSE;}
196
197 if(iReset) SetSCSISpeed(0xFFFF);
198 else
199 if(SetSCSISpeed(176*iSpeedLimit)<=0)
200 {
201 MessageBox(GetActiveWindow(),
202 "Failure: cannot change the drive speed!",
203 "cdvdPeops... speed limitation",
204 MB_OK|MB_ICONEXCLAMATION);
205 iUseSpeedLimit=0;
206 }
207 }
208 }
209
210 /////////////////////////////////////////////////////////
211 // checks, which direct subchannel reading type is supported
212
213 void GetBESubReadFunc(void)
214 {
215 unsigned char * pB=(unsigned char *)malloc(4096);
216
217 pReadFunc = ReadSCSI_BE_Sub; // pre-init read func
218
219 WaitUntilDriveIsReady(); // wait before first read
220
221 ReadSub_BE_2(0,pB,1); // first (unchecked) read
222 if(!ReadSub_BE_2(0,pB,1)) // read again, and check this time
223 { // -> read failed?
224 if(ReadSub_BE_2_1(0,pB,1)) // -> try different sub reading
225 { // --> success? mmm... let us check the data
226 DecodeSub_BE_2_1(pB+2352); // --> decode sub
227 if(*(pB+2352)==0x41) // --> check the first decoded byte
228 pReadFunc = ReadSCSI_BE_Sub_1; // ---> wow, byte is ok
229 }
230 }
231 free(pB);
232 }
233
234 /////////////////////////////////////////////////////////
235
236 int GetGenReadFunc(int iRM)
237 {
238 switch(iRM) // scsi read mode
239 {
240 // ------------------------------------------------ //
241 case MODE_BE_1:
242 case MODE_BE_2:
243 {
244 if(iUseSubReading==1)
245 GetBESubReadFunc();
246 else pReadFunc = ReadSCSI_BE;
247 } break;
248 // ------------------------------------------------ //
249 case MODE_28_1:
250 case MODE_28_2:
251 {
252 if(iUseSubReading==1)
253 pReadFunc = ReadSCSI_28_Sub;
254 else pReadFunc = ReadSCSI_28;
255 } break;
256 // ------------------------------------------------ //
257 case MODE_28_2048:
258 {
259 pReadFunc = ReadSCSI_28_2048;
260 } break;
261 // ------------------------------------------------ //
262 case MODE_28_2048_Ex:
263 {
264 pReadFunc = ReadSCSI_28_2048_Ex;
265 } break;
266 // ------------------------------------------------ //
267 default:
268 {
269 pReadFunc = ReadSCSI_Dummy;
270 } return -3;
271 // ------------------------------------------------ //
272 }
273 return 1;
274 }
275
276 /////////////////////////////////////////////////////////
277
278 int OpenGenCD(int iA,int iT,int iL)
279 {
280 pDeInitFunc = NULL; // init de-init func
281 pReadFunc = ReadSCSI_Dummy; // init (dummy) read func
282
283 if(iA==-1) return -1; // not configured properly
284
285 // -------------------------------------------------- //
286
287 if(iInterfaceMode>1) // ioctrl interfaces?
288 {
289 OpenIOCTLHandle(iA,iT,iL); // open w2k/xp ioctrl device
290 if(hIOCTL==NULL) return -2; // no cdrom available
291
292 if(iInterfaceMode==3) // special ioctl RAW mode?
293 { // -> get special reading funcs (non-scsi!)
294 if(iUseSubReading==1)
295 pReadFunc = ReadIOCTL_Raw_Sub;
296 else pReadFunc = ReadIOCTL_Raw;
297
298 WaitUntilDriveIsReady();
299 return 1;
300 }
301 }
302 else // aspi interface?
303 {
304 int iDevice=GetSCSIDevice(iA,iT,iL); // get device type
305 if(iDevice!=DTYPE_CDROM) return -2; // no cdrom? bye
306 }
307
308 if(CheckSCSIReadMode()==SS_COMP)
309 WaitUntilDriveIsReady();
310 else
311 {
312 CloseIOCTLHandle();
313 return -3;
314 }
315
316 return 1;
317 }
318
319 /////////////////////////////////////////////////////////
320
321 void CloseGenCD(void)
322 {
323 iCDROK=0; // no more cd available
324 if(pDeInitFunc) pDeInitFunc(); // deinit, if needed
325 pDeInitFunc = NULL;
326 pReadFunc = ReadSCSI_Dummy;
327 CloseIOCTLHandle(); // close ioctl drive file (if used)
328 }
329
330 /////////////////////////////////////////////////////////
331
332 void OpenGenInterface(void)
333 {
334 hASPI=NULL;
335
336 if(iInterfaceMode==0) return; // no interface? no fun
337 else
338 if(iInterfaceMode==1) // aspi
339 {
340 hASPI=LoadLibrary("WNASPI32.DLL");
341 if(hASPI)
342 {
343 pGetASPI32SupportInfo =
344 (DWORD(*)(void)) GetProcAddress(hASPI,"GetASPI32SupportInfo");
345 pSendASPI32Command =
346 (DWORD(*)(LPSRB))GetProcAddress(hASPI,"SendASPI32Command");
347
348 if(!pGetASPI32SupportInfo || !pSendASPI32Command)
349 {
350 iInterfaceMode=0;
351 return;
352 }
353 }
354 }
355 else // ioctl
356 {
357 if(iInterfaceMode<2 || iInterfaceMode>3) iInterfaceMode=2;
358 pGetASPI32SupportInfo = NULL;
359 pSendASPI32Command = IOCTLSendASPI32Command;
360 }
361 }
362
363 /////////////////////////////////////////////////////////
364
365 void CloseGenInterface(void)
366 {
367 pGetASPI32SupportInfo=NULL; // clear funcs
368 pSendASPI32Command=NULL;
369
370 if(hASPI) // free aspi
371 {
372 FreeLibrary(hASPI);
373 hASPI=NULL;
374 }
375 else CloseIOCTLHandle(); // or close ioctl file
376 }
377
378 /////////////////////////////////////////////////////////
379
380 int GetGenCDDrives(char * pDList)
381 {
382 if(hASPI) return GetSCSICDDrives(pDList); // aspi? use it
383 return GetIOCTLCDDrives(pDList); // or use ioctl
384 }
385
386 /////////////////////////////////////////////////////////

  ViewVC Help
Powered by ViewVC 1.1.22