ec3104_keyb.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * linux/drivers/char/ec3104_keyb.c
  3. *
  4. * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
  5. *
  6. * based on linux/drivers/char/pc_keyb.c, which had the following comments:
  7. *
  8. * Separation of the PC low-level part by Geert Uytterhoeven, May 1997
  9. * See keyboard.c for the whole history.
  10. *
  11. * Major cleanup by Martin Mares, May 1997
  12. *
  13. * Combined the keyboard and PS/2 mouse handling into one file,
  14. * because they share the same hardware.
  15. * Johan Myreen <jem@iki.fi> 1998-10-08.
  16. *
  17. * Code fixes to handle mouse ACKs properly.
  18. * C. Scott Ananian <cananian@alumni.princeton.edu> 1999-01-29.
  19. */
  20. /* EC3104 note:
  21. * This code was written without any documentation about the EC3104 chip. While
  22. * I hope I got most of the basic functionality right, the register names I use
  23. * are most likely completely different from those in the chip documentation.
  24. *
  25. * If you have any further information about the EC3104, please tell me
  26. * (prumpf@tux.org).
  27. */
  28. #include <linux/spinlock.h>
  29. #include <linux/sched.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/tty.h>
  32. #include <linux/mm.h>
  33. #include <linux/signal.h>
  34. #include <linux/init.h>
  35. #include <linux/kbd_ll.h>
  36. #include <linux/delay.h>
  37. #include <linux/random.h>
  38. #include <linux/poll.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/slab.h>
  41. #include <linux/kbd_kern.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/bitops.h>
  44. #include <asm/keyboard.h>
  45. #include <asm/uaccess.h>
  46. #include <asm/irq.h>
  47. #include <asm/system.h>
  48. #include <asm/ec3104.h>
  49. #include <asm/io.h>
  50. /* Some configuration switches are present in the include file... */
  51. #include <linux/pc_keyb.h>
  52. #define MSR_CTS 0x10
  53. #define MCR_RTS 0x02
  54. #define LSR_DR 0x01
  55. #define LSR_BOTH_EMPTY 0x60
  56. static struct e5_struct {
  57. u8 packet[8];
  58. int pos;
  59. int length;
  60. u8 cached_mcr;
  61. u8 last_msr;
  62. } ec3104_keyb;
  63. /* Simple translation table for the SysRq keys */
  64. #ifdef CONFIG_MAGIC_SYSRQ
  65. unsigned char ec3104_kbd_sysrq_xlate[128] =
  66. "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
  67. "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
  68. "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
  69. "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
  70. "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
  71. "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
  72. "\r\000/"; /* 0x60 - 0x6f */
  73. #endif
  74. static void kbd_write_command_w(int data);
  75. static void kbd_write_output_w(int data);
  76. #ifdef CONFIG_PSMOUSE
  77. static void aux_write_ack(int val);
  78. static void __aux_write_ack(int val);
  79. #endif
  80. static DEFINE_SPINLOCK(kbd_controller_lock);
  81. static unsigned char handle_kbd_event(void);
  82. /* used only by send_data - set by keyboard_interrupt */
  83. static volatile unsigned char reply_expected;
  84. static volatile unsigned char acknowledge;
  85. static volatile unsigned char resend;
  86. int ec3104_kbd_setkeycode(unsigned int scancode, unsigned int keycode)
  87. {
  88. return 0;
  89. }
  90. int ec3104_kbd_getkeycode(unsigned int scancode)
  91. {
  92. return 0;
  93. }
  94. /* yes, it probably would be faster to use an array. I don't care. */
  95. static inline unsigned char ec3104_scan2key(unsigned char scancode)
  96. {
  97. switch (scancode) {
  98. case 1: /* '`' */
  99. return 41;
  100. case 2 ... 27:
  101. return scancode;
  102. case 28: /* '\\' */
  103. return 43;
  104. case 29 ... 39:
  105. return scancode + 1;
  106. case 40: /* '\r' */
  107. return 28;
  108. case 41 ... 50:
  109. return scancode + 3;
  110. case 51: /* ' ' */
  111. return 57;
  112. case 52: /* escape */
  113. return 1;
  114. case 54: /* insert/delete (labelled delete) */
  115. /* this should arguably be 110, but I'd like to have ctrl-alt-del
  116. * working with a standard keymap */
  117. return 111;
  118. case 55: /* left */
  119. return 105;
  120. case 56: /* home */
  121. return 102;
  122. case 57: /* end */
  123. return 107;
  124. case 58: /* up */
  125. return 103;
  126. case 59: /* down */
  127. return 108;
  128. case 60: /* pgup */
  129. return 104;
  130. case 61: /* pgdown */
  131. return 109;
  132. case 62: /* right */
  133. return 106;
  134. case 79 ... 88: /* f1 - f10 */
  135. return scancode - 20;
  136. case 89 ... 90: /* f11 - f12 */
  137. return scancode - 2;
  138. case 91: /* left shift */
  139. return 42;
  140. case 92: /* right shift */
  141. return 54;
  142. case 93: /* left alt */
  143. return 56;
  144. case 94: /* right alt */
  145. return 100;
  146. case 95: /* left ctrl */
  147. return 29;
  148. case 96: /* right ctrl */
  149. return 97;
  150. case 97: /* caps lock */
  151. return 58;
  152. case 102: /* left windows */
  153. return 125;
  154. case 103: /* right windows */
  155. return 126;
  156. case 106: /* Fn */
  157. /* this is wrong. */
  158. return 84;
  159. default:
  160. return 0;
  161. }
  162. }
  163. int ec3104_kbd_translate(unsigned char scancode, unsigned char *keycode,
  164. char raw_mode)
  165. {
  166. scancode &= 0x7f;
  167. *keycode = ec3104_scan2key(scancode);
  168. return 1;
  169. }
  170. char ec3104_kbd_unexpected_up(unsigned char keycode)
  171. {
  172. return 0200;
  173. }
  174. static inline void handle_keyboard_event(unsigned char scancode)
  175. {
  176. #ifdef CONFIG_VT
  177. handle_scancode(scancode, !(scancode & 0x80));
  178. #endif
  179. tasklet_schedule(&keyboard_tasklet);
  180. }
  181. void ec3104_kbd_leds(unsigned char leds)
  182. {
  183. }
  184. static u8 e5_checksum(u8 *packet, int count)
  185. {
  186. int i;
  187. u8 sum = 0;
  188. for (i=0; i<count; i++)
  189. sum ^= packet[i];
  190. if (sum & 0x80)
  191. sum ^= 0xc0;
  192. return sum;
  193. }
  194. static void e5_wait_for_cts(struct e5_struct *k)
  195. {
  196. u8 msr;
  197. do {
  198. msr = ctrl_inb(EC3104_SER4_MSR);
  199. } while (!(msr & MSR_CTS));
  200. }
  201. static void e5_send_byte(u8 byte, struct e5_struct *k)
  202. {
  203. u8 status;
  204. do {
  205. status = ctrl_inb(EC3104_SER4_LSR);
  206. } while ((status & LSR_BOTH_EMPTY) != LSR_BOTH_EMPTY);
  207. printk("<%02x>", byte);
  208. ctrl_outb(byte, EC3104_SER4_DATA);
  209. do {
  210. status = ctrl_inb(EC3104_SER4_LSR);
  211. } while ((status & LSR_BOTH_EMPTY) != LSR_BOTH_EMPTY);
  212. }
  213. static int e5_send_packet(u8 *packet, int count, struct e5_struct *k)
  214. {
  215. int i;
  216. disable_irq(EC3104_IRQ_SER4);
  217. if (k->cached_mcr & MCR_RTS) {
  218. printk("e5_send_packet: too slow\n");
  219. enable_irq(EC3104_IRQ_SER4);
  220. return -EAGAIN;
  221. }
  222. k->cached_mcr |= MCR_RTS;
  223. ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
  224. e5_wait_for_cts(k);
  225. printk("p: ");
  226. for(i=0; i<count; i++)
  227. e5_send_byte(packet[i], k);
  228. e5_send_byte(e5_checksum(packet, count), k);
  229. printk("\n");
  230. udelay(1500);
  231. k->cached_mcr &= ~MCR_RTS;
  232. ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
  233. set_current_state(TASK_UNINTERRUPTIBLE);
  234. enable_irq(EC3104_IRQ_SER4);
  235. return 0;
  236. }
  237. /*
  238. * E5 packets we know about:
  239. * E5->host 0x80 0x05 <checksum> - resend packet
  240. * host->E5 0x83 0x43 <contrast> - set LCD contrast
  241. * host->E5 0x85 0x41 0x02 <brightness> 0x02 - set LCD backlight
  242. * E5->host 0x87 <ps2 packet> 0x00 <checksum> - external PS2
  243. * E5->host 0x88 <scancode> <checksum> - key press
  244. */
  245. static void e5_receive(struct e5_struct *k)
  246. {
  247. k->packet[k->pos++] = ctrl_inb(EC3104_SER4_DATA);
  248. if (k->pos == 1) {
  249. switch(k->packet[0]) {
  250. case 0x80:
  251. k->length = 3;
  252. break;
  253. case 0x87: /* PS2 ext */
  254. k->length = 6;
  255. break;
  256. case 0x88: /* keyboard */
  257. k->length = 3;
  258. break;
  259. default:
  260. k->length = 1;
  261. printk(KERN_WARNING "unknown E5 packet %02x\n",
  262. k->packet[0]);
  263. }
  264. }
  265. if (k->pos == k->length) {
  266. int i;
  267. if (e5_checksum(k->packet, k->length) != 0)
  268. printk(KERN_WARNING "E5: wrong checksum\n");
  269. #if 0
  270. printk("E5 packet [");
  271. for(i=0; i<k->length; i++) {
  272. printk("%02x ", k->packet[i]);
  273. }
  274. printk("(%02x)]\n", e5_checksum(k->packet, k->length-1));
  275. #endif
  276. switch(k->packet[0]) {
  277. case 0x80:
  278. case 0x88:
  279. handle_keyboard_event(k->packet[1]);
  280. break;
  281. }
  282. k->pos = k->length = 0;
  283. }
  284. }
  285. static void ec3104_keyb_interrupt(int irq, void *data)
  286. {
  287. struct e5_struct *k = &ec3104_keyb;
  288. u8 msr, lsr;
  289. msr = ctrl_inb(EC3104_SER4_MSR);
  290. if ((msr & MSR_CTS) && !(k->last_msr & MSR_CTS)) {
  291. if (k->cached_mcr & MCR_RTS)
  292. printk("confused: RTS already high\n");
  293. /* CTS went high. Send RTS. */
  294. k->cached_mcr |= MCR_RTS;
  295. ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
  296. } else if ((!(msr & MSR_CTS)) && (k->last_msr & MSR_CTS)) {
  297. /* CTS went low. */
  298. if (!(k->cached_mcr & MCR_RTS))
  299. printk("confused: RTS already low\n");
  300. k->cached_mcr &= ~MCR_RTS;
  301. ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
  302. }
  303. k->last_msr = msr;
  304. lsr = ctrl_inb(EC3104_SER4_LSR);
  305. if (lsr & LSR_DR)
  306. e5_receive(k);
  307. }
  308. static void ec3104_keyb_clear_state(void)
  309. {
  310. struct e5_struct *k = &ec3104_keyb;
  311. u8 msr, lsr;
  312. /* we want CTS to be low */
  313. k->last_msr = 0;
  314. for (;;) {
  315. msleep(100);
  316. msr = ctrl_inb(EC3104_SER4_MSR);
  317. lsr = ctrl_inb(EC3104_SER4_LSR);
  318. if (lsr & LSR_DR) {
  319. e5_receive(k);
  320. continue;
  321. }
  322. if ((msr & MSR_CTS) && !(k->last_msr & MSR_CTS)) {
  323. if (k->cached_mcr & MCR_RTS)
  324. printk("confused: RTS already high\n");
  325. /* CTS went high. Send RTS. */
  326. k->cached_mcr |= MCR_RTS;
  327. ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
  328. } else if ((!(msr & MSR_CTS)) && (k->last_msr & MSR_CTS)) {
  329. /* CTS went low. */
  330. if (!(k->cached_mcr & MCR_RTS))
  331. printk("confused: RTS already low\n");
  332. k->cached_mcr &= ~MCR_RTS;
  333. ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
  334. } else
  335. break;
  336. k->last_msr = msr;
  337. continue;
  338. }
  339. }
  340. void __init ec3104_kbd_init_hw(void)
  341. {
  342. ec3104_keyb.last_msr = ctrl_inb(EC3104_SER4_MSR);
  343. ec3104_keyb.cached_mcr = ctrl_inb(EC3104_SER4_MCR);
  344. ec3104_keyb_clear_state();
  345. /* Ok, finally allocate the IRQ, and off we go.. */
  346. request_irq(EC3104_IRQ_SER4, ec3104_keyb_interrupt, 0, "keyboard", NULL);
  347. }