ps2mult.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /***********************************************************************
  2. *
  3. * (C) Copyright 2004
  4. * DENX Software Engineering
  5. * Wolfgang Denk, wd@denx.de
  6. * All rights reserved.
  7. *
  8. * PS/2 multiplexer driver
  9. *
  10. * Originally from linux source (drivers/char/ps2mult.c)
  11. *
  12. * Uses simple serial driver (ps2ser.c) to access the multiplexer
  13. * Used by PS/2 keyboard driver (pc_keyb.c)
  14. *
  15. ***********************************************************************/
  16. #include <common.h>
  17. #ifdef CONFIG_PS2MULT
  18. #include <pc_keyb.h>
  19. #include <asm/atomic.h>
  20. #include <ps2mult.h>
  21. /* #define DEBUG_MULT */
  22. /* #define DEBUG_KEYB */
  23. #define KBD_STAT_DEFAULT (KBD_STAT_SELFTEST | KBD_STAT_UNLOCKED)
  24. #define PRINTF(format, args...) printf("ps2mult.c: " format, ## args)
  25. #ifdef DEBUG_MULT
  26. #define PRINTF_MULT(format, args...) printf("PS2MULT: " format, ## args)
  27. #else
  28. #define PRINTF_MULT(format, args...)
  29. #endif
  30. #ifdef DEBUG_KEYB
  31. #define PRINTF_KEYB(format, args...) printf("KEYB: " format, ## args)
  32. #else
  33. #define PRINTF_KEYB(format, args...)
  34. #endif
  35. static int init_done = 0;
  36. static int received_escape = 0;
  37. static int received_bsync = 0;
  38. static int received_selector = 0;
  39. static int kbd_command_active = 0;
  40. static int mouse_command_active = 0;
  41. static int ctl_command_active = 0;
  42. static u_char command_byte = 0;
  43. static void (*keyb_handler)(void *dev_id);
  44. static u_char ps2mult_buf [PS2BUF_SIZE];
  45. static atomic_t ps2mult_buf_cnt;
  46. static int ps2mult_buf_in_idx;
  47. static int ps2mult_buf_out_idx;
  48. static u_char ps2mult_buf_status [PS2BUF_SIZE];
  49. static void ps2mult_send_byte(u_char byte, u_char sel)
  50. {
  51. ps2ser_putc(sel);
  52. if (sel == PS2MULT_KB_SELECTOR) {
  53. PRINTF_MULT("0x%02x send KEYBOARD\n", byte);
  54. kbd_command_active = 1;
  55. } else {
  56. PRINTF_MULT("0x%02x send MOUSE\n", byte);
  57. mouse_command_active = 1;
  58. }
  59. switch (byte) {
  60. case PS2MULT_ESCAPE:
  61. case PS2MULT_BSYNC:
  62. case PS2MULT_KB_SELECTOR:
  63. case PS2MULT_MS_SELECTOR:
  64. case PS2MULT_SESSION_START:
  65. case PS2MULT_SESSION_END:
  66. ps2ser_putc(PS2MULT_ESCAPE);
  67. break;
  68. default:
  69. break;
  70. }
  71. ps2ser_putc(byte);
  72. }
  73. static void ps2mult_receive_byte(u_char byte, u_char sel)
  74. {
  75. u_char status = KBD_STAT_DEFAULT;
  76. #if 1 /* Ignore mouse in U-Boot */
  77. if (sel == PS2MULT_MS_SELECTOR) return;
  78. #endif
  79. if (sel == PS2MULT_KB_SELECTOR) {
  80. if (kbd_command_active) {
  81. if (!received_bsync) {
  82. PRINTF_MULT("0x%02x lost KEYBOARD !!!\n", byte);
  83. return;
  84. } else {
  85. kbd_command_active = 0;
  86. received_bsync = 0;
  87. }
  88. }
  89. PRINTF_MULT("0x%02x receive KEYBOARD\n", byte);
  90. status |= KBD_STAT_IBF | KBD_STAT_OBF;
  91. } else {
  92. if (mouse_command_active) {
  93. if (!received_bsync) {
  94. PRINTF_MULT("0x%02x lost MOUSE !!!\n", byte);
  95. return;
  96. } else {
  97. mouse_command_active = 0;
  98. received_bsync = 0;
  99. }
  100. }
  101. PRINTF_MULT("0x%02x receive MOUSE\n", byte);
  102. status |= KBD_STAT_IBF | KBD_STAT_OBF | KBD_STAT_MOUSE_OBF;
  103. }
  104. if (atomic_read(&ps2mult_buf_cnt) < PS2BUF_SIZE) {
  105. ps2mult_buf_status[ps2mult_buf_in_idx] = status;
  106. ps2mult_buf[ps2mult_buf_in_idx++] = byte;
  107. ps2mult_buf_in_idx &= (PS2BUF_SIZE - 1);
  108. atomic_inc(&ps2mult_buf_cnt);
  109. } else {
  110. PRINTF("buffer overflow\n");
  111. }
  112. if (received_bsync) {
  113. PRINTF("unexpected BSYNC\n");
  114. received_bsync = 0;
  115. }
  116. }
  117. void ps2mult_callback (int in_cnt)
  118. {
  119. int i;
  120. u_char byte;
  121. static int keyb_handler_active = 0;
  122. if (!init_done) {
  123. return;
  124. }
  125. for (i = 0; i < in_cnt; i ++) {
  126. byte = ps2ser_getc();
  127. if (received_escape) {
  128. ps2mult_receive_byte(byte, received_selector);
  129. received_escape = 0;
  130. } else switch (byte) {
  131. case PS2MULT_ESCAPE:
  132. PRINTF_MULT("ESCAPE receive\n");
  133. received_escape = 1;
  134. break;
  135. case PS2MULT_BSYNC:
  136. PRINTF_MULT("BSYNC receive\n");
  137. received_bsync = 1;
  138. break;
  139. case PS2MULT_KB_SELECTOR:
  140. case PS2MULT_MS_SELECTOR:
  141. PRINTF_MULT("%s receive\n",
  142. byte == PS2MULT_KB_SELECTOR ? "KB_SEL" : "MS_SEL");
  143. received_selector = byte;
  144. break;
  145. case PS2MULT_SESSION_START:
  146. case PS2MULT_SESSION_END:
  147. PRINTF_MULT("%s receive\n",
  148. byte == PS2MULT_SESSION_START ?
  149. "SESSION_START" : "SESSION_END");
  150. break;
  151. default:
  152. ps2mult_receive_byte(byte, received_selector);
  153. }
  154. }
  155. if (keyb_handler && !keyb_handler_active &&
  156. atomic_read(&ps2mult_buf_cnt)) {
  157. keyb_handler_active = 1;
  158. keyb_handler(NULL);
  159. keyb_handler_active = 0;
  160. }
  161. }
  162. u_char ps2mult_read_status(void)
  163. {
  164. u_char byte;
  165. if (atomic_read(&ps2mult_buf_cnt) == 0) {
  166. ps2ser_check();
  167. }
  168. if (atomic_read(&ps2mult_buf_cnt)) {
  169. byte = ps2mult_buf_status[ps2mult_buf_out_idx];
  170. } else {
  171. byte = KBD_STAT_DEFAULT;
  172. }
  173. PRINTF_KEYB("read_status()=0x%02x\n", byte);
  174. return byte;
  175. }
  176. u_char ps2mult_read_input(void)
  177. {
  178. u_char byte = 0;
  179. if (atomic_read(&ps2mult_buf_cnt) == 0) {
  180. ps2ser_check();
  181. }
  182. if (atomic_read(&ps2mult_buf_cnt)) {
  183. byte = ps2mult_buf[ps2mult_buf_out_idx++];
  184. ps2mult_buf_out_idx &= (PS2BUF_SIZE - 1);
  185. atomic_dec(&ps2mult_buf_cnt);
  186. }
  187. PRINTF_KEYB("read_input()=0x%02x\n", byte);
  188. return byte;
  189. }
  190. void ps2mult_write_output(u_char val)
  191. {
  192. int i;
  193. PRINTF_KEYB("write_output(0x%02x)\n", val);
  194. for (i = 0; i < KBD_TIMEOUT; i++) {
  195. if (!kbd_command_active && !mouse_command_active) {
  196. break;
  197. }
  198. udelay(1000);
  199. ps2ser_check();
  200. }
  201. if (kbd_command_active) {
  202. PRINTF("keyboard command not acknoledged\n");
  203. kbd_command_active = 0;
  204. }
  205. if (mouse_command_active) {
  206. PRINTF("mouse command not acknoledged\n");
  207. mouse_command_active = 0;
  208. }
  209. if (ctl_command_active) {
  210. switch (ctl_command_active) {
  211. case KBD_CCMD_WRITE_MODE:
  212. /* Scan code conversion not supported */
  213. command_byte = val & ~KBD_MODE_KCC;
  214. break;
  215. case KBD_CCMD_WRITE_AUX_OBUF:
  216. ps2mult_receive_byte(val, PS2MULT_MS_SELECTOR);
  217. break;
  218. case KBD_CCMD_WRITE_MOUSE:
  219. ps2mult_send_byte(val, PS2MULT_MS_SELECTOR);
  220. break;
  221. default:
  222. PRINTF("invalid controller command\n");
  223. break;
  224. }
  225. ctl_command_active = 0;
  226. return;
  227. }
  228. ps2mult_send_byte(val, PS2MULT_KB_SELECTOR);
  229. }
  230. void ps2mult_write_command(u_char val)
  231. {
  232. ctl_command_active = 0;
  233. PRINTF_KEYB("write_command(0x%02x)\n", val);
  234. switch (val) {
  235. case KBD_CCMD_READ_MODE:
  236. ps2mult_receive_byte(command_byte, PS2MULT_KB_SELECTOR);
  237. break;
  238. case KBD_CCMD_WRITE_MODE:
  239. ctl_command_active = val;
  240. break;
  241. case KBD_CCMD_MOUSE_DISABLE:
  242. break;
  243. case KBD_CCMD_MOUSE_ENABLE:
  244. break;
  245. case KBD_CCMD_SELF_TEST:
  246. ps2mult_receive_byte(0x55, PS2MULT_KB_SELECTOR);
  247. break;
  248. case KBD_CCMD_KBD_TEST:
  249. ps2mult_receive_byte(0x00, PS2MULT_KB_SELECTOR);
  250. break;
  251. case KBD_CCMD_KBD_DISABLE:
  252. break;
  253. case KBD_CCMD_KBD_ENABLE:
  254. break;
  255. case KBD_CCMD_WRITE_AUX_OBUF:
  256. ctl_command_active = val;
  257. break;
  258. case KBD_CCMD_WRITE_MOUSE:
  259. ctl_command_active = val;
  260. break;
  261. default:
  262. PRINTF("invalid controller command\n");
  263. break;
  264. }
  265. }
  266. static int ps2mult_getc_w (void)
  267. {
  268. int res = -1;
  269. int i;
  270. for (i = 0; i < KBD_TIMEOUT; i++) {
  271. if (ps2ser_check()) {
  272. res = ps2ser_getc();
  273. break;
  274. }
  275. udelay(1000);
  276. }
  277. switch (res) {
  278. case PS2MULT_KB_SELECTOR:
  279. case PS2MULT_MS_SELECTOR:
  280. received_selector = res;
  281. break;
  282. default:
  283. break;
  284. }
  285. return res;
  286. }
  287. int ps2mult_init (void)
  288. {
  289. int byte;
  290. int kbd_found = 0;
  291. int mouse_found = 0;
  292. ps2ser_init();
  293. ps2ser_putc(PS2MULT_SESSION_START);
  294. ps2ser_putc(PS2MULT_KB_SELECTOR);
  295. ps2ser_putc(KBD_CMD_RESET);
  296. do {
  297. byte = ps2mult_getc_w();
  298. } while (byte >= 0 && byte != KBD_REPLY_ACK);
  299. if (byte == KBD_REPLY_ACK) {
  300. byte = ps2mult_getc_w();
  301. if (byte == 0xaa) {
  302. kbd_found = 1;
  303. puts("keyboard");
  304. }
  305. }
  306. if (!kbd_found) {
  307. while (byte >= 0) {
  308. byte = ps2mult_getc_w();
  309. }
  310. }
  311. #if 1 /* detect mouse */
  312. ps2ser_putc(PS2MULT_MS_SELECTOR);
  313. ps2ser_putc(AUX_RESET);
  314. do {
  315. byte = ps2mult_getc_w();
  316. } while (byte >= 0 && byte != AUX_ACK);
  317. if (byte == AUX_ACK) {
  318. byte = ps2mult_getc_w();
  319. if (byte == 0xaa) {
  320. byte = ps2mult_getc_w();
  321. if (byte == 0x00) {
  322. mouse_found = 1;
  323. puts(", mouse");
  324. }
  325. }
  326. }
  327. if (!mouse_found) {
  328. while (byte >= 0) {
  329. byte = ps2mult_getc_w();
  330. }
  331. }
  332. #endif
  333. if (mouse_found || kbd_found) {
  334. if (!received_selector) {
  335. if (mouse_found) {
  336. received_selector = PS2MULT_MS_SELECTOR;
  337. } else {
  338. received_selector = PS2MULT_KB_SELECTOR;
  339. }
  340. }
  341. init_done = 1;
  342. } else {
  343. puts("No device found");
  344. }
  345. puts("\n");
  346. #if 0 /* for testing */
  347. {
  348. int i;
  349. u_char key[] = {
  350. 0x1f, 0x12, 0x14, 0x12, 0x31, 0x2f, 0x39, /* setenv */
  351. 0x1f, 0x14, 0x20, 0x17, 0x31, 0x39, /* stdin */
  352. 0x1f, 0x12, 0x13, 0x17, 0x1e, 0x26, 0x1c, /* serial */
  353. };
  354. for (i = 0; i < sizeof (key); i++) {
  355. ps2mult_receive_byte (key[i], PS2MULT_KB_SELECTOR);
  356. ps2mult_receive_byte (key[i] | 0x80, PS2MULT_KB_SELECTOR);
  357. }
  358. }
  359. #endif
  360. return init_done ? 0 : -1;
  361. }
  362. int ps2mult_request_irq(void (*handler)(void *))
  363. {
  364. keyb_handler = handler;
  365. return 0;
  366. }
  367. #endif /* CONFIG_PS2MULT */