src/cdrom/osf/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Jan 2005 05:05:21 +0000
changeset 1019 e3b3130f3af8
parent 874 9719e7f51a3a
child 1336 3692456e7b0f
permissions -rw-r--r--
Date: Fri, 31 Dec 2004 04:14:09 +0900
From: Hayashi Naoyuki
Subject: SDL patch for Tru64 UNIX 4.0X

SDL-1.2.8 support only Tru64 5.X.
This patch is for Tru64 UNIX 4.X.(tested on Tru64 UNIX 4.0G and 5.1B)

SDL-1.2.8/configure.in:
ld doesn't accept -pthread option.
cc -pthread has same effect as -D_REENTRANT when compiling,
and has same effect as -lpthread -lexc when linking.

SDL-1.2.8/include/begin_code.h:
Old Compaq C Compiler accept not inline but __inline.

SDL-1.2.8/src/audio/SDL_mixer_MMX.c:
SDL-1.2.8/src/audio/SDL_mixer_MMX.h:
Old Compaq C Compiler doesn't accept //.

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