hilkbd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. /* handle HIL interrupts */
  126. static irqreturn_t hil_interrupt(int irq, void *handle)
  127. {
  128. unsigned char s, c;
  129. s = hil_status();
  130. c = hil_read_data();
  131. switch (s >> 4) {
  132. case 0x5:
  133. handle_status(s, c);
  134. break;
  135. case 0x6:
  136. handle_data(s, c);
  137. break;
  138. case 0x4:
  139. hil_dev.s = s;
  140. hil_dev.c = c;
  141. mb();
  142. hil_dev.valid = 1;
  143. break;
  144. }
  145. return IRQ_HANDLED;
  146. }
  147. /* send a command to the HIL */
  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. /* initialise HIL */
  163. static int __init
  164. hil_keyb_init(void)
  165. {
  166. unsigned char c;
  167. unsigned int i, kbid;
  168. wait_queue_head_t hil_wait;
  169. int err;
  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. printk(KERN_ERR "HIL: hardware register was not found\n");
  180. err = -ENODEV;
  181. goto err1;
  182. }
  183. if (!request_region(HILBASE + HIL_DATA, 2, "hil")) {
  184. printk(KERN_ERR "HIL: IOPORT region already used\n");
  185. err = -EIO;
  186. goto err1;
  187. }
  188. #endif
  189. err = request_irq(HIL_IRQ, hil_interrupt, 0, "hil", hil_dev.dev_id);
  190. if (err) {
  191. printk(KERN_ERR "HIL: Can't get IRQ\n");
  192. goto err2;
  193. }
  194. /* Turn on interrupts */
  195. hil_do(HIL_INTON, NULL, 0);
  196. /* Look for keyboards */
  197. hil_dev.valid = 0; /* clear any pending data */
  198. hil_do(HIL_READKBDSADR, NULL, 0);
  199. init_waitqueue_head(&hil_wait);
  200. wait_event_interruptible_timeout(hil_wait, hil_dev.valid, 3*HZ);
  201. if (!hil_dev.valid) {
  202. printk(KERN_WARNING "HIL: timed out, assuming no keyboard present\n");
  203. }
  204. c = hil_dev.c;
  205. hil_dev.valid = 0;
  206. if (c == 0) {
  207. kbid = -1;
  208. printk(KERN_WARNING "HIL: no keyboard present\n");
  209. } else {
  210. kbid = ffz(~c);
  211. printk(KERN_INFO "HIL: keyboard found at id %d\n", kbid);
  212. }
  213. /* set it to raw mode */
  214. c = 0;
  215. hil_do(HIL_WRITEKBDSADR, &c, 1);
  216. for (i = 0; i < HIL_KEYCODES_SET1_TBLSIZE; i++)
  217. if (hphilkeyb_keycode[i] != KEY_RESERVED)
  218. set_bit(hphilkeyb_keycode[i], hil_dev.dev->keybit);
  219. hil_dev.dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REP);
  220. hil_dev.dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | BIT(LED_SCROLLL);
  221. hil_dev.dev->keycodemax = HIL_KEYCODES_SET1_TBLSIZE;
  222. hil_dev.dev->keycodesize= sizeof(hphilkeyb_keycode[0]);
  223. hil_dev.dev->keycode = hphilkeyb_keycode;
  224. hil_dev.dev->name = "HIL keyboard";
  225. hil_dev.dev->phys = "hpkbd/input0";
  226. hil_dev.dev->id.bustype = BUS_HIL;
  227. hil_dev.dev->id.vendor = PCI_VENDOR_ID_HP;
  228. hil_dev.dev->id.product = 0x0001;
  229. hil_dev.dev->id.version = 0x0010;
  230. err = input_register_device(hil_dev.dev);
  231. if (err) {
  232. printk(KERN_ERR "HIL: Can't register device\n");
  233. goto err3;
  234. }
  235. printk(KERN_INFO "input: %s, ID %d at 0x%08lx (irq %d) found and attached\n",
  236. hil_dev.dev->name, kbid, HILBASE, HIL_IRQ);
  237. return 0;
  238. err3:
  239. hil_do(HIL_INTOFF, NULL, 0);
  240. disable_irq(HIL_IRQ);
  241. free_irq(HIL_IRQ, hil_dev.dev_id);
  242. err2:
  243. #if defined(CONFIG_HP300)
  244. release_region(HILBASE + HIL_DATA, 2);
  245. err1:
  246. #endif
  247. input_free_device(hil_dev.dev);
  248. hil_dev.dev = NULL;
  249. return err;
  250. }
  251. #if defined(CONFIG_PARISC)
  252. static int __init
  253. hil_init_chip(struct parisc_device *dev)
  254. {
  255. if (!dev->irq) {
  256. printk(KERN_WARNING "HIL: IRQ not found for HIL bus at 0x%08lx\n", dev->hpa.start);
  257. return -ENODEV;
  258. }
  259. hil_base = dev->hpa.start;
  260. hil_irq = dev->irq;
  261. hil_dev.dev_id = dev;
  262. printk(KERN_INFO "Found HIL bus at 0x%08lx, IRQ %d\n", hil_base, hil_irq);
  263. return hil_keyb_init();
  264. }
  265. static struct parisc_device_id hil_tbl[] = {
  266. { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x00073 },
  267. { 0, }
  268. };
  269. MODULE_DEVICE_TABLE(parisc, hil_tbl);
  270. static struct parisc_driver hil_driver = {
  271. .name = "hil",
  272. .id_table = hil_tbl,
  273. .probe = hil_init_chip,
  274. };
  275. #endif /* CONFIG_PARISC */
  276. static int __init hil_init(void)
  277. {
  278. #if defined(CONFIG_PARISC)
  279. return register_parisc_driver(&hil_driver);
  280. #else
  281. return hil_keyb_init();
  282. #endif
  283. }
  284. static void __exit hil_exit(void)
  285. {
  286. if (HIL_IRQ) {
  287. disable_irq(HIL_IRQ);
  288. free_irq(HIL_IRQ, hil_dev.dev_id);
  289. }
  290. /* Turn off interrupts */
  291. hil_do(HIL_INTOFF, NULL, 0);
  292. input_unregister_device(hil_dev.dev);
  293. hil_dev.dev = NULL;
  294. #if defined(CONFIG_PARISC)
  295. unregister_parisc_driver(&hil_driver);
  296. #else
  297. release_region(HILBASE+HIL_DATA, 2);
  298. #endif
  299. }
  300. module_init(hil_init);
  301. module_exit(hil_exit);