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