Home | History | Annotate | Line # | Download | only in obio
asc.c revision 1.14
      1 /*	$NetBSD: asc.c,v 1.14 1996/11/09 17:26:26 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 
     48 #include <machine/autoconf.h>
     49 #include <machine/cpu.h>
     50 
     51 #include "ascvar.h"
     52 
     53 /* Global ASC location */
     54 volatile unsigned char *ASCBase = (unsigned char *) 0x14000;
     55 
     56 
     57 /* bell support data */
     58 static int asc_configured = 0;
     59 static int bell_freq = 1880;
     60 static int bell_length = 10;
     61 static int bell_volume = 100;
     62 static int bell_ringing = 0;
     63 
     64 static int  ascmatch __P((struct device *, void *, void *));
     65 static void ascattach __P((struct device *, struct device *, void *));
     66 
     67 struct cfattach asc_ca = {
     68 	sizeof(struct device), ascmatch, ascattach
     69 };
     70 
     71 struct cfdriver asc_cd = {
     72 	NULL, "asc", DV_DULL, NULL, 0
     73 };
     74 
     75 static int
     76 ascmatch(pdp, match, auxp)
     77 	struct device	*pdp;
     78 	void	*match, *auxp;
     79 {
     80 	if (badbaddr((unsigned char *) ASCBase))
     81 		return 0;
     82 	return 1;
     83 }
     84 
     85 static void
     86 ascattach(parent, dev, aux)
     87 	struct device *parent, *dev;
     88 	void   *aux;
     89 {
     90 	printf(" Apple sound chip.\n");
     91 	asc_configured = 1;
     92 }
     93 
     94 int
     95 asc_setbellparams(freq, length, volume)
     96     int freq;
     97     int length;
     98     int volume;
     99 {
    100 	if (!asc_configured) return (ENODEV);
    101 
    102 	/* I only perform these checks for sanity. */
    103 	/* I suppose someone might want a bell that rings */
    104 	/* all day, but then the can make kernel mods themselves. */
    105 
    106 	if (freq < 10 || freq > 40000)
    107 		return (EINVAL);
    108 	if (length < 0 || length > 3600)
    109 		return (EINVAL);
    110 	if (volume < 0 || volume > 100)
    111 		return (EINVAL);
    112 
    113 	bell_freq = freq;
    114 	bell_length = length;
    115 	bell_volume = volume;
    116 
    117 	return (0);
    118 }
    119 
    120 
    121 int
    122 asc_getbellparams(freq, length, volume)
    123     int *freq;
    124     int *length;
    125     int *volume;
    126 {
    127 	if (!asc_configured) return (ENODEV);
    128 
    129 	*freq = bell_freq;
    130 	*length = bell_length;
    131 	*volume = bell_volume;
    132 
    133 	return (0);
    134 }
    135 
    136 
    137 void
    138 asc_bellstop(param)
    139     int param;
    140 {
    141 	if (!asc_configured) return;
    142 
    143 	if (bell_ringing > 1000 || bell_ringing < 0)
    144 		panic("bell got out of synch?????");
    145 	if (--bell_ringing == 0) {
    146 		ASCBase[0x801] = 0;
    147 	}
    148 	/* disable ASC */
    149 }
    150 
    151 
    152 int
    153 asc_ringbell()
    154 {
    155 	int     i;
    156 	unsigned long freq;
    157 
    158 	if (!asc_configured) return (ENODEV);
    159 
    160 	if (bell_ringing == 0) {
    161 		for (i = 0; i < 0x800; i++)
    162 			ASCBase[i] = 0;
    163 
    164 		for (i = 0; i < 256; i++) {
    165 			ASCBase[i] = i / 4;
    166 			ASCBase[i + 512] = i / 4;
    167 			ASCBase[i + 1024] = i / 4;
    168 			ASCBase[i + 1536] = i / 4;
    169 		}		/* up part of wave, four voices ? */
    170 		for (i = 0; i < 256; i++) {
    171 			ASCBase[i + 256] = 0x3f - (i / 4);
    172 			ASCBase[i + 768] = 0x3f - (i / 4);
    173 			ASCBase[i + 1280] = 0x3f - (i / 4);
    174 			ASCBase[i + 1792] = 0x3f - (i / 4);
    175 		}		/* down part of wave, four voices ? */
    176 
    177 		/* Fix this.  Need to find exact ASC sampling freq */
    178 		freq = 65536 * bell_freq / 466;
    179 
    180 		/* printf("beep: from %d, %02x %02x %02x %02x\n",
    181 		 * cur_beep.freq, (freq >> 24) & 0xff, (freq >> 16) & 0xff,
    182 		 * (freq >> 8) & 0xff, (freq) & 0xff); */
    183 		for (i = 0; i < 8; i++) {
    184 			ASCBase[0x814 + 8 * i] = (freq >> 24) & 0xff;
    185 			ASCBase[0x815 + 8 * i] = (freq >> 16) & 0xff;
    186 			ASCBase[0x816 + 8 * i] = (freq >> 8) & 0xff;
    187 			ASCBase[0x817 + 8 * i] = (freq) & 0xff;
    188 		}		/* frequency; should put cur_beep.freq in here
    189 				 * somewhere. */
    190 
    191 		ASCBase[0x807] = 3;	/* 44 ? */
    192 		ASCBase[0x806] = 255 * bell_volume / 100;
    193 		ASCBase[0x805] = 0;
    194 		ASCBase[0x80f] = 0;
    195 		ASCBase[0x802] = 2;	/* sampled */
    196 		ASCBase[0x801] = 2;	/* enable sampled */
    197 	}
    198 	bell_ringing++;
    199 	timeout((void *) asc_bellstop, 0, bell_length);
    200 
    201 	return 0;
    202 }
    203