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