1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
/******************************************************************
* CopyPolicy: GNU Lesser General Public License 2.1 applies
* Copyright (C) Monty xiphmont@mit.edu
* FreeBSD porting (c) 2003
* Simon 'corecode' Schubert <corecode@corecode.ath.cx>
*
* CDROM code specific to the cooked ioctl interface
*
******************************************************************/
#include "low_interface.h"
#include "common_interface.h"
#include "utils.h"
#include <time.h>
static int timed_ioctl(cdrom_drive *d, int fd, int command, void *arg){
struct timespec tv1;
struct timespec tv2;
int ret1=clock_gettime(d->private_data->clock,&tv1);
int ret2=ioctl(fd, command,arg);
int ret3=clock_gettime(d->private_data->clock,&tv2);
if(ret1<0 || ret3<0){
d->private_data->last_milliseconds=-1;
}else{
d->private_data->last_milliseconds = (tv2.tv_sec-tv1.tv_sec)*1000. + (tv2.tv_nsec-tv1.tv_nsec)/1000000.;
}
return ret2;
}
#if defined(__linux__)
static int cooked_readtoc (cdrom_drive *d){
int i;
int tracks;
struct cdrom_tochdr hdr;
struct cdrom_tocentry entry;
/* get TocHeader to find out how many entries there are */
if(ioctl(d->ioctl_fd, CDROMREADTOCHDR, &hdr ))
switch(errno){
case EPERM:
cderror(d,"102: Permision denied on cdrom (ioctl) device\n");
return(-102);
default:
cderror(d,"004: Unable to read table of contents header\n");
return(-4);
}
/* get all TocEntries */
for(i=0;i<hdr.cdth_trk1;i++){
entry.cdte_track= i+1;
entry.cdte_format = CDROM_LBA;
if(ioctl(d->ioctl_fd,CDROMREADTOCENTRY,&entry)){
cderror(d,"005: Unable to read table of contents entry\n");
return(-5);
}
d->disc_toc[i].bFlags = (entry.cdte_adr << 4) | (entry.cdte_ctrl & 0x0f);
d->disc_toc[i].bTrack = i+1;
d->disc_toc[i].dwStartSector = entry.cdte_addr.lba;
}
entry.cdte_track = CDROM_LEADOUT;
entry.cdte_format = CDROM_LBA;
if(ioctl(d->ioctl_fd, CDROMREADTOCENTRY, &entry)){
cderror(d,"005: Unable to read table of contents entry\n");
return(-5);
}
d->disc_toc[i].bFlags = (entry.cdte_adr << 4) | (entry.cdte_ctrl & 0x0f);
d->disc_toc[i].bTrack = entry.cdte_track;
d->disc_toc[i].dwStartSector = entry.cdte_addr.lba;
tracks=hdr.cdth_trk1+1;
d->cd_extra=FixupTOC(d,tracks);
return(--tracks); /* without lead-out */
}
/* Set operating speed */
static int cooked_setspeed(cdrom_drive *d, int speed)
{
if(d->ioctl_fd!=-1)
return ioctl(d->ioctl_fd, CDROM_SELECT_SPEED, speed);
else
return 0;
}
/* read 'SectorBurst' adjacent sectors of audio sectors
* to Buffer '*p' beginning at sector 'lSector'
*/
static long cooked_read (cdrom_drive *d, void *p, long begin, long sectors){
int retry_count,err,ret=0;
struct cdrom_read_audio arg;
char *buffer=(char *)p;
/* read d->nsectors at a time, max. */
sectors=(sectors>d->nsectors?d->nsectors:sectors);
if(p==NULL)buffer = malloc(sectors*CD_FRAMESIZE_RAW);
arg.addr.lba = begin;
arg.addr_format = CDROM_LBA;
arg.nframes = sectors;
arg.buf=buffer;
retry_count=0;
do {
if((err=timed_ioctl(d,d->ioctl_fd, CDROMREADAUDIO, &arg))){
if(!d->error_retry){
ret=-7;
goto done;
}
switch(errno){
case ENOMEM:
/* D'oh. Possible kernel error. Keep limping */
if(sectors==1){
/* Nope, can't continue */
cderror(d,"300: Kernel memory error\n");
ret=-300;
goto done;
}
case ENXIO:
case EBADF:
case ENOMEDIUM:
errno=ENOMEDIUM;
ret=0;
goto done;
default:
if(sectors==1){
/* *Could* be I/O or media error. I think. If we're at
30 retries, we better skip this unhappy little
sector. */
if(retry_count>MAX_RETRIES-1){
char b[256];
sprintf(b,"010: Unable to access sector %ld: skipping...\n",
begin);
cderror(d,b);
ret=-10;
goto done;
}
break;
}
}
if(retry_count>4)
if(sectors>1)
sectors=sectors*3/4;
retry_count++;
if(retry_count>MAX_RETRIES){
cderror(d,"007: Unknown, unrecoverable error reading data\n");
ret=-7;
goto done;
}
}else
break;
} while (err);
ret=sectors;
done:
if(p==NULL && buffer)free(buffer);
return ret;
}
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
static int
cooked_readtoc(cdrom_drive *d)
{
int i;
struct ioc_toc_header hdr;
struct ioc_read_toc_single_entry entry;
if (ioctl(d->ioctl_fd, CDIOREADTOCHEADER, &hdr) == -1) {
int ret;
if (errno == EPERM) {
ret = -102;
cderror(d, "102: ");
} else {
ret = -4;
cderror(d, "004: Unable to read table of contents header: ");
}
cderror(d, strerror(errno));
cderror(d, "\n");
return ret;
}
entry.address_format = CD_LBA_FORMAT;
for (i = hdr.starting_track; i <= hdr.ending_track; ++i) {
entry.track = i;
if (ioctl(d->ioctl_fd, CDIOREADTOCENTRY, &entry) == -1) {
cderror(d, "005: Unable to read table of contents entry\n");
return -5;
}
d->disc_toc[i - hdr.starting_track].bFlags = entry.entry.control;
d->disc_toc[i - hdr.starting_track].bTrack = entry.entry.track;
d->disc_toc[i - hdr.starting_track].dwStartSector = be32_to_cpu(entry.entry.addr.lba);
}
entry.track = 0xaa; /* leadout */
if (ioctl(d->ioctl_fd, CDIOREADTOCENTRY, &entry) == -1) {
cderror(d, "005: Unable to read table of contents entry\n");
return -5;
}
d->disc_toc[i - hdr.starting_track].bFlags = entry.entry.control;
d->disc_toc[i - hdr.starting_track].bTrack = entry.entry.track;
d->disc_toc[i - hdr.starting_track].dwStartSector = be32_to_cpu(entry.entry.addr.lba);
d->cd_extra = FixupTOC(d, hdr.ending_track - hdr.starting_track + 2); /* with TOC */
return hdr.ending_track - hdr.starting_track + 1;
}
static int
cooked_setspeed(cdrom_drive *d, int speed)
{
#ifdef CDRIOCREADSPEED
speed *= 177;
return ioctl(d->ioctl_fd, CDRIOCREADSPEED, &speed);
#else
return -1;
#endif
}
static long
cooked_read(cdrom_drive *d, void *p, long begin, long sectors)
{
int retry_count = 0;
#ifndef CDIOCREADAUDIO
int bsize = CD_FRAMESIZE_RAW;
#else
struct ioc_read_audio arg;
if (sectors > d->nsectors)
sectors = d->nsectors;
arg.address_format = CD_LBA_FORMAT;
arg.address.lba = begin;
arg.buffer = p;
#endif
#ifndef CDIOCREADAUDIO
if (ioctl(d->ioctl_fd, CDRIOCSETBLOCKSIZE, &bsize) == -1)
return -7;
#endif
for (;;) {
#ifndef CDIOCREADAUDIO
if (pread(d->ioctl_fd, p, sectors*bsize, begin*bsize) != sectors*bsize) {
#else
arg.nframes = sectors;
if (ioctl(d->ioctl_fd, CDIOCREADAUDIO, &arg) == -1) {
#endif
if (!d->error_retry)
return -7;
switch (errno) {
case ENOMEM:
if (sectors == 1) {
cderror(d, "300: Kernel memory error\n");
return -300;
}
/* FALLTHROUGH */
default:
if (sectors == 1) {
if (retry_count > MAX_RETRIES - 1) {
char b[256];
snprintf(b, sizeof(b),
"010: Unable to access sector %ld; "
"skipping...\n", begin);
cderror(d, b);
return -10;
}
break;
}
}
if (retry_count > 4 && sectors > 1)
sectors = sectors * 3 / 4;
++retry_count;
if (retry_count > MAX_RETRIES) {
cderror(d, "007: Unknown, unrecoverable error reading data\n");
return -7;
}
} else
break;
}
return sectors;
}
#endif
/* hook */
static int Dummy (cdrom_drive *d,int Switch){
return(0);
}
static int verify_read_command(cdrom_drive *d){
int i;
int16_t *buff=malloc(CD_FRAMESIZE_RAW);
int audioflag=0;
cdmessage(d,"Verifying drive can read CDDA...\n");
d->enable_cdda(d,1);
for(i=1;i<=d->tracks;i++){
if(cdda_track_audiop(d,i)==1){
long firstsector=cdda_track_firstsector(d,i);
long lastsector=cdda_track_lastsector(d,i);
long sector=(firstsector+lastsector)>>1;
audioflag=1;
if(d->read_audio(d,buff,sector,1)>0){
cdmessage(d,"\tExpected command set reads OK.\n");
d->enable_cdda(d,0);
free(buff);
return(0);
}
}
}
d->enable_cdda(d,0);
if(!audioflag){
cdmessage(d,"\tCould not find any audio tracks on this disk.\n");
return(-403);
}
cdmessage(d,"\n\tUnable to read any data; "
"drive probably not CDDA capable.\n");
cderror(d,"006: Could not read any data from drive\n");
free(buff);
return(-6);
}
#include "drive_exceptions.h"
static void check_exceptions(cdrom_drive *d,exception *list){
int i=0;
while(list[i].model){
if(!strncmp(list[i].model,d->drive_model,strlen(list[i].model))){
if(list[i].bigendianp!=-1)d->bigendianp=list[i].bigendianp;
return;
}
i++;
}
}
/* set function pointers to use the ioctl routines */
int cooked_init_drive (cdrom_drive *d){
int ret;
#if defined(__linux__)
switch(d->drive_type){
case MATSUSHITA_CDROM_MAJOR: /* sbpcd 1 */
case MATSUSHITA_CDROM2_MAJOR: /* sbpcd 2 */
case MATSUSHITA_CDROM3_MAJOR: /* sbpcd 3 */
case MATSUSHITA_CDROM4_MAJOR: /* sbpcd 4 */
/* don't make the buffer too big; this sucker don't preempt */
cdmessage(d,"Attempting to set sbpcd buffer size...\n");
d->nsectors=8;
while(1){
/* this ioctl returns zero on error; exactly wrong, but that's
what it does. */
if(ioctl(d->ioctl_fd, CDROMAUDIOBUFSIZ, d->nsectors)==0){
d->nsectors>>=1;
if(d->nsectors==0){
char buffer[256];
d->nsectors=8;
sprintf(buffer,"\tTrouble setting buffer size. Defaulting to %d sectors.\n",
d->nsectors);
cdmessage(d,buffer);
break; /* Oh, well. Try to read anyway.*/
}
}else{
char buffer[256];
sprintf(buffer,"\tSetting read block size at %d sectors (%ld bytes).\n",
d->nsectors,(long)d->nsectors*CD_FRAMESIZE_RAW);
cdmessage(d,buffer);
break;
}
}
break;
case IDE0_MAJOR:
case IDE1_MAJOR:
case IDE2_MAJOR:
case IDE3_MAJOR:
d->nsectors=8; /* it's a define in the linux kernel; we have no
way of determining other than this guess tho */
d->bigendianp=0;
d->is_atapi=1;
check_exceptions(d,atapi_list);
break;
default:
d->nsectors=40;
}
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
d->nsectors = 26; /* FreeBSD only support 64K I/O transfer size */
#endif
d->enable_cdda = Dummy;
d->read_audio = cooked_read;
d->read_toc = cooked_readtoc;
d->set_speed = cooked_setspeed;
ret=d->tracks=d->read_toc(d);
if(d->tracks<1)
return(ret);
d->opened=1;
if((ret=verify_read_command(d)))return(ret);
d->error_retry=1;
return(0);
}
|