vserver 1.9.5.x5
[linux-2.6.git] / drivers / input / serio / parkbd.c
1 /*
2  * $Id: parkbd.c,v 1.10 2002/03/13 10:09:20 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  */
6
7 /*
8  *  Parallel port to Keyboard port adapter driver for Linux
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <linux/module.h>
32 #include <linux/parport.h>
33 #include <linux/init.h>
34 #include <linux/serio.h>
35
36 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
37 MODULE_DESCRIPTION("Parallel port to Keyboard port adapter driver");
38 MODULE_LICENSE("GPL");
39
40 static unsigned int parkbd_pp_no;
41 module_param_named(port, parkbd_pp_no, int, 0);
42 MODULE_PARM_DESC(port, "Parallel port the adapter is connected to (default is 0)");
43
44 static unsigned int parkbd_mode = SERIO_8042;
45 module_param_named(mode, parkbd_mode, uint, 0);
46 MODULE_PARM_DESC(mode, "Mode of operation: XT = 0/AT = 1 (default)");
47
48 #define PARKBD_CLOCK    0x01    /* Strobe & Ack */
49 #define PARKBD_DATA     0x02    /* AutoFd & Busy */
50
51 static int parkbd_buffer;
52 static int parkbd_counter;
53 static unsigned long parkbd_last;
54 static int parkbd_writing;
55 static unsigned long parkbd_start;
56
57 static struct pardevice *parkbd_dev;
58 static struct serio *parkbd_port;
59
60 static int parkbd_readlines(void)
61 {
62         return (parport_read_status(parkbd_dev->port) >> 6) ^ 2;
63 }
64
65 static void parkbd_writelines(int data)
66 {
67         parport_write_control(parkbd_dev->port, (~data & 3) | 0x10);
68 }
69
70 static int parkbd_write(struct serio *port, unsigned char c)
71 {
72         unsigned char p;
73
74         if (!parkbd_mode) return -1;
75
76         p = c ^ (c >> 4);
77         p = p ^ (p >> 2);
78         p = p ^ (p >> 1);
79
80         parkbd_counter = 0;
81         parkbd_writing = 1;
82         parkbd_buffer = c | (((int) (~p & 1)) << 8) | 0x600;
83
84         parkbd_writelines(2);
85
86         return 0;
87 }
88
89 static void parkbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
90 {
91
92         if (parkbd_writing) {
93
94                 if (parkbd_counter && ((parkbd_counter == 11) || time_after(jiffies, parkbd_last + HZ/100))) {
95                         parkbd_counter = 0;
96                         parkbd_buffer = 0;
97                         parkbd_writing = 0;
98                         parkbd_writelines(3);
99                         return;
100                 }
101
102                 parkbd_writelines(((parkbd_buffer >> parkbd_counter++) & 1) | 2);
103
104                 if (parkbd_counter == 11) {
105                         parkbd_counter = 0;
106                         parkbd_buffer = 0;
107                         parkbd_writing = 0;
108                         parkbd_writelines(3);
109                 }
110
111         } else {
112
113                 if ((parkbd_counter == parkbd_mode + 10) || time_after(jiffies, parkbd_last + HZ/100)) {
114                         parkbd_counter = 0;
115                         parkbd_buffer = 0;
116                 }
117
118                 parkbd_buffer |= (parkbd_readlines() >> 1) << parkbd_counter++;
119
120                 if (parkbd_counter == parkbd_mode + 10)
121                         serio_interrupt(parkbd_port, (parkbd_buffer >> (2 - parkbd_mode)) & 0xff, 0, regs);
122         }
123
124         parkbd_last = jiffies;
125 }
126
127 static int parkbd_getport(void)
128 {
129         struct parport *pp;
130
131         pp = parport_find_number(parkbd_pp_no);
132
133         if (pp == NULL) {
134                 printk(KERN_ERR "parkbd: no such parport\n");
135                 return -ENODEV;
136         }
137
138         parkbd_dev = parport_register_device(pp, "parkbd", NULL, NULL, parkbd_interrupt, PARPORT_DEV_EXCL, NULL);
139         parport_put_port(pp);
140
141         if (!parkbd_dev)
142                 return -ENODEV;
143
144         if (parport_claim(parkbd_dev)) {
145                 parport_unregister_device(parkbd_dev);
146                 return -EBUSY;
147         }
148
149         parkbd_start = jiffies;
150
151         return 0;
152 }
153
154 static struct serio * __init parkbd_allocate_serio(void)
155 {
156         struct serio *serio;
157
158         serio = kmalloc(sizeof(struct serio), GFP_KERNEL);
159         if (serio) {
160                 memset(serio, 0, sizeof(struct serio));
161                 serio->type = parkbd_mode;
162                 serio->write = parkbd_write,
163                 strlcpy(serio->name, "PARKBD AT/XT keyboard adapter", sizeof(serio->name));
164                 snprintf(serio->phys, sizeof(serio->phys), "%s/serio0", parkbd_dev->port->name);
165         }
166
167         return serio;
168 }
169
170 int __init parkbd_init(void)
171 {
172         int err;
173
174         err = parkbd_getport();
175         if (err)
176                 return err;
177
178         parkbd_port = parkbd_allocate_serio();
179         if (!parkbd_port) {
180                 parport_release(parkbd_dev);
181                 return -ENOMEM;
182         }
183
184         parkbd_writelines(3);
185
186         serio_register_port(parkbd_port);
187
188         printk(KERN_INFO "serio: PARKBD %s adapter on %s\n",
189                         parkbd_mode ? "AT" : "XT", parkbd_dev->port->name);
190
191         return 0;
192 }
193
194 void __exit parkbd_exit(void)
195 {
196         parport_release(parkbd_dev);
197         serio_unregister_port(parkbd_port);
198         parport_unregister_device(parkbd_dev);
199 }
200
201 module_init(parkbd_init);
202 module_exit(parkbd_exit);