keyboard.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * ebcdic keycode functions for s390 console drivers
  3. *
  4. * S390 version
  5. * Copyright IBM Corp. 2003
  6. * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
  7. */
  8. #include <linux/module.h>
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/sysrq.h>
  12. #include <linux/consolemap.h>
  13. #include <linux/kbd_kern.h>
  14. #include <linux/kbd_diacr.h>
  15. #include <asm/uaccess.h>
  16. #include "keyboard.h"
  17. /*
  18. * Handler Tables.
  19. */
  20. #define K_HANDLERS\
  21. k_self, k_fn, k_spec, k_ignore,\
  22. k_dead, k_ignore, k_ignore, k_ignore,\
  23. k_ignore, k_ignore, k_ignore, k_ignore,\
  24. k_ignore, k_ignore, k_ignore, k_ignore
  25. typedef void (k_handler_fn)(struct kbd_data *, unsigned char);
  26. static k_handler_fn K_HANDLERS;
  27. static k_handler_fn *k_handler[16] = { K_HANDLERS };
  28. /* maximum values each key_handler can handle */
  29. static const int kbd_max_vals[] = {
  30. 255, ARRAY_SIZE(func_table) - 1, NR_FN_HANDLER - 1, 0,
  31. NR_DEAD - 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  32. };
  33. static const int KBD_NR_TYPES = ARRAY_SIZE(kbd_max_vals);
  34. static unsigned char ret_diacr[NR_DEAD] = {
  35. '`', '\'', '^', '~', '"', ','
  36. };
  37. /*
  38. * Alloc/free of kbd_data structures.
  39. */
  40. struct kbd_data *
  41. kbd_alloc(void) {
  42. struct kbd_data *kbd;
  43. int i;
  44. kbd = kzalloc(sizeof(struct kbd_data), GFP_KERNEL);
  45. if (!kbd)
  46. goto out;
  47. kbd->key_maps = kzalloc(sizeof(key_maps), GFP_KERNEL);
  48. if (!kbd->key_maps)
  49. goto out_kbd;
  50. for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
  51. if (key_maps[i]) {
  52. kbd->key_maps[i] = kmemdup(key_maps[i],
  53. sizeof(u_short) * NR_KEYS,
  54. GFP_KERNEL);
  55. if (!kbd->key_maps[i])
  56. goto out_maps;
  57. }
  58. }
  59. kbd->func_table = kzalloc(sizeof(func_table), GFP_KERNEL);
  60. if (!kbd->func_table)
  61. goto out_maps;
  62. for (i = 0; i < ARRAY_SIZE(func_table); i++) {
  63. if (func_table[i]) {
  64. kbd->func_table[i] = kstrdup(func_table[i],
  65. GFP_KERNEL);
  66. if (!kbd->func_table[i])
  67. goto out_func;
  68. }
  69. }
  70. kbd->fn_handler =
  71. kzalloc(sizeof(fn_handler_fn *) * NR_FN_HANDLER, GFP_KERNEL);
  72. if (!kbd->fn_handler)
  73. goto out_func;
  74. kbd->accent_table = kmemdup(accent_table,
  75. sizeof(struct kbdiacruc) * MAX_DIACR,
  76. GFP_KERNEL);
  77. if (!kbd->accent_table)
  78. goto out_fn_handler;
  79. kbd->accent_table_size = accent_table_size;
  80. return kbd;
  81. out_fn_handler:
  82. kfree(kbd->fn_handler);
  83. out_func:
  84. for (i = 0; i < ARRAY_SIZE(func_table); i++)
  85. kfree(kbd->func_table[i]);
  86. kfree(kbd->func_table);
  87. out_maps:
  88. for (i = 0; i < ARRAY_SIZE(key_maps); i++)
  89. kfree(kbd->key_maps[i]);
  90. kfree(kbd->key_maps);
  91. out_kbd:
  92. kfree(kbd);
  93. out:
  94. return NULL;
  95. }
  96. void
  97. kbd_free(struct kbd_data *kbd)
  98. {
  99. int i;
  100. kfree(kbd->accent_table);
  101. kfree(kbd->fn_handler);
  102. for (i = 0; i < ARRAY_SIZE(func_table); i++)
  103. kfree(kbd->func_table[i]);
  104. kfree(kbd->func_table);
  105. for (i = 0; i < ARRAY_SIZE(key_maps); i++)
  106. kfree(kbd->key_maps[i]);
  107. kfree(kbd->key_maps);
  108. kfree(kbd);
  109. }
  110. /*
  111. * Generate ascii -> ebcdic translation table from kbd_data.
  112. */
  113. void
  114. kbd_ascebc(struct kbd_data *kbd, unsigned char *ascebc)
  115. {
  116. unsigned short *keymap, keysym;
  117. int i, j, k;
  118. memset(ascebc, 0x40, 256);
  119. for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
  120. keymap = kbd->key_maps[i];
  121. if (!keymap)
  122. continue;
  123. for (j = 0; j < NR_KEYS; j++) {
  124. k = ((i & 1) << 7) + j;
  125. keysym = keymap[j];
  126. if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
  127. KTYP(keysym) == (KT_LETTER | 0xf0))
  128. ascebc[KVAL(keysym)] = k;
  129. else if (KTYP(keysym) == (KT_DEAD | 0xf0))
  130. ascebc[ret_diacr[KVAL(keysym)]] = k;
  131. }
  132. }
  133. }
  134. #if 0
  135. /*
  136. * Generate ebcdic -> ascii translation table from kbd_data.
  137. */
  138. void
  139. kbd_ebcasc(struct kbd_data *kbd, unsigned char *ebcasc)
  140. {
  141. unsigned short *keymap, keysym;
  142. int i, j, k;
  143. memset(ebcasc, ' ', 256);
  144. for (i = 0; i < ARRAY_SIZE(key_maps); i++) {
  145. keymap = kbd->key_maps[i];
  146. if (!keymap)
  147. continue;
  148. for (j = 0; j < NR_KEYS; j++) {
  149. keysym = keymap[j];
  150. k = ((i & 1) << 7) + j;
  151. if (KTYP(keysym) == (KT_LATIN | 0xf0) ||
  152. KTYP(keysym) == (KT_LETTER | 0xf0))
  153. ebcasc[k] = KVAL(keysym);
  154. else if (KTYP(keysym) == (KT_DEAD | 0xf0))
  155. ebcasc[k] = ret_diacr[KVAL(keysym)];
  156. }
  157. }
  158. }
  159. #endif
  160. /*
  161. * We have a combining character DIACR here, followed by the character CH.
  162. * If the combination occurs in the table, return the corresponding value.
  163. * Otherwise, if CH is a space or equals DIACR, return DIACR.
  164. * Otherwise, conclude that DIACR was not combining after all,
  165. * queue it and return CH.
  166. */
  167. static unsigned int
  168. handle_diacr(struct kbd_data *kbd, unsigned int ch)
  169. {
  170. int i, d;
  171. d = kbd->diacr;
  172. kbd->diacr = 0;
  173. for (i = 0; i < kbd->accent_table_size; i++) {
  174. if (kbd->accent_table[i].diacr == d &&
  175. kbd->accent_table[i].base == ch)
  176. return kbd->accent_table[i].result;
  177. }
  178. if (ch == ' ' || ch == d)
  179. return d;
  180. kbd_put_queue(kbd->port, d);
  181. return ch;
  182. }
  183. /*
  184. * Handle dead key.
  185. */
  186. static void
  187. k_dead(struct kbd_data *kbd, unsigned char value)
  188. {
  189. value = ret_diacr[value];
  190. kbd->diacr = (kbd->diacr ? handle_diacr(kbd, value) : value);
  191. }
  192. /*
  193. * Normal character handler.
  194. */
  195. static void
  196. k_self(struct kbd_data *kbd, unsigned char value)
  197. {
  198. if (kbd->diacr)
  199. value = handle_diacr(kbd, value);
  200. kbd_put_queue(kbd->port, value);
  201. }
  202. /*
  203. * Special key handlers
  204. */
  205. static void
  206. k_ignore(struct kbd_data *kbd, unsigned char value)
  207. {
  208. }
  209. /*
  210. * Function key handler.
  211. */
  212. static void
  213. k_fn(struct kbd_data *kbd, unsigned char value)
  214. {
  215. if (kbd->func_table[value])
  216. kbd_puts_queue(kbd->port, kbd->func_table[value]);
  217. }
  218. static void
  219. k_spec(struct kbd_data *kbd, unsigned char value)
  220. {
  221. if (value >= NR_FN_HANDLER)
  222. return;
  223. if (kbd->fn_handler[value])
  224. kbd->fn_handler[value](kbd);
  225. }
  226. /*
  227. * Put utf8 character to tty flip buffer.
  228. * UTF-8 is defined for words of up to 31 bits,
  229. * but we need only 16 bits here
  230. */
  231. static void
  232. to_utf8(struct tty_port *port, ushort c)
  233. {
  234. if (c < 0x80)
  235. /* 0******* */
  236. kbd_put_queue(port, c);
  237. else if (c < 0x800) {
  238. /* 110***** 10****** */
  239. kbd_put_queue(port, 0xc0 | (c >> 6));
  240. kbd_put_queue(port, 0x80 | (c & 0x3f));
  241. } else {
  242. /* 1110**** 10****** 10****** */
  243. kbd_put_queue(port, 0xe0 | (c >> 12));
  244. kbd_put_queue(port, 0x80 | ((c >> 6) & 0x3f));
  245. kbd_put_queue(port, 0x80 | (c & 0x3f));
  246. }
  247. }
  248. /*
  249. * Process keycode.
  250. */
  251. void
  252. kbd_keycode(struct kbd_data *kbd, unsigned int keycode)
  253. {
  254. unsigned short keysym;
  255. unsigned char type, value;
  256. if (!kbd)
  257. return;
  258. if (keycode >= 384)
  259. keysym = kbd->key_maps[5][keycode - 384];
  260. else if (keycode >= 256)
  261. keysym = kbd->key_maps[4][keycode - 256];
  262. else if (keycode >= 128)
  263. keysym = kbd->key_maps[1][keycode - 128];
  264. else
  265. keysym = kbd->key_maps[0][keycode];
  266. type = KTYP(keysym);
  267. if (type >= 0xf0) {
  268. type -= 0xf0;
  269. if (type == KT_LETTER)
  270. type = KT_LATIN;
  271. value = KVAL(keysym);
  272. #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
  273. if (kbd->sysrq) {
  274. if (kbd->sysrq == K(KT_LATIN, '-')) {
  275. kbd->sysrq = 0;
  276. handle_sysrq(value);
  277. return;
  278. }
  279. if (value == '-') {
  280. kbd->sysrq = K(KT_LATIN, '-');
  281. return;
  282. }
  283. /* Incomplete sysrq sequence. */
  284. (*k_handler[KTYP(kbd->sysrq)])(kbd, KVAL(kbd->sysrq));
  285. kbd->sysrq = 0;
  286. } else if ((type == KT_LATIN && value == '^') ||
  287. (type == KT_DEAD && ret_diacr[value] == '^')) {
  288. kbd->sysrq = K(type, value);
  289. return;
  290. }
  291. #endif
  292. (*k_handler[type])(kbd, value);
  293. } else
  294. to_utf8(kbd->port, keysym);
  295. }
  296. /*
  297. * Ioctl stuff.
  298. */
  299. static int
  300. do_kdsk_ioctl(struct kbd_data *kbd, struct kbentry __user *user_kbe,
  301. int cmd, int perm)
  302. {
  303. struct kbentry tmp;
  304. ushort *key_map, val, ov;
  305. if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
  306. return -EFAULT;
  307. #if NR_KEYS < 256
  308. if (tmp.kb_index >= NR_KEYS)
  309. return -EINVAL;
  310. #endif
  311. #if MAX_NR_KEYMAPS < 256
  312. if (tmp.kb_table >= MAX_NR_KEYMAPS)
  313. return -EINVAL;
  314. #endif
  315. switch (cmd) {
  316. case KDGKBENT:
  317. key_map = kbd->key_maps[tmp.kb_table];
  318. if (key_map) {
  319. val = U(key_map[tmp.kb_index]);
  320. if (KTYP(val) >= KBD_NR_TYPES)
  321. val = K_HOLE;
  322. } else
  323. val = (tmp.kb_index ? K_HOLE : K_NOSUCHMAP);
  324. return put_user(val, &user_kbe->kb_value);
  325. case KDSKBENT:
  326. if (!perm)
  327. return -EPERM;
  328. if (!tmp.kb_index && tmp.kb_value == K_NOSUCHMAP) {
  329. /* disallocate map */
  330. key_map = kbd->key_maps[tmp.kb_table];
  331. if (key_map) {
  332. kbd->key_maps[tmp.kb_table] = NULL;
  333. kfree(key_map);
  334. }
  335. break;
  336. }
  337. if (KTYP(tmp.kb_value) >= KBD_NR_TYPES)
  338. return -EINVAL;
  339. if (KVAL(tmp.kb_value) > kbd_max_vals[KTYP(tmp.kb_value)])
  340. return -EINVAL;
  341. if (!(key_map = kbd->key_maps[tmp.kb_table])) {
  342. int j;
  343. key_map = kmalloc(sizeof(plain_map),
  344. GFP_KERNEL);
  345. if (!key_map)
  346. return -ENOMEM;
  347. kbd->key_maps[tmp.kb_table] = key_map;
  348. for (j = 0; j < NR_KEYS; j++)
  349. key_map[j] = U(K_HOLE);
  350. }
  351. ov = U(key_map[tmp.kb_index]);
  352. if (tmp.kb_value == ov)
  353. break; /* nothing to do */
  354. /*
  355. * Attention Key.
  356. */
  357. if (((ov == K_SAK) || (tmp.kb_value == K_SAK)) &&
  358. !capable(CAP_SYS_ADMIN))
  359. return -EPERM;
  360. key_map[tmp.kb_index] = U(tmp.kb_value);
  361. break;
  362. }
  363. return 0;
  364. }
  365. static int
  366. do_kdgkb_ioctl(struct kbd_data *kbd, struct kbsentry __user *u_kbs,
  367. int cmd, int perm)
  368. {
  369. unsigned char kb_func;
  370. char *p;
  371. int len;
  372. /* Get u_kbs->kb_func. */
  373. if (get_user(kb_func, &u_kbs->kb_func))
  374. return -EFAULT;
  375. #if MAX_NR_FUNC < 256
  376. if (kb_func >= MAX_NR_FUNC)
  377. return -EINVAL;
  378. #endif
  379. switch (cmd) {
  380. case KDGKBSENT:
  381. p = kbd->func_table[kb_func];
  382. if (p) {
  383. len = strlen(p);
  384. if (len >= sizeof(u_kbs->kb_string))
  385. len = sizeof(u_kbs->kb_string) - 1;
  386. if (copy_to_user(u_kbs->kb_string, p, len))
  387. return -EFAULT;
  388. } else
  389. len = 0;
  390. if (put_user('\0', u_kbs->kb_string + len))
  391. return -EFAULT;
  392. break;
  393. case KDSKBSENT:
  394. if (!perm)
  395. return -EPERM;
  396. len = strnlen_user(u_kbs->kb_string, sizeof(u_kbs->kb_string));
  397. if (!len)
  398. return -EFAULT;
  399. if (len > sizeof(u_kbs->kb_string))
  400. return -EINVAL;
  401. p = kmalloc(len, GFP_KERNEL);
  402. if (!p)
  403. return -ENOMEM;
  404. if (copy_from_user(p, u_kbs->kb_string, len)) {
  405. kfree(p);
  406. return -EFAULT;
  407. }
  408. /*
  409. * Make sure the string is terminated by 0. User could have
  410. * modified it between us running strnlen_user() and copying it.
  411. */
  412. p[len - 1] = 0;
  413. kfree(kbd->func_table[kb_func]);
  414. kbd->func_table[kb_func] = p;
  415. break;
  416. }
  417. return 0;
  418. }
  419. int kbd_ioctl(struct kbd_data *kbd, unsigned int cmd, unsigned long arg)
  420. {
  421. struct tty_struct *tty;
  422. void __user *argp;
  423. unsigned int ct;
  424. int perm;
  425. argp = (void __user *)arg;
  426. /*
  427. * To have permissions to do most of the vt ioctls, we either have
  428. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  429. */
  430. tty = tty_port_tty_get(kbd->port);
  431. /* FIXME this test is pretty racy */
  432. perm = current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG);
  433. tty_kref_put(tty);
  434. switch (cmd) {
  435. case KDGKBTYPE:
  436. return put_user(KB_101, (char __user *)argp);
  437. case KDGKBENT:
  438. case KDSKBENT:
  439. return do_kdsk_ioctl(kbd, argp, cmd, perm);
  440. case KDGKBSENT:
  441. case KDSKBSENT:
  442. return do_kdgkb_ioctl(kbd, argp, cmd, perm);
  443. case KDGKBDIACR:
  444. {
  445. struct kbdiacrs __user *a = argp;
  446. struct kbdiacr diacr;
  447. int i;
  448. if (put_user(kbd->accent_table_size, &a->kb_cnt))
  449. return -EFAULT;
  450. for (i = 0; i < kbd->accent_table_size; i++) {
  451. diacr.diacr = kbd->accent_table[i].diacr;
  452. diacr.base = kbd->accent_table[i].base;
  453. diacr.result = kbd->accent_table[i].result;
  454. if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr)))
  455. return -EFAULT;
  456. }
  457. return 0;
  458. }
  459. case KDGKBDIACRUC:
  460. {
  461. struct kbdiacrsuc __user *a = argp;
  462. ct = kbd->accent_table_size;
  463. if (put_user(ct, &a->kb_cnt))
  464. return -EFAULT;
  465. if (copy_to_user(a->kbdiacruc, kbd->accent_table,
  466. ct * sizeof(struct kbdiacruc)))
  467. return -EFAULT;
  468. return 0;
  469. }
  470. case KDSKBDIACR:
  471. {
  472. struct kbdiacrs __user *a = argp;
  473. struct kbdiacr diacr;
  474. int i;
  475. if (!perm)
  476. return -EPERM;
  477. if (get_user(ct, &a->kb_cnt))
  478. return -EFAULT;
  479. if (ct >= MAX_DIACR)
  480. return -EINVAL;
  481. kbd->accent_table_size = ct;
  482. for (i = 0; i < ct; i++) {
  483. if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr)))
  484. return -EFAULT;
  485. kbd->accent_table[i].diacr = diacr.diacr;
  486. kbd->accent_table[i].base = diacr.base;
  487. kbd->accent_table[i].result = diacr.result;
  488. }
  489. return 0;
  490. }
  491. case KDSKBDIACRUC:
  492. {
  493. struct kbdiacrsuc __user *a = argp;
  494. if (!perm)
  495. return -EPERM;
  496. if (get_user(ct, &a->kb_cnt))
  497. return -EFAULT;
  498. if (ct >= MAX_DIACR)
  499. return -EINVAL;
  500. kbd->accent_table_size = ct;
  501. if (copy_from_user(kbd->accent_table, a->kbdiacruc,
  502. ct * sizeof(struct kbdiacruc)))
  503. return -EFAULT;
  504. return 0;
  505. }
  506. default:
  507. return -ENOIOCTLCMD;
  508. }
  509. }
  510. EXPORT_SYMBOL(kbd_ioctl);
  511. EXPORT_SYMBOL(kbd_ascebc);
  512. EXPORT_SYMBOL(kbd_free);
  513. EXPORT_SYMBOL(kbd_alloc);
  514. EXPORT_SYMBOL(kbd_keycode);