ec3104_keyb.c 9.0 KB

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