lk201.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. *
  3. * This file is subject to the terms and conditions of the GNU General Public
  4. * License. See the file "COPYING" in the main directory of this archive
  5. * for more details.
  6. *
  7. * Copyright (C) 1999-2002 Harald Koerfgen <hkoerfg@web.de>
  8. * Copyright (C) 2001, 2002, 2003, 2004 Maciej W. Rozycki
  9. */
  10. #include <linux/errno.h>
  11. #include <linux/sched.h>
  12. #include <linux/tty.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/delay.h>
  16. #include <linux/kbd_ll.h>
  17. #include <linux/kbd_kern.h>
  18. #include <linux/vt_kern.h>
  19. #include <asm/keyboard.h>
  20. #include <asm/dec/tc.h>
  21. #include <asm/dec/machtype.h>
  22. #include <asm/dec/serial.h>
  23. #include "lk201.h"
  24. /*
  25. * Only handle DECstations that have an LK201 interface.
  26. * Maxine uses LK501 at the Access.Bus and various DECsystems
  27. * have no keyboard interface at all.
  28. */
  29. #define LK_IFACE (mips_machtype == MACH_DS23100 || \
  30. mips_machtype == MACH_DS5000_200 || \
  31. mips_machtype == MACH_DS5000_1XX || \
  32. mips_machtype == MACH_DS5000_2X0)
  33. /*
  34. * These use the Z8530 SCC. Others use the DZ11.
  35. */
  36. #define LK_IFACE_ZS (mips_machtype == MACH_DS5000_1XX || \
  37. mips_machtype == MACH_DS5000_2X0)
  38. /* Simple translation table for the SysRq keys */
  39. #ifdef CONFIG_MAGIC_SYSRQ
  40. /*
  41. * Actually no translation at all, at least until we figure out
  42. * how to define SysRq for LK201 and friends. --macro
  43. */
  44. unsigned char lk201_sysrq_xlate[128];
  45. unsigned char *kbd_sysrq_xlate = lk201_sysrq_xlate;
  46. unsigned char kbd_sysrq_key = -1;
  47. #endif
  48. #define KEYB_LINE 3
  49. static int __init lk201_init(void *);
  50. static void __init lk201_info(void *);
  51. static void lk201_rx_char(unsigned char, unsigned char);
  52. static struct dec_serial_hook lk201_hook = {
  53. .init_channel = lk201_init,
  54. .init_info = lk201_info,
  55. .rx_char = NULL,
  56. .poll_rx_char = NULL,
  57. .poll_tx_char = NULL,
  58. .cflags = B4800 | CS8 | CSTOPB | CLOCAL,
  59. };
  60. /*
  61. * This is used during keyboard initialisation
  62. */
  63. static unsigned char lk201_reset_string[] = {
  64. LK_CMD_SET_DEFAULTS,
  65. LK_CMD_MODE(LK_MODE_RPT_DOWN, 1),
  66. LK_CMD_MODE(LK_MODE_RPT_DOWN, 2),
  67. LK_CMD_MODE(LK_MODE_RPT_DOWN, 3),
  68. LK_CMD_MODE(LK_MODE_RPT_DOWN, 4),
  69. LK_CMD_MODE(LK_MODE_DOWN_UP, 5),
  70. LK_CMD_MODE(LK_MODE_DOWN_UP, 6),
  71. LK_CMD_MODE(LK_MODE_RPT_DOWN, 7),
  72. LK_CMD_MODE(LK_MODE_RPT_DOWN, 8),
  73. LK_CMD_MODE(LK_MODE_RPT_DOWN, 9),
  74. LK_CMD_MODE(LK_MODE_RPT_DOWN, 10),
  75. LK_CMD_MODE(LK_MODE_RPT_DOWN, 11),
  76. LK_CMD_MODE(LK_MODE_RPT_DOWN, 12),
  77. LK_CMD_MODE(LK_MODE_DOWN, 13),
  78. LK_CMD_MODE(LK_MODE_RPT_DOWN, 14),
  79. LK_CMD_DIS_KEYCLK,
  80. LK_CMD_ENB_BELL, LK_PARAM_VOLUME(4),
  81. };
  82. static void *lk201_handle;
  83. static int lk201_send(unsigned char ch)
  84. {
  85. if (lk201_hook.poll_tx_char(lk201_handle, ch)) {
  86. printk(KERN_ERR "lk201: transmit timeout\n");
  87. return -EIO;
  88. }
  89. return 0;
  90. }
  91. static inline int lk201_get_id(void)
  92. {
  93. return lk201_send(LK_CMD_REQ_ID);
  94. }
  95. static int lk201_reset(void)
  96. {
  97. int i, r;
  98. for (i = 0; i < sizeof(lk201_reset_string); i++) {
  99. r = lk201_send(lk201_reset_string[i]);
  100. if (r < 0)
  101. return r;
  102. }
  103. return 0;
  104. }
  105. static void lk201_report(unsigned char id[6])
  106. {
  107. char *report = "lk201: keyboard attached, ";
  108. switch (id[2]) {
  109. case LK_STAT_PWRUP_OK:
  110. printk(KERN_INFO "%sself-test OK\n", report);
  111. break;
  112. case LK_STAT_PWRUP_KDOWN:
  113. /* The keyboard will resend the power-up ID
  114. after all keys are released, so we don't
  115. bother handling the error specially. Still
  116. there may be a short-circuit inside.
  117. */
  118. printk(KERN_ERR "%skey down (stuck?), code: 0x%02x\n",
  119. report, id[3]);
  120. break;
  121. case LK_STAT_PWRUP_ERROR:
  122. printk(KERN_ERR "%sself-test failure\n", report);
  123. break;
  124. default:
  125. printk(KERN_ERR "%sunknown error: 0x%02x\n",
  126. report, id[2]);
  127. }
  128. }
  129. static void lk201_id(unsigned char id[6])
  130. {
  131. /*
  132. * Report whether there is an LK201 or an LK401
  133. * The LK401 has ALT keys...
  134. */
  135. switch (id[4]) {
  136. case 1:
  137. printk(KERN_INFO "lk201: LK201 detected\n");
  138. break;
  139. case 2:
  140. printk(KERN_INFO "lk201: LK401 detected\n");
  141. break;
  142. case 3:
  143. printk(KERN_INFO "lk201: LK443 detected\n");
  144. break;
  145. case 4:
  146. printk(KERN_INFO "lk201: LK421 detected\n");
  147. break;
  148. default:
  149. printk(KERN_WARNING
  150. "lk201: unknown keyboard detected, ID %d\n", id[4]);
  151. printk(KERN_WARNING "lk201: ... please report to "
  152. "<linux-mips@linux-mips.org>\n");
  153. }
  154. }
  155. #define DEFAULT_KEYB_REP_DELAY (250/5) /* [5ms] */
  156. #define DEFAULT_KEYB_REP_RATE 30 /* [cps] */
  157. static struct kbd_repeat kbdrate = {
  158. DEFAULT_KEYB_REP_DELAY,
  159. DEFAULT_KEYB_REP_RATE
  160. };
  161. static void parse_kbd_rate(struct kbd_repeat *r)
  162. {
  163. if (r->delay <= 0)
  164. r->delay = kbdrate.delay;
  165. if (r->rate <= 0)
  166. r->rate = kbdrate.rate;
  167. if (r->delay < 5)
  168. r->delay = 5;
  169. if (r->delay > 630)
  170. r->delay = 630;
  171. if (r->rate < 12)
  172. r->rate = 12;
  173. if (r->rate > 127)
  174. r->rate = 127;
  175. if (r->rate == 125)
  176. r->rate = 124;
  177. }
  178. static int write_kbd_rate(struct kbd_repeat *rep)
  179. {
  180. int delay, rate;
  181. int i;
  182. delay = rep->delay / 5;
  183. rate = rep->rate;
  184. for (i = 0; i < 4; i++) {
  185. if (lk201_hook.poll_tx_char(lk201_handle,
  186. LK_CMD_RPT_RATE(i)))
  187. return 1;
  188. if (lk201_hook.poll_tx_char(lk201_handle,
  189. LK_PARAM_DELAY(delay)))
  190. return 1;
  191. if (lk201_hook.poll_tx_char(lk201_handle,
  192. LK_PARAM_RATE(rate)))
  193. return 1;
  194. }
  195. return 0;
  196. }
  197. static int lk201_kbd_rate(struct kbd_repeat *rep)
  198. {
  199. if (rep == NULL)
  200. return -EINVAL;
  201. parse_kbd_rate(rep);
  202. if (write_kbd_rate(rep)) {
  203. memcpy(rep, &kbdrate, sizeof(struct kbd_repeat));
  204. return -EIO;
  205. }
  206. memcpy(&kbdrate, rep, sizeof(struct kbd_repeat));
  207. return 0;
  208. }
  209. static void lk201_kd_mksound(unsigned int hz, unsigned int ticks)
  210. {
  211. if (!ticks)
  212. return;
  213. /*
  214. * Can't set frequency and we "approximate"
  215. * duration by volume. ;-)
  216. */
  217. ticks /= HZ / 32;
  218. if (ticks > 7)
  219. ticks = 7;
  220. ticks = 7 - ticks;
  221. if (lk201_hook.poll_tx_char(lk201_handle, LK_CMD_ENB_BELL))
  222. return;
  223. if (lk201_hook.poll_tx_char(lk201_handle, LK_PARAM_VOLUME(ticks)))
  224. return;
  225. if (lk201_hook.poll_tx_char(lk201_handle, LK_CMD_BELL))
  226. return;
  227. }
  228. void kbd_leds(unsigned char leds)
  229. {
  230. unsigned char l = 0;
  231. if (!lk201_handle) /* FIXME */
  232. return;
  233. /* FIXME -- Only Hold and Lock LEDs for now. --macro */
  234. if (leds & LED_SCR)
  235. l |= LK_LED_HOLD;
  236. if (leds & LED_CAP)
  237. l |= LK_LED_LOCK;
  238. if (lk201_hook.poll_tx_char(lk201_handle, LK_CMD_LEDS_ON))
  239. return;
  240. if (lk201_hook.poll_tx_char(lk201_handle, LK_PARAM_LED_MASK(l)))
  241. return;
  242. if (lk201_hook.poll_tx_char(lk201_handle, LK_CMD_LEDS_OFF))
  243. return;
  244. if (lk201_hook.poll_tx_char(lk201_handle, LK_PARAM_LED_MASK(~l)))
  245. return;
  246. }
  247. int kbd_setkeycode(unsigned int scancode, unsigned int keycode)
  248. {
  249. return -EINVAL;
  250. }
  251. int kbd_getkeycode(unsigned int scancode)
  252. {
  253. return -EINVAL;
  254. }
  255. int kbd_translate(unsigned char scancode, unsigned char *keycode,
  256. char raw_mode)
  257. {
  258. *keycode = scancode;
  259. return 1;
  260. }
  261. char kbd_unexpected_up(unsigned char keycode)
  262. {
  263. return 0x80;
  264. }
  265. static void lk201_rx_char(unsigned char ch, unsigned char fl)
  266. {
  267. static unsigned char id[6];
  268. static int id_i;
  269. static int shift_state = 0;
  270. static int prev_scancode;
  271. unsigned char c = scancodeRemap[ch];
  272. if (fl != TTY_NORMAL && fl != TTY_OVERRUN) {
  273. printk(KERN_ERR "lk201: keyboard receive error: 0x%02x\n", fl);
  274. return;
  275. }
  276. /* Assume this is a power-up ID. */
  277. if (ch == LK_STAT_PWRUP_ID && !id_i) {
  278. id[id_i++] = ch;
  279. return;
  280. }
  281. /* Handle the power-up sequence. */
  282. if (id_i) {
  283. id[id_i++] = ch;
  284. if (id_i == 4) {
  285. /* OK, the power-up concluded. */
  286. lk201_report(id);
  287. if (id[2] == LK_STAT_PWRUP_OK)
  288. lk201_get_id();
  289. else {
  290. id_i = 0;
  291. printk(KERN_ERR "lk201: keyboard power-up "
  292. "error, skipping initialization\n");
  293. }
  294. } else if (id_i == 6) {
  295. /* We got the ID; report it and start operation. */
  296. id_i = 0;
  297. lk201_id(id);
  298. lk201_reset();
  299. }
  300. return;
  301. }
  302. /* Everything else is a scancode/status response. */
  303. id_i = 0;
  304. switch (ch) {
  305. case LK_STAT_RESUME_ERR:
  306. case LK_STAT_ERROR:
  307. case LK_STAT_INHIBIT_ACK:
  308. case LK_STAT_TEST_ACK:
  309. case LK_STAT_MODE_KEYDOWN:
  310. case LK_STAT_MODE_ACK:
  311. break;
  312. case LK_KEY_LOCK:
  313. shift_state ^= LK_LOCK;
  314. handle_scancode(c, (shift_state & LK_LOCK) ? 1 : 0);
  315. break;
  316. case LK_KEY_SHIFT:
  317. shift_state ^= LK_SHIFT;
  318. handle_scancode(c, (shift_state & LK_SHIFT) ? 1 : 0);
  319. break;
  320. case LK_KEY_CTRL:
  321. shift_state ^= LK_CTRL;
  322. handle_scancode(c, (shift_state & LK_CTRL) ? 1 : 0);
  323. break;
  324. case LK_KEY_COMP:
  325. shift_state ^= LK_COMP;
  326. handle_scancode(c, (shift_state & LK_COMP) ? 1 : 0);
  327. break;
  328. case LK_KEY_RELEASE:
  329. if (shift_state & LK_SHIFT)
  330. handle_scancode(scancodeRemap[LK_KEY_SHIFT], 0);
  331. if (shift_state & LK_CTRL)
  332. handle_scancode(scancodeRemap[LK_KEY_CTRL], 0);
  333. if (shift_state & LK_COMP)
  334. handle_scancode(scancodeRemap[LK_KEY_COMP], 0);
  335. if (shift_state & LK_LOCK)
  336. handle_scancode(scancodeRemap[LK_KEY_LOCK], 0);
  337. shift_state = 0;
  338. break;
  339. case LK_KEY_REPEAT:
  340. handle_scancode(prev_scancode, 1);
  341. break;
  342. default:
  343. prev_scancode = c;
  344. handle_scancode(c, 1);
  345. break;
  346. }
  347. tasklet_schedule(&keyboard_tasklet);
  348. }
  349. static void __init lk201_info(void *handle)
  350. {
  351. }
  352. static int __init lk201_init(void *handle)
  353. {
  354. /* First install handlers. */
  355. lk201_handle = handle;
  356. kbd_rate = lk201_kbd_rate;
  357. kd_mksound = lk201_kd_mksound;
  358. lk201_hook.rx_char = lk201_rx_char;
  359. /* Then just issue a reset -- the handlers will do the rest. */
  360. lk201_send(LK_CMD_POWER_UP);
  361. return 0;
  362. }
  363. void __init kbd_init_hw(void)
  364. {
  365. /* Maxine uses LK501 at the Access.Bus. */
  366. if (!LK_IFACE)
  367. return;
  368. printk(KERN_INFO "lk201: DECstation LK keyboard driver v0.05.\n");
  369. if (LK_IFACE_ZS) {
  370. /*
  371. * kbd_init_hw() is being called before
  372. * rs_init() so just register the kbd hook
  373. * and let zs_init do the rest :-)
  374. */
  375. if (!register_dec_serial_hook(KEYB_LINE, &lk201_hook))
  376. unregister_dec_serial_hook(KEYB_LINE);
  377. } else {
  378. /*
  379. * TODO: modify dz.c to allow similar hooks
  380. * for LK201 handling on DS2100, DS3100, and DS5000/200
  381. */
  382. printk(KERN_ERR "lk201: support for DZ11 not yet ready.\n");
  383. }
  384. }