kd.c revision 1.7 1 1.7 pk /* $NetBSD: kd.c,v 1.7 2000/03/19 13:22:14 pk Exp $ */
2 1.1 gwr
3 1.1 gwr /*-
4 1.1 gwr * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 1.1 gwr * All rights reserved.
6 1.1 gwr *
7 1.1 gwr * This code is derived from software contributed to The NetBSD Foundation
8 1.1 gwr * by Gordon W. Ross.
9 1.1 gwr *
10 1.1 gwr * Redistribution and use in source and binary forms, with or without
11 1.1 gwr * modification, are permitted provided that the following conditions
12 1.1 gwr * are met:
13 1.1 gwr * 1. Redistributions of source code must retain the above copyright
14 1.1 gwr * notice, this list of conditions and the following disclaimer.
15 1.1 gwr * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 gwr * notice, this list of conditions and the following disclaimer in the
17 1.1 gwr * documentation and/or other materials provided with the distribution.
18 1.1 gwr * 3. All advertising materials mentioning features or use of this software
19 1.1 gwr * must display the following acknowledgement:
20 1.1 gwr * This product includes software developed by the NetBSD
21 1.1 gwr * Foundation, Inc. and its contributors.
22 1.1 gwr * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 gwr * contributors may be used to endorse or promote products derived
24 1.1 gwr * from this software without specific prior written permission.
25 1.1 gwr *
26 1.1 gwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 gwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 gwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 gwr * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 gwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 gwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 gwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 gwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 gwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 gwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 gwr * POSSIBILITY OF SUCH DAMAGE.
37 1.1 gwr */
38 1.1 gwr
39 1.1 gwr /*
40 1.7 pk * Console driver based on PROM primitives.
41 1.1 gwr *
42 1.7 pk * This driver exists to provide a tty device that the indirect
43 1.7 pk * console driver can point to. It also provides a hook that
44 1.7 pk * allows another device to serve console input. This will normally
45 1.7 pk * be a keyboard driver (see sys/dev/sun/kbd.c)
46 1.1 gwr */
47 1.1 gwr
48 1.1 gwr #include <sys/param.h>
49 1.1 gwr #include <sys/proc.h>
50 1.1 gwr #include <sys/systm.h>
51 1.7 pk #include <sys/kernel.h>
52 1.1 gwr #include <sys/ioctl.h>
53 1.1 gwr #include <sys/tty.h>
54 1.1 gwr #include <sys/file.h>
55 1.1 gwr #include <sys/conf.h>
56 1.1 gwr #include <sys/device.h>
57 1.1 gwr
58 1.1 gwr #include <machine/bsd_openprom.h>
59 1.4 pk #include <machine/promlib.h>
60 1.1 gwr #include <machine/eeprom.h>
61 1.1 gwr #include <machine/psl.h>
62 1.1 gwr #include <machine/cpu.h>
63 1.1 gwr #include <machine/kbd.h>
64 1.1 gwr #include <machine/autoconf.h>
65 1.1 gwr #include <machine/conf.h>
66 1.1 gwr
67 1.1 gwr #ifdef RASTERCONSOLE
68 1.1 gwr #include <machine/fbio.h>
69 1.1 gwr #include <machine/fbvar.h>
70 1.1 gwr #endif
71 1.1 gwr
72 1.7 pk #include <dev/cons.h>
73 1.7 pk #include <sparc/dev/cons.h>
74 1.1 gwr
75 1.7 pk #include <dev/sun/event_var.h>
76 1.1 gwr #include <dev/sun/kbd_xlate.h>
77 1.7 pk #include <dev/sun/kbdvar.h>
78 1.1 gwr
79 1.1 gwr struct tty *fbconstty = 0; /* tty structure for frame buffer console */
80 1.1 gwr
81 1.1 gwr #define KDMAJOR 1
82 1.1 gwr #define PUT_WSIZE 64
83 1.1 gwr
84 1.1 gwr struct kd_softc {
85 1.1 gwr struct device kd_dev; /* required first: base device */
86 1.1 gwr struct tty *kd_tty;
87 1.1 gwr int rows, cols;
88 1.7 pk
89 1.7 pk /* Console input hook */
90 1.7 pk struct cons_channel *kd_in;
91 1.1 gwr };
92 1.1 gwr
93 1.1 gwr /*
94 1.1 gwr * There is no point in pretending there might be
95 1.1 gwr * more than one keyboard/display device.
96 1.1 gwr */
97 1.1 gwr static struct kd_softc kd_softc;
98 1.1 gwr
99 1.1 gwr static int kdparam(struct tty *, struct termios *);
100 1.1 gwr static void kdstart(struct tty *);
101 1.7 pk static void kd_init __P((struct kd_softc *));
102 1.7 pk static void kd_cons_input __P((int));
103 1.1 gwr
104 1.1 gwr
105 1.1 gwr /*
106 1.7 pk * Prepare the console tty; called on first open of /dev/console
107 1.1 gwr */
108 1.1 gwr void
109 1.7 pk kd_init(kd)
110 1.7 pk struct kd_softc *kd;
111 1.1 gwr {
112 1.1 gwr struct tty *tp;
113 1.1 gwr
114 1.1 gwr tp = ttymalloc();
115 1.1 gwr tp->t_oproc = kdstart;
116 1.1 gwr tp->t_param = kdparam;
117 1.7 pk tp->t_dev = makedev(KDMAJOR, 0);
118 1.1 gwr
119 1.1 gwr tty_attach(tp);
120 1.1 gwr kd->kd_tty = tp;
121 1.1 gwr
122 1.1 gwr /*
123 1.7 pk * Get the console struct winsize.
124 1.1 gwr */
125 1.1 gwr #ifdef RASTERCONSOLE
126 1.7 pk /* If the raster console driver is attached, copy its size */
127 1.7 pk kd->rows = fbrcons_rows();
128 1.7 pk kd->cols = fbrcons_cols();
129 1.1 gwr #endif
130 1.1 gwr
131 1.7 pk /* else, consult the PROM */
132 1.7 pk switch (prom_version()) {
133 1.7 pk char *prop;
134 1.7 pk struct eeprom *ep;
135 1.7 pk case PROM_OLDMON:
136 1.7 pk if ((ep = (struct eeprom *)eeprom_va) == NULL)
137 1.7 pk break;
138 1.7 pk if (kd->rows == 0)
139 1.7 pk kd->rows = (u_short)ep->eeTtyRows;
140 1.7 pk if (kd->cols == 0)
141 1.7 pk kd->cols = (u_short)ep->eeTtyCols;
142 1.7 pk break;
143 1.7 pk case PROM_OBP_V0:
144 1.7 pk case PROM_OBP_V2:
145 1.7 pk case PROM_OBP_V3:
146 1.7 pk case PROM_OPENFIRM:
147 1.1 gwr
148 1.1 gwr if (kd->rows == 0 &&
149 1.1 gwr (prop = getpropstring(optionsnode, "screen-#rows"))) {
150 1.7 pk int i = 0;
151 1.7 pk
152 1.1 gwr while (*prop != '\0')
153 1.1 gwr i = i * 10 + *prop++ - '0';
154 1.1 gwr kd->rows = (unsigned short)i;
155 1.1 gwr }
156 1.1 gwr if (kd->cols == 0 &&
157 1.1 gwr (prop = getpropstring(optionsnode, "screen-#columns"))) {
158 1.7 pk int i = 0;
159 1.7 pk
160 1.1 gwr while (*prop != '\0')
161 1.1 gwr i = i * 10 + *prop++ - '0';
162 1.1 gwr kd->cols = (unsigned short)i;
163 1.1 gwr }
164 1.7 pk break;
165 1.1 gwr }
166 1.1 gwr
167 1.1 gwr return;
168 1.1 gwr }
169 1.1 gwr
170 1.1 gwr struct tty *
171 1.1 gwr kdtty(dev)
172 1.1 gwr dev_t dev;
173 1.1 gwr {
174 1.1 gwr struct kd_softc *kd;
175 1.1 gwr
176 1.1 gwr kd = &kd_softc; /* XXX */
177 1.1 gwr return (kd->kd_tty);
178 1.1 gwr }
179 1.1 gwr
180 1.1 gwr int
181 1.1 gwr kdopen(dev, flag, mode, p)
182 1.1 gwr dev_t dev;
183 1.1 gwr int flag, mode;
184 1.1 gwr struct proc *p;
185 1.1 gwr {
186 1.1 gwr struct kd_softc *kd;
187 1.1 gwr int error, s, unit;
188 1.1 gwr struct tty *tp;
189 1.7 pk static int firstopen = 1;
190 1.7 pk
191 1.1 gwr unit = minor(dev);
192 1.1 gwr if (unit != 0)
193 1.1 gwr return ENXIO;
194 1.7 pk
195 1.1 gwr kd = &kd_softc; /* XXX */
196 1.7 pk if (firstopen) {
197 1.7 pk kd_init(kd);
198 1.7 pk firstopen = 0;
199 1.7 pk }
200 1.7 pk
201 1.1 gwr tp = kd->kd_tty;
202 1.1 gwr
203 1.1 gwr /* It's simpler to do this up here. */
204 1.1 gwr if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
205 1.1 gwr == (TS_ISOPEN | TS_XCLUDE))
206 1.1 gwr && (p->p_ucred->cr_uid != 0) )
207 1.1 gwr {
208 1.1 gwr return (EBUSY);
209 1.1 gwr }
210 1.1 gwr
211 1.1 gwr s = spltty();
212 1.1 gwr if ((tp->t_state & TS_ISOPEN) == 0) {
213 1.1 gwr /* First open. */
214 1.7 pk
215 1.7 pk /* Notify the input device that serves us */
216 1.7 pk struct cons_channel *cc = kd->kd_in;
217 1.7 pk if (cc != NULL &&
218 1.7 pk (error = (*cc->cc_iopen)(cc)) != 0) {
219 1.7 pk return (error);
220 1.7 pk }
221 1.7 pk
222 1.1 gwr ttychars(tp);
223 1.1 gwr tp->t_iflag = TTYDEF_IFLAG;
224 1.1 gwr tp->t_oflag = TTYDEF_OFLAG;
225 1.1 gwr tp->t_cflag = TTYDEF_CFLAG;
226 1.1 gwr tp->t_lflag = TTYDEF_LFLAG;
227 1.1 gwr tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
228 1.1 gwr (void) kdparam(tp, &tp->t_termios);
229 1.1 gwr ttsetwater(tp);
230 1.1 gwr tp->t_winsize.ws_row = kd->rows;
231 1.1 gwr tp->t_winsize.ws_col = kd->cols;
232 1.1 gwr /* Flush pending input? Clear translator? */
233 1.1 gwr /* This (pseudo)device always has SOFTCAR */
234 1.1 gwr tp->t_state |= TS_CARR_ON;
235 1.1 gwr }
236 1.1 gwr
237 1.1 gwr splx(s);
238 1.1 gwr
239 1.1 gwr return ((*linesw[tp->t_line].l_open)(dev, tp));
240 1.1 gwr }
241 1.1 gwr
242 1.1 gwr int
243 1.1 gwr kdclose(dev, flag, mode, p)
244 1.1 gwr dev_t dev;
245 1.1 gwr int flag, mode;
246 1.1 gwr struct proc *p;
247 1.1 gwr {
248 1.1 gwr struct kd_softc *kd;
249 1.1 gwr struct tty *tp;
250 1.7 pk struct cons_channel *cc;
251 1.1 gwr
252 1.1 gwr kd = &kd_softc; /* XXX */
253 1.1 gwr tp = kd->kd_tty;
254 1.1 gwr
255 1.1 gwr /* XXX This is for cons.c. */
256 1.1 gwr if ((tp->t_state & TS_ISOPEN) == 0)
257 1.1 gwr return 0;
258 1.1 gwr
259 1.1 gwr (*linesw[tp->t_line].l_close)(tp, flag);
260 1.1 gwr ttyclose(tp);
261 1.7 pk
262 1.7 pk if ((cc = kd->kd_in) != NULL)
263 1.7 pk (void)(*cc->cc_iclose)(cc->cc_dev);
264 1.7 pk
265 1.1 gwr return (0);
266 1.1 gwr }
267 1.1 gwr
268 1.1 gwr int
269 1.1 gwr kdread(dev, uio, flag)
270 1.1 gwr dev_t dev;
271 1.1 gwr struct uio *uio;
272 1.1 gwr int flag;
273 1.1 gwr {
274 1.1 gwr struct kd_softc *kd;
275 1.1 gwr struct tty *tp;
276 1.1 gwr
277 1.1 gwr kd = &kd_softc; /* XXX */
278 1.1 gwr tp = kd->kd_tty;
279 1.1 gwr
280 1.1 gwr return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
281 1.1 gwr }
282 1.1 gwr
283 1.1 gwr int
284 1.1 gwr kdwrite(dev, uio, flag)
285 1.1 gwr dev_t dev;
286 1.1 gwr struct uio *uio;
287 1.1 gwr int flag;
288 1.1 gwr {
289 1.1 gwr struct kd_softc *kd;
290 1.1 gwr struct tty *tp;
291 1.1 gwr
292 1.1 gwr kd = &kd_softc; /* XXX */
293 1.1 gwr tp = kd->kd_tty;
294 1.1 gwr
295 1.1 gwr return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
296 1.1 gwr }
297 1.1 gwr
298 1.1 gwr int
299 1.1 gwr kdioctl(dev, cmd, data, flag, p)
300 1.1 gwr dev_t dev;
301 1.1 gwr u_long cmd;
302 1.1 gwr caddr_t data;
303 1.1 gwr int flag;
304 1.1 gwr struct proc *p;
305 1.1 gwr {
306 1.1 gwr struct kd_softc *kd;
307 1.1 gwr struct tty *tp;
308 1.1 gwr int error;
309 1.1 gwr
310 1.1 gwr kd = &kd_softc; /* XXX */
311 1.1 gwr tp = kd->kd_tty;
312 1.1 gwr
313 1.1 gwr error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
314 1.1 gwr if (error >= 0)
315 1.1 gwr return error;
316 1.1 gwr error = ttioctl(tp, cmd, data, flag, p);
317 1.1 gwr if (error >= 0)
318 1.1 gwr return error;
319 1.1 gwr
320 1.1 gwr /* Handle any ioctl commands specific to kbd/display. */
321 1.1 gwr /* XXX - Send KB* ioctls to kbd module? */
322 1.1 gwr /* XXX - Send FB* ioctls to fb module? */
323 1.1 gwr
324 1.1 gwr return ENOTTY;
325 1.1 gwr }
326 1.1 gwr
327 1.1 gwr void
328 1.1 gwr kdstop(tp, flag)
329 1.1 gwr struct tty *tp;
330 1.1 gwr int flag;
331 1.1 gwr {
332 1.1 gwr
333 1.1 gwr }
334 1.1 gwr
335 1.1 gwr
336 1.1 gwr static int
337 1.1 gwr kdparam(tp, t)
338 1.1 gwr struct tty *tp;
339 1.1 gwr struct termios *t;
340 1.1 gwr {
341 1.1 gwr /* XXX - These are ignored... */
342 1.1 gwr tp->t_ispeed = t->c_ispeed;
343 1.1 gwr tp->t_ospeed = t->c_ospeed;
344 1.1 gwr tp->t_cflag = t->c_cflag;
345 1.1 gwr return 0;
346 1.1 gwr }
347 1.1 gwr
348 1.1 gwr
349 1.1 gwr static void kd_later(void*);
350 1.1 gwr static void kd_putfb(struct tty *);
351 1.1 gwr
352 1.1 gwr static void
353 1.1 gwr kdstart(tp)
354 1.1 gwr struct tty *tp;
355 1.1 gwr {
356 1.1 gwr struct clist *cl;
357 1.7 pk int s;
358 1.1 gwr
359 1.1 gwr s = spltty();
360 1.1 gwr if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
361 1.1 gwr goto out;
362 1.1 gwr
363 1.1 gwr cl = &tp->t_outq;
364 1.1 gwr if (cl->c_cc) {
365 1.7 pk tp->t_state |= TS_BUSY;
366 1.7 pk if ((s & PSR_PIL) == 0) {
367 1.7 pk /* called at level zero - update screen now. */
368 1.7 pk (void) spllowersoftclock();
369 1.7 pk kd_putfb(tp);
370 1.7 pk (void) spltty();
371 1.7 pk tp->t_state &= ~TS_BUSY;
372 1.1 gwr } else {
373 1.7 pk /* called at interrupt level - do it later */
374 1.7 pk timeout(kd_later, (void*)tp, 0);
375 1.1 gwr }
376 1.1 gwr }
377 1.1 gwr if (cl->c_cc <= tp->t_lowat) {
378 1.1 gwr if (tp->t_state & TS_ASLEEP) {
379 1.1 gwr tp->t_state &= ~TS_ASLEEP;
380 1.1 gwr wakeup((caddr_t)cl);
381 1.1 gwr }
382 1.1 gwr selwakeup(&tp->t_wsel);
383 1.1 gwr }
384 1.1 gwr out:
385 1.1 gwr splx(s);
386 1.1 gwr }
387 1.1 gwr
388 1.1 gwr /*
389 1.1 gwr * Timeout function to do delayed writes to the screen.
390 1.1 gwr * Called at splsoftclock when requested by kdstart.
391 1.1 gwr */
392 1.1 gwr static void
393 1.7 pk kd_later(arg)
394 1.7 pk void *arg;
395 1.1 gwr {
396 1.7 pk struct tty *tp = arg;
397 1.7 pk int s;
398 1.1 gwr
399 1.1 gwr kd_putfb(tp);
400 1.1 gwr
401 1.1 gwr s = spltty();
402 1.1 gwr tp->t_state &= ~TS_BUSY;
403 1.1 gwr (*linesw[tp->t_line].l_start)(tp);
404 1.1 gwr splx(s);
405 1.1 gwr }
406 1.1 gwr
407 1.1 gwr /*
408 1.1 gwr * Put text on the screen using the PROM monitor.
409 1.1 gwr * This can take a while, so to avoid missing
410 1.1 gwr * interrupts, this is called at splsoftclock.
411 1.1 gwr */
412 1.1 gwr static void
413 1.1 gwr kd_putfb(tp)
414 1.1 gwr struct tty *tp;
415 1.1 gwr {
416 1.1 gwr char buf[PUT_WSIZE];
417 1.1 gwr struct clist *cl = &tp->t_outq;
418 1.1 gwr char *p, *end;
419 1.1 gwr int len;
420 1.1 gwr
421 1.1 gwr while ((len = q_to_b(cl, buf, PUT_WSIZE-1)) > 0) {
422 1.1 gwr /* PROM will barf if high bits are set. */
423 1.1 gwr p = buf;
424 1.1 gwr end = buf + len;
425 1.1 gwr while (p < end)
426 1.1 gwr *p++ &= 0x7f;
427 1.4 pk
428 1.1 gwr /* Now let the PROM print it. */
429 1.4 pk prom_putstr(buf, len);
430 1.1 gwr }
431 1.1 gwr }
432 1.1 gwr
433 1.1 gwr /*
434 1.1 gwr * Our "interrupt" routine for input. This is called by
435 1.1 gwr * the keyboard driver (dev/sun/kbd.c) at spltty.
436 1.1 gwr */
437 1.1 gwr void
438 1.7 pk kd_cons_input(c)
439 1.1 gwr int c;
440 1.1 gwr {
441 1.1 gwr struct kd_softc *kd = &kd_softc;
442 1.1 gwr struct tty *tp;
443 1.1 gwr
444 1.1 gwr /* XXX: Make sure the device is open. */
445 1.1 gwr tp = kd->kd_tty;
446 1.1 gwr if (tp == NULL)
447 1.1 gwr return;
448 1.1 gwr if ((tp->t_state & TS_ISOPEN) == 0)
449 1.1 gwr return;
450 1.1 gwr
451 1.1 gwr (*linesw[tp->t_line].l_rint)(c, tp);
452 1.1 gwr }
453 1.1 gwr
454 1.7 pk void
455 1.7 pk cons_attach_input(cc)
456 1.7 pk struct cons_channel *cc;
457 1.7 pk {
458 1.7 pk struct kd_softc *kd = &kd_softc;
459 1.7 pk
460 1.7 pk kd->kd_in = cc;
461 1.7 pk cc->cc_upstream = kd_cons_input;
462 1.7 pk }
463 1.7 pk
464 1.7 pk
465 1.7 pk /*
466 1.7 pk * Default PROM-based console input stream
467 1.7 pk * Since the PROM does not notify us when data is available on the
468 1.7 pk * input channel these functions periodically poll the PROM.
469 1.7 pk */
470 1.7 pk static int kd_rom_iopen __P((struct cons_channel *));
471 1.7 pk static int kd_rom_iclose __P((struct cons_channel *));
472 1.7 pk static void kd_rom_intr __P((void *));
473 1.7 pk
474 1.7 pk static struct cons_channel prom_cons_channel;
475 1.7 pk
476 1.7 pk int
477 1.7 pk kd_rom_iopen(cc)
478 1.7 pk struct cons_channel *cc;
479 1.7 pk {
480 1.7 pk /* Poll for ROM input 4 times per second */
481 1.7 pk timeout(kd_rom_intr, cc, hz/4);
482 1.7 pk return (0);
483 1.7 pk }
484 1.7 pk
485 1.7 pk int
486 1.7 pk kd_rom_iclose(cc)
487 1.7 pk struct cons_channel *cc;
488 1.7 pk {
489 1.7 pk
490 1.7 pk untimeout(kd_rom_intr, cc);
491 1.7 pk return (0);
492 1.7 pk }
493 1.7 pk
494 1.7 pk /*
495 1.7 pk * "Interrupt" routine for input through ROM vectors
496 1.7 pk */
497 1.7 pk void
498 1.7 pk kd_rom_intr(arg)
499 1.7 pk void *arg;
500 1.7 pk {
501 1.7 pk struct cons_channel *cc = arg;
502 1.7 pk int s, c;
503 1.7 pk
504 1.7 pk /* Re-schedule */
505 1.7 pk timeout(kd_rom_intr, arg, hz/4);
506 1.7 pk
507 1.7 pk s = spltty();
508 1.7 pk
509 1.7 pk while ((c = prom_peekchar()) >= 0)
510 1.7 pk (*cc->cc_upstream)(c);
511 1.7 pk
512 1.7 pk splx(s);
513 1.7 pk }
514 1.1 gwr
515 1.7 pk /*****************************************************************/
516 1.7 pk
517 1.7 pk int prom_stdin_node;
518 1.7 pk int prom_stdout_node;
519 1.7 pk char prom_stdin_args[16];
520 1.7 pk char prom_stdout_args[16];
521 1.7 pk
522 1.7 pk extern void prom_cnprobe __P((struct consdev *));
523 1.7 pk static void prom_cninit __P((struct consdev *));
524 1.7 pk static int prom_cngetc __P((dev_t));
525 1.7 pk static void prom_cnputc __P((dev_t, int));
526 1.7 pk extern void prom_cnpollc __P((dev_t, int));
527 1.7 pk
528 1.7 pk /*
529 1.7 pk * The console is set to this one initially,
530 1.7 pk * which lets us use the PROM until consinit()
531 1.7 pk * is called to select a real console.
532 1.7 pk */
533 1.7 pk struct consdev consdev_prom = {
534 1.7 pk prom_cnprobe,
535 1.7 pk prom_cninit,
536 1.7 pk prom_cngetc,
537 1.7 pk prom_cnputc,
538 1.7 pk prom_cnpollc,
539 1.6 thorpej NULL,
540 1.1 gwr };
541 1.1 gwr
542 1.7 pk /*
543 1.7 pk * The console table pointer is statically initialized
544 1.7 pk * to point to the PROM table, so that early calls to printf will work.
545 1.7 pk */
546 1.7 pk struct consdev *cn_tab = &consdev_prom;
547 1.7 pk
548 1.7 pk void
549 1.7 pk prom_cnprobe(cn)
550 1.1 gwr struct consdev *cn;
551 1.1 gwr {
552 1.1 gwr }
553 1.1 gwr
554 1.1 gwr static void
555 1.7 pk prom_cninit(cn)
556 1.1 gwr struct consdev *cn;
557 1.1 gwr {
558 1.7 pk }
559 1.1 gwr
560 1.7 pk void
561 1.7 pk prom_cnpollc(dev, on)
562 1.7 pk dev_t dev;
563 1.7 pk int on;
564 1.7 pk {
565 1.1 gwr
566 1.7 pk if (on) {
567 1.7 pk /* Entering debugger. */
568 1.7 pk #if NFB > 0
569 1.7 pk fb_unblank();
570 1.7 pk #endif
571 1.7 pk } else {
572 1.7 pk /* Resuming kernel. */
573 1.7 pk }
574 1.7 pk }
575 1.1 gwr
576 1.1 gwr
577 1.7 pk /*
578 1.7 pk * PROM console input putchar.
579 1.7 pk */
580 1.1 gwr static int
581 1.7 pk prom_cngetc(dev)
582 1.1 gwr dev_t dev;
583 1.1 gwr {
584 1.7 pk int s, c;
585 1.1 gwr
586 1.7 pk s = splhigh();
587 1.7 pk c = prom_getchar();
588 1.7 pk splx(s);
589 1.7 pk return (c);
590 1.1 gwr }
591 1.1 gwr
592 1.7 pk /*
593 1.7 pk * PROM console output putchar.
594 1.7 pk */
595 1.1 gwr static void
596 1.7 pk prom_cnputc(dev, c)
597 1.1 gwr dev_t dev;
598 1.1 gwr int c;
599 1.1 gwr {
600 1.1 gwr
601 1.4 pk prom_putchar(c);
602 1.1 gwr }
603 1.1 gwr
604 1.7 pk
605 1.7 pk /*****************************************************************/
606 1.7 pk
607 1.7 pk static void prom_get_device_args __P((const char *, char *, unsigned int));
608 1.7 pk
609 1.7 pk void
610 1.7 pk prom_get_device_args(prop, args, sz)
611 1.7 pk const char *prop;
612 1.7 pk char *args;
613 1.7 pk unsigned int sz;
614 1.1 gwr {
615 1.7 pk char *cp, buffer[128];
616 1.7 pk
617 1.7 pk cp = getpropstringA(findroot(), (char *)prop, buffer, sizeof buffer);
618 1.1 gwr
619 1.7 pk /*
620 1.7 pk * Extract device-specific arguments from a PROM device path (if any)
621 1.7 pk */
622 1.7 pk cp = buffer + strlen(buffer);
623 1.7 pk while (cp >= buffer) {
624 1.7 pk if (*cp == ':') {
625 1.7 pk strncpy(args, cp+1, sz);
626 1.7 pk break;
627 1.7 pk }
628 1.7 pk cp--;
629 1.1 gwr }
630 1.1 gwr }
631 1.1 gwr
632 1.7 pk /*
633 1.7 pk *
634 1.7 pk */
635 1.7 pk void
636 1.7 pk consinit()
637 1.7 pk {
638 1.7 pk int inSource, outSink;
639 1.7 pk
640 1.7 pk switch (prom_version()) {
641 1.7 pk case PROM_OLDMON:
642 1.7 pk case PROM_OBP_V0:
643 1.7 pk /* The stdio handles identify the device type */
644 1.7 pk inSource = prom_stdin();
645 1.7 pk outSink = prom_stdout();
646 1.7 pk break;
647 1.7 pk
648 1.7 pk case PROM_OBP_V2:
649 1.7 pk case PROM_OBP_V3:
650 1.7 pk case PROM_OPENFIRM:
651 1.7 pk
652 1.7 pk /* Save PROM arguments for device matching */
653 1.7 pk prom_get_device_args("stdin-path", prom_stdin_args,
654 1.7 pk sizeof(prom_stdin_args));
655 1.7 pk prom_get_device_args("stdout-path", prom_stdout_args,
656 1.7 pk sizeof(prom_stdout_args));
657 1.7 pk
658 1.7 pk /*
659 1.7 pk * Translate the STDIO package instance (`ihandle') -- that
660 1.7 pk * the PROM has already opened for us -- to a device tree
661 1.7 pk * node (i.e. a `phandle').
662 1.7 pk */
663 1.7 pk
664 1.7 pk prom_stdin_node = prom_instance_to_package(prom_stdin());
665 1.7 pk if (prom_stdin_node == 0)
666 1.7 pk printf("consinit: cannot convert stdin ihandle\n");
667 1.7 pk
668 1.7 pk prom_stdout_node = prom_instance_to_package(prom_stdout());
669 1.7 pk if (prom_stdout_node == 0) {
670 1.7 pk printf("consinit: cannot convert stdout ihandle\n");
671 1.7 pk break;
672 1.7 pk }
673 1.7 pk
674 1.7 pk break;
675 1.7 pk
676 1.7 pk default:
677 1.7 pk break;
678 1.7 pk }
679 1.7 pk
680 1.7 pk /* Wire up /dev/console */
681 1.7 pk cn_tab->cn_dev = makedev(KDMAJOR, 0);
682 1.7 pk cn_tab->cn_pri = CN_INTERNAL;
683 1.7 pk
684 1.7 pk /* Set up initial PROM input channel for /dev/console */
685 1.7 pk prom_cons_channel.cc_dev = NULL;
686 1.7 pk prom_cons_channel.cc_iopen = kd_rom_iopen;
687 1.7 pk prom_cons_channel.cc_iclose = kd_rom_iclose;
688 1.7 pk cons_attach_input(&prom_cons_channel);
689 1.7 pk
690 1.7 pk #ifdef KGDB
691 1.7 pk zs_kgdb_init(); /* XXX */
692 1.7 pk #endif
693 1.7 pk }
694