hilkbd.c 7.6 KB

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