console.c 6.4 KB
Newer Older
1
/*
2
 * arch/xtensa/platforms/iss/console.c
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2001-2005 Tensilica Inc.
 *   Authors	Christian Zankel, Joe Taylor
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/console.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/major.h>
#include <linux/param.h>
20
#include <linux/seq_file.h>
21 22 23 24 25
#include <linux/serial.h>

#include <asm/uaccess.h>
#include <asm/irq.h>

26
#include <platform/simcall.h>
27 28 29 30

#include <linux/tty.h>
#include <linux/tty_flip.h>

31 32 33 34
#ifdef SERIAL_INLINE
#define _INLINE_ inline
#endif

35
#define SERIAL_MAX_NUM_LINES 1
36
#define SERIAL_TIMER_VALUE (HZ / 10)
37 38

static struct tty_driver *serial_driver;
J
Jiri Slaby 已提交
39
static struct tty_port serial_port;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
static struct timer_list serial_timer;

static DEFINE_SPINLOCK(timer_lock);

static char *serial_version = "0.1";
static char *serial_name = "ISS serial driver";

/*
 * This routine is called whenever a serial port is opened.  It
 * enables interrupts for a serial port, linking in its async structure into
 * the IRQ chain.   It also performs the serial-specific
 * initialization for the tty structure.
 */

static void rs_poll(unsigned long);

static int rs_open(struct tty_struct *tty, struct file * filp)
{
J
Jiri Slaby 已提交
58
	tty->port = &serial_port;
59 60
	spin_lock(&timer_lock);
	if (tty->count == 1) {
61
		setup_timer(&serial_timer, rs_poll, (unsigned long)tty);
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
	}
	spin_unlock(&timer_lock);

	return 0;
}


/*
 * ------------------------------------------------------------
 * iss_serial_close()
 *
 * This routine is called when the serial port gets closed.  First, we
 * wait for the last remaining data to be sent.  Then, we unlink its
 * async structure from the interrupt chain if necessary, and we free
 * that IRQ if nothing is left in the chain.
 * ------------------------------------------------------------
 */
static void rs_close(struct tty_struct *tty, struct file * filp)
{
82
	spin_lock_bh(&timer_lock);
83 84
	if (tty->count == 1)
		del_timer_sync(&serial_timer);
85
	spin_unlock_bh(&timer_lock);
86 87 88 89 90 91 92 93
}


static int rs_write(struct tty_struct * tty,
		    const unsigned char *buf, int count)
{
	/* see drivers/char/serialX.c to reference original version */

M
Max Filippov 已提交
94
	simc_write(1, buf, count);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	return count;
}

static void rs_poll(unsigned long priv)
{
	struct tty_struct* tty = (struct tty_struct*) priv;

	struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
	int i = 0;
	unsigned char c;

	spin_lock(&timer_lock);

	while (__simc(SYS_select_one, 0, XTISS_SELECT_ONE_READ, (int)&tv,0,0)){
		__simc (SYS_read, 0, (unsigned long)&c, 1, 0, 0);
110
		tty_insert_flip_char(tty, c, TTY_NORMAL);
111 112 113 114 115 116 117 118 119 120 121 122
		i++;
	}

	if (i)
		tty_flip_buffer_push(tty);


	mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
	spin_unlock(&timer_lock);
}


123
static int rs_put_char(struct tty_struct *tty, unsigned char ch)
124
{
M
Max Filippov 已提交
125
	return rs_write(tty, &ch, 1);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

static void rs_flush_chars(struct tty_struct *tty)
{
}

static int rs_write_room(struct tty_struct *tty)
{
	/* Let's say iss can always accept 2K characters.. */
	return 2 * 1024;
}

static int rs_chars_in_buffer(struct tty_struct *tty)
{
	/* the iss doesn't buffer characters */
	return 0;
}

static void rs_hangup(struct tty_struct *tty)
{
	/* Stub, once again.. */
}

static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
{
	/* Stub, once again.. */
}

154
static int rs_proc_show(struct seq_file *m, void *v)
155
{
156 157 158
	seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
	return 0;
}
159

160 161 162
static int rs_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, rs_proc_show, NULL);
163 164
}

165 166 167 168 169 170 171
static const struct file_operations rs_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= rs_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
172

173
static const struct tty_operations serial_ops = {
174 175 176 177 178 179 180 181 182
	.open = rs_open,
	.close = rs_close,
	.write = rs_write,
	.put_char = rs_put_char,
	.flush_chars = rs_flush_chars,
	.write_room = rs_write_room,
	.chars_in_buffer = rs_chars_in_buffer,
	.hangup = rs_hangup,
	.wait_until_sent = rs_wait_until_sent,
183
	.proc_fops = &rs_proc_fops,
184 185 186 187
};

int __init rs_init(void)
{
J
Jiri Slaby 已提交
188 189
	tty_port_init(&serial_port);

190
	serial_driver = alloc_tty_driver(SERIAL_MAX_NUM_LINES);
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

	printk ("%s %s\n", serial_name, serial_version);

	/* Initialize the tty_driver structure */

	serial_driver->driver_name = "iss_serial";
	serial_driver->name = "ttyS";
	serial_driver->major = TTY_MAJOR;
	serial_driver->minor_start = 64;
	serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
	serial_driver->subtype = SERIAL_TYPE_NORMAL;
	serial_driver->init_termios = tty_std_termios;
	serial_driver->init_termios.c_cflag =
		B9600 | CS8 | CREAD | HUPCL | CLOCAL;
	serial_driver->flags = TTY_DRIVER_REAL_RAW;

	tty_set_operations(serial_driver, &serial_ops);
J
Jiri Slaby 已提交
208
	tty_port_link_device(&serial_port, serial_driver, 0);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

	if (tty_register_driver(serial_driver))
		panic("Couldn't register serial driver\n");
	return 0;
}


static __exit void rs_exit(void)
{
	int error;

	if ((error = tty_unregister_driver(serial_driver)))
		printk("ISS_SERIAL: failed to unregister serial driver (%d)\n",
		       error);
	put_tty_driver(serial_driver);
}


/* We use `late_initcall' instead of just `__initcall' as a workaround for
 * the fact that (1) simcons_tty_init can't be called before tty_init,
 * (2) tty_init is called via `module_init', (3) if statically linked,
 * module_init == device_init, and (4) there's no ordering of init lists.
 * We can do this easily because simcons is always statically linked, but
 * other tty drivers that depend on tty_init and which must use
 * `module_init' to declare their init routines are likely to be broken.
 */

late_initcall(rs_init);


#ifdef CONFIG_SERIAL_CONSOLE

static void iss_console_write(struct console *co, const char *s, unsigned count)
{
	int len = strlen(s);

	if (s != 0 && *s != 0)
		__simc (SYS_write, 1, (unsigned long)s,
			count < len ? count : len,0,0);
}

static struct tty_driver* iss_console_device(struct console *c, int *index)
{
	*index = c->index;
	return serial_driver;
}


static struct console sercons = {
	.name = "ttyS",
	.write = iss_console_write,
	.device = iss_console_device,
	.flags = CON_PRINTBUFFER,
	.index = -1
};

static int __init iss_console_init(void)
{
	register_console(&sercons);
	return 0;
}

console_initcall(iss_console_init);

#endif /* CONFIG_SERIAL_CONSOLE */