keyboard.c 12 KB

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