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