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