Linux Audio

Check our new training course

Loading...
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Minimalistic braille device kernel support.
  4 *
  5 * By default, shows console messages on the braille device.
  6 * Pressing Insert switches to VC browsing.
  7 *
  8 *  Copyright (C) Samuel Thibault <samuel.thibault@ens-lyon.org>
  9 */
 10
 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>
 16
 17#include <linux/selection.h>
 18#include <linux/vt_kern.h>
 19#include <linux/consolemap.h>
 20
 21#include <linux/keyboard.h>
 22#include <linux/kbd_kern.h>
 23#include <linux/input.h>
 24
 25MODULE_AUTHOR("samuel.thibault@ens-lyon.org");
 26MODULE_DESCRIPTION("braille device");
 27
 28/*
 29 * Braille device support part.
 30 */
 31
 32/* Emit various sounds */
 33static bool sound;
 34module_param(sound, bool, 0);
 35MODULE_PARM_DESC(sound, "emit sounds");
 36
 37static void beep(unsigned int freq)
 38{
 39	if (sound)
 40		kd_mksound(freq, HZ/10);
 41}
 42
 43/* mini console */
 44#define WIDTH 40
 45#define BRAILLE_KEY KEY_INSERT
 46static u16 console_buf[WIDTH];
 47static int console_cursor;
 48
 49/* mini view of VC */
 50static int vc_x, vc_y, lastvc_x, lastvc_y;
 51
 52/* show console ? (or show VC) */
 53static int console_show = 1;
 54/* pending newline ? */
 55static int console_newline = 1;
 56static int lastVC = -1;
 57
 58static struct console *braille_co;
 59
 60/* Very VisioBraille-specific */
 61static void braille_write(u16 *buf)
 62{
 63	static u16 lastwrite[WIDTH];
 64	unsigned char data[1 + 1 + 2*WIDTH + 2 + 1], csum = 0, *c;
 65	u16 out;
 66	int i;
 67
 68	if (!braille_co)
 69		return;
 70
 71	if (!memcmp(lastwrite, buf, WIDTH * sizeof(*buf)))
 72		return;
 73	memcpy(lastwrite, buf, WIDTH * sizeof(*buf));
 74
 75#define SOH 1
 76#define STX 2
 77#define ETX 2
 78#define EOT 4
 79#define ENQ 5
 80	data[0] = STX;
 81	data[1] = '>';
 82	csum ^= '>';
 83	c = &data[2];
 84	for (i = 0; i < WIDTH; i++) {
 85		out = buf[i];
 86		if (out >= 0x100)
 87			out = '?';
 88		else if (out == 0x00)
 89			out = ' ';
 90		csum ^= out;
 91		if (out <= 0x05) {
 92			*c++ = SOH;
 93			out |= 0x40;
 94		}
 95		*c++ = out;
 96	}
 97
 98	if (csum <= 0x05) {
 99		*c++ = SOH;
100		csum |= 0x40;
101	}
102	*c++ = csum;
103	*c++ = ETX;
104
105	braille_co->write(braille_co, data, c - data);
106}
107
108/* Follow the VC cursor*/
109static void vc_follow_cursor(struct vc_data *vc)
110{
111	vc_x = vc->state.x - (vc->state.x % WIDTH);
112	vc_y = vc->state.y;
113	lastvc_x = vc->state.x;
114	lastvc_y = vc->state.y;
115}
116
117/* Maybe the VC cursor moved, if so follow it */
118static void vc_maybe_cursor_moved(struct vc_data *vc)
119{
120	if (vc->state.x != lastvc_x || vc->state.y != lastvc_y)
121		vc_follow_cursor(vc);
122}
123
124/* Show portion of VC at vc_x, vc_y */
125static void vc_refresh(struct vc_data *vc)
126{
127	u16 buf[WIDTH];
128	int i;
129
130	for (i = 0; i < WIDTH; i++) {
131		u16 glyph = screen_glyph(vc,
132				2 * (vc_x + i) + vc_y * vc->vc_size_row);
133		buf[i] = inverse_translate(vc, glyph, true);
134	}
135	braille_write(buf);
136}
137
138/*
139 * Link to keyboard
140 */
141
142static int keyboard_notifier_call(struct notifier_block *blk,
143				  unsigned long code, void *_param)
144{
145	struct keyboard_notifier_param *param = _param;
146	struct vc_data *vc = param->vc;
147	int ret = NOTIFY_OK;
148
149	if (!param->down)
150		return ret;
151
152	switch (code) {
153	case KBD_KEYCODE:
154		if (console_show) {
155			if (param->value == BRAILLE_KEY) {
156				console_show = 0;
157				beep(880);
158				vc_maybe_cursor_moved(vc);
159				vc_refresh(vc);
160				ret = NOTIFY_STOP;
161			}
162		} else {
163			ret = NOTIFY_STOP;
164			switch (param->value) {
165			case KEY_INSERT:
166				beep(440);
167				console_show = 1;
168				lastVC = -1;
169				braille_write(console_buf);
170				break;
171			case KEY_LEFT:
172				if (vc_x > 0) {
173					vc_x -= WIDTH;
174					if (vc_x < 0)
175						vc_x = 0;
176				} else if (vc_y >= 1) {
177					beep(880);
178					vc_y--;
179					vc_x = vc->vc_cols-WIDTH;
180				} else
181					beep(220);
182				break;
183			case KEY_RIGHT:
184				if (vc_x + WIDTH < vc->vc_cols) {
185					vc_x += WIDTH;
186				} else if (vc_y + 1 < vc->vc_rows) {
187					beep(880);
188					vc_y++;
189					vc_x = 0;
190				} else
191					beep(220);
192				break;
193			case KEY_DOWN:
194				if (vc_y + 1 < vc->vc_rows)
195					vc_y++;
196				else
197					beep(220);
198				break;
199			case KEY_UP:
200				if (vc_y >= 1)
201					vc_y--;
202				else
203					beep(220);
204				break;
205			case KEY_HOME:
206				vc_follow_cursor(vc);
207				break;
208			case KEY_PAGEUP:
209				vc_x = 0;
210				vc_y = 0;
211				break;
212			case KEY_PAGEDOWN:
213				vc_x = 0;
214				vc_y = vc->vc_rows-1;
215				break;
216			default:
217				ret = NOTIFY_OK;
218				break;
219			}
220			if (ret == NOTIFY_STOP)
221				vc_refresh(vc);
222		}
223		break;
224	case KBD_POST_KEYSYM:
225	{
226		unsigned char type = KTYP(param->value) - 0xf0;
227
228		if (type == KT_SPEC) {
229			unsigned char val = KVAL(param->value);
230			int on_off = -1;
231
232			switch (val) {
233			case KVAL(K_CAPS):
234				on_off = vt_get_leds(fg_console, VC_CAPSLOCK);
235				break;
236			case KVAL(K_NUM):
237				on_off = vt_get_leds(fg_console, VC_NUMLOCK);
238				break;
239			case KVAL(K_HOLD):
240				on_off = vt_get_leds(fg_console, VC_SCROLLOCK);
241				break;
242			}
243			if (on_off == 1)
244				beep(880);
245			else if (on_off == 0)
246				beep(440);
247		}
248	}
249		break;
250	case KBD_UNBOUND_KEYCODE:
251	case KBD_UNICODE:
252	case KBD_KEYSYM:
253		/* Unused */
254		break;
255	}
256	return ret;
257}
258
259static struct notifier_block keyboard_notifier_block = {
260	.notifier_call = keyboard_notifier_call,
261};
262
263static int vt_notifier_call(struct notifier_block *blk,
264			    unsigned long code, void *_param)
265{
266	struct vt_notifier_param *param = _param;
267	struct vc_data *vc = param->vc;
268
269	switch (code) {
270	case VT_ALLOCATE:
271		break;
272	case VT_DEALLOCATE:
273		break;
274	case VT_WRITE:
275	{
276		unsigned char c = param->c;
277
278		if (vc->vc_num != fg_console)
279			break;
280		switch (c) {
281		case '\b':
282		case 127:
283			if (console_cursor > 0) {
284				console_cursor--;
285				console_buf[console_cursor] = ' ';
286			}
287			break;
288		case '\n':
289		case '\v':
290		case '\f':
291		case '\r':
292			console_newline = 1;
293			break;
294		case '\t':
295			c = ' ';
296			fallthrough;
297		default:
298			if (c < 32)
299				/* Ignore other control sequences */
300				break;
301			if (console_newline) {
302				memset(console_buf, 0, sizeof(console_buf));
303				console_cursor = 0;
304				console_newline = 0;
305			}
306			if (console_cursor == WIDTH)
307				memmove(console_buf, &console_buf[1],
308					(WIDTH-1) * sizeof(*console_buf));
309			else
310				console_cursor++;
311			console_buf[console_cursor-1] = c;
312			break;
313		}
314		if (console_show)
315			braille_write(console_buf);
316		else {
317			vc_maybe_cursor_moved(vc);
318			vc_refresh(vc);
319		}
320		break;
321	}
322	case VT_UPDATE:
323		/* Maybe a VT switch, flush */
324		if (console_show) {
325			if (vc->vc_num != lastVC) {
326				lastVC = vc->vc_num;
327				memset(console_buf, 0, sizeof(console_buf));
328				console_cursor = 0;
329				braille_write(console_buf);
330			}
331		} else {
332			vc_maybe_cursor_moved(vc);
333			vc_refresh(vc);
334		}
335		break;
336	}
337	return NOTIFY_OK;
338}
339
340static struct notifier_block vt_notifier_block = {
341	.notifier_call = vt_notifier_call,
342};
343
344/*
345 * Called from printk.c when console=brl is given
346 */
347
348int braille_register_console(struct console *console, int index,
349		char *console_options, char *braille_options)
350{
351	int ret;
352
353	if (!console_options)
354		/* Only support VisioBraille for now */
355		console_options = "57600o8";
356	if (braille_co)
357		return -ENODEV;
358	if (console->setup) {
359		ret = console->setup(console, console_options);
360		if (ret != 0)
361			return ret;
362	}
363	console->flags |= CON_ENABLED;
364	console->index = index;
365	braille_co = console;
366	register_keyboard_notifier(&keyboard_notifier_block);
367	register_vt_notifier(&vt_notifier_block);
368	return 1;
369}
370
371int braille_unregister_console(struct console *console)
372{
373	if (braille_co != console)
374		return -EINVAL;
375	unregister_keyboard_notifier(&keyboard_notifier_block);
376	unregister_vt_notifier(&vt_notifier_block);
377	braille_co = NULL;
378	return 1;
379}
  1/*
  2 * Minimalistic braille device kernel support.
  3 *
  4 * By default, shows console messages on the braille device.
  5 * Pressing Insert switches to VC browsing.
  6 *
  7 *  Copyright (C) Samuel Thibault <samuel.thibault@ens-lyon.org>
  8 *
  9 * This program is free software ; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published by
 11 * the Free Software Foundation ; either version 2 of the License, or
 12 * (at your option) any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with the program ; if not, write to the Free Software
 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 22 */
 23
 24#include <linux/kernel.h>
 25#include <linux/module.h>
 26#include <linux/moduleparam.h>
 27#include <linux/console.h>
 28#include <linux/notifier.h>
 29
 30#include <linux/selection.h>
 31#include <linux/vt_kern.h>
 32#include <linux/consolemap.h>
 33
 34#include <linux/keyboard.h>
 35#include <linux/kbd_kern.h>
 36#include <linux/input.h>
 37
 38MODULE_AUTHOR("samuel.thibault@ens-lyon.org");
 39MODULE_DESCRIPTION("braille device");
 40MODULE_LICENSE("GPL");
 41
 42/*
 43 * Braille device support part.
 44 */
 45
 46/* Emit various sounds */
 47static bool sound;
 48module_param(sound, bool, 0);
 49MODULE_PARM_DESC(sound, "emit sounds");
 50
 51static void beep(unsigned int freq)
 52{
 53	if (sound)
 54		kd_mksound(freq, HZ/10);
 55}
 56
 57/* mini console */
 58#define WIDTH 40
 59#define BRAILLE_KEY KEY_INSERT
 60static u16 console_buf[WIDTH];
 61static int console_cursor;
 62
 63/* mini view of VC */
 64static int vc_x, vc_y, lastvc_x, lastvc_y;
 65
 66/* show console ? (or show VC) */
 67static int console_show = 1;
 68/* pending newline ? */
 69static int console_newline = 1;
 70static int lastVC = -1;
 71
 72static struct console *braille_co;
 73
 74/* Very VisioBraille-specific */
 75static void braille_write(u16 *buf)
 76{
 77	static u16 lastwrite[WIDTH];
 78	unsigned char data[1 + 1 + 2*WIDTH + 2 + 1], csum = 0, *c;
 79	u16 out;
 80	int i;
 81
 82	if (!braille_co)
 83		return;
 84
 85	if (!memcmp(lastwrite, buf, WIDTH * sizeof(*buf)))
 86		return;
 87	memcpy(lastwrite, buf, WIDTH * sizeof(*buf));
 88
 89#define SOH 1
 90#define STX 2
 91#define ETX 2
 92#define EOT 4
 93#define ENQ 5
 94	data[0] = STX;
 95	data[1] = '>';
 96	csum ^= '>';
 97	c = &data[2];
 98	for (i = 0; i < WIDTH; i++) {
 99		out = buf[i];
100		if (out >= 0x100)
101			out = '?';
102		else if (out == 0x00)
103			out = ' ';
104		csum ^= out;
105		if (out <= 0x05) {
106			*c++ = SOH;
107			out |= 0x40;
108		}
109		*c++ = out;
110	}
111
112	if (csum <= 0x05) {
113		*c++ = SOH;
114		csum |= 0x40;
115	}
116	*c++ = csum;
117	*c++ = ETX;
118
119	braille_co->write(braille_co, data, c - data);
120}
121
122/* Follow the VC cursor*/
123static void vc_follow_cursor(struct vc_data *vc)
124{
125	vc_x = vc->vc_x - (vc->vc_x % WIDTH);
126	vc_y = vc->vc_y;
127	lastvc_x = vc->vc_x;
128	lastvc_y = vc->vc_y;
129}
130
131/* Maybe the VC cursor moved, if so follow it */
132static void vc_maybe_cursor_moved(struct vc_data *vc)
133{
134	if (vc->vc_x != lastvc_x || vc->vc_y != lastvc_y)
135		vc_follow_cursor(vc);
136}
137
138/* Show portion of VC at vc_x, vc_y */
139static void vc_refresh(struct vc_data *vc)
140{
141	u16 buf[WIDTH];
142	int i;
143
144	for (i = 0; i < WIDTH; i++) {
145		u16 glyph = screen_glyph(vc,
146				2 * (vc_x + i) + vc_y * vc->vc_size_row);
147		buf[i] = inverse_translate(vc, glyph, 1);
148	}
149	braille_write(buf);
150}
151
152/*
153 * Link to keyboard
154 */
155
156static int keyboard_notifier_call(struct notifier_block *blk,
157				  unsigned long code, void *_param)
158{
159	struct keyboard_notifier_param *param = _param;
160	struct vc_data *vc = param->vc;
161	int ret = NOTIFY_OK;
162
163	if (!param->down)
164		return ret;
165
166	switch (code) {
167	case KBD_KEYCODE:
168		if (console_show) {
169			if (param->value == BRAILLE_KEY) {
170				console_show = 0;
171				beep(880);
172				vc_maybe_cursor_moved(vc);
173				vc_refresh(vc);
174				ret = NOTIFY_STOP;
175			}
176		} else {
177			ret = NOTIFY_STOP;
178			switch (param->value) {
179			case KEY_INSERT:
180				beep(440);
181				console_show = 1;
182				lastVC = -1;
183				braille_write(console_buf);
184				break;
185			case KEY_LEFT:
186				if (vc_x > 0) {
187					vc_x -= WIDTH;
188					if (vc_x < 0)
189						vc_x = 0;
190				} else if (vc_y >= 1) {
191					beep(880);
192					vc_y--;
193					vc_x = vc->vc_cols-WIDTH;
194				} else
195					beep(220);
196				break;
197			case KEY_RIGHT:
198				if (vc_x + WIDTH < vc->vc_cols) {
199					vc_x += WIDTH;
200				} else if (vc_y + 1 < vc->vc_rows) {
201					beep(880);
202					vc_y++;
203					vc_x = 0;
204				} else
205					beep(220);
206				break;
207			case KEY_DOWN:
208				if (vc_y + 1 < vc->vc_rows)
209					vc_y++;
210				else
211					beep(220);
212				break;
213			case KEY_UP:
214				if (vc_y >= 1)
215					vc_y--;
216				else
217					beep(220);
218				break;
219			case KEY_HOME:
220				vc_follow_cursor(vc);
221				break;
222			case KEY_PAGEUP:
223				vc_x = 0;
224				vc_y = 0;
225				break;
226			case KEY_PAGEDOWN:
227				vc_x = 0;
228				vc_y = vc->vc_rows-1;
229				break;
230			default:
231				ret = NOTIFY_OK;
232				break;
233			}
234			if (ret == NOTIFY_STOP)
235				vc_refresh(vc);
236		}
237		break;
238	case KBD_POST_KEYSYM:
239	{
240		unsigned char type = KTYP(param->value) - 0xf0;
241		if (type == KT_SPEC) {
242			unsigned char val = KVAL(param->value);
243			int on_off = -1;
244
245			switch (val) {
246			case KVAL(K_CAPS):
247				on_off = vt_get_leds(fg_console, VC_CAPSLOCK);
248				break;
249			case KVAL(K_NUM):
250				on_off = vt_get_leds(fg_console, VC_NUMLOCK);
251				break;
252			case KVAL(K_HOLD):
253				on_off = vt_get_leds(fg_console, VC_SCROLLOCK);
254				break;
255			}
256			if (on_off == 1)
257				beep(880);
258			else if (on_off == 0)
259				beep(440);
260		}
261	}
262	case KBD_UNBOUND_KEYCODE:
263	case KBD_UNICODE:
264	case KBD_KEYSYM:
265		/* Unused */
266		break;
267	}
268	return ret;
269}
270
271static struct notifier_block keyboard_notifier_block = {
272	.notifier_call = keyboard_notifier_call,
273};
274
275static int vt_notifier_call(struct notifier_block *blk,
276			    unsigned long code, void *_param)
277{
278	struct vt_notifier_param *param = _param;
279	struct vc_data *vc = param->vc;
280	switch (code) {
281	case VT_ALLOCATE:
282		break;
283	case VT_DEALLOCATE:
284		break;
285	case VT_WRITE:
286	{
287		unsigned char c = param->c;
288		if (vc->vc_num != fg_console)
289			break;
290		switch (c) {
291		case '\b':
292		case 127:
293			if (console_cursor > 0) {
294				console_cursor--;
295				console_buf[console_cursor] = ' ';
296			}
297			break;
298		case '\n':
299		case '\v':
300		case '\f':
301		case '\r':
302			console_newline = 1;
303			break;
304		case '\t':
305			c = ' ';
306			/* Fallthrough */
307		default:
308			if (c < 32)
309				/* Ignore other control sequences */
310				break;
311			if (console_newline) {
312				memset(console_buf, 0, sizeof(console_buf));
313				console_cursor = 0;
314				console_newline = 0;
315			}
316			if (console_cursor == WIDTH)
317				memmove(console_buf, &console_buf[1],
318					(WIDTH-1) * sizeof(*console_buf));
319			else
320				console_cursor++;
321			console_buf[console_cursor-1] = c;
322			break;
323		}
324		if (console_show)
325			braille_write(console_buf);
326		else {
327			vc_maybe_cursor_moved(vc);
328			vc_refresh(vc);
329		}
330		break;
331	}
332	case VT_UPDATE:
333		/* Maybe a VT switch, flush */
334		if (console_show) {
335			if (vc->vc_num != lastVC) {
336				lastVC = vc->vc_num;
337				memset(console_buf, 0, sizeof(console_buf));
338				console_cursor = 0;
339				braille_write(console_buf);
340			}
341		} else {
342			vc_maybe_cursor_moved(vc);
343			vc_refresh(vc);
344		}
345		break;
346	}
347	return NOTIFY_OK;
348}
349
350static struct notifier_block vt_notifier_block = {
351	.notifier_call = vt_notifier_call,
352};
353
354/*
355 * Called from printk.c when console=brl is given
356 */
357
358int braille_register_console(struct console *console, int index,
359		char *console_options, char *braille_options)
360{
361	int ret;
362
363	if (!(console->flags & CON_BRL))
364		return 0;
365	if (!console_options)
366		/* Only support VisioBraille for now */
367		console_options = "57600o8";
368	if (braille_co)
369		return -ENODEV;
370	if (console->setup) {
371		ret = console->setup(console, console_options);
372		if (ret != 0)
373			return ret;
374	}
375	console->flags |= CON_ENABLED;
376	console->index = index;
377	braille_co = console;
378	register_keyboard_notifier(&keyboard_notifier_block);
379	register_vt_notifier(&vt_notifier_block);
380	return 1;
381}
382
383int braille_unregister_console(struct console *console)
384{
385	if (braille_co != console)
386		return -EINVAL;
387	if (!(console->flags & CON_BRL))
388		return 0;
389	unregister_keyboard_notifier(&keyboard_notifier_block);
390	unregister_vt_notifier(&vt_notifier_block);
391	braille_co = NULL;
392	return 1;
393}