Import 2.3.1pre2
[davej-history.git] / drivers / sound / dmasound.c
blobd95f462f6bb7145d194956aca0754861d01ba872
2 /* linux/drivers/sound/dmasound.c */
4 /*
6 OSS/Free compatible Atari TT/Falcon and Amiga DMA sound driver for Linux/m68k
7 Extended to support Power Macintosh for Linux/ppc by Paul Mackerras
9 (c) 1995 by Michael Schlueter & Michael Marte
11 Michael Schlueter (michael@duck.syd.de) did the basic structure of the VFS
12 interface and the u-law to signed byte conversion.
14 Michael Marte (marte@informatik.uni-muenchen.de) did the sound queue,
15 /dev/mixer, /dev/sndstat and complemented the VFS interface. He would like
16 to thank:
17 Michael Schlueter for initial ideas and documentation on the MFP and
18 the DMA sound hardware.
19 Therapy? for their CD 'Troublegum' which really made me rock.
21 /dev/sndstat is based on code by Hannu Savolainen, the author of the
22 VoxWare family of drivers.
24 This file is subject to the terms and conditions of the GNU General Public
25 License. See the file COPYING in the main directory of this archive
26 for more details.
28 History:
29 1995/8/25 first release
31 1995/9/02 ++roman: fixed atari_stram_alloc() call, the timer programming
32 and several race conditions
34 1995/9/14 ++roman: After some discussion with Michael Schlueter, revised
35 the interrupt disabling
36 Slightly speeded up U8->S8 translation by using long
37 operations where possible
38 Added 4:3 interpolation for /dev/audio
40 1995/9/20 ++TeSche: Fixed a bug in sq_write and changed /dev/audio
41 converting to play at 12517Hz instead of 6258Hz.
43 1995/9/23 ++TeSche: Changed sq_interrupt() and sq_play() to pre-program
44 the DMA for another frame while there's still one
45 running. This allows the IRQ response to be
46 arbitrarily delayed and playing will still continue.
48 1995/10/14 ++Guenther_Kelleter@ac3.maus.de, ++TeSche: better support for
49 Falcon audio (the Falcon doesn't raise an IRQ at the
50 end of a frame, but at the beginning instead!). uses
51 'if (codec_dma)' in lots of places to simply switch
52 between Falcon and TT code.
54 1995/11/06 ++TeSche: started introducing a hardware abstraction scheme
55 (may perhaps also serve for Amigas?), can now play
56 samples at almost all frequencies by means of a more
57 generalized expand routine, takes a good deal of care
58 to cut data only at sample sizes, buffer size is now
59 a kernel runtime option, implemented fsync() & several
60 minor improvements
61 ++Guenther: useful hints and bug fixes, cross-checked it for
62 Falcons
64 1996/3/9 ++geert: support added for Amiga, A-law, 16-bit little endian.
65 Unification to drivers/sound/dmasound.c.
67 1996/4/6 ++Martin Mitchell: updated to 1.3 kernel.
69 1996/6/13 ++topi: fixed things that were broken (mainly the amiga
70 14-bit routines), /dev/sndstat shows now the real
71 hardware frequency, the lowpass filter is disabled
72 by default now.
74 1996/9/25 ++geert: modularization
76 1998-06-10 ++andreas: converted to use sound_core
81 #include <linux/module.h>
82 #include <linux/sched.h>
83 #include <linux/timer.h>
84 #include <linux/major.h>
85 #include <linux/config.h>
86 #include <linux/fcntl.h>
87 #include <linux/errno.h>
88 #include <linux/mm.h>
89 #include <linux/malloc.h>
90 #include <linux/sound.h>
91 #include <linux/init.h>
93 #if defined(__mc68000__) || defined(CONFIG_APUS)
94 #include <asm/setup.h>
95 #endif
96 #include <asm/system.h>
97 #include <asm/irq.h>
98 #include <asm/pgtable.h>
99 #include <asm/uaccess.h>
101 #ifdef CONFIG_ATARI
102 #include <asm/atarihw.h>
103 #include <asm/atariints.h>
104 #include <asm/atari_stram.h>
105 #endif /* CONFIG_ATARI */
106 #ifdef CONFIG_AMIGA
107 #include <asm/amigahw.h>
108 #include <asm/amigaints.h>
109 #endif /* CONFIG_AMIGA */
110 #ifdef CONFIG_PPC
111 #include <asm/prom.h>
112 #include <asm/io.h>
113 #include <asm/dbdma.h>
114 #include <asm/adb.h>
115 #include <asm/cuda.h>
116 #include <asm/pmu.h>
117 #include "awacs_defs.h"
118 #include <linux/nvram.h>
119 #include <linux/vt_kern.h>
120 #endif /* CONFIG_PPC */
122 #include "dmasound.h"
123 #include <linux/soundcard.h>
125 #define HAS_8BIT_TABLES
127 #ifdef MODULE
128 static int sq_unit = -1;
129 static int mixer_unit = -1;
130 static int state_unit = -1;
131 static int irq_installed = 0;
132 #endif /* MODULE */
133 static char **sound_buffers = NULL;
136 #ifdef CONFIG_ATARI
137 extern void atari_microwire_cmd(int cmd);
138 #endif /* CONFIG_ATARI */
140 #ifdef CONFIG_AMIGA
142 * The minimum period for audio depends on htotal (for OCS/ECS/AGA)
143 * (Imported from arch/m68k/amiga/amisound.c)
146 extern volatile u_short amiga_audio_min_period;
150 * amiga_mksound() should be able to restore the period after beeping
151 * (Imported from arch/m68k/amiga/amisound.c)
154 extern u_short amiga_audio_period;
158 * Audio DMA masks
161 #define AMI_AUDIO_OFF (DMAF_AUD0 | DMAF_AUD1 | DMAF_AUD2 | DMAF_AUD3)
162 #define AMI_AUDIO_8 (DMAF_SETCLR | DMAF_MASTER | DMAF_AUD0 | DMAF_AUD1)
163 #define AMI_AUDIO_14 (AMI_AUDIO_8 | DMAF_AUD2 | DMAF_AUD3)
165 #endif /* CONFIG_AMIGA */
167 #ifdef CONFIG_PPC
169 * Interrupt numbers and addresses, obtained from the device tree.
171 static int awacs_irq, awacs_tx_irq, awacs_rx_irq;
172 static volatile struct awacs_regs *awacs;
173 static volatile struct dbdma_regs *awacs_txdma, *awacs_rxdma;
174 static int awacs_rate_index;
175 static int awacs_subframe;
176 static int awacs_spkr_vol;
178 static int awacs_revision;
179 #define AWACS_BURGUNDY 100 /* fake revision # for burgundy */
182 * Space for the DBDMA command blocks.
184 static void *awacs_tx_cmd_space;
185 static volatile struct dbdma_cmd *awacs_tx_cmds;
188 * Cached values of AWACS registers (we can't read them).
189 * Except on the burgundy. XXX
191 int awacs_reg[5];
193 #define HAS_16BIT_TABLES
194 #undef HAS_8BIT_TABLES
197 * Stuff for outputting a beep. The values range from -327 to +327
198 * so we can multiply by an amplitude in the range 0..100 to get a
199 * signed short value to put in the output buffer.
201 static short beep_wform[256] = {
202 0, 40, 79, 117, 153, 187, 218, 245,
203 269, 288, 304, 316, 323, 327, 327, 324,
204 318, 310, 299, 288, 275, 262, 249, 236,
205 224, 213, 204, 196, 190, 186, 183, 182,
206 182, 183, 186, 189, 192, 196, 200, 203,
207 206, 208, 209, 209, 209, 207, 204, 201,
208 197, 193, 188, 183, 179, 174, 170, 166,
209 163, 161, 160, 159, 159, 160, 161, 162,
210 164, 166, 168, 169, 171, 171, 171, 170,
211 169, 167, 163, 159, 155, 150, 144, 139,
212 133, 128, 122, 117, 113, 110, 107, 105,
213 103, 103, 103, 103, 104, 104, 105, 105,
214 105, 103, 101, 97, 92, 86, 78, 68,
215 58, 45, 32, 18, 3, -11, -26, -41,
216 -55, -68, -79, -88, -95, -100, -102, -102,
217 -99, -93, -85, -75, -62, -48, -33, -16,
218 0, 16, 33, 48, 62, 75, 85, 93,
219 99, 102, 102, 100, 95, 88, 79, 68,
220 55, 41, 26, 11, -3, -18, -32, -45,
221 -58, -68, -78, -86, -92, -97, -101, -103,
222 -105, -105, -105, -104, -104, -103, -103, -103,
223 -103, -105, -107, -110, -113, -117, -122, -128,
224 -133, -139, -144, -150, -155, -159, -163, -167,
225 -169, -170, -171, -171, -171, -169, -168, -166,
226 -164, -162, -161, -160, -159, -159, -160, -161,
227 -163, -166, -170, -174, -179, -183, -188, -193,
228 -197, -201, -204, -207, -209, -209, -209, -208,
229 -206, -203, -200, -196, -192, -189, -186, -183,
230 -182, -182, -183, -186, -190, -196, -204, -213,
231 -224, -236, -249, -262, -275, -288, -299, -310,
232 -318, -324, -327, -327, -323, -316, -304, -288,
233 -269, -245, -218, -187, -153, -117, -79, -40,
236 #define BEEP_SPEED 2 /* 22050 Hz sample rate */
237 #define BEEP_BUFLEN 512
238 #define BEEP_VOLUME 15 /* 0 - 100 */
240 static int beep_volume = BEEP_VOLUME;
241 static int beep_playing = 0;
242 static short *beep_buf;
243 static volatile struct dbdma_cmd *beep_dbdma_cmd;
244 static void (*orig_mksound)(unsigned int, unsigned int);
246 /* Burgundy functions */
247 static void awacs_burgundy_wcw(unsigned addr,unsigned newval);
248 static unsigned awacs_burgundy_rcw(unsigned addr);
249 static void awacs_burgundy_write_volume(unsigned address, int volume);
250 static int awacs_burgundy_read_volume(unsigned address);
251 static void awacs_burgundy_write_mvolume(unsigned address, int volume);
252 static int awacs_burgundy_read_mvolume(unsigned address);
254 #ifdef CONFIG_PMAC_PBOOK
256 * Stuff for restoring after a sleep.
258 static int awacs_sleep_notify(struct notifier_block *, unsigned long, void *);
259 struct notifier_block awacs_sleep_notifier = {
260 awacs_sleep_notify
262 #endif /* CONFIG_PMAC_PBOOK */
264 #endif /* CONFIG_PPC */
266 /*** Some declarations *******************************************************/
269 #define DMASND_TT 1
270 #define DMASND_FALCON 2
271 #define DMASND_AMIGA 3
272 #define DMASND_AWACS 4
274 #define MAX_CATCH_RADIUS 10
275 #define MIN_BUFFERS 4
276 #define MIN_BUFSIZE 4
277 #define MAX_BUFSIZE 128 /* Limit for Amiga */
279 static int catchRadius = 0, numBufs = 4, bufSize = 32;
280 MODULE_PARM(catchRadius, "i");
281 MODULE_PARM(numBufs, "i");
282 MODULE_PARM(bufSize, "i");
284 #define arraysize(x) (sizeof(x)/sizeof(*(x)))
285 #define min(x, y) ((x) < (y) ? (x) : (y))
286 #define le2be16(x) (((x)<<8 & 0xff00) | ((x)>>8 & 0x00ff))
287 #define le2be16dbl(x) (((x)<<8 & 0xff00ff00) | ((x)>>8 & 0x00ff00ff))
289 #define IOCTL_IN(arg, ret) \
290 do { int error = get_user(ret, (int *)(arg)); \
291 if (error) return error; \
292 } while (0)
293 #define IOCTL_OUT(arg, ret) ioctl_return((int *)(arg), ret)
296 /*** Some low level helpers **************************************************/
298 #ifdef HAS_8BIT_TABLES
299 /* 8 bit mu-law */
301 static char ulaw2dma8[] = {
302 -126, -122, -118, -114, -110, -106, -102, -98,
303 -94, -90, -86, -82, -78, -74, -70, -66,
304 -63, -61, -59, -57, -55, -53, -51, -49,
305 -47, -45, -43, -41, -39, -37, -35, -33,
306 -31, -30, -29, -28, -27, -26, -25, -24,
307 -23, -22, -21, -20, -19, -18, -17, -16,
308 -16, -15, -15, -14, -14, -13, -13, -12,
309 -12, -11, -11, -10, -10, -9, -9, -8,
310 -8, -8, -7, -7, -7, -7, -6, -6,
311 -6, -6, -5, -5, -5, -5, -4, -4,
312 -4, -4, -4, -4, -3, -3, -3, -3,
313 -3, -3, -3, -3, -2, -2, -2, -2,
314 -2, -2, -2, -2, -2, -2, -2, -2,
315 -1, -1, -1, -1, -1, -1, -1, -1,
316 -1, -1, -1, -1, -1, -1, -1, -1,
317 -1, -1, -1, -1, -1, -1, -1, 0,
318 125, 121, 117, 113, 109, 105, 101, 97,
319 93, 89, 85, 81, 77, 73, 69, 65,
320 62, 60, 58, 56, 54, 52, 50, 48,
321 46, 44, 42, 40, 38, 36, 34, 32,
322 30, 29, 28, 27, 26, 25, 24, 23,
323 22, 21, 20, 19, 18, 17, 16, 15,
324 15, 14, 14, 13, 13, 12, 12, 11,
325 11, 10, 10, 9, 9, 8, 8, 7,
326 7, 7, 6, 6, 6, 6, 5, 5,
327 5, 5, 4, 4, 4, 4, 3, 3,
328 3, 3, 3, 3, 2, 2, 2, 2,
329 2, 2, 2, 2, 1, 1, 1, 1,
330 1, 1, 1, 1, 1, 1, 1, 1,
331 0, 0, 0, 0, 0, 0, 0, 0,
332 0, 0, 0, 0, 0, 0, 0, 0,
333 0, 0, 0, 0, 0, 0, 0, 0
336 /* 8 bit A-law */
338 static char alaw2dma8[] = {
339 -22, -21, -24, -23, -18, -17, -20, -19,
340 -30, -29, -32, -31, -26, -25, -28, -27,
341 -11, -11, -12, -12, -9, -9, -10, -10,
342 -15, -15, -16, -16, -13, -13, -14, -14,
343 -86, -82, -94, -90, -70, -66, -78, -74,
344 -118, -114, -126, -122, -102, -98, -110, -106,
345 -43, -41, -47, -45, -35, -33, -39, -37,
346 -59, -57, -63, -61, -51, -49, -55, -53,
347 -2, -2, -2, -2, -2, -2, -2, -2,
348 -2, -2, -2, -2, -2, -2, -2, -2,
349 -1, -1, -1, -1, -1, -1, -1, -1,
350 -1, -1, -1, -1, -1, -1, -1, -1,
351 -6, -6, -6, -6, -5, -5, -5, -5,
352 -8, -8, -8, -8, -7, -7, -7, -7,
353 -3, -3, -3, -3, -3, -3, -3, -3,
354 -4, -4, -4, -4, -4, -4, -4, -4,
355 21, 20, 23, 22, 17, 16, 19, 18,
356 29, 28, 31, 30, 25, 24, 27, 26,
357 10, 10, 11, 11, 8, 8, 9, 9,
358 14, 14, 15, 15, 12, 12, 13, 13,
359 86, 82, 94, 90, 70, 66, 78, 74,
360 118, 114, 126, 122, 102, 98, 110, 106,
361 43, 41, 47, 45, 35, 33, 39, 37,
362 59, 57, 63, 61, 51, 49, 55, 53,
363 1, 1, 1, 1, 1, 1, 1, 1,
364 1, 1, 1, 1, 1, 1, 1, 1,
365 0, 0, 0, 0, 0, 0, 0, 0,
366 0, 0, 0, 0, 0, 0, 0, 0,
367 5, 5, 5, 5, 4, 4, 4, 4,
368 7, 7, 7, 7, 6, 6, 6, 6,
369 2, 2, 2, 2, 2, 2, 2, 2,
370 3, 3, 3, 3, 3, 3, 3, 3
372 #endif /* HAS_8BIT_TABLES */
374 #ifdef HAS_16BIT_TABLES
376 /* 16 bit mu-law */
378 static short ulaw2dma16[] = {
379 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
380 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
381 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
382 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
383 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
384 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
385 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
386 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
387 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
388 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
389 -876, -844, -812, -780, -748, -716, -684, -652,
390 -620, -588, -556, -524, -492, -460, -428, -396,
391 -372, -356, -340, -324, -308, -292, -276, -260,
392 -244, -228, -212, -196, -180, -164, -148, -132,
393 -120, -112, -104, -96, -88, -80, -72, -64,
394 -56, -48, -40, -32, -24, -16, -8, 0,
395 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
396 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
397 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
398 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
399 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
400 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
401 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
402 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
403 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
404 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
405 876, 844, 812, 780, 748, 716, 684, 652,
406 620, 588, 556, 524, 492, 460, 428, 396,
407 372, 356, 340, 324, 308, 292, 276, 260,
408 244, 228, 212, 196, 180, 164, 148, 132,
409 120, 112, 104, 96, 88, 80, 72, 64,
410 56, 48, 40, 32, 24, 16, 8, 0,
413 /* 16 bit A-law */
415 static short alaw2dma16[] = {
416 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
417 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
418 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
419 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
420 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
421 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
422 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
423 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
424 -344, -328, -376, -360, -280, -264, -312, -296,
425 -472, -456, -504, -488, -408, -392, -440, -424,
426 -88, -72, -120, -104, -24, -8, -56, -40,
427 -216, -200, -248, -232, -152, -136, -184, -168,
428 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
429 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
430 -688, -656, -752, -720, -560, -528, -624, -592,
431 -944, -912, -1008, -976, -816, -784, -880, -848,
432 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
433 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
434 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
435 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
436 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
437 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
438 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
439 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
440 344, 328, 376, 360, 280, 264, 312, 296,
441 472, 456, 504, 488, 408, 392, 440, 424,
442 88, 72, 120, 104, 24, 8, 56, 40,
443 216, 200, 248, 232, 152, 136, 184, 168,
444 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
445 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
446 688, 656, 752, 720, 560, 528, 624, 592,
447 944, 912, 1008, 976, 816, 784, 880, 848,
449 #endif /* HAS_16BIT_TABLES */
452 #ifdef HAS_14BIT_TABLES
454 /* 14 bit mu-law (LSB) */
456 static char alaw2dma14l[] = {
457 33, 33, 33, 33, 33, 33, 33, 33,
458 33, 33, 33, 33, 33, 33, 33, 33,
459 33, 33, 33, 33, 33, 33, 33, 33,
460 33, 33, 33, 33, 33, 33, 33, 33,
461 1, 1, 1, 1, 1, 1, 1, 1,
462 1, 1, 1, 1, 1, 1, 1, 1,
463 49, 17, 49, 17, 49, 17, 49, 17,
464 49, 17, 49, 17, 49, 17, 49, 17,
465 41, 57, 9, 25, 41, 57, 9, 25,
466 41, 57, 9, 25, 41, 57, 9, 25,
467 37, 45, 53, 61, 5, 13, 21, 29,
468 37, 45, 53, 61, 5, 13, 21, 29,
469 35, 39, 43, 47, 51, 55, 59, 63,
470 3, 7, 11, 15, 19, 23, 27, 31,
471 34, 36, 38, 40, 42, 44, 46, 48,
472 50, 52, 54, 56, 58, 60, 62, 0,
473 31, 31, 31, 31, 31, 31, 31, 31,
474 31, 31, 31, 31, 31, 31, 31, 31,
475 31, 31, 31, 31, 31, 31, 31, 31,
476 31, 31, 31, 31, 31, 31, 31, 31,
477 63, 63, 63, 63, 63, 63, 63, 63,
478 63, 63, 63, 63, 63, 63, 63, 63,
479 15, 47, 15, 47, 15, 47, 15, 47,
480 15, 47, 15, 47, 15, 47, 15, 47,
481 23, 7, 55, 39, 23, 7, 55, 39,
482 23, 7, 55, 39, 23, 7, 55, 39,
483 27, 19, 11, 3, 59, 51, 43, 35,
484 27, 19, 11, 3, 59, 51, 43, 35,
485 29, 25, 21, 17, 13, 9, 5, 1,
486 61, 57, 53, 49, 45, 41, 37, 33,
487 30, 28, 26, 24, 22, 20, 18, 16,
488 14, 12, 10, 8, 6, 4, 2, 0
491 /* 14 bit A-law (LSB) */
493 static char alaw2dma14l[] = {
494 32, 32, 32, 32, 32, 32, 32, 32,
495 32, 32, 32, 32, 32, 32, 32, 32,
496 16, 48, 16, 48, 16, 48, 16, 48,
497 16, 48, 16, 48, 16, 48, 16, 48,
498 0, 0, 0, 0, 0, 0, 0, 0,
499 0, 0, 0, 0, 0, 0, 0, 0,
500 0, 0, 0, 0, 0, 0, 0, 0,
501 0, 0, 0, 0, 0, 0, 0, 0,
502 42, 46, 34, 38, 58, 62, 50, 54,
503 10, 14, 2, 6, 26, 30, 18, 22,
504 42, 46, 34, 38, 58, 62, 50, 54,
505 10, 14, 2, 6, 26, 30, 18, 22,
506 40, 56, 8, 24, 40, 56, 8, 24,
507 40, 56, 8, 24, 40, 56, 8, 24,
508 20, 28, 4, 12, 52, 60, 36, 44,
509 20, 28, 4, 12, 52, 60, 36, 44,
510 32, 32, 32, 32, 32, 32, 32, 32,
511 32, 32, 32, 32, 32, 32, 32, 32,
512 48, 16, 48, 16, 48, 16, 48, 16,
513 48, 16, 48, 16, 48, 16, 48, 16,
514 0, 0, 0, 0, 0, 0, 0, 0,
515 0, 0, 0, 0, 0, 0, 0, 0,
516 0, 0, 0, 0, 0, 0, 0, 0,
517 0, 0, 0, 0, 0, 0, 0, 0,
518 22, 18, 30, 26, 6, 2, 14, 10,
519 54, 50, 62, 58, 38, 34, 46, 42,
520 22, 18, 30, 26, 6, 2, 14, 10,
521 54, 50, 62, 58, 38, 34, 46, 42,
522 24, 8, 56, 40, 24, 8, 56, 40,
523 24, 8, 56, 40, 24, 8, 56, 40,
524 44, 36, 60, 52, 12, 4, 28, 20,
525 44, 36, 60, 52, 12, 4, 28, 20
527 #endif /* HAS_14BIT_TABLES */
530 /*** Translations ************************************************************/
533 #ifdef CONFIG_ATARI
534 static ssize_t ata_ct_law(const u_char *userPtr, size_t userCount,
535 u_char frame[], ssize_t *frameUsed,
536 ssize_t frameLeft);
537 static ssize_t ata_ct_s8(const u_char *userPtr, size_t userCount,
538 u_char frame[], ssize_t *frameUsed,
539 ssize_t frameLeft);
540 static ssize_t ata_ct_u8(const u_char *userPtr, size_t userCount,
541 u_char frame[], ssize_t *frameUsed,
542 ssize_t frameLeft);
543 static ssize_t ata_ct_s16be(const u_char *userPtr, size_t userCount,
544 u_char frame[], ssize_t *frameUsed,
545 ssize_t frameLeft);
546 static ssize_t ata_ct_u16be(const u_char *userPtr, size_t userCount,
547 u_char frame[], ssize_t *frameUsed,
548 ssize_t frameLeft);
549 static ssize_t ata_ct_s16le(const u_char *userPtr, size_t userCount,
550 u_char frame[], ssize_t *frameUsed,
551 ssize_t frameLeft);
552 static ssize_t ata_ct_u16le(const u_char *userPtr, size_t userCount,
553 u_char frame[], ssize_t *frameUsed,
554 ssize_t frameLeft);
555 static ssize_t ata_ctx_law(const u_char *userPtr, size_t userCount,
556 u_char frame[], ssize_t *frameUsed,
557 ssize_t frameLeft);
558 static ssize_t ata_ctx_s8(const u_char *userPtr, size_t userCount,
559 u_char frame[], ssize_t *frameUsed,
560 ssize_t frameLeft);
561 static ssize_t ata_ctx_u8(const u_char *userPtr, size_t userCount,
562 u_char frame[], ssize_t *frameUsed,
563 ssize_t frameLeft);
564 static ssize_t ata_ctx_s16be(const u_char *userPtr, size_t userCount,
565 u_char frame[], ssize_t *frameUsed,
566 ssize_t frameLeft);
567 static ssize_t ata_ctx_u16be(const u_char *userPtr, size_t userCount,
568 u_char frame[], ssize_t *frameUsed,
569 ssize_t frameLeft);
570 static ssize_t ata_ctx_s16le(const u_char *userPtr, size_t userCount,
571 u_char frame[], ssize_t *frameUsed,
572 ssize_t frameLeft);
573 static ssize_t ata_ctx_u16le(const u_char *userPtr, size_t userCount,
574 u_char frame[], ssize_t *frameUsed,
575 ssize_t frameLeft);
576 #endif /* CONFIG_ATARI */
578 #ifdef CONFIG_AMIGA
579 static ssize_t ami_ct_law(const u_char *userPtr, size_t userCount,
580 u_char frame[], ssize_t *frameUsed,
581 ssize_t frameLeft);
582 static ssize_t ami_ct_s8(const u_char *userPtr, size_t userCount,
583 u_char frame[], ssize_t *frameUsed,
584 ssize_t frameLeft);
585 static ssize_t ami_ct_u8(const u_char *userPtr, size_t userCount,
586 u_char frame[], ssize_t *frameUsed,
587 ssize_t frameLeft);
588 static ssize_t ami_ct_s16be(const u_char *userPtr, size_t userCount,
589 u_char frame[], ssize_t *frameUsed,
590 ssize_t frameLeft);
591 static ssize_t ami_ct_u16be(const u_char *userPtr, size_t userCount,
592 u_char frame[], ssize_t *frameUsed,
593 ssize_t frameLeft);
594 static ssize_t ami_ct_s16le(const u_char *userPtr, size_t userCount,
595 u_char frame[], ssize_t *frameUsed,
596 ssize_t frameLeft);
597 static ssize_t ami_ct_u16le(const u_char *userPtr, size_t userCount,
598 u_char frame[], ssize_t *frameUsed,
599 ssize_t frameLeft);
600 #endif /* CONFIG_AMIGA */
602 #ifdef CONFIG_PPC
603 static ssize_t pmac_ct_law(const u_char *userPtr, size_t userCount,
604 u_char frame[], ssize_t *frameUsed,
605 ssize_t frameLeft);
606 static ssize_t pmac_ct_s8(const u_char *userPtr, size_t userCount,
607 u_char frame[], ssize_t *frameUsed,
608 ssize_t frameLeft);
609 static ssize_t pmac_ct_u8(const u_char *userPtr, size_t userCount,
610 u_char frame[], ssize_t *frameUsed,
611 ssize_t frameLeft);
612 static ssize_t pmac_ct_s16(const u_char *userPtr, size_t userCount,
613 u_char frame[], ssize_t *frameUsed,
614 ssize_t frameLeft);
615 static ssize_t pmac_ct_u16(const u_char *userPtr, size_t userCount,
616 u_char frame[], ssize_t *frameUsed,
617 ssize_t frameLeft);
618 static ssize_t pmac_ctx_law(const u_char *userPtr, size_t userCount,
619 u_char frame[], ssize_t *frameUsed,
620 ssize_t frameLeft);
621 static ssize_t pmac_ctx_s8(const u_char *userPtr, size_t userCount,
622 u_char frame[], ssize_t *frameUsed,
623 ssize_t frameLeft);
624 static ssize_t pmac_ctx_u8(const u_char *userPtr, size_t userCount,
625 u_char frame[], ssize_t *frameUsed,
626 ssize_t frameLeft);
627 static ssize_t pmac_ctx_s16(const u_char *userPtr, size_t userCount,
628 u_char frame[], ssize_t *frameUsed,
629 ssize_t frameLeft);
630 static ssize_t pmac_ctx_u16(const u_char *userPtr, size_t userCount,
631 u_char frame[], ssize_t *frameUsed,
632 ssize_t frameLeft);
633 #endif /* CONFIG_PPC */
635 /*** Machine definitions *****************************************************/
638 typedef struct {
639 int type;
640 void *(*dma_alloc)(unsigned int, int);
641 void (*dma_free)(void *, unsigned int);
642 int (*irqinit)(void);
643 #ifdef MODULE
644 void (*irqcleanup)(void);
645 #endif /* MODULE */
646 void (*init)(void);
647 void (*silence)(void);
648 int (*setFormat)(int);
649 int (*setVolume)(int);
650 int (*setBass)(int);
651 int (*setTreble)(int);
652 int (*setGain)(int);
653 void (*play)(void);
654 } MACHINE;
657 /*** Low level stuff *********************************************************/
660 typedef struct {
661 int format; /* AFMT_* */
662 int stereo; /* 0 = mono, 1 = stereo */
663 int size; /* 8/16 bit*/
664 int speed; /* speed */
665 } SETTINGS;
667 typedef struct {
668 ssize_t (*ct_ulaw)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
669 ssize_t (*ct_alaw)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
670 ssize_t (*ct_s8)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
671 ssize_t (*ct_u8)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
672 ssize_t (*ct_s16be)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
673 ssize_t (*ct_u16be)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
674 ssize_t (*ct_s16le)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
675 ssize_t (*ct_u16le)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
676 } TRANS;
678 struct sound_settings {
679 MACHINE mach; /* machine dependent things */
680 SETTINGS hard; /* hardware settings */
681 SETTINGS soft; /* software settings */
682 SETTINGS dsp; /* /dev/dsp default settings */
683 TRANS *trans; /* supported translations */
684 int volume_left; /* volume (range is machine dependent) */
685 int volume_right;
686 int bass; /* tone (range is machine dependent) */
687 int treble;
688 int gain;
689 int minDev; /* minor device number currently open */
690 #if defined(CONFIG_ATARI) || defined(CONFIG_PPC)
691 int bal; /* balance factor for expanding (not volume!) */
692 u_long data; /* data for expanding */
693 #endif /* CONFIG_ATARI */
696 static struct sound_settings sound;
699 #ifdef CONFIG_ATARI
700 static void *AtaAlloc(unsigned int size, int flags);
701 static void AtaFree(void *, unsigned int size);
702 static int AtaIrqInit(void);
703 #ifdef MODULE
704 static void AtaIrqCleanUp(void);
705 #endif /* MODULE */
706 static int AtaSetBass(int bass);
707 static int AtaSetTreble(int treble);
708 static void TTSilence(void);
709 static void TTInit(void);
710 static int TTSetFormat(int format);
711 static int TTSetVolume(int volume);
712 static int TTSetGain(int gain);
713 static void FalconSilence(void);
714 static void FalconInit(void);
715 static int FalconSetFormat(int format);
716 static int FalconSetVolume(int volume);
717 static void ata_sq_play_next_frame(int index);
718 static void AtaPlay(void);
719 static void ata_sq_interrupt(int irq, void *dummy, struct pt_regs *fp);
720 #endif /* CONFIG_ATARI */
722 #ifdef CONFIG_AMIGA
723 static void *AmiAlloc(unsigned int size, int flags);
724 static void AmiFree(void *, unsigned int);
725 static int AmiIrqInit(void);
726 #ifdef MODULE
727 static void AmiIrqCleanUp(void);
728 #endif /* MODULE */
729 static void AmiSilence(void);
730 static void AmiInit(void);
731 static int AmiSetFormat(int format);
732 static int AmiSetVolume(int volume);
733 static int AmiSetTreble(int treble);
734 static void ami_sq_play_next_frame(int index);
735 static void AmiPlay(void);
736 static void ami_sq_interrupt(int irq, void *dummy, struct pt_regs *fp);
737 #endif /* CONFIG_AMIGA */
739 #ifdef CONFIG_PPC
740 static void *PMacAlloc(unsigned int size, int flags);
741 static void PMacFree(void *ptr, unsigned int size);
742 static int PMacIrqInit(void);
743 #ifdef MODULE
744 static void PMacIrqCleanup(void);
745 #endif /* MODULE */
746 static void PMacSilence(void);
747 static void PMacInit(void);
748 static void PMacPlay(void);
749 static int PMacSetFormat(int format);
750 static int PMacSetVolume(int volume);
751 static void pmac_awacs_tx_intr(int irq, void *devid, struct pt_regs *regs);
752 static void pmac_awacs_intr(int irq, void *devid, struct pt_regs *regs);
753 static void awacs_write(int val);
754 static int awacs_get_volume(int reg, int lshift);
755 static int awacs_volume_setter(int volume, int n, int mute, int lshift);
756 static void awacs_mksound(unsigned int hz, unsigned int ticks);
757 static void awacs_nosound(unsigned long xx);
758 #endif /* CONFIG_PPC */
760 /*** Mid level stuff *********************************************************/
763 static void sound_silence(void);
764 static void sound_init(void);
765 static int sound_set_format(int format);
766 static int sound_set_speed(int speed);
767 static int sound_set_stereo(int stereo);
768 static int sound_set_volume(int volume);
769 #ifdef CONFIG_ATARI
770 static int sound_set_bass(int bass);
771 #endif /* CONFIG_ATARI */
772 #if defined(CONFIG_ATARI) || defined(CONFIG_AMIGA)
773 static int sound_set_treble(int treble);
774 #endif /* CONFIG_ATARI || CONFIG_AMIGA */
775 static ssize_t sound_copy_translate(const u_char *userPtr,
776 size_t userCount,
777 u_char frame[], ssize_t *frameUsed,
778 ssize_t frameLeft);
782 * /dev/mixer abstraction
785 struct sound_mixer {
786 int busy;
789 static struct sound_mixer mixer;
792 * Sound queue stuff, the heart of the driver
795 struct sound_queue {
796 int max_count, block_size;
797 char **buffers;
798 int max_active;
800 /* it shouldn't be necessary to declare any of these volatile */
801 int front, rear, count;
802 int rear_size;
804 * The use of the playing field depends on the hardware
806 * Atari, PMac: The number of frames that are loaded/playing
808 * Amiga: Bit 0 is set: a frame is loaded
809 * Bit 1 is set: a frame is playing
811 int playing;
812 wait_queue_head_t write_queue, open_queue, sync_queue;
813 int open_mode;
814 int busy, syncing;
815 #ifdef CONFIG_ATARI
816 int ignore_int; /* ++TeSche: used for Falcon */
817 #endif /* CONFIG_ATARI */
818 #ifdef CONFIG_AMIGA
819 int block_size_half, block_size_quarter;
820 #endif /* CONFIG_AMIGA */
823 static struct sound_queue sq;
825 #define sq_block_address(i) (sq.buffers[i])
826 #define SIGNAL_RECEIVED (signal_pending(current))
827 #define NON_BLOCKING(open_mode) (open_mode & O_NONBLOCK)
828 #define ONE_SECOND HZ /* in jiffies (100ths of a second) */
829 #define NO_TIME_LIMIT 0xffffffff
830 #define SLEEP(queue, time_limit) \
831 interruptible_sleep_on_timeout(&queue, (time_limit));
832 #define WAKE_UP(queue) (wake_up_interruptible(&queue))
835 * /dev/sndstat
838 struct sound_state {
839 int busy;
840 char buf[512];
841 int len, ptr;
844 static struct sound_state state;
846 /*** Common stuff ********************************************************/
848 static long long sound_lseek(struct file *file, long long offset, int orig);
849 static inline int ioctl_return(int *addr, int value)
851 if (value < 0)
852 return(value);
854 return put_user(value, addr)? -EFAULT: 0;
858 /*** Config & Setup **********************************************************/
861 void dmasound_init(void);
862 void dmasound_setup(char *str, int *ints);
865 /*** Translations ************************************************************/
868 /* ++TeSche: radically changed for new expanding purposes...
870 * These two routines now deal with copying/expanding/translating the samples
871 * from user space into our buffer at the right frequency. They take care about
872 * how much data there's actually to read, how much buffer space there is and
873 * to convert samples into the right frequency/encoding. They will only work on
874 * complete samples so it may happen they leave some bytes in the input stream
875 * if the user didn't write a multiple of the current sample size. They both
876 * return the number of bytes they've used from both streams so you may detect
877 * such a situation. Luckily all programs should be able to cope with that.
879 * I think I've optimized anything as far as one can do in plain C, all
880 * variables should fit in registers and the loops are really short. There's
881 * one loop for every possible situation. Writing a more generalized and thus
882 * parameterized loop would only produce slower code. Feel free to optimize
883 * this in assembler if you like. :)
885 * I think these routines belong here because they're not yet really hardware
886 * independent, especially the fact that the Falcon can play 16bit samples
887 * only in stereo is hardcoded in both of them!
889 * ++geert: split in even more functions (one per format)
892 #ifdef CONFIG_ATARI
893 static ssize_t ata_ct_law(const u_char *userPtr, size_t userCount,
894 u_char frame[], ssize_t *frameUsed,
895 ssize_t frameLeft)
897 char *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma8 : alaw2dma8;
898 ssize_t count, used;
899 u_char *p = &frame[*frameUsed];
901 count = min(userCount, frameLeft);
902 if (sound.soft.stereo)
903 count &= ~1;
904 used = count;
905 while (count > 0) {
906 u_char data;
907 if (get_user(data, userPtr++))
908 return -EFAULT;
909 *p++ = table[data];
910 count--;
912 *frameUsed += used;
913 return used;
917 static ssize_t ata_ct_s8(const u_char *userPtr, size_t userCount,
918 u_char frame[], ssize_t *frameUsed,
919 ssize_t frameLeft)
921 ssize_t count, used;
922 void *p = &frame[*frameUsed];
924 count = min(userCount, frameLeft);
925 if (sound.soft.stereo)
926 count &= ~1;
927 used = count;
928 if (copy_from_user(p, userPtr, count))
929 return -EFAULT;
930 *frameUsed += used;
931 return(used);
935 static ssize_t ata_ct_u8(const u_char *userPtr, size_t userCount,
936 u_char frame[], ssize_t *frameUsed,
937 ssize_t frameLeft)
939 ssize_t count, used;
941 if (!sound.soft.stereo) {
942 u_char *p = &frame[*frameUsed];
943 count = min(userCount, frameLeft);
944 used = count;
945 while (count > 0) {
946 u_char data;
947 if (get_user(data, userPtr++))
948 return -EFAULT;
949 *p++ = data ^ 0x80;
950 count--;
952 } else {
953 u_short *p = (u_short *)&frame[*frameUsed];
954 count = min(userCount, frameLeft)>>1;
955 used = count*2;
956 while (count > 0) {
957 u_short data;
958 if (get_user(data, ((u_short *)userPtr)++))
959 return -EFAULT;
960 *p++ = data ^ 0x8080;
961 count--;
964 *frameUsed += used;
965 return(used);
969 static ssize_t ata_ct_s16be(const u_char *userPtr, size_t userCount,
970 u_char frame[], ssize_t *frameUsed,
971 ssize_t frameLeft)
973 ssize_t count, used;
975 if (!sound.soft.stereo) {
976 u_short *p = (u_short *)&frame[*frameUsed];
977 count = min(userCount, frameLeft)>>1;
978 used = count*2;
979 while (count > 0) {
980 u_short data;
981 if (get_user(data, ((u_short *)userPtr)++))
982 return -EFAULT;
983 *p++ = data;
984 *p++ = data;
985 count--;
987 *frameUsed += used*2;
988 } else {
989 void *p = (u_short *)&frame[*frameUsed];
990 count = min(userCount, frameLeft) & ~3;
991 used = count;
992 if (copy_from_user(p, userPtr, count))
993 return -EFAULT;
994 *frameUsed += used;
996 return(used);
1000 static ssize_t ata_ct_u16be(const u_char *userPtr, size_t userCount,
1001 u_char frame[], ssize_t *frameUsed,
1002 ssize_t frameLeft)
1004 ssize_t count, used;
1006 if (!sound.soft.stereo) {
1007 u_short *p = (u_short *)&frame[*frameUsed];
1008 count = min(userCount, frameLeft)>>1;
1009 used = count*2;
1010 while (count > 0) {
1011 u_short data;
1012 if (get_user(data, ((u_short *)userPtr)++))
1013 return -EFAULT;
1014 data ^= 0x8000;
1015 *p++ = data;
1016 *p++ = data;
1017 count--;
1019 *frameUsed += used*2;
1020 } else {
1021 u_long *p = (u_long *)&frame[*frameUsed];
1022 count = min(userCount, frameLeft)>>2;
1023 used = count*4;
1024 while (count > 0) {
1025 u_long data;
1026 if (get_user(data, ((u_int *)userPtr)++))
1027 return -EFAULT;
1028 *p++ = data ^ 0x80008000;
1029 count--;
1031 *frameUsed += used;
1033 return(used);
1037 static ssize_t ata_ct_s16le(const u_char *userPtr, size_t userCount,
1038 u_char frame[], ssize_t *frameUsed,
1039 ssize_t frameLeft)
1041 ssize_t count, used;
1043 count = frameLeft;
1044 if (!sound.soft.stereo) {
1045 u_short *p = (u_short *)&frame[*frameUsed];
1046 count = min(userCount, frameLeft)>>1;
1047 used = count*2;
1048 while (count > 0) {
1049 u_short data;
1050 if (get_user(data, ((u_short *)userPtr)++))
1051 return -EFAULT;
1052 data = le2be16(data);
1053 *p++ = data;
1054 *p++ = data;
1055 count--;
1057 *frameUsed += used*2;
1058 } else {
1059 u_long *p = (u_long *)&frame[*frameUsed];
1060 count = min(userCount, frameLeft)>>2;
1061 used = count*4;
1062 while (count > 0) {
1063 u_long data;
1064 if (get_user(data, ((u_int *)userPtr)++))
1065 return -EFAULT;
1066 data = le2be16dbl(data);
1067 *p++ = data;
1068 count--;
1070 *frameUsed += used;
1072 return(used);
1076 static ssize_t ata_ct_u16le(const u_char *userPtr, size_t userCount,
1077 u_char frame[], ssize_t *frameUsed,
1078 ssize_t frameLeft)
1080 ssize_t count, used;
1082 count = frameLeft;
1083 if (!sound.soft.stereo) {
1084 u_short *p = (u_short *)&frame[*frameUsed];
1085 count = min(userCount, frameLeft)>>1;
1086 used = count*2;
1087 while (count > 0) {
1088 u_short data;
1089 if (get_user(data, ((u_short *)userPtr)++))
1090 return -EFAULT;
1091 data = le2be16(data) ^ 0x8000;
1092 *p++ = data;
1093 *p++ = data;
1095 *frameUsed += used*2;
1096 } else {
1097 u_long *p = (u_long *)&frame[*frameUsed];
1098 count = min(userCount, frameLeft)>>2;
1099 used = count;
1100 while (count > 0) {
1101 u_long data;
1102 if (get_user(data, ((u_int *)userPtr)++))
1103 return -EFAULT;
1104 data = le2be16dbl(data) ^ 0x80008000;
1105 *p++ = data;
1106 count--;
1108 *frameUsed += used;
1110 return(used);
1114 static ssize_t ata_ctx_law(const u_char *userPtr, size_t userCount,
1115 u_char frame[], ssize_t *frameUsed,
1116 ssize_t frameLeft)
1118 char *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma8 : alaw2dma8;
1119 /* this should help gcc to stuff everything into registers */
1120 long bal = sound.bal;
1121 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1122 ssize_t used, usedf;
1124 used = userCount;
1125 usedf = frameLeft;
1126 if (!sound.soft.stereo) {
1127 u_char *p = &frame[*frameUsed];
1128 u_char data = sound.data;
1129 while (frameLeft) {
1130 u_char c;
1131 if (bal < 0) {
1132 if (!userCount)
1133 break;
1134 if (get_user(c, userPtr++))
1135 return -EFAULT;
1136 data = table[c];
1137 userCount--;
1138 bal += hSpeed;
1140 *p++ = data;
1141 frameLeft--;
1142 bal -= sSpeed;
1144 sound.data = data;
1145 } else {
1146 u_short *p = (u_short *)&frame[*frameUsed];
1147 u_short data = sound.data;
1148 while (frameLeft >= 2) {
1149 u_char c;
1150 if (bal < 0) {
1151 if (userCount < 2)
1152 break;
1153 if (get_user(c, userPtr++))
1154 return -EFAULT;
1155 data = table[c] << 8;
1156 if (get_user(c, userPtr++))
1157 return -EFAULT;
1158 data |= table[c];
1159 userCount -= 2;
1160 bal += hSpeed;
1162 *p++ = data;
1163 frameLeft -= 2;
1164 bal -= sSpeed;
1166 sound.data = data;
1168 sound.bal = bal;
1169 used -= userCount;
1170 *frameUsed += usedf-frameLeft;
1171 return(used);
1175 static ssize_t ata_ctx_s8(const u_char *userPtr, size_t userCount,
1176 u_char frame[], ssize_t *frameUsed,
1177 ssize_t frameLeft)
1179 /* this should help gcc to stuff everything into registers */
1180 long bal = sound.bal;
1181 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1182 ssize_t used, usedf;
1184 used = userCount;
1185 usedf = frameLeft;
1186 if (!sound.soft.stereo) {
1187 u_char *p = &frame[*frameUsed];
1188 u_char data = sound.data;
1189 while (frameLeft) {
1190 if (bal < 0) {
1191 if (!userCount)
1192 break;
1193 if (get_user(data, userPtr++))
1194 return -EFAULT;
1195 userCount--;
1196 bal += hSpeed;
1198 *p++ = data;
1199 frameLeft--;
1200 bal -= sSpeed;
1202 sound.data = data;
1203 } else {
1204 u_short *p = (u_short *)&frame[*frameUsed];
1205 u_short data = sound.data;
1206 while (frameLeft >= 2) {
1207 if (bal < 0) {
1208 if (userCount < 2)
1209 break;
1210 if (get_user(data, ((u_short *)userPtr)++))
1211 return -EFAULT;
1212 userCount -= 2;
1213 bal += hSpeed;
1215 *p++ = data;
1216 frameLeft -= 2;
1217 bal -= sSpeed;
1219 sound.data = data;
1221 sound.bal = bal;
1222 used -= userCount;
1223 *frameUsed += usedf-frameLeft;
1224 return(used);
1228 static ssize_t ata_ctx_u8(const u_char *userPtr, size_t userCount,
1229 u_char frame[], ssize_t *frameUsed,
1230 ssize_t frameLeft)
1232 /* this should help gcc to stuff everything into registers */
1233 long bal = sound.bal;
1234 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1235 ssize_t used, usedf;
1237 used = userCount;
1238 usedf = frameLeft;
1239 if (!sound.soft.stereo) {
1240 u_char *p = &frame[*frameUsed];
1241 u_char data = sound.data;
1242 while (frameLeft) {
1243 if (bal < 0) {
1244 if (!userCount)
1245 break;
1246 if (get_user(data, userPtr++))
1247 return -EFAULT;
1248 data ^= 0x80;
1249 userCount--;
1250 bal += hSpeed;
1252 *p++ = data;
1253 frameLeft--;
1254 bal -= sSpeed;
1256 sound.data = data;
1257 } else {
1258 u_short *p = (u_short *)&frame[*frameUsed];
1259 u_short data = sound.data;
1260 while (frameLeft >= 2) {
1261 if (bal < 0) {
1262 if (userCount < 2)
1263 break;
1264 if (get_user(data, ((u_short *)userPtr)++))
1265 return -EFAULT;
1266 data ^= 0x8080;
1267 userCount -= 2;
1268 bal += hSpeed;
1270 *p++ = data;
1271 frameLeft -= 2;
1272 bal -= sSpeed;
1274 sound.data = data;
1276 sound.bal = bal;
1277 used -= userCount;
1278 *frameUsed += usedf-frameLeft;
1279 return(used);
1283 static ssize_t ata_ctx_s16be(const u_char *userPtr, size_t userCount,
1284 u_char frame[], ssize_t *frameUsed,
1285 ssize_t frameLeft)
1287 /* this should help gcc to stuff everything into registers */
1288 long bal = sound.bal;
1289 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1290 ssize_t used, usedf;
1292 used = userCount;
1293 usedf = frameLeft;
1294 if (!sound.soft.stereo) {
1295 u_short *p = (u_short *)&frame[*frameUsed];
1296 u_short data = sound.data;
1297 while (frameLeft >= 4) {
1298 if (bal < 0) {
1299 if (userCount < 2)
1300 break;
1301 if (get_user(data, ((u_short *)userPtr)++))
1302 return -EFAULT;
1303 userCount -= 2;
1304 bal += hSpeed;
1306 *p++ = data;
1307 *p++ = data;
1308 frameLeft -= 4;
1309 bal -= sSpeed;
1311 sound.data = data;
1312 } else {
1313 u_long *p = (u_long *)&frame[*frameUsed];
1314 u_long data = sound.data;
1315 while (frameLeft >= 4) {
1316 if (bal < 0) {
1317 if (userCount < 4)
1318 break;
1319 if (get_user(data, ((u_int *)userPtr)++))
1320 return -EFAULT;
1321 userCount -= 4;
1322 bal += hSpeed;
1324 *p++ = data;
1325 frameLeft -= 4;
1326 bal -= sSpeed;
1328 sound.data = data;
1330 sound.bal = bal;
1331 used -= userCount;
1332 *frameUsed += usedf-frameLeft;
1333 return(used);
1337 static ssize_t ata_ctx_u16be(const u_char *userPtr, size_t userCount,
1338 u_char frame[], ssize_t *frameUsed,
1339 ssize_t frameLeft)
1341 /* this should help gcc to stuff everything into registers */
1342 long bal = sound.bal;
1343 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1344 ssize_t used, usedf;
1346 used = userCount;
1347 usedf = frameLeft;
1348 if (!sound.soft.stereo) {
1349 u_short *p = (u_short *)&frame[*frameUsed];
1350 u_short data = sound.data;
1351 while (frameLeft >= 4) {
1352 if (bal < 0) {
1353 if (userCount < 2)
1354 break;
1355 if (get_user(data, ((u_short *)userPtr)++))
1356 return -EFAULT;
1357 data ^= 0x8000;
1358 userCount -= 2;
1359 bal += hSpeed;
1361 *p++ = data;
1362 *p++ = data;
1363 frameLeft -= 4;
1364 bal -= sSpeed;
1366 sound.data = data;
1367 } else {
1368 u_long *p = (u_long *)&frame[*frameUsed];
1369 u_long data = sound.data;
1370 while (frameLeft >= 4) {
1371 if (bal < 0) {
1372 if (userCount < 4)
1373 break;
1374 if (get_user(data, ((u_int *)userPtr)++))
1375 return -EFAULT;
1376 data ^= 0x80008000;
1377 userCount -= 4;
1378 bal += hSpeed;
1380 *p++ = data;
1381 frameLeft -= 4;
1382 bal -= sSpeed;
1384 sound.data = data;
1386 sound.bal = bal;
1387 used -= userCount;
1388 *frameUsed += usedf-frameLeft;
1389 return(used);
1393 static ssize_t ata_ctx_s16le(const u_char *userPtr, size_t userCount,
1394 u_char frame[], ssize_t *frameUsed,
1395 ssize_t frameLeft)
1397 /* this should help gcc to stuff everything into registers */
1398 long bal = sound.bal;
1399 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1400 ssize_t used, usedf;
1402 used = userCount;
1403 usedf = frameLeft;
1404 if (!sound.soft.stereo) {
1405 u_short *p = (u_short *)&frame[*frameUsed];
1406 u_short data = sound.data;
1407 while (frameLeft >= 4) {
1408 if (bal < 0) {
1409 if (userCount < 2)
1410 break;
1411 if (get_user(data, ((u_short *)userPtr)++))
1412 return -EFAULT;
1413 data = le2be16(data);
1414 userCount -= 2;
1415 bal += hSpeed;
1417 *p++ = data;
1418 *p++ = data;
1419 frameLeft -= 4;
1420 bal -= sSpeed;
1422 sound.data = data;
1423 } else {
1424 u_long *p = (u_long *)&frame[*frameUsed];
1425 u_long data = sound.data;
1426 while (frameLeft >= 4) {
1427 if (bal < 0) {
1428 if (userCount < 4)
1429 break;
1430 if (get_user(data, ((u_int *)userPtr)++))
1431 return -EFAULT;
1432 data = le2be16dbl(data);
1433 userCount -= 4;
1434 bal += hSpeed;
1436 *p++ = data;
1437 frameLeft -= 4;
1438 bal -= sSpeed;
1440 sound.data = data;
1442 sound.bal = bal;
1443 used -= userCount;
1444 *frameUsed += usedf-frameLeft;
1445 return(used);
1449 static ssize_t ata_ctx_u16le(const u_char *userPtr, size_t userCount,
1450 u_char frame[], ssize_t *frameUsed,
1451 ssize_t frameLeft)
1453 /* this should help gcc to stuff everything into registers */
1454 long bal = sound.bal;
1455 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1456 ssize_t used, usedf;
1458 used = userCount;
1459 usedf = frameLeft;
1460 if (!sound.soft.stereo) {
1461 u_short *p = (u_short *)&frame[*frameUsed];
1462 u_short data = sound.data;
1463 while (frameLeft >= 4) {
1464 if (bal < 0) {
1465 if (userCount < 2)
1466 break;
1467 if (get_user(data, ((u_short *)userPtr)++))
1468 return -EFAULT;
1469 data = le2be16(data) ^ 0x8000;
1470 userCount -= 2;
1471 bal += hSpeed;
1473 *p++ = data;
1474 *p++ = data;
1475 frameLeft -= 4;
1476 bal -= sSpeed;
1478 sound.data = data;
1479 } else {
1480 u_long *p = (u_long *)&frame[*frameUsed];
1481 u_long data = sound.data;
1482 while (frameLeft >= 4) {
1483 if (bal < 0) {
1484 if (userCount < 4)
1485 break;
1486 if (get_user(data, ((u_int *)userPtr)++))
1487 return -EFAULT;
1488 data = le2be16dbl(data) ^ 0x80008000;
1489 userCount -= 4;
1490 bal += hSpeed;
1492 *p++ = data;
1493 frameLeft -= 4;
1494 bal -= sSpeed;
1496 sound.data = data;
1498 sound.bal = bal;
1499 used -= userCount;
1500 *frameUsed += usedf-frameLeft;
1501 return(used);
1503 #endif /* CONFIG_ATARI */
1506 #ifdef CONFIG_AMIGA
1507 static ssize_t ami_ct_law(const u_char *userPtr, size_t userCount,
1508 u_char frame[], ssize_t *frameUsed,
1509 ssize_t frameLeft)
1511 char *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma8 : alaw2dma8;
1512 ssize_t count, used;
1514 if (!sound.soft.stereo) {
1515 u_char *p = &frame[*frameUsed];
1516 count = min(userCount, frameLeft) & ~1;
1517 used = count;
1518 while (count > 0) {
1519 u_char data;
1520 if (get_user(data, userPtr++))
1521 return -EFAULT;
1522 *p++ = table[data];
1523 count--;
1525 } else {
1526 u_char *left = &frame[*frameUsed>>1];
1527 u_char *right = left+sq.block_size_half;
1528 count = min(userCount, frameLeft)>>1 & ~1;
1529 used = count*2;
1530 while (count > 0) {
1531 u_char data;
1532 if (get_user(data, userPtr++))
1533 return -EFAULT;
1534 *left++ = table[data];
1535 if (get_user(data, userPtr++))
1536 return -EFAULT;
1537 *right++ = table[data];
1538 count--;
1541 *frameUsed += used;
1542 return(used);
1546 static ssize_t ami_ct_s8(const u_char *userPtr, size_t userCount,
1547 u_char frame[], ssize_t *frameUsed, ssize_t frameLeft)
1549 ssize_t count, used;
1551 if (!sound.soft.stereo) {
1552 void *p = &frame[*frameUsed];
1553 count = min(userCount, frameLeft) & ~1;
1554 used = count;
1555 if (copy_from_user(p, userPtr, count))
1556 return -EFAULT;
1557 } else {
1558 u_char *left = &frame[*frameUsed>>1];
1559 u_char *right = left+sq.block_size_half;
1560 count = min(userCount, frameLeft)>>1 & ~1;
1561 used = count*2;
1562 while (count > 0) {
1563 if (get_user(*left++, userPtr++)
1564 || get_user(*right++, userPtr++))
1565 return -EFAULT;
1566 count--;
1569 *frameUsed += used;
1570 return(used);
1574 static ssize_t ami_ct_u8(const u_char *userPtr, size_t userCount,
1575 u_char frame[], ssize_t *frameUsed, ssize_t frameLeft)
1577 ssize_t count, used;
1579 if (!sound.soft.stereo) {
1580 char *p = &frame[*frameUsed];
1581 count = min(userCount, frameLeft) & ~1;
1582 used = count;
1583 while (count > 0) {
1584 u_char data;
1585 if (get_user(data, userPtr++))
1586 return -EFAULT;
1587 *p++ = data ^ 0x80;
1588 count--;
1590 } else {
1591 u_char *left = &frame[*frameUsed>>1];
1592 u_char *right = left+sq.block_size_half;
1593 count = min(userCount, frameLeft)>>1 & ~1;
1594 used = count*2;
1595 while (count > 0) {
1596 u_char data;
1597 if (get_user(data, userPtr++))
1598 return -EFAULT;
1599 *left++ = data ^ 0x80;
1600 if (get_user(data, userPtr++))
1601 return -EFAULT;
1602 *right++ = data ^ 0x80;
1603 count--;
1606 *frameUsed += used;
1607 return(used);
1611 static ssize_t ami_ct_s16be(const u_char *userPtr, size_t userCount,
1612 u_char frame[], ssize_t *frameUsed,
1613 ssize_t frameLeft)
1615 ssize_t count, used;
1616 u_short data;
1618 if (!sound.soft.stereo) {
1619 u_char *high = &frame[*frameUsed>>1];
1620 u_char *low = high+sq.block_size_half;
1621 count = min(userCount, frameLeft)>>1 & ~1;
1622 used = count*2;
1623 while (count > 0) {
1624 if (get_user(data, ((u_short *)userPtr)++))
1625 return -EFAULT;
1626 *high++ = data>>8;
1627 *low++ = (data>>2) & 0x3f;
1628 count--;
1630 } else {
1631 u_char *lefth = &frame[*frameUsed>>2];
1632 u_char *leftl = lefth+sq.block_size_quarter;
1633 u_char *righth = lefth+sq.block_size_half;
1634 u_char *rightl = righth+sq.block_size_quarter;
1635 count = min(userCount, frameLeft)>>2 & ~1;
1636 used = count*4;
1637 while (count > 0) {
1638 if (get_user(data, ((u_short *)userPtr)++))
1639 return -EFAULT;
1640 *lefth++ = data>>8;
1641 *leftl++ = (data>>2) & 0x3f;
1642 if (get_user(data, ((u_short *)userPtr)++))
1643 return -EFAULT;
1644 *righth++ = data>>8;
1645 *rightl++ = (data>>2) & 0x3f;
1646 count--;
1649 *frameUsed += used;
1650 return(used);
1654 static ssize_t ami_ct_u16be(const u_char *userPtr, size_t userCount,
1655 u_char frame[], ssize_t *frameUsed,
1656 ssize_t frameLeft)
1658 ssize_t count, used;
1659 u_short data;
1661 if (!sound.soft.stereo) {
1662 u_char *high = &frame[*frameUsed>>1];
1663 u_char *low = high+sq.block_size_half;
1664 count = min(userCount, frameLeft)>>1 & ~1;
1665 used = count*2;
1666 while (count > 0) {
1667 if (get_user(data, ((u_short *)userPtr)++))
1668 return -EFAULT;
1669 data ^= 0x8000;
1670 *high++ = data>>8;
1671 *low++ = (data>>2) & 0x3f;
1672 count--;
1674 } else {
1675 u_char *lefth = &frame[*frameUsed>>2];
1676 u_char *leftl = lefth+sq.block_size_quarter;
1677 u_char *righth = lefth+sq.block_size_half;
1678 u_char *rightl = righth+sq.block_size_quarter;
1679 count = min(userCount, frameLeft)>>2 & ~1;
1680 used = count*4;
1681 while (count > 0) {
1682 if (get_user(data, ((u_short *)userPtr)++))
1683 return -EFAULT;
1684 data ^= 0x8000;
1685 *lefth++ = data>>8;
1686 *leftl++ = (data>>2) & 0x3f;
1687 if (get_user(data, ((u_short *)userPtr)++))
1688 return -EFAULT;
1689 data ^= 0x8000;
1690 *righth++ = data>>8;
1691 *rightl++ = (data>>2) & 0x3f;
1692 count--;
1695 *frameUsed += used;
1696 return(used);
1700 static ssize_t ami_ct_s16le(const u_char *userPtr, size_t userCount,
1701 u_char frame[], ssize_t *frameUsed,
1702 ssize_t frameLeft)
1704 ssize_t count, used;
1705 u_short data;
1707 if (!sound.soft.stereo) {
1708 u_char *high = &frame[*frameUsed>>1];
1709 u_char *low = high+sq.block_size_half;
1710 count = min(userCount, frameLeft)>>1 & ~1;
1711 used = count*2;
1712 while (count > 0) {
1713 if (get_user(data, ((u_short *)userPtr)++))
1714 return -EFAULT;
1715 data = le2be16(data);
1716 *high++ = data>>8;
1717 *low++ = (data>>2) & 0x3f;
1718 count--;
1720 } else {
1721 u_char *lefth = &frame[*frameUsed>>2];
1722 u_char *leftl = lefth+sq.block_size_quarter;
1723 u_char *righth = lefth+sq.block_size_half;
1724 u_char *rightl = righth+sq.block_size_quarter;
1725 count = min(userCount, frameLeft)>>2 & ~1;
1726 used = count*4;
1727 while (count > 0) {
1728 if (get_user(data, ((u_short *)userPtr)++))
1729 return -EFAULT;
1730 data = le2be16(data);
1731 *lefth++ = data>>8;
1732 *leftl++ = (data>>2) & 0x3f;
1733 if (get_user(data, ((u_short *)userPtr)++))
1734 return -EFAULT;
1735 data = le2be16(data);
1736 *righth++ = data>>8;
1737 *rightl++ = (data>>2) & 0x3f;
1738 count--;
1741 *frameUsed += used;
1742 return(used);
1746 static ssize_t ami_ct_u16le(const u_char *userPtr, size_t userCount,
1747 u_char frame[], ssize_t *frameUsed,
1748 ssize_t frameLeft)
1750 ssize_t count, used;
1751 u_short data;
1753 if (!sound.soft.stereo) {
1754 u_char *high = &frame[*frameUsed>>1];
1755 u_char *low = high+sq.block_size_half;
1756 count = min(userCount, frameLeft)>>1 & ~1;
1757 used = count*2;
1758 while (count > 0) {
1759 if (get_user(data, ((u_short *)userPtr)++))
1760 return -EFAULT;
1761 data = le2be16(data) ^ 0x8000;
1762 *high++ = data>>8;
1763 *low++ = (data>>2) & 0x3f;
1764 count--;
1766 } else {
1767 u_char *lefth = &frame[*frameUsed>>2];
1768 u_char *leftl = lefth+sq.block_size_quarter;
1769 u_char *righth = lefth+sq.block_size_half;
1770 u_char *rightl = righth+sq.block_size_quarter;
1771 count = min(userCount, frameLeft)>>2 & ~1;
1772 used = count*4;
1773 while (count > 0) {
1774 if (get_user(data, ((u_short *)userPtr)++))
1775 return -EFAULT;
1776 data = le2be16(data) ^ 0x8000;
1777 *lefth++ = data>>8;
1778 *leftl++ = (data>>2) & 0x3f;
1779 if (get_user(data, ((u_short *)userPtr)++))
1780 return -EFAULT;
1781 data = le2be16(data) ^ 0x8000;
1782 *righth++ = data>>8;
1783 *rightl++ = (data>>2) & 0x3f;
1784 count--;
1787 *frameUsed += used;
1788 return(used);
1790 #endif /* CONFIG_AMIGA */
1792 #ifdef CONFIG_PPC
1793 static ssize_t pmac_ct_law(const u_char *userPtr, size_t userCount,
1794 u_char frame[], ssize_t *frameUsed,
1795 ssize_t frameLeft)
1797 short *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16;
1798 ssize_t count, used;
1799 short *p = (short *) &frame[*frameUsed];
1800 int val, stereo = sound.soft.stereo;
1802 frameLeft >>= 2;
1803 if (stereo)
1804 userCount >>= 1;
1805 used = count = min(userCount, frameLeft);
1806 while (count > 0) {
1807 u_char data;
1808 if (get_user(data, userPtr++))
1809 return -EFAULT;
1810 val = table[data];
1811 *p++ = val;
1812 if (stereo) {
1813 if (get_user(data, userPtr++))
1814 return -EFAULT;
1815 val = table[data];
1817 *p++ = val;
1818 count--;
1820 *frameUsed += used * 4;
1821 return stereo? used * 2: used;
1825 static ssize_t pmac_ct_s8(const u_char *userPtr, size_t userCount,
1826 u_char frame[], ssize_t *frameUsed,
1827 ssize_t frameLeft)
1829 ssize_t count, used;
1830 short *p = (short *) &frame[*frameUsed];
1831 int val, stereo = sound.soft.stereo;
1833 frameLeft >>= 2;
1834 if (stereo)
1835 userCount >>= 1;
1836 used = count = min(userCount, frameLeft);
1837 while (count > 0) {
1838 u_char data;
1839 if (get_user(data, userPtr++))
1840 return -EFAULT;
1841 val = data << 8;
1842 *p++ = val;
1843 if (stereo) {
1844 if (get_user(data, userPtr++))
1845 return -EFAULT;
1846 val = data << 8;
1848 *p++ = val;
1849 count--;
1851 *frameUsed += used * 4;
1852 return stereo? used * 2: used;
1856 static ssize_t pmac_ct_u8(const u_char *userPtr, size_t userCount,
1857 u_char frame[], ssize_t *frameUsed,
1858 ssize_t frameLeft)
1860 ssize_t count, used;
1861 short *p = (short *) &frame[*frameUsed];
1862 int val, stereo = sound.soft.stereo;
1864 frameLeft >>= 2;
1865 if (stereo)
1866 userCount >>= 1;
1867 used = count = min(userCount, frameLeft);
1868 while (count > 0) {
1869 u_char data;
1870 if (get_user(data, userPtr++))
1871 return -EFAULT;
1872 val = (data ^ 0x80) << 8;
1873 *p++ = val;
1874 if (stereo) {
1875 if (get_user(data, userPtr++))
1876 return -EFAULT;
1877 val = (data ^ 0x80) << 8;
1879 *p++ = val;
1880 count--;
1882 *frameUsed += used * 4;
1883 return stereo? used * 2: used;
1887 static ssize_t pmac_ct_s16(const u_char *userPtr, size_t userCount,
1888 u_char frame[], ssize_t *frameUsed,
1889 ssize_t frameLeft)
1891 ssize_t count, used;
1892 int stereo = sound.soft.stereo;
1893 short *fp = (short *) &frame[*frameUsed];
1895 frameLeft >>= 2;
1896 userCount >>= (stereo? 2: 1);
1897 used = count = min(userCount, frameLeft);
1898 if (!stereo) {
1899 short *up = (short *) userPtr;
1900 while (count > 0) {
1901 short data;
1902 if (get_user(data, up++))
1903 return -EFAULT;
1904 *fp++ = data;
1905 *fp++ = data;
1906 count--;
1908 } else {
1909 if (copy_from_user(fp, userPtr, count * 4))
1910 return -EFAULT;
1912 *frameUsed += used * 4;
1913 return stereo? used * 4: used * 2;
1916 static ssize_t pmac_ct_u16(const u_char *userPtr, size_t userCount,
1917 u_char frame[], ssize_t *frameUsed,
1918 ssize_t frameLeft)
1920 ssize_t count, used;
1921 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
1922 int stereo = sound.soft.stereo;
1923 short *fp = (short *) &frame[*frameUsed];
1924 short *up = (short *) userPtr;
1926 frameLeft >>= 2;
1927 userCount >>= (stereo? 2: 1);
1928 used = count = min(userCount, frameLeft);
1929 while (count > 0) {
1930 int data;
1931 if (get_user(data, up++))
1932 return -EFAULT;
1933 data ^= mask;
1934 *fp++ = data;
1935 if (stereo) {
1936 if (get_user(data, up++))
1937 return -EFAULT;
1938 data ^= mask;
1940 *fp++ = data;
1941 count--;
1943 *frameUsed += used * 4;
1944 return stereo? used * 4: used * 2;
1948 static ssize_t pmac_ctx_law(const u_char *userPtr, size_t userCount,
1949 u_char frame[], ssize_t *frameUsed,
1950 ssize_t frameLeft)
1952 unsigned short *table = (unsigned short *)
1953 (sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16);
1954 unsigned int data = sound.data;
1955 unsigned int *p = (unsigned int *) &frame[*frameUsed];
1956 int bal = sound.bal;
1957 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1958 int utotal, ftotal;
1959 int stereo = sound.soft.stereo;
1961 frameLeft >>= 2;
1962 if (stereo)
1963 userCount >>= 1;
1964 ftotal = frameLeft;
1965 utotal = userCount;
1966 while (frameLeft) {
1967 u_char c;
1968 if (bal < 0) {
1969 if (userCount == 0)
1970 break;
1971 if (get_user(c, userPtr++))
1972 return -EFAULT;
1973 data = table[c];
1974 if (stereo) {
1975 if (get_user(c, userPtr++))
1976 return -EFAULT;
1977 data = (data << 16) + table[c];
1978 } else
1979 data = (data << 16) + data;
1980 userCount--;
1981 bal += hSpeed;
1983 *p++ = data;
1984 frameLeft--;
1985 bal -= sSpeed;
1987 sound.bal = bal;
1988 sound.data = data;
1989 *frameUsed += (ftotal - frameLeft) * 4;
1990 utotal -= userCount;
1991 return stereo? utotal * 2: utotal;
1995 static ssize_t pmac_ctx_s8(const u_char *userPtr, size_t userCount,
1996 u_char frame[], ssize_t *frameUsed,
1997 ssize_t frameLeft)
1999 unsigned int *p = (unsigned int *) &frame[*frameUsed];
2000 unsigned int data = sound.data;
2001 int bal = sound.bal;
2002 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
2003 int stereo = sound.soft.stereo;
2004 int utotal, ftotal;
2006 frameLeft >>= 2;
2007 if (stereo)
2008 userCount >>= 1;
2009 ftotal = frameLeft;
2010 utotal = userCount;
2011 while (frameLeft) {
2012 u_char c;
2013 if (bal < 0) {
2014 if (userCount == 0)
2015 break;
2016 if (get_user(c, userPtr++))
2017 return -EFAULT;
2018 data = c << 8;
2019 if (stereo) {
2020 if (get_user(c, userPtr++))
2021 return -EFAULT;
2022 data = (data << 16) + (c << 8);
2023 } else
2024 data = (data << 16) + data;
2025 userCount--;
2026 bal += hSpeed;
2028 *p++ = data;
2029 frameLeft--;
2030 bal -= sSpeed;
2032 sound.bal = bal;
2033 sound.data = data;
2034 *frameUsed += (ftotal - frameLeft) * 4;
2035 utotal -= userCount;
2036 return stereo? utotal * 2: utotal;
2040 static ssize_t pmac_ctx_u8(const u_char *userPtr, size_t userCount,
2041 u_char frame[], ssize_t *frameUsed,
2042 ssize_t frameLeft)
2044 unsigned int *p = (unsigned int *) &frame[*frameUsed];
2045 unsigned int data = sound.data;
2046 int bal = sound.bal;
2047 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
2048 int stereo = sound.soft.stereo;
2049 int utotal, ftotal;
2051 frameLeft >>= 2;
2052 if (stereo)
2053 userCount >>= 1;
2054 ftotal = frameLeft;
2055 utotal = userCount;
2056 while (frameLeft) {
2057 u_char c;
2058 if (bal < 0) {
2059 if (userCount == 0)
2060 break;
2061 if (get_user(c, userPtr++))
2062 return -EFAULT;
2063 data = (c ^ 0x80) << 8;
2064 if (stereo) {
2065 if (get_user(c, userPtr++))
2066 return -EFAULT;
2067 data = (data << 16) + ((c ^ 0x80) << 8);
2068 } else
2069 data = (data << 16) + data;
2070 userCount--;
2071 bal += hSpeed;
2073 *p++ = data;
2074 frameLeft--;
2075 bal -= sSpeed;
2077 sound.bal = bal;
2078 sound.data = data;
2079 *frameUsed += (ftotal - frameLeft) * 4;
2080 utotal -= userCount;
2081 return stereo? utotal * 2: utotal;
2085 static ssize_t pmac_ctx_s16(const u_char *userPtr, size_t userCount,
2086 u_char frame[], ssize_t *frameUsed,
2087 ssize_t frameLeft)
2089 unsigned int *p = (unsigned int *) &frame[*frameUsed];
2090 unsigned int data = sound.data;
2091 unsigned short *up = (unsigned short *) userPtr;
2092 int bal = sound.bal;
2093 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
2094 int stereo = sound.soft.stereo;
2095 int utotal, ftotal;
2097 frameLeft >>= 2;
2098 userCount >>= (stereo? 2: 1);
2099 ftotal = frameLeft;
2100 utotal = userCount;
2101 while (frameLeft) {
2102 unsigned short c;
2103 if (bal < 0) {
2104 if (userCount == 0)
2105 break;
2106 if (get_user(data, up++))
2107 return -EFAULT;
2108 if (stereo) {
2109 if (get_user(c, up++))
2110 return -EFAULT;
2111 data = (data << 16) + c;
2112 } else
2113 data = (data << 16) + data;
2114 userCount--;
2115 bal += hSpeed;
2117 *p++ = data;
2118 frameLeft--;
2119 bal -= sSpeed;
2121 sound.bal = bal;
2122 sound.data = data;
2123 *frameUsed += (ftotal - frameLeft) * 4;
2124 utotal -= userCount;
2125 return stereo? utotal * 4: utotal * 2;
2129 static ssize_t pmac_ctx_u16(const u_char *userPtr, size_t userCount,
2130 u_char frame[], ssize_t *frameUsed,
2131 ssize_t frameLeft)
2133 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
2134 unsigned int *p = (unsigned int *) &frame[*frameUsed];
2135 unsigned int data = sound.data;
2136 unsigned short *up = (unsigned short *) userPtr;
2137 int bal = sound.bal;
2138 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
2139 int stereo = sound.soft.stereo;
2140 int utotal, ftotal;
2142 frameLeft >>= 2;
2143 userCount >>= (stereo? 2: 1);
2144 ftotal = frameLeft;
2145 utotal = userCount;
2146 while (frameLeft) {
2147 unsigned short c;
2148 if (bal < 0) {
2149 if (userCount == 0)
2150 break;
2151 if (get_user(data, up++))
2152 return -EFAULT;
2153 data ^= mask;
2154 if (stereo) {
2155 if (get_user(c, up++))
2156 return -EFAULT;
2157 data = (data << 16) + (c ^ mask);
2158 } else
2159 data = (data << 16) + data;
2160 userCount--;
2161 bal += hSpeed;
2163 *p++ = data;
2164 frameLeft--;
2165 bal -= sSpeed;
2167 sound.bal = bal;
2168 sound.data = data;
2169 *frameUsed += (ftotal - frameLeft) * 4;
2170 utotal -= userCount;
2171 return stereo? utotal * 4: utotal * 2;
2174 #endif /* CONFIG_PPC */
2177 #ifdef CONFIG_ATARI
2178 static TRANS transTTNormal = {
2179 ata_ct_law, ata_ct_law, ata_ct_s8, ata_ct_u8, NULL, NULL, NULL, NULL
2182 static TRANS transTTExpanding = {
2183 ata_ctx_law, ata_ctx_law, ata_ctx_s8, ata_ctx_u8, NULL, NULL, NULL, NULL
2186 static TRANS transFalconNormal = {
2187 ata_ct_law, ata_ct_law, ata_ct_s8, ata_ct_u8,
2188 ata_ct_s16be, ata_ct_u16be, ata_ct_s16le, ata_ct_u16le
2191 static TRANS transFalconExpanding = {
2192 ata_ctx_law, ata_ctx_law, ata_ctx_s8, ata_ctx_u8,
2193 ata_ctx_s16be, ata_ctx_u16be, ata_ctx_s16le, ata_ctx_u16le
2195 #endif /* CONFIG_ATARI */
2197 #ifdef CONFIG_AMIGA
2198 static TRANS transAmiga = {
2199 ami_ct_law, ami_ct_law, ami_ct_s8, ami_ct_u8,
2200 ami_ct_s16be, ami_ct_u16be, ami_ct_s16le, ami_ct_u16le
2202 #endif /* CONFIG_AMIGA */
2204 #ifdef CONFIG_PPC
2205 static TRANS transAwacsNormal = {
2206 pmac_ct_law, pmac_ct_law, pmac_ct_s8, pmac_ct_u8,
2207 pmac_ct_s16, pmac_ct_u16, pmac_ct_s16, pmac_ct_u16
2210 static TRANS transAwacsExpand = {
2211 pmac_ctx_law, pmac_ctx_law, pmac_ctx_s8, pmac_ctx_u8,
2212 pmac_ctx_s16, pmac_ctx_u16, pmac_ctx_s16, pmac_ctx_u16
2214 #endif /* CONFIG_PPC */
2216 /*** Low level stuff *********************************************************/
2219 #ifdef CONFIG_ATARI
2222 * Atari (TT/Falcon)
2225 static void *AtaAlloc(unsigned int size, int flags)
2227 return( atari_stram_alloc( size, NULL, "dmasound" ));
2230 static void AtaFree(void *obj, unsigned int size)
2232 atari_stram_free( obj );
2235 static int __init AtaIrqInit(void)
2237 /* Set up timer A. Timer A
2238 will receive a signal upon end of playing from the sound
2239 hardware. Furthermore Timer A is able to count events
2240 and will cause an interrupt after a programmed number
2241 of events. So all we need to keep the music playing is
2242 to provide the sound hardware with new data upon
2243 an interrupt from timer A. */
2244 mfp.tim_ct_a = 0; /* ++roman: Stop timer before programming! */
2245 mfp.tim_dt_a = 1; /* Cause interrupt after first event. */
2246 mfp.tim_ct_a = 8; /* Turn on event counting. */
2247 /* Register interrupt handler. */
2248 request_irq(IRQ_MFP_TIMA, ata_sq_interrupt, IRQ_TYPE_SLOW,
2249 "DMA sound", ata_sq_interrupt);
2250 mfp.int_en_a |= 0x20; /* Turn interrupt on. */
2251 mfp.int_mk_a |= 0x20;
2252 return(1);
2255 #ifdef MODULE
2256 static void AtaIrqCleanUp(void)
2258 mfp.tim_ct_a = 0; /* stop timer */
2259 mfp.int_en_a &= ~0x20; /* turn interrupt off */
2260 free_irq(IRQ_MFP_TIMA, ata_sq_interrupt);
2262 #endif /* MODULE */
2265 #define TONE_VOXWARE_TO_DB(v) \
2266 (((v) < 0) ? -12 : ((v) > 100) ? 12 : ((v) - 50) * 6 / 25)
2267 #define TONE_DB_TO_VOXWARE(v) (((v) * 25 + ((v) > 0 ? 5 : -5)) / 6 + 50)
2270 static int AtaSetBass(int bass)
2272 sound.bass = TONE_VOXWARE_TO_DB(bass);
2273 atari_microwire_cmd(MW_LM1992_BASS(sound.bass));
2274 return(TONE_DB_TO_VOXWARE(sound.bass));
2278 static int AtaSetTreble(int treble)
2280 sound.treble = TONE_VOXWARE_TO_DB(treble);
2281 atari_microwire_cmd(MW_LM1992_TREBLE(sound.treble));
2282 return(TONE_DB_TO_VOXWARE(sound.treble));
2288 * TT
2292 static void TTSilence(void)
2294 tt_dmasnd.ctrl = DMASND_CTRL_OFF;
2295 atari_microwire_cmd(MW_LM1992_PSG_HIGH); /* mix in PSG signal 1:1 */
2299 static void TTInit(void)
2301 int mode, i, idx;
2302 const int freq[4] = {50066, 25033, 12517, 6258};
2304 /* search a frequency that fits into the allowed error range */
2306 idx = -1;
2307 for (i = 0; i < arraysize(freq); i++)
2308 /* this isn't as much useful for a TT than for a Falcon, but
2309 * then it doesn't hurt very much to implement it for a TT too.
2311 if ((100 * abs(sound.soft.speed - freq[i]) / freq[i]) < catchRadius)
2312 idx = i;
2313 if (idx > -1) {
2314 sound.soft.speed = freq[idx];
2315 sound.trans = &transTTNormal;
2316 } else
2317 sound.trans = &transTTExpanding;
2319 TTSilence();
2320 sound.hard = sound.soft;
2322 if (sound.hard.speed > 50066) {
2323 /* we would need to squeeze the sound, but we won't do that */
2324 sound.hard.speed = 50066;
2325 mode = DMASND_MODE_50KHZ;
2326 sound.trans = &transTTNormal;
2327 } else if (sound.hard.speed > 25033) {
2328 sound.hard.speed = 50066;
2329 mode = DMASND_MODE_50KHZ;
2330 } else if (sound.hard.speed > 12517) {
2331 sound.hard.speed = 25033;
2332 mode = DMASND_MODE_25KHZ;
2333 } else if (sound.hard.speed > 6258) {
2334 sound.hard.speed = 12517;
2335 mode = DMASND_MODE_12KHZ;
2336 } else {
2337 sound.hard.speed = 6258;
2338 mode = DMASND_MODE_6KHZ;
2341 tt_dmasnd.mode = (sound.hard.stereo ?
2342 DMASND_MODE_STEREO : DMASND_MODE_MONO) |
2343 DMASND_MODE_8BIT | mode;
2345 sound.bal = -sound.soft.speed;
2349 static int TTSetFormat(int format)
2351 /* TT sound DMA supports only 8bit modes */
2353 switch (format) {
2354 case AFMT_QUERY:
2355 return(sound.soft.format);
2356 case AFMT_MU_LAW:
2357 case AFMT_A_LAW:
2358 case AFMT_S8:
2359 case AFMT_U8:
2360 break;
2361 default:
2362 format = AFMT_S8;
2365 sound.soft.format = format;
2366 sound.soft.size = 8;
2367 if (sound.minDev == SND_DEV_DSP) {
2368 sound.dsp.format = format;
2369 sound.dsp.size = 8;
2371 TTInit();
2373 return(format);
2377 #define VOLUME_VOXWARE_TO_DB(v) \
2378 (((v) < 0) ? -40 : ((v) > 100) ? 0 : ((v) * 2) / 5 - 40)
2379 #define VOLUME_DB_TO_VOXWARE(v) ((((v) + 40) * 5 + 1) / 2)
2382 static int TTSetVolume(int volume)
2384 sound.volume_left = VOLUME_VOXWARE_TO_DB(volume & 0xff);
2385 atari_microwire_cmd(MW_LM1992_BALLEFT(sound.volume_left));
2386 sound.volume_right = VOLUME_VOXWARE_TO_DB((volume & 0xff00) >> 8);
2387 atari_microwire_cmd(MW_LM1992_BALRIGHT(sound.volume_right));
2388 return(VOLUME_DB_TO_VOXWARE(sound.volume_left) |
2389 (VOLUME_DB_TO_VOXWARE(sound.volume_right) << 8));
2393 #define GAIN_VOXWARE_TO_DB(v) \
2394 (((v) < 0) ? -80 : ((v) > 100) ? 0 : ((v) * 4) / 5 - 80)
2395 #define GAIN_DB_TO_VOXWARE(v) ((((v) + 80) * 5 + 1) / 4)
2397 static int TTSetGain(int gain)
2399 sound.gain = GAIN_VOXWARE_TO_DB(gain);
2400 atari_microwire_cmd(MW_LM1992_VOLUME(sound.gain));
2401 return GAIN_DB_TO_VOXWARE(sound.gain);
2407 * Falcon
2411 static void FalconSilence(void)
2413 /* stop playback, set sample rate 50kHz for PSG sound */
2414 tt_dmasnd.ctrl = DMASND_CTRL_OFF;
2415 tt_dmasnd.mode = DMASND_MODE_50KHZ | DMASND_MODE_STEREO | DMASND_MODE_8BIT;
2416 tt_dmasnd.int_div = 0; /* STE compatible divider */
2417 tt_dmasnd.int_ctrl = 0x0;
2418 tt_dmasnd.cbar_src = 0x0000; /* no matrix inputs */
2419 tt_dmasnd.cbar_dst = 0x0000; /* no matrix outputs */
2420 tt_dmasnd.dac_src = 1; /* connect ADC to DAC, disconnect matrix */
2421 tt_dmasnd.adc_src = 3; /* ADC Input = PSG */
2425 static void FalconInit(void)
2427 int divider, i, idx;
2428 const int freq[8] = {49170, 32780, 24585, 19668, 16390, 12292, 9834, 8195};
2430 /* search a frequency that fits into the allowed error range */
2432 idx = -1;
2433 for (i = 0; i < arraysize(freq); i++)
2434 /* if we will tolerate 3% error 8000Hz->8195Hz (2.38%) would
2435 * be playable without expanding, but that now a kernel runtime
2436 * option
2438 if ((100 * abs(sound.soft.speed - freq[i]) / freq[i]) < catchRadius)
2439 idx = i;
2440 if (idx > -1) {
2441 sound.soft.speed = freq[idx];
2442 sound.trans = &transFalconNormal;
2443 } else
2444 sound.trans = &transFalconExpanding;
2446 FalconSilence();
2447 sound.hard = sound.soft;
2449 if (sound.hard.size == 16) {
2450 /* the Falcon can play 16bit samples only in stereo */
2451 sound.hard.stereo = 1;
2454 if (sound.hard.speed > 49170) {
2455 /* we would need to squeeze the sound, but we won't do that */
2456 sound.hard.speed = 49170;
2457 divider = 1;
2458 sound.trans = &transFalconNormal;
2459 } else if (sound.hard.speed > 32780) {
2460 sound.hard.speed = 49170;
2461 divider = 1;
2462 } else if (sound.hard.speed > 24585) {
2463 sound.hard.speed = 32780;
2464 divider = 2;
2465 } else if (sound.hard.speed > 19668) {
2466 sound.hard.speed = 24585;
2467 divider = 3;
2468 } else if (sound.hard.speed > 16390) {
2469 sound.hard.speed = 19668;
2470 divider = 4;
2471 } else if (sound.hard.speed > 12292) {
2472 sound.hard.speed = 16390;
2473 divider = 5;
2474 } else if (sound.hard.speed > 9834) {
2475 sound.hard.speed = 12292;
2476 divider = 7;
2477 } else if (sound.hard.speed > 8195) {
2478 sound.hard.speed = 9834;
2479 divider = 9;
2480 } else {
2481 sound.hard.speed = 8195;
2482 divider = 11;
2484 tt_dmasnd.int_div = divider;
2486 /* Setup Falcon sound DMA for playback */
2487 tt_dmasnd.int_ctrl = 0x4; /* Timer A int at play end */
2488 tt_dmasnd.track_select = 0x0; /* play 1 track, track 1 */
2489 tt_dmasnd.cbar_src = 0x0001; /* DMA(25MHz) --> DAC */
2490 tt_dmasnd.cbar_dst = 0x0000;
2491 tt_dmasnd.rec_track_select = 0;
2492 tt_dmasnd.dac_src = 2; /* connect matrix to DAC */
2493 tt_dmasnd.adc_src = 0; /* ADC Input = Mic */
2495 tt_dmasnd.mode = (sound.hard.stereo ?
2496 DMASND_MODE_STEREO : DMASND_MODE_MONO) |
2497 ((sound.hard.size == 8) ?
2498 DMASND_MODE_8BIT : DMASND_MODE_16BIT) |
2499 DMASND_MODE_6KHZ;
2501 sound.bal = -sound.soft.speed;
2505 static int FalconSetFormat(int format)
2507 int size;
2508 /* Falcon sound DMA supports 8bit and 16bit modes */
2510 switch (format) {
2511 case AFMT_QUERY:
2512 return(sound.soft.format);
2513 case AFMT_MU_LAW:
2514 case AFMT_A_LAW:
2515 case AFMT_U8:
2516 case AFMT_S8:
2517 size = 8;
2518 break;
2519 case AFMT_S16_BE:
2520 case AFMT_U16_BE:
2521 case AFMT_S16_LE:
2522 case AFMT_U16_LE:
2523 size = 16;
2524 break;
2525 default: /* :-) */
2526 size = 8;
2527 format = AFMT_S8;
2530 sound.soft.format = format;
2531 sound.soft.size = size;
2532 if (sound.minDev == SND_DEV_DSP) {
2533 sound.dsp.format = format;
2534 sound.dsp.size = sound.soft.size;
2537 FalconInit();
2539 return(format);
2543 /* This is for the Falcon output *attenuation* in 1.5dB steps,
2544 * i.e. output level from 0 to -22.5dB in -1.5dB steps.
2546 #define VOLUME_VOXWARE_TO_ATT(v) \
2547 ((v) < 0 ? 15 : (v) > 100 ? 0 : 15 - (v) * 3 / 20)
2548 #define VOLUME_ATT_TO_VOXWARE(v) (100 - (v) * 20 / 3)
2551 static int FalconSetVolume(int volume)
2553 sound.volume_left = VOLUME_VOXWARE_TO_ATT(volume & 0xff);
2554 sound.volume_right = VOLUME_VOXWARE_TO_ATT((volume & 0xff00) >> 8);
2555 tt_dmasnd.output_atten = sound.volume_left << 8 | sound.volume_right << 4;
2556 return(VOLUME_ATT_TO_VOXWARE(sound.volume_left) |
2557 VOLUME_ATT_TO_VOXWARE(sound.volume_right) << 8);
2561 static void ata_sq_play_next_frame(int index)
2563 char *start, *end;
2565 /* used by AtaPlay() if all doubts whether there really is something
2566 * to be played are already wiped out.
2568 start = sq_block_address(sq.front);
2569 end = start+((sq.count == index) ? sq.rear_size : sq.block_size);
2570 /* end might not be a legal virtual address. */
2571 DMASNDSetEnd(VTOP(end - 1) + 1);
2572 DMASNDSetBase(VTOP(start));
2573 /* Since only an even number of samples per frame can
2574 be played, we might lose one byte here. (TO DO) */
2575 sq.front = (sq.front+1) % sq.max_count;
2576 sq.playing++;
2577 tt_dmasnd.ctrl = DMASND_CTRL_ON | DMASND_CTRL_REPEAT;
2581 static void AtaPlay(void)
2583 /* ++TeSche: Note that sq.playing is no longer just a flag but holds
2584 * the number of frames the DMA is currently programmed for instead,
2585 * may be 0, 1 (currently being played) or 2 (pre-programmed).
2587 * Changes done to sq.count and sq.playing are a bit more subtle again
2588 * so now I must admit I also prefer disabling the irq here rather
2589 * than considering all possible situations. But the point is that
2590 * disabling the irq doesn't have any bad influence on this version of
2591 * the driver as we benefit from having pre-programmed the DMA
2592 * wherever possible: There's no need to reload the DMA at the exact
2593 * time of an interrupt but only at some time while the pre-programmed
2594 * frame is playing!
2596 atari_disable_irq(IRQ_MFP_TIMA);
2598 if (sq.playing == 2 || /* DMA is 'full' */
2599 sq.count <= 0) { /* nothing to do */
2600 atari_enable_irq(IRQ_MFP_TIMA);
2601 return;
2604 if (sq.playing == 0) {
2605 /* looks like there's nothing 'in' the DMA yet, so try
2606 * to put two frames into it (at least one is available).
2608 if (sq.count == 1 && sq.rear_size < sq.block_size && !sq.syncing) {
2609 /* hmmm, the only existing frame is not
2610 * yet filled and we're not syncing?
2612 atari_enable_irq(IRQ_MFP_TIMA);
2613 return;
2615 ata_sq_play_next_frame(1);
2616 if (sq.count == 1) {
2617 /* no more frames */
2618 atari_enable_irq(IRQ_MFP_TIMA);
2619 return;
2621 if (sq.count == 2 && sq.rear_size < sq.block_size && !sq.syncing) {
2622 /* hmmm, there were two frames, but the second
2623 * one is not yet filled and we're not syncing?
2625 atari_enable_irq(IRQ_MFP_TIMA);
2626 return;
2628 ata_sq_play_next_frame(2);
2629 } else {
2630 /* there's already a frame being played so we may only stuff
2631 * one new into the DMA, but even if this may be the last
2632 * frame existing the previous one is still on sq.count.
2634 if (sq.count == 2 && sq.rear_size < sq.block_size && !sq.syncing) {
2635 /* hmmm, the only existing frame is not
2636 * yet filled and we're not syncing?
2638 atari_enable_irq(IRQ_MFP_TIMA);
2639 return;
2641 ata_sq_play_next_frame(2);
2643 atari_enable_irq(IRQ_MFP_TIMA);
2647 static void ata_sq_interrupt(int irq, void *dummy, struct pt_regs *fp)
2649 #if 0
2650 /* ++TeSche: if you should want to test this... */
2651 static int cnt = 0;
2652 if (sq.playing == 2)
2653 if (++cnt == 10) {
2654 /* simulate losing an interrupt */
2655 cnt = 0;
2656 return;
2658 #endif
2660 if (sq.ignore_int && (sound.mach.type == DMASND_FALCON)) {
2661 /* ++TeSche: Falcon only: ignore first irq because it comes
2662 * immediately after starting a frame. after that, irqs come
2663 * (almost) like on the TT.
2665 sq.ignore_int = 0;
2666 return;
2669 if (!sq.playing) {
2670 /* playing was interrupted and sq_reset() has already cleared
2671 * the sq variables, so better don't do anything here.
2673 WAKE_UP(sq.sync_queue);
2674 return;
2677 /* Probably ;) one frame is finished. Well, in fact it may be that a
2678 * pre-programmed one is also finished because there has been a long
2679 * delay in interrupt delivery and we've completely lost one, but
2680 * there's no way to detect such a situation. In such a case the last
2681 * frame will be played more than once and the situation will recover
2682 * as soon as the irq gets through.
2684 sq.count--;
2685 sq.playing--;
2687 if (!sq.playing) {
2688 tt_dmasnd.ctrl = DMASND_CTRL_OFF;
2689 sq.ignore_int = 1;
2692 WAKE_UP(sq.write_queue);
2693 /* At least one block of the queue is free now
2694 so wake up a writing process blocked because
2695 of a full queue. */
2697 if ((sq.playing != 1) || (sq.count != 1))
2698 /* We must be a bit carefully here: sq.count indicates the
2699 * number of buffers used and not the number of frames to
2700 * be played. If sq.count==1 and sq.playing==1 that means
2701 * the only remaining frame was already programmed earlier
2702 * (and is currently running) so we mustn't call AtaPlay()
2703 * here, otherwise we'll play one frame too much.
2705 AtaPlay();
2707 if (!sq.playing) WAKE_UP(sq.sync_queue);
2708 /* We are not playing after AtaPlay(), so there
2709 is nothing to play any more. Wake up a process
2710 waiting for audio output to drain. */
2712 #endif /* CONFIG_ATARI */
2715 #ifdef CONFIG_AMIGA
2718 * Amiga
2722 static void *AmiAlloc(unsigned int size, int flags)
2724 return(amiga_chip_alloc((long)size));
2727 static void AmiFree(void *obj, unsigned int size)
2729 amiga_chip_free (obj);
2732 static int __init AmiIrqInit(void)
2734 /* turn off DMA for audio channels */
2735 custom.dmacon = AMI_AUDIO_OFF;
2737 /* Register interrupt handler. */
2738 if (request_irq(IRQ_AMIGA_AUD0, ami_sq_interrupt, 0,
2739 "DMA sound", ami_sq_interrupt))
2740 return(0);
2741 return(1);
2744 #ifdef MODULE
2745 static void AmiIrqCleanUp(void)
2747 /* turn off DMA for audio channels */
2748 custom.dmacon = AMI_AUDIO_OFF;
2749 /* release the interrupt */
2750 free_irq(IRQ_AMIGA_AUD0, ami_sq_interrupt);
2752 #endif /* MODULE */
2754 static void AmiSilence(void)
2756 /* turn off DMA for audio channels */
2757 custom.dmacon = AMI_AUDIO_OFF;
2761 static void AmiInit(void)
2763 int period, i;
2765 AmiSilence();
2767 if (sound.soft.speed)
2768 period = amiga_colorclock/sound.soft.speed-1;
2769 else
2770 period = amiga_audio_min_period;
2771 sound.hard = sound.soft;
2772 sound.trans = &transAmiga;
2774 if (period < amiga_audio_min_period) {
2775 /* we would need to squeeze the sound, but we won't do that */
2776 period = amiga_audio_min_period;
2777 } else if (period > 65535) {
2778 period = 65535;
2780 sound.hard.speed = amiga_colorclock/(period+1);
2782 for (i = 0; i < 4; i++)
2783 custom.aud[i].audper = period;
2784 amiga_audio_period = period;
2786 AmiSetTreble(50); /* recommended for newer amiga models */
2790 static int AmiSetFormat(int format)
2792 int size;
2794 /* Amiga sound DMA supports 8bit and 16bit (pseudo 14 bit) modes */
2796 switch (format) {
2797 case AFMT_QUERY:
2798 return(sound.soft.format);
2799 case AFMT_MU_LAW:
2800 case AFMT_A_LAW:
2801 case AFMT_U8:
2802 case AFMT_S8:
2803 size = 8;
2804 break;
2805 case AFMT_S16_BE:
2806 case AFMT_U16_BE:
2807 case AFMT_S16_LE:
2808 case AFMT_U16_LE:
2809 size = 16;
2810 break;
2811 default: /* :-) */
2812 size = 8;
2813 format = AFMT_S8;
2816 sound.soft.format = format;
2817 sound.soft.size = size;
2818 if (sound.minDev == SND_DEV_DSP) {
2819 sound.dsp.format = format;
2820 sound.dsp.size = sound.soft.size;
2822 AmiInit();
2824 return(format);
2828 #define VOLUME_VOXWARE_TO_AMI(v) \
2829 (((v) < 0) ? 0 : ((v) > 100) ? 64 : ((v) * 64)/100)
2830 #define VOLUME_AMI_TO_VOXWARE(v) ((v)*100/64)
2832 static int AmiSetVolume(int volume)
2834 sound.volume_left = VOLUME_VOXWARE_TO_AMI(volume & 0xff);
2835 custom.aud[0].audvol = sound.volume_left;
2836 sound.volume_right = VOLUME_VOXWARE_TO_AMI((volume & 0xff00) >> 8);
2837 custom.aud[1].audvol = sound.volume_right;
2838 return(VOLUME_AMI_TO_VOXWARE(sound.volume_left) |
2839 (VOLUME_AMI_TO_VOXWARE(sound.volume_right) << 8));
2842 static int AmiSetTreble(int treble)
2844 sound.treble = treble;
2845 if (treble < 50)
2846 ciaa.pra &= ~0x02;
2847 else
2848 ciaa.pra |= 0x02;
2849 return(treble);
2853 #define AMI_PLAY_LOADED 1
2854 #define AMI_PLAY_PLAYING 2
2855 #define AMI_PLAY_MASK 3
2858 static void ami_sq_play_next_frame(int index)
2860 u_char *start, *ch0, *ch1, *ch2, *ch3;
2861 u_long size;
2863 /* used by AmiPlay() if all doubts whether there really is something
2864 * to be played are already wiped out.
2866 start = sq_block_address(sq.front);
2867 size = (sq.count == index ? sq.rear_size : sq.block_size)>>1;
2869 if (sound.hard.stereo) {
2870 ch0 = start;
2871 ch1 = start+sq.block_size_half;
2872 size >>= 1;
2873 } else {
2874 ch0 = start;
2875 ch1 = start;
2877 if (sound.hard.size == 8) {
2878 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
2879 custom.aud[0].audlen = size;
2880 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
2881 custom.aud[1].audlen = size;
2882 custom.dmacon = AMI_AUDIO_8;
2883 } else {
2884 size >>= 1;
2885 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
2886 custom.aud[0].audlen = size;
2887 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
2888 custom.aud[1].audlen = size;
2889 if (sound.volume_left == 64 && sound.volume_right == 64) {
2890 /* We can play pseudo 14-bit only with the maximum volume */
2891 ch3 = ch0+sq.block_size_quarter;
2892 ch2 = ch1+sq.block_size_quarter;
2893 custom.aud[2].audvol = 1; /* we are being affected by the beeps */
2894 custom.aud[3].audvol = 1; /* restoring volume here helps a bit */
2895 custom.aud[2].audlc = (u_short *)ZTWO_PADDR(ch2);
2896 custom.aud[2].audlen = size;
2897 custom.aud[3].audlc = (u_short *)ZTWO_PADDR(ch3);
2898 custom.aud[3].audlen = size;
2899 custom.dmacon = AMI_AUDIO_14;
2900 } else
2901 custom.dmacon = AMI_AUDIO_8;
2903 sq.front = (sq.front+1) % sq.max_count;
2904 sq.playing |= AMI_PLAY_LOADED;
2908 static void AmiPlay(void)
2910 int minframes = 1;
2912 custom.intena = IF_AUD0;
2914 if (sq.playing & AMI_PLAY_LOADED) {
2915 /* There's already a frame loaded */
2916 custom.intena = IF_SETCLR | IF_AUD0;
2917 return;
2920 if (sq.playing & AMI_PLAY_PLAYING)
2921 /* Increase threshold: frame 1 is already being played */
2922 minframes = 2;
2924 if (sq.count < minframes) {
2925 /* Nothing to do */
2926 custom.intena = IF_SETCLR | IF_AUD0;
2927 return;
2930 if (sq.count <= minframes && sq.rear_size < sq.block_size && !sq.syncing) {
2931 /* hmmm, the only existing frame is not
2932 * yet filled and we're not syncing?
2934 custom.intena = IF_SETCLR | IF_AUD0;
2935 return;
2938 ami_sq_play_next_frame(minframes);
2940 custom.intena = IF_SETCLR | IF_AUD0;
2944 static void ami_sq_interrupt(int irq, void *dummy, struct pt_regs *fp)
2946 int minframes = 1;
2948 if (!sq.playing) {
2949 /* Playing was interrupted and sq_reset() has already cleared
2950 * the sq variables, so better don't do anything here.
2952 WAKE_UP(sq.sync_queue);
2953 return;
2956 if (sq.playing & AMI_PLAY_PLAYING) {
2957 /* We've just finished a frame */
2958 sq.count--;
2959 WAKE_UP(sq.write_queue);
2962 if (sq.playing & AMI_PLAY_LOADED)
2963 /* Increase threshold: frame 1 is already being played */
2964 minframes = 2;
2966 /* Shift the flags */
2967 sq.playing = (sq.playing<<1) & AMI_PLAY_MASK;
2969 if (!sq.playing)
2970 /* No frame is playing, disable audio DMA */
2971 custom.dmacon = AMI_AUDIO_OFF;
2973 if (sq.count >= minframes)
2974 /* Try to play the next frame */
2975 AmiPlay();
2977 if (!sq.playing)
2978 /* Nothing to play anymore.
2979 Wake up a process waiting for audio output to drain. */
2980 WAKE_UP(sq.sync_queue);
2982 #endif /* CONFIG_AMIGA */
2984 #ifdef CONFIG_PPC
2987 * PCI PowerMac, with AWACS and DBDMA.
2990 static void *PMacAlloc(unsigned int size, int flags)
2992 return kmalloc(size, flags);
2995 static void PMacFree(void *ptr, unsigned int size)
2997 kfree(ptr);
3000 static int __init PMacIrqInit(void)
3002 if (request_irq(awacs_irq, pmac_awacs_intr, 0, "AWACS", 0)
3003 || request_irq(awacs_tx_irq, pmac_awacs_tx_intr, 0, "AWACS out", 0))
3004 return 0;
3005 return 1;
3008 #ifdef MODULE
3009 static void PMacIrqCleanup(void)
3011 /* turn off output dma */
3012 out_le32(&awacs_txdma->control, RUN<<16);
3013 /* disable interrupts from awacs interface */
3014 out_le32(&awacs->control, in_le32(&awacs->control) & 0xfff);
3015 free_irq(awacs_irq, pmac_awacs_intr);
3016 free_irq(awacs_tx_irq, pmac_awacs_tx_intr);
3017 kfree(awacs_tx_cmd_space);
3018 if (beep_buf)
3019 kfree(beep_buf);
3020 kd_mksound = orig_mksound;
3021 #ifdef CONFIG_PMAC_PBOOK
3022 notifier_chain_unregister(&sleep_notifier_list, &awacs_sleep_notifier);
3023 #endif
3025 #endif /* MODULE */
3027 static void PMacSilence(void)
3029 /* turn off output dma */
3030 out_le32(&awacs_txdma->control, RUN<<16);
3033 static int awacs_freqs[8] = {
3034 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
3037 static void PMacInit(void)
3039 int i, tolerance;
3041 switch (sound.soft.format) {
3042 case AFMT_S16_LE:
3043 case AFMT_U16_LE:
3044 sound.hard.format = AFMT_S16_LE;
3045 break;
3046 default:
3047 sound.hard.format = AFMT_S16_BE;
3048 break;
3050 sound.hard.stereo = 1;
3051 sound.hard.size = 16;
3054 * If we have a sample rate which is within catchRadius percent
3055 * of the requested value, we don't have to expand the samples.
3056 * Otherwise choose the next higher rate.
3057 * N.B.: burgundy awacs (iMac and later) only works at 44100 Hz.
3059 i = (awacs_revision >= AWACS_BURGUNDY)? 1: 8;
3060 do {
3061 tolerance = catchRadius * awacs_freqs[--i] / 100;
3062 } while (sound.soft.speed > awacs_freqs[i] + tolerance && i > 0);
3063 if (sound.soft.speed >= awacs_freqs[i] - tolerance)
3064 sound.trans = &transAwacsNormal;
3065 else
3066 sound.trans = &transAwacsExpand;
3067 sound.hard.speed = awacs_freqs[i];
3068 awacs_rate_index = i;
3070 PMacSilence();
3071 /* XXX disable error interrupt on burgundy for now */
3072 out_le32(&awacs->control, MASK_IEPC | (i << 8) | 0x11
3073 | (awacs_revision < AWACS_BURGUNDY? MASK_IEE: 0));
3074 awacs_reg[1] = (awacs_reg[1] & ~MASK_SAMPLERATE) | (i << 3);
3075 awacs_write(awacs_reg[1] | MASK_ADDR1);
3076 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
3078 sound.bal = -sound.soft.speed;
3081 static int PMacSetFormat(int format)
3083 int size;
3085 switch (format) {
3086 case AFMT_QUERY:
3087 return sound.soft.format;
3088 case AFMT_MU_LAW:
3089 case AFMT_A_LAW:
3090 case AFMT_U8:
3091 case AFMT_S8:
3092 size = 8;
3093 break;
3094 case AFMT_S16_BE:
3095 case AFMT_U16_BE:
3096 case AFMT_S16_LE:
3097 case AFMT_U16_LE:
3098 size = 16;
3099 break;
3100 default: /* :-) */
3101 printk(KERN_ERR "dmasound: unknown format 0x%x, using AFMT_U8\n",
3102 format);
3103 size = 8;
3104 format = AFMT_U8;
3107 sound.soft.format = format;
3108 sound.soft.size = size;
3109 if (sound.minDev == SND_DEV_DSP) {
3110 sound.dsp.format = format;
3111 sound.dsp.size = size;
3114 PMacInit();
3116 return format;
3119 #define AWACS_VOLUME_TO_MASK(x) (15 - ((((x) - 1) * 15) / 99))
3120 #define AWACS_MASK_TO_VOLUME(y) (100 - ((y) * 99 / 15))
3122 static int awacs_get_volume(int reg, int lshift)
3124 int volume;
3126 volume = AWACS_MASK_TO_VOLUME((reg >> lshift) & 0xf);
3127 volume |= AWACS_MASK_TO_VOLUME(reg & 0xf) << 8;
3128 return volume;
3131 static int awacs_volume_setter(int volume, int n, int mute, int lshift)
3133 int r1, rn;
3135 if (mute && volume == 0) {
3136 r1 = awacs_reg[1] | mute;
3137 } else {
3138 r1 = awacs_reg[1] & ~mute;
3139 rn = awacs_reg[n] & ~(0xf | (0xf << lshift));
3140 rn |= ((AWACS_VOLUME_TO_MASK(volume & 0xff) & 0xf) << lshift);
3141 rn |= AWACS_VOLUME_TO_MASK((volume >> 8) & 0xff) & 0xf;
3142 awacs_reg[n] = rn;
3143 awacs_write((n << 12) | rn);
3144 volume = awacs_get_volume(rn, lshift);
3146 if (r1 != awacs_reg[1]) {
3147 awacs_reg[1] = r1;
3148 awacs_write(r1 | MASK_ADDR1);
3150 return volume;
3153 static int PMacSetVolume(int volume)
3155 return awacs_volume_setter(volume, 2, MASK_AMUTE, 6);
3158 static void PMacPlay(void)
3160 volatile struct dbdma_cmd *cp;
3161 int i, count;
3162 unsigned long flags;
3164 save_flags(flags); cli();
3165 if (beep_playing) {
3166 /* sound takes precedence over beeps */
3167 out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
3168 out_le32(&awacs->control,
3169 (in_le32(&awacs->control) & ~0x1f00)
3170 || (awacs_rate_index << 8));
3171 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
3172 beep_playing = 0;
3174 i = sq.front + sq.playing;
3175 if (i >= sq.max_count)
3176 i -= sq.max_count;
3177 while (sq.playing < 2 && sq.playing < sq.count) {
3178 count = (sq.count == sq.playing + 1)? sq.rear_size: sq.block_size;
3179 if (count < sq.block_size && !sq.syncing)
3180 /* last block not yet filled, and we're not syncing. */
3181 break;
3182 cp = &awacs_tx_cmds[i];
3183 st_le16(&cp->req_count, count);
3184 st_le16(&cp->xfer_status, 0);
3185 if (++i >= sq.max_count)
3186 i = 0;
3187 out_le16(&awacs_tx_cmds[i].command, DBDMA_STOP);
3188 out_le16(&cp->command, OUTPUT_MORE + INTR_ALWAYS);
3189 if (sq.playing == 0)
3190 out_le32(&awacs_txdma->cmdptr, virt_to_bus(cp));
3191 out_le32(&awacs_txdma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
3192 ++sq.playing;
3194 restore_flags(flags);
3197 static void
3198 pmac_awacs_tx_intr(int irq, void *devid, struct pt_regs *regs)
3200 int i = sq.front;
3201 int stat;
3202 volatile struct dbdma_cmd *cp;
3204 while (sq.playing > 0) {
3205 cp = &awacs_tx_cmds[i];
3206 stat = ld_le16(&cp->xfer_status);
3207 if ((stat & ACTIVE) == 0)
3208 break; /* this frame is still going */
3209 --sq.count;
3210 --sq.playing;
3211 if (++i >= sq.max_count)
3212 i = 0;
3214 if (i != sq.front)
3215 WAKE_UP(sq.write_queue);
3216 sq.front = i;
3218 PMacPlay();
3220 if (!sq.playing)
3221 WAKE_UP(sq.sync_queue);
3224 static void
3225 pmac_awacs_intr(int irq, void *devid, struct pt_regs *regs)
3227 int ctrl = in_le32(&awacs->control);
3229 if (ctrl & MASK_PORTCHG) {
3230 /* do something when headphone is plugged/unplugged? */
3232 if (ctrl & MASK_CNTLERR) {
3233 int err = (in_le32(&awacs->codec_stat) & MASK_ERRCODE) >> 16;
3234 if (err != 0 && awacs_revision < AWACS_BURGUNDY)
3235 printk(KERN_ERR "AWACS: error %x\n", err);
3237 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
3238 out_le32(&awacs->control, ctrl);
3241 static void
3242 awacs_write(int val)
3244 if (awacs_revision >= AWACS_BURGUNDY)
3245 return;
3246 while (in_le32(&awacs->codec_ctrl) & MASK_NEWECMD)
3247 ; /* XXX should have timeout */
3248 out_le32(&awacs->codec_ctrl, val | (awacs_subframe << 22));
3251 static void awacs_nosound(unsigned long xx)
3253 unsigned long flags;
3255 save_flags(flags); cli();
3256 if (beep_playing) {
3257 st_le16(&beep_dbdma_cmd->command, DBDMA_STOP);
3258 beep_playing = 0;
3260 restore_flags(flags);
3263 static struct timer_list beep_timer = {
3264 NULL, NULL, 0, 0, awacs_nosound
3267 static void awacs_mksound(unsigned int hz, unsigned int ticks)
3269 unsigned long flags;
3270 int beep_speed = (awacs_revision < AWACS_BURGUNDY)? BEEP_SPEED: 0;
3271 int srate = awacs_freqs[beep_speed];
3272 int period, ncycles, nsamples;
3273 int i, j, f;
3274 short *p;
3275 static int beep_hz_cache;
3276 static int beep_nsamples_cache;
3277 static int beep_volume_cache;
3279 if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) {
3280 #if 1
3281 /* this is a hack for broken X server code */
3282 hz = 750;
3283 ticks = 12;
3284 #else
3285 /* cancel beep currently playing */
3286 awacs_nosound(0);
3287 return;
3288 #endif
3290 save_flags(flags); cli();
3291 del_timer(&beep_timer);
3292 if (ticks) {
3293 beep_timer.expires = jiffies + ticks;
3294 add_timer(&beep_timer);
3296 if (beep_playing || sq.playing || beep_buf == NULL) {
3297 restore_flags(flags);
3298 return; /* too hard, sorry :-( */
3300 beep_playing = 1;
3301 st_le16(&beep_dbdma_cmd->command, OUTPUT_MORE + BR_ALWAYS);
3302 restore_flags(flags);
3304 if (hz == beep_hz_cache && beep_volume == beep_volume_cache) {
3305 nsamples = beep_nsamples_cache;
3306 } else {
3307 period = srate * 256 / hz; /* fixed point */
3308 ncycles = BEEP_BUFLEN * 256 / period;
3309 nsamples = (period * ncycles) >> 8;
3310 f = ncycles * 65536 / nsamples;
3311 j = 0;
3312 p = beep_buf;
3313 for (i = 0; i < nsamples; ++i, p += 2) {
3314 p[0] = p[1] = beep_wform[j >> 8] * beep_volume;
3315 j = (j + f) & 0xffff;
3317 beep_hz_cache = hz;
3318 beep_volume_cache = beep_volume;
3319 beep_nsamples_cache = nsamples;
3322 st_le16(&beep_dbdma_cmd->req_count, nsamples*4);
3323 st_le16(&beep_dbdma_cmd->xfer_status, 0);
3324 st_le32(&beep_dbdma_cmd->cmd_dep, virt_to_bus(beep_dbdma_cmd));
3325 st_le32(&beep_dbdma_cmd->phy_addr, virt_to_bus(beep_buf));
3327 save_flags(flags); cli();
3328 if (beep_playing) { /* i.e. haven't been terminated already */
3329 out_le32(&awacs_txdma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
3330 out_le32(&awacs->control,
3331 (in_le32(&awacs->control) & ~0x1f00)
3332 | (beep_speed << 8));
3333 out_le32(&awacs->byteswap, 0);
3334 out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
3335 out_le32(&awacs_txdma->control, RUN | (RUN << 16));
3337 restore_flags(flags);
3340 #ifdef CONFIG_PMAC_PBOOK
3342 * Save state when going to sleep, restore it afterwards.
3344 static int awacs_sleep_notify(struct notifier_block *this,
3345 unsigned long code, void *x)
3347 switch (code) {
3348 case PBOOK_SLEEP:
3349 /* XXX we should stop any dma in progress when going to sleep
3350 and restart it when we wake. */
3351 PMacSilence();
3352 break;
3353 case PBOOK_WAKE:
3354 out_le32(&awacs->control, MASK_IEPC
3355 | (awacs_rate_index << 8) | 0x11
3356 | (awacs_revision < AWACS_BURGUNDY? MASK_IEE: 0));
3357 awacs_write(awacs_reg[0] | MASK_ADDR0);
3358 awacs_write(awacs_reg[1] | MASK_ADDR1);
3359 awacs_write(awacs_reg[2] | MASK_ADDR2);
3360 awacs_write(awacs_reg[4] | MASK_ADDR4);
3361 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
3363 return NOTIFY_DONE;
3365 #endif /* CONFIG_PMAC_PBOOK */
3368 /* All the burgundy functions: */
3370 /* Waits for busy flag to clear */
3371 inline static void
3372 awacs_burgundy_busy_wait(void)
3374 while (in_le32(&awacs->codec_ctrl) & MASK_NEWECMD)
3378 inline static void
3379 awacs_burgundy_extend_wait(void)
3381 while (!(in_le32(&awacs->codec_stat) & MASK_EXTEND))
3383 while (in_le32(&awacs->codec_stat) & MASK_EXTEND)
3387 static void
3388 awacs_burgundy_wcw(unsigned addr, unsigned val)
3390 out_le32(&awacs->codec_ctrl, addr + 0x200c00 + (val & 0xff));
3391 awacs_burgundy_busy_wait();
3392 out_le32(&awacs->codec_ctrl, addr + 0x200d00 +((val>>8) & 0xff));
3393 awacs_burgundy_busy_wait();
3394 out_le32(&awacs->codec_ctrl, addr + 0x200e00 +((val>>16) & 0xff));
3395 awacs_burgundy_busy_wait();
3396 out_le32(&awacs->codec_ctrl, addr + 0x200f00 +((val>>24) & 0xff));
3397 awacs_burgundy_busy_wait();
3400 static unsigned
3401 awacs_burgundy_rcw(unsigned addr)
3403 unsigned val = 0;
3404 int flags;
3406 /* should have timeouts here */
3407 save_flags(flags); cli();
3409 out_le32(&awacs->codec_ctrl, addr + 0x100000);
3410 awacs_burgundy_busy_wait();
3411 awacs_burgundy_extend_wait();
3412 val += (in_le32(&awacs->codec_stat) >> 4) & 0xff;
3414 out_le32(&awacs->codec_ctrl, addr + 0x100100);
3415 awacs_burgundy_busy_wait();
3416 awacs_burgundy_extend_wait();
3417 val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<8;
3419 out_le32(&awacs->codec_ctrl, addr + 0x100200);
3420 awacs_burgundy_busy_wait();
3421 awacs_burgundy_extend_wait();
3422 val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<16;
3424 out_le32(&awacs->codec_ctrl, addr + 0x100300);
3425 awacs_burgundy_busy_wait();
3426 awacs_burgundy_extend_wait();
3427 val += ((in_le32(&awacs->codec_stat)>>4) & 0xff) <<24;
3429 restore_flags(flags);
3431 return val;
3435 static void
3436 awacs_burgundy_wcb(unsigned addr, unsigned val)
3438 out_le32(&awacs->codec_ctrl, addr + 0x300000 + (val & 0xff));
3439 awacs_burgundy_busy_wait();
3442 static unsigned
3443 awacs_burgundy_rcb(unsigned addr)
3445 unsigned val = 0;
3446 int flags;
3448 /* should have timeouts here */
3449 save_flags(flags); cli();
3451 out_le32(&awacs->codec_ctrl, addr + 0x100000);
3452 awacs_burgundy_busy_wait();
3453 awacs_burgundy_extend_wait();
3454 val += (in_le32(&awacs->codec_stat) >> 4) & 0xff;
3456 restore_flags(flags);
3458 return val;
3461 static int
3462 awacs_burgundy_check(void)
3464 /* Checks to see the chip is alive and kicking */
3465 int error = in_le32(&awacs->codec_ctrl) & MASK_ERRCODE;
3467 return error == 0xf0000;
3470 static int
3471 awacs_burgundy_init(void)
3473 if (awacs_burgundy_check()) {
3474 printk(KERN_WARNING "AWACS: disabled by MacOS :-(\n");
3475 return 1;
3478 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_OUTPUTENABLES,
3479 DEF_BURGUNDY_OUTPUTENABLES);
3480 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
3481 DEF_BURGUNDY_MORE_OUTPUTENABLES);
3482 awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_OUTPUTSELECTS,
3483 DEF_BURGUNDY_OUTPUTSELECTS);
3485 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_INPSEL21,
3486 DEF_BURGUNDY_INPSEL21);
3487 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_INPSEL3,
3488 DEF_BURGUNDY_INPSEL3);
3489 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINCD,
3490 DEF_BURGUNDY_GAINCD);
3491 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINLINE,
3492 DEF_BURGUNDY_GAINLINE);
3493 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINMIC,
3494 DEF_BURGUNDY_GAINMIC);
3495 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_GAINMODEM,
3496 DEF_BURGUNDY_GAINMODEM);
3498 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER,
3499 DEF_BURGUNDY_ATTENSPEAKER);
3500 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENLINEOUT,
3501 DEF_BURGUNDY_ATTENLINEOUT);
3502 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENHP,
3503 DEF_BURGUNDY_ATTENHP);
3505 awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_MASTER_VOLUME,
3506 DEF_BURGUNDY_MASTER_VOLUME);
3507 awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLCD,
3508 DEF_BURGUNDY_VOLCD);
3509 awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLLINE,
3510 DEF_BURGUNDY_VOLLINE);
3511 awacs_burgundy_wcw(MASK_ADDR_BURGUNDY_VOLMIC,
3512 DEF_BURGUNDY_VOLMIC);
3513 return 0;
3516 static void
3517 awacs_burgundy_write_volume(unsigned address, int volume)
3519 int hardvolume,lvolume,rvolume;
3521 lvolume = (volume & 0xff) ? (volume & 0xff) + 155 : 0;
3522 rvolume = ((volume >>8)&0xff) ? ((volume >> 8)&0xff ) + 155 : 0;
3524 hardvolume = lvolume + (rvolume << 16);
3526 awacs_burgundy_wcw(address, hardvolume);
3529 static int
3530 awacs_burgundy_read_volume(unsigned address)
3532 int softvolume,wvolume;
3534 wvolume = awacs_burgundy_rcw(address);
3536 softvolume = (wvolume & 0xff) - 155;
3537 softvolume += (((wvolume >> 16) & 0xff) - 155)<<8;
3539 return softvolume > 0 ? softvolume : 0;
3545 static int
3546 awacs_burgundy_read_mvolume(unsigned address)
3548 int lvolume,rvolume,wvolume;
3550 wvolume = awacs_burgundy_rcw(address);
3552 wvolume &= 0xffff;
3554 rvolume = (wvolume & 0xff) - 155;
3555 lvolume = ((wvolume & 0xff00)>>8) - 155;
3557 return lvolume + (rvolume << 8);
3561 static void
3562 awacs_burgundy_write_mvolume(unsigned address, int volume)
3564 int lvolume,rvolume,hardvolume;
3566 lvolume = (volume &0xff) ? (volume & 0xff) + 155 :0;
3567 rvolume = ((volume >>8) & 0xff) ? (volume >> 8) + 155 :0;
3569 hardvolume = lvolume + (rvolume << 8);
3570 hardvolume += (hardvolume << 16);
3572 awacs_burgundy_wcw(address, hardvolume);
3575 /* End burgundy functions */
3581 /* Turn on sound output, needed on G3 desktop powermacs */
3582 static void
3583 awacs_enable_amp(int spkr_vol)
3585 struct adb_request req;
3587 awacs_spkr_vol = spkr_vol;
3588 if (adb_hardware != ADB_VIACUDA)
3589 return;
3591 /* turn on headphones */
3592 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
3593 0x8a, 4, 0);
3594 while (!req.complete) cuda_poll();
3595 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
3596 0x8a, 6, 0);
3597 while (!req.complete) cuda_poll();
3599 /* turn on speaker */
3600 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
3601 0x8a, 3, (100 - (spkr_vol & 0xff)) * 32 / 100);
3602 while (!req.complete) cuda_poll();
3603 cuda_request(&req, NULL, 5, CUDA_PACKET, CUDA_GET_SET_IIC,
3604 0x8a, 5, (100 - ((spkr_vol >> 8) & 0xff)) * 32 / 100);
3605 while (!req.complete) cuda_poll();
3607 cuda_request(&req, NULL, 5, CUDA_PACKET,
3608 CUDA_GET_SET_IIC, 0x8a, 1, 0x29);
3609 while (!req.complete) cuda_poll();
3612 #endif /* CONFIG_PPC */
3614 /*** Machine definitions *****************************************************/
3617 #ifdef CONFIG_ATARI
3618 static MACHINE machTT = {
3619 DMASND_TT, AtaAlloc, AtaFree, AtaIrqInit,
3620 #ifdef MODULE
3621 AtaIrqCleanUp,
3622 #endif /* MODULE */
3623 TTInit, TTSilence, TTSetFormat, TTSetVolume,
3624 AtaSetBass, AtaSetTreble, TTSetGain,
3625 AtaPlay
3628 static MACHINE machFalcon = {
3629 DMASND_FALCON, AtaAlloc, AtaFree, AtaIrqInit,
3630 #ifdef MODULE
3631 AtaIrqCleanUp,
3632 #endif /* MODULE */
3633 FalconInit, FalconSilence, FalconSetFormat, FalconSetVolume,
3634 AtaSetBass, AtaSetTreble, NULL,
3635 AtaPlay
3637 #endif /* CONFIG_ATARI */
3639 #ifdef CONFIG_AMIGA
3640 static MACHINE machAmiga = {
3641 DMASND_AMIGA, AmiAlloc, AmiFree, AmiIrqInit,
3642 #ifdef MODULE
3643 AmiIrqCleanUp,
3644 #endif /* MODULE */
3645 AmiInit, AmiSilence, AmiSetFormat, AmiSetVolume,
3646 NULL, AmiSetTreble, NULL,
3647 AmiPlay
3649 #endif /* CONFIG_AMIGA */
3651 #ifdef CONFIG_PPC
3652 static MACHINE machPMac = {
3653 DMASND_AWACS, PMacAlloc, PMacFree, PMacIrqInit,
3654 #ifdef MODULE
3655 PMacIrqCleanup,
3656 #endif /* MODULE */
3657 PMacInit, PMacSilence, PMacSetFormat, PMacSetVolume,
3658 NULL, NULL, NULL, /* bass, treble, gain */
3659 PMacPlay
3661 #endif /* CONFIG_AMIGA */
3664 /*** Mid level stuff *********************************************************/
3667 static void sound_silence(void)
3669 /* update hardware settings one more */
3670 (*sound.mach.init)();
3672 (*sound.mach.silence)();
3676 static void sound_init(void)
3678 (*sound.mach.init)();
3682 static int sound_set_format(int format)
3684 return(*sound.mach.setFormat)(format);
3688 static int sound_set_speed(int speed)
3690 if (speed < 0)
3691 return(sound.soft.speed);
3693 sound.soft.speed = speed;
3694 (*sound.mach.init)();
3695 if (sound.minDev == SND_DEV_DSP)
3696 sound.dsp.speed = sound.soft.speed;
3698 return(sound.soft.speed);
3702 static int sound_set_stereo(int stereo)
3704 if (stereo < 0)
3705 return(sound.soft.stereo);
3707 stereo = !!stereo; /* should be 0 or 1 now */
3709 sound.soft.stereo = stereo;
3710 if (sound.minDev == SND_DEV_DSP)
3711 sound.dsp.stereo = stereo;
3712 (*sound.mach.init)();
3714 return(stereo);
3718 static int sound_set_volume(int volume)
3720 return(*sound.mach.setVolume)(volume);
3724 #ifdef CONFIG_ATARI
3725 static int sound_set_bass(int bass)
3727 return(sound.mach.setBass ? (*sound.mach.setBass)(bass) : 50);
3730 static int sound_set_gain(int gain)
3732 return sound.mach.setGain ? sound.mach.setGain(gain) : 100;
3734 #endif /* CONFIG_ATARI */
3736 #if defined(CONFIG_ATARI) || defined(CONFIG_AMIGA)
3737 static int sound_set_treble(int treble)
3739 return(sound.mach.setTreble ? (*sound.mach.setTreble)(treble) : 50);
3741 #endif /* CONFIG_ATARI || CONFIG_AMIGA */
3744 static ssize_t sound_copy_translate(const u_char *userPtr,
3745 size_t userCount,
3746 u_char frame[], ssize_t *frameUsed,
3747 ssize_t frameLeft)
3749 ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
3751 switch (sound.soft.format) {
3752 case AFMT_MU_LAW:
3753 ct_func = sound.trans->ct_ulaw;
3754 break;
3755 case AFMT_A_LAW:
3756 ct_func = sound.trans->ct_alaw;
3757 break;
3758 case AFMT_S8:
3759 ct_func = sound.trans->ct_s8;
3760 break;
3761 case AFMT_U8:
3762 ct_func = sound.trans->ct_u8;
3763 break;
3764 case AFMT_S16_BE:
3765 ct_func = sound.trans->ct_s16be;
3766 break;
3767 case AFMT_U16_BE:
3768 ct_func = sound.trans->ct_u16be;
3769 break;
3770 case AFMT_S16_LE:
3771 ct_func = sound.trans->ct_s16le;
3772 break;
3773 case AFMT_U16_LE:
3774 ct_func = sound.trans->ct_u16le;
3775 break;
3777 if (ct_func)
3778 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
3779 else
3780 return 0;
3785 * /dev/mixer abstraction
3789 #define RECLEVEL_VOXWARE_TO_GAIN(v) \
3790 ((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
3791 #define RECLEVEL_GAIN_TO_VOXWARE(v) (((v) * 20 + 2) / 3)
3794 static int mixer_open(struct inode *inode, struct file *file)
3796 MOD_INC_USE_COUNT;
3797 mixer.busy = 1;
3798 return 0;
3802 static int mixer_release(struct inode *inode, struct file *file)
3804 mixer.busy = 0;
3805 MOD_DEC_USE_COUNT;
3806 return 0;
3810 static int mixer_ioctl(struct inode *inode, struct file *file, u_int cmd,
3811 u_long arg)
3813 int data;
3814 switch (sound.mach.type) {
3815 #ifdef CONFIG_ATARI
3816 case DMASND_FALCON:
3817 switch (cmd) {
3818 case SOUND_MIXER_READ_DEVMASK:
3819 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
3820 case SOUND_MIXER_READ_RECMASK:
3821 return IOCTL_OUT(arg, SOUND_MASK_MIC);
3822 case SOUND_MIXER_READ_STEREODEVS:
3823 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC);
3824 case SOUND_MIXER_READ_CAPS:
3825 return IOCTL_OUT(arg, SOUND_CAP_EXCL_INPUT);
3826 case SOUND_MIXER_READ_VOLUME:
3827 return IOCTL_OUT(arg,
3828 VOLUME_ATT_TO_VOXWARE(sound.volume_left) |
3829 VOLUME_ATT_TO_VOXWARE(sound.volume_right) << 8);
3830 case SOUND_MIXER_WRITE_MIC:
3831 IOCTL_IN(arg, data);
3832 tt_dmasnd.input_gain =
3833 RECLEVEL_VOXWARE_TO_GAIN(data & 0xff) << 4 |
3834 RECLEVEL_VOXWARE_TO_GAIN(data >> 8 & 0xff);
3835 /* fall thru, return set value */
3836 case SOUND_MIXER_READ_MIC:
3837 return IOCTL_OUT(arg,
3838 RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain >> 4 & 0xf) |
3839 RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain & 0xf) << 8);
3840 case SOUND_MIXER_READ_SPEAKER:
3842 int porta;
3843 cli();
3844 sound_ym.rd_data_reg_sel = 14;
3845 porta = sound_ym.rd_data_reg_sel;
3846 sti();
3847 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3849 case SOUND_MIXER_WRITE_VOLUME:
3850 IOCTL_IN(arg, data);
3851 return IOCTL_OUT(arg, sound_set_volume(data));
3852 case SOUND_MIXER_WRITE_SPEAKER:
3854 int porta;
3855 IOCTL_IN(arg, data);
3856 cli();
3857 sound_ym.rd_data_reg_sel = 14;
3858 porta = (sound_ym.rd_data_reg_sel & ~0x40) |
3859 (data < 50 ? 0x40 : 0);
3860 sound_ym.wd_data = porta;
3861 sti();
3862 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3865 break;
3867 case DMASND_TT:
3868 switch (cmd) {
3869 case SOUND_MIXER_READ_DEVMASK:
3870 return IOCTL_OUT(arg,
3871 SOUND_MASK_VOLUME | SOUND_MASK_TREBLE | SOUND_MASK_BASS |
3872 (MACH_IS_TT ? SOUND_MASK_SPEAKER : 0));
3873 case SOUND_MIXER_READ_RECMASK:
3874 return IOCTL_OUT(arg, 0);
3875 case SOUND_MIXER_READ_STEREODEVS:
3876 return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
3877 case SOUND_MIXER_READ_VOLUME:
3878 return IOCTL_OUT(arg,
3879 VOLUME_DB_TO_VOXWARE(sound.volume_left) |
3880 (VOLUME_DB_TO_VOXWARE(sound.volume_right) << 8));
3881 case SOUND_MIXER_READ_BASS:
3882 return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(sound.bass));
3883 case SOUND_MIXER_READ_TREBLE:
3884 return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(sound.treble));
3885 case SOUND_MIXER_READ_OGAIN:
3886 return IOCTL_OUT(arg, GAIN_DB_TO_VOXWARE(sound.gain));
3887 case SOUND_MIXER_READ_SPEAKER:
3889 int porta;
3890 if (MACH_IS_TT) {
3891 cli();
3892 sound_ym.rd_data_reg_sel = 14;
3893 porta = sound_ym.rd_data_reg_sel;
3894 sti();
3895 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3898 break;
3899 case SOUND_MIXER_WRITE_VOLUME:
3900 IOCTL_IN(arg, data);
3901 return IOCTL_OUT(arg, sound_set_volume(data));
3902 case SOUND_MIXER_WRITE_BASS:
3903 IOCTL_IN(arg, data);
3904 return IOCTL_OUT(arg, sound_set_bass(data));
3905 case SOUND_MIXER_WRITE_TREBLE:
3906 IOCTL_IN(arg, data);
3907 return IOCTL_OUT(arg, sound_set_treble(data));
3908 case SOUND_MIXER_WRITE_OGAIN:
3909 IOCTL_IN(arg, data);
3910 return IOCTL_OUT(arg, sound_set_gain(data));
3911 case SOUND_MIXER_WRITE_SPEAKER:
3912 if (MACH_IS_TT) {
3913 int porta;
3914 IOCTL_IN(arg, data);
3915 cli();
3916 sound_ym.rd_data_reg_sel = 14;
3917 porta = (sound_ym.rd_data_reg_sel & ~0x40) |
3918 (data < 50 ? 0x40 : 0);
3919 sound_ym.wd_data = porta;
3920 sti();
3921 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3924 break;
3925 #endif /* CONFIG_ATARI */
3927 #ifdef CONFIG_AMIGA
3928 case DMASND_AMIGA:
3929 switch (cmd) {
3930 case SOUND_MIXER_READ_DEVMASK:
3931 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_TREBLE);
3932 case SOUND_MIXER_READ_RECMASK:
3933 return IOCTL_OUT(arg, 0);
3934 case SOUND_MIXER_READ_STEREODEVS:
3935 return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
3936 case SOUND_MIXER_READ_VOLUME:
3937 return IOCTL_OUT(arg,
3938 VOLUME_AMI_TO_VOXWARE(sound.volume_left) |
3939 VOLUME_AMI_TO_VOXWARE(sound.volume_right) << 8);
3940 case SOUND_MIXER_WRITE_VOLUME:
3941 IOCTL_IN(arg, data);
3942 return IOCTL_OUT(arg, sound_set_volume(data));
3943 case SOUND_MIXER_READ_TREBLE:
3944 return IOCTL_OUT(arg, sound.treble);
3945 case SOUND_MIXER_WRITE_TREBLE:
3946 IOCTL_IN(arg, data);
3947 return IOCTL_OUT(arg, sound_set_treble(data));
3949 break;
3950 #endif /* CONFIG_AMIGA */
3952 #ifdef CONFIG_PPC
3953 case DMASND_AWACS:
3954 if (awacs_revision<AWACS_BURGUNDY) { /* Different IOCTLS for burgundy*/
3955 switch (cmd) {
3956 case SOUND_MIXER_READ_DEVMASK:
3957 data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
3958 | SOUND_MASK_LINE | SOUND_MASK_MIC
3959 | SOUND_MASK_CD | SOUND_MASK_RECLEV
3960 | SOUND_MASK_ALTPCM;
3961 return IOCTL_OUT(arg, data);
3962 case SOUND_MIXER_READ_RECMASK:
3963 data = SOUND_MASK_LINE | SOUND_MASK_MIC
3964 | SOUND_MASK_CD;
3965 return IOCTL_OUT(arg, data);
3966 case SOUND_MIXER_READ_RECSRC:
3967 data = 0;
3968 if (awacs_reg[0] & MASK_MUX_AUDIN)
3969 data |= SOUND_MASK_LINE;
3970 if (awacs_reg[0] & MASK_MUX_MIC)
3971 data |= SOUND_MASK_MIC;
3972 if (awacs_reg[0] & MASK_MUX_CD)
3973 data |= SOUND_MASK_CD;
3974 return IOCTL_OUT(arg, data);
3975 case SOUND_MIXER_WRITE_RECSRC:
3976 IOCTL_IN(arg, data);
3977 data &= (SOUND_MASK_LINE
3978 | SOUND_MASK_MIC | SOUND_MASK_CD);
3979 awacs_reg[0] &= ~(MASK_MUX_CD | MASK_MUX_MIC
3980 | MASK_MUX_AUDIN);
3981 if (data & SOUND_MASK_LINE)
3982 awacs_reg[0] |= MASK_MUX_AUDIN;
3983 if (data & SOUND_MASK_MIC)
3984 awacs_reg[0] |= MASK_MUX_MIC;
3985 if (data & SOUND_MASK_CD)
3986 awacs_reg[0] |= MASK_MUX_CD;
3987 awacs_write(awacs_reg[0] | MASK_ADDR0);
3988 return IOCTL_OUT(arg, data);
3989 case SOUND_MIXER_READ_STEREODEVS:
3990 data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
3991 | SOUND_MASK_RECLEV;
3992 return IOCTL_OUT(arg, data);
3993 case SOUND_MIXER_READ_CAPS:
3994 return IOCTL_OUT(arg, 0);
3995 case SOUND_MIXER_READ_VOLUME:
3996 data = (awacs_reg[1] & MASK_AMUTE)? 0:
3997 awacs_get_volume(awacs_reg[2], 6);
3998 return IOCTL_OUT(arg, data);
3999 case SOUND_MIXER_WRITE_VOLUME:
4000 IOCTL_IN(arg, data);
4001 return IOCTL_OUT(arg, sound_set_volume(data));
4002 case SOUND_MIXER_READ_SPEAKER:
4003 if (awacs_revision == 3 && adb_hardware == ADB_VIACUDA)
4004 data = awacs_spkr_vol;
4005 else
4006 data = (awacs_reg[1] & MASK_CMUTE)? 0:
4007 awacs_get_volume(awacs_reg[4], 6);
4008 return IOCTL_OUT(arg, data);
4009 case SOUND_MIXER_WRITE_SPEAKER:
4010 IOCTL_IN(arg, data);
4011 if (awacs_revision == 3 && adb_hardware == ADB_VIACUDA)
4012 awacs_enable_amp(data);
4013 else
4014 data = awacs_volume_setter(data, 4, MASK_CMUTE, 6);
4015 return IOCTL_OUT(arg, data);
4016 case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
4017 IOCTL_IN(arg, data);
4018 beep_volume = data & 0xff;
4019 /* fall through */
4020 case SOUND_MIXER_READ_ALTPCM:
4021 return IOCTL_OUT(arg, beep_volume);
4022 case SOUND_MIXER_WRITE_LINE:
4023 IOCTL_IN(arg, data);
4024 awacs_reg[0] &= ~MASK_MUX_AUDIN;
4025 if ((data & 0xff) >= 50)
4026 awacs_reg[0] |= MASK_MUX_AUDIN;
4027 awacs_write(MASK_ADDR0 | awacs_reg[0]);
4028 /* fall through */
4029 case SOUND_MIXER_READ_LINE:
4030 data = (awacs_reg[0] & MASK_MUX_AUDIN)? 100: 0;
4031 return IOCTL_OUT(arg, data);
4032 case SOUND_MIXER_WRITE_MIC:
4033 IOCTL_IN(arg, data);
4034 data &= 0xff;
4035 awacs_reg[0] &= ~(MASK_MUX_MIC | MASK_GAINLINE);
4036 if (data >= 25) {
4037 awacs_reg[0] |= MASK_MUX_MIC;
4038 if (data >= 75)
4039 awacs_reg[0] |= MASK_GAINLINE;
4041 awacs_write(MASK_ADDR0 | awacs_reg[0]);
4042 /* fall through */
4043 case SOUND_MIXER_READ_MIC:
4044 data = (awacs_reg[0] & MASK_MUX_MIC)?
4045 (awacs_reg[0] & MASK_GAINLINE? 100: 50): 0;
4046 return IOCTL_OUT(arg, data);
4047 case SOUND_MIXER_WRITE_CD:
4048 IOCTL_IN(arg, data);
4049 awacs_reg[0] &= ~MASK_MUX_CD;
4050 if ((data & 0xff) >= 50)
4051 awacs_reg[0] |= MASK_MUX_CD;
4052 awacs_write(MASK_ADDR0 | awacs_reg[0]);
4053 /* fall through */
4054 case SOUND_MIXER_READ_CD:
4055 data = (awacs_reg[0] & MASK_MUX_CD)? 100: 0;
4056 return IOCTL_OUT(arg, data);
4057 case SOUND_MIXER_WRITE_RECLEV:
4058 IOCTL_IN(arg, data);
4059 data = awacs_volume_setter(data, 0, 0, 4);
4060 return IOCTL_OUT(arg, data);
4061 case SOUND_MIXER_READ_RECLEV:
4062 data = awacs_get_volume(awacs_reg[0], 4);
4063 return IOCTL_OUT(arg, data);
4065 break;
4066 } else {
4067 /* We are, we are, we are... Burgundy or better */
4068 switch(cmd) {
4069 case SOUND_MIXER_READ_DEVMASK:
4070 data = SOUND_MASK_VOLUME | SOUND_MASK_CD |
4071 SOUND_MASK_LINE | SOUND_MASK_MIC |
4072 SOUND_MASK_SPEAKER | SOUND_MASK_ALTPCM;
4073 return IOCTL_OUT(arg, data);
4074 case SOUND_MIXER_READ_RECMASK:
4075 data = SOUND_MASK_LINE | SOUND_MASK_MIC
4076 | SOUND_MASK_CD;
4077 return IOCTL_OUT(arg, data);
4078 case SOUND_MIXER_READ_RECSRC:
4079 data = 0;
4080 if (awacs_reg[0] & MASK_MUX_AUDIN)
4081 data |= SOUND_MASK_LINE;
4082 if (awacs_reg[0] & MASK_MUX_MIC)
4083 data |= SOUND_MASK_MIC;
4084 if (awacs_reg[0] & MASK_MUX_CD)
4085 data |= SOUND_MASK_CD;
4086 return IOCTL_OUT(arg, data);
4087 case SOUND_MIXER_WRITE_RECSRC:
4088 IOCTL_IN(arg, data);
4089 data &= (SOUND_MASK_LINE
4090 | SOUND_MASK_MIC | SOUND_MASK_CD);
4091 awacs_reg[0] &= ~(MASK_MUX_CD | MASK_MUX_MIC
4092 | MASK_MUX_AUDIN);
4093 if (data & SOUND_MASK_LINE)
4094 awacs_reg[0] |= MASK_MUX_AUDIN;
4095 if (data & SOUND_MASK_MIC)
4096 awacs_reg[0] |= MASK_MUX_MIC;
4097 if (data & SOUND_MASK_CD)
4098 awacs_reg[0] |= MASK_MUX_CD;
4099 awacs_write(awacs_reg[0] | MASK_ADDR0);
4100 return IOCTL_OUT(arg, data);
4101 case SOUND_MIXER_READ_STEREODEVS:
4102 data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
4103 | SOUND_MASK_RECLEV | SOUND_MASK_CD
4104 | SOUND_MASK_LINE;
4105 return IOCTL_OUT(arg, data);
4106 case SOUND_MIXER_READ_CAPS:
4107 return IOCTL_OUT(arg, 0);
4108 case SOUND_MIXER_WRITE_VOLUME:
4109 IOCTL_IN(arg, data);
4110 awacs_burgundy_write_mvolume(MASK_ADDR_BURGUNDY_MASTER_VOLUME, data);
4111 /* Fall through */
4112 case SOUND_MIXER_READ_VOLUME:
4113 return IOCTL_OUT(arg, awacs_burgundy_read_mvolume(MASK_ADDR_BURGUNDY_MASTER_VOLUME));
4114 case SOUND_MIXER_WRITE_SPEAKER:
4115 IOCTL_IN(arg, data);
4117 if (!(data & 0xff)) {
4118 /* Mute the left speaker */
4119 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
4120 awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) & ~0x2);
4121 } else {
4122 /* Unmute the left speaker */
4123 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
4124 awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) | 0x2);
4126 if (!(data & 0xff00)) {
4127 /* Mute the right speaker */
4128 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
4129 awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) & ~0x4);
4130 } else {
4131 /* Unmute the right speaker */
4132 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES,
4133 awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_MORE_OUTPUTENABLES) | 0x4);
4136 data = (((data&0xff)*16)/100 > 0xf ? 0xf :
4137 (((data&0xff)*16)/100)) +
4138 ((((data>>8)*16)/100 > 0xf ? 0xf :
4139 ((((data>>8)*16)/100)))<<4);
4141 awacs_burgundy_wcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER, ~data);
4142 /* Fall through */
4143 case SOUND_MIXER_READ_SPEAKER:
4144 data = awacs_burgundy_rcb(MASK_ADDR_BURGUNDY_ATTENSPEAKER);
4145 data = (((data & 0xf)*100)/16) + ((((data>>4)*100)/16)<<8);
4146 return IOCTL_OUT(arg, ~data);
4147 case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
4148 IOCTL_IN(arg, data);
4149 beep_volume = data & 0xff;
4150 /* fall through */
4151 case SOUND_MIXER_READ_ALTPCM:
4152 return IOCTL_OUT(arg, beep_volume);
4153 case SOUND_MIXER_WRITE_LINE:
4154 IOCTL_IN(arg, data);
4155 awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLLINE, data);
4157 /* fall through */
4158 case SOUND_MIXER_READ_LINE:
4159 data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLLINE);
4160 return IOCTL_OUT(arg, data);
4161 case SOUND_MIXER_WRITE_MIC:
4162 IOCTL_IN(arg, data);
4163 /* Mic is mono device */
4164 data = (data << 8) + (data << 24);
4165 awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLMIC, data);
4166 /* fall through */
4167 case SOUND_MIXER_READ_MIC:
4168 data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLMIC);
4169 data <<= 24;
4170 return IOCTL_OUT(arg, data);
4171 case SOUND_MIXER_WRITE_CD:
4172 IOCTL_IN(arg, data);
4173 awacs_burgundy_write_volume(MASK_ADDR_BURGUNDY_VOLCD, data);
4174 /* fall through */
4175 case SOUND_MIXER_READ_CD:
4176 data = awacs_burgundy_read_volume(MASK_ADDR_BURGUNDY_VOLCD);
4177 return IOCTL_OUT(arg, data);
4178 case SOUND_MIXER_WRITE_RECLEV:
4179 IOCTL_IN(arg, data);
4180 data = awacs_volume_setter(data, 0, 0, 4);
4181 return IOCTL_OUT(arg, data);
4182 case SOUND_MIXER_READ_RECLEV:
4183 data = awacs_get_volume(awacs_reg[0], 4);
4184 return IOCTL_OUT(arg, data);
4185 case SOUND_MIXER_OUTMASK:
4186 break;
4187 case SOUND_MIXER_OUTSRC:
4188 break;
4190 break;
4192 #endif
4195 return -EINVAL;
4199 static struct file_operations mixer_fops =
4201 sound_lseek,
4202 NULL, /* mixer_read */
4203 NULL, /* mixer_write */
4204 NULL, /* mixer_readdir */
4205 NULL, /* mixer_poll */
4206 mixer_ioctl,
4207 NULL, /* mixer_mmap */
4208 mixer_open,
4209 NULL, /* flush */
4210 mixer_release,
4214 static void __init mixer_init(void)
4216 #ifndef MODULE
4217 int mixer_unit;
4218 #endif
4219 mixer_unit = register_sound_mixer(&mixer_fops, -1);
4220 if (mixer_unit < 0)
4221 return;
4223 mixer.busy = 0;
4224 sound.treble = 0;
4225 sound.bass = 0;
4226 switch (sound.mach.type) {
4227 #ifdef CONFIG_ATARI
4228 case DMASND_TT:
4229 atari_microwire_cmd(MW_LM1992_VOLUME(0));
4230 sound.volume_left = 0;
4231 atari_microwire_cmd(MW_LM1992_BALLEFT(0));
4232 sound.volume_right = 0;
4233 atari_microwire_cmd(MW_LM1992_BALRIGHT(0));
4234 atari_microwire_cmd(MW_LM1992_TREBLE(0));
4235 atari_microwire_cmd(MW_LM1992_BASS(0));
4236 break;
4237 case DMASND_FALCON:
4238 sound.volume_left = (tt_dmasnd.output_atten & 0xf00) >> 8;
4239 sound.volume_right = (tt_dmasnd.output_atten & 0xf0) >> 4;
4240 break;
4241 #endif /* CONFIG_ATARI */
4242 #ifdef CONFIG_AMIGA
4243 case DMASND_AMIGA:
4244 sound.volume_left = 64;
4245 sound.volume_right = 64;
4246 custom.aud[0].audvol = sound.volume_left;
4247 custom.aud[3].audvol = 1; /* For pseudo 14bit */
4248 custom.aud[1].audvol = sound.volume_right;
4249 custom.aud[2].audvol = 1; /* For pseudo 14bit */
4250 sound.treble = 50;
4251 break;
4252 #endif /* CONFIG_AMIGA */
4258 * Sound queue stuff, the heart of the driver
4262 static int sq_allocate_buffers(void)
4264 int i;
4266 if (sound_buffers)
4267 return 0;
4268 sound_buffers = kmalloc (numBufs * sizeof(char *), GFP_KERNEL);
4269 if (!sound_buffers)
4270 return -ENOMEM;
4271 for (i = 0; i < numBufs; i++) {
4272 sound_buffers[i] = sound.mach.dma_alloc (bufSize << 10, GFP_KERNEL);
4273 if (!sound_buffers[i]) {
4274 while (i--)
4275 sound.mach.dma_free (sound_buffers[i], bufSize << 10);
4276 kfree (sound_buffers);
4277 sound_buffers = 0;
4278 return -ENOMEM;
4281 return 0;
4285 static void sq_release_buffers(void)
4287 int i;
4289 if (sound_buffers) {
4290 for (i = 0; i < numBufs; i++)
4291 sound.mach.dma_free (sound_buffers[i], bufSize << 10);
4292 kfree (sound_buffers);
4293 sound_buffers = 0;
4298 static void sq_setup(int numBufs, int bufSize, char **buffers)
4300 #ifdef CONFIG_PPC
4301 int i;
4302 volatile struct dbdma_cmd *cp;
4303 #endif /* CONFIG_PPC */
4305 sq.max_count = numBufs;
4306 sq.max_active = numBufs;
4307 sq.block_size = bufSize;
4308 sq.buffers = buffers;
4310 sq.front = sq.count = 0;
4311 sq.rear = -1;
4312 sq.syncing = 0;
4313 sq.playing = 0;
4315 #ifdef CONFIG_ATARI
4316 sq.ignore_int = 0;
4317 #endif /* CONFIG_ATARI */
4318 #ifdef CONFIG_AMIGA
4319 sq.block_size_half = sq.block_size>>1;
4320 sq.block_size_quarter = sq.block_size_half>>1;
4321 #endif /* CONFIG_AMIGA */
4322 #ifdef CONFIG_PPC
4323 cp = awacs_tx_cmds;
4324 memset((void *) cp, 0, (numBufs + 1) * sizeof(struct dbdma_cmd));
4325 for (i = 0; i < numBufs; ++i, ++cp) {
4326 st_le32(&cp->phy_addr, virt_to_bus(buffers[i]));
4328 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
4329 st_le32(&cp->cmd_dep, virt_to_bus(awacs_tx_cmds));
4330 out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
4331 out_le32(&awacs_txdma->cmdptr, virt_to_bus(awacs_tx_cmds));
4332 #endif /* CONFIG_PPC */
4335 static void sq_play(void)
4337 (*sound.mach.play)();
4341 /* ++TeSche: radically changed this one too */
4343 static ssize_t sq_write(struct file *file, const char *src, size_t uLeft,
4344 loff_t *ppos)
4346 ssize_t uWritten = 0;
4347 u_char *dest;
4348 ssize_t uUsed, bUsed, bLeft;
4350 /* ++TeSche: Is something like this necessary?
4351 * Hey, that's an honest question! Or does any other part of the
4352 * filesystem already checks this situation? I really don't know.
4354 if (uLeft == 0)
4355 return 0;
4357 /* The interrupt doesn't start to play the last, incomplete frame.
4358 * Thus we can append to it without disabling the interrupts! (Note
4359 * also that sq.rear isn't affected by the interrupt.)
4362 if (sq.count > 0 && (bLeft = sq.block_size-sq.rear_size) > 0) {
4363 dest = sq_block_address(sq.rear);
4364 bUsed = sq.rear_size;
4365 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
4366 if (uUsed <= 0)
4367 return uUsed;
4368 src += uUsed;
4369 uWritten += uUsed;
4370 uLeft -= uUsed;
4371 sq.rear_size = bUsed;
4374 do {
4375 while (sq.count == sq.max_active) {
4376 sq_play();
4377 if (NON_BLOCKING(sq.open_mode))
4378 return uWritten > 0 ? uWritten : -EAGAIN;
4379 SLEEP(sq.write_queue, ONE_SECOND);
4380 if (SIGNAL_RECEIVED)
4381 return uWritten > 0 ? uWritten : -EINTR;
4384 /* Here, we can avoid disabling the interrupt by first
4385 * copying and translating the data, and then updating
4386 * the sq variables. Until this is done, the interrupt
4387 * won't see the new frame and we can work on it
4388 * undisturbed.
4391 dest = sq_block_address((sq.rear+1) % sq.max_count);
4392 bUsed = 0;
4393 bLeft = sq.block_size;
4394 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
4395 if (uUsed <= 0)
4396 break;
4397 src += uUsed;
4398 uWritten += uUsed;
4399 uLeft -= uUsed;
4400 if (bUsed) {
4401 sq.rear = (sq.rear+1) % sq.max_count;
4402 sq.rear_size = bUsed;
4403 sq.count++;
4405 } while (bUsed); /* uUsed may have been 0 */
4407 sq_play();
4409 return uUsed < 0? uUsed: uWritten;
4413 static int sq_open(struct inode *inode, struct file *file)
4415 int rc = 0;
4417 MOD_INC_USE_COUNT;
4418 if (sq.busy) {
4419 rc = -EBUSY;
4420 if (NON_BLOCKING(file->f_flags))
4421 goto err_out;
4422 rc = -EINTR;
4423 while (sq.busy) {
4424 SLEEP(sq.open_queue, ONE_SECOND);
4425 if (SIGNAL_RECEIVED)
4426 goto err_out;
4428 rc = 0;
4430 sq.busy = 1;
4431 rc = sq_allocate_buffers();
4432 if (rc)
4433 goto err_out_nobusy;
4434 sq_setup(numBufs, bufSize << 10, sound_buffers);
4435 sq.open_mode = file->f_flags;
4436 #ifdef CONFIG_ATARI
4437 sq.ignore_int = 1;
4438 #endif /* CONFIG_ATARI */
4439 sound.minDev = MINOR(inode->i_rdev) & 0x0f;
4440 sound.soft = sound.dsp;
4441 sound.hard = sound.dsp;
4442 sound_init();
4443 if ((MINOR(inode->i_rdev) & 0x0f) == SND_DEV_AUDIO) {
4444 sound_set_speed(8000);
4445 sound_set_stereo(0);
4446 sound_set_format(AFMT_MU_LAW);
4448 return 0;
4449 err_out_nobusy:
4450 sq.busy = 0;
4451 WAKE_UP(sq.open_queue);
4452 err_out:
4453 MOD_DEC_USE_COUNT;
4454 return rc;
4458 static void sq_reset(void)
4460 sound_silence();
4461 sq.playing = 0;
4462 sq.count = 0;
4463 sq.front = (sq.rear+1) % sq.max_count;
4467 static int sq_fsync(struct file *filp, struct dentry *dentry)
4469 int rc = 0;
4471 sq.syncing = 1;
4472 sq_play(); /* there may be an incomplete frame waiting */
4474 while (sq.playing) {
4475 SLEEP(sq.sync_queue, ONE_SECOND);
4476 if (SIGNAL_RECEIVED) {
4477 /* While waiting for audio output to drain, an
4478 * interrupt occurred. Stop audio output immediately
4479 * and clear the queue. */
4480 sq_reset();
4481 rc = -EINTR;
4482 break;
4486 sq.syncing = 0;
4487 return rc;
4490 static int sq_release(struct inode *inode, struct file *file)
4492 int rc = 0;
4494 if (sq.busy)
4495 rc = sq_fsync(file, file->f_dentry);
4496 sound.soft = sound.dsp;
4497 sound.hard = sound.dsp;
4498 sound_silence();
4499 sq_release_buffers();
4500 MOD_DEC_USE_COUNT;
4502 sq.busy = 0;
4503 WAKE_UP(sq.open_queue);
4504 /* Wake up a process waiting for the queue being released.
4505 * Note: There may be several processes waiting for a call
4506 * to open() returning. */
4508 return rc;
4512 static int sq_ioctl(struct inode *inode, struct file *file, u_int cmd,
4513 u_long arg)
4515 u_long fmt;
4516 int data;
4517 int size, nbufs;
4519 switch (cmd) {
4520 case SNDCTL_DSP_RESET:
4521 sq_reset();
4522 return 0;
4523 case SNDCTL_DSP_POST:
4524 case SNDCTL_DSP_SYNC:
4525 return sq_fsync(file, file->f_dentry);
4527 /* ++TeSche: before changing any of these it's
4528 * probably wise to wait until sound playing has
4529 * settled down. */
4530 case SNDCTL_DSP_SPEED:
4531 sq_fsync(file, file->f_dentry);
4532 IOCTL_IN(arg, data);
4533 return IOCTL_OUT(arg, sound_set_speed(data));
4534 case SNDCTL_DSP_STEREO:
4535 sq_fsync(file, file->f_dentry);
4536 IOCTL_IN(arg, data);
4537 return IOCTL_OUT(arg, sound_set_stereo(data));
4538 case SOUND_PCM_WRITE_CHANNELS:
4539 sq_fsync(file, file->f_dentry);
4540 IOCTL_IN(arg, data);
4541 return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
4542 case SNDCTL_DSP_SETFMT:
4543 sq_fsync(file, file->f_dentry);
4544 IOCTL_IN(arg, data);
4545 return IOCTL_OUT(arg, sound_set_format(data));
4546 case SNDCTL_DSP_GETFMTS:
4547 fmt = 0;
4548 if (sound.trans) {
4549 if (sound.trans->ct_ulaw)
4550 fmt |= AFMT_MU_LAW;
4551 if (sound.trans->ct_alaw)
4552 fmt |= AFMT_A_LAW;
4553 if (sound.trans->ct_s8)
4554 fmt |= AFMT_S8;
4555 if (sound.trans->ct_u8)
4556 fmt |= AFMT_U8;
4557 if (sound.trans->ct_s16be)
4558 fmt |= AFMT_S16_BE;
4559 if (sound.trans->ct_u16be)
4560 fmt |= AFMT_U16_BE;
4561 if (sound.trans->ct_s16le)
4562 fmt |= AFMT_S16_LE;
4563 if (sound.trans->ct_u16le)
4564 fmt |= AFMT_U16_LE;
4566 return IOCTL_OUT(arg, fmt);
4567 case SNDCTL_DSP_GETBLKSIZE:
4568 size = sq.block_size
4569 * sound.soft.size * (sound.soft.stereo + 1)
4570 / (sound.hard.size * (sound.hard.stereo + 1));
4571 return IOCTL_OUT(arg, size);
4572 case SNDCTL_DSP_SUBDIVIDE:
4573 break;
4574 case SNDCTL_DSP_SETFRAGMENT:
4575 if (sq.count || sq.playing || sq.syncing)
4576 return -EINVAL;
4577 IOCTL_IN(arg, size);
4578 nbufs = size >> 16;
4579 if (nbufs < 2 || nbufs > numBufs)
4580 nbufs = numBufs;
4581 size &= 0xffff;
4582 if (size >= 8 && size <= 30) {
4583 size = 1 << size;
4584 size *= sound.hard.size * (sound.hard.stereo + 1);
4585 size /= sound.soft.size * (sound.soft.stereo + 1);
4586 if (size > (bufSize << 10))
4587 size = bufSize << 10;
4588 } else
4589 size = bufSize << 10;
4590 sq_setup(numBufs, size, sound_buffers);
4591 sq.max_active = nbufs;
4592 return 0;
4594 default:
4595 return mixer_ioctl(inode, file, cmd, arg);
4597 return -EINVAL;
4602 static struct file_operations sq_fops =
4604 sound_lseek,
4605 NULL, /* sq_read */
4606 sq_write,
4607 NULL, /* sq_readdir */
4608 NULL, /* sq_poll */
4609 sq_ioctl,
4610 NULL, /* sq_mmap */
4611 sq_open,
4612 NULL, /* flush */
4613 sq_release,
4617 static void __init sq_init(void)
4619 #ifndef MODULE
4620 int sq_unit;
4621 #endif
4622 sq_unit = register_sound_dsp(&sq_fops, -1);
4623 if (sq_unit < 0)
4624 return;
4626 init_waitqueue_head(&sq.write_queue);
4627 init_waitqueue_head(&sq.open_queue);
4628 init_waitqueue_head(&sq.sync_queue);
4629 sq.busy = 0;
4631 /* whatever you like as startup mode for /dev/dsp,
4632 * (/dev/audio hasn't got a startup mode). note that
4633 * once changed a new open() will *not* restore these!
4635 sound.dsp.format = AFMT_U8;
4636 sound.dsp.stereo = 0;
4637 sound.dsp.size = 8;
4639 /* set minimum rate possible without expanding */
4640 switch (sound.mach.type) {
4641 #ifdef CONFIG_ATARI
4642 case DMASND_TT:
4643 sound.dsp.speed = 6258;
4644 break;
4645 case DMASND_FALCON:
4646 sound.dsp.speed = 8195;
4647 break;
4648 #endif /* CONFIG_ATARI */
4649 #ifdef CONFIG_AMIGA
4650 case DMASND_AMIGA:
4651 sound.dsp.speed = 8000;
4652 break;
4653 #endif /* CONFIG_AMIGA */
4654 #ifdef CONFIG_PPC
4655 case DMASND_AWACS:
4656 sound.dsp.speed = 8000;
4657 break;
4658 #endif /* CONFIG_PPC */
4661 /* before the first open to /dev/dsp this wouldn't be set */
4662 sound.soft = sound.dsp;
4663 sound.hard = sound.dsp;
4665 sound_silence();
4669 * /dev/sndstat
4673 /* state.buf should not overflow! */
4675 static int state_open(struct inode *inode, struct file *file)
4677 char *buffer = state.buf, *mach = "";
4678 int len = 0;
4680 if (state.busy)
4681 return -EBUSY;
4683 MOD_INC_USE_COUNT;
4684 state.ptr = 0;
4685 state.busy = 1;
4687 switch (sound.mach.type) {
4688 #ifdef CONFIG_ATARI
4689 case DMASND_TT:
4690 case DMASND_FALCON:
4691 mach = "Atari ";
4692 break;
4693 #endif /* CONFIG_ATARI */
4694 #ifdef CONFIG_AMIGA
4695 case DMASND_AMIGA:
4696 mach = "Amiga ";
4697 break;
4698 #endif /* CONFIG_AMIGA */
4699 #ifdef CONFIG_PPC
4700 case DMASND_AWACS:
4701 sprintf(mach, "PowerMac (AWACS rev %d) ", awacs_revision);
4702 break;
4703 #endif /* CONFIG_PPC */
4705 len += sprintf(buffer+len, "%sDMA sound driver:\n", mach);
4707 len += sprintf(buffer+len, "\tsound.format = 0x%x", sound.soft.format);
4708 switch (sound.soft.format) {
4709 case AFMT_MU_LAW:
4710 len += sprintf(buffer+len, " (mu-law)");
4711 break;
4712 case AFMT_A_LAW:
4713 len += sprintf(buffer+len, " (A-law)");
4714 break;
4715 case AFMT_U8:
4716 len += sprintf(buffer+len, " (unsigned 8 bit)");
4717 break;
4718 case AFMT_S8:
4719 len += sprintf(buffer+len, " (signed 8 bit)");
4720 break;
4721 case AFMT_S16_BE:
4722 len += sprintf(buffer+len, " (signed 16 bit big)");
4723 break;
4724 case AFMT_U16_BE:
4725 len += sprintf(buffer+len, " (unsigned 16 bit big)");
4726 break;
4727 case AFMT_S16_LE:
4728 len += sprintf(buffer+len, " (signed 16 bit little)");
4729 break;
4730 case AFMT_U16_LE:
4731 len += sprintf(buffer+len, " (unsigned 16 bit little)");
4732 break;
4734 len += sprintf(buffer+len, "\n");
4735 len += sprintf(buffer+len, "\tsound.speed = %dHz (phys. %dHz)\n",
4736 sound.soft.speed, sound.hard.speed);
4737 len += sprintf(buffer+len, "\tsound.stereo = 0x%x (%s)\n",
4738 sound.soft.stereo, sound.soft.stereo ? "stereo" : "mono");
4739 switch (sound.mach.type) {
4740 #ifdef CONFIG_ATARI
4741 case DMASND_TT:
4742 len += sprintf(buffer+len, "\tsound.volume_left = %ddB [-40...0]\n",
4743 sound.volume_left);
4744 len += sprintf(buffer+len, "\tsound.volume_right = %ddB [-40...0]\n",
4745 sound.volume_right);
4746 len += sprintf(buffer+len, "\tsound.bass = %ddB [-12...+12]\n",
4747 sound.bass);
4748 len += sprintf(buffer+len, "\tsound.treble = %ddB [-12...+12]\n",
4749 sound.treble);
4750 break;
4751 case DMASND_FALCON:
4752 len += sprintf(buffer+len, "\tsound.volume_left = %ddB [-22.5...0]\n",
4753 sound.volume_left);
4754 len += sprintf(buffer+len, "\tsound.volume_right = %ddB [-22.5...0]\n",
4755 sound.volume_right);
4756 break;
4757 #endif /* CONFIG_ATARI */
4758 #ifdef CONFIG_AMIGA
4759 case DMASND_AMIGA:
4760 len += sprintf(buffer+len, "\tsound.volume_left = %d [0...64]\n",
4761 sound.volume_left);
4762 len += sprintf(buffer+len, "\tsound.volume_right = %d [0...64]\n",
4763 sound.volume_right);
4764 break;
4765 #endif /* CONFIG_AMIGA */
4767 len += sprintf(buffer+len, "\tsq.block_size = %d sq.max_count = %d"
4768 " sq.max_active = %d\n",
4769 sq.block_size, sq.max_count, sq.max_active);
4770 len += sprintf(buffer+len, "\tsq.count = %d sq.rear_size = %d\n", sq.count,
4771 sq.rear_size);
4772 len += sprintf(buffer+len, "\tsq.playing = %d sq.syncing = %d\n",
4773 sq.playing, sq.syncing);
4774 state.len = len;
4775 return 0;
4779 static int state_release(struct inode *inode, struct file *file)
4781 state.busy = 0;
4782 MOD_DEC_USE_COUNT;
4783 return 0;
4787 static ssize_t state_read(struct file *file, char *buf, size_t count,
4788 loff_t *ppos)
4790 int n = state.len - state.ptr;
4791 if (n > count)
4792 n = count;
4793 if (n <= 0)
4794 return 0;
4795 if (copy_to_user(buf, &state.buf[state.ptr], n))
4796 return -EFAULT;
4797 state.ptr += n;
4798 return n;
4802 static struct file_operations state_fops =
4804 sound_lseek,
4805 state_read,
4806 NULL, /* state_write */
4807 NULL, /* state_readdir */
4808 NULL, /* state_poll */
4809 NULL, /* state_ioctl */
4810 NULL, /* state_mmap */
4811 state_open,
4812 NULL, /* flush */
4813 state_release,
4817 static void __init state_init(void)
4819 #ifndef MODULE
4820 int state_unit;
4821 #endif
4822 state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
4823 if (state_unit < 0)
4824 return;
4825 state.busy = 0;
4829 /*** Common stuff ********************************************************/
4831 static long long sound_lseek(struct file *file, long long offset, int orig)
4833 return -ESPIPE;
4837 /*** Config & Setup **********************************************************/
4840 void __init dmasound_init(void)
4842 int has_sound = 0;
4843 #ifdef CONFIG_PPC
4844 struct device_node *np;
4845 #endif
4847 #if defined(__mc68000__) || defined(CONFIG_APUS)
4848 switch (m68k_machtype) {
4849 #ifdef CONFIG_ATARI
4850 case MACH_ATARI:
4851 if (ATARIHW_PRESENT(PCM_8BIT)) {
4852 if (ATARIHW_PRESENT(CODEC))
4853 sound.mach = machFalcon;
4854 else if (ATARIHW_PRESENT(MICROWIRE))
4855 sound.mach = machTT;
4856 else
4857 break;
4858 if ((mfp.int_en_a & mfp.int_mk_a & 0x20) == 0)
4859 has_sound = 1;
4860 else
4861 printk("DMA sound driver: Timer A interrupt already in use\n");
4863 break;
4865 #endif /* CONFIG_ATARI */
4866 #ifdef CONFIG_AMIGA
4867 case MACH_AMIGA:
4868 if (AMIGAHW_PRESENT(AMI_AUDIO)) {
4869 sound.mach = machAmiga;
4870 has_sound = 1;
4872 break;
4873 #endif /* CONFIG_AMIGA */
4875 #endif /* __mc68000__||CONFIG_APUS */
4877 #ifdef CONFIG_PPC
4878 awacs_subframe = 0;
4879 awacs_revision = 0;
4880 np = find_devices("awacs");
4881 if (np == 0) {
4883 * powermac G3 models have a node called "davbus"
4884 * with a child called "sound".
4886 struct device_node *sound;
4887 np = find_devices("davbus");
4888 sound = find_devices("sound");
4889 if (sound != 0 && sound->parent == np) {
4890 int *sfprop;
4891 sfprop = (int *) get_property(sound, "sub-frame", 0);
4892 if (sfprop != 0 && *sfprop >= 0 && *sfprop < 16)
4893 awacs_subframe = *sfprop;
4894 if (device_is_compatible(sound, "burgundy"))
4895 awacs_revision = AWACS_BURGUNDY;
4898 if (np != NULL && np->n_addrs >= 3 && np->n_intrs >= 3) {
4899 int vol;
4900 sound.mach = machPMac;
4901 has_sound = 1;
4902 awacs = (volatile struct awacs_regs *)
4903 ioremap(np->addrs[0].address, 0x80);
4904 awacs_txdma = (volatile struct dbdma_regs *)
4905 ioremap(np->addrs[1].address, 0x100);
4906 awacs_rxdma = (volatile struct dbdma_regs *)
4907 ioremap(np->addrs[2].address, 0x100);
4908 awacs_irq = np->intrs[0].line;
4909 awacs_tx_irq = np->intrs[1].line;
4910 awacs_rx_irq = np->intrs[2].line;
4911 awacs_tx_cmd_space = kmalloc((numBufs + 4) * sizeof(struct dbdma_cmd),
4912 GFP_KERNEL);
4913 if (awacs_tx_cmd_space == NULL) {
4914 printk(KERN_ERR "DMA sound driver: Not enough buffer memory, driver disabled!\n");
4915 return;
4917 awacs_tx_cmds = (volatile struct dbdma_cmd *)
4918 DBDMA_ALIGN(awacs_tx_cmd_space);
4919 awacs_reg[0] = MASK_MUX_CD;
4920 awacs_reg[1] = MASK_LOOPTHRU | MASK_PAROUT;
4921 /* get default volume from nvram */
4922 vol = (~nvram_read_byte(0x1308) & 7) << 1;
4923 awacs_reg[2] = vol + (vol << 6);
4924 awacs_reg[4] = vol + (vol << 6);
4925 out_le32(&awacs->control, 0x11);
4926 awacs_write(awacs_reg[0] + MASK_ADDR0);
4927 awacs_write(awacs_reg[1] + MASK_ADDR1);
4928 awacs_write(awacs_reg[2] + MASK_ADDR2);
4929 awacs_write(awacs_reg[4] + MASK_ADDR4);
4931 /* Initialize recent versions of the awacs */
4932 if (awacs_revision == 0) {
4933 awacs_revision =
4934 (in_le32(&awacs->codec_stat) >> 12) & 0xf;
4935 if (awacs_revision == 3) {
4936 awacs_write(0x6000);
4937 awacs_enable_amp(100 * 0x101);
4940 if (awacs_revision >= AWACS_BURGUNDY)
4941 awacs_burgundy_init();
4943 /* Initialize beep stuff */
4944 beep_dbdma_cmd = awacs_tx_cmds + (numBufs + 1);
4945 orig_mksound = kd_mksound;
4946 kd_mksound = awacs_mksound;
4947 beep_buf = (short *) kmalloc(BEEP_BUFLEN * 4, GFP_KERNEL);
4948 if (beep_buf == NULL)
4949 printk(KERN_WARNING "dmasound: no memory for "
4950 "beep buffer\n");
4951 #ifdef CONFIG_PMAC_PBOOK
4952 notifier_chain_register(&sleep_notifier_list,
4953 &awacs_sleep_notifier);
4954 #endif /* CONFIG_PMAC_PBOOK */
4956 #endif /* CONFIG_PPC */
4958 if (!has_sound)
4959 return;
4961 /* Set up sound queue, /dev/audio and /dev/dsp. */
4963 /* Set default settings. */
4964 sq_init();
4966 /* Set up /dev/sndstat. */
4967 state_init();
4969 /* Set up /dev/mixer. */
4970 mixer_init();
4972 if (!sound.mach.irqinit()) {
4973 printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n");
4974 return;
4976 #ifdef MODULE
4977 irq_installed = 1;
4978 #endif
4980 printk(KERN_INFO "DMA sound driver installed, using %d buffers of %dk.\n",
4981 numBufs, bufSize);
4983 return;
4987 #define MAXARGS 8 /* Should be sufficient for now */
4989 void __init dmasound_setup(char *str, int *ints)
4991 /* check the bootstrap parameter for "dmasound=" */
4993 switch (ints[0]) {
4994 case 3:
4995 if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
4996 printk("dmasound_setup: illegal catch radius, using default = %d\n", catchRadius);
4997 else
4998 catchRadius = ints[3];
4999 /* fall through */
5000 case 2:
5001 if (ints[1] < MIN_BUFFERS)
5002 printk("dmasound_setup: illegal number of buffers, using default = %d\n", numBufs);
5003 else
5004 numBufs = ints[1];
5005 if (ints[2] < MIN_BUFSIZE || ints[2] > MAX_BUFSIZE)
5006 printk("dmasound_setup: illegal buffer size, using default = %d\n", bufSize);
5007 else
5008 bufSize = ints[2];
5009 break;
5010 case 0:
5011 break;
5012 default:
5013 printk("dmasound_setup: illegal number of arguments\n");
5018 #ifdef MODULE
5020 int init_module(void)
5022 dmasound_init();
5023 return 0;
5027 void cleanup_module(void)
5029 if (irq_installed) {
5030 sound_silence();
5031 sound.mach.irqcleanup();
5034 sq_release_buffers();
5036 if (mixer_unit >= 0)
5037 unregister_sound_mixer(mixer_unit);
5038 if (state_unit >= 0)
5039 unregister_sound_special(state_unit);
5040 if (sq_unit >= 0)
5041 unregister_sound_dsp(sq_unit);
5044 #endif /* MODULE */