xref: /xnu-11417.140.69/osfmk/console/serial_general.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions /*
29*43a90889SApple OSS Distributions  * @OSF_COPYRIGHT@
30*43a90889SApple OSS Distributions  */
31*43a90889SApple OSS Distributions /*
32*43a90889SApple OSS Distributions  * @APPLE_FREE_COPYRIGHT@
33*43a90889SApple OSS Distributions  */
34*43a90889SApple OSS Distributions 
35*43a90889SApple OSS Distributions #include <kern/spl.h>
36*43a90889SApple OSS Distributions #include <mach/std_types.h>
37*43a90889SApple OSS Distributions #include <types.h>
38*43a90889SApple OSS Distributions #include <kern/thread.h>
39*43a90889SApple OSS Distributions #include <console/serial_protos.h>
40*43a90889SApple OSS Distributions #include <libkern/section_keywords.h>
41*43a90889SApple OSS Distributions 
42*43a90889SApple OSS Distributions extern void cons_cinput(char ch);               /* The BSD routine that gets characters */
43*43a90889SApple OSS Distributions 
44*43a90889SApple OSS Distributions SECURITY_READ_ONLY_LATE(unsigned int) serialmode;                               /* Serial mode keyboard and console control */
45*43a90889SApple OSS Distributions 
46*43a90889SApple OSS Distributions /*
47*43a90889SApple OSS Distributions  *  This routine will start a thread that polls the serial port, listening for
48*43a90889SApple OSS Distributions  *  characters that have been typed.
49*43a90889SApple OSS Distributions  */
50*43a90889SApple OSS Distributions 
51*43a90889SApple OSS Distributions void
serial_keyboard_init(void)52*43a90889SApple OSS Distributions serial_keyboard_init(void)
53*43a90889SApple OSS Distributions {
54*43a90889SApple OSS Distributions 	kern_return_t   result;
55*43a90889SApple OSS Distributions 	thread_t                thread;
56*43a90889SApple OSS Distributions 
57*43a90889SApple OSS Distributions 	if (!(serialmode & SERIALMODE_INPUT)) { /* Leave if we do not want a serial console */
58*43a90889SApple OSS Distributions 		return;
59*43a90889SApple OSS Distributions 	}
60*43a90889SApple OSS Distributions 
61*43a90889SApple OSS Distributions 	kprintf("Serial keyboard started\n");
62*43a90889SApple OSS Distributions 	result = kernel_thread_start_priority((thread_continue_t)serial_keyboard_start, NULL, MAXPRI_KERNEL, &thread);
63*43a90889SApple OSS Distributions 	if (result != KERN_SUCCESS) {
64*43a90889SApple OSS Distributions 		panic("serial_keyboard_init");
65*43a90889SApple OSS Distributions 	}
66*43a90889SApple OSS Distributions 
67*43a90889SApple OSS Distributions 	thread_deallocate(thread);
68*43a90889SApple OSS Distributions }
69*43a90889SApple OSS Distributions 
70*43a90889SApple OSS Distributions void
serial_keyboard_start(void)71*43a90889SApple OSS Distributions serial_keyboard_start(void)
72*43a90889SApple OSS Distributions {
73*43a90889SApple OSS Distributions 	/* Go see if there are any characters pending now */
74*43a90889SApple OSS Distributions 	serial_keyboard_poll();
75*43a90889SApple OSS Distributions }
76*43a90889SApple OSS Distributions 
77*43a90889SApple OSS Distributions __dead2
78*43a90889SApple OSS Distributions void
serial_keyboard_poll(void)79*43a90889SApple OSS Distributions serial_keyboard_poll(void)
80*43a90889SApple OSS Distributions {
81*43a90889SApple OSS Distributions 	int chr;
82*43a90889SApple OSS Distributions 	uint64_t next;
83*43a90889SApple OSS Distributions 
84*43a90889SApple OSS Distributions 	while (1) {
85*43a90889SApple OSS Distributions 		chr = _serial_getc(false); /* Get a character if there is one */
86*43a90889SApple OSS Distributions 		if (chr < 0) { /* The serial buffer is empty */
87*43a90889SApple OSS Distributions 			break;
88*43a90889SApple OSS Distributions 		}
89*43a90889SApple OSS Distributions 		cons_cinput((char)chr); /* Buffer up the character */
90*43a90889SApple OSS Distributions 	}
91*43a90889SApple OSS Distributions 
92*43a90889SApple OSS Distributions 	clock_interval_to_deadline(16, 1000000, &next); /* Get time of pop */
93*43a90889SApple OSS Distributions 
94*43a90889SApple OSS Distributions 	assert_wait_deadline((event_t)serial_keyboard_poll, THREAD_UNINT, next); /* Show we are "waiting" */
95*43a90889SApple OSS Distributions 	thread_block((thread_continue_t)serial_keyboard_poll); /* Wait for it */
96*43a90889SApple OSS Distributions 	panic("serial_keyboard_poll: Shouldn't never ever get here...");
97*43a90889SApple OSS Distributions }
98*43a90889SApple OSS Distributions 
99*43a90889SApple OSS Distributions boolean_t
console_is_serial(void)100*43a90889SApple OSS Distributions console_is_serial(void)
101*43a90889SApple OSS Distributions {
102*43a90889SApple OSS Distributions 	return cons_ops_index == SERIAL_CONS_OPS;
103*43a90889SApple OSS Distributions }
104*43a90889SApple OSS Distributions 
105*43a90889SApple OSS Distributions int
switch_to_video_console(void)106*43a90889SApple OSS Distributions switch_to_video_console(void)
107*43a90889SApple OSS Distributions {
108*43a90889SApple OSS Distributions 	int old_cons_ops = cons_ops_index;
109*43a90889SApple OSS Distributions 	cons_ops_index = VC_CONS_OPS;
110*43a90889SApple OSS Distributions 	return old_cons_ops;
111*43a90889SApple OSS Distributions }
112*43a90889SApple OSS Distributions 
113*43a90889SApple OSS Distributions int
switch_to_serial_console(void)114*43a90889SApple OSS Distributions switch_to_serial_console(void)
115*43a90889SApple OSS Distributions {
116*43a90889SApple OSS Distributions 	extern bool serial_console_enabled;
117*43a90889SApple OSS Distributions 	int old_cons_ops = cons_ops_index;
118*43a90889SApple OSS Distributions 
119*43a90889SApple OSS Distributions 	if (serial_console_enabled) {
120*43a90889SApple OSS Distributions 		cons_ops_index = SERIAL_CONS_OPS;
121*43a90889SApple OSS Distributions 	}
122*43a90889SApple OSS Distributions 
123*43a90889SApple OSS Distributions 	return old_cons_ops;
124*43a90889SApple OSS Distributions }
125*43a90889SApple OSS Distributions 
126*43a90889SApple OSS Distributions /* The switch_to_{video,serial,kgdb}_console functions return a cookie that
127*43a90889SApple OSS Distributions  *  can be used to restore the console to whatever it was before, in the
128*43a90889SApple OSS Distributions  *  same way that splwhatever() and splx() work.  */
129*43a90889SApple OSS Distributions void
switch_to_old_console(int old_console)130*43a90889SApple OSS Distributions switch_to_old_console(int old_console)
131*43a90889SApple OSS Distributions {
132*43a90889SApple OSS Distributions 	static boolean_t squawked;
133*43a90889SApple OSS Distributions 	uint32_t ops = old_console;
134*43a90889SApple OSS Distributions 
135*43a90889SApple OSS Distributions 	if ((ops >= nconsops) && !squawked) {
136*43a90889SApple OSS Distributions 		squawked = TRUE;
137*43a90889SApple OSS Distributions 		printf("switch_to_old_console: unknown ops %d\n", ops);
138*43a90889SApple OSS Distributions 	} else {
139*43a90889SApple OSS Distributions 		cons_ops_index = ops;
140*43a90889SApple OSS Distributions 	}
141*43a90889SApple OSS Distributions }
142*43a90889SApple OSS Distributions 
143*43a90889SApple OSS Distributions void
console_printbuf_state_init(struct console_printbuf_state * data,int write_on_newline,int can_block)144*43a90889SApple OSS Distributions console_printbuf_state_init(struct console_printbuf_state * data, int write_on_newline, int can_block)
145*43a90889SApple OSS Distributions {
146*43a90889SApple OSS Distributions 	if (data == NULL) {
147*43a90889SApple OSS Distributions 		return;
148*43a90889SApple OSS Distributions 	}
149*43a90889SApple OSS Distributions 	bzero(data, sizeof(struct console_printbuf_state));
150*43a90889SApple OSS Distributions 	if (write_on_newline) {
151*43a90889SApple OSS Distributions 		data->flags |= CONS_PB_WRITE_NEWLINE;
152*43a90889SApple OSS Distributions 	}
153*43a90889SApple OSS Distributions 	if (can_block) {
154*43a90889SApple OSS Distributions 		data->flags |= CONS_PB_CANBLOCK;
155*43a90889SApple OSS Distributions 	}
156*43a90889SApple OSS Distributions }
157*43a90889SApple OSS Distributions 
158*43a90889SApple OSS Distributions void
console_printbuf_putc(int ch,void * arg)159*43a90889SApple OSS Distributions console_printbuf_putc(int ch, void * arg)
160*43a90889SApple OSS Distributions {
161*43a90889SApple OSS Distributions 	struct console_printbuf_state * info = (struct console_printbuf_state *)arg;
162*43a90889SApple OSS Distributions 	info->total += 1;
163*43a90889SApple OSS Distributions 	if (info->pos < (SERIAL_CONS_BUF_SIZE - 1)) {
164*43a90889SApple OSS Distributions 		info->str[info->pos] = (char)ch;
165*43a90889SApple OSS Distributions 		info->pos += 1;
166*43a90889SApple OSS Distributions 	} else {
167*43a90889SApple OSS Distributions 		/*
168*43a90889SApple OSS Distributions 		 * when len(line) > SERIAL_CONS_BUF_SIZE, we truncate the message
169*43a90889SApple OSS Distributions 		 * if boot-arg 'drain_uart_sync=1' is set, then
170*43a90889SApple OSS Distributions 		 * drain all the buffer right now and append new ch
171*43a90889SApple OSS Distributions 		 */
172*43a90889SApple OSS Distributions 		if (serialmode & SERIALMODE_SYNCDRAIN) {
173*43a90889SApple OSS Distributions 			info->str[info->pos] = '\0';
174*43a90889SApple OSS Distributions 			console_write(info->str, info->pos);
175*43a90889SApple OSS Distributions 			info->pos            = 0;
176*43a90889SApple OSS Distributions 			info->str[info->pos] = (char)ch;
177*43a90889SApple OSS Distributions 			info->pos += 1;
178*43a90889SApple OSS Distributions 		}
179*43a90889SApple OSS Distributions 	}
180*43a90889SApple OSS Distributions 
181*43a90889SApple OSS Distributions 	info->str[info->pos] = '\0';
182*43a90889SApple OSS Distributions 	/* if newline, then try output to console */
183*43a90889SApple OSS Distributions 	if (ch == '\n' && info->flags & CONS_PB_WRITE_NEWLINE) {
184*43a90889SApple OSS Distributions 		console_write(info->str, info->pos);
185*43a90889SApple OSS Distributions 		info->pos            = 0;
186*43a90889SApple OSS Distributions 		info->str[info->pos] = '\0';
187*43a90889SApple OSS Distributions 	}
188*43a90889SApple OSS Distributions }
189*43a90889SApple OSS Distributions 
190*43a90889SApple OSS Distributions void
console_printbuf_clear(struct console_printbuf_state * info)191*43a90889SApple OSS Distributions console_printbuf_clear(struct console_printbuf_state * info)
192*43a90889SApple OSS Distributions {
193*43a90889SApple OSS Distributions 	if (info->pos != 0) {
194*43a90889SApple OSS Distributions 		console_write(info->str, info->pos);
195*43a90889SApple OSS Distributions 	}
196*43a90889SApple OSS Distributions 	info->pos = 0;
197*43a90889SApple OSS Distributions 	info->str[info->pos] = '\0';
198*43a90889SApple OSS Distributions 	info->total = 0;
199*43a90889SApple OSS Distributions }
200