ppi.c revision 1.5 1 /*
2 * Copyright (c) 1982, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * from: @(#)ppi.c 8.1 (Berkeley) 6/16/93
34 * $Id: ppi.c,v 1.5 1994/05/25 11:49:17 mycroft Exp $
35 */
36
37 /*
38 * Printer/Plotter HPIB interface
39 */
40
41 #include "ppi.h"
42 #if NPPI > 0
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/errno.h>
47 #include <sys/uio.h>
48 #include <sys/malloc.h>
49
50 #include <hp300/dev/device.h>
51 #include <hp300/dev/ppiioctl.h>
52
53 int ppiattach(), ppistart();
54 void ppitimo();
55 struct driver ppidriver = {
56 ppiattach, "ppi", ppistart,
57 };
58
59 struct ppi_softc {
60 int sc_flags;
61 struct devqueue sc_dq;
62 struct hp_device *sc_hd;
63 struct ppiparam sc_param;
64 #define sc_burst sc_param.burst
65 #define sc_timo sc_param.timo
66 #define sc_delay sc_param.delay
67 int sc_sec;
68 } ppi_softc[NPPI];
69
70 /* sc_flags values */
71 #define PPIF_ALIVE 0x01
72 #define PPIF_OPEN 0x02
73 #define PPIF_UIO 0x04
74 #define PPIF_TIMO 0x08
75 #define PPIF_DELAY 0x10
76
77 #define UNIT(x) minor(x)
78
79 #ifdef DEBUG
80 int ppidebug = 0x80;
81 #define PDB_FOLLOW 0x01
82 #define PDB_IO 0x02
83 #define PDB_NOCHECK 0x80
84 #endif
85
86 ppiattach(hd)
87 register struct hp_device *hd;
88 {
89 register struct ppi_softc *sc = &ppi_softc[hd->hp_unit];
90
91 #ifdef DEBUG
92 if ((ppidebug & PDB_NOCHECK) == 0)
93 #endif
94 /*
95 * XXX: the printer/plotter doesn't seem to really return
96 * an ID but this will at least prevent us from mistaking
97 * a cs80 disk or tape for a ppi device.
98 */
99 if (hpibid(hd->hp_ctlr, hd->hp_slave) & 0x200)
100 return(0);
101 sc->sc_flags = PPIF_ALIVE;
102 sc->sc_dq.dq_ctlr = hd->hp_ctlr;
103 sc->sc_dq.dq_unit = hd->hp_unit;
104 sc->sc_dq.dq_slave = hd->hp_slave;
105 sc->sc_dq.dq_driver = &ppidriver;
106 sc->sc_hd = hd;
107 return(1);
108 }
109
110 ppiopen(dev, flags)
111 dev_t dev;
112 {
113 register int unit = UNIT(dev);
114 register struct ppi_softc *sc = &ppi_softc[unit];
115
116 if (unit >= NPPI || (sc->sc_flags & PPIF_ALIVE) == 0)
117 return(ENXIO);
118 #ifdef DEBUG
119 if (ppidebug & PDB_FOLLOW)
120 printf("ppiopen(%x, %x): flags %x\n",
121 dev, flags, sc->sc_flags);
122 #endif
123 if (sc->sc_flags & PPIF_OPEN)
124 return(EBUSY);
125 sc->sc_flags |= PPIF_OPEN;
126 sc->sc_burst = PPI_BURST;
127 sc->sc_timo = ppimstohz(PPI_TIMO);
128 sc->sc_delay = ppimstohz(PPI_DELAY);
129 sc->sc_sec = -1;
130 return(0);
131 }
132
133 ppiclose(dev, flags)
134 dev_t dev;
135 {
136 register int unit = UNIT(dev);
137 register struct ppi_softc *sc = &ppi_softc[unit];
138
139 #ifdef DEBUG
140 if (ppidebug & PDB_FOLLOW)
141 printf("ppiclose(%x, %x): flags %x\n",
142 dev, flags, sc->sc_flags);
143 #endif
144 sc->sc_flags &= ~PPIF_OPEN;
145 return(0);
146 }
147
148 ppistart(unit)
149 int unit;
150 {
151 #ifdef DEBUG
152 if (ppidebug & PDB_FOLLOW)
153 printf("ppistart(%x)\n", unit);
154 #endif
155 ppi_softc[unit].sc_flags &= ~PPIF_DELAY;
156 wakeup(&ppi_softc[unit]);
157 return (0);
158 }
159
160 void
161 ppitimo(unit)
162 int unit;
163 {
164 #ifdef DEBUG
165 if (ppidebug & PDB_FOLLOW)
166 printf("ppitimo(%x)\n", unit);
167 #endif
168 ppi_softc[unit].sc_flags &= ~(PPIF_UIO|PPIF_TIMO);
169 wakeup(&ppi_softc[unit]);
170 }
171
172 ppiread(dev, uio)
173 dev_t dev;
174 struct uio *uio;
175 {
176
177 #ifdef DEBUG
178 if (ppidebug & PDB_FOLLOW)
179 printf("ppiread(%x, %x)\n", dev, uio);
180 #endif
181 return (ppirw(dev, uio));
182 }
183
184 ppiwrite(dev, uio)
185 dev_t dev;
186 struct uio *uio;
187 {
188
189 #ifdef DEBUG
190 if (ppidebug & PDB_FOLLOW)
191 printf("ppiwrite(%x, %x)\n", dev, uio);
192 #endif
193 return (ppirw(dev, uio));
194 }
195
196 ppirw(dev, uio)
197 dev_t dev;
198 register struct uio *uio;
199 {
200 int unit = UNIT(dev);
201 register struct ppi_softc *sc = &ppi_softc[unit];
202 register int s, len, cnt;
203 register char *cp;
204 int error = 0, gotdata = 0;
205 int buflen;
206 char *buf;
207
208 if (uio->uio_resid == 0)
209 return(0);
210
211 #ifdef DEBUG
212 if (ppidebug & (PDB_FOLLOW|PDB_IO))
213 printf("ppirw(%x, %x, %c): burst %d, timo %d, resid %x\n",
214 dev, uio, uio->uio_rw == UIO_READ ? 'R' : 'W',
215 sc->sc_burst, sc->sc_timo, uio->uio_resid);
216 #endif
217 buflen = min(sc->sc_burst, uio->uio_resid);
218 buf = (char *)malloc(buflen, M_DEVBUF, M_WAITOK);
219 sc->sc_flags |= PPIF_UIO;
220 if (sc->sc_timo > 0) {
221 sc->sc_flags |= PPIF_TIMO;
222 timeout(ppitimo, (void *)unit, sc->sc_timo);
223 }
224 while (uio->uio_resid > 0) {
225 len = min(buflen, uio->uio_resid);
226 cp = buf;
227 if (uio->uio_rw == UIO_WRITE) {
228 error = uiomove(cp, len, uio);
229 if (error)
230 break;
231 }
232 again:
233 s = splbio();
234 if ((sc->sc_flags & PPIF_UIO) && hpibreq(&sc->sc_dq) == 0)
235 sleep(sc, PRIBIO+1);
236 /*
237 * Check if we timed out during sleep or uiomove
238 */
239 (void) splsoftclock();
240 if ((sc->sc_flags & PPIF_UIO) == 0) {
241 #ifdef DEBUG
242 if (ppidebug & PDB_IO)
243 printf("ppirw: uiomove/sleep timo, flags %x\n",
244 sc->sc_flags);
245 #endif
246 if (sc->sc_flags & PPIF_TIMO) {
247 untimeout(ppitimo, (void *)unit);
248 sc->sc_flags &= ~PPIF_TIMO;
249 }
250 splx(s);
251 break;
252 }
253 splx(s);
254 /*
255 * Perform the operation
256 */
257 if (uio->uio_rw == UIO_WRITE)
258 cnt = hpibsend(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
259 sc->sc_sec, cp, len);
260 else
261 cnt = hpibrecv(sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
262 sc->sc_sec, cp, len);
263 s = splbio();
264 hpibfree(&sc->sc_dq);
265 #ifdef DEBUG
266 if (ppidebug & PDB_IO)
267 printf("ppirw: %s(%d, %d, %x, %x, %d) -> %d\n",
268 uio->uio_rw == UIO_READ ? "recv" : "send",
269 sc->sc_hd->hp_ctlr, sc->sc_hd->hp_slave,
270 sc->sc_sec, cp, len, cnt);
271 #endif
272 splx(s);
273 if (uio->uio_rw == UIO_READ) {
274 if (cnt) {
275 error = uiomove(cp, cnt, uio);
276 if (error)
277 break;
278 gotdata++;
279 }
280 /*
281 * Didn't get anything this time, but did in the past.
282 * Consider us done.
283 */
284 else if (gotdata)
285 break;
286 }
287 s = splsoftclock();
288 /*
289 * Operation timeout (or non-blocking), quit now.
290 */
291 if ((sc->sc_flags & PPIF_UIO) == 0) {
292 #ifdef DEBUG
293 if (ppidebug & PDB_IO)
294 printf("ppirw: timeout/done\n");
295 #endif
296 splx(s);
297 break;
298 }
299 /*
300 * Implement inter-read delay
301 */
302 if (sc->sc_delay > 0) {
303 sc->sc_flags |= PPIF_DELAY;
304 timeout((void (*)__P((void *)))ppistart, (void *)unit,
305 sc->sc_delay);
306 error = tsleep(sc, PCATCH|PZERO+1, "hpib", 0);
307 if (error) {
308 splx(s);
309 break;
310 }
311 }
312 splx(s);
313 /*
314 * Must not call uiomove again til we've used all data
315 * that we already grabbed.
316 */
317 if (uio->uio_rw == UIO_WRITE && cnt != len) {
318 cp += cnt;
319 len -= cnt;
320 cnt = 0;
321 goto again;
322 }
323 }
324 s = splsoftclock();
325 if (sc->sc_flags & PPIF_TIMO) {
326 untimeout(ppitimo, (void *)unit);
327 sc->sc_flags &= ~PPIF_TIMO;
328 }
329 if (sc->sc_flags & PPIF_DELAY) {
330 untimeout((void (*)__P((void *)))ppistart, (void *)unit);
331 sc->sc_flags &= ~PPIF_DELAY;
332 }
333 splx(s);
334 /*
335 * Adjust for those chars that we uiomove'ed but never wrote
336 */
337 if (uio->uio_rw == UIO_WRITE && cnt != len) {
338 uio->uio_resid += (len - cnt);
339 #ifdef DEBUG
340 if (ppidebug & PDB_IO)
341 printf("ppirw: short write, adjust by %d\n",
342 len-cnt);
343 #endif
344 }
345 free(buf, M_DEVBUF);
346 #ifdef DEBUG
347 if (ppidebug & (PDB_FOLLOW|PDB_IO))
348 printf("ppirw: return %d, resid %d\n", error, uio->uio_resid);
349 #endif
350 return (error);
351 }
352
353 ppiioctl(dev, cmd, data, flag, p)
354 dev_t dev;
355 int cmd;
356 caddr_t data;
357 int flag;
358 struct proc *p;
359 {
360 struct ppi_softc *sc = &ppi_softc[UNIT(dev)];
361 struct ppiparam *pp, *upp;
362 int error = 0;
363
364 switch (cmd) {
365 case PPIIOCGPARAM:
366 pp = &sc->sc_param;
367 upp = (struct ppiparam *)data;
368 upp->burst = pp->burst;
369 upp->timo = ppihztoms(pp->timo);
370 upp->delay = ppihztoms(pp->delay);
371 break;
372 case PPIIOCSPARAM:
373 pp = &sc->sc_param;
374 upp = (struct ppiparam *)data;
375 if (upp->burst < PPI_BURST_MIN || upp->burst > PPI_BURST_MAX ||
376 upp->delay < PPI_DELAY_MIN || upp->delay > PPI_DELAY_MAX)
377 return(EINVAL);
378 pp->burst = upp->burst;
379 pp->timo = ppimstohz(upp->timo);
380 pp->delay = ppimstohz(upp->delay);
381 break;
382 case PPIIOCSSEC:
383 sc->sc_sec = *(int *)data;
384 break;
385 default:
386 return(EINVAL);
387 }
388 return (error);
389 }
390
391 ppihztoms(h)
392 int h;
393 {
394 extern int hz;
395 register int m = h;
396
397 if (m > 0)
398 m = m * 1000 / hz;
399 return(m);
400 }
401
402 ppimstohz(m)
403 int m;
404 {
405 extern int hz;
406 register int h = m;
407
408 if (h > 0) {
409 h = h * hz / 1000;
410 if (h == 0)
411 h = 1000 / hz;
412 }
413 return(h);
414 }
415 #endif
416