1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include <linux/delay.h>
30#include <linux/module.h>
31#include <linux/ioport.h>
32#include <linux/init.h>
33#include <linux/interrupt.h>
34#include <linux/serio.h>
35#include <linux/errno.h>
36#include <linux/err.h>
37#include <linux/platform_device.h>
38
39#include <asm/io.h>
40
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION("82C710 C&T mouse port chip driver");
43MODULE_LICENSE("GPL");
44
45
46
47
48
49#define CT82C710_DEV_IDLE 0x01
50#define CT82C710_RX_FULL 0x02
51#define CT82C710_TX_IDLE 0x04
52#define CT82C710_RESET 0x08
53#define CT82C710_INTS_ON 0x10
54#define CT82C710_ERROR_FLAG 0x20
55#define CT82C710_CLEAR 0x40
56#define CT82C710_ENABLE 0x80
57
58#define CT82C710_IRQ 12
59
60#define CT82C710_DATA ct82c710_iores.start
61#define CT82C710_STATUS (ct82c710_iores.start + 1)
62
63static struct serio *ct82c710_port;
64static struct platform_device *ct82c710_device;
65static struct resource ct82c710_iores;
66
67
68
69
70
71
72static irqreturn_t ct82c710_interrupt(int cpl, void *dev_id)
73{
74 return serio_interrupt(ct82c710_port, inb(CT82C710_DATA), 0);
75}
76
77
78
79
80
81static int ct82c170_wait(void)
82{
83 int timeout = 60000;
84
85 while ((inb(CT82C710_STATUS) & (CT82C710_RX_FULL | CT82C710_TX_IDLE | CT82C710_DEV_IDLE))
86 != (CT82C710_DEV_IDLE | CT82C710_TX_IDLE) && timeout) {
87
88 if (inb_p(CT82C710_STATUS) & CT82C710_RX_FULL) inb_p(CT82C710_DATA);
89
90 udelay(1);
91 timeout--;
92 }
93
94 return !timeout;
95}
96
97static void ct82c710_close(struct serio *serio)
98{
99 if (ct82c170_wait())
100 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
101
102 outb_p(inb_p(CT82C710_STATUS) & ~(CT82C710_ENABLE | CT82C710_INTS_ON), CT82C710_STATUS);
103
104 if (ct82c170_wait())
105 printk(KERN_WARNING "ct82c710.c: Device busy in close()\n");
106
107 free_irq(CT82C710_IRQ, NULL);
108}
109
110static int ct82c710_open(struct serio *serio)
111{
112 unsigned char status;
113
114 if (request_irq(CT82C710_IRQ, ct82c710_interrupt, 0, "ct82c710", NULL))
115 return -1;
116
117 status = inb_p(CT82C710_STATUS);
118
119 status |= (CT82C710_ENABLE | CT82C710_RESET);
120 outb_p(status, CT82C710_STATUS);
121
122 status &= ~(CT82C710_RESET);
123 outb_p(status, CT82C710_STATUS);
124
125 status |= CT82C710_INTS_ON;
126 outb_p(status, CT82C710_STATUS);
127
128 while (ct82c170_wait()) {
129 printk(KERN_ERR "ct82c710: Device busy in open()\n");
130 status &= ~(CT82C710_ENABLE | CT82C710_INTS_ON);
131 outb_p(status, CT82C710_STATUS);
132 free_irq(CT82C710_IRQ, NULL);
133 return -1;
134 }
135
136 return 0;
137}
138
139
140
141
142
143static int ct82c710_write(struct serio *port, unsigned char c)
144{
145 if (ct82c170_wait()) return -1;
146 outb_p(c, CT82C710_DATA);
147 return 0;
148}
149
150
151
152
153
154static int __init ct82c710_detect(void)
155{
156 outb_p(0x55, 0x2fa);
157 outb_p(0xaa, 0x3fa);
158 outb_p(0x36, 0x3fa);
159 outb_p(0xe4, 0x3fa);
160 outb_p(0x1b, 0x2fa);
161 outb_p(0x0f, 0x390);
162 if (inb_p(0x391) != 0xe4)
163 return -ENODEV;
164
165 outb_p(0x0d, 0x390);
166 ct82c710_iores.start = inb_p(0x391) << 2;
167 ct82c710_iores.end = ct82c710_iores.start + 1;
168 ct82c710_iores.flags = IORESOURCE_IO;
169 outb_p(0x0f, 0x390);
170 outb_p(0x0f, 0x391);
171
172 return 0;
173}
174
175static int __devinit ct82c710_probe(struct platform_device *dev)
176{
177 ct82c710_port = kzalloc(sizeof(struct serio), GFP_KERNEL);
178 if (!ct82c710_port)
179 return -ENOMEM;
180
181 ct82c710_port->id.type = SERIO_8042;
182 ct82c710_port->dev.parent = &dev->dev;
183 ct82c710_port->open = ct82c710_open;
184 ct82c710_port->close = ct82c710_close;
185 ct82c710_port->write = ct82c710_write;
186 strlcpy(ct82c710_port->name, "C&T 82c710 mouse port",
187 sizeof(ct82c710_port->name));
188 snprintf(ct82c710_port->phys, sizeof(ct82c710_port->phys),
189 "isa%16llx/serio0", (unsigned long long)CT82C710_DATA);
190
191 serio_register_port(ct82c710_port);
192
193 return 0;
194}
195
196static int __devexit ct82c710_remove(struct platform_device *dev)
197{
198 serio_unregister_port(ct82c710_port);
199
200 return 0;
201}
202
203static struct platform_driver ct82c710_driver = {
204 .driver = {
205 .name = "ct82c710",
206 .owner = THIS_MODULE,
207 },
208 .probe = ct82c710_probe,
209 .remove = __devexit_p(ct82c710_remove),
210};
211
212
213static int __init ct82c710_init(void)
214{
215 int error;
216
217 error = ct82c710_detect();
218 if (error)
219 return error;
220
221 error = platform_driver_register(&ct82c710_driver);
222 if (error)
223 return error;
224
225 ct82c710_device = platform_device_alloc("ct82c710", -1);
226 if (!ct82c710_device) {
227 error = -ENOMEM;
228 goto err_unregister_driver;
229 }
230
231 error = platform_device_add_resources(ct82c710_device, &ct82c710_iores, 1);
232 if (error)
233 goto err_free_device;
234
235 error = platform_device_add(ct82c710_device);
236 if (error)
237 goto err_free_device;
238
239 serio_register_port(ct82c710_port);
240
241 printk(KERN_INFO "serio: C&T 82c710 mouse port at %#llx irq %d\n",
242 (unsigned long long)CT82C710_DATA, CT82C710_IRQ);
243
244 return 0;
245
246 err_free_device:
247 platform_device_put(ct82c710_device);
248 err_unregister_driver:
249 platform_driver_unregister(&ct82c710_driver);
250 return error;
251}
252
253static void __exit ct82c710_exit(void)
254{
255 platform_device_unregister(ct82c710_device);
256 platform_driver_unregister(&ct82c710_driver);
257}
258
259module_init(ct82c710_init);
260module_exit(ct82c710_exit);
261