1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/* Setcd.c: Set various flags to control the behaviour of your cdrom device.
(c) 1997 David A. van Leeuwen
(c) 2004 Thomas Fritzsche <tf@noto.de>
$Id: setcd.c,v 1.4 1999/08/24 19:07:34 david Exp $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h> /* tolower */
#include <unistd.h> /* lseek, read */
#include <string.h> /* strncmp, memset */
#include <sys/ioctl.h> /* ioctl */
#include <limits.h> /* INT_MAX */
#include <linux/cdrom.h>
#ifndef CDC_CLOSE_TRAY /* moved into cdrom.h... */
#include <linux/ucdrom.h>
#endif
#include <linux/iso_fs.h>
#define REVISION "$Revision: 1.4 $"
void usage(FILE * f, char * s)
{
fprintf(f, "Usage: %s [-hirs] [-ceflot 0|1] [-dx arg] [device] ...\n\n\t"
"-h print this message and copyright\n\t"
"-i print information about cdrom device\n\t"
"-s print status of cdrom device options\n\t"
"-r reset option flags to preferred default\n\t"
"-c * set auto-close flag: close tray if cdrom device is opened\n\t"
"-e * set auto-eject/auto-close behaviour\n\t"
"-f * use file flag O_NONBLOCK in open() call\n\t"
"-l * lock door if cdrom device is opened\n\t"
"-o * set auto_open flag: open tray if cdrom device is released\n\t"
"-t * enable cdrom type checking (audio/data)\n\t"
"-d n select disc number n in jukebox\n\t"
"-x n set speed of cdrom n times 150 KB/s (normal audio)\n", s);
}
#define SHOW(bit, text) if(bit & mask) \
printf(" %-20s %s\n", text ":", (bit & flags) ? "set" : "cleared");
void print_options(int flags, int mask)
{
SHOW(CDO_AUTO_CLOSE, "Auto close tray");
SHOW(CDO_AUTO_EJECT, "Auto open tray");
SHOW(CDO_USE_FFLAGS, "Use O_NONBLOCK flag");
SHOW(CDO_LOCK, "Lock tray");
SHOW(CDO_CHECK_TYPE, "Check CD type");
}
#undef SHOW
void print_audio_info(int fd)
{
struct cdrom_tochdr head;
struct cdrom_tocentry entry;
int last, first;
if (ioctl(fd, CDROMREADTOCHDR, &head)<0) {
perror("Can't read Table of Contents header");
return;
}
last = head.cdth_trk1; first = head.cdth_trk0;
printf(" %d Track%s, %d--%d\n", last-first+1, (last==first) ? "" : "s",
first, last);
entry.cdte_format=CDROM_MSF;
entry.cdte_track = 0xAA;
if (ioctl(fd, CDROMREADTOCENTRY, &entry)<0) {
perror("can't read last track entry");
return;
}
printf(" Total playing time %d minutes and %d seconds\n",
entry.cdte_addr.msf.minute, entry.cdte_addr.msf.second);
}
char * stripped(char * s, int size, int lower)
{
char *i, * end = s+size-1;
while (*end == ' ') --end;
if (end == s+size-1) *end='\0';
else *++end = '\0';
if (lower) for (i=s+1; i<end; i++) *i=tolower(*i);
return s;
}
void print_data_info(int fd)
{
struct iso_primary_descriptor * vdp;
char buf[ISOFS_BLOCK_SIZE];
int sector;
for(sector=16; sector < 100; sector++) {
lseek(fd, sector*ISOFS_BLOCK_SIZE, SEEK_SET);
read(fd, buf, ISOFS_BLOCK_SIZE);
vdp = (struct iso_primary_descriptor *) &buf;
if (!strncmp(vdp->id, ISO_STANDARD_ID, sizeof vdp->id)) {
/* found an iso-cdrom */
printf(" Volume name: %s\n",
stripped(vdp->volume_id, 32, 1));
printf(" Publisher: %s\n",
stripped(vdp->publisher_id, 128, 0));
printf(" Data preparer: %s\n",
stripped(vdp->preparer_id, 128, 0));
break;
}
}
}
void print_info(int fd)
{
int status = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
if (status<0) {
perror("can't read drive status");
fprintf(stderr, "%d\n", status);
exit (-8);
}
switch(status) {
case CDS_NO_INFO:
printf(" No information on drive status is available\n");
break;
case CDS_NO_DISC:
printf(" No disc is inserted\n");
break;
case CDS_TRAY_OPEN:
printf(" CD tray is open\n");
break;
case CDS_DRIVE_NOT_READY:
printf(" Drive is not ready\n");
break;
case CDS_DISC_OK:
printf(" Disc found in drive:");
status=ioctl(fd, CDROM_DISC_STATUS);
if (status < 0) {
fprintf(stderr, "%d\n", status);
break;
}
switch (status) {
#ifdef CDS_MIXED
case CDS_MIXED:
printf(" mixed type CD (data/audio)\n");
print_audio_info(fd);
print_data_info(fd);
break;
#endif
case CDS_AUDIO:
printf(" audio disc\n");
print_audio_info(fd);
break;
case CDS_DATA_1:
case CDS_DATA_2:
printf(" data disc type %d\n", status-CDS_DATA_1+1);
print_data_info(fd);
break;
case CDS_XA_2_1:
case CDS_XA_2_2:
printf(" XA data disc type %d\n", status-CDS_XA_2_1+1);
print_data_info(fd);
break;
default:
printf("Unknown status %d\n", status);
}
break;
default:
printf(" Unknow drive status return value %d\n", status);
}
}
int main(int ac, char ** av)
{
char * device = "/dev/cdrom";
int fd, c;
int status=0, reset=0, info=0, typecheck=-1, speed=-1,
auto_close=-1, auto_open=-1, fflags=-1, lock=-1, disc=-1;
int set=0, clear=0, flags=0;
extern int optind, optopt;
extern char * optarg;
if (ac == 1) {
usage(stderr, av[0]);
exit(-1);
}
while((c=getopt(ac, av, "c:d:e:f:hil:o:rst:x:"))!=EOF) {
switch(c) {
case 'c': auto_close=atoi(optarg); break;
case 'd': disc=atoi(optarg); break;
case 'f': fflags=atoi(optarg); break;
case 'e': auto_close=auto_open=atoi(optarg); break;
case 'i': info=1; break;
case 'l': lock=atoi(optarg); break;
case 'o': auto_open=atoi(optarg); break;
case 'r': reset=1; /* fall into next line */
case 's': status=1; break;
case 't': typecheck=atoi(optarg); break;
case 'x': speed=atoi(optarg); break;
case 'h':
printf("Setcd. Control the behaviour of a cdrom device.\n"
"(c) 1997 David A. van Leeuwen\n"
REVISION "\n\n");
usage(stdout, av[0]);
exit(0);
default:
usage(stderr, av[0]);
exit(-1);
}
}
do {
if (ac > optind) device = av[optind];
fd = open(device, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror("Can't open device");
exit(-2);
}
printf("%s:\n", device);
#define FLAG(i,opt) if (i>=0) { set += opt*(i>0); clear += opt*!i; }
if (reset) {
set = CDO_AUTO_CLOSE | CDO_USE_FFLAGS | CDO_LOCK |
CDO_CHECK_TYPE;
clear = CDO_AUTO_EJECT; /* nasty behaviour */
}
FLAG(auto_close, CDO_AUTO_CLOSE);
FLAG(auto_open, CDO_AUTO_EJECT);
FLAG(fflags, CDO_USE_FFLAGS);
FLAG(lock, CDO_LOCK);
FLAG(typecheck, CDO_CHECK_TYPE);
#undef FLAG
if (set && (flags=ioctl(fd, CDROM_SET_OPTIONS, set))<0) {
perror("can't set options");
exit(-3);
}
if (clear && (flags=ioctl(fd, CDROM_CLEAR_OPTIONS, clear))<0) {
perror("can't clear options");
exit(-4);
}
if (!flags && status && (flags=ioctl(fd, CDROM_SET_OPTIONS, 0))<0) {
perror("can't get options");
exit(-5);
}
if (set || clear || status)
print_options(flags, set | clear | ~0 * status);
if (speed>=0) {
// patch to use SET STREAMING command
struct cdrom_generic_command cgc;
struct request_sense sense;
unsigned char buffer[28];
unsigned long rw_size;
memset(&cgc, 0, sizeof(cgc));
memset(&sense, 0, sizeof(sense));
memset(&buffer, 0, sizeof(buffer));
/* SET STREAMING command */
cgc.cmd[0] = 0xb6;
/* 28 byte parameter list length */
cgc.cmd[10] = 28;
cgc.sense = &sense;
cgc.buffer = buffer;
cgc.buflen = sizeof(buffer);
cgc.data_direction = CGC_DATA_WRITE;
cgc.quiet = 1;
if(speed == 0) {
/* set Restore Drive Defaults */
buffer[0] = 4;
}
buffer[8] = 0xff;
buffer[9] = 0xff;
buffer[10] = 0xff;
buffer[11] = 0xff;
rw_size = 177 * speed;
/* read size */
buffer[12] = (rw_size >> 24) & 0xff;
buffer[13] = (rw_size >> 16) & 0xff;
buffer[14] = (rw_size >> 8) & 0xff;
buffer[15] = rw_size & 0xff;
/* read time 1 sec. */
buffer[18] = 0x03;
buffer[19] = 0xE8;
/* write size */
buffer[20] = (rw_size >> 24) & 0xff;
buffer[21] = (rw_size >> 16) & 0xff;
buffer[22] = (rw_size >> 8) & 0xff;
buffer[23] = rw_size & 0xff;
/* write time 1 sec. */
buffer[26] = 0x03;
buffer[27] = 0xE8;
if ((ioctl(fd, CDROM_SEND_PACKET, &cgc) != 0)
&& (ioctl(fd, CDROM_SELECT_SPEED, speed) < 0)) {
perror("can't set speed");
exit(-6);
} else {
printf(" Speed set at ");
if (!speed) printf("maximum rate / normal audio rate\n");
else printf("%d KB/s (%dx)\n", 150*speed, speed);
}
}
if (disc >= 0) {
if ((disc=ioctl(fd, CDROM_SELECT_DISC, disc)) < 0) {
perror("can't select disc");
exit(-7);
} else
printf(" Disc in slot %d selected\n", disc);
}
if (info) print_info(fd);
close(fd);
} while (ac > ++optind);
return 0;
}
/* It was not easy to find variable `c-file-style' in emacs info. In fact,
I found it in some other .c file.
* Local variables:
* c-file-style: "K&R"
* compile-command: "gcc -Wall setcd.c -o setcd"
* End:
*/
|