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 * Callback functions from the serio code.
42 static int serport_serio_write(struct serio *serio, unsigned char data)
44 struct serport *serport = serio->port_data;
45 return -(serport->tty->driver->write(serport->tty, 0, &data, 1) != 1);
48 static void serport_serio_close(struct serio *serio)
50 struct serport *serport = serio->port_data;
52 serport->serio->type = 0;
53 wake_up_interruptible(&serport->wait);
57 * serport_ldisc_open() is the routine that is called upon setting our line
58 * discipline on a tty. It prepares the serio struct.
61 static int serport_ldisc_open(struct tty_struct *tty)
63 struct serport *serport;
67 serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
68 serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
69 if (unlikely(!serport || !serio)) {
75 memset(serport, 0, sizeof(struct serport));
76 serport->serio = serio;
77 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
79 tty->disc_data = serport;
81 memset(serio, 0, sizeof(struct serio));
82 strlcpy(serio->name, "Serial port", sizeof(serio->name));
83 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
84 serio->type = SERIO_RS232;
85 serio->write = serport_serio_write;
86 serio->close = serport_serio_close;
87 serio->port_data = serport;
89 init_waitqueue_head(&serport->wait);
95 * serport_ldisc_close() is the opposite of serport_ldisc_open()
98 static void serport_ldisc_close(struct tty_struct *tty)
100 struct serport *serport = (struct serport*) tty->disc_data;
105 * serport_ldisc_receive() is called by the low level tty driver when characters
106 * are ready for us. We forward the characters, one by one to the 'interrupt'
109 * FIXME: We should get pt_regs from the tty layer and forward them to
110 * serio_interrupt here.
113 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
115 struct serport *serport = (struct serport*) tty->disc_data;
117 for (i = 0; i < count; i++)
118 serio_interrupt(serport->serio, cp[i], 0, NULL);
122 * serport_ldisc_room() reports how much room we do have for receiving data.
123 * Although we in fact have infinite room, we need to specify some value
124 * here, and 256 seems to be reasonable.
127 static int serport_ldisc_room(struct tty_struct *tty)
133 * serport_ldisc_read() just waits indefinitely if everything goes well.
134 * However, when the serio driver closes the serio port, it finishes,
135 * returning 0 characters.
138 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
140 struct serport *serport = (struct serport*) tty->disc_data;
143 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
146 serio_register_port(serport->serio);
147 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
148 wait_event_interruptible(serport->wait, !serport->serio->type);
149 serio_unregister_port(serport->serio);
151 clear_bit(SERPORT_BUSY, &serport->flags);
157 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
160 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
162 struct serport *serport = (struct serport*) tty->disc_data;
164 if (cmd == SPIOCSTYPE)
165 return get_user(serport->serio->type, (unsigned long __user *) arg);
170 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
172 struct serport *sp = (struct serport *) tty->disc_data;
174 serio_drv_write_wakeup(sp->serio);
178 * The line discipline structure.
181 static struct tty_ldisc serport_ldisc = {
182 .owner = THIS_MODULE,
184 .open = serport_ldisc_open,
185 .close = serport_ldisc_close,
186 .read = serport_ldisc_read,
187 .ioctl = serport_ldisc_ioctl,
188 .receive_buf = serport_ldisc_receive,
189 .receive_room = serport_ldisc_room,
190 .write_wakeup = serport_ldisc_write_wakeup
194 * The functions for insering/removing us as a module.
197 static int __init serport_init(void)
200 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
202 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
207 static void __exit serport_exit(void)
209 tty_register_ldisc(N_MOUSE, NULL);
212 module_init(serport_init);
213 module_exit(serport_exit);