Home | History | Annotate | Line # | Download | only in dev
ct.c revision 1.37
      1  1.37   tsutsui /*	$NetBSD: ct.c,v 1.37 2003/11/17 14:37:59 tsutsui Exp $	*/
      2  1.22   thorpej 
      3  1.22   thorpej /*-
      4  1.22   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  1.22   thorpej  * All rights reserved.
      6  1.22   thorpej  *
      7  1.22   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.22   thorpej  * by Jason R. Thorpe.
      9  1.22   thorpej  *
     10  1.22   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.22   thorpej  * modification, are permitted provided that the following conditions
     12  1.22   thorpej  * are met:
     13  1.22   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.22   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.22   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.22   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.22   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.22   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.22   thorpej  *    must display the following acknowledgement:
     20  1.22   thorpej  *	This product includes software developed by the NetBSD
     21  1.22   thorpej  *	Foundation, Inc. and its contributors.
     22  1.22   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.22   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.22   thorpej  *    from this software without specific prior written permission.
     25  1.22   thorpej  *
     26  1.22   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.22   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.22   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.22   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.22   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.22   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.22   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.22   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.22   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.22   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.22   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.22   thorpej  */
     38   1.9       cgd 
     39   1.1       cgd /*
     40   1.8   mycroft  * Copyright (c) 1982, 1990, 1993
     41   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51  1.36       agc  * 3. Neither the name of the University nor the names of its contributors
     52   1.1       cgd  *    may be used to endorse or promote products derived from this software
     53   1.1       cgd  *    without specific prior written permission.
     54   1.1       cgd  *
     55   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65   1.1       cgd  * SUCH DAMAGE.
     66   1.1       cgd  *
     67   1.9       cgd  *	@(#)ct.c	8.2 (Berkeley) 1/12/94
     68   1.1       cgd  */
     69   1.1       cgd 
     70   1.1       cgd /*
     71   1.1       cgd  * CS80 cartridge tape driver (9144, 88140, 9145)
     72   1.1       cgd  *
     73   1.1       cgd  * Reminder:
     74   1.1       cgd  *	C_CC bit (character count option) when used in the CS/80 command
     75   1.1       cgd  *	'set options' will cause the tape not to stream.
     76   1.1       cgd  *
     77   1.1       cgd  * TODO:
     78   1.1       cgd  *	make filesystem compatible
     79   1.1       cgd  *	make block mode work according to mtio(4) spec. (if possible)
     80   1.1       cgd  *	merge with cs80 disk driver
     81   1.1       cgd  *	finish support of 9145
     82   1.1       cgd  */
     83  1.28  gmcgarry 
     84  1.28  gmcgarry #include <sys/cdefs.h>
     85  1.37   tsutsui __KERNEL_RCSID(0, "$NetBSD: ct.c,v 1.37 2003/11/17 14:37:59 tsutsui Exp $");
     86   1.1       cgd 
     87   1.6   mycroft #include <sys/param.h>
     88  1.20    scottr #include <sys/systm.h>
     89   1.6   mycroft #include <sys/buf.h>
     90  1.20    scottr #include <sys/conf.h>
     91  1.20    scottr #include <sys/device.h>
     92   1.6   mycroft #include <sys/ioctl.h>
     93   1.6   mycroft #include <sys/mtio.h>
     94  1.20    scottr #include <sys/proc.h>
     95   1.6   mycroft #include <sys/tprintf.h>
     96  1.19   thorpej 
     97  1.19   thorpej #include <hp300/dev/hpibvar.h>
     98   1.1       cgd 
     99   1.6   mycroft #include <hp300/dev/ctreg.h>
    100   1.1       cgd 
    101   1.1       cgd /* number of eof marks to remember */
    102   1.1       cgd #define EOFS	128
    103   1.1       cgd 
    104   1.1       cgd struct	ct_softc {
    105  1.19   thorpej 	struct	device sc_dev;
    106  1.19   thorpej 	int	sc_slave;		/* HP-IB slave ID */
    107  1.19   thorpej 	int	sc_punit;		/* physical unit */
    108   1.1       cgd 	struct	ct_iocmd sc_ioc;
    109   1.1       cgd 	struct	ct_rscmd sc_rsc;
    110   1.1       cgd 	struct	ct_stat sc_stat;
    111   1.1       cgd 	struct	ct_ssmcmd sc_ssmc;
    112   1.1       cgd 	struct	ct_srcmd sc_src;
    113   1.1       cgd 	struct	ct_soptcmd sc_soptc;
    114   1.1       cgd 	struct	ct_ulcmd sc_ul;
    115   1.1       cgd 	struct	ct_wfmcmd sc_wfm;
    116   1.1       cgd 	struct	ct_clearcmd sc_clear;
    117  1.29   hannken 	struct	bufq_state sc_tab;
    118  1.24   thorpej 	int	sc_active;
    119   1.1       cgd 	struct	buf *sc_bp;
    120  1.19   thorpej 	struct	buf sc_bufstore;	/* XXX */
    121   1.1       cgd 	int	sc_blkno;
    122   1.1       cgd 	int	sc_cmd;
    123   1.1       cgd 	int	sc_resid;
    124   1.1       cgd 	char	*sc_addr;
    125   1.1       cgd 	int	sc_flags;
    126   1.1       cgd 	short	sc_type;
    127   1.1       cgd 	tpr_t	sc_tpr;
    128  1.19   thorpej 	struct	hpibqueue sc_hq;	/* entry on hpib job queue */
    129   1.1       cgd 	int	sc_eofp;
    130   1.1       cgd 	int	sc_eofs[EOFS];
    131  1.19   thorpej };
    132   1.1       cgd 
    133   1.1       cgd /* flags */
    134   1.1       cgd #define	CTF_OPEN	0x01
    135   1.1       cgd #define	CTF_ALIVE	0x02
    136   1.1       cgd #define	CTF_WRT		0x04
    137   1.1       cgd #define	CTF_CMD		0x08
    138   1.1       cgd #define	CTF_IO		0x10
    139   1.1       cgd #define	CTF_BEOF	0x20
    140   1.1       cgd #define	CTF_AEOF	0x40
    141   1.1       cgd #define	CTF_EOT		0x80
    142   1.1       cgd #define	CTF_STATWAIT	0x100
    143   1.1       cgd #define CTF_CANSTREAM	0x200
    144   1.1       cgd #define	CTF_WRTTN	0x400
    145   1.1       cgd 
    146  1.19   thorpej int	ctmatch __P((struct device *, struct cfdata *, void *));
    147  1.19   thorpej void	ctattach __P((struct device *, struct device *, void *));
    148  1.19   thorpej 
    149  1.32   thorpej CFATTACH_DECL(ct, sizeof(struct ct_softc),
    150  1.32   thorpej     ctmatch, ctattach, NULL, NULL);
    151  1.19   thorpej 
    152  1.23   thorpej extern struct cfdriver ct_cd;
    153  1.19   thorpej 
    154  1.30   gehenna dev_type_open(ctopen);
    155  1.30   gehenna dev_type_close(ctclose);
    156  1.30   gehenna dev_type_read(ctread);
    157  1.30   gehenna dev_type_write(ctwrite);
    158  1.30   gehenna dev_type_ioctl(ctioctl);
    159  1.30   gehenna dev_type_strategy(ctstrategy);
    160  1.30   gehenna 
    161  1.30   gehenna const struct bdevsw ct_bdevsw = {
    162  1.30   gehenna 	ctopen, ctclose, ctstrategy, ctioctl, nodump, nosize, D_TAPE
    163  1.30   gehenna };
    164  1.30   gehenna 
    165  1.30   gehenna const struct cdevsw ct_cdevsw = {
    166  1.30   gehenna 	ctopen, ctclose, ctread, ctwrite, ctioctl,
    167  1.33  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
    168  1.30   gehenna };
    169  1.30   gehenna 
    170  1.19   thorpej int	ctident __P((struct device *, struct ct_softc *,
    171  1.19   thorpej 	    struct hpibbus_attach_args *));
    172  1.19   thorpej 
    173  1.19   thorpej void	ctreset __P((struct ct_softc *));
    174  1.19   thorpej void	ctaddeof __P((struct ct_softc *));
    175  1.19   thorpej void	ctustart __P((struct ct_softc *));
    176  1.19   thorpej void	cteof __P((struct ct_softc *, struct buf *));
    177  1.19   thorpej void	ctdone __P((struct ct_softc *, struct buf *));
    178  1.19   thorpej 
    179  1.19   thorpej void	ctstart __P((void *));
    180  1.19   thorpej void	ctgo __P((void *));
    181  1.19   thorpej void	ctintr __P((void *));
    182  1.19   thorpej 
    183  1.19   thorpej void	ctcommand __P((dev_t, int, int));
    184  1.19   thorpej 
    185   1.1       cgd struct	ctinfo {
    186   1.1       cgd 	short	hwid;
    187   1.1       cgd 	short	punit;
    188   1.1       cgd 	char	*desc;
    189   1.1       cgd } ctinfo[] = {
    190  1.20    scottr 	{ CT7946ID,	1,	"7946A"	},
    191  1.20    scottr 	{ CT7912PID,	1,	"7912P"	},
    192  1.20    scottr 	{ CT7914PID,	1,	"7914P"	},
    193  1.20    scottr 	{ CT9144ID,	0,	"9144"	},
    194  1.20    scottr 	{ CT9145ID,	0,	"9145"	},
    195  1.26    kleink 	{ CT35401ID,	0,	"35401A"},
    196   1.1       cgd };
    197   1.1       cgd int	nctinfo = sizeof(ctinfo) / sizeof(ctinfo[0]);
    198   1.1       cgd 
    199   1.1       cgd #define	CT_NOREW	4
    200   1.1       cgd #define	CT_STREAM	8
    201   1.1       cgd #define	UNIT(x)		(minor(x) & 3)
    202   1.1       cgd #define	ctpunit(x)	((x) & 7)
    203   1.1       cgd 
    204   1.1       cgd #ifdef DEBUG
    205   1.1       cgd int ctdebug = 0;
    206   1.1       cgd #define CDB_FILES	0x01
    207   1.1       cgd #define CT_BSF		0x02
    208   1.1       cgd #endif
    209   1.1       cgd 
    210  1.13   thorpej int
    211  1.19   thorpej ctmatch(parent, match, aux)
    212  1.19   thorpej 	struct device *parent;
    213  1.19   thorpej 	struct cfdata *match;
    214  1.19   thorpej 	void *aux;
    215   1.1       cgd {
    216  1.19   thorpej 	struct hpibbus_attach_args *ha = aux;
    217  1.13   thorpej 
    218  1.19   thorpej 	return (ctident(parent, NULL, ha));
    219  1.13   thorpej }
    220  1.13   thorpej 
    221  1.13   thorpej void
    222  1.19   thorpej ctattach(parent, self, aux)
    223  1.19   thorpej 	struct device *parent, *self;
    224  1.19   thorpej 	void *aux;
    225  1.19   thorpej {
    226  1.19   thorpej 	struct ct_softc *sc = (struct ct_softc *)self;
    227  1.19   thorpej 	struct hpibbus_attach_args *ha = aux;
    228  1.19   thorpej 
    229  1.19   thorpej 	if (ctident(parent, sc, ha) == 0) {
    230  1.19   thorpej 		printf("\n%s: didn't respond to describe command!\n",
    231  1.19   thorpej 		    sc->sc_dev.dv_xname);
    232  1.19   thorpej 		return;
    233  1.19   thorpej 	}
    234  1.19   thorpej 
    235  1.19   thorpej 	sc->sc_slave = ha->ha_slave;
    236  1.19   thorpej 	sc->sc_punit = ha->ha_punit;
    237  1.19   thorpej 
    238  1.29   hannken 	bufq_alloc(&sc->sc_tab, BUFQ_FCFS);
    239  1.13   thorpej 
    240  1.19   thorpej 	/* Initialize hpib job queue entry. */
    241  1.19   thorpej 	sc->sc_hq.hq_softc = sc;
    242  1.19   thorpej 	sc->sc_hq.hq_slave = sc->sc_slave;
    243  1.19   thorpej 	sc->sc_hq.hq_start = ctstart;
    244  1.19   thorpej 	sc->sc_hq.hq_go = ctgo;
    245  1.19   thorpej 	sc->sc_hq.hq_intr = ctintr;
    246  1.13   thorpej 
    247  1.19   thorpej 	ctreset(sc);
    248   1.1       cgd 	sc->sc_flags |= CTF_ALIVE;
    249   1.1       cgd }
    250   1.1       cgd 
    251  1.13   thorpej int
    252  1.19   thorpej ctident(parent, sc, ha)
    253  1.19   thorpej 	struct device *parent;
    254  1.19   thorpej 	struct ct_softc *sc;
    255  1.19   thorpej 	struct hpibbus_attach_args *ha;
    256   1.1       cgd {
    257   1.1       cgd 	struct ct_describe desc;
    258   1.1       cgd 	u_char stat, cmd[3];
    259   1.1       cgd 	char name[7];
    260  1.19   thorpej 	int i, id, n, type, canstream;
    261  1.19   thorpej 
    262  1.19   thorpej 	type = canstream = 0;
    263   1.1       cgd 
    264  1.19   thorpej 	/* Verify that we have a CS80 device. */
    265  1.19   thorpej 	if ((ha->ha_id & 0x200) == 0)
    266  1.19   thorpej 		return (0);
    267  1.19   thorpej 
    268  1.19   thorpej 	/* Is it one of the tapes we support? */
    269  1.19   thorpej 	for (id = 0; id < nctinfo; id++)
    270  1.19   thorpej 		if (ha->ha_id == ctinfo[id].hwid)
    271   1.1       cgd 			break;
    272  1.19   thorpej 	if (id == nctinfo)
    273  1.19   thorpej 		return (0);
    274  1.19   thorpej 
    275  1.19   thorpej 	ha->ha_punit = ctinfo[id].punit;
    276   1.1       cgd 
    277   1.1       cgd 	/*
    278  1.19   thorpej 	 * So far, so good.  Get drive parameters.  Note command
    279  1.19   thorpej 	 * is always issued to unit 0.
    280   1.1       cgd 	 */
    281   1.1       cgd 	cmd[0] = C_SUNIT(0);
    282   1.1       cgd 	cmd[1] = C_SVOL(0);
    283   1.1       cgd 	cmd[2] = C_DESC;
    284  1.19   thorpej 	hpibsend(parent->dv_unit, ha->ha_slave, C_CMD, cmd, sizeof(cmd));
    285  1.19   thorpej 	hpibrecv(parent->dv_unit, ha->ha_slave, C_EXEC, &desc, 37);
    286  1.19   thorpej 	hpibrecv(parent->dv_unit, ha->ha_slave, C_QSTAT, &stat, sizeof(stat));
    287  1.19   thorpej 
    288  1.27  gmcgarry 	memset(name, 0, sizeof(name));
    289  1.19   thorpej 	if (stat == 0) {
    290  1.19   thorpej 		n = desc.d_name;
    291   1.1       cgd 		for (i = 5; i >= 0; i--) {
    292   1.1       cgd 			name[i] = (n & 0xf) + '0';
    293   1.1       cgd 			n >>= 4;
    294   1.1       cgd 		}
    295   1.1       cgd 	}
    296  1.19   thorpej 
    297  1.19   thorpej 	switch (ha->ha_id) {
    298   1.1       cgd 	case CT7946ID:
    299  1.27  gmcgarry 		if (memcmp(name, "079450", 6) == 0)
    300  1.19   thorpej 			return (0);		/* not really a 7946 */
    301   1.1       cgd 		/* fall into... */
    302   1.1       cgd 	case CT9144ID:
    303   1.1       cgd 	case CT9145ID:
    304  1.26    kleink 	case CT35401ID:
    305  1.19   thorpej 		type = CT9144;
    306  1.19   thorpej 		canstream = 1;
    307   1.1       cgd 		break;
    308   1.1       cgd 
    309   1.1       cgd 	case CT7912PID:
    310   1.1       cgd 	case CT7914PID:
    311  1.19   thorpej 		type = CT88140;
    312   1.1       cgd 		break;
    313   1.1       cgd 	}
    314  1.19   thorpej 
    315  1.19   thorpej 	if (sc != NULL) {
    316  1.19   thorpej 		sc->sc_type = type;
    317  1.19   thorpej 		sc->sc_flags = canstream ? CTF_CANSTREAM : 0;
    318  1.17  christos 		printf(": %s %stape\n", ctinfo[id].desc,
    319  1.19   thorpej 		    canstream ? "streaming " : "");
    320  1.19   thorpej 	}
    321  1.19   thorpej 
    322  1.19   thorpej 	return (1);
    323   1.1       cgd }
    324   1.1       cgd 
    325  1.19   thorpej void
    326  1.19   thorpej ctreset(sc)
    327  1.19   thorpej 	struct ct_softc *sc;
    328   1.1       cgd {
    329  1.19   thorpej 	int ctlr, slave;
    330   1.1       cgd 	u_char stat;
    331   1.1       cgd 
    332  1.19   thorpej 	ctlr = sc->sc_dev.dv_parent->dv_unit;
    333  1.19   thorpej 	slave = sc->sc_slave;
    334  1.19   thorpej 
    335   1.1       cgd 	sc->sc_clear.unit = C_SUNIT(sc->sc_punit);
    336   1.1       cgd 	sc->sc_clear.cmd = C_CLEAR;
    337  1.19   thorpej 	hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
    338  1.19   thorpej 	hpibswait(ctlr, slave);
    339  1.19   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    340  1.19   thorpej 
    341   1.1       cgd 	sc->sc_src.unit = C_SUNIT(CTCTLR);
    342   1.1       cgd 	sc->sc_src.nop = C_NOP;
    343   1.1       cgd 	sc->sc_src.cmd = C_SREL;
    344   1.1       cgd 	sc->sc_src.param = C_REL;
    345  1.19   thorpej 	hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
    346  1.19   thorpej 	hpibswait(ctlr, slave);
    347  1.19   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    348  1.19   thorpej 
    349   1.1       cgd 	sc->sc_ssmc.unit = C_SUNIT(sc->sc_punit);
    350   1.1       cgd 	sc->sc_ssmc.cmd = C_SSM;
    351   1.1       cgd 	sc->sc_ssmc.refm = REF_MASK;
    352   1.1       cgd 	sc->sc_ssmc.fefm = FEF_MASK;
    353   1.1       cgd 	sc->sc_ssmc.aefm = AEF_MASK;
    354   1.1       cgd 	sc->sc_ssmc.iefm = IEF_MASK;
    355  1.19   thorpej 	hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
    356  1.19   thorpej 	hpibswait(ctlr, slave);
    357  1.19   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    358  1.19   thorpej 
    359   1.1       cgd 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
    360   1.1       cgd 	sc->sc_soptc.nop = C_NOP;
    361   1.1       cgd 	sc->sc_soptc.cmd = C_SOPT;
    362   1.1       cgd 	sc->sc_soptc.opt = C_SPAR;
    363  1.19   thorpej 	hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
    364  1.19   thorpej 	hpibswait(ctlr, slave);
    365  1.19   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    366   1.1       cgd }
    367   1.1       cgd 
    368   1.1       cgd /*ARGSUSED*/
    369  1.19   thorpej int
    370  1.35      fvdl ctopen(dev, flag, type, p)
    371   1.1       cgd 	dev_t dev;
    372   1.1       cgd 	int flag, type;
    373  1.35      fvdl 	struct proc *p;
    374   1.1       cgd {
    375  1.19   thorpej 	struct ct_softc *sc;
    376   1.1       cgd 	u_char stat;
    377  1.19   thorpej 	int cc, ctlr, slave;
    378  1.19   thorpej 
    379  1.19   thorpej 	if (UNIT(dev) >= ct_cd.cd_ndevs ||
    380  1.19   thorpej 	    (sc = ct_cd.cd_devs[UNIT(dev)]) == NULL ||
    381  1.19   thorpej 	    (sc->sc_flags & CTF_ALIVE) == 0)
    382  1.19   thorpej 		return (ENXIO);
    383   1.1       cgd 
    384   1.1       cgd 	if (sc->sc_flags & CTF_OPEN)
    385  1.19   thorpej 		return (EBUSY);
    386  1.19   thorpej 
    387  1.19   thorpej 	ctlr = sc->sc_dev.dv_parent->dv_unit;
    388  1.19   thorpej 	slave = sc->sc_slave;
    389  1.19   thorpej 
    390   1.1       cgd 	sc->sc_soptc.unit = C_SUNIT(sc->sc_punit);
    391   1.1       cgd 	sc->sc_soptc.nop = C_NOP;
    392   1.1       cgd 	sc->sc_soptc.cmd = C_SOPT;
    393   1.1       cgd 	if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
    394   1.1       cgd 		sc->sc_soptc.opt = C_SPAR | C_IMRPT;
    395   1.1       cgd 	else
    396   1.1       cgd 		sc->sc_soptc.opt = C_SPAR;
    397  1.19   thorpej 
    398  1.37   tsutsui 	/*
    399   1.1       cgd 	 * Check the return of hpibsend() and hpibswait().
    400   1.1       cgd 	 * Drive could be loading/unloading a tape. If not checked,
    401  1.37   tsutsui 	 * driver hangs.
    402   1.1       cgd 	 */
    403  1.19   thorpej 	cc = hpibsend(ctlr, slave, C_CMD, &sc->sc_soptc, sizeof(sc->sc_soptc));
    404   1.1       cgd 	if (cc != sizeof(sc->sc_soptc))
    405  1.19   thorpej 		return (EBUSY);
    406  1.19   thorpej 
    407  1.19   thorpej 	hpibswait(ctlr, slave);
    408  1.19   thorpej 	cc = hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    409   1.1       cgd 	if (cc != sizeof(stat))
    410   1.1       cgd 		return(EBUSY);
    411  1.19   thorpej 
    412  1.35      fvdl 	sc->sc_tpr = tprintf_open(p);
    413   1.1       cgd 	sc->sc_flags |= CTF_OPEN;
    414   1.1       cgd 	return(0);
    415   1.1       cgd }
    416   1.1       cgd 
    417   1.1       cgd /*ARGSUSED*/
    418  1.19   thorpej int
    419  1.35      fvdl ctclose(dev, flag, fmt, p)
    420   1.1       cgd 	dev_t dev;
    421  1.19   thorpej 	int flag, fmt;
    422  1.35      fvdl 	struct proc *p;
    423   1.1       cgd {
    424  1.19   thorpej 	struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
    425   1.1       cgd 
    426   1.1       cgd 	if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
    427   1.1       cgd 	    (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
    428   1.1       cgd 		ctcommand(dev, MTWEOF, 2);
    429   1.1       cgd 		ctcommand(dev, MTBSR, 1);
    430   1.1       cgd 		if (sc->sc_eofp == EOFS - 1)
    431   1.1       cgd 			sc->sc_eofs[EOFS - 1]--;
    432   1.1       cgd 		else
    433   1.1       cgd 			sc->sc_eofp--;
    434   1.1       cgd #ifdef DEBUG
    435   1.1       cgd 		if(ctdebug & CT_BSF)
    436  1.17  christos 			printf("%s: ctclose backup eofs prt %d blk %d\n",
    437  1.19   thorpej 			       sc->sc_dev.dv_xname, sc->sc_eofp,
    438  1.13   thorpej 			       sc->sc_eofs[sc->sc_eofp]);
    439   1.1       cgd #endif
    440   1.1       cgd 	}
    441   1.1       cgd 	if ((minor(dev) & CT_NOREW) == 0)
    442   1.1       cgd 		ctcommand(dev, MTREW, 1);
    443   1.1       cgd 	sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
    444   1.1       cgd 	tprintf_close(sc->sc_tpr);
    445   1.1       cgd #ifdef DEBUG
    446   1.1       cgd 	if (ctdebug & CDB_FILES)
    447  1.17  christos 		printf("ctclose: flags %x\n", sc->sc_flags);
    448   1.1       cgd #endif
    449   1.1       cgd 	return(0);	/* XXX */
    450   1.1       cgd }
    451   1.1       cgd 
    452  1.19   thorpej void
    453   1.1       cgd ctcommand(dev, cmd, cnt)
    454   1.1       cgd 	dev_t dev;
    455   1.8   mycroft 	int cmd;
    456  1.20    scottr 	int cnt;
    457   1.1       cgd {
    458  1.19   thorpej 	struct ct_softc *sc = ct_cd.cd_devs[UNIT(dev)];
    459  1.20    scottr 	struct buf *bp = &sc->sc_bufstore;
    460  1.20    scottr 	struct buf *nbp = 0;
    461   1.1       cgd 
    462   1.1       cgd 	if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
    463   1.1       cgd 		cnt = sc->sc_eofs[EOFS - 1] - cnt;
    464   1.1       cgd 		ctcommand(dev, MTREW, 1);
    465   1.1       cgd 		ctcommand(dev, MTFSF, cnt);
    466   1.1       cgd 		cnt = 2;
    467   1.1       cgd 		cmd = MTBSR;
    468   1.1       cgd 	}
    469   1.1       cgd 
    470   1.1       cgd 	if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
    471   1.1       cgd 		cnt = 1;
    472   1.1       cgd 		cmd = MTREW;
    473   1.1       cgd 	}
    474   1.1       cgd 
    475   1.1       cgd 	sc->sc_flags |= CTF_CMD;
    476   1.1       cgd 	sc->sc_bp = bp;
    477   1.1       cgd 	sc->sc_cmd = cmd;
    478   1.1       cgd 	bp->b_dev = dev;
    479   1.1       cgd 	if (cmd == MTFSF) {
    480   1.1       cgd 		nbp = (struct buf *)geteblk(MAXBSIZE);
    481  1.25   thorpej 		bp->b_data = nbp->b_data;
    482   1.1       cgd 		bp->b_bcount = MAXBSIZE;
    483   1.1       cgd 	}
    484  1.14    scottr 
    485  1.14    scottr 	while (cnt-- > 0) {
    486  1.14    scottr 		bp->b_flags = B_BUSY;
    487  1.14    scottr 		if (cmd == MTBSF) {
    488  1.14    scottr 			sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
    489  1.14    scottr 			sc->sc_eofp--;
    490  1.14    scottr #ifdef DEBUG
    491  1.14    scottr 			if (ctdebug & CT_BSF)
    492  1.17  christos 				printf("%s: backup eof pos %d blk %d\n",
    493  1.37   tsutsui 				    sc->sc_dev.dv_xname, sc->sc_eofp,
    494  1.14    scottr 				    sc->sc_eofs[sc->sc_eofp]);
    495   1.1       cgd #endif
    496  1.14    scottr 		}
    497  1.14    scottr 		ctstrategy(bp);
    498  1.24   thorpej 		biowait(bp);
    499   1.1       cgd 	}
    500   1.1       cgd 	bp->b_flags = 0;
    501   1.1       cgd 	sc->sc_flags &= ~CTF_CMD;
    502   1.1       cgd 	if (nbp)
    503   1.1       cgd 		brelse(nbp);
    504   1.1       cgd }
    505   1.1       cgd 
    506   1.3   mycroft void
    507   1.1       cgd ctstrategy(bp)
    508  1.20    scottr 	struct buf *bp;
    509   1.1       cgd {
    510  1.20    scottr 	int s, unit;
    511  1.19   thorpej 	struct ct_softc *sc;
    512   1.1       cgd 
    513   1.1       cgd 	unit = UNIT(bp->b_dev);
    514  1.19   thorpej 	sc = ct_cd.cd_devs[unit];
    515  1.19   thorpej 
    516   1.1       cgd 	s = splbio();
    517  1.29   hannken 	BUFQ_PUT(&sc->sc_tab, bp);
    518  1.24   thorpej 	if (sc->sc_active == 0) {
    519  1.24   thorpej 		sc->sc_active = 1;
    520  1.19   thorpej 		ctustart(sc);
    521   1.1       cgd 	}
    522   1.1       cgd 	splx(s);
    523   1.1       cgd }
    524   1.1       cgd 
    525  1.19   thorpej void
    526  1.19   thorpej ctustart(sc)
    527  1.19   thorpej 	struct ct_softc *sc;
    528   1.1       cgd {
    529  1.20    scottr 	struct buf *bp;
    530   1.1       cgd 
    531  1.29   hannken 	bp = BUFQ_PEEK(&sc->sc_tab);
    532  1.25   thorpej 	sc->sc_addr = bp->b_data;
    533   1.1       cgd 	sc->sc_resid = bp->b_bcount;
    534  1.19   thorpej 	if (hpibreq(sc->sc_dev.dv_parent, &sc->sc_hq))
    535  1.19   thorpej 		ctstart(sc);
    536   1.1       cgd }
    537   1.1       cgd 
    538  1.19   thorpej void
    539  1.19   thorpej ctstart(arg)
    540  1.19   thorpej 	void *arg;
    541   1.1       cgd {
    542  1.19   thorpej 	struct ct_softc *sc = arg;
    543  1.20    scottr 	struct buf *bp;
    544  1.19   thorpej 	int i, ctlr, slave;
    545   1.1       cgd 
    546  1.19   thorpej 	ctlr = sc->sc_dev.dv_parent->dv_unit;
    547  1.19   thorpej 	slave = sc->sc_slave;
    548  1.19   thorpej 
    549  1.29   hannken 	bp = BUFQ_PEEK(&sc->sc_tab);
    550   1.1       cgd 	if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
    551   1.1       cgd 		switch(sc->sc_cmd) {
    552   1.1       cgd 		case MTFSF:
    553   1.1       cgd 			bp->b_flags |= B_READ;
    554   1.1       cgd 			goto mustio;
    555   1.1       cgd 
    556   1.1       cgd 		case MTBSF:
    557   1.1       cgd 			goto gotaddr;
    558   1.1       cgd 
    559   1.1       cgd 		case MTOFFL:
    560   1.1       cgd 			sc->sc_blkno = 0;
    561   1.1       cgd 			sc->sc_ul.unit = C_SUNIT(sc->sc_punit);
    562   1.1       cgd 			sc->sc_ul.cmd = C_UNLOAD;
    563  1.19   thorpej 			hpibsend(ctlr, slave, C_CMD, &sc->sc_ul,
    564  1.19   thorpej 			    sizeof(sc->sc_ul));
    565   1.1       cgd 			break;
    566   1.1       cgd 
    567   1.1       cgd 		case MTWEOF:
    568   1.1       cgd 			sc->sc_blkno++;
    569   1.1       cgd 			sc->sc_flags |= CTF_WRT;
    570   1.1       cgd 			sc->sc_wfm.unit = C_SUNIT(sc->sc_punit);
    571   1.1       cgd 			sc->sc_wfm.cmd = C_WFM;
    572  1.19   thorpej 			hpibsend(ctlr, slave, C_CMD, &sc->sc_wfm,
    573  1.19   thorpej 			    sizeof(sc->sc_wfm));
    574  1.19   thorpej 			ctaddeof(sc);
    575   1.1       cgd 			break;
    576   1.1       cgd 
    577   1.1       cgd 		case MTBSR:
    578   1.1       cgd 			sc->sc_blkno--;
    579   1.1       cgd 			goto gotaddr;
    580   1.1       cgd 
    581   1.1       cgd 		case MTFSR:
    582   1.1       cgd 			sc->sc_blkno++;
    583   1.1       cgd 			goto gotaddr;
    584   1.1       cgd 
    585   1.1       cgd 		case MTREW:
    586   1.1       cgd 			sc->sc_blkno = 0;
    587   1.1       cgd #ifdef DEBUG
    588   1.1       cgd 			if(ctdebug & CT_BSF)
    589  1.17  christos 				printf("%s: clearing eofs\n",
    590  1.19   thorpej 				    sc->sc_dev.dv_xname);
    591   1.1       cgd #endif
    592   1.1       cgd 			for (i=0; i<EOFS; i++)
    593   1.1       cgd 				sc->sc_eofs[i] = 0;
    594   1.1       cgd 			sc->sc_eofp = 0;
    595   1.1       cgd 
    596   1.1       cgd gotaddr:
    597   1.1       cgd 			sc->sc_ioc.saddr = C_SADDR;
    598   1.1       cgd 			sc->sc_ioc.addr0 = 0;
    599   1.1       cgd 			sc->sc_ioc.addr = sc->sc_blkno;
    600   1.1       cgd 			sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
    601   1.1       cgd 			sc->sc_ioc.nop2 = C_NOP;
    602   1.1       cgd 			sc->sc_ioc.slen = C_SLEN;
    603   1.1       cgd 			sc->sc_ioc.len = 0;
    604   1.1       cgd 			sc->sc_ioc.nop3 = C_NOP;
    605   1.1       cgd 			sc->sc_ioc.cmd = C_READ;
    606  1.19   thorpej 			hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc,
    607  1.19   thorpej 			    sizeof(sc->sc_ioc));
    608   1.1       cgd 			break;
    609   1.1       cgd 		}
    610  1.19   thorpej 	} else {
    611   1.1       cgd mustio:
    612   1.1       cgd 		if ((bp->b_flags & B_READ) &&
    613   1.1       cgd 		    sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
    614   1.1       cgd #ifdef DEBUG
    615   1.1       cgd 			if (ctdebug & CDB_FILES)
    616  1.19   thorpej 				printf("ctstart: before flags %x\n",
    617  1.19   thorpej 				    sc->sc_flags);
    618   1.1       cgd #endif
    619   1.1       cgd 			if (sc->sc_flags & CTF_BEOF) {
    620   1.1       cgd 				sc->sc_flags &= ~CTF_BEOF;
    621   1.1       cgd 				sc->sc_flags |= CTF_AEOF;
    622   1.1       cgd #ifdef DEBUG
    623   1.1       cgd 				if (ctdebug & CDB_FILES)
    624  1.19   thorpej 					printf("ctstart: after flags %x\n",
    625  1.19   thorpej 					    sc->sc_flags);
    626   1.1       cgd #endif
    627   1.1       cgd 			}
    628   1.1       cgd 			bp->b_resid = bp->b_bcount;
    629  1.19   thorpej 			ctdone(sc, bp);
    630   1.1       cgd 			return;
    631  1.37   tsutsui 		}
    632   1.1       cgd 		sc->sc_flags |= CTF_IO;
    633   1.1       cgd 		sc->sc_ioc.unit = C_SUNIT(sc->sc_punit);
    634   1.1       cgd 		sc->sc_ioc.saddr = C_SADDR;
    635   1.1       cgd 		sc->sc_ioc.addr0 = 0;
    636   1.1       cgd 		sc->sc_ioc.addr = sc->sc_blkno;
    637   1.1       cgd 		sc->sc_ioc.nop2 = C_NOP;
    638   1.1       cgd 		sc->sc_ioc.slen = C_SLEN;
    639   1.1       cgd 		sc->sc_ioc.len = sc->sc_resid;
    640   1.1       cgd 		sc->sc_ioc.nop3 = C_NOP;
    641   1.1       cgd 		if (bp->b_flags & B_READ)
    642   1.1       cgd 			sc->sc_ioc.cmd = C_READ;
    643   1.1       cgd 		else {
    644   1.1       cgd 			sc->sc_ioc.cmd = C_WRITE;
    645   1.1       cgd 			sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
    646   1.1       cgd 		}
    647  1.19   thorpej 		hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
    648   1.1       cgd 	}
    649  1.19   thorpej 	hpibawait(ctlr);
    650   1.1       cgd }
    651   1.1       cgd 
    652  1.19   thorpej void
    653  1.19   thorpej ctgo(arg)
    654  1.19   thorpej 	void *arg;
    655   1.1       cgd {
    656  1.19   thorpej 	struct ct_softc *sc = arg;
    657  1.20    scottr 	struct buf *bp;
    658  1.12   thorpej 	int rw;
    659   1.1       cgd 
    660  1.29   hannken 	bp = BUFQ_PEEK(&sc->sc_tab);
    661  1.12   thorpej 	rw = bp->b_flags & B_READ;
    662  1.19   thorpej 	hpibgo(sc->sc_dev.dv_parent->dv_unit, sc->sc_slave, C_EXEC,
    663  1.19   thorpej 	    sc->sc_addr, sc->sc_resid, rw, rw != 0);
    664   1.1       cgd }
    665   1.1       cgd 
    666   1.1       cgd /*
    667   1.1       cgd  * Hideous grue to handle EOF/EOT (mostly for reads)
    668   1.1       cgd  */
    669  1.19   thorpej void
    670   1.1       cgd cteof(sc, bp)
    671  1.20    scottr 	struct ct_softc *sc;
    672  1.20    scottr 	struct buf *bp;
    673   1.1       cgd {
    674   1.1       cgd 	long blks;
    675   1.1       cgd 
    676   1.1       cgd 	/*
    677   1.1       cgd 	 * EOT on a write is an error.
    678   1.1       cgd 	 */
    679   1.1       cgd 	if ((bp->b_flags & B_READ) == 0) {
    680   1.1       cgd 		bp->b_resid = bp->b_bcount;
    681   1.1       cgd 		bp->b_flags |= B_ERROR;
    682   1.1       cgd 		bp->b_error = ENOSPC;
    683   1.1       cgd 		sc->sc_flags |= CTF_EOT;
    684   1.1       cgd 		return;
    685   1.1       cgd 	}
    686   1.1       cgd 	/*
    687   1.1       cgd 	 * Use returned block position to determine how many blocks
    688   1.1       cgd 	 * we really read and update b_resid.
    689   1.1       cgd 	 */
    690   1.1       cgd 	blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
    691   1.1       cgd #ifdef DEBUG
    692   1.1       cgd 	if (ctdebug & CDB_FILES)
    693  1.21    scottr 		printf("cteof: bc %ld oblk %d nblk %ld read %ld, resid %ld\n",
    694   1.1       cgd 		       bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
    695   1.1       cgd 		       blks, bp->b_bcount - CTKTOB(blks));
    696   1.1       cgd #endif
    697   1.1       cgd 	if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
    698   1.1       cgd 		blks = 0;
    699   1.1       cgd 		sc->sc_blkno++;
    700   1.1       cgd 	}
    701   1.1       cgd 	else {
    702   1.1       cgd 		sc->sc_blkno = sc->sc_stat.c_blk;
    703   1.1       cgd 	}
    704   1.1       cgd 	bp->b_resid = bp->b_bcount - CTKTOB(blks);
    705   1.1       cgd 	/*
    706   1.1       cgd 	 * If we are at physical EOV or were after an EOF,
    707   1.1       cgd 	 * we are now at logical EOT.
    708   1.1       cgd 	 */
    709   1.1       cgd 	if ((sc->sc_stat.c_aef & AEF_EOV) ||
    710   1.1       cgd 	    (sc->sc_flags & CTF_AEOF)) {
    711   1.1       cgd 		sc->sc_flags |= CTF_EOT;
    712   1.1       cgd 		sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
    713   1.1       cgd 	}
    714   1.1       cgd 	/*
    715   1.1       cgd 	 * If we were before an EOF or we have just completed a FSF,
    716   1.1       cgd 	 * we are now after EOF.
    717   1.1       cgd 	 */
    718   1.1       cgd 	else if ((sc->sc_flags & CTF_BEOF) ||
    719  1.20    scottr 		 ((sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF)) {
    720   1.1       cgd 		sc->sc_flags |= CTF_AEOF;
    721   1.1       cgd 		sc->sc_flags &= ~CTF_BEOF;
    722   1.1       cgd 	}
    723   1.1       cgd 	/*
    724   1.1       cgd 	 * Otherwise if we read something we are now before EOF
    725   1.1       cgd 	 * (and no longer after EOF).
    726   1.1       cgd 	 */
    727   1.1       cgd 	else if (blks) {
    728   1.1       cgd 		sc->sc_flags |= CTF_BEOF;
    729   1.1       cgd 		sc->sc_flags &= ~CTF_AEOF;
    730   1.1       cgd 	}
    731   1.1       cgd 	/*
    732   1.1       cgd 	 * Finally, if we didn't read anything we just passed an EOF
    733   1.1       cgd 	 */
    734   1.1       cgd 	else
    735   1.1       cgd 		sc->sc_flags |= CTF_AEOF;
    736   1.1       cgd #ifdef DEBUG
    737   1.1       cgd 	if (ctdebug & CDB_FILES)
    738  1.17  christos 		printf("cteof: leaving flags %x\n", sc->sc_flags);
    739   1.1       cgd #endif
    740   1.1       cgd }
    741   1.1       cgd 
    742  1.19   thorpej /* ARGSUSED */
    743  1.19   thorpej void
    744  1.15   thorpej ctintr(arg)
    745  1.15   thorpej 	void *arg;
    746   1.1       cgd {
    747  1.20    scottr 	struct ct_softc *sc = arg;
    748  1.20    scottr 	struct buf *bp;
    749   1.1       cgd 	u_char stat;
    750  1.19   thorpej 	int ctlr, slave, unit;
    751  1.19   thorpej 
    752  1.19   thorpej 	ctlr = sc->sc_dev.dv_parent->dv_unit;
    753  1.19   thorpej 	slave = sc->sc_slave;
    754  1.19   thorpej 	unit = sc->sc_dev.dv_unit;
    755   1.1       cgd 
    756  1.29   hannken 	bp = BUFQ_PEEK(&sc->sc_tab);
    757   1.1       cgd 	if (bp == NULL) {
    758  1.19   thorpej 		printf("%s: bp == NULL\n", sc->sc_dev.dv_xname);
    759   1.1       cgd 		return;
    760   1.1       cgd 	}
    761   1.1       cgd 	if (sc->sc_flags & CTF_IO) {
    762   1.1       cgd 		sc->sc_flags &= ~CTF_IO;
    763  1.19   thorpej 		if (hpibustart(ctlr))
    764  1.19   thorpej 			ctgo(sc);
    765   1.1       cgd 		return;
    766   1.1       cgd 	}
    767   1.1       cgd 	if ((sc->sc_flags & CTF_STATWAIT) == 0) {
    768  1.19   thorpej 		if (hpibpptest(ctlr, slave) == 0) {
    769   1.1       cgd 			sc->sc_flags |= CTF_STATWAIT;
    770  1.19   thorpej 			hpibawait(ctlr);
    771   1.1       cgd 			return;
    772   1.1       cgd 		}
    773   1.1       cgd 	} else
    774   1.1       cgd 		sc->sc_flags &= ~CTF_STATWAIT;
    775  1.19   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
    776   1.1       cgd #ifdef DEBUG
    777   1.1       cgd 	if (ctdebug & CDB_FILES)
    778  1.17  christos 		printf("ctintr: before flags %x\n", sc->sc_flags);
    779   1.1       cgd #endif
    780   1.1       cgd 	if (stat) {
    781   1.1       cgd 		sc->sc_rsc.unit = C_SUNIT(sc->sc_punit);
    782   1.1       cgd 		sc->sc_rsc.cmd = C_STATUS;
    783  1.19   thorpej 		hpibsend(ctlr, slave, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
    784  1.19   thorpej 		hpibrecv(ctlr, slave, C_EXEC, &sc->sc_stat,
    785  1.19   thorpej 		    sizeof(sc->sc_stat));
    786  1.19   thorpej 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
    787   1.1       cgd #ifdef DEBUG
    788   1.1       cgd 		if (ctdebug & CDB_FILES)
    789  1.21    scottr 			printf("ctintr: return stat 0x%x, A%x F%x blk %ld\n",
    790   1.1       cgd 			       stat, sc->sc_stat.c_aef,
    791   1.1       cgd 			       sc->sc_stat.c_fef, sc->sc_stat.c_blk);
    792   1.1       cgd #endif
    793   1.1       cgd 		if (stat == 0) {
    794   1.1       cgd 			if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
    795   1.1       cgd 				cteof(sc, bp);
    796  1.19   thorpej 				ctaddeof(sc);
    797   1.1       cgd 				goto done;
    798   1.1       cgd 			}
    799   1.1       cgd 			if (sc->sc_stat.c_fef & FEF_PF) {
    800  1.19   thorpej 				ctreset(sc);
    801  1.19   thorpej 				ctstart(sc);
    802   1.1       cgd 				return;
    803   1.1       cgd 			}
    804   1.1       cgd 			if (sc->sc_stat.c_fef & FEF_REXMT) {
    805  1.19   thorpej 				ctstart(sc);
    806   1.1       cgd 				return;
    807   1.1       cgd 			}
    808   1.1       cgd 			if (sc->sc_stat.c_aef & 0x5800) {
    809   1.1       cgd 				if (sc->sc_stat.c_aef & 0x4000)
    810   1.1       cgd 					tprintf(sc->sc_tpr,
    811  1.13   thorpej 						"%s: uninitialized media\n",
    812  1.19   thorpej 						sc->sc_dev.dv_xname);
    813   1.1       cgd 				if (sc->sc_stat.c_aef & 0x1000)
    814   1.1       cgd 					tprintf(sc->sc_tpr,
    815  1.13   thorpej 						"%s: not ready\n",
    816  1.19   thorpej 						sc->sc_dev.dv_xname);
    817   1.1       cgd 				if (sc->sc_stat.c_aef & 0x0800)
    818   1.1       cgd 					tprintf(sc->sc_tpr,
    819  1.13   thorpej 						"%s: write protect\n",
    820  1.19   thorpej 						sc->sc_dev.dv_xname);
    821   1.1       cgd 			} else {
    822  1.20    scottr 				printf("%s err: v%d u%d ru%d bn%ld, ",
    823  1.19   thorpej 				       sc->sc_dev.dv_xname,
    824   1.1       cgd 				       (sc->sc_stat.c_vu>>4)&0xF,
    825   1.1       cgd 				       sc->sc_stat.c_vu&0xF,
    826   1.1       cgd 				       sc->sc_stat.c_pend,
    827   1.1       cgd 				       sc->sc_stat.c_blk);
    828  1.17  christos 				printf("R0x%x F0x%x A0x%x I0x%x\n",
    829   1.1       cgd 				       sc->sc_stat.c_ref,
    830   1.1       cgd 				       sc->sc_stat.c_fef,
    831   1.1       cgd 				       sc->sc_stat.c_aef,
    832   1.1       cgd 				       sc->sc_stat.c_ief);
    833   1.1       cgd 			}
    834   1.1       cgd 		} else
    835  1.17  christos 			printf("%s: request status failed\n",
    836  1.19   thorpej 			    sc->sc_dev.dv_xname);
    837   1.1       cgd 		bp->b_flags |= B_ERROR;
    838   1.1       cgd 		bp->b_error = EIO;
    839   1.1       cgd 		goto done;
    840   1.1       cgd 	} else
    841   1.1       cgd 		bp->b_resid = 0;
    842   1.1       cgd 	if (sc->sc_flags & CTF_CMD) {
    843   1.1       cgd 		switch (sc->sc_cmd) {
    844   1.1       cgd 		case MTFSF:
    845   1.1       cgd 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
    846   1.1       cgd 			sc->sc_blkno += CTBTOK(sc->sc_resid);
    847  1.19   thorpej 			ctstart(sc);
    848   1.1       cgd 			return;
    849   1.1       cgd 		case MTBSF:
    850   1.1       cgd 			sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
    851   1.1       cgd 			break;
    852   1.1       cgd 		case MTBSR:
    853   1.1       cgd 			sc->sc_flags &= ~CTF_BEOF;
    854   1.1       cgd 			if (sc->sc_flags & CTF_EOT) {
    855   1.1       cgd 				sc->sc_flags |= CTF_AEOF;
    856   1.1       cgd 				sc->sc_flags &= ~CTF_EOT;
    857   1.1       cgd 			} else if (sc->sc_flags & CTF_AEOF) {
    858   1.1       cgd 				sc->sc_flags |= CTF_BEOF;
    859   1.1       cgd 				sc->sc_flags &= ~CTF_AEOF;
    860   1.1       cgd 			}
    861   1.1       cgd 			break;
    862   1.1       cgd 		case MTWEOF:
    863   1.1       cgd 			sc->sc_flags &= ~CTF_BEOF;
    864   1.1       cgd 			if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
    865   1.1       cgd 				sc->sc_flags |= CTF_EOT;
    866   1.1       cgd 				sc->sc_flags &= ~CTF_AEOF;
    867   1.1       cgd 			} else
    868   1.1       cgd 				sc->sc_flags |= CTF_AEOF;
    869   1.1       cgd 			break;
    870   1.1       cgd 		case MTREW:
    871   1.1       cgd 		case MTOFFL:
    872   1.1       cgd 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
    873   1.1       cgd 			break;
    874   1.1       cgd 		}
    875   1.1       cgd 	} else {
    876   1.1       cgd 		sc->sc_flags &= ~CTF_AEOF;
    877   1.1       cgd 		sc->sc_blkno += CTBTOK(sc->sc_resid);
    878   1.1       cgd 	}
    879   1.1       cgd done:
    880   1.1       cgd #ifdef DEBUG
    881   1.1       cgd 	if (ctdebug & CDB_FILES)
    882  1.17  christos 		printf("ctintr: after flags %x\n", sc->sc_flags);
    883   1.1       cgd #endif
    884  1.19   thorpej 	ctdone(sc, bp);
    885  1.10   mycroft }
    886  1.10   mycroft 
    887  1.10   mycroft void
    888  1.19   thorpej ctdone(sc, bp)
    889  1.19   thorpej 	struct ct_softc *sc;
    890  1.20    scottr 	struct buf *bp;
    891  1.10   mycroft {
    892  1.10   mycroft 
    893  1.29   hannken 	(void)BUFQ_GET(&sc->sc_tab);
    894  1.10   mycroft 	biodone(bp);
    895  1.19   thorpej 	hpibfree(sc->sc_dev.dv_parent, &sc->sc_hq);
    896  1.29   hannken 	if (BUFQ_PEEK(&sc->sc_tab) == NULL) {
    897  1.24   thorpej 		sc->sc_active = 0;
    898   1.1       cgd 		return;
    899   1.1       cgd 	}
    900  1.19   thorpej 	ctustart(sc);
    901  1.11   thorpej }
    902  1.11   thorpej 
    903  1.11   thorpej int
    904  1.11   thorpej ctread(dev, uio, flags)
    905  1.11   thorpej 	dev_t dev;
    906  1.11   thorpej 	struct uio *uio;
    907  1.11   thorpej 	int flags;
    908  1.11   thorpej {
    909  1.11   thorpej 	return (physio(ctstrategy, NULL, dev, B_READ, minphys, uio));
    910  1.11   thorpej }
    911  1.11   thorpej 
    912  1.11   thorpej int
    913  1.11   thorpej ctwrite(dev, uio, flags)
    914  1.11   thorpej 	dev_t dev;
    915  1.11   thorpej 	struct uio *uio;
    916  1.11   thorpej 	int flags;
    917  1.11   thorpej {
    918  1.11   thorpej 	/* XXX: check for hardware write-protect? */
    919  1.11   thorpej 	return (physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio));
    920   1.1       cgd }
    921   1.1       cgd 
    922   1.1       cgd /*ARGSUSED*/
    923  1.19   thorpej int
    924  1.35      fvdl ctioctl(dev, cmd, data, flag, p)
    925   1.1       cgd 	dev_t dev;
    926  1.19   thorpej 	u_long cmd;
    927  1.19   thorpej 	int flag;
    928   1.1       cgd 	caddr_t data;
    929  1.35      fvdl 	struct proc *p;
    930   1.1       cgd {
    931  1.20    scottr 	struct mtop *op;
    932  1.20    scottr 	int cnt;
    933   1.1       cgd 
    934   1.1       cgd 	switch (cmd) {
    935   1.1       cgd 
    936   1.1       cgd 	case MTIOCTOP:
    937   1.1       cgd 		op = (struct mtop *)data;
    938   1.1       cgd 		switch(op->mt_op) {
    939   1.1       cgd 
    940   1.1       cgd 		case MTWEOF:
    941   1.1       cgd 		case MTFSF:
    942   1.1       cgd 		case MTBSR:
    943   1.1       cgd 		case MTBSF:
    944   1.1       cgd 		case MTFSR:
    945   1.1       cgd 			cnt = op->mt_count;
    946   1.1       cgd 			break;
    947   1.1       cgd 
    948   1.1       cgd 		case MTREW:
    949   1.1       cgd 		case MTOFFL:
    950   1.1       cgd 			cnt = 1;
    951   1.1       cgd 			break;
    952   1.1       cgd 
    953   1.1       cgd 		default:
    954   1.1       cgd 			return(EINVAL);
    955   1.1       cgd 		}
    956   1.1       cgd 		ctcommand(dev, op->mt_op, cnt);
    957   1.1       cgd 		break;
    958   1.1       cgd 
    959   1.1       cgd 	case MTIOCGET:
    960   1.1       cgd 		break;
    961   1.1       cgd 
    962   1.1       cgd 	default:
    963   1.1       cgd 		return(EINVAL);
    964   1.1       cgd 	}
    965   1.1       cgd 	return(0);
    966   1.1       cgd }
    967   1.1       cgd 
    968  1.19   thorpej void
    969  1.19   thorpej ctaddeof(sc)
    970  1.19   thorpej 	struct ct_softc *sc;
    971   1.1       cgd {
    972  1.19   thorpej 
    973   1.1       cgd 	if (sc->sc_eofp == EOFS - 1)
    974   1.1       cgd 		sc->sc_eofs[EOFS - 1]++;
    975   1.1       cgd 	else {
    976   1.1       cgd 		sc->sc_eofp++;
    977   1.1       cgd 		if (sc->sc_eofp == EOFS - 1)
    978   1.1       cgd 			sc->sc_eofs[EOFS - 1] = EOFS;
    979   1.1       cgd 		else
    980   1.1       cgd 			/* save blkno */
    981   1.1       cgd 			sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
    982   1.1       cgd 	}
    983   1.1       cgd #ifdef DEBUG
    984   1.1       cgd 	if (ctdebug & CT_BSF)
    985  1.17  christos 		printf("%s: add eof pos %d blk %d\n",
    986  1.19   thorpej 		       sc->sc_dev.dv_xname, sc->sc_eofp,
    987   1.1       cgd 		       sc->sc_eofs[sc->sc_eofp]);
    988   1.1       cgd #endif
    989   1.1       cgd }
    990