src/cdrom/osf/SDL_syscdrom.c
author Darren Alton <dalton@stevens.edu>
Tue, 10 Jun 2008 06:45:39 +0000
branchgsoc2008_nds
changeset 2669 e27bdcc80744
parent 1895 c121d94672cb
permissions -rw-r--r--
First commit. Added header configs for DS as well as a Makefile. Initial work on framebuffer video driver, currently not functioning as desired.
     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             } while (SDLcdrom);
   203             SDL_stack_free(cdpath);
   204         }
   205 
   206         /* If we found our drives, there's nothing left to do */
   207         if (SDL_numcds > 0) {
   208             return (0);
   209         }
   210     }
   211     /* Scan the system for CD-ROM drives */
   212     for (i = 0; checklist[i].dir; ++i) {
   213         DIR *devdir;
   214         struct dirent *devent;
   215         int name_len;
   216 
   217         devdir = opendir(checklist[i].dir);
   218         if (devdir) {
   219             name_len = SDL_strlen(checklist[i].name);
   220             while (devent = readdir(devdir))
   221                 if (SDL_memcmp
   222                     (checklist[i].name, devent->d_name, name_len) == 0)
   223                     if (devent->d_name[devent->d_namlen - 1] == 'c') {
   224                         SDL_snprintf(drive, SDL_arraysize(drive),
   225                                      "%s/%s", checklist[i].dir,
   226                                      devent->d_name);
   227 #ifdef DEBUG_CDROM
   228                         fprintf(stderr, "Try to add drive: %s\n", drive);
   229 #endif
   230                         if (CheckDrive(drive, &stbuf) > 0)
   231                             AddDrive(drive, &stbuf);
   232                     }
   233             closedir(devdir);
   234         } else {
   235 #ifdef DEBUG_CDROM
   236             fprintf(stderr, "cannot open dir: %s\n", checklist[i].dir);
   237 #endif
   238         }
   239     }
   240     return (0);
   241 }
   242 
   243 static const char *
   244 SDL_SYS_CDName(int drive)
   245 {
   246     return (SDL_cdlist[drive]);
   247 }
   248 
   249 static int
   250 SDL_SYS_CDOpen(int drive)
   251 {
   252     /* O_RDWR: To use ioctl(fd, SCSI_STOP_UNIT) */
   253     return (open(SDL_cdlist[drive], (O_RDWR | O_NDELAY), 0));
   254 }
   255 
   256 static int
   257 SDL_SYS_CDGetTOC(SDL_CD * cdrom)
   258 {
   259     struct cd_toc toc;
   260     struct cd_toc_header hdr;
   261     struct cd_toc_entry *cdte;
   262     int i;
   263     int okay = 0;
   264     if (ioctl(cdrom->id, CDROM_TOC_HEADER, &hdr)) {
   265         fprintf(stderr, "ioctl error CDROM_TOC_HEADER\n");
   266         return -1;
   267     }
   268     cdrom->numtracks = hdr.th_ending_track - hdr.th_starting_track + 1;
   269     if (cdrom->numtracks > SDL_MAX_TRACKS) {
   270         cdrom->numtracks = SDL_MAX_TRACKS;
   271     }
   272 #ifdef DEBUG_CDROM
   273     fprintf(stderr, "hdr.th_data_len1 = %d\n", hdr.th_data_len1);
   274     fprintf(stderr, "hdr.th_data_len0 = %d\n", hdr.th_data_len0);
   275     fprintf(stderr, "hdr.th_starting_track = %d\n", hdr.th_starting_track);
   276     fprintf(stderr, "hdr.th_ending_track = %d\n", hdr.th_ending_track);
   277     fprintf(stderr, "cdrom->numtracks = %d\n", cdrom->numtracks);
   278 #endif
   279     toc.toc_address_format = CDROM_LBA_FORMAT;
   280     toc.toc_starting_track = 0;
   281     toc.toc_alloc_length = (hdr.th_data_len1 << 8) +
   282         hdr.th_data_len0 + sizeof(hdr);
   283     if ((toc.toc_buffer = alloca(toc.toc_alloc_length)) == NULL) {
   284         fprintf(stderr, "cannot allocate toc.toc_buffer\n");
   285         return -1;
   286     }
   287 
   288     bzero(toc.toc_buffer, toc.toc_alloc_length);
   289     if (ioctl(cdrom->id, CDROM_TOC_ENTRYS, &toc)) {
   290         fprintf(stderr, "ioctl error CDROM_TOC_ENTRYS\n");
   291         return -1;
   292     }
   293 
   294     cdte = (struct cd_toc_entry *) ((char *) toc.toc_buffer + sizeof(hdr));
   295     for (i = 0; i <= cdrom->numtracks; ++i) {
   296         if (i == cdrom->numtracks) {
   297             cdrom->track[i].id = 0xAA;;
   298         } else {
   299             cdrom->track[i].id = hdr.th_starting_track + i;
   300         }
   301 
   302         cdrom->track[i].type = cdte[i].te_control & CDROM_DATA_TRACK;
   303         cdrom->track[i].offset =
   304             cdte[i].te_absaddr.lba.addr3 << 24 |
   305             cdte[i].te_absaddr.lba.addr2 << 16 |
   306             cdte[i].te_absaddr.lba.addr1 << 8 | cdte[i].te_absaddr.lba.addr0;
   307         cdrom->track[i].length = 0;
   308         if (i > 0) {
   309             cdrom->track[i - 1].length =
   310                 cdrom->track[i].offset - cdrom->track[i - 1].offset;
   311         }
   312     }
   313 #ifdef DEBUG_CDROM
   314     for (i = 0; i <= cdrom->numtracks; i++) {
   315         fprintf(stderr, "toc_entry[%d].te_track_number = %d\n",
   316                 i, cdte[i].te_track_number);
   317         fprintf(stderr, "cdrom->track[%d].id = %d\n", i, cdrom->track[i].id);
   318         fprintf(stderr, "cdrom->track[%d].type = %x\n", i,
   319                 cdrom->track[i].type);
   320         fprintf(stderr, "cdrom->track[%d].offset = %d\n", i,
   321                 cdrom->track[i].offset);
   322         fprintf(stderr, "cdrom->track[%d].length = %d\n", i,
   323                 cdrom->track[i].length);
   324     }
   325 #endif
   326     if (i == (cdrom->numtracks + 1)) {
   327         okay = 1;
   328     }
   329 
   330     return (okay ? 0 : -1);
   331 }
   332 
   333 /* Get CD-ROM status */
   334 static CDstatus
   335 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
   395 SDL_SYS_CDPlay(SDL_CD * cdrom, int start, int length)
   396 {
   397 /*
   398  * Play MSF
   399  */
   400     struct cd_play_audio_msf msf;
   401     int end;
   402 
   403     bzero(&msf, sizeof(msf));
   404     end = start + length;
   405     FRAMES_TO_MSF(start + 150,  /* LBA = 4500*M + 75*S + F - 150 */
   406                   &msf.msf_starting_M_unit,
   407                   &msf.msf_starting_S_unit, &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, &msf.msf_ending_F_unit);
   411 
   412     return (ioctl(cdrom->id, CDROM_PLAY_AUDIO_MSF, &msf));
   413 }
   414 
   415 /* Pause play */
   416 static int
   417 SDL_SYS_CDPause(SDL_CD * cdrom)
   418 {
   419     return (ioctl(cdrom->id, CDROM_PAUSE_PLAY));
   420 }
   421 
   422 /* Resume play */
   423 static int
   424 SDL_SYS_CDResume(SDL_CD * cdrom)
   425 {
   426     return (ioctl(cdrom->id, CDROM_RESUME_PLAY));
   427 }
   428 
   429 /* Stop play */
   430 static int
   431 SDL_SYS_CDStop(SDL_CD * cdrom)
   432 {
   433     return (ioctl(cdrom->id, SCSI_STOP_UNIT));
   434 }
   435 
   436 /* Eject the CD-ROM */
   437 static int
   438 SDL_SYS_CDEject(SDL_CD * cdrom)
   439 {
   440     return (ioctl(cdrom->id, CDROM_EJECT_CADDY));
   441 }
   442 
   443 /* Close the CD-ROM handle */
   444 static void
   445 SDL_SYS_CDClose(SDL_CD * cdrom)
   446 {
   447     close(cdrom->id);
   448 }
   449 
   450 void
   451 SDL_SYS_CDQuit(void)
   452 {
   453     int i;
   454 
   455     if (SDL_numcds > 0) {
   456         for (i = 0; i < SDL_numcds; ++i) {
   457             SDL_free(SDL_cdlist[i]);
   458         }
   459         SDL_numcds = 0;
   460     }
   461 }
   462 
   463 #endif /* SDL_CDROM_OSF */
   464 /* vi: set ts=4 sw=4 expandtab: */