Index: sys/arch/amd64/conf/GENERIC =================================================================== RCS file: /cvsroot/src/sys/arch/amd64/conf/GENERIC,v retrieving revision 1.446 diff -u -p -u -r1.446 GENERIC --- sys/arch/amd64/conf/GENERIC 10 Dec 2016 23:03:23 -0000 1.446 +++ sys/arch/amd64/conf/GENERIC 13 Dec 2016 12:02:46 -0000 @@ -1108,10 +1108,12 @@ hdafg* at hdaudiobus? # Audio support audio* at audiobus? -# The spkr driver provides a simple tone interface to the built in speaker. -spkr0 at pcppi? # PC speaker +spkr_synth* at audio? # audio device synthesized speaker +spkr_pcppi* at pcppi? # PC speaker -#spkr0 at audio0 # PC speaker (synthesized) +# The spkr driver provides a simple tone interface to the built in speaker +# or audio device +spkr* at spkrbus? # MPU 401 UARTs #mpu* at isa? port 0x330 irq 9 # MPU401 or compatible card Index: sys/arch/amd64/conf/MODULAR =================================================================== RCS file: /cvsroot/src/sys/arch/amd64/conf/MODULAR,v retrieving revision 1.5 diff -u -p -u -r1.5 MODULAR --- sys/arch/amd64/conf/MODULAR 9 Sep 2016 21:10:56 -0000 1.5 +++ sys/arch/amd64/conf/MODULAR 13 Dec 2016 12:02:46 -0000 @@ -4,8 +4,8 @@ # XXX: incomplete include "arch/amd64/conf/GENERIC" -options MODULAR # new style module(7) framework -options MODULAR_DEFAULT_AUTOLOAD +#options MODULAR # new style module(7) framework +#options MODULAR_DEFAULT_AUTOLOAD -no acpicpu* at cpu? -no est0 at cpu0 @@ -135,3 +135,5 @@ options MODULAR_DEFAULT_AUTOLOAD # userland interface to drivers, including autoconf and properties retrieval -no pseudo-device drvctl + +options TRAP_SIGDEBUG Index: sys/conf/files =================================================================== RCS file: /cvsroot/src/sys/conf/files,v retrieving revision 1.1166 diff -u -p -u -r1.1166 files --- sys/conf/files 2 Nov 2016 00:11:59 -0000 1.1166 +++ sys/conf/files 13 Dec 2016 12:02:48 -0000 @@ -330,6 +330,7 @@ define onewirebus { } define pckbport {[slot = -1]} define pckbport_machdep_cnattach define firmload +define spkrbus { } include "dev/files.audio" @@ -344,6 +345,10 @@ file dev/dev_verbose.c device video attach video at videobus +# speaker devices, attaches to audio or pcppi drivers +device spkr {} +attach spkr at spkrbus + # DTV subsystem # include "dev/dtv/files.dtv" Index: sys/dev/files.audio =================================================================== RCS file: /cvsroot/src/sys/dev/files.audio,v retrieving revision 1.7 diff -u -p -u -r1.7 files.audio --- sys/dev/files.audio 11 Dec 2016 06:30:11 -0000 1.7 +++ sys/dev/files.audio 13 Dec 2016 12:02:48 -0000 @@ -17,8 +17,8 @@ device audio {}: audiodev, auconv, aurat attach audio at audiobus device midi: audio attach midi at midibus -device spkr: audiobell -attach spkr at audio with spkr_synth +device spkr_synth {}: spkrbus +attach spkr_synth at audio # console bell via audio device # @@ -26,7 +26,7 @@ define audiobell file dev/auconv.c auconv file dev/audio.c audio needs-flag -file dev/audiobell.c audiobell needs-flag +file dev/audiobell.c spkr_synth needs-flag file dev/aurateconv.c aurateconv needs-flag file dev/auvolconv.c auvolconv file dev/midi.c midi needs-flag Index: sys/dev/spkr.c =================================================================== RCS file: /cvsroot/src/sys/dev/spkr.c,v retrieving revision 1.3 diff -u -p -u -r1.3 spkr.c --- sys/dev/spkr.c 9 Dec 2016 05:17:03 -0000 1.3 +++ sys/dev/spkr.c 13 Dec 2016 12:02:48 -0000 @@ -62,6 +62,8 @@ __KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.3 #include #include +#define SPKRDEBUG + dev_type_open(spkropen); dev_type_close(spkrclose); dev_type_write(spkrwrite); @@ -82,9 +84,26 @@ const struct cdevsw spkr_cdevsw = { .d_flag = D_OTHER }; -static void playinit(void); -static void playtone(int, int, int); -static void playstring(char *, int); +struct spkr_softc { + device_t sc_dev; + int sc_octave; /* currently selected octave */ + int sc_whole; /* whole-note time at current tempo, in ticks */ + int sc_value; /* whole divisor for note time, quarter note = 1 */ + int sc_fill; /* controls spacing of notes */ + bool sc_octtrack; /* octave-tracking on? */ + bool sc_octprefix; /* override current octave-tracking state? */ + char *sc_inbuf; + + /* attachment-specific hooks */ + device_t sc_hwdev; + void (*sc_tone)(device_t, u_int, u_int); + void (*sc_rest)(device_t, int); +}; + + +static void playinit(struct spkr_softc *); +static void playtone(struct spkr_softc *, int, int, int); +static void playstring(struct spkr_softc *, char *, int); /**************** PLAY STRING INTERPRETER BEGINS HERE ********************** * @@ -96,13 +115,6 @@ static void playstring(char *, int); #define dtoi(c) ((c) - '0') -static int octave; /* currently selected octave */ -static int whole; /* whole-note time at current tempo, in ticks */ -static int value; /* whole divisor for note time, quarter note = 1 */ -static int fill; /* controls spacing of notes */ -static bool octtrack; /* octave-tracking on? */ -static bool octprefix; /* override current octave-tracking state? */ - /* * Magic number avoidance... */ @@ -144,221 +156,211 @@ static const int pitchtab[] = #define NOCTAVES (int)(__arraycount(pitchtab) / OCTAVE_NOTES) static void -playinit(void) +playinit(struct spkr_softc *sc) { - octave = DFLT_OCTAVE; - whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; - fill = NORMAL; - value = DFLT_VALUE; - octtrack = false; - octprefix = true; /* act as though there was an initial O(n) */ + sc->sc_octave = DFLT_OCTAVE; + sc->sc_whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; + sc->sc_fill = NORMAL; + sc->sc_value = DFLT_VALUE; + sc->sc_octtrack = false; + sc->sc_octprefix = true;/* act as though there was an initial O(n) */ } -static void -playtone(int pitch, int val, int sustain) /* play tone of proper duration for current rhythm signature */ +static void +playtone(struct spkr_softc *sc, int pitch, int val, int sustain) { - int sound, silence, snum = 1, sdenom = 1; + int sound, silence, snum = 1, sdenom = 1; - /* this weirdness avoids floating-point arithmetic */ - for (; sustain; sustain--) - { - snum *= NUM_MULT; - sdenom *= DENOM_MULT; - } + /* this weirdness avoids floating-point arithmetic */ + for (; sustain; sustain--) { + snum *= NUM_MULT; + sdenom *= DENOM_MULT; + } - if (pitch == -1) - spkr_rest(whole * snum / (val * sdenom)); - else - { - sound = (whole * snum) / (val * sdenom) - - (whole * (FILLTIME - fill)) / (val * FILLTIME); - silence = whole * (FILLTIME-fill) * snum / (FILLTIME * val * sdenom); + if (pitch == -1) { + (*sc->sc_rest)(sc->sc_hwdev, sc->sc_whole + * snum / (val * sdenom)); + return; + } + + int fac = sc->sc_whole * (FILLTIME - sc->sc_fill); + int fval = FILLTIME * val; + sound = (sc->sc_whole * snum) / (val * sdenom) - fac / fval; + silence = fac * snum / (fval * sdenom); #ifdef SPKRDEBUG - printf("playtone: pitch %d for %d ticks, rest for %d ticks\n", + aprint_debug_dev(sc->sc_hwdev, + "%s: pitch %d for %d ticks, rest for %d ticks\n", __func__, pitch, sound, silence); #endif /* SPKRDEBUG */ - spkr_tone(pitchtab[pitch], sound); - if (fill != LEGATO) - spkr_rest(silence); - } + (*sc->sc_tone)(sc->sc_hwdev, pitchtab[pitch], sound); + if (sc->sc_fill != LEGATO) + (*sc->sc_rest)(sc->sc_hwdev, silence); } -static void -playstring(char *cp, int slen) /* interpret and play an item from a notation string */ +static void +playstring(struct spkr_softc *sc, char *cp, int slen) { - int pitch, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE; + int pitch, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE; -#define GETNUM(cp, v) for(v=0; slen > 0 && isdigit(cp[1]); ) \ - {v = v * 10 + (*++cp - '0'); slen--;} - for (; slen--; cp++) - { - int sustain, timeval, tempo; - char c = toupper(*cp); +#define GETNUM(cp, v) \ + for (v = 0; slen > 0 && isdigit((unsigned char)cp[1]); ) { \ + v = v * 10 + (*++cp - '0'); \ + slen--; \ + } + + for (; slen--; cp++) { + int sustain, timeval, tempo; + char c = toupper((unsigned char)*cp); #ifdef SPKRDEBUG - printf("playstring: %c (%x)\n", c, c); + aprint_debug_dev(sc->sc_hwdev, "%s: %c (%x)\n", __func__, c, c); #endif /* SPKRDEBUG */ - switch (c) - { - case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': - - /* compute pitch */ - pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES; - - /* this may be followed by an accidental sign */ - if (slen > 0 && (cp[1] == '#' || cp[1] == '+')) - { - ++pitch; - ++cp; - slen--; - } - else if (slen > 0 && cp[1] == '-') - { - --pitch; - ++cp; - slen--; - } - - /* - * If octave-tracking mode is on, and there has been no octave- - * setting prefix, find the version of the current letter note - * closest to the last regardless of octave. - */ - if (octtrack && !octprefix) - { - if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch)) - { - if (octave < NOCTAVES - 1) { - ++octave; - pitch += OCTAVE_NOTES; - } - } + switch (c) { + case 'A': case 'B': case 'C': case 'D': + case 'E': case 'F': case 'G': + /* compute pitch */ + pitch = notetab[c - 'A'] + sc->sc_octave * OCTAVE_NOTES; + + /* this may be followed by an accidental sign */ + if (slen > 0 && (cp[1] == '#' || cp[1] == '+')) { + ++pitch; + ++cp; + slen--; + } else if (slen > 0 && cp[1] == '-') { + --pitch; + ++cp; + slen--; + } + + /* + * If octave-tracking mode is on, and there has been no + * octave- setting prefix, find the version of the + * current letter note * closest to the last + * regardless of octave. + */ + if (sc->sc_octtrack && !sc->sc_octprefix) { + int d = abs(pitch - lastpitch); + if (d > abs(pitch + OCTAVE_NOTES - lastpitch)) { + if (sc->sc_octave < NOCTAVES - 1) { + ++sc->sc_octave; + pitch += OCTAVE_NOTES; + } + } + + if (d > abs(pitch - OCTAVE_NOTES - lastpitch)) { + if (sc->sc_octave > 0) { + --sc->sc_octave; + pitch -= OCTAVE_NOTES; + } + } + } + sc->sc_octprefix = false; + lastpitch = pitch; + + /* + * ...which may in turn be followed by an override + * time value + */ + GETNUM(cp, timeval); + if (timeval <= 0 || timeval > MIN_VALUE) + timeval = sc->sc_value; + + /* ...and/or sustain dots */ + for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) { + slen--; + sustain++; + } + + /* time to emit the actual tone */ + playtone(sc, pitch, timeval, sustain); + break; + + case 'O': + if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) { + sc->sc_octprefix = sc->sc_octtrack = false; + ++cp; + slen--; + } else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) { + sc->sc_octtrack = true; + ++cp; + slen--; + } else { + GETNUM(cp, sc->sc_octave); + if (sc->sc_octave >= NOCTAVES) + sc->sc_octave = DFLT_OCTAVE; + sc->sc_octprefix = true; + } + break; + + case '>': + if (sc->sc_octave < NOCTAVES - 1) + sc->sc_octave++; + sc->sc_octprefix = true; + break; + + case '<': + if (sc->sc_octave > 0) + sc->sc_octave--; + sc->sc_octprefix = true; + break; + + case 'N': + GETNUM(cp, pitch); + for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) { + slen--; + sustain++; + } + playtone(sc, pitch - 1, sc->sc_value, sustain); + break; - if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch)) - { - if (octave > 0) { - --octave; - pitch -= OCTAVE_NOTES; - } + case 'L': + GETNUM(cp, sc->sc_value); + if (sc->sc_value <= 0 || sc->sc_value > MIN_VALUE) + sc->sc_value = DFLT_VALUE; + break; + + case 'P': + case '~': + /* this may be followed by an override time value */ + GETNUM(cp, timeval); + if (timeval <= 0 || timeval > MIN_VALUE) + timeval = sc->sc_value; + for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) { + slen--; + sustain++; + } + playtone(sc, -1, timeval, sustain); + break; + + case 'T': + GETNUM(cp, tempo); + if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) + tempo = DFLT_TEMPO; + sc->sc_whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo; + break; + + case 'M': + if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) { + sc->sc_fill = NORMAL; + ++cp; + slen--; + } else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) { + sc->sc_fill = LEGATO; + ++cp; + slen--; + } else if (slen > 0 && (cp[1] == 'S' || cp[1] == 's')) { + sc->sc_fill = STACCATO; + ++cp; + slen--; + } + break; } - } - octprefix = false; - lastpitch = pitch; - - /* ...which may in turn be followed by an override time value */ - GETNUM(cp, timeval); - if (timeval <= 0 || timeval > MIN_VALUE) - timeval = value; - - /* ...and/or sustain dots */ - for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) - { - slen--; - sustain++; - } - - /* time to emit the actual tone */ - playtone(pitch, timeval, sustain); - break; - - case 'O': - if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) - { - octprefix = octtrack = false; - ++cp; - slen--; - } - else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) - { - octtrack = true; - ++cp; - slen--; - } - else - { - GETNUM(cp, octave); - if (octave >= NOCTAVES) - octave = DFLT_OCTAVE; - octprefix = true; - } - break; - - case '>': - if (octave < NOCTAVES - 1) - octave++; - octprefix = true; - break; - - case '<': - if (octave > 0) - octave--; - octprefix = true; - break; - - case 'N': - GETNUM(cp, pitch); - for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) - { - slen--; - sustain++; - } - playtone(pitch - 1, value, sustain); - break; - - case 'L': - GETNUM(cp, value); - if (value <= 0 || value > MIN_VALUE) - value = DFLT_VALUE; - break; - - case 'P': - case '~': - /* this may be followed by an override time value */ - GETNUM(cp, timeval); - if (timeval <= 0 || timeval > MIN_VALUE) - timeval = value; - for (sustain = 0; slen > 0 && cp[1] == '.'; cp++) - { - slen--; - sustain++; - } - playtone(-1, timeval, sustain); - break; - - case 'T': - GETNUM(cp, tempo); - if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) - tempo = DFLT_TEMPO; - whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo; - break; - - case 'M': - if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n')) - { - fill = NORMAL; - ++cp; - slen--; - } - else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l')) - { - fill = LEGATO; - ++cp; - slen--; - } - else if (slen > 0 && (cp[1] == 'S' || cp[1] == 's')) - { - fill = STACCATO; - ++cp; - slen--; - } - break; } - } } /******************* UNIX DRIVER HOOKS BEGIN HERE ************************** @@ -366,118 +368,163 @@ playstring(char *cp, int slen) * This section implements driver hooks to run playstring() and the spkr_tone() * and spkr_rest() functions defined above. */ +static int spkrprobe(device_t, cfdata_t, void *); +static void spkrattach(device_t, device_t, void *); -static int spkr_active; /* exclusion flag */ -int spkr_attached; -static void *spkr_inbuf; +CFATTACH_DECL3_NEW(spkr, sizeof(struct spkr_softc), + spkrprobe, spkrattach, NULL, NULL, NULL, NULL, + DVF_DETACH_SHUTDOWN); -int -spkr_probe(device_t parent, cfdata_t match, void *aux) +extern struct cfdriver spkr_cd; +#define spkrenter(d) device_lookup_private(&spkr_cd, d) + +static int +spkrprobe(device_t parent, cfdata_t match, void *aux) { - return (!spkr_attached); + struct spkr_attach_args *sa = aux; + return sa->sa_sig == SPKR_SIG; +} + +static void +spkrattach(device_t parent, device_t self, void *aux) +{ + struct spkr_softc *sc = device_private(self); + struct spkr_attach_args *sa = aux; + + sc->sc_dev = self; + sc->sc_hwdev = sa->sa_dev; + sc->sc_rest = sa->sa_rest; + sc->sc_tone = sa->sa_tone; + sc->sc_inbuf = NULL; + aprint_normal("\n"); } int spkropen(dev_t dev, int flags, int mode, struct lwp *l) { #ifdef SPKRDEBUG - printf("spkropen: entering with dev = %"PRIx64"\n", dev); + aprint_debug("%s: entering with dev = %"PRIx64"\n", __func__, dev); #endif /* SPKRDEBUG */ + struct spkr_softc *sc = spkrenter(minor(dev)); - if (minor(dev) != 0 || !spkr_attached) - return(ENXIO); - else if (spkr_active) - return(EBUSY); - else - { - playinit(); - spkr_inbuf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK); - spkr_active = 1; - } - return(0); + if (sc == NULL) + return ENXIO; + if (sc->sc_inbuf) + return EBUSY; + + sc->sc_inbuf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK); + playinit(sc); + return 0; } int spkrwrite(dev_t dev, struct uio *uio, int flags) { - int n; - int error; + int n; + int error; #ifdef SPKRDEBUG - printf("spkrwrite: entering with dev = %"PRIx64", count = %zu\n", - dev, uio->uio_resid); + aprint_debug("%s: entering with dev = %"PRIx64", count = %zu\n", + __func__, dev, uio->uio_resid); #endif /* SPKRDEBUG */ + struct spkr_softc *sc = spkrenter(minor(dev)); + + if (sc == NULL) + return ENXIO; + if (!sc->sc_inbuf) + return EINVAL; - if (minor(dev) != 0) - return(ENXIO); - else - { n = min(DEV_BSIZE, uio->uio_resid); - error = uiomove(spkr_inbuf, n, uio); - if (!error) - playstring((char *)spkr_inbuf, n); - return(error); - } + error = uiomove(sc->sc_inbuf, n, uio); + if (error) + return error; + playstring(sc, sc->sc_inbuf, n); + return 0; } int spkrclose(dev_t dev, int flags, int mode, struct lwp *l) { #ifdef SPKRDEBUG - printf("spkrclose: entering with dev = %"PRIx64"\n", dev); + aprint_debug("%s: entering with dev = %"PRIx64"\n", __func__, dev); #endif /* SPKRDEBUG */ + struct spkr_softc *sc = spkrenter(minor(dev)); + + if (sc == NULL) + return ENXIO; + if (!sc->sc_inbuf) + return EINVAL; + + sc->sc_tone(sc->sc_hwdev, 0, 0); + free(sc->sc_inbuf, M_DEVBUF); + sc->sc_inbuf = NULL; + + return 0; +} - if (minor(dev) != 0) - return(ENXIO); +static void +playonetone(struct spkr_softc *sc, tone_t *tp) +{ + if (tp->frequency == 0) + (*sc->sc_rest)(sc->sc_dev, tp->duration); else - { - spkr_tone(0, 0); - free(spkr_inbuf, M_DEVBUF); - spkr_active = 0; - } - return(0); + (*sc->sc_tone)(sc->sc_dev, tp->frequency, tp->duration); +} + +static int +spkr_print(void *aux, const char *pnp) +{ + struct spkr_attach_args *sa = aux; + if (pnp && sa->sa_sig == SPKR_SIG) + aprint_normal("%s at %s", sa->sa_type, pnp); + return UNCONF; +} + +device_t +spkr_attach_mi(struct spkr_attach_args *sa, device_t dev) +{ + if (sa->sa_sig != SPKR_SIG) + panic("bad sig %x", sa->sa_sig); + return config_found(dev, sa, spkr_print); } int spkrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) { + tone_t *tp; + tone_t ttp; + int error; #ifdef SPKRDEBUG - printf("spkrioctl: entering with dev = %"PRIx64", cmd = %lx\n", dev, cmd); + aprint_debug("%s: entering with dev = %"PRIx64", cmd = %lx\n", + __func__, dev, cmd); #endif /* SPKRDEBUG */ - if (minor(dev) != 0) - return(ENXIO); - else if (cmd == SPKRTONE) - { - tone_t *tp = (tone_t *)data; - - if (tp->frequency == 0) - spkr_rest(tp->duration); - else - spkr_tone(tp->frequency, tp->duration); - } - else if (cmd == SPKRTUNE) - { - tone_t *tp = (tone_t *)(*(void **)data); - tone_t ttp; - int error; + struct spkr_softc *sc = spkrenter(minor(dev)); - for (; ; tp++) { - error = copyin(tp, &ttp, sizeof(tone_t)); - if (error) - return(error); - if (ttp.duration == 0) - break; - if (ttp.frequency == 0) - spkr_rest(ttp.duration); - else - spkr_tone(ttp.frequency, ttp.duration); + if (sc == NULL) + return ENXIO; + if (!sc->sc_inbuf) + return EINVAL; + + switch (cmd) { + case SPKRTONE: + playonetone(sc, data); + return 0; + case SPKRTUNE: + for (tp = *(void **)data;; tp++) { + error = copyin(tp, &ttp, sizeof(tone_t)); + if (error) + return(error); + if (ttp.duration == 0) + break; + playonetone(sc, &ttp); + } + return 0; + default: + return ENOTTY; } - } - else - return(EINVAL); - return(0); } +#ifdef notyet #ifdef _MODULE extern struct cfdriver spkr_cd; #include "ioconf.c" @@ -522,3 +569,4 @@ spkr__modcmd(modcmd_t cmd, void *arg) return 0; #endif } +#endif Index: sys/dev/spkr_synth.c =================================================================== RCS file: /cvsroot/src/sys/dev/spkr_synth.c,v retrieving revision 1.6 diff -u -p -u -r1.6 spkr_synth.c --- sys/dev/spkr_synth.c 12 Dec 2016 10:46:39 -0000 1.6 +++ sys/dev/spkr_synth.c 13 Dec 2016 12:02:48 -0000 @@ -48,90 +48,128 @@ __KERNEL_RCSID(0, "$NetBSD: spkr_synth.c #include struct vbell_args { - device_t *cookie; u_int pitch; u_int period; u_int volume; bool dying; }; +#define SPKRDEBUG + static void bell_thread(void *) __dead; +#ifdef notyet static int beep_sysctl_device(SYSCTLFN_PROTO); +#endif #include #include #include -static void spkrattach(device_t, device_t, void *); -static int spkrdetach(device_t, int); -device_t speakerattach_mi(device_t); +static int spkr_synth_probe(device_t, cfdata_t, void *); +static void spkr_synth_attach(device_t, device_t, void *); +static int spkr_synth_detach(device_t, int); + +struct spkr_synth_softc { + device_t sc_dev; + device_t sc_spkr_dev; + lwp_t *sc_bellthread; + kmutex_t sc_bellock; + kcondvar_t sc_bellcv; + struct vbell_args sc_bell_args; +}; +#ifdef notyet #include "ioconf.h" MODULE(MODULE_CLASS_DRIVER, spkr, NULL /* "audio" */); + static int spkr_modcmd(modcmd_t cmd, void *arg) { return spkr__modcmd(cmd, arg); } +#endif -CFATTACH_DECL3_NEW(spkr_synth, 0, - spkr_probe, spkrattach, spkrdetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN); +CFATTACH_DECL3_NEW(spkr_synth, sizeof(struct spkr_synth_softc), + spkr_synth_probe, spkr_synth_attach, spkr_synth_detach, + NULL, NULL, NULL, DVF_DETACH_SHUTDOWN); +#ifdef notyet extern struct cfdriver audio_cd; - static struct sysctllog *spkr_sc_log; /* sysctl log */ -static int beep_unit = 0; +#endif -struct vbell_args sc_bell_args; -lwp_t *sc_bellthread; -kmutex_t sc_bellock; -kcondvar_t sc_bellcv; -struct spkr_attach_args { - device_t dev; -}; - -void -spkr_tone(u_int xhz, u_int ticks) +static void +spkr_synth_tone(device_t self, u_int xhz, u_int ticks) { + int beep_unit = device_unit(self); + audiobell(&beep_unit, xhz, ticks * (1000 / hz), 80, 0); } -void -spkr_rest(int ticks) +static void +spkr_synth_rest(device_t self, int ticks) { + int beep_unit = device_unit(self); + #ifdef SPKRDEBUG - printf("%s: %d\n", __func__, ticks); + aprint_normal_dev(self, "%s: %d\n", __func__, ticks); #endif /* SPKRDEBUG */ - if (ticks > 0) - audiobell(&beep_unit, 0, ticks * (1000 / hz), 80, 0); + if (ticks > 0) + audiobell(&beep_unit, 0, ticks * (1000 / hz), 80, 0); } -device_t -speakerattach_mi(device_t dev) +#ifdef notyet +static void +spkr_synth_play(device_t self, u_int pitch, u_int period, u_int volume) { - struct spkr_attach_args sa; - sa.dev = dev; - return config_found(dev, &sa, NULL); + struct spkr_synth_softc *sc = device_private(self); + + mutex_enter(&sc->sc_bellock); + sc->sc_bell_args.dying = false; + sc->sc_bell_args.pitch = pitch; + sc->sc_bell_args.period = period; + sc->sc_bell_args.volume = volume; + + cv_broadcast(&sc->sc_bellcv); + mutex_exit(&sc->sc_bellock); +} +#endif + +static int +spkr_synth_probe(device_t parent, cfdata_t cf, void *aux) +{ +printf("%s\n", __func__); + return 1; } static void -spkrattach(device_t parent, device_t self, void *aux) +spkr_synth_attach(device_t parent, device_t self, void *aux) { - const struct sysctlnode *node; + struct spkr_synth_softc *sc = device_private(self); + struct spkr_attach_args sa; - printf("\n"); - beep_unit = 0; - spkr_attached = 1; +printf("%s\n", __func__); + aprint_normal("\n"); + sc->sc_dev = self; if (!pmf_device_register(self, NULL, NULL)) aprint_error_dev(self, "couldn't establish power handler\n"); - mutex_init(&sc_bellock, MUTEX_DEFAULT, IPL_SCHED); - cv_init(&sc_bellcv, "bellcv"); + mutex_init(&sc->sc_bellock, MUTEX_DEFAULT, IPL_SCHED); + cv_init(&sc->sc_bellcv, "bellcv"); + + sa.sa_sig = SPKR_SIG; + sa.sa_type = "audio"; + sa.sa_dev = self; + sa.sa_rest = spkr_synth_rest; + sa.sa_tone = spkr_synth_tone; + sc->sc_spkr_dev = spkr_attach_mi(&sa, self); +#ifdef notyet + const struct sysctlnode *node; /* sysctl set-up for default audio device */ sysctl_createv(&spkr_sc_log, 0, NULL, &node, 0, @@ -152,76 +190,70 @@ spkrattach(device_t parent, device_t sel CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL); } +#endif kthread_create(PRI_BIO, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL, - bell_thread, &sc_bell_args, &sc_bellthread, "vbell"); + bell_thread, sc, &sc->sc_bellthread, device_xname(self)); } static int -spkrdetach(device_t self, int flags) +spkr_synth_detach(device_t self, int flags) { + struct spkr_synth_softc *sc = device_private(self); + int error; + + if ((error = config_detach(sc->sc_spkr_dev, 0)) != 0) + return error; pmf_device_deregister(self); - mutex_enter(&sc_bellock); - sc_bell_args.dying = true; + mutex_enter(&sc->sc_bellock); + sc->sc_bell_args.dying = true; - cv_broadcast(&sc_bellcv); - mutex_exit(&sc_bellock); + cv_broadcast(&sc->sc_bellcv); + mutex_exit(&sc->sc_bellock); - kthread_join(sc_bellthread); - cv_destroy(&sc_bellcv); - mutex_destroy(&sc_bellock); + kthread_join(sc->sc_bellthread); + cv_destroy(&sc->sc_bellcv); + mutex_destroy(&sc->sc_bellock); +#ifdef notyet /* delete sysctl nodes */ sysctl_teardown(&spkr_sc_log); - - spkr_attached = 0; +#endif return 0; } -void +static void bell_thread(void *arg) { - struct vbell_args *vb = arg; + struct spkr_synth_softc *sc = arg; + struct vbell_args *vb = &sc->sc_bell_args; + int beep_unit = device_unit(sc->sc_dev); u_int bpitch; u_int bperiod; u_int bvolume; for (;;) { - mutex_enter(&sc_bellock); - cv_wait_sig(&sc_bellcv, &sc_bellock); + mutex_enter(&sc->sc_bellock); + cv_wait_sig(&sc->sc_bellcv, &sc->sc_bellock); if (vb->dying == true) { - mutex_exit(&sc_bellock); + mutex_exit(&sc->sc_bellock); kthread_exit(0); } bpitch = vb->pitch; bperiod = vb->period; bvolume = vb->volume; - mutex_exit(&sc_bellock); + mutex_exit(&sc->sc_bellock); audiobell(&beep_unit, bpitch, bperiod, bvolume, 0); } } -void -speaker_play(u_int pitch, u_int period, u_int volume) -{ - if (spkr_attached == 0 || beep_unit == -1) - return; - - mutex_enter(&sc_bellock); - sc_bell_args.dying = false; - sc_bell_args.pitch = pitch; - sc_bell_args.period = period; - sc_bell_args.volume = volume; - - cv_broadcast(&sc_bellcv); - mutex_exit(&sc_bellock); -} +#ifdef notyet /* sysctl helper to set common audio channels */ static int beep_sysctl_device(SYSCTLFN_ARGS) @@ -247,3 +279,4 @@ beep_sysctl_device(SYSCTLFN_ARGS) return error; } +#endif Index: sys/dev/spkrvar.h =================================================================== RCS file: /cvsroot/src/sys/dev/spkrvar.h,v retrieving revision 1.3 diff -u -p -u -r1.3 spkrvar.h --- sys/dev/spkrvar.h 9 Dec 2016 05:45:20 -0000 1.3 +++ sys/dev/spkrvar.h 13 Dec 2016 12:02:48 -0000 @@ -5,14 +5,18 @@ #include -device_t speakerattach_mi(device_t); -void speaker_play(u_int, u_int, u_int); +struct spkr_attach_args { + int sa_sig; + const char *sa_type; + device_t sa_dev; + void (*sa_tone)(device_t, u_int, u_int); + void (*sa_rest)(device_t, int); +}; + +#define SPKR_SIG (('s' << 24) | ('p' << 16) | ('k' << 8) | 'r') + +device_t spkr_attach_mi(struct spkr_attach_args *, device_t); -// XXX: -void spkr_tone(u_int, u_int); -void spkr_rest(int); int spkr__modcmd(modcmd_t, void *); -int spkr_probe(device_t, cfdata_t, void *); -extern int spkr_attached; #endif /* _SYS_DEV_SPKRVAR_H */ Index: sys/dev/isa/files.isa =================================================================== RCS file: /cvsroot/src/sys/dev/isa/files.isa,v retrieving revision 1.167 diff -u -p -u -r1.167 files.isa --- sys/dev/isa/files.isa 9 Dec 2016 04:32:39 -0000 1.167 +++ sys/dev/isa/files.isa 13 Dec 2016 12:02:48 -0000 @@ -434,7 +434,8 @@ device pcppi {} attach pcppi at isa file dev/isa/pcppi.c pcppi needs-flag -attach spkr at pcppi with spkr_pcppi +device spkr_pcppi {}: spkrbus +attach spkr_pcppi at pcppi file dev/isa/spkr_pcppi.c spkr_pcppi attach midi at pcppi with midi_pcppi: midisyn Index: sys/dev/isa/spkr_pcppi.c =================================================================== RCS file: /cvsroot/src/sys/dev/isa/spkr_pcppi.c,v retrieving revision 1.4 diff -u -p -u -r1.4 spkr_pcppi.c --- sys/dev/isa/spkr_pcppi.c 9 Dec 2016 05:17:03 -0000 1.4 +++ sys/dev/isa/spkr_pcppi.c 13 Dec 2016 12:02:48 -0000 @@ -64,10 +64,17 @@ __KERNEL_RCSID(0, "$NetBSD: spkr_pcppi.c #include #include -extern int spkr_attached; -static void spkrattach(device_t, device_t, void *); -static int spkrdetach(device_t, int); +struct spkr_pcppi_softc { + device_t sc_dev; + device_t sc_spkr_dev; + pcppi_tag_t sc_pcppicookie; +}; + +static int spkr_pcppi_probe(device_t, cfdata_t, void *); +static void spkr_pcppi_attach(device_t, device_t, void *); +static int spkr_pcppi_detach(device_t, int); +#ifdef notyet #include "ioconf.h" MODULE(MODULE_CLASS_DRIVER, spkr, NULL /* "pcppi" */); @@ -77,54 +84,76 @@ spkr_modcmd(modcmd_t cmd, void *arg) { return spkr__modcmd(cmd, arg); } +#endif -CFATTACH_DECL_NEW(spkr_pcppi, 0, spkr_probe, spkrattach, spkrdetach, NULL); - -static pcppi_tag_t ppicookie; +CFATTACH_DECL_NEW(spkr_pcppi, sizeof(struct spkr_pcppi_softc), + spkr_pcppi_probe, spkr_pcppi_attach, spkr_pcppi_detach, NULL); #define SPKRPRI (PZERO - 1) -void -spkr_tone(u_int xhz, u_int ticks) +#define SPKRDEBUG + /* emit tone of frequency hz for given number of ticks */ +static void +spkr_pcppi_tone(device_t self, u_int xhz, u_int ticks) { - pcppi_bell(ppicookie, xhz, ticks, PCPPI_BELL_SLEEP); + struct spkr_pcppi_softc *sc = device_private(self); + pcppi_bell(sc->sc_pcppicookie, xhz, ticks, PCPPI_BELL_SLEEP); } -void -spkr_rest(int ticks) /* rest for given number of ticks */ +static void +spkr_pcppi_rest(device_t self, int ticks) { - /* - * Set timeout to endrest function, then give up the timeslice. - * This is so other processes can execute while the rest is being - * waited out. - */ + /* + * Set timeout to endrest function, then give up the timeslice. + * This is so other processes can execute while the rest is being + * waited out. + */ #ifdef SPKRDEBUG - printf("%s: %d\n", __func__, ticks); + aprint_normal_dev(self, "%s: %d\n", __func__, ticks); #endif /* SPKRDEBUG */ - if (ticks > 0) - tsleep(spkr_rest, SPKRPRI | PCATCH, "rest", ticks); + if (ticks > 0) + tsleep(self, SPKRPRI | PCATCH, device_xname(self), ticks); +} + +static int +spkr_pcppi_probe(device_t parent, cfdata_t cf, void *aux) +{ + return 1; } static void -spkrattach(device_t parent, device_t self, void *aux) +spkr_pcppi_attach(device_t parent, device_t self, void *aux) { + struct pcppi_attach_args *pa = aux; + struct spkr_attach_args sa; + struct spkr_pcppi_softc *sc = device_private(self); + aprint_naive("\n"); aprint_normal("\n"); - ppicookie = ((struct pcppi_attach_args *)aux)->pa_cookie; - spkr_attached = 1; + sa.sa_sig = SPKR_SIG; + sa.sa_type = "pcppi"; + sa.sa_dev = self; + sa.sa_rest = spkr_pcppi_rest; + sa.sa_tone = spkr_pcppi_tone; + + sc->sc_dev = self; + sc->sc_spkr_dev = spkr_attach_mi(&sa, self); + sc->sc_pcppicookie = pa->pa_cookie; if (!pmf_device_register(self, NULL, NULL)) aprint_error_dev(self, "couldn't establish power handler\n"); } static int -spkrdetach(device_t self, int flags) +spkr_pcppi_detach(device_t self, int flags) { + int error; + struct spkr_pcppi_softc *sc = device_private(self); + if ((error = config_detach(sc->sc_spkr_dev, flags)) != 0) + return error; + sc->sc_pcppicookie = NULL; pmf_device_deregister(self); - spkr_attached = 0; - ppicookie = NULL; - return 0; }