spkr.c revision 331643
1157873Simp/*- 2157873Simp * spkr.c -- device driver for console speaker 3157873Simp * 4157873Simp * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993 5157873Simp * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su> 6157873Simp * modified for PC98 by Kakefuda 7157873Simp */ 8157873Simp 9157873Simp#include <sys/cdefs.h> 10157873Simp__FBSDID("$FreeBSD: stable/11/sys/dev/speaker/spkr.c 331643 2018-03-27 18:52:27Z dim $"); 11157873Simp 12157873Simp#include <sys/param.h> 13157873Simp#include <sys/systm.h> 14157873Simp#include <sys/kernel.h> 15157873Simp#include <sys/module.h> 16157873Simp#include <sys/uio.h> 17157873Simp#include <sys/conf.h> 18157873Simp#include <sys/ctype.h> 19157873Simp#include <sys/malloc.h> 20157873Simp#include <machine/clock.h> 21157873Simp#include <dev/speaker/speaker.h> 22157873Simp 23157873Simpstatic d_open_t spkropen; 24157873Simpstatic d_close_t spkrclose; 25157873Simpstatic d_write_t spkrwrite; 26157873Simpstatic d_ioctl_t spkrioctl; 27157873Simp 28157873Simpstatic struct cdevsw spkr_cdevsw = { 29157873Simp .d_version = D_VERSION, 30157873Simp .d_flags = D_NEEDGIANT, 31157873Simp .d_open = spkropen, 32157873Simp .d_close = spkrclose, 33157873Simp .d_write = spkrwrite, 34157873Simp .d_ioctl = spkrioctl, 35157873Simp .d_name = "spkr", 36157873Simp}; 37157873Simp 38157873Simpstatic MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer"); 39157873Simp 40157873Simp/* 41157873Simp **************** MACHINE DEPENDENT PART STARTS HERE ************************* 42157873Simp * This section defines a function tone() which causes a tone of given 43157873Simp * frequency and duration from the ISA console speaker. 44157873Simp * Another function endtone() is defined to force sound off, and there is 45157873Simp * also a rest() entry point to do pauses. 46157873Simp * 47157873Simp * Audible sound is generated using the Programmable Interval Timer (PIT) and 48157873Simp * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The 49157873Simp * PPI controls whether sound is passed through at all; the PIT's channel 2 is 50157873Simp * used to generate clicks (a square wave) of whatever frequency is desired. 51157873Simp */ 52157873Simp 53157873Simp#define SPKRPRI PSOCK 54157873Simpstatic char endtone, endrest; 55157873Simp 56157873Simpstatic void tone(unsigned int thz, unsigned int centisecs); 57157873Simpstatic void rest(int centisecs); 58157873Simpstatic void playinit(void); 59157873Simpstatic void playtone(int pitch, int value, int sustain); 60157873Simpstatic void playstring(char *cp, size_t slen); 61157873Simp 62157873Simp/* 63157873Simp * Emit tone of frequency thz for given number of centisecs 64157873Simp */ 65157873Simpstatic void 66157873Simptone(unsigned int thz, unsigned int centisecs) 67157873Simp{ 68157873Simp int sps, timo; 69157873Simp 70157873Simp if (thz <= 0) 71157873Simp return; 72157873Simp 73157873Simp#ifdef DEBUG 74157873Simp (void) printf("tone: thz=%d centisecs=%d\n", thz, centisecs); 75157873Simp#endif /* DEBUG */ 76157873Simp 77157873Simp /* set timer to generate clicks at given frequency in Hertz */ 78157873Simp sps = splclock(); 79157873Simp 80157873Simp if (timer_spkr_acquire()) { 81157873Simp /* enter list of waiting procs ??? */ 82157873Simp splx(sps); 83157873Simp return; 84157873Simp } 85157873Simp splx(sps); 86157873Simp disable_intr(); 87157873Simp timer_spkr_setfreq(thz); 88157873Simp enable_intr(); 89157873Simp 90157873Simp /* 91157873Simp * Set timeout to endtone function, then give up the timeslice. 92157873Simp * This is so other processes can execute while the tone is being 93157873Simp * emitted. 94157873Simp */ 95157873Simp timo = centisecs * hz / 100; 96157873Simp if (timo > 0) 97157873Simp tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", timo); 98157873Simp sps = splclock(); 99157873Simp timer_spkr_release(); 100157873Simp splx(sps); 101157873Simp} 102157873Simp 103157873Simp/* 104157873Simp * Rest for given number of centisecs 105157873Simp */ 106157873Simpstatic void 107157873Simprest(int centisecs) 108157873Simp{ 109157873Simp int timo; 110157873Simp 111157873Simp /* 112157873Simp * Set timeout to endrest function, then give up the timeslice. 113157873Simp * This is so other processes can execute while the rest is being 114157873Simp * waited out. 115157873Simp */ 116157873Simp#ifdef DEBUG 117157873Simp (void) printf("rest: %d\n", centisecs); 118157873Simp#endif /* DEBUG */ 119157873Simp timo = centisecs * hz / 100; 120157873Simp if (timo > 0) 121157873Simp tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", timo); 122157873Simp} 123157873Simp 124157873Simp/* 125157873Simp **************** PLAY STRING INTERPRETER BEGINS HERE ********************** 126157873Simp * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement; 127157873Simp * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave- 128157873Simp * tracking facility are added. 129157873Simp * Requires tone(), rest(), and endtone(). String play is not interruptible 130157873Simp * except possibly at physical block boundaries. 131157873Simp */ 132157873Simp 133157873Simp#ifndef __bool_true_false_are_defined 134157873Simptypedef int bool; 135157873Simp#endif 136157873Simp#define TRUE 1 137157873Simp#define FALSE 0 138157873Simp 139157873Simp#define dtoi(c) ((c) - '0') 140157873Simp 141157873Simpstatic int octave; /* currently selected octave */ 142157873Simpstatic int whole; /* whole-note time at current tempo, in ticks */ 143157873Simpstatic int value; /* whole divisor for note time, quarter note = 1 */ 144157873Simpstatic int fill; /* controls spacing of notes */ 145157873Simpstatic bool octtrack; /* octave-tracking on? */ 146157873Simpstatic bool octprefix; /* override current octave-tracking state? */ 147157873Simp 148157873Simp/* 149157873Simp * Magic number avoidance... 150157873Simp */ 151157873Simp#define SECS_PER_MIN 60 /* seconds per minute */ 152157873Simp#define WHOLE_NOTE 4 /* quarter notes per whole note */ 153157873Simp#define MIN_VALUE 64 /* the most we can divide a note by */ 154157873Simp#define DFLT_VALUE 4 /* default value (quarter-note) */ 155157873Simp#define FILLTIME 8 /* for articulation, break note in parts */ 156157873Simp#define STACCATO 6 /* 6/8 = 3/4 of note is filled */ 157157873Simp#define NORMAL 7 /* 7/8ths of note interval is filled */ 158157873Simp#define LEGATO 8 /* all of note interval is filled */ 159157873Simp#define DFLT_OCTAVE 4 /* default octave */ 160157873Simp#define MIN_TEMPO 32 /* minimum tempo */ 161157873Simp#define DFLT_TEMPO 120 /* default tempo */ 162157873Simp#define MAX_TEMPO 255 /* max tempo */ 163157873Simp#define NUM_MULT 3 /* numerator of dot multiplier */ 164157873Simp#define DENOM_MULT 2 /* denominator of dot multiplier */ 165157873Simp 166157873Simp/* letter to half-tone: A B C D E F G */ 167157873Simpstatic int notetab[8] = {9, 11, 0, 2, 4, 5, 7}; 168157873Simp 169157873Simp/* 170157873Simp * This is the American Standard A440 Equal-Tempered scale with frequencies 171157873Simp * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook... 172157873Simp * our octave 0 is standard octave 2. 173157873Simp */ 174157873Simp#define OCTAVE_NOTES 12 /* semitones per octave */ 175157873Simpstatic int pitchtab[] = 176157873Simp{ 177157873Simp/* C C# D D# E F F# G G# A A# B*/ 178157873Simp/* 0 */ 65, 69, 73, 78, 82, 87, 93, 98, 103, 110, 117, 123, 179157873Simp/* 1 */ 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 180157873Simp/* 2 */ 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 181157873Simp/* 3 */ 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 182157873Simp/* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975, 183157873Simp/* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 184157873Simp/* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902, 185157873Simp}; 186157873Simp 187157873Simpstatic void 188157873Simpplayinit() 189157873Simp{ 190157873Simp octave = DFLT_OCTAVE; 191157873Simp whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO; 192157873Simp fill = NORMAL; 193157873Simp value = DFLT_VALUE; 194157873Simp octtrack = FALSE; 195157873Simp octprefix = TRUE; /* act as though there was an initial O(n) */ 196157873Simp} 197157873Simp 198157873Simp/* 199157873Simp * Play tone of proper duration for current rhythm signature 200157873Simp */ 201157873Simpstatic void 202157873Simpplaytone(int pitch, int value, int sustain) 203157873Simp{ 204157873Simp int sound, silence, snum = 1, sdenom = 1; 205157873Simp 206157873Simp /* this weirdness avoids floating-point arithmetic */ 207157873Simp for (; sustain; sustain--) { 208157873Simp /* See the BUGS section in the man page for discussion */ 209157873Simp snum *= NUM_MULT; 210157873Simp sdenom *= DENOM_MULT; 211157873Simp } 212157873Simp 213157873Simp if (value == 0 || sdenom == 0) 214157873Simp return; 215157873Simp 216157873Simp if (pitch == -1) 217157873Simp rest(whole * snum / (value * sdenom)); 218157873Simp else { 219157873Simp sound = (whole * snum) / (value * sdenom) 220157873Simp - (whole * (FILLTIME - fill)) / (value * FILLTIME); 221157873Simp silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom); 222157873Simp 223157873Simp#ifdef DEBUG 224157873Simp (void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n", 225157873Simp pitch, sound, silence); 226157873Simp#endif /* DEBUG */ 227157873Simp 228157873Simp tone(pitchtab[pitch], sound); 229157873Simp if (fill != LEGATO) 230157873Simp rest(silence); 231157873Simp } 232157873Simp} 233157873Simp 234157873Simp/* 235157873Simp * Interpret and play an item from a notation string 236157873Simp */ 237157873Simpstatic void 238157873Simpplaystring(char *cp, size_t slen) 239157873Simp{ 240157873Simp int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE; 241157873Simp 242157873Simp#define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \ 243157873Simp {v = v * 10 + (*++cp - '0'); slen--;} 244157873Simp for (; slen--; cp++) { 245157873Simp int sustain, timeval, tempo; 246157873Simp char c = toupper(*cp); 247157873Simp 248157873Simp#ifdef DEBUG 249157873Simp (void) printf("playstring: %c (%x)\n", c, c); 250157873Simp#endif /* DEBUG */ 251157873Simp 252157873Simp switch (c) { 253157873Simp case 'A': 254157873Simp case 'B': 255157873Simp case 'C': 256157873Simp case 'D': 257157873Simp case 'E': 258157873Simp case 'F': 259157873Simp case 'G': 260157873Simp /* compute pitch */ 261157873Simp pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES; 262157873Simp 263157873Simp /* this may be followed by an accidental sign */ 264157873Simp if (cp[1] == '#' || cp[1] == '+') { 265157873Simp ++pitch; 266157873Simp ++cp; 267157873Simp slen--; 268157873Simp } else if (cp[1] == '-') { 269157873Simp --pitch; 270157873Simp ++cp; 271157873Simp slen--; 272157873Simp } 273157873Simp 274157873Simp /* 275157873Simp * If octave-tracking mode is on, and there has been no octave- 276157873Simp * setting prefix, find the version of the current letter note 277157873Simp * closest to the last regardless of octave. 278157873Simp */ 279157873Simp if (octtrack && !octprefix) { 280157873Simp if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES - 281157873Simp lastpitch)) { 282157873Simp ++octave; 283157873Simp pitch += OCTAVE_NOTES; 284157873Simp } 285157873Simp 286157873Simp if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES) - 287157873Simp lastpitch)) { 288157873Simp --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 >= nitems(pitchtab) / OCTAVE_NOTES) 331 octave = DFLT_OCTAVE; 332 octprefix = TRUE; 333 } 334 break; 335 case '>': 336 if (octave < nitems(pitchtab) / OCTAVE_NOTES - 1) 337 octave++; 338 octprefix = TRUE; 339 break; 340 case '<': 341 if (octave > 0) 342 octave--; 343 octprefix = TRUE; 344 break; 345 case 'N': 346 GETNUM(cp, pitch); 347 for (sustain = 0; cp[1] == '.'; cp++) { 348 slen--; 349 sustain++; 350 } 351 oldfill = fill; 352 if (cp[1] == '_') { 353 fill = LEGATO; 354 ++cp; 355 slen--; 356 } 357 playtone(pitch - 1, value, sustain); 358 fill = oldfill; 359 break; 360 case 'L': 361 GETNUM(cp, value); 362 if (value <= 0 || value > MIN_VALUE) 363 value = DFLT_VALUE; 364 break; 365 case 'P': 366 case '~': 367 /* this may be followed by an override time value */ 368 GETNUM(cp, timeval); 369 if (timeval <= 0 || timeval > MIN_VALUE) 370 timeval = value; 371 for (sustain = 0; cp[1] == '.'; cp++) { 372 slen--; 373 sustain++; 374 } 375 playtone(-1, timeval, sustain); 376 break; 377 case 'T': 378 GETNUM(cp, tempo); 379 if (tempo < MIN_TEMPO || tempo > MAX_TEMPO) 380 tempo = DFLT_TEMPO; 381 whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / tempo; 382 break; 383 case 'M': 384 if (cp[1] == 'N' || cp[1] == 'n') { 385 fill = NORMAL; 386 ++cp; 387 slen--; 388 } else if (cp[1] == 'L' || cp[1] == 'l') { 389 fill = LEGATO; 390 ++cp; 391 slen--; 392 } else if (cp[1] == 'S' || cp[1] == 's') { 393 fill = STACCATO; 394 ++cp; 395 slen--; 396 } 397 break; 398 } 399 } 400} 401 402/* 403 * ****************** UNIX DRIVER HOOKS BEGIN HERE ************************** 404 * This section implements driver hooks to run playstring() and the tone(), 405 * endtone(), and rest() functions defined above. 406 */ 407 408static int spkr_active = FALSE; /* exclusion flag */ 409static char *spkr_inbuf; /* incoming buf */ 410 411static int 412spkropen(dev, flags, fmt, td) 413 struct cdev *dev; 414 int flags; 415 int fmt; 416 struct thread *td; 417{ 418#ifdef DEBUG 419 (void) printf("spkropen: entering with dev = %s\n", devtoname(dev)); 420#endif /* DEBUG */ 421 422 if (spkr_active) 423 return(EBUSY); 424 else { 425#ifdef DEBUG 426 (void) printf("spkropen: about to perform play initialization\n"); 427#endif /* DEBUG */ 428 playinit(); 429 spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK); 430 spkr_active = TRUE; 431 return(0); 432 } 433} 434 435static int 436spkrwrite(dev, uio, ioflag) 437 struct cdev *dev; 438 struct uio *uio; 439 int ioflag; 440{ 441#ifdef DEBUG 442 printf("spkrwrite: entering with dev = %s, count = %zd\n", 443 devtoname(dev), uio->uio_resid); 444#endif /* DEBUG */ 445 446 if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */ 447 return(E2BIG); 448 else { 449 unsigned n; 450 char *cp; 451 int error; 452 453 n = uio->uio_resid; 454 cp = spkr_inbuf; 455 error = uiomove(cp, n, uio); 456 if (!error) { 457 cp[n] = '\0'; 458 playstring(cp, n); 459 } 460 return(error); 461 } 462} 463 464static int 465spkrclose(dev, flags, fmt, td) 466 struct cdev *dev; 467 int flags; 468 int fmt; 469 struct thread *td; 470{ 471#ifdef DEBUG 472 (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev)); 473#endif /* DEBUG */ 474 475 wakeup(&endtone); 476 wakeup(&endrest); 477 free(spkr_inbuf, M_SPKR); 478 spkr_active = FALSE; 479 return(0); 480} 481 482static int 483spkrioctl(dev, cmd, cmdarg, flags, td) 484 struct cdev *dev; 485 unsigned long cmd; 486 caddr_t cmdarg; 487 int flags; 488 struct thread *td; 489{ 490#ifdef DEBUG 491 (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n", 492 devtoname(dev), cmd); 493#endif /* DEBUG */ 494 495 if (cmd == SPKRTONE) { 496 tone_t *tp = (tone_t *)cmdarg; 497 498 if (tp->frequency == 0) 499 rest(tp->duration); 500 else 501 tone(tp->frequency, tp->duration); 502 return 0; 503 } else if (cmd == SPKRTUNE) { 504 tone_t *tp = (tone_t *)(*(caddr_t *)cmdarg); 505 tone_t ttp; 506 int error; 507 508 for (; ; tp++) { 509 error = copyin(tp, &ttp, sizeof(tone_t)); 510 if (error) 511 return(error); 512 513 if (ttp.duration == 0) 514 break; 515 516 if (ttp.frequency == 0) 517 rest(ttp.duration); 518 else 519 tone(ttp.frequency, ttp.duration); 520 } 521 return(0); 522 } 523 return(EINVAL); 524} 525 526static struct cdev *speaker_dev; 527 528/* 529 * Module handling 530 */ 531static int 532speaker_modevent(module_t mod, int type, void *data) 533{ 534 int error = 0; 535 536 switch(type) { 537 case MOD_LOAD: 538 speaker_dev = make_dev(&spkr_cdevsw, 0, 539 UID_ROOT, GID_WHEEL, 0600, "speaker"); 540 break; 541 case MOD_SHUTDOWN: 542 case MOD_UNLOAD: 543 destroy_dev(speaker_dev); 544 break; 545 default: 546 error = EOPNOTSUPP; 547 } 548 return (error); 549} 550 551DEV_MODULE(speaker, speaker_modevent, NULL); 552