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