15b81b6b3SRodney W. Grimes /* 2e614aa8aSAndrey A. Chernov * spkr.c -- device driver for console speaker 35b81b6b3SRodney W. Grimes * 4e614aa8aSAndrey A. Chernov * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993 5e614aa8aSAndrey A. Chernov * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su> 630201b6cSMatthew N. Dodd * modified for PC98 by Kakefuda 76f78ca60SRodney W. Grimes * 8c3aac50fSPeter Wemm * $FreeBSD$ 95b81b6b3SRodney W. Grimes */ 105b81b6b3SRodney W. Grimes 11f540b106SGarrett Wollman #include <sys/param.h> 12f540b106SGarrett Wollman #include <sys/systm.h> 132a50a6d7SMike Smith #include <sys/bus.h> 14f540b106SGarrett Wollman #include <sys/kernel.h> 152a50a6d7SMike Smith #include <sys/module.h> 16f540b106SGarrett Wollman #include <sys/uio.h> 1787f6c662SJulian Elischer #include <sys/conf.h> 185b664c7cSPoul-Henning Kamp #include <sys/ctype.h> 19e4961fbfSPoul-Henning Kamp #include <sys/malloc.h> 202a50a6d7SMike Smith #include <isa/isavar.h> 2130201b6cSMatthew N. Dodd #ifdef PC98 2230201b6cSMatthew N. Dodd #include <pc98/pc98/pc98.h> 2330201b6cSMatthew N. Dodd #else 24f540b106SGarrett Wollman #include <i386/isa/isa.h> 2530201b6cSMatthew N. Dodd #endif 26f540b106SGarrett Wollman #include <i386/isa/timerreg.h> 27b5e8ce9fSBruce Evans #include <machine/clock.h> 28f540b106SGarrett Wollman #include <machine/speaker.h> 295b81b6b3SRodney W. Grimes 3087f6c662SJulian Elischer static d_open_t spkropen; 3187f6c662SJulian Elischer static d_close_t spkrclose; 3287f6c662SJulian Elischer static d_write_t spkrwrite; 3387f6c662SJulian Elischer static d_ioctl_t spkrioctl; 3487f6c662SJulian Elischer 3587f6c662SJulian Elischer #define CDEV_MAJOR 26 364e2f199eSPoul-Henning Kamp static struct cdevsw spkr_cdevsw = { 374e2f199eSPoul-Henning Kamp /* open */ spkropen, 384e2f199eSPoul-Henning Kamp /* close */ spkrclose, 394e2f199eSPoul-Henning Kamp /* read */ noread, 404e2f199eSPoul-Henning Kamp /* write */ spkrwrite, 414e2f199eSPoul-Henning Kamp /* ioctl */ spkrioctl, 424e2f199eSPoul-Henning Kamp /* poll */ nopoll, 434e2f199eSPoul-Henning Kamp /* mmap */ nommap, 444e2f199eSPoul-Henning Kamp /* strategy */ nostrategy, 454e2f199eSPoul-Henning Kamp /* name */ "spkr", 464e2f199eSPoul-Henning Kamp /* maj */ CDEV_MAJOR, 474e2f199eSPoul-Henning Kamp /* dump */ nodump, 484e2f199eSPoul-Henning Kamp /* psize */ nopsize, 494e2f199eSPoul-Henning Kamp /* flags */ 0, 504e2f199eSPoul-Henning Kamp }; 518af5d536SJulian Elischer 52959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer"); 53e4961fbfSPoul-Henning Kamp 545b81b6b3SRodney W. Grimes /**************** MACHINE DEPENDENT PART STARTS HERE ************************* 555b81b6b3SRodney W. Grimes * 565b81b6b3SRodney W. Grimes * This section defines a function tone() which causes a tone of given 579275c7ccSBruce Evans * frequency and duration from the ISA console speaker. 585b81b6b3SRodney W. Grimes * Another function endtone() is defined to force sound off, and there is 595b81b6b3SRodney W. Grimes * also a rest() entry point to do pauses. 605b81b6b3SRodney W. Grimes * 615b81b6b3SRodney W. Grimes * Audible sound is generated using the Programmable Interval Timer (PIT) and 629275c7ccSBruce Evans * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The 635b81b6b3SRodney W. Grimes * PPI controls whether sound is passed through at all; the PIT's channel 2 is 645b81b6b3SRodney W. Grimes * used to generate clicks (a square wave) of whatever frequency is desired. 655b81b6b3SRodney W. Grimes */ 665b81b6b3SRodney W. Grimes 675b81b6b3SRodney W. Grimes /* 6830201b6cSMatthew N. Dodd * XXX PPI control values should be in a header and used in clock.c. 695b81b6b3SRodney W. Grimes */ 7030201b6cSMatthew N. Dodd #ifdef PC98 7130201b6cSMatthew N. Dodd #define PPI_SPKR 0x08 /* turn these PPI bits on to pass sound */ 7230201b6cSMatthew N. Dodd #define PIT_COUNT 0x3fdb /* PIT count address */ 7330201b6cSMatthew N. Dodd 7430201b6cSMatthew N. Dodd #define SPEAKER_ON outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR) 7530201b6cSMatthew N. Dodd #define SPEAKER_OFF outb(IO_PPI, inb(IO_PPI) | PPI_SPKR) 7630201b6cSMatthew N. Dodd #define TIMER_ACQUIRE acquire_timer1(TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT) 7730201b6cSMatthew N. Dodd #define TIMER_RELEASE release_timer1() 7830201b6cSMatthew N. Dodd #define SPEAKER_WRITE(val) { \ 7930201b6cSMatthew N. Dodd outb(PIT_COUNT, (val & 0xff)); \ 8030201b6cSMatthew N. Dodd outb(PIT_COUNT, (val >> 8)); \ 8130201b6cSMatthew N. Dodd } 8230201b6cSMatthew N. Dodd #else 835b81b6b3SRodney W. Grimes #define PPI_SPKR 0x03 /* turn these PPI bits on to pass sound */ 845b81b6b3SRodney W. Grimes 8530201b6cSMatthew N. Dodd #define SPEAKER_ON outb(IO_PPI, inb(IO_PPI) | PPI_SPKR) 8630201b6cSMatthew N. Dodd #define SPEAKER_OFF outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR) 8730201b6cSMatthew N. Dodd #define TIMER_ACQUIRE acquire_timer2(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT) 8830201b6cSMatthew N. Dodd #define TIMER_RELEASE release_timer2() 8930201b6cSMatthew N. Dodd #define SPEAKER_WRITE(val) { \ 9030201b6cSMatthew N. Dodd outb(TIMER_CNTR2, (val & 0xff)); \ 9130201b6cSMatthew N. Dodd outb(TIMER_CNTR2, (val >> 8)); \ 9230201b6cSMatthew N. Dodd } 9330201b6cSMatthew N. Dodd #endif 9430201b6cSMatthew N. Dodd 951e064bd4SAndrey A. Chernov #define SPKRPRI PSOCK 961e064bd4SAndrey A. Chernov static char endtone, endrest; 975b81b6b3SRodney W. Grimes 9889c9a483SAlfred Perlstein static void tone(unsigned int thz, unsigned int ticks); 9989c9a483SAlfred Perlstein static void rest(int ticks); 10089c9a483SAlfred Perlstein static void playinit(void); 10189c9a483SAlfred Perlstein static void playtone(int pitch, int value, int sustain); 10289c9a483SAlfred Perlstein static void playstring(char *cp, size_t slen); 1030dfe10a6SBruce Evans 1041e064bd4SAndrey A. Chernov /* emit tone of frequency thz for given number of ticks */ 105f4de22acSJoerg Wunsch static void 106f4de22acSJoerg Wunsch tone(thz, ticks) 1071e064bd4SAndrey A. Chernov unsigned int thz, ticks; 1085b81b6b3SRodney W. Grimes { 1099714de6aSDavid Greenman unsigned int divisor; 110424e2d04SAndrey A. Chernov int sps; 1115b81b6b3SRodney W. Grimes 1129714de6aSDavid Greenman if (thz <= 0) 1139714de6aSDavid Greenman return; 1149714de6aSDavid Greenman 1159275c7ccSBruce Evans divisor = timer_freq / thz; 1169714de6aSDavid Greenman 1175b81b6b3SRodney W. Grimes #ifdef DEBUG 1181e064bd4SAndrey A. Chernov (void) printf("tone: thz=%d ticks=%d\n", thz, ticks); 1195b81b6b3SRodney W. Grimes #endif /* DEBUG */ 1205b81b6b3SRodney W. Grimes 1215b81b6b3SRodney W. Grimes /* set timer to generate clicks at given frequency in Hertz */ 122f4de22acSJoerg Wunsch sps = splclock(); 123bc4ff6e3SSøren Schmidt 12430201b6cSMatthew N. Dodd if (TIMER_ACQUIRE) { 125bc4ff6e3SSøren Schmidt /* enter list of waiting procs ??? */ 126f4de22acSJoerg Wunsch splx(sps); 127bc4ff6e3SSøren Schmidt return; 128bc4ff6e3SSøren Schmidt } 129f4de22acSJoerg Wunsch splx(sps); 130f4de22acSJoerg Wunsch disable_intr(); 13130201b6cSMatthew N. Dodd SPEAKER_WRITE(divisor); 132f4de22acSJoerg Wunsch enable_intr(); 1335b81b6b3SRodney W. Grimes 1345b81b6b3SRodney W. Grimes /* turn the speaker on */ 13530201b6cSMatthew N. Dodd SPEAKER_ON; 1365b81b6b3SRodney W. Grimes 1375b81b6b3SRodney W. Grimes /* 1385b81b6b3SRodney W. Grimes * Set timeout to endtone function, then give up the timeslice. 1395b81b6b3SRodney W. Grimes * This is so other processes can execute while the tone is being 1405b81b6b3SRodney W. Grimes * emitted. 1415b81b6b3SRodney W. Grimes */ 1423bdc031eSAndrey A. Chernov if (ticks > 0) 1433bdc031eSAndrey A. Chernov tsleep((caddr_t)&endtone, SPKRPRI | PCATCH, "spkrtn", ticks); 14430201b6cSMatthew N. Dodd SPEAKER_OFF; 145f4de22acSJoerg Wunsch sps = splclock(); 14630201b6cSMatthew N. Dodd TIMER_RELEASE; 147f4de22acSJoerg Wunsch splx(sps); 1485b81b6b3SRodney W. Grimes } 1495b81b6b3SRodney W. Grimes 1505b81b6b3SRodney W. Grimes /* rest for given number of ticks */ 151f4de22acSJoerg Wunsch static void 152f4de22acSJoerg Wunsch rest(ticks) 1535b81b6b3SRodney W. Grimes int ticks; 1545b81b6b3SRodney W. Grimes { 1555b81b6b3SRodney W. Grimes /* 1565b81b6b3SRodney W. Grimes * Set timeout to endrest function, then give up the timeslice. 1575b81b6b3SRodney W. Grimes * This is so other processes can execute while the rest is being 1585b81b6b3SRodney W. Grimes * waited out. 1595b81b6b3SRodney W. Grimes */ 1605b81b6b3SRodney W. Grimes #ifdef DEBUG 161e614aa8aSAndrey A. Chernov (void) printf("rest: %d\n", ticks); 1625b81b6b3SRodney W. Grimes #endif /* DEBUG */ 1633bdc031eSAndrey A. Chernov if (ticks > 0) 1643bdc031eSAndrey A. Chernov tsleep((caddr_t)&endrest, SPKRPRI | PCATCH, "spkrrs", ticks); 1655b81b6b3SRodney W. Grimes } 1665b81b6b3SRodney W. Grimes 1675b81b6b3SRodney W. Grimes /**************** PLAY STRING INTERPRETER BEGINS HERE ********************** 1685b81b6b3SRodney W. Grimes * 1695b81b6b3SRodney W. Grimes * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement; 170e614aa8aSAndrey A. Chernov * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave- 171e614aa8aSAndrey A. Chernov * tracking facility are added. 1725b81b6b3SRodney W. Grimes * Requires tone(), rest(), and endtone(). String play is not interruptible 1735b81b6b3SRodney W. Grimes * except possibly at physical block boundaries. 1745b81b6b3SRodney W. Grimes */ 1755b81b6b3SRodney W. Grimes 1765b81b6b3SRodney W. Grimes typedef int bool; 1775b81b6b3SRodney W. Grimes #define TRUE 1 1785b81b6b3SRodney W. Grimes #define FALSE 0 1795b81b6b3SRodney W. Grimes 1805b81b6b3SRodney W. Grimes #define dtoi(c) ((c) - '0') 1815b81b6b3SRodney W. Grimes 1825b81b6b3SRodney W. Grimes static int octave; /* currently selected octave */ 1835b81b6b3SRodney W. Grimes static int whole; /* whole-note time at current tempo, in ticks */ 1845b81b6b3SRodney W. Grimes static int value; /* whole divisor for note time, quarter note = 1 */ 1855b81b6b3SRodney W. Grimes static int fill; /* controls spacing of notes */ 1865b81b6b3SRodney W. Grimes static bool octtrack; /* octave-tracking on? */ 1875b81b6b3SRodney W. Grimes static bool octprefix; /* override current octave-tracking state? */ 1885b81b6b3SRodney W. Grimes 1895b81b6b3SRodney W. Grimes /* 1905b81b6b3SRodney W. Grimes * Magic number avoidance... 1915b81b6b3SRodney W. Grimes */ 1925b81b6b3SRodney W. Grimes #define SECS_PER_MIN 60 /* seconds per minute */ 1935b81b6b3SRodney W. Grimes #define WHOLE_NOTE 4 /* quarter notes per whole note */ 1945b81b6b3SRodney W. Grimes #define MIN_VALUE 64 /* the most we can divide a note by */ 1955b81b6b3SRodney W. Grimes #define DFLT_VALUE 4 /* default value (quarter-note) */ 1965b81b6b3SRodney W. Grimes #define FILLTIME 8 /* for articulation, break note in parts */ 1975b81b6b3SRodney W. Grimes #define STACCATO 6 /* 6/8 = 3/4 of note is filled */ 1985b81b6b3SRodney W. Grimes #define NORMAL 7 /* 7/8ths of note interval is filled */ 1995b81b6b3SRodney W. Grimes #define LEGATO 8 /* all of note interval is filled */ 2005b81b6b3SRodney W. Grimes #define DFLT_OCTAVE 4 /* default octave */ 2015b81b6b3SRodney W. Grimes #define MIN_TEMPO 32 /* minimum tempo */ 2025b81b6b3SRodney W. Grimes #define DFLT_TEMPO 120 /* default tempo */ 2035b81b6b3SRodney W. Grimes #define MAX_TEMPO 255 /* max tempo */ 2045b81b6b3SRodney W. Grimes #define NUM_MULT 3 /* numerator of dot multiplier */ 2055b81b6b3SRodney W. Grimes #define DENOM_MULT 2 /* denominator of dot multiplier */ 2065b81b6b3SRodney W. Grimes 2075b81b6b3SRodney W. Grimes /* letter to half-tone: A B C D E F G */ 2085b81b6b3SRodney W. Grimes static int notetab[8] = {9, 11, 0, 2, 4, 5, 7}; 2095b81b6b3SRodney W. Grimes 2105b81b6b3SRodney W. Grimes /* 2115b81b6b3SRodney W. Grimes * This is the American Standard A440 Equal-Tempered scale with frequencies 2125b81b6b3SRodney W. Grimes * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook... 2135b81b6b3SRodney W. Grimes * our octave 0 is standard octave 2. 2145b81b6b3SRodney W. Grimes */ 2155b81b6b3SRodney W. Grimes #define OCTAVE_NOTES 12 /* semitones per octave */ 2165b81b6b3SRodney W. Grimes static int pitchtab[] = 2175b81b6b3SRodney W. Grimes { 2185b81b6b3SRodney W. Grimes /* C C# D D# E F F# G G# A A# B*/ 2195b81b6b3SRodney W. Grimes /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123, 2205b81b6b3SRodney W. Grimes /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 2215b81b6b3SRodney W. Grimes /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 2225b81b6b3SRodney W. Grimes /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 2235b81b6b3SRodney W. Grimes /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 2245b81b6b3SRodney W. Grimes /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 2255b81b6b3SRodney W. Grimes /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902, 2265b81b6b3SRodney W. Grimes }; 2275b81b6b3SRodney W. Grimes 228f4de22acSJoerg Wunsch static void 229f4de22acSJoerg Wunsch playinit() 2305b81b6b3SRodney W. Grimes { 2315b81b6b3SRodney W. Grimes octave = DFLT_OCTAVE; 2321e064bd4SAndrey A. Chernov whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; 2335b81b6b3SRodney W. Grimes fill = NORMAL; 2345b81b6b3SRodney W. Grimes value = DFLT_VALUE; 2355b81b6b3SRodney W. Grimes octtrack = FALSE; 2365b81b6b3SRodney W. Grimes octprefix = TRUE; /* act as though there was an initial O(n) */ 2375b81b6b3SRodney W. Grimes } 2385b81b6b3SRodney W. Grimes 2395b81b6b3SRodney W. Grimes /* play tone of proper duration for current rhythm signature */ 240f4de22acSJoerg Wunsch static void 241f4de22acSJoerg Wunsch playtone(pitch, value, sustain) 2425b81b6b3SRodney W. Grimes int pitch, value, sustain; 2435b81b6b3SRodney W. Grimes { 2445b81b6b3SRodney W. Grimes register int sound, silence, snum = 1, sdenom = 1; 2455b81b6b3SRodney W. Grimes 2465b81b6b3SRodney W. Grimes /* this weirdness avoids floating-point arithmetic */ 2475b81b6b3SRodney W. Grimes for (; sustain; sustain--) 2485b81b6b3SRodney W. Grimes { 249e614aa8aSAndrey A. Chernov /* See the BUGS section in the man page for discussion */ 2505b81b6b3SRodney W. Grimes snum *= NUM_MULT; 2515b81b6b3SRodney W. Grimes sdenom *= DENOM_MULT; 2525b81b6b3SRodney W. Grimes } 2535b81b6b3SRodney W. Grimes 2549714de6aSDavid Greenman if (value == 0 || sdenom == 0) 2559714de6aSDavid Greenman return; 2569714de6aSDavid Greenman 2575b81b6b3SRodney W. Grimes if (pitch == -1) 258424e2d04SAndrey A. Chernov rest(whole * snum / (value * sdenom)); 2595b81b6b3SRodney W. Grimes else 2605b81b6b3SRodney W. Grimes { 2615b81b6b3SRodney W. Grimes sound = (whole * snum) / (value * sdenom) 2625b81b6b3SRodney W. Grimes - (whole * (FILLTIME - fill)) / (value * FILLTIME); 2635b81b6b3SRodney W. Grimes silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom); 2645b81b6b3SRodney W. Grimes 2655b81b6b3SRodney W. Grimes #ifdef DEBUG 266e614aa8aSAndrey A. Chernov (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n", 2675b81b6b3SRodney W. Grimes pitch, sound, silence); 2685b81b6b3SRodney W. Grimes #endif /* DEBUG */ 2695b81b6b3SRodney W. Grimes 270424e2d04SAndrey A. Chernov tone(pitchtab[pitch], sound); 2715b81b6b3SRodney W. Grimes if (fill != LEGATO) 272424e2d04SAndrey A. Chernov rest(silence); 2735b81b6b3SRodney W. Grimes } 2745b81b6b3SRodney W. Grimes } 2755b81b6b3SRodney W. Grimes 2765b81b6b3SRodney W. Grimes /* interpret and play an item from a notation string */ 277f4de22acSJoerg Wunsch static void 278f4de22acSJoerg Wunsch playstring(cp, slen) 2795b81b6b3SRodney W. Grimes char *cp; 2805b81b6b3SRodney W. Grimes size_t slen; 2815b81b6b3SRodney W. Grimes { 282e614aa8aSAndrey A. Chernov int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE; 2835b81b6b3SRodney W. Grimes 2845b81b6b3SRodney W. Grimes #define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \ 2855b81b6b3SRodney W. Grimes {v = v * 10 + (*++cp - '0'); slen--;} 2865b81b6b3SRodney W. Grimes for (; slen--; cp++) 2875b81b6b3SRodney W. Grimes { 2885b81b6b3SRodney W. Grimes int sustain, timeval, tempo; 2895b81b6b3SRodney W. Grimes register char c = toupper(*cp); 2905b81b6b3SRodney W. Grimes 2915b81b6b3SRodney W. Grimes #ifdef DEBUG 292e614aa8aSAndrey A. Chernov (void) printf("playstring: %c (%x)\n", c, c); 2935b81b6b3SRodney W. Grimes #endif /* DEBUG */ 2945b81b6b3SRodney W. Grimes 2955b81b6b3SRodney W. Grimes switch (c) 2965b81b6b3SRodney W. Grimes { 2975b81b6b3SRodney W. Grimes case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 2985b81b6b3SRodney W. Grimes 2995b81b6b3SRodney W. Grimes /* compute pitch */ 3005b81b6b3SRodney W. Grimes pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES; 3015b81b6b3SRodney W. Grimes 3025b81b6b3SRodney W. Grimes /* this may be followed by an accidental sign */ 3035b81b6b3SRodney W. Grimes if (cp[1] == '#' || cp[1] == '+') 3045b81b6b3SRodney W. Grimes { 3055b81b6b3SRodney W. Grimes ++pitch; 3065b81b6b3SRodney W. Grimes ++cp; 3075b81b6b3SRodney W. Grimes slen--; 3085b81b6b3SRodney W. Grimes } 3095b81b6b3SRodney W. Grimes else if (cp[1] == '-') 3105b81b6b3SRodney W. Grimes { 3115b81b6b3SRodney W. Grimes --pitch; 3125b81b6b3SRodney W. Grimes ++cp; 3135b81b6b3SRodney W. Grimes slen--; 3145b81b6b3SRodney W. Grimes } 3155b81b6b3SRodney W. Grimes 3165b81b6b3SRodney W. Grimes /* 3175b81b6b3SRodney W. Grimes * If octave-tracking mode is on, and there has been no octave- 3185b81b6b3SRodney W. Grimes * setting prefix, find the version of the current letter note 3195b81b6b3SRodney W. Grimes * closest to the last regardless of octave. 3205b81b6b3SRodney W. Grimes */ 3215b81b6b3SRodney W. Grimes if (octtrack && !octprefix) 3225b81b6b3SRodney W. Grimes { 3235b81b6b3SRodney W. Grimes if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch)) 3245b81b6b3SRodney W. Grimes { 3255b81b6b3SRodney W. Grimes ++octave; 3265b81b6b3SRodney W. Grimes pitch += OCTAVE_NOTES; 3275b81b6b3SRodney W. Grimes } 3285b81b6b3SRodney W. Grimes 3295b81b6b3SRodney W. Grimes if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch)) 3305b81b6b3SRodney W. Grimes { 3315b81b6b3SRodney W. Grimes --octave; 3325b81b6b3SRodney W. Grimes pitch -= OCTAVE_NOTES; 3335b81b6b3SRodney W. Grimes } 3345b81b6b3SRodney W. Grimes } 3355b81b6b3SRodney W. Grimes octprefix = FALSE; 3365b81b6b3SRodney W. Grimes lastpitch = pitch; 3375b81b6b3SRodney W. Grimes 3385b81b6b3SRodney W. Grimes /* ...which may in turn be followed by an override time value */ 3395b81b6b3SRodney W. Grimes GETNUM(cp, timeval); 3405b81b6b3SRodney W. Grimes if (timeval <= 0 || timeval > MIN_VALUE) 3415b81b6b3SRodney W. Grimes timeval = value; 3425b81b6b3SRodney W. Grimes 3435b81b6b3SRodney W. Grimes /* ...and/or sustain dots */ 3445b81b6b3SRodney W. Grimes for (sustain = 0; cp[1] == '.'; cp++) 3455b81b6b3SRodney W. Grimes { 3465b81b6b3SRodney W. Grimes slen--; 3475b81b6b3SRodney W. Grimes sustain++; 3485b81b6b3SRodney W. Grimes } 3495b81b6b3SRodney W. Grimes 350e614aa8aSAndrey A. Chernov /* ...and/or a slur mark */ 351e614aa8aSAndrey A. Chernov oldfill = fill; 352e614aa8aSAndrey A. Chernov if (cp[1] == '_') 353e614aa8aSAndrey A. Chernov { 354e614aa8aSAndrey A. Chernov fill = LEGATO; 355e614aa8aSAndrey A. Chernov ++cp; 356e614aa8aSAndrey A. Chernov slen--; 357e614aa8aSAndrey A. Chernov } 358e614aa8aSAndrey A. Chernov 3595b81b6b3SRodney W. Grimes /* time to emit the actual tone */ 360424e2d04SAndrey A. Chernov playtone(pitch, timeval, sustain); 361e614aa8aSAndrey A. Chernov 362e614aa8aSAndrey A. Chernov fill = oldfill; 3635b81b6b3SRodney W. Grimes break; 3645b81b6b3SRodney W. Grimes 3655b81b6b3SRodney W. Grimes case 'O': 3665b81b6b3SRodney W. Grimes if (cp[1] == 'N' || cp[1] == 'n') 3675b81b6b3SRodney W. Grimes { 3685b81b6b3SRodney W. Grimes octprefix = octtrack = FALSE; 3695b81b6b3SRodney W. Grimes ++cp; 3705b81b6b3SRodney W. Grimes slen--; 3715b81b6b3SRodney W. Grimes } 3725b81b6b3SRodney W. Grimes else if (cp[1] == 'L' || cp[1] == 'l') 3735b81b6b3SRodney W. Grimes { 3745b81b6b3SRodney W. Grimes octtrack = TRUE; 3755b81b6b3SRodney W. Grimes ++cp; 3765b81b6b3SRodney W. Grimes slen--; 3775b81b6b3SRodney W. Grimes } 3785b81b6b3SRodney W. Grimes else 3795b81b6b3SRodney W. Grimes { 3805b81b6b3SRodney W. Grimes GETNUM(cp, octave); 3810a97fb59SAndrey A. Chernov if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES) 3825b81b6b3SRodney W. Grimes octave = DFLT_OCTAVE; 3835b81b6b3SRodney W. Grimes octprefix = TRUE; 3845b81b6b3SRodney W. Grimes } 3855b81b6b3SRodney W. Grimes break; 3865b81b6b3SRodney W. Grimes 3875b81b6b3SRodney W. Grimes case '>': 3880a97fb59SAndrey A. Chernov if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1) 3895b81b6b3SRodney W. Grimes octave++; 3905b81b6b3SRodney W. Grimes octprefix = TRUE; 3915b81b6b3SRodney W. Grimes break; 3925b81b6b3SRodney W. Grimes 3935b81b6b3SRodney W. Grimes case '<': 3945b81b6b3SRodney W. Grimes if (octave > 0) 3955b81b6b3SRodney W. Grimes octave--; 3965b81b6b3SRodney W. Grimes octprefix = TRUE; 3975b81b6b3SRodney W. Grimes break; 3985b81b6b3SRodney W. Grimes 3995b81b6b3SRodney W. Grimes case 'N': 4005b81b6b3SRodney W. Grimes GETNUM(cp, pitch); 4015b81b6b3SRodney W. Grimes for (sustain = 0; cp[1] == '.'; cp++) 4025b81b6b3SRodney W. Grimes { 4035b81b6b3SRodney W. Grimes slen--; 4045b81b6b3SRodney W. Grimes sustain++; 4055b81b6b3SRodney W. Grimes } 406e614aa8aSAndrey A. Chernov oldfill = fill; 407e614aa8aSAndrey A. Chernov if (cp[1] == '_') 408e614aa8aSAndrey A. Chernov { 409e614aa8aSAndrey A. Chernov fill = LEGATO; 410e614aa8aSAndrey A. Chernov ++cp; 411e614aa8aSAndrey A. Chernov slen--; 412e614aa8aSAndrey A. Chernov } 413424e2d04SAndrey A. Chernov playtone(pitch - 1, value, sustain); 414e614aa8aSAndrey A. Chernov fill = oldfill; 4155b81b6b3SRodney W. Grimes break; 4165b81b6b3SRodney W. Grimes 4175b81b6b3SRodney W. Grimes case 'L': 4185b81b6b3SRodney W. Grimes GETNUM(cp, value); 4195b81b6b3SRodney W. Grimes if (value <= 0 || value > MIN_VALUE) 4205b81b6b3SRodney W. Grimes value = DFLT_VALUE; 4215b81b6b3SRodney W. Grimes break; 4225b81b6b3SRodney W. Grimes 4235b81b6b3SRodney W. Grimes case 'P': 4245b81b6b3SRodney W. Grimes case '~': 4255b81b6b3SRodney W. Grimes /* this may be followed by an override time value */ 4265b81b6b3SRodney W. Grimes GETNUM(cp, timeval); 4275b81b6b3SRodney W. Grimes if (timeval <= 0 || timeval > MIN_VALUE) 4285b81b6b3SRodney W. Grimes timeval = value; 4295b81b6b3SRodney W. Grimes for (sustain = 0; cp[1] == '.'; cp++) 4305b81b6b3SRodney W. Grimes { 4315b81b6b3SRodney W. Grimes slen--; 4325b81b6b3SRodney W. Grimes sustain++; 4335b81b6b3SRodney W. Grimes } 434424e2d04SAndrey A. Chernov playtone(-1, timeval, sustain); 4355b81b6b3SRodney W. Grimes break; 4365b81b6b3SRodney W. Grimes 4375b81b6b3SRodney W. Grimes case 'T': 4385b81b6b3SRodney W. Grimes GETNUM(cp, tempo); 4395b81b6b3SRodney W. Grimes if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) 4405b81b6b3SRodney W. Grimes tempo = DFLT_TEMPO; 4411e064bd4SAndrey A. Chernov whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo; 4425b81b6b3SRodney W. Grimes break; 4435b81b6b3SRodney W. Grimes 4445b81b6b3SRodney W. Grimes case 'M': 4455b81b6b3SRodney W. Grimes if (cp[1] == 'N' || cp[1] == 'n') 4465b81b6b3SRodney W. Grimes { 4475b81b6b3SRodney W. Grimes fill = NORMAL; 4485b81b6b3SRodney W. Grimes ++cp; 4495b81b6b3SRodney W. Grimes slen--; 4505b81b6b3SRodney W. Grimes } 4515b81b6b3SRodney W. Grimes else if (cp[1] == 'L' || cp[1] == 'l') 4525b81b6b3SRodney W. Grimes { 4535b81b6b3SRodney W. Grimes fill = LEGATO; 4545b81b6b3SRodney W. Grimes ++cp; 4555b81b6b3SRodney W. Grimes slen--; 4565b81b6b3SRodney W. Grimes } 4575b81b6b3SRodney W. Grimes else if (cp[1] == 'S' || cp[1] == 's') 4585b81b6b3SRodney W. Grimes { 4595b81b6b3SRodney W. Grimes fill = STACCATO; 4605b81b6b3SRodney W. Grimes ++cp; 4615b81b6b3SRodney W. Grimes slen--; 4625b81b6b3SRodney W. Grimes } 4635b81b6b3SRodney W. Grimes break; 4645b81b6b3SRodney W. Grimes } 4655b81b6b3SRodney W. Grimes } 4665b81b6b3SRodney W. Grimes } 4675b81b6b3SRodney W. Grimes 4685b81b6b3SRodney W. Grimes /******************* UNIX DRIVER HOOKS BEGIN HERE ************************** 4695b81b6b3SRodney W. Grimes * 4705b81b6b3SRodney W. Grimes * This section implements driver hooks to run playstring() and the tone(), 4715b81b6b3SRodney W. Grimes * endtone(), and rest() functions defined above. 4725b81b6b3SRodney W. Grimes */ 4735b81b6b3SRodney W. Grimes 474e614aa8aSAndrey A. Chernov static int spkr_active = FALSE; /* exclusion flag */ 475e4961fbfSPoul-Henning Kamp static char *spkr_inbuf; /* incoming buf */ 4765b81b6b3SRodney W. Grimes 4773412120fSPoul-Henning Kamp static int 478b40ce416SJulian Elischer spkropen(dev, flags, fmt, td) 4795b81b6b3SRodney W. Grimes dev_t dev; 48060039670SBruce Evans int flags; 48160039670SBruce Evans int fmt; 482b40ce416SJulian Elischer struct thread *td; 4835b81b6b3SRodney W. Grimes { 4845b81b6b3SRodney W. Grimes #ifdef DEBUG 485b8e49f68SBill Fumerola (void) printf("spkropen: entering with dev = %s\n", devtoname(dev)); 4865b81b6b3SRodney W. Grimes #endif /* DEBUG */ 4875b81b6b3SRodney W. Grimes 4885b81b6b3SRodney W. Grimes if (minor(dev) != 0) 4895b81b6b3SRodney W. Grimes return(ENXIO); 4905b81b6b3SRodney W. Grimes else if (spkr_active) 4915b81b6b3SRodney W. Grimes return(EBUSY); 4925b81b6b3SRodney W. Grimes else 4935b81b6b3SRodney W. Grimes { 494e614aa8aSAndrey A. Chernov #ifdef DEBUG 495e614aa8aSAndrey A. Chernov (void) printf("spkropen: about to perform play initialization\n"); 496e614aa8aSAndrey A. Chernov #endif /* DEBUG */ 4975b81b6b3SRodney W. Grimes playinit(); 498a163d034SWarner Losh spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK); 499e614aa8aSAndrey A. Chernov spkr_active = TRUE; 5005b81b6b3SRodney W. Grimes return(0); 5015b81b6b3SRodney W. Grimes } 502e614aa8aSAndrey A. Chernov } 5035b81b6b3SRodney W. Grimes 5043412120fSPoul-Henning Kamp static int 505f4de22acSJoerg Wunsch spkrwrite(dev, uio, ioflag) 5065b81b6b3SRodney W. Grimes dev_t dev; 5075b81b6b3SRodney W. Grimes struct uio *uio; 50860039670SBruce Evans int ioflag; 5095b81b6b3SRodney W. Grimes { 5105b81b6b3SRodney W. Grimes #ifdef DEBUG 511b8e49f68SBill Fumerola printf("spkrwrite: entering with dev = %s, count = %d\n", 512b8e49f68SBill Fumerola devtoname(dev), uio->uio_resid); 5135b81b6b3SRodney W. Grimes #endif /* DEBUG */ 5145b81b6b3SRodney W. Grimes 5155b81b6b3SRodney W. Grimes if (minor(dev) != 0) 5165b81b6b3SRodney W. Grimes return(ENXIO); 5173bd9f6dbSPeter Wemm else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */ 518e614aa8aSAndrey A. Chernov return(E2BIG); 5195b81b6b3SRodney W. Grimes else 5205b81b6b3SRodney W. Grimes { 521e614aa8aSAndrey A. Chernov unsigned n; 522e614aa8aSAndrey A. Chernov char *cp; 523e614aa8aSAndrey A. Chernov int error; 524e614aa8aSAndrey A. Chernov 525e614aa8aSAndrey A. Chernov n = uio->uio_resid; 526e4961fbfSPoul-Henning Kamp cp = spkr_inbuf; 5273bd9f6dbSPeter Wemm error = uiomove(cp, n, uio); 5283bd9f6dbSPeter Wemm if (!error) { 5293bd9f6dbSPeter Wemm cp[n] = '\0'; 530424e2d04SAndrey A. Chernov playstring(cp, n); 5313bd9f6dbSPeter Wemm } 5325b81b6b3SRodney W. Grimes return(error); 5335b81b6b3SRodney W. Grimes } 5345b81b6b3SRodney W. Grimes } 5355b81b6b3SRodney W. Grimes 5363412120fSPoul-Henning Kamp static int 537b40ce416SJulian Elischer spkrclose(dev, flags, fmt, td) 5385b81b6b3SRodney W. Grimes dev_t dev; 53960039670SBruce Evans int flags; 54060039670SBruce Evans int fmt; 541b40ce416SJulian Elischer struct thread *td; 5425b81b6b3SRodney W. Grimes { 5435b81b6b3SRodney W. Grimes #ifdef DEBUG 544b8e49f68SBill Fumerola (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev)); 5455b81b6b3SRodney W. Grimes #endif /* DEBUG */ 5465b81b6b3SRodney W. Grimes 5475b81b6b3SRodney W. Grimes if (minor(dev) != 0) 5485b81b6b3SRodney W. Grimes return(ENXIO); 5495b81b6b3SRodney W. Grimes else 5505b81b6b3SRodney W. Grimes { 5511e064bd4SAndrey A. Chernov wakeup((caddr_t)&endtone); 5521e064bd4SAndrey A. Chernov wakeup((caddr_t)&endrest); 553e4961fbfSPoul-Henning Kamp free(spkr_inbuf, M_SPKR); 554e614aa8aSAndrey A. Chernov spkr_active = FALSE; 5555b81b6b3SRodney W. Grimes return(0); 5565b81b6b3SRodney W. Grimes } 557e614aa8aSAndrey A. Chernov } 5585b81b6b3SRodney W. Grimes 5593412120fSPoul-Henning Kamp static int 560b40ce416SJulian Elischer spkrioctl(dev, cmd, cmdarg, flags, td) 5615b81b6b3SRodney W. Grimes dev_t dev; 56200671271SBruce Evans unsigned long cmd; 5635b81b6b3SRodney W. Grimes caddr_t cmdarg; 56460039670SBruce Evans int flags; 565b40ce416SJulian Elischer struct thread *td; 5665b81b6b3SRodney W. Grimes { 5675b81b6b3SRodney W. Grimes #ifdef DEBUG 568d9183205SBruce Evans (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n", 569d9183205SBruce Evans devtoname(dev), cmd); 5705b81b6b3SRodney W. Grimes #endif /* DEBUG */ 5715b81b6b3SRodney W. Grimes 5725b81b6b3SRodney W. Grimes if (minor(dev) != 0) 5735b81b6b3SRodney W. Grimes return(ENXIO); 5745b81b6b3SRodney W. Grimes else if (cmd == SPKRTONE) 5755b81b6b3SRodney W. Grimes { 5765b81b6b3SRodney W. Grimes tone_t *tp = (tone_t *)cmdarg; 5775b81b6b3SRodney W. Grimes 5785b81b6b3SRodney W. Grimes if (tp->frequency == 0) 579424e2d04SAndrey A. Chernov rest(tp->duration); 5805b81b6b3SRodney W. Grimes else 581424e2d04SAndrey A. Chernov tone(tp->frequency, tp->duration); 582424e2d04SAndrey A. Chernov return 0; 5835b81b6b3SRodney W. Grimes } 5845b81b6b3SRodney W. Grimes else if (cmd == SPKRTUNE) 5855b81b6b3SRodney W. Grimes { 5865b81b6b3SRodney W. Grimes tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg); 5875b81b6b3SRodney W. Grimes tone_t ttp; 5885b81b6b3SRodney W. Grimes int error; 5895b81b6b3SRodney W. Grimes 5905b81b6b3SRodney W. Grimes for (; ; tp++) { 5915b81b6b3SRodney W. Grimes error = copyin(tp, &ttp, sizeof(tone_t)); 5925b81b6b3SRodney W. Grimes if (error) 5935b81b6b3SRodney W. Grimes return(error); 5945b81b6b3SRodney W. Grimes if (ttp.duration == 0) 5955b81b6b3SRodney W. Grimes break; 5965b81b6b3SRodney W. Grimes if (ttp.frequency == 0) 597424e2d04SAndrey A. Chernov rest(ttp.duration); 5985b81b6b3SRodney W. Grimes else 599424e2d04SAndrey A. Chernov tone(ttp.frequency, ttp.duration); 6005b81b6b3SRodney W. Grimes } 6015b81b6b3SRodney W. Grimes return(0); 6025b81b6b3SRodney W. Grimes } 603e614aa8aSAndrey A. Chernov return(EINVAL); 604e614aa8aSAndrey A. Chernov } 6055b81b6b3SRodney W. Grimes 6062a50a6d7SMike Smith /* 6072a50a6d7SMike Smith * Install placeholder to claim the resources owned by the 6082a50a6d7SMike Smith * AT tone generator. 6092a50a6d7SMike Smith */ 61030201b6cSMatthew N. Dodd static struct isa_pnp_id speaker_ids[] = { 61130201b6cSMatthew N. Dodd #ifndef PC98 61230201b6cSMatthew N. Dodd { 0x0008d041 /* PNP0800 */, "PC speaker" }, 61330201b6cSMatthew N. Dodd #endif 6142a50a6d7SMike Smith { 0 } 6152a50a6d7SMike Smith }; 6162a50a6d7SMike Smith 61730201b6cSMatthew N. Dodd static dev_t speaker_dev; 618ce6d929bSWes Peters 6192a50a6d7SMike Smith static int 62030201b6cSMatthew N. Dodd speaker_probe(device_t dev) 6212a50a6d7SMike Smith { 622c7f718ecSMatthew N. Dodd int error; 623c7f718ecSMatthew N. Dodd 62430201b6cSMatthew N. Dodd error = ISA_PNP_PROBE(device_get_parent(dev), dev, speaker_ids); 625c7f718ecSMatthew N. Dodd 626c7f718ecSMatthew N. Dodd /* PnP match */ 627c7f718ecSMatthew N. Dodd if (error == 0) 628c7f718ecSMatthew N. Dodd return (0); 629c7f718ecSMatthew N. Dodd 630c7f718ecSMatthew N. Dodd /* No match */ 631c7f718ecSMatthew N. Dodd if (error == ENXIO) 632c7f718ecSMatthew N. Dodd return (ENXIO); 633c7f718ecSMatthew N. Dodd 634c7f718ecSMatthew N. Dodd /* Not configured by hints. */ 63530201b6cSMatthew N. Dodd if (strncmp(device_get_name(dev), "speaker", 9)) 636c7f718ecSMatthew N. Dodd return (ENXIO); 637c7f718ecSMatthew N. Dodd 63830201b6cSMatthew N. Dodd device_set_desc(dev, "PC speaker"); 639c7f718ecSMatthew N. Dodd 640c7f718ecSMatthew N. Dodd return (0); 6412a50a6d7SMike Smith } 6422a50a6d7SMike Smith 6432a50a6d7SMike Smith static int 64430201b6cSMatthew N. Dodd speaker_attach(device_t dev) 6452a50a6d7SMike Smith { 646c7f718ecSMatthew N. Dodd 64730201b6cSMatthew N. Dodd if (speaker_dev) { 648c7f718ecSMatthew N. Dodd device_printf(dev, "Already attached!\n"); 649c7f718ecSMatthew N. Dodd return (ENXIO); 650c7f718ecSMatthew N. Dodd } 651c7f718ecSMatthew N. Dodd 65230201b6cSMatthew N. Dodd speaker_dev = make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 653ce6d929bSWes Peters "speaker"); 654ce6d929bSWes Peters return (0); 655ce6d929bSWes Peters } 656ce6d929bSWes Peters 657ce6d929bSWes Peters static int 65830201b6cSMatthew N. Dodd speaker_detach(device_t dev) 659ce6d929bSWes Peters { 66030201b6cSMatthew N. Dodd destroy_dev(speaker_dev); 6612a50a6d7SMike Smith return (0); 6622a50a6d7SMike Smith } 6632a50a6d7SMike Smith 66430201b6cSMatthew N. Dodd static device_method_t speaker_methods[] = { 6652a50a6d7SMike Smith /* Device interface */ 66630201b6cSMatthew N. Dodd DEVMETHOD(device_probe, speaker_probe), 66730201b6cSMatthew N. Dodd DEVMETHOD(device_attach, speaker_attach), 66830201b6cSMatthew N. Dodd DEVMETHOD(device_detach, speaker_detach), 6692a50a6d7SMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 6702a50a6d7SMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 6712a50a6d7SMike Smith DEVMETHOD(device_resume, bus_generic_resume), 6722a50a6d7SMike Smith { 0, 0 } 6732a50a6d7SMike Smith }; 6742a50a6d7SMike Smith 67530201b6cSMatthew N. Dodd static driver_t speaker_driver = { 67630201b6cSMatthew N. Dodd "speaker", 67730201b6cSMatthew N. Dodd speaker_methods, 6782a50a6d7SMike Smith 1, /* no softc */ 6792a50a6d7SMike Smith }; 6802a50a6d7SMike Smith 68130201b6cSMatthew N. Dodd static devclass_t speaker_devclass; 6822a50a6d7SMike Smith 68330201b6cSMatthew N. Dodd DRIVER_MODULE(speaker, isa, speaker_driver, speaker_devclass, 0, 0); 68430201b6cSMatthew N. Dodd #ifndef PC98 68530201b6cSMatthew N. Dodd DRIVER_MODULE(speaker, acpi, speaker_driver, speaker_devclass, 0, 0); 68630201b6cSMatthew N. Dodd #endif 68753ac6efbSJulian Elischer 6885b81b6b3SRodney W. Grimes /* spkr.c ends here */ 689