cdrom.c
author nathan
Thu, 04 Oct 2012 12:48:27 +0200
branchtrunk
changeset 15 a9348bf5f6e7
parent 4 79da91042fcc
permissions -rw-r--r--
documentation update, bump version
     1 /* cdrom.c
     2 Copyright (c) 2000-2012 Craig Condit, Stefan Huelswitt.
     3 
     4 Redistribution and use in source and binary forms, with or without
     5 modification, are permitted provided that the following conditions are met: 
     6 
     7 1. Redistributions of source code must retain the above copyright notice,
     8    this list of conditions and the following disclaimer. 
     9 2. Redistributions in binary form must reproduce the above copyright notice,
    10    this list of conditions and the following disclaimer in the documentation
    11    and/or other materials provided with the distribution. 
    12 
    13 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
    14 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    16 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
    17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    19 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    20 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    23 SUCH DAMAGE.
    24 */
    25 
    26 #define _LARGEFILE64_SOURCE
    27 
    28 #include <stdlib.h>
    29 #include <stdio.h>
    30 #include <sys/ioctl.h>
    31 
    32 #ifdef linux
    33 #include <linux/fs.h>
    34 #endif
    35 
    36 #include "cdrom.h"
    37 #include "misc.h"
    38 #include "debug.h"
    39 
    40 /* size of leadin/out depending of how many tracks are on cd */
    41 #define LEADOUT_1	11400 /* 1th track */
    42 #define LEADOUT_2	6900  /* 2nd and more tracks */
    43 /* number of (unreadable) runout sectos */
    44 #define RUNOUT          2
    45 
    46 extern int fd;
    47 extern int verbose;
    48 extern char *prg_name;
    49 
    50 /****************************************************************************/
    51 
    52 void get_param(int fd, unsigned long *ahead, unsigned long *fahead)
    53 {
    54 #if defined(BLKRAGET) && defined(BLKFRAGET)
    55   *ahead = *fahead = 0;
    56   ioctl(fd,BLKRAGET,ahead);
    57   ioctl(fd,BLKFRAGET,fahead);
    58   DEBUG("get_param: readahead=%ld freadahead=%ld\n",*ahead,*fahead);
    59 #else
    60   fprintf(stderr,"Can't get readahead parameter. Ioctl's not available\n");
    61 #endif
    62 }
    63 
    64 /****************************************************************************/
    65 
    66 void set_param(int fd, unsigned long ahead, unsigned long fahead)
    67 {
    68 #if defined(BLKRAGET) && defined(BLKFRAGET)
    69   ioctl(fd,BLKRASET,ahead);
    70   ioctl(fd,BLKFRASET,fahead);
    71   DEBUG("set_param: readahead=%ld freadahead=%ld\n",ahead,fahead);
    72 #else
    73   fprintf(stderr,"Can't set readahead parameter. Ioctl's not available\n");
    74 #endif
    75 }
    76 
    77 /****************************************************************************/
    78 
    79 int getCdHeader(struct cd_header *cd)
    80 {
    81   struct cdrom_tochdr cd_header;
    82   struct cdrom_tocentry cd_entry;
    83   int tracks;
    84 
    85   if(ioctl(fd,CDROMREADTOCHDR,&cd_header)<0) {
    86     if(verbose) fprintf(stderr,"%s: Unable to read CD header, assuming empty CD-R\n",prg_name);
    87     cd->start_track=1; cd->end_track=0;
    88     cd->used=0;
    89     return 0;
    90     }
    91 
    92   cd->start_track=cd_header.cdth_trk0;
    93   cd->end_track  =cd_header.cdth_trk1;
    94   tracks=cd->end_track-cd->start_track+1;
    95 
    96   cd_entry.cdte_format=CDROM_LBA;
    97   cd_entry.cdte_track=CDROM_LEADOUT;
    98   if(ioctl(fd,CDROMREADTOCENTRY,&cd_entry)<0) error("Ioctl failed (lead-out)");
    99 
   100   cd->used=cd_entry.cdte_addr.lba + (tracks==1 ? LEADOUT_1:LEADOUT_2);
   101   return tracks;
   102 }
   103 
   104 /****************************************************************************/
   105 
   106 void getCdTrack(int num, struct cd_track *cd)
   107 {
   108   struct cdrom_tocentry cd_entry;
   109 
   110   cd_entry.cdte_format=CDROM_LBA;
   111   cd_entry.cdte_track=num;
   112   if(ioctl(fd,CDROMREADTOCENTRY,&cd_entry)<0) error("Ioctl failed (toc entry)");  
   113 
   114   cd->start_sec   =cd_entry.cdte_addr.lba;
   115   cd->leadout_size=RUNOUT+(num==cd->start_track ? LEADOUT_1:LEADOUT_2);
   116   cd->is_data     =(cd_entry.cdte_ctrl&CDROM_DATA_TRACK) ? 1:0;
   117 }