1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Minimalistic braille device kernel support.
5 * By default, shows console messages on the braille device.
6 * Pressing Insert switches to VC browsing.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/console.h>
15 #include <linux/notifier.h>
17 #include <linux/selection.h>
18 #include <linux/vt_kern.h>
19 #include <linux/consolemap.h>
21 #include <linux/keyboard.h>
22 #include <linux/kbd_kern.h>
23 #include <linux/input.h>
26 MODULE_DESCRIPTION("braille device");
27 MODULE_LICENSE("GPL");
30 * Braille device support part.
33 /* Emit various sounds */
35 module_param(sound, bool, 0);
36 MODULE_PARM_DESC(sound, "emit sounds");
38 static void beep(unsigned int freq)
41 kd_mksound(freq, HZ/10);
46 #define BRAILLE_KEY KEY_INSERT
47 static u16 console_buf[WIDTH];
48 static int console_cursor;
51 static int vc_x, vc_y, lastvc_x, lastvc_y;
53 /* show console ? (or show VC) */
54 static int console_show = 1;
55 /* pending newline ? */
56 static int console_newline = 1;
57 static int lastVC = -1;
59 static struct console *braille_co;
61 /* Very VisioBraille-specific */
62 static void braille_write(u16 *buf)
64 static u16 lastwrite[WIDTH];
65 unsigned char data[1 + 1 + 2*WIDTH + 2 + 1], csum = 0, *c;
72 if (!memcmp(lastwrite, buf, WIDTH * sizeof(*buf)))
74 memcpy(lastwrite, buf, WIDTH * sizeof(*buf));
85 for (i = 0; i < WIDTH; i++) {
106 braille_co->write(braille_co, data, c - data);
109 /* Follow the VC cursor*/
110 static void vc_follow_cursor(struct vc_data *vc)
112 vc_x = vc->state.x - (vc->state.x % WIDTH);
114 lastvc_x = vc->state.x;
115 lastvc_y = vc->state.y;
118 /* Maybe the VC cursor moved, if so follow it */
119 static void vc_maybe_cursor_moved(struct vc_data *vc)
121 if (vc->state.x != lastvc_x || vc->state.y != lastvc_y)
122 vc_follow_cursor(vc);
125 /* Show portion of VC at vc_x, vc_y */
126 static void vc_refresh(struct vc_data *vc)
131 for (i = 0; i < WIDTH; i++) {
132 u16 glyph = screen_glyph(vc,
133 2 * (vc_x + i) + vc_y * vc->vc_size_row);
134 buf[i] = inverse_translate(vc, glyph, 1);
143 static int keyboard_notifier_call(struct notifier_block *blk,
144 unsigned long code, void *_param)
146 struct keyboard_notifier_param *param = _param;
147 struct vc_data *vc = param->vc;
156 if (param->value == BRAILLE_KEY) {
159 vc_maybe_cursor_moved(vc);
165 switch (param->value) {
170 braille_write(console_buf);
177 } else if (vc_y >= 1) {
180 vc_x = vc->vc_cols-WIDTH;
185 if (vc_x + WIDTH < vc->vc_cols) {
187 } else if (vc_y + 1 < vc->vc_rows) {
195 if (vc_y + 1 < vc->vc_rows)
207 vc_follow_cursor(vc);
215 vc_y = vc->vc_rows-1;
221 if (ret == NOTIFY_STOP)
225 case KBD_POST_KEYSYM:
227 unsigned char type = KTYP(param->value) - 0xf0;
228 if (type == KT_SPEC) {
229 unsigned char val = KVAL(param->value);
234 on_off = vt_get_leds(fg_console, VC_CAPSLOCK);
237 on_off = vt_get_leds(fg_console, VC_NUMLOCK);
240 on_off = vt_get_leds(fg_console, VC_SCROLLOCK);
245 else if (on_off == 0)
249 case KBD_UNBOUND_KEYCODE:
258 static struct notifier_block keyboard_notifier_block = {
259 .notifier_call = keyboard_notifier_call,
262 static int vt_notifier_call(struct notifier_block *blk,
263 unsigned long code, void *_param)
265 struct vt_notifier_param *param = _param;
266 struct vc_data *vc = param->vc;
274 unsigned char c = param->c;
275 if (vc->vc_num != fg_console)
280 if (console_cursor > 0) {
282 console_buf[console_cursor] = ' ';
296 /* Ignore other control sequences */
298 if (console_newline) {
299 memset(console_buf, 0, sizeof(console_buf));
303 if (console_cursor == WIDTH)
304 memmove(console_buf, &console_buf[1],
305 (WIDTH-1) * sizeof(*console_buf));
308 console_buf[console_cursor-1] = c;
312 braille_write(console_buf);
314 vc_maybe_cursor_moved(vc);
320 /* Maybe a VT switch, flush */
322 if (vc->vc_num != lastVC) {
324 memset(console_buf, 0, sizeof(console_buf));
326 braille_write(console_buf);
329 vc_maybe_cursor_moved(vc);
337 static struct notifier_block vt_notifier_block = {
338 .notifier_call = vt_notifier_call,
342 * Called from printk.c when console=brl is given
345 int braille_register_console(struct console *console, int index,
346 char *console_options, char *braille_options)
350 if (!console_options)
351 /* Only support VisioBraille for now */
352 console_options = "57600o8";
355 if (console->setup) {
356 ret = console->setup(console, console_options);
360 console->flags |= CON_ENABLED;
361 console->index = index;
362 braille_co = console;
363 register_keyboard_notifier(&keyboard_notifier_block);
364 register_vt_notifier(&vt_notifier_block);
368 int braille_unregister_console(struct console *console)
370 if (braille_co != console)
372 unregister_keyboard_notifier(&keyboard_notifier_block);
373 unregister_vt_notifier(&vt_notifier_block);