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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
|
/*
* $Id: scsi.c,v 1.4 1999/02/12 13:55:19 dirk Exp $
*
* This file is part of WorkMan, the civilized CD player program
* (c) 1991-1997 by Steven Grimm (original author)
* (c) by Dirk F"orsterling (current 'author' = maintainer)
* The maintainer can be contacted by his e-mail address:
* milliByte@DeathsDoor.com
*
* 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.
*
* Frontend functions for sending raw SCSI commands to the CD-ROM drive.
* These depend on wm_scsi(), which should be defined in each platform
* module.
*/
static char scsi_id[] = "$Id: scsi.c,v 1.4 1999/02/12 13:55:19 dirk Exp $";
#include <stdio.h>
#include <stdlib.h>
#include "struct.h"
#include "config.h"
#include "proto.h"
#define SCMD_INQUIRY 0x12
#define SCMD_MODE_SELECT 0x15
#define SCMD_MODE_SENSE 0x1a
#define SCMD_START_STOP 0x1b
#define SCMD_PREVENT 0x1e
#define SCMD_READ_SUBCHANNEL 0x42
#define SCMD_READ_TOC 0x43
#define SCMD_PLAY_AUDIO_MSF 0x47
#define SCMD_PAUSE_RESUME 0x4b
#define SUBQ_STATUS_INVALID 0x00
#define SUBQ_STATUS_PLAY 0x11
#define SUBQ_STATUS_PAUSE 0x12
#define SUBQ_STATUS_DONE 0x13
#define SUBQ_STATUS_ERROR 0x14
#define SUBQ_STATUS_NONE 0x15
#define SUBQ_STATUS_NO_DISC 0x17 /* Illegal, but Toshiba returns it. */
#define SUBQ_ILLEGAL 0xff
#define PAGE_AUDIO 0x0e
#define LEADOUT 0xaa
/*
* Send a SCSI command over the bus, with all the CDB bytes specified
* as unsigned char parameters. This doesn't use varargs because some
* systems have stdargs instead and the number of bytes in a CDB is
* limited to 12 anyway.
*
* d Drive structure
* buf Buffer for data, both sending and receiving
* len Size of buffer
* dir TRUE if the command expects data to be returned in the buffer.
* a0- CDB bytes. Either 6, 10, or 12 of them, depending on the command.
*/
/*VARARGS4*/
int
sendscsi( struct wm_drive *d, void *buf,
unsigned int len, int dir,
unsigned char a0, unsigned char a1,
unsigned char a2, unsigned char a3,
unsigned char a4, unsigned char a5,
unsigned char a6, unsigned char a7,
unsigned char a8, unsigned char a9,
unsigned char a10, unsigned char a11 )
/* void *buf; * Buffer for command results */
/* unsigned int len; * Size of the buffer */
{
int cdblen = 0;
unsigned char cdb[12];
cdb[0] = a0;
cdb[1] = a1;
cdb[2] = a2;
cdb[3] = a3;
cdb[4] = a4;
cdb[5] = a5;
switch ((a0 >> 5) & 7) {
case 0:
cdblen = 6;
break;
case 5:
cdb[10] = a10;
cdb[11] = a11;
cdblen = 12;
case 1:
case 2:
case 6: /* assume 10-byte vendor-specific codes for now */
cdb[6] = a6;
cdb[7] = a7;
cdb[8] = a8;
cdb[9] = a9;
if (! cdblen)
cdblen = 10;
break;
}
return (wm_scsi(d, cdb, cdblen, buf, len, dir));
}
/*
* Send a MODE SENSE command and return the results (minus header cruft)
* in a user buffer.
*
* d Drive structure
* page Number of page to query (plus page control bits, if any)
* buf Result buffer
*/
int
wm_scsi_mode_sense( struct wm_drive *d, unsigned char page, unsigned char *buf )
{
unsigned char pagebuf[255];
int status, i, len, offset;
status = sendscsi(d, pagebuf, sizeof(pagebuf), 1, SCMD_MODE_SENSE, 0,
page, 0, sizeof(pagebuf), 0,0,0,0,0,0,0);
if (status < 0)
return (status);
/*
* The first byte of the returned data is the transfer length. Then
* two more bytes and the length of whatever header blocks are in
* front of the page we want.
*/
len = pagebuf[0] - pagebuf[3] - 3;
offset = pagebuf[3] + 4;
for (i = 0; i < len; i++)
buf[i] = pagebuf[offset + i];
return (0);
}
/*
* Send a MODE SELECT command.
*
* d Drive structure
* buf Page buffer (no need to put on block descriptors)
* len Size of page
*/
int
wm_scsi_mode_select(d, buf, len)
struct wm_drive *d;
unsigned char *buf;
unsigned char len;
{
unsigned char pagebuf[255];
int i;
pagebuf[0] = pagebuf[1] = pagebuf[2] = pagebuf[3] = 0;
for (i = 0; i < (int) len; i++)
pagebuf[i + 4] = buf[i];
return (sendscsi(d, pagebuf, len + 4, 0, SCMD_MODE_SELECT, 0x10, 0,
0, len + 4, 0,0,0,0,0,0,0));
}
/*
* Send an INQUIRY command to get the drive type.
*
* d Drive structure
* vendor Buffer for vendor name (8 bytes + null)
* model Buffer for model name (16 bytes + null)
* rev Buffer for revision level (4 bytes + null)
*
* If the model name begins with "CD-ROM" and zero or more spaces, that will
* all be stripped off since it's just extra junk to WorkMan.
*/
int
wm_scsi_get_drive_type( struct wm_drive *d, unsigned char *vendor,
unsigned char *model, unsigned char *rev )
{
unsigned char *s, *t, buf[36];
if (sendscsi(d, buf, sizeof(buf), 1, SCMD_INQUIRY, 0, 0, 0,
sizeof(buf), 0,0,0,0,0,0,0))
{
sprintf( vendor, "Generic");
sprintf( model, "drive type");
return (-1);
}
memcpy(vendor, buf + 8, 8);
vendor[8] = '\0';
memcpy(model, buf + 16, 16);
model[16] = '\0';
memcpy(rev, buf + 32, 4);
rev[4] = '\0';
/* Remove "CD-ROM " from the model. */
if (! strncmp(model, "CD-ROM", 6))
{
s = model + 6;
t = model;
while (*s == ' ' || *s == '\t')
s++;
while( (*t++ = *s++) )
;
}
#ifdef DEBUG
fprintf(stderr, "|%s|%s|%s|\n", vendor, model, rev);
#endif
return (0);
}
/*
* Send a SCSI-2 PAUSE/RESUME command. "resume" is 1 to resume, 0 to pause.
*/
int
wm_scsi2_pause_resume(d, resume)
struct wm_drive *d;
int resume;
{
return (sendscsi(d, NULL, 0, 0, SCMD_PAUSE_RESUME, 0, 0, 0, 0, 0, 0,
0, resume ? 1 : 0, 0,0,0));
}
/*
* Send a SCSI-2 "prevent media removal" command. "prevent" is 1 to lock
* caddy in.
*/
int
wm_scsi2_prevent(d, prevent)
struct wm_drive *d;
int prevent;
{
return (sendscsi(d, NULL, 0, 0, SCMD_PREVENT, 0, 0, 0, 0, 0, 0,
0, prevent ? 1 : 0, 0,0,0));
}
/*
* Send a SCSI-2 PLAY AUDIO MSF command. Pass the starting and ending
* frame numbers.
*/
int
wm_scsi2_play(d, sframe, eframe)
struct wm_drive *d;
int sframe, eframe;
{
return (sendscsi(d, NULL, 0, 0, SCMD_PLAY_AUDIO_MSF, 0, 0,
sframe / (60 * 75), (sframe / 75) % 60, sframe % 75,
eframe / (60 * 75), (eframe / 75) % 60, eframe % 75,
0,0,0));
}
/*
* Send a SCSI-2 READ TOC command to get the data for a particular track.
* Fill in track information from the returned data.
*/
int
wm_scsi2_get_trackinfo(d, track, data, startframe)
struct wm_drive *d;
int track, *data, *startframe;
{
unsigned char buf[12]; /* one track's worth of info */
if (sendscsi(d, buf, sizeof(buf), 1, SCMD_READ_TOC, 2,
0, 0, 0, 0, track, sizeof(buf) / 256,
sizeof(buf) % 256, 0,0,0))
return (-1);
*data = buf[5] & 4 ? 1 : 0;
*startframe = buf[9] * 60 * 75 + buf[10] * 75 + buf[11];
return (0);
}
/*
* Get the starting frame for the leadout area (which should be the same as
* the length of the disc as far as WorkMan is concerned).
*/
int
wm_scsi2_get_cdlen(d, frames)
struct wm_drive *d;
int *frames;
{
int tmp;
return (wm_scsi2_get_trackinfo(d, LEADOUT, &tmp, frames));
}
/*
* Get the current status of the drive by sending the appropriate SCSI-2
* READ SUB-CHANNEL command.
*/
int
wm_scsi2_get_drive_status(d, oldmode, mode, pos, track, index)
struct wm_drive *d;
enum cd_modes oldmode, *mode;
int *pos, *track, *index;
{
unsigned char buf[48];
/* If we can't get status, the CD is ejected, so default to that. */
*mode = EJECTED;
/* Is the device open? */
if (d->fd < 0)
{
/*
* stupid somehow, but necessary this time
*/
#if defined(SBPCD_HACK) && defined(linux)
switch( wmcd_open( d, 0 ) ) {
#else
switch( wmcd_open( d ) ) {
#endif
case -1: /* error */
return (-1);
case 1: /* retry */
return (0);
}
}
/* If we can't read status, the CD has been ejected. */
buf[1] = SUBQ_ILLEGAL;
if (sendscsi(d, buf, sizeof(buf), 1, SCMD_READ_SUBCHANNEL, 2, 64, 1,
0, 0, 0, sizeof(buf) / 256, sizeof(buf) % 256, 0,0,0))
return (0);
switch (buf[1]) {
case SUBQ_STATUS_PLAY:
*mode = PLAYING;
*track = buf[6];
*index = buf[7];
*pos = buf[9] * 60 * 75 + buf[10] * 75 + buf[11];
break;
case SUBQ_STATUS_PAUSE:
if (oldmode == PLAYING || oldmode == PAUSED)
{
*mode = PAUSED;
*track = buf[6];
*index = buf[7];
*pos = buf[9] * 60 * 75 +
buf[10] * 75 +
buf[11];
}
else
*mode = STOPPED;
break;
/*
* SUBQ_STATUS_DONE is sometimes returned when the CD is idle,
* even though the spec says it should only be returned when an
* audio play operation finishes.
*/
case SUBQ_STATUS_DONE:
case SUBQ_STATUS_NONE:
case SUBQ_STATUS_INVALID:
if (oldmode == PLAYING)
*mode = TRACK_DONE;
else
*mode = STOPPED;
break;
/*
* This usually means there's no disc in the drive.
*/
case SUBQ_STATUS_NO_DISC:
break;
/*
* This usually means the user ejected the CD manually.
*/
case SUBQ_STATUS_ERROR:
break;
case SUBQ_ILLEGAL: /* call didn't really succeed */
break;
default:
*mode = UNKNOWN;
#ifdef DEBUG
if( getenv( "WORKMAN_DEBUG" ) != NULL )
printf("wm_scsi2_get_drive_status: status is 0x%x\n",
buf[1]);
#endif
break;
}
return (0);
}
/*
* Get the number of tracks on the CD using the SCSI-2 READ TOC command.
*/
int
wm_scsi2_get_trackcount(d, tracks)
struct wm_drive *d;
int *tracks;
{
unsigned char buf[4];
if (sendscsi(d, buf, sizeof(buf), 1, SCMD_READ_TOC, 0,
0, 0, 0, 0, 0, sizeof(buf) / 256,
sizeof(buf) % 256, 0,0,0))
return (-1);
*tracks = buf[3] - buf[2] + 1;
return (0);
}
/*
* Pause the CD.
*/
int
wm_scsi2_pause(d)
struct wm_drive *d;
{
return (wm_scsi2_pause_resume(d, 0));
}
/*
* Resume playing after a pause.
*/
int
wm_scsi2_resume(d)
struct wm_drive *d;
{
return (wm_scsi2_pause_resume(d, 1));
}
/*
* Stop playing the CD by sending a START STOP UNIT command.
*/
int
wm_scsi2_stop(d)
struct wm_drive *d;
{
return (sendscsi(d, NULL, 0, 0, SCMD_START_STOP, 0, 0,0,0,0,0,0,0,0,0,0));
}
/*
* Eject the CD by sending a START STOP UNIT command.
*/
int
wm_scsi2_eject(d)
struct wm_drive *d;
{
/* Unlock the disc (possibly unnecessary). */
if (wm_scsi2_prevent(d, 0))
return (-1);
return (sendscsi(d, NULL, 0, 0, SCMD_START_STOP, 2, 0,0,0,0,0,0,0,0,0,0));
}
/*
* Get the volume by doing a MODE SENSE command.
*/
int
wm_scsi2_get_volume(d, left, right)
struct wm_drive *d;
int *left, *right;
{
unsigned char mode[16];
*left = *right = -1;
/* Get the current audio parameters first. */
if (wm_scsi_mode_sense(d, PAGE_AUDIO, mode))
return (-1);
*left = ((int) mode[9] * 100) / 255;
*right = ((int) mode[11] * 100) / 255;
return (0);
}
/*
* Set the volume by doing a MODE SELECT command.
*/
int
wm_scsi2_set_volume(d, left, right)
struct wm_drive *d;
int left, right;
{
unsigned char mode[16];
/* Get the current audio parameters first. */
if (wm_scsi_mode_sense(d, PAGE_AUDIO, mode))
return (-1);
/* Tweak the volume part of the parameters. */
mode[9] = (left * 255) / 100;
mode[11] = (right * 255) / 100;
/* And send them back to the drive. */
return (wm_scsi_mode_select(d, mode, sizeof(mode)));
}
|