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