hilkbd.c 7.6 KB

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