]> git.saurik.com Git - apple/xnu.git/blob - osfmk/console/serial_general.c
xnu-792.13.8.tar.gz
[apple/xnu.git] / osfmk / console / serial_general.c
1 /*
2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * @APPLE_FREE_COPYRIGHT@
35 */
36
37 #include <mach_kdb.h>
38 #include <platforms.h>
39 #include <kern/spl.h>
40 #include <mach/std_types.h>
41 #include <types.h>
42 #include <kern/thread.h>
43 #include <console/serial_protos.h>
44
45 extern unsigned int disableSerialOuput;
46 extern void cons_cinput(char ch); /* The BSD routine that gets characters */
47
48 unsigned int serialmode; /* Serial mode keyboard and console control */
49
50 /*
51 * This routine will start a thread that polls the serial port, listening for
52 * characters that have been typed.
53 */
54
55 void
56 serial_keyboard_init(void)
57 {
58 kern_return_t result;
59 thread_t thread;
60
61 if(!(serialmode & 2)) return; /* Leave if we do not want a serial console */
62
63 kprintf("Serial keyboard started\n");
64 result = kernel_thread_start_priority((thread_continue_t)serial_keyboard_start, NULL, MAXPRI_KERNEL, &thread);
65 if (result != KERN_SUCCESS)
66 panic("serial_keyboard_init");
67
68 thread_deallocate(thread);
69 }
70
71 void
72 serial_keyboard_start(void)
73 {
74
75 serial_keyboard_poll(); /* Go see if there are any characters pending now */
76 panic("serial_keyboard_start: we can't get back here\n");
77
78 }
79
80 void
81 serial_keyboard_poll(void)
82 {
83 int chr;
84 uint64_t next;
85
86
87 while(1) { /* Do this for a while */
88 chr = _serial_getc(0, 1, 0, 1); /* Get a character if there is one */
89 if(chr < 0) break; /* The serial buffer is empty */
90 cons_cinput((char)chr); /* Buffer up the character */
91 }
92
93 clock_interval_to_deadline(16, 1000000, &next); /* Get time of pop */
94
95 assert_wait_deadline((event_t)serial_keyboard_poll, THREAD_UNINT, next); /* Show we are "waiting" */
96 thread_block((thread_continue_t)serial_keyboard_poll); /* Wait for it */
97 panic("serial_keyboard_poll: Shouldn't never ever get here...\n");
98 }
99
100 boolean_t console_is_serial()
101 {
102 return cons_ops_index == SERIAL_CONS_OPS;
103 }
104
105 int
106 switch_to_video_console()
107 {
108 int old_cons_ops = cons_ops_index;
109 cons_ops_index = VC_CONS_OPS;
110 return old_cons_ops;
111 }
112
113 int
114 switch_to_serial_console()
115 {
116 int old_cons_ops = cons_ops_index;
117 cons_ops_index = SERIAL_CONS_OPS;
118 return old_cons_ops;
119 }
120
121 /* The switch_to_{video,serial,kgdb}_console functions return a cookie that
122 can be used to restore the console to whatever it was before, in the
123 same way that splwhatever() and splx() work. */
124 void
125 switch_to_old_console(int old_console)
126 {
127 static boolean_t squawked;
128 uint32_t ops = old_console;
129
130 if ((ops >= nconsops) && !squawked) {
131 squawked = TRUE;
132 printf("switch_to_old_console: unknown ops %d\n", ops);
133 } else
134 cons_ops_index = ops;
135 }