]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/i386/cons.c
xnu-6153.61.1.tar.gz
[apple/xnu.git] / bsd / dev / i386 / cons.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
2d21ac55
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
28
29/*
b0d623f7
A
30 * Indirect driver for console
31 *
32 * The purpose of this driver is to provide a device node indirection for
33 * the console device, which can be any tty class device. It does this by
34 * externalizing a global pointer "constty", which is then pointed at the
35 * console tty device.
36 *
37 * The default for this pointer is uninitialized; when it is NULL, we fall
38 * back to the "km" device, which is a tty BSD wrapper device for the
39 * Platform Expert console device. When it is non-NULL, we call through
40 * to the tty device device instead.
41 *
42 * The registration for this device node is static, and the devfs init
43 * code does not externalize a named device for it, to avoid software
44 * seeing the device and trying to open it.
45 *
46 * The upshot of this is that the console driver should not be set as your
47 * controlling tty, since you will get a reference to a device which does
48 * not have an actual device node in /dev, so its name cannot be looked up.
1c79356b
A
49 */
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/conf.h>
53#include <sys/ioctl.h>
54#include <sys/tty.h>
55#include <sys/proc.h>
56#include <sys/uio.h>
57
0a7de745 58struct tty *constty; /* current console device */
1c79356b 59
b0d623f7
A
60/*
61 * The km driver supplied the default console device for the systems
62 * (usually a raw frame buffer driver, but potentially a serial driver).
63 */
64extern struct tty *km_tty[1];
65
66/*
67 * cdevsw[] entries for the console device driver
68 */
69int cnopen(__unused dev_t dev, int flag, int devtype, proc_t pp);
70int cnclose(__unused dev_t dev, int flag, int mode, proc_t pp);
91447636
A
71int cnread(__unused dev_t dev, struct uio *uio, int ioflag);
72int cnwrite(__unused dev_t dev, struct uio *uio, int ioflag);
b0d623f7
A
73int cnioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flg, proc_t p);
74int cnselect(__unused dev_t dev, int flag, void * wql, proc_t p);
91447636 75
b0d623f7
A
76static dev_t
77cndev(void)
1c79356b 78{
0a7de745 79 if (constty) {
b0d623f7 80 return constty->t_dev;
0a7de745 81 } else {
b0d623f7 82 return km_tty[0]->t_dev;
0a7de745 83 }
1c79356b
A
84}
85
1c79356b 86int
b0d623f7 87cnopen(__unused dev_t dev, int flag, int devtype, struct proc *pp)
1c79356b 88{
b0d623f7 89 dev = cndev();
0a7de745 90 return (*cdevsw[major(dev)].d_open)(dev, flag, devtype, pp);
b0d623f7 91}
91447636
A
92
93
b0d623f7
A
94int
95cnclose(__unused dev_t dev, int flag, int mode, struct proc *pp)
96{
97 dev = cndev();
0a7de745 98 return (*cdevsw[major(dev)].d_close)(dev, flag, mode, pp);
1c79356b
A
99}
100
b0d623f7 101
1c79356b 102int
91447636 103cnread(__unused dev_t dev, struct uio *uio, int ioflag)
1c79356b 104{
b0d623f7 105 dev = cndev();
0a7de745 106 return (*cdevsw[major(dev)].d_read)(dev, uio, ioflag);
1c79356b
A
107}
108
b0d623f7 109
1c79356b 110int
91447636 111cnwrite(__unused dev_t dev, struct uio *uio, int ioflag)
1c79356b 112{
b0d623f7 113 dev = cndev();
0a7de745 114 return (*cdevsw[major(dev)].d_write)(dev, uio, ioflag);
1c79356b
A
115}
116
b0d623f7 117
1c79356b 118int
b0d623f7 119cnioctl(__unused dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
1c79356b 120{
b0d623f7
A
121 dev = cndev();
122#if 0
1c79356b
A
123 /*
124 * Superuser can always use this to wrest control of console
125 * output from the "virtual" console.
b0d623f7
A
126 *
127 * XXX Unfortunately, this code doesn't do what the author thougt
128 * XXX it did; use of the console device, a TIOCCONS would always
129 * XXX disassociate the console from a virtual terminal and send
130 * XXX it back to the fake tty.
1c79356b 131 */
91447636 132 if ((unsigned) cmd == TIOCCONS && constty) {
b0d623f7
A
133 int error = proc_suser(p);
134 if (!error) {
135 constty = NULL;
91447636 136 }
0a7de745 137 return error;
1c79356b 138 }
0a7de745 139#endif /* 0 */
91447636 140
0a7de745 141 return (*cdevsw[major(dev)].d_ioctl)(dev, cmd, addr, flag, p);
1c79356b
A
142}
143
1c79356b 144
1c79356b 145int
b0d623f7 146cnselect(__unused dev_t dev, int flag, void *wql, struct proc *p)
1c79356b 147{
b0d623f7 148 dev = cndev();
0a7de745 149 return (*cdevsw[major(dev)].d_select)(dev, flag, wql, p);
1c79356b 150}