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