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>
15 #include <sys/kernel.h>
16 #include <sys/module.h>
19 #include <sys/ctype.h>
20 #include <sys/malloc.h>
21 #include <isa/isavar.h>
22 #include <machine/clock.h>
23 #include <machine/speaker.h>
24 #include <machine/ppireg.h>
25 #include <machine/timerreg.h>
27 static d_open_t spkropen;
28 static d_close_t spkrclose;
29 static d_write_t spkrwrite;
30 static d_ioctl_t spkrioctl;
32 static struct cdevsw spkr_cdevsw = {
33 .d_version = D_VERSION,
34 .d_flags = D_NEEDGIANT,
42 static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
44 /**************** MACHINE DEPENDENT PART STARTS HERE *************************
46 * This section defines a function tone() which causes a tone of given
47 * frequency and duration from the ISA console speaker.
48 * Another function endtone() is defined to force sound off, and there is
49 * also a rest() entry point to do pauses.
51 * Audible sound is generated using the Programmable Interval Timer (PIT) and
52 * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
53 * PPI controls whether sound is passed through at all; the PIT's channel 2 is
54 * used to generate clicks (a square wave) of whatever frequency is desired.
58 #define SPKR_DESC "PC98 speaker"
60 #define SPKR_DESC "PC speaker"
64 static char endtone, endrest;
66 static void tone(unsigned int thz, unsigned int ticks);
67 static void rest(int ticks);
68 static void playinit(void);
69 static void playtone(int pitch, int value, int sustain);
70 static void playstring(char *cp, size_t slen);
72 /* emit tone of frequency thz for given number of ticks */
75 unsigned int thz, ticks;
83 divisor = timer_freq / thz;
86 (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
89 /* set timer to generate clicks at given frequency in Hertz */
92 if (timer_spkr_acquire()) {
93 /* enter list of waiting procs ??? */
99 spkr_set_pitch(divisor);
102 /* turn the speaker on */
106 * Set timeout to endtone function, then give up the timeslice.
107 * This is so other processes can execute while the tone is being
111 tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", ticks);
114 timer_spkr_release();
118 /* rest for given number of ticks */
124 * Set timeout to endrest function, then give up the timeslice.
125 * This is so other processes can execute while the rest is being
129 (void) printf("rest: %d\n", ticks);
132 tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", ticks);
135 /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
137 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
138 * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
139 * tracking facility are added.
140 * Requires tone(), rest(), and endtone(). String play is not interruptible
141 * except possibly at physical block boundaries.
148 #define dtoi(c) ((c) - '0')
150 static int octave; /* currently selected octave */
151 static int whole; /* whole-note time at current tempo, in ticks */
152 static int value; /* whole divisor for note time, quarter note = 1 */
153 static int fill; /* controls spacing of notes */
154 static bool octtrack; /* octave-tracking on? */
155 static bool octprefix; /* override current octave-tracking state? */
158 * Magic number avoidance...
160 #define SECS_PER_MIN 60 /* seconds per minute */
161 #define WHOLE_NOTE 4 /* quarter notes per whole note */
162 #define MIN_VALUE 64 /* the most we can divide a note by */
163 #define DFLT_VALUE 4 /* default value (quarter-note) */
164 #define FILLTIME 8 /* for articulation, break note in parts */
165 #define STACCATO 6 /* 6/8 = 3/4 of note is filled */
166 #define NORMAL 7 /* 7/8ths of note interval is filled */
167 #define LEGATO 8 /* all of note interval is filled */
168 #define DFLT_OCTAVE 4 /* default octave */
169 #define MIN_TEMPO 32 /* minimum tempo */
170 #define DFLT_TEMPO 120 /* default tempo */
171 #define MAX_TEMPO 255 /* max tempo */
172 #define NUM_MULT 3 /* numerator of dot multiplier */
173 #define DENOM_MULT 2 /* denominator of dot multiplier */
175 /* letter to half-tone: A B C D E F G */
176 static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
179 * This is the American Standard A440 Equal-Tempered scale with frequencies
180 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
181 * our octave 0 is standard octave 2.
183 #define OCTAVE_NOTES 12 /* semitones per octave */
184 static int pitchtab[] =
186 /* C C# D D# E F F# G G# A A# B*/
187 /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
188 /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
189 /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
190 /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
191 /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
192 /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
193 /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
199 octave = DFLT_OCTAVE;
200 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
204 octprefix = TRUE; /* act as though there was an initial O(n) */
207 /* play tone of proper duration for current rhythm signature */
209 playtone(pitch, value, sustain)
210 int pitch, value, sustain;
212 register int sound, silence, snum = 1, sdenom = 1;
214 /* this weirdness avoids floating-point arithmetic */
215 for (; sustain; sustain--)
217 /* See the BUGS section in the man page for discussion */
219 sdenom *= DENOM_MULT;
222 if (value == 0 || sdenom == 0)
226 rest(whole * snum / (value * sdenom));
229 sound = (whole * snum) / (value * sdenom)
230 - (whole * (FILLTIME - fill)) / (value * FILLTIME);
231 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
234 (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
235 pitch, sound, silence);
238 tone(pitchtab[pitch], sound);
244 /* interpret and play an item from a notation string */
250 int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
252 #define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
253 {v = v * 10 + (*++cp - '0'); slen--;}
256 int sustain, timeval, tempo;
257 register char c = toupper(*cp);
260 (void) printf("playstring: %c (%x)\n", c, c);
265 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
268 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
270 /* this may be followed by an accidental sign */
271 if (cp[1] == '#' || cp[1] == '+')
277 else if (cp[1] == '-')
285 * If octave-tracking mode is on, and there has been no octave-
286 * setting prefix, find the version of the current letter note
287 * closest to the last regardless of octave.
289 if (octtrack && !octprefix)
291 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
294 pitch += OCTAVE_NOTES;
297 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
300 pitch -= OCTAVE_NOTES;
306 /* ...which may in turn be followed by an override time value */
308 if (timeval <= 0 || timeval > MIN_VALUE)
311 /* ...and/or sustain dots */
312 for (sustain = 0; cp[1] == '.'; cp++)
318 /* ...and/or a slur mark */
327 /* time to emit the actual tone */
328 playtone(pitch, timeval, sustain);
334 if (cp[1] == 'N' || cp[1] == 'n')
336 octprefix = octtrack = FALSE;
340 else if (cp[1] == 'L' || cp[1] == 'l')
349 if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES)
350 octave = DFLT_OCTAVE;
356 if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1)
369 for (sustain = 0; cp[1] == '.'; cp++)
381 playtone(pitch - 1, value, sustain);
387 if (value <= 0 || value > MIN_VALUE)
393 /* this may be followed by an override time value */
395 if (timeval <= 0 || timeval > MIN_VALUE)
397 for (sustain = 0; cp[1] == '.'; cp++)
402 playtone(-1, timeval, sustain);
407 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
409 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
413 if (cp[1] == 'N' || cp[1] == 'n')
419 else if (cp[1] == 'L' || cp[1] == 'l')
425 else if (cp[1] == 'S' || cp[1] == 's')
436 /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
438 * This section implements driver hooks to run playstring() and the tone(),
439 * endtone(), and rest() functions defined above.
442 static int spkr_active = FALSE; /* exclusion flag */
443 static char *spkr_inbuf; /* incoming buf */
446 spkropen(dev, flags, fmt, td)
453 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
458 else if (spkr_active)
463 (void) printf("spkropen: about to perform play initialization\n");
466 spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
473 spkrwrite(dev, uio, ioflag)
479 printf("spkrwrite: entering with dev = %s, count = %d\n",
480 devtoname(dev), uio->uio_resid);
485 else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */
495 error = uiomove(cp, n, uio);
505 spkrclose(dev, flags, fmt, td)
512 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
521 free(spkr_inbuf, M_SPKR);
528 spkrioctl(dev, cmd, cmdarg, flags, td)
536 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
537 devtoname(dev), cmd);
542 else if (cmd == SPKRTONE)
544 tone_t *tp = (tone_t *)cmdarg;
546 if (tp->frequency == 0)
549 tone(tp->frequency, tp->duration);
552 else if (cmd == SPKRTUNE)
554 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg);
559 error = copyin(tp, &ttp, sizeof(tone_t));
562 if (ttp.duration == 0)
564 if (ttp.frequency == 0)
567 tone(ttp.frequency, ttp.duration);
575 * Install placeholder to claim the resources owned by the
578 static struct isa_pnp_id speaker_ids[] = {
580 { 0x0008d041 /* PNP0800 */, SPKR_DESC },
585 static struct cdev *speaker_dev;
588 speaker_probe(device_t dev)
592 error = ISA_PNP_PROBE(device_get_parent(dev), dev, speaker_ids);
602 /* Not configured by hints. */
603 if (strncmp(device_get_name(dev), "speaker", 9))
606 device_set_desc(dev, SPKR_DESC);
612 speaker_attach(device_t dev)
616 device_printf(dev, "Already attached!\n");
620 speaker_dev = make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
626 speaker_detach(device_t dev)
628 destroy_dev(speaker_dev);
632 static device_method_t speaker_methods[] = {
633 /* Device interface */
634 DEVMETHOD(device_probe, speaker_probe),
635 DEVMETHOD(device_attach, speaker_attach),
636 DEVMETHOD(device_detach, speaker_detach),
637 DEVMETHOD(device_shutdown, bus_generic_shutdown),
638 DEVMETHOD(device_suspend, bus_generic_suspend),
639 DEVMETHOD(device_resume, bus_generic_resume),
643 static driver_t speaker_driver = {
649 static devclass_t speaker_devclass;
651 DRIVER_MODULE(speaker, isa, speaker_driver, speaker_devclass, 0, 0);
653 DRIVER_MODULE(speaker, acpi, speaker_driver, speaker_devclass, 0, 0);
656 /* spkr.c ends here */