ec3104_keyb.c 9.1 KB

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