Skip to contentSkip to conversation

drivers/input/misc: new PC speaker sounds, clicks and notes

PATCH In review Karl Dahlke 2 participants 2 messages Dec 9, 2013 – Dec 16, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/input/misc: new PC speaker sounds, clicks and notes

From: Karl Dahlke <[email protected]>

The speaker driver can play a tone at a specified frequency,
or the standard control G bell,
which is a special case of TONE at 1000 hz 0.1 seconds.
This patch adds kd_mkpulse() to generate a soft click.
This is introduced to support accessibility modules and adapters in the future. With this in place, a module can easily provide soft clicks,
i.e. audible feedback, whenever a key is depressed,
or when that keystroke is echoed on screen, whichever you prefer. (Many people find the latter more valuable.)
This allows a blind user, for example, to have ongoing feedback while typing, even if he is, at the same time, listening to text that is already on screen. This is faster and more convenient than having characters echoed verbally. And it works all the time, even if speech or braille is not working for whatever reason. And there are many reasons those adapters could fail. Misconfigured sound card, bad serial connection,
software synth not loading, etc.
Thus these forms of audio feedback are very important.

This should not be confused with the click function in certain specialized keyboards such as lkkbd.c.
That function does a serio write to the client to activate clicks locally. In contrast, this function generates a one-time pulse at the pc speaker, or through a similar driver, so that modules can click whenever they want, e.g. when a key is echoed back to you by a running application, perhaps a thousand miles away over ssh, so you know all is well. A module may want to click, or issue a series of clicks, for many reasons.

Another function introduced by this patch is kd_mknotes,
which plays a series of tones in the background.
You could do this yourself with kd_mksound and timers,
but why should everyone reinvent the wheel?
It is better to write the function once, properly, in the kernel, and let modules use it thereafter.
Again, this is a means to an end.
Accessibility modules can generate rapid sequences of notes
to indicate various conditions, even error conditions, especially if speech or braille is not working.
These notes may be the only feedback the user has to diagnose the problem. Again, extremely valuable in a difficult situation.
This may be useful to other developers in other situations as well.

A third function kd_mksteps builds a chromatic scale,
from one frequency to another in steps.
This is a convenience on top of kd_mknotes.
The half-tone scale, with a step of 6%, starting at middle C, is approximately kd_mksteps(260, 530, 6, 150);
Use a negative step for a descending scale.

---

A few personal comments if I may.
I have tried for 15 years to get some of my work into the kernel, without success.
That is partly understandable, because my early work was quite intrusive, especially in 2.2 and 2.4,
and it was specifically one adapter, my adapter, and not a general approach. So you were wise to say no.
But two years ago I redesigned everything,
so that almost all of my adapter runs in user space, and connects to the kernel through a device driver.
The driver is not specific to my adapter, it supports any text based adapter in any language (unicode).
It is a general approach, a much better design.
I asked, two years ago, if someone would help me usher these changes into the 2.6 kernel, since Linux hacking is tremendously difficult, even if you can see!
My ideas were well received, but the overall response was,
"This isn't a high priority for us."
That is a direct quote.
Needless to say this made me sad.
But not unexpected; accessibility has never been a priority
for Linux, or Microsoft, and Aple only gets half a nod for its VoiceOver. There really are a lot of things vying for our atention, aren't there?

Well it's been two years and I thought I'd try again,
in the spirit of SubmittingPatches section 10, Don't Get Discouraged. If this or subsequent patches cannot be applied as-is,
please work with me on this project,
don't just tell me to read chapter 17 of some 8,000 page manual. I might really need some help from someone on the inside to make this fly. It would be much appreciated.

Here is the first patch, enhancing sounds as described in the commit log above. After this would come a supporting module, and then a device driver, and that's it.
Well possibly one more patch for a notifier sleep function,
but I would have to discuss that one with you-all.
That one is a bit more complicated.
So this is the first step.

The code passes the style checker,
and has been exercised, in 3.12.3, by myself and others.

drivers/input/misc/pcspkr.c +55 -15
diff -uprN a/drivers/input/misc/pcspkr.c b/drivers/input/misc/pcspkr.c
@@ -19,6 +19,8 @@
 #include <linux/input.h>
 #include <linux/platform_device.h>
 #include <linux/timex.h>
+#include <linux/delay.h>
+
 #include <asm/io.h>
 
 MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
