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 int serport_serio_open(struct serio *serio)
56 static void serport_serio_close(struct serio *serio)
58 struct serport *serport = serio->driver;
60 serport->serio.type = 0;
61 wake_up_interruptible(&serport->wait);
65 * serport_ldisc_open() is the routine that is called upon setting our line
66 * discipline on a tty. It prepares the serio struct.
69 static int serport_ldisc_open(struct tty_struct *tty)
71 struct serport *serport;
74 serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
75 if (unlikely(!serport))
77 memset(serport, 0, sizeof(struct serport));
79 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
81 tty->disc_data = serport;
83 snprintf(serport->phys, sizeof(serport->phys), "%s/serio0", tty_name(tty, name));
85 serport->serio.name = serport_name;
86 serport->serio.phys = serport->phys;
88 serport->serio.type = SERIO_RS232;
89 serport->serio.write = serport_serio_write;
90 serport->serio.open = serport_serio_open;
91 serport->serio.close = serport_serio_close;
92 serport->serio.driver = serport;
94 init_waitqueue_head(&serport->wait);
100 * serport_ldisc_close() is the opposite of serport_ldisc_open()
103 static void serport_ldisc_close(struct tty_struct *tty)
105 struct serport *serport = (struct serport*) tty->disc_data;
110 * serport_ldisc_receive() is called by the low level tty driver when characters
111 * are ready for us. We forward the characters, one by one to the 'interrupt'
114 * FIXME: We should get pt_regs from the tty layer and forward them to
115 * serio_interrupt here.
118 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
120 struct serport *serport = (struct serport*) tty->disc_data;
122 for (i = 0; i < count; i++)
123 serio_interrupt(&serport->serio, cp[i], 0, NULL);
127 * serport_ldisc_room() reports how much room we do have for receiving data.
128 * Although we in fact have infinite room, we need to specify some value
129 * here, and 256 seems to be reasonable.
132 static int serport_ldisc_room(struct tty_struct *tty)
138 * serport_ldisc_read() just waits indefinitely if everything goes well.
139 * However, when the serio driver closes the serio port, it finishes,
140 * returning 0 characters.
143 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char * buf, size_t nr)
145 struct serport *serport = (struct serport*) tty->disc_data;
148 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
151 serio_register_port(&serport->serio);
152 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
153 wait_event_interruptible(serport->wait, !serport->serio.type);
154 serio_unregister_port(&serport->serio);
156 clear_bit(SERPORT_BUSY, &serport->flags);
162 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
165 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
167 struct serport *serport = (struct serport*) tty->disc_data;
169 if (cmd == SPIOCSTYPE)
170 return get_user(serport->serio.type, (unsigned long *) arg);
175 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
177 struct serport *sp = (struct serport *) tty->disc_data;
179 serio_dev_write_wakeup(&sp->serio);
183 * The line discipline structure.
186 static struct tty_ldisc serport_ldisc = {
187 .owner = THIS_MODULE,
189 .open = serport_ldisc_open,
190 .close = serport_ldisc_close,
191 .read = serport_ldisc_read,
192 .ioctl = serport_ldisc_ioctl,
193 .receive_buf = serport_ldisc_receive,
194 .receive_room = serport_ldisc_room,
195 .write_wakeup = serport_ldisc_write_wakeup
199 * The functions for insering/removing us as a module.
202 static int __init serport_init(void)
205 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
207 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
212 static void __exit serport_exit(void)
214 tty_register_ldisc(N_MOUSE, NULL);
217 module_init(serport_init);
218 module_exit(serport_exit);