Home | History | Annotate | Line # | Download | only in dev
spkr.c revision 1.17.14.3
      1  1.17.14.3   thorpej /*	$NetBSD: spkr.c,v 1.17.14.3 2021/04/03 21:44:50 thorpej Exp $	*/
      2        1.1  christos 
      3        1.1  christos /*
      4        1.1  christos  * Copyright (c) 1990 Eric S. Raymond (esr (at) snark.thyrsus.com)
      5        1.1  christos  * Copyright (c) 1990 Andrew A. Chernov (ache (at) astral.msk.su)
      6        1.1  christos  * Copyright (c) 1990 Lennart Augustsson (lennart (at) augustsson.net)
      7        1.1  christos  * All rights reserved.
      8        1.1  christos  *
      9        1.1  christos  * Redistribution and use in source and binary forms, with or without
     10        1.1  christos  * modification, are permitted provided that the following conditions
     11        1.1  christos  * are met:
     12        1.1  christos  * 1. Redistributions of source code must retain the above copyright
     13        1.1  christos  *    notice, this list of conditions and the following disclaimer.
     14        1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15        1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     16        1.1  christos  *    documentation and/or other materials provided with the distribution.
     17        1.1  christos  * 3. All advertising materials mentioning features or use of this software
     18        1.1  christos  *    must display the following acknowledgement:
     19        1.1  christos  *	This product includes software developed by Eric S. Raymond
     20        1.1  christos  * 4. The name of the author may not be used to endorse or promote products
     21        1.1  christos  *    derived from this software without specific prior written permission.
     22        1.1  christos  *
     23        1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24        1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     25        1.1  christos  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     26        1.1  christos  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     27        1.1  christos  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     28        1.1  christos  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     29        1.1  christos  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30        1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     31        1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     32        1.1  christos  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     33        1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     34        1.1  christos  */
     35        1.1  christos 
     36        1.1  christos /*
     37        1.1  christos  * spkr.c -- device driver for console speaker on 80386
     38        1.1  christos  *
     39        1.1  christos  * v1.1 by Eric S. Raymond (esr (at) snark.thyrsus.com) Feb 1990
     40        1.1  christos  *      modified for 386bsd by Andrew A. Chernov <ache (at) astral.msk.su>
     41        1.1  christos  *      386bsd only clean version, all SYSV stuff removed
     42        1.1  christos  *      use hz value from param.c
     43        1.1  christos  */
     44        1.1  christos 
     45        1.1  christos #include <sys/cdefs.h>
     46  1.17.14.3   thorpej __KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.17.14.3 2021/04/03 21:44:50 thorpej Exp $");
     47        1.1  christos 
     48       1.10  pgoyette #if defined(_KERNEL_OPT)
     49        1.9       nat #include "wsmux.h"
     50       1.10  pgoyette #endif
     51        1.9       nat 
     52        1.1  christos #include <sys/param.h>
     53        1.1  christos #include <sys/systm.h>
     54        1.1  christos #include <sys/kernel.h>
     55        1.1  christos #include <sys/errno.h>
     56        1.1  christos #include <sys/device.h>
     57        1.1  christos #include <sys/malloc.h>
     58        1.1  christos #include <sys/module.h>
     59        1.1  christos #include <sys/uio.h>
     60        1.1  christos #include <sys/proc.h>
     61        1.1  christos #include <sys/ioctl.h>
     62        1.1  christos #include <sys/conf.h>
     63        1.1  christos 
     64        1.1  christos #include <sys/bus.h>
     65        1.1  christos 
     66        1.1  christos #include <dev/spkrio.h>
     67        1.2  christos #include <dev/spkrvar.h>
     68        1.9       nat #include <dev/wscons/wsconsio.h>
     69        1.9       nat #include <dev/wscons/wsbellvar.h>
     70        1.9       nat #include <dev/wscons/wsbellmuxvar.h>
     71        1.1  christos 
     72       1.15  riastrad #include "ioconf.h"
     73       1.15  riastrad 
     74        1.1  christos dev_type_open(spkropen);
     75        1.1  christos dev_type_close(spkrclose);
     76        1.1  christos dev_type_write(spkrwrite);
     77        1.1  christos dev_type_ioctl(spkrioctl);
     78        1.1  christos 
     79        1.1  christos const struct cdevsw spkr_cdevsw = {
     80        1.1  christos 	.d_open = spkropen,
     81        1.1  christos 	.d_close = spkrclose,
     82        1.1  christos 	.d_read = noread,
     83        1.1  christos 	.d_write = spkrwrite,
     84        1.1  christos 	.d_ioctl = spkrioctl,
     85        1.1  christos 	.d_stop = nostop,
     86        1.1  christos 	.d_tty = notty,
     87        1.1  christos 	.d_poll = nopoll,
     88        1.1  christos 	.d_mmap = nommap,
     89        1.1  christos 	.d_kqfilter = nokqfilter,
     90        1.1  christos 	.d_discard = nodiscard,
     91        1.1  christos 	.d_flag = D_OTHER
     92        1.1  christos };
     93        1.1  christos 
     94        1.4  christos static void playinit(struct spkr_softc *);
     95        1.4  christos static void playtone(struct spkr_softc *, int, int, int);
     96        1.4  christos static void playstring(struct spkr_softc *, const char *, size_t);
     97        1.1  christos 
     98        1.1  christos /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
     99        1.1  christos  *
    100        1.1  christos  * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
    101        1.1  christos  * M[LNS] are missing and the ~ synonym and octave-tracking facility is added.
    102  1.17.14.3   thorpej  * String play is not interruptible except possibly at physical block
    103  1.17.14.3   thorpej  * boundaries.
    104        1.1  christos  */
    105        1.1  christos 
    106        1.1  christos /*
    107        1.1  christos  * Magic number avoidance...
    108        1.1  christos  */
    109        1.1  christos #define SECS_PER_MIN	60	/* seconds per minute */
    110        1.1  christos #define WHOLE_NOTE	4	/* quarter notes per whole note */
    111        1.1  christos #define MIN_VALUE	64	/* the most we can divide a note by */
    112        1.1  christos #define DFLT_VALUE	4	/* default value (quarter-note) */
    113        1.1  christos #define FILLTIME	8	/* for articulation, break note in parts */
    114        1.1  christos #define STACCATO	6	/* 6/8 = 3/4 of note is filled */
    115        1.1  christos #define NORMAL		7	/* 7/8ths of note interval is filled */
    116        1.1  christos #define LEGATO		8	/* all of note interval is filled */
    117        1.1  christos #define DFLT_OCTAVE	4	/* default octave */
    118        1.1  christos #define MIN_TEMPO	32	/* minimum tempo */
    119        1.1  christos #define DFLT_TEMPO	120	/* default tempo */
    120        1.1  christos #define MAX_TEMPO	255	/* max tempo */
    121        1.1  christos #define NUM_MULT	3	/* numerator of dot multiplier */
    122        1.1  christos #define DENOM_MULT	2	/* denominator of dot multiplier */
    123        1.1  christos 
    124       1.17     isaki /* letter to half-tone:         A   B  C  D  E  F  G */
    125       1.17     isaki static const int notetab[8] = { 9, 11, 0, 2, 4, 5, 7 };
    126        1.1  christos 
    127        1.1  christos /*
    128        1.1  christos  * This is the American Standard A440 Equal-Tempered scale with frequencies
    129        1.1  christos  * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
    130        1.1  christos  * our octave 0 is standard octave 2.
    131        1.1  christos  */
    132        1.1  christos #define OCTAVE_NOTES	12	/* semitones per octave */
    133        1.1  christos static const int pitchtab[] =
    134        1.1  christos {
    135        1.1  christos /*        C     C#    D     D#    E     F     F#    G     G#    A     A#    B*/
    136        1.1  christos /* 0 */   65,   69,   73,   78,   82,   87,   93,   98,  103,  110,  117,  123,
    137        1.1  christos /* 1 */  131,  139,  147,  156,  165,  175,  185,  196,  208,  220,  233,  247,
    138        1.1  christos /* 2 */  262,  277,  294,  311,  330,  349,  370,  392,  415,  440,  466,  494,
    139        1.1  christos /* 3 */  523,  554,  587,  622,  659,  698,  740,  784,  831,  880,  932,  988,
    140        1.1  christos /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
    141        1.1  christos /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
    142        1.1  christos /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
    143        1.1  christos };
    144        1.1  christos #define NOCTAVES (int)(__arraycount(pitchtab) / OCTAVE_NOTES)
    145        1.1  christos 
    146        1.1  christos static void
    147        1.4  christos playinit(struct spkr_softc *sc)
    148        1.1  christos {
    149        1.4  christos 	sc->sc_octave = DFLT_OCTAVE;
    150        1.4  christos 	sc->sc_whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
    151        1.4  christos 	sc->sc_fill = NORMAL;
    152        1.4  christos 	sc->sc_value = DFLT_VALUE;
    153        1.4  christos 	sc->sc_octtrack = false;
    154        1.4  christos 	sc->sc_octprefix = true;/* act as though there was an initial O(n) */
    155        1.1  christos }
    156        1.1  christos 
    157  1.17.14.3   thorpej #define SPKRPRI (PZERO - 1)
    158  1.17.14.3   thorpej 
    159  1.17.14.3   thorpej /* Rest for given number of ticks */
    160        1.1  christos static void
    161  1.17.14.3   thorpej rest(struct spkr_softc *sc, int ticks)
    162        1.1  christos {
    163  1.17.14.3   thorpej 
    164  1.17.14.3   thorpej #ifdef SPKRDEBUG
    165  1.17.14.3   thorpej 	device_printf(sc->sc_dev, "%s: rest for %d ticks\n", __func__, ticks);
    166  1.17.14.3   thorpej #endif /* SPKRDEBUG */
    167  1.17.14.3   thorpej 	KASSERT(ticks > 0);
    168  1.17.14.3   thorpej 
    169  1.17.14.3   thorpej 	tsleep(sc->sc_dev, SPKRPRI | PCATCH, device_xname(sc->sc_dev), ticks);
    170  1.17.14.3   thorpej }
    171  1.17.14.3   thorpej 
    172  1.17.14.3   thorpej /*
    173  1.17.14.3   thorpej  * Play tone of proper duration for current rhythm signature.
    174  1.17.14.3   thorpej  * note indicates "O0C" = 0, "O0C#" = 1, "O0D" = 2, ... , and
    175  1.17.14.3   thorpej  * -1 indiacates a rest.
    176  1.17.14.3   thorpej  * val indicates the length, "L4" = 4, "L8" = 8.
    177  1.17.14.3   thorpej  * sustain indicates the number of subsequent dots that extend the sound
    178  1.17.14.3   thorpej  * by one a half.
    179  1.17.14.3   thorpej  */
    180  1.17.14.3   thorpej static void
    181  1.17.14.3   thorpej playtone(struct spkr_softc *sc, int note, int val, int sustain)
    182  1.17.14.3   thorpej {
    183  1.17.14.3   thorpej 	int whole;
    184  1.17.14.3   thorpej 	int total;
    185  1.17.14.3   thorpej 	int sound;
    186  1.17.14.3   thorpej 	int silence;
    187        1.1  christos 
    188        1.4  christos 	/* this weirdness avoids floating-point arithmetic */
    189  1.17.14.3   thorpej 	whole = sc->sc_whole;
    190        1.4  christos 	for (; sustain; sustain--) {
    191  1.17.14.3   thorpej 		whole *= NUM_MULT;
    192  1.17.14.3   thorpej 		val *= DENOM_MULT;
    193        1.4  christos 	}
    194        1.1  christos 
    195  1.17.14.3   thorpej 	/* Total length in tick */
    196  1.17.14.3   thorpej 	total = whole / val;
    197  1.17.14.3   thorpej 
    198  1.17.14.3   thorpej 	if (note == -1) {
    199  1.17.14.3   thorpej #ifdef SPKRDEBUG
    200  1.17.14.3   thorpej 		device_printf(sc->sc_dev, "%s: rest for %d ticks\n",
    201  1.17.14.3   thorpej 		    __func__, total);
    202  1.17.14.3   thorpej #endif /* SPKRDEBUG */
    203  1.17.14.3   thorpej 		if (total != 0)
    204  1.17.14.3   thorpej 			rest(sc, total);
    205        1.4  christos 		return;
    206        1.4  christos 	}
    207        1.4  christos 
    208  1.17.14.3   thorpej 	/*
    209  1.17.14.3   thorpej 	 * Rest 1/8 (if NORMAL) or 3/8 (if STACCATO) in tick.
    210  1.17.14.3   thorpej 	 * silence should be rounded down.
    211  1.17.14.3   thorpej 	 */
    212  1.17.14.3   thorpej 	silence = total * (FILLTIME - sc->sc_fill) / FILLTIME;
    213  1.17.14.3   thorpej 	sound = total - silence;
    214        1.1  christos 
    215        1.1  christos #ifdef SPKRDEBUG
    216  1.17.14.3   thorpej 	device_printf(sc->sc_dev,
    217  1.17.14.3   thorpej 	    "%s: note %d for %d ticks, rest for %d ticks\n", __func__,
    218  1.17.14.3   thorpej 	    note, sound, silence);
    219        1.1  christos #endif /* SPKRDEBUG */
    220        1.1  christos 
    221  1.17.14.3   thorpej 	if (sound != 0)
    222  1.17.14.3   thorpej 		(*sc->sc_tone)(sc->sc_dev, pitchtab[note], sound);
    223  1.17.14.3   thorpej 	if (silence != 0)
    224  1.17.14.3   thorpej 		rest(sc, silence);
    225        1.1  christos }
    226        1.1  christos 
    227        1.4  christos /* interpret and play an item from a notation string */
    228        1.1  christos static void
    229        1.4  christos playstring(struct spkr_softc *sc, const char *cp, size_t slen)
    230        1.1  christos {
    231  1.17.14.3   thorpej 	int pitch;
    232  1.17.14.3   thorpej 	int lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
    233        1.4  christos 
    234        1.4  christos #define GETNUM(cp, v)	\
    235        1.4  christos 	for (v = 0; slen > 0 && isdigit((unsigned char)cp[1]); ) { \
    236        1.4  christos 		v = v * 10 + (*++cp - '0'); \
    237        1.4  christos 		slen--; \
    238        1.4  christos 	}
    239        1.1  christos 
    240        1.4  christos 	for (; slen--; cp++) {
    241        1.4  christos 		int sustain, timeval, tempo;
    242        1.4  christos 		char c = toupper((unsigned char)*cp);
    243        1.1  christos 
    244        1.1  christos #ifdef SPKRDEBUG
    245  1.17.14.3   thorpej 		if (0x20 <= c && c < 0x7f) {
    246  1.17.14.3   thorpej 			device_printf(sc->sc_dev, "%s: '%c'\n", __func__, c);
    247  1.17.14.3   thorpej 		} else {
    248  1.17.14.3   thorpej 			device_printf(sc->sc_dev, "%s: (0x%x)\n", __func__, c);
    249  1.17.14.3   thorpej 		}
    250        1.1  christos #endif /* SPKRDEBUG */
    251        1.1  christos 
    252        1.4  christos 		switch (c) {
    253       1.17     isaki 		case 'A': case 'B': case 'C': case 'D':
    254        1.4  christos 		case 'E': case 'F': case 'G':
    255        1.4  christos 			/* compute pitch */
    256        1.4  christos 			pitch = notetab[c - 'A'] + sc->sc_octave * OCTAVE_NOTES;
    257        1.4  christos 
    258        1.4  christos 			/* this may be followed by an accidental sign */
    259        1.4  christos 			if (slen > 0 && (cp[1] == '#' || cp[1] == '+')) {
    260        1.4  christos 				++pitch;
    261        1.4  christos 				++cp;
    262        1.4  christos 				slen--;
    263        1.4  christos 			} else if (slen > 0 && cp[1] == '-') {
    264        1.4  christos 				--pitch;
    265        1.4  christos 				++cp;
    266        1.4  christos 				slen--;
    267        1.4  christos 			}
    268        1.4  christos 
    269        1.4  christos 			/*
    270        1.4  christos 			 * If octave-tracking mode is on, and there has been no
    271        1.4  christos 			 * octave- setting prefix, find the version of the
    272        1.4  christos 			 * current letter note * closest to the last
    273        1.4  christos 			 * regardless of octave.
    274        1.4  christos 			 */
    275        1.4  christos 			if (sc->sc_octtrack && !sc->sc_octprefix) {
    276        1.4  christos 				int d = abs(pitch - lastpitch);
    277        1.4  christos 				if (d > abs(pitch + OCTAVE_NOTES - lastpitch)) {
    278        1.4  christos 					if (sc->sc_octave < NOCTAVES - 1) {
    279        1.4  christos 						++sc->sc_octave;
    280        1.4  christos 						pitch += OCTAVE_NOTES;
    281        1.4  christos 					}
    282        1.4  christos 				}
    283        1.4  christos 
    284        1.4  christos 				if (d > abs(pitch - OCTAVE_NOTES - lastpitch)) {
    285        1.4  christos 					if (sc->sc_octave > 0) {
    286        1.4  christos 						--sc->sc_octave;
    287        1.4  christos 						pitch -= OCTAVE_NOTES;
    288        1.4  christos 					}
    289        1.4  christos 				}
    290        1.4  christos 			}
    291        1.4  christos 			sc->sc_octprefix = false;
    292        1.4  christos 			lastpitch = pitch;
    293        1.4  christos 
    294        1.4  christos 			/*
    295        1.4  christos 			 * ...which may in turn be followed by an override
    296        1.4  christos 			 * time value
    297        1.4  christos 			 */
    298        1.4  christos 			GETNUM(cp, timeval);
    299        1.4  christos 			if (timeval <= 0 || timeval > MIN_VALUE)
    300        1.4  christos 				timeval = sc->sc_value;
    301        1.4  christos 
    302        1.4  christos 			/* ...and/or sustain dots */
    303        1.4  christos 			for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) {
    304        1.4  christos 				slen--;
    305        1.4  christos 				sustain++;
    306        1.4  christos 			}
    307        1.4  christos 
    308        1.4  christos 			/* time to emit the actual tone */
    309        1.4  christos 			playtone(sc, pitch, timeval, sustain);
    310        1.4  christos 			break;
    311        1.4  christos 
    312        1.4  christos 		case 'O':
    313        1.4  christos 			if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) {
    314        1.4  christos 				sc->sc_octprefix = sc->sc_octtrack = false;
    315        1.4  christos 				++cp;
    316        1.4  christos 				slen--;
    317        1.4  christos 			} else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) {
    318        1.4  christos 				sc->sc_octtrack = true;
    319        1.4  christos 				++cp;
    320        1.4  christos 				slen--;
    321        1.4  christos 			} else {
    322        1.4  christos 				GETNUM(cp, sc->sc_octave);
    323        1.4  christos 				if (sc->sc_octave >= NOCTAVES)
    324        1.4  christos 					sc->sc_octave = DFLT_OCTAVE;
    325        1.4  christos 				sc->sc_octprefix = true;
    326        1.4  christos 			}
    327        1.4  christos 			break;
    328        1.4  christos 
    329        1.4  christos 		case '>':
    330        1.4  christos 			if (sc->sc_octave < NOCTAVES - 1)
    331        1.4  christos 				sc->sc_octave++;
    332        1.4  christos 			sc->sc_octprefix = true;
    333        1.4  christos 			break;
    334        1.4  christos 
    335        1.4  christos 		case '<':
    336        1.4  christos 			if (sc->sc_octave > 0)
    337        1.4  christos 				sc->sc_octave--;
    338        1.4  christos 			sc->sc_octprefix = true;
    339        1.4  christos 			break;
    340        1.4  christos 
    341        1.4  christos 		case 'N':
    342        1.4  christos 			GETNUM(cp, pitch);
    343        1.4  christos 			for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) {
    344        1.4  christos 				slen--;
    345        1.4  christos 				sustain++;
    346        1.4  christos 			}
    347        1.4  christos 			playtone(sc, pitch - 1, sc->sc_value, sustain);
    348        1.4  christos 			break;
    349        1.4  christos 
    350        1.4  christos 		case 'L':
    351        1.4  christos 			GETNUM(cp, sc->sc_value);
    352        1.4  christos 			if (sc->sc_value <= 0 || sc->sc_value > MIN_VALUE)
    353        1.4  christos 				sc->sc_value = DFLT_VALUE;
    354        1.4  christos 			break;
    355        1.4  christos 
    356        1.4  christos 		case 'P':
    357        1.4  christos 		case '~':
    358        1.4  christos 			/* this may be followed by an override time value */
    359        1.4  christos 			GETNUM(cp, timeval);
    360        1.4  christos 			if (timeval <= 0 || timeval > MIN_VALUE)
    361        1.4  christos 				timeval = sc->sc_value;
    362        1.4  christos 			for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) {
    363        1.4  christos 				slen--;
    364        1.4  christos 				sustain++;
    365        1.4  christos 			}
    366        1.4  christos 			playtone(sc, -1, timeval, sustain);
    367        1.4  christos 			break;
    368        1.4  christos 
    369        1.4  christos 		case 'T':
    370        1.4  christos 			GETNUM(cp, tempo);
    371        1.4  christos 			if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
    372        1.4  christos 				tempo = DFLT_TEMPO;
    373        1.4  christos 			sc->sc_whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
    374        1.4  christos 			break;
    375        1.1  christos 
    376        1.4  christos 		case 'M':
    377        1.4  christos 			if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) {
    378        1.4  christos 				sc->sc_fill = NORMAL;
    379        1.4  christos 				++cp;
    380        1.4  christos 				slen--;
    381        1.4  christos 			} else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) {
    382        1.4  christos 				sc->sc_fill = LEGATO;
    383        1.4  christos 				++cp;
    384        1.4  christos 				slen--;
    385        1.4  christos 			} else if (slen > 0 && (cp[1] == 'S' || cp[1] == 's')) {
    386        1.4  christos 				sc->sc_fill = STACCATO;
    387        1.4  christos 				++cp;
    388        1.4  christos 				slen--;
    389        1.4  christos 			}
    390        1.4  christos 			break;
    391        1.1  christos 		}
    392        1.1  christos 	}
    393        1.1  christos }
    394        1.1  christos 
    395  1.17.14.3   thorpej /******************* UNIX DRIVER HOOKS BEGIN HERE **************************/
    396        1.4  christos #define spkrenter(d)	device_lookup_private(&spkr_cd, d)
    397        1.1  christos 
    398  1.17.14.3   thorpej /*
    399  1.17.14.3   thorpej  * Attaches spkr.  Specify tone function with the following specification:
    400  1.17.14.3   thorpej  *
    401  1.17.14.3   thorpej  * void
    402  1.17.14.3   thorpej  * tone(device_t self, u_int pitch, u_int tick)
    403  1.17.14.3   thorpej  *	plays a beep with specified parameters.
    404  1.17.14.3   thorpej  *	The argument 'pitch' specifies the pitch of a beep in Hz.  The argument
    405  1.17.14.3   thorpej  *	'tick' specifies the period of a beep in tick(9).  This function waits
    406  1.17.14.3   thorpej  *	to finish playing the beep and then halts it.
    407  1.17.14.3   thorpej  *	If the pitch is zero, it halts all sound if any (for compatibility
    408  1.17.14.3   thorpej  *	with the past confused specifications, but there should be no sound at
    409  1.17.14.3   thorpej  *	this point).  And it returns immediately, without waiting ticks.  So
    410  1.17.14.3   thorpej  *	you cannot use this as a rest.
    411  1.17.14.3   thorpej  *	If the tick is zero, it returns immediately.
    412  1.17.14.3   thorpej  */
    413        1.4  christos void
    414  1.17.14.3   thorpej spkr_attach(device_t self, void (*tone)(device_t, u_int, u_int))
    415        1.4  christos {
    416        1.4  christos 	struct spkr_softc *sc = device_private(self);
    417        1.1  christos 
    418        1.6  pgoyette #ifdef SPKRDEBUG
    419        1.6  pgoyette 	aprint_debug("%s: entering for unit %d\n", __func__, self->dv_unit);
    420        1.6  pgoyette #endif /* SPKRDEBUG */
    421        1.4  christos 	sc->sc_dev = self;
    422        1.4  christos 	sc->sc_tone = tone;
    423        1.4  christos 	sc->sc_inbuf = NULL;
    424       1.12  pgoyette 	sc->sc_wsbelldev = NULL;
    425        1.9       nat 
    426  1.17.14.2   thorpej 	spkr_rescan(self, NULL, NULL);
    427        1.1  christos }
    428        1.1  christos 
    429        1.1  christos int
    430        1.6  pgoyette spkr_detach(device_t self, int flags)
    431        1.6  pgoyette {
    432        1.6  pgoyette 	struct spkr_softc *sc = device_private(self);
    433       1.13       nat 	int rc;
    434        1.6  pgoyette 
    435        1.6  pgoyette #ifdef SPKRDEBUG
    436        1.6  pgoyette 	aprint_debug("%s: entering for unit %d\n", __func__, self->dv_unit);
    437        1.6  pgoyette #endif /* SPKRDEBUG */
    438        1.6  pgoyette 	if (sc == NULL)
    439        1.6  pgoyette 		return ENXIO;
    440       1.14       nat 
    441       1.14       nat 	/* XXXNS If speaker never closes, we cannot complete the detach. */
    442       1.14       nat 	while ((flags & DETACH_FORCE) != 0 && sc->sc_inbuf != NULL)
    443       1.14       nat 		kpause("spkrwait", TRUE, 1, NULL);
    444        1.6  pgoyette 	if (sc->sc_inbuf != NULL)
    445        1.6  pgoyette 		return EBUSY;
    446        1.6  pgoyette 
    447       1.13       nat 	rc = config_detach_children(self, flags);
    448       1.13       nat 
    449       1.13       nat 	return rc;
    450        1.6  pgoyette }
    451        1.6  pgoyette 
    452       1.12  pgoyette /* ARGSUSED */
    453       1.12  pgoyette int
    454       1.12  pgoyette spkr_rescan(device_t self, const char *iattr, const int *locators)
    455       1.12  pgoyette {
    456       1.12  pgoyette #if NWSMUX > 0
    457       1.12  pgoyette 	struct spkr_softc *sc = device_private(self);
    458       1.12  pgoyette 	struct wsbelldev_attach_args a;
    459       1.12  pgoyette 
    460       1.12  pgoyette 	if (sc->sc_wsbelldev == NULL) {
    461       1.12  pgoyette 		a.accesscookie = sc;
    462  1.17.14.1   thorpej 		sc->sc_wsbelldev = config_found(self, &a, wsbelldevprint,
    463  1.17.14.1   thorpej 		    CFARG_EOL);
    464       1.12  pgoyette 	}
    465       1.12  pgoyette #endif
    466       1.12  pgoyette 	return 0;
    467       1.12  pgoyette }
    468       1.12  pgoyette 
    469       1.12  pgoyette int
    470       1.12  pgoyette spkr_childdet(device_t self, device_t child)
    471       1.12  pgoyette {
    472       1.12  pgoyette 	struct spkr_softc *sc = device_private(self);
    473       1.12  pgoyette 
    474       1.12  pgoyette 	if (sc->sc_wsbelldev == child)
    475       1.12  pgoyette 		sc->sc_wsbelldev = NULL;
    476       1.12  pgoyette 
    477       1.12  pgoyette 	return 0;
    478       1.12  pgoyette }
    479       1.12  pgoyette 
    480        1.6  pgoyette int
    481        1.1  christos spkropen(dev_t dev, int	flags, int mode, struct lwp *l)
    482        1.1  christos {
    483        1.4  christos 	struct spkr_softc *sc = spkrenter(minor(dev));
    484        1.1  christos 
    485  1.17.14.3   thorpej #ifdef SPKRDEBUG
    486  1.17.14.3   thorpej 	device_printf(sc->sc_dev, "%s: entering\n", __func__);
    487  1.17.14.3   thorpej #endif /* SPKRDEBUG */
    488        1.4  christos 	if (sc == NULL)
    489        1.4  christos 		return ENXIO;
    490        1.6  pgoyette 	if (sc->sc_inbuf != NULL)
    491        1.4  christos 		return EBUSY;
    492        1.4  christos 
    493        1.4  christos 	sc->sc_inbuf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
    494        1.4  christos 	playinit(sc);
    495        1.4  christos 	return 0;
    496        1.1  christos }
    497        1.1  christos 
    498        1.1  christos int
    499        1.1  christos spkrwrite(dev_t dev, struct uio *uio, int flags)
    500        1.1  christos {
    501        1.4  christos 	struct spkr_softc *sc = spkrenter(minor(dev));
    502        1.1  christos 
    503  1.17.14.3   thorpej #ifdef SPKRDEBUG
    504  1.17.14.3   thorpej 	device_printf(sc->sc_dev, "%s: entering with length = %zu\n",
    505  1.17.14.3   thorpej 	    __func__, uio->uio_resid);
    506  1.17.14.3   thorpej #endif /* SPKRDEBUG */
    507        1.4  christos 	if (sc == NULL)
    508        1.4  christos 		return ENXIO;
    509        1.6  pgoyette 	if (sc->sc_inbuf == NULL)
    510        1.4  christos 		return EINVAL;
    511        1.4  christos 
    512       1.16  riastrad 	size_t n = uimin(DEV_BSIZE, uio->uio_resid);
    513        1.4  christos 	int error = uiomove(sc->sc_inbuf, n, uio);
    514        1.4  christos 	if (error)
    515        1.4  christos 		return error;
    516        1.4  christos 	playstring(sc, sc->sc_inbuf, n);
    517        1.4  christos 	return 0;
    518        1.1  christos }
    519        1.1  christos 
    520        1.1  christos int
    521        1.1  christos spkrclose(dev_t dev, int flags, int mode, struct lwp *l)
    522        1.1  christos {
    523        1.4  christos 	struct spkr_softc *sc = spkrenter(minor(dev));
    524        1.4  christos 
    525  1.17.14.3   thorpej #ifdef SPKRDEBUG
    526  1.17.14.3   thorpej 	device_printf(sc->sc_dev, "%s: entering\n", __func__);
    527  1.17.14.3   thorpej #endif /* SPKRDEBUG */
    528        1.4  christos 	if (sc == NULL)
    529        1.4  christos 		return ENXIO;
    530        1.6  pgoyette 	if (sc->sc_inbuf == NULL)
    531        1.4  christos 		return EINVAL;
    532        1.4  christos 
    533        1.4  christos 	sc->sc_tone(sc->sc_dev, 0, 0);
    534        1.4  christos 	free(sc->sc_inbuf, M_DEVBUF);
    535        1.4  christos 	sc->sc_inbuf = NULL;
    536        1.1  christos 
    537        1.4  christos 	return 0;
    538        1.4  christos }
    539        1.4  christos 
    540  1.17.14.3   thorpej /*
    541  1.17.14.3   thorpej  * Play tone specified by tp.
    542  1.17.14.3   thorpej  * tp->frequency is the frequency (0 means a rest).
    543  1.17.14.3   thorpej  * tp->duration is the length in tick (returns immediately if 0).
    544  1.17.14.3   thorpej  */
    545        1.4  christos static void
    546        1.4  christos playonetone(struct spkr_softc *sc, tone_t *tp)
    547        1.4  christos {
    548  1.17.14.3   thorpej 	if (tp->duration <= 0)
    549  1.17.14.3   thorpej 		return;
    550  1.17.14.3   thorpej 
    551  1.17.14.3   thorpej 	if (tp->frequency == 0)
    552  1.17.14.3   thorpej 		rest(sc, tp->duration);
    553  1.17.14.3   thorpej 	else
    554  1.17.14.3   thorpej 		(*sc->sc_tone)(sc->sc_dev, tp->frequency, tp->duration);
    555        1.1  christos }
    556        1.1  christos 
    557        1.1  christos int
    558        1.1  christos spkrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    559        1.1  christos {
    560  1.17.14.3   thorpej 	struct spkr_softc *sc = spkrenter(minor(dev));
    561        1.4  christos 	tone_t *tp;
    562        1.4  christos 	tone_t ttp;
    563        1.4  christos 	int error;
    564  1.17.14.3   thorpej 
    565        1.1  christos #ifdef SPKRDEBUG
    566  1.17.14.3   thorpej 	device_printf(sc->sc_dev, "%s: entering with cmd = %lx\n",
    567  1.17.14.3   thorpej 	    __func__, cmd);
    568        1.1  christos #endif /* SPKRDEBUG */
    569        1.4  christos 	if (sc == NULL)
    570        1.4  christos 		return ENXIO;
    571        1.6  pgoyette 	if (sc->sc_inbuf == NULL)
    572        1.4  christos 		return EINVAL;
    573        1.4  christos 
    574        1.4  christos 	switch (cmd) {
    575       1.17     isaki 	case SPKRTONE:
    576        1.4  christos 		playonetone(sc, data);
    577        1.4  christos 		return 0;
    578        1.4  christos 	case SPKRTUNE:
    579        1.4  christos 		for (tp = *(void **)data;; tp++) {
    580        1.4  christos 			error = copyin(tp, &ttp, sizeof(tone_t));
    581        1.4  christos 			if (error)
    582        1.4  christos 				return(error);
    583        1.4  christos 			if (ttp.duration == 0)
    584        1.4  christos 				break;
    585        1.4  christos 			playonetone(sc, &ttp);
    586        1.4  christos 		}
    587        1.4  christos 		return 0;
    588        1.8       nat 	case SPKRGETVOL:
    589        1.8       nat 		if (data != NULL)
    590        1.8       nat 			*(u_int *)data = sc->sc_vol;
    591        1.8       nat 		return 0;
    592        1.8       nat 	case SPKRSETVOL:
    593        1.8       nat 		if (data != NULL && *(u_int *)data <= 100)
    594        1.8       nat 			sc->sc_vol = *(u_int *)data;
    595        1.8       nat 		return 0;
    596        1.4  christos 	default:
    597        1.4  christos 		return ENOTTY;
    598        1.1  christos 	}
    599        1.1  christos }
    600        1.1  christos 
    601        1.3  christos #ifdef _MODULE
    602        1.3  christos #include "ioconf.c"
    603        1.3  christos #endif
    604        1.3  christos 
    605        1.7  pgoyette MODULE(MODULE_CLASS_DRIVER, spkr, "audio" /* and/or pcppi */ );
    606        1.5  pgoyette 
    607        1.1  christos int
    608        1.5  pgoyette spkr_modcmd(modcmd_t cmd, void *arg)
    609        1.1  christos {
    610       1.11  pgoyette 	int error = 0;
    611        1.1  christos #ifdef _MODULE
    612        1.1  christos 	devmajor_t bmajor, cmajor;
    613       1.11  pgoyette #endif
    614        1.1  christos 
    615        1.1  christos 	switch(cmd) {
    616        1.1  christos 	case MODULE_CMD_INIT:
    617       1.11  pgoyette #ifdef _MODULE
    618        1.1  christos 		bmajor = cmajor = -1;
    619        1.1  christos 		error = devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
    620        1.1  christos 		    &spkr_cdevsw, &cmajor);
    621        1.1  christos 		if (error)
    622        1.1  christos 			break;
    623        1.1  christos 
    624        1.1  christos 		error = config_init_component(cfdriver_ioconf_spkr,
    625        1.6  pgoyette 		    cfattach_ioconf_spkr, cfdata_ioconf_spkr);
    626        1.1  christos 		if (error) {
    627        1.1  christos 			devsw_detach(NULL, &spkr_cdevsw);
    628        1.1  christos 		}
    629       1.11  pgoyette #endif
    630        1.1  christos 		break;
    631        1.1  christos 
    632        1.1  christos 	case MODULE_CMD_FINI:
    633       1.11  pgoyette #ifdef _MODULE
    634        1.6  pgoyette 		devsw_detach(NULL, &spkr_cdevsw);
    635        1.1  christos 		error = config_fini_component(cfdriver_ioconf_spkr,
    636        1.6  pgoyette 		    cfattach_ioconf_spkr, cfdata_ioconf_spkr);
    637        1.6  pgoyette 		if (error)
    638        1.6  pgoyette 			devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
    639        1.6  pgoyette 			    &spkr_cdevsw, &cmajor);
    640       1.11  pgoyette #endif
    641        1.1  christos 		break;
    642        1.6  pgoyette 
    643        1.1  christos 	default:
    644        1.1  christos 		error = ENOTTY;
    645        1.1  christos 		break;
    646        1.1  christos 	}
    647        1.1  christos 
    648        1.1  christos 	return error;
    649        1.1  christos }
    650