[PATCH] Char: moxa, do not initialize global static
[linux-2.6/kvm.git] / drivers / input / keyboard / hilkbd.c
blob35461eab2fafc3f88d454120637562abdff84a85
1 /*
2 * linux/drivers/hil/hilkbd.c
4 * Copyright (C) 1998 Philip Blundell <philb@gnu.org>
5 * Copyright (C) 1999 Matthew Wilcox <willy@bofh.ai>
6 * Copyright (C) 1999-2006 Helge Deller <deller@gmx.de>
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.
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.
18 #include <linux/pci_ids.h>
19 #include <linux/ioport.h>
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/input.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/hil.h>
26 #include <linux/io.h>
27 #include <linux/spinlock.h>
28 #include <asm/irq.h>
29 #ifdef CONFIG_HP300
30 #include <asm/hwtest.h>
31 #endif
34 MODULE_AUTHOR("Philip Blundell, Matthew Wilcox, Helge Deller");
35 MODULE_DESCRIPTION("HIL keyboard driver (basic functionality)");
36 MODULE_LICENSE("GPL v2");
39 #if defined(CONFIG_PARISC)
41 #include <asm/io.h>
42 #include <asm/hardware.h>
43 #include <asm/parisc-device.h>
44 static unsigned long hil_base; /* HPA for the HIL device */
45 static unsigned int hil_irq;
46 #define HILBASE hil_base /* HPPA (parisc) port address */
47 #define HIL_DATA 0x800
48 #define HIL_CMD 0x801
49 #define HIL_IRQ hil_irq
50 #define hil_readb(p) gsc_readb(p)
51 #define hil_writeb(v,p) gsc_writeb((v),(p))
53 #elif defined(CONFIG_HP300)
55 #define HILBASE 0xf0428000 /* HP300 (m86k) port address */
56 #define HIL_DATA 0x1
57 #define HIL_CMD 0x3
58 #define HIL_IRQ 2
59 #define hil_readb(p) readb(p)
60 #define hil_writeb(v,p) writeb((v),(p))
62 #else
63 #error "HIL is not supported on this platform"
64 #endif
68 /* HIL helper functions */
70 #define hil_busy() (hil_readb(HILBASE + HIL_CMD) & HIL_BUSY)
71 #define hil_data_available() (hil_readb(HILBASE + HIL_CMD) & HIL_DATA_RDY)
72 #define hil_status() (hil_readb(HILBASE + HIL_CMD))
73 #define hil_command(x) do { hil_writeb((x), HILBASE + HIL_CMD); } while (0)
74 #define hil_read_data() (hil_readb(HILBASE + HIL_DATA))
75 #define hil_write_data(x) do { hil_writeb((x), HILBASE + HIL_DATA); } while (0)
77 /* HIL constants */
79 #define HIL_BUSY 0x02
80 #define HIL_DATA_RDY 0x01
82 #define HIL_SETARD 0xA0 /* set auto-repeat delay */
83 #define HIL_SETARR 0xA2 /* set auto-repeat rate */
84 #define HIL_SETTONE 0xA3 /* set tone generator */
85 #define HIL_CNMT 0xB2 /* clear nmi */
86 #define HIL_INTON 0x5C /* Turn on interrupts. */
87 #define HIL_INTOFF 0x5D /* Turn off interrupts. */
89 #define HIL_READKBDSADR 0xF9
90 #define HIL_WRITEKBDSADR 0xE9
92 static unsigned int hphilkeyb_keycode[HIL_KEYCODES_SET1_TBLSIZE] =
93 { HIL_KEYCODES_SET1 };
95 /* HIL structure */
96 static struct {
97 struct input_dev *dev;
99 unsigned int curdev;
101 unsigned char s;
102 unsigned char c;
103 int valid;
105 unsigned char data[16];
106 unsigned int ptr;
107 spinlock_t lock;
109 void *dev_id; /* native bus device */
110 } hil_dev;
113 static void poll_finished(void)
115 int down;
116 int key;
117 unsigned char scode;
119 switch (hil_dev.data[0]) {
120 case 0x40:
121 down = (hil_dev.data[1] & 1) == 0;
122 scode = hil_dev.data[1] >> 1;
123 key = hphilkeyb_keycode[scode];
124 input_report_key(hil_dev.dev, key, down);
125 break;
127 hil_dev.curdev = 0;
130 static inline void handle_status(unsigned char s, unsigned char c)
132 if (c & 0x8) {
133 /* End of block */
134 if (c & 0x10)
135 poll_finished();
136 } else {
137 if (c & 0x10) {
138 if (hil_dev.curdev)
139 poll_finished(); /* just in case */
140 hil_dev.curdev = c & 7;
141 hil_dev.ptr = 0;
146 static inline void handle_data(unsigned char s, unsigned char c)
148 if (hil_dev.curdev) {
149 hil_dev.data[hil_dev.ptr++] = c;
150 hil_dev.ptr &= 15;
156 * Handle HIL interrupts.
158 static irqreturn_t hil_interrupt(int irq, void *handle)
160 unsigned char s, c;
162 s = hil_status();
163 c = hil_read_data();
165 switch (s >> 4) {
166 case 0x5:
167 handle_status(s, c);
168 break;
169 case 0x6:
170 handle_data(s, c);
171 break;
172 case 0x4:
173 hil_dev.s = s;
174 hil_dev.c = c;
175 mb();
176 hil_dev.valid = 1;
177 break;
179 return IRQ_HANDLED;
183 * Send a command to the HIL
186 static void hil_do(unsigned char cmd, unsigned char *data, unsigned int len)
188 unsigned long flags;
190 spin_lock_irqsave(&hil_dev.lock, flags);
191 while (hil_busy())
192 /* wait */;
193 hil_command(cmd);
194 while (len--) {
195 while (hil_busy())
196 /* wait */;
197 hil_write_data(*(data++));
199 spin_unlock_irqrestore(&hil_dev.lock, flags);
204 * Initialise HIL.
207 static int __init
208 hil_keyb_init(void)
210 unsigned char c;
211 unsigned int i, kbid;
212 wait_queue_head_t hil_wait;
214 if (hil_dev.dev) {
215 return -ENODEV; /* already initialized */
218 hil_dev.dev = input_allocate_device();
219 if (!hil_dev.dev)
220 return -ENOMEM;
221 hil_dev.dev->private = &hil_dev;
223 #if defined(CONFIG_HP300)
224 if (!hwreg_present((void *)(HILBASE + HIL_DATA)))
225 return -ENODEV;
227 request_region(HILBASE+HIL_DATA, 2, "hil");
228 #endif
230 request_irq(HIL_IRQ, hil_interrupt, 0, "hil", hil_dev.dev_id);
232 /* Turn on interrupts */
233 hil_do(HIL_INTON, NULL, 0);
235 /* Look for keyboards */
236 hil_dev.valid = 0; /* clear any pending data */
237 hil_do(HIL_READKBDSADR, NULL, 0);
239 init_waitqueue_head(&hil_wait);
240 wait_event_interruptible_timeout(hil_wait, hil_dev.valid, 3*HZ);
241 if (!hil_dev.valid) {
242 printk(KERN_WARNING "HIL: timed out, assuming no keyboard present.\n");
245 c = hil_dev.c;
246 hil_dev.valid = 0;
247 if (c == 0) {
248 kbid = -1;
249 printk(KERN_WARNING "HIL: no keyboard present.\n");
250 } else {
251 kbid = ffz(~c);
252 /* printk(KERN_INFO "HIL: keyboard found at id %d\n", kbid); */
255 /* set it to raw mode */
256 c = 0;
257 hil_do(HIL_WRITEKBDSADR, &c, 1);
259 for (i = 0; i < HIL_KEYCODES_SET1_TBLSIZE; i++)
260 if (hphilkeyb_keycode[i] != KEY_RESERVED)
261 set_bit(hphilkeyb_keycode[i], hil_dev.dev->keybit);
263 hil_dev.dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
264 hil_dev.dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);
265 hil_dev.dev->keycodemax = HIL_KEYCODES_SET1_TBLSIZE;
266 hil_dev.dev->keycodesize = sizeof(hphilkeyb_keycode[0]);
267 hil_dev.dev->keycode = hphilkeyb_keycode;
268 hil_dev.dev->name = "HIL keyboard";
269 hil_dev.dev->phys = "hpkbd/input0";
271 hil_dev.dev->id.bustype = BUS_HIL;
272 hil_dev.dev->id.vendor = PCI_VENDOR_ID_HP;
273 hil_dev.dev->id.product = 0x0001;
274 hil_dev.dev->id.version = 0x0010;
276 input_register_device(hil_dev.dev);
277 printk(KERN_INFO "input: %s, ID %d at 0x%08lx (irq %d) found and attached\n",
278 hil_dev.dev->name, kbid, HILBASE, HIL_IRQ);
280 return 0;
283 #if defined(CONFIG_PARISC)
284 static int __init
285 hil_init_chip(struct parisc_device *dev)
287 if (!dev->irq) {
288 printk(KERN_WARNING "HIL: IRQ not found for HIL bus at 0x%08lx\n", dev->hpa.start);
289 return -ENODEV;
292 hil_base = dev->hpa.start;
293 hil_irq = dev->irq;
294 hil_dev.dev_id = dev;
296 printk(KERN_INFO "Found HIL bus at 0x%08lx, IRQ %d\n", hil_base, hil_irq);
298 return hil_keyb_init();
301 static struct parisc_device_id hil_tbl[] = {
302 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00073 },
303 { 0, }
306 MODULE_DEVICE_TABLE(parisc, hil_tbl);
308 static struct parisc_driver hil_driver = {
309 .name = "hil",
310 .id_table = hil_tbl,
311 .probe = hil_init_chip,
313 #endif /* CONFIG_PARISC */
319 static int __init hil_init(void)
321 #if defined(CONFIG_PARISC)
322 return register_parisc_driver(&hil_driver);
323 #else
324 return hil_keyb_init();
325 #endif
329 static void __exit hil_exit(void)
331 if (HIL_IRQ) {
332 disable_irq(HIL_IRQ);
333 free_irq(HIL_IRQ, hil_dev.dev_id);
336 /* Turn off interrupts */
337 hil_do(HIL_INTOFF, NULL, 0);
339 input_unregister_device(hil_dev.dev);
341 hil_dev.dev = NULL;
343 #if defined(CONFIG_PARISC)
344 unregister_parisc_driver(&hil_driver);
345 #else
346 release_region(HILBASE+HIL_DATA, 2);
347 #endif
350 module_init(hil_init);
351 module_exit(hil_exit);