Change the kernel dev_t, representing a pointer to a specinfo structure,
[dragonfly.git] / sys / kern / tty_cons.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: @(#)cons.c 7.2 (Berkeley) 5/9/91
39 * $FreeBSD: src/sys/kern/tty_cons.c,v 1.81.2.4 2001/12/17 18:44:41 guido Exp $
b13267a5 40 * $DragonFly: src/sys/kern/tty_cons.c,v 1.17 2006/09/10 01:26:39 dillon Exp $
984263bc
MD
41 */
42
43#include "opt_ddb.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/conf.h>
48#include <sys/cons.h>
49#include <sys/kernel.h>
50#include <sys/proc.h>
51#include <sys/reboot.h>
52#include <sys/sysctl.h>
53#include <sys/tty.h>
54#include <sys/uio.h>
335dda38
MD
55#include <sys/msgport.h>
56#include <sys/msgport2.h>
57#include <sys/device.h>
984263bc
MD
58
59#include <ddb/ddb.h>
60
61#include <machine/cpu.h>
62
fef8985e
MD
63static d_open_t cnopen;
64static d_close_t cnclose;
65static d_read_t cnread;
66static d_write_t cnwrite;
67static d_ioctl_t cnioctl;
68static d_poll_t cnpoll;
69static d_kqfilter_t cnkqfilter;
e4c9c0c8 70
fef8985e 71static int cnintercept(struct dev_generic_args *ap);
984263bc
MD
72
73#define CDEV_MAJOR 0
fef8985e
MD
74static struct dev_ops cn_ops = {
75 { "console", CDEV_MAJOR, D_TTY | D_KQFILTER },
76 .d_open = cnopen,
77 .d_close = cnclose,
78 .d_read = cnread,
79 .d_write = cnwrite,
80 .d_ioctl = cnioctl,
81 .d_poll = cnpoll,
82 .d_kqfilter = cnkqfilter,
83};
84
85static struct dev_ops cn_iops = {
86 { "intercept", CDEV_MAJOR, D_TTY | D_KQFILTER },
87 .d_default = cnintercept
984263bc
MD
88};
89
fef8985e 90static struct dev_ops *cn_fwd_ops;
b13267a5 91static cdev_t cn_dev;
fef8985e 92static udev_t cn_udev;
984263bc 93SYSCTL_OPAQUE(_machdep, CPU_CONSDEV, consdev, CTLFLAG_RD,
b13267a5 94 &cn_udev, sizeof cn_udev, "T,cdev_t", "");
984263bc
MD
95
96static int cn_mute;
97
98int cons_unavail = 0; /* XXX:
99 * physical console not available for
100 * input (i.e., it is in graphics mode)
101 */
102
103static u_char cn_is_open; /* nonzero if logical console is open */
104static int openmode, openflag; /* how /dev/console was openned */
b13267a5 105static cdev_t cn_devfsdev; /* represents the device private info */
984263bc 106static u_char cn_phys_is_open; /* nonzero if physical device is open */
984263bc
MD
107 struct consdev *cn_tab; /* physical console device info */
108static u_char console_pausing; /* pause after each line during probe */
109static char *console_pausestr=
110"<pause; press any key to proceed to next line or '.' to end pause mode>";
111
112CONS_DRIVER(cons, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
dc62b251 113SET_DECLARE(cons_set, struct consdev);
984263bc
MD
114
115void
116cninit()
117{
118 struct consdev *best_cp, *cp, **list;
119
120 /*
121 * Find the first console with the highest priority.
122 */
123 best_cp = NULL;
dc62b251
MD
124 SET_FOREACH(list, cons_set) {
125 cp = *list;
984263bc
MD
126 if (cp->cn_probe == NULL)
127 continue;
128 (*cp->cn_probe)(cp);
129 if (cp->cn_pri > CN_DEAD &&
130 (best_cp == NULL || cp->cn_pri > best_cp->cn_pri))
131 best_cp = cp;
132 }
133
134 /*
135 * Check if we should mute the console (for security reasons perhaps)
136 * It can be changes dynamically using sysctl kern.consmute
137 * once we are up and going.
138 *
139 */
140 cn_mute = ((boothowto & (RB_MUTE
141 |RB_SINGLE
142 |RB_VERBOSE
143 |RB_ASKNAME
144 |RB_CONFIG)) == RB_MUTE);
145
146 /*
147 * If no console, give up.
148 */
149 if (best_cp == NULL) {
150 if (cn_tab != NULL && cn_tab->cn_term != NULL)
151 (*cn_tab->cn_term)(cn_tab);
152 cn_tab = best_cp;
153 return;
154 }
155
156 /*
157 * Initialize console, then attach to it. This ordering allows
158 * debugging using the previous console, if any.
159 */
160 (*best_cp->cn_init)(best_cp);
161 if (cn_tab != NULL && cn_tab != best_cp) {
162 /* Turn off the previous console. */
163 if (cn_tab->cn_term != NULL)
164 (*cn_tab->cn_term)(cn_tab);
165 }
166 if (boothowto & RB_PAUSE)
167 console_pausing = 1;
168 cn_tab = best_cp;
169}
170
e4c9c0c8
MD
171/*
172 * Hook the open and close functions on the selected device.
173 */
984263bc
MD
174void
175cninit_finish()
176{
984263bc
MD
177 if ((cn_tab == NULL) || cn_mute)
178 return;
179
180 /*
e4c9c0c8 181 * Hook the open and close functions. XXX bad hack.
984263bc 182 */
e4c9c0c8 183 if (dev_is_good(cn_tab->cn_dev))
fef8985e
MD
184 cn_fwd_ops = dev_ops_intercept(cn_tab->cn_dev, &cn_iops);
185 cn_dev = cn_tab->cn_dev;
186 cn_udev = dev2udev(cn_dev);
984263bc
MD
187 console_pausing = 0;
188}
189
190static void
191cnuninit(void)
192{
984263bc
MD
193 if (cn_tab == NULL)
194 return;
195
196 /*
e4c9c0c8 197 * Unhook the open and close functions. XXX bad hack
984263bc 198 */
fef8985e
MD
199 if (cn_fwd_ops)
200 dev_ops_restore(cn_tab->cn_dev, cn_fwd_ops);
201 cn_fwd_ops = NULL;
202 cn_dev = NULL;
203 cn_udev = NOUDEV;
984263bc
MD
204}
205
206/*
207 * User has changed the state of the console muting.
208 * This may require us to open or close the device in question.
209 */
210static int
211sysctl_kern_consmute(SYSCTL_HANDLER_ARGS)
212{
213 int error;
214 int ocn_mute;
215
216 ocn_mute = cn_mute;
217 error = sysctl_handle_int(oidp, &cn_mute, 0, req);
218 if((error == 0) && (cn_tab != NULL) && (req->newptr != NULL)) {
219 if(ocn_mute && !cn_mute) {
220 /*
221 * going from muted to unmuted.. open the physical dev
222 * if the console has been openned
223 */
224 cninit_finish();
e4c9c0c8 225 if (cn_is_open) {
984263bc 226 /* XXX curproc is not what we want really */
fef8985e
MD
227 error = dev_dopen(cn_dev, openflag,
228 openmode, curproc->p_ucred);
e4c9c0c8 229 }
984263bc
MD
230 /* if it failed, back it out */
231 if ( error != 0) cnuninit();
232 } else if (!ocn_mute && cn_mute) {
233 /*
234 * going from unmuted to muted.. close the physical dev
235 * if it's only open via /dev/console
236 */
e4c9c0c8 237 if (cn_is_open) {
fef8985e
MD
238 error = dev_dclose(cn_dev, openflag,
239 openmode);
e4c9c0c8
MD
240 }
241 if (error == 0)
242 cnuninit();
984263bc
MD
243 }
244 if (error != 0) {
245 /*
246 * back out the change if there was an error
247 */
248 cn_mute = ocn_mute;
249 }
250 }
251 return (error);
252}
253
254SYSCTL_PROC(_kern, OID_AUTO, consmute, CTLTYPE_INT|CTLFLAG_RW,
255 0, sizeof cn_mute, sysctl_kern_consmute, "I", "");
256
e4c9c0c8
MD
257/*
258 * We intercept the OPEN and CLOSE calls on the original device, and
259 * forward the rest through.
260 */
261static int
fef8985e 262cnintercept(struct dev_generic_args *ap)
335dda38 263{
335dda38
MD
264 int error;
265
fef8985e
MD
266 if (ap->a_desc == &dev_open_desc) {
267 error = cnopen((struct dev_open_args *)ap);
268 } else if (ap->a_desc == &dev_close_desc) {
269 error = cnclose((struct dev_close_args *)ap);
270 } else if (cn_fwd_ops) {
271 error = dev_doperate_ops(cn_fwd_ops, ap);
272 } else {
273 error = ENXIO;
335dda38 274 }
fef8985e 275 return (error);
335dda38
MD
276}
277
e4c9c0c8 278/*
fef8985e 279 * cnopen() is called as an intercept function (dev will be that of the
e4c9c0c8
MD
280 * actual physical device representing our console), and also called from
281 * the muting code and from the /dev/console switch (dev will have the
282 * console's cdevsw).
283 */
984263bc 284static int
fef8985e 285cnopen(struct dev_open_args *ap)
984263bc 286{
b13267a5 287 cdev_t dev = ap->a_head.a_dev;
fef8985e
MD
288 int flag = ap->a_oflags;
289 int mode = ap->a_devtype;
b13267a5 290 cdev_t cndev, physdev;
984263bc
MD
291 int retval = 0;
292
fef8985e 293 if (cn_tab == NULL || cn_fwd_ops == NULL)
984263bc
MD
294 return (0);
295 cndev = cn_tab->cn_dev;
296 physdev = (major(dev) == major(cndev) ? dev : cndev);
e4c9c0c8 297
984263bc
MD
298 /*
299 * If mute is active, then non console opens don't get here
e4c9c0c8
MD
300 * so we don't need to check for that. They bypass this and go
301 * straight to the device.
302 *
fef8985e
MD
303 * It is important to note that due to our intercept and the fact
304 * that we might be called via the original (intercepted) device,
305 * the original device's ops may point to us, so to avoid an
306 * infinite recursion we have to forward through cn_fwd_ops.
307 * This is a severe hack that really needs to be fixed XXX.
308 *
e4c9c0c8
MD
309 * XXX at the moment we assume that the port forwarding function
310 * is synchronous for open.
984263bc 311 */
e4c9c0c8 312 if (!cn_mute) {
fef8985e
MD
313 ap->a_head.a_dev = physdev;
314 retval = dev_doperate_ops(cn_fwd_ops, &ap->a_head);
e4c9c0c8 315 }
984263bc
MD
316 if (retval == 0) {
317 /*
318 * check if we openned it via /dev/console or
319 * via the physical entry (e.g. /dev/sio0).
320 */
321 if (dev == cndev)
322 cn_phys_is_open = 1;
323 else if (physdev == cndev) {
324 openmode = mode;
325 openflag = flag;
326 cn_is_open = 1;
327 }
328 dev->si_tty = physdev->si_tty;
329 }
330 return (retval);
331}
332
e4c9c0c8
MD
333/*
334 * cnclose() is called as a port intercept function (dev will be that of the
335 * actual physical device representing our console), and also called from
336 * the muting code and from the /dev/console switch (dev will have the
337 * console's cdevsw).
338 */
984263bc 339static int
fef8985e 340cnclose(struct dev_close_args *ap)
984263bc 341{
b13267a5
MD
342 cdev_t dev = ap->a_head.a_dev;
343 cdev_t cndev;
984263bc
MD
344 struct tty *cn_tp;
345
fef8985e 346 if (cn_tab == NULL || cn_fwd_ops == NULL)
984263bc
MD
347 return (0);
348 cndev = cn_tab->cn_dev;
349 cn_tp = cndev->si_tty;
350 /*
351 * act appropriatly depending on whether it's /dev/console
352 * or the pysical device (e.g. /dev/sio) that's being closed.
353 * in either case, don't actually close the device unless
354 * both are closed.
355 */
356 if (dev == cndev) {
357 /* the physical device is about to be closed */
358 cn_phys_is_open = 0;
359 if (cn_is_open) {
360 if (cn_tp) {
361 /* perform a ttyhalfclose() */
362 /* reset session and proc group */
8b90699b 363 ttyclearsession(cn_tp);
984263bc
MD
364 }
365 return (0);
366 }
367 } else if (major(dev) != major(cndev)) {
368 /* the logical console is about to be closed */
369 cn_is_open = 0;
370 if (cn_phys_is_open)
371 return (0);
372 dev = cndev;
373 }
fef8985e
MD
374 if (cn_fwd_ops) {
375 ap->a_head.a_dev = dev;
376 return (dev_doperate_ops(cn_fwd_ops, &ap->a_head));
e4c9c0c8 377 }
984263bc
MD
378 return (0);
379}
380
e4c9c0c8
MD
381/*
382 * The following functions are dispatched solely from the /dev/console
fef8985e 383 * device. Their job is primarily to forward the request through.
e4c9c0c8
MD
384 * If the console is not attached to anything then write()'s are sunk
385 * to null and reads return 0 (mostly).
386 */
984263bc 387static int
fef8985e 388cnread(struct dev_read_args *ap)
984263bc 389{
fef8985e 390 if (cn_tab == NULL || cn_fwd_ops == NULL)
984263bc 391 return (0);
fef8985e
MD
392 ap->a_head.a_dev = cn_tab->cn_dev;
393 return (dev_doperate(&ap->a_head));
984263bc
MD
394}
395
396static int
fef8985e 397cnwrite(struct dev_write_args *ap)
984263bc 398{
fef8985e 399 struct uio *uio = ap->a_uio;
b13267a5 400 cdev_t dev;
984263bc 401
fef8985e 402 if (cn_tab == NULL || cn_fwd_ops == NULL) {
984263bc
MD
403 uio->uio_resid = 0; /* dump the data */
404 return (0);
405 }
406 if (constty)
407 dev = constty->t_dev;
408 else
409 dev = cn_tab->cn_dev;
410 log_console(uio);
fef8985e
MD
411 ap->a_head.a_dev = dev;
412 return (dev_doperate(&ap->a_head));
984263bc
MD
413}
414
415static int
fef8985e 416cnioctl(struct dev_ioctl_args *ap)
984263bc
MD
417{
418 int error;
419
fef8985e 420 if (cn_tab == NULL || cn_fwd_ops == NULL)
984263bc 421 return (0);
fef8985e 422 KKASSERT(curproc != NULL);
984263bc
MD
423 /*
424 * Superuser can always use this to wrest control of console
425 * output from the "virtual" console.
426 */
fef8985e
MD
427 if (ap->a_cmd == TIOCCONS && constty) {
428 if (ap->a_cred) {
429 error = suser_cred(ap->a_cred, 0);
430 if (error)
431 return (error);
432 }
984263bc
MD
433 constty = NULL;
434 return (0);
435 }
fef8985e
MD
436 ap->a_head.a_dev = cn_tab->cn_dev;
437 return (dev_doperate(&ap->a_head));
984263bc
MD
438}
439
440static int
fef8985e 441cnpoll(struct dev_poll_args *ap)
984263bc 442{
fef8985e 443 if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL)
984263bc 444 return (1);
fef8985e
MD
445 ap->a_head.a_dev = cn_tab->cn_dev;
446 return (dev_doperate(&ap->a_head));
984263bc
MD
447}
448
449static int
fef8985e 450cnkqfilter(struct dev_kqfilter_args *ap)
984263bc 451{
fef8985e 452 if ((cn_tab == NULL) || cn_mute || cn_fwd_ops == NULL)
984263bc 453 return (1);
fef8985e
MD
454 ap->a_head.a_dev = cn_tab->cn_dev;
455 return (dev_doperate(&ap->a_head));
984263bc
MD
456}
457
e4c9c0c8
MD
458/*
459 * These synchronous functions are primarily used the kernel needs to
460 * access the keyboard (e.g. when running the debugger), or output data
461 * directly to the console.
462 */
984263bc 463int
e4c9c0c8 464cngetc(void)
984263bc
MD
465{
466 int c;
467 if ((cn_tab == NULL) || cn_mute)
468 return (-1);
469 c = (*cn_tab->cn_getc)(cn_tab->cn_dev);
470 if (c == '\r') c = '\n'; /* console input is always ICRNL */
471 return (c);
472}
473
474int
e4c9c0c8 475cncheckc(void)
984263bc
MD
476{
477 if ((cn_tab == NULL) || cn_mute)
478 return (-1);
479 return ((*cn_tab->cn_checkc)(cn_tab->cn_dev));
480}
481
482void
e4c9c0c8 483cnputc(int c)
984263bc
MD
484{
485 char *cp;
486
487 if ((cn_tab == NULL) || cn_mute)
488 return;
489 if (c) {
490 if (c == '\n')
491 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
492 (*cn_tab->cn_putc)(cn_tab->cn_dev, c);
493#ifdef DDB
494 if (console_pausing && !db_active && (c == '\n')) {
495#else
496 if (console_pausing && (c == '\n')) {
497#endif
498 for(cp=console_pausestr; *cp != '\0'; cp++)
499 (*cn_tab->cn_putc)(cn_tab->cn_dev, *cp);
500 if (cngetc() == '.')
501 console_pausing = 0;
502 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
503 for(cp=console_pausestr; *cp != '\0'; cp++)
504 (*cn_tab->cn_putc)(cn_tab->cn_dev, ' ');
505 (*cn_tab->cn_putc)(cn_tab->cn_dev, '\r');
506 }
507 }
508}
509
510void
e4c9c0c8 511cndbctl(int on)
984263bc
MD
512{
513 static int refcount;
514
515 if (cn_tab == NULL)
516 return;
517 if (!on)
518 refcount--;
519 if (refcount == 0 && cn_tab->cn_dbctl != NULL)
520 (*cn_tab->cn_dbctl)(cn_tab->cn_dev, on);
521 if (on)
522 refcount++;
523}
524
525static void
526cn_drvinit(void *unused)
527{
fef8985e
MD
528 dev_ops_add(&cn_ops, 0, 0);
529 cn_devfsdev = make_dev(&cn_ops, 0, UID_ROOT, GID_WHEEL,
e4c9c0c8 530 0600, "console");
984263bc
MD
531}
532
533SYSINIT(cndev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,cn_drvinit,NULL)