blob: 452c5f3e612937c61050fa7765fee5bcb8b55341 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/hil/hilkbd.c
3 *
4 * Copyright (C) 1998 Philip Blundell <philb@gnu.org>
5 * Copyright (C) 1999 Matthew Wilcox <willy@bofh.ai>
Helge Deller102c8c72006-03-26 07:41:55 -07006 * Copyright (C) 1999-2006 Helge Deller <deller@gmx.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Very basic HP Human Interface Loop (HIL) driver.
9 * This driver handles the keyboard on HP300 (m68k) and on some
10 * HP700 (parisc) series machines.
11 *
12 *
13 * This file is subject to the terms and conditions of the GNU General Public
14 * License version 2. See the file COPYING in the main directory of this
15 * archive for more details.
16 */
17
18#include <linux/pci_ids.h>
19#include <linux/ioport.h>
20#include <linux/module.h>
21#include <linux/config.h>
22#include <linux/errno.h>
23#include <linux/input.h>
24#include <linux/init.h>
Matthew Wilcox6ab0f5c2005-10-21 22:58:51 -040025#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/hil.h>
27#include <linux/spinlock.h>
28
29
30MODULE_AUTHOR("Philip Blundell, Matthew Wilcox, Helge Deller");
31MODULE_DESCRIPTION("HIL keyboard driver (basic functionality)");
32MODULE_LICENSE("GPL v2");
33
34
35#if defined(CONFIG_PARISC)
36
37 #include <asm/io.h>
38 #include <asm/hardware.h>
39 #include <asm/parisc-device.h>
40 static unsigned long hil_base; /* HPA for the HIL device */
41 static unsigned int hil_irq;
42 #define HILBASE hil_base /* HPPA (parisc) port address */
43 #define HIL_DATA 0x800
44 #define HIL_CMD 0x801
45 #define HIL_IRQ hil_irq
46 #define hil_readb(p) gsc_readb(p)
47 #define hil_writeb(v,p) gsc_writeb((v),(p))
48
49#elif defined(CONFIG_HP300)
50
51 #define HILBASE 0xf0428000 /* HP300 (m86k) port address */
52 #define HIL_DATA 0x1
53 #define HIL_CMD 0x3
54 #define HIL_IRQ 2
55 #define hil_readb(p) readb(p)
56 #define hil_writeb(v,p) writeb((v),(p))
57
58#else
59#error "HIL is not supported on this platform"
60#endif
61
62
63
64/* HIL helper functions */
65
66#define hil_busy() (hil_readb(HILBASE + HIL_CMD) & HIL_BUSY)
67#define hil_data_available() (hil_readb(HILBASE + HIL_CMD) & HIL_DATA_RDY)
68#define hil_status() (hil_readb(HILBASE + HIL_CMD))
69#define hil_command(x) do { hil_writeb((x), HILBASE + HIL_CMD); } while (0)
70#define hil_read_data() (hil_readb(HILBASE + HIL_DATA))
71#define hil_write_data(x) do { hil_writeb((x), HILBASE + HIL_DATA); } while (0)
72
73/* HIL constants */
74
75#define HIL_BUSY 0x02
76#define HIL_DATA_RDY 0x01
77
78#define HIL_SETARD 0xA0 /* set auto-repeat delay */
79#define HIL_SETARR 0xA2 /* set auto-repeat rate */
80#define HIL_SETTONE 0xA3 /* set tone generator */
81#define HIL_CNMT 0xB2 /* clear nmi */
82#define HIL_INTON 0x5C /* Turn on interrupts. */
83#define HIL_INTOFF 0x5D /* Turn off interrupts. */
84
85#define HIL_READKBDSADR 0xF9
86#define HIL_WRITEKBDSADR 0xE9
87
88static unsigned int hphilkeyb_keycode[HIL_KEYCODES_SET1_TBLSIZE] =
89 { HIL_KEYCODES_SET1 };
90
91/* HIL structure */
92static struct {
Helge Deller102c8c72006-03-26 07:41:55 -070093 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 unsigned int curdev;
96
97 unsigned char s;
98 unsigned char c;
99 int valid;
100
101 unsigned char data[16];
102 unsigned int ptr;
103 spinlock_t lock;
104
105 void *dev_id; /* native bus device */
106} hil_dev;
107
108
109static void poll_finished(void)
110{
111 int down;
112 int key;
113 unsigned char scode;
114
115 switch (hil_dev.data[0]) {
116 case 0x40:
117 down = (hil_dev.data[1] & 1) == 0;
118 scode = hil_dev.data[1] >> 1;
119 key = hphilkeyb_keycode[scode];
Helge Deller102c8c72006-03-26 07:41:55 -0700120 input_report_key(hil_dev.dev, key, down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 break;
122 }
123 hil_dev.curdev = 0;
124}
125
126static inline void handle_status(unsigned char s, unsigned char c)
127{
128 if (c & 0x8) {
129 /* End of block */
130 if (c & 0x10)
131 poll_finished();
132 } else {
133 if (c & 0x10) {
134 if (hil_dev.curdev)
135 poll_finished(); /* just in case */
136 hil_dev.curdev = c & 7;
137 hil_dev.ptr = 0;
138 }
139 }
140}
141
142static inline void handle_data(unsigned char s, unsigned char c)
143{
144 if (hil_dev.curdev) {
145 hil_dev.data[hil_dev.ptr++] = c;
146 hil_dev.ptr &= 15;
147 }
148}
149
150
151/*
152 * Handle HIL interrupts.
153 */
154static irqreturn_t hil_interrupt(int irq, void *handle, struct pt_regs *regs)
155{
156 unsigned char s, c;
157
158 s = hil_status();
159 c = hil_read_data();
160
161 switch (s >> 4) {
162 case 0x5:
163 handle_status(s, c);
164 break;
165 case 0x6:
166 handle_data(s, c);
167 break;
168 case 0x4:
169 hil_dev.s = s;
170 hil_dev.c = c;
171 mb();
172 hil_dev.valid = 1;
173 break;
174 }
175 return IRQ_HANDLED;
176}
177
178/*
179 * Send a command to the HIL
180 */
181
182static void hil_do(unsigned char cmd, unsigned char *data, unsigned int len)
183{
184 unsigned long flags;
185
186 spin_lock_irqsave(&hil_dev.lock, flags);
187 while (hil_busy())
188 /* wait */;
189 hil_command(cmd);
190 while (len--) {
191 while (hil_busy())
192 /* wait */;
193 hil_write_data(*(data++));
194 }
195 spin_unlock_irqrestore(&hil_dev.lock, flags);
196}
197
198
199/*
200 * Initialise HIL.
201 */
202
203static int __init
204hil_keyb_init(void)
205{
206 unsigned char c;
207 unsigned int i, kbid;
208 wait_queue_head_t hil_wait;
209
Helge Deller102c8c72006-03-26 07:41:55 -0700210 if (hil_dev.dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return -ENODEV; /* already initialized */
212 }
Helge Deller102c8c72006-03-26 07:41:55 -0700213
214 hil_dev.dev = input_allocate_device();
215 if (!hil_dev.dev)
216 return -ENOMEM;
217 hil_dev.dev->private = &hil_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219#if defined(CONFIG_HP300)
220 if (!hwreg_present((void *)(HILBASE + HIL_DATA)))
221 return -ENODEV;
222
223 request_region(HILBASE+HIL_DATA, 2, "hil");
224#endif
225
226 request_irq(HIL_IRQ, hil_interrupt, 0, "hil", hil_dev.dev_id);
227
228 /* Turn on interrupts */
229 hil_do(HIL_INTON, NULL, 0);
230
231 /* Look for keyboards */
232 hil_dev.valid = 0; /* clear any pending data */
233 hil_do(HIL_READKBDSADR, NULL, 0);
234
235 init_waitqueue_head(&hil_wait);
236 wait_event_interruptible_timeout(hil_wait, hil_dev.valid, 3*HZ);
237 if (!hil_dev.valid) {
238 printk(KERN_WARNING "HIL: timed out, assuming no keyboard present.\n");
239 }
240
241 c = hil_dev.c;
242 hil_dev.valid = 0;
243 if (c == 0) {
244 kbid = -1;
245 printk(KERN_WARNING "HIL: no keyboard present.\n");
246 } else {
247 kbid = ffz(~c);
248 /* printk(KERN_INFO "HIL: keyboard found at id %d\n", kbid); */
249 }
250
251 /* set it to raw mode */
252 c = 0;
253 hil_do(HIL_WRITEKBDSADR, &c, 1);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 for (i = 0; i < HIL_KEYCODES_SET1_TBLSIZE; i++)
256 if (hphilkeyb_keycode[i] != KEY_RESERVED)
Helge Deller102c8c72006-03-26 07:41:55 -0700257 set_bit(hphilkeyb_keycode[i], hil_dev.dev->keybit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Helge Deller102c8c72006-03-26 07:41:55 -0700259 hil_dev.dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
260 hil_dev.dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);
261 hil_dev.dev->keycodemax = HIL_KEYCODES_SET1_TBLSIZE;
262 hil_dev.dev->keycodesize = sizeof(hphilkeyb_keycode[0]);
263 hil_dev.dev->keycode = hphilkeyb_keycode;
264 hil_dev.dev->name = "HIL keyboard";
265 hil_dev.dev->phys = "hpkbd/input0";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Helge Deller102c8c72006-03-26 07:41:55 -0700267 hil_dev.dev->id.bustype = BUS_HIL;
268 hil_dev.dev->id.vendor = PCI_VENDOR_ID_HP;
269 hil_dev.dev->id.product = 0x0001;
270 hil_dev.dev->id.version = 0x0010;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Helge Deller102c8c72006-03-26 07:41:55 -0700272 input_register_device(hil_dev.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 printk(KERN_INFO "input: %s, ID %d at 0x%08lx (irq %d) found and attached\n",
Helge Deller102c8c72006-03-26 07:41:55 -0700274 hil_dev.dev->name, kbid, HILBASE, HIL_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 return 0;
277}
278
279#if defined(CONFIG_PARISC)
280static int __init
281hil_init_chip(struct parisc_device *dev)
282{
283 if (!dev->irq) {
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400284 printk(KERN_WARNING "HIL: IRQ not found for HIL bus at 0x%08lx\n", dev->hpa.start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -ENODEV;
286 }
287
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400288 hil_base = dev->hpa.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 hil_irq = dev->irq;
290 hil_dev.dev_id = dev;
291
292 printk(KERN_INFO "Found HIL bus at 0x%08lx, IRQ %d\n", hil_base, hil_irq);
293
294 return hil_keyb_init();
295}
296
297static struct parisc_device_id hil_tbl[] = {
298 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00073 },
299 { 0, }
300};
301
302MODULE_DEVICE_TABLE(parisc, hil_tbl);
303
304static struct parisc_driver hil_driver = {
Matthew Wilcoxbdad1f82005-10-21 22:36:23 -0400305 .name = "hil",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 .id_table = hil_tbl,
307 .probe = hil_init_chip,
308};
309#endif /* CONFIG_PARISC */
310
311
312
313
314
315static int __init hil_init(void)
316{
317#if defined(CONFIG_PARISC)
318 return register_parisc_driver(&hil_driver);
319#else
320 return hil_keyb_init();
321#endif
322}
323
324
325static void __exit hil_exit(void)
326{
327 if (HIL_IRQ) {
328 disable_irq(HIL_IRQ);
329 free_irq(HIL_IRQ, hil_dev.dev_id);
330 }
331
332 /* Turn off interrupts */
333 hil_do(HIL_INTOFF, NULL, 0);
334
Helge Deller102c8c72006-03-26 07:41:55 -0700335 input_unregister_device(hil_dev.dev);
336
337 input_free_device(hil_dev.dev);
338 hil_dev.dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340#if defined(CONFIG_PARISC)
341 unregister_parisc_driver(&hil_driver);
342#else
343 release_region(HILBASE+HIL_DATA, 2);
344#endif
345}
346
347module_init(hil_init);
348module_exit(hil_exit);
349