Input: serport - add compat handling for SPIOCSTYPE ioctl
[pandora-kernel.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/sched.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/serio.h>
23 #include <linux/tty.h>
24 #include <linux/compat.h>
25
26 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
27 MODULE_DESCRIPTION("Input device TTY line discipline");
28 MODULE_LICENSE("GPL");
29 MODULE_ALIAS_LDISC(N_MOUSE);
30
31 #define SERPORT_BUSY    1
32 #define SERPORT_ACTIVE  2
33 #define SERPORT_DEAD    3
34
35 struct serport {
36         struct tty_struct *tty;
37         wait_queue_head_t wait;
38         struct serio *serio;
39         struct serio_device_id id;
40         spinlock_t lock;
41         unsigned long flags;
42 };
43
44 /*
45  * Callback functions from the serio code.
46  */
47
48 static int serport_serio_write(struct serio *serio, unsigned char data)
49 {
50         struct serport *serport = serio->port_data;
51         return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
52 }
53
54 static int serport_serio_open(struct serio *serio)
55 {
56         struct serport *serport = serio->port_data;
57         unsigned long flags;
58
59         spin_lock_irqsave(&serport->lock, flags);
60         set_bit(SERPORT_ACTIVE, &serport->flags);
61         spin_unlock_irqrestore(&serport->lock, flags);
62
63         return 0;
64 }
65
66
67 static void serport_serio_close(struct serio *serio)
68 {
69         struct serport *serport = serio->port_data;
70         unsigned long flags;
71
72         spin_lock_irqsave(&serport->lock, flags);
73         clear_bit(SERPORT_ACTIVE, &serport->flags);
74         set_bit(SERPORT_DEAD, &serport->flags);
75         spin_unlock_irqrestore(&serport->lock, flags);
76
77         wake_up_interruptible(&serport->wait);
78 }
79
80 /*
81  * serport_ldisc_open() is the routine that is called upon setting our line
82  * discipline on a tty. It prepares the serio struct.
83  */
84
85 static int serport_ldisc_open(struct tty_struct *tty)
86 {
87         struct serport *serport;
88
89         if (!capable(CAP_SYS_ADMIN))
90                 return -EPERM;
91
92         serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
93         if (!serport)
94                 return -ENOMEM;
95
96         serport->tty = tty;
97         spin_lock_init(&serport->lock);
98         init_waitqueue_head(&serport->wait);
99
100         tty->disc_data = serport;
101         tty->receive_room = 256;
102         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
103
104         return 0;
105 }
106
107 /*
108  * serport_ldisc_close() is the opposite of serport_ldisc_open()
109  */
110
111 static void serport_ldisc_close(struct tty_struct *tty)
112 {
113         struct serport *serport = (struct serport *) tty->disc_data;
114
115         kfree(serport);
116 }
117
118 /*
119  * serport_ldisc_receive() is called by the low level tty driver when characters
120  * are ready for us. We forward the characters and flags, one by one to the
121  * 'interrupt' routine.
122  */
123
124 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
125 {
126         struct serport *serport = (struct serport*) tty->disc_data;
127         unsigned long flags;
128         unsigned int ch_flags;
129         int i;
130
131         spin_lock_irqsave(&serport->lock, flags);
132
133         if (!test_bit(SERPORT_ACTIVE, &serport->flags))
134                 goto out;
135
136         for (i = 0; i < count; i++) {
137                 switch (fp[i]) {
138                 case TTY_FRAME:
139                         ch_flags = SERIO_FRAME;
140                         break;
141
142                 case TTY_PARITY:
143                         ch_flags = SERIO_PARITY;
144                         break;
145
146                 default:
147                         ch_flags = 0;
148                         break;
149                 }
150
151                 serio_interrupt(serport->serio, cp[i], ch_flags);
152         }
153
154 out:
155         spin_unlock_irqrestore(&serport->lock, flags);
156 }
157
158 /*
159  * serport_ldisc_read() just waits indefinitely if everything goes well.
160  * However, when the serio driver closes the serio port, it finishes,
161  * returning 0 characters.
162  */
163
164 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
165 {
166         struct serport *serport = (struct serport*) tty->disc_data;
167         struct serio *serio;
168         char name[64];
169
170         if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
171                 return -EBUSY;
172
173         serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
174         if (!serio)
175                 return -ENOMEM;
176
177         strlcpy(serio->name, "Serial port", sizeof(serio->name));
178         snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
179         serio->id = serport->id;
180         serio->id.type = SERIO_RS232;
181         serio->write = serport_serio_write;
182         serio->open = serport_serio_open;
183         serio->close = serport_serio_close;
184         serio->port_data = serport;
185         serio->dev.parent = tty->dev;
186
187         serio_register_port(serport->serio);
188         printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
189
190         wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
191         serio_unregister_port(serport->serio);
192         serport->serio = NULL;
193
194         clear_bit(SERPORT_DEAD, &serport->flags);
195         clear_bit(SERPORT_BUSY, &serport->flags);
196
197         return 0;
198 }
199
200 static void serport_set_type(struct tty_struct *tty, unsigned long type)
201 {
202         struct serport *serport = tty->disc_data;
203
204         serport->id.proto = type & 0x000000ff;
205         serport->id.id    = (type & 0x0000ff00) >> 8;
206         serport->id.extra = (type & 0x00ff0000) >> 16;
207 }
208
209 /*
210  * serport_ldisc_ioctl() allows to set the port protocol, and device ID
211  */
212
213 static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
214                                unsigned int cmd, unsigned long arg)
215 {
216         if (cmd == SPIOCSTYPE) {
217                 unsigned long type;
218
219                 if (get_user(type, (unsigned long __user *) arg))
220                         return -EFAULT;
221
222                 serport_set_type(tty, type);
223                 return 0;
224         }
225
226         return -EINVAL;
227 }
228
229 #ifdef CONFIG_COMPAT
230 #define COMPAT_SPIOCSTYPE       _IOW('q', 0x01, compat_ulong_t)
231 static long serport_ldisc_compat_ioctl(struct tty_struct *tty,
232                                        struct file *file,
233                                        unsigned int cmd, unsigned long arg)
234 {
235         if (cmd == COMPAT_SPIOCSTYPE) {
236                 void __user *uarg = compat_ptr(arg);
237                 compat_ulong_t compat_type;
238
239                 if (get_user(compat_type, (compat_ulong_t __user *)uarg))
240                         return -EFAULT;
241
242                 serport_set_type(tty, compat_type);
243                 return 0;
244         }
245
246         return -EINVAL;
247 }
248 #endif
249
250 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
251 {
252         struct serport *serport = (struct serport *) tty->disc_data;
253         unsigned long flags;
254
255         spin_lock_irqsave(&serport->lock, flags);
256         if (test_bit(SERPORT_ACTIVE, &serport->flags))
257                 serio_drv_write_wakeup(serport->serio);
258         spin_unlock_irqrestore(&serport->lock, flags);
259 }
260
261 /*
262  * The line discipline structure.
263  */
264
265 static struct tty_ldisc_ops serport_ldisc = {
266         .owner =        THIS_MODULE,
267         .name =         "input",
268         .open =         serport_ldisc_open,
269         .close =        serport_ldisc_close,
270         .read =         serport_ldisc_read,
271         .ioctl =        serport_ldisc_ioctl,
272 #ifdef CONFIG_COMPAT
273         .compat_ioctl = serport_ldisc_compat_ioctl,
274 #endif
275         .receive_buf =  serport_ldisc_receive,
276         .write_wakeup = serport_ldisc_write_wakeup
277 };
278
279 /*
280  * The functions for insering/removing us as a module.
281  */
282
283 static int __init serport_init(void)
284 {
285         int retval;
286         retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
287         if (retval)
288                 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
289
290         return  retval;
291 }
292
293 static void __exit serport_exit(void)
294 {
295         tty_unregister_ldisc(N_MOUSE);
296 }
297
298 module_init(serport_init);
299 module_exit(serport_exit);