]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/dev/ppc/km.c
xnu-1228.tar.gz
[apple/xnu.git] / bsd / dev / ppc / km.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/* Copyright (c) 1992 NeXT Computer, Inc. All rights reserved.
29 *
30 * km.m - kernel keyboard/monitor module, procedural interface.
31 *
32 * HISTORY
33 */
34
35#include <sys/kernel.h>
36#include <sys/tty.h>
37
38#include <dev/ppc/cons.h>
39#include <sys/conf.h>
40#include <sys/systm.h>
41#include <sys/uio.h>
42#include <sys/fcntl.h> /* for kmopen */
43#include <sys/errno.h>
44#include <sys/proc.h> /* for kmopen */
45#include <sys/msgbuf.h>
46#include <sys/time.h>
47#include <dev/kmreg_com.h>
48#include <pexpert/pexpert.h>
49
50/*
51 * 'Global' variables, shared only by this file and conf.c.
52 */
53struct tty *km_tty[1] = { &cons };
54
55/*
56 * this works early on, after initialize_screen() but before autoconf (and thus
57 * before we have a kmDevice).
58 */
59int disableConsoleOutput;
60
61static int initialized = 0;
62
63extern void kminit(void);
64
65// used by or implemented in the osfmk project
66extern void cnputcusr(char); // From osfmk
67extern int cngetc(void); // From osfmk
68extern void cons_cinput(char ch); // Used by osfmk
69
70static int kmoutput(struct tty *tp);
71static void kmtimeout(struct tty *tp);
72static void kmstart(struct tty *tp);
73
74extern void KeyboardOpen(void);
75
76void
77kminit(void)
78{
79 cons.t_dev = makedev(12, 0);
80 initialized = 1;
81}
82/*
83 * cdevsw interface to km driver.
84 */
85int
86kmopen(dev_t dev, int flag, __unused int devtype, struct proc *pp)
87{
88 int unit;
89 struct tty *tp;
90 struct winsize *wp;
91 int ret;
92
93 unit = minor(dev);
94 if(unit >= 1)
95 return (ENXIO);
96
97 tp = (struct tty *)&cons;
98 tp->t_oproc = kmstart;
99 tp->t_param = NULL;
100 tp->t_dev = dev;
101
102 if ( !(tp->t_state & TS_ISOPEN) ) {
103 tp->t_iflag = TTYDEF_IFLAG;
104 tp->t_oflag = TTYDEF_OFLAG;
105 tp->t_cflag = (CREAD | CS8 | CLOCAL);
106 tp->t_lflag = TTYDEF_LFLAG;
107 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
108 termioschars(&tp->t_termios);
109 ttsetwater(tp);
110 } else if ((tp->t_state & TS_XCLUDE) && proc_suser(pp))
111 return EBUSY;
112
113 tp->t_state |= TS_CARR_ON; /* lie and say carrier exists and is on. */
114 ret = ((*linesw[tp->t_line].l_open)(dev, tp));
115 {
116 PE_Video video;
117 wp = &tp->t_winsize;
118 /* Magic numbers. These are CHARWIDTH and CHARHEIGHT
119 * from osfmk/ppc/POWERMAC/video_console.c
120 */
121 wp->ws_xpixel = 8;
122 wp->ws_ypixel = 16;
123
124 if (flag & O_POPUP)
125 PE_initialize_console(0, kPETextScreen);
126
127 bzero(&video, sizeof(video));
128 PE_current_console(&video);
129 if( video.v_width != 0 && video.v_height != 0 ) {
130 wp->ws_col = video.v_width / wp->ws_xpixel;
131 wp->ws_row = video.v_height / wp->ws_ypixel;
132 } else {
133 wp->ws_col = 100;
134 wp->ws_row = 36;
135 }
136 }
137 return ret;
138}
139
140int
141kmclose(__unused dev_t dev, __unused int flag, __unused int mode,
142 __unused struct proc *p)
143{
144
145 struct tty *tp;
146
147 tp = &cons;
148 (*linesw[tp->t_line].l_close)(tp,flag);
149 ttyclose(tp);
150 return (0);
151}
152
153int
154kmread(__unused dev_t dev, struct uio *uio, int ioflag)
155{
156 register struct tty *tp;
157
158 tp = &cons;
159 return ((*linesw[tp->t_line].l_read)(tp, uio, ioflag));
160}
161
162int
163kmwrite(__unused dev_t dev, struct uio *uio, int ioflag)
164{
165 register struct tty *tp;
166
167 tp = &cons;
168 return ((*linesw[tp->t_line].l_write)(tp, uio, ioflag));
169}
170
171int
172kmioctl( __unused dev_t dev, u_long cmd, caddr_t data, int flag,
173 struct proc *p)
174{
175 int error;
176 struct tty *tp = &cons;
177 struct winsize *wp;
178
179 switch (cmd) {
180
181
182
183 case KMIOCSIZE:
184 wp = (struct winsize *)data;
185 *wp = tp->t_winsize;
186 return 0;
187
188 case TIOCSWINSZ:
189 /* Prevent changing of console size --
190 * this ensures that login doesn't revert to the
191 * termcap-defined size
192 */
193 return EINVAL;
194
195 /* Bodge in the CLOCAL flag as the km device is always local */
196 case TIOCSETA:
197 case TIOCSETAW:
198 case TIOCSETAF: {
199 register struct termios *t = (struct termios *)data;
200 t->c_cflag |= CLOCAL;
201 /* No Break */
202 }
203 default:
204 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
205 if (ENOTTY != error)
206 return error;
207 return ttioctl (tp, cmd, data, flag, p);
208 }
209}
210
211int
212kmputc(__unused dev_t dev, char c)
213{
214
215 if( disableConsoleOutput)
216 return( 0);
217
218 if(!initialized)
219 return( 0);
220
221 if(c == '\n')
222 cnputcusr('\r');
223
224 cnputcusr(c);
225
226 return 0;
227}
228
229int
230kmgetc(__unused dev_t dev)
231{
232 int c;
233
234 c= cngetc();
235
236 if (c == '\r') {
237 c = '\n';
238 }
239 cnputcusr(c);
240 return c;
241}
242
243#if 0
244int
245kmgetc_silent(
246 __unused dev_t dev)
247{
248 int c;
249
250 c= cngetc();
251 if (c == '\r') {
252 c = '\n';
253 }
254 return c;
255}
256#endif /* 0 */
257
258/*
259 * Callouts from linesw.
260 */
261
262#define KM_LOWAT_DELAY ((ns_time_t)1000)
263
264static void
265kmstart(struct tty *tp)
266{
267 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
268 goto out;
269 if (tp->t_outq.c_cc == 0)
270 goto out;
271 tp->t_state |= TS_BUSY;
272 kmoutput(tp);
273 return;
274
275out:
276 (*linesw[tp->t_line].l_start)(tp);
277 return;
278}
279
280static void
281kmtimeout(struct tty *tp)
282{
283 boolean_t funnel_state;
284
285 funnel_state = thread_funnel_set(kernel_flock, TRUE);
286 kmoutput(tp);
287 (void) thread_funnel_set(kernel_flock, funnel_state);
288
289
290}
291static int
292kmoutput(struct tty *tp)
293{
294 /*
295 * FIXME - to be grokked...copied from m68k km.c.
296 */
297 char buf[80];
298 char *cp;
299 int cc = -1;
300
301 while (tp->t_outq.c_cc > 0) {
302 cc = ndqb(&tp->t_outq, 0);
303 if (cc == 0)
304 break;
305 cc = min(cc, sizeof buf);
306 (void) q_to_b(&tp->t_outq, (unsigned char *)buf, cc);
307 for (cp = buf; cp < &buf[cc]; cp++)
308 kmputc(tp->t_dev, *cp & 0x7f);
309 }
310 if (tp->t_outq.c_cc > 0) {
311 timeout((timeout_fcn_t)kmtimeout, tp, hz);
312 }
313 tp->t_state &= ~TS_BUSY;
314 (*linesw[tp->t_line].l_start)(tp);
315
316 return 0;
317}
318
319void cons_cinput(char ch)
320{
321 struct tty *tp = &cons;
322
323 (*linesw[tp->t_line].l_rint) (ch, tp);
324}
325