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