@@ -26,25 +28,26 @@ MODULE_DESCRIPTION("PC Speaker beeper dr
 MODULE_LICENSE("GPL");
 MODULE_ALIAS("platform:pcspkr");
 
-static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
+/* Toggle the speaker, but not if a tone is sounding */
+static void speaker_toggle(void)
 {
-	unsigned int count = 0;
+	char c;
 	unsigned long flags;
 
-	if (type != EV_SND)
-		return -1;
-
-	switch (code) {
-		case SND_BELL: if (value) value = 1000;
-		case SND_TONE: break;
-		default: return -1;
+	raw_spin_lock_irqsave(&i8253_lock, flags);
+	c = inb_p(0x61);
+	if ((c&3) != 3) {
+		c &= 0xfe;
+		c ^= 2; /* toggle */
+		outb(c, 0x61);
 	}
+	raw_spin_unlock_irqrestore(&i8253_lock, flags);
+}
 
-	if (value > 20 && value < 32767)
-		count = PIT_TICK_RATE / value;
-
+static void speaker_sing(unsigned int count)
+{
+	unsigned long flags;
 	raw_spin_lock_irqsave(&i8253_lock, flags);
-
 	if (count) {
 		/* set command for counter 2, 2 byte write */
 		outb_p(0xB6, 0x43);
@@ -57,8 +60,44 @@ static int pcspkr_event(struct input_dev
 		/* disable counter 2 */
 		outb(inb_p(0x61) & 0xFC, 0x61);
 	}
-
 	raw_spin_unlock_irqrestore(&i8253_lock, flags);
+}
+
+static int pcspkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
+{
+	unsigned int count;
+
+	if (type != EV_SND)
+		return -1;
+
+	switch (code) {
+	case SND_BELL:
+		if (value)
+			value = 1000;
+		/* fall through */
+
+	case SND_TONE:
+		count = 0;
+		if (value > 20 && value < 32767)
+			count = PIT_TICK_RATE / value;
+		speaker_sing(count);
+		break;
+
+	case SND_PULSE:
+		/* Don't hold the cpu for more than 1ms */
+		/* no reason for a pulse to ever be longer than that. */
+		if (value > 1000)
+			value = 1000;
+		/* if a pulse is too short it can't be heard anyways. */
+		if (value < 20)
+			break;
+		speaker_toggle();
+		udelay(value);
+		speaker_toggle();
+		break;
+
+	default: return -1;
+	}
 
 	return 0;
 }
@@ -81,7 +120,8 @@ static int pcspkr_probe(struct platform_
 	pcspkr_dev->dev.parent = &dev->dev;
 
 	pcspkr_dev->evbit[0] = BIT_MASK(EV_SND);
-	pcspkr_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
+	pcspkr_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE) |
+		BIT_MASK(SND_PULSE);
 	pcspkr_dev->event = pcspkr_event;
 
 	err = input_register_device(pcspkr_dev);
drivers/tty/vt/keyboard.c +207 -0

213 lines not on this page · raw message

include/linux/vt_kern.h +3 -0
diff -uprN a/include/linux/vt_kern.h b/include/linux/vt_kern.h
@@ -28,6 +28,9 @@
 #endif
 
 extern void kd_mksound(unsigned int hz, unsigned int ticks);
+extern void kd_mkpulse(unsigned int width);
+extern void kd_mknotes(const short *p);
+extern void kd_mksteps(int freq1, int freq2, int step, int duration);
 extern int kbd_rate(struct kbd_repeat *rep);
 extern int fg_console, last_console, want_console;
 
include/uapi/linux/input.h +1 -0
diff -uprN a/include/uapi/linux/input.h b/include/uapi/linux/input.h
@@ -906,6 +906,7 @@ struct input_keymap_entry {
 #define SND_CLICK		0x00
 #define SND_BELL		0x01
 #define SND_TONE		0x02
+#define SND_PULSE		0x03
 #define SND_MAX			0x07
 #define SND_CNT			(SND_MAX+1)
 
maintainer
#1 lore raw

Hi Karl,

Quoted 20 lines · Karl Dahlke

From: Karl Dahlke <[email protected]>

The speaker driver can play a tone at a specified frequency,
or the standard control G bell,
which is a special case of TONE at 1000 hz 0.1 seconds.
This patch adds kd_mkpulse() to generate a soft click.
This is introduced to support accessibility modules and adapters in the future. With this in place, a module can easily provide soft clicks,
i.e. audible feedback, whenever a key is depressed,
or when that keystroke is echoed on screen, whichever you prefer. (Many people find the latter more valuable.)
This allows a blind user, for example, to have ongoing feedback while typing, even if he is, at the same time, listening to text that is already on screen. This is faster and more convenient than having characters echoed verbally. And it works all the time, even if speech or braille is not working for whatever reason. And there are many reasons those adapters could fail. Misconfigured sound card, bad serial connection,
software synth not loading, etc.
Thus these forms of audio feedback are very important.

Quoted 8 lines

This should not be confused with the click function in certain specialized keyboards such as lkkbd.c.
That function does a serio write to the client to activate clicks locally. In contrast, this function generates a one-time pulse at the pc speaker, or through a similar driver, so that modules can click whenever they want, e.g. when a key is echoed back to you by a running application, perhaps a thousand miles away over ssh, so you know all is well. A module may want to click, or issue a series of clicks, for many reasons.

I do not think it is a good idea to add SND_PULSE as it can be easily implemented by SND_TONE with the additional benefit that parameters of the click can be adjusted. Also, if clicking is done elsewhere, it would work with other speaker drivers besides pcspkr.

Quoted 7 lines

Another function introduced by this patch is kd_mknotes, which plays a series of tones in the background.
You could do this yourself with kd_mksound and timers, but why should everyone reinvent the wheel?
It is better to write the function once, properly, in the kernel, and let modules use it thereafter.

Can it be put into a library instead? Especially given David's work on trying to push the VT code out of the kernel. Also, what if you want clicks to go through sound card and not the speaker interface?

Thanks.

Signature · 1 line
Dmitry