src/cdrom/bsdi/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 252 e8157fcb3114
child 1336 3692456e7b0f
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /*
    24  * Functions for system-level CD-ROM audio control for BSD/OS 4.x
    25  * This started life out as a copy of the freebsd/SDL_cdrom.c file but was
    26  * heavily modified.   Works for standard (MMC) SCSI and ATAPI CDrom drives.
    27  *
    28  * Steven Schultz - sms@to.gd-es.com
    29 */
    30 
    31 #include <sys/types.h>
    32 #include <stdlib.h>
    33 #include <sys/stat.h>
    34 #include <fcntl.h>
    35 #include <stdio.h>
    36 #include <string.h>
    37 #include <errno.h>
    38 #include <err.h>
    39 #include <unistd.h>
    40 #include <sys/ioctl.h>
    41 #include </sys/dev/scsi/scsi.h>
    42 #include </sys/dev/scsi/scsi_ioctl.h>
    43 
    44 #include "SDL_error.h"
    45 #include "SDL_cdrom.h"
    46 #include "SDL_syscdrom.h"
    47 
    48 /*
    49  * The msf_to_frame and frame_to_msf were yanked from libcdrom and inlined
    50  * here so that -lcdrom doesn't have to be dragged in for something so simple.
    51 */
    52 
    53 #define	FRAMES_PER_SECOND	75
    54 #define	FRAMES_PER_MINUTE	(FRAMES_PER_SECOND * 60)
    55 
    56 int
    57 msf_to_frame(int minute, int second, int frame)
    58 	{
    59 	return(minute * FRAMES_PER_MINUTE + second * FRAMES_PER_SECOND + frame);
    60 	}
    61 
    62 void
    63 frame_to_msf(int frame, int *minp, int *secp, int *framep)
    64 	{
    65 	*minp = frame / FRAMES_PER_MINUTE;
    66 	*secp = (frame % FRAMES_PER_MINUTE) / FRAMES_PER_SECOND;
    67 	*framep = frame % FRAMES_PER_SECOND;
    68 	}
    69 
    70 /* The maximum number of CD-ROM drives we'll detect */
    71 #define MAX_DRIVES	16	
    72 
    73 /* A list of available CD-ROM drives */
    74 static char *SDL_cdlist[MAX_DRIVES];
    75 static dev_t SDL_cdmode[MAX_DRIVES];
    76 
    77 /* The system-dependent CD control functions */
    78 static const char *SDL_SYS_CDName(int drive);
    79 static int SDL_SYS_CDOpen(int drive);
    80 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
    81 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
    82 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
    83 static int SDL_SYS_CDPause(SDL_CD *cdrom);
    84 static int SDL_SYS_CDResume(SDL_CD *cdrom);
    85 static int SDL_SYS_CDStop(SDL_CD *cdrom);
    86 static int SDL_SYS_CDEject(SDL_CD *cdrom);
    87 static void SDL_SYS_CDClose(SDL_CD *cdrom);
    88 
    89 typedef	struct	scsi_cdb cdb_t;
    90 
    91 static int scsi_cmd(int fd,
    92 		struct scsi_cdb *cdb,
    93 		int cdblen, 
    94 		int rw,
    95 		caddr_t data,
    96 		int datalen,
    97 		struct scsi_user_cdb *sus)
    98 	{
    99 	int	scsistatus;
   100 	unsigned char	*cp;
   101 	struct	scsi_user_cdb suc;
   102 
   103     /* safety checks */
   104 	if	(!cdb) return(-1);
   105 	if	(rw != SUC_READ && rw != SUC_WRITE) return(-1);
   106 
   107 	suc.suc_flags = rw;
   108 	suc.suc_cdblen = cdblen;
   109 	bcopy(cdb, suc.suc_cdb, cdblen);
   110 	suc.suc_datalen = datalen;
   111 	suc.suc_data = data;
   112 	suc.suc_timeout = 10;		/* 10 secs max for TUR or SENSE */
   113 	if	(ioctl(fd, SCSIRAWCDB, &suc) == -1)
   114 		return(-11);
   115 	scsistatus = suc.suc_sus.sus_status;
   116 	cp = suc.suc_sus.sus_sense;
   117 
   118 /*
   119  * If a place to copy the sense data back to has been provided then the
   120  * caller is responsible for checking the errors and printing any information
   121  * out if the status was not successful.
   122 */
   123 	if	(scsistatus != 0 && !sus)
   124 		{
   125 		fprintf(stderr,"scsistatus = %x cmd = %x\n",
   126 			scsistatus, cdb[0]);
   127 		fprintf(stderr, "sense %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x\n", 
   128 			cp[0], cp[1], cp[2], cp[3], cp[4], cp[5],
   129 			cp[6], cp[7], cp[8], cp[9], cp[10], cp[11],
   130 			cp[12], cp[13], cp[14], cp[15]);
   131 		return(1);
   132 		}
   133 	if	(sus)
   134 		bcopy(&suc, sus, sizeof (struct scsi_user_cdb));
   135 	if	(scsistatus)
   136 		return(1);	/* Return non-zero for unsuccessful status */
   137 	return(0);
   138 	}
   139 
   140 /* request vendor brand and model */
   141 unsigned char *Inquiry(int fd)
   142 	{
   143 	static struct scsi_cdb6 cdb = 
   144 		{
   145 		0x12, 
   146 		0, 0, 0,
   147 		56,
   148 		0
   149 		};
   150 	static unsigned char Inqbuffer[56];
   151 
   152 	if	(scsi_cmd(fd, (cdb_t *)&cdb, 6, SUC_READ, Inqbuffer, 
   153 			sizeof(Inqbuffer), 0))
   154 		return("\377");
   155 	return(Inqbuffer);
   156 	}
   157 
   158 #define ADD_SENSECODE 12
   159 #define ADD_SC_QUALIFIER 13
   160 
   161 int TestForMedium(int fd)
   162 	{
   163 	int	sts, asc, ascq;
   164 	struct	scsi_user_cdb sus;
   165 	static struct scsi_cdb6 cdb =
   166 		{
   167 		CMD_TEST_UNIT_READY, /* command */
   168 		0,	/* reserved */
   169 		0,	/* reserved */
   170 		0,	/* reserved */
   171 		0,	/* reserved */
   172 		0	/* reserved */
   173 		};
   174 
   175 again:	sts = scsi_cmd(fd, (cdb_t *)&cdb, 6, SUC_READ, 0, 0, &sus);
   176 	asc = sus.suc_sus.sus_sense[ADD_SENSECODE];
   177 	ascq = sus.suc_sus.sus_sense[ADD_SC_QUALIFIER];
   178 	if	(asc == 0x3a && ascq == 0x0)	/* no medium */
   179 		return(0);
   180 	if	(asc == 0x28 && ascq == 0x0)	/* medium changed */
   181 		goto again;
   182 	if	(asc == 0x4 && ascq == 0x1 )	/* coming ready */
   183 		{
   184 		sleep(2);
   185 		goto again;
   186 		}
   187 	return(1);
   188 	}
   189 
   190 /* Check a drive to see if it is a CD-ROM */
   191 static int CheckDrive(char *drive, struct stat *stbuf)
   192 {
   193 	int is_cd = 0, cdfd;
   194 	char *p;
   195 
   196 	/* If it doesn't exist, return -1 */
   197 	if ( stat(drive, stbuf) < 0 ) {
   198 		return(-1);
   199 	}
   200 
   201 	/* If it does exist, verify that it's an available CD-ROM */
   202 	cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
   203 	if ( cdfd >= 0 ) {
   204 		p = Inquiry(cdfd);
   205 		if (*p == TYPE_ROM)
   206 			is_cd = 1;
   207 		close(cdfd);
   208 	}
   209 	return(is_cd);
   210 }
   211 
   212 /* Add a CD-ROM drive to our list of valid drives */
   213 static void AddDrive(char *drive, struct stat *stbuf)
   214 {
   215 	int i;
   216 
   217 	if ( SDL_numcds < MAX_DRIVES ) {
   218 		/* Check to make sure it's not already in our list.
   219 	 	   This can happen when we see a drive via symbolic link.
   220 		 */
   221 		for ( i=0; i<SDL_numcds; ++i ) {
   222 			if ( stbuf->st_rdev == SDL_cdmode[i] ) {
   223 #ifdef DEBUG_CDROM
   224   fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
   225 #endif
   226 				return;
   227 			}
   228 		}
   229 
   230 		/* Add this drive to our list */
   231 		i = SDL_numcds;
   232 		SDL_cdlist[i] = (char *)malloc(strlen(drive)+1);
   233 		if ( SDL_cdlist[i] == NULL ) {
   234 			SDL_OutOfMemory();
   235 			return;
   236 		}
   237 		strcpy(SDL_cdlist[i], drive);
   238 		SDL_cdmode[i] = stbuf->st_rdev;
   239 		++SDL_numcds;
   240 #ifdef DEBUG_CDROM
   241   fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
   242 #endif
   243 	}
   244 }
   245 
   246 int  SDL_SYS_CDInit(void)
   247 {
   248 	/* checklist: /dev/rsr?c */
   249 	static char *checklist[] = {
   250 	"?0 rsr?", NULL
   251 	};
   252 	char *SDLcdrom;
   253 	int i, j, exists;
   254 	char drive[32];
   255 	struct stat stbuf;
   256 
   257 	/* Fill in our driver capabilities */
   258 	SDL_CDcaps.Name = SDL_SYS_CDName;
   259 	SDL_CDcaps.Open = SDL_SYS_CDOpen;
   260 	SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
   261 	SDL_CDcaps.Status = SDL_SYS_CDStatus;
   262 	SDL_CDcaps.Play = SDL_SYS_CDPlay;
   263 	SDL_CDcaps.Pause = SDL_SYS_CDPause;
   264 	SDL_CDcaps.Resume = SDL_SYS_CDResume;
   265 	SDL_CDcaps.Stop = SDL_SYS_CDStop;
   266 	SDL_CDcaps.Eject = SDL_SYS_CDEject;
   267 	SDL_CDcaps.Close = SDL_SYS_CDClose;
   268 
   269 	/* Look in the environment for our CD-ROM drive list */
   270 	SDLcdrom = getenv("SDL_CDROM");	/* ':' separated list of devices */
   271 	if ( SDLcdrom != NULL ) {
   272 		char *cdpath, *delim;
   273 		cdpath = malloc(strlen(SDLcdrom)+1);
   274 		if ( cdpath != NULL ) {
   275 			strcpy(cdpath, SDLcdrom);
   276 			SDLcdrom = cdpath;
   277 			do {
   278 				delim = strchr(SDLcdrom, ':');
   279 				if ( delim ) {
   280 					*delim++ = '\0';
   281 				}
   282 				if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
   283 					AddDrive(SDLcdrom, &stbuf);
   284 				}
   285 				if ( delim ) {
   286 					SDLcdrom = delim;
   287 				} else {
   288 					SDLcdrom = NULL;
   289 				}
   290 			} while ( SDLcdrom );
   291 			free(cdpath);
   292 		}
   293 
   294 		/* If we found our drives, there's nothing left to do */
   295 		if ( SDL_numcds > 0 ) {
   296 			return(0);
   297 		}
   298 	}
   299 
   300 	/* Scan the system for CD-ROM drives */
   301 	for ( i=0; checklist[i]; ++i ) {
   302 		if ( checklist[i][0] == '?' ) {
   303 			char *insert;
   304 			exists = 1;
   305 			for ( j=checklist[i][1]; exists; ++j ) {
   306 				sprintf(drive, "/dev/%sc", &checklist[i][3]);
   307 				insert = strchr(drive, '?');
   308 				if ( insert != NULL ) {
   309 					*insert = j;
   310 				}
   311 				switch (CheckDrive(drive, &stbuf)) {
   312 					/* Drive exists and is a CD-ROM */
   313 					case 1:
   314 						AddDrive(drive, &stbuf);
   315 						break;
   316 					/* Drive exists, but isn't a CD-ROM */
   317 					case 0:
   318 						break;
   319 					/* Drive doesn't exist */
   320 					case -1:
   321 						exists = 0;
   322 						break;
   323 				}
   324 			}
   325 		} else {
   326 			sprintf(drive, "/dev/%s", checklist[i]);
   327 			if ( CheckDrive(drive, &stbuf) > 0 ) {
   328 				AddDrive(drive, &stbuf);
   329 			}
   330 		}
   331 	}
   332 	return(0);
   333 }
   334 
   335 static const char *SDL_SYS_CDName(int drive)
   336 {
   337 	return(SDL_cdlist[drive]);
   338 }
   339 
   340 static int SDL_SYS_CDOpen(int drive)
   341 {
   342 	return(open(SDL_cdlist[drive], O_RDONLY | O_NONBLOCK | O_EXCL, 0));
   343 }
   344 
   345 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
   346 	{
   347 	u_char cdb[10], buf[4], *p, *toc;
   348 	struct scsi_user_cdb sus;
   349 	int i, sts, first_track, last_track, ntracks, toc_size;
   350 
   351 	bzero(cdb, sizeof (cdb));
   352 	cdb[0] = 0x43;		/* Read TOC */
   353 	cdb[1] = 0x2;		/* MSF */
   354 	cdb[8] = 4;		/* size TOC header */
   355 	sts = scsi_cmd(cdrom->id, (cdb_t *)cdb, 10, SUC_READ, buf, 4, &sus);
   356 	if	(sts < 0)
   357 		return(-1);
   358 	first_track = buf[2];
   359 	last_track = buf[3];
   360 	ntracks = last_track - first_track + 1;
   361 	cdrom->numtracks = ntracks;
   362 	toc_size = 4 + (ntracks + 1) * 8;
   363 	toc = (u_char *)malloc(toc_size);
   364 	if	(toc == NULL)
   365 		return(-1);
   366 	bzero(cdb, sizeof (cdb));
   367 	cdb[0] = 0x43;
   368 	cdb[1] = 0x2;
   369 	cdb[6] = first_track;
   370 	cdb[7] = toc_size >> 8;
   371 	cdb[8] = toc_size & 0xff;
   372 	sts = scsi_cmd(cdrom->id, (cdb_t *)cdb, 10, SUC_READ, toc, toc_size, 
   373 			&sus);
   374 	if	(sts < 0)
   375 		{
   376 		free(toc);
   377 		return(-1);
   378 		}
   379 
   380 	for	(i = 0, p = toc+4; i <= ntracks; i++, p+= 8)
   381 		{
   382 		if	(i == ntracks)
   383 			cdrom->track[i].id = 0xAA;	/* Leadout */
   384 		else
   385 			cdrom->track[i].id = first_track + i;
   386 		if	(p[1] & 0x20)
   387 			cdrom->track[i].type = SDL_DATA_TRACK;
   388 		else
   389 			cdrom->track[i].type = SDL_AUDIO_TRACK;
   390 		cdrom->track[i].offset = msf_to_frame(p[5], p[6], p[7]);
   391 		cdrom->track[i].length = 0;
   392 		if	(i > 0)
   393 			cdrom->track[i-1].length = cdrom->track[i].offset -
   394 						   cdrom->track[i-1].offset;
   395 		}
   396 	free(toc);
   397 	return(0);
   398 	}
   399 
   400 /* Get CD-ROM status */
   401 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
   402 	{
   403 	CDstatus status;
   404 	u_char	cdb[10], buf[16];
   405 	int	sts;
   406 	struct	scsi_user_cdb sus;
   407 
   408 	bzero(cdb, sizeof (cdb));
   409 	cdb[0] = 0x42;		/* read subq */
   410 	cdb[1] = 0x2;		/* MSF */
   411 	cdb[2] = 0x40;		/* q channel */
   412 	cdb[3] = 1;		/* current pos */
   413 	cdb[7] = sizeof (buf) >> 8;
   414 	cdb[8] = sizeof (buf) & 0xff;
   415 	sts = scsi_cmd(cdrom->id, (cdb_t *)cdb, 10, SUC_READ, buf, sizeof (buf),
   416 			&sus);
   417 	if	(sts < 0)
   418 		return(-1);
   419 	if	(sts)
   420 		{
   421 		if	(TestForMedium(cdrom->id) == 0)
   422 			status = CD_TRAYEMPTY;
   423 		else
   424 			status = CD_ERROR;
   425 		}
   426 	else
   427 		{
   428 		switch	(buf[1])
   429 			{
   430 			case	0x11:
   431 				status = CD_PLAYING;
   432 				break;
   433 			case	0x12:
   434 				status = CD_PAUSED;
   435 				break;
   436 			case	0x13:
   437 			case	0x14:
   438 			case	0x15:
   439 				status = CD_STOPPED;
   440 				break;
   441 			default:
   442 				status = CD_ERROR;
   443 				break;
   444 			}
   445 		}
   446 	if	(position)
   447 		{
   448 		if	( status == CD_PLAYING || (status == CD_PAUSED) )
   449 			*position = msf_to_frame(buf[9], buf[10], buf[11]);
   450 		else
   451 			*position = 0;
   452 		}
   453 	return(status);
   454 	}
   455 
   456 /* Start play */
   457 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
   458 	{
   459 	u_char	cdb[10];
   460 	int	sts, minute, second, frame, eminute, esecond, eframe;
   461 	struct	scsi_user_cdb sus;
   462 
   463 	bzero(cdb, sizeof(cdb));
   464 	cdb[0] = 0x47;		/* Play */
   465 	frame_to_msf(start, &minute, &second, &frame);
   466 	frame_to_msf(start + length, &eminute, &esecond, &eframe);
   467 	cdb[3] = minute;
   468 	cdb[4] = second;
   469 	cdb[5] = frame;
   470 	cdb[6] = eminute;
   471 	cdb[7] = esecond;
   472 	cdb[8] = eframe;
   473 	sts = scsi_cmd(cdrom->id, (cdb_t *)cdb, 10, SUC_READ, 0, 0, &sus);
   474 	return(sts);
   475 	}
   476 
   477 static	int
   478 pauseresume(SDL_CD *cdrom, int flag)
   479 	{
   480 	u_char	cdb[10];
   481 	struct	scsi_user_cdb sus;
   482 
   483 	bzero(cdb, sizeof (cdb));
   484 	cdb[0] = 0x4b;
   485 	cdb[8] = flag & 0x1;
   486 	return(scsi_cmd(cdrom->id, (cdb_t *)cdb, 10, SUC_READ, 0, 0, &sus));
   487 	}
   488 
   489 /* Pause play */
   490 static int SDL_SYS_CDPause(SDL_CD *cdrom)
   491 {
   492 	return(pauseresume(cdrom, 0));
   493 }
   494 
   495 /* Resume play */
   496 static int SDL_SYS_CDResume(SDL_CD *cdrom)
   497 {
   498 	return(pauseresume(cdrom, 1));
   499 }
   500 
   501 /* Stop play */
   502 static int SDL_SYS_CDStop(SDL_CD *cdrom)
   503 {
   504 	u_char cdb[6];
   505 	struct	scsi_user_cdb sus;
   506 
   507 	bzero(cdb, sizeof (cdb));
   508 	cdb[0] = 0x1b;		/* stop */
   509 	cdb[1] = 1;		/* immediate */
   510 	return(scsi_cmd(cdrom->id, (cdb_t *)cdb, 6, SUC_READ, 0, 0, &sus));
   511 }
   512 
   513 /* Eject the CD-ROM */
   514 static int SDL_SYS_CDEject(SDL_CD *cdrom)
   515 {
   516 	u_char cdb[6];
   517 	struct	scsi_user_cdb sus;
   518 
   519 	bzero(cdb, sizeof (cdb));
   520 	cdb[0] = 0x1b;		/* stop */
   521 	cdb[1] = 1;		/* immediate */
   522 	cdb[4] = 2;		/* eject */
   523 	return(scsi_cmd(cdrom->id, (cdb_t *)cdb, 6, SUC_READ, 0, 0, &sus));
   524 }
   525 
   526 /* Close the CD-ROM handle */
   527 static void SDL_SYS_CDClose(SDL_CD *cdrom)
   528 	{
   529 	close(cdrom->id);
   530 	}
   531 
   532 void SDL_SYS_CDQuit(void)
   533 {
   534 	int i;
   535 
   536 	if ( SDL_numcds > 0 ) {
   537 		for ( i=0; i<SDL_numcds; ++i ) {
   538 			free(SDL_cdlist[i]);
   539 			}
   540 		}
   541 	SDL_numcds = 0;
   542 }