]> git.saurik.com Git - apple/xnu.git/blob - osfmk/console/serial_general.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / console / serial_general.c
1 /*
2 * Copyright (c) 2005 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 /*
29 * @OSF_COPYRIGHT@
30 */
31 /*
32 * @APPLE_FREE_COPYRIGHT@
33 */
34
35 #include <mach_kdb.h>
36 #include <platforms.h>
37 #include <kern/spl.h>
38 #include <mach/std_types.h>
39 #include <types.h>
40 #include <kern/thread.h>
41 #include <console/serial_protos.h>
42
43 extern unsigned int disableSerialOuput;
44 extern void cons_cinput(char ch); /* The BSD routine that gets characters */
45
46 unsigned int serialmode; /* Serial mode keyboard and console control */
47
48 /*
49 * This routine will start a thread that polls the serial port, listening for
50 * characters that have been typed.
51 */
52
53 void
54 serial_keyboard_init(void)
55 {
56 kern_return_t result;
57 thread_t thread;
58
59 if(!(serialmode & 2)) return; /* Leave if we do not want a serial console */
60
61 kprintf("Serial keyboard started\n");
62 result = kernel_thread_start_priority((thread_continue_t)serial_keyboard_start, NULL, MAXPRI_KERNEL, &thread);
63 if (result != KERN_SUCCESS)
64 panic("serial_keyboard_init");
65
66 thread_deallocate(thread);
67 }
68
69 void
70 serial_keyboard_start(void)
71 {
72
73 serial_keyboard_poll(); /* Go see if there are any characters pending now */
74 panic("serial_keyboard_start: we can't get back here\n");
75
76 }
77
78 void
79 serial_keyboard_poll(void)
80 {
81 int chr;
82 uint64_t next;
83
84
85 while(1) { /* Do this for a while */
86 chr = _serial_getc(0, 1, 0, 1); /* Get a character if there is one */
87 if(chr < 0) break; /* The serial buffer is empty */
88 cons_cinput((char)chr); /* Buffer up the character */
89 }
90
91 clock_interval_to_deadline(16, 1000000, &next); /* Get time of pop */
92
93 assert_wait_deadline((event_t)serial_keyboard_poll, THREAD_UNINT, next); /* Show we are "waiting" */
94 thread_block((thread_continue_t)serial_keyboard_poll); /* Wait for it */
95 panic("serial_keyboard_poll: Shouldn't never ever get here...\n");
96 }
97
98 boolean_t console_is_serial()
99 {
100 return cons_ops_index == SERIAL_CONS_OPS;
101 }
102
103 int
104 switch_to_video_console()
105 {
106 int old_cons_ops = cons_ops_index;
107 cons_ops_index = VC_CONS_OPS;
108 return old_cons_ops;
109 }
110
111 int
112 switch_to_serial_console()
113 {
114 int old_cons_ops = cons_ops_index;
115 cons_ops_index = SERIAL_CONS_OPS;
116 return old_cons_ops;
117 }
118
119 /* The switch_to_{video,serial,kgdb}_console functions return a cookie that
120 can be used to restore the console to whatever it was before, in the
121 same way that splwhatever() and splx() work. */
122 void
123 switch_to_old_console(int old_console)
124 {
125 static boolean_t squawked;
126 uint32_t ops = old_console;
127
128 if ((ops >= nconsops) && !squawked) {
129 squawked = TRUE;
130 printf("switch_to_old_console: unknown ops %d\n", ops);
131 } else
132 cons_ops_index = ops;
133 }