2 * spkr.c -- device driver for console speaker
4 * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993
5 * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su>
6 * modified for PC98 by Kakefuda
10 __FBSDID("$FreeBSD$");
12 #include <sys/param.h>
13 #include <sys/systm.h>
14 #include <sys/kernel.h>
15 #include <sys/module.h>
18 #include <sys/ctype.h>
19 #include <sys/malloc.h>
20 #include <machine/clock.h>
21 #include <dev/speaker/speaker.h>
23 static d_open_t spkropen;
24 static d_close_t spkrclose;
25 static d_write_t spkrwrite;
26 static d_ioctl_t spkrioctl;
28 static struct cdevsw spkr_cdevsw = {
29 .d_version = D_VERSION,
30 .d_flags = D_NEEDGIANT,
38 static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
41 **************** MACHINE DEPENDENT PART STARTS HERE *************************
42 * This section defines a function tone() which causes a tone of given
43 * frequency and duration from the ISA console speaker.
44 * Another function endtone() is defined to force sound off, and there is
45 * also a rest() entry point to do pauses.
47 * Audible sound is generated using the Programmable Interval Timer (PIT) and
48 * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
49 * PPI controls whether sound is passed through at all; the PIT's channel 2 is
50 * used to generate clicks (a square wave) of whatever frequency is desired.
54 static char endtone, endrest;
56 static void tone(unsigned int thz, unsigned int centisecs);
57 static void rest(int centisecs);
58 static void playinit(void);
59 static void playtone(int pitch, int value, int sustain);
60 static void playstring(char *cp, size_t slen);
63 * Emit tone of frequency thz for given number of centisecs
66 tone(unsigned int thz, unsigned int centisecs)
74 (void) printf("tone: thz=%d centisecs=%d\n", thz, centisecs);
77 /* set timer to generate clicks at given frequency in Hertz */
78 if (timer_spkr_acquire()) {
79 /* enter list of waiting procs ??? */
83 timer_spkr_setfreq(thz);
87 * Set timeout to endtone function, then give up the timeslice.
88 * This is so other processes can execute while the tone is being
91 timo = centisecs * hz / 100;
93 tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", timo);
98 * Rest for given number of centisecs
106 * Set timeout to endrest function, then give up the timeslice.
107 * This is so other processes can execute while the rest is being
111 (void) printf("rest: %d\n", centisecs);
113 timo = centisecs * hz / 100;
115 tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", timo);
119 **************** PLAY STRING INTERPRETER BEGINS HERE **********************
120 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
121 * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
122 * tracking facility are added.
123 * Requires tone(), rest(), and endtone(). String play is not interruptible
124 * except possibly at physical block boundaries.
127 #ifndef __bool_true_false_are_defined
133 #define dtoi(c) ((c) - '0')
135 static int octave; /* currently selected octave */
136 static int whole; /* whole-note time at current tempo, in ticks */
137 static int value; /* whole divisor for note time, quarter note = 1 */
138 static int fill; /* controls spacing of notes */
139 static bool octtrack; /* octave-tracking on? */
140 static bool octprefix; /* override current octave-tracking state? */
143 * Magic number avoidance...
145 #define SECS_PER_MIN 60 /* seconds per minute */
146 #define WHOLE_NOTE 4 /* quarter notes per whole note */
147 #define MIN_VALUE 64 /* the most we can divide a note by */
148 #define DFLT_VALUE 4 /* default value (quarter-note) */
149 #define FILLTIME 8 /* for articulation, break note in parts */
150 #define STACCATO 6 /* 6/8 = 3/4 of note is filled */
151 #define NORMAL 7 /* 7/8ths of note interval is filled */
152 #define LEGATO 8 /* all of note interval is filled */
153 #define DFLT_OCTAVE 4 /* default octave */
154 #define MIN_TEMPO 32 /* minimum tempo */
155 #define DFLT_TEMPO 120 /* default tempo */
156 #define MAX_TEMPO 255 /* max tempo */
157 #define NUM_MULT 3 /* numerator of dot multiplier */
158 #define DENOM_MULT 2 /* denominator of dot multiplier */
160 /* letter to half-tone: A B C D E F G */
161 static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
164 * This is the American Standard A440 Equal-Tempered scale with frequencies
165 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
166 * our octave 0 is standard octave 2.
168 #define OCTAVE_NOTES 12 /* semitones per octave */
169 static int pitchtab[] =
171 /* C C# D D# E F F# G G# A A# B*/
172 /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
173 /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
174 /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
175 /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
176 /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
177 /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
178 /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
184 octave = DFLT_OCTAVE;
185 whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
189 octprefix = TRUE; /* act as though there was an initial O(n) */
193 * Play tone of proper duration for current rhythm signature
196 playtone(int pitch, int value, int sustain)
198 int sound, silence, snum = 1, sdenom = 1;
200 /* this weirdness avoids floating-point arithmetic */
201 for (; sustain; sustain--) {
202 /* See the BUGS section in the man page for discussion */
204 sdenom *= DENOM_MULT;
207 if (value == 0 || sdenom == 0)
211 rest(whole * snum / (value * sdenom));
213 sound = (whole * snum) / (value * sdenom)
214 - (whole * (FILLTIME - fill)) / (value * FILLTIME);
215 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
218 (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
219 pitch, sound, silence);
222 tone(pitchtab[pitch], sound);
229 * Interpret and play an item from a notation string
232 playstring(char *cp, size_t slen)
234 int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
236 #define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
237 {v = v * 10 + (*++cp - '0'); slen--;}
238 for (; slen--; cp++) {
239 int sustain, timeval, tempo;
240 char c = toupper(*cp);
243 (void) printf("playstring: %c (%x)\n", c, c);
255 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
257 /* this may be followed by an accidental sign */
258 if (cp[1] == '#' || cp[1] == '+') {
262 } else if (cp[1] == '-') {
269 * If octave-tracking mode is on, and there has been no octave-
270 * setting prefix, find the version of the current letter note
271 * closest to the last regardless of octave.
273 if (octtrack && !octprefix) {
274 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES -
277 pitch += OCTAVE_NOTES;
280 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES) -
283 pitch -= OCTAVE_NOTES;
289 /* ...which may in turn be followed by an override time value */
291 if (timeval <= 0 || timeval > MIN_VALUE)
294 /* ...and/or sustain dots */
295 for (sustain = 0; cp[1] == '.'; cp++) {
300 /* ...and/or a slur mark */
308 /* time to emit the actual tone */
309 playtone(pitch, timeval, sustain);
314 if (cp[1] == 'N' || cp[1] == 'n') {
315 octprefix = octtrack = FALSE;
318 } else if (cp[1] == 'L' || cp[1] == 'l') {
324 if (octave >= nitems(pitchtab) / OCTAVE_NOTES)
325 octave = DFLT_OCTAVE;
330 if (octave < nitems(pitchtab) / OCTAVE_NOTES - 1)
341 for (sustain = 0; cp[1] == '.'; cp++) {
351 playtone(pitch - 1, value, sustain);
356 if (value <= 0 || value > MIN_VALUE)
361 /* this may be followed by an override time value */
363 if (timeval <= 0 || timeval > MIN_VALUE)
365 for (sustain = 0; cp[1] == '.'; cp++) {
369 playtone(-1, timeval, sustain);
373 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
375 whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / tempo;
378 if (cp[1] == 'N' || cp[1] == 'n') {
382 } else if (cp[1] == 'L' || cp[1] == 'l') {
386 } else if (cp[1] == 'S' || cp[1] == 's') {
397 * ****************** UNIX DRIVER HOOKS BEGIN HERE **************************
398 * This section implements driver hooks to run playstring() and the tone(),
399 * endtone(), and rest() functions defined above.
402 static int spkr_active = FALSE; /* exclusion flag */
403 static char *spkr_inbuf; /* incoming buf */
406 spkropen(dev, flags, fmt, td)
413 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
420 (void) printf("spkropen: about to perform play initialization\n");
423 spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
430 spkrwrite(dev, uio, ioflag)
436 printf("spkrwrite: entering with dev = %s, count = %zd\n",
437 devtoname(dev), uio->uio_resid);
440 if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */
449 error = uiomove(cp, n, uio);
459 spkrclose(dev, flags, fmt, td)
466 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
471 free(spkr_inbuf, M_SPKR);
477 spkrioctl(dev, cmd, cmdarg, flags, td)
485 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
486 devtoname(dev), cmd);
489 if (cmd == SPKRTONE) {
490 tone_t *tp = (tone_t *)cmdarg;
492 if (tp->frequency == 0)
495 tone(tp->frequency, tp->duration);
497 } else if (cmd == SPKRTUNE) {
498 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg);
503 error = copyin(tp, &ttp, sizeof(tone_t));
507 if (ttp.duration == 0)
510 if (ttp.frequency == 0)
513 tone(ttp.frequency, ttp.duration);
520 static struct cdev *speaker_dev;
526 speaker_modevent(module_t mod, int type, void *data)
532 speaker_dev = make_dev(&spkr_cdevsw, 0,
533 UID_ROOT, GID_WHEEL, 0600, "speaker");
537 destroy_dev(speaker_dev);
545 DEV_MODULE(speaker, speaker_modevent, NULL);