patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / serio / ct82c710.c
1 /*
2  * $Id: ct82c710.c,v 1.11 2001/09/25 10:12:07 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  */
6
7 /*
8  *  82C710 C&T mouse port chip 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/delay.h>
32 #include <linux/module.h>
33 #include <linux/ioport.h>
34 #include <linux/config.h>
35 #include <linux/init.h>
36 #include <linux/interrupt.h>
37 #include <linux/serio.h>
38 #include <linux/errno.h>
39
40 #include <asm/io.h>
41
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
44 MODULE_LICENSE("GPL");
45
46 static char ct82c710_name[] = "C&T 82c710 mouse port";
47 static char ct82c710_phys[16];
48
49 /*
50  * ct82c710 interface
51  */
52
53 #define CT82C710_DEV_IDLE     0x01              /* Device Idle */
54 #define CT82C710_RX_FULL      0x02              /* Device Char received */
55 #define CT82C710_TX_IDLE      0x04              /* Device XMIT Idle */
56 #define CT82C710_RESET        0x08              /* Device Reset */
57 #define CT82C710_INTS_ON      0x10              /* Device Interrupt On */
58 #define CT82C710_ERROR_FLAG   0x20              /* Device Error */
59 #define CT82C710_CLEAR        0x40              /* Device Clear */
60 #define CT82C710_ENABLE       0x80              /* Device Enable */
61
62 #define CT82C710_IRQ          12
63
64 static int ct82c710_data;
65 static int ct82c710_status;
66
67 static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id, struct pt_regs * regs);
68
69 /*
70  * Wait for device to send output char and flush any input char.
71  */
72
73 static int ct82c170_wait(void)
74 {
75         int timeout = 60000;
76
77         while ((inb(ct82c710_status) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
78                        != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
79
80                 if (inb_p(ct82c710_status) & CT82C710_RX_FULL) inb_p(ct82c710_data);
81
82                 udelay(1);
83                 timeout--;
84         }
85
86         return !timeout;
87 }
88
89 static void ct82c710_close(struct serio *serio)
90 {
91         if (ct82c170_wait())
92                 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
93
94         outb_p(inb_p(ct82c710_status) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), ct82c710_status);
95
96         if (ct82c170_wait())
97                 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
98
99         free_irq(CT82C710_IRQ, NULL);
100 }
101
102 static int ct82c710_open(struct serio *serio)
103 {
104         unsigned char status;
105
106         if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL))
107                 return -1;
108
109         status = inb_p(ct82c710_status);
110
111         status |= (CT82C710_ENABLE | CT82C710_RESET);
112         outb_p(status, ct82c710_status);
113
114         status &= ~(CT82C710_RESET);
115         outb_p(status, ct82c710_status);
116
117         status |= CT82C710_INTS_ON;
118         outb_p(status, ct82c710_status);        /* Enable interrupts */
119
120         while (ct82c170_wait()) {
121                 printk(KERN_ERR "ct82c710: Device busy in open()\n");
122                 status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
123                 outb_p(status, ct82c710_status);
124                 free_irq(CT82C710_IRQ, NULL);
125                 return -1;
126         }
127
128         return 0;
129 }
130
131 /*
132  * Write to the 82C710 mouse device.
133  */
134
135 static int ct82c710_write(struct serio *port, unsigned char c)
136 {
137         if (ct82c170_wait()) return -1;
138         outb_p(c, ct82c710_data);
139         return 0;
140 }
141
142 static struct serio ct82c710_port =
143 {
144         .type   = SERIO_8042,
145         .name   = ct82c710_name,
146         .phys   = ct82c710_phys,
147         .write  = ct82c710_write,
148         .open   = ct82c710_open,
149         .close  = ct82c710_close,
150 };
151
152 /*
153  * Interrupt handler for the 82C710 mouse port. A character
154  * is waiting in the 82C710.
155  */
156
157 static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id, struct pt_regs * regs)
158 {
159         return serio_interrupt(&ct82c710_port, inb(ct82c710_data), 0, regs);
160 }
161
162 /*
163  * See if we can find a 82C710 device. Read mouse address.
164  */
165
166 static int __init ct82c710_probe(void)
167 {
168         outb_p(0x55, 0x2fa);                            /* Any value except 9, ff or 36 */
169         outb_p(0xaa, 0x3fa);                            /* Inverse of 55 */
170         outb_p(0x36, 0x3fa);                            /* Address the chip */
171         outb_p(0xe4, 0x3fa);                            /* 390/4; 390 = config address */
172         outb_p(0x1b, 0x2fa);                            /* Inverse of e4 */
173         outb_p(0x0f, 0x390);                            /* Write index */
174         if (inb_p(0x391) != 0xe4)                       /* Config address found? */
175                 return -1;                              /* No: no 82C710 here */
176
177         outb_p(0x0d, 0x390);                            /* Write index */
178         ct82c710_data = inb_p(0x391) << 2;              /* Get mouse I/O address */
179         ct82c710_status = ct82c710_data + 1;
180         outb_p(0x0f, 0x390);
181         outb_p(0x0f, 0x391);                            /* Close config mode */
182
183         return 0;
184 }
185
186 int __init ct82c710_init(void)
187 {
188         if (ct82c710_probe())
189                 return -ENODEV;
190
191         if (request_region(ct82c710_data, 2, "ct82c710"))
192                 return -EBUSY;
193
194         sprintf(ct82c710_phys, "isa%04x/serio0", ct82c710_data);
195
196         serio_register_port(&ct82c710_port);
197
198         printk(KERN_INFO "serio: C&T 82c710 mouse port at %#x irq %d\n",
199                 ct82c710_data, CT82C710_IRQ);
200
201         return 0;
202 }
203
204 void __exit ct82c710_exit(void)
205 {
206         serio_unregister_port(&ct82c710_port);
207         release_region(ct82c710_data, 2);
208 }
209
210 module_init(ct82c710_init);
211 module_exit(ct82c710_exit);