2 * Input device TTY line discipline
4 * Copyright (c) 1999-2002 Vojtech Pavlik
6 * This is a module that converts a tty line into a much simpler
7 * 'serial io port' abstraction that the input device drivers use.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published by
13 * the Free Software Foundation.
16 #include <asm/uaccess.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/serio.h>
22 #include <linux/tty.h>
24 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
25 MODULE_DESCRIPTION("Input device TTY line discipline");
26 MODULE_LICENSE("GPL");
27 MODULE_ALIAS_LDISC(N_MOUSE);
29 #define SERPORT_BUSY 1
32 struct tty_struct *tty;
33 wait_queue_head_t wait;
39 char serport_name[] = "Serial port";
42 * Callback functions from the serio code.
45 static int serport_serio_write(struct serio *serio, unsigned char data)
47 struct serport *serport = serio->driver;
48 return -(serport->tty->driver->write(serport->tty, 0, &data, 1) != 1);
51 static void serport_serio_close(struct serio *serio)
53 struct serport *serport = serio->driver;
55 serport->serio.type = 0;
56 wake_up_interruptible(&serport->wait);
60 * serport_ldisc_open() is the routine that is called upon setting our line
61 * discipline on a tty. It prepares the serio struct.
64 static int serport_ldisc_open(struct tty_struct *tty)
66 struct serport *serport;
69 serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
70 if (unlikely(!serport))
72 memset(serport, 0, sizeof(struct serport));
74 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
76 tty->disc_data = serport;
78 snprintf(serport->phys, sizeof(serport->phys), "%s/serio0", tty_name(tty, name));
80 serport->serio.name = serport_name;
81 serport->serio.phys = serport->phys;
83 serport->serio.type = SERIO_RS232;
84 serport->serio.write = serport_serio_write;
85 serport->serio.close = serport_serio_close;
86 serport->serio.driver = serport;
88 init_waitqueue_head(&serport->wait);
94 * serport_ldisc_close() is the opposite of serport_ldisc_open()
97 static void serport_ldisc_close(struct tty_struct *tty)
99 struct serport *serport = (struct serport*) tty->disc_data;
104 * serport_ldisc_receive() is called by the low level tty driver when characters
105 * are ready for us. We forward the characters, one by one to the 'interrupt'
108 * FIXME: We should get pt_regs from the tty layer and forward them to
109 * serio_interrupt here.
112 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
114 struct serport *serport = (struct serport*) tty->disc_data;
116 for (i = 0; i < count; i++)
117 serio_interrupt(&serport->serio, cp[i], 0, NULL);
121 * serport_ldisc_room() reports how much room we do have for receiving data.
122 * Although we in fact have infinite room, we need to specify some value
123 * here, and 256 seems to be reasonable.
126 static int serport_ldisc_room(struct tty_struct *tty)
132 * serport_ldisc_read() just waits indefinitely if everything goes well.
133 * However, when the serio driver closes the serio port, it finishes,
134 * returning 0 characters.
137 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
139 struct serport *serport = (struct serport*) tty->disc_data;
142 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
145 serio_register_port(&serport->serio);
146 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
147 wait_event_interruptible(serport->wait, !serport->serio.type);
148 serio_unregister_port(&serport->serio);
150 clear_bit(SERPORT_BUSY, &serport->flags);
156 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
159 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
161 struct serport *serport = (struct serport*) tty->disc_data;
163 if (cmd == SPIOCSTYPE)
164 return get_user(serport->serio.type, (unsigned long __user *) arg);
169 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
171 struct serport *sp = (struct serport *) tty->disc_data;
173 serio_dev_write_wakeup(&sp->serio);
177 * The line discipline structure.
180 static struct tty_ldisc serport_ldisc = {
181 .owner = THIS_MODULE,
183 .open = serport_ldisc_open,
184 .close = serport_ldisc_close,
185 .read = serport_ldisc_read,
186 .ioctl = serport_ldisc_ioctl,
187 .receive_buf = serport_ldisc_receive,
188 .receive_room = serport_ldisc_room,
189 .write_wakeup = serport_ldisc_write_wakeup
193 * The functions for insering/removing us as a module.
196 static int __init serport_init(void)
199 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
201 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
206 static void __exit serport_exit(void)
208 tty_register_ldisc(N_MOUSE, NULL);
211 module_init(serport_init);
212 module_exit(serport_exit);