patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / serio / gscps2.c
1 /*
2  * drivers/input/serio/gscps2.c
3  *
4  * Copyright (c) 2004 Helge Deller <deller@gmx.de>
5  * Copyright (c) 2002 Laurent Canet <canetl@esiee.fr>
6  * Copyright (c) 2002 Thibaut Varene <varenet@esiee.fr>
7  *
8  * Pieces of code based on linux-2.4's hp_mouse.c & hp_keyb.c
9  *      Copyright (c) 1999 Alex deVries <adevries@thepuffingroup.com>
10  *      Copyright (c) 1999-2000 Philipp Rumpf <prumpf@tux.org>
11  *      Copyright (c) 2000 Xavier Debacker <debackex@esiee.fr>
12  *      Copyright (c) 2000-2001 Thomas Marteau <marteaut@esiee.fr>
13  *
14  * HP GSC PS/2 port driver, found in PA/RISC Workstations
15  *
16  * This file is subject to the terms and conditions of the GNU General Public
17  * License.  See the file "COPYING" in the main directory of this archive
18  * for more details.
19  *
20  * TODO:
21  * - Dino testing (did HP ever shipped a machine on which this port
22  *                 was usable/enabled ?)
23  */
24
25 #include <linux/config.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/serio.h>
29 #include <linux/input.h>
30 #include <linux/interrupt.h>
31 #include <linux/spinlock.h>
32 #include <linux/delay.h>
33 #include <linux/ioport.h>
34 #include <linux/pci_ids.h>
35
36 #include <asm/irq.h>
37 #include <asm/io.h>
38 #include <asm/parisc-device.h>
39
40 MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>, Thibaut Varene <varenet@esiee.fr>, Helge Deller <deller@gmx.de>");
41 MODULE_DESCRIPTION("HP GSC PS/2 port driver");
42 MODULE_LICENSE("GPL");
43 MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
44
45 #define PFX "gscps2.c: "
46
47 /*
48  * Driver constants
49  */
50
51 /* various constants */
52 #define ENABLE                  1
53 #define DISABLE                 0
54
55 #define GSC_DINO_OFFSET         0x0800  /* offset for DINO controller versus LASI one */
56
57 /* PS/2 IO port offsets */
58 #define GSC_ID                  0x00    /* device ID offset (see: GSC_ID_XXX) */
59 #define GSC_RESET               0x00    /* reset port offset */
60 #define GSC_RCVDATA             0x04    /* receive port offset */
61 #define GSC_XMTDATA             0x04    /* transmit port offset */
62 #define GSC_CONTROL             0x08    /* see: Control register bits */
63 #define GSC_STATUS              0x0C    /* see: Status register bits */
64
65 /* Control register bits */
66 #define GSC_CTRL_ENBL           0x01    /* enable interface */
67 #define GSC_CTRL_LPBXR          0x02    /* loopback operation */
68 #define GSC_CTRL_DIAG           0x20    /* directly control clock/data line */
69 #define GSC_CTRL_DATDIR         0x40    /* data line direct control */
70 #define GSC_CTRL_CLKDIR         0x80    /* clock line direct control */
71
72 /* Status register bits */
73 #define GSC_STAT_RBNE           0x01    /* Receive Buffer Not Empty */
74 #define GSC_STAT_TBNE           0x02    /* Transmit Buffer Not Empty */
75 #define GSC_STAT_TERR           0x04    /* Timeout Error */
76 #define GSC_STAT_PERR           0x08    /* Parity Error */
77 #define GSC_STAT_CMPINTR        0x10    /* Composite Interrupt = irq on any port */
78 #define GSC_STAT_DATSHD         0x40    /* Data Line Shadow */
79 #define GSC_STAT_CLKSHD         0x80    /* Clock Line Shadow */
80
81 /* IDs returned by GSC_ID port register */
82 #define GSC_ID_KEYBOARD         0       /* device ID values */
83 #define GSC_ID_MOUSE            1
84
85
86 static irqreturn_t gscps2_interrupt(int irq, void *dev, struct pt_regs *regs);
87
88 #define BUFFER_SIZE 0x0f
89
90 /* GSC PS/2 port device struct */
91 struct gscps2port {
92         struct list_head node;
93         struct parisc_device *padev;
94         struct serio port;
95         spinlock_t lock;
96         char *addr;
97         u8 act, append; /* position in buffer[] */
98         struct {
99                 u8 data;
100                 u8 str;
101         } buffer[BUFFER_SIZE+1];
102         int id;
103         char name[32];
104 };
105
106 /*
107  * Various HW level routines
108  */
109
110 #define gscps2_readb_input(x)           readb((x)+GSC_RCVDATA)
111 #define gscps2_readb_control(x)         readb((x)+GSC_CONTROL)
112 #define gscps2_readb_status(x)          readb((x)+GSC_STATUS)
113 #define gscps2_writeb_control(x, y)     writeb((x), (y)+GSC_CONTROL)
114
115
116 /*
117  * wait_TBE() - wait for Transmit Buffer Empty
118  */
119
120 static int wait_TBE(char *addr)
121 {
122         int timeout = 25000; /* device is expected to react within 250 msec */
123         while (gscps2_readb_status(addr) & GSC_STAT_TBNE) {
124                 if (!--timeout)
125                         return 0;       /* This should not happen */
126                 udelay(10);
127         }
128         return 1;
129 }
130
131
132 /*
133  * gscps2_flush() - flush the receive buffer
134  */
135
136 static void gscps2_flush(struct gscps2port *ps2port)
137 {
138         while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
139                 gscps2_readb_input(ps2port->addr);
140         ps2port->act = ps2port->append = 0;
141 }
142
143 /*
144  * gscps2_writeb_output() - write a byte to the port
145  *
146  * returns 1 on sucess, 0 on error
147  */
148
149 static inline int gscps2_writeb_output(struct gscps2port *ps2port, u8 data)
150 {
151         unsigned long flags;
152         char *addr = ps2port->addr;
153
154         if (!wait_TBE(addr)) {
155                 printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
156                 return 0;
157         }
158
159         while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
160                 /* wait */;
161
162         spin_lock_irqsave(&ps2port->lock, flags);
163         writeb(data, addr+GSC_XMTDATA);
164         spin_unlock_irqrestore(&ps2port->lock, flags);
165
166         /* this is ugly, but due to timing of the port it seems to be necessary. */
167         mdelay(6);
168
169         /* make sure any received data is returned as fast as possible */
170         /* this is important e.g. when we set the LEDs on the keyboard */
171         gscps2_interrupt(0, NULL, NULL);
172
173         return 1;
174 }
175
176
177 /*
178  * gscps2_enable() - enables or disables the port
179  */
180
181 static void gscps2_enable(struct gscps2port *ps2port, int enable)
182 {
183         unsigned long flags;
184         u8 data;
185
186         /* now enable/disable the port */
187         spin_lock_irqsave(&ps2port->lock, flags);
188         gscps2_flush(ps2port);
189         data = gscps2_readb_control(ps2port->addr);
190         if (enable)
191                 data |= GSC_CTRL_ENBL;
192         else
193                 data &= ~GSC_CTRL_ENBL;
194         gscps2_writeb_control(data, ps2port->addr);
195         spin_unlock_irqrestore(&ps2port->lock, flags);
196         wait_TBE(ps2port->addr);
197         gscps2_flush(ps2port);
198 }
199
200 /*
201  * gscps2_reset() - resets the PS/2 port
202  */
203
204 static void gscps2_reset(struct gscps2port *ps2port)
205 {
206         char *addr = ps2port->addr;
207         unsigned long flags;
208
209         /* reset the interface */
210         spin_lock_irqsave(&ps2port->lock, flags);
211         gscps2_flush(ps2port);
212         writeb(0xff, addr+GSC_RESET);
213         gscps2_flush(ps2port);
214         spin_unlock_irqrestore(&ps2port->lock, flags);
215
216         /* enable it */
217         gscps2_enable(ps2port, ENABLE);
218 }
219
220 static LIST_HEAD(ps2port_list);
221
222 /**
223  * gscps2_interrupt() - Interruption service routine
224  *
225  * This function reads received PS/2 bytes and processes them on
226  * all interfaces.
227  * The problematic part here is, that the keyboard and mouse PS/2 port
228  * share the same interrupt and it's not possible to send data if any
229  * one of them holds input data. To solve this problem we try to receive
230  * the data as fast as possible and handle the reporting to the upper layer
231  * later.
232  */
233
234 static irqreturn_t gscps2_interrupt(int irq, void *dev, struct pt_regs *regs)
235 {
236         struct gscps2port *ps2port;
237
238         list_for_each_entry(ps2port, &ps2port_list, node) {
239
240           unsigned long flags;
241           spin_lock_irqsave(&ps2port->lock, flags);
242
243           while ( (ps2port->buffer[ps2port->append].str =
244                    gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
245                 ps2port->buffer[ps2port->append].data =
246                                 gscps2_readb_input(ps2port->addr);
247                 ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
248           }
249
250           spin_unlock_irqrestore(&ps2port->lock, flags);
251
252         } /* list_for_each_entry */
253
254         /* all data was read from the ports - now report the data to upper layer */
255
256         list_for_each_entry(ps2port, &ps2port_list, node) {
257
258           while (ps2port->act != ps2port->append) {
259
260             unsigned int rxflags;
261             u8 data, status;
262
263             /* Did new data arrived while we read existing data ?
264                If yes, exit now and let the new irq handler start over again */
265             if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
266                 return IRQ_HANDLED;
267
268             status = ps2port->buffer[ps2port->act].str;
269             data   = ps2port->buffer[ps2port->act].data;
270
271             ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
272             rxflags =   ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
273                         ((status & GSC_STAT_PERR) ? SERIO_PARITY  : 0 );
274
275             serio_interrupt(&ps2port->port, data, rxflags, regs);
276
277           } /* while() */
278
279         } /* list_for_each_entry */
280
281         return IRQ_HANDLED;
282 }
283
284
285 /*
286  * gscps2_write() - send a byte out through the aux interface.
287  */
288
289 static int gscps2_write(struct serio *port, unsigned char data)
290 {
291         struct gscps2port *ps2port = port->driver;
292
293         if (!gscps2_writeb_output(ps2port, data)) {
294                 printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
295                 return -1;
296         }
297         return 0;
298 }
299
300 /*
301  * gscps2_open() is called when a port is opened by the higher layer.
302  * It resets and enables the port.
303  */
304
305 static int gscps2_open(struct serio *port)
306 {
307         struct gscps2port *ps2port = port->driver;
308
309         gscps2_reset(ps2port);
310
311         gscps2_interrupt(0, NULL, NULL);
312
313         return 0;
314 }
315
316 /*
317  * gscps2_close() disables the port
318  */
319
320 static void gscps2_close(struct serio *port)
321 {
322         struct gscps2port *ps2port = port->driver;
323         gscps2_enable(ps2port, DISABLE);
324 }
325
326 static struct serio gscps2_serio_port =
327 {
328         .name =         "GSC PS/2",
329         .idbus =        BUS_GSC,
330         .idvendor =     PCI_VENDOR_ID_HP,
331         .idproduct =    0x0001,
332         .idversion =    0x0010,
333         .type =         SERIO_8042,
334         .write =        gscps2_write,
335         .open =         gscps2_open,
336         .close =        gscps2_close,
337 };
338
339 /**
340  * gscps2_probe() - Probes PS2 devices
341  * @return: success/error report
342  */
343
344 static int __init gscps2_probe(struct parisc_device *dev)
345 {
346         struct gscps2port *ps2port;
347         unsigned long hpa = dev->hpa;
348         int ret;
349
350         if (!dev->irq)
351                 return -ENODEV;
352
353         /* Offset for DINO PS/2. Works with LASI even */
354         if (dev->id.sversion == 0x96)
355                 hpa += GSC_DINO_OFFSET;
356
357         ps2port = kmalloc(sizeof(struct gscps2port), GFP_KERNEL);
358         if (!ps2port)
359                 return -ENOMEM;
360
361         dev_set_drvdata(&dev->dev, ps2port);
362
363         memset(ps2port, 0, sizeof(struct gscps2port));
364         ps2port->padev = dev;
365         ps2port->addr = ioremap(hpa, GSC_STATUS + 4);
366         spin_lock_init(&ps2port->lock);
367
368         gscps2_reset(ps2port);
369         ps2port->id = readb(ps2port->addr+GSC_ID) & 0x0f;
370         snprintf(ps2port->name, sizeof(ps2port->name)-1, "%s %s",
371                 gscps2_serio_port.name,
372                 (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse" );
373
374         memcpy(&ps2port->port, &gscps2_serio_port, sizeof(gscps2_serio_port));
375         ps2port->port.driver = ps2port;
376         ps2port->port.name = ps2port->name;
377         ps2port->port.phys = dev->dev.bus_id;
378
379         list_add_tail(&ps2port->node, &ps2port_list);
380
381         ret = -EBUSY;
382         if (request_irq(dev->irq, gscps2_interrupt, SA_SHIRQ, ps2port->name, ps2port))
383                 goto fail_miserably;
384
385         if ( (ps2port->id != GSC_ID_KEYBOARD) && (ps2port->id != GSC_ID_MOUSE) ) {
386                 printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
387                                 hpa, ps2port->id);
388                 ret = -ENODEV;
389                 goto fail;
390         }
391
392 #if 0
393         if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
394                 goto fail;
395 #endif
396
397         printk(KERN_INFO "serio: %s port at 0x%p irq %d @ %s\n",
398                 ps2port->name,
399                 ps2port->addr,
400                 ps2port->padev->irq,
401                 ps2port->port.phys);
402
403         serio_register_port(&ps2port->port);
404
405         return 0;
406
407 fail:
408         free_irq(dev->irq, ps2port);
409
410 fail_miserably:
411         list_del(&ps2port->node);
412         iounmap(ps2port->addr);
413         release_mem_region(dev->hpa, GSC_STATUS + 4);
414         kfree(ps2port);
415         return ret;
416 }
417
418 /**
419  * gscps2_remove() - Removes PS2 devices
420  * @return: success/error report
421  */
422
423 static int __devexit gscps2_remove(struct parisc_device *dev)
424 {
425         struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
426
427         serio_unregister_port(&ps2port->port);
428         free_irq(dev->irq, ps2port);
429         gscps2_flush(ps2port);
430         list_del(&ps2port->node);
431         iounmap(ps2port->addr);
432 #if 0
433         release_mem_region(dev->hpa, GSC_STATUS + 4);
434 #endif
435         dev_set_drvdata(&dev->dev, NULL);
436         kfree(ps2port);
437         return 0;
438 }
439
440
441 static struct parisc_device_id gscps2_device_tbl[] = {
442         { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
443 #ifdef DINO_TESTED
444         { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
445 #endif
446         { 0, }  /* 0 terminated list */
447 };
448
449 static struct parisc_driver parisc_ps2_driver = {
450         .name           = "GSC PS/2",
451         .id_table       = gscps2_device_tbl,
452         .probe          = gscps2_probe,
453         .remove         = gscps2_remove,
454 };
455
456 static int __init gscps2_init(void)
457 {
458         register_parisc_driver(&parisc_ps2_driver);
459         return 0;
460 }
461
462 static void __exit gscps2_exit(void)
463 {
464         unregister_parisc_driver(&parisc_ps2_driver);
465 }
466
467
468 module_init(gscps2_init);
469 module_exit(gscps2_exit);
470