kd.c revision 1.11 1 /* $NetBSD: kd.c,v 1.11 1995/03/28 16:09:41 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Gordon W. Ross
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 * 4. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gordon Ross
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Keyboard/Display device.
35 *
36 * This driver exists simply to provide a tty device that
37 * the indirect console driver can point to.
38 * The kbd driver sends its input here.
39 * Output goes to the screen via PROM printf.
40 */
41
42 #include <sys/param.h>
43 #include <sys/proc.h>
44 #include <sys/systm.h>
45 #include <sys/ioctl.h>
46 #include <sys/tty.h>
47 #include <sys/file.h>
48 #include <sys/conf.h>
49 #include <sys/device.h>
50
51 #include <machine/autoconf.h>
52 #include <machine/mon.h>
53 #include <machine/psl.h>
54
55 #include <dev/cons.h>
56
57 #define BURST 64
58
59 struct tty *kd_tty[1];
60
61 int kdopen(dev_t, int, int, struct proc *);
62 int kdclose(dev_t, int, int, struct proc *);
63 int kdread(dev_t, struct uio *, int);
64 int kdwrite(dev_t, struct uio *, int);
65 int kdioctl(dev_t, int, caddr_t, int, struct proc *);
66
67 static int kdparam(struct tty *, struct termios *);
68 static void kdstart(struct tty *);
69
70 int kd_is_console;
71
72 /* This is called by kbd_serial() like a pseudo-device. */
73 void
74 kd_attach(n)
75 int n;
76 {
77 kd_tty[0] = ttymalloc();
78
79 /* Tell keyboard module where to send read data. */
80 kbd_ascii(kd_tty[0]);
81 }
82
83 int
84 kdopen(dev, flag, mode, p)
85 dev_t dev;
86 int flag, mode;
87 struct proc *p;
88 {
89 int error, unit;
90 struct tty *tp;
91
92 unit = minor(dev);
93 if (unit) return ENXIO;
94
95 tp = kd_tty[unit];
96 if (tp == NULL)
97 return ENXIO;
98
99 if ((error = kbd_iopen()) != 0) {
100 #ifdef DIAGNOSTIC
101 printf("kd: kbd_iopen, error=%d\n", error);
102 #endif
103 return (error);
104 }
105
106 tp->t_oproc = kdstart;
107 tp->t_param = kdparam;
108 tp->t_dev = dev;
109 if ((tp->t_state & TS_ISOPEN) == 0) {
110 tp->t_state |= TS_WOPEN;
111 ttychars(tp);
112 tp->t_iflag = TTYDEF_IFLAG;
113 tp->t_oflag = TTYDEF_OFLAG;
114 tp->t_cflag = TTYDEF_CFLAG;
115 tp->t_lflag = TTYDEF_LFLAG;
116 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
117 kdparam(tp, &tp->t_termios);
118 ttsetwater(tp);
119 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
120 return EBUSY;
121 tp->t_state |= TS_CARR_ON;
122
123 return ((*linesw[tp->t_line].l_open)(dev, tp));
124 }
125
126 int
127 kdclose(dev, flag, mode, p)
128 dev_t dev;
129 int flag, mode;
130 struct proc *p;
131 {
132 int unit = minor(dev);
133 struct tty *tp = kd_tty[unit];
134
135 (*linesw[tp->t_line].l_close)(tp, flag);
136 ttyclose(tp);
137 return (0);
138 }
139
140 int
141 kdread(dev, uio, flag)
142 dev_t dev;
143 struct uio *uio;
144 int flag;
145 {
146 int unit = minor(dev);
147 struct tty *tp = kd_tty[unit];
148
149 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
150 }
151
152 int
153 kdwrite(dev, uio, flag)
154 dev_t dev;
155 struct uio *uio;
156 int flag;
157 {
158 int unit = minor(dev);
159 struct tty *tp = kd_tty[unit];
160
161 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
162 }
163
164 int
165 kdioctl(dev, cmd, data, flag, p)
166 dev_t dev;
167 int cmd;
168 caddr_t data;
169 int flag;
170 struct proc *p;
171 {
172 int error;
173 int unit = minor(dev);
174 struct tty *tp = kd_tty[unit];
175
176 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
177 if (error >= 0)
178 return error;
179 error = ttioctl(tp, cmd, data, flag, p);
180 if (error >= 0)
181 return error;
182
183 /* Handle any ioctl commands specific to kbd/display. */
184 /* XXX - Send KB* ioctls to kbd module? */
185 /* XXX - Send FB* ioctls to fb module? */
186
187 return ENOTTY;
188 }
189
190 static void kd_later(void*);
191 static void kd_putfb(struct tty *);
192
193 void
194 kdstart(tp)
195 struct tty *tp;
196 {
197 struct clist *cl;
198 register int s;
199
200 s = spltty();
201 if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
202 goto out;
203
204 cl = &tp->t_outq;
205 if (cl->c_cc) {
206 if (kd_is_console) {
207 tp->t_state |= TS_BUSY;
208 if ((s & PSL_IPL) == 0) {
209 /* called at level zero - update screen now. */
210 (void) splsoftclock();
211 kd_putfb(tp);
212 (void) spltty();
213 tp->t_state &= ~TS_BUSY;
214 } else {
215 /* called at interrupt level - do it later */
216 timeout(kd_later, (void*)tp, 0);
217 }
218 } else {
219 /*
220 * This driver uses the PROM for writing the screen,
221 * and that only works if this is the console device.
222 * If this is not the console, just flush the output.
223 * Sorry. (In that case, use xdm instead of getty.)
224 */
225 ndflush(cl, cl->c_cc);
226 }
227 }
228 if (cl->c_cc <= tp->t_lowat) {
229 if (tp->t_state & TS_ASLEEP) {
230 tp->t_state &= ~TS_ASLEEP;
231 wakeup((caddr_t)cl);
232 }
233 selwakeup(&tp->t_wsel);
234 }
235 out:
236 splx(s);
237 }
238
239 /*
240 * Timeout function to do delayed writes to the screen.
241 * Called at splsoftclock when requested by kdstart.
242 */
243 static void
244 kd_later(tpaddr)
245 void *tpaddr;
246 {
247 struct tty *tp = tpaddr;
248 register int s;
249
250 kd_putfb(tp);
251
252 s = spltty();
253 tp->t_state &= ~TS_BUSY;
254 if (tp->t_line)
255 (*linesw[tp->t_line].l_start)(tp);
256 else
257 kdstart(tp);
258 splx(s);
259 }
260
261 /*
262 * Put text on the screen using the PROM monitor.
263 * This can take a while, so to avoid missing
264 * interrupts, this is called at splsoftclock.
265 */
266 static void kd_putfb(tp)
267 struct tty *tp;
268 {
269 char buf[BURST];
270 struct clist *cl = &tp->t_outq;
271 char *p, *end;
272 int len;
273
274 while ((len = q_to_b(cl, buf, BURST-1)) > 0) {
275 /* PROM will barf if high bits are set. */
276 p = buf;
277 end = buf + len;
278 while (p < end)
279 *p++ &= 0x7f;
280 (romVectorPtr->fbWriteStr)(buf, len);
281 }
282 }
283
284
285 static int
286 kdparam(tp, t)
287 struct tty *tp;
288 struct termios *t;
289 {
290 /* XXX - These are ignored... */
291 tp->t_ispeed = t->c_ispeed;
292 tp->t_ospeed = t->c_ospeed;
293 tp->t_cflag = t->c_cflag;
294 return 0;
295 }
296
297
298 /*
299 * kd console support
300 */
301
302 extern int zscnprobe_kbd(), zscngetc(), kbd_translate();
303
304 kdcnprobe(cp)
305 struct consdev *cp;
306 {
307 int maj;
308
309 /* locate the major number */
310 for (maj = 0; maj < nchrdev; maj++)
311 if (cdevsw[maj].d_open == (void*)kdopen)
312 break;
313
314 /* initialize required fields */
315 cp->cn_dev = makedev(maj, 0);
316 cp->cn_pri = zscnprobe_kbd();
317 }
318
319 kdcninit(cp)
320 struct consdev *cp;
321 {
322
323 /* This prepares zscngetc() */
324 zs_set_conschan(1, 0);
325
326 /* This prepares kbd_translate() */
327 kbd_init_tables();
328
329 /* Indicate that it is OK to use the PROM fbwrite */
330 kd_is_console = 1;
331
332 mon_printf("console on kd0 (keyboard/display)\n");
333 }
334
335 kdcngetc(dev)
336 dev_t dev;
337 {
338 int c;
339
340 do {
341 c = zscngetc(0);
342 c = kbd_translate(c);
343 } while (c == -1);
344
345 return (c);
346 }
347
348 kdcnputc(dev, c)
349 dev_t dev;
350 int c;
351 {
352 (romVectorPtr->fbWriteChar)(c & 0x7f);
353 }
354
355 extern void fb_unblank();
356 void kdcnpollc(dev, on)
357 dev_t dev;
358 int on;
359 {
360 if (on)
361 fb_unblank();
362 }
363