186cb007fSWarner Losh /*- 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 75b81b6b3SRodney W. Grimes */ 85b81b6b3SRodney W. Grimes 9006124d8SDavid E. O'Brien #include <sys/cdefs.h> 10006124d8SDavid E. O'Brien __FBSDID("$FreeBSD$"); 11006124d8SDavid E. O'Brien 12f540b106SGarrett Wollman #include <sys/param.h> 13f540b106SGarrett Wollman #include <sys/systm.h> 142a50a6d7SMike Smith #include <sys/bus.h> 15f540b106SGarrett Wollman #include <sys/kernel.h> 162a50a6d7SMike Smith #include <sys/module.h> 17f540b106SGarrett Wollman #include <sys/uio.h> 1887f6c662SJulian Elischer #include <sys/conf.h> 195b664c7cSPoul-Henning Kamp #include <sys/ctype.h> 20e4961fbfSPoul-Henning Kamp #include <sys/malloc.h> 212a50a6d7SMike Smith #include <isa/isavar.h> 22b5e8ce9fSBruce Evans #include <machine/clock.h> 2324072ca3SYoshihiro Takahashi #include <machine/ppireg.h> 2424072ca3SYoshihiro Takahashi #include <machine/timerreg.h> 256d8200ffSRuslan Ermilov #include <dev/speaker/speaker.h> 265b81b6b3SRodney W. Grimes 2787f6c662SJulian Elischer static d_open_t spkropen; 2887f6c662SJulian Elischer static d_close_t spkrclose; 2987f6c662SJulian Elischer static d_write_t spkrwrite; 3087f6c662SJulian Elischer static d_ioctl_t spkrioctl; 3187f6c662SJulian Elischer 324e2f199eSPoul-Henning Kamp static struct cdevsw spkr_cdevsw = { 33dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 34dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 357ac40f5fSPoul-Henning Kamp .d_open = spkropen, 367ac40f5fSPoul-Henning Kamp .d_close = spkrclose, 377ac40f5fSPoul-Henning Kamp .d_write = spkrwrite, 387ac40f5fSPoul-Henning Kamp .d_ioctl = spkrioctl, 397ac40f5fSPoul-Henning Kamp .d_name = "spkr", 404e2f199eSPoul-Henning Kamp }; 418af5d536SJulian Elischer 42959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer"); 43e4961fbfSPoul-Henning Kamp 445b81b6b3SRodney W. Grimes /**************** MACHINE DEPENDENT PART STARTS HERE ************************* 455b81b6b3SRodney W. Grimes * 465b81b6b3SRodney W. Grimes * This section defines a function tone() which causes a tone of given 479275c7ccSBruce Evans * frequency and duration from the ISA console speaker. 485b81b6b3SRodney W. Grimes * Another function endtone() is defined to force sound off, and there is 495b81b6b3SRodney W. Grimes * also a rest() entry point to do pauses. 505b81b6b3SRodney W. Grimes * 515b81b6b3SRodney W. Grimes * Audible sound is generated using the Programmable Interval Timer (PIT) and 529275c7ccSBruce Evans * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The 535b81b6b3SRodney W. Grimes * PPI controls whether sound is passed through at all; the PIT's channel 2 is 545b81b6b3SRodney W. Grimes * used to generate clicks (a square wave) of whatever frequency is desired. 555b81b6b3SRodney W. Grimes */ 565b81b6b3SRodney W. Grimes 5730201b6cSMatthew N. Dodd #ifdef PC98 58257427efSMatthew N. Dodd #define SPKR_DESC "PC98 speaker" 5930201b6cSMatthew N. Dodd #else 60257427efSMatthew N. Dodd #define SPKR_DESC "PC speaker" 6130201b6cSMatthew N. Dodd #endif 6230201b6cSMatthew N. Dodd 631e064bd4SAndrey A. Chernov #define SPKRPRI PSOCK 641e064bd4SAndrey A. Chernov static char endtone, endrest; 655b81b6b3SRodney W. Grimes 66b2424ac0SBrian Somers static void tone(unsigned int thz, unsigned int centisecs); 67b2424ac0SBrian Somers static void rest(int centisecs); 6889c9a483SAlfred Perlstein static void playinit(void); 6989c9a483SAlfred Perlstein static void playtone(int pitch, int value, int sustain); 7089c9a483SAlfred Perlstein static void playstring(char *cp, size_t slen); 710dfe10a6SBruce Evans 72b2424ac0SBrian Somers /* emit tone of frequency thz for given number of centisecs */ 73f4de22acSJoerg Wunsch static void 74b2424ac0SBrian Somers tone(thz, centisecs) 75b2424ac0SBrian Somers unsigned int thz, centisecs; 765b81b6b3SRodney W. Grimes { 77b2424ac0SBrian Somers int sps, timo; 785b81b6b3SRodney W. Grimes 799714de6aSDavid Greenman if (thz <= 0) 809714de6aSDavid Greenman return; 819714de6aSDavid Greenman 825b81b6b3SRodney W. Grimes #ifdef DEBUG 83b2424ac0SBrian Somers (void) printf("tone: thz=%d centisecs=%d\n", thz, centisecs); 845b81b6b3SRodney W. Grimes #endif /* DEBUG */ 855b81b6b3SRodney W. Grimes 865b81b6b3SRodney W. Grimes /* set timer to generate clicks at given frequency in Hertz */ 87f4de22acSJoerg Wunsch sps = splclock(); 88bc4ff6e3SSøren Schmidt 8924072ca3SYoshihiro Takahashi if (timer_spkr_acquire()) { 90bc4ff6e3SSøren Schmidt /* enter list of waiting procs ??? */ 91f4de22acSJoerg Wunsch splx(sps); 92bc4ff6e3SSøren Schmidt return; 93bc4ff6e3SSøren Schmidt } 94f4de22acSJoerg Wunsch splx(sps); 95f4de22acSJoerg Wunsch disable_intr(); 96e4659858SPoul-Henning Kamp timer_spkr_setfreq(thz); 97f4de22acSJoerg Wunsch enable_intr(); 985b81b6b3SRodney W. Grimes 995b81b6b3SRodney W. Grimes /* turn the speaker on */ 10024072ca3SYoshihiro Takahashi ppi_spkr_on(); 1015b81b6b3SRodney W. Grimes 1025b81b6b3SRodney W. Grimes /* 1035b81b6b3SRodney W. Grimes * Set timeout to endtone function, then give up the timeslice. 1045b81b6b3SRodney W. Grimes * This is so other processes can execute while the tone is being 1055b81b6b3SRodney W. Grimes * emitted. 1065b81b6b3SRodney W. Grimes */ 107b2424ac0SBrian Somers timo = centisecs * hz / 100; 108b2424ac0SBrian Somers if (timo > 0) 109b2424ac0SBrian Somers tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", timo); 11024072ca3SYoshihiro Takahashi ppi_spkr_off(); 111f4de22acSJoerg Wunsch sps = splclock(); 11224072ca3SYoshihiro Takahashi timer_spkr_release(); 113f4de22acSJoerg Wunsch splx(sps); 1145b81b6b3SRodney W. Grimes } 1155b81b6b3SRodney W. Grimes 116b2424ac0SBrian Somers /* rest for given number of centisecs */ 117f4de22acSJoerg Wunsch static void 118b2424ac0SBrian Somers rest(centisecs) 119b2424ac0SBrian Somers int centisecs; 1205b81b6b3SRodney W. Grimes { 121b2424ac0SBrian Somers int timo; 122b2424ac0SBrian Somers 1235b81b6b3SRodney W. Grimes /* 1245b81b6b3SRodney W. Grimes * Set timeout to endrest function, then give up the timeslice. 1255b81b6b3SRodney W. Grimes * This is so other processes can execute while the rest is being 1265b81b6b3SRodney W. Grimes * waited out. 1275b81b6b3SRodney W. Grimes */ 1285b81b6b3SRodney W. Grimes #ifdef DEBUG 129b2424ac0SBrian Somers (void) printf("rest: %d\n", centisecs); 1305b81b6b3SRodney W. Grimes #endif /* DEBUG */ 131b2424ac0SBrian Somers timo = centisecs * hz / 100; 132b2424ac0SBrian Somers if (timo > 0) 133b2424ac0SBrian Somers tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", timo); 1345b81b6b3SRodney W. Grimes } 1355b81b6b3SRodney W. Grimes 1365b81b6b3SRodney W. Grimes /**************** PLAY STRING INTERPRETER BEGINS HERE ********************** 1375b81b6b3SRodney W. Grimes * 1385b81b6b3SRodney W. Grimes * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement; 139e614aa8aSAndrey A. Chernov * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave- 140e614aa8aSAndrey A. Chernov * tracking facility are added. 1415b81b6b3SRodney W. Grimes * Requires tone(), rest(), and endtone(). String play is not interruptible 1425b81b6b3SRodney W. Grimes * except possibly at physical block boundaries. 1435b81b6b3SRodney W. Grimes */ 1445b81b6b3SRodney W. Grimes 1455b81b6b3SRodney W. Grimes typedef int bool; 1465b81b6b3SRodney W. Grimes #define TRUE 1 1475b81b6b3SRodney W. Grimes #define FALSE 0 1485b81b6b3SRodney W. Grimes 1495b81b6b3SRodney W. Grimes #define dtoi(c) ((c) - '0') 1505b81b6b3SRodney W. Grimes 1515b81b6b3SRodney W. Grimes static int octave; /* currently selected octave */ 1525b81b6b3SRodney W. Grimes static int whole; /* whole-note time at current tempo, in ticks */ 1535b81b6b3SRodney W. Grimes static int value; /* whole divisor for note time, quarter note = 1 */ 1545b81b6b3SRodney W. Grimes static int fill; /* controls spacing of notes */ 1555b81b6b3SRodney W. Grimes static bool octtrack; /* octave-tracking on? */ 1565b81b6b3SRodney W. Grimes static bool octprefix; /* override current octave-tracking state? */ 1575b81b6b3SRodney W. Grimes 1585b81b6b3SRodney W. Grimes /* 1595b81b6b3SRodney W. Grimes * Magic number avoidance... 1605b81b6b3SRodney W. Grimes */ 1615b81b6b3SRodney W. Grimes #define SECS_PER_MIN 60 /* seconds per minute */ 1625b81b6b3SRodney W. Grimes #define WHOLE_NOTE 4 /* quarter notes per whole note */ 1635b81b6b3SRodney W. Grimes #define MIN_VALUE 64 /* the most we can divide a note by */ 1645b81b6b3SRodney W. Grimes #define DFLT_VALUE 4 /* default value (quarter-note) */ 1655b81b6b3SRodney W. Grimes #define FILLTIME 8 /* for articulation, break note in parts */ 1665b81b6b3SRodney W. Grimes #define STACCATO 6 /* 6/8 = 3/4 of note is filled */ 1675b81b6b3SRodney W. Grimes #define NORMAL 7 /* 7/8ths of note interval is filled */ 1685b81b6b3SRodney W. Grimes #define LEGATO 8 /* all of note interval is filled */ 1695b81b6b3SRodney W. Grimes #define DFLT_OCTAVE 4 /* default octave */ 1705b81b6b3SRodney W. Grimes #define MIN_TEMPO 32 /* minimum tempo */ 1715b81b6b3SRodney W. Grimes #define DFLT_TEMPO 120 /* default tempo */ 1725b81b6b3SRodney W. Grimes #define MAX_TEMPO 255 /* max tempo */ 1735b81b6b3SRodney W. Grimes #define NUM_MULT 3 /* numerator of dot multiplier */ 1745b81b6b3SRodney W. Grimes #define DENOM_MULT 2 /* denominator of dot multiplier */ 1755b81b6b3SRodney W. Grimes 1765b81b6b3SRodney W. Grimes /* letter to half-tone: A B C D E F G */ 1775b81b6b3SRodney W. Grimes static int notetab[8] = {9, 11, 0, 2, 4, 5, 7}; 1785b81b6b3SRodney W. Grimes 1795b81b6b3SRodney W. Grimes /* 1805b81b6b3SRodney W. Grimes * This is the American Standard A440 Equal-Tempered scale with frequencies 1815b81b6b3SRodney W. Grimes * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook... 1825b81b6b3SRodney W. Grimes * our octave 0 is standard octave 2. 1835b81b6b3SRodney W. Grimes */ 1845b81b6b3SRodney W. Grimes #define OCTAVE_NOTES 12 /* semitones per octave */ 1855b81b6b3SRodney W. Grimes static int pitchtab[] = 1865b81b6b3SRodney W. Grimes { 1875b81b6b3SRodney W. Grimes /* C C# D D# E F F# G G# A A# B*/ 1885b81b6b3SRodney W. Grimes /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123, 1895b81b6b3SRodney W. Grimes /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 1905b81b6b3SRodney W. Grimes /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 1915b81b6b3SRodney W. Grimes /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1925b81b6b3SRodney W. Grimes /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 1935b81b6b3SRodney W. Grimes /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 1945b81b6b3SRodney W. Grimes /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902, 1955b81b6b3SRodney W. Grimes }; 1965b81b6b3SRodney W. Grimes 197f4de22acSJoerg Wunsch static void 198f4de22acSJoerg Wunsch playinit() 1995b81b6b3SRodney W. Grimes { 2005b81b6b3SRodney W. Grimes octave = DFLT_OCTAVE; 2017344a290SBrian Somers whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; 2025b81b6b3SRodney W. Grimes fill = NORMAL; 2035b81b6b3SRodney W. Grimes value = DFLT_VALUE; 2045b81b6b3SRodney W. Grimes octtrack = FALSE; 2055b81b6b3SRodney W. Grimes octprefix = TRUE; /* act as though there was an initial O(n) */ 2065b81b6b3SRodney W. Grimes } 2075b81b6b3SRodney W. Grimes 2085b81b6b3SRodney W. Grimes /* play tone of proper duration for current rhythm signature */ 209f4de22acSJoerg Wunsch static void 210f4de22acSJoerg Wunsch playtone(pitch, value, sustain) 2115b81b6b3SRodney W. Grimes int pitch, value, sustain; 2125b81b6b3SRodney W. Grimes { 2135b81b6b3SRodney W. Grimes register int sound, silence, snum = 1, sdenom = 1; 2145b81b6b3SRodney W. Grimes 2155b81b6b3SRodney W. Grimes /* this weirdness avoids floating-point arithmetic */ 2165b81b6b3SRodney W. Grimes for (; sustain; sustain--) 2175b81b6b3SRodney W. Grimes { 218e614aa8aSAndrey A. Chernov /* See the BUGS section in the man page for discussion */ 2195b81b6b3SRodney W. Grimes snum *= NUM_MULT; 2205b81b6b3SRodney W. Grimes sdenom *= DENOM_MULT; 2215b81b6b3SRodney W. Grimes } 2225b81b6b3SRodney W. Grimes 2239714de6aSDavid Greenman if (value == 0 || sdenom == 0) 2249714de6aSDavid Greenman return; 2259714de6aSDavid Greenman 2265b81b6b3SRodney W. Grimes if (pitch == -1) 227424e2d04SAndrey A. Chernov rest(whole * snum / (value * sdenom)); 2285b81b6b3SRodney W. Grimes else 2295b81b6b3SRodney W. Grimes { 2305b81b6b3SRodney W. Grimes sound = (whole * snum) / (value * sdenom) 2315b81b6b3SRodney W. Grimes - (whole * (FILLTIME - fill)) / (value * FILLTIME); 2325b81b6b3SRodney W. Grimes silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom); 2335b81b6b3SRodney W. Grimes 2345b81b6b3SRodney W. Grimes #ifdef DEBUG 235e614aa8aSAndrey A. Chernov (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n", 2365b81b6b3SRodney W. Grimes pitch, sound, silence); 2375b81b6b3SRodney W. Grimes #endif /* DEBUG */ 2385b81b6b3SRodney W. Grimes 239424e2d04SAndrey A. Chernov tone(pitchtab[pitch], sound); 2405b81b6b3SRodney W. Grimes if (fill != LEGATO) 241424e2d04SAndrey A. Chernov rest(silence); 2425b81b6b3SRodney W. Grimes } 2435b81b6b3SRodney W. Grimes } 2445b81b6b3SRodney W. Grimes 2455b81b6b3SRodney W. Grimes /* interpret and play an item from a notation string */ 246f4de22acSJoerg Wunsch static void 247f4de22acSJoerg Wunsch playstring(cp, slen) 2485b81b6b3SRodney W. Grimes char *cp; 2495b81b6b3SRodney W. Grimes size_t slen; 2505b81b6b3SRodney W. Grimes { 251e614aa8aSAndrey A. Chernov int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE; 2525b81b6b3SRodney W. Grimes 2535b81b6b3SRodney W. Grimes #define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \ 2545b81b6b3SRodney W. Grimes {v = v * 10 + (*++cp - '0'); slen--;} 2555b81b6b3SRodney W. Grimes for (; slen--; cp++) 2565b81b6b3SRodney W. Grimes { 2575b81b6b3SRodney W. Grimes int sustain, timeval, tempo; 2585b81b6b3SRodney W. Grimes register char c = toupper(*cp); 2595b81b6b3SRodney W. Grimes 2605b81b6b3SRodney W. Grimes #ifdef DEBUG 261e614aa8aSAndrey A. Chernov (void) printf("playstring: %c (%x)\n", c, c); 2625b81b6b3SRodney W. Grimes #endif /* DEBUG */ 2635b81b6b3SRodney W. Grimes 2645b81b6b3SRodney W. Grimes switch (c) 2655b81b6b3SRodney W. Grimes { 2665b81b6b3SRodney W. Grimes case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 2675b81b6b3SRodney W. Grimes 2685b81b6b3SRodney W. Grimes /* compute pitch */ 2695b81b6b3SRodney W. Grimes pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES; 2705b81b6b3SRodney W. Grimes 2715b81b6b3SRodney W. Grimes /* this may be followed by an accidental sign */ 2725b81b6b3SRodney W. Grimes if (cp[1] == '#' || cp[1] == '+') 2735b81b6b3SRodney W. Grimes { 2745b81b6b3SRodney W. Grimes ++pitch; 2755b81b6b3SRodney W. Grimes ++cp; 2765b81b6b3SRodney W. Grimes slen--; 2775b81b6b3SRodney W. Grimes } 2785b81b6b3SRodney W. Grimes else if (cp[1] == '-') 2795b81b6b3SRodney W. Grimes { 2805b81b6b3SRodney W. Grimes --pitch; 2815b81b6b3SRodney W. Grimes ++cp; 2825b81b6b3SRodney W. Grimes slen--; 2835b81b6b3SRodney W. Grimes } 2845b81b6b3SRodney W. Grimes 2855b81b6b3SRodney W. Grimes /* 2865b81b6b3SRodney W. Grimes * If octave-tracking mode is on, and there has been no octave- 2875b81b6b3SRodney W. Grimes * setting prefix, find the version of the current letter note 2885b81b6b3SRodney W. Grimes * closest to the last regardless of octave. 2895b81b6b3SRodney W. Grimes */ 2905b81b6b3SRodney W. Grimes if (octtrack && !octprefix) 2915b81b6b3SRodney W. Grimes { 2925b81b6b3SRodney W. Grimes if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch)) 2935b81b6b3SRodney W. Grimes { 2945b81b6b3SRodney W. Grimes ++octave; 2955b81b6b3SRodney W. Grimes pitch += OCTAVE_NOTES; 2965b81b6b3SRodney W. Grimes } 2975b81b6b3SRodney W. Grimes 2985b81b6b3SRodney W. Grimes if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch)) 2995b81b6b3SRodney W. Grimes { 3005b81b6b3SRodney W. Grimes --octave; 3015b81b6b3SRodney W. Grimes pitch -= OCTAVE_NOTES; 3025b81b6b3SRodney W. Grimes } 3035b81b6b3SRodney W. Grimes } 3045b81b6b3SRodney W. Grimes octprefix = FALSE; 3055b81b6b3SRodney W. Grimes lastpitch = pitch; 3065b81b6b3SRodney W. Grimes 3075b81b6b3SRodney W. Grimes /* ...which may in turn be followed by an override time value */ 3085b81b6b3SRodney W. Grimes GETNUM(cp, timeval); 3095b81b6b3SRodney W. Grimes if (timeval <= 0 || timeval > MIN_VALUE) 3105b81b6b3SRodney W. Grimes timeval = value; 3115b81b6b3SRodney W. Grimes 3125b81b6b3SRodney W. Grimes /* ...and/or sustain dots */ 3135b81b6b3SRodney W. Grimes for (sustain = 0; cp[1] == '.'; cp++) 3145b81b6b3SRodney W. Grimes { 3155b81b6b3SRodney W. Grimes slen--; 3165b81b6b3SRodney W. Grimes sustain++; 3175b81b6b3SRodney W. Grimes } 3185b81b6b3SRodney W. Grimes 319e614aa8aSAndrey A. Chernov /* ...and/or a slur mark */ 320e614aa8aSAndrey A. Chernov oldfill = fill; 321e614aa8aSAndrey A. Chernov if (cp[1] == '_') 322e614aa8aSAndrey A. Chernov { 323e614aa8aSAndrey A. Chernov fill = LEGATO; 324e614aa8aSAndrey A. Chernov ++cp; 325e614aa8aSAndrey A. Chernov slen--; 326e614aa8aSAndrey A. Chernov } 327e614aa8aSAndrey A. Chernov 3285b81b6b3SRodney W. Grimes /* time to emit the actual tone */ 329424e2d04SAndrey A. Chernov playtone(pitch, timeval, sustain); 330e614aa8aSAndrey A. Chernov 331e614aa8aSAndrey A. Chernov fill = oldfill; 3325b81b6b3SRodney W. Grimes break; 3335b81b6b3SRodney W. Grimes 3345b81b6b3SRodney W. Grimes case 'O': 3355b81b6b3SRodney W. Grimes if (cp[1] == 'N' || cp[1] == 'n') 3365b81b6b3SRodney W. Grimes { 3375b81b6b3SRodney W. Grimes octprefix = octtrack = FALSE; 3385b81b6b3SRodney W. Grimes ++cp; 3395b81b6b3SRodney W. Grimes slen--; 3405b81b6b3SRodney W. Grimes } 3415b81b6b3SRodney W. Grimes else if (cp[1] == 'L' || cp[1] == 'l') 3425b81b6b3SRodney W. Grimes { 3435b81b6b3SRodney W. Grimes octtrack = TRUE; 3445b81b6b3SRodney W. Grimes ++cp; 3455b81b6b3SRodney W. Grimes slen--; 3465b81b6b3SRodney W. Grimes } 3475b81b6b3SRodney W. Grimes else 3485b81b6b3SRodney W. Grimes { 3495b81b6b3SRodney W. Grimes GETNUM(cp, octave); 3500a97fb59SAndrey A. Chernov if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES) 3515b81b6b3SRodney W. Grimes octave = DFLT_OCTAVE; 3525b81b6b3SRodney W. Grimes octprefix = TRUE; 3535b81b6b3SRodney W. Grimes } 3545b81b6b3SRodney W. Grimes break; 3555b81b6b3SRodney W. Grimes 3565b81b6b3SRodney W. Grimes case '>': 3570a97fb59SAndrey A. Chernov if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1) 3585b81b6b3SRodney W. Grimes octave++; 3595b81b6b3SRodney W. Grimes octprefix = TRUE; 3605b81b6b3SRodney W. Grimes break; 3615b81b6b3SRodney W. Grimes 3625b81b6b3SRodney W. Grimes case '<': 3635b81b6b3SRodney W. Grimes if (octave > 0) 3645b81b6b3SRodney W. Grimes octave--; 3655b81b6b3SRodney W. Grimes octprefix = TRUE; 3665b81b6b3SRodney W. Grimes break; 3675b81b6b3SRodney W. Grimes 3685b81b6b3SRodney W. Grimes case 'N': 3695b81b6b3SRodney W. Grimes GETNUM(cp, pitch); 3705b81b6b3SRodney W. Grimes for (sustain = 0; cp[1] == '.'; cp++) 3715b81b6b3SRodney W. Grimes { 3725b81b6b3SRodney W. Grimes slen--; 3735b81b6b3SRodney W. Grimes sustain++; 3745b81b6b3SRodney W. Grimes } 375e614aa8aSAndrey A. Chernov oldfill = fill; 376e614aa8aSAndrey A. Chernov if (cp[1] == '_') 377e614aa8aSAndrey A. Chernov { 378e614aa8aSAndrey A. Chernov fill = LEGATO; 379e614aa8aSAndrey A. Chernov ++cp; 380e614aa8aSAndrey A. Chernov slen--; 381e614aa8aSAndrey A. Chernov } 382424e2d04SAndrey A. Chernov playtone(pitch - 1, value, sustain); 383e614aa8aSAndrey A. Chernov fill = oldfill; 3845b81b6b3SRodney W. Grimes break; 3855b81b6b3SRodney W. Grimes 3865b81b6b3SRodney W. Grimes case 'L': 3875b81b6b3SRodney W. Grimes GETNUM(cp, value); 3885b81b6b3SRodney W. Grimes if (value <= 0 || value > MIN_VALUE) 3895b81b6b3SRodney W. Grimes value = DFLT_VALUE; 3905b81b6b3SRodney W. Grimes break; 3915b81b6b3SRodney W. Grimes 3925b81b6b3SRodney W. Grimes case 'P': 3935b81b6b3SRodney W. Grimes case '~': 3945b81b6b3SRodney W. Grimes /* this may be followed by an override time value */ 3955b81b6b3SRodney W. Grimes GETNUM(cp, timeval); 3965b81b6b3SRodney W. Grimes if (timeval <= 0 || timeval > MIN_VALUE) 3975b81b6b3SRodney W. Grimes timeval = value; 3985b81b6b3SRodney W. Grimes for (sustain = 0; cp[1] == '.'; cp++) 3995b81b6b3SRodney W. Grimes { 4005b81b6b3SRodney W. Grimes slen--; 4015b81b6b3SRodney W. Grimes sustain++; 4025b81b6b3SRodney W. Grimes } 403424e2d04SAndrey A. Chernov playtone(-1, timeval, sustain); 4045b81b6b3SRodney W. Grimes break; 4055b81b6b3SRodney W. Grimes 4065b81b6b3SRodney W. Grimes case 'T': 4075b81b6b3SRodney W. Grimes GETNUM(cp, tempo); 4085b81b6b3SRodney W. Grimes if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) 4095b81b6b3SRodney W. Grimes tempo = DFLT_TEMPO; 4107344a290SBrian Somers whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / tempo; 4115b81b6b3SRodney W. Grimes break; 4125b81b6b3SRodney W. Grimes 4135b81b6b3SRodney W. Grimes case 'M': 4145b81b6b3SRodney W. Grimes if (cp[1] == 'N' || cp[1] == 'n') 4155b81b6b3SRodney W. Grimes { 4165b81b6b3SRodney W. Grimes fill = NORMAL; 4175b81b6b3SRodney W. Grimes ++cp; 4185b81b6b3SRodney W. Grimes slen--; 4195b81b6b3SRodney W. Grimes } 4205b81b6b3SRodney W. Grimes else if (cp[1] == 'L' || cp[1] == 'l') 4215b81b6b3SRodney W. Grimes { 4225b81b6b3SRodney W. Grimes fill = LEGATO; 4235b81b6b3SRodney W. Grimes ++cp; 4245b81b6b3SRodney W. Grimes slen--; 4255b81b6b3SRodney W. Grimes } 4265b81b6b3SRodney W. Grimes else if (cp[1] == 'S' || cp[1] == 's') 4275b81b6b3SRodney W. Grimes { 4285b81b6b3SRodney W. Grimes fill = STACCATO; 4295b81b6b3SRodney W. Grimes ++cp; 4305b81b6b3SRodney W. Grimes slen--; 4315b81b6b3SRodney W. Grimes } 4325b81b6b3SRodney W. Grimes break; 4335b81b6b3SRodney W. Grimes } 4345b81b6b3SRodney W. Grimes } 4355b81b6b3SRodney W. Grimes } 4365b81b6b3SRodney W. Grimes 4375b81b6b3SRodney W. Grimes /******************* UNIX DRIVER HOOKS BEGIN HERE ************************** 4385b81b6b3SRodney W. Grimes * 4395b81b6b3SRodney W. Grimes * This section implements driver hooks to run playstring() and the tone(), 4405b81b6b3SRodney W. Grimes * endtone(), and rest() functions defined above. 4415b81b6b3SRodney W. Grimes */ 4425b81b6b3SRodney W. Grimes 443e614aa8aSAndrey A. Chernov static int spkr_active = FALSE; /* exclusion flag */ 444e4961fbfSPoul-Henning Kamp static char *spkr_inbuf; /* incoming buf */ 4455b81b6b3SRodney W. Grimes 4463412120fSPoul-Henning Kamp static int 447b40ce416SJulian Elischer spkropen(dev, flags, fmt, td) 44889c9c53dSPoul-Henning Kamp struct cdev *dev; 44960039670SBruce Evans int flags; 45060039670SBruce Evans int fmt; 451b40ce416SJulian Elischer struct thread *td; 4525b81b6b3SRodney W. Grimes { 4535b81b6b3SRodney W. Grimes #ifdef DEBUG 454b8e49f68SBill Fumerola (void) printf("spkropen: entering with dev = %s\n", devtoname(dev)); 4555b81b6b3SRodney W. Grimes #endif /* DEBUG */ 4565b81b6b3SRodney W. Grimes 4575b81b6b3SRodney W. Grimes if (minor(dev) != 0) 4585b81b6b3SRodney W. Grimes return(ENXIO); 4595b81b6b3SRodney W. Grimes else if (spkr_active) 4605b81b6b3SRodney W. Grimes return(EBUSY); 4615b81b6b3SRodney W. Grimes else 4625b81b6b3SRodney W. Grimes { 463e614aa8aSAndrey A. Chernov #ifdef DEBUG 464e614aa8aSAndrey A. Chernov (void) printf("spkropen: about to perform play initialization\n"); 465e614aa8aSAndrey A. Chernov #endif /* DEBUG */ 4665b81b6b3SRodney W. Grimes playinit(); 467a163d034SWarner Losh spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK); 468e614aa8aSAndrey A. Chernov spkr_active = TRUE; 4695b81b6b3SRodney W. Grimes return(0); 4705b81b6b3SRodney W. Grimes } 471e614aa8aSAndrey A. Chernov } 4725b81b6b3SRodney W. Grimes 4733412120fSPoul-Henning Kamp static int 474f4de22acSJoerg Wunsch spkrwrite(dev, uio, ioflag) 47589c9c53dSPoul-Henning Kamp struct cdev *dev; 4765b81b6b3SRodney W. Grimes struct uio *uio; 47760039670SBruce Evans int ioflag; 4785b81b6b3SRodney W. Grimes { 4795b81b6b3SRodney W. Grimes #ifdef DEBUG 480b8e49f68SBill Fumerola printf("spkrwrite: entering with dev = %s, count = %d\n", 481b8e49f68SBill Fumerola devtoname(dev), uio->uio_resid); 4825b81b6b3SRodney W. Grimes #endif /* DEBUG */ 4835b81b6b3SRodney W. Grimes 4845b81b6b3SRodney W. Grimes if (minor(dev) != 0) 4855b81b6b3SRodney W. Grimes return(ENXIO); 4863bd9f6dbSPeter Wemm else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */ 487e614aa8aSAndrey A. Chernov return(E2BIG); 4885b81b6b3SRodney W. Grimes else 4895b81b6b3SRodney W. Grimes { 490e614aa8aSAndrey A. Chernov unsigned n; 491e614aa8aSAndrey A. Chernov char *cp; 492e614aa8aSAndrey A. Chernov int error; 493e614aa8aSAndrey A. Chernov 494e614aa8aSAndrey A. Chernov n = uio->uio_resid; 495e4961fbfSPoul-Henning Kamp cp = spkr_inbuf; 4963bd9f6dbSPeter Wemm error = uiomove(cp, n, uio); 4973bd9f6dbSPeter Wemm if (!error) { 4983bd9f6dbSPeter Wemm cp[n] = '\0'; 499424e2d04SAndrey A. Chernov playstring(cp, n); 5003bd9f6dbSPeter Wemm } 5015b81b6b3SRodney W. Grimes return(error); 5025b81b6b3SRodney W. Grimes } 5035b81b6b3SRodney W. Grimes } 5045b81b6b3SRodney W. Grimes 5053412120fSPoul-Henning Kamp static int 506b40ce416SJulian Elischer spkrclose(dev, flags, fmt, td) 50789c9c53dSPoul-Henning Kamp struct cdev *dev; 50860039670SBruce Evans int flags; 50960039670SBruce Evans int fmt; 510b40ce416SJulian Elischer struct thread *td; 5115b81b6b3SRodney W. Grimes { 5125b81b6b3SRodney W. Grimes #ifdef DEBUG 513b8e49f68SBill Fumerola (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev)); 5145b81b6b3SRodney W. Grimes #endif /* DEBUG */ 5155b81b6b3SRodney W. Grimes 5165b81b6b3SRodney W. Grimes if (minor(dev) != 0) 5175b81b6b3SRodney W. Grimes return(ENXIO); 5185b81b6b3SRodney W. Grimes else 5195b81b6b3SRodney W. Grimes { 520521f364bSDag-Erling Smørgrav wakeup(&endtone); 521521f364bSDag-Erling Smørgrav wakeup(&endrest); 522e4961fbfSPoul-Henning Kamp free(spkr_inbuf, M_SPKR); 523e614aa8aSAndrey A. Chernov spkr_active = FALSE; 5245b81b6b3SRodney W. Grimes return(0); 5255b81b6b3SRodney W. Grimes } 526e614aa8aSAndrey A. Chernov } 5275b81b6b3SRodney W. Grimes 5283412120fSPoul-Henning Kamp static int 529b40ce416SJulian Elischer spkrioctl(dev, cmd, cmdarg, flags, td) 53089c9c53dSPoul-Henning Kamp struct cdev *dev; 53100671271SBruce Evans unsigned long cmd; 5325b81b6b3SRodney W. Grimes caddr_t cmdarg; 53360039670SBruce Evans int flags; 534b40ce416SJulian Elischer struct thread *td; 5355b81b6b3SRodney W. Grimes { 5365b81b6b3SRodney W. Grimes #ifdef DEBUG 537d9183205SBruce Evans (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n", 538d9183205SBruce Evans devtoname(dev), cmd); 5395b81b6b3SRodney W. Grimes #endif /* DEBUG */ 5405b81b6b3SRodney W. Grimes 5415b81b6b3SRodney W. Grimes if (minor(dev) != 0) 5425b81b6b3SRodney W. Grimes return(ENXIO); 5435b81b6b3SRodney W. Grimes else if (cmd == SPKRTONE) 5445b81b6b3SRodney W. Grimes { 5455b81b6b3SRodney W. Grimes tone_t *tp = (tone_t *)cmdarg; 5465b81b6b3SRodney W. Grimes 5475b81b6b3SRodney W. Grimes if (tp->frequency == 0) 548424e2d04SAndrey A. Chernov rest(tp->duration); 5495b81b6b3SRodney W. Grimes else 550424e2d04SAndrey A. Chernov tone(tp->frequency, tp->duration); 551424e2d04SAndrey A. Chernov return 0; 5525b81b6b3SRodney W. Grimes } 5535b81b6b3SRodney W. Grimes else if (cmd == SPKRTUNE) 5545b81b6b3SRodney W. Grimes { 5555b81b6b3SRodney W. Grimes tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg); 5565b81b6b3SRodney W. Grimes tone_t ttp; 5575b81b6b3SRodney W. Grimes int error; 5585b81b6b3SRodney W. Grimes 5595b81b6b3SRodney W. Grimes for (; ; tp++) { 5605b81b6b3SRodney W. Grimes error = copyin(tp, &ttp, sizeof(tone_t)); 5615b81b6b3SRodney W. Grimes if (error) 5625b81b6b3SRodney W. Grimes return(error); 5635b81b6b3SRodney W. Grimes if (ttp.duration == 0) 5645b81b6b3SRodney W. Grimes break; 5655b81b6b3SRodney W. Grimes if (ttp.frequency == 0) 566424e2d04SAndrey A. Chernov rest(ttp.duration); 5675b81b6b3SRodney W. Grimes else 568424e2d04SAndrey A. Chernov tone(ttp.frequency, ttp.duration); 5695b81b6b3SRodney W. Grimes } 5705b81b6b3SRodney W. Grimes return(0); 5715b81b6b3SRodney W. Grimes } 572e614aa8aSAndrey A. Chernov return(EINVAL); 573e614aa8aSAndrey A. Chernov } 5745b81b6b3SRodney W. Grimes 5752a50a6d7SMike Smith /* 5762a50a6d7SMike Smith * Install placeholder to claim the resources owned by the 5772a50a6d7SMike Smith * AT tone generator. 5782a50a6d7SMike Smith */ 57930201b6cSMatthew N. Dodd static struct isa_pnp_id speaker_ids[] = { 58030201b6cSMatthew N. Dodd #ifndef PC98 581257427efSMatthew N. Dodd { 0x0008d041 /* PNP0800 */, SPKR_DESC }, 58230201b6cSMatthew N. Dodd #endif 5832a50a6d7SMike Smith { 0 } 5842a50a6d7SMike Smith }; 5852a50a6d7SMike Smith 58689c9c53dSPoul-Henning Kamp static struct cdev *speaker_dev; 587ce6d929bSWes Peters 5882a50a6d7SMike Smith static int 58930201b6cSMatthew N. Dodd speaker_probe(device_t dev) 5902a50a6d7SMike Smith { 591c7f718ecSMatthew N. Dodd int error; 592c7f718ecSMatthew N. Dodd 59330201b6cSMatthew N. Dodd error = ISA_PNP_PROBE(device_get_parent(dev), dev, speaker_ids); 594c7f718ecSMatthew N. Dodd 595c7f718ecSMatthew N. Dodd /* PnP match */ 596c7f718ecSMatthew N. Dodd if (error == 0) 597c7f718ecSMatthew N. Dodd return (0); 598c7f718ecSMatthew N. Dodd 599c7f718ecSMatthew N. Dodd /* No match */ 600c7f718ecSMatthew N. Dodd if (error == ENXIO) 601c7f718ecSMatthew N. Dodd return (ENXIO); 602c7f718ecSMatthew N. Dodd 603c7f718ecSMatthew N. Dodd /* Not configured by hints. */ 60430201b6cSMatthew N. Dodd if (strncmp(device_get_name(dev), "speaker", 9)) 605c7f718ecSMatthew N. Dodd return (ENXIO); 606c7f718ecSMatthew N. Dodd 607257427efSMatthew N. Dodd device_set_desc(dev, SPKR_DESC); 608c7f718ecSMatthew N. Dodd 609c7f718ecSMatthew N. Dodd return (0); 6102a50a6d7SMike Smith } 6112a50a6d7SMike Smith 6122a50a6d7SMike Smith static int 61330201b6cSMatthew N. Dodd speaker_attach(device_t dev) 6142a50a6d7SMike Smith { 615c7f718ecSMatthew N. Dodd 61630201b6cSMatthew N. Dodd if (speaker_dev) { 617c7f718ecSMatthew N. Dodd device_printf(dev, "Already attached!\n"); 618c7f718ecSMatthew N. Dodd return (ENXIO); 619c7f718ecSMatthew N. Dodd } 620c7f718ecSMatthew N. Dodd 62130201b6cSMatthew N. Dodd speaker_dev = make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 622ce6d929bSWes Peters "speaker"); 623ce6d929bSWes Peters return (0); 624ce6d929bSWes Peters } 625ce6d929bSWes Peters 626ce6d929bSWes Peters static int 62730201b6cSMatthew N. Dodd speaker_detach(device_t dev) 628ce6d929bSWes Peters { 62930201b6cSMatthew N. Dodd destroy_dev(speaker_dev); 6302a50a6d7SMike Smith return (0); 6312a50a6d7SMike Smith } 6322a50a6d7SMike Smith 63330201b6cSMatthew N. Dodd static device_method_t speaker_methods[] = { 6342a50a6d7SMike Smith /* Device interface */ 63530201b6cSMatthew N. Dodd DEVMETHOD(device_probe, speaker_probe), 63630201b6cSMatthew N. Dodd DEVMETHOD(device_attach, speaker_attach), 63730201b6cSMatthew N. Dodd DEVMETHOD(device_detach, speaker_detach), 6382a50a6d7SMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 6392a50a6d7SMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 6402a50a6d7SMike Smith DEVMETHOD(device_resume, bus_generic_resume), 6412a50a6d7SMike Smith { 0, 0 } 6422a50a6d7SMike Smith }; 6432a50a6d7SMike Smith 64430201b6cSMatthew N. Dodd static driver_t speaker_driver = { 64530201b6cSMatthew N. Dodd "speaker", 64630201b6cSMatthew N. Dodd speaker_methods, 6472a50a6d7SMike Smith 1, /* no softc */ 6482a50a6d7SMike Smith }; 6492a50a6d7SMike Smith 65030201b6cSMatthew N. Dodd static devclass_t speaker_devclass; 6512a50a6d7SMike Smith 65230201b6cSMatthew N. Dodd DRIVER_MODULE(speaker, isa, speaker_driver, speaker_devclass, 0, 0); 65330201b6cSMatthew N. Dodd #ifndef PC98 65430201b6cSMatthew N. Dodd DRIVER_MODULE(speaker, acpi, speaker_driver, speaker_devclass, 0, 0); 65530201b6cSMatthew N. Dodd #endif 65653ac6efbSJulian Elischer 6575b81b6b3SRodney W. Grimes /* spkr.c ends here */ 658