patch-2_6_7-vs1_9_1_12
[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 MODULE_PARM(parkbd, "1i");
41 MODULE_PARM(parkbd_mode, "1i");
42
43 #define PARKBD_CLOCK    0x01    /* Strobe & Ack */
44 #define PARKBD_DATA     0x02    /* AutoFd & Busy */
45
46 static int parkbd;
47 static int parkbd_mode = SERIO_8042;
48
49 static int parkbd_buffer;
50 static int parkbd_counter;
51 static unsigned long parkbd_last;
52 static int parkbd_writing;
53 static unsigned long parkbd_start;
54
55 static struct pardevice *parkbd_dev;
56
57 static char parkbd_name[] = "PARKBD AT/XT keyboard adapter";
58 static char parkbd_phys[32];
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 struct serio parkbd_port =
90 {
91         .write  = parkbd_write,
92         .name   = parkbd_name,
93         .phys   = parkbd_phys,
94 };
95
96 static void parkbd_interrupt(int irq, void *dev_id, struct pt_regs *regs)
97 {
98
99         if (parkbd_writing) {
100
101                 if (parkbd_counter && ((parkbd_counter == 11) || time_after(jiffies, parkbd_last + HZ/100))) {
102                         parkbd_counter = 0;
103                         parkbd_buffer = 0;
104                         parkbd_writing = 0;
105                         parkbd_writelines(3);
106                         return;
107                 }
108
109                 parkbd_writelines(((parkbd_buffer >> parkbd_counter++) & 1) | 2);
110
111                 if (parkbd_counter == 11) {
112                         parkbd_counter = 0;
113                         parkbd_buffer = 0;
114                         parkbd_writing = 0;
115                         parkbd_writelines(3);
116                 }
117
118         } else {
119
120                 if ((parkbd_counter == parkbd_mode + 10) || time_after(jiffies, parkbd_last + HZ/100)) {
121                         parkbd_counter = 0;
122                         parkbd_buffer = 0;
123                 }
124
125                 parkbd_buffer |= (parkbd_readlines() >> 1) << parkbd_counter++;
126
127                 if (parkbd_counter == parkbd_mode + 10)
128                         serio_interrupt(&parkbd_port, (parkbd_buffer >> (2 - parkbd_mode)) & 0xff, 0, regs);
129         }
130
131         parkbd_last = jiffies;
132 }
133
134 static int parkbd_getport(void)
135 {
136         struct parport *pp;
137
138         if (parkbd < 0) {
139                 printk(KERN_ERR "parkbd: no port specified\n");
140                 return -ENODEV;
141         }
142
143         pp = parport_find_number(parkbd);
144
145         if (pp == NULL) {
146                 printk(KERN_ERR "parkbd: no such parport\n");
147                 return -ENODEV;
148         }
149
150         parkbd_dev = parport_register_device(pp, "parkbd", NULL, NULL, parkbd_interrupt, PARPORT_DEV_EXCL, NULL);
151         parport_put_port(pp);
152
153         if (!parkbd_dev)
154                 return -ENODEV;
155
156         if (parport_claim(parkbd_dev)) {
157                 parport_unregister_device(parkbd_dev);
158                 return -EBUSY;
159         }
160
161         parkbd_start = jiffies;
162
163         return 0;
164 }
165
166
167 int __init parkbd_init(void)
168 {
169         if (parkbd_getport()) return -1;
170         parkbd_writelines(3);
171         parkbd_port.type = parkbd_mode;
172
173         sprintf(parkbd_phys, "%s/serio0", parkbd_dev->port->name);
174
175         serio_register_port(&parkbd_port);
176
177         printk(KERN_INFO "serio: PARKBD %s adapter on %s\n",
178                         parkbd_mode ? "AT" : "XT", parkbd_dev->port->name);
179
180         return 0;
181 }
182
183 void __exit parkbd_exit(void)
184 {
185         parport_release(parkbd_dev);
186         serio_unregister_port(&parkbd_port);
187         parport_unregister_device(parkbd_dev);
188 }
189
190 module_init(parkbd_init);
191 module_exit(parkbd_exit);