Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / serio / serport.c
blob22f73683952bffaa99155e16f8a3bc666bb3e770
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
31 struct serport {
32 struct tty_struct *tty;
33 wait_queue_head_t wait;
34 struct serio *serio;
35 unsigned long flags;
39 * Callback functions from the serio code.
42 static int serport_serio_write(struct serio *serio, unsigned char data)
44 struct serport *serport = serio->port_data;
45 return -(serport->tty->driver->write(serport->tty, &data, 1) != 1);
48 static void serport_serio_close(struct serio *serio)
50 struct serport *serport = serio->port_data;
52 serport->serio->id.type = 0;
53 wake_up_interruptible(&serport->wait);
57 * serport_ldisc_open() is the routine that is called upon setting our line
58 * discipline on a tty. It prepares the serio struct.
61 static int serport_ldisc_open(struct tty_struct *tty)
63 struct serport *serport;
64 struct serio *serio;
65 char name[64];
67 if (!capable(CAP_SYS_ADMIN))
68 return -EPERM;
70 serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
71 serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
72 if (unlikely(!serport || !serio)) {
73 kfree(serport);
74 kfree(serio);
75 return -ENOMEM;
78 memset(serport, 0, sizeof(struct serport));
79 serport->serio = serio;
80 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
81 serport->tty = tty;
82 tty->disc_data = serport;
84 memset(serio, 0, sizeof(struct serio));
85 strlcpy(serio->name, "Serial port", sizeof(serio->name));
86 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", tty_name(tty, name));
87 serio->id.type = SERIO_RS232;
88 serio->write = serport_serio_write;
89 serio->close = serport_serio_close;
90 serio->port_data = serport;
92 init_waitqueue_head(&serport->wait);
94 return 0;
98 * serport_ldisc_close() is the opposite of serport_ldisc_open()
101 static void serport_ldisc_close(struct tty_struct *tty)
103 struct serport *serport = (struct serport*) tty->disc_data;
104 kfree(serport);
108 * serport_ldisc_receive() is called by the low level tty driver when characters
109 * are ready for us. We forward the characters, one by one to the 'interrupt'
110 * routine.
112 * FIXME: We should get pt_regs from the tty layer and forward them to
113 * serio_interrupt here.
116 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
118 struct serport *serport = (struct serport*) tty->disc_data;
119 int i;
120 for (i = 0; i < count; i++)
121 serio_interrupt(serport->serio, cp[i], 0, NULL);
125 * serport_ldisc_room() reports how much room we do have for receiving data.
126 * Although we in fact have infinite room, we need to specify some value
127 * here, and 256 seems to be reasonable.
130 static int serport_ldisc_room(struct tty_struct *tty)
132 return 256;
136 * serport_ldisc_read() just waits indefinitely if everything goes well.
137 * However, when the serio driver closes the serio port, it finishes,
138 * returning 0 characters.
141 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
143 struct serport *serport = (struct serport*) tty->disc_data;
144 char name[64];
146 if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
147 return -EBUSY;
149 serio_register_port(serport->serio);
150 printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
151 wait_event_interruptible(serport->wait, !serport->serio->id.type);
152 serio_unregister_port(serport->serio);
154 clear_bit(SERPORT_BUSY, &serport->flags);
156 return 0;
160 * serport_ldisc_ioctl() allows to set the port protocol, and device ID
163 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
165 struct serport *serport = (struct serport*) tty->disc_data;
166 struct serio *serio = serport->serio;
167 unsigned long type;
169 if (cmd == SPIOCSTYPE) {
170 if (get_user(type, (unsigned long __user *) arg))
171 return -EFAULT;
173 serio->id.proto = type & 0x000000ff;
174 serio->id.id = (type & 0x0000ff00) >> 8;
175 serio->id.extra = (type & 0x00ff0000) >> 16;
177 return 0;
180 return -EINVAL;
183 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
185 struct serport *sp = (struct serport *) tty->disc_data;
187 serio_drv_write_wakeup(sp->serio);
191 * The line discipline structure.
194 static struct tty_ldisc serport_ldisc = {
195 .owner = THIS_MODULE,
196 .name = "input",
197 .open = serport_ldisc_open,
198 .close = serport_ldisc_close,
199 .read = serport_ldisc_read,
200 .ioctl = serport_ldisc_ioctl,
201 .receive_buf = serport_ldisc_receive,
202 .receive_room = serport_ldisc_room,
203 .write_wakeup = serport_ldisc_write_wakeup
207 * The functions for insering/removing us as a module.
210 static int __init serport_init(void)
212 int retval;
213 retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
214 if (retval)
215 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
217 return retval;
220 static void __exit serport_exit(void)
222 tty_register_ldisc(N_MOUSE, NULL);
225 module_init(serport_init);
226 module_exit(serport_exit);