Debian Codegraph @

i386/i386at/lpr.c

raw
1 /*
2 * Mach Operating System
3 * Copyright (c) 1993-1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or [email protected]
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /*
27 * Parallel port printer driver v1.0
28 * All rights reserved.
29 */
30
31 #if NLPR > 0
32
33 #include <mach/std_types.h>
34 #include <sys/types.h>
35 #include <kern/printf.h>
36 #include <kern/mach_clock.h>
37 #include <device/conf.h>
38 #include <device/device_types.h>
39 #include <device/tty.h>
40 #include <device/io_req.h>
41
42 #include <i386/ipl.h>
43 #include <i386/pio.h>
44 #include <chips/busses.h>
45 #include <i386at/autoconf.h>
46 #include <i386at/lpr.h>
47
48 /*
49 * Driver information for auto-configuration stuff.
50 */
51
52 struct bus_device *lprinfo[NLPR]; /* ??? */
53
54 static vm_offset_t lpr_std[NLPR] = { 0 };
55 static struct bus_device *lpr_info[NLPR];
56 struct bus_driver lprdriver = {
57 lprprobe, 0, lprattach, 0, lpr_std, "lpr", lpr_info, 0, 0, 0};
58
59 struct tty lpr_tty[NLPR];
60
61 int lpr_alive[NLPR];
62
63 int
64 lprprobe(vm_offset_t port, struct bus_ctlr *dev)
65 {
66 u_short addr = (u_short) dev->address;
67 int unit = dev->unit;
68 int ret;
69
70 if ((unit < 0) || (unit >= NLPR)) {
71 printf("com %d out of range\n", unit);
72 return(0);
73 }
74
75 outb(INTR_ENAB(addr),0x07);
76 outb(DATA(addr),0xaa);
77 ret = inb(DATA(addr)) == 0xaa;
78 if (ret) {
79 if (lpr_alive[unit]) {
80 printf("lpr: Multiple alive entries for unit %d.\n", unit);
81 printf("lpr: Ignoring entry with address = %x .\n", addr);
82 ret = 0;
83 } else
84 lpr_alive[unit]++;
85 }
86 return(ret);
87 }
88
89 void lprattach(struct bus_device *dev)
90 {
91 u_char unit = dev->unit;
92 u_short addr = (u_short) dev->address;
93
94 if (unit >= NLPR) {
95 printf(", disabled by NLPR configuration\n");
96 return;
97 }
98
99 take_dev_irq(dev);
100 printf(", port = %zx, spl = %zd, pic = %d.",
101 dev->address, dev->sysdep, dev->sysdep1);
102 lprinfo[unit] = dev;
103
104 outb(INTR_ENAB(addr), inb(INTR_ENAB(addr)) & 0x0f);
105
106 return;
107 }
108
109 int
110 lpropen(dev_t dev, int flag, io_req_t ior)
111 {
112 int unit = minor(dev);
113 struct bus_device *isai;
114 struct tty *tp;
115 u_short addr;
116
117 if (unit >= NLPR)
118 return D_NO_SUCH_DEVICE;
119
120 isai = lprinfo[unit];
121 if (isai == NULL || !isai->alive)
122 return D_NO_SUCH_DEVICE;
123
124 tp = &lpr_tty[unit];
125 addr = (u_short) isai->address;
126 tp->t_dev = dev;
127 tp->t_addr = (void*) (natural_t) addr;
128 tp->t_oproc = lprstart;
129 tp->t_state |= TS_WOPEN;
130 tp->t_stop = lprstop;
131 tp->t_getstat = lprgetstat;
132 tp->t_setstat = lprsetstat;
133 if ((tp->t_state & TS_ISOPEN) == 0)
134 ttychars(tp);
135 outb(INTR_ENAB(addr), inb(INTR_ENAB(addr)) | 0x10);
136 tp->t_state |= TS_CARR_ON;
137 return (char_open(dev, tp, flag, ior));
138 }
139
140 void
141 lprclose(dev_t dev, int flag)
142 {
143 int unit = minor(dev);
144 struct tty *tp = &lpr_tty[unit];
145 u_short addr = (u_short) lprinfo[unit]->address;
146
147 ttyclose(tp);
148 if (tp->t_state&TS_HUPCLS || (tp->t_state&TS_ISOPEN)==0) {
149 outb(INTR_ENAB(addr), inb(INTR_ENAB(addr)) & 0x0f);
150 tp->t_state &= ~TS_BUSY;
151 }
152 }
153
154 int
155 lprread(dev_t dev, io_req_t ior)
156 {
157 return char_read(&lpr_tty[minor(dev)], ior);
158 }
159
160 int
161 lprwrite(dev_t dev, io_req_t ior)
162 {
163 return char_write(&lpr_tty[minor(dev)], ior);
164 }
165
166 int
167 lprportdeath(dev_t dev, mach_port_t port)
168 {
169 return (tty_portdeath(&lpr_tty[minor(dev)], (ipc_port_t)port));
170 }
171
172 io_return_t
173 lprgetstat(
174 dev_t dev,
175 dev_flavor_t flavor,
176 dev_status_t data, /* pointer to OUT array */
177 mach_msg_type_number_t *count /* out */
178 )
179 {
180 io_return_t result = D_SUCCESS;
181 int unit = minor(dev);
182
183 switch (flavor) {
184 default:
185 result = tty_get_status(&lpr_tty[unit], flavor, data, count);
186 break;
187 }
188 return (result);
189 }
190
191 io_return_t
192 lprsetstat(
193 dev_t dev,
194 dev_flavor_t flavor,
195 dev_status_t data,
196 mach_msg_type_number_t count)
197 {
198 io_return_t result = D_SUCCESS;
199 int unit = minor(dev);
200
201 switch (flavor) {
202 default:
203 result = tty_set_status(&lpr_tty[unit], flavor, data, count);
204 /* if (result == D_SUCCESS && flavor == TTY_STATUS)
205 lprparam(unit);
206 */ return (result);
207 }
208 return (D_SUCCESS);
209 }
210
211 void lprintr(int unit)
212 {
213 struct tty *tp = &lpr_tty[unit];
214
215 if ((tp->t_state & TS_ISOPEN) == 0)
216 return;
217
218 tp->t_state &= ~TS_BUSY;
219 if (tp->t_state&TS_FLUSH)
220 tp->t_state &=~TS_FLUSH;
221 tt_write_wakeup(tp);
222 lprstart(tp);
223 }
224
225 void lprstart(struct tty *tp)
226 {
227 spl_t s = spltty();
228 u_short addr = (natural_t) tp->t_addr;
229 int status = inb(STATUS(addr));
230 int nch;
231
232 if (tp->t_state & (TS_TIMEOUT|TS_TTSTOP|TS_BUSY)) {
233 splx(s);
234 return;
235 }
236
237 if (status & 0x20) {
238 printf("Printer out of paper!\n");
239 splx(s);
240 return;
241 }
242
243 if (tp->t_outq.c_cc <= TTLOWAT(tp)) {
244 tt_write_wakeup(tp);
245 }
246 if (tp->t_outq.c_cc == 0) {
247 splx(s);
248 return;
249 }
250 nch = getc(&tp->t_outq);
251 if (nch == -1) {
252 splx(s);
253 return;
254 }
255 if ((tp->t_flags & LITOUT) == 0 && (nch & 0200)) {
256 timeout((timer_func_t *)ttrstrt, (char *)tp, (nch & 0x7f) + 6);
257 tp->t_state |= TS_TIMEOUT;
258 return;
259 }
260 outb(DATA(addr), nch);
261 outb(INTR_ENAB(addr),inb(INTR_ENAB(addr)) | 0x01);
262 outb(INTR_ENAB(addr),inb(INTR_ENAB(addr)) & 0x1e);
263 tp->t_state |= TS_BUSY;
264 splx(s);
265 return;
266 }
267
268 void
269 lprstop(
270 struct tty *tp,
271 int flags)
272 {
273 if ((tp->t_state & TS_BUSY) && (tp->t_state & TS_TTSTOP) == 0)
274 tp->t_state |= TS_FLUSH;
275 }
276
277 void
278 lprpr_addr(unsigned short addr)
279 {
280 printf("DATA(%x) %x, STATUS(%x) %x, INTR_ENAB(%x) %x\n",
281 DATA(addr), inb(DATA(addr)),
282 STATUS(addr), inb(STATUS(addr)),
283 INTR_ENAB(addr), inb(INTR_ENAB(addr)));
284 }
285 #endif /* NLPR */