Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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