hilkbd.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/hil/hilkbd.c
  4. *
  5. * Copyright (C) 1998 Philip Blundell <philb@gnu.org>
  6. * Copyright (C) 1999 Matthew Wilcox <willy@infradead.org>
  7. * Copyright (C) 1999-2007 Helge Deller <deller@gmx.de>
  8. *
  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.
  12. */
  13. #include <linux/pci_ids.h>
  14. #include <linux/ioport.h>
  15. #include <linux/module.h>
  16. #include <linux/errno.h>
  17. #include <linux/input.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/hil.h>
  21. #include <linux/io.h>
  22. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #include <asm/irq.h>
  25. #ifdef CONFIG_HP300
  26. #include <asm/hwtest.h>
  27. #endif
  28. MODULE_AUTHOR("Philip Blundell, Matthew Wilcox, Helge Deller");
  29. MODULE_DESCRIPTION("HIL keyboard driver (basic functionality)");
  30. MODULE_LICENSE("GPL v2");
  31. #if defined(CONFIG_PARISC)
  32. #include <asm/io.h>
  33. #include <asm/hardware.h>
  34. #include <asm/parisc-device.h>
  35. static unsigned long hil_base; /* HPA for the HIL device */
  36. static unsigned int hil_irq;
  37. #define HILBASE hil_base /* HPPA (parisc) port address */
  38. #define HIL_DATA 0x800
  39. #define HIL_CMD 0x801
  40. #define HIL_IRQ hil_irq
  41. #define hil_readb(p) gsc_readb(p)
  42. #define hil_writeb(v,p) gsc_writeb((v),(p))
  43. #elif defined(CONFIG_HP300)
  44. #define HILBASE 0xf0428000UL /* HP300 (m68k) port address */
  45. #define HIL_DATA 0x1
  46. #define HIL_CMD 0x3
  47. #define HIL_IRQ 2
  48. #define hil_readb(p) readb((const volatile void __iomem *)(p))
  49. #define hil_writeb(v, p) writeb((v), (volatile void __iomem *)(p))
  50. #else
  51. #error "HIL is not supported on this platform"
  52. #endif
  53. /* HIL helper functions */
  54. #define hil_busy() (hil_readb(HILBASE + HIL_CMD) & HIL_BUSY)
  55. #define hil_data_available() (hil_readb(HILBASE + HIL_CMD) & HIL_DATA_RDY)
  56. #define hil_status() (hil_readb(HILBASE + HIL_CMD))
  57. #define hil_command(x) do { hil_writeb((x), HILBASE + HIL_CMD); } while (0)
  58. #define hil_read_data() (hil_readb(HILBASE + HIL_DATA))
  59. #define hil_write_data(x) do { hil_writeb((x), HILBASE + HIL_DATA); } while (0)
  60. /* HIL constants */
  61. #define HIL_BUSY 0x02
  62. #define HIL_DATA_RDY 0x01
  63. #define HIL_SETARD 0xA0 /* set auto-repeat delay */
  64. #define HIL_SETARR 0xA2 /* set auto-repeat rate */
  65. #define HIL_SETTONE 0xA3 /* set tone generator */
  66. #define HIL_CNMT 0xB2 /* clear nmi */
  67. #define HIL_INTON 0x5C /* Turn on interrupts. */
  68. #define HIL_INTOFF 0x5D /* Turn off interrupts. */
  69. #define HIL_READKBDSADR 0xF9
  70. #define HIL_WRITEKBDSADR 0xE9
  71. static unsigned int hphilkeyb_keycode[HIL_KEYCODES_SET1_TBLSIZE] __read_mostly =
  72. { HIL_KEYCODES_SET1 };
  73. /* HIL structure */
  74. static struct {
  75. struct input_dev *dev;
  76. unsigned int curdev;
  77. unsigned char s;
  78. unsigned char c;
  79. int valid;
  80. unsigned char data[16];
  81. unsigned int ptr;
  82. spinlock_t lock;
  83. void *dev_id; /* native bus device */
  84. } hil_dev;
  85. static void poll_finished(void)
  86. {
  87. int down;
  88. int key;
  89. unsigned char scode;
  90. switch (hil_dev.data[0]) {
  91. case 0x40:
  92. down = (hil_dev.data[1] & 1) == 0;
  93. scode = hil_dev.data[1] >> 1;
  94. key = hphilkeyb_keycode[scode];
  95. input_report_key(hil_dev.dev, key, down);
  96. break;
  97. }
  98. hil_dev.curdev = 0;
  99. }
  100. static inline void handle_status(unsigned char s, unsigned char c)
  101. {
  102. if (c & 0x8) {
  103. /* End of block */
  104. if (c & 0x10)
  105. poll_finished();
  106. } else {
  107. if (c & 0x10) {
  108. if (hil_dev.curdev)
  109. poll_finished(); /* just in case */
  110. hil_dev.curdev = c & 7;
  111. hil_dev.ptr = 0;
  112. }
  113. }
  114. }
  115. static inline void handle_data(unsigned char s, unsigned char c)
  116. {
  117. if (hil_dev.curdev) {
  118. hil_dev.data[hil_dev.ptr++] = c;
  119. hil_dev.ptr &= 15;
  120. }
  121. }
  122. /* handle HIL interrupts */
  123. static irqreturn_t hil_interrupt(int irq, void *handle)
  124. {
  125. unsigned char s, c;
  126. s = hil_status();
  127. c = hil_read_data();
  128. switch (s >> 4) {
  129. case 0x5:
  130. handle_status(s, c);
  131. break;
  132. case 0x6:
  133. handle_data(s, c);
  134. break;
  135. case 0x4:
  136. hil_dev.s = s;
  137. hil_dev.c = c;
  138. mb();
  139. hil_dev.valid = 1;
  140. break;
  141. }
  142. return IRQ_HANDLED;
  143. }
  144. /* send a command to the HIL */
  145. static void hil_do(unsigned char cmd, unsigned char *data, unsigned int len)
  146. {
  147. unsigned long flags;
  148. spin_lock_irqsave(&hil_dev.lock, flags);
  149. while (hil_busy())
  150. /* wait */;
  151. hil_command(cmd);
  152. while (len--) {
  153. while (hil_busy())
  154. /* wait */;
  155. hil_write_data(*(data++));
  156. }
  157. spin_unlock_irqrestore(&hil_dev.lock, flags);
  158. }
  159. /* initialize HIL */
  160. static int hil_keyb_init(void)
  161. {
  162. unsigned char c;
  163. unsigned int i, kbid;
  164. wait_queue_head_t hil_wait;
  165. int err;
  166. if (hil_dev.dev)
  167. return -ENODEV; /* already initialized */
  168. init_waitqueue_head(&hil_wait);
  169. spin_lock_init(&hil_dev.lock);
  170. hil_dev.dev = input_allocate_device();
  171. if (!hil_dev.dev)
  172. return -ENOMEM;
  173. err = request_irq(HIL_IRQ, hil_interrupt, 0, "hil", hil_dev.dev_id);
  174. if (err) {
  175. printk(KERN_ERR "HIL: Can't get IRQ\n");
  176. goto err1;
  177. }
  178. /* Turn on interrupts */
  179. hil_do(HIL_INTON, NULL, 0);
  180. /* Look for keyboards */
  181. hil_dev.valid = 0; /* clear any pending data */
  182. hil_do(HIL_READKBDSADR, NULL, 0);
  183. wait_event_interruptible_timeout(hil_wait, hil_dev.valid, 3 * HZ);
  184. if (!hil_dev.valid)
  185. printk(KERN_WARNING "HIL: timed out, assuming no keyboard present\n");
  186. c = hil_dev.c;
  187. hil_dev.valid = 0;
  188. if (c == 0) {
  189. kbid = -1;
  190. printk(KERN_WARNING "HIL: no keyboard present\n");
  191. } else {
  192. kbid = ffz(~c);
  193. printk(KERN_INFO "HIL: keyboard found at id %d\n", kbid);
  194. }
  195. /* set it to raw mode */
  196. c = 0;
  197. hil_do(HIL_WRITEKBDSADR, &c, 1);
  198. for (i = 0; i < HIL_KEYCODES_SET1_TBLSIZE; i++)
  199. if (hphilkeyb_keycode[i] != KEY_RESERVED)
  200. __set_bit(hphilkeyb_keycode[i], hil_dev.dev->keybit);
  201. hil_dev.dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  202. hil_dev.dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
  203. BIT_MASK(LED_SCROLLL);
  204. hil_dev.dev->keycodemax = HIL_KEYCODES_SET1_TBLSIZE;
  205. hil_dev.dev->keycodesize= sizeof(hphilkeyb_keycode[0]);
  206. hil_dev.dev->keycode = hphilkeyb_keycode;
  207. hil_dev.dev->name = "HIL keyboard";
  208. hil_dev.dev->phys = "hpkbd/input0";
  209. hil_dev.dev->id.bustype = BUS_HIL;
  210. hil_dev.dev->id.vendor = PCI_VENDOR_ID_HP;
  211. hil_dev.dev->id.product = 0x0001;
  212. hil_dev.dev->id.version = 0x0010;
  213. err = input_register_device(hil_dev.dev);
  214. if (err) {
  215. printk(KERN_ERR "HIL: Can't register device\n");
  216. goto err2;
  217. }
  218. printk(KERN_INFO "input: %s, ID %d at 0x%08lx (irq %d) found and attached\n",
  219. hil_dev.dev->name, kbid, HILBASE, HIL_IRQ);
  220. return 0;
  221. err2:
  222. hil_do(HIL_INTOFF, NULL, 0);
  223. free_irq(HIL_IRQ, hil_dev.dev_id);
  224. err1:
  225. input_free_device(hil_dev.dev);
  226. hil_dev.dev = NULL;
  227. return err;
  228. }
  229. static void hil_keyb_exit(void)
  230. {
  231. if (HIL_IRQ)
  232. free_irq(HIL_IRQ, hil_dev.dev_id);
  233. /* Turn off interrupts */
  234. hil_do(HIL_INTOFF, NULL, 0);
  235. input_unregister_device(hil_dev.dev);
  236. hil_dev.dev = NULL;
  237. }
  238. #if defined(CONFIG_PARISC)
  239. static int __init hil_probe_chip(struct parisc_device *dev)
  240. {
  241. /* Only allow one HIL keyboard */
  242. if (hil_dev.dev)
  243. return -ENODEV;
  244. if (!dev->irq) {
  245. printk(KERN_WARNING "HIL: IRQ not found for HIL bus at 0x%p\n",
  246. (void *)dev->hpa.start);
  247. return -ENODEV;
  248. }
  249. hil_base = dev->hpa.start;
  250. hil_irq = dev->irq;
  251. hil_dev.dev_id = dev;
  252. printk(KERN_INFO "Found HIL bus at 0x%08lx, IRQ %d\n", hil_base, hil_irq);
  253. return hil_keyb_init();
  254. }
  255. static int __exit hil_remove_chip(struct parisc_device *dev)
  256. {
  257. hil_keyb_exit();
  258. return 0;
  259. }
  260. static const struct parisc_device_id hil_tbl[] __initconst = {
  261. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00073 },
  262. { 0, }
  263. };
  264. #if 0
  265. /* Disabled to avoid conflicts with the HP SDC HIL drivers */
  266. MODULE_DEVICE_TABLE(parisc, hil_tbl);
  267. #endif
  268. static struct parisc_driver hil_driver __refdata = {
  269. .name = "hil",
  270. .id_table = hil_tbl,
  271. .probe = hil_probe_chip,
  272. .remove = __exit_p(hil_remove_chip),
  273. };
  274. static int __init hil_init(void)
  275. {
  276. return register_parisc_driver(&hil_driver);
  277. }
  278. static void __exit hil_exit(void)
  279. {
  280. unregister_parisc_driver(&hil_driver);
  281. }
  282. #else /* !CONFIG_PARISC */
  283. static int __init hil_init(void)
  284. {
  285. int error;
  286. /* Only allow one HIL keyboard */
  287. if (hil_dev.dev)
  288. return -EBUSY;
  289. if (!MACH_IS_HP300)
  290. return -ENODEV;
  291. if (!hwreg_present((void *)(HILBASE + HIL_DATA))) {
  292. printk(KERN_ERR "HIL: hardware register was not found\n");
  293. return -ENODEV;
  294. }
  295. if (!request_region(HILBASE + HIL_DATA, 2, "hil")) {
  296. printk(KERN_ERR "HIL: IOPORT region already used\n");
  297. return -EIO;
  298. }
  299. error = hil_keyb_init();
  300. if (error) {
  301. release_region(HILBASE + HIL_DATA, 2);
  302. return error;
  303. }
  304. return 0;
  305. }
  306. static void __exit hil_exit(void)
  307. {
  308. hil_keyb_exit();
  309. release_region(HILBASE + HIL_DATA, 2);
  310. }
  311. #endif /* CONFIG_PARISC */
  312. module_init(hil_init);
  313. module_exit(hil_exit);