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>
7 * $FreeBSD: src/sys/i386/isa/spkr.c,v 1.45 2000/01/29 16:00:32 peter Exp $
8 * $DragonFly: src/sys/dev/sound/isa/i386/spkr/Attic/spkr.c,v 1.2 2003/06/17 04:28:37 dillon Exp $
11 #include <sys/param.h>
12 #include <sys/systm.h>
13 #include <sys/kernel.h>
17 #include <sys/ctype.h>
18 #include <i386/isa/isa.h>
19 #include <i386/isa/timerreg.h>
20 #include <machine/clock.h>
21 #include <machine/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;
29 static struct cdevsw spkr_cdevsw = {
31 /* close */ spkrclose,
33 /* write */ spkrwrite,
34 /* ioctl */ spkrioctl,
37 /* strategy */ nostrategy,
46 /**************** MACHINE DEPENDENT PART STARTS HERE *************************
48 * This section defines a function tone() which causes a tone of given
49 * frequency and duration from the ISA console speaker.
50 * Another function endtone() is defined to force sound off, and there is
51 * also a rest() entry point to do pauses.
53 * Audible sound is generated using the Programmable Interval Timer (PIT) and
54 * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
55 * PPI controls whether sound is passed through at all; the PIT's channel 2 is
56 * used to generate clicks (a square wave) of whatever frequency is desired.
61 * XXX should be in a header and used in clock.c.
63 #define PPI_SPKR 0x03 /* turn these PPI bits on to pass sound */
66 static char endtone, endrest;
68 static void tone __P((unsigned int thz, unsigned int ticks));
69 static void rest __P((int ticks));
70 static void playinit __P((void));
71 static void playtone __P((int pitch, int value, int sustain));
72 static int abs __P((int n));
73 static void playstring __P((char *cp, size_t slen));
75 /* emit tone of frequency thz for given number of ticks */
78 unsigned int thz, ticks;
86 divisor = timer_freq / thz;
89 (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
92 /* set timer to generate clicks at given frequency in Hertz */
95 if (acquire_timer2(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT)) {
96 /* enter list of waiting procs ??? */
102 outb(TIMER_CNTR2, (divisor & 0xff)); /* send lo byte */
103 outb(TIMER_CNTR2, (divisor >> 8)); /* send hi byte */
106 /* turn the speaker on */
107 outb(IO_PPI, inb(IO_PPI) | PPI_SPKR);
110 * Set timeout to endtone function, then give up the timeslice.
111 * This is so other processes can execute while the tone is being
115 tsleep((caddr_t)&endtone, SPKRPRI | PCATCH, "spkrtn", ticks);
116 outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR);
122 /* rest for given number of ticks */
128 * Set timeout to endrest function, then give up the timeslice.
129 * This is so other processes can execute while the rest is being
133 (void) printf("rest: %d\n", ticks);
136 tsleep((caddr_t)&endrest, SPKRPRI | PCATCH, "spkrrs", ticks);
139 /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
141 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
142 * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
143 * tracking facility are added.
144 * Requires tone(), rest(), and endtone(). String play is not interruptible
145 * except possibly at physical block boundaries.
152 #define dtoi(c) ((c) - '0')
154 static int octave; /* currently selected octave */
155 static int whole; /* whole-note time at current tempo, in ticks */
156 static int value; /* whole divisor for note time, quarter note = 1 */
157 static int fill; /* controls spacing of notes */
158 static bool octtrack; /* octave-tracking on? */
159 static bool octprefix; /* override current octave-tracking state? */
162 * Magic number avoidance...
164 #define SECS_PER_MIN 60 /* seconds per minute */
165 #define WHOLE_NOTE 4 /* quarter notes per whole note */
166 #define MIN_VALUE 64 /* the most we can divide a note by */
167 #define DFLT_VALUE 4 /* default value (quarter-note) */
168 #define FILLTIME 8 /* for articulation, break note in parts */
169 #define STACCATO 6 /* 6/8 = 3/4 of note is filled */
170 #define NORMAL 7 /* 7/8ths of note interval is filled */
171 #define LEGATO 8 /* all of note interval is filled */
172 #define DFLT_OCTAVE 4 /* default octave */
173 #define MIN_TEMPO 32 /* minimum tempo */
174 #define DFLT_TEMPO 120 /* default tempo */
175 #define MAX_TEMPO 255 /* max tempo */
176 #define NUM_MULT 3 /* numerator of dot multiplier */
177 #define DENOM_MULT 2 /* denominator of dot multiplier */
179 /* letter to half-tone: A B C D E F G */
180 static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
183 * This is the American Standard A440 Equal-Tempered scale with frequencies
184 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
185 * our octave 0 is standard octave 2.
187 #define OCTAVE_NOTES 12 /* semitones per octave */
188 static int pitchtab[] =
190 /* C C# D D# E F F# G G# A A# B*/
191 /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
192 /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
193 /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
194 /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
195 /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
196 /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
197 /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
203 octave = DFLT_OCTAVE;
204 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
208 octprefix = TRUE; /* act as though there was an initial O(n) */
211 /* play tone of proper duration for current rhythm signature */
213 playtone(pitch, value, sustain)
214 int pitch, value, sustain;
216 register int sound, silence, snum = 1, sdenom = 1;
218 /* this weirdness avoids floating-point arithmetic */
219 for (; sustain; sustain--)
221 /* See the BUGS section in the man page for discussion */
223 sdenom *= DENOM_MULT;
226 if (value == 0 || sdenom == 0)
230 rest(whole * snum / (value * sdenom));
233 sound = (whole * snum) / (value * sdenom)
234 - (whole * (FILLTIME - fill)) / (value * FILLTIME);
235 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
238 (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
239 pitch, sound, silence);
242 tone(pitchtab[pitch], sound);
258 /* interpret and play an item from a notation string */
264 int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
266 #define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
267 {v = v * 10 + (*++cp - '0'); slen--;}
270 int sustain, timeval, tempo;
271 register char c = toupper(*cp);
274 (void) printf("playstring: %c (%x)\n", c, c);
279 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
282 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
284 /* this may be followed by an accidental sign */
285 if (cp[1] == '#' || cp[1] == '+')
291 else if (cp[1] == '-')
299 * If octave-tracking mode is on, and there has been no octave-
300 * setting prefix, find the version of the current letter note
301 * closest to the last regardless of octave.
303 if (octtrack && !octprefix)
305 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
308 pitch += OCTAVE_NOTES;
311 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
314 pitch -= OCTAVE_NOTES;
320 /* ...which may in turn be followed by an override time value */
322 if (timeval <= 0 || timeval > MIN_VALUE)
325 /* ...and/or sustain dots */
326 for (sustain = 0; cp[1] == '.'; cp++)
332 /* ...and/or a slur mark */
341 /* time to emit the actual tone */
342 playtone(pitch, timeval, sustain);
348 if (cp[1] == 'N' || cp[1] == 'n')
350 octprefix = octtrack = FALSE;
354 else if (cp[1] == 'L' || cp[1] == 'l')
363 if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES)
364 octave = DFLT_OCTAVE;
370 if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1)
383 for (sustain = 0; cp[1] == '.'; cp++)
395 playtone(pitch - 1, value, sustain);
401 if (value <= 0 || value > MIN_VALUE)
407 /* this may be followed by an override time value */
409 if (timeval <= 0 || timeval > MIN_VALUE)
411 for (sustain = 0; cp[1] == '.'; cp++)
416 playtone(-1, timeval, sustain);
421 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
423 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
427 if (cp[1] == 'N' || cp[1] == 'n')
433 else if (cp[1] == 'L' || cp[1] == 'l')
439 else if (cp[1] == 'S' || cp[1] == 's')
450 /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
452 * This section implements driver hooks to run playstring() and the tone(),
453 * endtone(), and rest() functions defined above.
456 static int spkr_active = FALSE; /* exclusion flag */
457 static struct buf *spkr_inbuf; /* incoming buf */
460 spkropen(dev, flags, fmt, p)
467 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
472 else if (spkr_active)
477 (void) printf("spkropen: about to perform play initialization\n");
480 spkr_inbuf = geteblk(DEV_BSIZE);
487 spkrwrite(dev, uio, ioflag)
493 printf("spkrwrite: entering with dev = %s, count = %d\n",
494 devtoname(dev), uio->uio_resid);
499 else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */
508 cp = spkr_inbuf->b_data;
509 error = uiomove(cp, n, uio);
519 spkrclose(dev, flags, fmt, p)
526 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
533 wakeup((caddr_t)&endtone);
534 wakeup((caddr_t)&endrest);
542 spkrioctl(dev, cmd, cmdarg, flags, p)
550 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
551 devtoname(dev), cmd);
556 else if (cmd == SPKRTONE)
558 tone_t *tp = (tone_t *)cmdarg;
560 if (tp->frequency == 0)
563 tone(tp->frequency, tp->duration);
566 else if (cmd == SPKRTUNE)
568 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg);
573 error = copyin(tp, &ttp, sizeof(tone_t));
576 if (ttp.duration == 0)
578 if (ttp.frequency == 0)
581 tone(ttp.frequency, ttp.duration);
589 spkr_drvinit(void *unused)
591 make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "speaker");
594 SYSINIT(spkrdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,spkr_drvinit,NULL)
597 /* spkr.c ends here */