Home | History | Annotate | Line # | Download | only in gpib
ct.c revision 1.27
      1 /*	$NetBSD: ct.c,v 1.27 2014/07/25 08:10:36 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.27 2014/07/25 08:10:36 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_discard = nodiscard,
    206 	.d_flag = D_TAPE
    207 };
    208 
    209 extern struct cfdriver ct_cd;
    210 
    211 struct	ctinfo {
    212 	short	hwid;
    213 	short	punit;
    214 	const char	*desc;
    215 } ctinfo[] = {
    216 	{ CT7946ID,	1,	"7946A"	},
    217 	{ CT7912PID,	1,	"7912P"	},
    218 	{ CT7914PID,	1,	"7914P"	},
    219 	{ CT9144ID,	0,	"9144"	},
    220 	{ CT9145ID,	0,	"9145"	},
    221 	{ CT35401ID,	0,	"35401A"},
    222 };
    223 int	nctinfo = sizeof(ctinfo) / sizeof(ctinfo[0]);
    224 
    225 #define	CT_NOREW	4
    226 #define	CT_STREAM	8
    227 #define	CTUNIT(x)	(minor(x) & 0x03)
    228 
    229 int
    230 ctlookup(int id, int slave, int punit)
    231 {
    232 	int i;
    233 
    234 	for (i = 0; i < nctinfo; i++)
    235 		if (ctinfo[i].hwid == id)
    236 			break;
    237 	if (i == nctinfo)
    238 		return (-1);
    239 	return (i);
    240 }
    241 
    242 int
    243 ctmatch(device_t parent, cfdata_t match, void *aux)
    244 {
    245 	struct cs80bus_attach_args *ca = aux;
    246 	int i;
    247 
    248 	if ((i = ctlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
    249 		return (0);
    250 	ca->ca_punit = ctinfo[i].punit;
    251 	return (1);
    252 }
    253 
    254 void
    255 ctattach(device_t parent, device_t self, void *aux)
    256 {
    257 	struct ct_softc *sc = device_private(self);
    258 	struct cs80bus_attach_args *ca = aux;
    259 	struct cs80_description csd;
    260 	char name[7];
    261 	int type, i, n, canstream = 0;
    262 
    263 	sc->sc_ic = ca->ca_ic;
    264 	sc->sc_slave = ca->ca_slave;
    265 	sc->sc_punit = ca->ca_punit;
    266 
    267 	if ((type = ctlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
    268 		return;
    269 
    270 	if (cs80reset(parent, sc->sc_slave, sc->sc_punit)) {
    271 		aprint_normal("\n");
    272 		aprint_error_dev(sc->sc_dev, "can't reset device\n");
    273 		return;
    274 	}
    275 
    276 	if (cs80describe(parent, sc->sc_slave, sc->sc_punit, &csd)) {
    277 		aprint_normal("\n");
    278 		aprint_error_dev(sc->sc_dev, "didn't respond to describe command\n");
    279 		return;
    280 	}
    281 	memset(name, 0, sizeof(name));
    282 	for (i=0, n=0; i<3; i++) {
    283 		name[n++] = (csd.d_name[i] >> 4) + '0';
    284 		name[n++] = (csd.d_name[i] & 0x0f) + '0';
    285 	}
    286 
    287 #ifdef DEBUG
    288 	if (ctdebug & CDB_IDENT) {
    289 		printf("\n%s: name: ('%s')\n",
    290 		    device_xname(sc->sc_dev),name);
    291 		printf("  iuw %x, maxxfr %d, ctype %d\n",
    292 		    csd.d_iuw, csd.d_cmaxxfr, csd.d_ctype);
    293 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
    294 		    csd.d_utype, csd.d_sectsize,
    295 		    csd.d_blkbuf, csd.d_burstsize, csd.d_blocktime);
    296 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
    297 		    csd.d_uavexfr, csd.d_retry, csd.d_access,
    298 		    csd.d_maxint, csd.d_fvbyte, csd.d_rvbyte);
    299 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
    300 		    csd.d_maxcylhead >> 8 , csd.d_maxcylhead & 0xff,
    301 		    csd.d_maxsect, csd.d_maxvsectl, csd.d_interleave);
    302 		printf("%s", device_xname(sc->sc_dev));
    303 	}
    304 #endif
    305 
    306 	switch (ca->ca_id) {
    307 	case CT7946ID:
    308 		if (memcmp(name, "079450", 6) == 0)
    309 			return;			/* not really a 7946 */
    310 		/* fall into... */
    311 	case CT9144ID:
    312 	case CT9145ID:
    313 	case CT35401ID:
    314 		sc->sc_type = CT9144;
    315 		canstream = 1;
    316 		break;
    317 
    318 	case CT7912PID:
    319 	case CT7914PID:
    320 		sc->sc_type = CT88140;
    321 		break;
    322 	default:
    323 		sc->sc_type = type;
    324 		break;
    325 	}
    326 
    327 	sc->sc_type = type;
    328 	sc->sc_flags = canstream ? CTF_CANSTREAM : 0;
    329 	printf(": %s %stape\n", ctinfo[type].desc,
    330 	    canstream ? "streaming " : "");
    331 
    332 	bufq_alloc(&sc->sc_tab, "fcfs", 0);
    333 
    334 	if (gpibregister(sc->sc_ic, sc->sc_slave, ctcallback, sc,
    335 	    &sc->sc_hdl)) {
    336 		aprint_error_dev(sc->sc_dev, "can't register callback\n");
    337 		return;
    338 	}
    339 
    340 	sc->sc_flags |= CTF_ALIVE;
    341 }
    342 
    343 /*ARGSUSED*/
    344 int
    345 ctopen(dev_t dev, int flag, int type, struct lwp *l)
    346 {
    347 	struct ct_softc *sc;
    348 	u_int8_t opt;
    349 
    350 	sc = device_lookup_private(&ct_cd, CTUNIT(dev));
    351 	if (sc == NULL || (sc->sc_flags & CTF_ALIVE) == 0)
    352 		return (ENXIO);
    353 
    354 	if (sc->sc_flags & CTF_OPEN)
    355 		return (EBUSY);
    356 
    357 	if ((dev & CT_STREAM) && (sc->sc_flags & CTF_CANSTREAM))
    358 		opt = C_SPAR | C_IMRPT;
    359 	else
    360 		opt = C_SPAR;
    361 
    362 	if (cs80setoptions(device_parent(sc->sc_dev), sc->sc_slave,
    363 	    sc->sc_punit, opt))
    364 		return (EBUSY);
    365 
    366 	sc->sc_tpr = tprintf_open(l->l_proc);
    367 	sc->sc_flags |= CTF_OPEN;
    368 
    369 	return (0);
    370 }
    371 
    372 /*ARGSUSED*/
    373 int
    374 ctclose(dev_t dev, int flag, int fmt, struct lwp *l)
    375 {
    376 	struct ct_softc *sc;
    377 
    378 	sc = device_lookup_private(&ct_cd, CTUNIT(dev));
    379 	if (sc == NULL)
    380 		return (ENXIO);
    381 
    382 	if ((sc->sc_flags & (CTF_WRT|CTF_WRTTN)) == (CTF_WRT|CTF_WRTTN) &&
    383 	    (sc->sc_flags & CTF_EOT) == 0 ) { /* XXX return error if EOT ?? */
    384 		ctcommand(dev, MTWEOF, 2);
    385 		ctcommand(dev, MTBSR, 1);
    386 		if (sc->sc_eofp == EOFS - 1)
    387 			sc->sc_eofs[EOFS - 1]--;
    388 		else
    389 			sc->sc_eofp--;
    390 		DPRINTF(CDB_BSF, ("%s: ctclose backup eofs prt %d blk %d\n",
    391 		    device_xname(sc->sc_dev), sc->sc_eofp,
    392 		    sc->sc_eofs[sc->sc_eofp]));
    393 	}
    394 
    395 	if ((minor(dev) & CT_NOREW) == 0)
    396 		ctcommand(dev, MTREW, 1);
    397 	sc->sc_flags &= ~(CTF_OPEN | CTF_WRT | CTF_WRTTN);
    398 	tprintf_close(sc->sc_tpr);
    399 	DPRINTF(CDB_FILES, ("ctclose: flags %x\n", sc->sc_flags));
    400 
    401 	return (0);	/* XXX */
    402 }
    403 
    404 void
    405 ctcommand(dev_t dev, int cmd, int cnt)
    406 {
    407 	struct ct_softc *sc;
    408 	struct buf *bp;
    409 	struct buf *nbp = 0;
    410 
    411 	sc = device_lookup_private(&ct_cd, CTUNIT(dev));
    412 	bp = &sc->sc_bufstore;
    413 
    414 	DPRINTF(CDB_FOLLOW, ("ctcommand: called\n"));
    415 
    416 	if (cmd == MTBSF && sc->sc_eofp == EOFS - 1) {
    417 		cnt = sc->sc_eofs[EOFS - 1] - cnt;
    418 		ctcommand(dev, MTREW, 1);
    419 		ctcommand(dev, MTFSF, cnt);
    420 		cnt = 2;
    421 		cmd = MTBSR;
    422 	}
    423 
    424 	if (cmd == MTBSF && sc->sc_eofp - cnt < 0) {
    425 		cnt = 1;
    426 		cmd = MTREW;
    427 	}
    428 
    429 	sc->sc_flags |= CTF_CMD;
    430 	sc->sc_bp = bp;
    431 	sc->sc_cmd = cmd;
    432 	bp->b_dev = dev;
    433 	bp->b_objlock = &buffer_lock;
    434 	if (cmd == MTFSF) {
    435 		nbp = (struct buf *)geteblk(MAXBSIZE);
    436 		bp->b_data = nbp->b_data;
    437 		bp->b_bcount = MAXBSIZE;
    438 	}
    439 
    440 	while (cnt-- > 0) {
    441 		bp->b_flags = 0;
    442 		bp->b_cflags = BC_BUSY;
    443 		bp->b_oflags = 0;
    444 		if (cmd == MTBSF) {
    445 			sc->sc_blkno = sc->sc_eofs[sc->sc_eofp];
    446 			sc->sc_eofp--;
    447 			DPRINTF(CDB_BSF, ("%s: backup eof pos %d blk %d\n",
    448 			    device_xname(sc->sc_dev), sc->sc_eofp,
    449 			    sc->sc_eofs[sc->sc_eofp]));
    450 		}
    451 		ctstrategy(bp);
    452 		biowait(bp);
    453 	}
    454 	bp->b_flags = 0;
    455 	sc->sc_flags &= ~CTF_CMD;
    456 	if (nbp)
    457 		brelse(nbp, 0);
    458 }
    459 
    460 void
    461 ctstrategy(struct buf *bp)
    462 {
    463 	struct ct_softc *sc;
    464 	int s;
    465 
    466 	DPRINTF(CDB_FOLLOW, ("cdstrategy(%p): dev %" PRIx64 ", bn %" PRIx64
    467 	    ", bcount %x, %c\n",
    468 	    bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    469 	    (bp->b_flags & B_READ) ? 'R' : 'W'));
    470 
    471 	sc = device_lookup_private(&ct_cd, CTUNIT(bp->b_dev));
    472 
    473 	s = splbio();
    474 	bufq_put(sc->sc_tab, bp);
    475 	if (sc->sc_active == 0) {
    476 		sc->sc_active = 1;
    477 		ctustart(sc);
    478 	}
    479 	splx(s);
    480 }
    481 
    482 void
    483 ctustart(struct ct_softc *sc)
    484 {
    485 	struct buf *bp;
    486 
    487 	bp = bufq_peek(sc->sc_tab);
    488 	sc->sc_addr = bp->b_data;
    489 	sc->sc_resid = bp->b_bcount;
    490 	if (gpibrequest(sc->sc_ic, sc->sc_hdl))
    491 		ctstart(sc);
    492 }
    493 
    494 void
    495 ctstart(struct ct_softc *sc)
    496 {
    497 	struct buf *bp;
    498 	struct ct_ulcmd ul;
    499 	struct ct_wfmcmd wfm;
    500 	int i, slave, punit;
    501 
    502 	slave = sc->sc_slave;
    503 	punit = sc->sc_punit;
    504 
    505 	bp = bufq_peek(sc->sc_tab);
    506 	if ((sc->sc_flags & CTF_CMD) && sc->sc_bp == bp) {
    507 		switch(sc->sc_cmd) {
    508 		case MTFSF:
    509 			bp->b_flags |= B_READ;
    510 			goto mustio;
    511 
    512 		case MTBSF:
    513 			goto gotaddr;
    514 
    515 		case MTOFFL:
    516 			sc->sc_blkno = 0;
    517 			ul.unit = CS80CMD_SUNIT(punit);
    518 			ul.cmd = CS80CMD_UNLOAD;
    519 			(void) cs80send(device_parent(sc->sc_dev), slave,
    520 			    punit, CS80CMD_SCMD, &ul, sizeof(ul));
    521 			break;
    522 
    523 		case MTWEOF:
    524 			sc->sc_blkno++;
    525 			sc->sc_flags |= CTF_WRT;
    526 			wfm.unit = CS80CMD_SUNIT(sc->sc_punit);
    527 			wfm.cmd = CS80CMD_WFM;
    528 			(void) cs80send(device_parent(sc->sc_dev), slave,
    529 			    punit, CS80CMD_SCMD, &wfm, sizeof(wfm));
    530 			ctaddeof(sc);
    531 			break;
    532 
    533 		case MTBSR:
    534 			sc->sc_blkno--;
    535 			goto gotaddr;
    536 
    537 		case MTFSR:
    538 			sc->sc_blkno++;
    539 			goto gotaddr;
    540 
    541 		case MTREW:
    542 			sc->sc_blkno = 0;
    543 			DPRINTF(CDB_BSF, ("%s: clearing eofs\n",
    544 			    device_xname(sc->sc_dev)));
    545 			for (i=0; i<EOFS; i++)
    546 				sc->sc_eofs[i] = 0;
    547 			sc->sc_eofp = 0;
    548 
    549 gotaddr:
    550 			sc->sc_ioc.unit = CS80CMD_SUNIT(sc->sc_punit);
    551 			sc->sc_ioc.saddr = CS80CMD_SADDR;
    552 			sc->sc_ioc.addr0 = 0;
    553 			sc->sc_ioc.addr = htobe32(sc->sc_blkno);
    554 			sc->sc_ioc.nop2 = CS80CMD_NOP;
    555 			sc->sc_ioc.slen = CS80CMD_SLEN;
    556 			sc->sc_ioc.len = htobe32(0);
    557 			sc->sc_ioc.nop3 = CS80CMD_NOP;
    558 			sc->sc_ioc.cmd = CS80CMD_READ;
    559 			(void) cs80send(device_parent(sc->sc_dev), slave,
    560 			    punit, CS80CMD_SCMD, &sc->sc_ioc,
    561 			    sizeof(sc->sc_ioc));
    562 			break;
    563 		}
    564 	} else {
    565 mustio:
    566 		if ((bp->b_flags & B_READ) &&
    567 		    sc->sc_flags & (CTF_BEOF|CTF_EOT)) {
    568 			DPRINTF(CDB_FILES, ("ctstart: before %x\n",
    569 				    sc->sc_flags));
    570 			if (sc->sc_flags & CTF_BEOF) {
    571 				sc->sc_flags &= ~CTF_BEOF;
    572 				sc->sc_flags |= CTF_AEOF;
    573 				DPRINTF(CDB_FILES, ("ctstart: after %x\n",
    574 				    sc->sc_flags));
    575 			}
    576 			bp->b_resid = bp->b_bcount;
    577 			ctdone(sc, bp);
    578 			return;
    579 		}
    580 		sc->sc_flags |= CTF_IO;
    581 		sc->sc_ioc.unit = CS80CMD_SUNIT(sc->sc_punit);
    582 		sc->sc_ioc.saddr = CS80CMD_SADDR;
    583 		sc->sc_ioc.addr0 = 0;
    584 		sc->sc_ioc.addr = htobe32(sc->sc_blkno);
    585 		sc->sc_ioc.nop2 = CS80CMD_NOP;
    586 		sc->sc_ioc.slen = CS80CMD_SLEN;
    587 		sc->sc_ioc.len = htobe32(sc->sc_resid);
    588 		sc->sc_ioc.nop3 = CS80CMD_NOP;
    589 		if (bp->b_flags & B_READ)
    590 			sc->sc_ioc.cmd = CS80CMD_READ;
    591 		else {
    592 			sc->sc_ioc.cmd = CS80CMD_WRITE;
    593 			sc->sc_flags |= (CTF_WRT | CTF_WRTTN);
    594 		}
    595 		(void) cs80send(device_parent(sc->sc_dev), slave, punit,
    596 		    CS80CMD_SCMD, &sc->sc_ioc, sizeof(sc->sc_ioc));
    597 	}
    598 	gpibawait(sc->sc_ic);
    599 }
    600 
    601 /*
    602  * Hideous grue to handle EOF/EOT (mostly for reads)
    603  */
    604 void
    605 cteof(struct ct_softc *sc, struct buf *bp)
    606 {
    607 	long blks;
    608 
    609 	/*
    610 	 * EOT on a write is an error.
    611 	 */
    612 	if ((bp->b_flags & B_READ) == 0) {
    613 		bp->b_resid = bp->b_bcount;
    614 		bp->b_error = ENOSPC;
    615 		sc->sc_flags |= CTF_EOT;
    616 		return;
    617 	}
    618 	/*
    619 	 * Use returned block position to determine how many blocks
    620 	 * we really read and update b_resid.
    621 	 */
    622 	blks = sc->sc_stat.c_blk - sc->sc_blkno - 1;
    623 	DPRINTF(CDB_FILES, ("cteof: bc %d oblk %d nblk %d read %ld, resid %ld\n",
    624 	    bp->b_bcount, sc->sc_blkno, sc->sc_stat.c_blk,
    625 	    blks, bp->b_bcount - CTKTOB(blks)));
    626 	if (blks == -1) { /* 9145 on EOF does not change sc_stat.c_blk */
    627 		blks = 0;
    628 		sc->sc_blkno++;
    629 	}
    630 	else {
    631 		sc->sc_blkno = sc->sc_stat.c_blk;
    632 	}
    633 	bp->b_resid = bp->b_bcount - CTKTOB(blks);
    634 	/*
    635 	 * If we are at physical EOV or were after an EOF,
    636 	 * we are now at logical EOT.
    637 	 */
    638 	if ((sc->sc_stat.c_aef & AEF_EOV) ||
    639 	    (sc->sc_flags & CTF_AEOF)) {
    640 		sc->sc_flags |= CTF_EOT;
    641 		sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF);
    642 	}
    643 	/*
    644 	 * If we were before an EOF or we have just completed a FSF,
    645 	 * we are now after EOF.
    646 	 */
    647 	else if ((sc->sc_flags & CTF_BEOF) ||
    648 		 ((sc->sc_flags & CTF_CMD) && sc->sc_cmd == MTFSF)) {
    649 		sc->sc_flags |= CTF_AEOF;
    650 		sc->sc_flags &= ~CTF_BEOF;
    651 	}
    652 	/*
    653 	 * Otherwise if we read something we are now before EOF
    654 	 * (and no longer after EOF).
    655 	 */
    656 	else if (blks) {
    657 		sc->sc_flags |= CTF_BEOF;
    658 		sc->sc_flags &= ~CTF_AEOF;
    659 	}
    660 	/*
    661 	 * Finally, if we didn't read anything we just passed an EOF
    662 	 */
    663 	else
    664 		sc->sc_flags |= CTF_AEOF;
    665 	DPRINTF(CDB_FILES, ("cteof: leaving flags %x\n", sc->sc_flags));
    666 }
    667 
    668 
    669 void
    670 ctcallback(void *v, int action)
    671 {
    672 	struct ct_softc *sc = v;
    673 
    674 	DPRINTF(CDB_FOLLOW, ("ctcallback: v=%p, action=%d\n", v, action));
    675 
    676 	switch (action) {
    677 	case GPIBCBF_START:
    678 		ctstart(sc);
    679 		break;
    680 	case GPIBCBF_INTR:
    681 		ctintr(sc);
    682 		break;
    683 #ifdef DEBUG
    684 	default:
    685 		DPRINTF(CDB_FAIL, ("ctcallback: unknown action %d\n", action));
    686 		break;
    687 #endif
    688 	}
    689 }
    690 
    691 void
    692 ctintr(struct ct_softc *sc)
    693 {
    694 	struct buf *bp;
    695 	u_int8_t stat;
    696 	int slave, punit;
    697 	int dir;
    698 
    699 	slave = sc->sc_slave;
    700 	punit = sc->sc_punit;
    701 
    702 	bp = bufq_peek(sc->sc_tab);
    703 	if (bp == NULL) {
    704 		aprint_error_dev(sc->sc_dev, "bp == NULL\n");
    705 		return;
    706 	}
    707 	if (sc->sc_flags & CTF_IO) {
    708 		sc->sc_flags &= ~CTF_IO;
    709 		dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
    710 		gpibxfer(sc->sc_ic, slave, CS80CMD_EXEC, sc->sc_addr,
    711 		    sc->sc_resid, dir, dir == GPIB_READ);
    712 		return;
    713 	}
    714 	if ((sc->sc_flags & CTF_STATWAIT) == 0) {
    715 		if (gpibpptest(sc->sc_ic, slave) == 0) {
    716 			sc->sc_flags |= CTF_STATWAIT;
    717 			gpibawait(sc->sc_ic);
    718 			return;
    719 		}
    720 	} else
    721 		sc->sc_flags &= ~CTF_STATWAIT;
    722 	(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
    723 	DPRINTF(CDB_FILES, ("ctintr: before flags %x\n", sc->sc_flags));
    724 	if (stat) {
    725 		sc->sc_rsc.unit = CS80CMD_SUNIT(punit);
    726 		sc->sc_rsc.cmd = CS80CMD_STATUS;
    727 		(void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &sc->sc_rsc,
    728 		    sizeof(sc->sc_rsc));
    729 		(void) gpibrecv(sc->sc_ic, slave, CS80CMD_EXEC, &sc->sc_stat,
    730 		    sizeof(sc->sc_stat));
    731 		(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
    732 		DPRINTF(CDB_FILES, ("ctintr: return stat 0x%x, A%x F%x blk %d\n",
    733 			       stat, sc->sc_stat.c_aef,
    734 			       sc->sc_stat.c_fef, sc->sc_stat.c_blk));
    735 		if (stat == 0) {
    736 			if (sc->sc_stat.c_aef & (AEF_EOF | AEF_EOV)) {
    737 				cteof(sc, bp);
    738 				ctaddeof(sc);
    739 				goto done;
    740 			}
    741 			if (sc->sc_stat.c_fef & FEF_PF) {
    742 				cs80reset(sc, slave, punit);
    743 				ctstart(sc);
    744 				return;
    745 			}
    746 			if (sc->sc_stat.c_fef & FEF_REXMT) {
    747 				ctstart(sc);
    748 				return;
    749 			}
    750 			if (sc->sc_stat.c_aef & 0x5800) {
    751 				if (sc->sc_stat.c_aef & 0x4000)
    752 					tprintf(sc->sc_tpr,
    753 					    "%s: uninitialized media\n",
    754 					    device_xname(sc->sc_dev));
    755 				if (sc->sc_stat.c_aef & 0x1000)
    756 					tprintf(sc->sc_tpr,
    757 					    "%s: not ready\n",
    758 					    device_xname(sc->sc_dev));
    759 				if (sc->sc_stat.c_aef & 0x0800)
    760 					tprintf(sc->sc_tpr,
    761 					    "%s: write protect\n",
    762 					    device_xname(sc->sc_dev));
    763 			} else {
    764 				printf("%s err: v%d u%d ru%d bn%d, ",
    765 				    device_xname(sc->sc_dev),
    766 				    (sc->sc_stat.c_vu>>4)&0xF,
    767 				    sc->sc_stat.c_vu&0xF,
    768 				    sc->sc_stat.c_pend,
    769 				    sc->sc_stat.c_blk);
    770 				printf("R0x%x F0x%x A0x%x I0x%x\n",
    771 				    sc->sc_stat.c_ref,
    772 				    sc->sc_stat.c_fef,
    773 				    sc->sc_stat.c_aef,
    774 				    sc->sc_stat.c_ief);
    775 			}
    776 		} else
    777 			aprint_error_dev(sc->sc_dev, "request status failed\n");
    778 		bp->b_error = EIO;
    779 		goto done;
    780 	} else
    781 		bp->b_resid = 0;
    782 	if (sc->sc_flags & CTF_CMD) {
    783 		switch (sc->sc_cmd) {
    784 		case MTFSF:
    785 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF);
    786 			sc->sc_blkno += CTBTOK(sc->sc_resid);
    787 			ctstart(sc);
    788 			return;
    789 		case MTBSF:
    790 			sc->sc_flags &= ~(CTF_AEOF|CTF_BEOF|CTF_EOT);
    791 			break;
    792 		case MTBSR:
    793 			sc->sc_flags &= ~CTF_BEOF;
    794 			if (sc->sc_flags & CTF_EOT) {
    795 				sc->sc_flags |= CTF_AEOF;
    796 				sc->sc_flags &= ~CTF_EOT;
    797 			} else if (sc->sc_flags & CTF_AEOF) {
    798 				sc->sc_flags |= CTF_BEOF;
    799 				sc->sc_flags &= ~CTF_AEOF;
    800 			}
    801 			break;
    802 		case MTWEOF:
    803 			sc->sc_flags &= ~CTF_BEOF;
    804 			if (sc->sc_flags & (CTF_AEOF|CTF_EOT)) {
    805 				sc->sc_flags |= CTF_EOT;
    806 				sc->sc_flags &= ~CTF_AEOF;
    807 			} else
    808 				sc->sc_flags |= CTF_AEOF;
    809 			break;
    810 		case MTREW:
    811 		case MTOFFL:
    812 			sc->sc_flags &= ~(CTF_BEOF|CTF_AEOF|CTF_EOT);
    813 			break;
    814 		}
    815 	} else {
    816 		sc->sc_flags &= ~CTF_AEOF;
    817 		sc->sc_blkno += CTBTOK(sc->sc_resid);
    818 	}
    819 done:
    820 	DPRINTF(CDB_FILES, ("ctintr: after flags %x\n", sc->sc_flags));
    821 	ctdone(sc, bp);
    822 }
    823 
    824 void
    825 ctdone(struct ct_softc *sc, struct buf *bp)
    826 {
    827 
    828 	(void)bufq_get(sc->sc_tab);
    829 	biodone(bp);
    830 	gpibrelease(sc->sc_ic, sc->sc_hdl);
    831 	if (bufq_peek(sc->sc_tab) == NULL) {
    832 		sc->sc_active = 0;
    833 		return;
    834 	}
    835 	ctustart(sc);
    836 }
    837 
    838 int
    839 ctread(dev_t dev, struct uio *uio, int flags)
    840 {
    841 	return (physio(ctstrategy, NULL, dev, B_READ, minphys, uio));
    842 }
    843 
    844 int
    845 ctwrite(dev_t dev, struct uio *uio, int flags)
    846 {
    847 	/* XXX: check for hardware write-protect? */
    848 	return (physio(ctstrategy, NULL, dev, B_WRITE, minphys, uio));
    849 }
    850 
    851 /*ARGSUSED*/
    852 int
    853 ctioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    854 {
    855 	struct mtop *op;
    856 	int cnt;
    857 
    858 	switch (cmd) {
    859 
    860 	case MTIOCTOP:
    861 		op = (struct mtop *)data;
    862 		switch(op->mt_op) {
    863 
    864 		case MTWEOF:
    865 		case MTFSF:
    866 		case MTBSR:
    867 		case MTBSF:
    868 		case MTFSR:
    869 			cnt = op->mt_count;
    870 			break;
    871 
    872 		case MTREW:
    873 		case MTOFFL:
    874 			cnt = 1;
    875 			break;
    876 
    877 		default:
    878 			return (EINVAL);
    879 		}
    880 		ctcommand(dev, op->mt_op, cnt);
    881 		break;
    882 
    883 	case MTIOCGET:
    884 		break;
    885 
    886 	default:
    887 		return (EINVAL);
    888 	}
    889 	return (0);
    890 }
    891 
    892 void
    893 ctaddeof(struct ct_softc *sc)
    894 {
    895 
    896 	if (sc->sc_eofp == EOFS - 1)
    897 		sc->sc_eofs[EOFS - 1]++;
    898 	else {
    899 		sc->sc_eofp++;
    900 		if (sc->sc_eofp == EOFS - 1)
    901 			sc->sc_eofs[EOFS - 1] = EOFS;
    902 		else
    903 			/* save blkno */
    904 			sc->sc_eofs[sc->sc_eofp] = sc->sc_blkno - 1;
    905 	}
    906 	DPRINTF(CDB_BSF, ("%s: add eof pos %d blk %d\n",
    907 		       device_xname(sc->sc_dev), sc->sc_eofp,
    908 		       sc->sc_eofs[sc->sc_eofp]));
    909 }
    910