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