1 // SPDX-License-Identifier: GPL-2.0
3 * ebcdic keycode functions for s390 console drivers
6 * Copyright IBM Corp. 2003
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
10 #include <linux/module.h>
11 #include <linux/sched/signal.h>
12 #include <linux/slab.h>
13 #include <linux/sysrq.h>
15 #include <linux/consolemap.h>
16 #include <linux/kbd_kern.h>
17 #include <linux/kbd_diacr.h>
18 #include <linux/uaccess.h>
26 k_self, k_fn, k_spec, k_ignore,\
27 k_dead, k_ignore, k_ignore, k_ignore,\
28 k_ignore, k_ignore, k_ignore, k_ignore,\
29 k_ignore, k_ignore, k_ignore, k_ignore
31 typedef void (k_handler_fn
)(struct kbd_data
*, unsigned char);
32 static k_handler_fn K_HANDLERS
;
33 static k_handler_fn
*k_handler
[16] = { K_HANDLERS
};
35 /* maximum values each key_handler can handle */
36 static const int kbd_max_vals
[] = {
37 255, ARRAY_SIZE(func_table
) - 1, NR_FN_HANDLER
- 1, 0,
38 NR_DEAD
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
40 static const int KBD_NR_TYPES
= ARRAY_SIZE(kbd_max_vals
);
42 static const unsigned char ret_diacr
[NR_DEAD
] = {
44 '\'', /* dead_acute */
45 '^', /* dead_circumflex */
47 '"', /* dead_diaeresis */
48 ',', /* dead_cedilla */
49 '_', /* dead_macron */
51 '.', /* dead_abovedot */
52 '*', /* dead_abovering */
53 '=', /* dead_doubleacute */
55 'k', /* dead_ogonek */
57 '#', /* dead_voiced_sound */
58 'o', /* dead_semivoiced_sound */
59 '!', /* dead_belowdot */
62 '-', /* dead_stroke */
63 ')', /* dead_abovecomma */
64 '(', /* dead_abovereversedcomma */
65 ':', /* dead_doublegrave */
66 'n', /* dead_invertedbreve */
67 ';', /* dead_belowcomma */
68 '$', /* dead_currency */
73 * Alloc/free of kbd_data structures.
80 kbd
= kzalloc(sizeof(struct kbd_data
), GFP_KERNEL
);
83 kbd
->key_maps
= kzalloc(sizeof(ebc_key_maps
), GFP_KERNEL
);
86 for (i
= 0; i
< ARRAY_SIZE(ebc_key_maps
); i
++) {
87 if (ebc_key_maps
[i
]) {
88 kbd
->key_maps
[i
] = kmemdup(ebc_key_maps
[i
],
89 sizeof(u_short
) * NR_KEYS
,
91 if (!kbd
->key_maps
[i
])
95 kbd
->func_table
= kzalloc(sizeof(ebc_func_table
), GFP_KERNEL
);
98 for (i
= 0; i
< ARRAY_SIZE(ebc_func_table
); i
++) {
99 if (ebc_func_table
[i
]) {
100 kbd
->func_table
[i
] = kstrdup(ebc_func_table
[i
],
102 if (!kbd
->func_table
[i
])
107 kcalloc(NR_FN_HANDLER
, sizeof(fn_handler_fn
*), GFP_KERNEL
);
108 if (!kbd
->fn_handler
)
110 kbd
->accent_table
= kmemdup(ebc_accent_table
,
111 sizeof(struct kbdiacruc
) * MAX_DIACR
,
113 if (!kbd
->accent_table
)
115 kbd
->accent_table_size
= ebc_accent_table_size
;
119 kfree(kbd
->fn_handler
);
121 for (i
= 0; i
< ARRAY_SIZE(ebc_func_table
); i
++)
122 kfree(kbd
->func_table
[i
]);
123 kfree(kbd
->func_table
);
125 for (i
= 0; i
< ARRAY_SIZE(ebc_key_maps
); i
++)
126 kfree(kbd
->key_maps
[i
]);
127 kfree(kbd
->key_maps
);
135 kbd_free(struct kbd_data
*kbd
)
139 kfree(kbd
->accent_table
);
140 kfree(kbd
->fn_handler
);
141 for (i
= 0; i
< ARRAY_SIZE(ebc_func_table
); i
++)
142 kfree(kbd
->func_table
[i
]);
143 kfree(kbd
->func_table
);
144 for (i
= 0; i
< ARRAY_SIZE(ebc_key_maps
); i
++)
145 kfree(kbd
->key_maps
[i
]);
146 kfree(kbd
->key_maps
);
151 * Generate ascii -> ebcdic translation table from kbd_data.
154 kbd_ascebc(struct kbd_data
*kbd
, unsigned char *ascebc
)
156 unsigned short *keymap
, keysym
;
159 memset(ascebc
, 0x40, 256);
160 for (i
= 0; i
< ARRAY_SIZE(ebc_key_maps
); i
++) {
161 keymap
= kbd
->key_maps
[i
];
164 for (j
= 0; j
< NR_KEYS
; j
++) {
165 k
= ((i
& 1) << 7) + j
;
167 if (KTYP(keysym
) == (KT_LATIN
| 0xf0) ||
168 KTYP(keysym
) == (KT_LETTER
| 0xf0))
169 ascebc
[KVAL(keysym
)] = k
;
170 else if (KTYP(keysym
) == (KT_DEAD
| 0xf0))
171 ascebc
[ret_diacr
[KVAL(keysym
)]] = k
;
178 * Generate ebcdic -> ascii translation table from kbd_data.
181 kbd_ebcasc(struct kbd_data
*kbd
, unsigned char *ebcasc
)
183 unsigned short *keymap
, keysym
;
186 memset(ebcasc
, ' ', 256);
187 for (i
= 0; i
< ARRAY_SIZE(ebc_key_maps
); i
++) {
188 keymap
= kbd
->key_maps
[i
];
191 for (j
= 0; j
< NR_KEYS
; j
++) {
193 k
= ((i
& 1) << 7) + j
;
194 if (KTYP(keysym
) == (KT_LATIN
| 0xf0) ||
195 KTYP(keysym
) == (KT_LETTER
| 0xf0))
196 ebcasc
[k
] = KVAL(keysym
);
197 else if (KTYP(keysym
) == (KT_DEAD
| 0xf0))
198 ebcasc
[k
] = ret_diacr
[KVAL(keysym
)];
205 * We have a combining character DIACR here, followed by the character CH.
206 * If the combination occurs in the table, return the corresponding value.
207 * Otherwise, if CH is a space or equals DIACR, return DIACR.
208 * Otherwise, conclude that DIACR was not combining after all,
209 * queue it and return CH.
212 handle_diacr(struct kbd_data
*kbd
, unsigned int ch
)
219 for (i
= 0; i
< kbd
->accent_table_size
; i
++) {
220 if (kbd
->accent_table
[i
].diacr
== d
&&
221 kbd
->accent_table
[i
].base
== ch
)
222 return kbd
->accent_table
[i
].result
;
225 if (ch
== ' ' || ch
== d
)
228 kbd_put_queue(kbd
->port
, d
);
236 k_dead(struct kbd_data
*kbd
, unsigned char value
)
238 value
= ret_diacr
[value
];
239 kbd
->diacr
= (kbd
->diacr
? handle_diacr(kbd
, value
) : value
);
243 * Normal character handler.
246 k_self(struct kbd_data
*kbd
, unsigned char value
)
249 value
= handle_diacr(kbd
, value
);
250 kbd_put_queue(kbd
->port
, value
);
254 * Special key handlers
257 k_ignore(struct kbd_data
*kbd
, unsigned char value
)
262 * Function key handler.
265 k_fn(struct kbd_data
*kbd
, unsigned char value
)
267 if (kbd
->func_table
[value
])
268 kbd_puts_queue(kbd
->port
, kbd
->func_table
[value
]);
272 k_spec(struct kbd_data
*kbd
, unsigned char value
)
274 if (value
>= NR_FN_HANDLER
)
276 if (kbd
->fn_handler
[value
])
277 kbd
->fn_handler
[value
](kbd
);
281 * Put utf8 character to tty flip buffer.
282 * UTF-8 is defined for words of up to 31 bits,
283 * but we need only 16 bits here
286 to_utf8(struct tty_port
*port
, ushort c
)
290 kbd_put_queue(port
, c
);
291 else if (c
< 0x800) {
292 /* 110***** 10****** */
293 kbd_put_queue(port
, 0xc0 | (c
>> 6));
294 kbd_put_queue(port
, 0x80 | (c
& 0x3f));
296 /* 1110**** 10****** 10****** */
297 kbd_put_queue(port
, 0xe0 | (c
>> 12));
298 kbd_put_queue(port
, 0x80 | ((c
>> 6) & 0x3f));
299 kbd_put_queue(port
, 0x80 | (c
& 0x3f));
307 kbd_keycode(struct kbd_data
*kbd
, unsigned int keycode
)
309 unsigned short keysym
;
310 unsigned char type
, value
;
316 keysym
= kbd
->key_maps
[5][keycode
- 384];
317 else if (keycode
>= 256)
318 keysym
= kbd
->key_maps
[4][keycode
- 256];
319 else if (keycode
>= 128)
320 keysym
= kbd
->key_maps
[1][keycode
- 128];
322 keysym
= kbd
->key_maps
[0][keycode
];
327 if (type
== KT_LETTER
)
329 value
= KVAL(keysym
);
330 #ifdef CONFIG_MAGIC_SYSRQ /* Handle the SysRq Hack */
332 if (kbd
->sysrq
== K(KT_LATIN
, '-')) {
338 kbd
->sysrq
= K(KT_LATIN
, '-');
341 /* Incomplete sysrq sequence. */
342 (*k_handler
[KTYP(kbd
->sysrq
)])(kbd
, KVAL(kbd
->sysrq
));
344 } else if ((type
== KT_LATIN
&& value
== '^') ||
345 (type
== KT_DEAD
&& ret_diacr
[value
] == '^')) {
346 kbd
->sysrq
= K(type
, value
);
350 (*k_handler
[type
])(kbd
, value
);
352 to_utf8(kbd
->port
, keysym
);
359 do_kdsk_ioctl(struct kbd_data
*kbd
, struct kbentry __user
*user_kbe
,
363 unsigned long kb_index
, kb_table
;
364 ushort
*key_map
, val
, ov
;
366 if (copy_from_user(&tmp
, user_kbe
, sizeof(struct kbentry
)))
368 kb_index
= (unsigned long) tmp
.kb_index
;
370 if (kb_index
>= NR_KEYS
)
373 kb_table
= (unsigned long) tmp
.kb_table
;
374 #if MAX_NR_KEYMAPS < 256
375 if (kb_table
>= MAX_NR_KEYMAPS
)
377 kb_table
= array_index_nospec(kb_table
, MAX_NR_KEYMAPS
);
382 key_map
= kbd
->key_maps
[kb_table
];
384 val
= U(key_map
[kb_index
]);
385 if (KTYP(val
) >= KBD_NR_TYPES
)
388 val
= (kb_index
? K_HOLE
: K_NOSUCHMAP
);
389 return put_user(val
, &user_kbe
->kb_value
);
393 if (!kb_index
&& tmp
.kb_value
== K_NOSUCHMAP
) {
394 /* disallocate map */
395 key_map
= kbd
->key_maps
[kb_table
];
397 kbd
->key_maps
[kb_table
] = NULL
;
403 if (KTYP(tmp
.kb_value
) >= KBD_NR_TYPES
)
405 if (KVAL(tmp
.kb_value
) > kbd_max_vals
[KTYP(tmp
.kb_value
)])
408 if (!(key_map
= kbd
->key_maps
[kb_table
])) {
411 key_map
= kmalloc(sizeof(plain_map
),
415 kbd
->key_maps
[kb_table
] = key_map
;
416 for (j
= 0; j
< NR_KEYS
; j
++)
417 key_map
[j
] = U(K_HOLE
);
419 ov
= U(key_map
[kb_index
]);
420 if (tmp
.kb_value
== ov
)
421 break; /* nothing to do */
425 if (((ov
== K_SAK
) || (tmp
.kb_value
== K_SAK
)) &&
426 !capable(CAP_SYS_ADMIN
))
428 key_map
[kb_index
] = U(tmp
.kb_value
);
435 do_kdgkb_ioctl(struct kbd_data
*kbd
, struct kbsentry __user
*u_kbs
,
438 unsigned char kb_func
;
442 /* Get u_kbs->kb_func. */
443 if (get_user(kb_func
, &u_kbs
->kb_func
))
445 #if MAX_NR_FUNC < 256
446 if (kb_func
>= MAX_NR_FUNC
)
452 p
= kbd
->func_table
[kb_func
];
455 if (len
>= sizeof(u_kbs
->kb_string
))
456 len
= sizeof(u_kbs
->kb_string
) - 1;
457 if (copy_to_user(u_kbs
->kb_string
, p
, len
))
461 if (put_user('\0', u_kbs
->kb_string
+ len
))
467 p
= strndup_user(u_kbs
->kb_string
, sizeof(u_kbs
->kb_string
));
470 kfree(kbd
->func_table
[kb_func
]);
471 kbd
->func_table
[kb_func
] = p
;
477 int kbd_ioctl(struct kbd_data
*kbd
, unsigned int cmd
, unsigned long arg
)
479 struct tty_struct
*tty
;
484 argp
= (void __user
*)arg
;
487 * To have permissions to do most of the vt ioctls, we either have
488 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
490 tty
= tty_port_tty_get(kbd
->port
);
491 /* FIXME this test is pretty racy */
492 perm
= current
->signal
->tty
== tty
|| capable(CAP_SYS_TTY_CONFIG
);
496 return put_user(KB_101
, (char __user
*)argp
);
499 return do_kdsk_ioctl(kbd
, argp
, cmd
, perm
);
502 return do_kdgkb_ioctl(kbd
, argp
, cmd
, perm
);
505 struct kbdiacrs __user
*a
= argp
;
506 struct kbdiacr diacr
;
509 if (put_user(kbd
->accent_table_size
, &a
->kb_cnt
))
511 for (i
= 0; i
< kbd
->accent_table_size
; i
++) {
512 diacr
.diacr
= kbd
->accent_table
[i
].diacr
;
513 diacr
.base
= kbd
->accent_table
[i
].base
;
514 diacr
.result
= kbd
->accent_table
[i
].result
;
515 if (copy_to_user(a
->kbdiacr
+ i
, &diacr
, sizeof(struct kbdiacr
)))
522 struct kbdiacrsuc __user
*a
= argp
;
524 ct
= kbd
->accent_table_size
;
525 if (put_user(ct
, &a
->kb_cnt
))
527 if (copy_to_user(a
->kbdiacruc
, kbd
->accent_table
,
528 ct
* sizeof(struct kbdiacruc
)))
534 struct kbdiacrs __user
*a
= argp
;
535 struct kbdiacr diacr
;
540 if (get_user(ct
, &a
->kb_cnt
))
544 kbd
->accent_table_size
= ct
;
545 for (i
= 0; i
< ct
; i
++) {
546 if (copy_from_user(&diacr
, a
->kbdiacr
+ i
, sizeof(struct kbdiacr
)))
548 kbd
->accent_table
[i
].diacr
= diacr
.diacr
;
549 kbd
->accent_table
[i
].base
= diacr
.base
;
550 kbd
->accent_table
[i
].result
= diacr
.result
;
556 struct kbdiacrsuc __user
*a
= argp
;
560 if (get_user(ct
, &a
->kb_cnt
))
564 kbd
->accent_table_size
= ct
;
565 if (copy_from_user(kbd
->accent_table
, a
->kbdiacruc
,
566 ct
* sizeof(struct kbdiacruc
)))
575 EXPORT_SYMBOL(kbd_ioctl
);
576 EXPORT_SYMBOL(kbd_ascebc
);
577 EXPORT_SYMBOL(kbd_free
);
578 EXPORT_SYMBOL(kbd_alloc
);
579 EXPORT_SYMBOL(kbd_keycode
);