spkr.c revision 619
1/* 2 * spkr.c -- device driver for console speaker on 80386 3 * 4 * v1.1 by Eric S. Raymond (esr@snark.thyrsus.com) Feb 1990 5 * modified for 386bsd by Andrew A. Chernov <ache@astral.msk.su> 6 * 386bsd only clean version, all SYSV stuff removed 7 * use hz value from param.c 8 * 9 * $Id$ 10 */ 11 12#include "speaker.h" 13 14#if NSPEAKER > 0 15 16#include "param.h" 17#include "systm.h" 18#include "kernel.h" 19#include "errno.h" 20#include "buf.h" 21#include "uio.h" 22#include "spkr.h" 23 24/**************** MACHINE DEPENDENT PART STARTS HERE ************************* 25 * 26 * This section defines a function tone() which causes a tone of given 27 * frequency and duration from the 80x86's console speaker. 28 * Another function endtone() is defined to force sound off, and there is 29 * also a rest() entry point to do pauses. 30 * 31 * Audible sound is generated using the Programmable Interval Timer (PIT) and 32 * Programmable Peripheral Interface (PPI) attached to the 80x86's speaker. The 33 * PPI controls whether sound is passed through at all; the PIT's channel 2 is 34 * used to generate clicks (a square wave) of whatever frequency is desired. 35 */ 36 37/* 38 * PIT and PPI port addresses and control values 39 * 40 * Most of the magic is hidden in the TIMER_PREP value, which selects PIT 41 * channel 2, frequency LSB first, square-wave mode and binary encoding. 42 * The encoding is as follows: 43 * 44 * +----------+----------+---------------+-----+ 45 * | 1 0 | 1 1 | 0 1 1 | 0 | 46 * | SC1 SC0 | RW1 RW0 | M2 M1 M0 | BCD | 47 * +----------+----------+---------------+-----+ 48 * Counter Write Mode 3 Binary 49 * Channel 2 LSB first, (Square Wave) Encoding 50 * MSB second 51 */ 52#define PPI 0x61 /* port of Programmable Peripheral Interface */ 53#define PPI_SPKR 0x03 /* turn these PPI bits on to pass sound */ 54#define PIT_CTRL 0x43 /* PIT control address */ 55#define PIT_COUNT 0x42 /* PIT count address */ 56#define PIT_MODE 0xB6 /* set timer mode for sound generation */ 57 58/* 59 * Magic numbers for timer control. 60 */ 61#define TIMER_CLK 1193180L /* corresponds to 18.2 MHz tick rate */ 62 63static int endtone() 64/* turn off the speaker, ending current tone */ 65{ 66 wakeup((caddr_t)endtone); 67 outb(PPI, inb(PPI) & ~PPI_SPKR); 68} 69 70static void tone(hz, ticks) 71/* emit tone of frequency hz for given number of ticks */ 72unsigned int hz, ticks; 73{ 74 unsigned int divisor = TIMER_CLK / hz; 75 int sps; 76 77#ifdef DEBUG 78 printf("tone: hz=%d ticks=%d\n", hz, ticks); 79#endif /* DEBUG */ 80 81 /* set timer to generate clicks at given frequency in Hertz */ 82 sps = spltty(); 83 outb(PIT_CTRL, PIT_MODE); /* prepare timer */ 84 outb(PIT_COUNT, (unsigned char) divisor); /* send lo byte */ 85 outb(PIT_COUNT, (divisor >> 8)); /* send hi byte */ 86 splx(sps); 87 88 /* turn the speaker on */ 89 outb(PPI, inb(PPI) | PPI_SPKR); 90 91 /* 92 * Set timeout to endtone function, then give up the timeslice. 93 * This is so other processes can execute while the tone is being 94 * emitted. 95 */ 96 timeout((caddr_t)endtone, (caddr_t)NULL, ticks); 97 sleep((caddr_t)endtone, PZERO - 1); 98} 99 100static int endrest() 101/* end a rest */ 102{ 103 wakeup((caddr_t)endrest); 104} 105 106static void rest(ticks) 107/* rest for given number of ticks */ 108int ticks; 109{ 110 /* 111 * Set timeout to endrest function, then give up the timeslice. 112 * This is so other processes can execute while the rest is being 113 * waited out. 114 */ 115#ifdef DEBUG 116 printf("rest: %d\n", ticks); 117#endif /* DEBUG */ 118 timeout((caddr_t)endrest, (caddr_t)NULL, ticks); 119 sleep((caddr_t)endrest, PZERO - 1); 120} 121 122/**************** PLAY STRING INTERPRETER BEGINS HERE ********************** 123 * 124 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement; 125 * M[LNS] are missing and the ~ synonym and octave-tracking facility is added. 126 * Requires tone(), rest(), and endtone(). String play is not interruptible 127 * except possibly at physical block boundaries. 128 */ 129 130typedef int bool; 131#define TRUE 1 132#define FALSE 0 133 134#define toupper(c) ((c) - ' ' * (((c) >= 'a') && ((c) <= 'z'))) 135#define isdigit(c) (((c) >= '0') && ((c) <= '9')) 136#define dtoi(c) ((c) - '0') 137 138static int octave; /* currently selected octave */ 139static int whole; /* whole-note time at current tempo, in ticks */ 140static int value; /* whole divisor for note time, quarter note = 1 */ 141static int fill; /* controls spacing of notes */ 142static bool octtrack; /* octave-tracking on? */ 143static bool octprefix; /* override current octave-tracking state? */ 144 145/* 146 * Magic number avoidance... 147 */ 148#define SECS_PER_MIN 60 /* seconds per minute */ 149#define WHOLE_NOTE 4 /* quarter notes per whole note */ 150#define MIN_VALUE 64 /* the most we can divide a note by */ 151#define DFLT_VALUE 4 /* default value (quarter-note) */ 152#define FILLTIME 8 /* for articulation, break note in parts */ 153#define STACCATO 6 /* 6/8 = 3/4 of note is filled */ 154#define NORMAL 7 /* 7/8ths of note interval is filled */ 155#define LEGATO 8 /* all of note interval is filled */ 156#define DFLT_OCTAVE 4 /* default octave */ 157#define MIN_TEMPO 32 /* minimum tempo */ 158#define DFLT_TEMPO 120 /* default tempo */ 159#define MAX_TEMPO 255 /* max tempo */ 160#define NUM_MULT 3 /* numerator of dot multiplier */ 161#define DENOM_MULT 2 /* denominator of dot multiplier */ 162 163/* letter to half-tone: A B C D E F G */ 164static int notetab[8] = {9, 11, 0, 2, 4, 5, 7}; 165 166/* 167 * This is the American Standard A440 Equal-Tempered scale with frequencies 168 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook... 169 * our octave 0 is standard octave 2. 170 */ 171#define OCTAVE_NOTES 12 /* semitones per octave */ 172static int pitchtab[] = 173{ 174/* C C# D D# E F F# G G# A A# B*/ 175/* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123, 176/* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 177/* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 178/* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 179/* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 180/* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 181/* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902, 182}; 183 184static void playinit() 185{ 186 octave = DFLT_OCTAVE; 187 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; 188 fill = NORMAL; 189 value = DFLT_VALUE; 190 octtrack = FALSE; 191 octprefix = TRUE; /* act as though there was an initial O(n) */ 192} 193 194static void playtone(pitch, value, sustain) 195/* play tone of proper duration for current rhythm signature */ 196int pitch, value, sustain; 197{ 198 register int sound, silence, snum = 1, sdenom = 1; 199 200 /* this weirdness avoids floating-point arithmetic */ 201 for (; sustain; sustain--) 202 { 203 snum *= NUM_MULT; 204 sdenom *= DENOM_MULT; 205 } 206 207 if (pitch == -1) 208 rest(whole * snum / (value * sdenom)); 209 else 210 { 211 sound = (whole * snum) / (value * sdenom) 212 - (whole * (FILLTIME - fill)) / (value * FILLTIME); 213 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom); 214 215#ifdef DEBUG 216 printf("playtone: pitch %d for %d ticks, rest for %d ticks\n", 217 pitch, sound, silence); 218#endif /* DEBUG */ 219 220 tone(pitchtab[pitch], sound); 221 if (fill != LEGATO) 222 rest(silence); 223 } 224} 225 226static int abs(n) 227int n; 228{ 229 if (n < 0) 230 return(-n); 231 else 232 return(n); 233} 234 235static void playstring(cp, slen) 236/* interpret and play an item from a notation string */ 237char *cp; 238size_t slen; 239{ 240 int pitch, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE; 241 242#define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \ 243 {v = v * 10 + (*++cp - '0'); slen--;} 244 for (; slen--; cp++) 245 { 246 int sustain, timeval, tempo; 247 register char c = toupper(*cp); 248 249#ifdef DEBUG 250 printf("playstring: %c (%x)\n", c, c); 251#endif /* DEBUG */ 252 253 switch (c) 254 { 255 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 256 257 /* compute pitch */ 258 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES; 259 260 /* this may be followed by an accidental sign */ 261 if (cp[1] == '#' || cp[1] == '+') 262 { 263 ++pitch; 264 ++cp; 265 slen--; 266 } 267 else if (cp[1] == '-') 268 { 269 --pitch; 270 ++cp; 271 slen--; 272 } 273 274 /* 275 * If octave-tracking mode is on, and there has been no octave- 276 * setting prefix, find the version of the current letter note 277 * closest to the last regardless of octave. 278 */ 279 if (octtrack && !octprefix) 280 { 281 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch)) 282 { 283 ++octave; 284 pitch += OCTAVE_NOTES; 285 } 286 287 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch)) 288 { 289 --octave; 290 pitch -= OCTAVE_NOTES; 291 } 292 } 293 octprefix = FALSE; 294 lastpitch = pitch; 295 296 /* ...which may in turn be followed by an override time value */ 297 GETNUM(cp, timeval); 298 if (timeval <= 0 || timeval > MIN_VALUE) 299 timeval = value; 300 301 /* ...and/or sustain dots */ 302 for (sustain = 0; cp[1] == '.'; cp++) 303 { 304 slen--; 305 sustain++; 306 } 307 308 /* time to emit the actual tone */ 309 playtone(pitch, timeval, sustain); 310 break; 311 312 case 'O': 313 if (cp[1] == 'N' || cp[1] == 'n') 314 { 315 octprefix = octtrack = FALSE; 316 ++cp; 317 slen--; 318 } 319 else if (cp[1] == 'L' || cp[1] == 'l') 320 { 321 octtrack = TRUE; 322 ++cp; 323 slen--; 324 } 325 else 326 { 327 GETNUM(cp, octave); 328 if (octave >= sizeof(pitchtab) / OCTAVE_NOTES) 329 octave = DFLT_OCTAVE; 330 octprefix = TRUE; 331 } 332 break; 333 334 case '>': 335 if (octave < sizeof(pitchtab) / OCTAVE_NOTES - 1) 336 octave++; 337 octprefix = TRUE; 338 break; 339 340 case '<': 341 if (octave > 0) 342 octave--; 343 octprefix = TRUE; 344 break; 345 346 case 'N': 347 GETNUM(cp, pitch); 348 for (sustain = 0; cp[1] == '.'; cp++) 349 { 350 slen--; 351 sustain++; 352 } 353 playtone(pitch - 1, value, sustain); 354 break; 355 356 case 'L': 357 GETNUM(cp, value); 358 if (value <= 0 || value > MIN_VALUE) 359 value = DFLT_VALUE; 360 break; 361 362 case 'P': 363 case '~': 364 /* this may be followed by an override time value */ 365 GETNUM(cp, timeval); 366 if (timeval <= 0 || timeval > MIN_VALUE) 367 timeval = value; 368 for (sustain = 0; cp[1] == '.'; cp++) 369 { 370 slen--; 371 sustain++; 372 } 373 playtone(-1, timeval, sustain); 374 break; 375 376 case 'T': 377 GETNUM(cp, tempo); 378 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) 379 tempo = DFLT_TEMPO; 380 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo; 381 break; 382 383 case 'M': 384 if (cp[1] == 'N' || cp[1] == 'n') 385 { 386 fill = NORMAL; 387 ++cp; 388 slen--; 389 } 390 else if (cp[1] == 'L' || cp[1] == 'l') 391 { 392 fill = LEGATO; 393 ++cp; 394 slen--; 395 } 396 else if (cp[1] == 'S' || cp[1] == 's') 397 { 398 fill = STACCATO; 399 ++cp; 400 slen--; 401 } 402 break; 403 } 404 } 405} 406 407/******************* UNIX DRIVER HOOKS BEGIN HERE ************************** 408 * 409 * This section implements driver hooks to run playstring() and the tone(), 410 * endtone(), and rest() functions defined above. 411 */ 412 413static int spkr_active; /* exclusion flag */ 414static struct buf *spkr_inbuf; /* incoming buf */ 415 416int spkropen(dev) 417dev_t dev; 418{ 419#ifdef DEBUG 420 printf("spkropen: entering with dev = %x\n", dev); 421#endif /* DEBUG */ 422 423 if (minor(dev) != 0) 424 return(ENXIO); 425 else if (spkr_active) 426 return(EBUSY); 427 else 428 { 429 playinit(); 430 spkr_inbuf = geteblk(DEV_BSIZE); 431 spkr_active = 1; 432 } 433 return(0); 434} 435 436int spkrwrite(dev, uio) 437dev_t dev; 438struct uio *uio; 439{ 440 register unsigned n; 441 char *cp; 442 int error; 443#ifdef DEBUG 444 printf("spkrwrite: entering with dev = %x, count = %d\n", 445 dev, uio->uio_resid); 446#endif /* DEBUG */ 447 448 if (minor(dev) != 0) 449 return(ENXIO); 450 else 451 { 452 n = MIN(DEV_BSIZE, uio->uio_resid); 453 cp = spkr_inbuf->b_un.b_addr; 454 error = uiomove(cp, n, uio); 455 if (!error) 456 playstring(cp, n); 457 return(error); 458 } 459} 460 461int spkrclose(dev) 462dev_t dev; 463{ 464#ifdef DEBUG 465 printf("spkrclose: entering with dev = %x\n", dev); 466#endif /* DEBUG */ 467 468 if (minor(dev) != 0) 469 return(ENXIO); 470 else 471 { 472 endtone(); 473 brelse(spkr_inbuf); 474 spkr_active = 0; 475 } 476 return(0); 477} 478 479int spkrioctl(dev, cmd, cmdarg) 480dev_t dev; 481int cmd; 482caddr_t cmdarg; 483{ 484#ifdef DEBUG 485 printf("spkrioctl: entering with dev = %x, cmd = %x\n", dev, cmd); 486#endif /* DEBUG */ 487 488 if (minor(dev) != 0) 489 return(ENXIO); 490 else if (cmd == SPKRTONE) 491 { 492 tone_t *tp = (tone_t *)cmdarg; 493 494 if (tp->frequency == 0) 495 rest(tp->duration); 496 else 497 tone(tp->frequency, tp->duration); 498 } 499 else if (cmd == SPKRTUNE) 500 { 501 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg); 502 tone_t ttp; 503 int error; 504 505 for (; ; tp++) { 506 error = copyin(tp, &ttp, sizeof(tone_t)); 507 if (error) 508 return(error); 509 if (ttp.duration == 0) 510 break; 511 if (ttp.frequency == 0) 512 rest(ttp.duration); 513 else 514 tone(ttp.frequency, ttp.duration); 515 } 516 } 517 else 518 return(EINVAL); 519 return(0); 520} 521 522#endif /* NSPEAKER > 0 */ 523/* spkr.c ends here */ 524