hilkbd.c 8.5 KB

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