src/cdrom/osf/SDL_syscdrom.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 29 Oct 2006 14:45:46 +0000
changeset 2063 dea73e1d07b0
parent 1895 c121d94672cb
child 2669 e27bdcc80744
permissions -rw-r--r--
Merged Ryan's fix, in case we need it later.
     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 #include "SDL_config.h"
    22 
    23 #ifdef SDL_CDROM_OSF
    24 
    25 /* Functions for system-level CD-ROM audio control */
    26 
    27 /* #define DEBUG_CDROM 1 */
    28 
    29 #include <sys/types.h>
    30 #include <dirent.h>
    31 #include <sys/stat.h>
    32 #include <fcntl.h>
    33 #include <io/cam/cdrom.h>
    34 #include <io/cam/rzdisk.h>
    35 #include <io/common/devgetinfo.h>
    36 
    37 #include "SDL_cdrom.h"
    38 #include "../SDL_syscdrom.h"
    39 
    40 /* The maximum number of CD-ROM drives we'll detect */
    41 #define MAX_DRIVES 16
    42 
    43 /* A list of available CD-ROM drives */
    44 static char *SDL_cdlist[MAX_DRIVES];
    45 static dev_t SDL_cdmode[MAX_DRIVES];
    46 
    47 /* The system-dependent CD control functions */
    48 static const char *SDL_SYS_CDName(int drive);
    49 static int SDL_SYS_CDOpen(int drive);
    50 static int SDL_SYS_CDGetTOC(SDL_CD * cdrom);
    51 static CDstatus SDL_SYS_CDStatus(SDL_CD * cdrom, int *position);
    52 static int SDL_SYS_CDPlay(SDL_CD * cdrom, int start, int length);
    53 static int SDL_SYS_CDPause(SDL_CD * cdrom);
    54 static int SDL_SYS_CDResume(SDL_CD * cdrom);
    55 static int SDL_SYS_CDStop(SDL_CD * cdrom);
    56 static int SDL_SYS_CDEject(SDL_CD * cdrom);
    57 static void SDL_SYS_CDClose(SDL_CD * cdrom);
    58 
    59 /* Check a drive to see if it is a CD-ROM */
    60 /* Caution!! Not tested. */
    61 static int
    62 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     };
    72 #endif
    73 
    74     bzero(&msp, sizeof(msp));
    75     bzero(&inq, sizeof(inq));
    76 
    77     /* If it doesn't exist, return -1 */
    78     if (stat(drive, stbuf) < 0) {
    79         return (-1);
    80     }
    81 
    82     if ((cdfd = open(drive, (O_RDWR | O_NDELAY), 0)) >= 0) {
    83         msp.msp_addr = (caddr_t) & inq;
    84         msp.msp_pgcode = 0;
    85         msp.msp_pgctrl = 0;
    86         msp.msp_length = sizeof(inq);
    87         msp.msp_setps = 0;
    88 
    89         if (ioctl(cdfd, SCSI_GET_INQUIRY_DATA, &msp))
    90             return (0);
    91 
    92 #ifdef DEBUG_CDROM
    93         fprintf(stderr, "Device Type: %s\n", devtype[inq.perfdt]);
    94         fprintf(stderr, "Vendor: %.8s\n", inq.vndrid);
    95         fprintf(stderr, "Product: %.8s\n", inq.prodid);
    96         fprintf(stderr, "Revision: %.8s\n", inq.revlvl);
    97 #endif
    98         if (inq.perfdt == DTYPE_RODIRECT)
    99             is_cd = 1;
   100     }
   101 
   102     return (is_cd);
   103 }
   104 
   105 /* Add a CD-ROM drive to our list of valid drives */
   106 static void
   107 AddDrive(char *drive, struct stat *stbuf)
   108 {
   109     int i;
   110 
   111     if (SDL_numcds < MAX_DRIVES) {
   112         /* Check to make sure it's not already in our list.
   113          * This can happen when we see a drive via symbolic link.
   114          *
   115          */
   116         for (i = 0; i < SDL_numcds; ++i) {
   117             if (stbuf->st_rdev == SDL_cdmode[i]) {
   118 #ifdef DEBUG_CDROM
   119                 fprintf(stderr, "Duplicate drive detected: %s == %s\n",
   120                         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] = SDL_strdup(drive);
   129         if (SDL_cdlist[i] == NULL) {
   130             SDL_OutOfMemory();
   131             return;
   132         }
   133         SDL_cdmode[i] = stbuf->st_rdev;
   134         ++SDL_numcds;
   135 #ifdef DEBUG_CDROM
   136         fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
   137 #endif
   138     }
   139 }
   140 
   141 int
   142 SDL_SYS_CDInit(void)
   143 {
   144     /* checklist:
   145      *
   146      * Tru64 5.X (/dev/rdisk/cdrom?c)
   147      * dir: /dev/rdisk, name: cdrom
   148      *
   149      * Digital UNIX 4.0X (/dev/rrz?c)
   150      * dir: /dev, name: rrz
   151      *
   152      */
   153     struct
   154     {
   155         char *dir;
   156         char *name;
   157     } checklist[] = {
   158         {
   159         "/dev/rdisk", "cdrom"}, {
   160         "/dev", "rrz"}, {
   161     NULL, NULL}};
   162     char drive[32];
   163     char *SDLcdrom;
   164     int i, j, exists;
   165     struct stat stbuf;
   166 
   167     /* Fill in our driver capabilities */
   168     SDL_CDcaps.Name = SDL_SYS_CDName;
   169     SDL_CDcaps.Open = SDL_SYS_CDOpen;
   170     SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
   171     SDL_CDcaps.Status = SDL_SYS_CDStatus;
   172     SDL_CDcaps.Play = SDL_SYS_CDPlay;
   173     SDL_CDcaps.Pause = SDL_SYS_CDPause;
   174     SDL_CDcaps.Resume = SDL_SYS_CDResume;
   175     SDL_CDcaps.Stop = SDL_SYS_CDStop;
   176     SDL_CDcaps.Eject = SDL_SYS_CDEject;
   177     SDL_CDcaps.Close = SDL_SYS_CDClose;
   178 
   179 
   180     /* Look in the environment for our CD-ROM drive list */
   181     SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */
   182     if (SDLcdrom != NULL) {
   183         char *cdpath, *delim;
   184         size_t len = SDL_strlen(SDLcdrom) + 1;
   185         cdpath = SDL_stack_alloc(char, len);
   186         if (cdpath != NULL) {
   187             SDL_strlcpy(cdpath, SDLcdrom, len);
   188             SDLcdrom = cdpath;
   189             do {
   190                 delim = SDL_strchr(SDLcdrom, ':');
   191                 if (delim) {
   192                     *delim++ = '\0';
   193                 }
   194                 if (CheckDrive(SDLcdrom, &stbuf) > 0) {
   195                     AddDrive(SDLcdrom, &stbuf);
   196                 }
   197                 if (delim) {
   198                     SDLcdrom = delim;
   199                 } else {
   200                     SDLcdrom = NULL;
   201                 }
   202             }
   203             while (SDLcdrom);
   204             SDL_stack_free(cdpath);
   205         }
   206 
   207         /* If we found our drives, there's nothing left to do */
   208         if (SDL_numcds > 0) {
   209             return (0);
   210         }
   211     }
   212     /* Scan the system for CD-ROM drives */
   213     for (i = 0; checklist[i].dir; ++i) {
   214         DIR *devdir;
   215         struct dirent *devent;
   216         int name_len;
   217 
   218         devdir = opendir(checklist[i].dir);
   219         if (devdir) {
   220             name_len = SDL_strlen(checklist[i].name);
   221             while (devent = readdir(devdir))
   222                 if (SDL_memcmp
   223                     (checklist[i].name, devent->d_name, name_len) == 0)
   224                     if (devent->d_name[devent->d_namlen - 1] == 'c') {
   225                         SDL_snprintf(drive, SDL_arraysize(drive),
   226                                      "%s/%s", checklist[i].dir,
   227                                      devent->d_name);
   228 #ifdef DEBUG_CDROM
   229                         fprintf(stderr, "Try to add drive: %s\n", drive);
   230 #endif
   231                         if (CheckDrive(drive, &stbuf) > 0)
   232                             AddDrive(drive, &stbuf);
   233                     }
   234             closedir(devdir);
   235         } else {
   236 #ifdef DEBUG_CDROM
   237             fprintf(stderr, "cannot open dir: %s\n", checklist[i].dir);
   238 #endif
   239         }
   240     }
   241     return (0);
   242 }
   243 
   244 static const char *
   245 SDL_SYS_CDName(int drive)
   246 {
   247     return (SDL_cdlist[drive]);
   248 }
   249 
   250 static int
   251 SDL_SYS_CDOpen(int drive)
   252 {
   253     /* O_RDWR: To use ioctl(fd, SCSI_STOP_UNIT) */
   254     return (open(SDL_cdlist[drive], (O_RDWR | O_NDELAY), 0));
   255 }
   256 
   257 static int
   258 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 = cdte[i].te_control & CDROM_DATA_TRACK;
   304         cdrom->track[i].offset =
   305             cdte[i].te_absaddr.lba.addr3 << 24 |
   306             cdte[i].te_absaddr.lba.addr2 << 16 |
   307             cdte[i].te_absaddr.lba.addr1 << 8 | cdte[i].te_absaddr.lba.addr0;
   308         cdrom->track[i].length = 0;
   309         if (i > 0) {
   310             cdrom->track[i - 1].length =
   311                 cdrom->track[i].offset - 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,
   320                 cdrom->track[i].type);
   321         fprintf(stderr, "cdrom->track[%d].offset = %d\n", i,
   322                 cdrom->track[i].offset);
   323         fprintf(stderr, "cdrom->track[%d].length = %d\n", i,
   324                 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
   336 SDL_SYS_CDStatus(SDL_CD * cdrom, int *position)
   337 {
   338     CDstatus status;
   339     struct cd_sub_channel sc;
   340     struct cd_subc_channel_data scd;
   341 
   342     sc.sch_address_format = CDROM_LBA_FORMAT;
   343     sc.sch_data_format = CDROM_CURRENT_POSITION;
   344     sc.sch_track_number = 0;
   345     sc.sch_alloc_length = sizeof(scd);
   346     sc.sch_buffer = (caddr_t) & scd;
   347     if (ioctl(cdrom->id, CDROM_READ_SUBCHANNEL, &sc)) {
   348         status = CD_ERROR;
   349         fprintf(stderr, "ioctl error CDROM_READ_SUBCHANNEL \n");
   350     } else {
   351         switch (scd.scd_header.sh_audio_status) {
   352         case AS_AUDIO_INVALID:
   353             status = CD_STOPPED;
   354             break;
   355         case AS_PLAY_IN_PROGRESS:
   356             status = CD_PLAYING;
   357             break;
   358         case AS_PLAY_PAUSED:
   359             status = CD_PAUSED;
   360             break;
   361         case AS_PLAY_COMPLETED:
   362             status = CD_STOPPED;
   363             break;
   364         case AS_PLAY_ERROR:
   365             status = CD_ERROR;
   366             break;
   367         case AS_NO_STATUS:
   368             status = CD_STOPPED;
   369             break;
   370         default:
   371             status = CD_ERROR;
   372             break;
   373         }
   374 #ifdef DEBUG_CDROM
   375         fprintf(stderr, "scd.scd_header.sh_audio_status = %x\n",
   376                 scd.scd_header.sh_audio_status);
   377 #endif
   378     }
   379     if (position) {
   380         if (status == CD_PLAYING || (status == CD_PAUSED)) {
   381             *position =
   382                 scd.scd_position_data.scp_absaddr.lba.addr3 << 24 |
   383                 scd.scd_position_data.scp_absaddr.lba.addr2 << 16 |
   384                 scd.scd_position_data.scp_absaddr.lba.addr1 << 8 |
   385                 scd.scd_position_data.scp_absaddr.lba.addr0;
   386         } else {
   387             *position = 0;
   388         }
   389     }
   390 
   391     return status;
   392 }
   393 
   394 /* Start play */
   395 static int
   396 SDL_SYS_CDPlay(SDL_CD * cdrom, int start, int length)
   397 {
   398 /*
   399  * Play MSF
   400  */
   401     struct cd_play_audio_msf msf;
   402     int end;
   403 
   404     bzero(&msf, sizeof(msf));
   405     end = start + length;
   406     FRAMES_TO_MSF(start + 150,  /* LBA = 4500*M + 75*S + F - 150 */
   407                   &msf.msf_starting_M_unit,
   408                   &msf.msf_starting_S_unit, &msf.msf_starting_F_unit);
   409     FRAMES_TO_MSF(end + 150,    /* LBA = 4500*M + 75*S + F - 150 */
   410                   &msf.msf_ending_M_unit,
   411                   &msf.msf_ending_S_unit, &msf.msf_ending_F_unit);
   412 
   413     return (ioctl(cdrom->id, CDROM_PLAY_AUDIO_MSF, &msf));
   414 }
   415 
   416 /* Pause play */
   417 static int
   418 SDL_SYS_CDPause(SDL_CD * cdrom)
   419 {
   420     return (ioctl(cdrom->id, CDROM_PAUSE_PLAY));
   421 }
   422 
   423 /* Resume play */
   424 static int
   425 SDL_SYS_CDResume(SDL_CD * cdrom)
   426 {
   427     return (ioctl(cdrom->id, CDROM_RESUME_PLAY));
   428 }
   429 
   430 /* Stop play */
   431 static int
   432 SDL_SYS_CDStop(SDL_CD * cdrom)
   433 {
   434     return (ioctl(cdrom->id, SCSI_STOP_UNIT));
   435 }
   436 
   437 /* Eject the CD-ROM */
   438 static int
   439 SDL_SYS_CDEject(SDL_CD * cdrom)
   440 {
   441     return (ioctl(cdrom->id, CDROM_EJECT_CADDY));
   442 }
   443 
   444 /* Close the CD-ROM handle */
   445 static void
   446 SDL_SYS_CDClose(SDL_CD * cdrom)
   447 {
   448     close(cdrom->id);
   449 }
   450 
   451 void
   452 SDL_SYS_CDQuit(void)
   453 {
   454     int i;
   455 
   456     if (SDL_numcds > 0) {
   457         for (i = 0; i < SDL_numcds; ++i) {
   458             SDL_free(SDL_cdlist[i]);
   459         }
   460         SDL_numcds = 0;
   461     }
   462 }
   463 
   464 #endif /* SDL_CDROM_OSF */
   465 /* vi: set ts=4 sw=4 expandtab: */