blob: 8d9ba0c3827c5b2aac77e8321324729ea642d7b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/input/serio/gscps2.c
3 *
Helge Deller5076c152006-03-27 12:52:15 -07004 * Copyright (c) 2004-2006 Helge Deller <deller@gmx.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (c) 2002 Laurent Canet <canetl@esiee.fr>
6 * Copyright (c) 2002 Thibaut Varene <varenet@parisc-linux.org>
7 *
8 * Pieces of code based on linux-2.4's hp_mouse.c & hp_keyb.c
Dmitry Torokhova0ee2032009-12-11 21:50:47 -08009 * Copyright (c) 1999 Alex deVries <alex@onefishtwo.ca>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/init.h>
26#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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
40MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>, Thibaut Varene <varenet@parisc-linux.org>, Helge Deller <deller@gmx.de>");
41MODULE_DESCRIPTION("HP GSC PS2 port driver");
42MODULE_LICENSE("GPL");
43MODULE_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
David Howells7d12e782006-10-05 14:55:46 +010086static irqreturn_t gscps2_interrupt(int irq, void *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#define BUFFER_SIZE 0x0f
89
90/* GSC PS/2 port device struct */
91struct 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};
104
105/*
106 * Various HW level routines
107 */
108
109#define gscps2_readb_input(x) readb((x)+GSC_RCVDATA)
110#define gscps2_readb_control(x) readb((x)+GSC_CONTROL)
111#define gscps2_readb_status(x) readb((x)+GSC_STATUS)
112#define gscps2_writeb_control(x, y) writeb((x), (y)+GSC_CONTROL)
113
114
115/*
116 * wait_TBE() - wait for Transmit Buffer Empty
117 */
118
119static int wait_TBE(char *addr)
120{
121 int timeout = 25000; /* device is expected to react within 250 msec */
122 while (gscps2_readb_status(addr) & GSC_STAT_TBNE) {
123 if (!--timeout)
124 return 0; /* This should not happen */
125 udelay(10);
126 }
127 return 1;
128}
129
130
131/*
132 * gscps2_flush() - flush the receive buffer
133 */
134
135static void gscps2_flush(struct gscps2port *ps2port)
136{
137 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
138 gscps2_readb_input(ps2port->addr);
139 ps2port->act = ps2port->append = 0;
140}
141
142/*
143 * gscps2_writeb_output() - write a byte to the port
144 *
Joe Perches5047fab2008-02-03 17:15:20 +0200145 * returns 1 on success, 0 on error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 */
147
148static inline int gscps2_writeb_output(struct gscps2port *ps2port, u8 data)
149{
150 unsigned long flags;
151 char *addr = ps2port->addr;
152
153 if (!wait_TBE(addr)) {
154 printk(KERN_DEBUG PFX "timeout - could not write byte %#x\n", data);
155 return 0;
156 }
157
158 while (gscps2_readb_status(ps2port->addr) & GSC_STAT_RBNE)
159 /* wait */;
160
161 spin_lock_irqsave(&ps2port->lock, flags);
162 writeb(data, addr+GSC_XMTDATA);
163 spin_unlock_irqrestore(&ps2port->lock, flags);
164
165 /* this is ugly, but due to timing of the port it seems to be necessary. */
166 mdelay(6);
167
168 /* make sure any received data is returned as fast as possible */
169 /* this is important e.g. when we set the LEDs on the keyboard */
Matthew Wilcoxbe577a52006-10-06 20:47:23 -0600170 gscps2_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 return 1;
173}
174
175
176/*
177 * gscps2_enable() - enables or disables the port
178 */
179
180static void gscps2_enable(struct gscps2port *ps2port, int enable)
181{
182 unsigned long flags;
183 u8 data;
184
185 /* now enable/disable the port */
186 spin_lock_irqsave(&ps2port->lock, flags);
187 gscps2_flush(ps2port);
188 data = gscps2_readb_control(ps2port->addr);
189 if (enable)
190 data |= GSC_CTRL_ENBL;
191 else
192 data &= ~GSC_CTRL_ENBL;
193 gscps2_writeb_control(data, ps2port->addr);
194 spin_unlock_irqrestore(&ps2port->lock, flags);
195 wait_TBE(ps2port->addr);
196 gscps2_flush(ps2port);
197}
198
199/*
200 * gscps2_reset() - resets the PS/2 port
201 */
202
203static void gscps2_reset(struct gscps2port *ps2port)
204{
205 char *addr = ps2port->addr;
206 unsigned long flags;
207
208 /* reset the interface */
209 spin_lock_irqsave(&ps2port->lock, flags);
210 gscps2_flush(ps2port);
211 writeb(0xff, addr+GSC_RESET);
212 gscps2_flush(ps2port);
213 spin_unlock_irqrestore(&ps2port->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216static LIST_HEAD(ps2port_list);
217
218/**
219 * gscps2_interrupt() - Interruption service routine
220 *
221 * This function reads received PS/2 bytes and processes them on
222 * all interfaces.
223 * The problematic part here is, that the keyboard and mouse PS/2 port
224 * share the same interrupt and it's not possible to send data if any
225 * one of them holds input data. To solve this problem we try to receive
226 * the data as fast as possible and handle the reporting to the upper layer
227 * later.
228 */
229
David Howells7d12e782006-10-05 14:55:46 +0100230static irqreturn_t gscps2_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct gscps2port *ps2port;
233
234 list_for_each_entry(ps2port, &ps2port_list, node) {
235
236 unsigned long flags;
237 spin_lock_irqsave(&ps2port->lock, flags);
238
239 while ( (ps2port->buffer[ps2port->append].str =
240 gscps2_readb_status(ps2port->addr)) & GSC_STAT_RBNE ) {
241 ps2port->buffer[ps2port->append].data =
242 gscps2_readb_input(ps2port->addr);
243 ps2port->append = ((ps2port->append+1) & BUFFER_SIZE);
244 }
245
246 spin_unlock_irqrestore(&ps2port->lock, flags);
247
248 } /* list_for_each_entry */
249
250 /* all data was read from the ports - now report the data to upper layer */
251
252 list_for_each_entry(ps2port, &ps2port_list, node) {
253
254 while (ps2port->act != ps2port->append) {
255
256 unsigned int rxflags;
257 u8 data, status;
258
259 /* Did new data arrived while we read existing data ?
260 If yes, exit now and let the new irq handler start over again */
261 if (gscps2_readb_status(ps2port->addr) & GSC_STAT_CMPINTR)
262 return IRQ_HANDLED;
263
264 status = ps2port->buffer[ps2port->act].str;
265 data = ps2port->buffer[ps2port->act].data;
266
267 ps2port->act = ((ps2port->act+1) & BUFFER_SIZE);
268 rxflags = ((status & GSC_STAT_TERR) ? SERIO_TIMEOUT : 0 ) |
269 ((status & GSC_STAT_PERR) ? SERIO_PARITY : 0 );
270
David Howells7d12e782006-10-05 14:55:46 +0100271 serio_interrupt(ps2port->port, data, rxflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 } /* while() */
274
275 } /* list_for_each_entry */
276
277 return IRQ_HANDLED;
278}
279
280
281/*
282 * gscps2_write() - send a byte out through the aux interface.
283 */
284
285static int gscps2_write(struct serio *port, unsigned char data)
286{
287 struct gscps2port *ps2port = port->port_data;
288
289 if (!gscps2_writeb_output(ps2port, data)) {
290 printk(KERN_DEBUG PFX "sending byte %#x failed.\n", data);
291 return -1;
292 }
293 return 0;
294}
295
296/*
297 * gscps2_open() is called when a port is opened by the higher layer.
298 * It resets and enables the port.
299 */
300
301static int gscps2_open(struct serio *port)
302{
303 struct gscps2port *ps2port = port->port_data;
304
305 gscps2_reset(ps2port);
306
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400307 /* enable it */
308 gscps2_enable(ps2port, ENABLE);
309
Matthew Wilcoxbe577a52006-10-06 20:47:23 -0600310 gscps2_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return 0;
313}
314
315/*
316 * gscps2_close() disables the port
317 */
318
319static void gscps2_close(struct serio *port)
320{
321 struct gscps2port *ps2port = port->port_data;
322 gscps2_enable(ps2port, DISABLE);
323}
324
325/**
326 * gscps2_probe() - Probes PS2 devices
327 * @return: success/error report
328 */
329
Bill Pemberton5298cc42012-11-23 21:38:25 -0800330static int gscps2_probe(struct parisc_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct gscps2port *ps2port;
333 struct serio *serio;
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400334 unsigned long hpa = dev->hpa.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 int ret;
336
337 if (!dev->irq)
338 return -ENODEV;
339
340 /* Offset for DINO PS/2. Works with LASI even */
341 if (dev->id.sversion == 0x96)
342 hpa += GSC_DINO_OFFSET;
343
Mariusz Kozlowski7460caf2007-08-30 00:18:00 -0400344 ps2port = kzalloc(sizeof(struct gscps2port), GFP_KERNEL);
345 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!ps2port || !serio) {
347 ret = -ENOMEM;
348 goto fail_nomem;
349 }
350
351 dev_set_drvdata(&dev->dev, ps2port);
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 ps2port->port = serio;
354 ps2port->padev = dev;
Helge Deller5076c152006-03-27 12:52:15 -0700355 ps2port->addr = ioremap_nocache(hpa, GSC_STATUS + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_lock_init(&ps2port->lock);
357
358 gscps2_reset(ps2port);
359 ps2port->id = readb(ps2port->addr + GSC_ID) & 0x0f;
360
Guy Martin7bfbeae2010-11-02 15:23:08 +0000361 snprintf(serio->name, sizeof(serio->name), "gsc-ps2-%s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 (ps2port->id == GSC_ID_KEYBOARD) ? "keyboard" : "mouse");
Kay Sievers4e8718a2009-01-29 22:56:08 -0800363 strlcpy(serio->phys, dev_name(&dev->dev), sizeof(serio->phys));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 serio->id.type = SERIO_8042;
365 serio->write = gscps2_write;
366 serio->open = gscps2_open;
367 serio->close = gscps2_close;
368 serio->port_data = ps2port;
369 serio->dev.parent = &dev->dev;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 ret = -EBUSY;
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700372 if (request_irq(dev->irq, gscps2_interrupt, IRQF_SHARED, ps2port->port->name, ps2port))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 goto fail_miserably;
374
375 if (ps2port->id != GSC_ID_KEYBOARD && ps2port->id != GSC_ID_MOUSE) {
376 printk(KERN_WARNING PFX "Unsupported PS/2 port at 0x%08lx (id=%d) ignored\n",
377 hpa, ps2port->id);
378 ret = -ENODEV;
379 goto fail;
380 }
381
382#if 0
383 if (!request_mem_region(hpa, GSC_STATUS + 4, ps2port->port.name))
384 goto fail;
385#endif
386
387 printk(KERN_INFO "serio: %s port at 0x%p irq %d @ %s\n",
388 ps2port->port->name,
389 ps2port->addr,
390 ps2port->padev->irq,
391 ps2port->port->phys);
392
393 serio_register_port(ps2port->port);
394
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400395 list_add_tail(&ps2port->node, &ps2port_list);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 0;
398
399fail:
400 free_irq(dev->irq, ps2port);
401
402fail_miserably:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 iounmap(ps2port->addr);
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -0400404 release_mem_region(dev->hpa.start, GSC_STATUS + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406fail_nomem:
407 kfree(ps2port);
408 kfree(serio);
409 return ret;
410}
411
412/**
413 * gscps2_remove() - Removes PS2 devices
414 * @return: success/error report
415 */
416
Bill Pembertone2619cf2012-11-23 21:50:47 -0800417static int gscps2_remove(struct parisc_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 struct gscps2port *ps2port = dev_get_drvdata(&dev->dev);
420
421 serio_unregister_port(ps2port->port);
422 free_irq(dev->irq, ps2port);
423 gscps2_flush(ps2port);
424 list_del(&ps2port->node);
425 iounmap(ps2port->addr);
426#if 0
427 release_mem_region(dev->hpa, GSC_STATUS + 4);
428#endif
429 dev_set_drvdata(&dev->dev, NULL);
430 kfree(ps2port);
431 return 0;
432}
433
434
435static struct parisc_device_id gscps2_device_tbl[] = {
436 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00084 }, /* LASI PS/2 */
437#ifdef DINO_TESTED
438 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00096 }, /* DINO PS/2 */
439#endif
440 { 0, } /* 0 terminated list */
441};
442
443static struct parisc_driver parisc_ps2_driver = {
Matthew Wilcoxbdad1f82005-10-21 22:36:23 -0400444 .name = "gsc_ps2",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 .id_table = gscps2_device_tbl,
446 .probe = gscps2_probe,
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800447 .remove = gscps2_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448};
449
450static int __init gscps2_init(void)
451{
452 register_parisc_driver(&parisc_ps2_driver);
453 return 0;
454}
455
456static void __exit gscps2_exit(void)
457{
458 unregister_parisc_driver(&parisc_ps2_driver);
459}
460
461
462module_init(gscps2_init);
463module_exit(gscps2_exit);
464