pcsxr-1.9.92
[pcsx_rearmed.git] / plugins / dfcdrom / cdr.h
CommitLineData
ef79bbde
P
1/*
2 * Copyright (c) 2010, Wei Mingzhi <whistler@openoffice.org>.
3 * All Rights Reserved.
4 *
5 * Based on: Cdrom for Psemu Pro like Emulators
6 * By: linuzappz <linuzappz@hotmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses>.
20 */
21
22#ifndef __CDR_H__
23#define __CDR_H__
24
25//#define DEBUG 1
26
27#include "config.h"
28
29#ifdef ENABLE_NLS
30#include <libintl.h>
31#include <locale.h>
32#define _(x) gettext(x)
33#define N_(x) (x)
34#else
35#define _(x) (x)
36#define N_(x) (x)
37#endif
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <stdint.h>
42#include <fcntl.h>
43#include <sys/ioctl.h>
44#include <sys/stat.h>
45#include <unistd.h>
46#include <pthread.h>
47#include <time.h>
48#include <string.h>
49
50#include "psemu_plugin_defs.h"
51
52#if defined (__linux__)
53
54#include <linux/cdrom.h>
55#include <scsi/scsi.h>
56#include <scsi/sg.h>
57
58#ifndef CDROMSETSPINDOWN
59#define CDROMSETSPINDOWN 0x531e
60#endif
61
62#define DEV_DEF "/dev/cdrom"
63
64#else
65
66struct cdrom_msf {
67 unsigned char cdmsf_min0; /* start minute */
68 unsigned char cdmsf_sec0; /* start second */
69 unsigned char cdmsf_frame0; /* start frame */
70 unsigned char cdmsf_min1; /* end minute */
71 unsigned char cdmsf_sec1; /* end second */
72 unsigned char cdmsf_frame1; /* end frame */
73};
74
75#define CD_SECS 60
76#define CD_FRAMES 75
77#define CD_MSF_OFFSET 150
78#define CD_FRAMESIZE_SUB 96
79
80#if defined (__FreeBSD__)
81#define DEV_DEF "/dev/cd0"
82#else
83#define DEV_DEF ""
84#endif
85
86#if !defined (USE_LIBCDIO) && !defined (_MACOSX)
87#define USE_NULL 1
88#endif
89
90#endif
91
92extern char CdromDev[256];
93extern long ReadMode;
94extern long UseSubQ;
95extern long CacheSize;
96extern long CdrSpeed;
97extern long SpinDown;
98
99#define NORMAL 0
100#define THREADED 1
101#define READ_MODES 2
102
103#ifndef CD_FRAMESIZE_RAW
104#define CD_FRAMESIZE_RAW 2352
105#endif
106
107#define DATA_SIZE (CD_FRAMESIZE_RAW - 12)
108
109// spindown codes
110#define SPINDOWN_VENDOR_SPECIFIC 0x00
111#define SPINDOWN_125MS 0x01
112#define SPINDOWN_250MS 0x02
113#define SPINDOWN_500MS 0x03
114#define SPINDOWN_1S 0x04
115#define SPINDOWN_2S 0x05
116#define SPINDOWN_4S 0x06
117#define SPINDOWN_8S 0x07
118#define SPINDOWN_16S 0x08
119#define SPINDOWN_32S 0x09
120#define SPINDOWN_1MIN 0x0A
121#define SPINDOWN_2MIN 0x0B
122#define SPINDOWN_4MIN 0x0C
123#define SPINDOWN_8MIN 0x0D
124#define SPINDOWN_16MIN 0x0E
125#define SPINDOWN_32MIN 0x0F
126
127typedef struct _MMC_READ_CD {
128 unsigned char Code; // 0xBE
129
130 unsigned char RelativeAddress : 1;
131 unsigned char : 1;
132 unsigned char ExpectedSectorType : 3;
133 unsigned char Lun : 3;
134
135 unsigned char StartingLBA[4];
136 unsigned char TransferBlocks[3];
137
138 unsigned char : 1;
139 unsigned char ErrorFlags : 2;
140 unsigned char IncludeEDC : 1;
141 unsigned char IncludeUserData : 1;
142 unsigned char HeaderCode : 2;
143 unsigned char IncludeSyncData : 1;
144
145 unsigned char SubChannelSelection : 3;
146 unsigned char : 5;
147
148 unsigned char Ctrl;
149} MMC_READ_CD;
150
151#define itob(i) ((i)/10*16 + (i)%10) /* u_char to BCD */
152#define btoi(b) ((b)/16*10 + (b)%16) /* BCD to u_char */
153
154struct CdrStat {
155 unsigned long Type;
156 unsigned long Status;
157 unsigned char Time[3]; // current playing time
158};
159
160struct SubQ {
161 char res0[12];
162 unsigned char ControlAndADR;
163 unsigned char TrackNumber;
164 unsigned char IndexNumber;
165 unsigned char TrackRelativeAddress[3];
166 unsigned char Filler;
167 unsigned char AbsoluteAddress[3];
168 unsigned char CRC[2];
169 char res1[72];
170};
171
172typedef union {
173 struct cdrom_msf msf;
174 unsigned char buf[CD_FRAMESIZE_RAW];
175} crdata;
176
177typedef struct {
178 crdata cr;
179 int ret;
180} CacheData;
181
182long ReadNormal();
183long ReadThreaded();
184unsigned char* GetBNormal();
185unsigned char* GetBThreaded();
186
187long CDRstop(void);
188
189void LoadConf();
190void SaveConf();
191
192#ifdef DEBUG
193#define PRINTF printf
194#else
195#define PRINTF(...) /* */
196#endif
197
198unsigned int msf_to_lba(char m, char s, char f);
199void lba_to_msf(unsigned int s, unsigned char *msf);
200void DecodeRawSubData(unsigned char *subbuffer);
201unsigned short calcCrc(unsigned char *d, int len);
202
203int OpenCdHandle();
204void CloseCdHandle();
205int IsCdHandleOpen();
206long GetTN(unsigned char *buffer);
207long GetTD(unsigned char track, unsigned char *buffer);
208long GetTE(unsigned char track, unsigned char *m, unsigned char *s, unsigned char *f);
209long ReadSector(crdata *cr);
210long PlayCDDA(unsigned char *sector);
211long StopCDDA();
212long GetStatus(int playing, struct CdrStat *stat);
213unsigned char *ReadSub(const unsigned char *time);
214
215#endif