Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / isa / spkr.c
blob6e9807b4ad112bac0963f7a5a18a8744e4b62d19
1 /* $NetBSD: spkr.c,v 1.29 2009/01/11 10:43:06 cegger Exp $ */
3 /*
4 * Copyright (c) 1990 Eric S. Raymond (esr@snark.thyrsus.com)
5 * Copyright (c) 1990 Andrew A. Chernov (ache@astral.msk.su)
6 * Copyright (c) 1990 Lennart Augustsson (lennart@augustsson.net)
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Eric S. Raymond
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * spkr.c -- device driver for console speaker on 80386
39 * v1.1 by Eric S. Raymond (esr@snark.thyrsus.com) Feb 1990
40 * modified for 386bsd by Andrew A. Chernov <ache@astral.msk.su>
41 * 386bsd only clean version, all SYSV stuff removed
42 * use hz value from param.c
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.29 2009/01/11 10:43:06 cegger Exp $");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53 #include <sys/malloc.h>
54 #include <sys/uio.h>
55 #include <sys/proc.h>
56 #include <sys/ioctl.h>
57 #include <sys/conf.h>
59 #include <sys/bus.h>
61 #include <dev/isa/pcppivar.h>
63 #include <dev/isa/spkrio.h>
65 int spkrprobe(device_t, cfdata_t, void *);
66 void spkrattach(device_t, device_t, void *);
68 CFATTACH_DECL_NEW(spkr, 0,
69 spkrprobe, spkrattach, NULL, NULL);
71 dev_type_open(spkropen);
72 dev_type_close(spkrclose);
73 dev_type_write(spkrwrite);
74 dev_type_ioctl(spkrioctl);
76 const struct cdevsw spkr_cdevsw = {
77 spkropen, spkrclose, noread, spkrwrite, spkrioctl,
78 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
81 static pcppi_tag_t ppicookie;
83 #define SPKRPRI (PZERO - 1)
85 static void tone(u_int, u_int);
86 static void rest(int);
87 static void playinit(void);
88 static void playtone(int, int, int);
89 static void playstring(char *, int);
91 static
92 void tone(xhz, ticks)
93 /* emit tone of frequency hz for given number of ticks */
94 u_int xhz, ticks;
96 pcppi_bell(ppicookie, xhz, ticks, PCPPI_BELL_SLEEP);
99 static void
100 rest(ticks)
101 /* rest for given number of ticks */
102 int ticks;
105 * Set timeout to endrest function, then give up the timeslice.
106 * This is so other processes can execute while the rest is being
107 * waited out.
109 #ifdef SPKRDEBUG
110 printf("rest: %d\n", ticks);
111 #endif /* SPKRDEBUG */
112 if (ticks > 0)
113 tsleep(rest, SPKRPRI | PCATCH, "rest", ticks);
116 /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
118 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
119 * M[LNS] are missing and the ~ synonym and octave-tracking facility is added.
120 * Requires tone(), rest(), and endtone(). String play is not interruptible
121 * except possibly at physical block boundaries.
124 #define dtoi(c) ((c) - '0')
126 static int octave; /* currently selected octave */
127 static int whole; /* whole-note time at current tempo, in ticks */
128 static int value; /* whole divisor for note time, quarter note = 1 */
129 static int fill; /* controls spacing of notes */
130 static bool octtrack; /* octave-tracking on? */
131 static bool octprefix; /* override current octave-tracking state? */
134 * Magic number avoidance...
136 #define SECS_PER_MIN 60 /* seconds per minute */
137 #define WHOLE_NOTE 4 /* quarter notes per whole note */
138 #define MIN_VALUE 64 /* the most we can divide a note by */
139 #define DFLT_VALUE 4 /* default value (quarter-note) */
140 #define FILLTIME 8 /* for articulation, break note in parts */
141 #define STACCATO 6 /* 6/8 = 3/4 of note is filled */
142 #define NORMAL 7 /* 7/8ths of note interval is filled */
143 #define LEGATO 8 /* all of note interval is filled */
144 #define DFLT_OCTAVE 4 /* default octave */
145 #define MIN_TEMPO 32 /* minimum tempo */
146 #define DFLT_TEMPO 120 /* default tempo */
147 #define MAX_TEMPO 255 /* max tempo */
148 #define NUM_MULT 3 /* numerator of dot multiplier */
149 #define DENOM_MULT 2 /* denominator of dot multiplier */
151 /* letter to half-tone: A B C D E F G */
152 static const int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
155 * This is the American Standard A440 Equal-Tempered scale with frequencies
156 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
157 * our octave 0 is standard octave 2.
159 #define OCTAVE_NOTES 12 /* semitones per octave */
160 static const int pitchtab[] =
162 /* C C# D D# E F F# G G# A A# B*/
163 /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
164 /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
165 /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
166 /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
167 /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
168 /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
169 /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
171 #define NOCTAVES (__arraycount(pitchtab) / OCTAVE_NOTES)
173 static void
174 playinit(void)
176 octave = DFLT_OCTAVE;
177 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
178 fill = NORMAL;
179 value = DFLT_VALUE;
180 octtrack = false;
181 octprefix = true; /* act as though there was an initial O(n) */
184 static void
185 playtone(pitch, val, sustain)
186 /* play tone of proper duration for current rhythm signature */
187 int pitch, val, sustain;
189 int sound, silence, snum = 1, sdenom = 1;
191 /* this weirdness avoids floating-point arithmetic */
192 for (; sustain; sustain--)
194 snum *= NUM_MULT;
195 sdenom *= DENOM_MULT;
198 if (pitch == -1)
199 rest(whole * snum / (val * sdenom));
200 else
202 sound = (whole * snum) / (val * sdenom)
203 - (whole * (FILLTIME - fill)) / (val * FILLTIME);
204 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * val * sdenom);
206 #ifdef SPKRDEBUG
207 printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
208 pitch, sound, silence);
209 #endif /* SPKRDEBUG */
211 tone(pitchtab[pitch], sound);
212 if (fill != LEGATO)
213 rest(silence);
217 static void
218 playstring(cp, slen)
219 /* interpret and play an item from a notation string */
220 char *cp;
221 int slen;
223 int pitch, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
225 #define GETNUM(cp, v) for(v=0; slen > 0 && isdigit(cp[1]); ) \
226 {v = v * 10 + (*++cp - '0'); slen--;}
227 for (; slen--; cp++)
229 int sustain, timeval, tempo;
230 char c = toupper(*cp);
232 #ifdef SPKRDEBUG
233 printf("playstring: %c (%x)\n", c, c);
234 #endif /* SPKRDEBUG */
236 switch (c)
238 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
240 /* compute pitch */
241 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
243 /* this may be followed by an accidental sign */
244 if (slen > 0 && (cp[1] == '#' || cp[1] == '+'))
246 ++pitch;
247 ++cp;
248 slen--;
250 else if (slen > 0 && cp[1] == '-')
252 --pitch;
253 ++cp;
254 slen--;
258 * If octave-tracking mode is on, and there has been no octave-
259 * setting prefix, find the version of the current letter note
260 * closest to the last regardless of octave.
262 if (octtrack && !octprefix)
264 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
266 if (octave < NOCTAVES - 1) {
267 ++octave;
268 pitch += OCTAVE_NOTES;
272 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
274 if (octave > 0) {
275 --octave;
276 pitch -= OCTAVE_NOTES;
280 octprefix = false;
281 lastpitch = pitch;
283 /* ...which may in turn be followed by an override time value */
284 GETNUM(cp, timeval);
285 if (timeval <= 0 || timeval > MIN_VALUE)
286 timeval = value;
288 /* ...and/or sustain dots */
289 for (sustain = 0; slen > 0 && cp[1] == '.'; cp++)
291 slen--;
292 sustain++;
295 /* time to emit the actual tone */
296 playtone(pitch, timeval, sustain);
297 break;
299 case 'O':
300 if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n'))
302 octprefix = octtrack = false;
303 ++cp;
304 slen--;
306 else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l'))
308 octtrack = true;
309 ++cp;
310 slen--;
312 else
314 GETNUM(cp, octave);
315 if (octave >= NOCTAVES)
316 octave = DFLT_OCTAVE;
317 octprefix = true;
319 break;
321 case '>':
322 if (octave < NOCTAVES - 1)
323 octave++;
324 octprefix = true;
325 break;
327 case '<':
328 if (octave > 0)
329 octave--;
330 octprefix = true;
331 break;
333 case 'N':
334 GETNUM(cp, pitch);
335 for (sustain = 0; slen > 0 && cp[1] == '.'; cp++)
337 slen--;
338 sustain++;
340 playtone(pitch - 1, value, sustain);
341 break;
343 case 'L':
344 GETNUM(cp, value);
345 if (value <= 0 || value > MIN_VALUE)
346 value = DFLT_VALUE;
347 break;
349 case 'P':
350 case '~':
351 /* this may be followed by an override time value */
352 GETNUM(cp, timeval);
353 if (timeval <= 0 || timeval > MIN_VALUE)
354 timeval = value;
355 for (sustain = 0; slen > 0 && cp[1] == '.'; cp++)
357 slen--;
358 sustain++;
360 playtone(-1, timeval, sustain);
361 break;
363 case 'T':
364 GETNUM(cp, tempo);
365 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
366 tempo = DFLT_TEMPO;
367 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
368 break;
370 case 'M':
371 if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n'))
373 fill = NORMAL;
374 ++cp;
375 slen--;
377 else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l'))
379 fill = LEGATO;
380 ++cp;
381 slen--;
383 else if (slen > 0 && (cp[1] == 'S' || cp[1] == 's'))
385 fill = STACCATO;
386 ++cp;
387 slen--;
389 break;
394 /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
396 * This section implements driver hooks to run playstring() and the tone(),
397 * endtone(), and rest() functions defined above.
400 static int spkr_active; /* exclusion flag */
401 static void *spkr_inbuf;
403 static int spkr_attached = 0;
406 spkrprobe(device_t parent, cfdata_t match, void *aux)
408 return (!spkr_attached);
411 void
412 spkrattach(device_t parent, device_t self, void *aux)
414 printf("\n");
415 ppicookie = ((struct pcppi_attach_args *)aux)->pa_cookie;
416 spkr_attached = 1;
417 if (!device_pmf_is_registered(self))
418 if (!pmf_device_register(self, NULL, NULL))
419 aprint_error_dev(self,
420 "couldn't establish power handler\n");
425 spkropen(dev_t dev, int flags, int mode, struct lwp *l)
427 #ifdef SPKRDEBUG
428 printf("spkropen: entering with dev = %"PRIx64"\n", dev);
429 #endif /* SPKRDEBUG */
431 if (minor(dev) != 0 || !spkr_attached)
432 return(ENXIO);
433 else if (spkr_active)
434 return(EBUSY);
435 else
437 playinit();
438 spkr_inbuf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
439 spkr_active = 1;
441 return(0);
445 spkrwrite(dev_t dev, struct uio *uio, int flags)
447 int n;
448 int error;
449 #ifdef SPKRDEBUG
450 printf("spkrwrite: entering with dev = %"PRIx64", count = %d\n",
451 dev, uio->uio_resid);
452 #endif /* SPKRDEBUG */
454 if (minor(dev) != 0)
455 return(ENXIO);
456 else
458 n = min(DEV_BSIZE, uio->uio_resid);
459 error = uiomove(spkr_inbuf, n, uio);
460 if (!error)
461 playstring((char *)spkr_inbuf, n);
462 return(error);
466 int spkrclose(dev_t dev, int flags, int mode,
467 struct lwp *l)
469 #ifdef SPKRDEBUG
470 printf("spkrclose: entering with dev = %"PRIx64"\n", dev);
471 #endif /* SPKRDEBUG */
473 if (minor(dev) != 0)
474 return(ENXIO);
475 else
477 tone(0, 0);
478 free(spkr_inbuf, M_DEVBUF);
479 spkr_active = 0;
481 return(0);
484 int spkrioctl(dev_t dev, u_long cmd, void *data, int flag,
485 struct lwp *l)
487 #ifdef SPKRDEBUG
488 printf("spkrioctl: entering with dev = %"PRIx64", cmd = %lx\n", dev, cmd);
489 #endif /* SPKRDEBUG */
491 if (minor(dev) != 0)
492 return(ENXIO);
493 else if (cmd == SPKRTONE)
495 tone_t *tp = (tone_t *)data;
497 if (tp->frequency == 0)
498 rest(tp->duration);
499 else
500 tone(tp->frequency, tp->duration);
502 else if (cmd == SPKRTUNE)
504 tone_t *tp = (tone_t *)(*(void **)data);
505 tone_t ttp;
506 int error;
508 for (; ; tp++) {
509 error = copyin(tp, &ttp, sizeof(tone_t));
510 if (error)
511 return(error);
512 if (ttp.duration == 0)
513 break;
514 if (ttp.frequency == 0)
515 rest(ttp.duration);
516 else
517 tone(ttp.frequency, ttp.duration);
520 else
521 return(EINVAL);
522 return(0);
525 /* spkr.c ends here */