#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/sx.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/ctype.h>
#include <sys/malloc.h>
#include <machine/clock.h>
#include <dev/speaker/speaker.h>
static d_open_t spkropen;
static d_close_t spkrclose;
static d_write_t spkrwrite;
static d_ioctl_t spkrioctl;
static struct cdevsw spkr_cdevsw = {
.d_version = D_VERSION,
.d_open = spkropen,
.d_close = spkrclose,
.d_write = spkrwrite,
.d_ioctl = spkrioctl,
.d_name = "spkr",
};
static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
#define SPKRPRI PSOCK
static char endtone, endrest;
struct spkr_state;
static void tone(unsigned int thz, unsigned int centisecs);
static void rest(int centisecs);
static void playinit(struct spkr_state *state);
static void playtone(struct spkr_state *state, int pitch, int value, int sustain);
static void playstring(struct spkr_state *state, char *cp, size_t slen);
static void
tone(unsigned int thz, unsigned int centisecs)
{
int timo;
if (thz <= 0)
return;
#ifdef DEBUG
(void) printf("tone: thz=%d centisecs=%d\n", thz, centisecs);
#endif
if (timer_spkr_acquire()) {
return;
}
timer_spkr_setfreq(thz);
timo = centisecs * hz / 100;
if (timo > 0)
tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", timo);
timer_spkr_release();
}
static void
rest(int centisecs)
{
int timo;
#ifdef DEBUG
(void) printf("rest: %d\n", centisecs);
#endif
timo = centisecs * hz / 100;
if (timo > 0)
tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", timo);
}
#define dtoi(c) ((c) - '0')
struct spkr_state {
char *inbuf;
int octave;
int whole;
int value;
int fill;
bool octtrack;
bool octprefix;
};
#define SECS_PER_MIN 60
#define WHOLE_NOTE 4
#define MIN_VALUE 64
#define DFLT_VALUE 4
#define FILLTIME 8
#define STACCATO 6
#define NORMAL 7
#define LEGATO 8
#define DFLT_OCTAVE 4
#define MIN_TEMPO 32
#define DFLT_TEMPO 120
#define MAX_TEMPO 255
#define NUM_MULT 3
#define DENOM_MULT 2
static const int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
#define OCTAVE_NOTES 12
static const int pitchtab[] =
{
65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123,
131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247,
262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
};
static void
playinit(struct spkr_state *state)
{
state->octave = DFLT_OCTAVE;
state->whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
state->fill = NORMAL;
state->value = DFLT_VALUE;
state->octtrack = false;
state->octprefix = true;
}
static void
playtone(struct spkr_state *state, int pitch, int value, int sustain)
{
int sound, silence, snum = 1, sdenom = 1;
int whole = state->whole;
int fill = state->fill;
for (; sustain; sustain--) {
snum *= NUM_MULT;
sdenom *= DENOM_MULT;
}
if (value == 0 || sdenom == 0)
return;
if (pitch == -1)
rest(whole * snum / (value * sdenom));
else {
sound = (whole * snum) / (value * sdenom)
- (whole * (FILLTIME - fill)) / (value * FILLTIME);
silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
#ifdef DEBUG
(void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
pitch, sound, silence);
#endif
tone(pitchtab[pitch], sound);
if (fill != LEGATO)
rest(silence);
}
}
static void
playstring(struct spkr_state *state, char *cp, size_t slen)
{
int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
#define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
{v = v * 10 + (*++cp - '0'); slen--;}
for (; slen--; cp++) {
int sustain, timeval, tempo;
char c = toupper(*cp);
#ifdef DEBUG
(void) printf("playstring: %c (%x)\n", c, c);
#endif
switch (c) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
pitch = notetab[c - 'A'] + state->octave * OCTAVE_NOTES;
if (cp[1] == '#' || cp[1] == '+') {
++pitch;
++cp;
slen--;
} else if (cp[1] == '-') {
--pitch;
++cp;
slen--;
}
if (state->octtrack && !state->octprefix) {
if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES -
lastpitch)) {
++state->octave;
pitch += OCTAVE_NOTES;
}
if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES) -
lastpitch)) {
--state->octave;
pitch -= OCTAVE_NOTES;
}
}
state->octprefix = false;
lastpitch = pitch;
GETNUM(cp, timeval);
if (timeval <= 0 || timeval > MIN_VALUE)
timeval = state->value;
for (sustain = 0; cp[1] == '.'; cp++) {
slen--;
sustain++;
}
oldfill = state->fill;
if (cp[1] == '_') {
state->fill = LEGATO;
++cp;
slen--;
}
playtone(state, pitch, timeval, sustain);
state->fill = oldfill;
break;
case 'O':
if (cp[1] == 'N' || cp[1] == 'n') {
state->octprefix = state->octtrack = false;
++cp;
slen--;
} else if (cp[1] == 'L' || cp[1] == 'l') {
state->octtrack = true;
++cp;
slen--;
} else {
GETNUM(cp, state->octave);
if (state->octave >= nitems(pitchtab) / OCTAVE_NOTES)
state->octave = DFLT_OCTAVE;
state->octprefix = true;
}
break;
case '>':
if (state->octave < nitems(pitchtab) / OCTAVE_NOTES - 1)
state->octave++;
state->octprefix = true;
break;
case '<':
if (state->octave > 0)
state->octave--;
state->octprefix = true;
break;
case 'N':
GETNUM(cp, pitch);
for (sustain = 0; cp[1] == '.'; cp++) {
slen--;
sustain++;
}
oldfill = state->fill;
if (cp[1] == '_') {
state->fill = LEGATO;
++cp;
slen--;
}
playtone(state, pitch - 1, state->value, sustain);
state->fill = oldfill;
break;
case 'L':
GETNUM(cp, state->value);
if (state->value <= 0 || state->value > MIN_VALUE)
state->value = DFLT_VALUE;
break;
case 'P':
case '~':
GETNUM(cp, timeval);
if (timeval <= 0 || timeval > MIN_VALUE)
timeval = state->value;
for (sustain = 0; cp[1] == '.'; cp++) {
slen--;
sustain++;
}
playtone(state, -1, timeval, sustain);
break;
case 'T':
GETNUM(cp, tempo);
if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
tempo = DFLT_TEMPO;
state->whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / tempo;
break;
case 'M':
if (cp[1] == 'N' || cp[1] == 'n') {
state->fill = NORMAL;
++cp;
slen--;
} else if (cp[1] == 'L' || cp[1] == 'l') {
state->fill = LEGATO;
++cp;
slen--;
} else if (cp[1] == 'S' || cp[1] == 's') {
state->fill = STACCATO;
++cp;
slen--;
}
break;
}
}
}
static struct sx spkr_op_locked;
static void
spkr_dtor(void *data)
{
struct spkr_state *state = data;
free(state->inbuf, M_SPKR);
free(state, M_SPKR);
}
static int
spkropen(struct cdev *dev, int flags, int fmt, struct thread *td)
{
struct spkr_state *state;
int error;
#ifdef DEBUG
(void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
#endif
state = malloc(sizeof(*state), M_SPKR, M_WAITOK | M_ZERO);
state->inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
playinit(state);
error = devfs_set_cdevpriv(state, spkr_dtor);
if (error) {
free(state->inbuf, M_SPKR);
free(state, M_SPKR);
return (error);
}
return (0);
}
static int
spkrwrite(struct cdev *dev, struct uio *uio, int ioflag)
{
struct spkr_state *state;
int error;
unsigned n;
#ifdef DEBUG
printf("spkrwrite: entering with dev = %s, count = %zd\n",
devtoname(dev), uio->uio_resid);
#endif
if (uio->uio_resid > (DEV_BSIZE - 1))
return (E2BIG);
error = devfs_get_cdevpriv((void **)&state);
if (error)
return (error);
n = uio->uio_resid;
error = uiomove(state->inbuf, n, uio);
if (error)
return (error);
state->inbuf[n] = '\0';
sx_xlock(&spkr_op_locked);
playstring(state, state->inbuf, n);
sx_xunlock(&spkr_op_locked);
return (0);
}
static int
spkrclose(struct cdev *dev, int flags, int fmt, struct thread *td)
{
#ifdef DEBUG
(void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
#endif
wakeup(&endtone);
wakeup(&endrest);
return (0);
}
static int
spkrioctl(struct cdev *dev, unsigned long cmd, caddr_t cmdarg, int flags,
struct thread *td)
{
#ifdef DEBUG
(void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
devtoname(dev), cmd);
#endif
if (cmd == SPKRTONE) {
tone_t *tp = (tone_t *)cmdarg;
sx_xlock(&spkr_op_locked);
if (tp->frequency == 0)
rest(tp->duration);
else
tone(tp->frequency, tp->duration);
sx_xunlock(&spkr_op_locked);
return (0);
} else if (cmd == SPKRTUNE) {
tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg);
tone_t ttp;
int error;
sx_xlock(&spkr_op_locked);
for (; ; tp++) {
error = copyin(tp, &ttp, sizeof(tone_t));
if (error) {
sx_xunlock(&spkr_op_locked);
return (error);
}
if (ttp.duration == 0)
break;
if (ttp.frequency == 0)
rest(ttp.duration);
else
tone(ttp.frequency, ttp.duration);
}
sx_xunlock(&spkr_op_locked);
return (0);
}
return (EINVAL);
}
static struct cdev *speaker_dev;
static int
speaker_modevent(module_t mod, int type, void *data)
{
int error = 0;
switch(type) {
case MOD_LOAD:
sx_init(&spkr_op_locked, "spkr");
speaker_dev = make_dev(&spkr_cdevsw, 0,
UID_ROOT, GID_WHEEL, 0600, "speaker");
break;
case MOD_SHUTDOWN:
case MOD_UNLOAD:
destroy_dev(speaker_dev);
sx_destroy(&spkr_op_locked);
break;
default:
error = EOPNOTSUPP;
}
return (error);
}
DEV_MODULE(speaker, speaker_modevent, NULL);