FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/spkr.c
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>
10 __FBSDID("$FreeBSD: releng/6.0/sys/i386/isa/spkr.c 146211 2005-05-14 09:10:02Z nyan $");
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 #include <machine/clock.h>
23 #include <machine/speaker.h>
24 #include <machine/ppireg.h>
25 #include <machine/timerreg.h>
26
27 static d_open_t spkropen;
28 static d_close_t spkrclose;
29 static d_write_t spkrwrite;
30 static d_ioctl_t spkrioctl;
31
32 static struct cdevsw spkr_cdevsw = {
33 .d_version = D_VERSION,
34 .d_flags = D_NEEDGIANT,
35 .d_open = spkropen,
36 .d_close = spkrclose,
37 .d_write = spkrwrite,
38 .d_ioctl = spkrioctl,
39 .d_name = "spkr",
40 };
41
42 static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
43
44 /**************** MACHINE DEPENDENT PART STARTS HERE *************************
45 *
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.
50 *
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.
55 */
56
57 #ifdef PC98
58 #define SPKR_DESC "PC98 speaker"
59 #else
60 #define SPKR_DESC "PC speaker"
61 #endif
62
63 #define SPKRPRI PSOCK
64 static char endtone, endrest;
65
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);
71
72 /* emit tone of frequency thz for given number of ticks */
73 static void
74 tone(thz, ticks)
75 unsigned int thz, ticks;
76 {
77 unsigned int divisor;
78 int sps;
79
80 if (thz <= 0)
81 return;
82
83 divisor = timer_freq / thz;
84
85 #ifdef DEBUG
86 (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
87 #endif /* DEBUG */
88
89 /* set timer to generate clicks at given frequency in Hertz */
90 sps = splclock();
91
92 if (timer_spkr_acquire()) {
93 /* enter list of waiting procs ??? */
94 splx(sps);
95 return;
96 }
97 splx(sps);
98 disable_intr();
99 spkr_set_pitch(divisor);
100 enable_intr();
101
102 /* turn the speaker on */
103 ppi_spkr_on();
104
105 /*
106 * Set timeout to endtone function, then give up the timeslice.
107 * This is so other processes can execute while the tone is being
108 * emitted.
109 */
110 if (ticks > 0)
111 tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", ticks);
112 ppi_spkr_off();
113 sps = splclock();
114 timer_spkr_release();
115 splx(sps);
116 }
117
118 /* rest for given number of ticks */
119 static void
120 rest(ticks)
121 int ticks;
122 {
123 /*
124 * Set timeout to endrest function, then give up the timeslice.
125 * This is so other processes can execute while the rest is being
126 * waited out.
127 */
128 #ifdef DEBUG
129 (void) printf("rest: %d\n", ticks);
130 #endif /* DEBUG */
131 if (ticks > 0)
132 tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", ticks);
133 }
134
135 /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
136 *
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.
142 */
143
144 typedef int bool;
145 #define TRUE 1
146 #define FALSE 0
147
148 #define dtoi(c) ((c) - '')
149
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? */
156
157 /*
158 * Magic number avoidance...
159 */
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 */
174
175 /* letter to half-tone: A B C D E F G */
176 static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
177
178 /*
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.
182 */
183 #define OCTAVE_NOTES 12 /* semitones per octave */
184 static int pitchtab[] =
185 {
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,
194 };
195
196 static void
197 playinit()
198 {
199 octave = DFLT_OCTAVE;
200 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
201 fill = NORMAL;
202 value = DFLT_VALUE;
203 octtrack = FALSE;
204 octprefix = TRUE; /* act as though there was an initial O(n) */
205 }
206
207 /* play tone of proper duration for current rhythm signature */
208 static void
209 playtone(pitch, value, sustain)
210 int pitch, value, sustain;
211 {
212 register int sound, silence, snum = 1, sdenom = 1;
213
214 /* this weirdness avoids floating-point arithmetic */
215 for (; sustain; sustain--)
216 {
217 /* See the BUGS section in the man page for discussion */
218 snum *= NUM_MULT;
219 sdenom *= DENOM_MULT;
220 }
221
222 if (value == 0 || sdenom == 0)
223 return;
224
225 if (pitch == -1)
226 rest(whole * snum / (value * sdenom));
227 else
228 {
229 sound = (whole * snum) / (value * sdenom)
230 - (whole * (FILLTIME - fill)) / (value * FILLTIME);
231 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
232
233 #ifdef DEBUG
234 (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
235 pitch, sound, silence);
236 #endif /* DEBUG */
237
238 tone(pitchtab[pitch], sound);
239 if (fill != LEGATO)
240 rest(silence);
241 }
242 }
243
244 /* interpret and play an item from a notation string */
245 static void
246 playstring(cp, slen)
247 char *cp;
248 size_t slen;
249 {
250 int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
251
252 #define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
253 {v = v * 10 + (*++cp - ''); slen--;}
254 for (; slen--; cp++)
255 {
256 int sustain, timeval, tempo;
257 register char c = toupper(*cp);
258
259 #ifdef DEBUG
260 (void) printf("playstring: %c (%x)\n", c, c);
261 #endif /* DEBUG */
262
263 switch (c)
264 {
265 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
266
267 /* compute pitch */
268 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
269
270 /* this may be followed by an accidental sign */
271 if (cp[1] == '#' || cp[1] == '+')
272 {
273 ++pitch;
274 ++cp;
275 slen--;
276 }
277 else if (cp[1] == '-')
278 {
279 --pitch;
280 ++cp;
281 slen--;
282 }
283
284 /*
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.
288 */
289 if (octtrack && !octprefix)
290 {
291 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
292 {
293 ++octave;
294 pitch += OCTAVE_NOTES;
295 }
296
297 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
298 {
299 --octave;
300 pitch -= OCTAVE_NOTES;
301 }
302 }
303 octprefix = FALSE;
304 lastpitch = pitch;
305
306 /* ...which may in turn be followed by an override time value */
307 GETNUM(cp, timeval);
308 if (timeval <= 0 || timeval > MIN_VALUE)
309 timeval = value;
310
311 /* ...and/or sustain dots */
312 for (sustain = 0; cp[1] == '.'; cp++)
313 {
314 slen--;
315 sustain++;
316 }
317
318 /* ...and/or a slur mark */
319 oldfill = fill;
320 if (cp[1] == '_')
321 {
322 fill = LEGATO;
323 ++cp;
324 slen--;
325 }
326
327 /* time to emit the actual tone */
328 playtone(pitch, timeval, sustain);
329
330 fill = oldfill;
331 break;
332
333 case 'O':
334 if (cp[1] == 'N' || cp[1] == 'n')
335 {
336 octprefix = octtrack = FALSE;
337 ++cp;
338 slen--;
339 }
340 else if (cp[1] == 'L' || cp[1] == 'l')
341 {
342 octtrack = TRUE;
343 ++cp;
344 slen--;
345 }
346 else
347 {
348 GETNUM(cp, octave);
349 if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES)
350 octave = DFLT_OCTAVE;
351 octprefix = TRUE;
352 }
353 break;
354
355 case '>':
356 if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1)
357 octave++;
358 octprefix = TRUE;
359 break;
360
361 case '<':
362 if (octave > 0)
363 octave--;
364 octprefix = TRUE;
365 break;
366
367 case 'N':
368 GETNUM(cp, pitch);
369 for (sustain = 0; cp[1] == '.'; cp++)
370 {
371 slen--;
372 sustain++;
373 }
374 oldfill = fill;
375 if (cp[1] == '_')
376 {
377 fill = LEGATO;
378 ++cp;
379 slen--;
380 }
381 playtone(pitch - 1, value, sustain);
382 fill = oldfill;
383 break;
384
385 case 'L':
386 GETNUM(cp, value);
387 if (value <= 0 || value > MIN_VALUE)
388 value = DFLT_VALUE;
389 break;
390
391 case 'P':
392 case '~':
393 /* this may be followed by an override time value */
394 GETNUM(cp, timeval);
395 if (timeval <= 0 || timeval > MIN_VALUE)
396 timeval = value;
397 for (sustain = 0; cp[1] == '.'; cp++)
398 {
399 slen--;
400 sustain++;
401 }
402 playtone(-1, timeval, sustain);
403 break;
404
405 case 'T':
406 GETNUM(cp, tempo);
407 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
408 tempo = DFLT_TEMPO;
409 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
410 break;
411
412 case 'M':
413 if (cp[1] == 'N' || cp[1] == 'n')
414 {
415 fill = NORMAL;
416 ++cp;
417 slen--;
418 }
419 else if (cp[1] == 'L' || cp[1] == 'l')
420 {
421 fill = LEGATO;
422 ++cp;
423 slen--;
424 }
425 else if (cp[1] == 'S' || cp[1] == 's')
426 {
427 fill = STACCATO;
428 ++cp;
429 slen--;
430 }
431 break;
432 }
433 }
434 }
435
436 /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
437 *
438 * This section implements driver hooks to run playstring() and the tone(),
439 * endtone(), and rest() functions defined above.
440 */
441
442 static int spkr_active = FALSE; /* exclusion flag */
443 static char *spkr_inbuf; /* incoming buf */
444
445 static int
446 spkropen(dev, flags, fmt, td)
447 struct cdev *dev;
448 int flags;
449 int fmt;
450 struct thread *td;
451 {
452 #ifdef DEBUG
453 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
454 #endif /* DEBUG */
455
456 if (minor(dev) != 0)
457 return(ENXIO);
458 else if (spkr_active)
459 return(EBUSY);
460 else
461 {
462 #ifdef DEBUG
463 (void) printf("spkropen: about to perform play initialization\n");
464 #endif /* DEBUG */
465 playinit();
466 spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
467 spkr_active = TRUE;
468 return(0);
469 }
470 }
471
472 static int
473 spkrwrite(dev, uio, ioflag)
474 struct cdev *dev;
475 struct uio *uio;
476 int ioflag;
477 {
478 #ifdef DEBUG
479 printf("spkrwrite: entering with dev = %s, count = %d\n",
480 devtoname(dev), uio->uio_resid);
481 #endif /* DEBUG */
482
483 if (minor(dev) != 0)
484 return(ENXIO);
485 else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */
486 return(E2BIG);
487 else
488 {
489 unsigned n;
490 char *cp;
491 int error;
492
493 n = uio->uio_resid;
494 cp = spkr_inbuf;
495 error = uiomove(cp, n, uio);
496 if (!error) {
497 cp[n] = '\0';
498 playstring(cp, n);
499 }
500 return(error);
501 }
502 }
503
504 static int
505 spkrclose(dev, flags, fmt, td)
506 struct cdev *dev;
507 int flags;
508 int fmt;
509 struct thread *td;
510 {
511 #ifdef DEBUG
512 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
513 #endif /* DEBUG */
514
515 if (minor(dev) != 0)
516 return(ENXIO);
517 else
518 {
519 wakeup(&endtone);
520 wakeup(&endrest);
521 free(spkr_inbuf, M_SPKR);
522 spkr_active = FALSE;
523 return(0);
524 }
525 }
526
527 static int
528 spkrioctl(dev, cmd, cmdarg, flags, td)
529 struct cdev *dev;
530 unsigned long cmd;
531 caddr_t cmdarg;
532 int flags;
533 struct thread *td;
534 {
535 #ifdef DEBUG
536 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
537 devtoname(dev), cmd);
538 #endif /* DEBUG */
539
540 if (minor(dev) != 0)
541 return(ENXIO);
542 else if (cmd == SPKRTONE)
543 {
544 tone_t *tp = (tone_t *)cmdarg;
545
546 if (tp->frequency == 0)
547 rest(tp->duration);
548 else
549 tone(tp->frequency, tp->duration);
550 return 0;
551 }
552 else if (cmd == SPKRTUNE)
553 {
554 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg);
555 tone_t ttp;
556 int error;
557
558 for (; ; tp++) {
559 error = copyin(tp, &ttp, sizeof(tone_t));
560 if (error)
561 return(error);
562 if (ttp.duration == 0)
563 break;
564 if (ttp.frequency == 0)
565 rest(ttp.duration);
566 else
567 tone(ttp.frequency, ttp.duration);
568 }
569 return(0);
570 }
571 return(EINVAL);
572 }
573
574 /*
575 * Install placeholder to claim the resources owned by the
576 * AT tone generator.
577 */
578 static struct isa_pnp_id speaker_ids[] = {
579 #ifndef PC98
580 { 0x0008d041 /* PNP0800 */, SPKR_DESC },
581 #endif
582 { 0 }
583 };
584
585 static struct cdev *speaker_dev;
586
587 static int
588 speaker_probe(device_t dev)
589 {
590 int error;
591
592 error = ISA_PNP_PROBE(device_get_parent(dev), dev, speaker_ids);
593
594 /* PnP match */
595 if (error == 0)
596 return (0);
597
598 /* No match */
599 if (error == ENXIO)
600 return (ENXIO);
601
602 /* Not configured by hints. */
603 if (strncmp(device_get_name(dev), "speaker", 9))
604 return (ENXIO);
605
606 device_set_desc(dev, SPKR_DESC);
607
608 return (0);
609 }
610
611 static int
612 speaker_attach(device_t dev)
613 {
614
615 if (speaker_dev) {
616 device_printf(dev, "Already attached!\n");
617 return (ENXIO);
618 }
619
620 speaker_dev = make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
621 "speaker");
622 return (0);
623 }
624
625 static int
626 speaker_detach(device_t dev)
627 {
628 destroy_dev(speaker_dev);
629 return (0);
630 }
631
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),
640 { 0, 0 }
641 };
642
643 static driver_t speaker_driver = {
644 "speaker",
645 speaker_methods,
646 1, /* no softc */
647 };
648
649 static devclass_t speaker_devclass;
650
651 DRIVER_MODULE(speaker, isa, speaker_driver, speaker_devclass, 0, 0);
652 #ifndef PC98
653 DRIVER_MODULE(speaker, acpi, speaker_driver, speaker_devclass, 0, 0);
654 #endif
655
656 /* spkr.c ends here */
Cache object: 66b472cd5bae06a97523f26dba935e9f
|