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