1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/hil/hilkbd.c
9 * Very basic HP Human Interface Loop (HIL) driver.
10 * This driver handles the keyboard on HP300 (m68k) and on some
11 * HP700 (parisc) series machines.
14 #include <linux/pci_ids.h>
15 #include <linux/ioport.h>
16 #include <linux/module.h>
17 #include <linux/errno.h>
18 #include <linux/input.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/hil.h>
23 #include <linux/sched.h>
24 #include <linux/spinlock.h>
27 #include <asm/hwtest.h>
31 MODULE_AUTHOR("Philip Blundell, Matthew Wilcox, Helge Deller");
32 MODULE_DESCRIPTION("HIL keyboard driver (basic functionality)");
33 MODULE_LICENSE("GPL v2");
36 #if defined(CONFIG_PARISC)
39 #include <asm/hardware.h>
40 #include <asm/parisc-device.h>
41 static unsigned long hil_base; /* HPA for the HIL device */
42 static unsigned int hil_irq;
43 #define HILBASE hil_base /* HPPA (parisc) port address */
44 #define HIL_DATA 0x800
46 #define HIL_IRQ hil_irq
47 #define hil_readb(p) gsc_readb(p)
48 #define hil_writeb(v,p) gsc_writeb((v),(p))
50 #elif defined(CONFIG_HP300)
52 #define HILBASE 0xf0428000UL /* HP300 (m68k) port address */
56 #define hil_readb(p) readb((const volatile void __iomem *)(p))
57 #define hil_writeb(v, p) writeb((v), (volatile void __iomem *)(p))
60 #error "HIL is not supported on this platform"
65 /* HIL helper functions */
67 #define hil_busy() (hil_readb(HILBASE + HIL_CMD) & HIL_BUSY)
68 #define hil_data_available() (hil_readb(HILBASE + HIL_CMD) & HIL_DATA_RDY)
69 #define hil_status() (hil_readb(HILBASE + HIL_CMD))
70 #define hil_command(x) do { hil_writeb((x), HILBASE + HIL_CMD); } while (0)
71 #define hil_read_data() (hil_readb(HILBASE + HIL_DATA))
72 #define hil_write_data(x) do { hil_writeb((x), HILBASE + HIL_DATA); } while (0)
77 #define HIL_DATA_RDY 0x01
79 #define HIL_SETARD 0xA0 /* set auto-repeat delay */
80 #define HIL_SETARR 0xA2 /* set auto-repeat rate */
81 #define HIL_SETTONE 0xA3 /* set tone generator */
82 #define HIL_CNMT 0xB2 /* clear nmi */
83 #define HIL_INTON 0x5C /* Turn on interrupts. */
84 #define HIL_INTOFF 0x5D /* Turn off interrupts. */
86 #define HIL_READKBDSADR 0xF9
87 #define HIL_WRITEKBDSADR 0xE9
89 static unsigned int hphilkeyb_keycode[HIL_KEYCODES_SET1_TBLSIZE] __read_mostly =
90 { HIL_KEYCODES_SET1 };
94 struct input_dev *dev;
102 unsigned char data[16];
106 void *dev_id; /* native bus device */
110 static void poll_finished(void)
116 switch (hil_dev.data[0]) {
118 down = (hil_dev.data[1] & 1) == 0;
119 scode = hil_dev.data[1] >> 1;
120 key = hphilkeyb_keycode[scode];
121 input_report_key(hil_dev.dev, key, down);
128 static inline void handle_status(unsigned char s, unsigned char c)
137 poll_finished(); /* just in case */
138 hil_dev.curdev = c & 7;
145 static inline void handle_data(unsigned char s, unsigned char c)
147 if (hil_dev.curdev) {
148 hil_dev.data[hil_dev.ptr++] = c;
154 /* handle HIL interrupts */
155 static irqreturn_t hil_interrupt(int irq, void *handle)
180 /* send a command to the HIL */
181 static void hil_do(unsigned char cmd, unsigned char *data, unsigned int len)
183 guard(spinlock_irqsave)(&hil_dev.lock);
191 hil_write_data(*(data++));
197 static int hil_keyb_init(void)
200 unsigned int i, kbid;
201 wait_queue_head_t hil_wait;
205 return -ENODEV; /* already initialized */
207 init_waitqueue_head(&hil_wait);
208 spin_lock_init(&hil_dev.lock);
210 hil_dev.dev = input_allocate_device();
214 err = request_irq(HIL_IRQ, hil_interrupt, 0, "hil", hil_dev.dev_id);
216 printk(KERN_ERR "HIL: Can't get IRQ\n");
220 /* Turn on interrupts */
221 hil_do(HIL_INTON, NULL, 0);
223 /* Look for keyboards */
224 hil_dev.valid = 0; /* clear any pending data */
225 hil_do(HIL_READKBDSADR, NULL, 0);
227 wait_event_interruptible_timeout(hil_wait, hil_dev.valid, 3 * HZ);
229 printk(KERN_WARNING "HIL: timed out, assuming no keyboard present\n");
235 printk(KERN_WARNING "HIL: no keyboard present\n");
238 printk(KERN_INFO "HIL: keyboard found at id %d\n", kbid);
241 /* set it to raw mode */
243 hil_do(HIL_WRITEKBDSADR, &c, 1);
245 for (i = 0; i < HIL_KEYCODES_SET1_TBLSIZE; i++)
246 if (hphilkeyb_keycode[i] != KEY_RESERVED)
247 __set_bit(hphilkeyb_keycode[i], hil_dev.dev->keybit);
249 hil_dev.dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
250 hil_dev.dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
251 BIT_MASK(LED_SCROLLL);
252 hil_dev.dev->keycodemax = HIL_KEYCODES_SET1_TBLSIZE;
253 hil_dev.dev->keycodesize= sizeof(hphilkeyb_keycode[0]);
254 hil_dev.dev->keycode = hphilkeyb_keycode;
255 hil_dev.dev->name = "HIL keyboard";
256 hil_dev.dev->phys = "hpkbd/input0";
258 hil_dev.dev->id.bustype = BUS_HIL;
259 hil_dev.dev->id.vendor = PCI_VENDOR_ID_HP;
260 hil_dev.dev->id.product = 0x0001;
261 hil_dev.dev->id.version = 0x0010;
263 err = input_register_device(hil_dev.dev);
265 printk(KERN_ERR "HIL: Can't register device\n");
269 printk(KERN_INFO "input: %s, ID %d at 0x%08lx (irq %d) found and attached\n",
270 hil_dev.dev->name, kbid, HILBASE, HIL_IRQ);
275 hil_do(HIL_INTOFF, NULL, 0);
276 free_irq(HIL_IRQ, hil_dev.dev_id);
278 input_free_device(hil_dev.dev);
283 static void hil_keyb_exit(void)
286 free_irq(HIL_IRQ, hil_dev.dev_id);
288 /* Turn off interrupts */
289 hil_do(HIL_INTOFF, NULL, 0);
291 input_unregister_device(hil_dev.dev);
295 #if defined(CONFIG_PARISC)
296 static int __init hil_probe_chip(struct parisc_device *dev)
298 /* Only allow one HIL keyboard */
303 printk(KERN_WARNING "HIL: IRQ not found for HIL bus at 0x%p\n",
304 (void *)dev->hpa.start);
308 hil_base = dev->hpa.start;
310 hil_dev.dev_id = dev;
312 printk(KERN_INFO "Found HIL bus at 0x%08lx, IRQ %d\n", hil_base, hil_irq);
314 return hil_keyb_init();
317 static void __exit hil_remove_chip(struct parisc_device *dev)
322 static const struct parisc_device_id hil_tbl[] __initconst = {
323 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00073 },
328 /* Disabled to avoid conflicts with the HP SDC HIL drivers */
329 MODULE_DEVICE_TABLE(parisc, hil_tbl);
332 static struct parisc_driver hil_driver __refdata = {
335 .probe = hil_probe_chip,
336 .remove = __exit_p(hil_remove_chip),
339 static int __init hil_init(void)
341 return register_parisc_driver(&hil_driver);
344 static void __exit hil_exit(void)
346 unregister_parisc_driver(&hil_driver);
349 #else /* !CONFIG_PARISC */
351 static int __init hil_init(void)
355 /* Only allow one HIL keyboard */
362 if (!hwreg_present((void *)(HILBASE + HIL_DATA))) {
363 printk(KERN_ERR "HIL: hardware register was not found\n");
367 if (!request_region(HILBASE + HIL_DATA, 2, "hil")) {
368 printk(KERN_ERR "HIL: IOPORT region already used\n");
372 error = hil_keyb_init();
374 release_region(HILBASE + HIL_DATA, 2);
381 static void __exit hil_exit(void)
384 release_region(HILBASE + HIL_DATA, 2);
387 #endif /* CONFIG_PARISC */
389 module_init(hil_init);
390 module_exit(hil_exit);