-
Notifications
You must be signed in to change notification settings - Fork 1
/
pcspkr.h
76 lines (65 loc) · 1.62 KB
/
pcspkr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
pcspkr - listen to pcm sound over the internal pc speaker
Written in 2014 by <Ahmet Inan> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <signal.h>
#include <sys/io.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
void disable_speaker_and_counter()
{
outb(inb(0x61) & 12, 0x61);
}
void enable_speaker_and_counter()
{
outb((inb(0x61) & 12) | 3, 0x61);
}
void reset_counter(unsigned char c)
{
outb(c, 0x42);
}
void move_speaker(int v)
{
static int init;
static unsigned char settings;
if (!init) {
init = 1;
settings = (inb(0x61) & 12) | 1;
}
outb(settings | (v << 1), 0x61);
}
void prepare_hardware()
{
disable_speaker_and_counter();
outb(160, 0x43); // 16bit counter, mode 0, MSB only, timer 2
reset_counter(0);
outb(144, 0x43); // 16bit counter, mode 0, LSB only, timer 2
reset_counter(0);
}
void cleanup(int signum)
{
(void)signum;
disable_speaker_and_counter();
exit(1);
}
void install_signal_handlers()
{
signal(SIGTERM, cleanup);
signal(SIGINT, cleanup);
}
void set_io_permissions()
{
ioperm(0x42, 2, 1);
ioperm(0x61, 1, 1);
}
void set_realtime_scheduling()
{
const int policy = SCHED_FIFO;
struct sched_param param;
sched_getparam(0, ¶m);
param.sched_priority = sched_get_priority_max(policy);
sched_setscheduler(0, policy, ¶m);
}