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