2 * drivers/input/serio/gscps2.c
4 * Copyright (c) 2004-2006 Helge Deller <deller@gmx.de>
5 * Copyright (c) 2002 Laurent Canet <canetl@esiee.fr>
6 * Copyright (c) 2002 Thibaut Varene <varenet@parisc-linux.org>
8 * Pieces of code based on linux-2.4's hp_mouse.c & hp_keyb.c
9 * Copyright (c) 1999 Alex deVries <alex@onefishtwo.ca>
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>
14 * HP GSC PS/2 port driver, found in PA/RISC Workstations
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
21 * - Dino testing (did HP ever shipped a machine on which this port
22 * was usable/enabled ?)
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.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>
37 #include <asm/parisc-device.h>
39 MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>, Thibaut Varene <varenet@parisc-linux.org>, Helge Deller <deller@gmx.de>");
40 MODULE_DESCRIPTION("HP GSC PS2 port driver");
41 MODULE_LICENSE("GPL");
43 #define PFX "gscps2.c: "
49 /* various constants */
53 #define GSC_DINO_OFFSET 0x0800 /* offset for DINO controller versus LASI one */
55 /* PS/2 IO port offsets */
56 #define GSC_ID 0x00 /* device ID offset (see: GSC_ID_XXX) */
57 #define GSC_RESET 0x00 /* reset port offset */
58 #define GSC_RCVDATA 0x04 /* receive port offset */
59 #define GSC_XMTDATA 0x04 /* transmit port offset */
60 #define GSC_CONTROL 0x08 /* see: Control register bits */
61 #define GSC_STATUS 0x0C /* see: Status register bits */
63 /* Control register bits */
64 #define GSC_CTRL_ENBL 0x01 /* enable interface */
65 #define GSC_CTRL_LPBXR 0x02 /* loopback operation */
66 #define GSC_CTRL_DIAG 0x20 /* directly control clock/data line */
67 #define GSC_CTRL_DATDIR 0x40 /* data line direct control */
68 #define GSC_CTRL_CLKDIR 0x80 /* clock line direct control */
70 /* Status register bits */
71 #define GSC_STAT_RBNE 0x01 /* Receive Buffer Not Empty */
72 #define GSC_STAT_TBNE 0x02 /* Transmit Buffer Not Empty */
73 #define GSC_STAT_TERR 0x04 /* Timeout Error */
74 #define GSC_STAT_PERR 0x08 /* Parity Error */
75 #define GSC_STAT_CMPINTR 0x10 /* Composite Interrupt = irq on any port */
76 #define GSC_STAT_DATSHD 0x40 /* Data Line Shadow */
77 #define GSC_STAT_CLKSHD 0x80 /* Clock Line Shadow */
79 /* IDs returned by GSC_ID port register */
80 #define GSC_ID_KEYBOARD 0 /* device ID values */
81 #define GSC_ID_MOUSE 1
84 static irqreturn_t gscps2_interrupt(int irq, void *dev);
86 #define BUFFER_SIZE 0x0f
88 /* GSC PS/2 port device struct */
90 struct list_head node;
91 struct parisc_device *padev;
95 u8 act, append; /* position in buffer[] */
99 } buffer[BUFFER_SIZE+1];
104 * Various HW level routines
107 #define gscps2_readb_input(x) readb((x)+GSC_RCVDATA)
108 #define gscps2_readb_control(x) readb((x)+GSC_CONTROL)
109 #define gscps2_readb_status(x) readb((x)+GSC_STATUS)
110 #define gscps2_writeb_control(x, y) writeb((x), (y)+GSC_CONTROL)
114 * wait_TBE() - wait for Transmit Buffer Empty
117 static int wait_TBE(char *addr)
119 int timeout = 25000; /* device is expected to react within 250 msec */
120 while (gscps2_readb_status(addr) & GSC_STAT_TBNE) {
122 return 0; /* This should not happen */
130 * gscps2_flush() - flush the receive buffer
133 static void gscps2_flush(struct gscps2port *ps2port)
135 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
136 gscps2_readb_input(ps2port->addr);
137 ps2port->act = ps2port->append = 0;
141 * gscps2_writeb_output() - write a byte to the port
143 * returns 1 on success, 0 on error
146 static inline int gscps2_writeb_output(struct gscps2port *ps2port, u8 data)
149 char *addr = ps2port->addr;
151 if (!wait_TBE(addr)) {
152 printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
156 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
159 spin_lock_irqsave(&ps2port->lock, flags);
160 writeb(data, addr+GSC_XMTDATA);
161 spin_unlock_irqrestore(&ps2port->lock, flags);
163 /* this is ugly, but due to timing of the port it seems to be necessary. */
166 /* make sure any received data is returned as fast as possible */
167 /* this is important e.g. when we set the LEDs on the keyboard */
168 gscps2_interrupt(0, NULL);
175 * gscps2_enable() - enables or disables the port
178 static void gscps2_enable(struct gscps2port *ps2port, int enable)
183 /* now enable/disable the port */
184 spin_lock_irqsave(&ps2port->lock, flags);
185 gscps2_flush(ps2port);
186 data = gscps2_readb_control(ps2port->addr);
188 data |= GSC_CTRL_ENBL;
190 data &= ~GSC_CTRL_ENBL;
191 gscps2_writeb_control(data, ps2port->addr);
192 spin_unlock_irqrestore(&ps2port->lock, flags);
193 wait_TBE(ps2port->addr);
194 gscps2_flush(ps2port);
198 * gscps2_reset() - resets the PS/2 port
201 static void gscps2_reset(struct gscps2port *ps2port)
203 char *addr = ps2port->addr;
206 /* reset the interface */
207 spin_lock_irqsave(&ps2port->lock, flags);
208 gscps2_flush(ps2port);
209 writeb(0xff, addr+GSC_RESET);
210 gscps2_flush(ps2port);
211 spin_unlock_irqrestore(&ps2port->lock, flags);
214 static LIST_HEAD(ps2port_list);
217 * gscps2_interrupt() - Interruption service routine
219 * This function reads received PS/2 bytes and processes them on
221 * The problematic part here is, that the keyboard and mouse PS/2 port
222 * share the same interrupt and it's not possible to send data if any
223 * one of them holds input data. To solve this problem we try to receive
224 * the data as fast as possible and handle the reporting to the upper layer
228 static irqreturn_t gscps2_interrupt(int irq, void *dev)
230 struct gscps2port *ps2port;
232 list_for_each_entry(ps2port, &ps2port_list, node) {
235 spin_lock_irqsave(&ps2port->lock, flags);
237 while ( (ps2port->buffer[ps2port->append].str =
238 gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
239 ps2port->buffer[ps2port->append].data =
240 gscps2_readb_input(ps2port->addr);
241 ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
244 spin_unlock_irqrestore(&ps2port->lock, flags);
246 } /* list_for_each_entry */
248 /* all data was read from the ports - now report the data to upper layer */
250 list_for_each_entry(ps2port, &ps2port_list, node) {
252 while (ps2port->act != ps2port->append) {
254 unsigned int rxflags;
257 /* Did new data arrived while we read existing data ?
258 If yes, exit now and let the new irq handler start over again */
259 if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
262 status = ps2port->buffer[ps2port->act].str;
263 data = ps2port->buffer[ps2port->act].data;
265 ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
266 rxflags = ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
267 ((status & GSC_STAT_PERR) ? SERIO_PARITY : 0 );
269 serio_interrupt(ps2port->port, data, rxflags);
273 } /* list_for_each_entry */
280 * gscps2_write() - send a byte out through the aux interface.
283 static int gscps2_write(struct serio *port, unsigned char data)
285 struct gscps2port *ps2port = port->port_data;
287 if (!gscps2_writeb_output(ps2port, data)) {
288 printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
295 * gscps2_open() is called when a port is opened by the higher layer.
296 * It resets and enables the port.
299 static int gscps2_open(struct serio *port)
301 struct gscps2port *ps2port = port->port_data;
303 gscps2_reset(ps2port);
306 gscps2_enable(ps2port, ENABLE);
308 gscps2_interrupt(0, NULL);
314 * gscps2_close() disables the port
317 static void gscps2_close(struct serio *port)
319 struct gscps2port *ps2port = port->port_data;
320 gscps2_enable(ps2port, DISABLE);
324 * gscps2_probe() - Probes PS2 devices
325 * @return: success/error report
328 static int __init gscps2_probe(struct parisc_device *dev)
330 struct gscps2port *ps2port;
332 unsigned long hpa = dev->hpa.start;
338 /* Offset for DINO PS/2. Works with LASI even */
339 if (dev->id.sversion == 0x96)
340 hpa += GSC_DINO_OFFSET;
342 ps2port = kzalloc(sizeof(struct gscps2port), GFP_KERNEL);
343 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
344 if (!ps2port || !serio) {
349 dev_set_drvdata(&dev->dev, ps2port);
351 ps2port->port = serio;
352 ps2port->padev = dev;
353 ps2port->addr = ioremap_nocache(hpa, GSC_STATUS + 4);
354 spin_lock_init(&ps2port->lock);
356 gscps2_reset(ps2port);
357 ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
359 snprintf(serio->name, sizeof(serio->name), "gsc-ps2-%s",
360 (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
361 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
362 serio->id.type = SERIO_8042;
363 serio->write = gscps2_write;
364 serio->open = gscps2_open;
365 serio->close = gscps2_close;
366 serio->port_data = ps2port;
367 serio->dev.parent = &dev->dev;
370 if (request_irq(dev->irq, gscps2_interrupt, IRQF_SHARED, ps2port->port->name, ps2port))
373 if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
374 printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
381 if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
385 pr_info("serio: %s port at 0x%08lx irq %d @ %s\n",
389 ps2port->port->phys);
391 serio_register_port(ps2port->port);
393 list_add_tail(&ps2port->node, &ps2port_list);
398 free_irq(dev->irq, ps2port);
401 iounmap(ps2port->addr);
402 release_mem_region(dev->hpa.start, GSC_STATUS + 4);
411 * gscps2_remove() - Removes PS2 devices
412 * @return: success/error report
415 static int __exit gscps2_remove(struct parisc_device *dev)
417 struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
419 serio_unregister_port(ps2port->port);
420 free_irq(dev->irq, ps2port);
421 gscps2_flush(ps2port);
422 list_del(&ps2port->node);
423 iounmap(ps2port->addr);
425 release_mem_region(dev->hpa, GSC_STATUS + 4);
427 dev_set_drvdata(&dev->dev, NULL);
433 static const struct parisc_device_id gscps2_device_tbl[] __initconst = {
434 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
436 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
438 { 0, } /* 0 terminated list */
440 MODULE_DEVICE_TABLE(parisc, gscps2_device_tbl);
442 static struct parisc_driver parisc_ps2_driver __refdata = {
444 .id_table = gscps2_device_tbl,
445 .probe = gscps2_probe,
446 .remove = __exit_p(gscps2_remove),
449 static int __init gscps2_init(void)
451 register_parisc_driver(&parisc_ps2_driver);
455 static void __exit gscps2_exit(void)
457 unregister_parisc_driver(&parisc_ps2_driver);
461 module_init(gscps2_init);
462 module_exit(gscps2_exit);