2 * linux/arch/alpha/kernel/srmcons.c
4 * Callback based driver for SRM Console console device.
5 * (TTY driver and console driver)
8 #include <linux/config.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/console.h>
12 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/timer.h>
17 #include <linux/tty.h>
18 #include <linux/tty_driver.h>
19 #include <linux/tty_flip.h>
21 #include <asm/console.h>
22 #include <asm/uaccess.h>
25 static spinlock_t srmcons_callback_lock = SPIN_LOCK_UNLOCKED;
26 static int srm_is_registered_console = 0;
31 #define MAX_SRM_CONSOLE_DEVICES 1 /* only support 1 console device */
33 struct srmcons_private {
34 struct tty_struct *tty;
35 struct timer_list timer;
39 typedef union _srmcons_result {
42 unsigned long status :3;
47 /* called with callback_lock held */
49 srmcons_do_receive_chars(struct tty_struct *tty)
51 srmcons_result result;
52 int count = 0, loops = 0;
55 result.as_long = callback_getc(0);
56 if (result.bits.status < 2) {
57 tty_insert_flip_char(tty, (char)result.bits.c, 0);
60 } while((result.bits.status & 1) && (++loops < 10));
63 tty_schedule_flip(tty);
69 srmcons_receive_chars(unsigned long data)
71 struct srmcons_private *srmconsp = (struct srmcons_private *)data;
75 local_irq_save(flags);
76 if (spin_trylock(&srmcons_callback_lock)) {
77 if (!srmcons_do_receive_chars(srmconsp->tty))
79 spin_unlock(&srmcons_callback_lock);
82 spin_lock(&srmconsp->lock);
84 srmconsp->timer.expires = jiffies + incr;
85 add_timer(&srmconsp->timer);
87 spin_unlock(&srmconsp->lock);
89 local_irq_restore(flags);
92 /* called with callback_lock held */
94 srmcons_do_write(struct tty_struct *tty, const char *buf, int count)
96 static char str_cr[1] = "\r";
97 long c, remaining = count;
98 srmcons_result result;
102 for (cur = (char *)buf; remaining > 0; ) {
105 * Break it up into reasonable size chunks to allow a chance
106 * for input to get in
108 for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
113 result.as_long = callback_puts(0, cur, c);
115 remaining -= result.bits.c;
116 cur += result.bits.c;
119 * Check for pending input iff a tty was provided
122 srmcons_do_receive_chars(tty);
126 result.as_long = callback_puts(0, str_cr, 1);
127 if (result.bits.c > 0)
135 srmcons_write(struct tty_struct *tty,
136 const unsigned char *buf, int count)
140 spin_lock_irqsave(&srmcons_callback_lock, flags);
141 srmcons_do_write(tty, (const char *) buf, count);
142 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
148 srmcons_write_room(struct tty_struct *tty)
154 srmcons_chars_in_buffer(struct tty_struct *tty)
160 srmcons_get_private_struct(struct srmcons_private **ps)
162 static struct srmcons_private *srmconsp = NULL;
163 static spinlock_t srmconsp_lock = SPIN_LOCK_UNLOCKED;
167 spin_lock_irqsave(&srmconsp_lock, flags);
170 if (srmconsp != NULL) {
175 srmconsp = kmalloc(sizeof(*srmconsp), GFP_KERNEL);
176 if (srmconsp == NULL) {
181 srmconsp->tty = NULL;
182 srmconsp->lock = SPIN_LOCK_UNLOCKED;
183 init_timer(&srmconsp->timer);
188 spin_unlock_irqrestore(&srmconsp_lock, flags);
194 srmcons_open(struct tty_struct *tty, struct file *filp)
196 struct srmcons_private *srmconsp;
200 retval = srmcons_get_private_struct(&srmconsp);
204 spin_lock_irqsave(&srmconsp->lock, flags);
206 if (!srmconsp->tty) {
207 tty->driver_data = srmconsp;
210 srmconsp->timer.function = srmcons_receive_chars;
211 srmconsp->timer.data = (unsigned long)srmconsp;
212 srmconsp->timer.expires = jiffies + 10;
213 add_timer(&srmconsp->timer);
216 spin_unlock_irqrestore(&srmconsp->lock, flags);
222 srmcons_close(struct tty_struct *tty, struct file *filp)
224 struct srmcons_private *srmconsp = tty->driver_data;
227 spin_lock_irqsave(&srmconsp->lock, flags);
229 if (tty->count == 1) {
230 srmconsp->tty = NULL;
231 del_timer(&srmconsp->timer);
234 spin_unlock_irqrestore(&srmconsp->lock, flags);
238 static struct tty_driver *srmcons_driver;
240 static struct tty_operations srmcons_ops = {
241 .open = srmcons_open,
242 .close = srmcons_close,
243 .write = srmcons_write,
244 .write_room = srmcons_write_room,
245 .chars_in_buffer= srmcons_chars_in_buffer,
251 if (srm_is_registered_console) {
252 struct tty_driver *driver;
255 driver = alloc_tty_driver(MAX_SRM_CONSOLE_DEVICES);
258 driver->driver_name = "srm";
259 driver->name = "srm";
260 driver->major = 0; /* dynamic */
261 driver->minor_start = 0;
262 driver->type = TTY_DRIVER_TYPE_SYSTEM;
263 driver->subtype = SYSTEM_TYPE_SYSCONS;
264 driver->init_termios = tty_std_termios;
265 tty_set_operations(driver, &srmcons_ops);
266 err = tty_register_driver(driver);
268 put_tty_driver(driver);
271 srmcons_driver = driver;
277 module_init(srmcons_init);
284 srm_console_write(struct console *co, const char *s, unsigned count)
288 spin_lock_irqsave(&srmcons_callback_lock, flags);
289 srmcons_do_write(NULL, s, count);
290 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
293 static struct tty_driver *
294 srm_console_device(struct console *co, int *index)
297 return srmcons_driver;
301 srm_console_setup(struct console *co, char *options)
306 static struct console srmcons = {
308 .write = srm_console_write,
309 .device = srm_console_device,
310 .setup = srm_console_setup,
311 .flags = CON_PRINTBUFFER,
316 register_srm_console(void)
318 if (!srm_is_registered_console) {
319 callback_open_console();
320 register_console(&srmcons);
321 srm_is_registered_console = 1;
326 unregister_srm_console(void)
328 if (srm_is_registered_console) {
329 callback_close_console();
330 unregister_console(&srmcons);
331 srm_is_registered_console = 0;