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