ppi.c revision 1.1 1 1.1 gmcgarry /* $NetBSD: ppi.c,v 1.1 2003/06/02 03:47:09 gmcgarry Exp $ */
2 1.1 gmcgarry
3 1.1 gmcgarry /*-
4 1.1 gmcgarry * Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
5 1.1 gmcgarry * All rights reserved.
6 1.1 gmcgarry *
7 1.1 gmcgarry * This code is derived from software contributed to The NetBSD Foundation
8 1.1 gmcgarry * by Jason R. Thorpe.
9 1.1 gmcgarry *
10 1.1 gmcgarry * Redistribution and use in source and binary forms, with or without
11 1.1 gmcgarry * modification, are permitted provided that the following conditions
12 1.1 gmcgarry * are met:
13 1.1 gmcgarry * 1. Redistributions of source code must retain the above copyright
14 1.1 gmcgarry * notice, this list of conditions and the following disclaimer.
15 1.1 gmcgarry * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 gmcgarry * notice, this list of conditions and the following disclaimer in the
17 1.1 gmcgarry * documentation and/or other materials provided with the distribution.
18 1.1 gmcgarry * 3. All advertising materials mentioning features or use of this software
19 1.1 gmcgarry * must display the following acknowledgement:
20 1.1 gmcgarry * This product includes software developed by the NetBSD
21 1.1 gmcgarry * Foundation, Inc. and its contributors.
22 1.1 gmcgarry * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 gmcgarry * contributors may be used to endorse or promote products derived
24 1.1 gmcgarry * from this software without specific prior written permission.
25 1.1 gmcgarry *
26 1.1 gmcgarry * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 gmcgarry * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 gmcgarry * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 gmcgarry * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 gmcgarry * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 gmcgarry * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 gmcgarry * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 gmcgarry * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 gmcgarry * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 gmcgarry * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 gmcgarry * POSSIBILITY OF SUCH DAMAGE.
37 1.1 gmcgarry */
38 1.1 gmcgarry
39 1.1 gmcgarry /*
40 1.1 gmcgarry * Copyright (c) 1982, 1990, 1993
41 1.1 gmcgarry * The Regents of the University of California. All rights reserved.
42 1.1 gmcgarry *
43 1.1 gmcgarry * Redistribution and use in source and binary forms, with or without
44 1.1 gmcgarry * modification, are permitted provided that the following conditions
45 1.1 gmcgarry * are met:
46 1.1 gmcgarry * 1. Redistributions of source code must retain the above copyright
47 1.1 gmcgarry * notice, this list of conditions and the following disclaimer.
48 1.1 gmcgarry * 2. Redistributions in binary form must reproduce the above copyright
49 1.1 gmcgarry * notice, this list of conditions and the following disclaimer in the
50 1.1 gmcgarry * documentation and/or other materials provided with the distribution.
51 1.1 gmcgarry * 3. All advertising materials mentioning features or use of this software
52 1.1 gmcgarry * must display the following acknowledgement:
53 1.1 gmcgarry * This product includes software developed by the University of
54 1.1 gmcgarry * California, Berkeley and its contributors.
55 1.1 gmcgarry * 4. Neither the name of the University nor the names of its contributors
56 1.1 gmcgarry * may be used to endorse or promote products derived from this software
57 1.1 gmcgarry * without specific prior written permission.
58 1.1 gmcgarry *
59 1.1 gmcgarry * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 1.1 gmcgarry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 1.1 gmcgarry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 1.1 gmcgarry * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 1.1 gmcgarry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 1.1 gmcgarry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 1.1 gmcgarry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 1.1 gmcgarry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 1.1 gmcgarry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 1.1 gmcgarry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 1.1 gmcgarry * SUCH DAMAGE.
70 1.1 gmcgarry *
71 1.1 gmcgarry * @(#)ppi.c 8.1 (Berkeley) 6/16/93
72 1.1 gmcgarry */
73 1.1 gmcgarry
74 1.1 gmcgarry /*
75 1.1 gmcgarry * Printer/Plotter GPIB interface
76 1.1 gmcgarry */
77 1.1 gmcgarry
78 1.1 gmcgarry #include <sys/cdefs.h>
79 1.1 gmcgarry __KERNEL_RCSID(0, "$NetBSD: ppi.c,v 1.1 2003/06/02 03:47:09 gmcgarry Exp $");
80 1.1 gmcgarry
81 1.1 gmcgarry #include <sys/param.h>
82 1.1 gmcgarry #include <sys/systm.h>
83 1.1 gmcgarry #include <sys/callout.h>
84 1.1 gmcgarry #include <sys/conf.h>
85 1.1 gmcgarry #include <sys/device.h>
86 1.1 gmcgarry #include <sys/malloc.h>
87 1.1 gmcgarry #include <sys/proc.h>
88 1.1 gmcgarry #include <sys/uio.h>
89 1.1 gmcgarry
90 1.1 gmcgarry #include <dev/gpib/gpibvar.h>
91 1.1 gmcgarry
92 1.1 gmcgarry #include <dev/gpib/ppiio.h>
93 1.1 gmcgarry
94 1.1 gmcgarry struct ppi_softc {
95 1.1 gmcgarry struct device sc_dev;
96 1.1 gmcgarry gpib_chipset_tag_t sc_ic;
97 1.1 gmcgarry gpib_handle_t sc_hdl;
98 1.1 gmcgarry
99 1.1 gmcgarry int sc_address; /* GPIB address */
100 1.1 gmcgarry int sc_flags;
101 1.1 gmcgarry int sc_sec;
102 1.1 gmcgarry struct ppiparam sc_param;
103 1.1 gmcgarry #define sc_burst sc_param.burst
104 1.1 gmcgarry #define sc_timo sc_param.timo
105 1.1 gmcgarry #define sc_delay sc_param.delay
106 1.1 gmcgarry struct callout sc_timo_ch;
107 1.1 gmcgarry struct callout sc_start_ch;
108 1.1 gmcgarry };
109 1.1 gmcgarry
110 1.1 gmcgarry /* sc_flags values */
111 1.1 gmcgarry #define PPIF_ALIVE 0x01
112 1.1 gmcgarry #define PPIF_OPEN 0x02
113 1.1 gmcgarry #define PPIF_UIO 0x04
114 1.1 gmcgarry #define PPIF_TIMO 0x08
115 1.1 gmcgarry #define PPIF_DELAY 0x10
116 1.1 gmcgarry
117 1.1 gmcgarry int ppimatch(struct device *, struct cfdata *, void *);
118 1.1 gmcgarry void ppiattach(struct device *, struct device *, void *);
119 1.1 gmcgarry
120 1.1 gmcgarry CFATTACH_DECL(ppi, sizeof(struct ppi_softc),
121 1.1 gmcgarry ppimatch, ppiattach, NULL, NULL);
122 1.1 gmcgarry
123 1.1 gmcgarry extern struct cfdriver ppi_cd;
124 1.1 gmcgarry
125 1.1 gmcgarry void ppicallback(void *, int);
126 1.1 gmcgarry void ppistart(void *);
127 1.1 gmcgarry
128 1.1 gmcgarry void ppitimo(void *);
129 1.1 gmcgarry int ppirw(dev_t, struct uio *);
130 1.1 gmcgarry int ppihztoms(int);
131 1.1 gmcgarry int ppimstohz(int);
132 1.1 gmcgarry
133 1.1 gmcgarry dev_type_open(ppiopen);
134 1.1 gmcgarry dev_type_close(ppiclose);
135 1.1 gmcgarry dev_type_read(ppiread);
136 1.1 gmcgarry dev_type_write(ppiwrite);
137 1.1 gmcgarry dev_type_ioctl(ppiioctl);
138 1.1 gmcgarry
139 1.1 gmcgarry const struct cdevsw ppi_cdevsw = {
140 1.1 gmcgarry ppiopen, ppiclose, ppiread, ppiwrite, ppiioctl,
141 1.1 gmcgarry nostop, notty, nopoll, nommap, nokqfilter,
142 1.1 gmcgarry };
143 1.1 gmcgarry
144 1.1 gmcgarry #define UNIT(x) minor(x)
145 1.1 gmcgarry
146 1.1 gmcgarry #ifdef DEBUG
147 1.1 gmcgarry int ppidebug = 0x80;
148 1.1 gmcgarry #define PDB_FOLLOW 0x01
149 1.1 gmcgarry #define PDB_IO 0x02
150 1.1 gmcgarry #define PDB_NOCHECK 0x80
151 1.1 gmcgarry #define DPRINTF(mask, str) if (ppidebug & (mask)) printf str
152 1.1 gmcgarry #else
153 1.1 gmcgarry #define DPRINTF(mask, str) /* nothing */
154 1.1 gmcgarry #endif
155 1.1 gmcgarry
156 1.1 gmcgarry int
157 1.1 gmcgarry ppimatch(parent, match, aux)
158 1.1 gmcgarry struct device *parent;
159 1.1 gmcgarry struct cfdata *match;
160 1.1 gmcgarry void *aux;
161 1.1 gmcgarry {
162 1.1 gmcgarry
163 1.1 gmcgarry return (1);
164 1.1 gmcgarry }
165 1.1 gmcgarry
166 1.1 gmcgarry void
167 1.1 gmcgarry ppiattach(parent, self, aux)
168 1.1 gmcgarry struct device *parent, *self;
169 1.1 gmcgarry void *aux;
170 1.1 gmcgarry {
171 1.1 gmcgarry struct ppi_softc *sc = (struct ppi_softc *)self;
172 1.1 gmcgarry struct gpib_attach_args *ga = aux;
173 1.1 gmcgarry
174 1.1 gmcgarry printf("\n");
175 1.1 gmcgarry
176 1.1 gmcgarry sc->sc_ic = ga->ga_ic;
177 1.1 gmcgarry sc->sc_address = ga->ga_address;
178 1.1 gmcgarry
179 1.1 gmcgarry callout_init(&sc->sc_timo_ch);
180 1.1 gmcgarry callout_init(&sc->sc_start_ch);
181 1.1 gmcgarry
182 1.1 gmcgarry if (gpibregister(sc->sc_ic, sc->sc_address, ppicallback, sc,
183 1.1 gmcgarry &sc->sc_hdl)) {
184 1.1 gmcgarry printf("%s: can't register callback\n", sc->sc_dev.dv_xname);
185 1.1 gmcgarry return;
186 1.1 gmcgarry }
187 1.1 gmcgarry
188 1.1 gmcgarry sc->sc_flags = PPIF_ALIVE;
189 1.1 gmcgarry }
190 1.1 gmcgarry
191 1.1 gmcgarry int
192 1.1 gmcgarry ppiopen(dev, flags, fmt, p)
193 1.1 gmcgarry dev_t dev;
194 1.1 gmcgarry int flags, fmt;
195 1.1 gmcgarry struct proc *p;
196 1.1 gmcgarry {
197 1.1 gmcgarry int unit = UNIT(dev);
198 1.1 gmcgarry struct ppi_softc *sc;
199 1.1 gmcgarry
200 1.1 gmcgarry if (unit >= ppi_cd.cd_ndevs ||
201 1.1 gmcgarry (sc = ppi_cd.cd_devs[unit]) == NULL ||
202 1.1 gmcgarry (sc->sc_flags & PPIF_ALIVE) == 0)
203 1.1 gmcgarry return (ENXIO);
204 1.1 gmcgarry
205 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppiopen(%x, %x): flags %x\n",
206 1.1 gmcgarry dev, flags, sc->sc_flags));
207 1.1 gmcgarry
208 1.1 gmcgarry if (sc->sc_flags & PPIF_OPEN)
209 1.1 gmcgarry return (EBUSY);
210 1.1 gmcgarry sc->sc_flags |= PPIF_OPEN;
211 1.1 gmcgarry sc->sc_burst = PPI_BURST;
212 1.1 gmcgarry sc->sc_timo = ppimstohz(PPI_TIMO);
213 1.1 gmcgarry sc->sc_delay = ppimstohz(PPI_DELAY);
214 1.1 gmcgarry sc->sc_sec = -1;
215 1.1 gmcgarry return (0);
216 1.1 gmcgarry }
217 1.1 gmcgarry
218 1.1 gmcgarry int
219 1.1 gmcgarry ppiclose(dev, flags, fmt, p)
220 1.1 gmcgarry dev_t dev;
221 1.1 gmcgarry int flags, fmt;
222 1.1 gmcgarry struct proc *p;
223 1.1 gmcgarry {
224 1.1 gmcgarry int unit = UNIT(dev);
225 1.1 gmcgarry struct ppi_softc *sc = ppi_cd.cd_devs[unit];
226 1.1 gmcgarry
227 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppiclose(%x, %x): flags %x\n",
228 1.1 gmcgarry dev, flags, sc->sc_flags));
229 1.1 gmcgarry
230 1.1 gmcgarry sc->sc_flags &= ~PPIF_OPEN;
231 1.1 gmcgarry return (0);
232 1.1 gmcgarry }
233 1.1 gmcgarry
234 1.1 gmcgarry void
235 1.1 gmcgarry ppicallback(v, action)
236 1.1 gmcgarry void *v;
237 1.1 gmcgarry int action;
238 1.1 gmcgarry {
239 1.1 gmcgarry struct ppi_softc *sc = v;
240 1.1 gmcgarry
241 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppicallback: v=%p, action=%d\n", v, action));
242 1.1 gmcgarry
243 1.1 gmcgarry switch (action) {
244 1.1 gmcgarry case GPIBCBF_START:
245 1.1 gmcgarry ppistart(sc);
246 1.1 gmcgarry case GPIBCBF_INTR:
247 1.1 gmcgarry /* no-op */
248 1.1 gmcgarry break;
249 1.1 gmcgarry #ifdef DEBUG
250 1.1 gmcgarry default:
251 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppicallback: unknown action %d\n",
252 1.1 gmcgarry action));
253 1.1 gmcgarry break;
254 1.1 gmcgarry #endif
255 1.1 gmcgarry }
256 1.1 gmcgarry }
257 1.1 gmcgarry
258 1.1 gmcgarry void
259 1.1 gmcgarry ppistart(v)
260 1.1 gmcgarry void *v;
261 1.1 gmcgarry {
262 1.1 gmcgarry struct ppi_softc *sc = v;
263 1.1 gmcgarry
264 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppistart(%x)\n", sc->sc_dev.dv_unit));
265 1.1 gmcgarry
266 1.1 gmcgarry sc->sc_flags &= ~PPIF_DELAY;
267 1.1 gmcgarry wakeup(sc);
268 1.1 gmcgarry }
269 1.1 gmcgarry
270 1.1 gmcgarry void
271 1.1 gmcgarry ppitimo(arg)
272 1.1 gmcgarry void *arg;
273 1.1 gmcgarry {
274 1.1 gmcgarry struct ppi_softc *sc = arg;
275 1.1 gmcgarry
276 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppitimo(%x)\n", sc->sc_dev.dv_unit));
277 1.1 gmcgarry
278 1.1 gmcgarry sc->sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
279 1.1 gmcgarry wakeup(sc);
280 1.1 gmcgarry }
281 1.1 gmcgarry
282 1.1 gmcgarry int
283 1.1 gmcgarry ppiread(dev, uio, flags)
284 1.1 gmcgarry dev_t dev;
285 1.1 gmcgarry struct uio *uio;
286 1.1 gmcgarry int flags;
287 1.1 gmcgarry {
288 1.1 gmcgarry
289 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppiread(%x, %p)\n", dev, uio));
290 1.1 gmcgarry
291 1.1 gmcgarry return (ppirw(dev, uio));
292 1.1 gmcgarry }
293 1.1 gmcgarry
294 1.1 gmcgarry int
295 1.1 gmcgarry ppiwrite(dev, uio, flags)
296 1.1 gmcgarry dev_t dev;
297 1.1 gmcgarry struct uio *uio;
298 1.1 gmcgarry int flags;
299 1.1 gmcgarry {
300 1.1 gmcgarry
301 1.1 gmcgarry DPRINTF(PDB_FOLLOW, ("ppiwrite(%x, %p)\n", dev, uio));
302 1.1 gmcgarry
303 1.1 gmcgarry return (ppirw(dev, uio));
304 1.1 gmcgarry }
305 1.1 gmcgarry
306 1.1 gmcgarry int
307 1.1 gmcgarry ppirw(dev, uio)
308 1.1 gmcgarry dev_t dev;
309 1.1 gmcgarry struct uio *uio;
310 1.1 gmcgarry {
311 1.1 gmcgarry int unit = UNIT(dev);
312 1.1 gmcgarry struct ppi_softc *sc = ppi_cd.cd_devs[unit];
313 1.1 gmcgarry int s, len, cnt;
314 1.1 gmcgarry char *cp;
315 1.1 gmcgarry int error = 0, gotdata = 0;
316 1.1 gmcgarry int buflen, address;
317 1.1 gmcgarry char *buf;
318 1.1 gmcgarry
319 1.1 gmcgarry if (uio->uio_resid == 0)
320 1.1 gmcgarry return (0);
321 1.1 gmcgarry
322 1.1 gmcgarry address = sc->sc_address;
323 1.1 gmcgarry
324 1.1 gmcgarry DPRINTF(PDB_FOLLOW|PDB_IO,
325 1.1 gmcgarry ("ppirw(%x, %p, %c): burst %d, timo %d, resid %x\n",
326 1.1 gmcgarry dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
327 1.1 gmcgarry sc->sc_burst, sc->sc_timo, uio->uio_resid));
328 1.1 gmcgarry
329 1.1 gmcgarry buflen = min(sc->sc_burst, uio->uio_resid);
330 1.1 gmcgarry buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
331 1.1 gmcgarry sc->sc_flags |= PPIF_UIO;
332 1.1 gmcgarry if (sc->sc_timo > 0) {
333 1.1 gmcgarry sc->sc_flags |= PPIF_TIMO;
334 1.1 gmcgarry callout_reset(&sc->sc_timo_ch, sc->sc_timo, ppitimo, sc);
335 1.1 gmcgarry }
336 1.1 gmcgarry len = cnt = 0;
337 1.1 gmcgarry while (uio->uio_resid > 0) {
338 1.1 gmcgarry len = min(buflen, uio->uio_resid);
339 1.1 gmcgarry cp = buf;
340 1.1 gmcgarry if (uio->uio_rw == UIO_WRITE) {
341 1.1 gmcgarry error = uiomove(cp, len, uio);
342 1.1 gmcgarry if (error)
343 1.1 gmcgarry break;
344 1.1 gmcgarry }
345 1.1 gmcgarry again:
346 1.1 gmcgarry s = splbio();
347 1.1 gmcgarry if (sc->sc_flags & PPIF_UIO) {
348 1.1 gmcgarry if (gpibrequest(sc->sc_ic, sc->sc_hdl) == 0)
349 1.1 gmcgarry (void) tsleep(sc, PRIBIO + 1, "ppirw", 0);
350 1.1 gmcgarry }
351 1.1 gmcgarry /*
352 1.1 gmcgarry * Check if we timed out during sleep or uiomove
353 1.1 gmcgarry */
354 1.1 gmcgarry (void) spllowersoftclock();
355 1.1 gmcgarry if ((sc->sc_flags & PPIF_UIO) == 0) {
356 1.1 gmcgarry DPRINTF(PDB_IO,
357 1.1 gmcgarry ("ppirw: uiomove/sleep timo, flags %x\n",
358 1.1 gmcgarry sc->sc_flags));
359 1.1 gmcgarry if (sc->sc_flags & PPIF_TIMO) {
360 1.1 gmcgarry callout_stop(&sc->sc_timo_ch);
361 1.1 gmcgarry sc->sc_flags &= ~PPIF_TIMO;
362 1.1 gmcgarry }
363 1.1 gmcgarry splx(s);
364 1.1 gmcgarry break;
365 1.1 gmcgarry }
366 1.1 gmcgarry splx(s);
367 1.1 gmcgarry /*
368 1.1 gmcgarry * Perform the operation
369 1.1 gmcgarry */
370 1.1 gmcgarry if (uio->uio_rw == UIO_WRITE)
371 1.1 gmcgarry cnt = gpibsend(sc->sc_ic, address, sc->sc_sec,
372 1.1 gmcgarry cp, len);
373 1.1 gmcgarry else
374 1.1 gmcgarry cnt = gpibrecv(sc->sc_ic, address, sc->sc_sec,
375 1.1 gmcgarry cp, len);
376 1.1 gmcgarry s = splbio();
377 1.1 gmcgarry gpibrelease(sc->sc_ic, sc->sc_hdl);
378 1.1 gmcgarry DPRINTF(PDB_IO, ("ppirw: %s(%d, %x, %p, %d) -> %d\n",
379 1.1 gmcgarry uio->uio_rw == UIO_READ ? "recv" : "send",
380 1.1 gmcgarry address, sc->sc_sec, cp, len, cnt));
381 1.1 gmcgarry splx(s);
382 1.1 gmcgarry if (uio->uio_rw == UIO_READ) {
383 1.1 gmcgarry if (cnt) {
384 1.1 gmcgarry error = uiomove(cp, cnt, uio);
385 1.1 gmcgarry if (error)
386 1.1 gmcgarry break;
387 1.1 gmcgarry gotdata++;
388 1.1 gmcgarry }
389 1.1 gmcgarry /*
390 1.1 gmcgarry * Didn't get anything this time, but did in the past.
391 1.1 gmcgarry * Consider us done.
392 1.1 gmcgarry */
393 1.1 gmcgarry else if (gotdata)
394 1.1 gmcgarry break;
395 1.1 gmcgarry }
396 1.1 gmcgarry s = splsoftclock();
397 1.1 gmcgarry /*
398 1.1 gmcgarry * Operation timeout (or non-blocking), quit now.
399 1.1 gmcgarry */
400 1.1 gmcgarry if ((sc->sc_flags & PPIF_UIO) == 0) {
401 1.1 gmcgarry DPRINTF(PDB_IO, ("ppirw: timeout/done\n"));
402 1.1 gmcgarry splx(s);
403 1.1 gmcgarry break;
404 1.1 gmcgarry }
405 1.1 gmcgarry /*
406 1.1 gmcgarry * Implement inter-read delay
407 1.1 gmcgarry */
408 1.1 gmcgarry if (sc->sc_delay > 0) {
409 1.1 gmcgarry sc->sc_flags |= PPIF_DELAY;
410 1.1 gmcgarry callout_reset(&sc->sc_start_ch, sc->sc_delay,
411 1.1 gmcgarry ppistart, sc);
412 1.1 gmcgarry error = tsleep(sc, (PCATCH|PZERO) + 1, "gpib", 0);
413 1.1 gmcgarry if (error) {
414 1.1 gmcgarry splx(s);
415 1.1 gmcgarry break;
416 1.1 gmcgarry }
417 1.1 gmcgarry }
418 1.1 gmcgarry splx(s);
419 1.1 gmcgarry /*
420 1.1 gmcgarry * Must not call uiomove again til we've used all data
421 1.1 gmcgarry * that we already grabbed.
422 1.1 gmcgarry */
423 1.1 gmcgarry if (uio->uio_rw == UIO_WRITE && cnt != len) {
424 1.1 gmcgarry cp += cnt;
425 1.1 gmcgarry len -= cnt;
426 1.1 gmcgarry cnt = 0;
427 1.1 gmcgarry goto again;
428 1.1 gmcgarry }
429 1.1 gmcgarry }
430 1.1 gmcgarry s = splsoftclock();
431 1.1 gmcgarry if (sc->sc_flags & PPIF_TIMO) {
432 1.1 gmcgarry callout_stop(&sc->sc_timo_ch);
433 1.1 gmcgarry sc->sc_flags &= ~PPIF_TIMO;
434 1.1 gmcgarry }
435 1.1 gmcgarry if (sc->sc_flags & PPIF_DELAY) {
436 1.1 gmcgarry callout_stop(&sc->sc_start_ch);
437 1.1 gmcgarry sc->sc_flags &= ~PPIF_DELAY;
438 1.1 gmcgarry }
439 1.1 gmcgarry splx(s);
440 1.1 gmcgarry /*
441 1.1 gmcgarry * Adjust for those chars that we uiomove'ed but never wrote
442 1.1 gmcgarry */
443 1.1 gmcgarry if (uio->uio_rw == UIO_WRITE && cnt != len) {
444 1.1 gmcgarry uio->uio_resid += (len - cnt);
445 1.1 gmcgarry DPRINTF(PDB_IO, ("ppirw: short write, adjust by %d\n",
446 1.1 gmcgarry len - cnt));
447 1.1 gmcgarry }
448 1.1 gmcgarry free(buf, M_DEVBUF);
449 1.1 gmcgarry DPRINTF(PDB_FOLLOW|PDB_IO, ("ppirw: return %d, resid %d\n",
450 1.1 gmcgarry error, uio->uio_resid));
451 1.1 gmcgarry return (error);
452 1.1 gmcgarry }
453 1.1 gmcgarry
454 1.1 gmcgarry int
455 1.1 gmcgarry ppiioctl(dev, cmd, data, flag, p)
456 1.1 gmcgarry dev_t dev;
457 1.1 gmcgarry u_long cmd;
458 1.1 gmcgarry caddr_t data;
459 1.1 gmcgarry int flag;
460 1.1 gmcgarry struct proc *p;
461 1.1 gmcgarry {
462 1.1 gmcgarry struct ppi_softc *sc = ppi_cd.cd_devs[UNIT(dev)];
463 1.1 gmcgarry struct ppiparam *pp, *upp;
464 1.1 gmcgarry int error = 0;
465 1.1 gmcgarry
466 1.1 gmcgarry switch (cmd) {
467 1.1 gmcgarry case PPIIOCGPARAM:
468 1.1 gmcgarry pp = &sc->sc_param;
469 1.1 gmcgarry upp = (struct ppiparam *)data;
470 1.1 gmcgarry upp->burst = pp->burst;
471 1.1 gmcgarry upp->timo = ppihztoms(pp->timo);
472 1.1 gmcgarry upp->delay = ppihztoms(pp->delay);
473 1.1 gmcgarry break;
474 1.1 gmcgarry case PPIIOCSPARAM:
475 1.1 gmcgarry pp = &sc->sc_param;
476 1.1 gmcgarry upp = (struct ppiparam *)data;
477 1.1 gmcgarry if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
478 1.1 gmcgarry upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
479 1.1 gmcgarry return (EINVAL);
480 1.1 gmcgarry pp->burst = upp->burst;
481 1.1 gmcgarry pp->timo = ppimstohz(upp->timo);
482 1.1 gmcgarry pp->delay = ppimstohz(upp->delay);
483 1.1 gmcgarry break;
484 1.1 gmcgarry case PPIIOCSSEC:
485 1.1 gmcgarry sc->sc_sec = *(int *)data;
486 1.1 gmcgarry break;
487 1.1 gmcgarry default:
488 1.1 gmcgarry return (EINVAL);
489 1.1 gmcgarry }
490 1.1 gmcgarry return (error);
491 1.1 gmcgarry }
492 1.1 gmcgarry
493 1.1 gmcgarry int
494 1.1 gmcgarry ppihztoms(h)
495 1.1 gmcgarry int h;
496 1.1 gmcgarry {
497 1.1 gmcgarry extern int hz;
498 1.1 gmcgarry int m = h;
499 1.1 gmcgarry
500 1.1 gmcgarry if (m > 0)
501 1.1 gmcgarry m = m * 1000 / hz;
502 1.1 gmcgarry return (m);
503 1.1 gmcgarry }
504 1.1 gmcgarry
505 1.1 gmcgarry int
506 1.1 gmcgarry ppimstohz(m)
507 1.1 gmcgarry int m;
508 1.1 gmcgarry {
509 1.1 gmcgarry extern int hz;
510 1.1 gmcgarry int h = m;
511 1.1 gmcgarry
512 1.1 gmcgarry if (h > 0) {
513 1.1 gmcgarry h = h * hz / 1000;
514 1.1 gmcgarry if (h == 0)
515 1.1 gmcgarry h = 1000 / hz;
516 1.1 gmcgarry }
517 1.1 gmcgarry return (h);
518 1.1 gmcgarry }
519