Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / input / serio / serport.c
blob54a680cc704d29bebdf3135f75d02c2069f2f1cf
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 <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
30 #define SERPORT_ACTIVE 2
31 #define SERPORT_DEAD 3
33 struct serport {
34 struct tty_struct *tty;
35 wait_queue_head_t wait;
36 struct serio *serio;
37 struct serio_device_id id;
38 spinlock_t lock;
39 unsigned long flags;
43 * Callback functions from the serio code.
46 static int serport_serio_write(struct serio *serio, unsigned char data)
48 struct serport *serport = serio->port_data;
49 return -(serport->tty->driver->write(serport->tty, &data, 1) != 1);
52 static int serport_serio_open(struct serio *serio)
54 struct serport *serport = serio->port_data;
55 unsigned long flags;
57 spin_lock_irqsave(&serport->lock, flags);
58 set_bit(SERPORT_ACTIVE, &serport->flags);
59 spin_unlock_irqrestore(&serport->lock, flags);
61 return 0;
65 static void serport_serio_close(struct serio *serio)
67 struct serport *serport = serio->port_data;
68 unsigned long flags;
70 spin_lock_irqsave(&serport->lock, flags);
71 clear_bit(SERPORT_ACTIVE, &serport->flags);
72 set_bit(SERPORT_DEAD, &serport->flags);
73 spin_unlock_irqrestore(&serport->lock, flags);
75 wake_up_interruptible(&serport->wait);
79 * serport_ldisc_open() is the routine that is called upon setting our line
80 * discipline on a tty. It prepares the serio struct.
83 static int serport_ldisc_open(struct tty_struct *tty)
85 struct serport *serport;
87 if (!capable(CAP_SYS_ADMIN))
88 return -EPERM;
90 serport = kzalloc(sizeof(struct serport), GFP_KERNEL);
91 if (!serport)
92 return -ENOMEM;
94 serport->tty = tty;
95 spin_lock_init(&serport->lock);
96 init_waitqueue_head(&serport->wait);
98 tty->disc_data = serport;
99 tty->receive_room = 256;
100 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
102 return 0;
106 * serport_ldisc_close() is the opposite of serport_ldisc_open()
109 static void serport_ldisc_close(struct tty_struct *tty)
111 struct serport *serport = (struct serport *) tty->disc_data;
113 kfree(serport);
117 * serport_ldisc_receive() is called by the low level tty driver when characters
118 * are ready for us. We forward the characters, one by one to the 'interrupt'
119 * routine.
121 * FIXME: We should get pt_regs from the tty layer and forward them to
122 * serio_interrupt here.
125 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
127 struct serport *serport = (struct serport*) tty->disc_data;
128 unsigned long flags;
129 int i;
131 spin_lock_irqsave(&serport->lock, flags);
133 if (!test_bit(SERPORT_ACTIVE, &serport->flags))
134 goto out;
136 for (i = 0; i < count; i++)
137 serio_interrupt(serport->serio, cp[i], 0, NULL);
139 out:
140 spin_unlock_irqrestore(&serport->lock, flags);
144 * serport_ldisc_read() just waits indefinitely if everything goes well.
145 * However, when the serio driver closes the serio port, it finishes,
146 * returning 0 characters.
149 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
151 struct serport *serport = (struct serport*) tty->disc_data;
152 struct serio *serio;
153 char name[64];
155 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
156 return -EBUSY;
158 serport->serio = serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
159 if (!serio)
160 return -ENOMEM;
162 strlcpy(serio->name, "Serial port", sizeof(serio->name));
163 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
164 serio->id = serport->id;
165 serio->id.type = SERIO_RS232;
166 serio->write = serport_serio_write;
167 serio->open = serport_serio_open;
168 serio->close = serport_serio_close;
169 serio->port_data = serport;
171 serio_register_port(serport->serio);
172 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
174 wait_event_interruptible(serport->wait, test_bit(SERPORT_DEAD, &serport->flags));
175 serio_unregister_port(serport->serio);
176 serport->serio = NULL;
178 clear_bit(SERPORT_DEAD, &serport->flags);
179 clear_bit(SERPORT_BUSY, &serport->flags);
181 return 0;
185 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
188 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
190 struct serport *serport = (struct serport*) tty->disc_data;
191 unsigned long type;
193 if (cmd == SPIOCSTYPE) {
194 if (get_user(type, (unsigned long __user *) arg))
195 return -EFAULT;
197 serport->id.proto = type & 0x000000ff;
198 serport->id.id = (type & 0x0000ff00) >> 8;
199 serport->id.extra = (type & 0x00ff0000) >> 16;
201 return 0;
204 return -EINVAL;
207 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
209 struct serport *serport = (struct serport *) tty->disc_data;
210 unsigned long flags;
212 spin_lock_irqsave(&serport->lock, flags);
213 if (test_bit(SERPORT_ACTIVE, &serport->flags))
214 serio_drv_write_wakeup(serport->serio);
215 spin_unlock_irqrestore(&serport->lock, flags);
219 * The line discipline structure.
222 static struct tty_ldisc serport_ldisc = {
223 .owner = THIS_MODULE,
224 .name = "input",
225 .open = serport_ldisc_open,
226 .close = serport_ldisc_close,
227 .read = serport_ldisc_read,
228 .ioctl = serport_ldisc_ioctl,
229 .receive_buf = serport_ldisc_receive,
230 .write_wakeup = serport_ldisc_write_wakeup
234 * The functions for insering/removing us as a module.
237 static int __init serport_init(void)
239 int retval;
240 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
241 if (retval)
242 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
244 return retval;
247 static void __exit serport_exit(void)
249 tty_unregister_ldisc(N_MOUSE);
252 module_init(serport_init);
253 module_exit(serport_exit);