patch-2_6_7-vs1_9_1_12
[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         char phys[32];
37 };
38
39 char serport_name[] = "Serial port";
40
41 /*
42  * Callback functions from the serio code.
43  */
44
45 static int serport_serio_write(struct serio *serio, unsigned char data)
46 {
47         struct serport *serport = serio->driver;
48         return -(serport->tty->driver->write(serport->tty, 0, &data, 1) != 1);
49 }
50
51 static void serport_serio_close(struct serio *serio)
52 {
53         struct serport *serport = serio->driver;
54
55         serport->serio.type = 0;
56         wake_up_interruptible(&serport->wait);
57 }
58
59 /*
60  * serport_ldisc_open() is the routine that is called upon setting our line
61  * discipline on a tty. It prepares the serio struct.
62  */
63
64 static int serport_ldisc_open(struct tty_struct *tty)
65 {
66         struct serport *serport;
67         char name[64];
68
69         serport = kmalloc(sizeof(struct serport), GFP_KERNEL);
70         if (unlikely(!serport))
71                 return -ENOMEM;
72         memset(serport, 0, sizeof(struct serport));
73
74         set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
75         serport->tty = tty;
76         tty->disc_data = serport;
77
78         snprintf(serport->phys, sizeof(serport->phys), "%s/serio0", tty_name(tty, name));
79
80         serport->serio.name = serport_name;
81         serport->serio.phys = serport->phys;
82
83         serport->serio.type = SERIO_RS232;
84         serport->serio.write = serport_serio_write;
85         serport->serio.close = serport_serio_close;
86         serport->serio.driver = serport;
87
88         init_waitqueue_head(&serport->wait);
89
90         return 0;
91 }
92
93 /*
94  * serport_ldisc_close() is the opposite of serport_ldisc_open()
95  */
96
97 static void serport_ldisc_close(struct tty_struct *tty)
98 {
99         struct serport *serport = (struct serport*) tty->disc_data;
100         kfree(serport);
101 }
102
103 /*
104  * serport_ldisc_receive() is called by the low level tty driver when characters
105  * are ready for us. We forward the characters, one by one to the 'interrupt'
106  * routine.
107  *
108  * FIXME: We should get pt_regs from the tty layer and forward them to
109  *        serio_interrupt here.
110  */
111
112 static void serport_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
113 {
114         struct serport *serport = (struct serport*) tty->disc_data;
115         int i;
116         for (i = 0; i < count; i++)
117                 serio_interrupt(&serport->serio, cp[i], 0, NULL);
118 }
119
120 /*
121  * serport_ldisc_room() reports how much room we do have for receiving data.
122  * Although we in fact have infinite room, we need to specify some value
123  * here, and 256 seems to be reasonable.
124  */
125
126 static int serport_ldisc_room(struct tty_struct *tty)
127 {
128         return 256;
129 }
130
131 /*
132  * serport_ldisc_read() just waits indefinitely if everything goes well.
133  * However, when the serio driver closes the serio port, it finishes,
134  * returning 0 characters.
135  */
136
137 static ssize_t serport_ldisc_read(struct tty_struct * tty, struct file * file, unsigned char __user * buf, size_t nr)
138 {
139         struct serport *serport = (struct serport*) tty->disc_data;
140         char name[64];
141
142         if (test_and_set_bit(SERPORT_BUSY, &serport->flags))
143                 return -EBUSY;
144
145         serio_register_port(&serport->serio);
146         printk(KERN_INFO "serio: Serial port %s\n", tty_name(tty, name));
147         wait_event_interruptible(serport->wait, !serport->serio.type);
148         serio_unregister_port(&serport->serio);
149
150         clear_bit(SERPORT_BUSY, &serport->flags);
151
152         return 0;
153 }
154
155 /*
156  * serport_ldisc_ioctl() allows to set the port protocol, and device ID
157  */
158
159 static int serport_ldisc_ioctl(struct tty_struct * tty, struct file * file, unsigned int cmd, unsigned long arg)
160 {
161         struct serport *serport = (struct serport*) tty->disc_data;
162
163         if (cmd == SPIOCSTYPE)
164                 return get_user(serport->serio.type, (unsigned long __user *) arg);
165
166         return -EINVAL;
167 }
168
169 static void serport_ldisc_write_wakeup(struct tty_struct * tty)
170 {
171         struct serport *sp = (struct serport *) tty->disc_data;
172
173         serio_dev_write_wakeup(&sp->serio);
174 }
175
176 /*
177  * The line discipline structure.
178  */
179
180 static struct tty_ldisc serport_ldisc = {
181         .owner =        THIS_MODULE,
182         .name =         "input",
183         .open =         serport_ldisc_open,
184         .close =        serport_ldisc_close,
185         .read =         serport_ldisc_read,
186         .ioctl =        serport_ldisc_ioctl,
187         .receive_buf =  serport_ldisc_receive,
188         .receive_room = serport_ldisc_room,
189         .write_wakeup = serport_ldisc_write_wakeup
190 };
191
192 /*
193  * The functions for insering/removing us as a module.
194  */
195
196 static int __init serport_init(void)
197 {
198         int retval;
199         retval = tty_register_ldisc(N_MOUSE, &serport_ldisc);
200         if (retval)
201                 printk(KERN_ERR "serport.c: Error registering line discipline.\n");
202
203         return  retval;
204 }
205
206 static void __exit serport_exit(void)
207 {
208         tty_register_ldisc(N_MOUSE, NULL);
209 }
210
211 module_init(serport_init);
212 module_exit(serport_exit);