hilkbd.c 8.2 KB

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