Deleted Added
full compact
spkr.c (130585) spkr.c (139790)
1/*
1/*-
2 * spkr.c -- device driver for console speaker
3 *
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
7 */
8
9#include <sys/cdefs.h>
2 * spkr.c -- device driver for console speaker
3 *
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
7 */
8
9#include <sys/cdefs.h>
10__FBSDID("$FreeBSD: head/sys/dev/speaker/spkr.c 130585 2004-06-16 09:47:26Z phk $");
10__FBSDID("$FreeBSD: head/sys/dev/speaker/spkr.c 139790 2005-01-06 22:18:23Z imp $");
11
12#include <sys/param.h>
13#include <sys/systm.h>
14#include <sys/bus.h>
15#include <sys/kernel.h>
16#include <sys/module.h>
17#include <sys/uio.h>
18#include <sys/conf.h>
19#include <sys/ctype.h>
20#include <sys/malloc.h>
21#include <isa/isavar.h>
22#ifdef PC98
23#include <pc98/pc98/pc98.h>
24#else
25#include <i386/isa/isa.h>
26#endif
27#include <i386/isa/timerreg.h>
28#include <machine/clock.h>
29#include <machine/speaker.h>
30
31static d_open_t spkropen;
32static d_close_t spkrclose;
33static d_write_t spkrwrite;
34static d_ioctl_t spkrioctl;
35
36static struct cdevsw spkr_cdevsw = {
37 .d_version = D_VERSION,
38 .d_flags = D_NEEDGIANT,
39 .d_open = spkropen,
40 .d_close = spkrclose,
41 .d_write = spkrwrite,
42 .d_ioctl = spkrioctl,
43 .d_name = "spkr",
44};
45
46static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
47
48/**************** MACHINE DEPENDENT PART STARTS HERE *************************
49 *
50 * This section defines a function tone() which causes a tone of given
51 * frequency and duration from the ISA console speaker.
52 * Another function endtone() is defined to force sound off, and there is
53 * also a rest() entry point to do pauses.
54 *
55 * Audible sound is generated using the Programmable Interval Timer (PIT) and
56 * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
57 * PPI controls whether sound is passed through at all; the PIT's channel 2 is
58 * used to generate clicks (a square wave) of whatever frequency is desired.
59 */
60
61/*
62 * XXX PPI control values should be in a header and used in clock.c.
63 */
64#ifdef PC98
65#define SPKR_DESC "PC98 speaker"
66#define PPI_SPKR 0x08 /* turn these PPI bits on to pass sound */
67#define PIT_COUNT 0x3fdb /* PIT count address */
68
69#define SPEAKER_ON outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR)
70#define SPEAKER_OFF outb(IO_PPI, inb(IO_PPI) | PPI_SPKR)
71#define TIMER_ACQUIRE acquire_timer1(TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT)
72#define TIMER_RELEASE release_timer1()
73#define SPEAKER_WRITE(val) { \
74 outb(PIT_COUNT, (val & 0xff)); \
75 outb(PIT_COUNT, (val >> 8)); \
76 }
77#else
78#define SPKR_DESC "PC speaker"
79#define PPI_SPKR 0x03 /* turn these PPI bits on to pass sound */
80
81#define SPEAKER_ON outb(IO_PPI, inb(IO_PPI) | PPI_SPKR)
82#define SPEAKER_OFF outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR)
83#define TIMER_ACQUIRE acquire_timer2(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT)
84#define TIMER_RELEASE release_timer2()
85#define SPEAKER_WRITE(val) { \
86 outb(TIMER_CNTR2, (val & 0xff)); \
87 outb(TIMER_CNTR2, (val >> 8)); \
88 }
89#endif
90
91#define SPKRPRI PSOCK
92static char endtone, endrest;
93
94static void tone(unsigned int thz, unsigned int ticks);
95static void rest(int ticks);
96static void playinit(void);
97static void playtone(int pitch, int value, int sustain);
98static void playstring(char *cp, size_t slen);
99
100/* emit tone of frequency thz for given number of ticks */
101static void
102tone(thz, ticks)
103 unsigned int thz, ticks;
104{
105 unsigned int divisor;
106 int sps;
107
108 if (thz <= 0)
109 return;
110
111 divisor = timer_freq / thz;
112
113#ifdef DEBUG
114 (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
115#endif /* DEBUG */
116
117 /* set timer to generate clicks at given frequency in Hertz */
118 sps = splclock();
119
120 if (TIMER_ACQUIRE) {
121 /* enter list of waiting procs ??? */
122 splx(sps);
123 return;
124 }
125 splx(sps);
126 disable_intr();
127 SPEAKER_WRITE(divisor);
128 enable_intr();
129
130 /* turn the speaker on */
131 SPEAKER_ON;
132
133 /*
134 * Set timeout to endtone function, then give up the timeslice.
135 * This is so other processes can execute while the tone is being
136 * emitted.
137 */
138 if (ticks > 0)
139 tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", ticks);
140 SPEAKER_OFF;
141 sps = splclock();
142 TIMER_RELEASE;
143 splx(sps);
144}
145
146/* rest for given number of ticks */
147static void
148rest(ticks)
149 int ticks;
150{
151 /*
152 * Set timeout to endrest function, then give up the timeslice.
153 * This is so other processes can execute while the rest is being
154 * waited out.
155 */
156#ifdef DEBUG
157 (void) printf("rest: %d\n", ticks);
158#endif /* DEBUG */
159 if (ticks > 0)
160 tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", ticks);
161}
162
163/**************** PLAY STRING INTERPRETER BEGINS HERE **********************
164 *
165 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
166 * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
167 * tracking facility are added.
168 * Requires tone(), rest(), and endtone(). String play is not interruptible
169 * except possibly at physical block boundaries.
170 */
171
172typedef int bool;
173#define TRUE 1
174#define FALSE 0
175
176#define dtoi(c) ((c) - '0')
177
178static int octave; /* currently selected octave */
179static int whole; /* whole-note time at current tempo, in ticks */
180static int value; /* whole divisor for note time, quarter note = 1 */
181static int fill; /* controls spacing of notes */
182static bool octtrack; /* octave-tracking on? */
183static bool octprefix; /* override current octave-tracking state? */
184
185/*
186 * Magic number avoidance...
187 */
188#define SECS_PER_MIN 60 /* seconds per minute */
189#define WHOLE_NOTE 4 /* quarter notes per whole note */
190#define MIN_VALUE 64 /* the most we can divide a note by */
191#define DFLT_VALUE 4 /* default value (quarter-note) */
192#define FILLTIME 8 /* for articulation, break note in parts */
193#define STACCATO 6 /* 6/8 = 3/4 of note is filled */
194#define NORMAL 7 /* 7/8ths of note interval is filled */
195#define LEGATO 8 /* all of note interval is filled */
196#define DFLT_OCTAVE 4 /* default octave */
197#define MIN_TEMPO 32 /* minimum tempo */
198#define DFLT_TEMPO 120 /* default tempo */
199#define MAX_TEMPO 255 /* max tempo */
200#define NUM_MULT 3 /* numerator of dot multiplier */
201#define DENOM_MULT 2 /* denominator of dot multiplier */
202
203/* letter to half-tone: A B C D E F G */
204static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
205
206/*
207 * This is the American Standard A440 Equal-Tempered scale with frequencies
208 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
209 * our octave 0 is standard octave 2.
210 */
211#define OCTAVE_NOTES 12 /* semitones per octave */
212static int pitchtab[] =
213{
214/* C C# D D# E F F# G G# A A# B*/
215/* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
216/* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
217/* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
218/* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
219/* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
220/* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
221/* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
222};
223
224static void
225playinit()
226{
227 octave = DFLT_OCTAVE;
228 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
229 fill = NORMAL;
230 value = DFLT_VALUE;
231 octtrack = FALSE;
232 octprefix = TRUE; /* act as though there was an initial O(n) */
233}
234
235/* play tone of proper duration for current rhythm signature */
236static void
237playtone(pitch, value, sustain)
238 int pitch, value, sustain;
239{
240 register int sound, silence, snum = 1, sdenom = 1;
241
242 /* this weirdness avoids floating-point arithmetic */
243 for (; sustain; sustain--)
244 {
245 /* See the BUGS section in the man page for discussion */
246 snum *= NUM_MULT;
247 sdenom *= DENOM_MULT;
248 }
249
250 if (value == 0 || sdenom == 0)
251 return;
252
253 if (pitch == -1)
254 rest(whole * snum / (value * sdenom));
255 else
256 {
257 sound = (whole * snum) / (value * sdenom)
258 - (whole * (FILLTIME - fill)) / (value * FILLTIME);
259 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
260
261#ifdef DEBUG
262 (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
263 pitch, sound, silence);
264#endif /* DEBUG */
265
266 tone(pitchtab[pitch], sound);
267 if (fill != LEGATO)
268 rest(silence);
269 }
270}
271
272/* interpret and play an item from a notation string */
273static void
274playstring(cp, slen)
275 char *cp;
276 size_t slen;
277{
278 int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
279
280#define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
281 {v = v * 10 + (*++cp - '0'); slen--;}
282 for (; slen--; cp++)
283 {
284 int sustain, timeval, tempo;
285 register char c = toupper(*cp);
286
287#ifdef DEBUG
288 (void) printf("playstring: %c (%x)\n", c, c);
289#endif /* DEBUG */
290
291 switch (c)
292 {
293 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
294
295 /* compute pitch */
296 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
297
298 /* this may be followed by an accidental sign */
299 if (cp[1] == '#' || cp[1] == '+')
300 {
301 ++pitch;
302 ++cp;
303 slen--;
304 }
305 else if (cp[1] == '-')
306 {
307 --pitch;
308 ++cp;
309 slen--;
310 }
311
312 /*
313 * If octave-tracking mode is on, and there has been no octave-
314 * setting prefix, find the version of the current letter note
315 * closest to the last regardless of octave.
316 */
317 if (octtrack && !octprefix)
318 {
319 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
320 {
321 ++octave;
322 pitch += OCTAVE_NOTES;
323 }
324
325 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
326 {
327 --octave;
328 pitch -= OCTAVE_NOTES;
329 }
330 }
331 octprefix = FALSE;
332 lastpitch = pitch;
333
334 /* ...which may in turn be followed by an override time value */
335 GETNUM(cp, timeval);
336 if (timeval <= 0 || timeval > MIN_VALUE)
337 timeval = value;
338
339 /* ...and/or sustain dots */
340 for (sustain = 0; cp[1] == '.'; cp++)
341 {
342 slen--;
343 sustain++;
344 }
345
346 /* ...and/or a slur mark */
347 oldfill = fill;
348 if (cp[1] == '_')
349 {
350 fill = LEGATO;
351 ++cp;
352 slen--;
353 }
354
355 /* time to emit the actual tone */
356 playtone(pitch, timeval, sustain);
357
358 fill = oldfill;
359 break;
360
361 case 'O':
362 if (cp[1] == 'N' || cp[1] == 'n')
363 {
364 octprefix = octtrack = FALSE;
365 ++cp;
366 slen--;
367 }
368 else if (cp[1] == 'L' || cp[1] == 'l')
369 {
370 octtrack = TRUE;
371 ++cp;
372 slen--;
373 }
374 else
375 {
376 GETNUM(cp, octave);
377 if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES)
378 octave = DFLT_OCTAVE;
379 octprefix = TRUE;
380 }
381 break;
382
383 case '>':
384 if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1)
385 octave++;
386 octprefix = TRUE;
387 break;
388
389 case '<':
390 if (octave > 0)
391 octave--;
392 octprefix = TRUE;
393 break;
394
395 case 'N':
396 GETNUM(cp, pitch);
397 for (sustain = 0; cp[1] == '.'; cp++)
398 {
399 slen--;
400 sustain++;
401 }
402 oldfill = fill;
403 if (cp[1] == '_')
404 {
405 fill = LEGATO;
406 ++cp;
407 slen--;
408 }
409 playtone(pitch - 1, value, sustain);
410 fill = oldfill;
411 break;
412
413 case 'L':
414 GETNUM(cp, value);
415 if (value <= 0 || value > MIN_VALUE)
416 value = DFLT_VALUE;
417 break;
418
419 case 'P':
420 case '~':
421 /* this may be followed by an override time value */
422 GETNUM(cp, timeval);
423 if (timeval <= 0 || timeval > MIN_VALUE)
424 timeval = value;
425 for (sustain = 0; cp[1] == '.'; cp++)
426 {
427 slen--;
428 sustain++;
429 }
430 playtone(-1, timeval, sustain);
431 break;
432
433 case 'T':
434 GETNUM(cp, tempo);
435 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
436 tempo = DFLT_TEMPO;
437 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
438 break;
439
440 case 'M':
441 if (cp[1] == 'N' || cp[1] == 'n')
442 {
443 fill = NORMAL;
444 ++cp;
445 slen--;
446 }
447 else if (cp[1] == 'L' || cp[1] == 'l')
448 {
449 fill = LEGATO;
450 ++cp;
451 slen--;
452 }
453 else if (cp[1] == 'S' || cp[1] == 's')
454 {
455 fill = STACCATO;
456 ++cp;
457 slen--;
458 }
459 break;
460 }
461 }
462}
463
464/******************* UNIX DRIVER HOOKS BEGIN HERE **************************
465 *
466 * This section implements driver hooks to run playstring() and the tone(),
467 * endtone(), and rest() functions defined above.
468 */
469
470static int spkr_active = FALSE; /* exclusion flag */
471static char *spkr_inbuf; /* incoming buf */
472
473static int
474spkropen(dev, flags, fmt, td)
475 struct cdev *dev;
476 int flags;
477 int fmt;
478 struct thread *td;
479{
480#ifdef DEBUG
481 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
482#endif /* DEBUG */
483
484 if (minor(dev) != 0)
485 return(ENXIO);
486 else if (spkr_active)
487 return(EBUSY);
488 else
489 {
490#ifdef DEBUG
491 (void) printf("spkropen: about to perform play initialization\n");
492#endif /* DEBUG */
493 playinit();
494 spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
495 spkr_active = TRUE;
496 return(0);
497 }
498}
499
500static int
501spkrwrite(dev, uio, ioflag)
502 struct cdev *dev;
503 struct uio *uio;
504 int ioflag;
505{
506#ifdef DEBUG
507 printf("spkrwrite: entering with dev = %s, count = %d\n",
508 devtoname(dev), uio->uio_resid);
509#endif /* DEBUG */
510
511 if (minor(dev) != 0)
512 return(ENXIO);
513 else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */
514 return(E2BIG);
515 else
516 {
517 unsigned n;
518 char *cp;
519 int error;
520
521 n = uio->uio_resid;
522 cp = spkr_inbuf;
523 error = uiomove(cp, n, uio);
524 if (!error) {
525 cp[n] = '\0';
526 playstring(cp, n);
527 }
528 return(error);
529 }
530}
531
532static int
533spkrclose(dev, flags, fmt, td)
534 struct cdev *dev;
535 int flags;
536 int fmt;
537 struct thread *td;
538{
539#ifdef DEBUG
540 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
541#endif /* DEBUG */
542
543 if (minor(dev) != 0)
544 return(ENXIO);
545 else
546 {
547 wakeup(&endtone);
548 wakeup(&endrest);
549 free(spkr_inbuf, M_SPKR);
550 spkr_active = FALSE;
551 return(0);
552 }
553}
554
555static int
556spkrioctl(dev, cmd, cmdarg, flags, td)
557 struct cdev *dev;
558 unsigned long cmd;
559 caddr_t cmdarg;
560 int flags;
561 struct thread *td;
562{
563#ifdef DEBUG
564 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
565 devtoname(dev), cmd);
566#endif /* DEBUG */
567
568 if (minor(dev) != 0)
569 return(ENXIO);
570 else if (cmd == SPKRTONE)
571 {
572 tone_t *tp = (tone_t *)cmdarg;
573
574 if (tp->frequency == 0)
575 rest(tp->duration);
576 else
577 tone(tp->frequency, tp->duration);
578 return 0;
579 }
580 else if (cmd == SPKRTUNE)
581 {
582 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg);
583 tone_t ttp;
584 int error;
585
586 for (; ; tp++) {
587 error = copyin(tp, &ttp, sizeof(tone_t));
588 if (error)
589 return(error);
590 if (ttp.duration == 0)
591 break;
592 if (ttp.frequency == 0)
593 rest(ttp.duration);
594 else
595 tone(ttp.frequency, ttp.duration);
596 }
597 return(0);
598 }
599 return(EINVAL);
600}
601
602/*
603 * Install placeholder to claim the resources owned by the
604 * AT tone generator.
605 */
606static struct isa_pnp_id speaker_ids[] = {
607#ifndef PC98
608 { 0x0008d041 /* PNP0800 */, SPKR_DESC },
609#endif
610 { 0 }
611};
612
613static struct cdev *speaker_dev;
614
615static int
616speaker_probe(device_t dev)
617{
618 int error;
619
620 error = ISA_PNP_PROBE(device_get_parent(dev), dev, speaker_ids);
621
622 /* PnP match */
623 if (error == 0)
624 return (0);
625
626 /* No match */
627 if (error == ENXIO)
628 return (ENXIO);
629
630 /* Not configured by hints. */
631 if (strncmp(device_get_name(dev), "speaker", 9))
632 return (ENXIO);
633
634 device_set_desc(dev, SPKR_DESC);
635
636 return (0);
637}
638
639static int
640speaker_attach(device_t dev)
641{
642
643 if (speaker_dev) {
644 device_printf(dev, "Already attached!\n");
645 return (ENXIO);
646 }
647
648 speaker_dev = make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
649 "speaker");
650 return (0);
651}
652
653static int
654speaker_detach(device_t dev)
655{
656 destroy_dev(speaker_dev);
657 return (0);
658}
659
660static device_method_t speaker_methods[] = {
661 /* Device interface */
662 DEVMETHOD(device_probe, speaker_probe),
663 DEVMETHOD(device_attach, speaker_attach),
664 DEVMETHOD(device_detach, speaker_detach),
665 DEVMETHOD(device_shutdown, bus_generic_shutdown),
666 DEVMETHOD(device_suspend, bus_generic_suspend),
667 DEVMETHOD(device_resume, bus_generic_resume),
668 { 0, 0 }
669};
670
671static driver_t speaker_driver = {
672 "speaker",
673 speaker_methods,
674 1, /* no softc */
675};
676
677static devclass_t speaker_devclass;
678
679DRIVER_MODULE(speaker, isa, speaker_driver, speaker_devclass, 0, 0);
680#ifndef PC98
681DRIVER_MODULE(speaker, acpi, speaker_driver, speaker_devclass, 0, 0);
682#endif
683
684/* spkr.c ends here */
11
12#include <sys/param.h>
13#include <sys/systm.h>
14#include <sys/bus.h>
15#include <sys/kernel.h>
16#include <sys/module.h>
17#include <sys/uio.h>
18#include <sys/conf.h>
19#include <sys/ctype.h>
20#include <sys/malloc.h>
21#include <isa/isavar.h>
22#ifdef PC98
23#include <pc98/pc98/pc98.h>
24#else
25#include <i386/isa/isa.h>
26#endif
27#include <i386/isa/timerreg.h>
28#include <machine/clock.h>
29#include <machine/speaker.h>
30
31static d_open_t spkropen;
32static d_close_t spkrclose;
33static d_write_t spkrwrite;
34static d_ioctl_t spkrioctl;
35
36static struct cdevsw spkr_cdevsw = {
37 .d_version = D_VERSION,
38 .d_flags = D_NEEDGIANT,
39 .d_open = spkropen,
40 .d_close = spkrclose,
41 .d_write = spkrwrite,
42 .d_ioctl = spkrioctl,
43 .d_name = "spkr",
44};
45
46static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
47
48/**************** MACHINE DEPENDENT PART STARTS HERE *************************
49 *
50 * This section defines a function tone() which causes a tone of given
51 * frequency and duration from the ISA console speaker.
52 * Another function endtone() is defined to force sound off, and there is
53 * also a rest() entry point to do pauses.
54 *
55 * Audible sound is generated using the Programmable Interval Timer (PIT) and
56 * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
57 * PPI controls whether sound is passed through at all; the PIT's channel 2 is
58 * used to generate clicks (a square wave) of whatever frequency is desired.
59 */
60
61/*
62 * XXX PPI control values should be in a header and used in clock.c.
63 */
64#ifdef PC98
65#define SPKR_DESC "PC98 speaker"
66#define PPI_SPKR 0x08 /* turn these PPI bits on to pass sound */
67#define PIT_COUNT 0x3fdb /* PIT count address */
68
69#define SPEAKER_ON outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR)
70#define SPEAKER_OFF outb(IO_PPI, inb(IO_PPI) | PPI_SPKR)
71#define TIMER_ACQUIRE acquire_timer1(TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT)
72#define TIMER_RELEASE release_timer1()
73#define SPEAKER_WRITE(val) { \
74 outb(PIT_COUNT, (val & 0xff)); \
75 outb(PIT_COUNT, (val >> 8)); \
76 }
77#else
78#define SPKR_DESC "PC speaker"
79#define PPI_SPKR 0x03 /* turn these PPI bits on to pass sound */
80
81#define SPEAKER_ON outb(IO_PPI, inb(IO_PPI) | PPI_SPKR)
82#define SPEAKER_OFF outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR)
83#define TIMER_ACQUIRE acquire_timer2(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT)
84#define TIMER_RELEASE release_timer2()
85#define SPEAKER_WRITE(val) { \
86 outb(TIMER_CNTR2, (val & 0xff)); \
87 outb(TIMER_CNTR2, (val >> 8)); \
88 }
89#endif
90
91#define SPKRPRI PSOCK
92static char endtone, endrest;
93
94static void tone(unsigned int thz, unsigned int ticks);
95static void rest(int ticks);
96static void playinit(void);
97static void playtone(int pitch, int value, int sustain);
98static void playstring(char *cp, size_t slen);
99
100/* emit tone of frequency thz for given number of ticks */
101static void
102tone(thz, ticks)
103 unsigned int thz, ticks;
104{
105 unsigned int divisor;
106 int sps;
107
108 if (thz <= 0)
109 return;
110
111 divisor = timer_freq / thz;
112
113#ifdef DEBUG
114 (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
115#endif /* DEBUG */
116
117 /* set timer to generate clicks at given frequency in Hertz */
118 sps = splclock();
119
120 if (TIMER_ACQUIRE) {
121 /* enter list of waiting procs ??? */
122 splx(sps);
123 return;
124 }
125 splx(sps);
126 disable_intr();
127 SPEAKER_WRITE(divisor);
128 enable_intr();
129
130 /* turn the speaker on */
131 SPEAKER_ON;
132
133 /*
134 * Set timeout to endtone function, then give up the timeslice.
135 * This is so other processes can execute while the tone is being
136 * emitted.
137 */
138 if (ticks > 0)
139 tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", ticks);
140 SPEAKER_OFF;
141 sps = splclock();
142 TIMER_RELEASE;
143 splx(sps);
144}
145
146/* rest for given number of ticks */
147static void
148rest(ticks)
149 int ticks;
150{
151 /*
152 * Set timeout to endrest function, then give up the timeslice.
153 * This is so other processes can execute while the rest is being
154 * waited out.
155 */
156#ifdef DEBUG
157 (void) printf("rest: %d\n", ticks);
158#endif /* DEBUG */
159 if (ticks > 0)
160 tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", ticks);
161}
162
163/**************** PLAY STRING INTERPRETER BEGINS HERE **********************
164 *
165 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
166 * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
167 * tracking facility are added.
168 * Requires tone(), rest(), and endtone(). String play is not interruptible
169 * except possibly at physical block boundaries.
170 */
171
172typedef int bool;
173#define TRUE 1
174#define FALSE 0
175
176#define dtoi(c) ((c) - '0')
177
178static int octave; /* currently selected octave */
179static int whole; /* whole-note time at current tempo, in ticks */
180static int value; /* whole divisor for note time, quarter note = 1 */
181static int fill; /* controls spacing of notes */
182static bool octtrack; /* octave-tracking on? */
183static bool octprefix; /* override current octave-tracking state? */
184
185/*
186 * Magic number avoidance...
187 */
188#define SECS_PER_MIN 60 /* seconds per minute */
189#define WHOLE_NOTE 4 /* quarter notes per whole note */
190#define MIN_VALUE 64 /* the most we can divide a note by */
191#define DFLT_VALUE 4 /* default value (quarter-note) */
192#define FILLTIME 8 /* for articulation, break note in parts */
193#define STACCATO 6 /* 6/8 = 3/4 of note is filled */
194#define NORMAL 7 /* 7/8ths of note interval is filled */
195#define LEGATO 8 /* all of note interval is filled */
196#define DFLT_OCTAVE 4 /* default octave */
197#define MIN_TEMPO 32 /* minimum tempo */
198#define DFLT_TEMPO 120 /* default tempo */
199#define MAX_TEMPO 255 /* max tempo */
200#define NUM_MULT 3 /* numerator of dot multiplier */
201#define DENOM_MULT 2 /* denominator of dot multiplier */
202
203/* letter to half-tone: A B C D E F G */
204static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
205
206/*
207 * This is the American Standard A440 Equal-Tempered scale with frequencies
208 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
209 * our octave 0 is standard octave 2.
210 */
211#define OCTAVE_NOTES 12 /* semitones per octave */
212static int pitchtab[] =
213{
214/* C C# D D# E F F# G G# A A# B*/
215/* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
216/* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
217/* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
218/* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
219/* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
220/* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
221/* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
222};
223
224static void
225playinit()
226{
227 octave = DFLT_OCTAVE;
228 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
229 fill = NORMAL;
230 value = DFLT_VALUE;
231 octtrack = FALSE;
232 octprefix = TRUE; /* act as though there was an initial O(n) */
233}
234
235/* play tone of proper duration for current rhythm signature */
236static void
237playtone(pitch, value, sustain)
238 int pitch, value, sustain;
239{
240 register int sound, silence, snum = 1, sdenom = 1;
241
242 /* this weirdness avoids floating-point arithmetic */
243 for (; sustain; sustain--)
244 {
245 /* See the BUGS section in the man page for discussion */
246 snum *= NUM_MULT;
247 sdenom *= DENOM_MULT;
248 }
249
250 if (value == 0 || sdenom == 0)
251 return;
252
253 if (pitch == -1)
254 rest(whole * snum / (value * sdenom));
255 else
256 {
257 sound = (whole * snum) / (value * sdenom)
258 - (whole * (FILLTIME - fill)) / (value * FILLTIME);
259 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
260
261#ifdef DEBUG
262 (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
263 pitch, sound, silence);
264#endif /* DEBUG */
265
266 tone(pitchtab[pitch], sound);
267 if (fill != LEGATO)
268 rest(silence);
269 }
270}
271
272/* interpret and play an item from a notation string */
273static void
274playstring(cp, slen)
275 char *cp;
276 size_t slen;
277{
278 int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
279
280#define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
281 {v = v * 10 + (*++cp - '0'); slen--;}
282 for (; slen--; cp++)
283 {
284 int sustain, timeval, tempo;
285 register char c = toupper(*cp);
286
287#ifdef DEBUG
288 (void) printf("playstring: %c (%x)\n", c, c);
289#endif /* DEBUG */
290
291 switch (c)
292 {
293 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
294
295 /* compute pitch */
296 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
297
298 /* this may be followed by an accidental sign */
299 if (cp[1] == '#' || cp[1] == '+')
300 {
301 ++pitch;
302 ++cp;
303 slen--;
304 }
305 else if (cp[1] == '-')
306 {
307 --pitch;
308 ++cp;
309 slen--;
310 }
311
312 /*
313 * If octave-tracking mode is on, and there has been no octave-
314 * setting prefix, find the version of the current letter note
315 * closest to the last regardless of octave.
316 */
317 if (octtrack && !octprefix)
318 {
319 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
320 {
321 ++octave;
322 pitch += OCTAVE_NOTES;
323 }
324
325 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
326 {
327 --octave;
328 pitch -= OCTAVE_NOTES;
329 }
330 }
331 octprefix = FALSE;
332 lastpitch = pitch;
333
334 /* ...which may in turn be followed by an override time value */
335 GETNUM(cp, timeval);
336 if (timeval <= 0 || timeval > MIN_VALUE)
337 timeval = value;
338
339 /* ...and/or sustain dots */
340 for (sustain = 0; cp[1] == '.'; cp++)
341 {
342 slen--;
343 sustain++;
344 }
345
346 /* ...and/or a slur mark */
347 oldfill = fill;
348 if (cp[1] == '_')
349 {
350 fill = LEGATO;
351 ++cp;
352 slen--;
353 }
354
355 /* time to emit the actual tone */
356 playtone(pitch, timeval, sustain);
357
358 fill = oldfill;
359 break;
360
361 case 'O':
362 if (cp[1] == 'N' || cp[1] == 'n')
363 {
364 octprefix = octtrack = FALSE;
365 ++cp;
366 slen--;
367 }
368 else if (cp[1] == 'L' || cp[1] == 'l')
369 {
370 octtrack = TRUE;
371 ++cp;
372 slen--;
373 }
374 else
375 {
376 GETNUM(cp, octave);
377 if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES)
378 octave = DFLT_OCTAVE;
379 octprefix = TRUE;
380 }
381 break;
382
383 case '>':
384 if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1)
385 octave++;
386 octprefix = TRUE;
387 break;
388
389 case '<':
390 if (octave > 0)
391 octave--;
392 octprefix = TRUE;
393 break;
394
395 case 'N':
396 GETNUM(cp, pitch);
397 for (sustain = 0; cp[1] == '.'; cp++)
398 {
399 slen--;
400 sustain++;
401 }
402 oldfill = fill;
403 if (cp[1] == '_')
404 {
405 fill = LEGATO;
406 ++cp;
407 slen--;
408 }
409 playtone(pitch - 1, value, sustain);
410 fill = oldfill;
411 break;
412
413 case 'L':
414 GETNUM(cp, value);
415 if (value <= 0 || value > MIN_VALUE)
416 value = DFLT_VALUE;
417 break;
418
419 case 'P':
420 case '~':
421 /* this may be followed by an override time value */
422 GETNUM(cp, timeval);
423 if (timeval <= 0 || timeval > MIN_VALUE)
424 timeval = value;
425 for (sustain = 0; cp[1] == '.'; cp++)
426 {
427 slen--;
428 sustain++;
429 }
430 playtone(-1, timeval, sustain);
431 break;
432
433 case 'T':
434 GETNUM(cp, tempo);
435 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
436 tempo = DFLT_TEMPO;
437 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
438 break;
439
440 case 'M':
441 if (cp[1] == 'N' || cp[1] == 'n')
442 {
443 fill = NORMAL;
444 ++cp;
445 slen--;
446 }
447 else if (cp[1] == 'L' || cp[1] == 'l')
448 {
449 fill = LEGATO;
450 ++cp;
451 slen--;
452 }
453 else if (cp[1] == 'S' || cp[1] == 's')
454 {
455 fill = STACCATO;
456 ++cp;
457 slen--;
458 }
459 break;
460 }
461 }
462}
463
464/******************* UNIX DRIVER HOOKS BEGIN HERE **************************
465 *
466 * This section implements driver hooks to run playstring() and the tone(),
467 * endtone(), and rest() functions defined above.
468 */
469
470static int spkr_active = FALSE; /* exclusion flag */
471static char *spkr_inbuf; /* incoming buf */
472
473static int
474spkropen(dev, flags, fmt, td)
475 struct cdev *dev;
476 int flags;
477 int fmt;
478 struct thread *td;
479{
480#ifdef DEBUG
481 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
482#endif /* DEBUG */
483
484 if (minor(dev) != 0)
485 return(ENXIO);
486 else if (spkr_active)
487 return(EBUSY);
488 else
489 {
490#ifdef DEBUG
491 (void) printf("spkropen: about to perform play initialization\n");
492#endif /* DEBUG */
493 playinit();
494 spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
495 spkr_active = TRUE;
496 return(0);
497 }
498}
499
500static int
501spkrwrite(dev, uio, ioflag)
502 struct cdev *dev;
503 struct uio *uio;
504 int ioflag;
505{
506#ifdef DEBUG
507 printf("spkrwrite: entering with dev = %s, count = %d\n",
508 devtoname(dev), uio->uio_resid);
509#endif /* DEBUG */
510
511 if (minor(dev) != 0)
512 return(ENXIO);
513 else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */
514 return(E2BIG);
515 else
516 {
517 unsigned n;
518 char *cp;
519 int error;
520
521 n = uio->uio_resid;
522 cp = spkr_inbuf;
523 error = uiomove(cp, n, uio);
524 if (!error) {
525 cp[n] = '\0';
526 playstring(cp, n);
527 }
528 return(error);
529 }
530}
531
532static int
533spkrclose(dev, flags, fmt, td)
534 struct cdev *dev;
535 int flags;
536 int fmt;
537 struct thread *td;
538{
539#ifdef DEBUG
540 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
541#endif /* DEBUG */
542
543 if (minor(dev) != 0)
544 return(ENXIO);
545 else
546 {
547 wakeup(&endtone);
548 wakeup(&endrest);
549 free(spkr_inbuf, M_SPKR);
550 spkr_active = FALSE;
551 return(0);
552 }
553}
554
555static int
556spkrioctl(dev, cmd, cmdarg, flags, td)
557 struct cdev *dev;
558 unsigned long cmd;
559 caddr_t cmdarg;
560 int flags;
561 struct thread *td;
562{
563#ifdef DEBUG
564 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
565 devtoname(dev), cmd);
566#endif /* DEBUG */
567
568 if (minor(dev) != 0)
569 return(ENXIO);
570 else if (cmd == SPKRTONE)
571 {
572 tone_t *tp = (tone_t *)cmdarg;
573
574 if (tp->frequency == 0)
575 rest(tp->duration);
576 else
577 tone(tp->frequency, tp->duration);
578 return 0;
579 }
580 else if (cmd == SPKRTUNE)
581 {
582 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg);
583 tone_t ttp;
584 int error;
585
586 for (; ; tp++) {
587 error = copyin(tp, &ttp, sizeof(tone_t));
588 if (error)
589 return(error);
590 if (ttp.duration == 0)
591 break;
592 if (ttp.frequency == 0)
593 rest(ttp.duration);
594 else
595 tone(ttp.frequency, ttp.duration);
596 }
597 return(0);
598 }
599 return(EINVAL);
600}
601
602/*
603 * Install placeholder to claim the resources owned by the
604 * AT tone generator.
605 */
606static struct isa_pnp_id speaker_ids[] = {
607#ifndef PC98
608 { 0x0008d041 /* PNP0800 */, SPKR_DESC },
609#endif
610 { 0 }
611};
612
613static struct cdev *speaker_dev;
614
615static int
616speaker_probe(device_t dev)
617{
618 int error;
619
620 error = ISA_PNP_PROBE(device_get_parent(dev), dev, speaker_ids);
621
622 /* PnP match */
623 if (error == 0)
624 return (0);
625
626 /* No match */
627 if (error == ENXIO)
628 return (ENXIO);
629
630 /* Not configured by hints. */
631 if (strncmp(device_get_name(dev), "speaker", 9))
632 return (ENXIO);
633
634 device_set_desc(dev, SPKR_DESC);
635
636 return (0);
637}
638
639static int
640speaker_attach(device_t dev)
641{
642
643 if (speaker_dev) {
644 device_printf(dev, "Already attached!\n");
645 return (ENXIO);
646 }
647
648 speaker_dev = make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
649 "speaker");
650 return (0);
651}
652
653static int
654speaker_detach(device_t dev)
655{
656 destroy_dev(speaker_dev);
657 return (0);
658}
659
660static device_method_t speaker_methods[] = {
661 /* Device interface */
662 DEVMETHOD(device_probe, speaker_probe),
663 DEVMETHOD(device_attach, speaker_attach),
664 DEVMETHOD(device_detach, speaker_detach),
665 DEVMETHOD(device_shutdown, bus_generic_shutdown),
666 DEVMETHOD(device_suspend, bus_generic_suspend),
667 DEVMETHOD(device_resume, bus_generic_resume),
668 { 0, 0 }
669};
670
671static driver_t speaker_driver = {
672 "speaker",
673 speaker_methods,
674 1, /* no softc */
675};
676
677static devclass_t speaker_devclass;
678
679DRIVER_MODULE(speaker, isa, speaker_driver, speaker_devclass, 0, 0);
680#ifndef PC98
681DRIVER_MODULE(speaker, acpi, speaker_driver, speaker_devclass, 0, 0);
682#endif
683
684/* spkr.c ends here */