2 * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
3 * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/console.h>
23 #include <linux/major.h>
24 #include <linux/kernel.h>
25 #include <linux/sysrq.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/sched.h>
29 #include <linux/kbd_kern.h>
30 #include <asm/uaccess.h>
31 #include <linux/spinlock.h>
33 extern int hvc_count(int *);
34 extern int hvc_get_chars(int index, char *buf, int count);
35 extern int hvc_put_chars(int index, const char *buf, int count);
40 #define MAX_NR_HVC_CONSOLES 4
42 #define TIMEOUT ((HZ + 99) / 100)
44 struct tty_driver hvc_driver;
45 static int hvc_refcount;
46 static struct tty_struct *hvc_table[MAX_NR_HVC_CONSOLES];
47 static struct termios *hvc_termios[MAX_NR_HVC_CONSOLES];
48 static struct termios *hvc_termios_locked[MAX_NR_HVC_CONSOLES];
49 static int hvc_offset;
50 #ifdef CONFIG_MAGIC_SYSRQ
51 static int sysrq_pressed;
56 #define __ALIGNED__ __attribute__((__aligned__(8)))
61 struct tty_struct *tty;
64 char outbuf[N_OUTBUF] __ALIGNED__;
68 struct hvc_struct hvc_struct[MAX_NR_HVC_CONSOLES];
70 static int hvc_open(struct tty_struct *tty, struct file * filp)
72 int line = tty->index;
73 struct hvc_struct *hp;
76 if (line < 0 || line >= MAX_NR_HVC_CONSOLES)
78 hp = &hvc_struct[line];
80 tty->driver_data = hp;
81 spin_lock_irqsave(&hp->lock, flags);
84 spin_unlock_irqrestore(&hp->lock, flags);
89 static void hvc_close(struct tty_struct *tty, struct file * filp)
91 struct hvc_struct *hp = tty->driver_data;
94 if (tty_hung_up_p(filp))
96 spin_lock_irqsave(&hp->lock, flags);
99 else if (hp->count < 0)
100 printk(KERN_ERR "hvc_close %lu: oops, count is %d\n",
101 hp - hvc_struct, hp->count);
102 spin_unlock_irqrestore(&hp->lock, flags);
105 static void hvc_hangup(struct tty_struct *tty)
107 struct hvc_struct *hp = tty->driver_data;
113 /* called with hp->lock held */
114 static void hvc_push(struct hvc_struct *hp)
118 n = hvc_put_chars(hp->index + hvc_offset, hp->outbuf, hp->n_outbuf);
122 /* throw away output on error; this happens when
123 there is no session connected to the vterm. */
127 if (hp->n_outbuf > 0)
128 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
133 static int hvc_write(struct tty_struct *tty, int from_user,
134 const unsigned char *buf, int count)
136 struct hvc_struct *hp = tty->driver_data;
138 int todo, written = 0;
141 spin_lock_irqsave(&hp->lock, flags);
142 while (count > 0 && (todo = N_OUTBUF - hp->n_outbuf) > 0) {
145 p = hp->outbuf + hp->n_outbuf;
147 todo -= copy_from_user(p, buf, todo);
154 memcpy(p, buf, todo);
157 hp->n_outbuf += todo;
161 spin_unlock_irqrestore(&hp->lock, flags);
166 static int hvc_write_room(struct tty_struct *tty)
168 struct hvc_struct *hp = tty->driver_data;
170 return N_OUTBUF - hp->n_outbuf;
173 static int hvc_chars_in_buffer(struct tty_struct *tty)
175 struct hvc_struct *hp = tty->driver_data;
180 static void hvc_poll(int index)
182 struct hvc_struct *hp = &hvc_struct[index];
183 struct tty_struct *tty;
185 char buf[16] __ALIGNED__;
188 spin_lock_irqsave(&hp->lock, flags);
190 if (hp->n_outbuf > 0)
196 if (TTY_FLIPBUF_SIZE - tty->flip.count < sizeof(buf))
198 n = hvc_get_chars(index + hvc_offset, buf, sizeof(buf));
201 for (i = 0; i < n; ++i) {
202 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
203 if (buf[i] == '\x0f') { /* ^O -- should support a sequence */
206 } else if (sysrq_pressed) {
207 handle_sysrq(buf[i], NULL, tty);
212 tty_insert_flip_char(tty, buf[i], 0);
216 tty_schedule_flip(tty);
220 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
221 && tty->ldisc.write_wakeup)
222 (tty->ldisc.write_wakeup)(tty);
223 wake_up_interruptible(&tty->write_wait);
227 spin_unlock_irqrestore(&hp->lock, flags);
230 #if defined (CONFIG_XMON)
231 extern unsigned long cpus_in_xmon;
233 unsigned long cpus_in_xmon=0;
237 int khvcd(void *unused)
245 for (i = 0; i < MAX_NR_HVC_CONSOLES; ++i)
248 set_current_state(TASK_INTERRUPTIBLE);
249 schedule_timeout(TIMEOUT);
253 int __init hvc_init(void)
257 memset(&hvc_driver, 0, sizeof(struct tty_driver));
259 hvc_driver.magic = TTY_DRIVER_MAGIC;
260 hvc_driver.driver_name = "hvc";
261 hvc_driver.name = "hvc/";
262 hvc_driver.major = HVC_MAJOR;
263 hvc_driver.minor_start = HVC_MINOR;
264 hvc_driver.num = hvc_count(&hvc_offset);
265 if (hvc_driver.num > MAX_NR_HVC_CONSOLES)
266 hvc_driver.num = MAX_NR_HVC_CONSOLES;
267 hvc_driver.type = TTY_DRIVER_TYPE_SYSTEM;
268 hvc_driver.init_termios = tty_std_termios;
269 hvc_driver.flags = TTY_DRIVER_REAL_RAW;
270 hvc_driver.refcount = &hvc_refcount;
271 hvc_driver.table = hvc_table;
272 hvc_driver.termios = hvc_termios;
273 hvc_driver.termios_locked = hvc_termios_locked;
275 hvc_driver.open = hvc_open;
276 hvc_driver.close = hvc_close;
277 hvc_driver.write = hvc_write;
278 hvc_driver.hangup = hvc_hangup;
279 hvc_driver.write_room = hvc_write_room;
280 hvc_driver.chars_in_buffer = hvc_chars_in_buffer;
282 for (i = 0; i < hvc_driver.num; i++) {
283 hvc_struct[i].lock = SPIN_LOCK_UNLOCKED;
284 hvc_struct[i].index = i;
285 tty_register_device(&hvc_driver, i);
288 if (tty_register_driver(&hvc_driver))
289 panic("Couldn't register hvc console driver\n");
291 if (hvc_driver.num > 0)
292 kernel_thread(khvcd, NULL, CLONE_KERNEL);
297 static void __exit hvc_exit(void)
301 void hvc_console_print(struct console *co, const char *b, unsigned count)
303 char c[16] __ALIGNED__;
308 while (count > 0 || i > 0) {
309 if (count > 0 && i < sizeof(c)) {
310 if (b[n] == '\n' && !donecr) {
319 r = hvc_put_chars(co->index + hvc_offset, c, i);
321 /* throw away chars on error */
332 static struct tty_driver *hvc_console_device(struct console *c, int *index)
338 static int __init hvc_console_setup(struct console *co, char *options)
340 if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES
341 || co->index >= hvc_count(&hvc_offset))
346 struct console hvc_con_driver = {
348 .write = hvc_console_print,
349 .device = hvc_console_device,
350 .setup = hvc_console_setup,
351 .flags = CON_PRINTBUFFER,
355 static int __init hvc_console_init(void)
357 register_console(&hvc_con_driver);
360 console_initcall(hvc_console_init);
362 module_init(hvc_init);
363 module_exit(hvc_exit);