1 /* 2 * spkr.c -- device driver for console speaker 3 * 4 * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993 5 * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su> 6 * 7 * $FreeBSD$ 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/kernel.h> 13 #include <sys/uio.h> 14 #include <sys/conf.h> 15 #include <sys/ctype.h> 16 #include <sys/malloc.h> 17 #include <i386/isa/isa.h> 18 #include <i386/isa/timerreg.h> 19 #include <machine/clock.h> 20 #include <machine/speaker.h> 21 22 static d_open_t spkropen; 23 static d_close_t spkrclose; 24 static d_write_t spkrwrite; 25 static d_ioctl_t spkrioctl; 26 27 #define CDEV_MAJOR 26 28 static struct cdevsw spkr_cdevsw = { 29 /* open */ spkropen, 30 /* close */ spkrclose, 31 /* read */ noread, 32 /* write */ spkrwrite, 33 /* ioctl */ spkrioctl, 34 /* poll */ nopoll, 35 /* mmap */ nommap, 36 /* strategy */ nostrategy, 37 /* name */ "spkr", 38 /* maj */ CDEV_MAJOR, 39 /* dump */ nodump, 40 /* psize */ nopsize, 41 /* flags */ 0, 42 /* bmaj */ -1 43 }; 44 45 MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer"); 46 47 /**************** MACHINE DEPENDENT PART STARTS HERE ************************* 48 * 49 * This section defines a function tone() which causes a tone of given 50 * frequency and duration from the ISA console speaker. 51 * Another function endtone() is defined to force sound off, and there is 52 * also a rest() entry point to do pauses. 53 * 54 * Audible sound is generated using the Programmable Interval Timer (PIT) and 55 * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The 56 * PPI controls whether sound is passed through at all; the PIT's channel 2 is 57 * used to generate clicks (a square wave) of whatever frequency is desired. 58 */ 59 60 /* 61 * PPI control values. 62 * XXX should be in a header and used in clock.c. 63 */ 64 #define PPI_SPKR 0x03 /* turn these PPI bits on to pass sound */ 65 66 #define SPKRPRI PSOCK 67 static char endtone, endrest; 68 69 static void tone __P((unsigned int thz, unsigned int ticks)); 70 static void rest __P((int ticks)); 71 static void playinit __P((void)); 72 static void playtone __P((int pitch, int value, int sustain)); 73 static int abs __P((int n)); 74 static void playstring __P((char *cp, size_t slen)); 75 76 /* emit tone of frequency thz for given number of ticks */ 77 static void 78 tone(thz, ticks) 79 unsigned int thz, ticks; 80 { 81 unsigned int divisor; 82 int sps; 83 84 if (thz <= 0) 85 return; 86 87 divisor = timer_freq / thz; 88 89 #ifdef DEBUG 90 (void) printf("tone: thz=%d ticks=%d\n", thz, ticks); 91 #endif /* DEBUG */ 92 93 /* set timer to generate clicks at given frequency in Hertz */ 94 sps = splclock(); 95 96 if (acquire_timer2(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT)) { 97 /* enter list of waiting procs ??? */ 98 splx(sps); 99 return; 100 } 101 splx(sps); 102 disable_intr(); 103 outb(TIMER_CNTR2, (divisor & 0xff)); /* send lo byte */ 104 outb(TIMER_CNTR2, (divisor >> 8)); /* send hi byte */ 105 enable_intr(); 106 107 /* turn the speaker on */ 108 outb(IO_PPI, inb(IO_PPI) | PPI_SPKR); 109 110 /* 111 * Set timeout to endtone function, then give up the timeslice. 112 * This is so other processes can execute while the tone is being 113 * emitted. 114 */ 115 if (ticks > 0) 116 tsleep((caddr_t)&endtone, SPKRPRI | PCATCH, "spkrtn", ticks); 117 outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR); 118 sps = splclock(); 119 release_timer2(); 120 splx(sps); 121 } 122 123 /* rest for given number of ticks */ 124 static void 125 rest(ticks) 126 int ticks; 127 { 128 /* 129 * Set timeout to endrest function, then give up the timeslice. 130 * This is so other processes can execute while the rest is being 131 * waited out. 132 */ 133 #ifdef DEBUG 134 (void) printf("rest: %d\n", ticks); 135 #endif /* DEBUG */ 136 if (ticks > 0) 137 tsleep((caddr_t)&endrest, SPKRPRI | PCATCH, "spkrrs", ticks); 138 } 139 140 /**************** PLAY STRING INTERPRETER BEGINS HERE ********************** 141 * 142 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement; 143 * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave- 144 * tracking facility are added. 145 * Requires tone(), rest(), and endtone(). String play is not interruptible 146 * except possibly at physical block boundaries. 147 */ 148 149 typedef int bool; 150 #define TRUE 1 151 #define FALSE 0 152 153 #define dtoi(c) ((c) - '0') 154 155 static int octave; /* currently selected octave */ 156 static int whole; /* whole-note time at current tempo, in ticks */ 157 static int value; /* whole divisor for note time, quarter note = 1 */ 158 static int fill; /* controls spacing of notes */ 159 static bool octtrack; /* octave-tracking on? */ 160 static bool octprefix; /* override current octave-tracking state? */ 161 162 /* 163 * Magic number avoidance... 164 */ 165 #define SECS_PER_MIN 60 /* seconds per minute */ 166 #define WHOLE_NOTE 4 /* quarter notes per whole note */ 167 #define MIN_VALUE 64 /* the most we can divide a note by */ 168 #define DFLT_VALUE 4 /* default value (quarter-note) */ 169 #define FILLTIME 8 /* for articulation, break note in parts */ 170 #define STACCATO 6 /* 6/8 = 3/4 of note is filled */ 171 #define NORMAL 7 /* 7/8ths of note interval is filled */ 172 #define LEGATO 8 /* all of note interval is filled */ 173 #define DFLT_OCTAVE 4 /* default octave */ 174 #define MIN_TEMPO 32 /* minimum tempo */ 175 #define DFLT_TEMPO 120 /* default tempo */ 176 #define MAX_TEMPO 255 /* max tempo */ 177 #define NUM_MULT 3 /* numerator of dot multiplier */ 178 #define DENOM_MULT 2 /* denominator of dot multiplier */ 179 180 /* letter to half-tone: A B C D E F G */ 181 static int notetab[8] = {9, 11, 0, 2, 4, 5, 7}; 182 183 /* 184 * This is the American Standard A440 Equal-Tempered scale with frequencies 185 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook... 186 * our octave 0 is standard octave 2. 187 */ 188 #define OCTAVE_NOTES 12 /* semitones per octave */ 189 static int pitchtab[] = 190 { 191 /* C C# D D# E F F# G G# A A# B*/ 192 /* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123, 193 /* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 194 /* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 195 /* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 196 /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 197 /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 198 /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902, 199 }; 200 201 static void 202 playinit() 203 { 204 octave = DFLT_OCTAVE; 205 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; 206 fill = NORMAL; 207 value = DFLT_VALUE; 208 octtrack = FALSE; 209 octprefix = TRUE; /* act as though there was an initial O(n) */ 210 } 211 212 /* play tone of proper duration for current rhythm signature */ 213 static void 214 playtone(pitch, value, sustain) 215 int pitch, value, sustain; 216 { 217 register int sound, silence, snum = 1, sdenom = 1; 218 219 /* this weirdness avoids floating-point arithmetic */ 220 for (; sustain; sustain--) 221 { 222 /* See the BUGS section in the man page for discussion */ 223 snum *= NUM_MULT; 224 sdenom *= DENOM_MULT; 225 } 226 227 if (value == 0 || sdenom == 0) 228 return; 229 230 if (pitch == -1) 231 rest(whole * snum / (value * sdenom)); 232 else 233 { 234 sound = (whole * snum) / (value * sdenom) 235 - (whole * (FILLTIME - fill)) / (value * FILLTIME); 236 silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom); 237 238 #ifdef DEBUG 239 (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n", 240 pitch, sound, silence); 241 #endif /* DEBUG */ 242 243 tone(pitchtab[pitch], sound); 244 if (fill != LEGATO) 245 rest(silence); 246 } 247 } 248 249 static int 250 abs(n) 251 int n; 252 { 253 if (n < 0) 254 return(-n); 255 else 256 return(n); 257 } 258 259 /* interpret and play an item from a notation string */ 260 static void 261 playstring(cp, slen) 262 char *cp; 263 size_t slen; 264 { 265 int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE; 266 267 #define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \ 268 {v = v * 10 + (*++cp - '0'); slen--;} 269 for (; slen--; cp++) 270 { 271 int sustain, timeval, tempo; 272 register char c = toupper(*cp); 273 274 #ifdef DEBUG 275 (void) printf("playstring: %c (%x)\n", c, c); 276 #endif /* DEBUG */ 277 278 switch (c) 279 { 280 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 281 282 /* compute pitch */ 283 pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES; 284 285 /* this may be followed by an accidental sign */ 286 if (cp[1] == '#' || cp[1] == '+') 287 { 288 ++pitch; 289 ++cp; 290 slen--; 291 } 292 else if (cp[1] == '-') 293 { 294 --pitch; 295 ++cp; 296 slen--; 297 } 298 299 /* 300 * If octave-tracking mode is on, and there has been no octave- 301 * setting prefix, find the version of the current letter note 302 * closest to the last regardless of octave. 303 */ 304 if (octtrack && !octprefix) 305 { 306 if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch)) 307 { 308 ++octave; 309 pitch += OCTAVE_NOTES; 310 } 311 312 if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch)) 313 { 314 --octave; 315 pitch -= OCTAVE_NOTES; 316 } 317 } 318 octprefix = FALSE; 319 lastpitch = pitch; 320 321 /* ...which may in turn be followed by an override time value */ 322 GETNUM(cp, timeval); 323 if (timeval <= 0 || timeval > MIN_VALUE) 324 timeval = value; 325 326 /* ...and/or sustain dots */ 327 for (sustain = 0; cp[1] == '.'; cp++) 328 { 329 slen--; 330 sustain++; 331 } 332 333 /* ...and/or a slur mark */ 334 oldfill = fill; 335 if (cp[1] == '_') 336 { 337 fill = LEGATO; 338 ++cp; 339 slen--; 340 } 341 342 /* time to emit the actual tone */ 343 playtone(pitch, timeval, sustain); 344 345 fill = oldfill; 346 break; 347 348 case 'O': 349 if (cp[1] == 'N' || cp[1] == 'n') 350 { 351 octprefix = octtrack = FALSE; 352 ++cp; 353 slen--; 354 } 355 else if (cp[1] == 'L' || cp[1] == 'l') 356 { 357 octtrack = TRUE; 358 ++cp; 359 slen--; 360 } 361 else 362 { 363 GETNUM(cp, octave); 364 if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES) 365 octave = DFLT_OCTAVE; 366 octprefix = TRUE; 367 } 368 break; 369 370 case '>': 371 if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1) 372 octave++; 373 octprefix = TRUE; 374 break; 375 376 case '<': 377 if (octave > 0) 378 octave--; 379 octprefix = TRUE; 380 break; 381 382 case 'N': 383 GETNUM(cp, pitch); 384 for (sustain = 0; cp[1] == '.'; cp++) 385 { 386 slen--; 387 sustain++; 388 } 389 oldfill = fill; 390 if (cp[1] == '_') 391 { 392 fill = LEGATO; 393 ++cp; 394 slen--; 395 } 396 playtone(pitch - 1, value, sustain); 397 fill = oldfill; 398 break; 399 400 case 'L': 401 GETNUM(cp, value); 402 if (value <= 0 || value > MIN_VALUE) 403 value = DFLT_VALUE; 404 break; 405 406 case 'P': 407 case '~': 408 /* this may be followed by an override time value */ 409 GETNUM(cp, timeval); 410 if (timeval <= 0 || timeval > MIN_VALUE) 411 timeval = value; 412 for (sustain = 0; cp[1] == '.'; cp++) 413 { 414 slen--; 415 sustain++; 416 } 417 playtone(-1, timeval, sustain); 418 break; 419 420 case 'T': 421 GETNUM(cp, tempo); 422 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) 423 tempo = DFLT_TEMPO; 424 whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo; 425 break; 426 427 case 'M': 428 if (cp[1] == 'N' || cp[1] == 'n') 429 { 430 fill = NORMAL; 431 ++cp; 432 slen--; 433 } 434 else if (cp[1] == 'L' || cp[1] == 'l') 435 { 436 fill = LEGATO; 437 ++cp; 438 slen--; 439 } 440 else if (cp[1] == 'S' || cp[1] == 's') 441 { 442 fill = STACCATO; 443 ++cp; 444 slen--; 445 } 446 break; 447 } 448 } 449 } 450 451 /******************* UNIX DRIVER HOOKS BEGIN HERE ************************** 452 * 453 * This section implements driver hooks to run playstring() and the tone(), 454 * endtone(), and rest() functions defined above. 455 */ 456 457 static int spkr_active = FALSE; /* exclusion flag */ 458 static char *spkr_inbuf; /* incoming buf */ 459 460 int 461 spkropen(dev, flags, fmt, p) 462 dev_t dev; 463 int flags; 464 int fmt; 465 struct proc *p; 466 { 467 #ifdef DEBUG 468 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev)); 469 #endif /* DEBUG */ 470 471 if (minor(dev) != 0) 472 return(ENXIO); 473 else if (spkr_active) 474 return(EBUSY); 475 else 476 { 477 #ifdef DEBUG 478 (void) printf("spkropen: about to perform play initialization\n"); 479 #endif /* DEBUG */ 480 playinit(); 481 spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK); 482 spkr_active = TRUE; 483 return(0); 484 } 485 } 486 487 int 488 spkrwrite(dev, uio, ioflag) 489 dev_t dev; 490 struct uio *uio; 491 int ioflag; 492 { 493 #ifdef DEBUG 494 printf("spkrwrite: entering with dev = %s, count = %d\n", 495 devtoname(dev), uio->uio_resid); 496 #endif /* DEBUG */ 497 498 if (minor(dev) != 0) 499 return(ENXIO); 500 else if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */ 501 return(E2BIG); 502 else 503 { 504 unsigned n; 505 char *cp; 506 int error; 507 508 n = uio->uio_resid; 509 cp = spkr_inbuf; 510 error = uiomove(cp, n, uio); 511 if (!error) { 512 cp[n] = '\0'; 513 playstring(cp, n); 514 } 515 return(error); 516 } 517 } 518 519 int 520 spkrclose(dev, flags, fmt, p) 521 dev_t dev; 522 int flags; 523 int fmt; 524 struct proc *p; 525 { 526 #ifdef DEBUG 527 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev)); 528 #endif /* DEBUG */ 529 530 if (minor(dev) != 0) 531 return(ENXIO); 532 else 533 { 534 wakeup((caddr_t)&endtone); 535 wakeup((caddr_t)&endrest); 536 free(spkr_inbuf, M_SPKR); 537 spkr_active = FALSE; 538 return(0); 539 } 540 } 541 542 int 543 spkrioctl(dev, cmd, cmdarg, flags, p) 544 dev_t dev; 545 unsigned long cmd; 546 caddr_t cmdarg; 547 int flags; 548 struct proc *p; 549 { 550 #ifdef DEBUG 551 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n", 552 devtoname(dev), cmd); 553 #endif /* DEBUG */ 554 555 if (minor(dev) != 0) 556 return(ENXIO); 557 else if (cmd == SPKRTONE) 558 { 559 tone_t *tp = (tone_t *)cmdarg; 560 561 if (tp->frequency == 0) 562 rest(tp->duration); 563 else 564 tone(tp->frequency, tp->duration); 565 return 0; 566 } 567 else if (cmd == SPKRTUNE) 568 { 569 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg); 570 tone_t ttp; 571 int error; 572 573 for (; ; tp++) { 574 error = copyin(tp, &ttp, sizeof(tone_t)); 575 if (error) 576 return(error); 577 if (ttp.duration == 0) 578 break; 579 if (ttp.frequency == 0) 580 rest(ttp.duration); 581 else 582 tone(ttp.frequency, ttp.duration); 583 } 584 return(0); 585 } 586 return(EINVAL); 587 } 588 589 static void 590 spkr_drvinit(void *unused) 591 { 592 make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "speaker"); 593 } 594 595 SYSINIT(spkrdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,spkr_drvinit,NULL) 596 597 598 /* spkr.c ends here */ 599