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