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