1 /* 2 Interface to the CDROM drive. 3 4 -- Roy Keene [021119991200] cdplay@rkeene.org 5 */ 6 7 #include <stdlib.h> 8 #include <err.h> 9 #include <errno.h> 10 #include <asm/errno.h> 11 #include <unistd.h> 12 #include <sys/ioctl.h> 13 #include <fcntl.h> 14 #include <sys/types.h> 15 #include <sys/stat.h> 16 #ifdef DEBUG 17 #include <stdio.h> 18 #endif 19 #include <linux/cdrom.h> 20 #include "cdrom.h" 21 22 23 int open_cdrom(char *cdromfile) { 24 int cdrom_fd; 25 cdrom_fd=open(cdromfile, O_RDONLY|O_NONBLOCK); 26 #ifdef DEBUG 27 printf("cdrom.c: open_cdrom(\"%s\")=%i\n",cdromfile,cdrom_fd); 28 if (cdrom_fd<0) { printf("cdrom.c: open_cdrom: Couldn't open %s\n",cdromfile); } 29 #endif 30 return cdrom_fd; 31 } 32 33 void cd_eject(int cdrom_fd) { 34 #ifdef DEBUG 35 printf("cdrom.c: cd_eject(cdrom_fd)\n"); 36 #endif 37 cd_stop(cdrom_fd); 38 #ifdef DEBUG 39 printf("cdrom.c: cd_eject: ioctl(cdrom,CDROMEJECT)=%i\n",ioctl(cdrom_fd, CDROMEJECT)); 40 #else 41 ioctl(cdrom_fd, CDROMEJECT); 42 #endif 43 } 44 45 void cd_playtrack(int cdrom_fd, int track) { 46 static struct cdrom_ti trackindex; 47 trackindex.cdti_trk0=track; 48 trackindex.cdti_trk1=track; 49 trackindex.cdti_ind0=0; 50 trackindex.cdti_ind1=0; 51 #ifdef DEBUG 52 printf("cdrom.c: cd_playtrack(cdrom_fd, %i)\n",track); 53 printf("cdrom.c: cd_playtrack: ioctl(cdrom_fd,CDROMSTART)=%i\n",ioctl(cdrom_fd,CDROMSTART)); 54 printf("cdrom.c: cd_playtrack: ioctl(cdrom_fd,CDROMPLAYTRKIND,&trackindex)=%i\n",ioctl(cdrom_fd,CDROMPLAYTRKIND,&trackindex)); 55 #else 56 ioctl(cdrom_fd,CDROMSTART); 57 ioctl(cdrom_fd,CDROMPLAYTRKIND,&trackindex); 58 #endif 59 } 60 61 void cd_play(int cdrom_fd, int starttrack) { 62 static struct cdrom_ti trackindex; 63 trackindex.cdti_trk0=starttrack; 64 trackindex.cdti_ind0=0; 65 trackindex.cdti_ind1=0; 66 #ifdef DEBUG 67 printf("cdrom.c: cd_play(cdrom_fd, %i)\n",starttrack); 68 trackindex.cdti_trk1=cd_num_tracks(cdrom_fd, 0); 69 printf("cdrom.c: cd_play: ioctl(cdrom_fd,CDROMSTART)=%i\n",ioctl(cdrom_fd,CDROMSTART)); 70 perror("cdrom.c: cd_play: perror"); 71 printf("cdrom.c: cd_play: ioctl(cdrom_fd,CDROMPLAYTRKIND,&trackindex)=%i\n",ioctl(cdrom_fd,CDROMPLAYTRKIND,&trackindex)); 72 perror("cdrom.c: cd_play: perror"); 73 #else 74 trackindex.cdti_trk1=cd_num_tracks(cdrom_fd, 0); 75 ioctl(cdrom_fd,CDROMSTART); 76 ioctl(cdrom_fd,CDROMPLAYTRKIND,&trackindex); 77 #endif 78 } 79 80 81 void cd_stop(int cdrom_fd) { 82 #ifdef DEBUG 83 printf("cdrom.c: cd_stop(cdrom_fd)\n"); 84 printf("cdrom.c: cd_stop: ioctl(cdrom_fd,CDROMSTOP)=%i\n",ioctl(cdrom_fd,CDROMSTOP)); 85 #else 86 ioctl(cdrom_fd,CDROMSTOP); 87 #endif 88 } 89 90 91 int cd_num_tracks(int cdrom_fd, int start) { 92 struct cdrom_tochdr toc; 93 #ifdef DEBUG 94 printf("cdrom.c: cd_num_tracks(cdrom_fd, %i)\n", start); 95 printf("cdrom.c: cd_num_tracks: ioctl(cdrom_fd, CDROMREADTOCHDR, &toc)=%i\n", ioctl(cdrom_fd, CDROMREADTOCHDR, &toc)); 96 printf("cdrom.c: cd_num_tracks: toc.cdth_trk0=%i\n",toc.cdth_trk0); 97 printf("cdrom.c: cd_num_tracks: toc.cdth_trk1=%i\n",toc.cdth_trk1); 98 #else 99 ioctl(cdrom_fd, CDROMREADTOCHDR, &toc); 100 #endif 101 if (start==1) return(toc.cdth_trk0); 102 return(toc.cdth_trk1-toc.cdth_trk0+1); 103 } 104 105 106 int cd_isplaying(int cdrom_fd) { 107 struct cdrom_subchnl status; 108 status.cdsc_format=CDROM_MSF; 109 #ifdef DEBUG 110 int ioctl_retval, retval=0; 111 ioctl_retval=ioctl(cdrom_fd,CDROMSUBCHNL,&status); 112 if (status.cdsc_audiostatus==17) retval=1; 113 printf("cdrom.c: cd_isplaying(cdrom_fd)=%i\n",retval); 114 printf("cdrom.c: cd_isplaying: ioctl(cdrom_fd, CDROMSUBCHNL, &status)=%i\n",ioctl_retval); 115 printf("cdrom.c: cd_isplaying: status.cdsc_audiostatus=%i\n",status.cdsc_audiostatus); 116 printf("cdrom.c: cd_isplaying: status.cdsc_ctrl=%i\n",status.cdsc_ctrl); 117 printf("cdrom.c: cd_isplaying: status.cdsc_format=%i\n",status.cdsc_format); 118 printf("cdrom.c: cd_isplaying: status.cdsc_adr=%i\n",status.cdsc_adr); 119 printf("cdrom.c: cd_isplaying: status.cdsc_trk=%i\n",status.cdsc_trk); 120 printf("cdrom.c: cd_isplaying: status.cdsc_ind=%i\n",status.cdsc_ind); 121 printf("cdrom.c: cd_isplaying: status.cdsc_absaddr.msf.minute=%lu\n",(long) status.cdsc_absaddr.msf.minute); 122 printf("cdrom.c: cd_isplaying: status.cdsc_absaddr.msf.second=%lu\n",(long) status.cdsc_absaddr.msf.second); 123 return(retval); 124 #else 125 ioctl(cdrom_fd,CDROMSUBCHNL,&status); 126 if (status.cdsc_audiostatus==17) return(1); 127 return(0); 128 #endif 129 } 130 131 int cd_currenttrack(int cdrom_fd) { 132 struct cdrom_subchnl status; 133 status.cdsc_format=CDROM_MSF; 134 #ifdef DEBUG 135 int retval=0; 136 retval=ioctl(cdrom_fd,CDROMSUBCHNL,&status); 137 printf("cdrom.c: cd_currenttrack(cdrom_fd)=%i\n",status.cdsc_trk); 138 printf("cdrom.c: cd_currenttrack: ioctl(cdrom_fd, CDROMSUBCHNL, &status)=%i\n",retval); 139 printf("cdrom.c: cd_currenttrack: status.cdsc_audiostatus=%i\n",status.cdsc_audiostatus); 140 printf("cdrom.c: cd_currenttrack: status.cdsc_ctrl=%i\n",status.cdsc_ctrl); 141 printf("cdrom.c: cd_currenttrack: status.cdsc_format=%i\n",status.cdsc_format); 142 printf("cdrom.c: cd_currenttrack: status.cdsc_adr=%i\n",status.cdsc_adr); 143 printf("cdrom.c: cd_currenttrack: status.cdsc_trk=%i\n",status.cdsc_trk); 144 printf("cdrom.c: cd_currenttrack: status.cdsc_ind=%i\n",status.cdsc_ind); 145 printf("cdrom.c: cd_currenttrack: status.cdsc_absaddr.msf.minute=%lu\n",(long) status.cdsc_absaddr.msf.minute); 146 printf("cdrom.c: cd_currenttrack: status.cdsc_absaddr.msf.second=%lu\n",(long) status.cdsc_absaddr.msf.second); 147 #else 148 ioctl(cdrom_fd,CDROMSUBCHNL,&status); 149 #endif 150 return(status.cdsc_trk); 151 } 152 153 void cd_setvolume(int cdrom_fd, unsigned char volume) { 154 struct cdrom_volctrl cdvolume; 155 cdvolume.channel0=volume; 156 cdvolume.channel1=volume; 157 cdvolume.channel2=volume; 158 cdvolume.channel3=volume; 159 #ifdef DEBUG 160 printf("cdrom.c: cd_setvolume(cdrom_fd, %i)\n",volume); 161 printf("cdrom.c: cd_setvolume: ioctl(cdrom_fd, CDROMVOLCTRL, &cdvolume)=%i\n", ioctl(cdrom_fd, CDROMVOLCTRL, &cdvolume)); 162 #else 163 ioctl(cdrom_fd, CDROMVOLCTRL, &cdvolume); 164 #endif 165 } 166 167 int cd_getvolume(int cdrom_fd) { 168 struct cdrom_volctrl cdvolume; 169 #ifdef DEBUG 170 int ioctl_retval; 171 ioctl_retval=ioctl(cdrom_fd, CDROMVOLREAD, &cdvolume); 172 printf("cdrom.c: cd_getvolume(cdrom_fd)=%i\n",cdvolume.channel0); 173 printf("cdrom.c: cd_getvolume: ioctl(cdrom_fd, CDROMVOLREAD, &cdvolume)=%i\n",ioctl_retval); 174 printf("cdrom.c: cd_getvolume: cdvolume.channel0=%i\n",cdvolume.channel0); 175 printf("cdrom.c: cd_getvolume: cdvolume.channel1=%i\n",cdvolume.channel1); 176 printf("cdrom.c: cd_getvolume: cdvolume.channel2=%i\n",cdvolume.channel2); 177 printf("cdrom.c: cd_getvolume: cdvolume.channel3=%i\n",cdvolume.channel3); 178 #else 179 ioctl(cdrom_fd, CDROMVOLREAD, &cdvolume); 180 #endif 181 return(cdvolume.channel0); 182 } |