1 /* $NetBSD: spkr.c,v 1.29 2009/01/11 10:43:06 cegger Exp $ */
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)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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>
56 #include <sys/ioctl.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);
93 /* emit tone of frequency hz for given number of ticks */
96 pcppi_bell(ppicookie
, xhz
, ticks
, PCPPI_BELL_SLEEP
);
101 /* rest for given number of 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
110 printf("rest: %d\n", ticks
);
111 #endif /* SPKRDEBUG */
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)
176 octave
= DFLT_OCTAVE
;
177 whole
= (hz
* SECS_PER_MIN
* WHOLE_NOTE
) / DFLT_TEMPO
;
181 octprefix
= true; /* act as though there was an initial O(n) */
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
--)
195 sdenom
*= DENOM_MULT
;
199 rest(whole
* snum
/ (val
* sdenom
));
202 sound
= (whole
* snum
) / (val
* sdenom
)
203 - (whole
* (FILLTIME
- fill
)) / (val
* FILLTIME
);
204 silence
= whole
* (FILLTIME
-fill
) * snum
/ (FILLTIME
* val
* sdenom
);
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
);
219 /* interpret and play an item from a notation string */
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--;}
229 int sustain
, timeval
, tempo
;
230 char c
= toupper(*cp
);
233 printf("playstring: %c (%x)\n", c
, c
);
234 #endif /* SPKRDEBUG */
238 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
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] == '+'))
250 else if (slen
> 0 && cp
[1] == '-')
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) {
268 pitch
+= OCTAVE_NOTES
;
272 if (abs(pitch
-lastpitch
) > abs((pitch
-OCTAVE_NOTES
)-lastpitch
))
276 pitch
-= OCTAVE_NOTES
;
283 /* ...which may in turn be followed by an override time value */
285 if (timeval
<= 0 || timeval
> MIN_VALUE
)
288 /* ...and/or sustain dots */
289 for (sustain
= 0; slen
> 0 && cp
[1] == '.'; cp
++)
295 /* time to emit the actual tone */
296 playtone(pitch
, timeval
, sustain
);
300 if (slen
> 0 && (cp
[1] == 'N' || cp
[1] == 'n'))
302 octprefix
= octtrack
= false;
306 else if (slen
> 0 && (cp
[1] == 'L' || cp
[1] == 'l'))
315 if (octave
>= NOCTAVES
)
316 octave
= DFLT_OCTAVE
;
322 if (octave
< NOCTAVES
- 1)
335 for (sustain
= 0; slen
> 0 && cp
[1] == '.'; cp
++)
340 playtone(pitch
- 1, value
, sustain
);
345 if (value
<= 0 || value
> MIN_VALUE
)
351 /* this may be followed by an override time value */
353 if (timeval
<= 0 || timeval
> MIN_VALUE
)
355 for (sustain
= 0; slen
> 0 && cp
[1] == '.'; cp
++)
360 playtone(-1, timeval
, sustain
);
365 if (tempo
< MIN_TEMPO
|| tempo
> MAX_TEMPO
)
367 whole
= (hz
* SECS_PER_MIN
* WHOLE_NOTE
) / tempo
;
371 if (slen
> 0 && (cp
[1] == 'N' || cp
[1] == 'n'))
377 else if (slen
> 0 && (cp
[1] == 'L' || cp
[1] == 'l'))
383 else if (slen
> 0 && (cp
[1] == 'S' || cp
[1] == 's'))
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
);
412 spkrattach(device_t parent
, device_t self
, void *aux
)
415 ppicookie
= ((struct pcppi_attach_args
*)aux
)->pa_cookie
;
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
)
428 printf("spkropen: entering with dev = %"PRIx64
"\n", dev
);
429 #endif /* SPKRDEBUG */
431 if (minor(dev
) != 0 || !spkr_attached
)
433 else if (spkr_active
)
438 spkr_inbuf
= malloc(DEV_BSIZE
, M_DEVBUF
, M_WAITOK
);
445 spkrwrite(dev_t dev
, struct uio
*uio
, int flags
)
450 printf("spkrwrite: entering with dev = %"PRIx64
", count = %d\n",
451 dev
, uio
->uio_resid
);
452 #endif /* SPKRDEBUG */
458 n
= min(DEV_BSIZE
, uio
->uio_resid
);
459 error
= uiomove(spkr_inbuf
, n
, uio
);
461 playstring((char *)spkr_inbuf
, n
);
466 int spkrclose(dev_t dev
, int flags
, int mode
,
470 printf("spkrclose: entering with dev = %"PRIx64
"\n", dev
);
471 #endif /* SPKRDEBUG */
478 free(spkr_inbuf
, M_DEVBUF
);
484 int spkrioctl(dev_t dev
, u_long cmd
, void *data
, int flag
,
488 printf("spkrioctl: entering with dev = %"PRIx64
", cmd = %lx\n", dev
, cmd
);
489 #endif /* SPKRDEBUG */
493 else if (cmd
== SPKRTONE
)
495 tone_t
*tp
= (tone_t
*)data
;
497 if (tp
->frequency
== 0)
500 tone(tp
->frequency
, tp
->duration
);
502 else if (cmd
== SPKRTUNE
)
504 tone_t
*tp
= (tone_t
*)(*(void **)data
);
509 error
= copyin(tp
, &ttp
, sizeof(tone_t
));
512 if (ttp
.duration
== 0)
514 if (ttp
.frequency
== 0)
517 tone(ttp
.frequency
, ttp
.duration
);
525 /* spkr.c ends here */