1 // SPDX-License-Identifier: GPL-2.0
3 * linux/arch/alpha/kernel/srmcons.c
5 * Callback based driver for SRM Console console device.
6 * (TTY driver and console driver)
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 <linux/uaccess.h>
25 static DEFINE_SPINLOCK(srmcons_callback_lock);
26 static int srm_is_registered_console = 0;
31 #define MAX_SRM_CONSOLE_DEVICES 1 /* only support 1 console device */
33 struct srmcons_private {
35 struct timer_list timer;
38 typedef union _srmcons_result {
41 unsigned long status :3;
46 /* called with callback_lock held */
48 srmcons_do_receive_chars(struct tty_port *port)
50 srmcons_result result;
51 int count = 0, loops = 0;
54 result.as_long = callback_getc(0);
55 if (result.bits.status < 2) {
56 tty_insert_flip_char(port, (char)result.bits.c, 0);
59 } while((result.bits.status & 1) && (++loops < 10));
62 tty_flip_buffer_push(port);
68 srmcons_receive_chars(struct timer_list *t)
70 struct srmcons_private *srmconsp = from_timer(srmconsp, t, timer);
71 struct tty_port *port = &srmconsp->port;
75 local_irq_save(flags);
76 if (spin_trylock(&srmcons_callback_lock)) {
77 if (!srmcons_do_receive_chars(port))
79 spin_unlock(&srmcons_callback_lock);
82 spin_lock(&port->lock);
84 mod_timer(&srmconsp->timer, jiffies + incr);
85 spin_unlock(&port->lock);
87 local_irq_restore(flags);
90 /* called with callback_lock held */
92 srmcons_do_write(struct tty_port *port, const char *buf, int count)
94 static char str_cr[1] = "\r";
95 long c, remaining = count;
96 srmcons_result result;
100 for (cur = (char *)buf; remaining > 0; ) {
103 * Break it up into reasonable size chunks to allow a chance
104 * for input to get in
106 for (c = 0; c < min_t(long, 128L, remaining) && !need_cr; c++)
111 result.as_long = callback_puts(0, cur, c);
113 remaining -= result.bits.c;
114 cur += result.bits.c;
117 * Check for pending input iff a tty port was provided
120 srmcons_do_receive_chars(port);
124 result.as_long = callback_puts(0, str_cr, 1);
125 if (result.bits.c > 0)
133 srmcons_write(struct tty_struct *tty, const u8 *buf, size_t count)
137 spin_lock_irqsave(&srmcons_callback_lock, flags);
138 srmcons_do_write(tty->port, (const char *) buf, count);
139 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
145 srmcons_write_room(struct tty_struct *tty)
151 srmcons_open(struct tty_struct *tty, struct file *filp)
153 struct srmcons_private *srmconsp = &srmcons_singleton;
154 struct tty_port *port = &srmconsp->port;
157 spin_lock_irqsave(&port->lock, flags);
160 tty->driver_data = srmconsp;
162 port->tty = tty; /* XXX proper refcounting */
163 mod_timer(&srmconsp->timer, jiffies + 10);
166 spin_unlock_irqrestore(&port->lock, flags);
172 srmcons_close(struct tty_struct *tty, struct file *filp)
174 struct srmcons_private *srmconsp = tty->driver_data;
175 struct tty_port *port = &srmconsp->port;
178 spin_lock_irqsave(&port->lock, flags);
180 if (tty->count == 1) {
182 del_timer(&srmconsp->timer);
185 spin_unlock_irqrestore(&port->lock, flags);
189 static struct tty_driver *srmcons_driver;
191 static const struct tty_operations srmcons_ops = {
192 .open = srmcons_open,
193 .close = srmcons_close,
194 .write = srmcons_write,
195 .write_room = srmcons_write_room,
201 timer_setup(&srmcons_singleton.timer, srmcons_receive_chars, 0);
202 if (srm_is_registered_console) {
203 struct tty_driver *driver;
206 driver = tty_alloc_driver(MAX_SRM_CONSOLE_DEVICES, 0);
208 return PTR_ERR(driver);
210 tty_port_init(&srmcons_singleton.port);
212 driver->driver_name = "srm";
213 driver->name = "srm";
214 driver->major = 0; /* dynamic */
215 driver->minor_start = 0;
216 driver->type = TTY_DRIVER_TYPE_SYSTEM;
217 driver->subtype = SYSTEM_TYPE_SYSCONS;
218 driver->init_termios = tty_std_termios;
219 tty_set_operations(driver, &srmcons_ops);
220 tty_port_link_device(&srmcons_singleton.port, driver, 0);
221 err = tty_register_driver(driver);
223 tty_driver_kref_put(driver);
224 tty_port_destroy(&srmcons_singleton.port);
227 srmcons_driver = driver;
232 device_initcall(srmcons_init);
239 srm_console_write(struct console *co, const char *s, unsigned count)
243 spin_lock_irqsave(&srmcons_callback_lock, flags);
244 srmcons_do_write(NULL, s, count);
245 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
248 static struct tty_driver *
249 srm_console_device(struct console *co, int *index)
252 return srmcons_driver;
256 srm_console_setup(struct console *co, char *options)
261 static struct console srmcons = {
263 .write = srm_console_write,
264 .device = srm_console_device,
265 .setup = srm_console_setup,
266 .flags = CON_PRINTBUFFER | CON_BOOT,
271 register_srm_console(void)
273 if (!srm_is_registered_console) {
274 callback_open_console();
275 register_console(&srmcons);
276 srm_is_registered_console = 1;
281 unregister_srm_console(void)
283 if (srm_is_registered_console) {
284 callback_close_console();
285 unregister_console(&srmcons);
286 srm_is_registered_console = 0;