src/cdrom/osf/SDL_syscdrom.c
changeset 654 e92bcf2573cb
child 873 89666943c598
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/cdrom/osf/SDL_syscdrom.c	Tue Jul 22 14:01:21 2003 +0000
     1.3 @@ -0,0 +1,453 @@
     1.4 +/*
     1.5 +    Tru64 audio module for SDL (Simple DirectMedia Layer)
     1.6 +    Copyright (C) 2003
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +
    1.23 +*/
    1.24 +
    1.25 +
    1.26 +/* Functions for system-level CD-ROM audio control */
    1.27 +
    1.28 +//#define DEBUG_CDROM 1
    1.29 +
    1.30 +#include <sys/types.h>
    1.31 +#include <sys/stat.h>
    1.32 +#include <fcntl.h>
    1.33 +#include <io/cam/cdrom.h>
    1.34 +#include <io/cam/rzdisk.h>
    1.35 +#include <io/common/devgetinfo.h>
    1.36 +#include <alloca.h>
    1.37 +#include <stdlib.h>
    1.38 +#include <stdio.h>
    1.39 +#include <string.h>
    1.40 +#include <errno.h>
    1.41 +
    1.42 +#include "SDL_error.h"
    1.43 +#include "SDL_cdrom.h"
    1.44 +#include "SDL_syscdrom.h"
    1.45 +
    1.46 +/* The maximum number of CD-ROM drives we'll detect */
    1.47 +#define MAX_DRIVES 16
    1.48 +
    1.49 +/* A list of available CD-ROM drives */
    1.50 +static char *SDL_cdlist[MAX_DRIVES];
    1.51 +static dev_t SDL_cdmode[MAX_DRIVES];
    1.52 +
    1.53 +/* The system-dependent CD control functions */
    1.54 +static const char *SDL_SYS_CDName(int drive);
    1.55 +static int         SDL_SYS_CDOpen(int drive);
    1.56 +static int         SDL_SYS_CDGetTOC(SDL_CD *cdrom);
    1.57 +static CDstatus    SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
    1.58 +static int         SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
    1.59 +static int         SDL_SYS_CDPause(SDL_CD *cdrom);
    1.60 +static int         SDL_SYS_CDResume(SDL_CD *cdrom);
    1.61 +static int         SDL_SYS_CDStop(SDL_CD *cdrom);
    1.62 +static int         SDL_SYS_CDEject(SDL_CD *cdrom);
    1.63 +static void        SDL_SYS_CDClose(SDL_CD *cdrom);
    1.64 +
    1.65 +/* Some ioctl() errno values which occur when the tray is empty */
    1.66 +#define ERRNO_TRAYEMPTY(errno)	\
    1.67 +    ((errno == EIO) || (errno == ENOENT) || (errno == EINVAL))
    1.68 +
    1.69 +/* Check a drive to see if it is a CD-ROM */
    1.70 +/* Caution!! Not tested. */ 
    1.71 +static int CheckDrive(char *drive, struct stat *stbuf)
    1.72 +{
    1.73 +    int is_cd, cdfd;
    1.74 +    struct cd_sub_channel info;
    1.75 +
    1.76 +    /* If it doesn't exist, return -1 */
    1.77 +    if ( stat(drive, stbuf) < 0 ) {
    1.78 +	return(-1);
    1.79 +    }
    1.80 +
    1.81 +    /* If it does exist, verify that it's an available CD-ROM */
    1.82 +    is_cd = 0;
    1.83 +    if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) {
    1.84 +	cdfd = open(drive, (O_RDWR|O_NDELAY), 0);
    1.85 +	if ( cdfd >= 0 ) {
    1.86 +	    info.sch_address_format = CDROM_MSF_FORMAT;
    1.87 +	    info.sch_data_format = CDROM_CURRENT_POSITION;
    1.88 +	    info.sch_alloc_length = 0;
    1.89 +	    info.sch_track_number = 0;
    1.90 +	    info.sch_buffer = NULL;
    1.91 +	    /*
    1.92 +	     *
    1.93 +	     * Under Linux, EIO occurs when a disk is not present.
    1.94 +	     * This isn't 100% reliable, so we use the USE_MNTENT
    1.95 +	     * code above instead.
    1.96 +	     *
    1.97 +	     */
    1.98 +	    if ( (ioctl(cdfd, CDROM_READ_SUBCHANNEL, &info) == 0) ||
    1.99 +		    ERRNO_TRAYEMPTY(errno) ) {
   1.100 +		is_cd = 1;
   1.101 +	    }
   1.102 +
   1.103 +	    close(cdfd);
   1.104 +	}
   1.105 +    }
   1.106 +
   1.107 +    return(is_cd);
   1.108 +}
   1.109 +
   1.110 +/* Add a CD-ROM drive to our list of valid drives */
   1.111 +static void AddDrive(char *drive, struct stat *stbuf)
   1.112 +{
   1.113 +    int i;
   1.114 +
   1.115 +    if ( SDL_numcds < MAX_DRIVES ) {
   1.116 +	/* Check to make sure it's not already in our list.
   1.117 +	 * This can happen when we see a drive via symbolic link.
   1.118 +	 *
   1.119 +	 * /
   1.120 +	for ( i=0; i<SDL_numcds; ++i ) {
   1.121 +	    if ( stbuf->st_rdev == SDL_cdmode[i] ) {
   1.122 +#ifdef DEBUG_CDROM
   1.123 +  fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
   1.124 +#endif
   1.125 +	    return;
   1.126 +	    }
   1.127 +	}
   1.128 +
   1.129 +	/* Add this drive to our list */
   1.130 +	i = SDL_numcds;
   1.131 +	SDL_cdlist[i] = (char *)malloc(strlen(drive)+1);
   1.132 +	if ( SDL_cdlist[i] == NULL ) {
   1.133 +	    SDL_OutOfMemory();
   1.134 +	    return;
   1.135 +	}
   1.136 +
   1.137 +	strcpy(SDL_cdlist[i], drive);
   1.138 +	SDL_cdmode[i] = stbuf->st_rdev;
   1.139 +	++SDL_numcds;
   1.140 +#ifdef DEBUG_CDROM
   1.141 +  fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
   1.142 +#endif
   1.143 +    }
   1.144 +}
   1.145 +
   1.146 +int  SDL_SYS_CDInit(void)
   1.147 +{
   1.148 +    /* checklist: /dev/cdrom,/dev/cd?c /dev/acd?c
   1.149 +     * /dev/matcd?c /dev/mcd?c /dev/scd?c
   1.150 +     *
   1.151 +     */
   1.152 +    static char *checklist[] = {
   1.153 +	"?0 rdisk/cdrom?",NULL};
   1.154 +    char drive[32];
   1.155 +    char *SDLcdrom;
   1.156 +    int i, j, exists;
   1.157 +    struct stat stbuf;
   1.158 +
   1.159 +    /* Fill in our driver capabilities */
   1.160 +    SDL_CDcaps.Name   = SDL_SYS_CDName;
   1.161 +    SDL_CDcaps.Open   = SDL_SYS_CDOpen;
   1.162 +    SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
   1.163 +    SDL_CDcaps.Status = SDL_SYS_CDStatus;
   1.164 +    SDL_CDcaps.Play   = SDL_SYS_CDPlay;
   1.165 +    SDL_CDcaps.Pause  = SDL_SYS_CDPause;
   1.166 +    SDL_CDcaps.Resume = SDL_SYS_CDResume;
   1.167 +    SDL_CDcaps.Stop   = SDL_SYS_CDStop;
   1.168 +    SDL_CDcaps.Eject  = SDL_SYS_CDEject;
   1.169 +    SDL_CDcaps.Close  = SDL_SYS_CDClose;
   1.170 +
   1.171 +
   1.172 +    /* Look in the environment for our CD-ROM drive list */
   1.173 +    SDLcdrom = getenv("SDL_CDROM");	/* ':' separated list of devices */
   1.174 +    if ( SDLcdrom != NULL ) {
   1.175 +	char *cdpath, *delim;
   1.176 +	cdpath = malloc(strlen(SDLcdrom)+1);
   1.177 +	if ( cdpath != NULL ) {
   1.178 +	    strcpy(cdpath, SDLcdrom);
   1.179 +	    SDLcdrom = cdpath;
   1.180 +	    do {
   1.181 +		delim = strchr(SDLcdrom, ':');
   1.182 +		if ( delim ) {
   1.183 +		    *delim++ = '\0';
   1.184 +		}
   1.185 +		if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
   1.186 +		    AddDrive(SDLcdrom, &stbuf);
   1.187 +		}
   1.188 +		if ( delim ) {
   1.189 +		    SDLcdrom = delim;
   1.190 +		} else {
   1.191 +		    SDLcdrom = NULL;
   1.192 +		}
   1.193 +	    } while ( SDLcdrom );
   1.194 +	    free(cdpath);
   1.195 +	}
   1.196 +
   1.197 +	/* If we found our drives, there's nothing left to do */
   1.198 +	if ( SDL_numcds > 0 ) {
   1.199 +	    return(0);
   1.200 +	}
   1.201 +    }
   1.202 +    /* Scan the system for CD-ROM drives */
   1.203 +    for ( i=0; checklist[i]; ++i ) {
   1.204 +	if ( checklist[i][0] == '?' ) {
   1.205 +	    char *insert;
   1.206 +	    exists = 1;
   1.207 +	    for ( j=checklist[i][1]; exists; ++j ) {
   1.208 +		sprintf(drive, "/dev/%sc", &checklist[i][3]);
   1.209 +		insert = strchr(drive, '?');
   1.210 +		if ( insert != NULL ) {
   1.211 +		    *insert = j;
   1.212 +		}
   1.213 +		switch (CheckDrive(drive, &stbuf)) {
   1.214 +		    /* Drive exists and is a CD-ROM */
   1.215 +		    case 1:
   1.216 +			AddDrive(drive, &stbuf);
   1.217 +			break;
   1.218 +			/* Drive exists, but isn't a CD-ROM */
   1.219 +		    case 0:
   1.220 +			break;
   1.221 +			/* Drive doesn't exist */
   1.222 +		    case -1:
   1.223 +			exists = 0;
   1.224 +			break;
   1.225 +		}
   1.226 +	    }
   1.227 +	} else {
   1.228 +	    sprintf(drive, "/dev/%s", checklist[i]);
   1.229 +	    if ( CheckDrive(drive, &stbuf) > 0 ) {
   1.230 +		AddDrive(drive, &stbuf);
   1.231 +	    }
   1.232 +	}
   1.233 +    }
   1.234 +/*
   1.235 +    SDLcdrom=malloc(sizeof(char) * 32);
   1.236 +    strcpy(SDLcdrom,"/dev/rdisk/cdrom0c");
   1.237 +    SDL_cdlist[0] = SDLcdrom;
   1.238 +    stat(SDLcdrom, &stbuf);
   1.239 +    SDL_cdmode[0] = stbuf.st_rdev;
   1.240 +    SDL_numcds = 1;
   1.241 + */
   1.242 +    return (0);
   1.243 +}
   1.244 +
   1.245 +static const char *SDL_SYS_CDName(int drive)
   1.246 +{
   1.247 +    return(SDL_cdlist[drive]);
   1.248 +}
   1.249 +
   1.250 +static int SDL_SYS_CDOpen(int drive)
   1.251 +{
   1.252 +    /* O_RDWR: To use ioctl(fd, SCSI_STOP_UNIT) */
   1.253 +    return(open(SDL_cdlist[drive], (O_RDWR|O_NDELAY), 0));
   1.254 +}
   1.255 +
   1.256 +static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
   1.257 +{
   1.258 +    struct cd_toc                  toc;
   1.259 +    struct cd_toc_header           hdr;
   1.260 +    struct cd_toc_entry          *cdte;
   1.261 +    int i;
   1.262 +    int okay = 0;
   1.263 +    if ( ioctl(cdrom->id, CDROM_TOC_HEADER, &hdr) ) {
   1.264 +	fprintf(stderr,"ioctl error CDROM_TOC_HEADER\n");
   1.265 +	return -1;
   1.266 +    }
   1.267 +    cdrom->numtracks = hdr.th_ending_track - hdr.th_starting_track + 1;
   1.268 +    if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
   1.269 +	cdrom->numtracks = SDL_MAX_TRACKS;
   1.270 +    }
   1.271 +#ifdef DEBUG_CDROM
   1.272 +  fprintf(stderr,"hdr.th_data_len1 = %d\n", hdr.th_data_len1);
   1.273 +  fprintf(stderr,"hdr.th_data_len0 = %d\n", hdr.th_data_len0);
   1.274 +  fprintf(stderr,"hdr.th_starting_track = %d\n", hdr.th_starting_track);
   1.275 +  fprintf(stderr,"hdr.th_ending_track = %d\n", hdr.th_ending_track);
   1.276 +  fprintf(stderr,"cdrom->numtracks = %d\n", cdrom->numtracks);
   1.277 +#endif
   1.278 +    toc.toc_address_format = CDROM_LBA_FORMAT;
   1.279 +    toc.toc_starting_track = 0;
   1.280 +    toc.toc_alloc_length = (hdr.th_data_len1 << 8) +
   1.281 +			    hdr.th_data_len0 + sizeof(hdr);
   1.282 +    if ( (toc.toc_buffer = alloca(toc.toc_alloc_length)) == NULL) {
   1.283 +	fprintf(stderr,"cannot allocate toc.toc_buffer\n");
   1.284 +	return -1;
   1.285 +    }
   1.286 +
   1.287 +    bzero (toc.toc_buffer, toc.toc_alloc_length);
   1.288 +    if (ioctl(cdrom->id, CDROM_TOC_ENTRYS, &toc)) {
   1.289 +	fprintf(stderr,"ioctl error CDROM_TOC_ENTRYS\n");
   1.290 +	return -1;
   1.291 +    }
   1.292 +
   1.293 +    (char *)cdte = toc.toc_buffer + sizeof(hdr);
   1.294 +	for (i=0; i <= cdrom->numtracks; ++i) {
   1.295 +	    if (i == cdrom->numtracks ) {
   1.296 +		cdrom->track[i].id = 0xAA;;
   1.297 +	    } else {
   1.298 +		cdrom->track[i].id = hdr.th_starting_track + i;
   1.299 +	    }
   1.300 +
   1.301 +	    cdrom->track[i].type =
   1.302 +		cdte[i].te_control & CDROM_DATA_TRACK;
   1.303 +	    cdrom->track[i].offset =
   1.304 +		cdte[i].te_absaddr.lba.addr3 << 24 |
   1.305 +		cdte[i].te_absaddr.lba.addr2 << 16 |
   1.306 +		cdte[i].te_absaddr.lba.addr1 << 8  |
   1.307 +		cdte[i].te_absaddr.lba.addr0;
   1.308 +	    cdrom->track[i].length = 0;
   1.309 +	    if ( i > 0 ) {
   1.310 +		cdrom->track[i - 1].length =
   1.311 +		    cdrom->track[i].offset -
   1.312 +		    cdrom->track[i - 1].offset;
   1.313 +	    }
   1.314 +	}
   1.315 +#ifdef DEBUG_CDROM
   1.316 +  for (i = 0; i <= cdrom->numtracks; i++) {
   1.317 +    fprintf(stderr,"toc_entry[%d].te_track_number = %d\n",
   1.318 +	    i,cdte[i].te_track_number);
   1.319 +    fprintf(stderr,"cdrom->track[%d].id = %d\n", i,cdrom->track[i].id);
   1.320 +    fprintf(stderr,"cdrom->track[%d].type = %x\n", i,cdrom->track[i].type);
   1.321 +    fprintf(stderr,"cdrom->track[%d].offset = %d\n", i,cdrom->track[i].offset);
   1.322 +    fprintf(stderr,"cdrom->track[%d].length = %d\n", i,cdrom->track[i].length);
   1.323 +  }
   1.324 +#endif
   1.325 +    if ( i == (cdrom->numtracks+1) ) {
   1.326 +	okay = 1;
   1.327 +    }
   1.328 +
   1.329 +    return(okay ? 0 : -1);
   1.330 +}
   1.331 +
   1.332 +/* Get CD-ROM status */
   1.333 +static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
   1.334 +{
   1.335 +    CDstatus                     status;
   1.336 +    struct cd_sub_channel            sc;
   1.337 +    struct cd_subc_channel_data     scd;
   1.338 +
   1.339 +    sc.sch_address_format = CDROM_LBA_FORMAT;
   1.340 +    sc.sch_data_format    = CDROM_CURRENT_POSITION;
   1.341 +    sc.sch_track_number   = 0;
   1.342 +    sc.sch_alloc_length   = sizeof(scd);
   1.343 +    sc.sch_buffer         = (caddr_t)&scd;
   1.344 +    if ( ioctl(cdrom->id, CDROM_READ_SUBCHANNEL, &sc) ) {
   1.345 +	status = CD_ERROR;
   1.346 +	fprintf(stderr,"ioctl error CDROM_READ_SUBCHANNEL \n");
   1.347 +    } else {
   1.348 +	switch (scd.scd_header.sh_audio_status) {
   1.349 +	    case AS_AUDIO_INVALID:
   1.350 +		status = CD_STOPPED;
   1.351 +		break;
   1.352 +	    case AS_PLAY_IN_PROGRESS:
   1.353 +		status = CD_PLAYING;
   1.354 +		break;
   1.355 +	    case AS_PLAY_PAUSED:
   1.356 +		status = CD_PAUSED;
   1.357 +		break;
   1.358 +	    case AS_PLAY_COMPLETED:
   1.359 +		status = CD_STOPPED;
   1.360 +		break;
   1.361 +	    case AS_PLAY_ERROR:
   1.362 +		status = CD_ERROR;
   1.363 +		break;
   1.364 +	    case AS_NO_STATUS:
   1.365 +		status = CD_STOPPED;
   1.366 +		break;
   1.367 +	    default:
   1.368 +		status = CD_ERROR;
   1.369 +		break;
   1.370 +	}
   1.371 +#ifdef DEBUG_CDROM
   1.372 +  fprintf(stderr,"scd.scd_header.sh_audio_status = %x\n",
   1.373 +	scd.scd_header.sh_audio_status);
   1.374 +#endif
   1.375 +    }
   1.376 +    if (position) {
   1.377 +	if (status == CD_PLAYING || (status == CD_PAUSED) ) {
   1.378 +	    *position =
   1.379 +		scd.scd_position_data.scp_absaddr.lba.addr3 << 24 |
   1.380 +		scd.scd_position_data.scp_absaddr.lba.addr2 << 16 |
   1.381 +		scd.scd_position_data.scp_absaddr.lba.addr1 << 8  |
   1.382 +		scd.scd_position_data.scp_absaddr.lba.addr0;
   1.383 +	} else {
   1.384 +	    *position = 0;
   1.385 +	}
   1.386 +    }
   1.387 +
   1.388 +    return status;
   1.389 +}
   1.390 +
   1.391 +/* Start play */
   1.392 +static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
   1.393 +{
   1.394 +/* Play MSF
   1.395 + *
   1.396 + */
   1.397 +    struct cd_play_audio_msf msf;
   1.398 +    int end;
   1.399 +
   1.400 +    bzero(&msf, sizeof(msf));
   1.401 +    end = start +length;
   1.402 +    FRAMES_TO_MSF(start,
   1.403 +		  &msf.msf_starting_M_unit,
   1.404 +		  &msf.msf_starting_S_unit,
   1.405 +		  &msf.msf_starting_F_unit);
   1.406 +    FRAMES_TO_MSF(end,
   1.407 +		  &msf.msf_ending_M_unit,
   1.408 +		  &msf.msf_ending_S_unit,
   1.409 +		  &msf.msf_ending_F_unit);
   1.410 +
   1.411 +    return(ioctl(cdrom->id, CDROM_PLAY_AUDIO_MSF, &msf));
   1.412 +}
   1.413 +
   1.414 +/* Pause play */
   1.415 +static int SDL_SYS_CDPause(SDL_CD *cdrom)
   1.416 +{
   1.417 +    return(ioctl(cdrom->id, CDROM_PAUSE_PLAY));
   1.418 +}
   1.419 +
   1.420 +/* Resume play */
   1.421 +static int SDL_SYS_CDResume(SDL_CD *cdrom)
   1.422 +{
   1.423 +    return(ioctl(cdrom->id, CDROM_RESUME_PLAY));
   1.424 +}
   1.425 +
   1.426 +/* Stop play */
   1.427 +static int SDL_SYS_CDStop(SDL_CD *cdrom)
   1.428 +{
   1.429 +    return(ioctl(cdrom->id, SCSI_STOP_UNIT));
   1.430 +}
   1.431 +
   1.432 +/* Eject the CD-ROM */
   1.433 +static int SDL_SYS_CDEject(SDL_CD *cdrom)
   1.434 +{
   1.435 +    return(ioctl(cdrom->id, CDROM_EJECT_CADDY));
   1.436 +}
   1.437 +
   1.438 +/* Close the CD-ROM handle */
   1.439 +static void SDL_SYS_CDClose(SDL_CD *cdrom)
   1.440 +{
   1.441 +    close(cdrom->id);
   1.442 +}
   1.443 +
   1.444 +void SDL_SYS_CDQuit(void)
   1.445 +{
   1.446 +    int i;
   1.447 +
   1.448 +    if ( SDL_numcds > 0 ) {
   1.449 +	for ( i=0; i<SDL_numcds; ++i ) {
   1.450 +	    free(SDL_cdlist[i]);
   1.451 +	}
   1.452 +	SDL_numcds = 0;
   1.453 +    }
   1.454 +}
   1.455 +
   1.456 +