From e3c028a0d1df371cdf26d924540e60f09dcf70d0 Mon Sep 17 00:00:00 2001 From: Bastian Krause Date: Wed, 4 Mar 2020 17:25:41 +0100 Subject: [PATCH 1/2] beep: check for supported events before sending SND_TONE The device might not support SND_TONE events, so check it and print an error if this event type is not supported. Signed-off-by: Bastian Krause --- beep.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/beep.c b/beep.c index 7da2e70..0162c52 100644 --- a/beep.c +++ b/beep.c @@ -106,17 +106,26 @@ void do_beep(int freq) { perror("ioctl"); } } else { - /* BEEP_TYPE_EVDEV */ - struct input_event e; + /* BEEP_TYPE_EVDEV */ + struct input_event e; + unsigned long evbit = 0; - e.type = EV_SND; - e.code = SND_TONE; - e.value = freq; + e.type = EV_SND; - if(write(console_fd, &e, sizeof(struct input_event)) < 0) { - putchar('\a'); /* See above */ - perror("write"); - } + /* check supported events and act accordingly */ + ioctl(console_fd, EVIOCGBIT(EV_SND, sizeof(evbit)), &evbit); + if(evbit & (1 << SND_TONE)) { + e.code = SND_TONE; + e.value = freq; + } else { + perror("no supported event type"); + return; + } + + if(write(console_fd, &e, sizeof(struct input_event)) < 0) { + putchar('\a'); /* See above */ + perror("write"); + } } } From a75d99100ad5e05da88a311a02c128dfca916674 Mon Sep 17 00:00:00 2001 From: Bastian Krause Date: Wed, 4 Mar 2020 17:39:03 +0100 Subject: [PATCH 2/2] beep: support SND_BELL as fallback The gpio-beeper driver does not support SND_TONE, but SND_BELL. So send that instead if SND_TONE is not supported. Signed-off-by: Bastian Krause --- beep.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/beep.c b/beep.c index 0162c52..9c9ddee 100644 --- a/beep.c +++ b/beep.c @@ -117,6 +117,9 @@ void do_beep(int freq) { if(evbit & (1 << SND_TONE)) { e.code = SND_TONE; e.value = freq; + } else if(evbit & (1 << SND_BELL)) { + e.code = SND_BELL; + e.value = (freq != 0); } else { perror("no supported event type"); return;