vserver 1.9.3
[linux-2.6.git] / drivers / input / serio / serport.c
1 /*
2  * Input device TTY line discipline
3  *
4  * Copyright (c) 1999-2002 Vojtech Pavlik
5  *
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.
8  */
9
10 /*
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.
14  */
15
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>
23
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);
28
29 #define SERPORT_BUSY    1
30
31 struct serport {
32         struct tty_struct *tty;
33         wait_queue_head_t wait;
34         struct serio *serio;
35         unsigned long flags;
36 };
37
38 /*
39  * Callback functions from the serio code.
40  */
41
42 static int serport_serio_write(struct serio *serio, unsigned char data)
43 {
44         struct serport *serport = serio->port_data;
45         return -(serport->tty->driver->write(serport->tty, 0, &data, 1) != 1);
46 }
47
48 static void serport_serio_close(struct serio *serio)
49 {
50         struct serport *serport = serio->port_data;
51
52         serport->serio->type = 0;
53         wake_up_interruptible(&serport->wait);
54 }
55
56 /*
57  * serport_ldisc_open() is the routine that is called upon setting our line
58  * discipline on a tty. It prepares the serio struct.
59  */
60
61 static int serport_ldisc_open(struct tty_struct *tty)
62 {
63         struct serport *serport;
64         struct serio *serio;
65         char name[64];
66
67         serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
68         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
69         if (unlikely(!serport || !serio)) {
70                 kfree(serport);
71                 kfree(serio);
72                 return -ENOMEM;
73         }
74
75         memset(serport, 0, sizeof(struct serport));
76         serport->serio = serio;
77         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
78         serport->tty = tty;
79         tty->disc_data = serport;
80
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;
88
89         init_waitqueue_head(&serport->wait);
90
91         return 0;
92 }
93
94 /*
95  * serport_ldisc_close() is the opposite of serport_ldisc_open()
96  */
97
98 static void serport_ldisc_close(struct tty_struct *tty)
99 {
100         struct serport *serport = (struct serport*) tty->disc_data;
101         kfree(serport);
102 }
103
104 /*
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'
107  * routine.
108  *
109  * FIXME: We should get pt_regs from the tty layer and forward them to
110  *        serio_interrupt here.
111  */
112
113 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
114 {
115         struct serport *serport = (struct serport*) tty->disc_data;
116         int i;
117         for (i = 0; i < count; i++)
118                 serio_interrupt(serport->serio, cp[i], 0, NULL);
119 }
120
121 /*
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.
125  */
126
127 static int serport_ldisc_room(struct tty_struct *tty)
128 {
129         return 256;
130 }
131
132 /*
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.
136  */
137
138 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
139 {
140         struct serport *serport = (struct serport*) tty->disc_data;
141         char name[64];
142
143         if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
144                 return -EBUSY;
145
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);
150
151         clear_bit(SERPORT_BUSY, &serport->flags);
152
153         return 0;
154 }
155
156 /*
157  * serport_ldisc_ioctl() allows to set the port protocol, and device ID
158  */
159
160 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
161 {
162         struct serport *serport = (struct serport*) tty->disc_data;
163
164         if (cmd == SPIOCSTYPE)
165                 return get_user(serport->serio->type, (unsigned long __user *) arg);
166
167         return -EINVAL;
168 }
169
170 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
171 {
172         struct serport *sp = (struct serport *) tty->disc_data;
173
174         serio_drv_write_wakeup(sp->serio);
175 }
176
177 /*
178  * The line discipline structure.
179  */
180
181 static struct tty_ldisc serport_ldisc = {
182         .owner =        THIS_MODULE,
183         .name =         "input",
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
191 };
192
193 /*
194  * The functions for insering/removing us as a module.
195  */
196
197 static int __init serport_init(void)
198 {
199         int retval;
200         retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
201         if (retval)
202                 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
203
204         return  retval;
205 }
206
207 static void __exit serport_exit(void)
208 {
209         tty_register_ldisc(N_MOUSE, NULL);
210 }
211
212 module_init(serport_init);
213 module_exit(serport_exit);