]> git.saurik.com Git - apple/xnu.git/blame - bsd/dev/ppc/km.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / bsd / dev / ppc / km.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 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.
8f6c56a5 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.
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
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
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
91447636 35#include <sys/kernel.h>
1c79356b
A
36#include <sys/tty.h>
37
b0d623f7 38#include <machine/cons.h>
1c79356b
A
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 */
b0d623f7 53struct tty *km_tty[1] = { 0 };
1c79356b
A
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
91447636
A
61static int initialized = 0;
62
91447636
A
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
1c79356b
A
69
70static int kmoutput(struct tty *tp);
b0d623f7 71static void kmtimeout(void *tp);
1c79356b
A
72static void kmstart(struct tty *tp);
73
74extern void KeyboardOpen(void);
75
91447636
A
76void
77kminit(void)
1c79356b 78{
b0d623f7
A
79 km_tty[0] = ttymalloc();
80 km_tty[0]->t_dev = makedev(12, 0);
1c79356b
A
81 initialized = 1;
82}
b0d623f7 83
1c79356b
A
84/*
85 * cdevsw interface to km driver.
86 */
87int
b0d623f7 88kmopen(dev_t dev, int flag, __unused int devtype, proc_t pp)
1c79356b 89{
1c79356b
A
90 int unit;
91 struct tty *tp;
92 struct winsize *wp;
93 int ret;
94
95 unit = minor(dev);
96 if(unit >= 1)
97 return (ENXIO);
98
b0d623f7
A
99 tp = km_tty[unit];
100
101 tty_lock(tp);
102
1c79356b
A
103 tp->t_oproc = kmstart;
104 tp->t_param = NULL;
105 tp->t_dev = dev;
106
107 if ( !(tp->t_state & TS_ISOPEN) ) {
108 tp->t_iflag = TTYDEF_IFLAG;
109 tp->t_oflag = TTYDEF_OFLAG;
110 tp->t_cflag = (CREAD | CS8 | CLOCAL);
111 tp->t_lflag = TTYDEF_LFLAG;
112 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
113 termioschars(&tp->t_termios);
114 ttsetwater(tp);
b0d623f7
A
115 } else if ((tp->t_state & TS_XCLUDE) && proc_suser(pp)) {
116 ret = EBUSY;
117 goto out;
118 }
1c79356b
A
119
120 tp->t_state |= TS_CARR_ON; /* lie and say carrier exists and is on. */
b0d623f7 121
1c79356b
A
122 ret = ((*linesw[tp->t_line].l_open)(dev, tp));
123 {
124 PE_Video video;
125 wp = &tp->t_winsize;
b0d623f7
A
126 /*
127 * Magic numbers. These are CHARWIDTH and CHARHEIGHT
1c79356b
A
128 * from osfmk/ppc/POWERMAC/video_console.c
129 */
130 wp->ws_xpixel = 8;
131 wp->ws_ypixel = 16;
132
b0d623f7
A
133 tty_unlock(tp); /* XXX race window */
134
1c79356b
A
135 if (flag & O_POPUP)
136 PE_initialize_console(0, kPETextScreen);
137
138 bzero(&video, sizeof(video));
139 PE_current_console(&video);
b0d623f7
A
140
141 tty_lock(tp);
142
1c79356b
A
143 if( video.v_width != 0 && video.v_height != 0 ) {
144 wp->ws_col = video.v_width / wp->ws_xpixel;
145 wp->ws_row = video.v_height / wp->ws_ypixel;
146 } else {
147 wp->ws_col = 100;
148 wp->ws_row = 36;
149 }
150 }
b0d623f7
A
151
152out:
153 tty_unlock(tp);
154
1c79356b
A
155 return ret;
156}
157
158int
b0d623f7 159kmclose(dev_t dev, __unused int flag, __unused int mode, __unused proc_t p)
1c79356b 160{
b0d623f7
A
161 int ret;
162 struct tty *tp = km_tty[minor(dev)];
1c79356b 163
b0d623f7
A
164 tty_lock(tp);
165 ret = (*linesw[tp->t_line].l_close)(tp,flag);
1c79356b 166 ttyclose(tp);
b0d623f7
A
167 tty_unlock(tp);
168
169 return (ret);
1c79356b
A
170}
171
172int
b0d623f7 173kmread(dev_t dev, struct uio *uio, int ioflag)
1c79356b 174{
b0d623f7
A
175 int ret;
176 struct tty *tp = km_tty[minor(dev)];
177
178 tty_lock(tp);
179 ret = (*linesw[tp->t_line].l_read)(tp, uio, ioflag);
180 tty_unlock(tp);
181
182 return (ret);
1c79356b
A
183}
184
185int
b0d623f7 186kmwrite(dev_t dev, struct uio *uio, int ioflag)
1c79356b 187{
b0d623f7
A
188 int ret;
189 struct tty *tp = km_tty[minor(dev)];
190
191 tty_lock(tp);
192 ret = (*linesw[tp->t_line].l_write)(tp, uio, ioflag);
193 tty_unlock(tp);
194
195 return (ret);
1c79356b
A
196}
197
198int
b0d623f7 199kmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, proc_t p)
1c79356b 200{
b0d623f7
A
201 int error = 0;
202 struct tty *tp = km_tty[minor(dev)];
1c79356b 203 struct winsize *wp;
b0d623f7
A
204
205 tty_lock(tp);
1c79356b
A
206
207 switch (cmd) {
1c79356b
A
208 case KMIOCSIZE:
209 wp = (struct winsize *)data;
210 *wp = tp->t_winsize;
b0d623f7 211 break;
1c79356b
A
212
213 case TIOCSWINSZ:
214 /* Prevent changing of console size --
215 * this ensures that login doesn't revert to the
216 * termcap-defined size
217 */
b0d623f7
A
218 error = EINVAL;
219 break;
1c79356b
A
220
221 /* Bodge in the CLOCAL flag as the km device is always local */
b0d623f7
A
222 case TIOCSETA_32:
223 case TIOCSETAW_32:
224 case TIOCSETAF_32:
225 {
226 struct termios32 *t = (struct termios32 *)data;
227 t->c_cflag |= CLOCAL;
228 /* No Break */
229 }
230 goto fallthrough;
231 case TIOCSETA_64:
232 case TIOCSETAW_64:
233 case TIOCSETAF_64:
234 {
235 struct user_termios *t = (struct user_termios *)data;
236 t->c_cflag |= CLOCAL;
237 /* No Break */
238 }
239fallthrough:
1c79356b
A
240 default:
241 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
91447636 242 if (ENOTTY != error)
b0d623f7
A
243 break;
244 error = ttioctl_locked(tp, cmd, data, flag, p);
245 break;
1c79356b 246 }
1c79356b 247
b0d623f7 248 tty_unlock(tp);
1c79356b 249
b0d623f7 250 return (error);
1c79356b
A
251}
252
b0d623f7
A
253/*
254 * kmputc
255 *
256 * Output a character to the serial console driver via cnputcusr(),
257 * which is exported by that driver.
258 *
259 * Locks: Assumes tp in the calling tty driver code is locked on
260 * entry, remains locked on exit
261 *
262 * Notes: Called from kmoutput(); giving the locking output
263 * assumptions here, this routine should be static (and
264 * inlined, given there is only one call site).
265 */
1c79356b 266int
b0d623f7 267kmputc(__unused dev_t dev, char c)
1c79356b 268{
b0d623f7
A
269 if(!disableConsoleOutput && initialized) {
270 /* OCRNL */
271 if(c == '\n')
272 cnputcusr('\r');
273 cnputcusr(c);
1c79356b 274 }
1c79356b 275
b0d623f7 276 return (0);
1c79356b 277}
b0d623f7 278
1c79356b
A
279
280/*
281 * Callouts from linesw.
282 */
283
284#define KM_LOWAT_DELAY ((ns_time_t)1000)
285
b0d623f7
A
286/*
287 * t_oproc for this driver; called from within the line discipline
288 *
289 * Locks: Assumes tp is locked on entry, remains locked on exit
290 */
1c79356b 291static void
91447636 292kmstart(struct tty *tp)
1c79356b 293{
1c79356b
A
294 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
295 goto out;
296 if (tp->t_outq.c_cc == 0)
297 goto out;
298 tp->t_state |= TS_BUSY;
91447636
A
299 kmoutput(tp);
300 return;
301
1c79356b 302out:
91447636
A
303 (*linesw[tp->t_line].l_start)(tp);
304 return;
1c79356b
A
305}
306
b0d623f7
A
307/*
308 * One-shot output retry timeout from kmoutput(); re-calls kmoutput() at
309 * intervals until the output queue for the tty is empty, at which point
310 * the timeout is not rescheduled by kmoutput()
311 *
312 * This function must take the tty_lock() around the kmoutput() call; it
313 * ignores the return value.
314 */
1c79356b 315static void
b0d623f7 316kmtimeout(void *arg)
1c79356b 317{
b0d623f7 318 struct tty *tp = (struct tty *)arg;
1c79356b 319
b0d623f7
A
320 tty_lock(tp);
321 (void)kmoutput(tp);
322 tty_unlock(tp);
1c79356b 323}
b0d623f7
A
324
325/*
326 * kmoutput
327 *
328 * Locks: Assumes tp is locked on entry, remains locked on exit
329 *
330 * Notes: Called from kmstart() and kmtimeout(); kmtimeout() is a
331 * timer initiated by this routine to deal with pending
332 * output not yet flushed (output is flushed at a maximum
333 * of sizeof(buf) charatcers at a time before dropping into
334 * the timeout code).
335 */
1c79356b 336static int
91447636 337kmoutput(struct tty *tp)
1c79356b 338{
b0d623f7
A
339 char buf[80]; /* buffer; limits output per call */
340 char *cp;
341 int cc = -1;
342
1c79356b 343
b0d623f7 344 /* While there is data available to be output... */
1c79356b
A
345 while (tp->t_outq.c_cc > 0) {
346 cc = ndqb(&tp->t_outq, 0);
347 if (cc == 0)
348 break;
b0d623f7
A
349 /*
350 * attempt to output as many characters as are available,
351 * up to the available transfer buffer size.
352 */
1c79356b 353 cc = min(cc, sizeof buf);
b0d623f7 354 /* copy the output queue contents to the buffer */
2d21ac55 355 (void) q_to_b(&tp->t_outq, (unsigned char *)buf, cc);
b0d623f7
A
356 for (cp = buf; cp < &buf[cc]; cp++) {
357 /* output the buffer one charatcer at a time */
358 kmputc(tp->t_dev, *cp & 0x7f);
359 }
1c79356b
A
360 }
361 if (tp->t_outq.c_cc > 0) {
55e303ae 362 timeout((timeout_fcn_t)kmtimeout, tp, hz);
1c79356b
A
363 }
364 tp->t_state &= ~TS_BUSY;
91447636 365 (*linesw[tp->t_line].l_start)(tp);
1c79356b
A
366
367 return 0;
368}
91447636 369
b0d623f7
A
370/*
371 * cons_cinput
372 *
373 * Driver character input from the polled mode serial console driver calls
374 * this routine to input a character from the serial driver into the tty
375 * line discipline specific input processing receiv interrupt routine,
376 * l_rint().
377 *
378 * Locks: Assumes that the tty_lock() is NOT held on the tp, so a
379 * serial driver should NOT call this function as a result
380 * of being called from a function which already holds the
381 * lock; ECHOE will be handled at the line discipline, if
382 * output echo processing is going to occur.
383 */
384void
385cons_cinput(char ch)
1c79356b 386{
b0d623f7
A
387 struct tty *tp = km_tty[0]; /* XXX */
388
389 tty_lock(tp);
1c79356b 390 (*linesw[tp->t_line].l_rint) (ch, tp);
b0d623f7 391 tty_unlock(tp);
1c79356b 392}