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