added my responsibility for the `cpm' port
[unix-history] / sys / i386 / isa / spkr.c
index 7f767a7..d273f31 100644 (file)
@@ -4,7 +4,7 @@
  * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993
  * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su>
  *
  * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993
  * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su>
  *
- *    $Id: spkr.c,v 1.5 1993/11/15 01:33:11 ache Exp $
+ *    $Id: spkr.c,v 1.7 1994/01/25 23:04:27 ache Exp $
  */
 
 #include "speaker.h"
  */
 
 #include "speaker.h"
@@ -17,7 +17,8 @@
 #include "errno.h"
 #include "buf.h"
 #include "uio.h"
 #include "errno.h"
 #include "buf.h"
 #include "uio.h"
-
+#include "i386/isa/isa.h"
+#include "i386/isa/timerreg.h"
 #include "machine/speaker.h"
 
 /**************** MACHINE DEPENDENT PART STARTS HERE *************************
 #include "machine/speaker.h"
 
 /**************** MACHINE DEPENDENT PART STARTS HERE *************************
  *  Channel 2  LSB first,  (Square Wave) Encoding 
  *             MSB second
  */
  *  Channel 2  LSB first,  (Square Wave) Encoding 
  *             MSB second
  */
-#define PPI            0x61    /* port of Programmable Peripheral Interface */
 #define PPI_SPKR       0x03    /* turn these PPI bits on to pass sound */
 #define PPI_SPKR       0x03    /* turn these PPI bits on to pass sound */
-#define PIT_CTRL       0x43    /* PIT control address */
-#define PIT_COUNT      0x42    /* PIT count address */
 #define PIT_MODE       0xB6    /* set timer mode for sound generation */
 
 /*
 #define PIT_MODE       0xB6    /* set timer mode for sound generation */
 
 /*
 #define SPKRPRI PSOCK
 static char endtone, endrest;
 
 #define SPKRPRI PSOCK
 static char endtone, endrest;
 
-static int tone(thz, ticks)
+static void tone(thz, ticks)
 /* emit tone of frequency thz for given number of ticks */
 unsigned int thz, ticks;
 {
     unsigned int divisor = TIMER_CLK / thz;
 /* emit tone of frequency thz for given number of ticks */
 unsigned int thz, ticks;
 {
     unsigned int divisor = TIMER_CLK / thz;
-    int sps, error;
+    int sps;
 
 #ifdef DEBUG
     (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
 
 #ifdef DEBUG
     (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
@@ -75,34 +73,32 @@ unsigned int thz, ticks;
 
     /* set timer to generate clicks at given frequency in Hertz */
     sps = spltty();
 
     /* set timer to generate clicks at given frequency in Hertz */
     sps = spltty();
-    outb(PIT_CTRL, PIT_MODE);          /* prepare timer */
-    outb(PIT_COUNT, (divisor & 0xff)); /* send lo byte */
-    outb(PIT_COUNT, (divisor >> 8));   /* send hi byte */
+
+    if (acquire_timer2(PIT_MODE)) {
+       /* enter list of waiting procs ??? */
+       return;
+    }
+    outb(TIMER_CNTR2, (divisor & 0xff));       /* send lo byte */
+    outb(TIMER_CNTR2, (divisor >> 8)); /* send hi byte */
     splx(sps);
 
     /* turn the speaker on */
     splx(sps);
 
     /* turn the speaker on */
-    outb(PPI, inb(PPI) | PPI_SPKR);
+    outb(IO_PPI, inb(IO_PPI) | PPI_SPKR);
 
     /*
      * Set timeout to endtone function, then give up the timeslice.
      * This is so other processes can execute while the tone is being
      * emitted.
      */
 
     /*
      * Set timeout to endtone function, then give up the timeslice.
      * This is so other processes can execute while the tone is being
      * emitted.
      */
-    while ((error = tsleep((caddr_t)&endtone,
-               SPKRPRI | PCATCH, "spkrtn", ticks)) == ERESTART)
-               ;
-    outb(PPI, inb(PPI) & ~PPI_SPKR);
-
-    if (error == EWOULDBLOCK)
-       error = 0;
-    return error;
+    (void) tsleep((caddr_t)&endtone, SPKRPRI | PCATCH, "spkrtn", ticks);
+    outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR);
+    release_timer2();
 }
 
 }
 
-static int rest(ticks)
+static void rest(ticks)
 /* rest for given number of ticks */
 int    ticks;
 {
 /* rest for given number of ticks */
 int    ticks;
 {
-    int error;
     /*
      * Set timeout to endrest function, then give up the timeslice.
      * This is so other processes can execute while the rest is being
     /*
      * Set timeout to endrest function, then give up the timeslice.
      * This is so other processes can execute while the rest is being
@@ -111,12 +107,7 @@ int        ticks;
 #ifdef DEBUG
     (void) printf("rest: %d\n", ticks);
 #endif /* DEBUG */
 #ifdef DEBUG
     (void) printf("rest: %d\n", ticks);
 #endif /* DEBUG */
-    while ((error = tsleep((caddr_t)&endrest,
-               SPKRPRI | PCATCH, "spkrrs", ticks)) == ERESTART)
-               ;
-    if (error == EWOULDBLOCK)
-       error = 0;
-    return error;
+    (void) tsleep((caddr_t)&endrest, SPKRPRI | PCATCH, "spkrrs", ticks);
 }
 
 /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
 }
 
 /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
@@ -192,12 +183,11 @@ static void playinit()
     octprefix = TRUE;  /* act as though there was an initial O(n) */
 }
 
     octprefix = TRUE;  /* act as though there was an initial O(n) */
 }
 
-static int playtone(pitch, value, sustain)
+static void playtone(pitch, value, sustain)
 /* play tone of proper duration for current rhythm signature */
 int    pitch, value, sustain;
 {
     register int       sound, silence, snum = 1, sdenom = 1;
 /* play tone of proper duration for current rhythm signature */
 int    pitch, value, sustain;
 {
     register int       sound, silence, snum = 1, sdenom = 1;
-    int error;
 
     /* this weirdness avoids floating-point arithmetic */
     for (; sustain; sustain--)
 
     /* this weirdness avoids floating-point arithmetic */
     for (; sustain; sustain--)
@@ -208,7 +198,7 @@ int pitch, value, sustain;
     }
 
     if (pitch == -1)
     }
 
     if (pitch == -1)
-       error = rest(whole * snum / (value * sdenom));
+       rest(whole * snum / (value * sdenom));
     else
     {
        sound = (whole * snum) / (value * sdenom)
     else
     {
        sound = (whole * snum) / (value * sdenom)
@@ -220,12 +210,10 @@ int       pitch, value, sustain;
                        pitch, sound, silence);
 #endif /* DEBUG */
 
                        pitch, sound, silence);
 #endif /* DEBUG */
 
-       error = tone(pitchtab[pitch], sound);
-       if (error) return error;
+       tone(pitchtab[pitch], sound);
        if (fill != LEGATO)
        if (fill != LEGATO)
-           error = rest(silence);
+           rest(silence);
     }
     }
-    return error;
 }
 
 static int abs(n)
 }
 
 static int abs(n)
@@ -237,13 +225,12 @@ int n;
        return(n);
 }
 
        return(n);
 }
 
-static int playstring(cp, slen)
+static void playstring(cp, slen)
 /* interpret and play an item from a notation string */
 char   *cp;
 size_t slen;
 {
     int                pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
 /* interpret and play an item from a notation string */
 char   *cp;
 size_t slen;
 {
     int                pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
-    int error;
 
 #define GETNUM(cp, v)  for(v=0; isdigit(cp[1]) && slen > 0; ) \
                                {v = v * 10 + (*++cp - '0'); slen--;}
 
 #define GETNUM(cp, v)  for(v=0; isdigit(cp[1]) && slen > 0; ) \
                                {v = v * 10 + (*++cp - '0'); slen--;}
@@ -321,10 +308,9 @@ size_t     slen;
            }
 
            /* time to emit the actual tone */
            }
 
            /* time to emit the actual tone */
-           error = playtone(pitch, timeval, sustain);
+           playtone(pitch, timeval, sustain);
 
            fill = oldfill;
 
            fill = oldfill;
-           if (error) return error;
            break;
 
        case 'O':
            break;
 
        case 'O':
@@ -375,9 +361,8 @@ size_t      slen;
                ++cp;
                slen--;
            }
                ++cp;
                slen--;
            }
-           error = playtone(pitch - 1, value, sustain);
+           playtone(pitch - 1, value, sustain);
            fill = oldfill;
            fill = oldfill;
-           if (error) return error;
            break;
 
        case 'L':
            break;
 
        case 'L':
@@ -397,8 +382,7 @@ size_t      slen;
                slen--;
                sustain++;
            }
                slen--;
                sustain++;
            }
-           error = playtone(-1, timeval, sustain);
-           if (error) return error;
+           playtone(-1, timeval, sustain);
            break;
 
        case 'T':
            break;
 
        case 'T':
@@ -430,7 +414,6 @@ size_t      slen;
            break;
        }
     }
            break;
        }
     }
-    return 0;
 }
 
 /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
 }
 
 /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
@@ -487,7 +470,7 @@ struct uio  *uio;
        n = uio->uio_resid;
        cp = spkr_inbuf->b_un.b_addr;
        if (!(error = uiomove(cp, n, uio)))
        n = uio->uio_resid;
        cp = spkr_inbuf->b_un.b_addr;
        if (!(error = uiomove(cp, n, uio)))
-               error = playstring(cp, n);
+               playstring(cp, n);
        return(error);
     }
 }
        return(error);
     }
 }
@@ -527,9 +510,10 @@ caddr_t    cmdarg;
        tone_t  *tp = (tone_t *)cmdarg;
 
        if (tp->frequency == 0)
        tone_t  *tp = (tone_t *)cmdarg;
 
        if (tp->frequency == 0)
-           return rest(tp->duration);
+           rest(tp->duration);
        else
        else
-           return tone(tp->frequency, tp->duration);
+           tone(tp->frequency, tp->duration);
+       return 0;
     }
     else if (cmd == SPKRTUNE)
     {
     }
     else if (cmd == SPKRTUNE)
     {
@@ -544,11 +528,9 @@ caddr_t    cmdarg;
            if (ttp.duration == 0)
                    break;
            if (ttp.frequency == 0)
            if (ttp.duration == 0)
                    break;
            if (ttp.frequency == 0)
-               error = rest(ttp.duration);
+                rest(ttp.duration);
            else
            else
-               error = tone(ttp.frequency, ttp.duration);
-           if (error)
-                   return(error);
+                tone(ttp.frequency, ttp.duration);
        }
        return(0);
     }
        }
        return(0);
     }