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