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#include <linux/init.h>
26#include <linux/module.h>
27#include <linux/serio.h>
28#include <linux/input.h>
29#include <linux/interrupt.h>
30#include <linux/spinlock.h>
31#include <linux/delay.h>
32#include <linux/ioport.h>
33#include <linux/pci_ids.h>
34
35#include <asm/irq.h>
36#include <asm/io.h>
37#include <asm/parisc-device.h>
38
39MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>, Thibaut Varene <varenet@parisc-linux.org>, Helge Deller <deller@gmx.de>");
40MODULE_DESCRIPTION("HP GSC PS2 port driver");
41MODULE_LICENSE("GPL");
42MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
43
44#define PFX "gscps2.c: "
45
46
47
48
49
50
51#define ENABLE 1
52#define DISABLE 0
53
54#define GSC_DINO_OFFSET 0x0800
55
56
57#define GSC_ID 0x00
58#define GSC_RESET 0x00
59#define GSC_RCVDATA 0x04
60#define GSC_XMTDATA 0x04
61#define GSC_CONTROL 0x08
62#define GSC_STATUS 0x0C
63
64
65#define GSC_CTRL_ENBL 0x01
66#define GSC_CTRL_LPBXR 0x02
67#define GSC_CTRL_DIAG 0x20
68#define GSC_CTRL_DATDIR 0x40
69#define GSC_CTRL_CLKDIR 0x80
70
71
72#define GSC_STAT_RBNE 0x01
73#define GSC_STAT_TBNE 0x02
74#define GSC_STAT_TERR 0x04
75#define GSC_STAT_PERR 0x08
76#define GSC_STAT_CMPINTR 0x10
77#define GSC_STAT_DATSHD 0x40
78#define GSC_STAT_CLKSHD 0x80
79
80
81#define GSC_ID_KEYBOARD 0
82#define GSC_ID_MOUSE 1
83
84
85static irqreturn_t gscps2_interrupt(int irq, void *dev);
86
87#define BUFFER_SIZE 0x0f
88
89
90struct gscps2port {
91 struct list_head node;
92 struct parisc_device *padev;
93 struct serio *port;
94 spinlock_t lock;
95 char *addr;
96 u8 act, append;
97 struct {
98 u8 data;
99 u8 str;
100 } buffer[BUFFER_SIZE+1];
101 int id;
102};
103
104
105
106
107
108#define gscps2_readb_input(x) readb((x)+GSC_RCVDATA)
109#define gscps2_readb_control(x) readb((x)+GSC_CONTROL)
110#define gscps2_readb_status(x) readb((x)+GSC_STATUS)
111#define gscps2_writeb_control(x, y) writeb((x), (y)+GSC_CONTROL)
112
113
114
115
116
117
118static int wait_TBE(char *addr)
119{
120 int timeout = 25000;
121 while (gscps2_readb_status(addr) & GSC_STAT_TBNE) {
122 if (!--timeout)
123 return 0;
124 udelay(10);
125 }
126 return 1;
127}
128
129
130
131
132
133
134static void gscps2_flush(struct gscps2port *ps2port)
135{
136 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
137 gscps2_readb_input(ps2port->addr);
138 ps2port->act = ps2port->append = 0;
139}
140
141
142
143
144
145
146
147static inline int gscps2_writeb_output(struct gscps2port *ps2port, u8 data)
148{
149 unsigned long flags;
150 char *addr = ps2port->addr;
151
152 if (!wait_TBE(addr)) {
153 printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
154 return 0;
155 }
156
157 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
158 ;
159
160 spin_lock_irqsave(&ps2port->lock, flags);
161 writeb(data, addr+GSC_XMTDATA);
162 spin_unlock_irqrestore(&ps2port->lock, flags);
163
164
165 mdelay(6);
166
167
168
169 gscps2_interrupt(0, NULL);
170
171 return 1;
172}
173
174
175
176
177
178
179static void gscps2_enable(struct gscps2port *ps2port, int enable)
180{
181 unsigned long flags;
182 u8 data;
183
184
185 spin_lock_irqsave(&ps2port->lock, flags);
186 gscps2_flush(ps2port);
187 data = gscps2_readb_control(ps2port->addr);
188 if (enable)
189 data |= GSC_CTRL_ENBL;
190 else
191 data &= ~GSC_CTRL_ENBL;
192 gscps2_writeb_control(data, ps2port->addr);
193 spin_unlock_irqrestore(&ps2port->lock, flags);
194 wait_TBE(ps2port->addr);
195 gscps2_flush(ps2port);
196}
197
198
199
200
201
202static void gscps2_reset(struct gscps2port *ps2port)
203{
204 char *addr = ps2port->addr;
205 unsigned long flags;
206
207
208 spin_lock_irqsave(&ps2port->lock, flags);
209 gscps2_flush(ps2port);
210 writeb(0xff, addr+GSC_RESET);
211 gscps2_flush(ps2port);
212 spin_unlock_irqrestore(&ps2port->lock, flags);
213}
214
215static LIST_HEAD(ps2port_list);
216
217
218
219
220
221
222
223
224
225
226
227
228
229static irqreturn_t gscps2_interrupt(int irq, void *dev)
230{
231 struct gscps2port *ps2port;
232
233 list_for_each_entry(ps2port, &ps2port_list, node) {
234
235 unsigned long flags;
236 spin_lock_irqsave(&ps2port->lock, flags);
237
238 while ( (ps2port->buffer[ps2port->append].str =
239 gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
240 ps2port->buffer[ps2port->append].data =
241 gscps2_readb_input(ps2port->addr);
242 ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
243 }
244
245 spin_unlock_irqrestore(&ps2port->lock, flags);
246
247 }
248
249
250
251 list_for_each_entry(ps2port, &ps2port_list, node) {
252
253 while (ps2port->act != ps2port->append) {
254
255 unsigned int rxflags;
256 u8 data, status;
257
258
259
260 if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
261 return IRQ_HANDLED;
262
263 status = ps2port->buffer[ps2port->act].str;
264 data = ps2port->buffer[ps2port->act].data;
265
266 ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
267 rxflags = ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
268 ((status & GSC_STAT_PERR) ? SERIO_PARITY : 0 );
269
270 serio_interrupt(ps2port->port, data, rxflags);
271
272 }
273
274 }
275
276 return IRQ_HANDLED;
277}
278
279
280
281
282
283
284static int gscps2_write(struct serio *port, unsigned char data)
285{
286 struct gscps2port *ps2port = port->port_data;
287
288 if (!gscps2_writeb_output(ps2port, data)) {
289 printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
290 return -1;
291 }
292 return 0;
293}
294
295
296
297
298
299
300static int gscps2_open(struct serio *port)
301{
302 struct gscps2port *ps2port = port->port_data;
303
304 gscps2_reset(ps2port);
305
306
307 gscps2_enable(ps2port, ENABLE);
308
309 gscps2_interrupt(0, NULL);
310
311 return 0;
312}
313
314
315
316
317
318static void gscps2_close(struct serio *port)
319{
320 struct gscps2port *ps2port = port->port_data;
321 gscps2_enable(ps2port, DISABLE);
322}
323
324
325
326
327
328
329static int __init gscps2_probe(struct parisc_device *dev)
330{
331 struct gscps2port *ps2port;
332 struct serio *serio;
333 unsigned long hpa = dev->hpa.start;
334 int ret;
335
336 if (!dev->irq)
337 return -ENODEV;
338
339
340 if (dev->id.sversion == 0x96)
341 hpa += GSC_DINO_OFFSET;
342
343 ps2port = kzalloc(sizeof(struct gscps2port), GFP_KERNEL);
344 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
345 if (!ps2port || !serio) {
346 ret = -ENOMEM;
347 goto fail_nomem;
348 }
349
350 dev_set_drvdata(&dev->dev, ps2port);
351
352 ps2port->port = serio;
353 ps2port->padev = dev;
354 ps2port->addr = ioremap_nocache(hpa, GSC_STATUS + 4);
355 spin_lock_init(&ps2port->lock);
356
357 gscps2_reset(ps2port);
358 ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
359
360 snprintf(serio->name, sizeof(serio->name), "GSC PS/2 %s",
361 (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
362 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
363 serio->id.type = SERIO_8042;
364 serio->write = gscps2_write;
365 serio->open = gscps2_open;
366 serio->close = gscps2_close;
367 serio->port_data = ps2port;
368 serio->dev.parent = &dev->dev;
369
370 ret = -EBUSY;
371 if (request_irq(dev->irq, gscps2_interrupt, IRQF_SHARED, ps2port->port->name, ps2port))
372 goto fail_miserably;
373
374 if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
375 printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
376 hpa, ps2port->id);
377 ret = -ENODEV;
378 goto fail;
379 }
380
381#if 0
382 if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
383 goto fail;
384#endif
385
386 printk(KERN_INFO "serio: %s port at 0x%p irq %d @ %s\n",
387 ps2port->port->name,
388 ps2port->addr,
389 ps2port->padev->irq,
390 ps2port->port->phys);
391
392 serio_register_port(ps2port->port);
393
394 list_add_tail(&ps2port->node, &ps2port_list);
395
396 return 0;
397
398fail:
399 free_irq(dev->irq, ps2port);
400
401fail_miserably:
402 iounmap(ps2port->addr);
403 release_mem_region(dev->hpa.start, GSC_STATUS + 4);
404
405fail_nomem:
406 kfree(ps2port);
407 kfree(serio);
408 return ret;
409}
410
411
412
413
414
415
416static int __devexit gscps2_remove(struct parisc_device *dev)
417{
418 struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
419
420 serio_unregister_port(ps2port->port);
421 free_irq(dev->irq, ps2port);
422 gscps2_flush(ps2port);
423 list_del(&ps2port->node);
424 iounmap(ps2port->addr);
425#if 0
426 release_mem_region(dev->hpa, GSC_STATUS + 4);
427#endif
428 dev_set_drvdata(&dev->dev, NULL);
429 kfree(ps2port);
430 return 0;
431}
432
433
434static struct parisc_device_id gscps2_device_tbl[] = {
435 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 },
436#ifdef DINO_TESTED
437 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 },
438#endif
439 { 0, }
440};
441
442static struct parisc_driver parisc_ps2_driver = {
443 .name = "gsc_ps2",
444 .id_table = gscps2_device_tbl,
445 .probe = gscps2_probe,
446 .remove = gscps2_remove,
447};
448
449static int __init gscps2_init(void)
450{
451 register_parisc_driver(&parisc_ps2_driver);
452 return 0;
453}
454
455static void __exit gscps2_exit(void)
456{
457 unregister_parisc_driver(&parisc_ps2_driver);
458}
459
460
461module_init(gscps2_init);
462module_exit(gscps2_exit);
463
464