/* rcb.c  --  Read CD-DA Blocks.
 *
 * Send a raw SCSI message via the USCSI facility of the CDROM device driver
 * to read raw (CD-DA audio) frames from a Toshiba XM3401xx drive. Print the
 * binary samples to the standard output. If you want to read CD-DA frames,
 * the drive should be in density 0x82 mode (e.g. via the set_raw program).
 * Note: SunOS can't handle large SCSI transfers, so limit the number of
 * frames you want to read (or do it in a loop).
 *
 * Author: Adrie Koolen (adrie@ica.philips.nl)
 */

#include <stdio.h>
#include <sys/types.h>
#include <scsi/scsi_types.h>
#include <scsi/impl/uscsi.h>

u_char scsibuf[] = {
	0x28,	/* READ */
	0x08,	/* LUN */
	0x00,
	0x00,
	0x00,
	0x00,
	0x00,
	0x00,	/* nr of blocks. */
	0x01,	/* 1 block */
	0x00
};

u_char *databuf;

struct uscsi_cmd usc = {
	(caddr_t) scsibuf,
	sizeof(scsibuf),
	0,
	0,
	0,
	(USCSI_DIAGNOSE | USCSI_ISOLATE | USCSI_READ)
};

char *program;


main(argc, argv)
int argc;
char *argv[];
{
	int fd;
	int len;
	int i;
	int bnr, nrbl;		/* Block Number, Number of Blocks to read. */

	program = argv[0];

	bnr = (argc > 1) ? atoi(argv[1]) : 0;
	scsibuf[3] = (bnr >> 16) & 0xFF;
	scsibuf[4] = (bnr >> 8) & 0xFF;
	scsibuf[5] = bnr & 0xFF;

	nrbl = (argc > 2) ? atoi(argv[2]) : 1;
	scsibuf[7] = nrbl / 256;
	scsibuf[8] = nrbl % 256;

	databuf = (u_char *)malloc(2352 * nrbl);
	if (databuf < 0)
		pfatal("malloc");

	usc.uscsi_bufaddr = (caddr_t) databuf;
	usc.uscsi_buflen = 2352 * nrbl;

	if ((fd = open("/dev/rsr0", 0)) < 0) 
		pfatal("open");

	if (ioctl(fd, USCSICMD, usc) < 0)
		pfatal("ioctl");

	write(1, databuf, usc.uscsi_buflen);
}

pfatal(s)
char *s;
{
	fprintf(stderr, "%s: ", program);
	perror(s);
	fprintf(stderr, "\n");
	exit(1);
}
