mt.c revision 1.11.22.2 1 1.11.22.2 ad /* $NetBSD: mt.c,v 1.11.22.2 2007/07/29 12:15:44 ad Exp $ */
2 1.11.22.2 ad
3 1.11.22.2 ad /*-
4 1.11.22.2 ad * Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
5 1.11.22.2 ad * All rights reserved.
6 1.11.22.2 ad *
7 1.11.22.2 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.11.22.2 ad * by Jason R. Thorpe.
9 1.11.22.2 ad *
10 1.11.22.2 ad * Redistribution and use in source and binary forms, with or without
11 1.11.22.2 ad * modification, are permitted provided that the following conditions
12 1.11.22.2 ad * are met:
13 1.11.22.2 ad * 1. Redistributions of source code must retain the above copyright
14 1.11.22.2 ad * notice, this list of conditions and the following disclaimer.
15 1.11.22.2 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.11.22.2 ad * notice, this list of conditions and the following disclaimer in the
17 1.11.22.2 ad * documentation and/or other materials provided with the distribution.
18 1.11.22.2 ad * 3. All advertising materials mentioning features or use of this software
19 1.11.22.2 ad * must display the following acknowledgement:
20 1.11.22.2 ad * This product includes software developed by the NetBSD
21 1.11.22.2 ad * Foundation, Inc. and its contributors.
22 1.11.22.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.11.22.2 ad * contributors may be used to endorse or promote products derived
24 1.11.22.2 ad * from this software without specific prior written permission.
25 1.11.22.2 ad *
26 1.11.22.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.11.22.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.11.22.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.11.22.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.11.22.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.11.22.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.11.22.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.11.22.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.11.22.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.11.22.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.11.22.2 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.11.22.2 ad */
38 1.11.22.2 ad
39 1.11.22.2 ad /*
40 1.11.22.2 ad * Copyright (c) 1982, 1990, 1993
41 1.11.22.2 ad * The Regents of the University of California. All rights reserved.
42 1.11.22.2 ad *
43 1.11.22.2 ad * This code is derived from software contributed to Berkeley by
44 1.11.22.2 ad * the Systems Programming Group of the University of Utah Computer
45 1.11.22.2 ad * Science Department.
46 1.11.22.2 ad *
47 1.11.22.2 ad * Redistribution and use in source and binary forms, with or without
48 1.11.22.2 ad * modification, are permitted provided that the following conditions
49 1.11.22.2 ad * are met:
50 1.11.22.2 ad * 1. Redistributions of source code must retain the above copyright
51 1.11.22.2 ad * notice, this list of conditions and the following disclaimer.
52 1.11.22.2 ad * 2. Redistributions in binary form must reproduce the above copyright
53 1.11.22.2 ad * notice, this list of conditions and the following disclaimer in the
54 1.11.22.2 ad * documentation and/or other materials provided with the distribution.
55 1.11.22.2 ad * 3. Neither the name of the University nor the names of its contributors
56 1.11.22.2 ad * may be used to endorse or promote products derived from this software
57 1.11.22.2 ad * without specific prior written permission.
58 1.11.22.2 ad *
59 1.11.22.2 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.11.22.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.11.22.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.11.22.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.11.22.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.11.22.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.11.22.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.11.22.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.11.22.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.11.22.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.11.22.2 ad * SUCH DAMAGE.
70 1.11.22.2 ad *
71 1.11.22.2 ad * from: Utah $Hdr: rd.c 1.44 92/12/26$
72 1.11.22.2 ad *
73 1.11.22.2 ad * @(#)rd.c 8.2 (Berkeley) 5/19/94
74 1.11.22.2 ad */
75 1.11.22.2 ad
76 1.11.22.2 ad /*
77 1.11.22.2 ad * Copyright (c) 1988 University of Utah.
78 1.11.22.2 ad *
79 1.11.22.2 ad * This code is derived from software contributed to Berkeley by
80 1.11.22.2 ad * the Systems Programming Group of the University of Utah Computer
81 1.11.22.2 ad * Science Department.
82 1.11.22.2 ad *
83 1.11.22.2 ad * Redistribution and use in source and binary forms, with or without
84 1.11.22.2 ad * modification, are permitted provided that the following conditions
85 1.11.22.2 ad * are met:
86 1.11.22.2 ad * 1. Redistributions of source code must retain the above copyright
87 1.11.22.2 ad * notice, this list of conditions and the following disclaimer.
88 1.11.22.2 ad * 2. Redistributions in binary form must reproduce the above copyright
89 1.11.22.2 ad * notice, this list of conditions and the following disclaimer in the
90 1.11.22.2 ad * documentation and/or other materials provided with the distribution.
91 1.11.22.2 ad * 3. All advertising materials mentioning features or use of this software
92 1.11.22.2 ad * must display the following acknowledgement:
93 1.11.22.2 ad * This product includes software developed by the University of
94 1.11.22.2 ad * California, Berkeley and its contributors.
95 1.11.22.2 ad * 4. Neither the name of the University nor the names of its contributors
96 1.11.22.2 ad * may be used to endorse or promote products derived from this software
97 1.11.22.2 ad * without specific prior written permission.
98 1.11.22.2 ad *
99 1.11.22.2 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
100 1.11.22.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
101 1.11.22.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
102 1.11.22.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
103 1.11.22.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
104 1.11.22.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
105 1.11.22.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
106 1.11.22.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
107 1.11.22.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
108 1.11.22.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
109 1.11.22.2 ad * SUCH DAMAGE.
110 1.11.22.2 ad *
111 1.11.22.2 ad * from: Utah $Hdr: rd.c 1.44 92/12/26$
112 1.11.22.2 ad *
113 1.11.22.2 ad * @(#)rd.c 8.2 (Berkeley) 5/19/94
114 1.11.22.2 ad */
115 1.11.22.2 ad
116 1.11.22.2 ad /*
117 1.11.22.2 ad * Magnetic tape driver (HP7974a, HP7978a/b, HP7979a, HP7980a, HP7980xc)
118 1.11.22.2 ad * Original version contributed by Mt. Xinu.
119 1.11.22.2 ad * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
120 1.11.22.2 ad * Computer Science, Victoria University of Wellington
121 1.11.22.2 ad */
122 1.11.22.2 ad
123 1.11.22.2 ad #include <sys/cdefs.h>
124 1.11.22.2 ad __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.11.22.2 2007/07/29 12:15:44 ad Exp $");
125 1.11.22.2 ad
126 1.11.22.2 ad #include <sys/param.h>
127 1.11.22.2 ad #include <sys/systm.h>
128 1.11.22.2 ad #include <sys/callout.h>
129 1.11.22.2 ad #include <sys/buf.h>
130 1.11.22.2 ad #include <sys/bufq.h>
131 1.11.22.2 ad #include <sys/ioctl.h>
132 1.11.22.2 ad #include <sys/mtio.h>
133 1.11.22.2 ad #include <sys/file.h>
134 1.11.22.2 ad #include <sys/proc.h>
135 1.11.22.2 ad #include <sys/tty.h>
136 1.11.22.2 ad #include <sys/kernel.h>
137 1.11.22.2 ad #include <sys/tprintf.h>
138 1.11.22.2 ad #include <sys/device.h>
139 1.11.22.2 ad #include <sys/conf.h>
140 1.11.22.2 ad
141 1.11.22.2 ad #include <dev/gpib/gpibvar.h>
142 1.11.22.2 ad #include <dev/gpib/cs80busvar.h>
143 1.11.22.2 ad
144 1.11.22.2 ad #include <dev/gpib/mtreg.h>
145 1.11.22.2 ad
146 1.11.22.2 ad #ifdef DEBUG
147 1.11.22.2 ad int mtdebug = 0;
148 1.11.22.2 ad #define MDB_ANY 0xff
149 1.11.22.2 ad #define MDB_FOLLOW 0x01
150 1.11.22.2 ad #define DPRINTF(mask, str) if (mtdebug & (mask)) printf str
151 1.11.22.2 ad #else
152 1.11.22.2 ad #define DPRINTF(mask, str) /* nothing */
153 1.11.22.2 ad #endif
154 1.11.22.2 ad
155 1.11.22.2 ad struct mt_softc {
156 1.11.22.2 ad struct device sc_dev;
157 1.11.22.2 ad
158 1.11.22.2 ad gpib_chipset_tag_t sc_ic;
159 1.11.22.2 ad gpib_handle_t sc_hdl;
160 1.11.22.2 ad
161 1.11.22.2 ad int sc_slave; /* GPIB slave address (0-6) */
162 1.11.22.2 ad short sc_flags; /* see below */
163 1.11.22.2 ad u_char sc_lastdsj; /* place for DSJ in mtreaddsj() */
164 1.11.22.2 ad u_char sc_lastecmd; /* place for End Command in mtreaddsj() */
165 1.11.22.2 ad short sc_recvtimeo; /* count of gpibsend timeouts to prevent hang */
166 1.11.22.2 ad short sc_statindex; /* index for next sc_stat when MTF_STATTIMEO */
167 1.11.22.2 ad struct mt_stat sc_stat;/* status bytes last read from device */
168 1.11.22.2 ad short sc_density; /* current density of tape (mtio.h format) */
169 1.11.22.2 ad short sc_type; /* tape drive model (hardware IDs) */
170 1.11.22.2 ad tpr_t sc_ttyp;
171 1.11.22.2 ad struct bufq_state *sc_tab;/* buf queue */
172 1.11.22.2 ad int sc_active;
173 1.11.22.2 ad struct buf sc_bufstore; /* XXX buffer storage */
174 1.11.22.2 ad
175 1.11.22.2 ad struct callout sc_start_ch;
176 1.11.22.2 ad struct callout sc_intr_ch;
177 1.11.22.2 ad };
178 1.11.22.2 ad
179 1.11.22.2 ad #define MTUNIT(x) (minor(x) & 0x03)
180 1.11.22.2 ad
181 1.11.22.2 ad #define B_CMD B_DEVPRIVATE /* command buf instead of data */
182 1.11.22.2 ad #define b_cmd b_blkno /* blkno holds cmd when B_CMD */
183 1.11.22.2 ad
184 1.11.22.2 ad int mtmatch(struct device *, struct cfdata *, void *);
185 1.11.22.2 ad void mtattach(struct device *, struct device *, void *);
186 1.11.22.2 ad
187 1.11.22.2 ad CFATTACH_DECL(mt, sizeof(struct mt_softc),
188 1.11.22.2 ad mtmatch, mtattach, NULL, NULL);
189 1.11.22.2 ad
190 1.11.22.2 ad int mtlookup(int, int, int);
191 1.11.22.2 ad void mtustart(struct mt_softc *);
192 1.11.22.2 ad int mtreaddsj(struct mt_softc *, int);
193 1.11.22.2 ad int mtcommand(dev_t, int, int);
194 1.11.22.2 ad
195 1.11.22.2 ad void mtintr_callout(void *);
196 1.11.22.2 ad void mtstart_callout(void *);
197 1.11.22.2 ad
198 1.11.22.2 ad void mtcallback(void *, int);
199 1.11.22.2 ad void mtstart(struct mt_softc *);
200 1.11.22.2 ad void mtintr(struct mt_softc *);
201 1.11.22.2 ad
202 1.11.22.2 ad dev_type_open(mtopen);
203 1.11.22.2 ad dev_type_close(mtclose);
204 1.11.22.2 ad dev_type_read(mtread);
205 1.11.22.2 ad dev_type_write(mtwrite);
206 1.11.22.2 ad dev_type_ioctl(mtioctl);
207 1.11.22.2 ad dev_type_strategy(mtstrategy);
208 1.11.22.2 ad
209 1.11.22.2 ad const struct bdevsw mt_bdevsw = {
210 1.11.22.2 ad mtopen, mtclose, mtstrategy, mtioctl, nodump, nosize, D_TAPE
211 1.11.22.2 ad };
212 1.11.22.2 ad
213 1.11.22.2 ad const struct cdevsw mt_cdevsw = {
214 1.11.22.2 ad mtopen, mtclose, mtread, mtwrite, mtioctl,
215 1.11.22.2 ad nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
216 1.11.22.2 ad };
217 1.11.22.2 ad
218 1.11.22.2 ad
219 1.11.22.2 ad extern struct cfdriver mt_cd;
220 1.11.22.2 ad
221 1.11.22.2 ad struct mtinfo {
222 1.11.22.2 ad u_short hwid;
223 1.11.22.2 ad const char *desc;
224 1.11.22.2 ad } mtinfo[] = {
225 1.11.22.2 ad { MT7978ID, "7978" },
226 1.11.22.2 ad { MT7979AID, "7979A" },
227 1.11.22.2 ad { MT7980ID, "7980" },
228 1.11.22.2 ad { MT7974AID, "7974A" },
229 1.11.22.2 ad };
230 1.11.22.2 ad int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
231 1.11.22.2 ad
232 1.11.22.2 ad
233 1.11.22.2 ad int
234 1.11.22.2 ad mtlookup(id, slave, punit)
235 1.11.22.2 ad int id;
236 1.11.22.2 ad int slave;
237 1.11.22.2 ad int punit;
238 1.11.22.2 ad {
239 1.11.22.2 ad int i;
240 1.11.22.2 ad
241 1.11.22.2 ad for (i = 0; i < nmtinfo; i++)
242 1.11.22.2 ad if (mtinfo[i].hwid == id)
243 1.11.22.2 ad break;
244 1.11.22.2 ad if (i == nmtinfo)
245 1.11.22.2 ad return (-1);
246 1.11.22.2 ad return (0);
247 1.11.22.2 ad }
248 1.11.22.2 ad
249 1.11.22.2 ad int
250 1.11.22.2 ad mtmatch(parent, match, aux)
251 1.11.22.2 ad struct device *parent;
252 1.11.22.2 ad struct cfdata *match;
253 1.11.22.2 ad void *aux;
254 1.11.22.2 ad {
255 1.11.22.2 ad struct cs80bus_attach_args *ca = aux;
256 1.11.22.2 ad
257 1.11.22.2 ad ca->ca_punit = 0;
258 1.11.22.2 ad return (mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) == 0);
259 1.11.22.2 ad }
260 1.11.22.2 ad
261 1.11.22.2 ad void
262 1.11.22.2 ad mtattach(parent, self, aux)
263 1.11.22.2 ad struct device *parent, *self;
264 1.11.22.2 ad void *aux;
265 1.11.22.2 ad {
266 1.11.22.2 ad struct mt_softc *sc = device_private(self);
267 1.11.22.2 ad struct cs80bus_attach_args *ca = aux;
268 1.11.22.2 ad int type;
269 1.11.22.2 ad
270 1.11.22.2 ad sc->sc_ic = ca->ca_ic;
271 1.11.22.2 ad sc->sc_slave = ca->ca_slave;
272 1.11.22.2 ad
273 1.11.22.2 ad if ((type = mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
274 1.11.22.2 ad return;
275 1.11.22.2 ad
276 1.11.22.2 ad printf(": %s tape\n", mtinfo[type].desc);
277 1.11.22.2 ad
278 1.11.22.2 ad sc->sc_type = type;
279 1.11.22.2 ad sc->sc_flags = MTF_EXISTS;
280 1.11.22.2 ad
281 1.11.22.2 ad bufq_alloc(&sc->sc_tab, "fcfs", 0);
282 1.11.22.2 ad callout_init(&sc->sc_start_ch, 0);
283 1.11.22.2 ad callout_init(&sc->sc_intr_ch, 0);
284 1.11.22.2 ad
285 1.11.22.2 ad if (gpibregister(sc->sc_ic, sc->sc_slave, mtcallback, sc,
286 1.11.22.2 ad &sc->sc_hdl)) {
287 1.11.22.2 ad printf("%s: can't register callback\n", sc->sc_dev.dv_xname);
288 1.11.22.2 ad return;
289 1.11.22.2 ad }
290 1.11.22.2 ad }
291 1.11.22.2 ad
292 1.11.22.2 ad /*
293 1.11.22.2 ad * Perform a read of "Device Status Jump" register and update the
294 1.11.22.2 ad * status if necessary. If status is read, the given "ecmd" is also
295 1.11.22.2 ad * performed, unless "ecmd" is zero. Returns DSJ value, -1 on failure
296 1.11.22.2 ad * and -2 on "temporary" failure.
297 1.11.22.2 ad */
298 1.11.22.2 ad int
299 1.11.22.2 ad mtreaddsj(sc, ecmd)
300 1.11.22.2 ad struct mt_softc *sc;
301 1.11.22.2 ad int ecmd;
302 1.11.22.2 ad {
303 1.11.22.2 ad int retval;
304 1.11.22.2 ad
305 1.11.22.2 ad if (sc->sc_flags & MTF_STATTIMEO)
306 1.11.22.2 ad goto getstats;
307 1.11.22.2 ad retval = gpibrecv(sc->sc_ic,
308 1.11.22.2 ad (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
309 1.11.22.2 ad MTT_DSJ, &(sc->sc_lastdsj), 1);
310 1.11.22.2 ad sc->sc_flags &= ~MTF_DSJTIMEO;
311 1.11.22.2 ad if (retval != 1) {
312 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s can't gpibrecv DSJ",
313 1.11.22.2 ad sc->sc_dev.dv_xname));
314 1.11.22.2 ad if (sc->sc_recvtimeo == 0)
315 1.11.22.2 ad sc->sc_recvtimeo = hz;
316 1.11.22.2 ad if (--sc->sc_recvtimeo == 0)
317 1.11.22.2 ad return (-1);
318 1.11.22.2 ad if (retval == 0)
319 1.11.22.2 ad sc->sc_flags |= MTF_DSJTIMEO;
320 1.11.22.2 ad return (-2);
321 1.11.22.2 ad }
322 1.11.22.2 ad sc->sc_recvtimeo = 0;
323 1.11.22.2 ad sc->sc_statindex = 0;
324 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s readdsj: 0x%x", sc->sc_dev.dv_xname,
325 1.11.22.2 ad sc->sc_lastdsj));
326 1.11.22.2 ad sc->sc_lastecmd = ecmd;
327 1.11.22.2 ad switch (sc->sc_lastdsj) {
328 1.11.22.2 ad case 0:
329 1.11.22.2 ad if (ecmd & MTE_DSJ_FORCE)
330 1.11.22.2 ad break;
331 1.11.22.2 ad return (0);
332 1.11.22.2 ad
333 1.11.22.2 ad case 2:
334 1.11.22.2 ad sc->sc_lastecmd = MTE_COMPLETE;
335 1.11.22.2 ad case 1:
336 1.11.22.2 ad break;
337 1.11.22.2 ad
338 1.11.22.2 ad default:
339 1.11.22.2 ad printf("%s readdsj: DSJ 0x%x\n", sc->sc_dev.dv_xname,
340 1.11.22.2 ad sc->sc_lastdsj);
341 1.11.22.2 ad return (-1);
342 1.11.22.2 ad }
343 1.11.22.2 ad
344 1.11.22.2 ad getstats:
345 1.11.22.2 ad retval = gpibrecv(sc->sc_ic,
346 1.11.22.2 ad (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave, MTT_STAT,
347 1.11.22.2 ad ((char *)&(sc->sc_stat)) + sc->sc_statindex,
348 1.11.22.2 ad sizeof(sc->sc_stat) - sc->sc_statindex);
349 1.11.22.2 ad sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
350 1.11.22.2 ad if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
351 1.11.22.2 ad if (sc->sc_recvtimeo == 0)
352 1.11.22.2 ad sc->sc_recvtimeo = hz;
353 1.11.22.2 ad if (--sc->sc_recvtimeo != 0) {
354 1.11.22.2 ad if (retval >= 0) {
355 1.11.22.2 ad sc->sc_statindex += retval;
356 1.11.22.2 ad sc->sc_flags |= MTF_STATCONT;
357 1.11.22.2 ad }
358 1.11.22.2 ad sc->sc_flags |= MTF_STATTIMEO;
359 1.11.22.2 ad return (-2);
360 1.11.22.2 ad }
361 1.11.22.2 ad printf("%s readdsj: can't read status", sc->sc_dev.dv_xname);
362 1.11.22.2 ad return (-1);
363 1.11.22.2 ad }
364 1.11.22.2 ad sc->sc_recvtimeo = 0;
365 1.11.22.2 ad sc->sc_statindex = 0;
366 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s readdsj: status is %x %x %x %x %x %x",
367 1.11.22.2 ad sc->sc_dev.dv_xname,
368 1.11.22.2 ad sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
369 1.11.22.2 ad sc->sc_stat4, sc->sc_stat5, sc->sc_stat6));
370 1.11.22.2 ad if (sc->sc_lastecmd)
371 1.11.22.2 ad (void) gpibsend(sc->sc_ic, sc->sc_slave,
372 1.11.22.2 ad MTL_ECMD, &(sc->sc_lastecmd), 1);
373 1.11.22.2 ad return ((int) sc->sc_lastdsj);
374 1.11.22.2 ad }
375 1.11.22.2 ad
376 1.11.22.2 ad int
377 1.11.22.2 ad mtopen(dev, flag, mode, l)
378 1.11.22.2 ad dev_t dev;
379 1.11.22.2 ad int flag, mode;
380 1.11.22.2 ad struct lwp *l;
381 1.11.22.2 ad {
382 1.11.22.2 ad struct mt_softc *sc;
383 1.11.22.2 ad int req_den;
384 1.11.22.2 ad int error;
385 1.11.22.2 ad
386 1.11.22.2 ad sc = device_lookup(&mt_cd, MTUNIT(dev));
387 1.11.22.2 ad if (sc == NULL || (sc->sc_flags & MTF_EXISTS) == 0)
388 1.11.22.2 ad return (ENXIO);
389 1.11.22.2 ad
390 1.11.22.2 ad if (sc->sc_flags & MTF_OPEN)
391 1.11.22.2 ad return (EBUSY);
392 1.11.22.2 ad
393 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s open: flags 0x%x", sc->sc_dev.dv_xname,
394 1.11.22.2 ad sc->sc_flags));
395 1.11.22.2 ad
396 1.11.22.2 ad sc->sc_flags |= MTF_OPEN;
397 1.11.22.2 ad sc->sc_ttyp = tprintf_open(l->l_proc);
398 1.11.22.2 ad if ((sc->sc_flags & MTF_ALIVE) == 0) {
399 1.11.22.2 ad error = mtcommand(dev, MTRESET, 0);
400 1.11.22.2 ad if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
401 1.11.22.2 ad goto errout;
402 1.11.22.2 ad if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
403 1.11.22.2 ad (void) mtcommand(dev, MTREW, 0);
404 1.11.22.2 ad }
405 1.11.22.2 ad for (;;) {
406 1.11.22.2 ad if ((error = mtcommand(dev, MTNOP, 0)) != 0)
407 1.11.22.2 ad goto errout;
408 1.11.22.2 ad if (!(sc->sc_flags & MTF_REW))
409 1.11.22.2 ad break;
410 1.11.22.2 ad if (tsleep((void *) &lbolt, PCATCH | (PZERO + 1),
411 1.11.22.2 ad "mt", 0) != 0) {
412 1.11.22.2 ad error = EINTR;
413 1.11.22.2 ad goto errout;
414 1.11.22.2 ad }
415 1.11.22.2 ad }
416 1.11.22.2 ad if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
417 1.11.22.2 ad error = EROFS;
418 1.11.22.2 ad goto errout;
419 1.11.22.2 ad }
420 1.11.22.2 ad if (!(sc->sc_stat1 & SR1_ONLINE)) {
421 1.11.22.2 ad uprintf("%s: not online\n", sc->sc_dev.dv_xname);
422 1.11.22.2 ad error = EIO;
423 1.11.22.2 ad goto errout;
424 1.11.22.2 ad }
425 1.11.22.2 ad /*
426 1.11.22.2 ad * Select density:
427 1.11.22.2 ad * - find out what density the drive is set to
428 1.11.22.2 ad * (i.e. the density of the current tape)
429 1.11.22.2 ad * - if we are going to write
430 1.11.22.2 ad * - if we're not at the beginning of the tape
431 1.11.22.2 ad * - complain if we want to change densities
432 1.11.22.2 ad * - otherwise, select the mtcommand to set the density
433 1.11.22.2 ad *
434 1.11.22.2 ad * If the drive doesn't support it then don't change the recorded
435 1.11.22.2 ad * density.
436 1.11.22.2 ad *
437 1.11.22.2 ad * The original MOREbsd code had these additional conditions
438 1.11.22.2 ad * for the mid-tape change
439 1.11.22.2 ad *
440 1.11.22.2 ad * req_den != T_BADBPI &&
441 1.11.22.2 ad * sc->sc_density != T_6250BPI
442 1.11.22.2 ad *
443 1.11.22.2 ad * which suggests that it would be possible to write multiple
444 1.11.22.2 ad * densities if req_den == T_BAD_BPI or the current tape
445 1.11.22.2 ad * density was 6250. Testing of our 7980 suggests that the
446 1.11.22.2 ad * device cannot change densities mid-tape.
447 1.11.22.2 ad *
448 1.11.22.2 ad * ajv (at) comp.vuw.ac.nz
449 1.11.22.2 ad */
450 1.11.22.2 ad sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
451 1.11.22.2 ad (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
452 1.11.22.2 ad (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
453 1.11.22.2 ad req_den = (dev & T_DENSEL);
454 1.11.22.2 ad
455 1.11.22.2 ad if (flag & FWRITE) {
456 1.11.22.2 ad if (!(sc->sc_stat1 & SR1_BOT)) {
457 1.11.22.2 ad if (sc->sc_density != req_den) {
458 1.11.22.2 ad uprintf("%s: can't change density mid-tape\n",
459 1.11.22.2 ad sc->sc_dev.dv_xname);
460 1.11.22.2 ad error = EIO;
461 1.11.22.2 ad goto errout;
462 1.11.22.2 ad }
463 1.11.22.2 ad }
464 1.11.22.2 ad else {
465 1.11.22.2 ad int mtset_density =
466 1.11.22.2 ad (req_den == T_800BPI ? MTSET800BPI : (
467 1.11.22.2 ad req_den == T_1600BPI ? MTSET1600BPI : (
468 1.11.22.2 ad req_den == T_6250BPI ? MTSET6250BPI : (
469 1.11.22.2 ad sc->sc_type == MT7980ID
470 1.11.22.2 ad ? MTSET6250DC
471 1.11.22.2 ad : MTSET6250BPI))));
472 1.11.22.2 ad if (mtcommand(dev, mtset_density, 0) == 0)
473 1.11.22.2 ad sc->sc_density = req_den;
474 1.11.22.2 ad }
475 1.11.22.2 ad }
476 1.11.22.2 ad return (0);
477 1.11.22.2 ad errout:
478 1.11.22.2 ad sc->sc_flags &= ~MTF_OPEN;
479 1.11.22.2 ad return (error);
480 1.11.22.2 ad }
481 1.11.22.2 ad
482 1.11.22.2 ad int
483 1.11.22.2 ad mtclose(dev, flag, fmt, l)
484 1.11.22.2 ad dev_t dev;
485 1.11.22.2 ad int flag, fmt;
486 1.11.22.2 ad struct lwp *l;
487 1.11.22.2 ad {
488 1.11.22.2 ad struct mt_softc *sc;
489 1.11.22.2 ad
490 1.11.22.2 ad sc = device_lookup(&mt_cd, MTUNIT(dev));
491 1.11.22.2 ad if (sc == NULL)
492 1.11.22.2 ad return (ENXIO);
493 1.11.22.2 ad
494 1.11.22.2 ad if (sc->sc_flags & MTF_WRT) {
495 1.11.22.2 ad (void) mtcommand(dev, MTWEOF, 2);
496 1.11.22.2 ad (void) mtcommand(dev, MTBSF, 0);
497 1.11.22.2 ad }
498 1.11.22.2 ad if ((minor(dev) & T_NOREWIND) == 0)
499 1.11.22.2 ad (void) mtcommand(dev, MTREW, 0);
500 1.11.22.2 ad sc->sc_flags &= ~MTF_OPEN;
501 1.11.22.2 ad tprintf_close(sc->sc_ttyp);
502 1.11.22.2 ad return (0);
503 1.11.22.2 ad }
504 1.11.22.2 ad
505 1.11.22.2 ad int
506 1.11.22.2 ad mtcommand(dev, cmd, cnt)
507 1.11.22.2 ad dev_t dev;
508 1.11.22.2 ad int cmd;
509 1.11.22.2 ad int cnt;
510 1.11.22.2 ad {
511 1.11.22.2 ad struct mt_softc *sc;
512 1.11.22.2 ad struct buf *bp;
513 1.11.22.2 ad int error = 0;
514 1.11.22.2 ad
515 1.11.22.2 ad sc = device_lookup(&mt_cd, MTUNIT(dev));
516 1.11.22.2 ad bp = &sc->sc_bufstore;
517 1.11.22.2 ad
518 1.11.22.2 ad if (bp->b_flags & B_BUSY)
519 1.11.22.2 ad return (EBUSY);
520 1.11.22.2 ad
521 1.11.22.2 ad bp->b_cmd = cmd;
522 1.11.22.2 ad bp->b_dev = dev;
523 1.11.22.2 ad do {
524 1.11.22.2 ad bp->b_flags = B_BUSY | B_CMD;
525 1.11.22.2 ad mtstrategy(bp);
526 1.11.22.2 ad biowait(bp);
527 1.11.22.2 ad if (bp->b_error != 0) {
528 1.11.22.2 ad error = (int) (unsigned) bp->b_error;
529 1.11.22.2 ad break;
530 1.11.22.2 ad }
531 1.11.22.2 ad } while (--cnt > 0);
532 1.11.22.2 ad #if 0
533 1.11.22.2 ad bp->b_flags = 0 /*&= ~B_BUSY*/;
534 1.11.22.2 ad #else
535 1.11.22.2 ad bp->b_flags &= ~B_BUSY;
536 1.11.22.2 ad #endif
537 1.11.22.2 ad return (error);
538 1.11.22.2 ad }
539 1.11.22.2 ad
540 1.11.22.2 ad /*
541 1.11.22.2 ad * Only thing to check here is for legal record lengths (writes only).
542 1.11.22.2 ad */
543 1.11.22.2 ad void
544 1.11.22.2 ad mtstrategy(bp)
545 1.11.22.2 ad struct buf *bp;
546 1.11.22.2 ad {
547 1.11.22.2 ad struct mt_softc *sc;
548 1.11.22.2 ad int s;
549 1.11.22.2 ad
550 1.11.22.2 ad sc = device_lookup(&mt_cd, MTUNIT(bp->b_dev));
551 1.11.22.2 ad
552 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s strategy", sc->sc_dev.dv_xname));
553 1.11.22.2 ad
554 1.11.22.2 ad if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
555 1.11.22.2 ad #define WRITE_BITS_IGNORED 8
556 1.11.22.2 ad #if 0
557 1.11.22.2 ad if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
558 1.11.22.2 ad tprintf(sc->sc_ttyp,
559 1.11.22.2 ad "%s: write record must be multiple of %d\n",
560 1.11.22.2 ad sc->sc_dev.dv_xname, 1 << WRITE_BITS_IGNORED);
561 1.11.22.2 ad goto error;
562 1.11.22.2 ad }
563 1.11.22.2 ad #endif
564 1.11.22.2 ad s = 16 * 1024;
565 1.11.22.2 ad if (sc->sc_stat2 & SR2_LONGREC) {
566 1.11.22.2 ad switch (sc->sc_density) {
567 1.11.22.2 ad case T_1600BPI:
568 1.11.22.2 ad s = 32 * 1024;
569 1.11.22.2 ad break;
570 1.11.22.2 ad
571 1.11.22.2 ad case T_6250BPI:
572 1.11.22.2 ad case T_BADBPI:
573 1.11.22.2 ad s = 60 * 1024;
574 1.11.22.2 ad break;
575 1.11.22.2 ad }
576 1.11.22.2 ad }
577 1.11.22.2 ad if (bp->b_bcount > s) {
578 1.11.22.2 ad tprintf(sc->sc_ttyp,
579 1.11.22.2 ad "%s: write record (%d) too big: limit (%d)\n",
580 1.11.22.2 ad sc->sc_dev.dv_xname, bp->b_bcount, s);
581 1.11.22.2 ad #if 0 /* XXX see above */
582 1.11.22.2 ad error:
583 1.11.22.2 ad #endif
584 1.11.22.2 ad bp->b_error = EIO;
585 1.11.22.2 ad biodone(bp);
586 1.11.22.2 ad return;
587 1.11.22.2 ad }
588 1.11.22.2 ad }
589 1.11.22.2 ad s = splbio();
590 1.11.22.2 ad BUFQ_PUT(sc->sc_tab, bp);
591 1.11.22.2 ad if (sc->sc_active == 0) {
592 1.11.22.2 ad sc->sc_active = 1;
593 1.11.22.2 ad mtustart(sc);
594 1.11.22.2 ad }
595 1.11.22.2 ad splx(s);
596 1.11.22.2 ad }
597 1.11.22.2 ad
598 1.11.22.2 ad void
599 1.11.22.2 ad mtustart(sc)
600 1.11.22.2 ad struct mt_softc *sc;
601 1.11.22.2 ad {
602 1.11.22.2 ad
603 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s ustart", sc->sc_dev.dv_xname));
604 1.11.22.2 ad if (gpibrequest(sc->sc_ic, sc->sc_hdl))
605 1.11.22.2 ad mtstart(sc);
606 1.11.22.2 ad }
607 1.11.22.2 ad
608 1.11.22.2 ad void
609 1.11.22.2 ad mtcallback(v, action)
610 1.11.22.2 ad void *v;
611 1.11.22.2 ad int action;
612 1.11.22.2 ad {
613 1.11.22.2 ad struct mt_softc *sc = v;
614 1.11.22.2 ad
615 1.11.22.2 ad DPRINTF(MDB_FOLLOW, ("mtcallback: v=%p, action=%d\n", v, action));
616 1.11.22.2 ad
617 1.11.22.2 ad switch (action) {
618 1.11.22.2 ad case GPIBCBF_START:
619 1.11.22.2 ad mtstart(sc);
620 1.11.22.2 ad break;
621 1.11.22.2 ad case GPIBCBF_INTR:
622 1.11.22.2 ad mtintr(sc);
623 1.11.22.2 ad break;
624 1.11.22.2 ad #ifdef DEBUG
625 1.11.22.2 ad default:
626 1.11.22.2 ad printf("mtcallback: unknown action %d\n", action);
627 1.11.22.2 ad break;
628 1.11.22.2 ad #endif
629 1.11.22.2 ad }
630 1.11.22.2 ad }
631 1.11.22.2 ad
632 1.11.22.2 ad void
633 1.11.22.2 ad mtintr_callout(arg)
634 1.11.22.2 ad void *arg;
635 1.11.22.2 ad {
636 1.11.22.2 ad struct mt_softc *sc = arg;
637 1.11.22.2 ad int s = splbio();
638 1.11.22.2 ad
639 1.11.22.2 ad gpibppclear(sc->sc_ic);
640 1.11.22.2 ad mtintr(sc);
641 1.11.22.2 ad splx(s);
642 1.11.22.2 ad }
643 1.11.22.2 ad
644 1.11.22.2 ad void
645 1.11.22.2 ad mtstart_callout(arg)
646 1.11.22.2 ad void *arg;
647 1.11.22.2 ad {
648 1.11.22.2 ad int s = splbio();
649 1.11.22.2 ad
650 1.11.22.2 ad mtstart((struct mt_softc *)arg);
651 1.11.22.2 ad splx(s);
652 1.11.22.2 ad }
653 1.11.22.2 ad
654 1.11.22.2 ad void
655 1.11.22.2 ad mtstart(sc)
656 1.11.22.2 ad struct mt_softc *sc;
657 1.11.22.2 ad {
658 1.11.22.2 ad struct buf *bp;
659 1.11.22.2 ad short cmdcount = 1;
660 1.11.22.2 ad u_char cmdbuf[2];
661 1.11.22.2 ad
662 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s start", sc->sc_dev.dv_xname));
663 1.11.22.2 ad sc->sc_flags &= ~MTF_WRT;
664 1.11.22.2 ad bp = BUFQ_PEEK(sc->sc_tab);
665 1.11.22.2 ad if ((sc->sc_flags & MTF_ALIVE) == 0 &&
666 1.11.22.2 ad ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
667 1.11.22.2 ad goto fatalerror;
668 1.11.22.2 ad
669 1.11.22.2 ad if (sc->sc_flags & MTF_REW) {
670 1.11.22.2 ad if (!gpibpptest(sc->sc_ic, sc->sc_slave))
671 1.11.22.2 ad goto stillrew;
672 1.11.22.2 ad switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
673 1.11.22.2 ad case 0:
674 1.11.22.2 ad case 1:
675 1.11.22.2 ad stillrew:
676 1.11.22.2 ad if ((sc->sc_stat1 & SR1_BOT) ||
677 1.11.22.2 ad !(sc->sc_stat1 & SR1_ONLINE)) {
678 1.11.22.2 ad sc->sc_flags &= ~MTF_REW;
679 1.11.22.2 ad break;
680 1.11.22.2 ad }
681 1.11.22.2 ad case -2:
682 1.11.22.2 ad /*
683 1.11.22.2 ad * -2 means "timeout" reading DSJ, which is probably
684 1.11.22.2 ad * temporary. This is considered OK when doing a NOP,
685 1.11.22.2 ad * but not otherwise.
686 1.11.22.2 ad */
687 1.11.22.2 ad if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
688 1.11.22.2 ad callout_reset(&sc->sc_start_ch, hz >> 5,
689 1.11.22.2 ad mtstart_callout, sc);
690 1.11.22.2 ad return;
691 1.11.22.2 ad }
692 1.11.22.2 ad case 2:
693 1.11.22.2 ad if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
694 1.11.22.2 ad bp->b_error = EBUSY;
695 1.11.22.2 ad goto done;
696 1.11.22.2 ad }
697 1.11.22.2 ad goto done;
698 1.11.22.2 ad
699 1.11.22.2 ad default:
700 1.11.22.2 ad goto fatalerror;
701 1.11.22.2 ad }
702 1.11.22.2 ad }
703 1.11.22.2 ad if (bp->b_flags & B_CMD) {
704 1.11.22.2 ad if (sc->sc_flags & MTF_PASTEOT) {
705 1.11.22.2 ad switch(bp->b_cmd) {
706 1.11.22.2 ad case MTFSF:
707 1.11.22.2 ad case MTWEOF:
708 1.11.22.2 ad case MTFSR:
709 1.11.22.2 ad bp->b_error = ENOSPC;
710 1.11.22.2 ad goto done;
711 1.11.22.2 ad
712 1.11.22.2 ad case MTBSF:
713 1.11.22.2 ad case MTOFFL:
714 1.11.22.2 ad case MTBSR:
715 1.11.22.2 ad case MTREW:
716 1.11.22.2 ad sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
717 1.11.22.2 ad break;
718 1.11.22.2 ad }
719 1.11.22.2 ad }
720 1.11.22.2 ad switch(bp->b_cmd) {
721 1.11.22.2 ad case MTFSF:
722 1.11.22.2 ad if (sc->sc_flags & MTF_HITEOF)
723 1.11.22.2 ad goto done;
724 1.11.22.2 ad cmdbuf[0] = MTTC_FSF;
725 1.11.22.2 ad break;
726 1.11.22.2 ad
727 1.11.22.2 ad case MTBSF:
728 1.11.22.2 ad if (sc->sc_flags & MTF_HITBOF)
729 1.11.22.2 ad goto done;
730 1.11.22.2 ad cmdbuf[0] = MTTC_BSF;
731 1.11.22.2 ad break;
732 1.11.22.2 ad
733 1.11.22.2 ad case MTOFFL:
734 1.11.22.2 ad sc->sc_flags |= MTF_REW;
735 1.11.22.2 ad cmdbuf[0] = MTTC_REWOFF;
736 1.11.22.2 ad break;
737 1.11.22.2 ad
738 1.11.22.2 ad case MTWEOF:
739 1.11.22.2 ad cmdbuf[0] = MTTC_WFM;
740 1.11.22.2 ad break;
741 1.11.22.2 ad
742 1.11.22.2 ad case MTBSR:
743 1.11.22.2 ad cmdbuf[0] = MTTC_BSR;
744 1.11.22.2 ad break;
745 1.11.22.2 ad
746 1.11.22.2 ad case MTFSR:
747 1.11.22.2 ad cmdbuf[0] = MTTC_FSR;
748 1.11.22.2 ad break;
749 1.11.22.2 ad
750 1.11.22.2 ad case MTREW:
751 1.11.22.2 ad sc->sc_flags |= MTF_REW;
752 1.11.22.2 ad cmdbuf[0] = MTTC_REW;
753 1.11.22.2 ad break;
754 1.11.22.2 ad
755 1.11.22.2 ad case MTNOP:
756 1.11.22.2 ad /*
757 1.11.22.2 ad * NOP is supposed to set status bits.
758 1.11.22.2 ad * Force readdsj to do it.
759 1.11.22.2 ad */
760 1.11.22.2 ad switch (mtreaddsj(sc,
761 1.11.22.2 ad MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
762 1.11.22.2 ad default:
763 1.11.22.2 ad goto done;
764 1.11.22.2 ad
765 1.11.22.2 ad case -1:
766 1.11.22.2 ad /*
767 1.11.22.2 ad * If this fails, perform a device clear
768 1.11.22.2 ad * to fix any protocol problems and (most
769 1.11.22.2 ad * likely) get the status.
770 1.11.22.2 ad */
771 1.11.22.2 ad bp->b_cmd = MTRESET;
772 1.11.22.2 ad break;
773 1.11.22.2 ad
774 1.11.22.2 ad case -2:
775 1.11.22.2 ad callout_reset(&sc->sc_start_ch, hz >> 5,
776 1.11.22.2 ad mtstart_callout, sc);
777 1.11.22.2 ad return;
778 1.11.22.2 ad }
779 1.11.22.2 ad
780 1.11.22.2 ad case MTRESET:
781 1.11.22.2 ad /*
782 1.11.22.2 ad * 1) selected device clear (send with "-2" secondary)
783 1.11.22.2 ad * 2) set timeout, then wait for "service request"
784 1.11.22.2 ad * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
785 1.11.22.2 ad */
786 1.11.22.2 ad if (gpibsend(sc->sc_ic, sc->sc_slave, -2, NULL, 0)){
787 1.11.22.2 ad printf("%s can't reset", sc->sc_dev.dv_xname);
788 1.11.22.2 ad goto fatalerror;
789 1.11.22.2 ad }
790 1.11.22.2 ad callout_reset(&sc->sc_intr_ch, 4*hz, mtintr_callout,
791 1.11.22.2 ad sc);
792 1.11.22.2 ad gpibawait(sc->sc_ic);
793 1.11.22.2 ad return;
794 1.11.22.2 ad
795 1.11.22.2 ad case MTSET800BPI:
796 1.11.22.2 ad cmdbuf[0] = MTTC_800;
797 1.11.22.2 ad break;
798 1.11.22.2 ad
799 1.11.22.2 ad case MTSET1600BPI:
800 1.11.22.2 ad cmdbuf[0] = MTTC_1600;
801 1.11.22.2 ad break;
802 1.11.22.2 ad
803 1.11.22.2 ad case MTSET6250BPI:
804 1.11.22.2 ad cmdbuf[0] = MTTC_6250;
805 1.11.22.2 ad break;
806 1.11.22.2 ad
807 1.11.22.2 ad case MTSET6250DC:
808 1.11.22.2 ad cmdbuf[0] = MTTC_DC6250;
809 1.11.22.2 ad break;
810 1.11.22.2 ad }
811 1.11.22.2 ad } else {
812 1.11.22.2 ad if (sc->sc_flags & MTF_PASTEOT) {
813 1.11.22.2 ad bp->b_error = ENOSPC;
814 1.11.22.2 ad goto done;
815 1.11.22.2 ad }
816 1.11.22.2 ad if (bp->b_flags & B_READ) {
817 1.11.22.2 ad sc->sc_flags |= MTF_IO;
818 1.11.22.2 ad cmdbuf[0] = MTTC_READ;
819 1.11.22.2 ad } else {
820 1.11.22.2 ad sc->sc_flags |= MTF_WRT | MTF_IO;
821 1.11.22.2 ad cmdbuf[0] = MTTC_WRITE;
822 1.11.22.2 ad cmdbuf[1] = (bp->b_bcount +((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
823 1.11.22.2 ad cmdcount = 2;
824 1.11.22.2 ad }
825 1.11.22.2 ad }
826 1.11.22.2 ad if (gpibsend(sc->sc_ic, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
827 1.11.22.2 ad == cmdcount) {
828 1.11.22.2 ad if (sc->sc_flags & MTF_REW)
829 1.11.22.2 ad goto done;
830 1.11.22.2 ad gpibawait(sc->sc_ic);
831 1.11.22.2 ad return;
832 1.11.22.2 ad }
833 1.11.22.2 ad fatalerror:
834 1.11.22.2 ad /*
835 1.11.22.2 ad * If anything fails, the drive is probably hosed, so mark it not
836 1.11.22.2 ad * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
837 1.11.22.2 ad * if, last we heard, it was REWinding, remember that).
838 1.11.22.2 ad */
839 1.11.22.2 ad sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
840 1.11.22.2 ad bp->b_error = EIO;
841 1.11.22.2 ad done:
842 1.11.22.2 ad sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
843 1.11.22.2 ad (void)BUFQ_GET(sc->sc_tab);
844 1.11.22.2 ad biodone(bp);
845 1.11.22.2 ad gpibrelease(sc->sc_ic, sc->sc_hdl);
846 1.11.22.2 ad if ((bp = BUFQ_PEEK(sc->sc_tab)) == NULL)
847 1.11.22.2 ad sc->sc_active = 0;
848 1.11.22.2 ad else
849 1.11.22.2 ad mtustart(sc);
850 1.11.22.2 ad }
851 1.11.22.2 ad
852 1.11.22.2 ad void
853 1.11.22.2 ad mtintr(sc)
854 1.11.22.2 ad struct mt_softc *sc;
855 1.11.22.2 ad {
856 1.11.22.2 ad struct buf *bp;
857 1.11.22.2 ad int slave, dir, i;
858 1.11.22.2 ad u_char cmdbuf[4];
859 1.11.22.2 ad
860 1.11.22.2 ad slave = sc->sc_slave;
861 1.11.22.2 ad
862 1.11.22.2 ad bp = BUFQ_PEEK(sc->sc_tab);
863 1.11.22.2 ad if (bp == NULL) {
864 1.11.22.2 ad printf("%s intr: bp == NULL", sc->sc_dev.dv_xname);
865 1.11.22.2 ad return;
866 1.11.22.2 ad }
867 1.11.22.2 ad
868 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s intr", sc->sc_dev.dv_xname));
869 1.11.22.2 ad
870 1.11.22.2 ad /*
871 1.11.22.2 ad * Some operation completed. Read status bytes and report errors.
872 1.11.22.2 ad * Clear EOF flags here `cause they're set once on specific conditions
873 1.11.22.2 ad * below when a command succeeds.
874 1.11.22.2 ad * A DSJ of 2 always means keep waiting. If the command was READ
875 1.11.22.2 ad * (and we're in data DMA phase) stop data transfer first.
876 1.11.22.2 ad */
877 1.11.22.2 ad sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
878 1.11.22.2 ad if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
879 1.11.22.2 ad !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
880 1.11.22.2 ad cmdbuf[0] = MTE_STOP;
881 1.11.22.2 ad (void) gpibsend(sc->sc_ic, slave, MTL_ECMD,cmdbuf,1);
882 1.11.22.2 ad }
883 1.11.22.2 ad switch (mtreaddsj(sc, 0)) {
884 1.11.22.2 ad case 0:
885 1.11.22.2 ad break;
886 1.11.22.2 ad
887 1.11.22.2 ad case 1:
888 1.11.22.2 ad /*
889 1.11.22.2 ad * If we're in the middle of a READ/WRITE and have yet to
890 1.11.22.2 ad * start the data transfer, a DSJ of one should terminate it.
891 1.11.22.2 ad */
892 1.11.22.2 ad sc->sc_flags &= ~MTF_IO;
893 1.11.22.2 ad break;
894 1.11.22.2 ad
895 1.11.22.2 ad case 2:
896 1.11.22.2 ad (void) gpibawait(sc->sc_ic);
897 1.11.22.2 ad return;
898 1.11.22.2 ad
899 1.11.22.2 ad case -2:
900 1.11.22.2 ad /*
901 1.11.22.2 ad * -2 means that the drive failed to respond quickly enough
902 1.11.22.2 ad * to the request for DSJ. It's probably just "busy" figuring
903 1.11.22.2 ad * it out and will know in a little bit...
904 1.11.22.2 ad */
905 1.11.22.2 ad callout_reset(&sc->sc_intr_ch, hz >> 5, mtintr_callout, sc);
906 1.11.22.2 ad return;
907 1.11.22.2 ad
908 1.11.22.2 ad default:
909 1.11.22.2 ad printf("%s intr: can't get drive stat", sc->sc_dev.dv_xname);
910 1.11.22.2 ad goto error;
911 1.11.22.2 ad }
912 1.11.22.2 ad if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
913 1.11.22.2 ad i = sc->sc_stat4 & SR4_ERCLMASK;
914 1.11.22.2 ad printf("%s: %s error, retry %d, SR2/3 %x/%x, code %d",
915 1.11.22.2 ad sc->sc_dev.dv_xname, i == SR4_DEVICE ? "device" :
916 1.11.22.2 ad (i == SR4_PROTOCOL ? "protocol" :
917 1.11.22.2 ad (i == SR4_SELFTEST ? "selftest" : "unknown")),
918 1.11.22.2 ad sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
919 1.11.22.2 ad sc->sc_stat3, sc->sc_stat5);
920 1.11.22.2 ad
921 1.11.22.2 ad if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
922 1.11.22.2 ad callout_stop(&sc->sc_intr_ch);
923 1.11.22.2 ad if (sc->sc_stat3 & SR3_POWERUP)
924 1.11.22.2 ad sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
925 1.11.22.2 ad goto error;
926 1.11.22.2 ad }
927 1.11.22.2 ad /*
928 1.11.22.2 ad * Report and clear any soft errors.
929 1.11.22.2 ad */
930 1.11.22.2 ad if (sc->sc_stat1 & SR1_SOFTERR) {
931 1.11.22.2 ad printf("%s: soft error, retry %d\n", sc->sc_dev.dv_xname,
932 1.11.22.2 ad sc->sc_stat4 & SR4_RETRYMASK);
933 1.11.22.2 ad sc->sc_stat1 &= ~SR1_SOFTERR;
934 1.11.22.2 ad }
935 1.11.22.2 ad /*
936 1.11.22.2 ad * We've initiated a read or write, but haven't actually started to
937 1.11.22.2 ad * DMA the data yet. At this point, the drive's ready.
938 1.11.22.2 ad */
939 1.11.22.2 ad if (sc->sc_flags & MTF_IO) {
940 1.11.22.2 ad sc->sc_flags &= ~MTF_IO;
941 1.11.22.2 ad dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
942 1.11.22.2 ad gpibxfer(sc->sc_ic, slave,
943 1.11.22.2 ad dir == GPIB_READ ? MTT_READ : MTL_WRITE,
944 1.11.22.2 ad bp->b_data, bp->b_bcount, dir, dir == GPIB_READ);
945 1.11.22.2 ad return;
946 1.11.22.2 ad }
947 1.11.22.2 ad /*
948 1.11.22.2 ad * Check for End Of Tape - we're allowed to hit EOT and then write (or
949 1.11.22.2 ad * read) one more record. If we get here and have not already hit EOT,
950 1.11.22.2 ad * return ENOSPC to inform the process that it's hit it. If we get
951 1.11.22.2 ad * here and HAVE already hit EOT, don't allow any more operations that
952 1.11.22.2 ad * move the tape forward.
953 1.11.22.2 ad */
954 1.11.22.2 ad if (sc->sc_stat1 & SR1_EOT) {
955 1.11.22.2 ad if (sc->sc_flags & MTF_ATEOT)
956 1.11.22.2 ad sc->sc_flags |= MTF_PASTEOT;
957 1.11.22.2 ad else {
958 1.11.22.2 ad bp->b_error = ENOSPC;
959 1.11.22.2 ad sc->sc_flags |= MTF_ATEOT;
960 1.11.22.2 ad }
961 1.11.22.2 ad }
962 1.11.22.2 ad /*
963 1.11.22.2 ad * If a motion command was being executed, check for Tape Marks.
964 1.11.22.2 ad * If we were doing data, make sure we got the right amount, and
965 1.11.22.2 ad * check for hitting tape marks on reads.
966 1.11.22.2 ad */
967 1.11.22.2 ad if (bp->b_flags & B_CMD) {
968 1.11.22.2 ad if (sc->sc_stat1 & SR1_EOF) {
969 1.11.22.2 ad if (bp->b_cmd == MTFSR)
970 1.11.22.2 ad sc->sc_flags |= MTF_HITEOF;
971 1.11.22.2 ad if (bp->b_cmd == MTBSR)
972 1.11.22.2 ad sc->sc_flags |= MTF_HITBOF;
973 1.11.22.2 ad }
974 1.11.22.2 ad if (bp->b_cmd == MTRESET) {
975 1.11.22.2 ad callout_stop(&sc->sc_intr_ch);
976 1.11.22.2 ad sc->sc_flags |= MTF_ALIVE;
977 1.11.22.2 ad }
978 1.11.22.2 ad } else {
979 1.11.22.2 ad i = gpibrecv(sc->sc_ic, slave, MTT_BCNT, cmdbuf, 2);
980 1.11.22.2 ad if (i != 2) {
981 1.11.22.2 ad printf("%s intr: can't get xfer length\n",
982 1.11.22.2 ad sc->sc_dev.dv_xname);
983 1.11.22.2 ad goto error;
984 1.11.22.2 ad }
985 1.11.22.2 ad i = (int) *((u_short *) cmdbuf);
986 1.11.22.2 ad if (i <= bp->b_bcount) {
987 1.11.22.2 ad if (i == 0)
988 1.11.22.2 ad sc->sc_flags |= MTF_HITEOF;
989 1.11.22.2 ad bp->b_resid = bp->b_bcount - i;
990 1.11.22.2 ad DPRINTF(MDB_ANY, ("%s intr: bcount %ld, resid %ld",
991 1.11.22.2 ad sc->sc_dev.dv_xname, bp->b_bcount, bp->b_resid));
992 1.11.22.2 ad } else {
993 1.11.22.2 ad tprintf(sc->sc_ttyp,
994 1.11.22.2 ad "%s: record (%d) larger than wanted (%d)\n",
995 1.11.22.2 ad sc->sc_dev.dv_xname, i, bp->b_bcount);
996 1.11.22.2 ad error:
997 1.11.22.2 ad sc->sc_flags &= ~MTF_IO;
998 1.11.22.2 ad bp->b_error = EIO;
999 1.11.22.2 ad }
1000 1.11.22.2 ad }
1001 1.11.22.2 ad /*
1002 1.11.22.2 ad * The operation is completely done.
1003 1.11.22.2 ad * Let the drive know with an END command.
1004 1.11.22.2 ad */
1005 1.11.22.2 ad cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
1006 1.11.22.2 ad (void) gpibsend(sc->sc_ic, slave, MTL_ECMD, cmdbuf, 1);
1007 1.11.22.2 ad bp->b_flags &= ~B_CMD;
1008 1.11.22.2 ad (void)BUFQ_GET(sc->sc_tab);
1009 1.11.22.2 ad biodone(bp);
1010 1.11.22.2 ad gpibrelease(sc->sc_ic, sc->sc_hdl);
1011 1.11.22.2 ad if (BUFQ_PEEK(sc->sc_tab) == NULL)
1012 1.11.22.2 ad sc->sc_active = 0;
1013 1.11.22.2 ad else
1014 1.11.22.2 ad mtustart(sc);
1015 1.11.22.2 ad }
1016 1.11.22.2 ad
1017 1.11.22.2 ad int
1018 1.11.22.2 ad mtread(dev, uio, flags)
1019 1.11.22.2 ad dev_t dev;
1020 1.11.22.2 ad struct uio *uio;
1021 1.11.22.2 ad int flags;
1022 1.11.22.2 ad {
1023 1.11.22.2 ad struct mt_softc *sc;
1024 1.11.22.2 ad
1025 1.11.22.2 ad sc = device_lookup(&mt_cd, MTUNIT(dev));
1026 1.11.22.2 ad
1027 1.11.22.2 ad return (physio(mtstrategy, &sc->sc_bufstore,
1028 1.11.22.2 ad dev, B_READ, minphys, uio));
1029 1.11.22.2 ad }
1030 1.11.22.2 ad
1031 1.11.22.2 ad int
1032 1.11.22.2 ad mtwrite(dev, uio, flags)
1033 1.11.22.2 ad dev_t dev;
1034 1.11.22.2 ad struct uio *uio;
1035 1.11.22.2 ad int flags;
1036 1.11.22.2 ad {
1037 1.11.22.2 ad struct mt_softc *sc;
1038 1.11.22.2 ad
1039 1.11.22.2 ad sc = device_lookup(&mt_cd, MTUNIT(dev));
1040 1.11.22.2 ad
1041 1.11.22.2 ad return (physio(mtstrategy, &sc->sc_bufstore,
1042 1.11.22.2 ad dev, B_WRITE, minphys, uio));
1043 1.11.22.2 ad }
1044 1.11.22.2 ad
1045 1.11.22.2 ad int
1046 1.11.22.2 ad mtioctl(dev, cmd, data, flag, l)
1047 1.11.22.2 ad dev_t dev;
1048 1.11.22.2 ad u_long cmd;
1049 1.11.22.2 ad void *data;
1050 1.11.22.2 ad int flag;
1051 1.11.22.2 ad struct lwp *l;
1052 1.11.22.2 ad {
1053 1.11.22.2 ad struct mtop *op;
1054 1.11.22.2 ad int cnt;
1055 1.11.22.2 ad
1056 1.11.22.2 ad switch (cmd) {
1057 1.11.22.2 ad case MTIOCTOP:
1058 1.11.22.2 ad op = (struct mtop *)data;
1059 1.11.22.2 ad switch(op->mt_op) {
1060 1.11.22.2 ad case MTWEOF:
1061 1.11.22.2 ad case MTFSF:
1062 1.11.22.2 ad case MTBSR:
1063 1.11.22.2 ad case MTBSF:
1064 1.11.22.2 ad case MTFSR:
1065 1.11.22.2 ad cnt = op->mt_count;
1066 1.11.22.2 ad break;
1067 1.11.22.2 ad
1068 1.11.22.2 ad case MTOFFL:
1069 1.11.22.2 ad case MTREW:
1070 1.11.22.2 ad case MTNOP:
1071 1.11.22.2 ad cnt = 0;
1072 1.11.22.2 ad break;
1073 1.11.22.2 ad
1074 1.11.22.2 ad default:
1075 1.11.22.2 ad return (EINVAL);
1076 1.11.22.2 ad }
1077 1.11.22.2 ad return (mtcommand(dev, op->mt_op, cnt));
1078 1.11.22.2 ad
1079 1.11.22.2 ad case MTIOCGET:
1080 1.11.22.2 ad break;
1081 1.11.22.2 ad
1082 1.11.22.2 ad default:
1083 1.11.22.2 ad return (EINVAL);
1084 1.11.22.2 ad }
1085 1.11.22.2 ad return (0);
1086 1.11.22.2 ad }
1087