| 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 | |
| 66 | struct 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 | |
| 92 | extern char CdromDev[256]; |
| 93 | extern long ReadMode; |
| 94 | extern long UseSubQ; |
| 95 | extern long CacheSize; |
| 96 | extern long CdrSpeed; |
| 97 | extern 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 | |
| 127 | typedef 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 | |
| 154 | struct CdrStat { |
| 155 | unsigned long Type; |
| 156 | unsigned long Status; |
| 157 | unsigned char Time[3]; // current playing time |
| 158 | }; |
| 159 | |
| 160 | struct 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 | |
| 172 | typedef union { |
| 173 | struct cdrom_msf msf; |
| 174 | unsigned char buf[CD_FRAMESIZE_RAW]; |
| 175 | } crdata; |
| 176 | |
| 177 | typedef struct { |
| 178 | crdata cr; |
| 179 | int ret; |
| 180 | } CacheData; |
| 181 | |
| 182 | long ReadNormal(); |
| 183 | long ReadThreaded(); |
| 184 | unsigned char* GetBNormal(); |
| 185 | unsigned char* GetBThreaded(); |
| 186 | |
| 187 | long CDRstop(void); |
| 188 | |
| 189 | void LoadConf(); |
| 190 | void SaveConf(); |
| 191 | |
| 192 | #ifdef DEBUG |
| 193 | #define PRINTF printf |
| 194 | #else |
| 195 | #define PRINTF(...) /* */ |
| 196 | #endif |
| 197 | |
| 198 | unsigned int msf_to_lba(char m, char s, char f); |
| 199 | void lba_to_msf(unsigned int s, unsigned char *msf); |
| 200 | void DecodeRawSubData(unsigned char *subbuffer); |
| 201 | unsigned short calcCrc(unsigned char *d, int len); |
| 202 | |
| 203 | int OpenCdHandle(); |
| 204 | void CloseCdHandle(); |
| 205 | int IsCdHandleOpen(); |
| 206 | long GetTN(unsigned char *buffer); |
| 207 | long GetTD(unsigned char track, unsigned char *buffer); |
| 208 | long GetTE(unsigned char track, unsigned char *m, unsigned char *s, unsigned char *f); |
| 209 | long ReadSector(crdata *cr); |
| 210 | long PlayCDDA(unsigned char *sector); |
| 211 | long StopCDDA(); |
| 212 | long GetStatus(int playing, struct CdrStat *stat); |
| 213 | unsigned char *ReadSub(const unsigned char *time); |
| 214 | |
| 215 | #endif |