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