hilkbd.c 7.5 KB

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