Linux Audio

Check our new training course

Loading...
v5.9
  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");
 27MODULE_LICENSE("GPL");
 28
 29/*
 30 * Braille device support part.
 31 */
 32
 33/* Emit various sounds */
 34static bool sound;
 35module_param(sound, bool, 0);
 36MODULE_PARM_DESC(sound, "emit sounds");
 37
 38static void beep(unsigned int freq)
 39{
 40	if (sound)
 41		kd_mksound(freq, HZ/10);
 42}
 43
 44/* mini console */
 45#define WIDTH 40
 46#define BRAILLE_KEY KEY_INSERT
 47static u16 console_buf[WIDTH];
 48static int console_cursor;
 49
 50/* mini view of VC */
 51static int vc_x, vc_y, lastvc_x, lastvc_y;
 52
 53/* show console ? (or show VC) */
 54static int console_show = 1;
 55/* pending newline ? */
 56static int console_newline = 1;
 57static int lastVC = -1;
 58
 59static struct console *braille_co;
 60
 61/* Very VisioBraille-specific */
 62static void braille_write(u16 *buf)
 63{
 64	static u16 lastwrite[WIDTH];
 65	unsigned char data[1 + 1 + 2*WIDTH + 2 + 1], csum = 0, *c;
 66	u16 out;
 67	int i;
 68
 69	if (!braille_co)
 70		return;
 71
 72	if (!memcmp(lastwrite, buf, WIDTH * sizeof(*buf)))
 73		return;
 74	memcpy(lastwrite, buf, WIDTH * sizeof(*buf));
 75
 76#define SOH 1
 77#define STX 2
 78#define ETX 2
 79#define EOT 4
 80#define ENQ 5
 81	data[0] = STX;
 82	data[1] = '>';
 83	csum ^= '>';
 84	c = &data[2];
 85	for (i = 0; i < WIDTH; i++) {
 86		out = buf[i];
 87		if (out >= 0x100)
 88			out = '?';
 89		else if (out == 0x00)
 90			out = ' ';
 91		csum ^= out;
 92		if (out <= 0x05) {
 93			*c++ = SOH;
 94			out |= 0x40;
 95		}
 96		*c++ = out;
 97	}
 98
 99	if (csum <= 0x05) {
100		*c++ = SOH;
101		csum |= 0x40;
102	}
103	*c++ = csum;
104	*c++ = ETX;
105
106	braille_co->write(braille_co, data, c - data);
107}
108
109/* Follow the VC cursor*/
110static void vc_follow_cursor(struct vc_data *vc)
111{
112	vc_x = vc->state.x - (vc->state.x % WIDTH);
113	vc_y = vc->state.y;
114	lastvc_x = vc->state.x;
115	lastvc_y = vc->state.y;
116}
117
118/* Maybe the VC cursor moved, if so follow it */
119static void vc_maybe_cursor_moved(struct vc_data *vc)
120{
121	if (vc->state.x != lastvc_x || vc->state.y != lastvc_y)
122		vc_follow_cursor(vc);
123}
124
125/* Show portion of VC at vc_x, vc_y */
126static void vc_refresh(struct vc_data *vc)
127{
128	u16 buf[WIDTH];
129	int i;
130
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);
135	}
136	braille_write(buf);
137}
138
139/*
140 * Link to keyboard
141 */
142
143static int keyboard_notifier_call(struct notifier_block *blk,
144				  unsigned long code, void *_param)
145{
146	struct keyboard_notifier_param *param = _param;
147	struct vc_data *vc = param->vc;
148	int ret = NOTIFY_OK;
149
150	if (!param->down)
151		return ret;
152
153	switch (code) {
154	case KBD_KEYCODE:
155		if (console_show) {
156			if (param->value == BRAILLE_KEY) {
157				console_show = 0;
158				beep(880);
159				vc_maybe_cursor_moved(vc);
160				vc_refresh(vc);
161				ret = NOTIFY_STOP;
162			}
163		} else {
164			ret = NOTIFY_STOP;
165			switch (param->value) {
166			case KEY_INSERT:
167				beep(440);
168				console_show = 1;
169				lastVC = -1;
170				braille_write(console_buf);
171				break;
172			case KEY_LEFT:
173				if (vc_x > 0) {
174					vc_x -= WIDTH;
175					if (vc_x < 0)
176						vc_x = 0;
177				} else if (vc_y >= 1) {
178					beep(880);
179					vc_y--;
180					vc_x = vc->vc_cols-WIDTH;
181				} else
182					beep(220);
183				break;
184			case KEY_RIGHT:
185				if (vc_x + WIDTH < vc->vc_cols) {
186					vc_x += WIDTH;
187				} else if (vc_y + 1 < vc->vc_rows) {
188					beep(880);
189					vc_y++;
190					vc_x = 0;
191				} else
192					beep(220);
193				break;
194			case KEY_DOWN:
195				if (vc_y + 1 < vc->vc_rows)
196					vc_y++;
197				else
198					beep(220);
199				break;
200			case KEY_UP:
201				if (vc_y >= 1)
202					vc_y--;
203				else
204					beep(220);
205				break;
206			case KEY_HOME:
207				vc_follow_cursor(vc);
208				break;
209			case KEY_PAGEUP:
210				vc_x = 0;
211				vc_y = 0;
212				break;
213			case KEY_PAGEDOWN:
214				vc_x = 0;
215				vc_y = vc->vc_rows-1;
216				break;
217			default:
218				ret = NOTIFY_OK;
219				break;
220			}
221			if (ret == NOTIFY_STOP)
222				vc_refresh(vc);
223		}
224		break;
225	case KBD_POST_KEYSYM:
226	{
227		unsigned char type = KTYP(param->value) - 0xf0;
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	case KBD_UNBOUND_KEYCODE:
250	case KBD_UNICODE:
251	case KBD_KEYSYM:
252		/* Unused */
253		break;
254	}
255	return ret;
256}
257
258static struct notifier_block keyboard_notifier_block = {
259	.notifier_call = keyboard_notifier_call,
260};
261
262static int vt_notifier_call(struct notifier_block *blk,
263			    unsigned long code, void *_param)
264{
265	struct vt_notifier_param *param = _param;
266	struct vc_data *vc = param->vc;
267	switch (code) {
268	case VT_ALLOCATE:
269		break;
270	case VT_DEALLOCATE:
271		break;
272	case VT_WRITE:
273	{
274		unsigned char c = param->c;
275		if (vc->vc_num != fg_console)
276			break;
277		switch (c) {
278		case '\b':
279		case 127:
280			if (console_cursor > 0) {
281				console_cursor--;
282				console_buf[console_cursor] = ' ';
283			}
284			break;
285		case '\n':
286		case '\v':
287		case '\f':
288		case '\r':
289			console_newline = 1;
290			break;
291		case '\t':
292			c = ' ';
293			fallthrough;
294		default:
295			if (c < 32)
296				/* Ignore other control sequences */
297				break;
298			if (console_newline) {
299				memset(console_buf, 0, sizeof(console_buf));
300				console_cursor = 0;
301				console_newline = 0;
302			}
303			if (console_cursor == WIDTH)
304				memmove(console_buf, &console_buf[1],
305					(WIDTH-1) * sizeof(*console_buf));
306			else
307				console_cursor++;
308			console_buf[console_cursor-1] = c;
309			break;
310		}
311		if (console_show)
312			braille_write(console_buf);
313		else {
314			vc_maybe_cursor_moved(vc);
315			vc_refresh(vc);
316		}
317		break;
318	}
319	case VT_UPDATE:
320		/* Maybe a VT switch, flush */
321		if (console_show) {
322			if (vc->vc_num != lastVC) {
323				lastVC = vc->vc_num;
324				memset(console_buf, 0, sizeof(console_buf));
325				console_cursor = 0;
326				braille_write(console_buf);
327			}
328		} else {
329			vc_maybe_cursor_moved(vc);
330			vc_refresh(vc);
331		}
332		break;
333	}
334	return NOTIFY_OK;
335}
336
337static struct notifier_block vt_notifier_block = {
338	.notifier_call = vt_notifier_call,
339};
340
341/*
342 * Called from printk.c when console=brl is given
343 */
344
345int braille_register_console(struct console *console, int index,
346		char *console_options, char *braille_options)
347{
348	int ret;
349
350	if (!console_options)
351		/* Only support VisioBraille for now */
352		console_options = "57600o8";
353	if (braille_co)
354		return -ENODEV;
355	if (console->setup) {
356		ret = console->setup(console, console_options);
357		if (ret != 0)
358			return ret;
359	}
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);
365	return 1;
366}
367
368int braille_unregister_console(struct console *console)
369{
370	if (braille_co != console)
371		return -EINVAL;
372	unregister_keyboard_notifier(&keyboard_notifier_block);
373	unregister_vt_notifier(&vt_notifier_block);
374	braille_co = NULL;
375	return 1;
376}
v3.1
 
  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 int 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 = vc_kbd_led(kbd_table + fg_console,
248						VC_CAPSLOCK);
249				break;
250			case KVAL(K_NUM):
251				on_off = vc_kbd_led(kbd_table + fg_console,
252						VC_NUMLOCK);
253				break;
254			case KVAL(K_HOLD):
255				on_off = vc_kbd_led(kbd_table + fg_console,
256						VC_SCROLLOCK);
257				break;
258			}
259			if (on_off == 1)
260				beep(880);
261			else if (on_off == 0)
262				beep(440);
263		}
264	}
265	case KBD_UNBOUND_KEYCODE:
266	case KBD_UNICODE:
267	case KBD_KEYSYM:
268		/* Unused */
269		break;
270	}
271	return ret;
272}
273
274static struct notifier_block keyboard_notifier_block = {
275	.notifier_call = keyboard_notifier_call,
276};
277
278static int vt_notifier_call(struct notifier_block *blk,
279			    unsigned long code, void *_param)
280{
281	struct vt_notifier_param *param = _param;
282	struct vc_data *vc = param->vc;
283	switch (code) {
284	case VT_ALLOCATE:
285		break;
286	case VT_DEALLOCATE:
287		break;
288	case VT_WRITE:
289	{
290		unsigned char c = param->c;
291		if (vc->vc_num != fg_console)
292			break;
293		switch (c) {
294		case '\b':
295		case 127:
296			if (console_cursor > 0) {
297				console_cursor--;
298				console_buf[console_cursor] = ' ';
299			}
300			break;
301		case '\n':
302		case '\v':
303		case '\f':
304		case '\r':
305			console_newline = 1;
306			break;
307		case '\t':
308			c = ' ';
309			/* Fallthrough */
310		default:
311			if (c < 32)
312				/* Ignore other control sequences */
313				break;
314			if (console_newline) {
315				memset(console_buf, 0, sizeof(console_buf));
316				console_cursor = 0;
317				console_newline = 0;
318			}
319			if (console_cursor == WIDTH)
320				memmove(console_buf, &console_buf[1],
321					(WIDTH-1) * sizeof(*console_buf));
322			else
323				console_cursor++;
324			console_buf[console_cursor-1] = c;
325			break;
326		}
327		if (console_show)
328			braille_write(console_buf);
329		else {
330			vc_maybe_cursor_moved(vc);
331			vc_refresh(vc);
332		}
333		break;
334	}
335	case VT_UPDATE:
336		/* Maybe a VT switch, flush */
337		if (console_show) {
338			if (vc->vc_num != lastVC) {
339				lastVC = vc->vc_num;
340				memset(console_buf, 0, sizeof(console_buf));
341				console_cursor = 0;
342				braille_write(console_buf);
343			}
344		} else {
345			vc_maybe_cursor_moved(vc);
346			vc_refresh(vc);
347		}
348		break;
349	}
350	return NOTIFY_OK;
351}
352
353static struct notifier_block vt_notifier_block = {
354	.notifier_call = vt_notifier_call,
355};
356
357/*
358 * Called from printk.c when console=brl is given
359 */
360
361int braille_register_console(struct console *console, int index,
362		char *console_options, char *braille_options)
363{
364	int ret;
 
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 0;
381}
382
383int braille_unregister_console(struct console *console)
384{
385	if (braille_co != console)
386		return -EINVAL;
387	unregister_keyboard_notifier(&keyboard_notifier_block);
388	unregister_vt_notifier(&vt_notifier_block);
389	braille_co = NULL;
390	return 0;
391}