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>
32 #include <linux/cpumask.h>
34 extern int hvc_count(int *);
35 extern int hvc_get_chars(int index, char *buf, int count);
36 extern int hvc_put_chars(int index, const char *buf, int count);
41 #define MAX_NR_HVC_CONSOLES 4
43 #define TIMEOUT ((HZ + 99) / 100)
45 static struct tty_driver *hvc_driver;
46 static int hvc_offset;
47 #ifdef CONFIG_MAGIC_SYSRQ
48 static int sysrq_pressed;
53 #define __ALIGNED__ __attribute__((__aligned__(8)))
58 struct tty_struct *tty;
61 char outbuf[N_OUTBUF] __ALIGNED__;
65 struct hvc_struct hvc_struct[MAX_NR_HVC_CONSOLES];
67 static int hvc_open(struct tty_struct *tty, struct file * filp)
69 int line = tty->index;
70 struct hvc_struct *hp;
73 if (line < 0 || line >= MAX_NR_HVC_CONSOLES)
75 hp = &hvc_struct[line];
77 tty->driver_data = hp;
78 spin_lock_irqsave(&hp->lock, flags);
81 spin_unlock_irqrestore(&hp->lock, flags);
86 static void hvc_close(struct tty_struct *tty, struct file * filp)
88 struct hvc_struct *hp = tty->driver_data;
91 if (tty_hung_up_p(filp))
93 spin_lock_irqsave(&hp->lock, flags);
96 else if (hp->count < 0)
97 printk(KERN_ERR "hvc_close %lu: oops, count is %d\n",
98 hp - hvc_struct, hp->count);
99 spin_unlock_irqrestore(&hp->lock, flags);
102 static void hvc_hangup(struct tty_struct *tty)
104 struct hvc_struct *hp = tty->driver_data;
110 /* called with hp->lock held */
111 static void hvc_push(struct hvc_struct *hp)
115 n = hvc_put_chars(hp->index + hvc_offset, hp->outbuf, hp->n_outbuf);
119 /* throw away output on error; this happens when
120 there is no session connected to the vterm. */
124 if (hp->n_outbuf > 0)
125 memmove(hp->outbuf, hp->outbuf + n, hp->n_outbuf);
130 static int hvc_write(struct tty_struct *tty, int from_user,
131 const unsigned char *buf, int count)
133 struct hvc_struct *hp = tty->driver_data;
135 int tbsize, rsize, written = 0;
139 tbsize = min(count, (int)PAGE_SIZE);
140 if (!(tbuf = kmalloc(tbsize, GFP_KERNEL)))
143 while ((rsize = count - written) > 0) {
149 rsize -= copy_from_user(p, buf, rsize);
158 spin_lock_irqsave(&hp->lock, flags);
159 for (wsize = N_OUTBUF - hp->n_outbuf; rsize && wsize;
160 wsize = N_OUTBUF - hp->n_outbuf) {
163 memcpy(hp->outbuf + hp->n_outbuf, p, wsize);
164 hp->n_outbuf += wsize;
169 spin_unlock_irqrestore(&hp->lock, flags);
180 spin_lock_irqsave(&hp->lock, flags);
181 while (count > 0 && (rsize = N_OUTBUF - hp->n_outbuf) > 0) {
184 memcpy(hp->outbuf + hp->n_outbuf, buf, rsize);
187 hp->n_outbuf += rsize;
191 spin_unlock_irqrestore(&hp->lock, flags);
197 static int hvc_write_room(struct tty_struct *tty)
199 struct hvc_struct *hp = tty->driver_data;
201 return N_OUTBUF - hp->n_outbuf;
204 static int hvc_chars_in_buffer(struct tty_struct *tty)
206 struct hvc_struct *hp = tty->driver_data;
211 static void hvc_poll(int index)
213 struct hvc_struct *hp = &hvc_struct[index];
214 struct tty_struct *tty;
216 char buf[16] __ALIGNED__;
219 spin_lock_irqsave(&hp->lock, flags);
221 if (hp->n_outbuf > 0)
227 if (TTY_FLIPBUF_SIZE - tty->flip.count < sizeof(buf))
229 n = hvc_get_chars(index + hvc_offset, buf, sizeof(buf));
232 for (i = 0; i < n; ++i) {
233 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
234 if (buf[i] == '\x0f') { /* ^O -- should support a sequence */
237 } else if (sysrq_pressed) {
238 handle_sysrq(buf[i], NULL, tty);
243 tty_insert_flip_char(tty, buf[i], 0);
247 tty_schedule_flip(tty);
251 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP))
252 && tty->ldisc.write_wakeup)
253 (tty->ldisc.write_wakeup)(tty);
254 wake_up_interruptible(&tty->write_wait);
258 spin_unlock_irqrestore(&hp->lock, flags);
261 #if defined(CONFIG_XMON) && defined(CONFIG_SMP)
262 extern cpumask_t cpus_in_xmon;
264 static const cpumask_t cpus_in_xmon = CPU_MASK_NONE;
268 int khvcd(void *unused)
275 if (cpus_empty(cpus_in_xmon)) {
276 for (i = 0; i < MAX_NR_HVC_CONSOLES; ++i)
279 set_current_state(TASK_INTERRUPTIBLE);
280 schedule_timeout(TIMEOUT);
284 static struct tty_operations hvc_ops = {
288 .hangup = hvc_hangup,
289 .write_room = hvc_write_room,
290 .chars_in_buffer = hvc_chars_in_buffer,
293 int __init hvc_init(void)
295 int num = hvc_count(&hvc_offset);
298 if (num > MAX_NR_HVC_CONSOLES)
299 num = MAX_NR_HVC_CONSOLES;
301 hvc_driver = alloc_tty_driver(num);
305 hvc_driver->owner = THIS_MODULE;
306 hvc_driver->devfs_name = "hvc/";
307 hvc_driver->driver_name = "hvc";
308 hvc_driver->name = "hvc";
309 hvc_driver->major = HVC_MAJOR;
310 hvc_driver->minor_start = HVC_MINOR;
311 hvc_driver->type = TTY_DRIVER_TYPE_SYSTEM;
312 hvc_driver->init_termios = tty_std_termios;
313 hvc_driver->flags = TTY_DRIVER_REAL_RAW;
314 tty_set_operations(hvc_driver, &hvc_ops);
315 for (i = 0; i < num; i++) {
316 hvc_struct[i].lock = SPIN_LOCK_UNLOCKED;
317 hvc_struct[i].index = i;
320 if (tty_register_driver(hvc_driver))
321 panic("Couldn't register hvc console driver\n");
324 kernel_thread(khvcd, NULL, CLONE_KERNEL);
329 static void __exit hvc_exit(void)
333 void hvc_console_print(struct console *co, const char *b, unsigned count)
335 char c[16] __ALIGNED__;
340 while (count > 0 || i > 0) {
341 if (count > 0 && i < sizeof(c)) {
342 if (b[n] == '\n' && !donecr) {
351 r = hvc_put_chars(co->index + hvc_offset, c, i);
353 /* throw away chars on error */
364 static struct tty_driver *hvc_console_device(struct console *c, int *index)
370 static int __init hvc_console_setup(struct console *co, char *options)
372 if (co->index < 0 || co->index >= MAX_NR_HVC_CONSOLES
373 || co->index >= hvc_count(&hvc_offset))
378 struct console hvc_con_driver = {
380 .write = hvc_console_print,
381 .device = hvc_console_device,
382 .setup = hvc_console_setup,
383 .flags = CON_PRINTBUFFER,
387 static int __init hvc_console_init(void)
389 register_console(&hvc_con_driver);
392 console_initcall(hvc_console_init);
394 module_init(hvc_init);
395 module_exit(hvc_exit);