]>
Commit | Line | Data |
---|---|---|
0c530ab8 | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved. |
0c530ab8 | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 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. | |
0a7de745 | 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. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
0c530ab8 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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
0c530ab8 A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
31 | /* | |
32 | * @APPLE_FREE_COPYRIGHT@ | |
33 | */ | |
34 | ||
0c530ab8 A |
35 | #include <kern/spl.h> |
36 | #include <mach/std_types.h> | |
37 | #include <types.h> | |
38 | #include <kern/thread.h> | |
39 | #include <console/serial_protos.h> | |
813fb2f6 | 40 | #include <libkern/section_keywords.h> |
0c530ab8 | 41 | |
0a7de745 | 42 | extern void cons_cinput(char ch); /* The BSD routine that gets characters */ |
0c530ab8 | 43 | |
0a7de745 | 44 | SECURITY_READ_ONLY_LATE(unsigned int) serialmode; /* Serial mode keyboard and console control */ |
0c530ab8 A |
45 | |
46 | /* | |
47 | * This routine will start a thread that polls the serial port, listening for | |
48 | * characters that have been typed. | |
49 | */ | |
50 | ||
51 | void | |
52 | serial_keyboard_init(void) | |
53 | { | |
0a7de745 A |
54 | kern_return_t result; |
55 | thread_t thread; | |
0c530ab8 | 56 | |
0a7de745 | 57 | if (!(serialmode & SERIALMODE_INPUT)) { /* Leave if we do not want a serial console */ |
2d21ac55 | 58 | return; |
0a7de745 | 59 | } |
0c530ab8 A |
60 | |
61 | kprintf("Serial keyboard started\n"); | |
62 | result = kernel_thread_start_priority((thread_continue_t)serial_keyboard_start, NULL, MAXPRI_KERNEL, &thread); | |
0a7de745 | 63 | if (result != KERN_SUCCESS) { |
0c530ab8 | 64 | panic("serial_keyboard_init"); |
0a7de745 | 65 | } |
0c530ab8 A |
66 | |
67 | thread_deallocate(thread); | |
68 | } | |
69 | ||
70 | void | |
71 | serial_keyboard_start(void) | |
72 | { | |
2d21ac55 A |
73 | /* Go see if there are any characters pending now */ |
74 | serial_keyboard_poll(); | |
0c530ab8 A |
75 | } |
76 | ||
cb323159 | 77 | __dead2 |
0c530ab8 A |
78 | void |
79 | serial_keyboard_poll(void) | |
80 | { | |
81 | int chr; | |
82 | uint64_t next; | |
83 | ||
0a7de745 A |
84 | while (1) { |
85 | chr = _serial_getc(0, 1, 0, 1); /* Get a character if there is one */ | |
86 | if (chr < 0) { /* The serial buffer is empty */ | |
2d21ac55 | 87 | break; |
0a7de745 A |
88 | } |
89 | cons_cinput((char)chr); /* Buffer up the character */ | |
0c530ab8 A |
90 | } |
91 | ||
0a7de745 | 92 | clock_interval_to_deadline(16, 1000000, &next); /* Get time of pop */ |
0c530ab8 | 93 | |
0a7de745 A |
94 | assert_wait_deadline((event_t)serial_keyboard_poll, THREAD_UNINT, next); /* Show we are "waiting" */ |
95 | thread_block((thread_continue_t)serial_keyboard_poll); /* Wait for it */ | |
0c530ab8 A |
96 | panic("serial_keyboard_poll: Shouldn't never ever get here...\n"); |
97 | } | |
98 | ||
2d21ac55 A |
99 | boolean_t |
100 | console_is_serial(void) | |
0c530ab8 | 101 | { |
0a7de745 | 102 | return cons_ops_index == SERIAL_CONS_OPS; |
0c530ab8 A |
103 | } |
104 | ||
105 | int | |
2d21ac55 | 106 | switch_to_video_console(void) |
0c530ab8 A |
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 | |
2d21ac55 | 114 | switch_to_serial_console(void) |
0c530ab8 | 115 | { |
c3c9b80d | 116 | extern bool serial_console_enabled; |
0c530ab8 | 117 | int old_cons_ops = cons_ops_index; |
c3c9b80d A |
118 | |
119 | if (serial_console_enabled) { | |
120 | cons_ops_index = SERIAL_CONS_OPS; | |
121 | } | |
122 | ||
0c530ab8 A |
123 | return old_cons_ops; |
124 | } | |
125 | ||
126 | /* The switch_to_{video,serial,kgdb}_console functions return a cookie that | |
0a7de745 A |
127 | * can be used to restore the console to whatever it was before, in the |
128 | * same way that splwhatever() and splx() work. */ | |
0c530ab8 A |
129 | void |
130 | switch_to_old_console(int old_console) | |
131 | { | |
132 | static boolean_t squawked; | |
133 | uint32_t ops = old_console; | |
134 | ||
135 | if ((ops >= nconsops) && !squawked) { | |
136 | squawked = TRUE; | |
137 | printf("switch_to_old_console: unknown ops %d\n", ops); | |
0a7de745 | 138 | } else { |
0c530ab8 | 139 | cons_ops_index = ops; |
0a7de745 | 140 | } |
0c530ab8 | 141 | } |
5ba3f43e A |
142 | |
143 | void | |
144 | console_printbuf_state_init(struct console_printbuf_state * data, int write_on_newline, int can_block) | |
145 | { | |
0a7de745 | 146 | if (data == NULL) { |
5ba3f43e | 147 | return; |
0a7de745 | 148 | } |
5ba3f43e | 149 | bzero(data, sizeof(struct console_printbuf_state)); |
0a7de745 | 150 | if (write_on_newline) { |
5ba3f43e | 151 | data->flags |= CONS_PB_WRITE_NEWLINE; |
0a7de745 A |
152 | } |
153 | if (can_block) { | |
5ba3f43e | 154 | data->flags |= CONS_PB_CANBLOCK; |
0a7de745 | 155 | } |
5ba3f43e A |
156 | } |
157 | ||
158 | void | |
159 | console_printbuf_putc(int ch, void * arg) | |
160 | { | |
161 | struct console_printbuf_state * info = (struct console_printbuf_state *)arg; | |
162 | info->total += 1; | |
163 | if (info->pos < (SERIAL_CONS_BUF_SIZE - 1)) { | |
f427ee49 | 164 | info->str[info->pos] = (char)ch; |
5ba3f43e A |
165 | info->pos += 1; |
166 | } else { | |
167 | /* | |
168 | * when len(line) > SERIAL_CONS_BUF_SIZE, we truncate the message | |
169 | * if boot-arg 'drain_uart_sync=1' is set, then | |
170 | * drain all the buffer right now and append new ch | |
171 | */ | |
172 | if (serialmode & SERIALMODE_SYNCDRAIN) { | |
173 | info->str[info->pos] = '\0'; | |
174 | console_write(info->str, info->pos); | |
175 | info->pos = 0; | |
f427ee49 | 176 | info->str[info->pos] = (char)ch; |
5ba3f43e A |
177 | info->pos += 1; |
178 | } | |
179 | } | |
180 | ||
181 | info->str[info->pos] = '\0'; | |
182 | /* if newline, then try output to console */ | |
183 | if (ch == '\n' && info->flags & CONS_PB_WRITE_NEWLINE) { | |
184 | console_write(info->str, info->pos); | |
185 | info->pos = 0; | |
186 | info->str[info->pos] = '\0'; | |
187 | } | |
188 | } | |
189 | ||
190 | void | |
0a7de745 A |
191 | console_printbuf_clear(struct console_printbuf_state * info) |
192 | { | |
5ba3f43e A |
193 | if (info->pos != 0) { |
194 | console_write(info->str, info->pos); | |
195 | } | |
196 | info->pos = 0; | |
197 | info->str[info->pos] = '\0'; | |
198 | info->total = 0; | |
199 | } |