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
30 #define SERPORT_ACTIVE 2
31 #define SERPORT_DEAD 3
34 struct tty_struct *tty;
35 wait_queue_head_t wait;
37 struct serio_device_id id;
43 * Callback functions from the serio code.
46 static int serport_serio_write(struct serio *serio, unsigned char data)
48 struct serport *serport = serio->port_data;
49 return -(serport->tty->driver->write(serport->tty, &data, 1) != 1);
52 static int serport_serio_open(struct serio *serio)
54 struct serport *serport = serio->port_data;
57 spin_lock_irqsave(&serport->lock, flags);
58 set_bit(SERPORT_ACTIVE, &serport->flags);
59 spin_unlock_irqrestore(&serport->lock, flags);
65 static void serport_serio_close(struct serio *serio)
67 struct serport *serport = serio->port_data;
70 spin_lock_irqsave(&serport->lock, flags);
71 clear_bit(SERPORT_ACTIVE, &serport->flags);
72 set_bit(SERPORT_DEAD, &serport->flags);
73 spin_unlock_irqrestore(&serport->lock, flags);
75 wake_up_interruptible(&serport->wait);
79 * serport_ldisc_open() is the routine that is called upon setting our line
80 * discipline on a tty. It prepares the serio struct.
83 static int serport_ldisc_open(struct tty_struct *tty)
85 struct serport *serport;
87 if (!capable(CAP_SYS_ADMIN))
90 serport = kcalloc(1, sizeof(struct serport), GFP_KERNEL);
95 spin_lock_init(&serport->lock);
96 init_waitqueue_head(&serport->wait);
98 tty->disc_data = serport;
99 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
105 * serport_ldisc_close() is the opposite of serport_ldisc_open()
108 static void serport_ldisc_close(struct tty_struct *tty)
110 struct serport *serport = (struct serport *) tty->disc_data;
116 * serport_ldisc_receive() is called by the low level tty driver when characters
117 * are ready for us. We forward the characters, one by one to the 'interrupt'
120 * FIXME: We should get pt_regs from the tty layer and forward them to
121 * serio_interrupt here.
124 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
126 struct serport *serport = (struct serport*) tty->disc_data;
130 spin_lock_irqsave(&serport->lock, flags);
132 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
135 for (i = 0; i < count; i++)
136 serio_interrupt(serport->serio, cp[i], 0, NULL);
139 spin_unlock_irqrestore(&serport->lock, flags);
143 * serport_ldisc_room() reports how much room we do have for receiving data.
144 * Although we in fact have infinite room, we need to specify some value
145 * here, and 256 seems to be reasonable.
148 static int serport_ldisc_room(struct tty_struct *tty)
154 * serport_ldisc_read() just waits indefinitely if everything goes well.
155 * However, when the serio driver closes the serio port, it finishes,
156 * returning 0 characters.
159 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
161 struct serport *serport = (struct serport*) tty->disc_data;
165 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
168 serport->serio = serio = kcalloc(1, sizeof(struct serio), GFP_KERNEL);
172 strlcpy(serio->name, "Serial port", sizeof(serio->name));
173 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
174 serio->id = serport->id;
175 serio->id.type = SERIO_RS232;
176 serio->write = serport_serio_write;
177 serio->open = serport_serio_open;
178 serio->close = serport_serio_close;
179 serio->port_data = serport;
181 serio_register_port(serport->serio);
182 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
184 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
185 serio_unregister_port(serport->serio);
186 serport->serio = NULL;
188 clear_bit(SERPORT_DEAD, &serport->flags);
189 clear_bit(SERPORT_BUSY, &serport->flags);
195 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
198 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
200 struct serport *serport = (struct serport*) tty->disc_data;
203 if (cmd == SPIOCSTYPE) {
204 if (get_user(type, (unsigned long __user *) arg))
207 serport->id.proto = type & 0x000000ff;
208 serport->id.id = (type & 0x0000ff00) >> 8;
209 serport->id.extra = (type & 0x00ff0000) >> 16;
217 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
219 struct serport *serport = (struct serport *) tty->disc_data;
222 spin_lock_irqsave(&serport->lock, flags);
223 if (test_bit(SERPORT_ACTIVE, &serport->flags))
224 serio_drv_write_wakeup(serport->serio);
225 spin_unlock_irqrestore(&serport->lock, flags);
229 * The line discipline structure.
232 static struct tty_ldisc serport_ldisc = {
233 .owner = THIS_MODULE,
235 .open = serport_ldisc_open,
236 .close = serport_ldisc_close,
237 .read = serport_ldisc_read,
238 .ioctl = serport_ldisc_ioctl,
239 .receive_buf = serport_ldisc_receive,
240 .receive_room = serport_ldisc_room,
241 .write_wakeup = serport_ldisc_write_wakeup
245 * The functions for insering/removing us as a module.
248 static int __init serport_init(void)
251 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
253 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
258 static void __exit serport_exit(void)
260 tty_unregister_ldisc(N_MOUSE);
263 module_init(serport_init);
264 module_exit(serport_exit);