Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / input / serio / serport.c
blobf8ead9f9c77eaa7737c1cf94cc1a5dc23051a858
1 /*
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.
8 */
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 <linux/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>
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);
31 #define SERPORT_BUSY 1
32 #define SERPORT_ACTIVE 2
33 #define SERPORT_DEAD 3
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;
45 * Callback functions from the serio code.
48 static int serport_serio_write(struct serio *serio, unsigned char data)
50 struct serport *serport = serio->port_data;
51 return -(serport->tty->ops->write(serport->tty, &data, 1) != 1);
54 static int serport_serio_open(struct serio *serio)
56 struct serport *serport = serio->port_data;
57 unsigned long flags;
59 spin_lock_irqsave(&serport->lock, flags);
60 set_bit(SERPORT_ACTIVE, &serport->flags);
61 spin_unlock_irqrestore(&serport->lock, flags);
63 return 0;
67 static void serport_serio_close(struct serio *serio)
69 struct serport *serport = serio->port_data;
70 unsigned long flags;
72 spin_lock_irqsave(&serport->lock, flags);
73 clear_bit(SERPORT_ACTIVE, &serport->flags);
74 spin_unlock_irqrestore(&serport->lock, flags);
78 * serport_ldisc_open() is the routine that is called upon setting our line
79 * discipline on a tty. It prepares the serio struct.
82 static int serport_ldisc_open(struct tty_struct *tty)
84 struct serport *serport;
86 if (!capable(CAP_SYS_ADMIN))
87 return -EPERM;
89 serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
90 if (!serport)
91 return -ENOMEM;
93 serport->tty = tty;
94 spin_lock_init(&serport->lock);
95 init_waitqueue_head(&serport->wait);
97 tty->disc_data = serport;
98 tty->receive_room = 256;
99 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
101 return 0;
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;
112 kfree(serport);
116 * serport_ldisc_receive() is called by the low level tty driver when characters
117 * are ready for us. We forward the characters and flags, one by one to the
118 * 'interrupt' routine.
121 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
123 struct serport *serport = (struct serport*) tty->disc_data;
124 unsigned long flags;
125 unsigned int ch_flags = 0;
126 int i;
128 spin_lock_irqsave(&serport->lock, flags);
130 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
131 goto out;
133 for (i = 0; i < count; i++) {
134 if (fp) {
135 switch (fp[i]) {
136 case TTY_FRAME:
137 ch_flags = SERIO_FRAME;
138 break;
140 case TTY_PARITY:
141 ch_flags = SERIO_PARITY;
142 break;
144 default:
145 ch_flags = 0;
146 break;
150 serio_interrupt(serport->serio, cp[i], ch_flags);
153 out:
154 spin_unlock_irqrestore(&serport->lock, flags);
158 * serport_ldisc_read() just waits indefinitely if everything goes well.
159 * However, when the serio driver closes the serio port, it finishes,
160 * returning 0 characters.
163 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
165 struct serport *serport = (struct serport*) tty->disc_data;
166 struct serio *serio;
168 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
169 return -EBUSY;
171 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
172 if (!serio)
173 return -ENOMEM;
175 strlcpy(serio->name, "Serial port", sizeof(serio->name));
176 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty));
177 serio->id = serport->id;
178 serio->id.type = SERIO_RS232;
179 serio->write = serport_serio_write;
180 serio->open = serport_serio_open;
181 serio->close = serport_serio_close;
182 serio->port_data = serport;
183 serio->dev.parent = tty->dev;
185 serio_register_port(serport->serio);
186 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty));
188 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
189 serio_unregister_port(serport->serio);
190 serport->serio = NULL;
192 clear_bit(SERPORT_DEAD, &serport->flags);
193 clear_bit(SERPORT_BUSY, &serport->flags);
195 return 0;
198 static void serport_set_type(struct tty_struct *tty, unsigned long type)
200 struct serport *serport = tty->disc_data;
202 serport->id.proto = type & 0x000000ff;
203 serport->id.id = (type & 0x0000ff00) >> 8;
204 serport->id.extra = (type & 0x00ff0000) >> 16;
208 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
211 static int serport_ldisc_ioctl(struct tty_struct *tty, struct file *file,
212 unsigned int cmd, unsigned long arg)
214 if (cmd == SPIOCSTYPE) {
215 unsigned long type;
217 if (get_user(type, (unsigned long __user *) arg))
218 return -EFAULT;
220 serport_set_type(tty, type);
221 return 0;
224 return -EINVAL;
227 #ifdef CONFIG_COMPAT
228 #define COMPAT_SPIOCSTYPE _IOW('q', 0x01, compat_ulong_t)
229 static long serport_ldisc_compat_ioctl(struct tty_struct *tty,
230 struct file *file,
231 unsigned int cmd, unsigned long arg)
233 if (cmd == COMPAT_SPIOCSTYPE) {
234 void __user *uarg = compat_ptr(arg);
235 compat_ulong_t compat_type;
237 if (get_user(compat_type, (compat_ulong_t __user *)uarg))
238 return -EFAULT;
240 serport_set_type(tty, compat_type);
241 return 0;
244 return -EINVAL;
246 #endif
248 static int serport_ldisc_hangup(struct tty_struct *tty)
250 struct serport *serport = (struct serport *) tty->disc_data;
251 unsigned long flags;
253 spin_lock_irqsave(&serport->lock, flags);
254 set_bit(SERPORT_DEAD, &serport->flags);
255 spin_unlock_irqrestore(&serport->lock, flags);
257 wake_up_interruptible(&serport->wait);
258 return 0;
261 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
263 struct serport *serport = (struct serport *) tty->disc_data;
264 unsigned long flags;
266 spin_lock_irqsave(&serport->lock, flags);
267 if (test_bit(SERPORT_ACTIVE, &serport->flags))
268 serio_drv_write_wakeup(serport->serio);
269 spin_unlock_irqrestore(&serport->lock, flags);
273 * The line discipline structure.
276 static struct tty_ldisc_ops serport_ldisc = {
277 .owner = THIS_MODULE,
278 .name = "input",
279 .open = serport_ldisc_open,
280 .close = serport_ldisc_close,
281 .read = serport_ldisc_read,
282 .ioctl = serport_ldisc_ioctl,
283 #ifdef CONFIG_COMPAT
284 .compat_ioctl = serport_ldisc_compat_ioctl,
285 #endif
286 .receive_buf = serport_ldisc_receive,
287 .hangup = serport_ldisc_hangup,
288 .write_wakeup = serport_ldisc_write_wakeup
292 * The functions for insering/removing us as a module.
295 static int __init serport_init(void)
297 int retval;
298 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
299 if (retval)
300 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
302 return retval;
305 static void __exit serport_exit(void)
307 tty_unregister_ldisc(N_MOUSE);
310 module_init(serport_init);
311 module_exit(serport_exit);