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