19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Tru64 audio module for SDL (Simple DirectMedia Layer)
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 2003
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_CDROM_OSF
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Functions for system-level CD-ROM audio control */
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* #define DEBUG_CDROM 1 */
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <dirent.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/stat.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <io/cam/cdrom.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <io/cam/rzdisk.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <io/common/devgetinfo.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_cdrom.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_syscdrom.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The maximum number of CD-ROM drives we'll detect */
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_DRIVES 16
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* A list of available CD-ROM drives */
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic char *SDL_cdlist[MAX_DRIVES];
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic dev_t SDL_cdmode[MAX_DRIVES];
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The system-dependent CD control functions */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDOpen(int drive);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDGetTOC(SDL_CD *cdrom);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus    SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDPause(SDL_CD *cdrom);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDResume(SDL_CD *cdrom);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDStop(SDL_CD *cdrom);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int         SDL_SYS_CDEject(SDL_CD *cdrom);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void        SDL_SYS_CDClose(SDL_CD *cdrom);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Check a drive to see if it is a CD-ROM */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Caution!! Not tested. */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int CheckDrive(char *drive, struct stat *stbuf)
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int cdfd, is_cd = 0;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct mode_sel_sns_params msp;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct inquiry_info inq;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *devtype[] = {"Disk", "Tape", "Printer", "Processor", "WORM",
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"CD-ROM", "Scanner", "Optical", "Changer", "Comm", "Unknown"};
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bzero(&msp, sizeof(msp));
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bzero(&inq, sizeof(inq));
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* If it doesn't exist, return -1 */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( stat(drive, stbuf) < 0 ) {
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(-1);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (cdfd = open(drive, (O_RDWR|O_NDELAY), 0)) >= 0 ) {
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	msp.msp_addr   =   (caddr_t) &inq;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	msp.msp_pgcode =                0;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	msp.msp_pgctrl =                0;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	msp.msp_length =      sizeof(inq);
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	msp.msp_setps  =                0;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(cdfd, SCSI_GET_INQUIRY_DATA, &msp) )
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return (0);
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Device Type: %s\n", devtype[inq.perfdt]);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Vendor: %.8s\n", inq.vndrid);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Product: %.8s\n", inq.prodid);
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Revision: %.8s\n", inq.revlvl);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( inq.perfdt == DTYPE_RODIRECT )
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    is_cd = 1;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(is_cd);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Add a CD-ROM drive to our list of valid drives */
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void AddDrive(char *drive, struct stat *stbuf)
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_numcds < MAX_DRIVES ) {
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to make sure it's not already in our list.
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * This can happen when we see a drive via symbolic link.
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_numcds; ++i ) {
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if ( stbuf->st_rdev == SDL_cdmode[i] ) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Add this drive to our list */
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	i = SDL_numcds;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_cdlist[i] = SDL_strdup(drive);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_cdlist[i] == NULL ) {
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_OutOfMemory();
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_cdmode[i] = stbuf->st_rdev;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++SDL_numcds;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint  SDL_SYS_CDInit(void)
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* checklist:
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * Tru64 5.X (/dev/rdisk/cdrom?c)
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * dir: /dev/rdisk, name: cdrom
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * Digital UNIX 4.0X (/dev/rrz?c)
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     * dir: /dev, name: rrz
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     *
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     */
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct {
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *dir;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *name;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } checklist[] = {
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{"/dev/rdisk", "cdrom"},
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{"/dev", "rrz"},
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{NULL, NULL}};
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char drive[32];
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char *SDLcdrom;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i, j, exists;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct stat stbuf;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Fill in our driver capabilities */
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Name   = SDL_SYS_CDName;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Open   = SDL_SYS_CDOpen;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Status = SDL_SYS_CDStatus;
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Play   = SDL_SYS_CDPlay;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Pause  = SDL_SYS_CDPause;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Resume = SDL_SYS_CDResume;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Stop   = SDL_SYS_CDStop;
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Eject  = SDL_SYS_CDEject;
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CDcaps.Close  = SDL_SYS_CDClose;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Look in the environment for our CD-ROM drive list */
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDLcdrom = SDL_getenv("SDL_CDROM");	/* ':' separated list of devices */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDLcdrom != NULL ) {
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *cdpath, *delim;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t len = SDL_strlen(SDLcdrom)+1;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdpath = SDL_stack_alloc(char, len);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cdpath != NULL ) {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_strlcpy(cdpath, SDLcdrom, len);
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDLcdrom = cdpath;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    do {
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		delim = SDL_strchr(SDLcdrom, ':');
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( delim ) {
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    *delim++ = '\0';
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    AddDrive(SDLcdrom, &stbuf);
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( delim ) {
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    SDLcdrom = delim;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    SDLcdrom = NULL;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    } while ( SDLcdrom );
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_stack_free(cdpath);
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we found our drives, there's nothing left to do */
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_numcds > 0 ) {
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return(0);
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Scan the system for CD-ROM drives */
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for ( i = 0; checklist[i].dir; ++i) {
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DIR *devdir;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct dirent *devent;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int name_len;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	devdir = opendir(checklist[i].dir);
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (devdir) {
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    name_len = SDL_strlen(checklist[i].name);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    while (devent = readdir(devdir))
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (SDL_memcmp(checklist[i].name, devent->d_name, name_len) == 0)
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    if (devent->d_name[devent->d_namlen-1] == 'c') {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_snprintf(drive, SDL_arraysize(drive), "%s/%s", checklist[i].dir, devent->d_name);
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			fprintf(stderr, "Try to add drive: %s\n", drive);
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( CheckDrive(drive, &stbuf) > 0 )
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    AddDrive(drive, &stbuf);
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    }
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    closedir(devdir);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    fprintf(stderr, "cannot open dir: %s\n", checklist[i].dir);
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (0);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *SDL_SYS_CDName(int drive)
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(SDL_cdlist[drive]);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDOpen(int drive)
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* O_RDWR: To use ioctl(fd, SCSI_STOP_UNIT) */
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(open(SDL_cdlist[drive], (O_RDWR|O_NDELAY), 0));
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct cd_toc                  toc;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct cd_toc_header           hdr;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct cd_toc_entry          *cdte;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int okay = 0;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( ioctl(cdrom->id, CDROM_TOC_HEADER, &hdr) ) {
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr,"ioctl error CDROM_TOC_HEADER\n");
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cdrom->numtracks = hdr.th_ending_track - hdr.th_starting_track + 1;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->numtracks = SDL_MAX_TRACKS;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr,"hdr.th_data_len1 = %d\n", hdr.th_data_len1);
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr,"hdr.th_data_len0 = %d\n", hdr.th_data_len0);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr,"hdr.th_starting_track = %d\n", hdr.th_starting_track);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr,"hdr.th_ending_track = %d\n", hdr.th_ending_track);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr,"cdrom->numtracks = %d\n", cdrom->numtracks);
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    toc.toc_address_format = CDROM_LBA_FORMAT;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    toc.toc_starting_track = 0;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    toc.toc_alloc_length = (hdr.th_data_len1 << 8) +
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    hdr.th_data_len0 + sizeof(hdr);
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( (toc.toc_buffer = alloca(toc.toc_alloc_length)) == NULL) {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr,"cannot allocate toc.toc_buffer\n");
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bzero (toc.toc_buffer, toc.toc_alloc_length);
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ioctl(cdrom->id, CDROM_TOC_ENTRYS, &toc)) {
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr,"ioctl error CDROM_TOC_ENTRYS\n");
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return -1;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cdte =(struct cd_toc_entry *) ((char *) toc.toc_buffer + sizeof(hdr));
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i=0; i <= cdrom->numtracks; ++i) {
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (i == cdrom->numtracks ) {
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdrom->track[i].id = 0xAA;;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdrom->track[i].id = hdr.th_starting_track + i;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->track[i].type =
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdte[i].te_control & CDROM_DATA_TRACK;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->track[i].offset =
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdte[i].te_absaddr.lba.addr3 << 24 |
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdte[i].te_absaddr.lba.addr2 << 16 |
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdte[i].te_absaddr.lba.addr1 << 8  |
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdte[i].te_absaddr.lba.addr0;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cdrom->track[i].length = 0;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( i > 0 ) {
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    cdrom->track[i - 1].length =
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cdrom->track[i].offset -
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cdrom->track[i - 1].offset;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (i = 0; i <= cdrom->numtracks; i++) {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr,"toc_entry[%d].te_track_number = %d\n",
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    i,cdte[i].te_track_number);
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr,"cdrom->track[%d].id = %d\n", i,cdrom->track[i].id);
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr,"cdrom->track[%d].type = %x\n", i,cdrom->track[i].type);
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr,"cdrom->track[%d].offset = %d\n", i,cdrom->track[i].offset);
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fprintf(stderr,"cdrom->track[%d].length = %d\n", i,cdrom->track[i].length);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( i == (cdrom->numtracks+1) ) {
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	okay = 1;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(okay ? 0 : -1);
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Get CD-ROM status */
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    CDstatus                     status;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct cd_sub_channel            sc;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct cd_subc_channel_data     scd;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sc.sch_address_format = CDROM_LBA_FORMAT;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sc.sch_data_format    = CDROM_CURRENT_POSITION;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sc.sch_track_number   = 0;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sc.sch_alloc_length   = sizeof(scd);
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    sc.sch_buffer         = (caddr_t)&scd;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( ioctl(cdrom->id, CDROM_READ_SUBCHANNEL, &sc) ) {
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	status = CD_ERROR;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr,"ioctl error CDROM_READ_SUBCHANNEL \n");
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (scd.scd_header.sh_audio_status) {
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AS_AUDIO_INVALID:
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_STOPPED;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AS_PLAY_IN_PROGRESS:
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_PLAYING;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AS_PLAY_PAUSED:
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_PAUSED;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AS_PLAY_COMPLETED:
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_STOPPED;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AS_PLAY_ERROR:
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_ERROR;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case AS_NO_STATUS:
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_STOPPED;
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		status = CD_ERROR;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_CDROM
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr,"scd.scd_header.sh_audio_status = %x\n",
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scd.scd_header.sh_audio_status);
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (position) {
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (status == CD_PLAYING || (status == CD_PAUSED) ) {
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    *position =
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		scd.scd_position_data.scp_absaddr.lba.addr3 << 24 |
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		scd.scd_position_data.scp_absaddr.lba.addr2 << 16 |
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		scd.scd_position_data.scp_absaddr.lba.addr1 << 8  |
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		scd.scd_position_data.scp_absaddr.lba.addr0;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    *position = 0;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return status;
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Start play */
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Play MSF
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    struct cd_play_audio_msf msf;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int end;
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    bzero(&msf, sizeof(msf));
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    end = start +length;
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FRAMES_TO_MSF(start + 150, /* LBA = 4500*M + 75*S + F - 150 */
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  &msf.msf_starting_M_unit,
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  &msf.msf_starting_S_unit,
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  &msf.msf_starting_F_unit);
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FRAMES_TO_MSF(end + 150, /* LBA = 4500*M + 75*S + F - 150 */
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  &msf.msf_ending_M_unit,
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  &msf.msf_ending_S_unit,
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  &msf.msf_ending_F_unit);
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(ioctl(cdrom->id, CDROM_PLAY_AUDIO_MSF, &msf));
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Pause play */
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDPause(SDL_CD *cdrom)
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(ioctl(cdrom->id, CDROM_PAUSE_PLAY));
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Resume play */
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDResume(SDL_CD *cdrom)
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(ioctl(cdrom->id, CDROM_RESUME_PLAY));
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Stop play */
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDStop(SDL_CD *cdrom)
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(ioctl(cdrom->id, SCSI_STOP_UNIT));
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Eject the CD-ROM */
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int SDL_SYS_CDEject(SDL_CD *cdrom)
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(ioctl(cdrom->id, CDROM_EJECT_CADDY));
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Close the CD-ROM handle */
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SDL_SYS_CDClose(SDL_CD *cdrom)
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    close(cdrom->id);
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_CDQuit(void)
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( SDL_numcds > 0 ) {
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<SDL_numcds; ++i ) {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_free(SDL_cdlist[i]);
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_numcds = 0;
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_CDROM_OSF */
445