spkr.c revision 1.9 1 /* $NetBSD: spkr.c,v 1.9 2017/06/11 03:55:56 nat Exp $ */
2
3 /*
4 * Copyright (c) 1990 Eric S. Raymond (esr (at) snark.thyrsus.com)
5 * Copyright (c) 1990 Andrew A. Chernov (ache (at) astral.msk.su)
6 * Copyright (c) 1990 Lennart Augustsson (lennart (at) augustsson.net)
7 * All rights reserved.
8 *
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.
22 *
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.
34 */
35
36 /*
37 * spkr.c -- device driver for console speaker on 80386
38 *
39 * v1.1 by Eric S. Raymond (esr (at) snark.thyrsus.com) Feb 1990
40 * modified for 386bsd by Andrew A. Chernov <ache (at) astral.msk.su>
41 * 386bsd only clean version, all SYSV stuff removed
42 * use hz value from param.c
43 */
44
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.9 2017/06/11 03:55:56 nat Exp $");
47
48 #include "wsmux.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/errno.h>
54 #include <sys/device.h>
55 #include <sys/malloc.h>
56 #include <sys/module.h>
57 #include <sys/uio.h>
58 #include <sys/proc.h>
59 #include <sys/ioctl.h>
60 #include <sys/conf.h>
61
62 #include <sys/bus.h>
63
64 #include <dev/spkrio.h>
65 #include <dev/spkrvar.h>
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wscons/wsbellvar.h>
68 #include <dev/wscons/wsbellmuxvar.h>
69
70 dev_type_open(spkropen);
71 dev_type_close(spkrclose);
72 dev_type_write(spkrwrite);
73 dev_type_ioctl(spkrioctl);
74
75 const struct cdevsw spkr_cdevsw = {
76 .d_open = spkropen,
77 .d_close = spkrclose,
78 .d_read = noread,
79 .d_write = spkrwrite,
80 .d_ioctl = spkrioctl,
81 .d_stop = nostop,
82 .d_tty = notty,
83 .d_poll = nopoll,
84 .d_mmap = nommap,
85 .d_kqfilter = nokqfilter,
86 .d_discard = nodiscard,
87 .d_flag = D_OTHER
88 };
89
90 static void playinit(struct spkr_softc *);
91 static void playtone(struct spkr_softc *, int, int, int);
92 static void playstring(struct spkr_softc *, const char *, size_t);
93
94 /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
95 *
96 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
97 * M[LNS] are missing and the ~ synonym and octave-tracking facility is added.
98 * Requires spkr_tone(), spkr_rest(). String play is not interruptible
99 * except possibly at physical block boundaries.
100 */
101
102 #define dtoi(c) ((c) - '0')
103
104 /*
105 * Magic number avoidance...
106 */
107 #define SECS_PER_MIN 60 /* seconds per minute */
108 #define WHOLE_NOTE 4 /* quarter notes per whole note */
109 #define MIN_VALUE 64 /* the most we can divide a note by */
110 #define DFLT_VALUE 4 /* default value (quarter-note) */
111 #define FILLTIME 8 /* for articulation, break note in parts */
112 #define STACCATO 6 /* 6/8 = 3/4 of note is filled */
113 #define NORMAL 7 /* 7/8ths of note interval is filled */
114 #define LEGATO 8 /* all of note interval is filled */
115 #define DFLT_OCTAVE 4 /* default octave */
116 #define MIN_TEMPO 32 /* minimum tempo */
117 #define DFLT_TEMPO 120 /* default tempo */
118 #define MAX_TEMPO 255 /* max tempo */
119 #define NUM_MULT 3 /* numerator of dot multiplier */
120 #define DENOM_MULT 2 /* denominator of dot multiplier */
121
122 /* letter to half-tone: A B C D E F G */
123 static const int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
124
125 /*
126 * This is the American Standard A440 Equal-Tempered scale with frequencies
127 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
128 * our octave 0 is standard octave 2.
129 */
130 #define OCTAVE_NOTES 12 /* semitones per octave */
131 static const int pitchtab[] =
132 {
133 /* C C# D D# E F F# G G# A A# B*/
134 /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
135 /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
136 /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
137 /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
138 /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
139 /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
140 /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
141 };
142 #define NOCTAVES (int)(__arraycount(pitchtab) / OCTAVE_NOTES)
143
144 static void
145 playinit(struct spkr_softc *sc)
146 {
147 sc->sc_octave = DFLT_OCTAVE;
148 sc->sc_whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
149 sc->sc_fill = NORMAL;
150 sc->sc_value = DFLT_VALUE;
151 sc->sc_octtrack = false;
152 sc->sc_octprefix = true;/* act as though there was an initial O(n) */
153 }
154
155 /* play tone of proper duration for current rhythm signature */
156 static void
157 playtone(struct spkr_softc *sc, int pitch, int val, int sustain)
158 {
159 int sound, silence, snum = 1, sdenom = 1;
160
161 /* this weirdness avoids floating-point arithmetic */
162 for (; sustain; sustain--) {
163 snum *= NUM_MULT;
164 sdenom *= DENOM_MULT;
165 }
166
167 if (pitch == -1) {
168 (*sc->sc_rest)(sc->sc_dev, sc->sc_whole
169 * snum / (val * sdenom));
170 return;
171 }
172
173 int fac = sc->sc_whole * (FILLTIME - sc->sc_fill);
174 int fval = FILLTIME * val;
175 sound = (sc->sc_whole * snum) / (val * sdenom) - fac / fval;
176 silence = fac * snum / (fval * sdenom);
177
178 #ifdef SPKRDEBUG
179 aprint_debug_dev(sc->sc_dev,
180 "%s: pitch %d for %d ticks, rest for %d ticks\n", __func__,
181 pitch, sound, silence);
182 #endif /* SPKRDEBUG */
183
184 (*sc->sc_tone)(sc->sc_dev, pitchtab[pitch], sound);
185 if (sc->sc_fill != LEGATO)
186 (*sc->sc_rest)(sc->sc_dev, silence);
187 }
188
189 /* interpret and play an item from a notation string */
190 static void
191 playstring(struct spkr_softc *sc, const char *cp, size_t slen)
192 {
193 int pitch, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
194
195 #define GETNUM(cp, v) \
196 for (v = 0; slen > 0 && isdigit((unsigned char)cp[1]); ) { \
197 v = v * 10 + (*++cp - '0'); \
198 slen--; \
199 }
200
201 for (; slen--; cp++) {
202 int sustain, timeval, tempo;
203 char c = toupper((unsigned char)*cp);
204
205 #ifdef SPKRDEBUG
206 aprint_debug_dev(sc->sc_dev, "%s: %c (%x)\n", __func__, c, c);
207 #endif /* SPKRDEBUG */
208
209 switch (c) {
210 case 'A': case 'B': case 'C': case 'D':
211 case 'E': case 'F': case 'G':
212 /* compute pitch */
213 pitch = notetab[c - 'A'] + sc->sc_octave * OCTAVE_NOTES;
214
215 /* this may be followed by an accidental sign */
216 if (slen > 0 && (cp[1] == '#' || cp[1] == '+')) {
217 ++pitch;
218 ++cp;
219 slen--;
220 } else if (slen > 0 && cp[1] == '-') {
221 --pitch;
222 ++cp;
223 slen--;
224 }
225
226 /*
227 * If octave-tracking mode is on, and there has been no
228 * octave- setting prefix, find the version of the
229 * current letter note * closest to the last
230 * regardless of octave.
231 */
232 if (sc->sc_octtrack && !sc->sc_octprefix) {
233 int d = abs(pitch - lastpitch);
234 if (d > abs(pitch + OCTAVE_NOTES - lastpitch)) {
235 if (sc->sc_octave < NOCTAVES - 1) {
236 ++sc->sc_octave;
237 pitch += OCTAVE_NOTES;
238 }
239 }
240
241 if (d > abs(pitch - OCTAVE_NOTES - lastpitch)) {
242 if (sc->sc_octave > 0) {
243 --sc->sc_octave;
244 pitch -= OCTAVE_NOTES;
245 }
246 }
247 }
248 sc->sc_octprefix = false;
249 lastpitch = pitch;
250
251 /*
252 * ...which may in turn be followed by an override
253 * time value
254 */
255 GETNUM(cp, timeval);
256 if (timeval <= 0 || timeval > MIN_VALUE)
257 timeval = sc->sc_value;
258
259 /* ...and/or sustain dots */
260 for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) {
261 slen--;
262 sustain++;
263 }
264
265 /* time to emit the actual tone */
266 playtone(sc, pitch, timeval, sustain);
267 break;
268
269 case 'O':
270 if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) {
271 sc->sc_octprefix = sc->sc_octtrack = false;
272 ++cp;
273 slen--;
274 } else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) {
275 sc->sc_octtrack = true;
276 ++cp;
277 slen--;
278 } else {
279 GETNUM(cp, sc->sc_octave);
280 if (sc->sc_octave >= NOCTAVES)
281 sc->sc_octave = DFLT_OCTAVE;
282 sc->sc_octprefix = true;
283 }
284 break;
285
286 case '>':
287 if (sc->sc_octave < NOCTAVES - 1)
288 sc->sc_octave++;
289 sc->sc_octprefix = true;
290 break;
291
292 case '<':
293 if (sc->sc_octave > 0)
294 sc->sc_octave--;
295 sc->sc_octprefix = true;
296 break;
297
298 case 'N':
299 GETNUM(cp, pitch);
300 for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) {
301 slen--;
302 sustain++;
303 }
304 playtone(sc, pitch - 1, sc->sc_value, sustain);
305 break;
306
307 case 'L':
308 GETNUM(cp, sc->sc_value);
309 if (sc->sc_value <= 0 || sc->sc_value > MIN_VALUE)
310 sc->sc_value = DFLT_VALUE;
311 break;
312
313 case 'P':
314 case '~':
315 /* this may be followed by an override time value */
316 GETNUM(cp, timeval);
317 if (timeval <= 0 || timeval > MIN_VALUE)
318 timeval = sc->sc_value;
319 for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) {
320 slen--;
321 sustain++;
322 }
323 playtone(sc, -1, timeval, sustain);
324 break;
325
326 case 'T':
327 GETNUM(cp, tempo);
328 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
329 tempo = DFLT_TEMPO;
330 sc->sc_whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
331 break;
332
333 case 'M':
334 if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) {
335 sc->sc_fill = NORMAL;
336 ++cp;
337 slen--;
338 } else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) {
339 sc->sc_fill = LEGATO;
340 ++cp;
341 slen--;
342 } else if (slen > 0 && (cp[1] == 'S' || cp[1] == 's')) {
343 sc->sc_fill = STACCATO;
344 ++cp;
345 slen--;
346 }
347 break;
348 }
349 }
350 }
351
352 /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
353 *
354 * This section implements driver hooks to run playstring() and the spkr_tone()
355 * and spkr_rest() functions defined above.
356 */
357 extern struct cfdriver spkr_cd;
358 #define spkrenter(d) device_lookup_private(&spkr_cd, d)
359
360 void
361 spkr_attach(device_t self, void (*tone)(device_t, u_int, u_int),
362 void (*rest)(device_t, int))
363 {
364 struct spkr_softc *sc = device_private(self);
365
366 #ifdef SPKRDEBUG
367 aprint_debug("%s: entering for unit %d\n", __func__, self->dv_unit);
368 #endif /* SPKRDEBUG */
369 sc->sc_dev = self;
370 sc->sc_tone = tone;
371 sc->sc_rest = rest;
372 sc->sc_inbuf = NULL;
373
374 #if (NWSMUX > 0)
375 struct wsbelldev_attach_args a;
376
377 a.accesscookie = sc;
378 sc->sc_wsbelldev = config_found(self, &a, wsbelldevprint);
379 #endif
380 }
381
382 int
383 spkr_detach(device_t self, int flags)
384 {
385 struct spkr_softc *sc = device_private(self);
386
387 #ifdef SPKRDEBUG
388 aprint_debug("%s: entering for unit %d\n", __func__, self->dv_unit);
389 #endif /* SPKRDEBUG */
390 if (sc == NULL)
391 return ENXIO;
392 if (sc->sc_inbuf != NULL)
393 return EBUSY;
394
395 return 0;
396 }
397
398 int
399 spkropen(dev_t dev, int flags, int mode, struct lwp *l)
400 {
401 #ifdef SPKRDEBUG
402 aprint_debug("%s: entering with dev = %"PRIx64"\n", __func__, dev);
403 #endif /* SPKRDEBUG */
404 struct spkr_softc *sc = spkrenter(minor(dev));
405
406 if (sc == NULL)
407 return ENXIO;
408 if (sc->sc_inbuf != NULL)
409 return EBUSY;
410
411 sc->sc_inbuf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
412 playinit(sc);
413 return 0;
414 }
415
416 int
417 spkrwrite(dev_t dev, struct uio *uio, int flags)
418 {
419 #ifdef SPKRDEBUG
420 aprint_debug("%s: entering with dev = %"PRIx64", count = %zu\n",
421 __func__, dev, uio->uio_resid);
422 #endif /* SPKRDEBUG */
423 struct spkr_softc *sc = spkrenter(minor(dev));
424
425 if (sc == NULL)
426 return ENXIO;
427 if (sc->sc_inbuf == NULL)
428 return EINVAL;
429
430 size_t n = min(DEV_BSIZE, uio->uio_resid);
431 int error = uiomove(sc->sc_inbuf, n, uio);
432 if (error)
433 return error;
434 playstring(sc, sc->sc_inbuf, n);
435 return 0;
436 }
437
438 int
439 spkrclose(dev_t dev, int flags, int mode, struct lwp *l)
440 {
441 #ifdef SPKRDEBUG
442 aprint_debug("%s: entering with dev = %"PRIx64"\n", __func__, dev);
443 #endif /* SPKRDEBUG */
444 struct spkr_softc *sc = spkrenter(minor(dev));
445
446 if (sc == NULL)
447 return ENXIO;
448 if (sc->sc_inbuf == NULL)
449 return EINVAL;
450
451 sc->sc_tone(sc->sc_dev, 0, 0);
452 free(sc->sc_inbuf, M_DEVBUF);
453 sc->sc_inbuf = NULL;
454
455 return 0;
456 }
457
458 static void
459 playonetone(struct spkr_softc *sc, tone_t *tp)
460 {
461 if (tp->frequency == 0)
462 (*sc->sc_rest)(sc->sc_dev, tp->duration);
463 else
464 (*sc->sc_tone)(sc->sc_dev, tp->frequency, tp->duration);
465 }
466
467 int
468 spkrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
469 {
470 tone_t *tp;
471 tone_t ttp;
472 int error;
473 #ifdef SPKRDEBUG
474 aprint_debug("%s: entering with dev = %"PRIx64", cmd = %lx\n",
475 __func__, dev, cmd);
476 #endif /* SPKRDEBUG */
477
478 struct spkr_softc *sc = spkrenter(minor(dev));
479
480 if (sc == NULL)
481 return ENXIO;
482 if (sc->sc_inbuf == NULL)
483 return EINVAL;
484
485 switch (cmd) {
486 case SPKRTONE:
487 playonetone(sc, data);
488 return 0;
489 case SPKRTUNE:
490 for (tp = *(void **)data;; tp++) {
491 error = copyin(tp, &ttp, sizeof(tone_t));
492 if (error)
493 return(error);
494 if (ttp.duration == 0)
495 break;
496 playonetone(sc, &ttp);
497 }
498 return 0;
499 case SPKRGETVOL:
500 if (data != NULL)
501 *(u_int *)data = sc->sc_vol;
502 return 0;
503 case SPKRSETVOL:
504 if (data != NULL && *(u_int *)data <= 100)
505 sc->sc_vol = *(u_int *)data;
506 return 0;
507 default:
508 return ENOTTY;
509 }
510 }
511
512 #ifdef _MODULE
513 extern struct cfdriver spkr_cd;
514 #include "ioconf.c"
515 #endif
516
517 MODULE(MODULE_CLASS_DRIVER, spkr, "audio" /* and/or pcppi */ );
518
519 int
520 spkr_modcmd(modcmd_t cmd, void *arg)
521 {
522 #ifdef _MODULE
523 devmajor_t bmajor, cmajor;
524 int error = 0;
525
526 switch(cmd) {
527 case MODULE_CMD_INIT:
528 bmajor = cmajor = -1;
529 error = devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
530 &spkr_cdevsw, &cmajor);
531 if (error)
532 break;
533
534 error = config_init_component(cfdriver_ioconf_spkr,
535 cfattach_ioconf_spkr, cfdata_ioconf_spkr);
536 if (error) {
537 devsw_detach(NULL, &spkr_cdevsw);
538 }
539 break;
540
541 case MODULE_CMD_FINI:
542 devsw_detach(NULL, &spkr_cdevsw);
543 error = config_fini_component(cfdriver_ioconf_spkr,
544 cfattach_ioconf_spkr, cfdata_ioconf_spkr);
545 if (error)
546 devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
547 &spkr_cdevsw, &cmajor);
548 break;
549
550 default:
551 error = ENOTTY;
552 break;
553 }
554
555 return error;
556 #else
557 return 0;
558 #endif
559 }
560