Deleted Added
full compact
lpt.c (47718) lpt.c (50254)
1/*
2 * Copyright (c) 1990 William F. Jolitz, TeleMuse
3 * 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 software is a component of "386BSD" developed by
16 * William F. Jolitz, TeleMuse.
17 * 4. Neither the name of the developer nor the name "386BSD"
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
25 * NOT MAKE USE OF THIS WORK.
26 *
27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
29 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 * from: unknown origin, 386BSD 0.1
49 * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
50 * From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
1/*
2 * Copyright (c) 1990 William F. Jolitz, TeleMuse
3 * 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 software is a component of "386BSD" developed by
16 * William F. Jolitz, TeleMuse.
17 * 4. Neither the name of the developer nor the name "386BSD"
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS A COMPONENT OF 386BSD DEVELOPED BY WILLIAM F. JOLITZ
22 * AND IS INTENDED FOR RESEARCH AND EDUCATIONAL PURPOSES ONLY. THIS
23 * SOFTWARE SHOULD NOT BE CONSIDERED TO BE A COMMERCIAL PRODUCT.
24 * THE DEVELOPER URGES THAT USERS WHO REQUIRE A COMMERCIAL PRODUCT
25 * NOT MAKE USE OF THIS WORK.
26 *
27 * FOR USERS WHO WISH TO UNDERSTAND THE 386BSD SYSTEM DEVELOPED
28 * BY WILLIAM F. JOLITZ, WE RECOMMEND THE USER STUDY WRITTEN
29 * REFERENCES SUCH AS THE "PORTING UNIX TO THE 386" SERIES
30 * (BEGINNING JANUARY 1991 "DR. DOBBS JOURNAL", USA AND BEGINNING
31 * JUNE 1991 "UNIX MAGAZIN", GERMANY) BY WILLIAM F. JOLITZ AND
32 * LYNNE GREER JOLITZ, AS WELL AS OTHER BOOKS ON UNIX AND THE
33 * ON-LINE 386BSD USER MANUAL BEFORE USE. A BOOK DISCUSSING THE INTERNALS
34 * OF 386BSD ENTITLED "386BSD FROM THE INSIDE OUT" WILL BE AVAILABLE LATE 1992.
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPER ``AS IS'' AND
37 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39 * ARE DISCLAIMED. IN NO EVENT SHALL THE DEVELOPER BE LIABLE
40 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 *
48 * from: unknown origin, 386BSD 0.1
49 * From Id: lpt.c,v 1.55.2.1 1996/11/12 09:08:38 phk Exp
50 * From Id: nlpt.c,v 1.14 1999/02/08 13:55:43 des Exp
51 * $Id: lpt.c,v 1.5 1999/05/31 11:24:56 phk Exp $
51 * $Id: lpt.c,v 1.6 1999/06/03 22:03:35 peter Exp $
52 */
53
54/*
55 * Device Driver for AT parallel printer port
56 * Written by William Jolitz 12/18/90
57 */
58
59/*
60 * Updated for ppbus by Nicolas Souchu
61 * [Mon Jul 28 1997]
62 */
63
64
65#ifdef KERNEL
52 */
53
54/*
55 * Device Driver for AT parallel printer port
56 * Written by William Jolitz 12/18/90
57 */
58
59/*
60 * Updated for ppbus by Nicolas Souchu
61 * [Mon Jul 28 1997]
62 */
63
64
65#ifdef KERNEL
66#include "opt_devfs.h"
67
68#include <sys/param.h>
69#include <sys/systm.h>
70#include <sys/conf.h>
71#include <sys/buf.h>
72#include <sys/kernel.h>
73#include <sys/uio.h>
74#include <sys/syslog.h>
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/conf.h>
70#include <sys/buf.h>
71#include <sys/kernel.h>
72#include <sys/uio.h>
73#include <sys/syslog.h>
75#ifdef DEVFS
76#include <sys/devfsext.h>
77#endif /*DEVFS*/
78#include <sys/malloc.h>
79
80#include <machine/clock.h>
81#include <machine/lpt.h>
82#endif /*KERNEL*/
83
84#include <dev/ppbus/ppbconf.h>
85#include <dev/ppbus/ppb_1284.h>
86#include <dev/ppbus/lpt.h>
87
88#include "opt_lpt.h"
89
90#ifndef LPT_DEBUG
91#define lprintf(args)
92#else
93#define lprintf(args) \
94 do { \
95 if (lptflag) \
96 printf args; \
97 } while (0)
98static int volatile lptflag = 1;
99#endif
100
101#define LPINITRDY 4 /* wait up to 4 seconds for a ready */
102#define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
103#define LPTOUTMAX 1 /* maximal timeout 1 s */
104#define LPPRI (PZERO+8)
105#define BUFSIZE 1024
106#define BUFSTATSIZE 32
107
108#define LPTUNIT(s) ((s)&0x03)
109#define LPTFLAGS(s) ((s)&0xfc)
110
111struct lpt_data {
112 unsigned short lpt_unit;
113
114 struct ppb_device lpt_dev;
115
116 short sc_state;
117 /* default case: negative prime, negative ack, handshake strobe,
118 prime once */
119 u_char sc_control;
120 char sc_flags;
121#define LP_POS_INIT 0x04 /* if we are a postive init signal */
122#define LP_POS_ACK 0x08 /* if we are a positive going ack */
123#define LP_NO_PRIME 0x10 /* don't prime the printer at all */
124#define LP_PRIMEOPEN 0x20 /* prime on every open */
125#define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
126#define LP_BYPASS 0x80 /* bypass printer ready checks */
127 struct buf *sc_inbuf;
128 struct buf *sc_statbuf;
129 short sc_xfercnt ;
130 char sc_primed;
131 char *sc_cp ;
132 u_short sc_irq ; /* IRQ status of port */
133#define LP_HAS_IRQ 0x01 /* we have an irq available */
134#define LP_USE_IRQ 0x02 /* we are using our irq */
135#define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
136#define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
137 u_char sc_backoff ; /* time to call lptout() again */
138
74#include <sys/malloc.h>
75
76#include <machine/clock.h>
77#include <machine/lpt.h>
78#endif /*KERNEL*/
79
80#include <dev/ppbus/ppbconf.h>
81#include <dev/ppbus/ppb_1284.h>
82#include <dev/ppbus/lpt.h>
83
84#include "opt_lpt.h"
85
86#ifndef LPT_DEBUG
87#define lprintf(args)
88#else
89#define lprintf(args) \
90 do { \
91 if (lptflag) \
92 printf args; \
93 } while (0)
94static int volatile lptflag = 1;
95#endif
96
97#define LPINITRDY 4 /* wait up to 4 seconds for a ready */
98#define LPTOUTINITIAL 10 /* initial timeout to wait for ready 1/10 s */
99#define LPTOUTMAX 1 /* maximal timeout 1 s */
100#define LPPRI (PZERO+8)
101#define BUFSIZE 1024
102#define BUFSTATSIZE 32
103
104#define LPTUNIT(s) ((s)&0x03)
105#define LPTFLAGS(s) ((s)&0xfc)
106
107struct lpt_data {
108 unsigned short lpt_unit;
109
110 struct ppb_device lpt_dev;
111
112 short sc_state;
113 /* default case: negative prime, negative ack, handshake strobe,
114 prime once */
115 u_char sc_control;
116 char sc_flags;
117#define LP_POS_INIT 0x04 /* if we are a postive init signal */
118#define LP_POS_ACK 0x08 /* if we are a positive going ack */
119#define LP_NO_PRIME 0x10 /* don't prime the printer at all */
120#define LP_PRIMEOPEN 0x20 /* prime on every open */
121#define LP_AUTOLF 0x40 /* tell printer to do an automatic lf */
122#define LP_BYPASS 0x80 /* bypass printer ready checks */
123 struct buf *sc_inbuf;
124 struct buf *sc_statbuf;
125 short sc_xfercnt ;
126 char sc_primed;
127 char *sc_cp ;
128 u_short sc_irq ; /* IRQ status of port */
129#define LP_HAS_IRQ 0x01 /* we have an irq available */
130#define LP_USE_IRQ 0x02 /* we are using our irq */
131#define LP_ENABLE_IRQ 0x04 /* enable IRQ on open */
132#define LP_ENABLE_EXT 0x10 /* we shall use advanced mode when possible */
133 u_char sc_backoff ; /* time to call lptout() again */
134
139#ifdef DEVFS
140 void *devfs_token;
141 void *devfs_token_ctl;
142#endif
143};
144
145static int nlpt = 0;
146#define MAXLPT 8 /* XXX not much better! */
147static struct lpt_data *lptdata[MAXLPT];
148
149#define LPT_NAME "lpt" /* our official name */
150
151static timeout_t lptout;
152static int lpt_port_test(struct lpt_data *sc, u_char data, u_char mask);
153static int lpt_detect(struct lpt_data *sc);
154
155/*
156 * Make ourselves visible as a ppbus driver
157 */
158
159static struct ppb_device *lptprobe(struct ppb_data *ppb);
160static int lptattach(struct ppb_device *dev);
161static void lptintr(int unit);
162
163static void lpt_intr(int unit); /* without spls */
164
165#ifdef KERNEL
166
167static struct ppb_driver lptdriver = {
168 lptprobe, lptattach, LPT_NAME
169};
170DATA_SET(ppbdriver_set, lptdriver);
171
172#endif /* KERNEL */
173
174/* bits for state */
175#define OPEN (1<<0) /* device is open */
176#define ASLP (1<<1) /* awaiting draining of printer */
177#define EERROR (1<<2) /* error was received from printer */
178#define OBUSY (1<<3) /* printer is busy doing output */
179#define LPTOUT (1<<4) /* timeout while not selected */
180#define TOUT (1<<5) /* timeout while not selected */
181#define LPTINIT (1<<6) /* waiting to initialize for open */
182#define INTERRUPTED (1<<7) /* write call was interrupted */
183
184#define HAVEBUS (1<<8) /* the driver owns the bus */
185
186
187/* status masks to interrogate printer status */
188#define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
189#define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
190
191/* Printer Ready condition - from lpa.c */
192/* Only used in polling code */
193#define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
194#define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
195#define NOT_READY(lpt) ((ppb_rstr(&(lpt)->lpt_dev)^LPS_INVERT)&LPS_MASK)
196
197#define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
198#define MAX_SPIN 20 /* Max delay for device ready in usecs */
199
200
201static d_open_t lptopen;
202static d_close_t lptclose;
203static d_write_t lptwrite;
204static d_read_t lptread;
205static d_ioctl_t lptioctl;
206
207#define CDEV_MAJOR 16
208static struct cdevsw lpt_cdevsw = {
209 /* open */ lptopen,
210 /* close */ lptclose,
211 /* read */ lptread,
212 /* write */ lptwrite,
213 /* ioctl */ lptioctl,
214 /* stop */ nostop,
215 /* reset */ noreset,
216 /* devtotty */ nodevtotty,
217 /* poll */ nopoll,
218 /* mmap */ nommap,
219 /* strategy */ nostrategy,
220 /* name */ LPT_NAME,
221 /* parms */ noparms,
222 /* maj */ CDEV_MAJOR,
223 /* dump */ nodump,
224 /* psize */ nopsize,
225 /* flags */ 0,
226 /* maxio */ 0,
227 /* bmaj */ -1
228};
229
230static int
231lpt_request_ppbus(struct lpt_data *sc, int how)
232{
233 int error;
234
235 if (sc->sc_state & HAVEBUS)
236 return (0);
237
238 /* we have the bus only if the request succeded */
239 if ((error = ppb_request_bus(&sc->lpt_dev, how)) == 0)
240 sc->sc_state |= HAVEBUS;
241
242 return (error);
243}
244
245static int
246lpt_release_ppbus(struct lpt_data *sc)
247{
248 ppb_release_bus(&sc->lpt_dev);
249 sc->sc_state &= ~HAVEBUS;
250
251 return (0);
252}
253
254/*
255 * Internal routine to lptprobe to do port tests of one byte value
256 */
257static int
258lpt_port_test(struct lpt_data *sc, u_char data, u_char mask)
259{
260 int temp, timeout;
261
262 data = data & mask;
263 ppb_wdtr(&sc->lpt_dev, data);
264 timeout = 10000;
265 do {
266 DELAY(10);
267 temp = ppb_rdtr(&sc->lpt_dev) & mask;
268 }
269 while (temp != data && --timeout);
270 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
271 return (temp == data);
272}
273
274/*
275 * Probe simplified by replacing multiple loops with a hardcoded
276 * test pattern - 1999/02/08 des@freebsd.org
277 *
278 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
279 * Based partially on Rod Grimes' printer probe
280 *
281 * Logic:
282 * 1) If no port address was given, use the bios detected ports
283 * and autodetect what ports the printers are on.
284 * 2) Otherwise, probe the data port at the address given,
285 * using the method in Rod Grimes' port probe.
286 * (Much code ripped off directly from Rod's probe.)
287 *
288 * Comments from Rod's probe:
289 * Logic:
290 * 1) You should be able to write to and read back the same value
291 * to the data port. Do an alternating zeros, alternating ones,
292 * walking zero, and walking one test to check for stuck bits.
293 *
294 * 2) You should be able to write to and read back the same value
295 * to the control port lower 5 bits, the upper 3 bits are reserved
296 * per the IBM PC technical reference manauls and different boards
297 * do different things with them. Do an alternating zeros, alternating
298 * ones, walking zero, and walking one test to check for stuck bits.
299 *
300 * Some printers drag the strobe line down when the are powered off
301 * so this bit has been masked out of the control port test.
302 *
303 * XXX Some printers may not like a fast pulse on init or strobe, I
304 * don't know at this point, if that becomes a problem these bits
305 * should be turned off in the mask byte for the control port test.
306 *
307 * We are finally left with a mask of 0x14, due to some printers
308 * being adamant about holding other bits high ........
309 *
310 * Before probing the control port, we write a 0 to the data port -
311 * If not, some printers chuck out garbage when the strobe line
312 * gets toggled.
313 *
314 * 3) Set the data and control ports to a value of 0
315 *
316 * This probe routine has been tested on Epson Lx-800, HP LJ3P,
317 * Epson FX-1170 and C.Itoh 8510RM
318 * printers.
319 * Quick exit on fail added.
320 */
321static int
322lpt_detect(struct lpt_data *sc)
323{
324 static u_char testbyte[18] = {
325 0x55, /* alternating zeros */
326 0xaa, /* alternating ones */
327 0xfe, 0xfd, 0xfb, 0xf7,
328 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
329 0x01, 0x02, 0x04, 0x08,
330 0x10, 0x20, 0x40, 0x80 /* walking one */
331 };
332 int i, error, status;
333
334 status = 1; /* assume success */
335
336 if ((error = lpt_request_ppbus(sc, PPB_DONTWAIT))) {
337 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
338 status = 0;
339 goto end_probe;
340 }
341
342 for (i = 0; i < 18 && status; i++)
343 if (!lpt_port_test(sc, testbyte[i], 0xff)) {
344 status = 0;
345 goto end_probe;
346 }
347
348end_probe:
349 /* write 0's to control and data ports */
350 ppb_wdtr(&sc->lpt_dev, 0);
351 ppb_wctr(&sc->lpt_dev, 0);
352
353 lpt_release_ppbus(sc);
354
355 return (status);
356}
357
358/*
359 * lptprobe()
360 */
361static struct ppb_device *
362lptprobe(struct ppb_data *ppb)
363{
364 struct lpt_data *sc;
365 static int once;
366
367 if (!once++)
368 cdevsw_add(&lpt_cdevsw);
369
370 sc = (struct lpt_data *) malloc(sizeof(struct lpt_data),
371 M_TEMP, M_NOWAIT);
372 if (!sc) {
373 printf(LPT_NAME ": cannot malloc!\n");
374 return (0);
375 }
376 bzero(sc, sizeof(struct lpt_data));
377
378 lptdata[nlpt] = sc;
379
380 /*
381 * lpt dependent initialisation.
382 */
383 sc->lpt_unit = nlpt;
384
385 /*
386 * ppbus dependent initialisation.
387 */
388 sc->lpt_dev.id_unit = sc->lpt_unit;
389 sc->lpt_dev.name = lptdriver.name;
390 sc->lpt_dev.ppb = ppb;
391 sc->lpt_dev.intr = lptintr;
392
393 /*
394 * Now, try to detect the printer.
395 */
396 if (!lpt_detect(sc)) {
397 free(sc, M_TEMP);
398 return (0);
399 }
400
401 /* Ok, go to next device on next probe */
402 nlpt ++;
403
404 return (&sc->lpt_dev);
405}
406
407static int
408lptattach(struct ppb_device *dev)
409{
410 struct lpt_data *sc = lptdata[dev->id_unit];
411 int error;
412
413 /*
414 * Report ourselves
415 */
416 printf(LPT_NAME "%d: <generic printer> on ppbus %d\n",
417 dev->id_unit, dev->ppb->ppb_link->adapter_unit);
418
419 sc->sc_primed = 0; /* not primed yet */
420
421 if ((error = lpt_request_ppbus(sc, PPB_DONTWAIT))) {
422 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
423 return (0);
424 }
425
426 ppb_wctr(&sc->lpt_dev, LPC_NINIT);
427
428 /* check if we can use interrupt, should be done by ppc stuff */
429 lprintf(("oldirq %x\n", sc->sc_irq));
430 if (ppb_get_irq(&sc->lpt_dev)) {
431 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
432 printf(LPT_NAME "%d: Interrupt-driven port\n", dev->id_unit);
433 } else {
434 sc->sc_irq = 0;
435 lprintf((LPT_NAME "%d: Polled port\n", dev->id_unit));
436 }
437 lprintf(("irq %x\n", sc->sc_irq));
438
439 lpt_release_ppbus(sc);
440
135};
136
137static int nlpt = 0;
138#define MAXLPT 8 /* XXX not much better! */
139static struct lpt_data *lptdata[MAXLPT];
140
141#define LPT_NAME "lpt" /* our official name */
142
143static timeout_t lptout;
144static int lpt_port_test(struct lpt_data *sc, u_char data, u_char mask);
145static int lpt_detect(struct lpt_data *sc);
146
147/*
148 * Make ourselves visible as a ppbus driver
149 */
150
151static struct ppb_device *lptprobe(struct ppb_data *ppb);
152static int lptattach(struct ppb_device *dev);
153static void lptintr(int unit);
154
155static void lpt_intr(int unit); /* without spls */
156
157#ifdef KERNEL
158
159static struct ppb_driver lptdriver = {
160 lptprobe, lptattach, LPT_NAME
161};
162DATA_SET(ppbdriver_set, lptdriver);
163
164#endif /* KERNEL */
165
166/* bits for state */
167#define OPEN (1<<0) /* device is open */
168#define ASLP (1<<1) /* awaiting draining of printer */
169#define EERROR (1<<2) /* error was received from printer */
170#define OBUSY (1<<3) /* printer is busy doing output */
171#define LPTOUT (1<<4) /* timeout while not selected */
172#define TOUT (1<<5) /* timeout while not selected */
173#define LPTINIT (1<<6) /* waiting to initialize for open */
174#define INTERRUPTED (1<<7) /* write call was interrupted */
175
176#define HAVEBUS (1<<8) /* the driver owns the bus */
177
178
179/* status masks to interrogate printer status */
180#define RDY_MASK (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR) /* ready ? */
181#define LP_READY (LPS_SEL|LPS_NBSY|LPS_NERR)
182
183/* Printer Ready condition - from lpa.c */
184/* Only used in polling code */
185#define LPS_INVERT (LPS_NBSY | LPS_NACK | LPS_SEL | LPS_NERR)
186#define LPS_MASK (LPS_NBSY | LPS_NACK | LPS_OUT | LPS_SEL | LPS_NERR)
187#define NOT_READY(lpt) ((ppb_rstr(&(lpt)->lpt_dev)^LPS_INVERT)&LPS_MASK)
188
189#define MAX_SLEEP (hz*5) /* Timeout while waiting for device ready */
190#define MAX_SPIN 20 /* Max delay for device ready in usecs */
191
192
193static d_open_t lptopen;
194static d_close_t lptclose;
195static d_write_t lptwrite;
196static d_read_t lptread;
197static d_ioctl_t lptioctl;
198
199#define CDEV_MAJOR 16
200static struct cdevsw lpt_cdevsw = {
201 /* open */ lptopen,
202 /* close */ lptclose,
203 /* read */ lptread,
204 /* write */ lptwrite,
205 /* ioctl */ lptioctl,
206 /* stop */ nostop,
207 /* reset */ noreset,
208 /* devtotty */ nodevtotty,
209 /* poll */ nopoll,
210 /* mmap */ nommap,
211 /* strategy */ nostrategy,
212 /* name */ LPT_NAME,
213 /* parms */ noparms,
214 /* maj */ CDEV_MAJOR,
215 /* dump */ nodump,
216 /* psize */ nopsize,
217 /* flags */ 0,
218 /* maxio */ 0,
219 /* bmaj */ -1
220};
221
222static int
223lpt_request_ppbus(struct lpt_data *sc, int how)
224{
225 int error;
226
227 if (sc->sc_state & HAVEBUS)
228 return (0);
229
230 /* we have the bus only if the request succeded */
231 if ((error = ppb_request_bus(&sc->lpt_dev, how)) == 0)
232 sc->sc_state |= HAVEBUS;
233
234 return (error);
235}
236
237static int
238lpt_release_ppbus(struct lpt_data *sc)
239{
240 ppb_release_bus(&sc->lpt_dev);
241 sc->sc_state &= ~HAVEBUS;
242
243 return (0);
244}
245
246/*
247 * Internal routine to lptprobe to do port tests of one byte value
248 */
249static int
250lpt_port_test(struct lpt_data *sc, u_char data, u_char mask)
251{
252 int temp, timeout;
253
254 data = data & mask;
255 ppb_wdtr(&sc->lpt_dev, data);
256 timeout = 10000;
257 do {
258 DELAY(10);
259 temp = ppb_rdtr(&sc->lpt_dev) & mask;
260 }
261 while (temp != data && --timeout);
262 lprintf(("out=%x\tin=%x\ttout=%d\n", data, temp, timeout));
263 return (temp == data);
264}
265
266/*
267 * Probe simplified by replacing multiple loops with a hardcoded
268 * test pattern - 1999/02/08 des@freebsd.org
269 *
270 * New lpt port probe Geoff Rehmet - Rhodes University - 14/2/94
271 * Based partially on Rod Grimes' printer probe
272 *
273 * Logic:
274 * 1) If no port address was given, use the bios detected ports
275 * and autodetect what ports the printers are on.
276 * 2) Otherwise, probe the data port at the address given,
277 * using the method in Rod Grimes' port probe.
278 * (Much code ripped off directly from Rod's probe.)
279 *
280 * Comments from Rod's probe:
281 * Logic:
282 * 1) You should be able to write to and read back the same value
283 * to the data port. Do an alternating zeros, alternating ones,
284 * walking zero, and walking one test to check for stuck bits.
285 *
286 * 2) You should be able to write to and read back the same value
287 * to the control port lower 5 bits, the upper 3 bits are reserved
288 * per the IBM PC technical reference manauls and different boards
289 * do different things with them. Do an alternating zeros, alternating
290 * ones, walking zero, and walking one test to check for stuck bits.
291 *
292 * Some printers drag the strobe line down when the are powered off
293 * so this bit has been masked out of the control port test.
294 *
295 * XXX Some printers may not like a fast pulse on init or strobe, I
296 * don't know at this point, if that becomes a problem these bits
297 * should be turned off in the mask byte for the control port test.
298 *
299 * We are finally left with a mask of 0x14, due to some printers
300 * being adamant about holding other bits high ........
301 *
302 * Before probing the control port, we write a 0 to the data port -
303 * If not, some printers chuck out garbage when the strobe line
304 * gets toggled.
305 *
306 * 3) Set the data and control ports to a value of 0
307 *
308 * This probe routine has been tested on Epson Lx-800, HP LJ3P,
309 * Epson FX-1170 and C.Itoh 8510RM
310 * printers.
311 * Quick exit on fail added.
312 */
313static int
314lpt_detect(struct lpt_data *sc)
315{
316 static u_char testbyte[18] = {
317 0x55, /* alternating zeros */
318 0xaa, /* alternating ones */
319 0xfe, 0xfd, 0xfb, 0xf7,
320 0xef, 0xdf, 0xbf, 0x7f, /* walking zero */
321 0x01, 0x02, 0x04, 0x08,
322 0x10, 0x20, 0x40, 0x80 /* walking one */
323 };
324 int i, error, status;
325
326 status = 1; /* assume success */
327
328 if ((error = lpt_request_ppbus(sc, PPB_DONTWAIT))) {
329 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
330 status = 0;
331 goto end_probe;
332 }
333
334 for (i = 0; i < 18 && status; i++)
335 if (!lpt_port_test(sc, testbyte[i], 0xff)) {
336 status = 0;
337 goto end_probe;
338 }
339
340end_probe:
341 /* write 0's to control and data ports */
342 ppb_wdtr(&sc->lpt_dev, 0);
343 ppb_wctr(&sc->lpt_dev, 0);
344
345 lpt_release_ppbus(sc);
346
347 return (status);
348}
349
350/*
351 * lptprobe()
352 */
353static struct ppb_device *
354lptprobe(struct ppb_data *ppb)
355{
356 struct lpt_data *sc;
357 static int once;
358
359 if (!once++)
360 cdevsw_add(&lpt_cdevsw);
361
362 sc = (struct lpt_data *) malloc(sizeof(struct lpt_data),
363 M_TEMP, M_NOWAIT);
364 if (!sc) {
365 printf(LPT_NAME ": cannot malloc!\n");
366 return (0);
367 }
368 bzero(sc, sizeof(struct lpt_data));
369
370 lptdata[nlpt] = sc;
371
372 /*
373 * lpt dependent initialisation.
374 */
375 sc->lpt_unit = nlpt;
376
377 /*
378 * ppbus dependent initialisation.
379 */
380 sc->lpt_dev.id_unit = sc->lpt_unit;
381 sc->lpt_dev.name = lptdriver.name;
382 sc->lpt_dev.ppb = ppb;
383 sc->lpt_dev.intr = lptintr;
384
385 /*
386 * Now, try to detect the printer.
387 */
388 if (!lpt_detect(sc)) {
389 free(sc, M_TEMP);
390 return (0);
391 }
392
393 /* Ok, go to next device on next probe */
394 nlpt ++;
395
396 return (&sc->lpt_dev);
397}
398
399static int
400lptattach(struct ppb_device *dev)
401{
402 struct lpt_data *sc = lptdata[dev->id_unit];
403 int error;
404
405 /*
406 * Report ourselves
407 */
408 printf(LPT_NAME "%d: <generic printer> on ppbus %d\n",
409 dev->id_unit, dev->ppb->ppb_link->adapter_unit);
410
411 sc->sc_primed = 0; /* not primed yet */
412
413 if ((error = lpt_request_ppbus(sc, PPB_DONTWAIT))) {
414 printf(LPT_NAME ": cannot alloc ppbus (%d)!\n", error);
415 return (0);
416 }
417
418 ppb_wctr(&sc->lpt_dev, LPC_NINIT);
419
420 /* check if we can use interrupt, should be done by ppc stuff */
421 lprintf(("oldirq %x\n", sc->sc_irq));
422 if (ppb_get_irq(&sc->lpt_dev)) {
423 sc->sc_irq = LP_HAS_IRQ | LP_USE_IRQ | LP_ENABLE_IRQ;
424 printf(LPT_NAME "%d: Interrupt-driven port\n", dev->id_unit);
425 } else {
426 sc->sc_irq = 0;
427 lprintf((LPT_NAME "%d: Polled port\n", dev->id_unit));
428 }
429 lprintf(("irq %x\n", sc->sc_irq));
430
431 lpt_release_ppbus(sc);
432
441#ifdef DEVFS
442 sc->devfs_token = devfs_add_devswf(&lpt_cdevsw,
443 dev->id_unit, DV_CHR,
444 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", dev->id_unit);
445 sc->devfs_token_ctl = devfs_add_devswf(&lpt_cdevsw,
446 dev->id_unit | LP_BYPASS, DV_CHR,
447 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", dev->id_unit);
448#endif
449
433 make_dev(&lpt_cdevsw, dev->id_unit,
434 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d", dev->id_unit);
435 make_dev(&lpt_cdevsw, dev->id_unit | LP_BYPASS,
436 UID_ROOT, GID_WHEEL, 0600, LPT_NAME "%d.ctl", dev->id_unit);
450 return (1);
451}
452
453static void
454lptout(void *arg)
455{
456 struct lpt_data *sc = arg;
457 int pl;
458
459 lprintf(("T %x ", ppb_rstr(&sc->lpt_dev)));
460 if (sc->sc_state & OPEN) {
461 sc->sc_backoff++;
462 if (sc->sc_backoff > hz/LPTOUTMAX)
463 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
464 timeout(lptout, (caddr_t)sc, sc->sc_backoff);
465 } else
466 sc->sc_state &= ~TOUT;
467
468 if (sc->sc_state & EERROR)
469 sc->sc_state &= ~EERROR;
470
471 /*
472 * Avoid possible hangs do to missed interrupts
473 */
474 if (sc->sc_xfercnt) {
475 pl = spltty();
476 lpt_intr(sc->lpt_unit);
477 splx(pl);
478 } else {
479 sc->sc_state &= ~OBUSY;
480 wakeup((caddr_t)sc);
481 }
482}
483
484/*
485 * lptopen -- reset the printer, then wait until it's selected and not busy.
486 * If LP_BYPASS flag is selected, then we do not try to select the
487 * printer -- this is just used for passing ioctls.
488 */
489
490static int
491lptopen(dev_t dev, int flags, int fmt, struct proc *p)
492{
493 struct lpt_data *sc;
494
495 int s;
496 int trys, err;
497 u_int unit = LPTUNIT(minor(dev));
498
499 if ((unit >= nlpt))
500 return (ENXIO);
501
502 sc = lptdata[unit];
503
504 if (sc->sc_state) {
505 lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
506 return(EBUSY);
507 } else
508 sc->sc_state |= LPTINIT;
509
510 sc->sc_flags = LPTFLAGS(minor(dev));
511
512 /* Check for open with BYPASS flag set. */
513 if (sc->sc_flags & LP_BYPASS) {
514 sc->sc_state = OPEN;
515 return(0);
516 }
517
518 /* request the ppbus only if we don't have it already */
519 if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
520 return (err);
521
522 s = spltty();
523 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
524
525 /* set IRQ status according to ENABLE_IRQ flag */
526 if (sc->sc_irq & LP_ENABLE_IRQ)
527 sc->sc_irq |= LP_USE_IRQ;
528 else
529 sc->sc_irq &= ~LP_USE_IRQ;
530
531 /* init printer */
532 if ((sc->sc_flags & LP_NO_PRIME) == 0) {
533 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
534 ppb_wctr(&sc->lpt_dev, 0);
535 sc->sc_primed++;
536 DELAY(500);
537 }
538 }
539
540 ppb_wctr(&sc->lpt_dev, LPC_SEL|LPC_NINIT);
541
542 /* wait till ready (printer running diagnostics) */
543 trys = 0;
544 do {
545 /* ran out of waiting for the printer */
546 if (trys++ >= LPINITRDY*4) {
547 splx(s);
548 sc->sc_state = 0;
549 lprintf(("status %x\n", ppb_rstr(&sc->lpt_dev)));
550
551 lpt_release_ppbus(sc);
552 return (EBUSY);
553 }
554
555 /* wait 1/4 second, give up if we get a signal */
556 if (tsleep((caddr_t)sc, LPPRI|PCATCH, "lptinit", hz/4) !=
557 EWOULDBLOCK) {
558 sc->sc_state = 0;
559 splx(s);
560
561 lpt_release_ppbus(sc);
562 return (EBUSY);
563 }
564
565 /* is printer online and ready for output */
566 } while ((ppb_rstr(&sc->lpt_dev) &
567 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
568 (LPS_SEL|LPS_NBSY|LPS_NERR));
569
570 sc->sc_control = LPC_SEL|LPC_NINIT;
571 if (sc->sc_flags & LP_AUTOLF)
572 sc->sc_control |= LPC_AUTOL;
573
574 /* enable interrupt if interrupt-driven */
575 if (sc->sc_irq & LP_USE_IRQ)
576 sc->sc_control |= LPC_ENA;
577
578 ppb_wctr(&sc->lpt_dev, sc->sc_control);
579
580 sc->sc_state = OPEN;
581 sc->sc_inbuf = geteblk(BUFSIZE);
582 sc->sc_statbuf = geteblk(BUFSTATSIZE);
583 sc->sc_xfercnt = 0;
584 splx(s);
585
586 /* release the ppbus */
587 lpt_release_ppbus(sc);
588
589 /* only use timeout if using interrupt */
590 lprintf(("irq %x\n", sc->sc_irq));
591 if (sc->sc_irq & LP_USE_IRQ) {
592 sc->sc_state |= TOUT;
593 timeout(lptout, (caddr_t)sc,
594 (sc->sc_backoff = hz/LPTOUTINITIAL));
595 }
596
597 lprintf(("opened.\n"));
598 return(0);
599}
600
601/*
602 * lptclose -- close the device, free the local line buffer.
603 *
604 * Check for interrupted write call added.
605 */
606
607static int
608lptclose(dev_t dev, int flags, int fmt, struct proc *p)
609{
610 struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
611 int err;
612
613 if(sc->sc_flags & LP_BYPASS)
614 goto end_close;
615
616 if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
617 return (err);
618
619 sc->sc_state &= ~OPEN;
620
621 /* if the last write was interrupted, don't complete it */
622 if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
623 while ((ppb_rstr(&sc->lpt_dev) &
624 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
625 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
626 /* wait 1/4 second, give up if we get a signal */
627 if (tsleep((caddr_t)sc, LPPRI|PCATCH,
628 "lpclose", hz) != EWOULDBLOCK)
629 break;
630
631 ppb_wctr(&sc->lpt_dev, LPC_NINIT);
632 brelse(sc->sc_inbuf);
633 brelse(sc->sc_statbuf);
634
635end_close:
636 /* release the bus anyway */
637 lpt_release_ppbus(sc);
638
639 sc->sc_state = 0;
640 sc->sc_xfercnt = 0;
641 lprintf(("closed.\n"));
642 return(0);
643}
644
645/*
646 * lpt_pushbytes()
647 * Workhorse for actually spinning and writing bytes to printer
648 * Derived from lpa.c
649 * Originally by ?
650 *
651 * This code is only used when we are polling the port
652 */
653static int
654lpt_pushbytes(struct lpt_data *sc)
655{
656 int spin, err, tic;
657 char ch;
658
659 lprintf(("p"));
660 /* loop for every character .. */
661 while (sc->sc_xfercnt > 0) {
662 /* printer data */
663 ch = *(sc->sc_cp);
664 sc->sc_cp++;
665 sc->sc_xfercnt--;
666
667 /*
668 * Wait for printer ready.
669 * Loop 20 usecs testing BUSY bit, then sleep
670 * for exponentially increasing timeout. (vak)
671 */
672 for (spin = 0; NOT_READY(sc) && spin < MAX_SPIN; ++spin)
673 DELAY(1); /* XXX delay is NOT this accurate! */
674 if (spin >= MAX_SPIN) {
675 tic = 0;
676 while (NOT_READY(sc)) {
677 /*
678 * Now sleep, every cycle a
679 * little longer ..
680 */
681 tic = tic + tic + 1;
682 /*
683 * But no more than 10 seconds. (vak)
684 */
685 if (tic > MAX_SLEEP)
686 tic = MAX_SLEEP;
687 err = tsleep((caddr_t)sc, LPPRI,
688 LPT_NAME "poll", tic);
689 if (err != EWOULDBLOCK) {
690 return (err);
691 }
692 }
693 }
694
695 /* output data */
696 ppb_wdtr(&sc->lpt_dev, ch);
697 /* strobe */
698 ppb_wctr(&sc->lpt_dev, sc->sc_control|LPC_STB);
699 ppb_wctr(&sc->lpt_dev, sc->sc_control);
700
701 }
702 return(0);
703}
704
705/*
706 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
707 */
708
709static int
710lptread(dev_t dev, struct uio *uio, int ioflag)
711{
712 struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
713 int error = 0, len;
714
715 if ((error = ppb_1284_negociate(&sc->lpt_dev, PPB_NIBBLE, 0)))
716 return (error);
717
718 /* read data in an other buffer, read/write may be simultaneous */
719 len = 0;
720 while (uio->uio_resid) {
721 if ((error = ppb_1284_read(&sc->lpt_dev, PPB_NIBBLE,
722 sc->sc_statbuf->b_data, min(BUFSTATSIZE,
723 uio->uio_resid), &len))) {
724 goto error;
725 }
726
727 if (!len)
728 goto error; /* no more data */
729
730 if ((error = uiomove(sc->sc_statbuf->b_data, len, uio)))
731 goto error;
732 }
733
734error:
735 ppb_1284_terminate(&sc->lpt_dev);
736 return (error);
737}
738
739/*
740 * lptwrite --copy a line from user space to a local buffer, then call
741 * putc to get the chars moved to the output queue.
742 *
743 * Flagging of interrupted write added.
744 */
745
746static int
747lptwrite(dev_t dev, struct uio *uio, int ioflag)
748{
749 register unsigned n;
750 int pl, err;
751 u_int unit = LPTUNIT(minor(dev));
752 struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
753
754 if(sc->sc_flags & LP_BYPASS) {
755 /* we can't do writes in bypass mode */
756 return(EPERM);
757 }
758
759 /* request the ppbus only if we don't have it already */
760 if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
761 return (err);
762
763 sc->sc_state &= ~INTERRUPTED;
764 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
765 sc->sc_cp = sc->sc_inbuf->b_data ;
766 uiomove(sc->sc_cp, n, uio);
767 sc->sc_xfercnt = n ;
768
769 if (sc->sc_irq & LP_ENABLE_EXT) {
770 /* try any extended mode */
771 err = ppb_write(&sc->lpt_dev, sc->sc_cp,
772 sc->sc_xfercnt, 0);
773 switch (err) {
774 case 0:
775 /* if not all data was sent, we could rely
776 * on polling for the last bytes */
777 sc->sc_xfercnt = 0;
778 break;
779 case EINTR:
780 sc->sc_state |= INTERRUPTED;
781 return(err);
782 case EINVAL:
783 /* advanced mode not avail */
784 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
785 break;
786 default:
787 return(err);
788 }
789 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
790 lprintf(("i"));
791 /* if the printer is ready for a char, */
792 /* give it one */
793 if ((sc->sc_state & OBUSY) == 0){
794 lprintf(("\nC %d. ", sc->sc_xfercnt));
795 pl = spltty();
796 lpt_intr(sc->lpt_unit);
797 (void) splx(pl);
798 }
799 lprintf(("W "));
800 if (sc->sc_state & OBUSY)
801 if ((err = tsleep((caddr_t)sc,
802 LPPRI|PCATCH, LPT_NAME "write", 0))) {
803 sc->sc_state |= INTERRUPTED;
804 return(err);
805 }
806 }
807
808 /* check to see if we must do a polled write */
809 if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
810 lprintf(("p"));
811
812 err = lpt_pushbytes(sc);
813
814 if (err)
815 return(err);
816 }
817 }
818
819 /* we have not been interrupted, release the ppbus */
820 lpt_release_ppbus(sc);
821
822 return(0);
823}
824
825/*
826 * lpt_intr -- handle printer interrupts which occur when the printer is
827 * ready to accept another char.
828 *
829 * do checking for interrupted write call.
830 */
831
832static void
833lpt_intr(int unit)
834{
835 struct lpt_data *sc = lptdata[unit];
836 int sts;
837 int i;
838
839 /* we must own the bus to use it */
840 if ((sc->sc_state & HAVEBUS) == 0)
841 return;
842
843 /*
844 * Is printer online and ready for output?
845 *
846 * Avoid falling back to lptout() too quickly. First spin-loop
847 * to see if the printer will become ready ``really soon now''.
848 */
849 for (i = 0; i < 100 &&
850 ((sts=ppb_rstr(&sc->lpt_dev)) & RDY_MASK) != LP_READY; i++) ;
851
852 if ((sts & RDY_MASK) == LP_READY) {
853 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
854 sc->sc_backoff = hz/LPTOUTINITIAL;
855
856 if (sc->sc_xfercnt) {
857 /* send char */
858 /*lprintf(("%x ", *sc->sc_cp)); */
859 ppb_wdtr(&sc->lpt_dev, *sc->sc_cp++) ;
860 ppb_wctr(&sc->lpt_dev, sc->sc_control|LPC_STB);
861 /* DELAY(X) */
862 ppb_wctr(&sc->lpt_dev, sc->sc_control);
863
864 /* any more data for printer */
865 if(--(sc->sc_xfercnt) > 0) return;
866 }
867
868 /*
869 * No more data waiting for printer.
870 * Wakeup is not done if write call was interrupted.
871 */
872 sc->sc_state &= ~OBUSY;
873
874 if(!(sc->sc_state & INTERRUPTED))
875 wakeup((caddr_t)sc);
876 lprintf(("w "));
877 return;
878 } else { /* check for error */
879 if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
880 (sc->sc_state & OPEN))
881 sc->sc_state |= EERROR;
882 /* lptout() will jump in and try to restart. */
883 }
884 lprintf(("sts %x ", sts));
885}
886
887static void
888lptintr(int unit)
889{
890 /* call the interrupt at required spl level */
891 int s = spltty();
892
893 lpt_intr(unit);
894
895 splx(s);
896 return;
897}
898
899static int
900lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
901{
902 int error = 0;
903 struct lpt_data *sc;
904 u_int unit = LPTUNIT(minor(dev));
905 u_char old_sc_irq; /* old printer IRQ status */
906
907 sc = lptdata[unit];
908
909 switch (cmd) {
910 case LPT_IRQ :
911 if(sc->sc_irq & LP_HAS_IRQ) {
912 /*
913 * NOTE:
914 * If the IRQ status is changed,
915 * this will only be visible on the
916 * next open.
917 *
918 * If interrupt status changes,
919 * this gets syslog'd.
920 */
921 old_sc_irq = sc->sc_irq;
922 switch(*(int*)data) {
923 case 0:
924 sc->sc_irq &= (~LP_ENABLE_IRQ);
925 break;
926 case 1:
927 sc->sc_irq &= (~LP_ENABLE_EXT);
928 sc->sc_irq |= LP_ENABLE_IRQ;
929 break;
930 case 2:
931 /* classic irq based transfer and advanced
932 * modes are in conflict
933 */
934 sc->sc_irq &= (~LP_ENABLE_IRQ);
935 sc->sc_irq |= LP_ENABLE_EXT;
936 break;
937 case 3:
938 sc->sc_irq &= (~LP_ENABLE_EXT);
939 break;
940 default:
941 break;
942 }
943
944 if (old_sc_irq != sc->sc_irq )
945 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
946 unit,
947 (sc->sc_irq & LP_ENABLE_IRQ)?
948 "interrupt-driven":"polled",
949 (sc->sc_irq & LP_ENABLE_EXT)?
950 "extended":"standard");
951 } else /* polled port */
952 error = EOPNOTSUPP;
953 break;
954 default:
955 error = ENODEV;
956 }
957
958 return(error);
959}
437 return (1);
438}
439
440static void
441lptout(void *arg)
442{
443 struct lpt_data *sc = arg;
444 int pl;
445
446 lprintf(("T %x ", ppb_rstr(&sc->lpt_dev)));
447 if (sc->sc_state & OPEN) {
448 sc->sc_backoff++;
449 if (sc->sc_backoff > hz/LPTOUTMAX)
450 sc->sc_backoff = sc->sc_backoff > hz/LPTOUTMAX;
451 timeout(lptout, (caddr_t)sc, sc->sc_backoff);
452 } else
453 sc->sc_state &= ~TOUT;
454
455 if (sc->sc_state & EERROR)
456 sc->sc_state &= ~EERROR;
457
458 /*
459 * Avoid possible hangs do to missed interrupts
460 */
461 if (sc->sc_xfercnt) {
462 pl = spltty();
463 lpt_intr(sc->lpt_unit);
464 splx(pl);
465 } else {
466 sc->sc_state &= ~OBUSY;
467 wakeup((caddr_t)sc);
468 }
469}
470
471/*
472 * lptopen -- reset the printer, then wait until it's selected and not busy.
473 * If LP_BYPASS flag is selected, then we do not try to select the
474 * printer -- this is just used for passing ioctls.
475 */
476
477static int
478lptopen(dev_t dev, int flags, int fmt, struct proc *p)
479{
480 struct lpt_data *sc;
481
482 int s;
483 int trys, err;
484 u_int unit = LPTUNIT(minor(dev));
485
486 if ((unit >= nlpt))
487 return (ENXIO);
488
489 sc = lptdata[unit];
490
491 if (sc->sc_state) {
492 lprintf((LPT_NAME ": still open %x\n", sc->sc_state));
493 return(EBUSY);
494 } else
495 sc->sc_state |= LPTINIT;
496
497 sc->sc_flags = LPTFLAGS(minor(dev));
498
499 /* Check for open with BYPASS flag set. */
500 if (sc->sc_flags & LP_BYPASS) {
501 sc->sc_state = OPEN;
502 return(0);
503 }
504
505 /* request the ppbus only if we don't have it already */
506 if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
507 return (err);
508
509 s = spltty();
510 lprintf((LPT_NAME " flags 0x%x\n", sc->sc_flags));
511
512 /* set IRQ status according to ENABLE_IRQ flag */
513 if (sc->sc_irq & LP_ENABLE_IRQ)
514 sc->sc_irq |= LP_USE_IRQ;
515 else
516 sc->sc_irq &= ~LP_USE_IRQ;
517
518 /* init printer */
519 if ((sc->sc_flags & LP_NO_PRIME) == 0) {
520 if((sc->sc_flags & LP_PRIMEOPEN) || sc->sc_primed == 0) {
521 ppb_wctr(&sc->lpt_dev, 0);
522 sc->sc_primed++;
523 DELAY(500);
524 }
525 }
526
527 ppb_wctr(&sc->lpt_dev, LPC_SEL|LPC_NINIT);
528
529 /* wait till ready (printer running diagnostics) */
530 trys = 0;
531 do {
532 /* ran out of waiting for the printer */
533 if (trys++ >= LPINITRDY*4) {
534 splx(s);
535 sc->sc_state = 0;
536 lprintf(("status %x\n", ppb_rstr(&sc->lpt_dev)));
537
538 lpt_release_ppbus(sc);
539 return (EBUSY);
540 }
541
542 /* wait 1/4 second, give up if we get a signal */
543 if (tsleep((caddr_t)sc, LPPRI|PCATCH, "lptinit", hz/4) !=
544 EWOULDBLOCK) {
545 sc->sc_state = 0;
546 splx(s);
547
548 lpt_release_ppbus(sc);
549 return (EBUSY);
550 }
551
552 /* is printer online and ready for output */
553 } while ((ppb_rstr(&sc->lpt_dev) &
554 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
555 (LPS_SEL|LPS_NBSY|LPS_NERR));
556
557 sc->sc_control = LPC_SEL|LPC_NINIT;
558 if (sc->sc_flags & LP_AUTOLF)
559 sc->sc_control |= LPC_AUTOL;
560
561 /* enable interrupt if interrupt-driven */
562 if (sc->sc_irq & LP_USE_IRQ)
563 sc->sc_control |= LPC_ENA;
564
565 ppb_wctr(&sc->lpt_dev, sc->sc_control);
566
567 sc->sc_state = OPEN;
568 sc->sc_inbuf = geteblk(BUFSIZE);
569 sc->sc_statbuf = geteblk(BUFSTATSIZE);
570 sc->sc_xfercnt = 0;
571 splx(s);
572
573 /* release the ppbus */
574 lpt_release_ppbus(sc);
575
576 /* only use timeout if using interrupt */
577 lprintf(("irq %x\n", sc->sc_irq));
578 if (sc->sc_irq & LP_USE_IRQ) {
579 sc->sc_state |= TOUT;
580 timeout(lptout, (caddr_t)sc,
581 (sc->sc_backoff = hz/LPTOUTINITIAL));
582 }
583
584 lprintf(("opened.\n"));
585 return(0);
586}
587
588/*
589 * lptclose -- close the device, free the local line buffer.
590 *
591 * Check for interrupted write call added.
592 */
593
594static int
595lptclose(dev_t dev, int flags, int fmt, struct proc *p)
596{
597 struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
598 int err;
599
600 if(sc->sc_flags & LP_BYPASS)
601 goto end_close;
602
603 if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
604 return (err);
605
606 sc->sc_state &= ~OPEN;
607
608 /* if the last write was interrupted, don't complete it */
609 if((!(sc->sc_state & INTERRUPTED)) && (sc->sc_irq & LP_USE_IRQ))
610 while ((ppb_rstr(&sc->lpt_dev) &
611 (LPS_SEL|LPS_OUT|LPS_NBSY|LPS_NERR)) !=
612 (LPS_SEL|LPS_NBSY|LPS_NERR) || sc->sc_xfercnt)
613 /* wait 1/4 second, give up if we get a signal */
614 if (tsleep((caddr_t)sc, LPPRI|PCATCH,
615 "lpclose", hz) != EWOULDBLOCK)
616 break;
617
618 ppb_wctr(&sc->lpt_dev, LPC_NINIT);
619 brelse(sc->sc_inbuf);
620 brelse(sc->sc_statbuf);
621
622end_close:
623 /* release the bus anyway */
624 lpt_release_ppbus(sc);
625
626 sc->sc_state = 0;
627 sc->sc_xfercnt = 0;
628 lprintf(("closed.\n"));
629 return(0);
630}
631
632/*
633 * lpt_pushbytes()
634 * Workhorse for actually spinning and writing bytes to printer
635 * Derived from lpa.c
636 * Originally by ?
637 *
638 * This code is only used when we are polling the port
639 */
640static int
641lpt_pushbytes(struct lpt_data *sc)
642{
643 int spin, err, tic;
644 char ch;
645
646 lprintf(("p"));
647 /* loop for every character .. */
648 while (sc->sc_xfercnt > 0) {
649 /* printer data */
650 ch = *(sc->sc_cp);
651 sc->sc_cp++;
652 sc->sc_xfercnt--;
653
654 /*
655 * Wait for printer ready.
656 * Loop 20 usecs testing BUSY bit, then sleep
657 * for exponentially increasing timeout. (vak)
658 */
659 for (spin = 0; NOT_READY(sc) && spin < MAX_SPIN; ++spin)
660 DELAY(1); /* XXX delay is NOT this accurate! */
661 if (spin >= MAX_SPIN) {
662 tic = 0;
663 while (NOT_READY(sc)) {
664 /*
665 * Now sleep, every cycle a
666 * little longer ..
667 */
668 tic = tic + tic + 1;
669 /*
670 * But no more than 10 seconds. (vak)
671 */
672 if (tic > MAX_SLEEP)
673 tic = MAX_SLEEP;
674 err = tsleep((caddr_t)sc, LPPRI,
675 LPT_NAME "poll", tic);
676 if (err != EWOULDBLOCK) {
677 return (err);
678 }
679 }
680 }
681
682 /* output data */
683 ppb_wdtr(&sc->lpt_dev, ch);
684 /* strobe */
685 ppb_wctr(&sc->lpt_dev, sc->sc_control|LPC_STB);
686 ppb_wctr(&sc->lpt_dev, sc->sc_control);
687
688 }
689 return(0);
690}
691
692/*
693 * lptread --retrieve printer status in IEEE1284 NIBBLE mode
694 */
695
696static int
697lptread(dev_t dev, struct uio *uio, int ioflag)
698{
699 struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
700 int error = 0, len;
701
702 if ((error = ppb_1284_negociate(&sc->lpt_dev, PPB_NIBBLE, 0)))
703 return (error);
704
705 /* read data in an other buffer, read/write may be simultaneous */
706 len = 0;
707 while (uio->uio_resid) {
708 if ((error = ppb_1284_read(&sc->lpt_dev, PPB_NIBBLE,
709 sc->sc_statbuf->b_data, min(BUFSTATSIZE,
710 uio->uio_resid), &len))) {
711 goto error;
712 }
713
714 if (!len)
715 goto error; /* no more data */
716
717 if ((error = uiomove(sc->sc_statbuf->b_data, len, uio)))
718 goto error;
719 }
720
721error:
722 ppb_1284_terminate(&sc->lpt_dev);
723 return (error);
724}
725
726/*
727 * lptwrite --copy a line from user space to a local buffer, then call
728 * putc to get the chars moved to the output queue.
729 *
730 * Flagging of interrupted write added.
731 */
732
733static int
734lptwrite(dev_t dev, struct uio *uio, int ioflag)
735{
736 register unsigned n;
737 int pl, err;
738 u_int unit = LPTUNIT(minor(dev));
739 struct lpt_data *sc = lptdata[LPTUNIT(minor(dev))];
740
741 if(sc->sc_flags & LP_BYPASS) {
742 /* we can't do writes in bypass mode */
743 return(EPERM);
744 }
745
746 /* request the ppbus only if we don't have it already */
747 if ((err = lpt_request_ppbus(sc, PPB_WAIT|PPB_INTR)) != 0)
748 return (err);
749
750 sc->sc_state &= ~INTERRUPTED;
751 while ((n = min(BUFSIZE, uio->uio_resid)) != 0) {
752 sc->sc_cp = sc->sc_inbuf->b_data ;
753 uiomove(sc->sc_cp, n, uio);
754 sc->sc_xfercnt = n ;
755
756 if (sc->sc_irq & LP_ENABLE_EXT) {
757 /* try any extended mode */
758 err = ppb_write(&sc->lpt_dev, sc->sc_cp,
759 sc->sc_xfercnt, 0);
760 switch (err) {
761 case 0:
762 /* if not all data was sent, we could rely
763 * on polling for the last bytes */
764 sc->sc_xfercnt = 0;
765 break;
766 case EINTR:
767 sc->sc_state |= INTERRUPTED;
768 return(err);
769 case EINVAL:
770 /* advanced mode not avail */
771 log(LOG_NOTICE, LPT_NAME "%d: advanced mode not avail, polling\n", unit);
772 break;
773 default:
774 return(err);
775 }
776 } else while ((sc->sc_xfercnt > 0)&&(sc->sc_irq & LP_USE_IRQ)) {
777 lprintf(("i"));
778 /* if the printer is ready for a char, */
779 /* give it one */
780 if ((sc->sc_state & OBUSY) == 0){
781 lprintf(("\nC %d. ", sc->sc_xfercnt));
782 pl = spltty();
783 lpt_intr(sc->lpt_unit);
784 (void) splx(pl);
785 }
786 lprintf(("W "));
787 if (sc->sc_state & OBUSY)
788 if ((err = tsleep((caddr_t)sc,
789 LPPRI|PCATCH, LPT_NAME "write", 0))) {
790 sc->sc_state |= INTERRUPTED;
791 return(err);
792 }
793 }
794
795 /* check to see if we must do a polled write */
796 if(!(sc->sc_irq & LP_USE_IRQ) && (sc->sc_xfercnt)) {
797 lprintf(("p"));
798
799 err = lpt_pushbytes(sc);
800
801 if (err)
802 return(err);
803 }
804 }
805
806 /* we have not been interrupted, release the ppbus */
807 lpt_release_ppbus(sc);
808
809 return(0);
810}
811
812/*
813 * lpt_intr -- handle printer interrupts which occur when the printer is
814 * ready to accept another char.
815 *
816 * do checking for interrupted write call.
817 */
818
819static void
820lpt_intr(int unit)
821{
822 struct lpt_data *sc = lptdata[unit];
823 int sts;
824 int i;
825
826 /* we must own the bus to use it */
827 if ((sc->sc_state & HAVEBUS) == 0)
828 return;
829
830 /*
831 * Is printer online and ready for output?
832 *
833 * Avoid falling back to lptout() too quickly. First spin-loop
834 * to see if the printer will become ready ``really soon now''.
835 */
836 for (i = 0; i < 100 &&
837 ((sts=ppb_rstr(&sc->lpt_dev)) & RDY_MASK) != LP_READY; i++) ;
838
839 if ((sts & RDY_MASK) == LP_READY) {
840 sc->sc_state = (sc->sc_state | OBUSY) & ~EERROR;
841 sc->sc_backoff = hz/LPTOUTINITIAL;
842
843 if (sc->sc_xfercnt) {
844 /* send char */
845 /*lprintf(("%x ", *sc->sc_cp)); */
846 ppb_wdtr(&sc->lpt_dev, *sc->sc_cp++) ;
847 ppb_wctr(&sc->lpt_dev, sc->sc_control|LPC_STB);
848 /* DELAY(X) */
849 ppb_wctr(&sc->lpt_dev, sc->sc_control);
850
851 /* any more data for printer */
852 if(--(sc->sc_xfercnt) > 0) return;
853 }
854
855 /*
856 * No more data waiting for printer.
857 * Wakeup is not done if write call was interrupted.
858 */
859 sc->sc_state &= ~OBUSY;
860
861 if(!(sc->sc_state & INTERRUPTED))
862 wakeup((caddr_t)sc);
863 lprintf(("w "));
864 return;
865 } else { /* check for error */
866 if(((sts & (LPS_NERR | LPS_OUT) ) != LPS_NERR) &&
867 (sc->sc_state & OPEN))
868 sc->sc_state |= EERROR;
869 /* lptout() will jump in and try to restart. */
870 }
871 lprintf(("sts %x ", sts));
872}
873
874static void
875lptintr(int unit)
876{
877 /* call the interrupt at required spl level */
878 int s = spltty();
879
880 lpt_intr(unit);
881
882 splx(s);
883 return;
884}
885
886static int
887lptioctl(dev_t dev, u_long cmd, caddr_t data, int flags, struct proc *p)
888{
889 int error = 0;
890 struct lpt_data *sc;
891 u_int unit = LPTUNIT(minor(dev));
892 u_char old_sc_irq; /* old printer IRQ status */
893
894 sc = lptdata[unit];
895
896 switch (cmd) {
897 case LPT_IRQ :
898 if(sc->sc_irq & LP_HAS_IRQ) {
899 /*
900 * NOTE:
901 * If the IRQ status is changed,
902 * this will only be visible on the
903 * next open.
904 *
905 * If interrupt status changes,
906 * this gets syslog'd.
907 */
908 old_sc_irq = sc->sc_irq;
909 switch(*(int*)data) {
910 case 0:
911 sc->sc_irq &= (~LP_ENABLE_IRQ);
912 break;
913 case 1:
914 sc->sc_irq &= (~LP_ENABLE_EXT);
915 sc->sc_irq |= LP_ENABLE_IRQ;
916 break;
917 case 2:
918 /* classic irq based transfer and advanced
919 * modes are in conflict
920 */
921 sc->sc_irq &= (~LP_ENABLE_IRQ);
922 sc->sc_irq |= LP_ENABLE_EXT;
923 break;
924 case 3:
925 sc->sc_irq &= (~LP_ENABLE_EXT);
926 break;
927 default:
928 break;
929 }
930
931 if (old_sc_irq != sc->sc_irq )
932 log(LOG_NOTICE, LPT_NAME "%d: switched to %s %s mode\n",
933 unit,
934 (sc->sc_irq & LP_ENABLE_IRQ)?
935 "interrupt-driven":"polled",
936 (sc->sc_irq & LP_ENABLE_EXT)?
937 "extended":"standard");
938 } else /* polled port */
939 error = EOPNOTSUPP;
940 break;
941 default:
942 error = ENODEV;
943 }
944
945 return(error);
946}