src/cdrom/osf/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 11 Mar 2004 12:59:41 +0000
changeset 874 9719e7f51a3a
parent 873 89666943c598
child 1019 e3b3130f3af8
permissions -rw-r--r--
Date: Thu, 11 Mar 2004 18:45:17 +0900
From: Hayashi Naoyuki
Subject: Re: Tru64 cdrom and pthread option fix

I wrote
/* Some CD-ROM drives cannot play the first 150 frames. */
in src/cdrom/osf/SDL_syscdrom.c
and
Some CD-ROM drives(ex. TEAC CD-532E) cannot play first 150 frames(aka gap).

but this is not right.

It is written in MMC that LBA = 4500*M + 75*S + F - 150.
     1 /*
     2     Tru64 audio module for SDL (Simple DirectMedia Layer)
     3     Copyright (C) 2003
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19 
    20 */
    21 
    22 
    23 /* Functions for system-level CD-ROM audio control */
    24 
    25 //#define DEBUG_CDROM 1
    26 
    27 #include <sys/types.h>
    28 #include <sys/stat.h>
    29 #include <fcntl.h>
    30 #include <io/cam/cdrom.h>
    31 #include <io/cam/rzdisk.h>
    32 #include <io/common/devgetinfo.h>
    33 #include <alloca.h>
    34 #include <stdlib.h>
    35 #include <stdio.h>
    36 #include <string.h>
    37 #include <errno.h>
    38 
    39 #include "SDL_error.h"
    40 #include "SDL_cdrom.h"
    41 #include "SDL_syscdrom.h"
    42 
    43 /* The maximum number of CD-ROM drives we'll detect */
    44 #define MAX_DRIVES 16
    45 
    46 /* A list of available CD-ROM drives */
    47 static char *SDL_cdlist[MAX_DRIVES];
    48 static dev_t SDL_cdmode[MAX_DRIVES];
    49 
    50 /* The system-dependent CD control functions */
    51 static const char *SDL_SYS_CDName(int drive);
    52 static int         SDL_SYS_CDOpen(int drive);
    53 static int         SDL_SYS_CDGetTOC(SDL_CD *cdrom);
    54 static CDstatus    SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
    55 static int         SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
    56 static int         SDL_SYS_CDPause(SDL_CD *cdrom);
    57 static int         SDL_SYS_CDResume(SDL_CD *cdrom);
    58 static int         SDL_SYS_CDStop(SDL_CD *cdrom);
    59 static int         SDL_SYS_CDEject(SDL_CD *cdrom);
    60 static void        SDL_SYS_CDClose(SDL_CD *cdrom);
    61 
    62 /* Check a drive to see if it is a CD-ROM */
    63 /* Caution!! Not tested. */ 
    64 static int CheckDrive(char *drive, struct stat *stbuf)
    65 {
    66     int cdfd, is_cd = 0;
    67     struct mode_sel_sns_params msp;
    68     struct inquiry_info inq;
    69 
    70 #ifdef DEBUG_CDROM
    71     char *devtype[] = {"Disk", "Tape", "Printer", "Processor", "WORM",
    72 	"CD-ROM", "Scanner", "Optical", "Changer", "Comm", "Unknown"};
    73 #endif
    74 
    75     bzero(&msp, sizeof(msp));
    76     bzero(&inq, sizeof(inq));
    77 
    78     /* If it doesn't exist, return -1 */
    79     if ( stat(drive, stbuf) < 0 ) {
    80 	return(-1);
    81     }
    82 
    83     if ( (cdfd = open(drive, (O_RDWR|O_NDELAY), 0)) >= 0 ) {
    84 	msp.msp_addr   =   (caddr_t) &inq;
    85 	msp.msp_pgcode =                0;
    86 	msp.msp_pgctrl =                0;
    87 	msp.msp_length =      sizeof(inq);
    88 	msp.msp_setps  =                0;
    89 
    90 	if ( ioctl(cdfd, SCSI_GET_INQUIRY_DATA, &msp) )
    91 	    return (0);
    92 
    93 #ifdef DEBUG_CDROM
    94 	fprintf(stderr, "Device Type: %s\n", devtype[inq.perfdt]);
    95 	fprintf(stderr, "Vendor: %.8s\n", inq.vndrid);
    96 	fprintf(stderr, "Product: %.8s\n", inq.prodid);
    97 	fprintf(stderr, "Revision: %.8s\n", inq.revlvl);
    98 #endif
    99 	if ( inq.perfdt == DTYPE_RODIRECT )
   100 	    is_cd = 1;
   101     }
   102 
   103     return(is_cd);
   104 }
   105 
   106 /* Add a CD-ROM drive to our list of valid drives */
   107 static void AddDrive(char *drive, struct stat *stbuf)
   108 {
   109     int i;
   110 
   111     if ( SDL_numcds < MAX_DRIVES ) {
   112 	/* Check to make sure it's not already in our list.
   113 	 * This can happen when we see a drive via symbolic link.
   114 	 *
   115 	 */
   116 	for ( i=0; i<SDL_numcds; ++i ) {
   117 	    if ( stbuf->st_rdev == SDL_cdmode[i] ) {
   118 #ifdef DEBUG_CDROM
   119   fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
   120 #endif
   121 	    return;
   122 	    }
   123 	}
   124 
   125 	/* Add this drive to our list */
   126 	i = SDL_numcds;
   127 	SDL_cdlist[i] = (char *)malloc(strlen(drive)+1);
   128 	if ( SDL_cdlist[i] == NULL ) {
   129 	    SDL_OutOfMemory();
   130 	    return;
   131 	}
   132 
   133 	strcpy(SDL_cdlist[i], drive);
   134 	SDL_cdmode[i] = stbuf->st_rdev;
   135 	++SDL_numcds;
   136 #ifdef DEBUG_CDROM
   137   fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
   138 #endif
   139     }
   140 }
   141 
   142 int  SDL_SYS_CDInit(void)
   143 {
   144     /* checklist: /dev/rdisk/cdrom?c
   145      *
   146      */
   147     static char *checklist[] = {
   148 	"?0 rdisk/cdrom?",NULL};
   149     char drive[32];
   150     char *SDLcdrom;
   151     int i, j, exists;
   152     struct stat stbuf;
   153 
   154     /* Fill in our driver capabilities */
   155     SDL_CDcaps.Name   = SDL_SYS_CDName;
   156     SDL_CDcaps.Open   = SDL_SYS_CDOpen;
   157     SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
   158     SDL_CDcaps.Status = SDL_SYS_CDStatus;
   159     SDL_CDcaps.Play   = SDL_SYS_CDPlay;
   160     SDL_CDcaps.Pause  = SDL_SYS_CDPause;
   161     SDL_CDcaps.Resume = SDL_SYS_CDResume;
   162     SDL_CDcaps.Stop   = SDL_SYS_CDStop;
   163     SDL_CDcaps.Eject  = SDL_SYS_CDEject;
   164     SDL_CDcaps.Close  = SDL_SYS_CDClose;
   165 
   166 
   167     /* Look in the environment for our CD-ROM drive list */
   168     SDLcdrom = getenv("SDL_CDROM");	/* ':' separated list of devices */
   169     if ( SDLcdrom != NULL ) {
   170 	char *cdpath, *delim;
   171 	cdpath = malloc(strlen(SDLcdrom)+1);
   172 	if ( cdpath != NULL ) {
   173 	    strcpy(cdpath, SDLcdrom);
   174 	    SDLcdrom = cdpath;
   175 	    do {
   176 		delim = strchr(SDLcdrom, ':');
   177 		if ( delim ) {
   178 		    *delim++ = '\0';
   179 		}
   180 		if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
   181 		    AddDrive(SDLcdrom, &stbuf);
   182 		}
   183 		if ( delim ) {
   184 		    SDLcdrom = delim;
   185 		} else {
   186 		    SDLcdrom = NULL;
   187 		}
   188 	    } while ( SDLcdrom );
   189 	    free(cdpath);
   190 	}
   191 
   192 	/* If we found our drives, there's nothing left to do */
   193 	if ( SDL_numcds > 0 ) {
   194 	    return(0);
   195 	}
   196     }
   197     /* Scan the system for CD-ROM drives */
   198     for ( i=0; checklist[i]; ++i ) {
   199 	if ( checklist[i][0] == '?' ) {
   200 	    char *insert;
   201 	    exists = 1;
   202 	    for ( j=checklist[i][1]; exists; ++j ) {
   203 		sprintf(drive, "/dev/%sc", &checklist[i][3]);
   204 		insert = strchr(drive, '?');
   205 		if ( insert != NULL ) {
   206 		    *insert = j;
   207 		}
   208 		switch (CheckDrive(drive, &stbuf)) {
   209 		    /* Drive exists and is a CD-ROM */
   210 		    case 1:
   211 			AddDrive(drive, &stbuf);
   212 			break;
   213 			/* Drive exists, but isn't a CD-ROM */
   214 		    case 0:
   215 			break;
   216 			/* Drive doesn't exist */
   217 		    case -1:
   218 			exists = 0;
   219 			break;
   220 		}
   221 	    }
   222 	} else {
   223 	    sprintf(drive, "/dev/%s", checklist[i]);
   224 	    if ( CheckDrive(drive, &stbuf) > 0 ) {
   225 		AddDrive(drive, &stbuf);
   226 	    }
   227 	}
   228     }
   229 /*
   230     SDLcdrom=malloc(sizeof(char) * 32);
   231     strcpy(SDLcdrom,"/dev/rdisk/cdrom0c");
   232     SDL_cdlist[0] = SDLcdrom;
   233     stat(SDLcdrom, &stbuf);
   234     SDL_cdmode[0] = stbuf.st_rdev;
   235     SDL_numcds = 1;
   236  */
   237     return (0);
   238 }
   239 
   240 static const char *SDL_SYS_CDName(int drive)
   241 {
   242     return(SDL_cdlist[drive]);
   243 }
   244 
   245 static int SDL_SYS_CDOpen(int drive)
   246 {
   247     /* O_RDWR: To use ioctl(fd, SCSI_STOP_UNIT) */
   248     return(open(SDL_cdlist[drive], (O_RDWR|O_NDELAY), 0));
   249 }
   250 
   251 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
   252 {
   253     struct cd_toc                  toc;
   254     struct cd_toc_header           hdr;
   255     struct cd_toc_entry          *cdte;
   256     int i;
   257     int okay = 0;
   258     if ( ioctl(cdrom->id, CDROM_TOC_HEADER, &hdr) ) {
   259 	fprintf(stderr,"ioctl error CDROM_TOC_HEADER\n");
   260 	return -1;
   261     }
   262     cdrom->numtracks = hdr.th_ending_track - hdr.th_starting_track + 1;
   263     if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
   264 	cdrom->numtracks = SDL_MAX_TRACKS;
   265     }
   266 #ifdef DEBUG_CDROM
   267   fprintf(stderr,"hdr.th_data_len1 = %d\n", hdr.th_data_len1);
   268   fprintf(stderr,"hdr.th_data_len0 = %d\n", hdr.th_data_len0);
   269   fprintf(stderr,"hdr.th_starting_track = %d\n", hdr.th_starting_track);
   270   fprintf(stderr,"hdr.th_ending_track = %d\n", hdr.th_ending_track);
   271   fprintf(stderr,"cdrom->numtracks = %d\n", cdrom->numtracks);
   272 #endif
   273     toc.toc_address_format = CDROM_LBA_FORMAT;
   274     toc.toc_starting_track = 0;
   275     toc.toc_alloc_length = (hdr.th_data_len1 << 8) +
   276 			    hdr.th_data_len0 + sizeof(hdr);
   277     if ( (toc.toc_buffer = alloca(toc.toc_alloc_length)) == NULL) {
   278 	fprintf(stderr,"cannot allocate toc.toc_buffer\n");
   279 	return -1;
   280     }
   281 
   282     bzero (toc.toc_buffer, toc.toc_alloc_length);
   283     if (ioctl(cdrom->id, CDROM_TOC_ENTRYS, &toc)) {
   284 	fprintf(stderr,"ioctl error CDROM_TOC_ENTRYS\n");
   285 	return -1;
   286     }
   287 
   288     cdte =(struct cd_toc_entry *) ((char *) toc.toc_buffer + sizeof(hdr));
   289     for (i=0; i <= cdrom->numtracks; ++i) {
   290 	if (i == cdrom->numtracks ) {
   291 	    cdrom->track[i].id = 0xAA;;
   292 	} else {
   293 	    cdrom->track[i].id = hdr.th_starting_track + i;
   294 	}
   295 
   296 	cdrom->track[i].type =
   297 	    cdte[i].te_control & CDROM_DATA_TRACK;
   298 	cdrom->track[i].offset =
   299 	    cdte[i].te_absaddr.lba.addr3 << 24 |
   300 	    cdte[i].te_absaddr.lba.addr2 << 16 |
   301 	    cdte[i].te_absaddr.lba.addr1 << 8  |
   302 	    cdte[i].te_absaddr.lba.addr0;
   303 	cdrom->track[i].length = 0;
   304 	if ( i > 0 ) {
   305 	    cdrom->track[i - 1].length =
   306 		cdrom->track[i].offset -
   307 		cdrom->track[i - 1].offset;
   308 	}
   309     }
   310 #ifdef DEBUG_CDROM
   311   for (i = 0; i <= cdrom->numtracks; i++) {
   312     fprintf(stderr,"toc_entry[%d].te_track_number = %d\n",
   313 	    i,cdte[i].te_track_number);
   314     fprintf(stderr,"cdrom->track[%d].id = %d\n", i,cdrom->track[i].id);
   315     fprintf(stderr,"cdrom->track[%d].type = %x\n", i,cdrom->track[i].type);
   316     fprintf(stderr,"cdrom->track[%d].offset = %d\n", i,cdrom->track[i].offset);
   317     fprintf(stderr,"cdrom->track[%d].length = %d\n", i,cdrom->track[i].length);
   318   }
   319 #endif
   320     if ( i == (cdrom->numtracks+1) ) {
   321 	okay = 1;
   322     }
   323 
   324     return(okay ? 0 : -1);
   325 }
   326 
   327 /* Get CD-ROM status */
   328 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
   329 {
   330     CDstatus                     status;
   331     struct cd_sub_channel            sc;
   332     struct cd_subc_channel_data     scd;
   333 
   334     sc.sch_address_format = CDROM_LBA_FORMAT;
   335     sc.sch_data_format    = CDROM_CURRENT_POSITION;
   336     sc.sch_track_number   = 0;
   337     sc.sch_alloc_length   = sizeof(scd);
   338     sc.sch_buffer         = (caddr_t)&scd;
   339     if ( ioctl(cdrom->id, CDROM_READ_SUBCHANNEL, &sc) ) {
   340 	status = CD_ERROR;
   341 	fprintf(stderr,"ioctl error CDROM_READ_SUBCHANNEL \n");
   342     } else {
   343 	switch (scd.scd_header.sh_audio_status) {
   344 	    case AS_AUDIO_INVALID:
   345 		status = CD_STOPPED;
   346 		break;
   347 	    case AS_PLAY_IN_PROGRESS:
   348 		status = CD_PLAYING;
   349 		break;
   350 	    case AS_PLAY_PAUSED:
   351 		status = CD_PAUSED;
   352 		break;
   353 	    case AS_PLAY_COMPLETED:
   354 		status = CD_STOPPED;
   355 		break;
   356 	    case AS_PLAY_ERROR:
   357 		status = CD_ERROR;
   358 		break;
   359 	    case AS_NO_STATUS:
   360 		status = CD_STOPPED;
   361 		break;
   362 	    default:
   363 		status = CD_ERROR;
   364 		break;
   365 	}
   366 #ifdef DEBUG_CDROM
   367   fprintf(stderr,"scd.scd_header.sh_audio_status = %x\n",
   368 	scd.scd_header.sh_audio_status);
   369 #endif
   370     }
   371     if (position) {
   372 	if (status == CD_PLAYING || (status == CD_PAUSED) ) {
   373 	    *position =
   374 		scd.scd_position_data.scp_absaddr.lba.addr3 << 24 |
   375 		scd.scd_position_data.scp_absaddr.lba.addr2 << 16 |
   376 		scd.scd_position_data.scp_absaddr.lba.addr1 << 8  |
   377 		scd.scd_position_data.scp_absaddr.lba.addr0;
   378 	} else {
   379 	    *position = 0;
   380 	}
   381     }
   382 
   383     return status;
   384 }
   385 
   386 /* Start play */
   387 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
   388 {
   389 /*
   390  * Play MSF
   391  */
   392     struct cd_play_audio_msf msf;
   393     int end;
   394 
   395     bzero(&msf, sizeof(msf));
   396     end = start +length;
   397     FRAMES_TO_MSF(start + 150, /* LBA = 4500*M + 75*S + F - 150 */
   398 		  &msf.msf_starting_M_unit,
   399 		  &msf.msf_starting_S_unit,
   400 		  &msf.msf_starting_F_unit);
   401     FRAMES_TO_MSF(end + 150, /* LBA = 4500*M + 75*S + F - 150 */
   402 		  &msf.msf_ending_M_unit,
   403 		  &msf.msf_ending_S_unit,
   404 		  &msf.msf_ending_F_unit);
   405 
   406     return(ioctl(cdrom->id, CDROM_PLAY_AUDIO_MSF, &msf));
   407 }
   408 
   409 /* Pause play */
   410 static int SDL_SYS_CDPause(SDL_CD *cdrom)
   411 {
   412     return(ioctl(cdrom->id, CDROM_PAUSE_PLAY));
   413 }
   414 
   415 /* Resume play */
   416 static int SDL_SYS_CDResume(SDL_CD *cdrom)
   417 {
   418     return(ioctl(cdrom->id, CDROM_RESUME_PLAY));
   419 }
   420 
   421 /* Stop play */
   422 static int SDL_SYS_CDStop(SDL_CD *cdrom)
   423 {
   424     return(ioctl(cdrom->id, SCSI_STOP_UNIT));
   425 }
   426 
   427 /* Eject the CD-ROM */
   428 static int SDL_SYS_CDEject(SDL_CD *cdrom)
   429 {
   430     return(ioctl(cdrom->id, CDROM_EJECT_CADDY));
   431 }
   432 
   433 /* Close the CD-ROM handle */
   434 static void SDL_SYS_CDClose(SDL_CD *cdrom)
   435 {
   436     close(cdrom->id);
   437 }
   438 
   439 void SDL_SYS_CDQuit(void)
   440 {
   441     int i;
   442 
   443     if ( SDL_numcds > 0 ) {
   444 	for ( i=0; i<SDL_numcds; ++i ) {
   445 	    free(SDL_cdlist[i]);
   446 	}
   447 	SDL_numcds = 0;
   448     }
   449 }
   450 
   451