Home | History | Annotate | Line # | Download | only in obio
asc.c revision 1.8
      1 /*	$NetBSD: asc.c,v 1.8 1995/09/21 03:36:25 briggs Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1993	Allen K. Briggs, Chris P. Caputo,
      5  *			Michael L. Finch, Bradley A. Grantham, and
      6  *			Lawrence A. Kesteloot
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed by the Alice Group.
     20  * 4. The names of the Alice Group or any of its members may not be used
     21  *    to endorse or promote products derived from this software without
     22  *    specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     33  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * ASC driver code and asc_ringbell() support
     38  */
     39 
     40 #include <sys/types.h>
     41 #include <sys/cdefs.h>
     42 #include <sys/errno.h>
     43 #include <sys/time.h>
     44 #include <sys/systm.h>
     45 #include <sys/param.h>
     46 #include <sys/device.h>
     47 #include <machine/cpu.h>
     48 
     49 
     50 /* Global ASC location */
     51 volatile unsigned char *ASCBase = (unsigned char *) 0x14000;
     52 
     53 
     54 /* bell support data */
     55 static int bell_freq = 1880;
     56 static int bell_length = 10;
     57 static int bell_volume = 100;
     58 static int bell_ringing = 0;
     59 
     60 static int ascprobe __P((struct device *, struct cfdata *, void *));
     61 static void ascattach __P((struct device *, struct device *, void *));
     62 extern int matchbyname __P((struct device *, void *, void *));
     63 
     64 struct cfdriver asccd =
     65 {NULL, "asc", matchbyname, ascattach,
     66 DV_DULL, sizeof(struct device), NULL, 0};
     67 
     68 static int
     69 ascprobe(parent, cf, aux)
     70 	struct device *parent;
     71 	struct cfdata *cf;
     72 	void   *aux;
     73 {
     74 	if (strcmp(*((char **) aux), asccd.cd_name))
     75 		return 0;
     76 
     77 	return 1;
     78 }
     79 
     80 
     81 static void
     82 ascattach(parent, dev, aux)
     83 	struct device *parent, *dev;
     84 	void   *aux;
     85 {
     86 	printf(" Apple sound chip.\n");
     87 
     88 	ASCBase = IOBase + ASCBase;
     89 }
     90 
     91 int
     92 asc_setbellparams(
     93     int freq,
     94     int length,
     95     int volume)
     96 {
     97 	/* I only perform these checks for sanity. */
     98 	/* I suppose someone might want a bell that rings */
     99 	/* all day, but then the can make kernel mods themselves. */
    100 
    101 	if (freq < 10 || freq > 40000)
    102 		return (EINVAL);
    103 	if (length < 0 || length > 3600)
    104 		return (EINVAL);
    105 	if (volume < 0 || volume > 100)
    106 		return (EINVAL);
    107 
    108 	bell_freq = freq;
    109 	bell_length = length;
    110 	bell_volume = volume;
    111 
    112 	return (0);
    113 }
    114 
    115 
    116 int
    117 asc_getbellparams(
    118     int *freq,
    119     int *length,
    120     int *volume)
    121 {
    122 	*freq = bell_freq;
    123 	*length = bell_length;
    124 	*volume = bell_volume;
    125 
    126 	return (0);
    127 }
    128 
    129 
    130 void
    131 asc_bellstop(
    132     int param)
    133 {
    134 	if (bell_ringing > 1000 || bell_ringing < 0)
    135 		panic("bell got out of synch?????");
    136 	if (--bell_ringing == 0) {
    137 		ASCBase[0x801] = 0;
    138 	}
    139 	/* disable ASC */
    140 }
    141 
    142 
    143 int
    144 asc_ringbell()
    145 {
    146 	int     i;
    147 	unsigned long freq;
    148 
    149 	if (bell_ringing == 0) {
    150 		for (i = 0; i < 0x800; i++)
    151 			ASCBase[i] = 0;
    152 
    153 		for (i = 0; i < 256; i++) {
    154 			ASCBase[i] = i / 4;
    155 			ASCBase[i + 512] = i / 4;
    156 			ASCBase[i + 1024] = i / 4;
    157 			ASCBase[i + 1536] = i / 4;
    158 		}		/* up part of wave, four voices ? */
    159 		for (i = 0; i < 256; i++) {
    160 			ASCBase[i + 256] = 0x3f - (i / 4);
    161 			ASCBase[i + 768] = 0x3f - (i / 4);
    162 			ASCBase[i + 1280] = 0x3f - (i / 4);
    163 			ASCBase[i + 1792] = 0x3f - (i / 4);
    164 		}		/* down part of wave, four voices ? */
    165 
    166 		/* Fix this.  Need to find exact ASC sampling freq */
    167 		freq = 65536 * bell_freq / 466;
    168 
    169 		/* printf("beep: from %d, %02x %02x %02x %02x\n",
    170 		 * cur_beep.freq, (freq >> 24) & 0xff, (freq >> 16) & 0xff,
    171 		 * (freq >> 8) & 0xff, (freq) & 0xff); */
    172 		for (i = 0; i < 8; i++) {
    173 			ASCBase[0x814 + 8 * i] = (freq >> 24) & 0xff;
    174 			ASCBase[0x815 + 8 * i] = (freq >> 16) & 0xff;
    175 			ASCBase[0x816 + 8 * i] = (freq >> 8) & 0xff;
    176 			ASCBase[0x817 + 8 * i] = (freq) & 0xff;
    177 		}		/* frequency; should put cur_beep.freq in here
    178 				 * somewhere. */
    179 
    180 		ASCBase[0x807] = 3;	/* 44 ? */
    181 		ASCBase[0x806] = 255 * bell_volume / 100;
    182 		ASCBase[0x805] = 0;
    183 		ASCBase[0x80f] = 0;
    184 		ASCBase[0x802] = 2;	/* sampled */
    185 		ASCBase[0x801] = 2;	/* enable sampled */
    186 	}
    187 	bell_ringing++;
    188 	timeout((void *) asc_bellstop, 0, bell_length);
    189 }
    190