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