1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/arch/alpha/kernel/srmcons.c
4 *
5 * Callback based driver for SRM Console console device.
6 * (TTY driver and console driver)
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/console.h>
12 #include <linux/delay.h>
13 #include <linux/mm.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>
20
21 #include <asm/console.h>
22 #include <linux/uaccess.h>
23
24 #include "proto.h"
25
26
27 static DEFINE_SPINLOCK(srmcons_callback_lock);
28 static int srm_is_registered_console = 0;
29
30 /*
31 * The TTY driver
32 */
33 #define MAX_SRM_CONSOLE_DEVICES 1 /* only support 1 console device */
34
35 struct srmcons_private {
36 struct tty_port port;
37 struct timer_list timer;
38 } srmcons_singleton;
39
40 typedef union _srmcons_result {
41 struct {
42 unsigned long c :61;
43 unsigned long status :3;
44 } bits;
45 long as_long;
46 } srmcons_result;
47
48 /* called with callback_lock held */
49 static int
srmcons_do_receive_chars(struct tty_port * port)50 srmcons_do_receive_chars(struct tty_port *port)
51 {
52 srmcons_result result;
53 int count = 0, loops = 0;
54
55 do {
56 result.as_long = callback_getc(0);
57 if (result.bits.status < 2) {
58 tty_insert_flip_char(port, (u8)result.bits.c, 0);
59 count++;
60 }
61 } while((result.bits.status & 1) && (++loops < 10));
62
63 if (count)
64 tty_flip_buffer_push(port);
65
66 return count;
67 }
68
69 static void
srmcons_receive_chars(struct timer_list * t)70 srmcons_receive_chars(struct timer_list *t)
71 {
72 struct srmcons_private *srmconsp = from_timer(srmconsp, t, timer);
73 struct tty_port *port = &srmconsp->port;
74 unsigned long flags;
75 int incr = 10;
76
77 local_irq_save(flags);
78 if (spin_trylock(&srmcons_callback_lock)) {
79 if (!srmcons_do_receive_chars(port))
80 incr = 100;
81 spin_unlock(&srmcons_callback_lock);
82 }
83
84 spin_lock(&port->lock);
85 if (port->tty)
86 mod_timer(&srmconsp->timer, jiffies + incr);
87 spin_unlock(&port->lock);
88
89 local_irq_restore(flags);
90 }
91
92 /* called with callback_lock held */
93 static void
srmcons_do_write(struct tty_port * port,const u8 * buf,size_t count)94 srmcons_do_write(struct tty_port *port, const u8 *buf, size_t count)
95 {
96 size_t c;
97 srmcons_result result;
98
99 while (count > 0) {
100 bool need_cr = false;
101 /*
102 * Break it up into reasonable size chunks to allow a chance
103 * for input to get in
104 */
105 for (c = 0; c < min_t(size_t, 128U, count) && !need_cr; c++)
106 if (buf[c] == '\n')
107 need_cr = true;
108
109 while (c > 0) {
110 result.as_long = callback_puts(0, buf, c);
111 c -= result.bits.c;
112 count -= result.bits.c;
113 buf += result.bits.c;
114
115 /*
116 * Check for pending input iff a tty port was provided
117 */
118 if (port)
119 srmcons_do_receive_chars(port);
120 }
121
122 while (need_cr) {
123 result.as_long = callback_puts(0, "\r", 1);
124 if (result.bits.c > 0)
125 need_cr = false;
126 }
127 }
128 }
129
130 static ssize_t
srmcons_write(struct tty_struct * tty,const u8 * buf,size_t count)131 srmcons_write(struct tty_struct *tty, const u8 *buf, size_t count)
132 {
133 unsigned long flags;
134
135 spin_lock_irqsave(&srmcons_callback_lock, flags);
136 srmcons_do_write(tty->port, buf, count);
137 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
138
139 return count;
140 }
141
142 static unsigned int
srmcons_write_room(struct tty_struct * tty)143 srmcons_write_room(struct tty_struct *tty)
144 {
145 return 512;
146 }
147
148 static int
srmcons_open(struct tty_struct * tty,struct file * filp)149 srmcons_open(struct tty_struct *tty, struct file *filp)
150 {
151 struct srmcons_private *srmconsp = &srmcons_singleton;
152 struct tty_port *port = &srmconsp->port;
153 unsigned long flags;
154
155 spin_lock_irqsave(&port->lock, flags);
156
157 if (!port->tty) {
158 tty->driver_data = srmconsp;
159 tty->port = port;
160 port->tty = tty; /* XXX proper refcounting */
161 mod_timer(&srmconsp->timer, jiffies + 10);
162 }
163
164 spin_unlock_irqrestore(&port->lock, flags);
165
166 return 0;
167 }
168
169 static void
srmcons_close(struct tty_struct * tty,struct file * filp)170 srmcons_close(struct tty_struct *tty, struct file *filp)
171 {
172 struct srmcons_private *srmconsp = tty->driver_data;
173 struct tty_port *port = &srmconsp->port;
174 unsigned long flags;
175
176 spin_lock_irqsave(&port->lock, flags);
177
178 if (tty->count == 1) {
179 port->tty = NULL;
180 timer_delete(&srmconsp->timer);
181 }
182
183 spin_unlock_irqrestore(&port->lock, flags);
184 }
185
186
187 static struct tty_driver *srmcons_driver;
188
189 static const struct tty_operations srmcons_ops = {
190 .open = srmcons_open,
191 .close = srmcons_close,
192 .write = srmcons_write,
193 .write_room = srmcons_write_room,
194 };
195
196 static int __init
srmcons_init(void)197 srmcons_init(void)
198 {
199 struct tty_driver *driver;
200 int err;
201
202 timer_setup(&srmcons_singleton.timer, srmcons_receive_chars, 0);
203
204 if (!srm_is_registered_console)
205 return -ENODEV;
206
207 driver = tty_alloc_driver(MAX_SRM_CONSOLE_DEVICES, 0);
208 if (IS_ERR(driver))
209 return PTR_ERR(driver);
210
211 tty_port_init(&srmcons_singleton.port);
212
213 driver->driver_name = "srm";
214 driver->name = "srm";
215 driver->major = 0; /* dynamic */
216 driver->minor_start = 0;
217 driver->type = TTY_DRIVER_TYPE_SYSTEM;
218 driver->subtype = SYSTEM_TYPE_SYSCONS;
219 driver->init_termios = tty_std_termios;
220 tty_set_operations(driver, &srmcons_ops);
221 tty_port_link_device(&srmcons_singleton.port, driver, 0);
222 err = tty_register_driver(driver);
223 if (err)
224 goto err_free_drv;
225
226 srmcons_driver = driver;
227
228 return 0;
229 err_free_drv:
230 tty_driver_kref_put(driver);
231 tty_port_destroy(&srmcons_singleton.port);
232
233 return err;
234 }
235 device_initcall(srmcons_init);
236
237 /*
238 * The console driver
239 */
240 static void
srm_console_write(struct console * co,const char * s,unsigned count)241 srm_console_write(struct console *co, const char *s, unsigned count)
242 {
243 unsigned long flags;
244
245 spin_lock_irqsave(&srmcons_callback_lock, flags);
246 srmcons_do_write(NULL, s, count);
247 spin_unlock_irqrestore(&srmcons_callback_lock, flags);
248 }
249
250 static struct tty_driver *
srm_console_device(struct console * co,int * index)251 srm_console_device(struct console *co, int *index)
252 {
253 *index = co->index;
254 return srmcons_driver;
255 }
256
257 static int
srm_console_setup(struct console * co,char * options)258 srm_console_setup(struct console *co, char *options)
259 {
260 return 0;
261 }
262
263 static struct console srmcons = {
264 .name = "srm",
265 .write = srm_console_write,
266 .device = srm_console_device,
267 .setup = srm_console_setup,
268 .flags = CON_PRINTBUFFER | CON_BOOT,
269 .index = -1,
270 };
271
272 void __init
register_srm_console(void)273 register_srm_console(void)
274 {
275 if (!srm_is_registered_console) {
276 callback_open_console();
277 register_console(&srmcons);
278 srm_is_registered_console = 1;
279 }
280 }
281
282 void __init
unregister_srm_console(void)283 unregister_srm_console(void)
284 {
285 if (srm_is_registered_console) {
286 callback_close_console();
287 unregister_console(&srmcons);
288 srm_is_registered_console = 0;
289 }
290 }
291