diff options
author | peter <peter@FreeBSD.org> | 2000-03-15 21:55:43 +0000 |
---|---|---|
committer | peter <peter@FreeBSD.org> | 2000-03-15 21:55:43 +0000 |
commit | 8a8c3a195af46fa7f5e11b369396c50917395148 (patch) | |
tree | 7bb6f2be2271b49054ad66bb55bb2cff80f77dd5 /sys | |
parent | 56d89f32eb4325dee118d58a464a0f3ff2c6959f (diff) | |
download | FreeBSD-src-8a8c3a195af46fa7f5e11b369396c50917395148.zip FreeBSD-src-8a8c3a195af46fa7f5e11b369396c50917395148.tar.gz |
Remove left over files from wd; Since pc98 still uses some of them those
were copied. Some cleanup is still needed there, ie: unifdef -DPC98 etc.
Diffstat (limited to 'sys')
-rw-r--r-- | sys/i386/isa/atapi.c | 929 | ||||
-rw-r--r-- | sys/i386/isa/atapi.h | 294 | ||||
-rw-r--r-- | sys/i386/isa/wd_cd.c | 1479 | ||||
-rw-r--r-- | sys/i386/isa/wd_cd.h | 349 | ||||
-rw-r--r-- | sys/i386/isa/wfd.c | 727 | ||||
-rw-r--r-- | sys/i386/isa/wst.c | 774 |
6 files changed, 0 insertions, 4552 deletions
diff --git a/sys/i386/isa/atapi.c b/sys/i386/isa/atapi.c deleted file mode 100644 index 8ee0164..0000000 --- a/sys/i386/isa/atapi.c +++ /dev/null @@ -1,929 +0,0 @@ -/* - * Device-independent level for ATAPI drivers. - * - * Copyright (C) 1995 Cronyx Ltd. - * Author Serge Vakulenko, <vak@cronyx.ru> - * - * This software is distributed with NO WARRANTIES, not even the implied - * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * Authors grant any other persons or organisations permission to use - * or modify this software as long as this message is kept with the software, - * all derivative works or modified versions. - * - * Version 1.9, Mon Oct 9 22:34:47 MSK 1995 - * - * $FreeBSD$ - */ - -/* - * The ATAPI level is implemented as a machine-dependent layer - * between the device driver and the IDE controller. - * All the machine- and controller dependency is isolated inside - * the ATAPI level, while all the device dependency is located - * in the device subdriver. - * - * It seems that an ATAPI bus will became popular for medium-speed - * storage devices such as CD-ROMs, magneto-optical disks, tape streamers etc. - * - * To ease the development of new ATAPI drivers, the subdriver - * interface was designed to be as simple as possible. - * - * Three routines are available for the subdriver to access the device: - * - * struct atapires atapi_request_wait (ata, unit, cmd, a1, a2, a3, a4, a5, - * a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, addr, count); - * struct atapi *ata; -- atapi controller descriptor - * int unit; -- device unit number on the IDE bus - * u_char cmd; -- ATAPI command code - * u_char a1..a15; -- ATAPI command arguments - * char *addr; -- address of the data buffer for i/o - * int count; -- data length, >0 for read ops, <0 for write ops - * - * The atapi_request_wait() function puts the op in the queue of ATAPI - * commands for the IDE controller, starts the controller, the waits for - * operation to be completed (using tsleep). - * The function should be called from the user phase only (open(), close(), - * ioctl() etc). - * Ata and unit args are the values which the subdriver gets from the ATAPI - * level via attach() call. - * Buffer pointed to by *addr should be placed in core memory, static - * or dynamic, but not in stack. - * The function returns the error code structure, which consists of: - * - atapi driver code value - * - controller status port value - * - controller error port value - * - * struct atapires atapi_request_immediate (ata, unit, cmd, a1, a2, a3, - * a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, - * addr, count); - * - * The atapi_request_immediate() function is similar to atapi_request_wait(), - * but it does not use interrupts for performing the request. - * It should be used during an attach phase to get parameters from the device. - * - * void atapi_request_callback (ata, unit, cmd, a1, a2, a3, a4, a5, - * a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, - * addr, count, done, x, y); - * struct atapi *ata; -- atapi controller descriptor - * int unit; -- device unit number on the IDE bus - * u_char cmd; -- ATAPI command code - * u_char a1..a15; -- ATAPI command arguments - * char *addr; -- address of the data buffer for i/o - * int count; -- data length, >0 for read ops, <0 for write ops - * void (*done)(); -- function to call when op finished - * void *x, *y; -- arguments for done() function - * - * The atapi_request_callback() function puts the op in the queue of ATAPI - * commands for the IDE controller, starts the controller, then returns. - * When the operation finishes, then the callback function done() - * will be called on the interrupt level. - * The function is designed to be callable from the interrupt phase. - * The done() functions is called with the following arguments: - * (void) (*done) (x, y, count, errcode) - * void *x, *y; -- arguments from the atapi_request_callback() - * int count; -- the data residual count - * struct atapires errcode; -- error code structure, see above - * - * The new driver could be added in three steps: - * 1. Add entries for the new driver to bdevsw and cdevsw tables in conf.c. - * You will need to make at least three routines: open(), close(), - * strategy() and possibly ioctl(). - * 2. Make attach() routine, which should allocate all the needed data - * structures and print the device description string (see xxxattach()). - * 3. Add an appropriate case to the switch in atapi_attach() routine, - * call attach() routine of the new driver here. Add the appropriate - * #include line at the top of attach.c. - * That's all! - * - * Use #define DEBUG in atapi.c to enable tracing of all i/o operations - * on the IDE bus. - */ -#undef DEBUG - -#include "wdc.h" - -#include "wcd.h" -#include "wfd.h" -#include "wst.h" -/* #include "wmd.h" -- add your driver here */ - -#if NWDC > 0 - -#include <sys/param.h> -#include <sys/systm.h> -#include <sys/malloc.h> - -#include <machine/clock.h> - -#include <i386/isa/atapi.h> - -/* this code is compiled part of the module */ - -#ifdef DEBUG -# define print(s) printf s -#else -# define print(s) {/*void*/} -#endif - -/* - * ATAPI packet command phase. - */ -#define PHASE_CMDOUT (ARS_DRQ | ARI_CMD) -#define PHASE_DATAIN (ARS_DRQ | ARI_IN) -#define PHASE_DATAOUT ARS_DRQ -#define PHASE_COMPLETED (ARI_IN | ARI_CMD) -#define PHASE_ABORTED 0 /* nonstandard - for NEC 260 */ - -static struct atapi atapitab[NWDC]; - -static struct atapi_params *atapi_probe (int port, int unit); -static int atapi_wait (int port, u_char bits_wanted); -static void atapi_send_cmd (struct atapi *ata, struct atapicmd *ac); -static int atapi_io (struct atapi *ata, struct atapicmd *ac); -static int atapi_start_cmd (struct atapi *ata, struct atapicmd *ac); -static int atapi_wait_cmd (struct atapi *ata, struct atapicmd *ac); - -extern void wdstart (int ctrlr); -extern int acdattach(struct atapi*, int, struct atapi_params*, int); -extern int wfdattach(struct atapi*, int, struct atapi_params*, int); -extern int wstattach(struct atapi*, int, struct atapi_params*, int); - -/* - * Probe the ATAPI device at IDE controller `ctlr', drive `unit'. - * Called at splbio(). - */ -int atapi_attach (int ctlr, int unit, int port) -{ - struct atapi *ata = atapitab + ctlr; - struct atapi_params *ap; - char buf [sizeof(ap->model) + 1]; - char revbuf [sizeof(ap->revision) + 1]; - struct atapicmd *ac; - - print (("atapi%d.%d at 0x%x: attach called\n", ctlr, unit, port)); - ap = atapi_probe (port, unit); - if (! ap) - return (0); - - bcopy (ap->model, buf, sizeof(buf)-1); - buf[sizeof(buf)-1] = 0; - - bcopy (ap->revision, revbuf, sizeof(revbuf)-1); - revbuf[sizeof(revbuf)-1] = 0; - - printf ("wdc%d: unit %d (atapi): <%s/%s>", ctlr, unit, buf, revbuf); - - /* device is removable */ - if (ap->removable) - printf (", removable"); - - /* packet command size */ - switch (ap->cmdsz) { - case AT_PSIZE_12: break; - case AT_PSIZE_16: printf (", cmd16"); ata->cmd16 = 1; break; - default: printf (", cmd%d", ap->cmdsz); - } - - /* DRQ type */ - switch (ap->drqtype) { - case AT_DRQT_MPROC: ata->slow = 1; break; - case AT_DRQT_INTR: printf (", intr"); ata->intrcmd = 1; break; - case AT_DRQT_ACCEL: printf (", accel"); ata->accel = 1; break; - default: printf (", drq%d", ap->drqtype); - } - if (ata->slow) - ata->intrcmd = 0; - - /* - * If we have two devices, one supporting INTR and one ACCEL, we - * have to pessimise - clear INTR and set slow. - */ - if (ata->accel && ata->intrcmd) { - ata->intrcmd = 0; - ata->slow = 1; - } - - /* overlap operation supported */ - if (ap->ovlapflag) - printf (", ovlap"); - - /* interleaved DMA supported */ - if (ap->idmaflag) - printf (", idma"); - /* DMA supported */ - else if (ap->dmaflag) - printf (", dma"); - - /* IORDY can be disabled */ - if (ap->iordydis) - printf (", iordis"); - /* IORDY supported */ - else if (ap->iordyflag) - printf (", iordy"); - - printf ("\n"); - - ata->port = port; - ata->ctrlr = ctlr; - ata->attached[unit] = 0; -#ifdef DEBUG - ata->debug = 1; -#else - ata->debug = 0; -#endif - /* Initialize free queue. */ - ata->cmdrq[15].next = 0; - for (ac = ata->cmdrq+14; ac >= ata->cmdrq; --ac) - ac->next = ac+1; - ata->free = ata->cmdrq; - - if (ap->proto != AT_PROTO_ATAPI) { - printf ("wdc%d: unit %d: unknown ATAPI protocol=%d\n", - ctlr, unit, ap->proto); - free (ap, M_TEMP); - return (0); - } - switch (ap->devtype) { - default: - /* unknown ATAPI device */ - printf ("wdc%d: unit %d: unknown ATAPI type=%d\n", - ctlr, unit, ap->devtype); - break; - - case AT_TYPE_DIRECT: /* direct-access */ -#if NWFD > 0 - /* ATAPI Floppy(LS-120) */ - if (wfdattach (ata, unit, ap, ata->debug) < 0) - break; - /* Device attached successfully. */ - ata->attached[unit] = 1; - return (1); -#else - printf ("wdc%d: ATAPI Floppies not configured\n", ctlr); - break; -#endif - case AT_TYPE_CDROM: /* CD-ROM device */ -#if NWCD > 0 - /* ATAPI CD-ROM & CD-R/RW drives */ - if (acdattach (ata, unit, ap, ata->debug) < 0) - break; - ata->attached[unit] = 1; - return (1); -#else - printf ("wdc%d: ATAPI CD-ROMs not configured\n", ctlr); - break; -#endif - - case AT_TYPE_TAPE: /* streaming tape */ -#if NWST > 0 - /* ATAPI Streaming Tape */ - if (wstattach (ata, unit, ap, ata->debug) < 0) - break; - /* Device attached successfully. */ - ata->attached[unit] = 1; - return (1); -#else - printf ("wdc%d: ATAPI streaming tapes not configured\n", ctlr); -#endif - break; - - case AT_TYPE_OPTICAL: /* optical disk */ -#if NWMD > 0 - /* Add your driver here */ -#else - printf ("wdc%d: ATAPI optical disks not supported yet\n", ctlr); -#endif - break; - } - /* Attach failed. */ - free (ap, M_TEMP); - return (0); -} - -static char *cmdname (u_char cmd) -{ - static char buf[8]; - - switch (cmd) { - case 0x00: return ("TEST_UNIT_READY"); - case 0x01: return ("REZERO_UNIT"); - case 0x03: return ("REQUEST_SENSE"); - case 0x04: return ("FORMAT_UNIT"); - case 0x1b: return ("START_STOP"); - case 0x1e: return ("PREVENT_ALLOW"); - case 0x25: return ("READ_CAPACITY"); - case 0x28: return ("READ_BIG"); - case 0x2a: return ("WRITE_BIG"); - case 0x35: return ("SYNCHRONIZE_CACHE"); - case 0x42: return ("READ_SUBCHANNEL"); - case 0x43: return ("READ_TOC"); - case 0x51: return ("READ_DISC_INFO"); - case 0x52: return ("READ_TRACK_INFO"); - case 0x53: return ("RESERVE_TRACK"); - case 0x54: return ("SEND_OPC_INFO"); - case 0x55: return ("MODE_SELECT"); - case 0x58: return ("REPAIR_TRACK"); - case 0x59: return ("READ_MASTER_CUE"); - case 0x5a: return ("MODE_SENSE"); - case 0x5b: return ("CLOSE_TRACK/SESSION"); - case 0x5c: return ("READ_BUFFER_CAPACITY"); - case 0x5d: return ("SEND_CUE_SHEET"); - case 0x47: return ("PLAY_MSF"); - case 0x4b: return ("PAUSE"); - case 0x48: return ("PLAY_TRACK"); - case 0xa1: return ("BLANK_CMD"); - case 0xa5: return ("PLAY_BIG"); - case 0xb4: return ("PLAY_CD"); - case 0xbd: return ("ATAPI_MECH_STATUS"); - case 0xbe: return ("READ_CD"); - } - snprintf (buf, sizeof(buf), "[0x%x]", cmd); - return (buf); -} - -static void bswap (char *buf, int len) -{ - u_short *p = (u_short*) (buf + len); - while (--p >= (u_short*) buf) - *p = ntohs (*p); -} - -static void btrim (char *buf, int len) -{ - char *p; - - /* Remove the trailing spaces. */ - for (p=buf; p<buf+len; ++p) - if (! *p) - *p = ' '; - for (p=buf+len-1; p>=buf && *p==' '; --p) - *p = 0; -} - -/* - * Issue IDENTIFY command to ATAPI drive to ask it what it is. - */ -static struct atapi_params *atapi_probe (int port, int unit) -{ - struct atapi_params *ap; - char tb [DEV_BSIZE]; - - /* Wait for controller not busy. */ - outb (port + AR_DRIVE, unit ? ARD_DRIVE1 : ARD_DRIVE0); - if (atapi_wait (port, 0) < 0) { - print (("atapiX.%d at 0x%x: controller busy, status=%b\n", - unit, port, inb (port + AR_STATUS), ARS_BITS)); - return (0); - } - - /* Issue ATAPI IDENTIFY command. */ - outb (port + AR_DRIVE, unit ? ARD_DRIVE1 : ARD_DRIVE0); - outb (port + AR_COMMAND, ATAPIC_IDENTIFY); - - /* Check that device is present. */ - if (inb (port + AR_STATUS) == 0xff) { - print (("atapiX.%d at 0x%x: no device\n", unit, port)); - if (unit == 1) - /* Select unit 0. */ - outb (port + AR_DRIVE, ARD_DRIVE0); - return (0); - } - - /* Wait for data ready. */ - if (atapi_wait (port, ARS_DRQ) != 0) { - print (("atapiX.%d at 0x%x: identify not ready, status=%b\n", - unit, port, inb (port + AR_STATUS), ARS_BITS)); - if (unit == 1) - /* Select unit 0. */ - outb (port + AR_DRIVE, ARD_DRIVE0); - return (0); - } - - /* check that DRQ isn't a fake */ - if (inb (port + AR_STATUS) == 0xff) { - print (("atapiX.%d at 0x%x: no device\n", unit, port)); - if (unit == 1) - /* Select unit 0. */ - outb (port + AR_DRIVE, ARD_DRIVE0); - return (0); - } - - /* Obtain parameters. */ - insw (port + AR_DATA, tb, sizeof(tb) / sizeof(short)); - - ap = malloc (sizeof *ap, M_TEMP, M_NOWAIT); - if (! ap) - return (0); - bcopy (tb, ap, sizeof *ap); - - /* - * Shuffle string byte order. - * Mitsumi and NEC drives don't need this. - */ - if (! ((ap->model[0] == 'N' && ap->model[1] == 'E') || - (ap->model[0] == 'F' && ap->model[1] == 'X'))) - bswap (ap->model, sizeof(ap->model)); - bswap (ap->serial, sizeof(ap->serial)); - bswap (ap->revision, sizeof(ap->revision)); - - /* Clean up the model name, serial and revision numbers. */ - btrim (ap->model, sizeof(ap->model)); - btrim (ap->serial, sizeof(ap->serial)); - btrim (ap->revision, sizeof(ap->revision)); - return (ap); -} - -/* - * Wait uninterruptibly until controller is not busy and certain - * status bits are set. - * The wait is usually short unless it is for the controller to process - * an entire critical command. - * Return 1 for (possibly stale) controller errors, -1 for timeout errors, - * or 0 for no errors. - */ -static int atapi_wait (int port, u_char bits_wanted) -{ - int cnt; - u_char s; - - /* Wait 5 sec for BUSY deassert. */ - for (cnt=500000; cnt>0; --cnt) { - s = inb (port + AR_STATUS); - if (! (s & ARS_BSY)) - break; - DELAY (10); - } - if (cnt <= 0) - return (-1); - if (! bits_wanted) - return (s & ARS_CHECK); - - /* Wait 50 msec for bits wanted. */ - for (cnt=5000; cnt>0; --cnt) { - s = inb (port + AR_STATUS); - if ((s & bits_wanted) == bits_wanted) - return (s & ARS_CHECK); - DELAY (10); - } - return (-1); -} - -void atapi_debug (struct atapi *ata, int on) -{ - ata->debug = on; -} - -static struct atapicmd *atapi_alloc (struct atapi *ata) -{ - struct atapicmd *ac; - - while (! ata->free) - tsleep ((caddr_t)ata, PRIBIO, "atacmd", 100); - ac = ata->free; - ata->free = ac->next; - ac->busy = 1; - return (ac); -} - -static void atapi_free (struct atapi *ata, struct atapicmd *ac) -{ - if (! ata->free) - wakeup ((caddr_t)ata); - ac->busy = 0; - ac->next = ata->free; - ata->free = ac; -} - -/* - * Add new command request to the end of the queue. - */ -static void atapi_enqueue (struct atapi *ata, struct atapicmd *ac) -{ - ac->next = 0; - if (ata->tail) - ata->tail->next = ac; - else - ata->queue = ac; - ata->tail = ac; -} - -static void atapi_done (struct atapi *ata) -{ - struct atapicmd *ac = ata->queue; - - if (! ac) - return; /* cannot happen */ - - ata->queue = ac->next; - if (! ata->queue) - ata->tail = 0; - - if (ac->callback) { - (*ac->callback) (ac->cbarg1, ac->cbarg2, ac->count, ac->result); - atapi_free (ata, ac); - } else - wakeup ((caddr_t)ac); -} - -/* - * Start new packet op. Called from wdstart(). - * Return 1 if op started, and we are waiting for interrupt. - * Return 0 when idle. - */ -int atapi_start (int ctrlr) -{ - struct atapi *ata = atapitab + ctrlr; - struct atapicmd *ac; -again: - ac = ata->queue; - if (! ac) - return (0); - - /* Start packet command. */ - if (atapi_start_cmd (ata, ac) < 0) { - atapi_done (ata); - goto again; - } - - if (ata->intrcmd) - /* Wait for interrupt before sending packet command */ - return (1); - - /* Wait for DRQ. */ - if (atapi_wait_cmd (ata, ac) < 0) { - atapi_done (ata); - goto again; - } - - /* Send packet command. */ - atapi_send_cmd (ata, ac); - return (1); -} - -/* - * Start new packet op. Returns -1 on errors. - */ -int atapi_start_cmd (struct atapi *ata, struct atapicmd *ac) -{ - ac->result.error = 0; - ac->result.status = 0; - - outb (ata->port + AR_DRIVE, ac->unit ? ARD_DRIVE1 : ARD_DRIVE0); - if (atapi_wait (ata->port, 0) < 0) { - printf ("atapi%d.%d: controller not ready for cmd\n", - ata->ctrlr, ac->unit); - ac->result.code = RES_NOTRDY; - return (-1); - } - - /* Set up the controller registers. */ - outb (ata->port + AR_FEATURES, 0); - outb (ata->port + AR_IREASON, 0); - outb (ata->port + AR_TAG, 0); - outb (ata->port + AR_CNTLO, ac->count & 0xff); - outb (ata->port + AR_CNTHI, ac->count >> 8); - outb (ata->port + AR_COMMAND, ATAPIC_PACKET); - - if (ata->debug) - printf ("atapi%d.%d: start\n", ata->ctrlr, ac->unit); - return (0); -} - -/* - * Wait for DRQ before sending packet cmd. Returns -1 on errors. - */ -int atapi_wait_cmd (struct atapi *ata, struct atapicmd *ac) -{ - /* Wait for DRQ from 100 usec to 3 msec for slow devices */ - int cnt = ata->intrcmd ? 10000 : ata->slow ? 3000 : 100; - int ireason = 0, phase = 0; - - /* Wait for command phase. */ - for (; cnt>0; cnt-=10) { - ireason = inb (ata->port + AR_IREASON); - ac->result.status = inb (ata->port + AR_STATUS); - phase = (ireason & (ARI_CMD | ARI_IN)) | - (ac->result.status & (ARS_DRQ | ARS_BSY)); - if (phase == PHASE_CMDOUT) - break; - DELAY (10); - } - - if (phase != PHASE_CMDOUT) { - ac->result.code = RES_NODRQ; - ac->result.error = inb (ata->port + AR_ERROR); - printf ("atapi%d.%d: invalid command phase, ireason=0x%x, status=%b, error=%b\n", - ata->ctrlr, ac->unit, ireason, - ac->result.status, ARS_BITS, - ac->result.error, AER_BITS); - return (-1); - } - return (0); -} - -/* - * Send packet cmd. - */ -void atapi_send_cmd (struct atapi *ata, struct atapicmd *ac) -{ - outsw (ata->port + AR_DATA, ac->cmd, ata->cmd16 ? 8 : 6); - if (ata->debug) - printf ("atapi%d.%d: send cmd %s %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x\n", - ata->ctrlr, ac->unit, cmdname (ac->cmd[0]), ac->cmd[0], - ac->cmd[1], ac->cmd[2], ac->cmd[3], ac->cmd[4], - ac->cmd[5], ac->cmd[6], ac->cmd[7], ac->cmd[8], - ac->cmd[9], ac->cmd[10], ac->cmd[11], ac->cmd[12], - ac->cmd[13], ac->cmd[14], ac->cmd[15]); -} - -/* - * Interrupt routine for the controller. Called from wdintr(). - * Finish the started op, wakeup wait-type commands, - * run callbacks for callback-type commands, then return. - * Do not start new op here, it will be done by wdstart, - * which is called just after us. - * Return 1 if op continues, and we are waiting for new interrupt. - * Return 0 when idle. - */ -int atapi_intr (int ctrlr) -{ - struct atapi *ata = atapitab + ctrlr; - struct atapicmd *ac = ata->queue; - - if (! ac) { - printf ("atapi%d: stray interrupt\n", ata->ctrlr); - return (0); - } - if (atapi_io (ata, ac) > 0) - return (1); - atapi_done (ata); - return (0); -} - -/* - * Process the i/o phase, transferring the command/data to/from the device. - * Return 1 if op continues, and we are waiting for new interrupt. - * Return 0 when idle. - */ -int atapi_io (struct atapi *ata, struct atapicmd *ac) -{ - u_char ireason; - u_short len, i; - - if (atapi_wait (ata->port, 0) < 0) { - ac->result.status = inb (ata->port + AR_STATUS); - ac->result.error = inb (ata->port + AR_ERROR); - ac->result.code = RES_NOTRDY; - printf ("atapi%d.%d: controller not ready, status=%b, error=%b\n", - ata->ctrlr, ac->unit, ac->result.status, ARS_BITS, - ac->result.error, AER_BITS); - return (0); - } - - ac->result.status = inb (ata->port + AR_STATUS); - ac->result.error = inb (ata->port + AR_ERROR); - len = inb (ata->port + AR_CNTLO); - len |= inb (ata->port + AR_CNTHI) << 8; - ireason = inb (ata->port + AR_IREASON); - - if (ata->debug) { - printf ("atapi%d.%d: intr ireason=0x%x, len=%d, status=%b, error=%b\n", - ata->ctrlr, ac->unit, ireason, len, - ac->result.status, ARS_BITS, - ac->result.error, AER_BITS); - } - switch ((ireason & (ARI_CMD | ARI_IN)) | (ac->result.status & ARS_DRQ)) { - default: - printf ("atapi%d.%d: unknown phase\n", ata->ctrlr, ac->unit); - ac->result.code = RES_ERR; - break; - - case PHASE_CMDOUT: - /* Send packet command. */ - if (! (ac->result.status & ARS_DRQ)) { - printf ("atapi%d.%d: no cmd drq\n", - ata->ctrlr, ac->unit); - ac->result.code = RES_NODRQ; - break; - } - atapi_send_cmd (ata, ac); - return (1); - - case PHASE_DATAOUT: - /* Write data */ - if (ac->count > 0) { - printf ("atapi%d.%d: invalid data direction\n", - ata->ctrlr, ac->unit); - ac->result.code = RES_INVDIR; - break; - } - if (-ac->count < len) { - print (("atapi%d.%d: send data underrun, %d bytes left\n", - ata->ctrlr, ac->unit, -ac->count)); - ac->result.code = RES_UNDERRUN; - outsw (ata->port + AR_DATA, ac->addr, - -ac->count / sizeof(short)); - for (i= -ac->count; i<len; i+=sizeof(short)) - outw (ata->port + AR_DATA, 0); - } else - outsw (ata->port + AR_DATA, ac->addr, - len / sizeof(short)); - ac->addr += len; - ac->count += len; - return (1); - - case PHASE_DATAIN: - /* Read data */ - if (ac->count < 0) { - printf ("atapi%d.%d: invalid data direction\n", - ata->ctrlr, ac->unit); - ac->result.code = RES_INVDIR; - break; - } - if (ac->count < len) { - print (("atapi%d.%d: recv data overrun, %d bytes left\n", - ata->ctrlr, ac->unit, ac->count)); - ac->result.code = RES_OVERRUN; - insw (ata->port + AR_DATA, ac->addr, - ac->count / sizeof(short)); - for (i=ac->count; i<len; i+=sizeof(short)) - inw (ata->port + AR_DATA); - } else - insw (ata->port + AR_DATA, ac->addr, - len / sizeof(short)); - ac->addr += len; - ac->count -= len; - return (1); - - case PHASE_ABORTED: - case PHASE_COMPLETED: - if (ac->result.status & (ARS_CHECK | ARS_DF)) - ac->result.code = RES_ERR; - else if (ac->count < 0) { - print (("atapi%d.%d: send data overrun, %d bytes left\n", - ata->ctrlr, ac->unit, -ac->count)); - ac->result.code = RES_OVERRUN; - } else if (ac->count > 0) { - print (("atapi%d.%d: recv data underrun, %d bytes left\n", - ata->ctrlr, ac->unit, ac->count)); - ac->result.code = RES_UNDERRUN; - bzero (ac->addr, ac->count); - } else - ac->result.code = RES_OK; - break; - } - return (0); -} - -/* - * Queue new packet request, then call wdstart(). - * Called on splbio(). - */ -void atapi_request_callback (struct atapi *ata, int unit, - u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4, - u_char a5, u_char a6, u_char a7, u_char a8, u_char a9, - u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15, - char *addr, int count, atapi_callback_t *done, void *x, void *y) -{ - struct atapicmd *ac; - - ac = atapi_alloc (ata); - ac->cmd[0] = cmd; ac->cmd[1] = a1; - ac->cmd[2] = a2; ac->cmd[3] = a3; - ac->cmd[4] = a4; ac->cmd[5] = a5; - ac->cmd[6] = a6; ac->cmd[7] = a7; - ac->cmd[8] = a8; ac->cmd[9] = a9; - ac->cmd[10] = a10; ac->cmd[11] = a11; - ac->cmd[12] = a12; ac->cmd[13] = a13; - ac->cmd[14] = a14; ac->cmd[15] = a15; - ac->unit = unit; - ac->addr = addr; - ac->count = count; - ac->callback = done; - ac->cbarg1 = x; - ac->cbarg2 = y; - - if (ata->debug) - printf ("atapi%d.%d: req cb %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x len=%d\n", - ata->ctrlr, ac->unit, ac->cmd[0], ac->cmd[1], - ac->cmd[2], ac->cmd[3], ac->cmd[4], ac->cmd[5], - ac->cmd[6], ac->cmd[7], ac->cmd[8], ac->cmd[9], - ac->cmd[10], ac->cmd[11], ac->cmd[12], - ac->cmd[13], ac->cmd[14], ac->cmd[15], count); - atapi_enqueue (ata, ac); - wdstart (ata->ctrlr); -} - -/* - * Queue new packet request, then call wdstart(). - * Wait until the request is finished. - * Called on spl0(). - * Return atapi error. - * Buffer pointed to by *addr should be placed in core memory, not in stack! - */ -struct atapires atapi_request_wait (struct atapi *ata, int unit, - u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4, - u_char a5, u_char a6, u_char a7, u_char a8, u_char a9, - u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15, - char *addr, int count) -{ - struct atapicmd *ac; - int x = splbio (); - struct atapires result; - - ac = atapi_alloc (ata); - ac->cmd[0] = cmd; ac->cmd[1] = a1; - ac->cmd[2] = a2; ac->cmd[3] = a3; - ac->cmd[4] = a4; ac->cmd[5] = a5; - ac->cmd[6] = a6; ac->cmd[7] = a7; - ac->cmd[8] = a8; ac->cmd[9] = a9; - ac->cmd[10] = a10; ac->cmd[11] = a11; - ac->cmd[12] = a12; ac->cmd[13] = a13; - ac->cmd[14] = a14; ac->cmd[15] = a15; - ac->unit = unit; - ac->addr = addr; - ac->count = count; - ac->callback = 0; - ac->cbarg1 = 0; - ac->cbarg2 = 0; - - if (ata->debug) - printf ("atapi%d.%d: req w %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x len=%d\n", - ata->ctrlr, ac->unit, ac->cmd[0], ac->cmd[1], - ac->cmd[2], ac->cmd[3], ac->cmd[4], ac->cmd[5], - ac->cmd[6], ac->cmd[7], ac->cmd[8], ac->cmd[9], - ac->cmd[10], ac->cmd[11], ac->cmd[12], - ac->cmd[13], ac->cmd[14], ac->cmd[15], count); - atapi_enqueue (ata, ac); - wdstart (ata->ctrlr); - if (ata->tail == ac) - tsleep ((caddr_t)ac, PRIBIO, "atareq", 0); - - result = ac->result; - atapi_free (ata, ac); - splx (x); - return (result); -} - -/* - * Perform a packet command on the device. - * Should be called on splbio(). - * Return atapi error. - */ -struct atapires atapi_request_immediate (struct atapi *ata, int unit, - u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4, - u_char a5, u_char a6, u_char a7, u_char a8, u_char a9, - u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15, - char *addr, int count) -{ - struct atapicmd cmdbuf, *ac = &cmdbuf; - int cnt; - - ac->cmd[0] = cmd; ac->cmd[1] = a1; - ac->cmd[2] = a2; ac->cmd[3] = a3; - ac->cmd[4] = a4; ac->cmd[5] = a5; - ac->cmd[6] = a6; ac->cmd[7] = a7; - ac->cmd[8] = a8; ac->cmd[9] = a9; - ac->cmd[10] = a10; ac->cmd[11] = a11; - ac->cmd[12] = a12; ac->cmd[13] = a13; - ac->cmd[14] = a14; ac->cmd[15] = a15; - ac->unit = unit; - ac->addr = addr; - ac->count = count; - ac->callback = 0; - ac->cbarg1 = 0; - ac->cbarg2 = 0; - - if (ata->debug) - printf ("atapi%d.%d: req im %x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x-%x len=%d\n", - ata->ctrlr, ac->unit, ac->cmd[0], ac->cmd[1], - ac->cmd[2], ac->cmd[3], ac->cmd[4], ac->cmd[5], - ac->cmd[6], ac->cmd[7], ac->cmd[8], ac->cmd[9], - ac->cmd[10], ac->cmd[11], ac->cmd[12], - ac->cmd[13], ac->cmd[14], ac->cmd[15], count); - - /* Start packet command, wait for DRQ. */ - if (atapi_start_cmd (ata, ac) >= 0 && atapi_wait_cmd (ata, ac) >= 0) { - /* Send packet command. */ - atapi_send_cmd (ata, ac); - - /* Wait for data i/o phase. */ - for (cnt=20000; cnt>0; --cnt) - if (((inb (ata->port + AR_IREASON) & (ARI_CMD | ARI_IN)) | - (inb (ata->port + AR_STATUS) & ARS_DRQ)) != PHASE_CMDOUT) - break; - - /* Do all needed i/o. */ - while (atapi_io (ata, ac)) - /* Wait for DRQ deassert. */ - for (cnt=2000; cnt>0; --cnt) { - if (! (inb (ata->port + AR_STATUS) & ARS_DRQ)) - break; - DELAY(10); - } - } - return (ac->result); -} - -#endif /* NWDC */ diff --git a/sys/i386/isa/atapi.h b/sys/i386/isa/atapi.h deleted file mode 100644 index 66989a4..0000000 --- a/sys/i386/isa/atapi.h +++ /dev/null @@ -1,294 +0,0 @@ -/* - * Device-independent level for ATAPI drivers. - * - * Copyright (C) 1995 Cronyx Ltd. - * Author Serge Vakulenko, <vak@cronyx.ru> - * - * This software is distributed with NO WARRANTIES, not even the implied - * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * - * Authors grant any other persons or organizations permission to use - * or modify this software as long as this message is kept with the software, - * all derivative works or modified versions. - * - * Version 1.9, Thu Oct 12 15:53:50 MSK 1995 - * $FreeBSD$ - */ - -#define atapi_attach wdc_atapi_attach -#define acdattach wdc_acdattach - -/* - * Disk Controller ATAPI register definitions. - */ -#ifdef PC98 -#define AR_DATA 0x0 /* RW - data register (16 bits) */ -#define AR_ERROR 0x2 /* R - error register */ -#define AR_FEATURES 0x2 /* W - features */ -#define AR_IREASON 0x4 /* RW - interrupt reason */ -#define AR_TAG 0x6 /* - reserved for SAM TAG byte */ -#define AR_CNTLO 0x8 /* RW - byte count, low byte */ -#define AR_CNTHI 0xa /* RW - byte count, high byte */ -#define AR_DRIVE 0xc /* RW - drive select */ -#define AR_COMMAND 0xe /* W - command register */ -#define AR_STATUS 0xe /* R - immediate status */ -#else -#define AR_DATA 0x0 /* RW - data register (16 bits) */ -#define AR_ERROR 0x1 /* R - error register */ -#define AR_FEATURES 0x1 /* W - features */ -#define AR_IREASON 0x2 /* RW - interrupt reason */ -#define AR_TAG 0x3 /* - reserved for SAM TAG byte */ -#define AR_CNTLO 0x4 /* RW - byte count, low byte */ -#define AR_CNTHI 0x5 /* RW - byte count, high byte */ -#define AR_DRIVE 0x6 /* RW - drive select */ -#define AR_COMMAND 0x7 /* W - command register */ -#define AR_STATUS 0x7 /* R - immediate status */ -#endif - -/* - * Status register bits - */ -#define ARS_CHECK 0x01 /* error occured, see sense key/code */ - /* bit 0x02 reserved */ -#define ARS_CORR 0x04 /* correctable error occured */ -#define ARS_DRQ 0x08 /* data request / ireason valid */ -#define ARS_DSC 0x10 /* immediate operation completed */ -#define ARS_DF 0x20 /* drive fault */ -#define ARS_DRDY 0x40 /* ready to get command */ -#define ARS_BSY 0x80 /* registers busy */ - /* for overlap mode only: */ -#define ARS_SERVICE 0x10 /* service is requested */ -#define ARS_DMARDY 0x20 /* ready to start a DMA transfer */ -#define ARS_BITS "\20\010busy\7ready\6fault\5opdone\4drq\3corr\1check" - -/* - * Error register bits - */ -#define AER_ILI 0x01 /* illegal length indication */ -#define AER_EOM 0x02 /* end of media detected */ -#define AER_ABRT 0x04 /* command aborted */ -#define AER_MCR 0x08 /* media change requested */ -#define AER_SKEY 0xf0 /* sense key mask */ -#define AER_SK_NO_SENSE 0x00 /* no specific sense key info */ -#define AER_SK_RECOVERED_ERROR 0x10 /* command succeeded, data recovered */ -#define AER_SK_NOT_READY 0x20 /* no access to drive */ -#define AER_SK_MEDIUM_ERROR 0x30 /* non-recovered data error */ -#define AER_SK_HARDWARE_ERROR 0x40 /* non-recoverable hardware failure */ -#define AER_SK_ILLEGAL_REQUEST 0x50 /* invalid command parameter(s) */ -#define AER_SK_UNIT_ATTENTION 0x60 /* media changed */ -#define AER_SK_DATA_PROTECT 0x70 /* reading read-protected sector */ -#define AER_SK_BLANK_CHECK 0x80 /* blank check */ -#define AER_SK_VENDOR_SPECIFIC 0x90 /* vendor specific skey */ -#define AER_SK_COPY_ABORTED 0xa0 /* copy aborted */ -#define AER_SK_ABORTED_COMMAND 0xb0 /* command aborted, try again */ -#define AER_SK_EQUAL 0xc0 /* equal */ -#define AER_SK_VOLUME_OVERFLOW 0xd0 /* volume overflow */ -#define AER_SK_MISCOMPARE 0xe0 /* data did not match the medium */ -#define AER_SK_RESERVED 0xf0 -#define AER_BITS "\20\4mchg\3abort\2eom\1ili" - -/* - * Feature register bits - */ -#define ARF_DMA 0x01 /* transfer data via DMA */ -#define ARF_OVERLAP 0x02 /* release the bus until completion */ - -/* - * Interrupt reason register bits - */ -#define ARI_CMD 0x01 /* command(1) or data(0) */ -#define ARI_IN 0x02 /* transfer to(1) or from(0) the host */ -#define ARI_RELEASE 0x04 /* bus released until completion */ - -/* - * Drive register values - */ -#define ARD_DRIVE0 0xa0 /* drive 0 selected */ -#define ARD_DRIVE1 0xb0 /* drive 1 selected */ - -/* - * ATA commands - */ -#define ATAPIC_IDENTIFY 0xa1 /* get drive parameters */ -#define ATAPIC_PACKET 0xa0 /* execute packet command */ - -/* - * Mandatory packet commands - */ -#define ATAPI_TEST_UNIT_READY 0x00 /* check if the device is ready */ -#define ATAPI_REZERO_UNIT 0x01 /* reinit device */ -#define ATAPI_REQUEST_SENSE 0x03 /* get sense data */ -#define ATAPI_START_STOP 0x1b /* start/stop the media */ -#define ATAPI_PREVENT_ALLOW 0x1e /* prevent/allow media removal */ -#define ATAPI_READ_CAPACITY 0x25 /* get volume capacity */ -#define ATAPI_READ_BIG 0x28 /* read data */ -#define ATAPI_WRITE_BIG 0x2a /* write data */ -#define ATAPI_SYNCHRONIZE_CACHE 0x35 /* flush write buf, close write chan */ -#define ATAPI_READ_SUBCHANNEL 0x42 /* get subchannel info */ -#define ATAPI_READ_TOC 0x43 /* get table of contents */ -#define ATAPI_READ_TRACK_INFO 0x52 /* get track information structure */ -#define ATAPI_MODE_SELECT 0x55 /* set device parameters */ -#define ATAPI_MODE_SENSE 0x5a /* get device parameters */ -#define ATAPI_CLOSE_TRACK 0x5b /* close track/session */ -#define ATAPI_LOAD_UNLOAD 0xa6 /* changer control command */ -#define ATAPI_PLAY_CD 0xb4 /* universal play command */ -#define ATAPI_MECH_STATUS 0xbd /* get changer mechanism status */ -#define ATAPI_READ_CD 0xbe /* read data */ -/* - * Optional packet commands - */ -#define ATAPI_PLAY_MSF 0x47 /* play by MSF address */ -#define ATAPI_PAUSE 0x4b /* stop/start audio operation */ - -/* - * Nonstandard packet commands - */ -#define ATAPI_PLAY_TRACK 0x48 /* play by track number */ -#define ATAPI_PLAY_BIG 0xa5 /* play by logical block address */ - -#define DSC_POLL_INTERVAL 10 - -/* - * Drive parameter information - */ -struct atapi_params { - unsigned cmdsz : 2; /* packet command size */ -#define AT_PSIZE_12 0 /* 12 bytes */ -#define AT_PSIZE_16 1 /* 16 bytes */ - unsigned : 3; - unsigned drqtype : 2; /* DRQ type */ -#define AT_DRQT_MPROC 0 /* microprocessor DRQ - 3 msec delay */ -#define AT_DRQT_INTR 1 /* interrupt DRQ - 10 msec delay */ -#define AT_DRQT_ACCEL 2 /* accelerated DRQ - 50 usec delay */ - unsigned removable : 1; /* device is removable */ - unsigned devtype : 5; /* device type */ -#define AT_TYPE_DIRECT 0 /* direct-access (magnetic disk) */ -#define AT_TYPE_TAPE 1 /* streaming tape (QIC-121 model) */ -#define AT_TYPE_CDROM 5 /* CD-ROM device */ -#define AT_TYPE_OPTICAL 7 /* optical disk */ - unsigned : 1; - unsigned proto : 2; /* command protocol */ -#define AT_PROTO_ATAPI 2 - short reserved1[9]; - char serial[20]; /* serial number - optional */ - short reserved2[3]; - char revision[8]; /* firmware revision */ - char model[40]; /* model name */ - short reserved3[2]; - u_char vendor_cap; /* vendor unique capabilities */ - unsigned dmaflag : 1; /* DMA supported */ - unsigned lbaflag : 1; /* LBA supported - always 1 */ - unsigned iordydis : 1; /* IORDY can be disabled */ - unsigned iordyflag : 1; /* IORDY supported */ - unsigned : 1; - unsigned ovlapflag : 1; /* overlap operation supported */ - unsigned : 1; - unsigned idmaflag : 1; /* interleaved DMA supported */ - short reserved4; - u_short pio_timing; /* PIO cycle timing */ - u_short dma_timing; /* DMA cycle timing */ - u_short flags; -#define AT_FLAG_54_58 1 /* words 54-58 valid */ -#define AT_FLAG_64_70 2 /* words 64-70 valid */ - short reserved5[8]; - u_char swdma_flag; /* singleword DMA mode supported */ - u_char swdma_active; /* singleword DMA mode active */ - u_char mwdma_flag; /* multiword DMA mode supported */ - u_char mwdma_active; /* multiword DMA mode active */ - u_char apio_flag; /* advanced PIO mode supported */ - u_char reserved6; - u_short mwdma_min; /* min. M/W DMA time per word (ns) */ - u_short mwdma_dflt; /* recommended M/W DMA time (ns) - optional */ - u_short pio_nfctl_min; /* min. PIO cycle time w/o flow ctl - optional */ - u_short pio_iordy_min; /* min. PIO c/t with IORDY flow ctl - optional */ - short reserved7[2]; - u_short rls_ovlap; /* release time (us) for overlap cmd - optional */ - u_short rls_service; /* release time (us) for service cmd - optional */ -}; - -/* - * ATAPI operation result structure - */ -struct atapires { - u_char code; /* result code */ -#define RES_OK 0 /* i/o done */ -#define RES_ERR 1 /* i/o finished with error */ -#define RES_NOTRDY 2 /* controller not ready */ -#define RES_NODRQ 3 /* no data request */ -#define RES_INVDIR 4 /* invalid bus phase direction */ -#define RES_OVERRUN 5 /* data overrun */ -#define RES_UNDERRUN 6 /* data underrun */ - u_char status; /* status register contents */ - u_char error; /* error register contents */ -}; - -struct atapidrv { /* delayed attach info */ - int ctlr; /* IDE controller, 0/1 */ - int unit; /* drive unit, 0/1 */ - int port; /* controller base port */ - int attached; /* the drive is attached */ -}; - -struct buf; -struct dmy; -typedef void atapi_callback_t(struct dmy *, struct buf *, int, struct atapires); - -struct atapicmd { /* ATAPI command block */ - struct atapicmd *next; /* next command in queue */ - int busy; /* busy flag */ - u_char cmd[16]; /* command and args */ - int unit; /* drive unit number */ - int count; /* byte count, >0 - read, <0 - write */ - char *addr; /* data to transfer */ - atapi_callback_t *callback; /* call when done */ - void *cbarg1; /* callback arg 1 */ - void *cbarg2; /* callback arg 1 */ - struct atapires result; /* resulting error code */ -}; - -struct atapi { /* ATAPI controller data */ - u_short port; /* i/o port base */ - u_char ctrlr; /* physical controller number */ - u_char debug : 1; /* trace enable flag */ - u_char cmd16 : 1; /* 16-byte command flag */ - u_char intrcmd : 1; /* interrupt before cmd flag */ - u_char slow : 1; /* slow reaction device */ - u_char accel : 1; /* accelerated reaction device */ - u_char use_dsc : 1; /* use DSC completition handeling */ - u_char wait_for_dsc : 1; - u_int dsc_timeout; - u_char attached[2]; /* units are attached to subdrivers */ - struct atapi_params *params[2]; /* params for units 0,1 */ - struct atapicmd *queue; /* queue of commands to perform */ - struct atapicmd *tail; /* tail of queue */ - struct atapicmd *free; /* queue of free command blocks */ - struct atapicmd cmdrq[16]; /* pool of command requests */ -}; - -#ifdef _KERNEL -struct atapi; - -extern struct atapidrv atapi_drvtab[4]; /* delayed attach info */ -extern int atapi_ndrv; /* the number of potential drives */ -extern struct atapi *atapi_tab; /* the table of atapi controllers */ - -int atapi_attach (int ctlr, int unit, int port); -int atapi_start (int ctrlr); -int atapi_intr (int ctrlr); -void atapi_debug (struct atapi *ata, int on); -struct atapires atapi_request_wait (struct atapi *ata, int unit, - u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4, - u_char a5, u_char a6, u_char a7, u_char a8, u_char a9, - u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15, - char *addr, int count); -void atapi_request_callback (struct atapi *ata, int unit, - u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4, - u_char a5, u_char a6, u_char a7, u_char a8, u_char a9, - u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15, - char *addr, int count, atapi_callback_t *done, void *x, void *y); -struct atapires atapi_request_immediate (struct atapi *ata, int unit, - u_char cmd, u_char a1, u_char a2, u_char a3, u_char a4, - u_char a5, u_char a6, u_char a7, u_char a8, u_char a9, - u_char a10, u_char a11, u_char a12, u_char a13, u_char a14, u_char a15, - char *addr, int count); -#endif diff --git a/sys/i386/isa/wd_cd.c b/sys/i386/isa/wd_cd.c deleted file mode 100644 index d8f5aa3..0000000 --- a/sys/i386/isa/wd_cd.c +++ /dev/null @@ -1,1479 +0,0 @@ -/*- - * Copyright (c) 1998, 1999 Søren Schmidt - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer, - * without modification, immediately at the beginning of the file. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include <sys/param.h> -#include <sys/systm.h> -#include <sys/kernel.h> -#include <sys/proc.h> -#include <sys/malloc.h> -#include <sys/buf.h> -#include <sys/disklabel.h> -#include <sys/devicestat.h> -#include <sys/cdio.h> -#include <sys/wormio.h> -#include <sys/fcntl.h> -#include <sys/conf.h> -#include <sys/stat.h> -#include <i386/isa/atapi.h> -#include <i386/isa/wd_cd.h> - -static d_open_t acdopen; -static d_close_t acdclose; -static d_ioctl_t acdioctl; -static d_strategy_t acdstrategy; - -#define CDEV_MAJOR 69 -#define BDEV_MAJOR 19 -static struct cdevsw acd_cdevsw = { - /* open */ acdopen, - /* close */ acdclose, - /* read */ physread, - /* write */ physwrite, - /* ioctl */ acdioctl, - /* poll */ nopoll, - /* mmap */ nommap, - /* strategy */ acdstrategy, - /* name */ "wcd", - /* maj */ CDEV_MAJOR, - /* dump */ nodump, - /* psize */ nopsize, - /* flags */ D_DISK, - /* bmaj */ BDEV_MAJOR -}; - -#define NUNIT 16 /* Max # of devices */ - -#define F_BOPEN 0x0001 /* The block device is opened */ -#define F_MEDIA_CHANGED 0x0002 /* The media have changed since open */ -#define F_DEBUG 0x0004 /* Print debug info */ -#define F_LOCKED 0x0008 /* This unit is locked (or should be) */ -#define F_TRACK_PREP 0x0010 /* Track should be prep'ed */ -#define F_TRACK_PREPED 0x0020 /* Track has been prep'ed */ -#define F_DISK_PREPED 0x0040 /* Disk has been prep'ed */ -#define F_WRITTEN 0x0080 /* The medium has been written to */ - -static struct acd *acdtab[NUNIT]; -static int acdnlun = 0; /* Number of configured drives */ - -int acdattach(struct atapi *, int, struct atapi_params *, int); -static struct acd *acd_init_lun(struct atapi *, int, struct atapi_params *, int, -struct devstat *); -static void acd_start(struct acd *); -static void acd_done(struct acd *, struct buf *, int, struct atapires); -static int acd_read_toc(struct acd *); -static int acd_request_wait(struct acd *, u_char, u_char, u_char, u_char, u_char, u_char, u_char, u_char, u_char, u_char, char *, int); -static void acd_describe(struct acd *); -static int acd_setchan(struct acd *, u_char, u_char, u_char, u_char); -static int acd_eject(struct acd *, int); -static void acd_select_slot(struct acd *); -static int acd_open_disk(struct acd *, int); -static int acd_open_track(struct acd *, struct wormio_prepare_track *); -static int acd_close_track(struct acd *); -static int acd_close_disk(struct acd *); -static int acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info); -static int acd_blank_disk(struct acd *); -static void atapi_dump(int ctrlr, int lun, char *label, void *data, int len); -static void atapi_error(struct atapi *ata, int unit, struct atapires result); - -struct acd * -acd_init_lun(struct atapi *ata, int unit, struct atapi_params *ap, int lun, - struct devstat *device_stats) -{ - struct acd *ptr; - - if (!(ptr = malloc(sizeof(struct acd), M_TEMP, M_NOWAIT))) - return NULL; - bzero(ptr, sizeof(struct acd)); - bufq_init(&ptr->buf_queue); - ptr->ata = ata; - ptr->unit = unit; - ptr->lun = lun; - ptr->param = ap; - ptr->flags = F_MEDIA_CHANGED; - ptr->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED); - ptr->block_size = 2048; - ptr->refcnt = 0; - ptr->slot = -1; - ptr->changer_info = NULL; - if (device_stats == NULL) { - if (!(ptr->device_stats = malloc(sizeof(struct devstat), - M_TEMP, M_NOWAIT))) - return NULL; - bzero(ptr->device_stats, sizeof(struct devstat)); - } - else - ptr->device_stats = device_stats; - make_dev(&acd_cdevsw, dkmakeminor(lun, 0, 0), - UID_ROOT, GID_OPERATOR, 0640, "rwcd%da", lun); - make_dev(&acd_cdevsw, dkmakeminor(lun, 0, RAW_PART), - UID_ROOT, GID_OPERATOR, 0640, "rwcd%dc", lun); - make_dev(&acd_cdevsw, dkmakeminor(lun, 0, 0), - UID_ROOT, GID_OPERATOR, 0640, "wcd%da", lun); - make_dev(&acd_cdevsw, dkmakeminor(lun, 0, RAW_PART), - UID_ROOT, GID_OPERATOR, 0640, "wcd%dc", lun); - return ptr; -} - -int -acdattach(struct atapi *ata, int unit, struct atapi_params *ap, int debug) -{ - struct acd *cdp; - struct atapires result; - struct changer *chp; - int i, count; - - if (acdnlun >= NUNIT) { - printf("wcd: too many units\n"); - return 0; - } - if (!atapi_request_immediate) { - printf("wcd: configuration error, ATAPI code not present!\n"); - return 0; - } - if ((cdp = acd_init_lun(ata, unit, ap, acdnlun, NULL)) == NULL) { - printf("wcd: out of memory\n"); - return 0; - } - acdtab[acdnlun] = cdp; - - if (debug) { - cdp->flags |= F_DEBUG; - atapi_dump(cdp->ata->ctrlr, cdp->lun, "info", ap, sizeof(*ap)); - } - - /* Get drive capabilities, some drives needs this repeated */ - for (count = 0 ; count < 5 ; count++) { - result = atapi_request_immediate(ata, unit, - ATAPI_MODE_SENSE, - 0, ATAPI_CDROM_CAP_PAGE, - 0, 0, 0, 0, - sizeof(cdp->cap)>>8, sizeof(cdp->cap), - 0, 0, 0, 0, 0, 0, 0, - (char *)&cdp->cap, sizeof(cdp->cap)); - if (result.code == 0 || result.code == RES_UNDERRUN) - break; - } - - /* Some drives have shorter capabilities page. */ - if (result.code == RES_UNDERRUN) - result.code = 0; - - if (result.code == 0) { - cdp->cap.max_speed = ntohs(cdp->cap.max_speed); - cdp->cap.max_vol_levels = ntohs(cdp->cap.max_vol_levels); - cdp->cap.buf_size = ntohs(cdp->cap.buf_size); - cdp->cap.cur_speed = ntohs(cdp->cap.cur_speed); - acd_describe(cdp); - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "cap", &cdp->cap, - sizeof(cdp->cap)); - } - /* If this is a changer device, allocate the neeeded lun's */ - if (cdp->cap.mech == MST_MECH_CHANGER) { - char string[16]; - struct acd *tmpcdp = cdp; - - chp = malloc(sizeof(struct changer), M_TEMP, M_NOWAIT); - if (chp == NULL) { - printf("wcd: out of memory\n"); - return 0; - } - bzero(chp, sizeof(struct changer)); - result = atapi_request_immediate(ata, unit, ATAPI_MECH_STATUS, - 0, 0, 0, 0, 0, 0, 0, - sizeof(struct changer)>>8, - sizeof(struct changer), - 0, 0, 0, 0, 0, 0, - (char *)chp, sizeof(struct changer)); - if (cdp->flags & F_DEBUG) { - printf("result.code=%d curr=%02x slots=%d len=%d\n", - result.code, chp->current_slot, chp->slots, - htons(chp->table_length)); - } - if (result.code == RES_UNDERRUN) - result.code = 0; - - if (result.code == 0) { - chp->table_length = htons(chp->table_length); - for (i = 0; i < chp->slots && acdnlun < NUNIT; i++) { - if (i > 0) { - tmpcdp = acd_init_lun(ata, unit, ap, acdnlun, - cdp->device_stats); - if (!tmpcdp) { - printf("wcd: out of memory\n"); - return 0; - } - } - tmpcdp->slot = i; - tmpcdp->changer_info = chp; - printf("wcd%d: changer slot %d %s\n", acdnlun, i, - (chp->slot[i].present ? "disk present" : "no disk")); - acdtab[acdnlun++] = tmpcdp; - } - if (acdnlun >= NUNIT) { - printf("wcd: too many units\n"); - return 0; - } - } - sprintf(string, "wcd%d-", cdp->lun); - devstat_add_entry(cdp->device_stats, string, tmpcdp->lun, DEV_BSIZE, - DEVSTAT_NO_ORDERED_TAGS, - DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE, - DEVSTAT_PRIORITY_CD); - } - else { - acdnlun++; - devstat_add_entry(cdp->device_stats, "wcd", cdp->lun, DEV_BSIZE, - DEVSTAT_NO_ORDERED_TAGS, - DEVSTAT_TYPE_CDROM | DEVSTAT_TYPE_IF_IDE, - DEVSTAT_PRIORITY_CD); - } - return 1; -} - -void -acd_describe(struct acd *cdp) -{ - int comma; - char *mechanism; - - printf("wcd%d: drive speed ", cdp->lun); - if (cdp->cap.cur_speed != cdp->cap.max_speed) - printf("%d - ", cdp->cap.cur_speed * 1000 / 1024); - printf("%dKB/sec", cdp->cap.max_speed * 1000 / 1024); - if (cdp->cap.buf_size) - printf(", %dKB cache\n", cdp->cap.buf_size); - - printf("wcd%d: supported read types:", cdp->lun); - comma = 0; - if (cdp->cap.read_cdr) { - printf(" CD-R"); comma = 1; - } - if (cdp->cap.read_cdrw) { - printf("%s CD-RW", comma ? "," : ""); comma = 1; - } - if (cdp->cap.cd_da) { - printf("%s CD-DA", comma ? "," : ""); comma = 1; - } - if (cdp->cap.method2) - printf("%s packet track", comma ? "," : ""); - if (cdp->cap.write_cdr || cdp->cap.write_cdrw) { - printf("\nwcd%d: supported write types:", cdp->lun); - comma = 0; - if (cdp->cap.write_cdr) { - printf(" CD-R" ); comma = 1; - } - if (cdp->cap.write_cdrw) { - printf("%s CD-RW", comma ? "," : ""); comma = 1; - } - if (cdp->cap.test_write) { - printf("%s test write", comma ? "," : ""); comma = 1; - } - } - if (cdp->cap.audio_play) { - printf("\nwcd%d: Audio: ", cdp->lun); - if (cdp->cap.audio_play) - printf("play"); - if (cdp->cap.max_vol_levels) - printf(", %d volume levels", cdp->cap.max_vol_levels); - } - printf("\nwcd%d: Mechanism: ", cdp->lun); - switch (cdp->cap.mech) { - case MST_MECH_CADDY: - mechanism = "caddy"; break; - case MST_MECH_TRAY: - mechanism = "tray"; break; - case MST_MECH_POPUP: - mechanism = "popup"; break; - case MST_MECH_CHANGER: - mechanism = "changer"; break; - case MST_MECH_CARTRIDGE: - mechanism = "cartridge"; break; - default: - mechanism = 0; break; - } - if (mechanism) - printf("%s%s", cdp->cap.eject ? "ejectable " : "", mechanism); - else if (cdp->cap.eject) - printf("ejectable"); - - if (cdp->cap.mech != MST_MECH_CHANGER) { - printf("\nwcd%d: Medium: ", cdp->lun); - switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) { - case MST_CDROM: - printf("CD-ROM "); break; - case MST_CDR: - printf("CD-R "); break; - case MST_CDRW: - printf("CD-RW "); break; - case MST_DOOR_OPEN: - printf("door open"); break; - case MST_NO_DISC: - printf("no/blank disc inside"); break; - case MST_FMT_ERROR: - printf("medium format error"); break; - } - if ((cdp->cap.medium_type & MST_TYPE_MASK_HIGH) < MST_TYPE_MASK_HIGH) { - switch (cdp->cap.medium_type & MST_TYPE_MASK_LOW) { - case MST_DATA_120: - printf("120mm data disc loaded"); break; - case MST_AUDIO_120: - printf("120mm audio disc loaded"); break; - case MST_COMB_120: - printf("120mm data/audio disc loaded"); break; - case MST_PHOTO_120: - printf("120mm photo disc loaded"); break; - case MST_DATA_80: - printf("80mm data disc loaded"); break; - case MST_AUDIO_80: - printf("80mm audio disc loaded"); break; - case MST_COMB_80: - printf("80mm data/audio disc loaded"); break; - case MST_PHOTO_80: - printf("80mm photo disc loaded"); break; - case MST_FMT_NONE: - switch (cdp->cap.medium_type & MST_TYPE_MASK_HIGH) { - case MST_CDROM: - printf("unknown medium"); break; - case MST_CDR: - case MST_CDRW: - printf("blank medium"); break; - } - break; - default: - printf("unknown type=0x%x", cdp->cap.medium_type); break; - } - } - } - if (cdp->cap.lock) - printf(cdp->cap.locked ? ", locked" : ", unlocked"); - if (cdp->cap.prevent) - printf(", lock protected"); - printf("\n"); -} - -static int -acdopen(dev_t dev, int flags, int fmt, struct proc *p) -{ - int lun = dkunit(dev); - struct acd *cdp; - - if (lun >= acdnlun || !atapi_request_immediate) - return ENXIO; - cdp = acdtab[lun]; - - if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) { - /* Prevent user eject */ - acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); - cdp->flags |= F_LOCKED; - } - if (fmt == S_IFBLK) - cdp->flags |= F_BOPEN; - else - ++cdp->refcnt; - dev->si_bsize_phys = cdp->block_size; - if (!(flags & O_NONBLOCK) && acd_read_toc(cdp) && !(flags & FWRITE)) - printf("acd%d: read_toc failed\n", lun); - return 0; -} - -int -acdclose(dev_t dev, int flags, int fmt, struct proc *p) -{ - struct acd *cdp = acdtab[dkunit(dev)]; - - if (fmt == S_IFBLK) - cdp->flags &= ~F_BOPEN; - else - --cdp->refcnt; - - /* Are we the last open ?? */ - if (!(cdp->flags & F_BOPEN) && !cdp->refcnt) { - /* Yup, do we need to close any written tracks */ - if ((flags & FWRITE) != 0) { - if ((cdp->flags & F_TRACK_PREPED) != 0) { - acd_close_track(cdp); - cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP); - } - } - /* Allow the user eject */ - acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - } - cdp->flags &= ~F_LOCKED; - return 0; -} - -void -acdstrategy(struct buf *bp) -{ - int lun = dkunit(bp->b_dev); - struct acd *cdp = acdtab[lun]; - int x; - -#ifdef NOTYET - /* allow write only on CD-R/RW media */ /* all for now SOS */ - if (!(bp->b_flags & B_READ) && !(writeable_media)) { - bp->b_error = EROFS; - bp->b_flags |= B_ERROR; - biodone(bp); - return; - } -#endif - - if (bp->b_bcount == 0) { - bp->b_resid = 0; - biodone(bp); - return; - } - - bp->b_pblkno = bp->b_blkno; - bp->b_resid = bp->b_bcount; - - x = splbio(); - bufqdisksort(&cdp->buf_queue, bp); - acd_start(cdp); - splx(x); -} - -static void -acd_start(struct acd *cdp) -{ - struct buf *bp = bufq_first(&cdp->buf_queue); - u_long lba, blocks; - int cmd; - int count; - - if (!bp) - return; - - bufq_remove(&cdp->buf_queue, bp); - - /* Should reject all queued entries if media have changed. */ - if (cdp->flags & F_MEDIA_CHANGED) { - bp->b_error = EIO; - bp->b_flags |= B_ERROR; - biodone(bp); - return; - } - - acd_select_slot(cdp); - - if ((bp->b_flags & B_READ) == B_WRITE) { - if ((cdp->flags & F_TRACK_PREPED) == 0) { - if ((cdp->flags & F_TRACK_PREP) == 0) { - printf("wcd%d: sequence error\n", cdp->lun); - bp->b_error = EIO; - bp->b_flags |= B_ERROR; - biodone(bp); - return; - } else { - if (acd_open_track(cdp, &cdp->preptrack) != 0) { - biodone(bp); - return; - } - cdp->flags |= F_TRACK_PREPED; - } - } - } - - if (bp->b_flags & B_READ) -#ifdef NOTYET - lba = bp->b_offset / cdp->block_size; -#else - lba = bp->b_blkno / (cdp->block_size / DEV_BSIZE); -#endif - else - lba = cdp->next_writeable_lba + (bp->b_offset / cdp->block_size); - blocks = (bp->b_bcount + (cdp->block_size - 1)) / cdp->block_size; - - if ((bp->b_flags & B_READ) == B_WRITE) { - cmd = ATAPI_WRITE_BIG; - count = -bp->b_bcount; - } else { - cmd = ATAPI_READ_BIG; - count = bp->b_bcount; - } - - devstat_start_transaction(cdp->device_stats); - - atapi_request_callback(cdp->ata, cdp->unit, cmd, 0, - lba>>24, lba>>16, lba>>8, lba, 0, - blocks>>8, blocks, 0, 0, 0, 0, 0, 0, 0, - (u_char *)bp->b_data, count, - (atapi_callback_t *)acd_done, cdp, bp); -} - -static void -acd_done(struct acd *cdp, struct buf *bp, int resid, struct atapires result) -{ - - if (result.code) { - atapi_error(cdp->ata, cdp->unit, result); - bp->b_error = EIO; - bp->b_flags |= B_ERROR; - } else { - bp->b_resid = resid; - if ((bp->b_flags & B_READ) == B_WRITE) - cdp->flags |= F_WRITTEN; - } - devstat_end_transaction_buf(cdp->device_stats, bp); - biodone(bp); - acd_start(cdp); -} - -static int -acd_request_wait(struct acd *cdp, u_char cmd, u_char a1, u_char a2, - u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8, - u_char a9, char *addr, int count) -{ - struct atapires result; - - result = atapi_request_wait(cdp->ata, cdp->unit, cmd, a1, a2, a3, a4, a5, - a6, a7, a8, a9, 0, 0, 0, 0, 0, 0, addr, count); - if (result.code) { - atapi_error(cdp->ata, cdp->unit, result); - return EIO; - } - return 0; -} - -static __inline void -lba2msf(int lba, u_char *m, u_char *s, u_char *f) -{ - lba += 150; - lba &= 0xffffff; - *m = lba / (60 * 75); - lba %= (60 * 75); - *s = lba / 75; - *f = lba % 75; -} - -static __inline int -msf2lba(u_char m, u_char s, u_char f) -{ - return (m * 60 + s) * 75 + f - 150; -} - -int -acdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) -{ - int lun = dkunit(dev); - struct acd *cdp = acdtab[lun]; - int error = 0; - - if (cdp->flags & F_MEDIA_CHANGED) - switch (cmd) { - case CDIOCRESET: - break; - default: - acd_read_toc(cdp); - acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); - cdp->flags |= F_LOCKED; - break; - } - switch (cmd) { -/* - case CDIOCRESUME: - bzero(cdb); - cdb->cmd = ATAPI_PAUSE; - cdb->b8 = 0x01; - return atapi_cmd_wait(cdp->ata, cdp->unit, cdb, 0, 0, timout, 0); -*/ - case CDIOCRESUME: - return acd_request_wait(cdp, ATAPI_PAUSE, - 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0); - - case CDIOCPAUSE: - return acd_request_wait(cdp, ATAPI_PAUSE, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - case CDIOCSTART: - return acd_request_wait(cdp, ATAPI_START_STOP, - 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); - - case CDIOCSTOP: - return acd_request_wait(cdp, ATAPI_START_STOP, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - case CDIOCALLOW: - acd_select_slot(cdp); - cdp->flags &= ~F_LOCKED; - return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - case CDIOCPREVENT: - acd_select_slot(cdp); - cdp->flags |= F_LOCKED; - return acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); - - case CDIOCRESET: - error = suser(p); - if (error) - return (error); - return acd_request_wait(cdp, ATAPI_TEST_UNIT_READY, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - case CDIOCEJECT: - if ((cdp->flags & F_BOPEN) && cdp->refcnt) - return EBUSY; - return acd_eject(cdp, 0); - - case CDIOCCLOSE: - if ((cdp->flags & F_BOPEN) && cdp->refcnt) - return 0; - return acd_eject(cdp, 1); - - case CDIOREADTOCHEADER: - if (!cdp->toc.hdr.ending_track) - return EIO; - bcopy(&cdp->toc.hdr, addr, sizeof(cdp->toc.hdr)); - break; - - case CDIOREADTOCENTRYS: - { - struct ioc_read_toc_entry *te = (struct ioc_read_toc_entry *)addr; - struct toc *toc = &cdp->toc; - struct toc buf; - u_long len; - u_char starting_track = te->starting_track; - - if (!cdp->toc.hdr.ending_track) - return EIO; - - if (te->data_len < sizeof(toc->tab[0]) || - (te->data_len % sizeof(toc->tab[0])) != 0 || - (te->address_format != CD_MSF_FORMAT && - te->address_format != CD_LBA_FORMAT)) - return EINVAL; - - if (!starting_track) - starting_track = toc->hdr.starting_track; - else if (starting_track == 170) - starting_track = toc->hdr.ending_track + 1; - else if (starting_track < toc->hdr.starting_track || - starting_track > toc->hdr.ending_track + 1) - return EINVAL; - - len = ((toc->hdr.ending_track + 1 - starting_track) + 1) * - sizeof(toc->tab[0]); - if (te->data_len < len) - len = te->data_len; - if (len > sizeof(toc->tab)) - return EINVAL; - - if (te->address_format == CD_MSF_FORMAT) { - struct cd_toc_entry *entry; - - buf = cdp->toc; - toc = &buf; - entry = toc->tab + (toc->hdr.ending_track + 1 - - toc->hdr.starting_track) + 1; - while (--entry >= toc->tab) - lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute, - &entry->addr.msf.second, &entry->addr.msf.frame); - } - return copyout(toc->tab + starting_track - toc->hdr.starting_track, - te->data, len); - } - - case CDIOREADTOCENTRY: - { - struct ioc_read_toc_single_entry *te = - (struct ioc_read_toc_single_entry *)addr; - struct toc *toc = &cdp->toc; - struct toc buf; - u_char track = te->track; - - if (!cdp->toc.hdr.ending_track) - return EIO; - - if (te->address_format != CD_MSF_FORMAT && - te->address_format != CD_LBA_FORMAT) - return EINVAL; - - if (!track) - track = toc->hdr.starting_track; - else if (track == 170) - track = toc->hdr.ending_track + 1; - else if (track < toc->hdr.starting_track || - track > toc->hdr.ending_track + 1) - return EINVAL; - - if (te->address_format == CD_MSF_FORMAT) { - struct cd_toc_entry *entry; - - buf = cdp->toc; - toc = &buf; - entry = toc->tab + (track - toc->hdr.starting_track); - lba2msf(ntohl(entry->addr.lba), &entry->addr.msf.minute, - &entry->addr.msf.second, &entry->addr.msf.frame); - } - bcopy(toc->tab + track - toc->hdr.starting_track, - &te->entry, sizeof(struct cd_toc_entry)); - } - break; - - case CDIOCREADSUBCHANNEL: - { - struct ioc_read_subchannel *args = - (struct ioc_read_subchannel *)addr; - struct cd_sub_channel_info data; - u_long len = args->data_len; - int abslba, rellba; - - if (len > sizeof(data) || - len < sizeof(struct cd_sub_channel_header)) - return EINVAL; - - if (acd_request_wait(cdp, ATAPI_READ_SUBCHANNEL, - 0, 0x40, 1, 0, 0, 0, - sizeof(cdp->subchan)>>8, sizeof(cdp->subchan), - 0, - (char *)&cdp->subchan, - sizeof(cdp->subchan)) != 0) - return EIO; - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "subchan", &cdp->subchan, - sizeof(cdp->subchan)); - - abslba = cdp->subchan.abslba; - rellba = cdp->subchan.rellba; - if (args->address_format == CD_MSF_FORMAT) { - lba2msf(ntohl(abslba), - &data.what.position.absaddr.msf.minute, - &data.what.position.absaddr.msf.second, - &data.what.position.absaddr.msf.frame); - lba2msf(ntohl(rellba), - &data.what.position.reladdr.msf.minute, - &data.what.position.reladdr.msf.second, - &data.what.position.reladdr.msf.frame); - } else { - data.what.position.absaddr.lba = abslba; - data.what.position.reladdr.lba = rellba; - } - data.header.audio_status = cdp->subchan.audio_status; - data.what.position.control = cdp->subchan.control & 0xf; - data.what.position.addr_type = cdp->subchan.control >> 4; - data.what.position.track_number = cdp->subchan.track; - data.what.position.index_number = cdp->subchan.indx; - return copyout(&data, args->data, len); - } - - case CDIOCPLAYMSF: - { - struct ioc_play_msf *args = (struct ioc_play_msf *)addr; - - return acd_request_wait(cdp, ATAPI_PLAY_MSF, 0, 0, - args->start_m, args->start_s, args->start_f, - args->end_m, args->end_s, args->end_f, - 0, 0, 0); - } - - case CDIOCPLAYBLOCKS: - { - struct ioc_play_blocks *args = (struct ioc_play_blocks *)addr; - - return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0, - args->blk>>24 & 0xff, args->blk>>16 & 0xff, - args->blk>>8 & 0xff, args->blk & 0xff, - args->len>>24 & 0xff, args->len>>16 & 0xff, - args->len>>8 & 0xff, args->len & 0xff, - 0, 0); - } - - case CDIOCPLAYTRACKS: - { - struct ioc_play_track *args = (struct ioc_play_track *)addr; - u_long start, len; - int t1, t2; - - if (!cdp->toc.hdr.ending_track) - return EIO; - - if (args->end_track < cdp->toc.hdr.ending_track + 1) - ++args->end_track; - if (args->end_track > cdp->toc.hdr.ending_track + 1) - args->end_track = cdp->toc.hdr.ending_track + 1; - t1 = args->start_track - cdp->toc.hdr.starting_track; - t2 = args->end_track - cdp->toc.hdr.starting_track; - if (t1 < 0 || t2 < 0) - return EINVAL; - start = ntohl(cdp->toc.tab[t1].addr.lba); - len = ntohl(cdp->toc.tab[t2].addr.lba) - start; - - return acd_request_wait(cdp, ATAPI_PLAY_BIG, 0, - start>>24 & 0xff, start>>16 & 0xff, - start>>8 & 0xff, start & 0xff, - len>>24 & 0xff, len>>16 & 0xff, - len>>8 & 0xff, len & 0xff, 0, 0); - } - - case CDIOCREADAUDIO: - { - struct ioc_read_audio* args = (struct ioc_read_audio*) addr; - int lba, frames, result = 0; - u_char *buffer, *ubuf = args->buffer; - - if (!cdp->toc.hdr.ending_track) - return EIO; - - if ((frames = args->nframes) < 0) - return EINVAL; - - if (args->address_format == CD_LBA_FORMAT) - lba = args->address.lba; - else if (args->address_format == CD_MSF_FORMAT) - lba = msf2lba(args->address.msf.minute, - args->address.msf.second, - args->address.msf.frame); - else - return EINVAL; -#ifndef CD_BUFFER_BLOCKS -#define CD_BUFFER_BLOCKS 8 -#endif - if (!(buffer = malloc(CD_BUFFER_BLOCKS * 2352, M_TEMP, M_NOWAIT))) - return ENOMEM; - - while (frames > 0) { - u_char blocks; - int size; - - blocks = (frames>CD_BUFFER_BLOCKS) ? CD_BUFFER_BLOCKS : frames; - size = blocks * 2352; - - result = acd_request_wait(cdp, ATAPI_READ_CD, 4, - lba>>24, (lba>>16)&0xff, - (lba>>8)&0xff, lba&0xff, 0, 0, - blocks, 0xf0, buffer, size); - if (result != 0) - break; - - result = copyout(buffer, ubuf, size); - if (result != 0) - break; - - ubuf += size; - frames -= blocks; - lba += blocks; - } - - free(buffer, M_TEMP); - return result; - } - - case CDIOCGETVOL: - { - struct ioc_vol *arg = (struct ioc_vol *)addr; - - error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE, - 0, 0, 0, 0, - sizeof(cdp->au)>>8, sizeof(cdp->au), 0, - (char *)&cdp->au, sizeof(cdp->au)); - if (error) - return error; - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au, - sizeof(cdp->au)); - if (cdp->au.page_code != CDROM_AUDIO_PAGE) - return EIO; - arg->vol[0] = cdp->au.port[0].volume; - arg->vol[1] = cdp->au.port[1].volume; - arg->vol[2] = cdp->au.port[2].volume; - arg->vol[3] = cdp->au.port[3].volume; - } - break; - - case CDIOCSETVOL: - { - struct ioc_vol *arg = (struct ioc_vol *)addr; - - error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE, - 0, 0, 0, 0, - sizeof(cdp->au)>>8, sizeof(cdp->au), 0, - (char *)&cdp->au, sizeof(cdp->au)); - if (error) - return error; - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au, - sizeof(cdp->au)); - if (cdp->au.page_code != CDROM_AUDIO_PAGE) - return EIO; - - error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, - CDROM_AUDIO_PAGE_MASK, 0, 0, 0, 0, - sizeof(cdp->aumask)>>8,sizeof(cdp->aumask), - 0, - (char *)&cdp->aumask, sizeof(cdp->aumask)); - if (error) - return error; - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "mask", &cdp->aumask, - sizeof(cdp->aumask)); - - cdp->au.data_length = 0; - cdp->au.port[0].channels = CHANNEL_0; - cdp->au.port[1].channels = CHANNEL_1; - cdp->au.port[0].volume = arg->vol[0] & cdp->aumask.port[0].volume; - cdp->au.port[1].volume = arg->vol[1] & cdp->aumask.port[1].volume; - cdp->au.port[2].volume = arg->vol[2] & cdp->aumask.port[2].volume; - cdp->au.port[3].volume = arg->vol[3] & cdp->aumask.port[3].volume; - return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10, - 0, 0, 0, 0, 0, - sizeof(cdp->au)>>8, sizeof(cdp->au), - 0, (char *)&cdp->au, -sizeof(cdp->au)); - } - - case CDIOCSETPATCH: - { - struct ioc_patch *arg = (struct ioc_patch *)addr; - - return acd_setchan(cdp, arg->patch[0], arg->patch[1], - arg->patch[2], arg->patch[3]); - } - - case CDIOCSETMONO: - return acd_setchan(cdp, CHANNEL_0|CHANNEL_1, CHANNEL_0|CHANNEL_1, 0, 0); - - case CDIOCSETSTEREO: - return acd_setchan(cdp, CHANNEL_0, CHANNEL_1, 0, 0); - - case CDIOCSETMUTE: - return acd_setchan(cdp, 0, 0, 0, 0); - - case CDIOCSETLEFT: - return acd_setchan(cdp, CHANNEL_0, CHANNEL_0, 0, 0); - - case CDIOCSETRIGHT: - return acd_setchan(cdp, CHANNEL_1, CHANNEL_1, 0, 0); - - case CDRIOCNEXTWRITEABLEADDR: - { - struct acd_track_info track_info; - - if ((error = acd_read_track_info(cdp, 0xff, &track_info))) - break; - if (!track_info.nwa_valid) - return EINVAL; - cdp->next_writeable_lba = track_info.next_writeable_addr; - *(int*)addr = track_info.next_writeable_addr; - } - break; - - case WORMIOCPREPDISK: - { - struct wormio_prepare_disk *w = (struct wormio_prepare_disk *)addr; - - if (w->dummy != 0 && w->dummy != 1) - error = EINVAL; - else { - error = acd_open_disk(cdp, w->dummy); - if (error == 0) { - cdp->flags |= F_DISK_PREPED; - cdp->dummy = w->dummy; - cdp->speed = w->speed; - } - } - } - break; - - case WORMIOCPREPTRACK: - { - struct wormio_prepare_track *w =(struct wormio_prepare_track *)addr; - - if (w->audio != 0 && w->audio != 1) - error = EINVAL; - else if (w->audio == 0 && w->preemp) - error = EINVAL; - else if ((cdp->flags & F_DISK_PREPED) == 0) { - error = EINVAL; - printf("wcd%d: sequence error (PREP_TRACK)\n", cdp->lun); - } else { - cdp->flags |= F_TRACK_PREP; - cdp->preptrack = *w; - } - } - break; - - case WORMIOCFINISHTRACK: - if ((cdp->flags & F_TRACK_PREPED) != 0) - error = acd_close_track(cdp); - cdp->flags &= ~(F_TRACK_PREPED | F_TRACK_PREP); - break; - - case WORMIOCFIXATION: - { - struct wormio_fixation *w = - (struct wormio_fixation *)addr; - - if ((cdp->flags & F_WRITTEN) == 0) - error = EINVAL; - else if (w->toc_type < 0 /* WORM_TOC_TYPE_AUDIO */ || - w->toc_type > 4 /* WORM_TOC_TYPE_CDI */ ) - error = EINVAL; - else if (w->onp != 0 && w->onp != 1) - error = EINVAL; - else { - /* no fixation needed if dummy write */ - if (cdp->dummy == 0) - error = acd_close_disk(cdp); - cdp->flags &= - ~(F_WRITTEN|F_DISK_PREPED|F_TRACK_PREP|F_TRACK_PREPED); - } - } - break; - - case CDRIOCBLANK: - return acd_blank_disk(cdp); - - default: - return ENOTTY; - } - return error; -} - -static int -acd_read_toc(struct acd *cdp) -{ - int ntracks, len; - struct atapires result; - - bzero(&cdp->toc, sizeof(cdp->toc)); - bzero(&cdp->info, sizeof(cdp->info)); - - acd_select_slot(cdp); - - result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0); - - if (result.code == RES_ERR && - (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION) { - cdp->flags |= F_MEDIA_CHANGED; - cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED); - result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_TEST_UNIT_READY, - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0); - } - - if (result.code) { - atapi_error(cdp->ata, cdp->unit, result); - return EIO; - } - - cdp->flags &= ~F_MEDIA_CHANGED; - - len = sizeof(struct ioc_toc_header) + sizeof(struct cd_toc_entry); - if (acd_request_wait(cdp, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0, - len>>8, len & 0xff, 0, (char *)&cdp->toc, len) != 0) { - bzero(&cdp->toc, sizeof(cdp->toc)); - return 0; - } - ntracks = cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1; - if (ntracks <= 0 || ntracks > MAXTRK) { - bzero(&cdp->toc, sizeof(cdp->toc)); - return 0; - } - - len = sizeof(struct ioc_toc_header) + ntracks * sizeof(struct cd_toc_entry); - if (acd_request_wait(cdp, ATAPI_READ_TOC, 0, 0, 0, 0, 0, 0, - len>>8, len & 0xff, 0, (char *)&cdp->toc, len) & 0xff){ - bzero(&cdp->toc, sizeof(cdp->toc)); - return 0; - } - - cdp->toc.hdr.len = ntohs(cdp->toc.hdr.len); - - if (acd_request_wait(cdp, ATAPI_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0, - (char *)&cdp->info, sizeof(cdp->info)) != 0) - bzero(&cdp->info, sizeof(cdp->info)); - - cdp->toc.tab[ntracks].control = cdp->toc.tab[ntracks - 1].control; - cdp->toc.tab[ntracks].addr_type = cdp->toc.tab[ntracks - 1].addr_type; - cdp->toc.tab[ntracks].track = 170; - cdp->toc.tab[ntracks].addr.lba = cdp->info.volsize; - - cdp->info.volsize = ntohl(cdp->info.volsize); - cdp->info.blksize = ntohl(cdp->info.blksize); - - if (cdp->info.volsize && cdp->toc.hdr.ending_track - && (cdp->flags & F_DEBUG)) { - printf("wcd%d: ", cdp->lun); - if (cdp->toc.tab[0].control & 4) - printf("%ldMB ", cdp->info.volsize / 512); - else - printf("%ld:%ld audio ", cdp->info.volsize / 75 / 60, - cdp->info.volsize / 75 % 60); - printf("(%ld sectors (%ld bytes)), %d tracks\n", - cdp->info.volsize, cdp->info.blksize, - cdp->toc.hdr.ending_track - cdp->toc.hdr.starting_track + 1); - } - return 0; -} - -/* - * Set up the audio channel masks. - */ -static int -acd_setchan(struct acd *cdp, u_char c0, u_char c1, u_char c2, u_char c3) -{ - int error; - - error = acd_request_wait(cdp, ATAPI_MODE_SENSE, 0, CDROM_AUDIO_PAGE, - 0, 0, 0, 0, - sizeof(cdp->au)>>8, sizeof(cdp->au), 0, - (char *)&cdp->au, sizeof(cdp->au)); - if (error) - return error; - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "au", &cdp->au, sizeof(cdp->au)); - if (cdp->au.page_code != CDROM_AUDIO_PAGE) - return EIO; - - cdp->au.data_length = 0; - cdp->au.port[0].channels = c0; - cdp->au.port[1].channels = c1; - cdp->au.port[2].channels = c2; - cdp->au.port[3].channels = c3; - return acd_request_wait(cdp, ATAPI_MODE_SELECT, 0x10, - 0, 0, 0, 0, 0, - sizeof(cdp->au)>>8, sizeof(cdp->au), 0, - (char *)&cdp->au, -sizeof(cdp->au)); -} - -static int -acd_eject(struct acd *cdp, int close) -{ - struct atapires result; - - acd_select_slot(cdp); - - result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_START_STOP, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - if (result.code == RES_ERR && - ((result.error & AER_SKEY) == AER_SK_NOT_READY || - (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)) { - int err; - - if (!close) - return 0; - err = acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 3, - 0, 0, 0, 0, 0, 0, 0); - if (err) - return err; - - acd_read_toc(cdp); - - acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0); - cdp->flags |= F_LOCKED; - return 0; - } - if (result.code) { - atapi_error(cdp->ata, cdp->unit, result); - return EIO; - } - if (close) - return 0; - - tsleep((caddr_t) &lbolt, PRIBIO, "wcdej1", 0); - tsleep((caddr_t) &lbolt, PRIBIO, "wcdej2", 0); - - acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - cdp->flags &= ~F_LOCKED; - - cdp->flags |= F_MEDIA_CHANGED; - cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED); - return acd_request_wait(cdp, ATAPI_START_STOP, 0, 0, 0, 2, - 0, 0, 0, 0, 0, 0, 0); -} - -static void -acd_select_slot(struct acd *cdp) -{ - if (cdp->slot < 0 || cdp->changer_info->current_slot == cdp->slot) - return; - - /* Unlock (might not be needed but its cheaper than asking) */ - acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - /* Unload the current media from player */ - acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 2, - 0, 0, 0, cdp->changer_info->current_slot, 0, 0, 0); - - /* load the wanted slot */ - acd_request_wait(cdp, ATAPI_LOAD_UNLOAD, 0, 0, 0, 3, - 0, 0, 0, cdp->slot, 0, 0, 0); - - cdp->changer_info->current_slot = cdp->slot; - - /* Lock the media if needed */ - if (cdp->flags & F_LOCKED) { - acd_request_wait(cdp, ATAPI_PREVENT_ALLOW, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0); - } -} - -static int -acd_open_disk(struct acd *cdp, int test) -{ - cdp->next_writeable_lba = 0; - return 0; -} - -static int -acd_close_disk(struct acd *cdp) -{ - return acd_request_wait(cdp, ATAPI_CLOSE_TRACK, 0x00, - 0x02, 0, 0, 0/*track*/, 0, 0, 0, 0, 0, 0); -} - -static int -acd_open_track(struct acd *cdp, struct wormio_prepare_track *ptp) -{ - struct write_param param; - struct atapires result; - - result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SENSE, - 0, 0x05, 0, 0, 0, 0, - sizeof(param)>>8, sizeof(param), - 0, 0, 0, 0, 0, 0, 0, - (char *)¶m, sizeof(param)); - - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param)); - - if (result.code == RES_UNDERRUN) - result.code = 0; - - if (result.code) { - atapi_error(cdp->ata, cdp->unit, result); - return EIO; - } - param.page_code = 0x05; - param.page_length = 0x32; - param.test_write = cdp->dummy ? 1 : 0; - param.write_type = CDR_WTYPE_TRACK; - - switch (ptp->audio) { -/* switch (data_type) { */ - - case 0: -/* case CDR_DATA: */ - cdp->block_size = 2048; - param.track_mode = CDR_TMODE_DATA; - param.data_block_type = CDR_DB_ROM_MODE1; - param.session_format = CDR_SESS_CDROM; - break; - - default: -/* case CDR_AUDIO: */ - cdp->block_size = 2352; - if (ptp->preemp) - param.track_mode = CDR_TMODE_AUDIO; - else - param.track_mode = 0; - param.data_block_type = CDR_DB_RAW; - param.session_format = CDR_SESS_CDROM; - break; - -/* - case CDR_MODE2: - param.track_mode = CDR_TMODE_DATA; - param.data_block_type = CDR_DB_ROM_MODE2; - param.session_format = CDR_SESS_CDROM; - break; - - case CDR_XA1: - param.track_mode = CDR_TMODE_DATA; - param.data_block_type = CDR_DB_XA_MODE1; - param.session_format = CDR_SESS_CDROM_XA; - break; - - case CDR_XA2: - param.track_mode = CDR_TMODE_DATA; - param.data_block_type = CDR_DB_XA_MODE2_F1; - param.session_format = CDR_SESS_CDROM_XA; - break; - - case CDR_CDI: - param.track_mode = CDR_TMODE_DATA; - param.data_block_type = CDR_DB_XA_MODE2_F1; - param.session_format = CDR_SESS_CDI; - break; -*/ - } - - param.multi_session = CDR_MSES_NONE; - param.fp = 0; - param.packet_size = 0; - - if (cdp->flags & F_DEBUG) - atapi_dump(cdp->ata->ctrlr, cdp->lun, "0x05", ¶m, sizeof(param)); - - result = atapi_request_wait(cdp->ata, cdp->unit, ATAPI_MODE_SELECT, - 0x10, 0, 0, 0, 0, 0, - sizeof(param)>>8, sizeof(param), - 0, 0, 0, 0, 0, 0, 0, - (char *)¶m, -sizeof(param)); - - if (result.code == RES_UNDERRUN) - result.code = 0; - - if (result.code) { - atapi_error(cdp->ata, cdp->unit, result); - return EIO; - } - return 0; -} - -static int -acd_close_track(struct acd *cdp) -{ - return acd_request_wait(cdp, ATAPI_SYNCHRONIZE_CACHE, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); -} - -static int -acd_read_track_info(struct acd *cdp, int lba, struct acd_track_info *info) -{ - int error; - - error = acd_request_wait(cdp, ATAPI_READ_TRACK_INFO, 0x01, - lba>>24, (lba>>16)&0xff, - (lba>>8)&0xff, lba&0xff, - 0, - sizeof(*info)>>8, sizeof(*info), 0, - (char *)info, sizeof(*info)); - if (error) - return error; - info->track_start_addr = ntohl(info->track_start_addr); - info->next_writeable_addr = ntohl(info->next_writeable_addr); - info->free_blocks = ntohl(info->free_blocks); - info->fixed_packet_size = ntohl(info->fixed_packet_size); - info->track_length = ntohl(info->track_length); - return 0; -} - -static int -acd_blank_disk(struct acd *cdp) -{ - int error; - - error = acd_request_wait(cdp, 0xa1, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - cdp->flags |= F_MEDIA_CHANGED; - cdp->flags &= ~(F_WRITTEN|F_TRACK_PREP|F_TRACK_PREPED); - return error; -} - -static void -atapi_error(struct atapi *ata, int unit, struct atapires result) -{ - if (result.code != RES_ERR) { - printf("atapi%d:%d: ERROR %d, status=%b, error=%b\n", - ata->ctrlr, unit, result.code, result.status, - ARS_BITS, result.error, AER_BITS); - return; - } - switch (result.error & AER_SKEY) { - case AER_SK_NOT_READY: - if (ata->debug) - printf("atapi%d:%d: not ready\n", ata->ctrlr, unit); - break; - - case AER_SK_BLANK_CHECK: - if (ata->debug) - printf("atapi%d:%d: blank check\n", ata->ctrlr, unit); - break; - - case AER_SK_MEDIUM_ERROR: - if (ata->debug) - printf("atapi%d:%d: medium error\n", ata->ctrlr, unit); - break; - - case AER_SK_HARDWARE_ERROR: - if (ata->debug) - printf("atapi%d:%d: hardware error\n", ata->ctrlr, unit); - break; - - case AER_SK_ILLEGAL_REQUEST: - if (ata->debug) - printf("atapi%d:%d: illegal request\n", ata->ctrlr, unit); - break; - - case AER_SK_UNIT_ATTENTION: - if (ata->debug) - printf("atapi%d:%d: unit attention\n", ata->ctrlr, unit); - break; - - case AER_SK_DATA_PROTECT: - if (ata->debug) - printf("atapi%d:%d: reading protected data\n", ata->ctrlr, unit); - break; - - case AER_SK_ABORTED_COMMAND: - if (ata->debug) - printf("atapi%d:%d: command aborted\n", ata->ctrlr, unit); - break; - - case AER_SK_MISCOMPARE: - if (ata->debug) - printf("atapi%d:%d: data don't match medium\n", ata->ctrlr, unit); - break; - - default: - if (ata->debug) - printf("atapi%d:%d: unknown error, status=%b, error=%b\n", - ata->ctrlr, unit, result.status, ARS_BITS, - result.error, AER_BITS); - } -} - -static void -atapi_dump(int ctrlr, int lun, char *label, void *data, int len) -{ - u_char *p = data; - - printf ("atapi%d%d: %s %x", ctrlr, lun, label, *p++); - while (--len > 0) printf ("-%x", *p++); - printf ("\n"); -} - -static void -acd_drvinit(void *unused) -{ - cdevsw_add(&acd_cdevsw); -} - -SYSINIT(acddev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE + CDEV_MAJOR, acd_drvinit, NULL) diff --git a/sys/i386/isa/wd_cd.h b/sys/i386/isa/wd_cd.h deleted file mode 100644 index a666522..0000000 --- a/sys/i386/isa/wd_cd.h +++ /dev/null @@ -1,349 +0,0 @@ -/*- - * Copyright (c) 1998, 1999 Søren Schmidt - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer, - * without modification, immediately at the beginning of the file. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -/* - * CDROM Table Of Contents - */ -#define MAXTRK 99 -struct toc { - struct ioc_toc_header hdr; - struct cd_toc_entry tab[MAXTRK + 1]; -}; - -/* - * CDROM Audio Control Parameters Page - */ -struct audiopage { - /* Mode Page data header */ - u_short data_length; - u_char medium_type; - u_char dev_spec; - u_char unused[2]; - u_short blk_desc_len; - - /* Audio control page */ - u_char page_code; -#define CDROM_AUDIO_PAGE 0x0e -#define CDROM_AUDIO_PAGE_MASK 0x4e - - u_char param_len; - u_char flags; -#define CD_PA_SOTC 0x02 -#define CD_PA_IMMED 0x04 - - u_char reserved3; - u_char reserved4; - u_char reserved5; - u_short lb_per_sec; - struct port_control { - u_char channels:4; -#define CHANNEL_0 1 -#define CHANNEL_1 2 -#define CHANNEL_2 4 -#define CHANNEL_3 8 - u_char volume; - } port[4]; -}; - -/* - * CDROM Capabilities and Mechanical Status Page - */ -struct cappage { - /* Mode data header */ - u_short data_length; - u_char medium_type; /* Present media type */ -#define MST_TYPE_MASK_LOW 0x0f -#define MST_FMT_NONE 0x00 -#define MST_DATA_120 0x01 -#define MST_AUDIO_120 0x02 -#define MST_COMB_120 0x03 -#define MST_PHOTO_120 0x04 -#define MST_DATA_80 0x05 -#define MST_AUDIO_80 0x06 -#define MST_COMB_80 0x07 -#define MST_PHOTO_80 0x08 - -#define MST_TYPE_MASK_HIGH 0x70 -#define MST_CDROM 0x00 -#define MST_CDR 0x10 -#define MST_CDRW 0x20 - -#define MST_NO_DISC 0x70 -#define MST_DOOR_OPEN 0x71 -#define MST_FMT_ERROR 0x72 - - u_char dev_spec; - u_char unused[2]; - u_short blk_desc_len; - - /* Capabilities page */ - u_char page_code; -#define ATAPI_CDROM_CAP_PAGE 0x2a - - u_char param_len; - u_char read_cdr:1; /* Supports CD-R read */ - u_char read_cdrw:1; /* Supports CD-RW read */ - u_char method2:1; /* Supports reading packet tracks */ - u_char byte2_37:5; - u_char write_cdr:1; /* Supports CD-R write */ - u_char write_cdrw:1; /* Supports CD-RW write */ - u_char test_write:1; /* Supports test writing */ - u_char byte3_37:5; - u_char audio_play:1; /* Audio play supported */ - u_char composite:1; /* Composite audio/video supported */ - u_char dport1:1; /* Digital audio on port 1 */ - u_char dport2:1; /* Digital audio on port 2 */ - u_char mode2_form1:1; /* Mode 2 form 1 (XA) read */ - u_char mode2_form2:1; /* Mode 2 form 2 format */ - u_char multisession:1; /* Multi-session photo-CD */ - u_char:1; - u_char cd_da:1; /* Audio-CD read supported */ - u_char cd_da_stream:1; /* CD-DA streaming */ - u_char rw:1; /* Combined R-W subchannels */ - u_char rw_corr:1; /* R-W subchannel data corrected */ - u_char c2:1; /* C2 error pointers supported */ - u_char isrc:1; /* Can return the ISRC info */ - u_char upc:1; /* Can return the catalog number UPC */ - u_char:1; - u_char lock:1; /* Can be locked */ - u_char locked:1; /* Current lock state */ - u_char prevent:1; /* Prevent jumper installed */ - u_char eject:1; /* Can eject */ - u_char:1; - u_char mech:3; /* Loading mechanism type */ -#define MST_MECH_CADDY 0 -#define MST_MECH_TRAY 1 -#define MST_MECH_POPUP 2 -#define MST_MECH_CHANGER 4 -#define MST_MECH_CARTRIDGE 5 - - u_char sep_vol:1; /* Independent volume of channels */ - u_char sep_mute:1; /* Independent mute of channels */ - u_char:6; - - u_short max_speed; /* Max raw data rate in bytes/1000 */ - u_short max_vol_levels; /* Number of discrete volume levels */ - u_short buf_size; /* Internal buffer size in bytes/1024 */ - u_short cur_speed; /* Current data rate in bytes/1000 */ - - u_char reserved3; - u_char bckf:1; /* Data valid on failing edge of BCK */ - u_char rch:1; /* High LRCK indicates left channel */ - u_char lsbf:1; /* Set if LSB first */ - u_char dlen:2; -#define MST_DLEN_32 0 -#define MST_DLEN_16 1 -#define MST_DLEN_24 2 -#define MST_DLEN_24_I2S 3 - - u_char:3; - u_char reserved4[2]; -}; - -/* - * CDROM Changer mechanism status structure - */ -struct changer { - u_char current_slot:5; /* Active changer slot */ - u_char mech_state:2; /* Current changer state */ -#define CH_READY 0 -#define CH_LOADING 1 -#define CH_UNLOADING 2 -#define CH_INITIALIZING 3 - - u_char fault:1; /* Fault in last operation */ - u_char reserved0:5; - u_char cd_state:3; /* Current mechanism state */ -#define CD_IDLE 0 -#define CD_AUDIO_ACTIVE 1 -#define CD_AUDIO_SCAN 2 -#define CD_HOST_ACTIVE 3 -#define CD_NO_STATE 7 - - u_char current_lba[3]; /* Current LBA */ - u_char slots; /* Number of available slots */ - u_short table_length; /* Slot table length */ - struct { - u_char changed:1; /* Media has changed in this slot */ - u_char unused:6; - u_char present:1; /* Slot has a CD present */ - u_char reserved0; - u_char reserved1; - u_char reserved2; - } slot[32]; -}; - -/* - * CDROM Write Parameters Mode Page (Burners ONLY) - */ -struct write_param { - /* Mode Page data header */ - u_short data_length; - u_char medium_type; - u_char dev_spec; - u_char unused[2]; - u_short blk_desc_len; - - /* Write Parameters mode page */ - u_char page_code; /* 0x05 */ - u_char page_length; /* 0x32 */ - u_char write_type:4; /* Write stream type */ -#define CDR_WTYPE_PACKET 0x00 -#define CDR_WTYPE_TRACK 0x01 -#define CDR_WTYPE_SESSION 0x02 -#define CDR_WTYPE_RAW 0x03 - - u_char test_write:1; /* Test write enable */ - u_char reserved2_567:3; - u_char track_mode:4; /* Track mode */ -#define CDR_TMODE_AUDIO 0x01 -#define CDR_TMODE_INCR_DATA 0x01 -#define CDR_TMODE_ALLOW_COPY 0x02 -#define CDR_TMODE_DATA 0x04 -#define CDR_TMODE_QUAD_AUDIO 0x08 - - u_char copy:1; /* Generation stamp */ - u_char fp:1; /* Fixed packet type */ - u_char multi_session:2; /* Multi-session type */ -#define CDR_MSES_NONE 0x00 -#define CDR_MSES_FINAL 0x01 -#define CDR_MSES_RESERVED 0x02 -#define CDR_MSES_NULTI 0x03 - - u_char data_block_type:4; /* Data block type code */ -#define CDR_DB_RAW 0x0 /* 2352 bytes of raw data */ -#define CDR_DB_RAW_PQ 0x1 /* 2368 bytes raw data + P/Q subchan */ -#define CDR_DB_RAW_PW 0x2 /* 2448 bytes raw data + P-W subchan */ -#define CDR_DB_RAW_PW_R 0x3 /* 2448 bytes raw data + P-W raw sub */ -#define CDR_DB_RES_4 0x4 /* Reserved */ -#define CDR_DB_RES_5 0x5 /* Reserved */ -#define CDR_DB_RES_6 0x6 /* Reserved */ -#define CDR_DB_VS_7 0x7 /* Vendor specific */ -#define CDR_DB_ROM_MODE1 0x8 /* 2048 bytes Mode 1 (ISO/IEC 10149) */ -#define CDR_DB_ROM_MODE2 0x9 /* 2336 bytes Mode 2 (ISO/IEC 10149) */ -#define CDR_DB_XA_MODE1 0x10 /* 2048 bytes Mode 1 (CD-ROM XA 1) */ -#define CDR_DB_XA_MODE2_F1 0x11 /* 2056 bytes Mode 2 (CD-ROM XA 1) */ -#define CDR_DB_XA_MODE2_F2 0x12 /* 2324 bytes Mode 2 (CD-ROM XA 2) */ -#define CDR_DB_XA_MODE2_MIX 0x13 /* 2332 bytes Mode 2 (CD-ROM XA 1/2) */ -#define CDR_DB_RES_14 0x14 /* Reserved */ -#define CDR_DB_VS_15 0x15 /* Vendor specific */ - - u_char reserved4_4567:4; - u_char reserved5; - u_char reserved6; - u_char host_app_code:6; /* Host application code */ - u_char reserved7_67:2; - u_char session_format; /* Session format */ -#define CDR_SESS_CDROM 0x00 -#define CDR_SESS_CDI 0x10 -#define CDR_SESS_CDROM_XA 0x20 - - u_char reserved9; - u_int packet_size; /* Packet size in bytes */ - u_short audio_pause_length; /* Audio pause length in secs */ - u_char media_catalog_number[16]; - u_char isr_code[16]; - u_char sub_hdr_byte0; - u_char sub_hdr_byte1; - u_char sub_hdr_byte2; - u_char sub_hdr_byte3; -/* - u_char vendor_specific_byte0; - u_char vendor_specific_byte1; - u_char vendor_specific_byte2; - u_char vendor_specific_byte3; -*/ - -} __attribute__((packed)); -/* - * CDROM Read Track Information structure - */ -struct acd_track_info { - u_short data_length; - u_char track_number; /* Current track number */ - u_char session_number; /* Current session number */ - u_char reserved4; - u_char track_mode:4; /* Mode of this track */ - u_char copy:1; /* Generation stamp */ - u_char damage:1; /* Damaged track */ - u_char reserved5_67:2; - u_char data_mode:4; /* Data mode of this disc */ - u_char fp:1; /* Fixed packet */ - u_char packet:1; /* Packet track */ - u_char blank:1; /* Blank (empty) track */ - u_char rt:1; /* Reserved track */ - u_char nwa_valid:1; /* next_writeable_addr field valid */ - u_char reserved7_17:7; - u_int track_start_addr; /* Start of this track */ - u_int next_writeable_addr; /* Next writeable addr on this disc */ - u_int free_blocks; /* Free block on this disc */ - u_int fixed_packet_size; /* Size of packets on this track */ - u_int track_length; /* Length of this track */ -}; - -/* - * Structure describing an ATAPI CDROM device - */ -struct acd { - int unit; /* IDE bus drive unit */ - int lun; /* Logical device unit */ - int flags; /* Device state flags */ - int refcnt; /* The number of raw opens */ - struct atapi *ata; /* Controller structure */ - struct buf_queue_head buf_queue; /* Queue of i/o requests */ - struct atapi_params *param; /* Drive parameters table */ - struct toc toc; /* Table of disc contents */ - struct { - u_long volsize; /* Volume size in blocks */ - u_long blksize; /* Block size in bytes */ - } info; - struct audiopage au; /* Audio page info */ - struct cappage cap; /* Capabilities page info */ - struct audiopage aumask; /* Audio page mask */ - struct { /* Subchannel info */ - u_char void0; - u_char audio_status; - u_short data_length; - u_char data_format; - u_char control; - u_char track; - u_char indx; - u_long abslba; - u_long rellba; - } subchan; - struct changer *changer_info; /* Changer info */ - int slot; /* This lun's slot number */ - struct devstat *device_stats; /* Devstat parameters */ - u_int block_size; /* Blocksize currently used */ - u_char dummy; /* Use dummy writes */ - u_char speed; /* Select drive speed */ - u_int next_writeable_lba; /* Next writable position */ - struct wormio_prepare_track preptrack; /* Scratch region */ -}; diff --git a/sys/i386/isa/wfd.c b/sys/i386/isa/wfd.c deleted file mode 100644 index f8d9e60..0000000 --- a/sys/i386/isa/wfd.c +++ /dev/null @@ -1,727 +0,0 @@ -/* - * Copyright (c) 1997,1998 Junichi Satoh <junichi@astec.co.jp> - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer as - * the first lines of this file unmodified. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY Junichi Satoh ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL Junichi Satoh BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -/* - * ATAPI Floppy, LS-120 driver - */ - -#include "wdc.h" - -#include <sys/param.h> -#include <sys/systm.h> -#include <sys/kernel.h> -#include <sys/conf.h> -#include <sys/proc.h> -#include <sys/malloc.h> -#include <sys/buf.h> -#include <sys/devicestat.h> -#include <sys/disklabel.h> -#include <sys/diskslice.h> -#include <sys/cdio.h> - -#include <i386/isa/atapi.h> - -static d_open_t wfdopen; -static d_close_t wfdclose; -static d_ioctl_t wfdioctl; -static d_strategy_t wfdstrategy; - -#define CDEV_MAJOR 87 -#define BDEV_MAJOR 1 - -static struct cdevsw wfd_cdevsw = { - /* open */ wfdopen, - /* close */ wfdclose, - /* read */ physread, - /* write */ physwrite, - /* ioctl */ wfdioctl, - /* poll */ nopoll, - /* mmap */ nommap, - /* strategy */ wfdstrategy, - /* name */ "wfd", - /* maj */ CDEV_MAJOR, - /* dump */ nodump, - /* psize */ nopsize, - /* flags */ D_DISK, - /* bmaj */ BDEV_MAJOR -}; - -int wfdattach(struct atapi*, int, struct atapi_params*, int); - -#define NUNIT (NWDC*2) /* Max. number of devices */ -#define UNIT(d) ((minor(d) >> 3) & 3) /* Unit part of minor device number */ - -#define F_BOPEN 0x0001 /* The block device is opened */ -#define F_MEDIA_CHANGED 0x0002 /* The media have changed since open */ -#define F_DEBUG 0x0004 /* Print debug info */ - -/* - * LS-120 Capabilities and Mechanical Status Page - */ -struct cappage { - /* Mode data header */ - u_short data_length; - u_char medium_type; -#define MDT_UNKNOWN 0x00 -#define MDT_NO_DISC 0x70 -#define MDT_DOOR_OPEN 0x71 -#define MDT_FMT_ERROR 0x72 - -#define MDT_2DD_UN 0x10 -#define MDT_2DD 0x11 -#define MDT_2HD_UN 0x20 -#define MDT_2HD_12_98 0x22 -#define MDT_2HD_12 0x23 -#define MDT_2HD_144 0x24 -#define MDT_LS120 0x31 - - unsigned reserved0 :7; - unsigned wp :1; /* Write protect */ - u_char reserved1[4]; - - /* Capabilities page */ - unsigned page_code :6; /* Page code - Should be 0x5 */ -#define CAP_PAGE 0x05 - unsigned reserved1_6 :1; /* Reserved */ - unsigned ps :1; /* The device is capable of saving the page */ - u_char page_length; /* Page Length - Should be 0x1e */ - u_short transfer_rate; /* In kilobits per second */ - u_char heads, sectors; /* Number of heads, Number of sectors per track */ - u_short sector_size; /* Byes per sector */ - u_short cyls; /* Number of cylinders */ - u_char reserved10[10]; - u_char motor_delay; /* Motor off delay */ - u_char reserved21[7]; - u_short rpm; /* Rotations per minute */ - u_char reserved30[2]; -}; - -struct wfd { - struct atapi *ata; /* Controller structure */ - int unit; /* IDE bus drive unit */ - int lun; /* Logical device unit */ - int flags; /* Device state flags */ - int refcnt; /* The number of raw opens */ - int maxblks; /* transfer size limit */ - struct buf_queue_head buf_queue; /* Queue of i/o requests */ - struct atapi_params *param; /* Drive parameters table */ - struct cappage cap; /* Capabilities page info */ - char description[80]; /* Device description */ - struct diskslices *dk_slices; /* virtual drives */ - - struct devstat device_stats; -}; - -static struct wfd *wfdtab[NUNIT]; /* Drive info by unit number */ -static int wfdnlun = 0; /* Number of configured drives */ - -static void wfd_start (struct wfd *t); -static void wfd_done (struct wfd *t, struct buf *bp, int resid, - struct atapires result); -static void wfd_error (struct wfd *t, struct atapires result); -static int wfd_request_wait (struct wfd *t, u_char cmd, u_char a1, u_char a2, - u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8, - u_char a9, char *addr, int count); -static void wfd_describe (struct wfd *t); -static int wfd_eject (struct wfd *t, int closeit); - -/* - * Dump the array in hexadecimal format for debugging purposes. - */ -static void wfd_dump (int lun, char *label, void *data, int len) -{ - u_char *p = data; - - printf ("wfd%d: %s %x", lun, label, *p++); - while (--len > 0) - printf ("-%x", *p++); - printf ("\n"); -} - -int -wfdattach (struct atapi *ata, int unit, struct atapi_params *ap, int debug) -{ - struct wfd *t; - struct atapires result; - int lun, i; - - if (wfdnlun >= NUNIT) { - printf ("wfd: too many units\n"); - return (0); - } - if (!atapi_request_immediate) { - printf("wfd: configuration error, ATAPI core code not present!\n"); - printf("wfd: check `options ATAPI_STATIC' in your kernel config file!\n"); - return (0); - } - t = malloc (sizeof (struct wfd), M_TEMP, M_NOWAIT); - if (! t) { - printf ("wfd: out of memory\n"); - return (0); - } - wfdtab[wfdnlun] = t; - bzero (t, sizeof (struct wfd)); - bufq_init(&t->buf_queue); - t->ata = ata; - t->unit = unit; - lun = t->lun = wfdnlun; - t->param = ap; - t->flags = F_MEDIA_CHANGED; - t->refcnt = 0; - if (debug) { - t->flags |= F_DEBUG; - /* Print params. */ - wfd_dump (t->lun, "info", ap, sizeof *ap); - } - - /* Get drive capabilities. */ - /* Do it twice to avoid the stale media changed state. */ - for (i = 0; i < 2; i++) { - result = atapi_request_immediate (ata, unit, ATAPI_MODE_SENSE, - 0, CAP_PAGE, 0, 0, 0, 0, - sizeof (t->cap) >> 8, sizeof (t->cap), - 0, 0, 0, 0, 0, 0, 0, (char*) &t->cap, sizeof (t->cap)); - } - - if (result.code == RES_ERR && - (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION) - result = atapi_request_immediate (ata, unit, ATAPI_MODE_SENSE, - 0, CAP_PAGE, 0, 0, 0, 0, sizeof (t->cap) >> 8, - sizeof (t->cap), 0, 0, 0, 0, 0, 0, 0, - (char*) &t->cap, sizeof (t->cap)); - - /* Some drives have shorter capabilities page. */ - if (result.code == RES_UNDERRUN) - result.code = 0; - - if (result.code == 0) { - wfd_describe (t); - if (t->flags & F_DEBUG) - wfd_dump (t->lun, "cap", &t->cap, sizeof t->cap); - } else - return -1; - - /* - * The IOMEGA ZIP 100, at firmware 21.* and 23.* at least - * is known to lock up if transfers > 64 blocks are - * requested. - */ - if (!strcmp(ap->model, "IOMEGA ZIP 100 ATAPI")) { - printf("wfd%d: buggy Zip drive, 64-block transfer limit set\n", - t->lun); - t->maxblks = 64; - } else { - t->maxblks = 0; /* no limit */ - } - - - - make_dev(&wfd_cdevsw, dkmakeminor(t->lun, WHOLE_DISK_SLICE, RAW_PART), - UID_ROOT, GID_OPERATOR, 0640, "rwfd%d", t->lun); - - /* - * Export the drive to the devstat interface. - */ - devstat_add_entry(&t->device_stats, "wfd", - t->lun, t->cap.sector_size, - DEVSTAT_NO_ORDERED_TAGS, - DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_IDE, - DEVSTAT_PRIORITY_WFD); - wfdnlun++; - return (1); -} - -void wfd_describe (struct wfd *t) -{ - int no_print = 0; - - t->cap.cyls = ntohs (t->cap.cyls); - t->cap.sector_size = ntohs (t->cap.sector_size); - - printf ("wfd%d: ", t->lun); - switch (t->cap.medium_type) { - case MDT_UNKNOWN: - printf ("medium type unknown (no disk)"); - no_print = 1; - break; - case MDT_2DD_UN: - printf ("2DD(capacity unknown) floppy disk loaded"); - no_print = 1; - break; - case MDT_2DD: - printf ("720KB floppy disk loaded"); - break; - case MDT_2HD_UN: - printf ("2HD(capacity unknown) floppy disk loaded"); - no_print = 1; - break; - case MDT_2HD_12_98: - printf ("1.25MB(PC-9801 format) floppy disk loaded"); - break; - case MDT_2HD_12: - printf ("1.2MB floppy disk loaded"); - break; - case MDT_2HD_144: - printf ("1.44MB floppy disk loaded"); - break; - case MDT_LS120: - printf ("120MB floppy disk loaded"); - break; - case MDT_NO_DISC: - printf ("no disc inside"); - no_print = 1; - break; - case MDT_DOOR_OPEN: - printf ("door open"); - no_print = 1; - break; - case MDT_FMT_ERROR: - printf ("medium format error"); - no_print = 1; - break; - default: - printf ("medium type=0x%x", t->cap.medium_type); - break; - } - if (t->cap.wp) - printf(", write protected"); - printf ("\n"); - - if (!no_print) { - printf ("wfd%d: ", t->lun); - printf ("%u cyls", t->cap.cyls); - printf (", %u heads, %u S/T", t->cap.heads, t->cap.sectors); - printf (", %u B/S", t->cap.sector_size); - printf ("\n"); - } -} - -int wfdopen (dev_t dev, int flags, int fmt, struct proc *p) -{ - int lun = UNIT(dev); - struct wfd *t; - struct atapires result; - int errcode = 0; - struct disklabel label; - - /* Check that the device number is legal - * and the ATAPI driver is loaded. */ - if (lun >= wfdnlun || ! atapi_request_immediate) - return (ENXIO); - t = wfdtab[lun]; - - t->flags &= ~F_MEDIA_CHANGED; - /* Lock the media. */ - wfd_request_wait (t, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); - - /* Sense the media type */ - result = atapi_request_wait (t->ata, t->unit, ATAPI_MODE_SENSE, - 0, CAP_PAGE, 0, 0, 0, 0, - sizeof (t->cap) >> 8, sizeof (t->cap), - 0, 0, 0, 0, 0, 0, 0, - (char*) &t->cap, sizeof (t->cap)); - if (result.code) - printf ("wfd%d: Sense the media type is failed.\n", t->lun); - else { - t->cap.cyls = ntohs (t->cap.cyls); - t->cap.sector_size = ntohs (t->cap.sector_size); - } - - /* Build label for whole disk. */ - bzero(&label, sizeof label); - label.d_secsize = t->cap.sector_size; - label.d_nsectors = t->cap.sectors; - label.d_ntracks = t->cap.heads; - label.d_ncylinders = t->cap.cyls; - label.d_secpercyl = t->cap.heads * t->cap.sectors; - label.d_rpm = 720; - label.d_secperunit = label.d_secpercyl * t->cap.cyls; - - /* Initialize slice tables. */ - errcode = dsopen(dev, fmt, 0, &t->dk_slices, &label); - if (errcode != 0) - return errcode; - - t->flags |= F_BOPEN; - return (0); -} - -/* - * Close the device. Only called if we are the LAST - * occurence of an open device. - */ -int wfdclose (dev_t dev, int flags, int fmt, struct proc *p) -{ - int lun = UNIT(dev); - struct wfd *t = wfdtab[lun]; - - dsclose(dev, fmt, t->dk_slices); - if(!dsisopen(t->dk_slices)) { - /* If we were the last open of the entire device, release it. */ - if (! t->refcnt) - wfd_request_wait (t, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - t->flags &= ~F_BOPEN; - } - return (0); -} - -/* - * Actually translate the requested transfer into one the physical driver can - * understand. The transfer is described by a buf and will include only one - * physical transfer. - */ -void wfdstrategy (struct buf *bp) -{ - int lun = UNIT(bp->b_dev); - struct wfd *t = wfdtab[lun]; - int x; - - /* If it's a null transfer, return immediatly. */ - if (bp->b_bcount == 0) { - bp->b_resid = 0; - biodone (bp); - return; - } - - /* - * Do bounds checking, adjust transfer, and set b_pblkno. - */ - if (dscheck(bp, t->dk_slices) <= 0) { - biodone(bp); - return; - } - - x = splbio(); - - /* Place it in the queue of disk activities for this disk. */ - bufqdisksort (&t->buf_queue, bp); - - /* Tell the device to get going on the transfer if it's - * not doing anything, otherwise just wait for completion. */ - wfd_start (t); - splx(x); -} - -/* - * Look to see if there is a buf waiting for the device - * and that the device is not already busy. If both are true, - * It dequeues the buf and creates an ATAPI command to perform the - * transfer in the buf. - * The bufs are queued by the strategy routine (wfdstrategy). - * Must be called at the correct (splbio) level. - */ -static void wfd_start (struct wfd *t) -{ - struct buf *bp = bufq_first(&t->buf_queue); - u_long blkno, nblk; - u_char op_code; - long count; - int pxcount, pxnblk; - u_char *pxdest; - - - /* See if there is a buf to do and we are not already doing one. */ - if (! bp) - return; - - /* Unqueue the request. */ - bufq_remove(&t->buf_queue, bp); - - /* Tell devstat we are starting on the transaction */ - devstat_start_transaction(&t->device_stats); - - /* We have a buf, now we should make a command - * First, translate the block to absolute and put it in terms of the - * logical blocksize of the device. */ - blkno = bp->b_pblkno / (t->cap.sector_size / 512); - nblk = (bp->b_bcount + (t->cap.sector_size - 1)) / t->cap.sector_size; - - if ((t->maxblks == 0) || (nblk <= t->maxblks)) { - - if(bp->b_flags & B_READ) { - op_code = ATAPI_READ_BIG; - count = bp->b_bcount; - } else { - op_code = ATAPI_WRITE_BIG; - count = -bp->b_bcount; - } - - /* only one transfer */ - (int)bp->b_driver1 = 0; - (int)bp->b_driver2 = 0; - atapi_request_callback (t->ata, t->unit, op_code, 0, - blkno>>24, blkno>>16, blkno>>8, blkno, - 0, nblk>>8, nblk, 0, 0, - 0, 0, 0, 0, 0, - (u_char*) bp->b_data, count, - (void*)wfd_done, t, bp); - } else { - - /* - * We can't handle this request in a single - * read/write operation. Instead, queue a set of - * transfers, and record the number of transfers - * and the running residual in the b_driver - * fields of the bp. - */ - - if(bp->b_flags & B_READ) { - op_code = ATAPI_READ_BIG; - } else { - op_code = ATAPI_WRITE_BIG; - } - - /* calculate number of transfers */ - (int)bp->b_driver1 = (nblk - 1) / t->maxblks; - (int)bp->b_driver2 = 0; - - pxdest = (u_char *)bp->b_data; - pxcount = bp->b_bcount; - - /* construct partial transfer requests */ - while (nblk > 0) { - pxnblk = min(nblk, t->maxblks); - count = min(pxcount, t->maxblks * t->cap.sector_size); - - atapi_request_callback(t->ata, t->unit, op_code, 0, - blkno>>24, blkno>>16, blkno>>8, - blkno, 0, pxnblk>>8, pxnblk, - 0, 0, 0, 0, 0, 0, 0, - pxdest, - (bp->b_flags & B_READ) ? - count : -count, - (void*)wfd_done, t, bp); - nblk -= pxnblk; - pxcount -= count; - pxdest += count; - blkno += pxnblk; - } - } -} - -static void wfd_done (struct wfd *t, struct buf *bp, int resid, - struct atapires result) -{ - - if (result.code) { - wfd_error (t, result); - bp->b_error = EIO; - bp->b_flags |= B_ERROR; - } else - (int)bp->b_driver2 += resid; - /* - * We can't call biodone until all outstanding - * transfer fragments are handled. If one hits - * an error, we will be returning an error, but - * only when all are complete. - */ - if (((int)bp->b_driver1)-- <= 0) { - bp->b_resid = (int)bp->b_driver2; - devstat_end_transaction_buf(&t->device_stats, bp); - biodone (bp); - } - - wfd_start (t); -} - -static void wfd_error (struct wfd *t, struct atapires result) -{ - if (result.code != RES_ERR) - return; - switch (result.error & AER_SKEY) { - case AER_SK_NOT_READY: - if (result.error & ~AER_SKEY) { - /* Not Ready */ - printf ("wfd%d: not ready\n", t->lun); - return; - } - /* Tray open. */ - if (! (t->flags & F_MEDIA_CHANGED)) - printf ("wfd%d: tray open\n", t->lun); - t->flags |= F_MEDIA_CHANGED; - return; - - case AER_SK_UNIT_ATTENTION: - /* Media changed. */ - if (! (t->flags & F_MEDIA_CHANGED)) - printf ("wfd%d: media changed\n", t->lun); - t->flags |= F_MEDIA_CHANGED; - return; - - case AER_SK_ILLEGAL_REQUEST: - /* Unknown command or invalid command arguments. */ - if (t->flags & F_DEBUG) - printf ("wfd%d: invalid command\n", t->lun); - return; - } - printf ("wfd%d: i/o error, status=%b, error=%b\n", t->lun, - result.status, ARS_BITS, result.error, AER_BITS); -} - -static int wfd_request_wait (struct wfd *t, u_char cmd, u_char a1, u_char a2, - u_char a3, u_char a4, u_char a5, u_char a6, u_char a7, u_char a8, - u_char a9, char *addr, int count) -{ - struct atapires result; - - result = atapi_request_wait (t->ata, t->unit, cmd, - a1, a2, a3, a4, a5, a6, a7, a8, a9, 0, 0, 0, 0, 0, 0, - addr, count); - if (result.code) { - wfd_error (t, result); - return (EIO); - } - return (0); -} - -/* - * Perform special action on behalf of the user. - * Knows about the internals of this device - */ -int wfdioctl (dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) -{ - int lun = UNIT(dev); - struct wfd *t = wfdtab[lun]; - int error = 0; - - error = dsioctl(dev, cmd, addr, flag, &t->dk_slices); - if (error != ENOIOCTL) - return (error); - - if (t->flags & F_MEDIA_CHANGED) - switch (cmd) { - case CDIOCSETDEBUG: - case CDIOCCLRDEBUG: - case CDIOCRESET: - /* These ops are media change transparent. */ - break; - default: - /* Lock the media. */ - wfd_request_wait (t, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); - break; - } - switch (cmd) { - case CDIOCSETDEBUG: - error = suser(p); - if (error) - return (error); - t->flags |= F_DEBUG; - atapi_debug (t->ata, 1); - return 0; - case CDIOCCLRDEBUG: - error = suser(p); - if (error) - return (error); - t->flags &= ~F_DEBUG; - atapi_debug (t->ata, 0); - return 0; - case CDIOCRESET: - error = suser(p); - if (error) - return (error); - return wfd_request_wait (t, ATAPI_TEST_UNIT_READY, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - case CDIOCEJECT: - /* Don't allow eject if the device is opened - * by somebody (not us) in block mode. */ - if ((t->flags & F_BOPEN) && t->refcnt) - return (EBUSY); - return wfd_eject (t, 0); - case CDIOCCLOSE: - if ((t->flags & F_BOPEN) && t->refcnt) - return (0); - return wfd_eject (t, 1); - default: - return (ENOTTY); - } - return (error); -} - -static int wfd_eject (struct wfd *t, int closeit) -{ - struct atapires result; - - /* Try to stop the disc. */ - result = atapi_request_wait (t->ata, t->unit, ATAPI_START_STOP, - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - if (result.code == RES_ERR && - ((result.error & AER_SKEY) == AER_SK_NOT_READY || - (result.error & AER_SKEY) == AER_SK_UNIT_ATTENTION)) { - int err; - - if (!closeit) - return (0); - /* - * The disc was unloaded. - * Load it (close tray). - * Read the table of contents. - */ - err = wfd_request_wait (t, ATAPI_START_STOP, - 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0); - if (err) - return (err); - - /* Lock the media. */ - wfd_request_wait (t, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0); - - return (0); - } - - if (result.code) { - wfd_error (t, result); - return (EIO); - } - - if (closeit) - return (0); - - /* Give it some time to stop spinning. */ - tsleep ((caddr_t)&lbolt, PRIBIO, "wfdej1", 0); - tsleep ((caddr_t)&lbolt, PRIBIO, "wfdej2", 0); - - /* Unlock. */ - wfd_request_wait (t, ATAPI_PREVENT_ALLOW, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); - - /* Eject. */ - t->flags |= F_MEDIA_CHANGED; - return wfd_request_wait (t, ATAPI_START_STOP, - 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0); -} - -static void wfd_drvinit(void *unused) -{ - cdevsw_add(&wfd_cdevsw); -} - -SYSINIT(wfddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,wfd_drvinit,NULL) diff --git a/sys/i386/isa/wst.c b/sys/i386/isa/wst.c deleted file mode 100644 index 6ba3762..0000000 --- a/sys/i386/isa/wst.c +++ /dev/null @@ -1,774 +0,0 @@ -/*- - * Copyright (c) 1998 Søren Schmidt - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer, - * without modification, immediately at the beginning of the file. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD$ - */ - -#include "wdc.h" -#include "opt_ddb.h" - -#include <sys/param.h> -#include <sys/systm.h> -#include <sys/kernel.h> -#include <sys/conf.h> -#include <sys/malloc.h> -#include <sys/buf.h> -#include <sys/mtio.h> -#include <machine/clock.h> -#include <i386/isa/atapi.h> - -static d_open_t wstopen; -static d_close_t wstclose; -static d_ioctl_t wstioctl; -static d_strategy_t wststrategy; - -#define CDEV_MAJOR 90 - -static struct cdevsw wst_cdevsw = { - /* open */ wstopen, - /* close */ wstclose, - /* read */ physread, - /* write */ physwrite, - /* ioctl */ wstioctl, - /* poll */ nopoll, - /* mmap */ nommap, - /* strategy */ wststrategy, - /* name */ "wst", - /* maj */ CDEV_MAJOR, - /* dump */ nodump, - /* psize */ nopsize, - /* flags */ 0, - /* bmaj */ -1 -}; - -static unsigned int wst_total = 0; - -#define NUNIT (NWDC*2) -#define UNIT(d) ((minor(d) >> 3) & 3) - -#define WST_OPEN 0x0001 /* The device is opened */ -#define WST_MEDIA_CHANGED 0x0002 /* The media have changed */ -#define WST_DATA_WRITTEN 0x0004 /* Data has been written */ -#define WST_FM_WRITTEN 0x0008 /* Filemark has been written */ -#define WST_DEBUG 0x0010 /* Print debug info */ -#define WST_CTL_WARN 0x0020 /* Have we warned about CTL wrong? */ - -/* ATAPI tape commands not in std ATAPI command set */ -#define ATAPI_TAPE_REWIND 0x01 -#define ATAPI_TAPE_REQUEST_SENSE 0x03 -#define ATAPI_TAPE_READ_CMD 0x08 -#define ATAPI_TAPE_WRITE_CMD 0x0a -#define ATAPI_TAPE_WEOF 0x10 -#define WEOF_WRITE_MASK 0x01 -#define ATAPI_TAPE_SPACE_CMD 0x11 -#define SP_FM 0x01 -#define SP_EOD 0x03 -#define ATAPI_TAPE_ERASE 0x19 -#define ATAPI_TAPE_MODE_SENSE 0x1a -#define ATAPI_TAPE_LOAD_UNLOAD 0x1b -#define LU_LOAD_MASK 0x01 -#define LU_RETENSION_MASK 0x02 -#define LU_EOT_MASK 0x04 - -#define DSC_POLL_INTERVAL 10 - -/* - * MODE SENSE parameter header - */ -struct wst_header { - u_char data_length; /* Total length of data */ - u_char medium_type; /* Medium type (if any) */ - u_char dsp; /* Device specific parameter */ - u_char bdl; /* Block Descriptor Length */ -}; - -/* - * ATAPI tape drive Capabilities and Mechanical Status Page - */ -#define ATAPI_TAPE_CAP_PAGE 0x2a - -struct wst_cappage { - u_int page_code :6; /* Page code == 0x2a */ - u_int reserved1_67 :2; - u_char page_length; /* Page Length == 0x12 */ - u_char reserved2; - u_char reserved3; - u_int readonly :1; /* Read Only Mode */ - u_int reserved4_1234 :4; - u_int reverse :1; /* Supports reverse direction */ - u_int reserved4_67 :2; - u_int reserved5_012 :3; - u_int eformat :1; /* Supports ERASE formatting */ - u_int reserved5_4 :1; - u_int qfa :1; /* Supports QFA formats */ - u_int reserved5_67 :2; - u_int lock :1; /* Supports locking media */ - u_int locked :1; /* The media is locked */ - u_int prevent :1; /* Defaults to prevent state */ - u_int eject :1; /* Supports eject */ - u_int disconnect :1; /* Can break request > ctl */ - u_int reserved6_5 :1; - u_int ecc :1; /* Supports error correction */ - u_int compress :1; /* Supports data compression */ - u_int reserved7_0 :1; - u_int blk512 :1; /* Supports 512b block size */ - u_int blk1024 :1; /* Supports 1024b block size */ - u_int reserved7_3456 :4; - u_int slowb :1; /* Restricts byte count */ - u_short max_speed; /* Supported speed in KBps */ - u_short max_defects; /* Max stored defect entries */ - u_short ctl; /* Continuous Transfer Limit */ - u_short speed; /* Current Speed, in KBps */ - u_short buffer_size; /* Buffer Size, in 512 bytes */ - u_char reserved18; - u_char reserved19; -}; - -/* - * REQUEST SENSE structure - */ -struct wst_reqsense { - u_int error_code :7; /* Current or deferred errors */ - u_int valid :1; /* Follows QIC-157C */ - u_char reserved1; /* Segment Number - Reserved */ - u_int sense_key :4; /* Sense Key */ - u_int reserved2_4 :1; /* Reserved */ - u_int ili :1; /* Incorrect Length Indicator */ - u_int eom :1; /* End Of Medium */ - u_int filemark :1; /* Filemark */ - u_int info __attribute__((packed)); /* Cmd specific info */ - u_char asl; /* Additional sense length (n-7) */ - u_int command_specific; /* Additional cmd specific info */ - u_char asc; /* Additional Sense Code */ - u_char ascq; /* Additional Sense Code Qualifier */ - u_char replaceable_unit_code; /* Field Replaceable Unit Code */ - u_int sk_specific1 :7; /* Sense Key Specific */ - u_int sksv :1; /* Sense Key Specific info valid */ - u_char sk_specific2; /* Sense Key Specific */ - u_char sk_specific3; /* Sense Key Specific */ - u_char pad[2]; /* Padding */ -}; - -struct wst { - struct atapi *ata; /* Controller structure */ - int unit; /* IDE bus drive unit */ - int lun; /* Logical device unit */ - int flags; /* Device state flags */ - int blksize; /* Block size (512 | 1024) */ - struct buf_queue_head buf_queue; /* Queue of i/o requests */ - struct atapi_params *param; /* Drive parameters table */ - struct wst_header header; /* MODE SENSE param header */ - struct wst_cappage cap; /* Capabilities page info */ -}; - -static struct wst *wsttab[NUNIT]; /* Drive info by unit number */ -static int wstnlun = 0; /* Number of config'd drives */ - -int wstattach(struct atapi *ata, int unit, struct atapi_params *ap, int debug); -static int wst_sense(struct wst *t); -static void wst_describe(struct wst *t); -static void wst_poll_dsc(struct wst *t); -static void wst_start(struct wst *t); -static void wst_done(struct wst *t, struct buf *bp, int resid, struct atapires result); -static int wst_error(struct wst *t, struct atapires result); -static void wst_drvinit(void *unused); -static int wst_space_cmd(struct wst *t, u_char function, u_int count); -static int wst_write_filemark(struct wst *t, u_char function); -static int wst_erase(struct wst *t); -static int wst_load_unload(struct wst *t, u_char finction); -static int wst_rewind(struct wst *t); -static void wst_reset(struct wst *t); - -#ifdef DDB -void wst_dump(int lun, char *label, void *data, int len); - -void -wst_dump(int lun, char *label, void *data, int len) -{ - u_char *p = data; - - printf("wst%d: %s %x", lun, label, *p++); - while(--len > 0) - printf("-%x", *p++); - printf("\n"); -} -#endif - -int -wstattach(struct atapi *ata, int unit, struct atapi_params *ap, int debug) -{ - struct wst *t; - int lun; - - if (wstnlun >= NUNIT) { - printf("wst: too many units\n"); - return(-1); - } - if (!atapi_request_immediate) { - printf("wst: configuration error, ATAPI core code not present!\n"); - printf( - "wst: check `options ATAPI_STATIC' in your kernel config file!\n"); - return(-1); - } - t = malloc(sizeof(struct wst), M_TEMP, M_NOWAIT); - if (!t) { - printf("wst: out of memory\n"); - return(-1); - } - wsttab[wstnlun] = t; - bzero(t, sizeof(struct wst)); - bufq_init(&t->buf_queue); - t->ata = ata; - t->unit = unit; - t->ata->use_dsc = 1; - lun = t->lun = wstnlun; - t->param = ap; - t->flags = WST_MEDIA_CHANGED | WST_DEBUG; - - if (wst_sense(t)) - return -1; - - wst_describe(t); - wstnlun++; - - make_dev(&wst_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0640, "rwst%d", t->lun); - return(1); -} - -static int -wst_sense(struct wst *t) -{ - struct atapires result; - int count; - char buffer[255]; - - /* Get drive capabilities, some drives needs this repeated */ - for (count = 0 ; count < 5 ; count++) { - result = atapi_request_immediate(t->ata, t->unit, - ATAPI_TAPE_MODE_SENSE, - 8, /* DBD = 1 no block descr */ - ATAPI_TAPE_CAP_PAGE, - sizeof(buffer)>>8, sizeof(buffer), - 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, buffer, sizeof(buffer)); - if (result.code == 0 || result.code == RES_UNDERRUN) - break; - } - - /* Some drives have shorter capabilities page. */ - if (result.code == RES_UNDERRUN) - result.code = 0; - - if (result.code == 0) { - bcopy(buffer, &t->header, sizeof(struct wst_header)); - bcopy(buffer+sizeof(struct wst_header), - &t->cap, sizeof(struct wst_cappage)); - if (t->cap.page_code != ATAPI_TAPE_CAP_PAGE) - return 1; - t->cap.max_speed = ntohs(t->cap.max_speed); - t->cap.max_defects = ntohs(t->cap.max_defects); - t->cap.ctl = ntohs(t->cap.ctl); - t->cap.speed = ntohs(t->cap.speed); - t->cap.buffer_size = ntohs(t->cap.buffer_size); - t->blksize = (t->cap.blk512 ? 512 : (t->cap.blk1024 ? 1024 : 0)); - return 0; - } - return 1; -} - -static void -wst_describe(struct wst *t) -{ - printf("wst%d: ", t->lun); - switch (t->header.medium_type) { - case 0x00: printf("Drive empty"); break; - case 0x17: printf("Travan 1 (400 Mbyte) media"); break; - case 0xb6: printf("Travan 4 (4 Gbyte) media"); break; - default: printf("Unknown media (0x%x)", t->header.medium_type); - } - if (t->cap.readonly) printf(", readonly"); - if (t->cap.reverse) printf(", reverse"); - if (t->cap.eformat) printf(", eformat"); - if (t->cap.qfa) printf(", qfa"); - if (t->cap.lock) printf(", lock"); - if (t->cap.locked) printf(", locked"); - if (t->cap.prevent) printf(", prevent"); - if (t->cap.eject) printf(", eject"); - if (t->cap.disconnect) printf(", disconnect"); - if (t->cap.ecc) printf(", ecc"); - if (t->cap.compress) printf(", compress"); - if (t->cap.blk512) printf(", 512b"); - if (t->cap.blk1024) printf(", 1024b"); - if (t->cap.slowb) printf(", slowb"); - printf("\nwst%d: ", t->lun); - printf("Max speed=%dKb/s, ", t->cap.max_speed); - printf("Transfer limit=%d blocks, ", t->cap.ctl); - printf("Buffer size=%d blocks", t->cap.buffer_size); - printf("\n"); -} - -int -wstopen(dev_t dev, int flags, int fmt, struct proc *p) -{ - int lun = UNIT(dev); - struct wst *t; - - /* Check that the device number and that the ATAPI driver is loaded. */ - if (lun >= wstnlun || !atapi_request_immediate) { - printf("ENXIO lun=%d, wstnlun=%d, im=%p\n", - lun, wstnlun, (void *)atapi_request_immediate); - return(ENXIO); - } - t = wsttab[lun]; - if (t->flags == WST_OPEN) - return EBUSY; - if (wst_sense(t)) - printf("wst%d: Sense media type failed\n", t->lun); - t->flags &= ~WST_MEDIA_CHANGED; - t->flags &= ~(WST_DATA_WRITTEN | WST_FM_WRITTEN); - t->flags |= WST_OPEN; - return(0); -} - -int -wstclose(dev_t dev, int flags, int fmt, struct proc *p) -{ - int lun = UNIT(dev); - struct wst *t = wsttab[lun]; - - /* Flush buffers, some drives fail here, but they should report ctl = 0 */ - if (t->cap.ctl && (t->flags & WST_DATA_WRITTEN)) - wst_write_filemark(t, 0); - - /* Write filemark if data written to tape */ - if ((t->flags & (WST_DATA_WRITTEN | WST_FM_WRITTEN)) == WST_DATA_WRITTEN) - wst_write_filemark(t, WEOF_WRITE_MASK); - - /* If minor is even rewind on close */ - if (!(minor(dev) & 0x01)) - wst_rewind(t); - - t->flags &= ~WST_OPEN; - if (t->flags & WST_DEBUG) - printf("wst%d: %ud total bytes transferred\n", t->lun, wst_total); - t->flags &= ~WST_CTL_WARN; - return(0); -} - -void -wststrategy(struct buf *bp) -{ - int lun = UNIT(bp->b_dev); - struct wst *t = wsttab[lun]; - int x; - - /* If it's a null transfer, return immediatly. */ - if (bp->b_bcount == 0) { - bp->b_resid = 0; - biodone(bp); - return; - } - - /* Check for != blocksize requests */ - if (bp->b_bcount % t->blksize) { - printf("wst%d: bad request, must be multiple of %d\n", lun, t->blksize); - bp->b_error = EIO; - bp->b_flags |= B_ERROR; - biodone(bp); - return; - } - - if (bp->b_bcount > t->blksize*t->cap.ctl) { - if ((t->flags & WST_CTL_WARN) == 0) { - printf("wst%d: WARNING: CTL exceeded %ld>%d\n", - lun, bp->b_bcount, t->blksize*t->cap.ctl); - t->flags |= WST_CTL_WARN; - } - } - - x = splbio(); - wst_total += bp->b_bcount; - bufq_insert_tail(&t->buf_queue, bp); - wst_start(t); - splx(x); -} - -static void -wst_poll_dsc(struct wst *t) -{ - /* We should use a final timeout here SOS XXX */ - if (!(inb(t->ata->port + AR_STATUS) & ARS_DSC)) { - timeout((timeout_t*)wst_poll_dsc, t, DSC_POLL_INTERVAL); - return; - } - t->ata->wait_for_dsc = 0; - wakeup((caddr_t)t); -} - -static void -wst_start(struct wst *t) -{ - struct buf *bp = bufq_first(&t->buf_queue); - u_long blk_count; - u_char op_code; - long byte_count; - - if (!bp) - return; - - if (t->ata->wait_for_dsc) - printf("wst%d: ERROR! allready waiting for DSC\n", t->lun); - - /* Sleep waiting for a ready drive (DSC) */ - if (t->ata->use_dsc && !(inb(t->ata->port + AR_STATUS) & ARS_DSC)) { - t->ata->wait_for_dsc = 1; - timeout((timeout_t*)wst_poll_dsc, t, DSC_POLL_INTERVAL); - tsleep((caddr_t) t, 0, "wstdsc", 0); - } - - bufq_remove(&t->buf_queue, bp); - blk_count = bp->b_bcount / t->blksize; - - if (bp->b_flags & B_READ) { - op_code = ATAPI_TAPE_READ_CMD; - byte_count = bp->b_bcount; - } else { - op_code = ATAPI_TAPE_WRITE_CMD; - t->flags |= WST_DATA_WRITTEN; - byte_count = -bp->b_bcount; - } - - atapi_request_callback(t->ata, t->unit, op_code, 1, - blk_count>>16, blk_count>>8, blk_count, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - (u_char*) bp->b_data, byte_count, - (void*)wst_done, t, bp); -} - -static void -wst_done(struct wst *t, struct buf *bp, int resid, - struct atapires result) -{ - if (result.code) { - printf("wst_done: "); - wst_error(t, result); - bp->b_error = EIO; - bp->b_flags |= B_ERROR; - } - else - bp->b_resid = resid; - - biodone(bp); - /*wst_start(t);*/ -} - -static int -wst_error(struct wst *t, struct atapires result) -{ - struct wst_reqsense sense; - - if (result.code != RES_ERR) { - printf("wst%d: ERROR code=%d, status=%b, error=%b\n", t->lun, - result.code, result.status, ARS_BITS, result.error, AER_BITS); - return 1; - } - - if ((result.error & AER_SKEY) && (result.status & ARS_CHECK)) { - atapi_request_immediate(t->ata, t->unit, - ATAPI_TAPE_REQUEST_SENSE, - 0, 0, 0, sizeof(sense), - 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, (char*) &sense, sizeof(struct wst_reqsense)); - /*wst_dump(t->lun, "req_sense", &sense, sizeof(struct wst_reqsense));*/ - } - switch (result.error & AER_SKEY) { - case AER_SK_NOT_READY: - if (result.error & ~AER_SKEY) { - if (t->flags & WST_DEBUG) - printf("wst%d: not ready\n", t->lun); - break; - } - if (!(t->flags & WST_MEDIA_CHANGED)) - if (t->flags & WST_DEBUG) - printf("wst%d: no media\n", t->lun); - t->flags |= WST_MEDIA_CHANGED; - break; - - case AER_SK_BLANK_CHECK: - if (t->flags & WST_DEBUG) - printf("wst%d: EOD encountered\n", t->lun); - break; - - case AER_SK_MEDIUM_ERROR: - if (t->flags & WST_DEBUG) - printf("wst%d: nonrecovered data error\n", t->lun); - break; - - case AER_SK_HARDWARE_ERROR: - if (t->flags & WST_DEBUG) - printf("wst%d: nonrecovered hardware error\n", t->lun); - break; - - case AER_SK_ILLEGAL_REQUEST: - if (t->flags & WST_DEBUG) - printf("wst%d: invalid command\n", t->lun); - break; - - case AER_SK_UNIT_ATTENTION: - if (!(t->flags & WST_MEDIA_CHANGED)) - printf("wst%d: media changed\n", t->lun); - t->flags |= WST_MEDIA_CHANGED; - break; - - case AER_SK_DATA_PROTECT: - if (t->flags & WST_DEBUG) - printf("wst%d: reading read protected data\n", t->lun); - break; - - case AER_SK_ABORTED_COMMAND: - if (t->flags & WST_DEBUG) - printf("wst%d: command aborted\n", t->lun); - break; - - case AER_SK_MISCOMPARE: - if (t->flags & WST_DEBUG) - printf("wst%d: data don't match medium\n", t->lun); - break; - - default: - printf("wst%d: i/o error, status=%b, error=%b\n", t->lun, - result.status, ARS_BITS, result.error, AER_BITS); - } - printf("total=%u ERR=%x len=%ld ASC=%x ASCQ=%x\n", - wst_total, sense.error_code, (long)ntohl(sense.info), - sense.asc, sense.ascq); - return 1; -} - -int -wstioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) -{ - int lun = UNIT(dev); - int error = 0; - struct wst *t = wsttab[lun]; - - switch (cmd) { - case MTIOCGET: - { - struct mtget *g = (struct mtget *) addr; - - bzero(g, sizeof(struct mtget)); - g->mt_type = 7; - g->mt_density = 1; - g->mt_blksiz = t->blksize; - g->mt_comp = t->cap.compress; - g->mt_density0 = 0; g->mt_density1 = 0; - g->mt_density2 = 0; g->mt_density3 = 0; - g->mt_blksiz0 = 0; g->mt_blksiz1 = 0; - g->mt_blksiz2 = 0; g->mt_blksiz3 = 0; - g->mt_comp0 = 0; g->mt_comp1 = 0; - g->mt_comp2 = 0; g->mt_comp3 = 0; - break; - } - case MTIOCTOP: - { - int i; - struct mtop *mt = (struct mtop *)addr; - - switch ((short) (mt->mt_op)) { - case MTWEOF: - for (i=0; i < mt->mt_count && !error; i++) - error = wst_write_filemark(t, WEOF_WRITE_MASK); - break; - case MTFSF: - if (mt->mt_count) - error = wst_space_cmd(t, SP_FM, mt->mt_count); - break; - case MTBSF: - if (mt->mt_count) - error = wst_space_cmd(t, SP_FM, -(mt->mt_count)); - break; - case MTFSR: - error = EINVAL; break; - case MTBSR: - error = EINVAL; break; - case MTREW: - error = wst_rewind(t); - break; - case MTOFFL: -#if 1 /* Misuse as a reset func for now */ - wst_reset(t); - wst_sense(t); - wst_describe(t); -#else - if (error = wst_rewind(t)) - break; - error = wst_load_unload(t, !LU_LOAD_MASK); -#endif - break; - case MTNOP: - error = wst_write_filemark(t, 0); - break; - case MTCACHE: - error = EINVAL; break; - case MTNOCACHE: - error = EINVAL; break; - case MTSETBSIZ: - error = EINVAL; break; - case MTSETDNSTY: - error = EINVAL; break; - case MTERASE: - error = wst_erase(t); - break; - case MTEOD: - error = wst_space_cmd(t, SP_EOD, 0); - break; - case MTCOMP: - error = EINVAL; break; - case MTRETENS: - error = wst_load_unload(t, LU_RETENSION_MASK|LU_LOAD_MASK); - break; - default: - error = EINVAL; - } - return error; - } - default: - return(ENOTTY); - } - return(error); -} - -static int -wst_space_cmd(struct wst *t, u_char function, u_int count) -{ - struct atapires result; - - result = atapi_request_wait(t->ata, t->unit, - ATAPI_TAPE_SPACE_CMD, function, - count>>16, count>>8, count, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0); - if (result.code) { - printf("wst_space_cmd: "); - wst_error(t, result); - return EIO; - } - return 0; -} - -static int -wst_write_filemark(struct wst *t, u_char function) -{ - struct atapires result; - - if (function) { - if (t->flags & WST_FM_WRITTEN) - t->flags &= ~WST_DATA_WRITTEN; - else - t->flags |= WST_FM_WRITTEN; - } - result = atapi_request_wait(t->ata, t->unit, - ATAPI_TAPE_WEOF, 0, 0, 0, function, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0); - if (result.code) { - printf("wst_write_filemark: "); - wst_error(t, result); - return EIO; - } - return 0; -} - -static int -wst_load_unload(struct wst *t, u_char function) -{ - struct atapires result; - - result = atapi_request_wait(t->ata, t->unit, - ATAPI_TAPE_LOAD_UNLOAD, 0, 0, 0, function, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0); - if (result.code) { - printf("wst_load_unload: "); - wst_error(t, result); - return EIO; - } - return 0; -} - -static int -wst_erase(struct wst *t) -{ - int error; - struct atapires result; - - error = wst_rewind(t); - if (error) - return error; - result = atapi_request_wait(t->ata, t->unit, - ATAPI_TAPE_ERASE, 3, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - NULL, 0); - if (result.code) { - printf("wst_erase: "); - wst_error(t, result); - return EIO; - } - return 0; -} - -static int -wst_rewind(struct wst *t) -{ - struct atapires result; - - result = atapi_request_wait(t->ata, t->unit, - ATAPI_TAPE_REWIND, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - NULL, 0); - if (result.code) { - printf("wst_rewind: "); - wst_error(t, result); - return EIO; - } - return 0; -} - -static void -wst_reset(struct wst *t) -{ - outb(t->ata->port + AR_DRIVE, ARD_DRIVE1); - DELAY(30); - outb(t->ata->port + AR_COMMAND, 0x08); - DELAY(30); -} - -static void -wst_drvinit(void *unused) -{ - cdevsw_add(&wst_cdevsw); -} - -SYSINIT(wstdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,wst_drvinit,NULL) |