Import 2.1.118
[davej-history.git] / drivers / sound / dmasound.c
blobb351c73db614f0c62eec75227f320737b2c20d39
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 #ifdef __mc68000__
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_PMAC
111 #include <asm/prom.h>
112 #include <asm/io.h>
113 #include <asm/dbdma.h>
114 #ifdef CONFIG_PMAC_PBOOK
115 #include <asm/adb.h>
116 #include <asm/pmu.h>
117 #endif /* CONFIG_PMAC_PBOOK */
118 #include "awacs_defs.h"
119 #include <linux/nvram.h>
120 #include <linux/vt_kern.h>
121 #endif /* CONFIG_PMAC */
123 #include "dmasound.h"
124 #include <linux/soundcard.h>
126 #define HAS_8BIT_TABLES
128 #ifdef MODULE
129 static int sq_unit = -1;
130 static int mixer_unit = -1;
131 static int state_unit = -1;
132 static int irq_installed = 0;
133 #endif /* MODULE */
134 static char **sound_buffers = NULL;
137 #ifdef CONFIG_ATARI
138 extern void atari_microwire_cmd(int cmd);
139 #endif /* CONFIG_ATARI */
141 #ifdef CONFIG_AMIGA
143 * The minimum period for audio depends on htotal (for OCS/ECS/AGA)
144 * (Imported from arch/m68k/amiga/amisound.c)
147 extern volatile u_short amiga_audio_min_period;
151 * amiga_mksound() should be able to restore the period after beeping
152 * (Imported from arch/m68k/amiga/amisound.c)
155 extern u_short amiga_audio_period;
159 * Audio DMA masks
162 #define AMI_AUDIO_OFF (DMAF_AUD0 | DMAF_AUD1 | DMAF_AUD2 | DMAF_AUD3)
163 #define AMI_AUDIO_8 (DMAF_SETCLR | DMAF_MASTER | DMAF_AUD0 | DMAF_AUD1)
164 #define AMI_AUDIO_14 (AMI_AUDIO_8 | DMAF_AUD2 | DMAF_AUD3)
166 #endif /* CONFIG_AMIGA */
168 #ifdef CONFIG_PMAC
170 * Interrupt numbers and addresses, obtained from the device tree.
172 static int awacs_irq, awacs_tx_irq, awacs_rx_irq;
173 static volatile struct awacs_regs *awacs;
174 static volatile struct dbdma_regs *awacs_txdma, *awacs_rxdma;
175 static int awacs_rate_index;
176 static int awacs_subframe;
179 * Space for the DBDMA command blocks.
181 static void *awacs_tx_cmd_space;
182 static volatile struct dbdma_cmd *awacs_tx_cmds;
185 * Cached values of AWACS registers (we can't read them).
187 int awacs_reg[5];
189 #define HAS_16BIT_TABLES
190 #undef HAS_8BIT_TABLES
193 * Stuff for outputting a beep. The values range from -327 to +327
194 * so we can multiply by an amplitude in the range 0..100 to get a
195 * signed short value to put in the output buffer.
197 static short beep_wform[256] = {
198 0, 40, 79, 117, 153, 187, 218, 245,
199 269, 288, 304, 316, 323, 327, 327, 324,
200 318, 310, 299, 288, 275, 262, 249, 236,
201 224, 213, 204, 196, 190, 186, 183, 182,
202 182, 183, 186, 189, 192, 196, 200, 203,
203 206, 208, 209, 209, 209, 207, 204, 201,
204 197, 193, 188, 183, 179, 174, 170, 166,
205 163, 161, 160, 159, 159, 160, 161, 162,
206 164, 166, 168, 169, 171, 171, 171, 170,
207 169, 167, 163, 159, 155, 150, 144, 139,
208 133, 128, 122, 117, 113, 110, 107, 105,
209 103, 103, 103, 103, 104, 104, 105, 105,
210 105, 103, 101, 97, 92, 86, 78, 68,
211 58, 45, 32, 18, 3, -11, -26, -41,
212 -55, -68, -79, -88, -95, -100, -102, -102,
213 -99, -93, -85, -75, -62, -48, -33, -16,
214 0, 16, 33, 48, 62, 75, 85, 93,
215 99, 102, 102, 100, 95, 88, 79, 68,
216 55, 41, 26, 11, -3, -18, -32, -45,
217 -58, -68, -78, -86, -92, -97, -101, -103,
218 -105, -105, -105, -104, -104, -103, -103, -103,
219 -103, -105, -107, -110, -113, -117, -122, -128,
220 -133, -139, -144, -150, -155, -159, -163, -167,
221 -169, -170, -171, -171, -171, -169, -168, -166,
222 -164, -162, -161, -160, -159, -159, -160, -161,
223 -163, -166, -170, -174, -179, -183, -188, -193,
224 -197, -201, -204, -207, -209, -209, -209, -208,
225 -206, -203, -200, -196, -192, -189, -186, -183,
226 -182, -182, -183, -186, -190, -196, -204, -213,
227 -224, -236, -249, -262, -275, -288, -299, -310,
228 -318, -324, -327, -327, -323, -316, -304, -288,
229 -269, -245, -218, -187, -153, -117, -79, -40,
232 #define BEEP_SPEED 2 /* 22050 Hz sample rate */
233 #define BEEP_BUFLEN 512
234 #define BEEP_VOLUME 15 /* 0 - 100 */
236 static int beep_volume = BEEP_VOLUME;
237 static int beep_playing = 0;
238 static short *beep_buf;
239 static volatile struct dbdma_cmd *beep_dbdma_cmd;
240 static void (*orig_mksound)(unsigned int, unsigned int);
242 #ifdef CONFIG_PMAC_PBOOK
244 * Stuff for restoring after a sleep.
246 static int awacs_sleep_notify(struct notifier_block *, unsigned long, void *);
247 struct notifier_block awacs_sleep_notifier = {
248 awacs_sleep_notify
250 #endif /* CONFIG_PMAC_PBOOK */
252 #endif /* CONFIG_PMAC */
254 /*** Some declarations *******************************************************/
257 #define DMASND_TT 1
258 #define DMASND_FALCON 2
259 #define DMASND_AMIGA 3
260 #define DMASND_AWACS 4
262 #define MAX_CATCH_RADIUS 10
263 #define MIN_BUFFERS 4
264 #define MIN_BUFSIZE 4
265 #define MAX_BUFSIZE 128 /* Limit for Amiga */
267 static int catchRadius = 0, numBufs = 4, bufSize = 32;
268 MODULE_PARM(catchRadius, "i");
269 MODULE_PARM(numBufs, "i");
270 MODULE_PARM(bufSize, "i");
272 #define arraysize(x) (sizeof(x)/sizeof(*(x)))
273 #define min(x, y) ((x) < (y) ? (x) : (y))
274 #define le2be16(x) (((x)<<8 & 0xff00) | ((x)>>8 & 0x00ff))
275 #define le2be16dbl(x) (((x)<<8 & 0xff00ff00) | ((x)>>8 & 0x00ff00ff))
277 #define IOCTL_IN(arg, ret) \
278 do { int error = get_user(ret, (int *)(arg)); \
279 if (error) return error; \
280 } while (0)
281 #define IOCTL_OUT(arg, ret) ioctl_return((int *)(arg), ret)
284 /*** Some low level helpers **************************************************/
286 #ifdef HAS_8BIT_TABLES
287 /* 8 bit mu-law */
289 static char ulaw2dma8[] = {
290 -126, -122, -118, -114, -110, -106, -102, -98,
291 -94, -90, -86, -82, -78, -74, -70, -66,
292 -63, -61, -59, -57, -55, -53, -51, -49,
293 -47, -45, -43, -41, -39, -37, -35, -33,
294 -31, -30, -29, -28, -27, -26, -25, -24,
295 -23, -22, -21, -20, -19, -18, -17, -16,
296 -16, -15, -15, -14, -14, -13, -13, -12,
297 -12, -11, -11, -10, -10, -9, -9, -8,
298 -8, -8, -7, -7, -7, -7, -6, -6,
299 -6, -6, -5, -5, -5, -5, -4, -4,
300 -4, -4, -4, -4, -3, -3, -3, -3,
301 -3, -3, -3, -3, -2, -2, -2, -2,
302 -2, -2, -2, -2, -2, -2, -2, -2,
303 -1, -1, -1, -1, -1, -1, -1, -1,
304 -1, -1, -1, -1, -1, -1, -1, -1,
305 -1, -1, -1, -1, -1, -1, -1, 0,
306 125, 121, 117, 113, 109, 105, 101, 97,
307 93, 89, 85, 81, 77, 73, 69, 65,
308 62, 60, 58, 56, 54, 52, 50, 48,
309 46, 44, 42, 40, 38, 36, 34, 32,
310 30, 29, 28, 27, 26, 25, 24, 23,
311 22, 21, 20, 19, 18, 17, 16, 15,
312 15, 14, 14, 13, 13, 12, 12, 11,
313 11, 10, 10, 9, 9, 8, 8, 7,
314 7, 7, 6, 6, 6, 6, 5, 5,
315 5, 5, 4, 4, 4, 4, 3, 3,
316 3, 3, 3, 3, 2, 2, 2, 2,
317 2, 2, 2, 2, 1, 1, 1, 1,
318 1, 1, 1, 1, 1, 1, 1, 1,
319 0, 0, 0, 0, 0, 0, 0, 0,
320 0, 0, 0, 0, 0, 0, 0, 0,
321 0, 0, 0, 0, 0, 0, 0, 0
324 /* 8 bit A-law */
326 static char alaw2dma8[] = {
327 -22, -21, -24, -23, -18, -17, -20, -19,
328 -30, -29, -32, -31, -26, -25, -28, -27,
329 -11, -11, -12, -12, -9, -9, -10, -10,
330 -15, -15, -16, -16, -13, -13, -14, -14,
331 -86, -82, -94, -90, -70, -66, -78, -74,
332 -118, -114, -126, -122, -102, -98, -110, -106,
333 -43, -41, -47, -45, -35, -33, -39, -37,
334 -59, -57, -63, -61, -51, -49, -55, -53,
335 -2, -2, -2, -2, -2, -2, -2, -2,
336 -2, -2, -2, -2, -2, -2, -2, -2,
337 -1, -1, -1, -1, -1, -1, -1, -1,
338 -1, -1, -1, -1, -1, -1, -1, -1,
339 -6, -6, -6, -6, -5, -5, -5, -5,
340 -8, -8, -8, -8, -7, -7, -7, -7,
341 -3, -3, -3, -3, -3, -3, -3, -3,
342 -4, -4, -4, -4, -4, -4, -4, -4,
343 21, 20, 23, 22, 17, 16, 19, 18,
344 29, 28, 31, 30, 25, 24, 27, 26,
345 10, 10, 11, 11, 8, 8, 9, 9,
346 14, 14, 15, 15, 12, 12, 13, 13,
347 86, 82, 94, 90, 70, 66, 78, 74,
348 118, 114, 126, 122, 102, 98, 110, 106,
349 43, 41, 47, 45, 35, 33, 39, 37,
350 59, 57, 63, 61, 51, 49, 55, 53,
351 1, 1, 1, 1, 1, 1, 1, 1,
352 1, 1, 1, 1, 1, 1, 1, 1,
353 0, 0, 0, 0, 0, 0, 0, 0,
354 0, 0, 0, 0, 0, 0, 0, 0,
355 5, 5, 5, 5, 4, 4, 4, 4,
356 7, 7, 7, 7, 6, 6, 6, 6,
357 2, 2, 2, 2, 2, 2, 2, 2,
358 3, 3, 3, 3, 3, 3, 3, 3
360 #endif /* HAS_8BIT_TABLES */
362 #ifdef HAS_16BIT_TABLES
364 /* 16 bit mu-law */
366 static short ulaw2dma16[] = {
367 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
368 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
369 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
370 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
371 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
372 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
373 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
374 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
375 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
376 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
377 -876, -844, -812, -780, -748, -716, -684, -652,
378 -620, -588, -556, -524, -492, -460, -428, -396,
379 -372, -356, -340, -324, -308, -292, -276, -260,
380 -244, -228, -212, -196, -180, -164, -148, -132,
381 -120, -112, -104, -96, -88, -80, -72, -64,
382 -56, -48, -40, -32, -24, -16, -8, 0,
383 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
384 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
385 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
386 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
387 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
388 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
389 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
390 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
391 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
392 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
393 876, 844, 812, 780, 748, 716, 684, 652,
394 620, 588, 556, 524, 492, 460, 428, 396,
395 372, 356, 340, 324, 308, 292, 276, 260,
396 244, 228, 212, 196, 180, 164, 148, 132,
397 120, 112, 104, 96, 88, 80, 72, 64,
398 56, 48, 40, 32, 24, 16, 8, 0,
401 /* 16 bit A-law */
403 static short alaw2dma16[] = {
404 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
405 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
406 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
407 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
408 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
409 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
410 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
411 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
412 -344, -328, -376, -360, -280, -264, -312, -296,
413 -472, -456, -504, -488, -408, -392, -440, -424,
414 -88, -72, -120, -104, -24, -8, -56, -40,
415 -216, -200, -248, -232, -152, -136, -184, -168,
416 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
417 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
418 -688, -656, -752, -720, -560, -528, -624, -592,
419 -944, -912, -1008, -976, -816, -784, -880, -848,
420 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
421 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
422 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
423 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
424 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
425 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
426 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
427 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
428 344, 328, 376, 360, 280, 264, 312, 296,
429 472, 456, 504, 488, 408, 392, 440, 424,
430 88, 72, 120, 104, 24, 8, 56, 40,
431 216, 200, 248, 232, 152, 136, 184, 168,
432 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
433 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
434 688, 656, 752, 720, 560, 528, 624, 592,
435 944, 912, 1008, 976, 816, 784, 880, 848,
437 #endif /* HAS_16BIT_TABLES */
440 #ifdef HAS_14BIT_TABLES
442 /* 14 bit mu-law (LSB) */
444 static char alaw2dma14l[] = {
445 33, 33, 33, 33, 33, 33, 33, 33,
446 33, 33, 33, 33, 33, 33, 33, 33,
447 33, 33, 33, 33, 33, 33, 33, 33,
448 33, 33, 33, 33, 33, 33, 33, 33,
449 1, 1, 1, 1, 1, 1, 1, 1,
450 1, 1, 1, 1, 1, 1, 1, 1,
451 49, 17, 49, 17, 49, 17, 49, 17,
452 49, 17, 49, 17, 49, 17, 49, 17,
453 41, 57, 9, 25, 41, 57, 9, 25,
454 41, 57, 9, 25, 41, 57, 9, 25,
455 37, 45, 53, 61, 5, 13, 21, 29,
456 37, 45, 53, 61, 5, 13, 21, 29,
457 35, 39, 43, 47, 51, 55, 59, 63,
458 3, 7, 11, 15, 19, 23, 27, 31,
459 34, 36, 38, 40, 42, 44, 46, 48,
460 50, 52, 54, 56, 58, 60, 62, 0,
461 31, 31, 31, 31, 31, 31, 31, 31,
462 31, 31, 31, 31, 31, 31, 31, 31,
463 31, 31, 31, 31, 31, 31, 31, 31,
464 31, 31, 31, 31, 31, 31, 31, 31,
465 63, 63, 63, 63, 63, 63, 63, 63,
466 63, 63, 63, 63, 63, 63, 63, 63,
467 15, 47, 15, 47, 15, 47, 15, 47,
468 15, 47, 15, 47, 15, 47, 15, 47,
469 23, 7, 55, 39, 23, 7, 55, 39,
470 23, 7, 55, 39, 23, 7, 55, 39,
471 27, 19, 11, 3, 59, 51, 43, 35,
472 27, 19, 11, 3, 59, 51, 43, 35,
473 29, 25, 21, 17, 13, 9, 5, 1,
474 61, 57, 53, 49, 45, 41, 37, 33,
475 30, 28, 26, 24, 22, 20, 18, 16,
476 14, 12, 10, 8, 6, 4, 2, 0
479 /* 14 bit A-law (LSB) */
481 static char alaw2dma14l[] = {
482 32, 32, 32, 32, 32, 32, 32, 32,
483 32, 32, 32, 32, 32, 32, 32, 32,
484 16, 48, 16, 48, 16, 48, 16, 48,
485 16, 48, 16, 48, 16, 48, 16, 48,
486 0, 0, 0, 0, 0, 0, 0, 0,
487 0, 0, 0, 0, 0, 0, 0, 0,
488 0, 0, 0, 0, 0, 0, 0, 0,
489 0, 0, 0, 0, 0, 0, 0, 0,
490 42, 46, 34, 38, 58, 62, 50, 54,
491 10, 14, 2, 6, 26, 30, 18, 22,
492 42, 46, 34, 38, 58, 62, 50, 54,
493 10, 14, 2, 6, 26, 30, 18, 22,
494 40, 56, 8, 24, 40, 56, 8, 24,
495 40, 56, 8, 24, 40, 56, 8, 24,
496 20, 28, 4, 12, 52, 60, 36, 44,
497 20, 28, 4, 12, 52, 60, 36, 44,
498 32, 32, 32, 32, 32, 32, 32, 32,
499 32, 32, 32, 32, 32, 32, 32, 32,
500 48, 16, 48, 16, 48, 16, 48, 16,
501 48, 16, 48, 16, 48, 16, 48, 16,
502 0, 0, 0, 0, 0, 0, 0, 0,
503 0, 0, 0, 0, 0, 0, 0, 0,
504 0, 0, 0, 0, 0, 0, 0, 0,
505 0, 0, 0, 0, 0, 0, 0, 0,
506 22, 18, 30, 26, 6, 2, 14, 10,
507 54, 50, 62, 58, 38, 34, 46, 42,
508 22, 18, 30, 26, 6, 2, 14, 10,
509 54, 50, 62, 58, 38, 34, 46, 42,
510 24, 8, 56, 40, 24, 8, 56, 40,
511 24, 8, 56, 40, 24, 8, 56, 40,
512 44, 36, 60, 52, 12, 4, 28, 20,
513 44, 36, 60, 52, 12, 4, 28, 20
515 #endif /* HAS_14BIT_TABLES */
518 /*** Translations ************************************************************/
521 #ifdef CONFIG_ATARI
522 static ssize_t ata_ct_law(const u_char *userPtr, size_t userCount,
523 u_char frame[], ssize_t *frameUsed,
524 ssize_t frameLeft);
525 static ssize_t ata_ct_s8(const u_char *userPtr, size_t userCount,
526 u_char frame[], ssize_t *frameUsed,
527 ssize_t frameLeft);
528 static ssize_t ata_ct_u8(const u_char *userPtr, size_t userCount,
529 u_char frame[], ssize_t *frameUsed,
530 ssize_t frameLeft);
531 static ssize_t ata_ct_s16be(const u_char *userPtr, size_t userCount,
532 u_char frame[], ssize_t *frameUsed,
533 ssize_t frameLeft);
534 static ssize_t ata_ct_u16be(const u_char *userPtr, size_t userCount,
535 u_char frame[], ssize_t *frameUsed,
536 ssize_t frameLeft);
537 static ssize_t ata_ct_s16le(const u_char *userPtr, size_t userCount,
538 u_char frame[], ssize_t *frameUsed,
539 ssize_t frameLeft);
540 static ssize_t ata_ct_u16le(const u_char *userPtr, size_t userCount,
541 u_char frame[], ssize_t *frameUsed,
542 ssize_t frameLeft);
543 static ssize_t ata_ctx_law(const u_char *userPtr, size_t userCount,
544 u_char frame[], ssize_t *frameUsed,
545 ssize_t frameLeft);
546 static ssize_t ata_ctx_s8(const u_char *userPtr, size_t userCount,
547 u_char frame[], ssize_t *frameUsed,
548 ssize_t frameLeft);
549 static ssize_t ata_ctx_u8(const u_char *userPtr, size_t userCount,
550 u_char frame[], ssize_t *frameUsed,
551 ssize_t frameLeft);
552 static ssize_t ata_ctx_s16be(const u_char *userPtr, size_t userCount,
553 u_char frame[], ssize_t *frameUsed,
554 ssize_t frameLeft);
555 static ssize_t ata_ctx_u16be(const u_char *userPtr, size_t userCount,
556 u_char frame[], ssize_t *frameUsed,
557 ssize_t frameLeft);
558 static ssize_t ata_ctx_s16le(const u_char *userPtr, size_t userCount,
559 u_char frame[], ssize_t *frameUsed,
560 ssize_t frameLeft);
561 static ssize_t ata_ctx_u16le(const u_char *userPtr, size_t userCount,
562 u_char frame[], ssize_t *frameUsed,
563 ssize_t frameLeft);
564 #endif /* CONFIG_ATARI */
566 #ifdef CONFIG_AMIGA
567 static ssize_t ami_ct_law(const u_char *userPtr, size_t userCount,
568 u_char frame[], ssize_t *frameUsed,
569 ssize_t frameLeft);
570 static ssize_t ami_ct_s8(const u_char *userPtr, size_t userCount,
571 u_char frame[], ssize_t *frameUsed,
572 ssize_t frameLeft);
573 static ssize_t ami_ct_u8(const u_char *userPtr, size_t userCount,
574 u_char frame[], ssize_t *frameUsed,
575 ssize_t frameLeft);
576 static ssize_t ami_ct_s16be(const u_char *userPtr, size_t userCount,
577 u_char frame[], ssize_t *frameUsed,
578 ssize_t frameLeft);
579 static ssize_t ami_ct_u16be(const u_char *userPtr, size_t userCount,
580 u_char frame[], ssize_t *frameUsed,
581 ssize_t frameLeft);
582 static ssize_t ami_ct_s16le(const u_char *userPtr, size_t userCount,
583 u_char frame[], ssize_t *frameUsed,
584 ssize_t frameLeft);
585 static ssize_t ami_ct_u16le(const u_char *userPtr, size_t userCount,
586 u_char frame[], ssize_t *frameUsed,
587 ssize_t frameLeft);
588 #endif /* CONFIG_AMIGA */
590 #ifdef CONFIG_PMAC
591 static ssize_t pmac_ct_law(const u_char *userPtr, size_t userCount,
592 u_char frame[], ssize_t *frameUsed,
593 ssize_t frameLeft);
594 static ssize_t pmac_ct_s8(const u_char *userPtr, size_t userCount,
595 u_char frame[], ssize_t *frameUsed,
596 ssize_t frameLeft);
597 static ssize_t pmac_ct_u8(const u_char *userPtr, size_t userCount,
598 u_char frame[], ssize_t *frameUsed,
599 ssize_t frameLeft);
600 static ssize_t pmac_ct_s16(const u_char *userPtr, size_t userCount,
601 u_char frame[], ssize_t *frameUsed,
602 ssize_t frameLeft);
603 static ssize_t pmac_ct_u16(const u_char *userPtr, size_t userCount,
604 u_char frame[], ssize_t *frameUsed,
605 ssize_t frameLeft);
606 static ssize_t pmac_ctx_law(const u_char *userPtr, size_t userCount,
607 u_char frame[], ssize_t *frameUsed,
608 ssize_t frameLeft);
609 static ssize_t pmac_ctx_s8(const u_char *userPtr, size_t userCount,
610 u_char frame[], ssize_t *frameUsed,
611 ssize_t frameLeft);
612 static ssize_t pmac_ctx_u8(const u_char *userPtr, size_t userCount,
613 u_char frame[], ssize_t *frameUsed,
614 ssize_t frameLeft);
615 static ssize_t pmac_ctx_s16(const u_char *userPtr, size_t userCount,
616 u_char frame[], ssize_t *frameUsed,
617 ssize_t frameLeft);
618 static ssize_t pmac_ctx_u16(const u_char *userPtr, size_t userCount,
619 u_char frame[], ssize_t *frameUsed,
620 ssize_t frameLeft);
621 #endif /* CONFIG_PMAC */
623 /*** Machine definitions *****************************************************/
626 typedef struct {
627 int type;
628 void *(*dma_alloc)(unsigned int, int);
629 void (*dma_free)(void *, unsigned int);
630 int (*irqinit)(void);
631 #ifdef MODULE
632 void (*irqcleanup)(void);
633 #endif /* MODULE */
634 void (*init)(void);
635 void (*silence)(void);
636 int (*setFormat)(int);
637 int (*setVolume)(int);
638 int (*setBass)(int);
639 int (*setTreble)(int);
640 int (*setGain)(int);
641 void (*play)(void);
642 } MACHINE;
645 /*** Low level stuff *********************************************************/
648 typedef struct {
649 int format; /* AFMT_* */
650 int stereo; /* 0 = mono, 1 = stereo */
651 int size; /* 8/16 bit*/
652 int speed; /* speed */
653 } SETTINGS;
655 typedef struct {
656 ssize_t (*ct_ulaw)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
657 ssize_t (*ct_alaw)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
658 ssize_t (*ct_s8)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
659 ssize_t (*ct_u8)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
660 ssize_t (*ct_s16be)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
661 ssize_t (*ct_u16be)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
662 ssize_t (*ct_s16le)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
663 ssize_t (*ct_u16le)(const u_char *, size_t, u_char *, ssize_t *, ssize_t);
664 } TRANS;
666 struct sound_settings {
667 MACHINE mach; /* machine dependent things */
668 SETTINGS hard; /* hardware settings */
669 SETTINGS soft; /* software settings */
670 SETTINGS dsp; /* /dev/dsp default settings */
671 TRANS *trans; /* supported translations */
672 int volume_left; /* volume (range is machine dependent) */
673 int volume_right;
674 int bass; /* tone (range is machine dependent) */
675 int treble;
676 int gain;
677 int minDev; /* minor device number currently open */
678 #if defined(CONFIG_ATARI) || defined(CONFIG_PMAC)
679 int bal; /* balance factor for expanding (not volume!) */
680 u_long data; /* data for expanding */
681 #endif /* CONFIG_ATARI */
684 static struct sound_settings sound;
687 #ifdef CONFIG_ATARI
688 static void *AtaAlloc(unsigned int size, int flags) __init;
689 static void AtaFree(void *, unsigned int size) __init;
690 static int AtaIrqInit(void) __init;
691 #ifdef MODULE
692 static void AtaIrqCleanUp(void);
693 #endif /* MODULE */
694 static int AtaSetBass(int bass);
695 static int AtaSetTreble(int treble);
696 static void TTSilence(void);
697 static void TTInit(void);
698 static int TTSetFormat(int format);
699 static int TTSetVolume(int volume);
700 static int TTSetGain(int gain);
701 static void FalconSilence(void);
702 static void FalconInit(void);
703 static int FalconSetFormat(int format);
704 static int FalconSetVolume(int volume);
705 static void ata_sq_play_next_frame(int index);
706 static void AtaPlay(void);
707 static void ata_sq_interrupt(int irq, void *dummy, struct pt_regs *fp);
708 #endif /* CONFIG_ATARI */
710 #ifdef CONFIG_AMIGA
711 static void *AmiAlloc(unsigned int size, int flags) __init;
712 static void AmiFree(void *, unsigned int) __init;
713 static int AmiIrqInit(void) __init;
714 #ifdef MODULE
715 static void AmiIrqCleanUp(void);
716 #endif /* MODULE */
717 static void AmiSilence(void);
718 static void AmiInit(void);
719 static int AmiSetFormat(int format);
720 static int AmiSetVolume(int volume);
721 static int AmiSetTreble(int treble);
722 static void ami_sq_play_next_frame(int index);
723 static void AmiPlay(void);
724 static void ami_sq_interrupt(int irq, void *dummy, struct pt_regs *fp);
725 #endif /* CONFIG_AMIGA */
727 #ifdef CONFIG_PMAC
728 static void *PMacAlloc(unsigned int size, int flags) __init;
729 static void PMacFree(void *ptr, unsigned int size) __init;
730 static int PMacIrqInit(void) __init;
731 #ifdef MODULE
732 static void PMacIrqCleanup(void);
733 #endif /* MODULE */
734 static void PMacSilence(void);
735 static void PMacInit(void);
736 static void PMacPlay(void);
737 static int PMacSetFormat(int format);
738 static int PMacSetVolume(int volume);
739 static void pmac_awacs_tx_intr(int irq, void *devid, struct pt_regs *regs);
740 static void pmac_awacs_intr(int irq, void *devid, struct pt_regs *regs);
741 static void awacs_write(int val);
742 static int awacs_get_volume(int reg, int lshift);
743 static int awacs_volume_setter(int volume, int n, int mute, int lshift);
744 static void awacs_mksound(unsigned int hz, unsigned int ticks);
745 static void awacs_nosound(unsigned long xx);
746 #endif /* CONFIG_PMAC */
748 /*** Mid level stuff *********************************************************/
751 static void sound_silence(void);
752 static void sound_init(void);
753 static int sound_set_format(int format);
754 static int sound_set_speed(int speed);
755 static int sound_set_stereo(int stereo);
756 static int sound_set_volume(int volume);
757 #ifdef CONFIG_ATARI
758 static int sound_set_bass(int bass);
759 #endif /* CONFIG_ATARI */
760 #if defined(CONFIG_ATARI) || defined(CONFIG_AMIGA)
761 static int sound_set_treble(int treble);
762 #endif /* CONFIG_ATARI || CONFIG_AMIGA */
763 static ssize_t sound_copy_translate(const u_char *userPtr,
764 size_t userCount,
765 u_char frame[], ssize_t *frameUsed,
766 ssize_t frameLeft);
770 * /dev/mixer abstraction
773 struct sound_mixer {
774 int busy;
777 static struct sound_mixer mixer;
780 * Sound queue stuff, the heart of the driver
783 struct sound_queue {
784 int max_count, block_size;
785 char **buffers;
786 int max_active;
788 /* it shouldn't be necessary to declare any of these volatile */
789 int front, rear, count;
790 int rear_size;
792 * The use of the playing field depends on the hardware
794 * Atari, PMac: The number of frames that are loaded/playing
796 * Amiga: Bit 0 is set: a frame is loaded
797 * Bit 1 is set: a frame is playing
799 int playing;
800 struct wait_queue *write_queue, *open_queue, *sync_queue;
801 int open_mode;
802 int busy, syncing;
803 #ifdef CONFIG_ATARI
804 int ignore_int; /* ++TeSche: used for Falcon */
805 #endif /* CONFIG_ATARI */
806 #ifdef CONFIG_AMIGA
807 int block_size_half, block_size_quarter;
808 #endif /* CONFIG_AMIGA */
811 static struct sound_queue sq;
813 #define sq_block_address(i) (sq.buffers[i])
814 #define SIGNAL_RECEIVED (signal_pending(current))
815 #define NON_BLOCKING(open_mode) (open_mode & O_NONBLOCK)
816 #define ONE_SECOND HZ /* in jiffies (100ths of a second) */
817 #define NO_TIME_LIMIT 0xffffffff
818 #define SLEEP(queue, time_limit) \
819 current->timeout = jiffies+(time_limit); \
820 interruptible_sleep_on(&queue);
821 #define WAKE_UP(queue) (wake_up_interruptible(&queue))
824 * /dev/sndstat
827 struct sound_state {
828 int busy;
829 char buf[512];
830 int len, ptr;
833 static struct sound_state state;
835 /*** Common stuff ********************************************************/
837 static long long sound_lseek(struct file *file, long long offset, int orig);
838 static inline int ioctl_return(int *addr, int value)
840 if (value < 0)
841 return(value);
843 return put_user(value, addr)? -EFAULT: 0;
847 /*** Config & Setup **********************************************************/
850 void dmasound_init(void);
851 void dmasound_setup(char *str, int *ints);
854 /*** Translations ************************************************************/
857 /* ++TeSche: radically changed for new expanding purposes...
859 * These two routines now deal with copying/expanding/translating the samples
860 * from user space into our buffer at the right frequency. They take care about
861 * how much data there's actually to read, how much buffer space there is and
862 * to convert samples into the right frequency/encoding. They will only work on
863 * complete samples so it may happen they leave some bytes in the input stream
864 * if the user didn't write a multiple of the current sample size. They both
865 * return the number of bytes they've used from both streams so you may detect
866 * such a situation. Luckily all programs should be able to cope with that.
868 * I think I've optimized anything as far as one can do in plain C, all
869 * variables should fit in registers and the loops are really short. There's
870 * one loop for every possible situation. Writing a more generalized and thus
871 * parameterized loop would only produce slower code. Feel free to optimize
872 * this in assembler if you like. :)
874 * I think these routines belong here because they're not yet really hardware
875 * independent, especially the fact that the Falcon can play 16bit samples
876 * only in stereo is hardcoded in both of them!
878 * ++geert: split in even more functions (one per format)
881 #ifdef CONFIG_ATARI
882 static ssize_t ata_ct_law(const u_char *userPtr, size_t userCount,
883 u_char frame[], ssize_t *frameUsed,
884 ssize_t frameLeft)
886 char *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma8 : alaw2dma8;
887 ssize_t count, used;
888 u_char *p = &frame[*frameUsed];
890 count = min(userCount, frameLeft);
891 if (sound.soft.stereo)
892 count &= ~1;
893 used = count;
894 while (count > 0) {
895 u_char data;
896 if (get_user(data, userPtr++))
897 return -EFAULT;
898 *p++ = table[data];
899 count--;
901 *frameUsed += used;
902 return used;
906 static ssize_t ata_ct_s8(const u_char *userPtr, size_t userCount,
907 u_char frame[], ssize_t *frameUsed,
908 ssize_t frameLeft)
910 ssize_t count, used;
911 void *p = &frame[*frameUsed];
913 count = min(userCount, frameLeft);
914 if (sound.soft.stereo)
915 count &= ~1;
916 used = count;
917 if (copy_from_user(p, userPtr, count))
918 return -EFAULT;
919 *frameUsed += used;
920 return(used);
924 static ssize_t ata_ct_u8(const u_char *userPtr, size_t userCount,
925 u_char frame[], ssize_t *frameUsed,
926 ssize_t frameLeft)
928 ssize_t count, used;
930 if (!sound.soft.stereo) {
931 u_char *p = &frame[*frameUsed];
932 count = min(userCount, frameLeft);
933 used = count;
934 while (count > 0) {
935 u_char data;
936 if (get_user(data, userPtr++))
937 return -EFAULT;
938 *p++ = data ^ 0x80;
939 count--;
941 } else {
942 u_short *p = (u_short *)&frame[*frameUsed];
943 count = min(userCount, frameLeft)>>1;
944 used = count*2;
945 while (count > 0) {
946 u_short data;
947 if (get_user(data, ((u_short *)userPtr)++))
948 return -EFAULT;
949 *p++ = data ^ 0x8080;
950 count--;
953 *frameUsed += used;
954 return(used);
958 static ssize_t ata_ct_s16be(const u_char *userPtr, size_t userCount,
959 u_char frame[], ssize_t *frameUsed,
960 ssize_t frameLeft)
962 ssize_t count, used;
964 if (!sound.soft.stereo) {
965 u_short *p = (u_short *)&frame[*frameUsed];
966 count = min(userCount, frameLeft)>>1;
967 used = count*2;
968 while (count > 0) {
969 u_short data;
970 if (get_user(data, ((u_short *)userPtr)++))
971 return -EFAULT;
972 *p++ = data;
973 *p++ = data;
974 count--;
976 *frameUsed += used*2;
977 } else {
978 void *p = (u_short *)&frame[*frameUsed];
979 count = min(userCount, frameLeft) & ~3;
980 used = count;
981 if (copy_from_user(p, userPtr, count))
982 return -EFAULT;
983 *frameUsed += used;
985 return(used);
989 static ssize_t ata_ct_u16be(const u_char *userPtr, size_t userCount,
990 u_char frame[], ssize_t *frameUsed,
991 ssize_t frameLeft)
993 ssize_t count, used;
995 if (!sound.soft.stereo) {
996 u_short *p = (u_short *)&frame[*frameUsed];
997 count = min(userCount, frameLeft)>>1;
998 used = count*2;
999 while (count > 0) {
1000 u_short data;
1001 if (get_user(data, ((u_short *)userPtr)++))
1002 return -EFAULT;
1003 data ^= 0x8000;
1004 *p++ = data;
1005 *p++ = data;
1006 count--;
1008 *frameUsed += used*2;
1009 } else {
1010 u_long *p = (u_long *)&frame[*frameUsed];
1011 count = min(userCount, frameLeft)>>2;
1012 used = count*4;
1013 while (count > 0) {
1014 u_long data;
1015 if (get_user(data, ((u_int *)userPtr)++))
1016 return -EFAULT;
1017 *p++ = data ^ 0x80008000;
1018 count--;
1020 *frameUsed += used;
1022 return(used);
1026 static ssize_t ata_ct_s16le(const u_char *userPtr, size_t userCount,
1027 u_char frame[], ssize_t *frameUsed,
1028 ssize_t frameLeft)
1030 ssize_t count, used;
1032 count = frameLeft;
1033 if (!sound.soft.stereo) {
1034 u_short *p = (u_short *)&frame[*frameUsed];
1035 count = min(userCount, frameLeft)>>1;
1036 used = count*2;
1037 while (count > 0) {
1038 u_short data;
1039 if (get_user(data, ((u_short *)userPtr)++))
1040 return -EFAULT;
1041 data = le2be16(data);
1042 *p++ = data;
1043 *p++ = data;
1044 count--;
1046 *frameUsed += used*2;
1047 } else {
1048 u_long *p = (u_long *)&frame[*frameUsed];
1049 count = min(userCount, frameLeft)>>2;
1050 used = count*4;
1051 while (count > 0) {
1052 u_long data;
1053 if (get_user(data, ((u_int *)userPtr)++))
1054 return -EFAULT;
1055 data = le2be16dbl(data);
1056 *p++ = data;
1057 count--;
1059 *frameUsed += used;
1061 return(used);
1065 static ssize_t ata_ct_u16le(const u_char *userPtr, size_t userCount,
1066 u_char frame[], ssize_t *frameUsed,
1067 ssize_t frameLeft)
1069 ssize_t count, used;
1071 count = frameLeft;
1072 if (!sound.soft.stereo) {
1073 u_short *p = (u_short *)&frame[*frameUsed];
1074 count = min(userCount, frameLeft)>>1;
1075 used = count*2;
1076 while (count > 0) {
1077 u_short data;
1078 if (get_user(data, ((u_short *)userPtr)++))
1079 return -EFAULT;
1080 data = le2be16(data) ^ 0x8000;
1081 *p++ = data;
1082 *p++ = data;
1084 *frameUsed += used*2;
1085 } else {
1086 u_long *p = (u_long *)&frame[*frameUsed];
1087 count = min(userCount, frameLeft)>>2;
1088 used = count;
1089 while (count > 0) {
1090 u_long data;
1091 if (get_user(data, ((u_int *)userPtr)++))
1092 return -EFAULT;
1093 data = le2be16dbl(data) ^ 0x80008000;
1094 *p++ = data;
1095 count--;
1097 *frameUsed += used;
1099 return(used);
1103 static ssize_t ata_ctx_law(const u_char *userPtr, size_t userCount,
1104 u_char frame[], ssize_t *frameUsed,
1105 ssize_t frameLeft)
1107 char *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma8 : alaw2dma8;
1108 /* this should help gcc to stuff everything into registers */
1109 long bal = sound.bal;
1110 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1111 ssize_t used, usedf;
1113 used = userCount;
1114 usedf = frameLeft;
1115 if (!sound.soft.stereo) {
1116 u_char *p = &frame[*frameUsed];
1117 u_char data = sound.data;
1118 while (frameLeft) {
1119 u_char c;
1120 if (bal < 0) {
1121 if (!userCount)
1122 break;
1123 if (get_user(c, userPtr++))
1124 return -EFAULT;
1125 data = table[c];
1126 userCount--;
1127 bal += hSpeed;
1129 *p++ = data;
1130 frameLeft--;
1131 bal -= sSpeed;
1133 sound.data = data;
1134 } else {
1135 u_short *p = (u_short *)&frame[*frameUsed];
1136 u_short data = sound.data;
1137 while (frameLeft >= 2) {
1138 u_char c;
1139 if (bal < 0) {
1140 if (userCount < 2)
1141 break;
1142 if (get_user(c, userPtr++))
1143 return -EFAULT;
1144 data = table[c] << 8;
1145 if (get_user(c, userPtr++))
1146 return -EFAULT;
1147 data |= table[c];
1148 userCount -= 2;
1149 bal += hSpeed;
1151 *p++ = data;
1152 frameLeft -= 2;
1153 bal -= sSpeed;
1155 sound.data = data;
1157 sound.bal = bal;
1158 used -= userCount;
1159 *frameUsed += usedf-frameLeft;
1160 return(used);
1164 static ssize_t ata_ctx_s8(const u_char *userPtr, size_t userCount,
1165 u_char frame[], ssize_t *frameUsed,
1166 ssize_t frameLeft)
1168 /* this should help gcc to stuff everything into registers */
1169 long bal = sound.bal;
1170 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1171 ssize_t used, usedf;
1173 used = userCount;
1174 usedf = frameLeft;
1175 if (!sound.soft.stereo) {
1176 u_char *p = &frame[*frameUsed];
1177 u_char data = sound.data;
1178 while (frameLeft) {
1179 if (bal < 0) {
1180 if (!userCount)
1181 break;
1182 if (get_user(data, userPtr++))
1183 return -EFAULT;
1184 userCount--;
1185 bal += hSpeed;
1187 *p++ = data;
1188 frameLeft--;
1189 bal -= sSpeed;
1191 sound.data = data;
1192 } else {
1193 u_short *p = (u_short *)&frame[*frameUsed];
1194 u_short data = sound.data;
1195 while (frameLeft >= 2) {
1196 if (bal < 0) {
1197 if (userCount < 2)
1198 break;
1199 if (get_user(data, ((u_short *)userPtr)++))
1200 return -EFAULT;
1201 userCount -= 2;
1202 bal += hSpeed;
1204 *p++ = data;
1205 frameLeft -= 2;
1206 bal -= sSpeed;
1208 sound.data = data;
1210 sound.bal = bal;
1211 used -= userCount;
1212 *frameUsed += usedf-frameLeft;
1213 return(used);
1217 static ssize_t ata_ctx_u8(const u_char *userPtr, size_t userCount,
1218 u_char frame[], ssize_t *frameUsed,
1219 ssize_t frameLeft)
1221 /* this should help gcc to stuff everything into registers */
1222 long bal = sound.bal;
1223 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1224 ssize_t used, usedf;
1226 used = userCount;
1227 usedf = frameLeft;
1228 if (!sound.soft.stereo) {
1229 u_char *p = &frame[*frameUsed];
1230 u_char data = sound.data;
1231 while (frameLeft) {
1232 if (bal < 0) {
1233 if (!userCount)
1234 break;
1235 if (get_user(data, userPtr++))
1236 return -EFAULT;
1237 data ^= 0x80;
1238 userCount--;
1239 bal += hSpeed;
1241 *p++ = data;
1242 frameLeft--;
1243 bal -= sSpeed;
1245 sound.data = data;
1246 } else {
1247 u_short *p = (u_short *)&frame[*frameUsed];
1248 u_short data = sound.data;
1249 while (frameLeft >= 2) {
1250 if (bal < 0) {
1251 if (userCount < 2)
1252 break;
1253 if (get_user(data, ((u_short *)userPtr)++))
1254 return -EFAULT;
1255 data ^= 0x8080;
1256 userCount -= 2;
1257 bal += hSpeed;
1259 *p++ = data;
1260 frameLeft -= 2;
1261 bal -= sSpeed;
1263 sound.data = data;
1265 sound.bal = bal;
1266 used -= userCount;
1267 *frameUsed += usedf-frameLeft;
1268 return(used);
1272 static ssize_t ata_ctx_s16be(const u_char *userPtr, size_t userCount,
1273 u_char frame[], ssize_t *frameUsed,
1274 ssize_t frameLeft)
1276 /* this should help gcc to stuff everything into registers */
1277 long bal = sound.bal;
1278 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1279 ssize_t used, usedf;
1281 used = userCount;
1282 usedf = frameLeft;
1283 if (!sound.soft.stereo) {
1284 u_short *p = (u_short *)&frame[*frameUsed];
1285 u_short data = sound.data;
1286 while (frameLeft >= 4) {
1287 if (bal < 0) {
1288 if (userCount < 2)
1289 break;
1290 if (get_user(data, ((u_short *)userPtr)++))
1291 return -EFAULT;
1292 userCount -= 2;
1293 bal += hSpeed;
1295 *p++ = data;
1296 *p++ = data;
1297 frameLeft -= 4;
1298 bal -= sSpeed;
1300 sound.data = data;
1301 } else {
1302 u_long *p = (u_long *)&frame[*frameUsed];
1303 u_long data = sound.data;
1304 while (frameLeft >= 4) {
1305 if (bal < 0) {
1306 if (userCount < 4)
1307 break;
1308 if (get_user(data, ((u_int *)userPtr)++))
1309 return -EFAULT;
1310 userCount -= 4;
1311 bal += hSpeed;
1313 *p++ = data;
1314 frameLeft -= 4;
1315 bal -= sSpeed;
1317 sound.data = data;
1319 sound.bal = bal;
1320 used -= userCount;
1321 *frameUsed += usedf-frameLeft;
1322 return(used);
1326 static ssize_t ata_ctx_u16be(const u_char *userPtr, size_t userCount,
1327 u_char frame[], ssize_t *frameUsed,
1328 ssize_t frameLeft)
1330 /* this should help gcc to stuff everything into registers */
1331 long bal = sound.bal;
1332 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1333 ssize_t used, usedf;
1335 used = userCount;
1336 usedf = frameLeft;
1337 if (!sound.soft.stereo) {
1338 u_short *p = (u_short *)&frame[*frameUsed];
1339 u_short data = sound.data;
1340 while (frameLeft >= 4) {
1341 if (bal < 0) {
1342 if (userCount < 2)
1343 break;
1344 if (get_user(data, ((u_short *)userPtr)++))
1345 return -EFAULT;
1346 data ^= 0x8000;
1347 userCount -= 2;
1348 bal += hSpeed;
1350 *p++ = data;
1351 *p++ = data;
1352 frameLeft -= 4;
1353 bal -= sSpeed;
1355 sound.data = data;
1356 } else {
1357 u_long *p = (u_long *)&frame[*frameUsed];
1358 u_long data = sound.data;
1359 while (frameLeft >= 4) {
1360 if (bal < 0) {
1361 if (userCount < 4)
1362 break;
1363 if (get_user(data, ((u_int *)userPtr)++))
1364 return -EFAULT;
1365 data ^= 0x80008000;
1366 userCount -= 4;
1367 bal += hSpeed;
1369 *p++ = data;
1370 frameLeft -= 4;
1371 bal -= sSpeed;
1373 sound.data = data;
1375 sound.bal = bal;
1376 used -= userCount;
1377 *frameUsed += usedf-frameLeft;
1378 return(used);
1382 static ssize_t ata_ctx_s16le(const u_char *userPtr, size_t userCount,
1383 u_char frame[], ssize_t *frameUsed,
1384 ssize_t frameLeft)
1386 /* this should help gcc to stuff everything into registers */
1387 long bal = sound.bal;
1388 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1389 ssize_t used, usedf;
1391 used = userCount;
1392 usedf = frameLeft;
1393 if (!sound.soft.stereo) {
1394 u_short *p = (u_short *)&frame[*frameUsed];
1395 u_short data = sound.data;
1396 while (frameLeft >= 4) {
1397 if (bal < 0) {
1398 if (userCount < 2)
1399 break;
1400 if (get_user(data, ((u_short *)userPtr)++))
1401 return -EFAULT;
1402 data = le2be16(data);
1403 userCount -= 2;
1404 bal += hSpeed;
1406 *p++ = data;
1407 *p++ = data;
1408 frameLeft -= 4;
1409 bal -= sSpeed;
1411 sound.data = data;
1412 } else {
1413 u_long *p = (u_long *)&frame[*frameUsed];
1414 u_long data = sound.data;
1415 while (frameLeft >= 4) {
1416 if (bal < 0) {
1417 if (userCount < 4)
1418 break;
1419 if (get_user(data, ((u_int *)userPtr)++))
1420 return -EFAULT;
1421 data = le2be16dbl(data);
1422 userCount -= 4;
1423 bal += hSpeed;
1425 *p++ = data;
1426 frameLeft -= 4;
1427 bal -= sSpeed;
1429 sound.data = data;
1431 sound.bal = bal;
1432 used -= userCount;
1433 *frameUsed += usedf-frameLeft;
1434 return(used);
1438 static ssize_t ata_ctx_u16le(const u_char *userPtr, size_t userCount,
1439 u_char frame[], ssize_t *frameUsed,
1440 ssize_t frameLeft)
1442 /* this should help gcc to stuff everything into registers */
1443 long bal = sound.bal;
1444 long hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1445 ssize_t used, usedf;
1447 used = userCount;
1448 usedf = frameLeft;
1449 if (!sound.soft.stereo) {
1450 u_short *p = (u_short *)&frame[*frameUsed];
1451 u_short data = sound.data;
1452 while (frameLeft >= 4) {
1453 if (bal < 0) {
1454 if (userCount < 2)
1455 break;
1456 if (get_user(data, ((u_short *)userPtr)++))
1457 return -EFAULT;
1458 data = le2be16(data) ^ 0x8000;
1459 userCount -= 2;
1460 bal += hSpeed;
1462 *p++ = data;
1463 *p++ = data;
1464 frameLeft -= 4;
1465 bal -= sSpeed;
1467 sound.data = data;
1468 } else {
1469 u_long *p = (u_long *)&frame[*frameUsed];
1470 u_long data = sound.data;
1471 while (frameLeft >= 4) {
1472 if (bal < 0) {
1473 if (userCount < 4)
1474 break;
1475 if (get_user(data, ((u_int *)userPtr)++))
1476 return -EFAULT;
1477 data = le2be16dbl(data) ^ 0x80008000;
1478 userCount -= 4;
1479 bal += hSpeed;
1481 *p++ = data;
1482 frameLeft -= 4;
1483 bal -= sSpeed;
1485 sound.data = data;
1487 sound.bal = bal;
1488 used -= userCount;
1489 *frameUsed += usedf-frameLeft;
1490 return(used);
1492 #endif /* CONFIG_ATARI */
1495 #ifdef CONFIG_AMIGA
1496 static ssize_t ami_ct_law(const u_char *userPtr, size_t userCount,
1497 u_char frame[], ssize_t *frameUsed,
1498 ssize_t frameLeft)
1500 char *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma8 : alaw2dma8;
1501 ssize_t count, used;
1503 if (!sound.soft.stereo) {
1504 u_char *p = &frame[*frameUsed];
1505 count = min(userCount, frameLeft) & ~1;
1506 used = count;
1507 while (count > 0) {
1508 u_char data;
1509 if (get_user(data, userPtr++))
1510 return -EFAULT;
1511 *p++ = table[data];
1512 count--;
1514 } else {
1515 u_char *left = &frame[*frameUsed>>1];
1516 u_char *right = left+sq.block_size_half;
1517 count = min(userCount, frameLeft)>>1 & ~1;
1518 used = count*2;
1519 while (count > 0) {
1520 u_char data;
1521 if (get_user(data, userPtr++))
1522 return -EFAULT;
1523 *left++ = table[data];
1524 if (get_user(data, userPtr++))
1525 return -EFAULT;
1526 *right++ = table[data];
1527 count--;
1530 *frameUsed += used;
1531 return(used);
1535 static ssize_t ami_ct_s8(const u_char *userPtr, size_t userCount,
1536 u_char frame[], ssize_t *frameUsed, ssize_t frameLeft)
1538 ssize_t count, used;
1540 if (!sound.soft.stereo) {
1541 void *p = &frame[*frameUsed];
1542 count = min(userCount, frameLeft) & ~1;
1543 used = count;
1544 if (copy_from_user(p, userPtr, count))
1545 return -EFAULT;
1546 } else {
1547 u_char *left = &frame[*frameUsed>>1];
1548 u_char *right = left+sq.block_size_half;
1549 count = min(userCount, frameLeft)>>1 & ~1;
1550 used = count*2;
1551 while (count > 0) {
1552 if (get_user(*left++, userPtr++)
1553 || get_user(*right++, userPtr++))
1554 return -EFAULT;
1555 count--;
1558 *frameUsed += used;
1559 return(used);
1563 static ssize_t ami_ct_u8(const u_char *userPtr, size_t userCount,
1564 u_char frame[], ssize_t *frameUsed, ssize_t frameLeft)
1566 ssize_t count, used;
1568 if (!sound.soft.stereo) {
1569 char *p = &frame[*frameUsed];
1570 count = min(userCount, frameLeft) & ~1;
1571 used = count;
1572 while (count > 0) {
1573 u_char data;
1574 if (get_user(data, userPtr++))
1575 return -EFAULT;
1576 *p++ = data ^ 0x80;
1577 count--;
1579 } else {
1580 u_char *left = &frame[*frameUsed>>1];
1581 u_char *right = left+sq.block_size_half;
1582 count = min(userCount, frameLeft)>>1 & ~1;
1583 used = count*2;
1584 while (count > 0) {
1585 u_char data;
1586 if (get_user(data, userPtr++))
1587 return -EFAULT;
1588 *left++ = data ^ 0x80;
1589 if (get_user(data, userPtr++))
1590 return -EFAULT;
1591 *right++ = data ^ 0x80;
1592 count--;
1595 *frameUsed += used;
1596 return(used);
1600 static ssize_t ami_ct_s16be(const u_char *userPtr, size_t userCount,
1601 u_char frame[], ssize_t *frameUsed,
1602 ssize_t frameLeft)
1604 ssize_t count, used;
1605 u_short data;
1607 if (!sound.soft.stereo) {
1608 u_char *high = &frame[*frameUsed>>1];
1609 u_char *low = high+sq.block_size_half;
1610 count = min(userCount, frameLeft)>>1 & ~1;
1611 used = count*2;
1612 while (count > 0) {
1613 if (get_user(data, ((u_short *)userPtr)++))
1614 return -EFAULT;
1615 *high++ = data>>8;
1616 *low++ = (data>>2) & 0x3f;
1617 count--;
1619 } else {
1620 u_char *lefth = &frame[*frameUsed>>2];
1621 u_char *leftl = lefth+sq.block_size_quarter;
1622 u_char *righth = lefth+sq.block_size_half;
1623 u_char *rightl = righth+sq.block_size_quarter;
1624 count = min(userCount, frameLeft)>>2 & ~1;
1625 used = count*4;
1626 while (count > 0) {
1627 if (get_user(data, ((u_short *)userPtr)++))
1628 return -EFAULT;
1629 *lefth++ = data>>8;
1630 *leftl++ = (data>>2) & 0x3f;
1631 if (get_user(data, ((u_short *)userPtr)++))
1632 return -EFAULT;
1633 *righth++ = data>>8;
1634 *rightl++ = (data>>2) & 0x3f;
1635 count--;
1638 *frameUsed += used;
1639 return(used);
1643 static ssize_t ami_ct_u16be(const u_char *userPtr, size_t userCount,
1644 u_char frame[], ssize_t *frameUsed,
1645 ssize_t frameLeft)
1647 ssize_t count, used;
1648 u_short data;
1650 if (!sound.soft.stereo) {
1651 u_char *high = &frame[*frameUsed>>1];
1652 u_char *low = high+sq.block_size_half;
1653 count = min(userCount, frameLeft)>>1 & ~1;
1654 used = count*2;
1655 while (count > 0) {
1656 if (get_user(data, ((u_short *)userPtr)++))
1657 return -EFAULT;
1658 data ^= 0x8000;
1659 *high++ = data>>8;
1660 *low++ = (data>>2) & 0x3f;
1661 count--;
1663 } else {
1664 u_char *lefth = &frame[*frameUsed>>2];
1665 u_char *leftl = lefth+sq.block_size_quarter;
1666 u_char *righth = lefth+sq.block_size_half;
1667 u_char *rightl = righth+sq.block_size_quarter;
1668 count = min(userCount, frameLeft)>>2 & ~1;
1669 used = count*4;
1670 while (count > 0) {
1671 if (get_user(data, ((u_short *)userPtr)++))
1672 return -EFAULT;
1673 data ^= 0x8000;
1674 *lefth++ = data>>8;
1675 *leftl++ = (data>>2) & 0x3f;
1676 if (get_user(data, ((u_short *)userPtr)++))
1677 return -EFAULT;
1678 data ^= 0x8000;
1679 *righth++ = data>>8;
1680 *rightl++ = (data>>2) & 0x3f;
1681 count--;
1684 *frameUsed += used;
1685 return(used);
1689 static ssize_t ami_ct_s16le(const u_char *userPtr, size_t userCount,
1690 u_char frame[], ssize_t *frameUsed,
1691 ssize_t frameLeft)
1693 ssize_t count, used;
1694 u_short data;
1696 if (!sound.soft.stereo) {
1697 u_char *high = &frame[*frameUsed>>1];
1698 u_char *low = high+sq.block_size_half;
1699 count = min(userCount, frameLeft)>>1 & ~1;
1700 used = count*2;
1701 while (count > 0) {
1702 if (get_user(data, ((u_short *)userPtr)++))
1703 return -EFAULT;
1704 data = le2be16(data);
1705 *high++ = data>>8;
1706 *low++ = (data>>2) & 0x3f;
1707 count--;
1709 } else {
1710 u_char *lefth = &frame[*frameUsed>>2];
1711 u_char *leftl = lefth+sq.block_size_quarter;
1712 u_char *righth = lefth+sq.block_size_half;
1713 u_char *rightl = righth+sq.block_size_quarter;
1714 count = min(userCount, frameLeft)>>2 & ~1;
1715 used = count*4;
1716 while (count > 0) {
1717 if (get_user(data, ((u_short *)userPtr)++))
1718 return -EFAULT;
1719 data = le2be16(data);
1720 *lefth++ = data>>8;
1721 *leftl++ = (data>>2) & 0x3f;
1722 if (get_user(data, ((u_short *)userPtr)++))
1723 return -EFAULT;
1724 data = le2be16(data);
1725 *righth++ = data>>8;
1726 *rightl++ = (data>>2) & 0x3f;
1727 count--;
1730 *frameUsed += used;
1731 return(used);
1735 static ssize_t ami_ct_u16le(const u_char *userPtr, size_t userCount,
1736 u_char frame[], ssize_t *frameUsed,
1737 ssize_t frameLeft)
1739 ssize_t count, used;
1740 u_short data;
1742 if (!sound.soft.stereo) {
1743 u_char *high = &frame[*frameUsed>>1];
1744 u_char *low = high+sq.block_size_half;
1745 count = min(userCount, frameLeft)>>1 & ~1;
1746 used = count*2;
1747 while (count > 0) {
1748 if (get_user(data, ((u_short *)userPtr)++))
1749 return -EFAULT;
1750 data = le2be16(data) ^ 0x8000;
1751 *high++ = data>>8;
1752 *low++ = (data>>2) & 0x3f;
1753 count--;
1755 } else {
1756 u_char *lefth = &frame[*frameUsed>>2];
1757 u_char *leftl = lefth+sq.block_size_quarter;
1758 u_char *righth = lefth+sq.block_size_half;
1759 u_char *rightl = righth+sq.block_size_quarter;
1760 count = min(userCount, frameLeft)>>2 & ~1;
1761 used = count*4;
1762 while (count > 0) {
1763 if (get_user(data, ((u_short *)userPtr)++))
1764 return -EFAULT;
1765 data = le2be16(data) ^ 0x8000;
1766 *lefth++ = data>>8;
1767 *leftl++ = (data>>2) & 0x3f;
1768 if (get_user(data, ((u_short *)userPtr)++))
1769 return -EFAULT;
1770 data = le2be16(data) ^ 0x8000;
1771 *righth++ = data>>8;
1772 *rightl++ = (data>>2) & 0x3f;
1773 count--;
1776 *frameUsed += used;
1777 return(used);
1779 #endif /* CONFIG_AMIGA */
1781 #ifdef CONFIG_PMAC
1782 static ssize_t pmac_ct_law(const u_char *userPtr, size_t userCount,
1783 u_char frame[], ssize_t *frameUsed,
1784 ssize_t frameLeft)
1786 short *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16;
1787 ssize_t count, used;
1788 short *p = (short *) &frame[*frameUsed];
1789 int val, stereo = sound.soft.stereo;
1791 frameLeft >>= 2;
1792 if (stereo)
1793 userCount >>= 1;
1794 used = count = min(userCount, frameLeft);
1795 while (count > 0) {
1796 u_char data;
1797 if (get_user(data, userPtr++))
1798 return -EFAULT;
1799 val = table[data];
1800 *p++ = val;
1801 if (stereo) {
1802 if (get_user(data, userPtr++))
1803 return -EFAULT;
1804 val = table[data];
1806 *p++ = val;
1807 count--;
1809 *frameUsed += used * 4;
1810 return stereo? used * 2: used;
1814 static ssize_t pmac_ct_s8(const u_char *userPtr, size_t userCount,
1815 u_char frame[], ssize_t *frameUsed,
1816 ssize_t frameLeft)
1818 ssize_t count, used;
1819 short *p = (short *) &frame[*frameUsed];
1820 int val, stereo = sound.soft.stereo;
1822 frameLeft >>= 2;
1823 if (stereo)
1824 userCount >>= 1;
1825 used = count = min(userCount, frameLeft);
1826 while (count > 0) {
1827 u_char data;
1828 if (get_user(data, userPtr++))
1829 return -EFAULT;
1830 val = data << 8;
1831 *p++ = val;
1832 if (stereo) {
1833 if (get_user(data, userPtr++))
1834 return -EFAULT;
1835 val = data << 8;
1837 *p++ = val;
1838 count--;
1840 *frameUsed += used * 4;
1841 return stereo? used * 2: used;
1845 static ssize_t pmac_ct_u8(const u_char *userPtr, size_t userCount,
1846 u_char frame[], ssize_t *frameUsed,
1847 ssize_t frameLeft)
1849 ssize_t count, used;
1850 short *p = (short *) &frame[*frameUsed];
1851 int val, stereo = sound.soft.stereo;
1853 frameLeft >>= 2;
1854 if (stereo)
1855 userCount >>= 1;
1856 used = count = min(userCount, frameLeft);
1857 while (count > 0) {
1858 u_char data;
1859 if (get_user(data, userPtr++))
1860 return -EFAULT;
1861 val = (data ^ 0x80) << 8;
1862 *p++ = val;
1863 if (stereo) {
1864 if (get_user(data, userPtr++))
1865 return -EFAULT;
1866 val = (data ^ 0x80) << 8;
1868 *p++ = val;
1869 count--;
1871 *frameUsed += used * 4;
1872 return stereo? used * 2: used;
1876 static ssize_t pmac_ct_s16(const u_char *userPtr, size_t userCount,
1877 u_char frame[], ssize_t *frameUsed,
1878 ssize_t frameLeft)
1880 ssize_t count, used;
1881 int stereo = sound.soft.stereo;
1882 short *fp = (short *) &frame[*frameUsed];
1884 frameLeft >>= 2;
1885 userCount >>= (stereo? 2: 1);
1886 used = count = min(userCount, frameLeft);
1887 if (!stereo) {
1888 short *up = (short *) userPtr;
1889 while (count > 0) {
1890 short data;
1891 if (get_user(data, up++))
1892 return -EFAULT;
1893 *fp++ = data;
1894 *fp++ = data;
1895 count--;
1897 } else {
1898 if (copy_from_user(fp, userPtr, count * 4))
1899 return -EFAULT;
1901 *frameUsed += used * 4;
1902 return stereo? used * 4: used * 2;
1905 static ssize_t pmac_ct_u16(const u_char *userPtr, size_t userCount,
1906 u_char frame[], ssize_t *frameUsed,
1907 ssize_t frameLeft)
1909 ssize_t count, used;
1910 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
1911 int stereo = sound.soft.stereo;
1912 short *fp = (short *) &frame[*frameUsed];
1913 short *up = (short *) userPtr;
1915 frameLeft >>= 2;
1916 userCount >>= (stereo? 2: 1);
1917 used = count = min(userCount, frameLeft);
1918 while (count > 0) {
1919 int data;
1920 if (get_user(data, up++))
1921 return -EFAULT;
1922 data ^= mask;
1923 *fp++ = data;
1924 if (stereo) {
1925 if (get_user(data, up++))
1926 return -EFAULT;
1927 data ^= mask;
1929 *fp++ = data;
1930 count--;
1932 *frameUsed += used * 4;
1933 return stereo? used * 4: used * 2;
1937 static ssize_t pmac_ctx_law(const u_char *userPtr, size_t userCount,
1938 u_char frame[], ssize_t *frameUsed,
1939 ssize_t frameLeft)
1941 unsigned short *table = (unsigned short *)
1942 (sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16);
1943 unsigned int data = sound.data;
1944 unsigned int *p = (unsigned int *) &frame[*frameUsed];
1945 int bal = sound.bal;
1946 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1947 int utotal, ftotal;
1948 int stereo = sound.soft.stereo;
1950 frameLeft >>= 2;
1951 if (stereo)
1952 userCount >>= 1;
1953 ftotal = frameLeft;
1954 utotal = userCount;
1955 while (frameLeft) {
1956 u_char c;
1957 if (bal < 0) {
1958 if (userCount == 0)
1959 break;
1960 if (get_user(c, userPtr++))
1961 return -EFAULT;
1962 data = table[c];
1963 if (stereo) {
1964 if (get_user(c, userPtr++))
1965 return -EFAULT;
1966 data = (data << 16) + table[c];
1967 } else
1968 data = (data << 16) + data;
1969 userCount--;
1970 bal += hSpeed;
1972 *p++ = data;
1973 frameLeft--;
1974 bal -= sSpeed;
1976 sound.bal = bal;
1977 sound.data = data;
1978 *frameUsed += (ftotal - frameLeft) * 4;
1979 utotal -= userCount;
1980 return stereo? utotal * 2: utotal;
1984 static ssize_t pmac_ctx_s8(const u_char *userPtr, size_t userCount,
1985 u_char frame[], ssize_t *frameUsed,
1986 ssize_t frameLeft)
1988 unsigned int *p = (unsigned int *) &frame[*frameUsed];
1989 unsigned int data = sound.data;
1990 int bal = sound.bal;
1991 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
1992 int stereo = sound.soft.stereo;
1993 int utotal, ftotal;
1995 frameLeft >>= 2;
1996 if (stereo)
1997 userCount >>= 1;
1998 ftotal = frameLeft;
1999 utotal = userCount;
2000 while (frameLeft) {
2001 u_char c;
2002 if (bal < 0) {
2003 if (userCount == 0)
2004 break;
2005 if (get_user(c, userPtr++))
2006 return -EFAULT;
2007 data = c << 8;
2008 if (stereo) {
2009 if (get_user(c, userPtr++))
2010 return -EFAULT;
2011 data = (data << 16) + (c << 8);
2012 } else
2013 data = (data << 16) + data;
2014 userCount--;
2015 bal += hSpeed;
2017 *p++ = data;
2018 frameLeft--;
2019 bal -= sSpeed;
2021 sound.bal = bal;
2022 sound.data = data;
2023 *frameUsed += (ftotal - frameLeft) * 4;
2024 utotal -= userCount;
2025 return stereo? utotal * 2: utotal;
2029 static ssize_t pmac_ctx_u8(const u_char *userPtr, size_t userCount,
2030 u_char frame[], ssize_t *frameUsed,
2031 ssize_t frameLeft)
2033 unsigned int *p = (unsigned int *) &frame[*frameUsed];
2034 unsigned int data = sound.data;
2035 int bal = sound.bal;
2036 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
2037 int stereo = sound.soft.stereo;
2038 int utotal, ftotal;
2040 frameLeft >>= 2;
2041 if (stereo)
2042 userCount >>= 1;
2043 ftotal = frameLeft;
2044 utotal = userCount;
2045 while (frameLeft) {
2046 u_char c;
2047 if (bal < 0) {
2048 if (userCount == 0)
2049 break;
2050 if (get_user(c, userPtr++))
2051 return -EFAULT;
2052 data = (c ^ 0x80) << 8;
2053 if (stereo) {
2054 if (get_user(c, userPtr++))
2055 return -EFAULT;
2056 data = (data << 16) + ((c ^ 0x80) << 8);
2057 } else
2058 data = (data << 16) + data;
2059 userCount--;
2060 bal += hSpeed;
2062 *p++ = data;
2063 frameLeft--;
2064 bal -= sSpeed;
2066 sound.bal = bal;
2067 sound.data = data;
2068 *frameUsed += (ftotal - frameLeft) * 4;
2069 utotal -= userCount;
2070 return stereo? utotal * 2: utotal;
2074 static ssize_t pmac_ctx_s16(const u_char *userPtr, size_t userCount,
2075 u_char frame[], ssize_t *frameUsed,
2076 ssize_t frameLeft)
2078 unsigned int *p = (unsigned int *) &frame[*frameUsed];
2079 unsigned int data = sound.data;
2080 unsigned short *up = (unsigned short *) userPtr;
2081 int bal = sound.bal;
2082 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
2083 int stereo = sound.soft.stereo;
2084 int utotal, ftotal;
2086 frameLeft >>= 2;
2087 if (stereo)
2088 userCount >>= 1;
2089 ftotal = frameLeft;
2090 utotal = userCount;
2091 while (frameLeft) {
2092 unsigned short c;
2093 if (bal < 0) {
2094 if (userCount == 0)
2095 break;
2096 if (get_user(data, up++))
2097 return -EFAULT;
2098 if (stereo) {
2099 if (get_user(c, up++))
2100 return -EFAULT;
2101 data = (data << 16) + c;
2102 } else
2103 data = (data << 16) + data;
2104 userCount--;
2105 bal += hSpeed;
2107 *p++ = data;
2108 frameLeft--;
2109 bal -= sSpeed;
2111 sound.bal = bal;
2112 sound.data = data;
2113 *frameUsed += (ftotal - frameLeft) * 4;
2114 utotal -= userCount;
2115 return stereo? utotal * 4: utotal * 2;
2119 static ssize_t pmac_ctx_u16(const u_char *userPtr, size_t userCount,
2120 u_char frame[], ssize_t *frameUsed,
2121 ssize_t frameLeft)
2123 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
2124 unsigned int *p = (unsigned int *) &frame[*frameUsed];
2125 unsigned int data = sound.data;
2126 unsigned short *up = (unsigned short *) userPtr;
2127 int bal = sound.bal;
2128 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
2129 int stereo = sound.soft.stereo;
2130 int utotal, ftotal;
2132 frameLeft >>= 2;
2133 if (stereo)
2134 userCount >>= 1;
2135 ftotal = frameLeft;
2136 utotal = userCount;
2137 while (frameLeft) {
2138 unsigned short c;
2139 if (bal < 0) {
2140 if (userCount == 0)
2141 break;
2142 if (get_user(data, up++))
2143 return -EFAULT;
2144 data ^= mask;
2145 if (stereo) {
2146 if (get_user(c, up++))
2147 return -EFAULT;
2148 data = (data << 16) + (c ^ mask);
2149 } else
2150 data = (data << 16) + data;
2151 userCount--;
2152 bal += hSpeed;
2154 *p++ = data;
2155 frameLeft--;
2156 bal -= sSpeed;
2158 sound.bal = bal;
2159 sound.data = data;
2160 *frameUsed += (ftotal - frameLeft) * 4;
2161 utotal -= userCount;
2162 return stereo? utotal * 4: utotal * 2;
2165 #endif /* CONFIG_PMAC */
2168 #ifdef CONFIG_ATARI
2169 static TRANS transTTNormal = {
2170 ata_ct_law, ata_ct_law, ata_ct_s8, ata_ct_u8, NULL, NULL, NULL, NULL
2173 static TRANS transTTExpanding = {
2174 ata_ctx_law, ata_ctx_law, ata_ctx_s8, ata_ctx_u8, NULL, NULL, NULL, NULL
2177 static TRANS transFalconNormal = {
2178 ata_ct_law, ata_ct_law, ata_ct_s8, ata_ct_u8,
2179 ata_ct_s16be, ata_ct_u16be, ata_ct_s16le, ata_ct_u16le
2182 static TRANS transFalconExpanding = {
2183 ata_ctx_law, ata_ctx_law, ata_ctx_s8, ata_ctx_u8,
2184 ata_ctx_s16be, ata_ctx_u16be, ata_ctx_s16le, ata_ctx_u16le
2186 #endif /* CONFIG_ATARI */
2188 #ifdef CONFIG_AMIGA
2189 static TRANS transAmiga = {
2190 ami_ct_law, ami_ct_law, ami_ct_s8, ami_ct_u8,
2191 ami_ct_s16be, ami_ct_u16be, ami_ct_s16le, ami_ct_u16le
2193 #endif /* CONFIG_AMIGA */
2195 #ifdef CONFIG_PMAC
2196 static TRANS transAwacsNormal = {
2197 pmac_ct_law, pmac_ct_law, pmac_ct_s8, pmac_ct_u8,
2198 pmac_ct_s16, pmac_ct_u16, pmac_ct_s16, pmac_ct_u16
2201 static TRANS transAwacsExpand = {
2202 pmac_ctx_law, pmac_ctx_law, pmac_ctx_s8, pmac_ctx_u8,
2203 pmac_ctx_s16, pmac_ctx_u16, pmac_ctx_s16, pmac_ctx_u16
2205 #endif /* CONFIG_PMAC */
2207 /*** Low level stuff *********************************************************/
2210 #ifdef CONFIG_ATARI
2213 * Atari (TT/Falcon)
2216 static void *AtaAlloc(unsigned int size, int flags)
2218 return( atari_stram_alloc( size, NULL, "dmasound" ));
2221 static void AtaFree(void *obj, unsigned int size)
2223 atari_stram_free( obj );
2226 static int AtaIrqInit(void)
2228 /* Set up timer A. Timer A
2229 will receive a signal upon end of playing from the sound
2230 hardware. Furthermore Timer A is able to count events
2231 and will cause an interrupt after a programmed number
2232 of events. So all we need to keep the music playing is
2233 to provide the sound hardware with new data upon
2234 an interrupt from timer A. */
2235 mfp.tim_ct_a = 0; /* ++roman: Stop timer before programming! */
2236 mfp.tim_dt_a = 1; /* Cause interrupt after first event. */
2237 mfp.tim_ct_a = 8; /* Turn on event counting. */
2238 /* Register interrupt handler. */
2239 request_irq(IRQ_MFP_TIMA, ata_sq_interrupt, IRQ_TYPE_SLOW,
2240 "DMA sound", ata_sq_interrupt);
2241 mfp.int_en_a |= 0x20; /* Turn interrupt on. */
2242 mfp.int_mk_a |= 0x20;
2243 return(1);
2246 #ifdef MODULE
2247 static void AtaIrqCleanUp(void)
2249 mfp.tim_ct_a = 0; /* stop timer */
2250 mfp.int_en_a &= ~0x20; /* turn interrupt off */
2251 free_irq(IRQ_MFP_TIMA, ata_sq_interrupt);
2253 #endif /* MODULE */
2256 #define TONE_VOXWARE_TO_DB(v) \
2257 (((v) < 0) ? -12 : ((v) > 100) ? 12 : ((v) - 50) * 6 / 25)
2258 #define TONE_DB_TO_VOXWARE(v) (((v) * 25 + ((v) > 0 ? 5 : -5)) / 6 + 50)
2261 static int AtaSetBass(int bass)
2263 sound.bass = TONE_VOXWARE_TO_DB(bass);
2264 atari_microwire_cmd(MW_LM1992_BASS(sound.bass));
2265 return(TONE_DB_TO_VOXWARE(sound.bass));
2269 static int AtaSetTreble(int treble)
2271 sound.treble = TONE_VOXWARE_TO_DB(treble);
2272 atari_microwire_cmd(MW_LM1992_TREBLE(sound.treble));
2273 return(TONE_DB_TO_VOXWARE(sound.treble));
2279 * TT
2283 static void TTSilence(void)
2285 tt_dmasnd.ctrl = DMASND_CTRL_OFF;
2286 atari_microwire_cmd(MW_LM1992_PSG_HIGH); /* mix in PSG signal 1:1 */
2290 static void TTInit(void)
2292 int mode, i, idx;
2293 const int freq[4] = {50066, 25033, 12517, 6258};
2295 /* search a frequency that fits into the allowed error range */
2297 idx = -1;
2298 for (i = 0; i < arraysize(freq); i++)
2299 /* this isn't as much useful for a TT than for a Falcon, but
2300 * then it doesn't hurt very much to implement it for a TT too.
2302 if ((100 * abs(sound.soft.speed - freq[i]) / freq[i]) < catchRadius)
2303 idx = i;
2304 if (idx > -1) {
2305 sound.soft.speed = freq[idx];
2306 sound.trans = &transTTNormal;
2307 } else
2308 sound.trans = &transTTExpanding;
2310 TTSilence();
2311 sound.hard = sound.soft;
2313 if (sound.hard.speed > 50066) {
2314 /* we would need to squeeze the sound, but we won't do that */
2315 sound.hard.speed = 50066;
2316 mode = DMASND_MODE_50KHZ;
2317 sound.trans = &transTTNormal;
2318 } else if (sound.hard.speed > 25033) {
2319 sound.hard.speed = 50066;
2320 mode = DMASND_MODE_50KHZ;
2321 } else if (sound.hard.speed > 12517) {
2322 sound.hard.speed = 25033;
2323 mode = DMASND_MODE_25KHZ;
2324 } else if (sound.hard.speed > 6258) {
2325 sound.hard.speed = 12517;
2326 mode = DMASND_MODE_12KHZ;
2327 } else {
2328 sound.hard.speed = 6258;
2329 mode = DMASND_MODE_6KHZ;
2332 tt_dmasnd.mode = (sound.hard.stereo ?
2333 DMASND_MODE_STEREO : DMASND_MODE_MONO) |
2334 DMASND_MODE_8BIT | mode;
2336 sound.bal = -sound.soft.speed;
2340 static int TTSetFormat(int format)
2342 /* TT sound DMA supports only 8bit modes */
2344 switch (format) {
2345 case AFMT_QUERY:
2346 return(sound.soft.format);
2347 case AFMT_MU_LAW:
2348 case AFMT_A_LAW:
2349 case AFMT_S8:
2350 case AFMT_U8:
2351 break;
2352 default:
2353 format = AFMT_S8;
2356 sound.soft.format = format;
2357 sound.soft.size = 8;
2358 if (sound.minDev == SND_DEV_DSP) {
2359 sound.dsp.format = format;
2360 sound.dsp.size = 8;
2362 TTInit();
2364 return(format);
2368 #define VOLUME_VOXWARE_TO_DB(v) \
2369 (((v) < 0) ? -40 : ((v) > 100) ? 0 : ((v) * 2) / 5 - 40)
2370 #define VOLUME_DB_TO_VOXWARE(v) ((((v) + 40) * 5 + 1) / 2)
2373 static int TTSetVolume(int volume)
2375 sound.volume_left = VOLUME_VOXWARE_TO_DB(volume & 0xff);
2376 atari_microwire_cmd(MW_LM1992_BALLEFT(sound.volume_left));
2377 sound.volume_right = VOLUME_VOXWARE_TO_DB((volume & 0xff00) >> 8);
2378 atari_microwire_cmd(MW_LM1992_BALRIGHT(sound.volume_right));
2379 return(VOLUME_DB_TO_VOXWARE(sound.volume_left) |
2380 (VOLUME_DB_TO_VOXWARE(sound.volume_right) << 8));
2384 #define GAIN_VOXWARE_TO_DB(v) \
2385 (((v) < 0) ? -80 : ((v) > 100) ? 0 : ((v) * 4) / 5 - 80)
2386 #define GAIN_DB_TO_VOXWARE(v) ((((v) + 80) * 5 + 1) / 4)
2388 static int TTSetGain(int gain)
2390 sound.gain = GAIN_VOXWARE_TO_DB(gain);
2391 atari_microwire_cmd(MW_LM1992_VOLUME(sound.gain));
2392 return GAIN_DB_TO_VOXWARE(sound.gain);
2398 * Falcon
2402 static void FalconSilence(void)
2404 /* stop playback, set sample rate 50kHz for PSG sound */
2405 tt_dmasnd.ctrl = DMASND_CTRL_OFF;
2406 tt_dmasnd.mode = DMASND_MODE_50KHZ | DMASND_MODE_STEREO | DMASND_MODE_8BIT;
2407 tt_dmasnd.int_div = 0; /* STE compatible divider */
2408 tt_dmasnd.int_ctrl = 0x0;
2409 tt_dmasnd.cbar_src = 0x0000; /* no matrix inputs */
2410 tt_dmasnd.cbar_dst = 0x0000; /* no matrix outputs */
2411 tt_dmasnd.dac_src = 1; /* connect ADC to DAC, disconnect matrix */
2412 tt_dmasnd.adc_src = 3; /* ADC Input = PSG */
2416 static void FalconInit(void)
2418 int divider, i, idx;
2419 const int freq[8] = {49170, 32780, 24585, 19668, 16390, 12292, 9834, 8195};
2421 /* search a frequency that fits into the allowed error range */
2423 idx = -1;
2424 for (i = 0; i < arraysize(freq); i++)
2425 /* if we will tolerate 3% error 8000Hz->8195Hz (2.38%) would
2426 * be playable without expanding, but that now a kernel runtime
2427 * option
2429 if ((100 * abs(sound.soft.speed - freq[i]) / freq[i]) < catchRadius)
2430 idx = i;
2431 if (idx > -1) {
2432 sound.soft.speed = freq[idx];
2433 sound.trans = &transFalconNormal;
2434 } else
2435 sound.trans = &transFalconExpanding;
2437 FalconSilence();
2438 sound.hard = sound.soft;
2440 if (sound.hard.size == 16) {
2441 /* the Falcon can play 16bit samples only in stereo */
2442 sound.hard.stereo = 1;
2445 if (sound.hard.speed > 49170) {
2446 /* we would need to squeeze the sound, but we won't do that */
2447 sound.hard.speed = 49170;
2448 divider = 1;
2449 sound.trans = &transFalconNormal;
2450 } else if (sound.hard.speed > 32780) {
2451 sound.hard.speed = 49170;
2452 divider = 1;
2453 } else if (sound.hard.speed > 24585) {
2454 sound.hard.speed = 32780;
2455 divider = 2;
2456 } else if (sound.hard.speed > 19668) {
2457 sound.hard.speed = 24585;
2458 divider = 3;
2459 } else if (sound.hard.speed > 16390) {
2460 sound.hard.speed = 19668;
2461 divider = 4;
2462 } else if (sound.hard.speed > 12292) {
2463 sound.hard.speed = 16390;
2464 divider = 5;
2465 } else if (sound.hard.speed > 9834) {
2466 sound.hard.speed = 12292;
2467 divider = 7;
2468 } else if (sound.hard.speed > 8195) {
2469 sound.hard.speed = 9834;
2470 divider = 9;
2471 } else {
2472 sound.hard.speed = 8195;
2473 divider = 11;
2475 tt_dmasnd.int_div = divider;
2477 /* Setup Falcon sound DMA for playback */
2478 tt_dmasnd.int_ctrl = 0x4; /* Timer A int at play end */
2479 tt_dmasnd.track_select = 0x0; /* play 1 track, track 1 */
2480 tt_dmasnd.cbar_src = 0x0001; /* DMA(25MHz) --> DAC */
2481 tt_dmasnd.cbar_dst = 0x0000;
2482 tt_dmasnd.rec_track_select = 0;
2483 tt_dmasnd.dac_src = 2; /* connect matrix to DAC */
2484 tt_dmasnd.adc_src = 0; /* ADC Input = Mic */
2486 tt_dmasnd.mode = (sound.hard.stereo ?
2487 DMASND_MODE_STEREO : DMASND_MODE_MONO) |
2488 ((sound.hard.size == 8) ?
2489 DMASND_MODE_8BIT : DMASND_MODE_16BIT) |
2490 DMASND_MODE_6KHZ;
2492 sound.bal = -sound.soft.speed;
2496 static int FalconSetFormat(int format)
2498 int size;
2499 /* Falcon sound DMA supports 8bit and 16bit modes */
2501 switch (format) {
2502 case AFMT_QUERY:
2503 return(sound.soft.format);
2504 case AFMT_MU_LAW:
2505 case AFMT_A_LAW:
2506 case AFMT_U8:
2507 case AFMT_S8:
2508 size = 8;
2509 break;
2510 case AFMT_S16_BE:
2511 case AFMT_U16_BE:
2512 case AFMT_S16_LE:
2513 case AFMT_U16_LE:
2514 size = 16;
2515 break;
2516 default: /* :-) */
2517 size = 8;
2518 format = AFMT_S8;
2521 sound.soft.format = format;
2522 sound.soft.size = size;
2523 if (sound.minDev == SND_DEV_DSP) {
2524 sound.dsp.format = format;
2525 sound.dsp.size = sound.soft.size;
2528 FalconInit();
2530 return(format);
2534 /* This is for the Falcon output *attenuation* in 1.5dB steps,
2535 * i.e. output level from 0 to -22.5dB in -1.5dB steps.
2537 #define VOLUME_VOXWARE_TO_ATT(v) \
2538 ((v) < 0 ? 15 : (v) > 100 ? 0 : 15 - (v) * 3 / 20)
2539 #define VOLUME_ATT_TO_VOXWARE(v) (100 - (v) * 20 / 3)
2542 static int FalconSetVolume(int volume)
2544 sound.volume_left = VOLUME_VOXWARE_TO_ATT(volume & 0xff);
2545 sound.volume_right = VOLUME_VOXWARE_TO_ATT((volume & 0xff00) >> 8);
2546 tt_dmasnd.output_atten = sound.volume_left << 8 | sound.volume_right << 4;
2547 return(VOLUME_ATT_TO_VOXWARE(sound.volume_left) |
2548 VOLUME_ATT_TO_VOXWARE(sound.volume_right) << 8);
2552 static void ata_sq_play_next_frame(int index)
2554 char *start, *end;
2556 /* used by AtaPlay() if all doubts whether there really is something
2557 * to be played are already wiped out.
2559 start = sq_block_address(sq.front);
2560 end = start+((sq.count == index) ? sq.rear_size : sq.block_size);
2561 /* end might not be a legal virtual address. */
2562 DMASNDSetEnd(VTOP(end - 1) + 1);
2563 DMASNDSetBase(VTOP(start));
2564 /* Since only an even number of samples per frame can
2565 be played, we might lose one byte here. (TO DO) */
2566 sq.front = (sq.front+1) % sq.max_count;
2567 sq.playing++;
2568 tt_dmasnd.ctrl = DMASND_CTRL_ON | DMASND_CTRL_REPEAT;
2572 static void AtaPlay(void)
2574 /* ++TeSche: Note that sq.playing is no longer just a flag but holds
2575 * the number of frames the DMA is currently programmed for instead,
2576 * may be 0, 1 (currently being played) or 2 (pre-programmed).
2578 * Changes done to sq.count and sq.playing are a bit more subtle again
2579 * so now I must admit I also prefer disabling the irq here rather
2580 * than considering all possible situations. But the point is that
2581 * disabling the irq doesn't have any bad influence on this version of
2582 * the driver as we benefit from having pre-programmed the DMA
2583 * wherever possible: There's no need to reload the DMA at the exact
2584 * time of an interrupt but only at some time while the pre-programmed
2585 * frame is playing!
2587 atari_disable_irq(IRQ_MFP_TIMA);
2589 if (sq.playing == 2 || /* DMA is 'full' */
2590 sq.count <= 0) { /* nothing to do */
2591 atari_enable_irq(IRQ_MFP_TIMA);
2592 return;
2595 if (sq.playing == 0) {
2596 /* looks like there's nothing 'in' the DMA yet, so try
2597 * to put two frames into it (at least one is available).
2599 if (sq.count == 1 && sq.rear_size < sq.block_size && !sq.syncing) {
2600 /* hmmm, the only existing frame is not
2601 * yet filled and we're not syncing?
2603 atari_enable_irq(IRQ_MFP_TIMA);
2604 return;
2606 ata_sq_play_next_frame(1);
2607 if (sq.count == 1) {
2608 /* no more frames */
2609 atari_enable_irq(IRQ_MFP_TIMA);
2610 return;
2612 if (sq.count == 2 && sq.rear_size < sq.block_size && !sq.syncing) {
2613 /* hmmm, there were two frames, but the second
2614 * one is not yet filled and we're not syncing?
2616 atari_enable_irq(IRQ_MFP_TIMA);
2617 return;
2619 ata_sq_play_next_frame(2);
2620 } else {
2621 /* there's already a frame being played so we may only stuff
2622 * one new into the DMA, but even if this may be the last
2623 * frame existing the previous one is still on sq.count.
2625 if (sq.count == 2 && sq.rear_size < sq.block_size && !sq.syncing) {
2626 /* hmmm, the only existing frame is not
2627 * yet filled and we're not syncing?
2629 atari_enable_irq(IRQ_MFP_TIMA);
2630 return;
2632 ata_sq_play_next_frame(2);
2634 atari_enable_irq(IRQ_MFP_TIMA);
2638 static void ata_sq_interrupt(int irq, void *dummy, struct pt_regs *fp)
2640 #if 0
2641 /* ++TeSche: if you should want to test this... */
2642 static int cnt = 0;
2643 if (sq.playing == 2)
2644 if (++cnt == 10) {
2645 /* simulate losing an interrupt */
2646 cnt = 0;
2647 return;
2649 #endif
2651 if (sq.ignore_int && (sound.mach.type == DMASND_FALCON)) {
2652 /* ++TeSche: Falcon only: ignore first irq because it comes
2653 * immediately after starting a frame. after that, irqs come
2654 * (almost) like on the TT.
2656 sq.ignore_int = 0;
2657 return;
2660 if (!sq.playing) {
2661 /* playing was interrupted and sq_reset() has already cleared
2662 * the sq variables, so better don't do anything here.
2664 WAKE_UP(sq.sync_queue);
2665 return;
2668 /* Probably ;) one frame is finished. Well, in fact it may be that a
2669 * pre-programmed one is also finished because there has been a long
2670 * delay in interrupt delivery and we've completely lost one, but
2671 * there's no way to detect such a situation. In such a case the last
2672 * frame will be played more than once and the situation will recover
2673 * as soon as the irq gets through.
2675 sq.count--;
2676 sq.playing--;
2678 if (!sq.playing) {
2679 tt_dmasnd.ctrl = DMASND_CTRL_OFF;
2680 sq.ignore_int = 1;
2683 WAKE_UP(sq.write_queue);
2684 /* At least one block of the queue is free now
2685 so wake up a writing process blocked because
2686 of a full queue. */
2688 if ((sq.playing != 1) || (sq.count != 1))
2689 /* We must be a bit carefully here: sq.count indicates the
2690 * number of buffers used and not the number of frames to
2691 * be played. If sq.count==1 and sq.playing==1 that means
2692 * the only remaining frame was already programmed earlier
2693 * (and is currently running) so we mustn't call AtaPlay()
2694 * here, otherwise we'll play one frame too much.
2696 AtaPlay();
2698 if (!sq.playing) WAKE_UP(sq.sync_queue);
2699 /* We are not playing after AtaPlay(), so there
2700 is nothing to play any more. Wake up a process
2701 waiting for audio output to drain. */
2703 #endif /* CONFIG_ATARI */
2706 #ifdef CONFIG_AMIGA
2709 * Amiga
2713 static void *AmiAlloc(unsigned int size, int flags)
2715 return(amiga_chip_alloc((long)size));
2718 static void AmiFree(void *obj, unsigned int size)
2720 amiga_chip_free (obj);
2723 static int AmiIrqInit(void)
2725 /* turn off DMA for audio channels */
2726 custom.dmacon = AMI_AUDIO_OFF;
2728 /* Register interrupt handler. */
2729 if (request_irq(IRQ_AMIGA_AUD0, ami_sq_interrupt, 0,
2730 "DMA sound", ami_sq_interrupt))
2731 return(0);
2732 return(1);
2735 #ifdef MODULE
2736 static void AmiIrqCleanUp(void)
2738 /* turn off DMA for audio channels */
2739 custom.dmacon = AMI_AUDIO_OFF;
2740 /* release the interrupt */
2741 free_irq(IRQ_AMIGA_AUD0, ami_sq_interrupt);
2743 #endif /* MODULE */
2745 static void AmiSilence(void)
2747 /* turn off DMA for audio channels */
2748 custom.dmacon = AMI_AUDIO_OFF;
2752 static void AmiInit(void)
2754 int period, i;
2756 AmiSilence();
2758 if (sound.soft.speed)
2759 period = amiga_colorclock/sound.soft.speed-1;
2760 else
2761 period = amiga_audio_min_period;
2762 sound.hard = sound.soft;
2763 sound.trans = &transAmiga;
2765 if (period < amiga_audio_min_period) {
2766 /* we would need to squeeze the sound, but we won't do that */
2767 period = amiga_audio_min_period;
2768 } else if (period > 65535) {
2769 period = 65535;
2771 sound.hard.speed = amiga_colorclock/(period+1);
2773 for (i = 0; i < 4; i++)
2774 custom.aud[i].audper = period;
2775 amiga_audio_period = period;
2777 AmiSetTreble(50); /* recommended for newer amiga models */
2781 static int AmiSetFormat(int format)
2783 int size;
2785 /* Amiga sound DMA supports 8bit and 16bit (pseudo 14 bit) modes */
2787 switch (format) {
2788 case AFMT_QUERY:
2789 return(sound.soft.format);
2790 case AFMT_MU_LAW:
2791 case AFMT_A_LAW:
2792 case AFMT_U8:
2793 case AFMT_S8:
2794 size = 8;
2795 break;
2796 case AFMT_S16_BE:
2797 case AFMT_U16_BE:
2798 case AFMT_S16_LE:
2799 case AFMT_U16_LE:
2800 size = 16;
2801 break;
2802 default: /* :-) */
2803 size = 8;
2804 format = AFMT_S8;
2807 sound.soft.format = format;
2808 sound.soft.size = size;
2809 if (sound.minDev == SND_DEV_DSP) {
2810 sound.dsp.format = format;
2811 sound.dsp.size = sound.soft.size;
2813 AmiInit();
2815 return(format);
2819 #define VOLUME_VOXWARE_TO_AMI(v) \
2820 (((v) < 0) ? 0 : ((v) > 100) ? 64 : ((v) * 64)/100)
2821 #define VOLUME_AMI_TO_VOXWARE(v) ((v)*100/64)
2823 static int AmiSetVolume(int volume)
2825 sound.volume_left = VOLUME_VOXWARE_TO_AMI(volume & 0xff);
2826 custom.aud[0].audvol = sound.volume_left;
2827 sound.volume_right = VOLUME_VOXWARE_TO_AMI((volume & 0xff00) >> 8);
2828 custom.aud[1].audvol = sound.volume_right;
2829 return(VOLUME_AMI_TO_VOXWARE(sound.volume_left) |
2830 (VOLUME_AMI_TO_VOXWARE(sound.volume_right) << 8));
2833 static int AmiSetTreble(int treble)
2835 sound.treble = treble;
2836 if (treble < 50)
2837 ciaa.pra &= ~0x02;
2838 else
2839 ciaa.pra |= 0x02;
2840 return(treble);
2844 #define AMI_PLAY_LOADED 1
2845 #define AMI_PLAY_PLAYING 2
2846 #define AMI_PLAY_MASK 3
2849 static void ami_sq_play_next_frame(int index)
2851 u_char *start, *ch0, *ch1, *ch2, *ch3;
2852 u_long size;
2854 /* used by AmiPlay() if all doubts whether there really is something
2855 * to be played are already wiped out.
2857 start = sq_block_address(sq.front);
2858 size = (sq.count == index ? sq.rear_size : sq.block_size)>>1;
2860 if (sound.hard.stereo) {
2861 ch0 = start;
2862 ch1 = start+sq.block_size_half;
2863 size >>= 1;
2864 } else {
2865 ch0 = start;
2866 ch1 = start;
2868 if (sound.hard.size == 8) {
2869 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
2870 custom.aud[0].audlen = size;
2871 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
2872 custom.aud[1].audlen = size;
2873 custom.dmacon = AMI_AUDIO_8;
2874 } else {
2875 size >>= 1;
2876 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
2877 custom.aud[0].audlen = size;
2878 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
2879 custom.aud[1].audlen = size;
2880 if (sound.volume_left == 64 && sound.volume_right == 64) {
2881 /* We can play pseudo 14-bit only with the maximum volume */
2882 ch3 = ch0+sq.block_size_quarter;
2883 ch2 = ch1+sq.block_size_quarter;
2884 custom.aud[2].audvol = 1; /* we are being affected by the beeps */
2885 custom.aud[3].audvol = 1; /* restoring volume here helps a bit */
2886 custom.aud[2].audlc = (u_short *)ZTWO_PADDR(ch2);
2887 custom.aud[2].audlen = size;
2888 custom.aud[3].audlc = (u_short *)ZTWO_PADDR(ch3);
2889 custom.aud[3].audlen = size;
2890 custom.dmacon = AMI_AUDIO_14;
2891 } else
2892 custom.dmacon = AMI_AUDIO_8;
2894 sq.front = (sq.front+1) % sq.max_count;
2895 sq.playing |= AMI_PLAY_LOADED;
2899 static void AmiPlay(void)
2901 int minframes = 1;
2903 custom.intena = IF_AUD0;
2905 if (sq.playing & AMI_PLAY_LOADED) {
2906 /* There's already a frame loaded */
2907 custom.intena = IF_SETCLR | IF_AUD0;
2908 return;
2911 if (sq.playing & AMI_PLAY_PLAYING)
2912 /* Increase threshold: frame 1 is already being played */
2913 minframes = 2;
2915 if (sq.count < minframes) {
2916 /* Nothing to do */
2917 custom.intena = IF_SETCLR | IF_AUD0;
2918 return;
2921 if (sq.count <= minframes && sq.rear_size < sq.block_size && !sq.syncing) {
2922 /* hmmm, the only existing frame is not
2923 * yet filled and we're not syncing?
2925 custom.intena = IF_SETCLR | IF_AUD0;
2926 return;
2929 ami_sq_play_next_frame(minframes);
2931 custom.intena = IF_SETCLR | IF_AUD0;
2935 static void ami_sq_interrupt(int irq, void *dummy, struct pt_regs *fp)
2937 int minframes = 1;
2939 if (!sq.playing) {
2940 /* Playing was interrupted and sq_reset() has already cleared
2941 * the sq variables, so better don't do anything here.
2943 WAKE_UP(sq.sync_queue);
2944 return;
2947 if (sq.playing & AMI_PLAY_PLAYING) {
2948 /* We've just finished a frame */
2949 sq.count--;
2950 WAKE_UP(sq.write_queue);
2953 if (sq.playing & AMI_PLAY_LOADED)
2954 /* Increase threshold: frame 1 is already being played */
2955 minframes = 2;
2957 /* Shift the flags */
2958 sq.playing = (sq.playing<<1) & AMI_PLAY_MASK;
2960 if (!sq.playing)
2961 /* No frame is playing, disable audio DMA */
2962 custom.dmacon = AMI_AUDIO_OFF;
2964 if (sq.count >= minframes)
2965 /* Try to play the next frame */
2966 AmiPlay();
2968 if (!sq.playing)
2969 /* Nothing to play anymore.
2970 Wake up a process waiting for audio output to drain. */
2971 WAKE_UP(sq.sync_queue);
2973 #endif /* CONFIG_AMIGA */
2975 #ifdef CONFIG_PMAC
2978 * PCI PowerMac, with AWACS and DBDMA.
2981 static void *PMacAlloc(unsigned int size, int flags)
2983 return kmalloc(size, flags);
2986 static void PMacFree(void *ptr, unsigned int size)
2988 kfree(ptr);
2991 static int PMacIrqInit(void)
2993 if (request_irq(awacs_irq, pmac_awacs_intr, 0, "AWACS", 0)
2994 || request_irq(awacs_tx_irq, pmac_awacs_tx_intr, 0, "AWACS out", 0))
2995 return 0;
2996 return 1;
2999 #ifdef MODULE
3000 static void PMacIrqCleanup(void)
3002 /* turn off output dma */
3003 out_le32(&awacs_txdma->control, RUN<<16);
3004 /* disable interrupts from awacs interface */
3005 out_le32(&awacs->control, in_le32(&awacs->control) & 0xfff);
3006 free_irq(awacs_irq, pmac_awacs_intr);
3007 free_irq(awacs_tx_irq, pmac_awacs_tx_intr);
3008 kfree(awacs_tx_cmd_space);
3009 if (beep_buf)
3010 kfree(beep_buf);
3011 kd_mksound = orig_mksound;
3013 #endif /* MODULE */
3015 static void PMacSilence(void)
3017 /* turn off output dma */
3018 out_le32(&awacs_txdma->control, RUN<<16);
3021 static int awacs_freqs[8] = {
3022 44100, 29400, 22050, 17640, 14700, 11025, 8820, 7350
3025 static void PMacInit(void)
3027 int i, tolerance;
3029 switch (sound.soft.format) {
3030 case AFMT_S16_LE:
3031 case AFMT_U16_LE:
3032 sound.hard.format = AFMT_S16_LE;
3033 break;
3034 default:
3035 sound.hard.format = AFMT_S16_BE;
3036 break;
3038 sound.hard.stereo = 1;
3039 sound.hard.size = 16;
3042 * If we have a sample rate which is within catchRadius percent
3043 * of the requested value, we don't have to expand the samples.
3044 * Otherwise choose the next higher rate.
3046 i = 8;
3047 do {
3048 tolerance = catchRadius * awacs_freqs[--i] / 100;
3049 } while (sound.soft.speed > awacs_freqs[i] + tolerance && i > 0);
3050 if (sound.soft.speed >= awacs_freqs[i] - tolerance)
3051 sound.trans = &transAwacsNormal;
3052 else
3053 sound.trans = &transAwacsExpand;
3054 sound.hard.speed = awacs_freqs[i];
3055 awacs_rate_index = i;
3057 PMacSilence();
3058 out_le32(&awacs->control, MASK_IEPC | MASK_IEE | (i << 8) | 0x11);
3059 awacs_reg[1] = (awacs_reg[1] & ~MASK_SAMPLERATE) | (i << 3);
3060 awacs_write(awacs_reg[1] | MASK_ADDR1);
3061 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
3063 sound.bal = -sound.soft.speed;
3066 static int PMacSetFormat(int format)
3068 int size;
3070 switch (format) {
3071 case AFMT_QUERY:
3072 return sound.soft.format;
3073 case AFMT_MU_LAW:
3074 case AFMT_A_LAW:
3075 case AFMT_U8:
3076 case AFMT_S8:
3077 size = 8;
3078 break;
3079 case AFMT_S16_BE:
3080 case AFMT_U16_BE:
3081 case AFMT_S16_LE:
3082 case AFMT_U16_LE:
3083 size = 16;
3084 break;
3085 default: /* :-) */
3086 printk(KERN_ERR "dmasound: unknown format 0x%x, using AFMT_U8\n",
3087 format);
3088 size = 8;
3089 format = AFMT_U8;
3092 sound.soft.format = format;
3093 sound.soft.size = size;
3094 if (sound.minDev == SND_DEV_DSP) {
3095 sound.dsp.format = format;
3096 sound.dsp.size = size;
3099 PMacInit();
3101 return format;
3104 #define AWACS_VOLUME_TO_MASK(x) (15 - ((((x) - 1) * 15) / 99))
3105 #define AWACS_MASK_TO_VOLUME(y) (100 - ((y) * 99 / 15))
3107 static int awacs_get_volume(int reg, int lshift)
3109 int volume;
3111 volume = AWACS_MASK_TO_VOLUME((reg >> lshift) & 0xf);
3112 volume |= AWACS_MASK_TO_VOLUME(reg & 0xf) << 8;
3113 return volume;
3116 static int awacs_volume_setter(int volume, int n, int mute, int lshift)
3118 int r1, rn;
3120 if (mute && volume == 0) {
3121 r1 = awacs_reg[1] | mute;
3122 } else {
3123 r1 = awacs_reg[1] & ~mute;
3124 rn = awacs_reg[n] & ~(0xf | (0xf << lshift));
3125 rn |= ((AWACS_VOLUME_TO_MASK(volume & 0xff) & 0xf) << lshift);
3126 rn |= AWACS_VOLUME_TO_MASK((volume >> 8) & 0xff) & 0xf;
3127 awacs_reg[n] = rn;
3128 awacs_write((n << 12) | rn);
3129 volume = awacs_get_volume(rn, lshift);
3131 if (r1 != awacs_reg[1]) {
3132 awacs_reg[1] = r1;
3133 awacs_write(r1 | MASK_ADDR1);
3135 return volume;
3138 static int PMacSetVolume(int volume)
3140 return awacs_volume_setter(volume, 2, MASK_AMUTE, 6);
3143 static void PMacPlay(void)
3145 volatile struct dbdma_cmd *cp;
3146 int i, count;
3147 unsigned long flags;
3149 save_flags(flags); cli();
3150 if (beep_playing) {
3151 /* sound takes precedence over beeps */
3152 out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
3153 out_le32(&awacs->control,
3154 (in_le32(&awacs->control) & ~0x1f00)
3155 || (awacs_rate_index << 8));
3156 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
3157 beep_playing = 0;
3159 i = sq.front + sq.playing;
3160 if (i >= sq.max_count)
3161 i -= sq.max_count;
3162 while (sq.playing < 2 && sq.playing < sq.count) {
3163 count = (sq.count == sq.playing + 1)? sq.rear_size: sq.block_size;
3164 if (count < sq.block_size && !sq.syncing)
3165 /* last block not yet filled, and we're not syncing. */
3166 break;
3167 cp = &awacs_tx_cmds[i];
3168 st_le16(&cp->req_count, count);
3169 st_le16(&cp->xfer_status, 0);
3170 if (++i >= sq.max_count)
3171 i = 0;
3172 out_le16(&awacs_tx_cmds[i].command, DBDMA_STOP);
3173 out_le16(&cp->command, OUTPUT_MORE + INTR_ALWAYS);
3174 if (sq.playing == 0)
3175 out_le32(&awacs_txdma->cmdptr, virt_to_bus(cp));
3176 out_le32(&awacs_txdma->control, ((RUN|WAKE) << 16) + (RUN|WAKE));
3177 ++sq.playing;
3179 restore_flags(flags);
3182 static void
3183 pmac_awacs_tx_intr(int irq, void *devid, struct pt_regs *regs)
3185 int i = sq.front;
3186 int stat;
3187 volatile struct dbdma_cmd *cp;
3189 while (sq.playing > 0) {
3190 cp = &awacs_tx_cmds[i];
3191 stat = ld_le16(&cp->xfer_status);
3192 if ((stat & ACTIVE) == 0)
3193 break; /* this frame is still going */
3194 --sq.count;
3195 --sq.playing;
3196 if (++i >= sq.max_count)
3197 i = 0;
3199 if (i != sq.front)
3200 WAKE_UP(sq.write_queue);
3201 sq.front = i;
3203 PMacPlay();
3205 if (!sq.playing)
3206 WAKE_UP(sq.sync_queue);
3209 static void
3210 pmac_awacs_intr(int irq, void *devid, struct pt_regs *regs)
3212 int ctrl = in_le32(&awacs->control);
3214 if (ctrl & MASK_PORTCHG) {
3215 /* do something when headphone is plugged/unplugged? */
3217 if (ctrl & MASK_CNTLERR) {
3218 printk(KERN_ERR "AWACS: error, status = %x\n",
3219 in_le32(&awacs->codec_stat));
3221 /* Writing 1s to the CNTLERR and PORTCHG bits clears them... */
3222 out_le32(&awacs->control, ctrl);
3225 static void
3226 awacs_write(int val)
3228 while (in_le32(&awacs->codec_ctrl) & MASK_NEWECMD)
3229 ; /* XXX should have timeout */
3230 out_le32(&awacs->codec_ctrl, val);
3233 static void awacs_nosound(unsigned long xx)
3235 unsigned long flags;
3237 save_flags(flags); cli();
3238 if (beep_playing) {
3239 st_le16(&beep_dbdma_cmd->command, DBDMA_STOP);
3240 beep_playing = 0;
3242 restore_flags(flags);
3245 static struct timer_list beep_timer = {
3246 NULL, NULL, 0, 0, awacs_nosound
3249 static void awacs_mksound(unsigned int hz, unsigned int ticks)
3251 unsigned long flags;
3252 int srate = awacs_freqs[BEEP_SPEED];
3253 int period, ncycles, nsamples;
3254 int i, j, f;
3255 short *p;
3256 static int beep_hz_cache;
3257 static int beep_nsamples_cache;
3258 static int beep_volume_cache;
3260 if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) {
3261 #if 1
3262 /* this is a hack for broken X server code */
3263 hz = 750;
3264 ticks = 12;
3265 #else
3266 /* cancel beep currently playing */
3267 awacs_nosound(0);
3268 return;
3269 #endif
3271 save_flags(flags); cli();
3272 del_timer(&beep_timer);
3273 if (ticks) {
3274 beep_timer.expires = jiffies + ticks;
3275 add_timer(&beep_timer);
3277 if (beep_playing || sq.playing || beep_buf == NULL) {
3278 restore_flags(flags);
3279 return; /* too hard, sorry :-( */
3281 beep_playing = 1;
3282 st_le16(&beep_dbdma_cmd->command, OUTPUT_MORE + BR_ALWAYS);
3283 restore_flags(flags);
3285 if (hz == beep_hz_cache && beep_volume == beep_volume_cache) {
3286 nsamples = beep_nsamples_cache;
3287 } else {
3288 period = srate * 256 / hz; /* fixed point */
3289 ncycles = BEEP_BUFLEN * 256 / period;
3290 nsamples = (period * ncycles) >> 8;
3291 f = ncycles * 65536 / nsamples;
3292 j = 0;
3293 p = beep_buf;
3294 for (i = 0; i < nsamples; ++i, p += 2) {
3295 p[0] = p[1] = beep_wform[j >> 8] * beep_volume;
3296 j = (j + f) & 0xffff;
3298 beep_hz_cache = hz;
3299 beep_volume_cache = beep_volume;
3300 beep_nsamples_cache = nsamples;
3303 st_le16(&beep_dbdma_cmd->req_count, nsamples*4);
3304 st_le16(&beep_dbdma_cmd->xfer_status, 0);
3305 st_le32(&beep_dbdma_cmd->cmd_dep, virt_to_bus(beep_dbdma_cmd));
3306 st_le32(&beep_dbdma_cmd->phy_addr, virt_to_bus(beep_buf));
3308 save_flags(flags); cli();
3309 if (beep_playing) { /* i.e. haven't been terminated already */
3310 out_le32(&awacs_txdma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
3311 out_le32(&awacs->control,
3312 (in_le32(&awacs->control) & ~0x1f00)
3313 | (BEEP_SPEED << 8));
3314 out_le32(&awacs->byteswap, 0);
3315 out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
3316 out_le32(&awacs_txdma->control, RUN | (RUN << 16));
3318 restore_flags(flags);
3321 #ifdef CONFIG_PMAC_PBOOK
3323 * Save state when going to sleep, restore it afterwards.
3325 static int awacs_sleep_notify(struct notifier_block *this,
3326 unsigned long code, void *x)
3328 switch (code) {
3329 case PBOOK_SLEEP:
3330 /* XXX we should stop any dma in progress when going to sleep
3331 and restart it when we wake. */
3332 PMacSilence();
3333 break;
3334 case PBOOK_WAKE:
3335 out_le32(&awacs->control, MASK_IEPC | MASK_IEE |
3336 (awacs_rate_index << 8) | 0x11);
3337 awacs_write(awacs_reg[0] | MASK_ADDR0);
3338 awacs_write(awacs_reg[1] | MASK_ADDR1);
3339 awacs_write(awacs_reg[2] | MASK_ADDR2);
3340 awacs_write(awacs_reg[4] | MASK_ADDR4);
3341 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
3343 return NOTIFY_DONE;
3345 #endif /* CONFIG_PMAC_PBOOK */
3347 #endif /* CONFIG_PMAC */
3349 /*** Machine definitions *****************************************************/
3352 #ifdef CONFIG_ATARI
3353 static MACHINE machTT = {
3354 DMASND_TT, AtaAlloc, AtaFree, AtaIrqInit,
3355 #ifdef MODULE
3356 AtaIrqCleanUp,
3357 #endif /* MODULE */
3358 TTInit, TTSilence, TTSetFormat, TTSetVolume,
3359 AtaSetBass, AtaSetTreble, TTSetGain,
3360 AtaPlay
3363 static MACHINE machFalcon = {
3364 DMASND_FALCON, AtaAlloc, AtaFree, AtaIrqInit,
3365 #ifdef MODULE
3366 AtaIrqCleanUp,
3367 #endif /* MODULE */
3368 FalconInit, FalconSilence, FalconSetFormat, FalconSetVolume,
3369 AtaSetBass, AtaSetTreble, NULL,
3370 AtaPlay
3372 #endif /* CONFIG_ATARI */
3374 #ifdef CONFIG_AMIGA
3375 static MACHINE machAmiga = {
3376 DMASND_AMIGA, AmiAlloc, AmiFree, AmiIrqInit,
3377 #ifdef MODULE
3378 AmiIrqCleanUp,
3379 #endif /* MODULE */
3380 AmiInit, AmiSilence, AmiSetFormat, AmiSetVolume,
3381 NULL, AmiSetTreble, NULL,
3382 AmiPlay
3384 #endif /* CONFIG_AMIGA */
3386 #ifdef CONFIG_PMAC
3387 static MACHINE machPMac = {
3388 DMASND_AWACS, PMacAlloc, PMacFree, PMacIrqInit,
3389 #ifdef MODULE
3390 PMacIrqCleanup,
3391 #endif /* MODULE */
3392 PMacInit, PMacSilence, PMacSetFormat, PMacSetVolume,
3393 NULL, NULL, NULL, /* bass, treble, gain */
3394 PMacPlay
3396 #endif /* CONFIG_AMIGA */
3399 /*** Mid level stuff *********************************************************/
3402 static void sound_silence(void)
3404 /* update hardware settings one more */
3405 (*sound.mach.init)();
3407 (*sound.mach.silence)();
3411 static void sound_init(void)
3413 (*sound.mach.init)();
3417 static int sound_set_format(int format)
3419 return(*sound.mach.setFormat)(format);
3423 static int sound_set_speed(int speed)
3425 if (speed < 0)
3426 return(sound.soft.speed);
3428 sound.soft.speed = speed;
3429 (*sound.mach.init)();
3430 if (sound.minDev == SND_DEV_DSP)
3431 sound.dsp.speed = sound.soft.speed;
3433 return(sound.soft.speed);
3437 static int sound_set_stereo(int stereo)
3439 if (stereo < 0)
3440 return(sound.soft.stereo);
3442 stereo = !!stereo; /* should be 0 or 1 now */
3444 sound.soft.stereo = stereo;
3445 if (sound.minDev == SND_DEV_DSP)
3446 sound.dsp.stereo = stereo;
3447 (*sound.mach.init)();
3449 return(stereo);
3453 static int sound_set_volume(int volume)
3455 return(*sound.mach.setVolume)(volume);
3459 #ifdef CONFIG_ATARI
3460 static int sound_set_bass(int bass)
3462 return(sound.mach.setBass ? (*sound.mach.setBass)(bass) : 50);
3465 static int sound_set_gain(int gain)
3467 return sound.mach.setGain ? sound.mach.setGain(gain) : 100;
3469 #endif /* CONFIG_ATARI */
3471 #if defined(CONFIG_ATARI) || defined(CONFIG_AMIGA)
3472 static int sound_set_treble(int treble)
3474 return(sound.mach.setTreble ? (*sound.mach.setTreble)(treble) : 50);
3476 #endif /* CONFIG_ATARI || CONFIG_AMIGA */
3479 static ssize_t sound_copy_translate(const u_char *userPtr,
3480 size_t userCount,
3481 u_char frame[], ssize_t *frameUsed,
3482 ssize_t frameLeft)
3484 ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
3486 switch (sound.soft.format) {
3487 case AFMT_MU_LAW:
3488 ct_func = sound.trans->ct_ulaw;
3489 break;
3490 case AFMT_A_LAW:
3491 ct_func = sound.trans->ct_alaw;
3492 break;
3493 case AFMT_S8:
3494 ct_func = sound.trans->ct_s8;
3495 break;
3496 case AFMT_U8:
3497 ct_func = sound.trans->ct_u8;
3498 break;
3499 case AFMT_S16_BE:
3500 ct_func = sound.trans->ct_s16be;
3501 break;
3502 case AFMT_U16_BE:
3503 ct_func = sound.trans->ct_u16be;
3504 break;
3505 case AFMT_S16_LE:
3506 ct_func = sound.trans->ct_s16le;
3507 break;
3508 case AFMT_U16_LE:
3509 ct_func = sound.trans->ct_u16le;
3510 break;
3512 if (ct_func)
3513 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
3514 else
3515 return 0;
3520 * /dev/mixer abstraction
3524 #define RECLEVEL_VOXWARE_TO_GAIN(v) \
3525 ((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
3526 #define RECLEVEL_GAIN_TO_VOXWARE(v) (((v) * 20 + 2) / 3)
3529 static int mixer_open(struct inode *inode, struct file *file)
3531 MOD_INC_USE_COUNT;
3532 mixer.busy = 1;
3533 return 0;
3537 static int mixer_release(struct inode *inode, struct file *file)
3539 mixer.busy = 0;
3540 MOD_DEC_USE_COUNT;
3541 return 0;
3545 static int mixer_ioctl(struct inode *inode, struct file *file, u_int cmd,
3546 u_long arg)
3548 int data;
3549 switch (sound.mach.type) {
3550 #ifdef CONFIG_ATARI
3551 case DMASND_FALCON:
3552 switch (cmd) {
3553 case SOUND_MIXER_READ_DEVMASK:
3554 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC | SOUND_MASK_SPEAKER);
3555 case SOUND_MIXER_READ_RECMASK:
3556 return IOCTL_OUT(arg, SOUND_MASK_MIC);
3557 case SOUND_MIXER_READ_STEREODEVS:
3558 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_MIC);
3559 case SOUND_MIXER_READ_CAPS:
3560 return IOCTL_OUT(arg, SOUND_CAP_EXCL_INPUT);
3561 case SOUND_MIXER_READ_VOLUME:
3562 return IOCTL_OUT(arg,
3563 VOLUME_ATT_TO_VOXWARE(sound.volume_left) |
3564 VOLUME_ATT_TO_VOXWARE(sound.volume_right) << 8);
3565 case SOUND_MIXER_WRITE_MIC:
3566 IOCTL_IN(arg, data);
3567 tt_dmasnd.input_gain =
3568 RECLEVEL_VOXWARE_TO_GAIN(data & 0xff) << 4 |
3569 RECLEVEL_VOXWARE_TO_GAIN(data >> 8 & 0xff);
3570 /* fall thru, return set value */
3571 case SOUND_MIXER_READ_MIC:
3572 return IOCTL_OUT(arg,
3573 RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain >> 4 & 0xf) |
3574 RECLEVEL_GAIN_TO_VOXWARE(tt_dmasnd.input_gain & 0xf) << 8);
3575 case SOUND_MIXER_READ_SPEAKER:
3577 int porta;
3578 cli();
3579 sound_ym.rd_data_reg_sel = 14;
3580 porta = sound_ym.rd_data_reg_sel;
3581 sti();
3582 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3584 case SOUND_MIXER_WRITE_VOLUME:
3585 IOCTL_IN(arg, data);
3586 return IOCTL_OUT(arg, sound_set_volume(data));
3587 case SOUND_MIXER_WRITE_SPEAKER:
3589 int porta;
3590 IOCTL_IN(arg, data);
3591 cli();
3592 sound_ym.rd_data_reg_sel = 14;
3593 porta = (sound_ym.rd_data_reg_sel & ~0x40) |
3594 (data < 50 ? 0x40 : 0);
3595 sound_ym.wd_data = porta;
3596 sti();
3597 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3600 break;
3602 case DMASND_TT:
3603 switch (cmd) {
3604 case SOUND_MIXER_READ_DEVMASK:
3605 return IOCTL_OUT(arg,
3606 SOUND_MASK_VOLUME | SOUND_MASK_TREBLE | SOUND_MASK_BASS |
3607 (MACH_IS_TT ? SOUND_MASK_SPEAKER : 0));
3608 case SOUND_MIXER_READ_RECMASK:
3609 return IOCTL_OUT(arg, 0);
3610 case SOUND_MIXER_READ_STEREODEVS:
3611 return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
3612 case SOUND_MIXER_READ_VOLUME:
3613 return IOCTL_OUT(arg,
3614 VOLUME_DB_TO_VOXWARE(sound.volume_left) |
3615 (VOLUME_DB_TO_VOXWARE(sound.volume_right) << 8));
3616 case SOUND_MIXER_READ_BASS:
3617 return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(sound.bass));
3618 case SOUND_MIXER_READ_TREBLE:
3619 return IOCTL_OUT(arg, TONE_DB_TO_VOXWARE(sound.treble));
3620 case SOUND_MIXER_READ_OGAIN:
3621 return IOCTL_OUT(arg, GAIN_DB_TO_VOXWARE(sound.gain));
3622 case SOUND_MIXER_READ_SPEAKER:
3624 int porta;
3625 if (MACH_IS_TT) {
3626 cli();
3627 sound_ym.rd_data_reg_sel = 14;
3628 porta = sound_ym.rd_data_reg_sel;
3629 sti();
3630 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3633 break;
3634 case SOUND_MIXER_WRITE_VOLUME:
3635 IOCTL_IN(arg, data);
3636 return IOCTL_OUT(arg, sound_set_volume(data));
3637 case SOUND_MIXER_WRITE_BASS:
3638 IOCTL_IN(arg, data);
3639 return IOCTL_OUT(arg, sound_set_bass(data));
3640 case SOUND_MIXER_WRITE_TREBLE:
3641 IOCTL_IN(arg, data);
3642 return IOCTL_OUT(arg, sound_set_treble(data));
3643 case SOUND_MIXER_WRITE_OGAIN:
3644 IOCTL_IN(arg, data);
3645 return IOCTL_OUT(arg, sound_set_gain(data));
3646 case SOUND_MIXER_WRITE_SPEAKER:
3647 if (MACH_IS_TT) {
3648 int porta;
3649 IOCTL_IN(arg, data);
3650 cli();
3651 sound_ym.rd_data_reg_sel = 14;
3652 porta = (sound_ym.rd_data_reg_sel & ~0x40) |
3653 (data < 50 ? 0x40 : 0);
3654 sound_ym.wd_data = porta;
3655 sti();
3656 return IOCTL_OUT(arg, porta & 0x40 ? 0 : 100);
3659 break;
3660 #endif /* CONFIG_ATARI */
3662 #ifdef CONFIG_AMIGA
3663 case DMASND_AMIGA:
3664 switch (cmd) {
3665 case SOUND_MIXER_READ_DEVMASK:
3666 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_TREBLE);
3667 case SOUND_MIXER_READ_RECMASK:
3668 return IOCTL_OUT(arg, 0);
3669 case SOUND_MIXER_READ_STEREODEVS:
3670 return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
3671 case SOUND_MIXER_READ_VOLUME:
3672 return IOCTL_OUT(arg,
3673 VOLUME_AMI_TO_VOXWARE(sound.volume_left) |
3674 VOLUME_AMI_TO_VOXWARE(sound.volume_right) << 8);
3675 case SOUND_MIXER_WRITE_VOLUME:
3676 IOCTL_IN(arg, data);
3677 return IOCTL_OUT(arg, sound_set_volume(data));
3678 case SOUND_MIXER_READ_TREBLE:
3679 return IOCTL_OUT(arg, sound.treble);
3680 case SOUND_MIXER_WRITE_TREBLE:
3681 IOCTL_IN(arg, data);
3682 return IOCTL_OUT(arg, sound_set_treble(data));
3684 break;
3685 #endif /* CONFIG_AMIGA */
3687 #ifdef CONFIG_PMAC
3688 case DMASND_AWACS:
3689 switch (cmd) {
3690 case SOUND_MIXER_READ_DEVMASK:
3691 data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
3692 | SOUND_MASK_LINE | SOUND_MASK_MIC
3693 | SOUND_MASK_CD | SOUND_MASK_RECLEV
3694 | SOUND_MASK_ALTPCM;
3695 return IOCTL_OUT(arg, data);
3696 case SOUND_MIXER_READ_RECMASK:
3697 data = SOUND_MASK_LINE | SOUND_MASK_MIC
3698 | SOUND_MASK_CD;
3699 return IOCTL_OUT(arg, data);
3700 case SOUND_MIXER_READ_RECSRC:
3701 data = 0;
3702 if (awacs_reg[0] & MASK_MUX_AUDIN)
3703 data |= SOUND_MASK_LINE;
3704 if (awacs_reg[0] & MASK_MUX_MIC)
3705 data |= SOUND_MASK_MIC;
3706 if (awacs_reg[0] & MASK_MUX_CD)
3707 data |= SOUND_MASK_CD;
3708 return IOCTL_OUT(arg, data);
3709 case SOUND_MIXER_WRITE_RECSRC:
3710 IOCTL_IN(arg, data);
3711 data &= (SOUND_MASK_LINE
3712 | SOUND_MASK_MIC | SOUND_MASK_CD);
3713 awacs_reg[0] &= ~(MASK_MUX_CD | MASK_MUX_MIC
3714 | MASK_MUX_AUDIN);
3715 if (data & SOUND_MASK_LINE)
3716 awacs_reg[0] |= MASK_MUX_AUDIN;
3717 if (data & SOUND_MASK_MIC)
3718 awacs_reg[0] |= MASK_MUX_MIC;
3719 if (data & SOUND_MASK_CD)
3720 awacs_reg[0] |= MASK_MUX_CD;
3721 awacs_write(awacs_reg[0] | MASK_ADDR0);
3722 return IOCTL_OUT(arg, data);
3723 case SOUND_MIXER_READ_STEREODEVS:
3724 data = SOUND_MASK_VOLUME | SOUND_MASK_SPEAKER
3725 | SOUND_MASK_RECLEV;
3726 return IOCTL_OUT(arg, data);
3727 case SOUND_MIXER_READ_CAPS:
3728 return IOCTL_OUT(arg, 0);
3729 case SOUND_MIXER_READ_VOLUME:
3730 data = (awacs_reg[1] & MASK_AMUTE)? 0:
3731 awacs_get_volume(awacs_reg[2], 6);
3732 return IOCTL_OUT(arg, data);
3733 case SOUND_MIXER_WRITE_VOLUME:
3734 IOCTL_IN(arg, data);
3735 return IOCTL_OUT(arg, sound_set_volume(data));
3736 case SOUND_MIXER_READ_SPEAKER:
3737 data = (awacs_reg[1] & MASK_CMUTE)? 0:
3738 awacs_get_volume(awacs_reg[4], 6);
3739 return IOCTL_OUT(arg, data);
3740 case SOUND_MIXER_WRITE_SPEAKER:
3741 IOCTL_IN(arg, data);
3742 data = awacs_volume_setter(data, 4, MASK_CMUTE, 6);
3743 return IOCTL_OUT(arg, data);
3744 case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
3745 IOCTL_IN(arg, data);
3746 beep_volume = data & 0xff;
3747 /* fall through */
3748 case SOUND_MIXER_READ_ALTPCM:
3749 return IOCTL_OUT(arg, beep_volume);
3750 case SOUND_MIXER_WRITE_LINE:
3751 IOCTL_IN(arg, data);
3752 awacs_reg[0] &= ~MASK_MUX_AUDIN;
3753 if ((data & 0xff) >= 50)
3754 awacs_reg[0] |= MASK_MUX_AUDIN;
3755 awacs_write(MASK_ADDR0 | awacs_reg[0]);
3756 /* fall through */
3757 case SOUND_MIXER_READ_LINE:
3758 data = (awacs_reg[0] & MASK_MUX_AUDIN)? 100: 0;
3759 return IOCTL_OUT(arg, data);
3760 case SOUND_MIXER_WRITE_MIC:
3761 IOCTL_IN(arg, data);
3762 data &= 0xff;
3763 awacs_reg[0] &= ~(MASK_MUX_MIC | MASK_GAINLINE);
3764 if (data >= 25) {
3765 awacs_reg[0] |= MASK_MUX_MIC;
3766 if (data >= 75)
3767 awacs_reg[0] |= MASK_GAINLINE;
3769 awacs_write(MASK_ADDR0 | awacs_reg[0]);
3770 /* fall through */
3771 case SOUND_MIXER_READ_MIC:
3772 data = (awacs_reg[0] & MASK_MUX_MIC)?
3773 (awacs_reg[0] & MASK_GAINLINE? 100: 50): 0;
3774 return IOCTL_OUT(arg, data);
3775 case SOUND_MIXER_WRITE_CD:
3776 IOCTL_IN(arg, data);
3777 awacs_reg[0] &= ~MASK_MUX_CD;
3778 if ((data & 0xff) >= 50)
3779 awacs_reg[0] |= MASK_MUX_CD;
3780 awacs_write(MASK_ADDR0 | awacs_reg[0]);
3781 /* fall through */
3782 case SOUND_MIXER_READ_CD:
3783 data = (awacs_reg[0] & MASK_MUX_CD)? 100: 0;
3784 return IOCTL_OUT(arg, data);
3785 case SOUND_MIXER_WRITE_RECLEV:
3786 IOCTL_IN(arg, data);
3787 data = awacs_volume_setter(data, 0, 0, 4);
3788 return IOCTL_OUT(arg, data);
3789 case SOUND_MIXER_READ_RECLEV:
3790 data = awacs_get_volume(awacs_reg[0], 4);
3791 return IOCTL_OUT(arg, data);
3793 break;
3794 #endif
3797 return -EINVAL;
3801 static struct file_operations mixer_fops =
3803 sound_lseek,
3804 NULL, /* mixer_read */
3805 NULL, /* mixer_write */
3806 NULL, /* mixer_readdir */
3807 NULL, /* mixer_poll */
3808 mixer_ioctl,
3809 NULL, /* mixer_mmap */
3810 mixer_open,
3811 NULL, /* flush */
3812 mixer_release,
3816 __initfunc(static void mixer_init(void))
3818 #ifndef MODULE
3819 int mixer_unit;
3820 #endif
3821 mixer_unit = register_sound_mixer(&mixer_fops);
3822 if (mixer_unit < 0)
3823 return;
3825 mixer.busy = 0;
3826 sound.treble = 0;
3827 sound.bass = 0;
3828 switch (sound.mach.type) {
3829 #ifdef CONFIG_ATARI
3830 case DMASND_TT:
3831 atari_microwire_cmd(MW_LM1992_VOLUME(0));
3832 sound.volume_left = 0;
3833 atari_microwire_cmd(MW_LM1992_BALLEFT(0));
3834 sound.volume_right = 0;
3835 atari_microwire_cmd(MW_LM1992_BALRIGHT(0));
3836 atari_microwire_cmd(MW_LM1992_TREBLE(0));
3837 atari_microwire_cmd(MW_LM1992_BASS(0));
3838 break;
3839 case DMASND_FALCON:
3840 sound.volume_left = (tt_dmasnd.output_atten & 0xf00) >> 8;
3841 sound.volume_right = (tt_dmasnd.output_atten & 0xf0) >> 4;
3842 break;
3843 #endif /* CONFIG_ATARI */
3844 #ifdef CONFIG_AMIGA
3845 case DMASND_AMIGA:
3846 sound.volume_left = 64;
3847 sound.volume_right = 64;
3848 custom.aud[0].audvol = sound.volume_left;
3849 custom.aud[3].audvol = 1; /* For pseudo 14bit */
3850 custom.aud[1].audvol = sound.volume_right;
3851 custom.aud[2].audvol = 1; /* For pseudo 14bit */
3852 sound.treble = 50;
3853 break;
3854 #endif /* CONFIG_AMIGA */
3860 * Sound queue stuff, the heart of the driver
3864 static void sq_setup(int numBufs, int bufSize, char **buffers)
3866 #ifdef CONFIG_PMAC
3867 int i;
3868 volatile struct dbdma_cmd *cp;
3869 #endif /* CONFIG_PMAC */
3871 sq.max_count = numBufs;
3872 sq.max_active = numBufs;
3873 sq.block_size = bufSize;
3874 sq.buffers = buffers;
3876 sq.front = sq.count = 0;
3877 sq.rear = -1;
3878 sq.write_queue = sq.open_queue = sq.sync_queue = 0;
3879 sq.busy = 0;
3880 sq.syncing = 0;
3882 sq.playing = 0;
3884 #ifdef CONFIG_ATARI
3885 sq.ignore_int = 0;
3886 #endif /* CONFIG_ATARI */
3887 #ifdef CONFIG_AMIGA
3888 sq.block_size_half = sq.block_size>>1;
3889 sq.block_size_quarter = sq.block_size_half>>1;
3890 #endif /* CONFIG_AMIGA */
3891 #ifdef CONFIG_PMAC
3892 cp = awacs_tx_cmds;
3893 memset((void *) cp, 0, (numBufs + 1) * sizeof(struct dbdma_cmd));
3894 for (i = 0; i < numBufs; ++i, ++cp) {
3895 st_le32(&cp->phy_addr, virt_to_bus(buffers[i]));
3897 st_le16(&cp->command, DBDMA_NOP + BR_ALWAYS);
3898 st_le32(&cp->cmd_dep, virt_to_bus(awacs_tx_cmds));
3899 out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
3900 out_le32(&awacs_txdma->cmdptr, virt_to_bus(awacs_tx_cmds));
3901 #endif /* CONFIG_PMAC */
3904 static void sq_play(void)
3906 (*sound.mach.play)();
3910 /* ++TeSche: radically changed this one too */
3912 static ssize_t sq_write(struct file *file, const char *src, size_t uLeft,
3913 loff_t *ppos)
3915 ssize_t uWritten = 0;
3916 u_char *dest;
3917 ssize_t uUsed, bUsed, bLeft;
3919 /* ++TeSche: Is something like this necessary?
3920 * Hey, that's an honest question! Or does any other part of the
3921 * filesystem already checks this situation? I really don't know.
3923 if (uLeft == 0)
3924 return 0;
3926 /* The interrupt doesn't start to play the last, incomplete frame.
3927 * Thus we can append to it without disabling the interrupts! (Note
3928 * also that sq.rear isn't affected by the interrupt.)
3931 if (sq.count > 0 && (bLeft = sq.block_size-sq.rear_size) > 0) {
3932 dest = sq_block_address(sq.rear);
3933 bUsed = sq.rear_size;
3934 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
3935 if (uUsed <= 0)
3936 return uUsed;
3937 src += uUsed;
3938 uWritten += uUsed;
3939 uLeft -= uUsed;
3940 sq.rear_size = bUsed;
3943 do {
3944 while (sq.count == sq.max_active) {
3945 sq_play();
3946 if (NON_BLOCKING(sq.open_mode))
3947 return uWritten > 0 ? uWritten : -EAGAIN;
3948 SLEEP(sq.write_queue, ONE_SECOND);
3949 if (SIGNAL_RECEIVED)
3950 return uWritten > 0 ? uWritten : -EINTR;
3953 /* Here, we can avoid disabling the interrupt by first
3954 * copying and translating the data, and then updating
3955 * the sq variables. Until this is done, the interrupt
3956 * won't see the new frame and we can work on it
3957 * undisturbed.
3960 dest = sq_block_address((sq.rear+1) % sq.max_count);
3961 bUsed = 0;
3962 bLeft = sq.block_size;
3963 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
3964 if (uUsed <= 0)
3965 break;
3966 src += uUsed;
3967 uWritten += uUsed;
3968 uLeft -= uUsed;
3969 if (bUsed) {
3970 sq.rear = (sq.rear+1) % sq.max_count;
3971 sq.rear_size = bUsed;
3972 sq.count++;
3974 } while (bUsed); /* uUsed may have been 0 */
3976 sq_play();
3978 return uUsed < 0? uUsed: uWritten;
3982 static int sq_open(struct inode *inode, struct file *file)
3984 int rc = 0;
3986 MOD_INC_USE_COUNT;
3987 if (sq.busy) {
3988 rc = -EBUSY;
3989 if (NON_BLOCKING(file->f_flags))
3990 goto err_out;
3991 rc = -EINTR;
3992 while (sq.busy) {
3993 SLEEP(sq.open_queue, ONE_SECOND);
3994 if (SIGNAL_RECEIVED)
3995 goto err_out;
3997 rc = 0;
3999 sq_setup(numBufs, bufSize << 10, sound_buffers);
4000 sq.open_mode = file->f_flags;
4001 sq.busy = 1;
4002 #ifdef CONFIG_ATARI
4003 sq.ignore_int = 1;
4004 #endif /* CONFIG_ATARI */
4005 sound.minDev = MINOR(inode->i_rdev) & 0x0f;
4006 sound.soft = sound.dsp;
4007 sound.hard = sound.dsp;
4008 sound_init();
4009 if ((MINOR(inode->i_rdev) & 0x0f) == SND_DEV_AUDIO) {
4010 sound_set_speed(8000);
4011 sound_set_stereo(0);
4012 sound_set_format(AFMT_MU_LAW);
4014 return 0;
4015 err_out:
4016 MOD_DEC_USE_COUNT;
4017 return rc;
4021 static void sq_reset(void)
4023 sound_silence();
4024 sq.playing = 0;
4025 sq.count = 0;
4026 sq.front = (sq.rear+1) % sq.max_count;
4030 static int sq_fsync(struct file *filp, struct dentry *dentry)
4032 int rc = 0;
4034 sq.syncing = 1;
4035 sq_play(); /* there may be an incomplete frame waiting */
4037 while (sq.playing) {
4038 SLEEP(sq.sync_queue, ONE_SECOND);
4039 if (SIGNAL_RECEIVED) {
4040 /* While waiting for audio output to drain, an
4041 * interrupt occurred. Stop audio output immediately
4042 * and clear the queue. */
4043 sq_reset();
4044 rc = -EINTR;
4045 break;
4049 sq.syncing = 0;
4050 return rc;
4053 static int sq_release(struct inode *inode, struct file *file)
4055 int rc = 0;
4056 if (sq.busy) {
4057 rc = sq_fsync(file, file->f_dentry);
4058 sq.busy = 0;
4059 WAKE_UP(sq.open_queue);
4060 /* Wake up a process waiting for the queue being released.
4061 * Note: There may be several processes waiting for a call
4062 * to open() returning. */
4064 sound.soft = sound.dsp;
4065 sound.hard = sound.dsp;
4066 sound_silence();
4067 if (rc == 0)
4068 MOD_DEC_USE_COUNT;
4069 return rc;
4073 static int sq_ioctl(struct inode *inode, struct file *file, u_int cmd,
4074 u_long arg)
4076 u_long fmt;
4077 int data;
4078 int size, nbufs;
4080 switch (cmd) {
4081 case SNDCTL_DSP_RESET:
4082 sq_reset();
4083 return 0;
4084 case SNDCTL_DSP_POST:
4085 case SNDCTL_DSP_SYNC:
4086 return sq_fsync(file, file->f_dentry);
4088 /* ++TeSche: before changing any of these it's
4089 * probably wise to wait until sound playing has
4090 * settled down. */
4091 case SNDCTL_DSP_SPEED:
4092 sq_fsync(file, file->f_dentry);
4093 IOCTL_IN(arg, data);
4094 return IOCTL_OUT(arg, sound_set_speed(data));
4095 case SNDCTL_DSP_STEREO:
4096 sq_fsync(file, file->f_dentry);
4097 IOCTL_IN(arg, data);
4098 return IOCTL_OUT(arg, sound_set_stereo(data));
4099 case SOUND_PCM_WRITE_CHANNELS:
4100 sq_fsync(file, file->f_dentry);
4101 IOCTL_IN(arg, data);
4102 return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
4103 case SNDCTL_DSP_SETFMT:
4104 sq_fsync(file, file->f_dentry);
4105 IOCTL_IN(arg, data);
4106 return IOCTL_OUT(arg, sound_set_format(data));
4107 case SNDCTL_DSP_GETFMTS:
4108 fmt = 0;
4109 if (sound.trans) {
4110 if (sound.trans->ct_ulaw)
4111 fmt |= AFMT_MU_LAW;
4112 if (sound.trans->ct_alaw)
4113 fmt |= AFMT_A_LAW;
4114 if (sound.trans->ct_s8)
4115 fmt |= AFMT_S8;
4116 if (sound.trans->ct_u8)
4117 fmt |= AFMT_U8;
4118 if (sound.trans->ct_s16be)
4119 fmt |= AFMT_S16_BE;
4120 if (sound.trans->ct_u16be)
4121 fmt |= AFMT_U16_BE;
4122 if (sound.trans->ct_s16le)
4123 fmt |= AFMT_S16_LE;
4124 if (sound.trans->ct_u16le)
4125 fmt |= AFMT_U16_LE;
4127 return IOCTL_OUT(arg, fmt);
4128 case SNDCTL_DSP_GETBLKSIZE:
4129 size = sq.block_size
4130 * sound.soft.size * (sound.soft.stereo + 1)
4131 / (sound.hard.size * (sound.hard.stereo + 1));
4132 return IOCTL_OUT(arg, size);
4133 case SNDCTL_DSP_SUBDIVIDE:
4134 break;
4135 case SNDCTL_DSP_SETFRAGMENT:
4136 if (sq.count || sq.playing || sq.syncing)
4137 return -EINVAL;
4138 IOCTL_IN(arg, size);
4139 nbufs = size >> 16;
4140 if (nbufs < 2 || nbufs > numBufs)
4141 nbufs = numBufs;
4142 size &= 0xffff;
4143 if (size >= 8 && size <= 30) {
4144 size = 1 << size;
4145 size *= sound.hard.size * (sound.hard.stereo + 1);
4146 size /= sound.soft.size * (sound.soft.stereo + 1);
4147 if (size > (bufSize << 10))
4148 size = bufSize << 10;
4149 } else
4150 size = bufSize << 10;
4151 sq_setup(numBufs, size, sound_buffers);
4152 sq.max_active = nbufs;
4153 return 0;
4155 default:
4156 return mixer_ioctl(inode, file, cmd, arg);
4158 return -EINVAL;
4163 static struct file_operations sq_fops =
4165 sound_lseek,
4166 NULL, /* sq_read */
4167 sq_write,
4168 NULL, /* sq_readdir */
4169 NULL, /* sq_poll */
4170 sq_ioctl,
4171 NULL, /* sq_mmap */
4172 sq_open,
4173 NULL, /* flush */
4174 sq_release,
4178 __initfunc(static void sq_init(void))
4180 #ifndef MODULE
4181 int sq_unit;
4182 #endif
4183 sq_unit = register_sound_dsp(&sq_fops);
4184 if (sq_unit < 0)
4185 return;
4187 /* whatever you like as startup mode for /dev/dsp,
4188 * (/dev/audio hasn't got a startup mode). note that
4189 * once changed a new open() will *not* restore these!
4191 sound.dsp.format = AFMT_U8;
4192 sound.dsp.stereo = 0;
4193 sound.dsp.size = 8;
4195 /* set minimum rate possible without expanding */
4196 switch (sound.mach.type) {
4197 #ifdef CONFIG_ATARI
4198 case DMASND_TT:
4199 sound.dsp.speed = 6258;
4200 break;
4201 case DMASND_FALCON:
4202 sound.dsp.speed = 8195;
4203 break;
4204 #endif /* CONFIG_ATARI */
4205 #ifdef CONFIG_AMIGA
4206 case DMASND_AMIGA:
4207 sound.dsp.speed = 8000;
4208 break;
4209 #endif /* CONFIG_AMIGA */
4210 #ifdef CONFIG_PMAC
4211 case DMASND_AWACS:
4212 sound.dsp.speed = 8000;
4213 break;
4214 #endif /* CONFIG_PMAC */
4217 /* before the first open to /dev/dsp this wouldn't be set */
4218 sound.soft = sound.dsp;
4219 sound.hard = sound.dsp;
4221 sound_silence();
4225 * /dev/sndstat
4229 /* state.buf should not overflow! */
4231 static int state_open(struct inode *inode, struct file *file)
4233 char *buffer = state.buf, *mach = "";
4234 int len = 0;
4236 if (state.busy)
4237 return -EBUSY;
4239 MOD_INC_USE_COUNT;
4240 state.ptr = 0;
4241 state.busy = 1;
4243 switch (sound.mach.type) {
4244 #ifdef CONFIG_ATARI
4245 case DMASND_TT:
4246 case DMASND_FALCON:
4247 mach = "Atari ";
4248 break;
4249 #endif /* CONFIG_ATARI */
4250 #ifdef CONFIG_AMIGA
4251 case DMASND_AMIGA:
4252 mach = "Amiga ";
4253 break;
4254 #endif /* CONFIG_AMIGA */
4255 #ifdef CONFIG_PMAC
4256 case DMASND_AWACS:
4257 mach = "PowerMac ";
4258 break;
4259 #endif /* CONFIG_PMAC */
4261 len += sprintf(buffer+len, "%sDMA sound driver:\n", mach);
4263 len += sprintf(buffer+len, "\tsound.format = 0x%x", sound.soft.format);
4264 switch (sound.soft.format) {
4265 case AFMT_MU_LAW:
4266 len += sprintf(buffer+len, " (mu-law)");
4267 break;
4268 case AFMT_A_LAW:
4269 len += sprintf(buffer+len, " (A-law)");
4270 break;
4271 case AFMT_U8:
4272 len += sprintf(buffer+len, " (unsigned 8 bit)");
4273 break;
4274 case AFMT_S8:
4275 len += sprintf(buffer+len, " (signed 8 bit)");
4276 break;
4277 case AFMT_S16_BE:
4278 len += sprintf(buffer+len, " (signed 16 bit big)");
4279 break;
4280 case AFMT_U16_BE:
4281 len += sprintf(buffer+len, " (unsigned 16 bit big)");
4282 break;
4283 case AFMT_S16_LE:
4284 len += sprintf(buffer+len, " (signed 16 bit little)");
4285 break;
4286 case AFMT_U16_LE:
4287 len += sprintf(buffer+len, " (unsigned 16 bit little)");
4288 break;
4290 len += sprintf(buffer+len, "\n");
4291 len += sprintf(buffer+len, "\tsound.speed = %dHz (phys. %dHz)\n",
4292 sound.soft.speed, sound.hard.speed);
4293 len += sprintf(buffer+len, "\tsound.stereo = 0x%x (%s)\n",
4294 sound.soft.stereo, sound.soft.stereo ? "stereo" : "mono");
4295 switch (sound.mach.type) {
4296 #ifdef CONFIG_ATARI
4297 case DMASND_TT:
4298 len += sprintf(buffer+len, "\tsound.volume_left = %ddB [-40...0]\n",
4299 sound.volume_left);
4300 len += sprintf(buffer+len, "\tsound.volume_right = %ddB [-40...0]\n",
4301 sound.volume_right);
4302 len += sprintf(buffer+len, "\tsound.bass = %ddB [-12...+12]\n",
4303 sound.bass);
4304 len += sprintf(buffer+len, "\tsound.treble = %ddB [-12...+12]\n",
4305 sound.treble);
4306 break;
4307 case DMASND_FALCON:
4308 len += sprintf(buffer+len, "\tsound.volume_left = %ddB [-22.5...0]\n",
4309 sound.volume_left);
4310 len += sprintf(buffer+len, "\tsound.volume_right = %ddB [-22.5...0]\n",
4311 sound.volume_right);
4312 break;
4313 #endif /* CONFIG_ATARI */
4314 #ifdef CONFIG_AMIGA
4315 case DMASND_AMIGA:
4316 len += sprintf(buffer+len, "\tsound.volume_left = %d [0...64]\n",
4317 sound.volume_left);
4318 len += sprintf(buffer+len, "\tsound.volume_right = %d [0...64]\n",
4319 sound.volume_right);
4320 break;
4321 #endif /* CONFIG_AMIGA */
4323 len += sprintf(buffer+len, "\tsq.block_size = %d sq.max_count = %d"
4324 " sq.max_active = %d\n",
4325 sq.block_size, sq.max_count, sq.max_active);
4326 len += sprintf(buffer+len, "\tsq.count = %d sq.rear_size = %d\n", sq.count,
4327 sq.rear_size);
4328 len += sprintf(buffer+len, "\tsq.playing = %d sq.syncing = %d\n",
4329 sq.playing, sq.syncing);
4330 state.len = len;
4331 return 0;
4335 static int state_release(struct inode *inode, struct file *file)
4337 state.busy = 0;
4338 MOD_DEC_USE_COUNT;
4339 return 0;
4343 static ssize_t state_read(struct file *file, char *buf, size_t count,
4344 loff_t *ppos)
4346 int n = state.len - state.ptr;
4347 if (n > count)
4348 n = count;
4349 if (n <= 0)
4350 return 0;
4351 if (copy_to_user(buf, &state.buf[state.ptr], n))
4352 return -EFAULT;
4353 state.ptr += n;
4354 return n;
4358 static struct file_operations state_fops =
4360 sound_lseek,
4361 state_read,
4362 NULL, /* state_write */
4363 NULL, /* state_readdir */
4364 NULL, /* state_poll */
4365 NULL, /* state_ioctl */
4366 NULL, /* state_mmap */
4367 state_open,
4368 NULL, /* flush */
4369 state_release,
4373 __initfunc(static void state_init(void))
4375 #ifndef MODULE
4376 int state_unit;
4377 #endif
4378 state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
4379 if (state_unit < 0)
4380 return;
4381 state.busy = 0;
4385 /*** Common stuff ********************************************************/
4387 static long long sound_lseek(struct file *file, long long offset, int orig)
4389 return -ESPIPE;
4393 /*** Config & Setup **********************************************************/
4396 __initfunc(void dmasound_init(void))
4398 int has_sound = 0;
4399 int i;
4400 #ifdef CONFIG_PMAC
4401 struct device_node *np;
4402 #endif
4404 #ifdef __mc68000__
4405 switch (m68k_machtype) {
4406 #ifdef CONFIG_ATARI
4407 case MACH_ATARI:
4408 if (ATARIHW_PRESENT(PCM_8BIT)) {
4409 if (ATARIHW_PRESENT(CODEC))
4410 sound.mach = machFalcon;
4411 else if (ATARIHW_PRESENT(MICROWIRE))
4412 sound.mach = machTT;
4413 else
4414 break;
4415 if ((mfp.int_en_a & mfp.int_mk_a & 0x20) == 0)
4416 has_sound = 1;
4417 else
4418 printk("DMA sound driver: Timer A interrupt already in use\n");
4420 break;
4422 #endif /* CONFIG_ATARI */
4423 #ifdef CONFIG_AMIGA
4424 case MACH_AMIGA:
4425 if (AMIGAHW_PRESENT(AMI_AUDIO)) {
4426 sound.mach = machAmiga;
4427 has_sound = 1;
4429 break;
4430 #endif /* CONFIG_AMIGA */
4432 #endif /* __mc68000__ */
4434 #ifdef CONFIG_PMAC
4435 awacs_subframe = 0;
4436 np = find_devices("awacs");
4437 if (np == 0) {
4439 * powermac G3 models have a node called "davbus"
4440 * with a child called "sound".
4442 struct device_node *sound;
4443 np = find_devices("davbus");
4444 sound = find_devices("sound");
4445 if (sound != 0 && sound->parent == np) {
4446 int *sfprop;
4447 sfprop = (int *) get_property(sound, "sub-frame", 0);
4448 if (sfprop != 0 && *sfprop >= 0 && *sfprop < 16)
4449 awacs_subframe = *sfprop;
4452 if (np != NULL && np->n_addrs >= 3 && np->n_intrs >= 3) {
4453 int vol;
4454 sound.mach = machPMac;
4455 has_sound = 1;
4456 awacs = (volatile struct awacs_regs *)
4457 ioremap(np->addrs[0].address, 0x80);
4458 awacs_txdma = (volatile struct dbdma_regs *)
4459 ioremap(np->addrs[1].address, 0x100);
4460 awacs_rxdma = (volatile struct dbdma_regs *)
4461 ioremap(np->addrs[2].address, 0x100);
4462 awacs_irq = np->intrs[0].line;
4463 awacs_tx_irq = np->intrs[1].line;
4464 awacs_rx_irq = np->intrs[2].line;
4465 awacs_tx_cmd_space = kmalloc((numBufs + 4) * sizeof(struct dbdma_cmd),
4466 GFP_KERNEL);
4467 if (awacs_tx_cmd_space == NULL)
4468 goto out_of_memory;
4469 awacs_tx_cmds = (volatile struct dbdma_cmd *)
4470 DBDMA_ALIGN(awacs_tx_cmd_space);
4471 awacs_reg[0] = MASK_MUX_CD;
4472 awacs_reg[1] = MASK_LOOPTHRU | MASK_PAROUT;
4473 /* get default volume from nvram */
4474 vol = (~nvram_read_byte(0x1308) & 7) << 1;
4475 awacs_reg[2] = vol + (vol << 6);
4476 awacs_reg[4] = vol + (vol << 6);
4477 out_le32(&awacs->control, 0x11);
4478 awacs_write(awacs_reg[0] + MASK_ADDR0);
4479 awacs_write(awacs_reg[1] + MASK_ADDR1);
4480 awacs_write(awacs_reg[2] + MASK_ADDR2);
4481 awacs_write(awacs_reg[4] + MASK_ADDR4);
4483 /* Initialize beep stuff */
4484 beep_dbdma_cmd = awacs_tx_cmds + (numBufs + 1);
4485 orig_mksound = kd_mksound;
4486 kd_mksound = awacs_mksound;
4487 beep_buf = (short *) kmalloc(BEEP_BUFLEN * 2, GFP_KERNEL);
4488 if (beep_buf == NULL)
4489 printk(KERN_WARNING "dmasound: no memory for "
4490 "beep buffer\n");
4491 #ifdef CONFIG_PMAC_PBOOK
4492 notifier_chain_register(&sleep_notifier_list,
4493 &awacs_sleep_notifier);
4494 #endif /* CONFIG_PMAC_PBOOK */
4496 #endif /* CONFIG_PMAC */
4498 if (!has_sound)
4499 return;
4501 /* Set up sound queue, /dev/audio and /dev/dsp. */
4502 sound_buffers = kmalloc (numBufs * sizeof(char *), GFP_KERNEL);
4503 if (!sound_buffers) {
4504 out_of_memory:
4505 printk("DMA sound driver: Not enough buffer memory, driver disabled!\n");
4506 return;
4508 for (i = 0; i < numBufs; i++) {
4509 sound_buffers[i] = sound.mach.dma_alloc (bufSize << 10, GFP_KERNEL);
4510 if (!sound_buffers[i]) {
4511 while (i--)
4512 sound.mach.dma_free (sound_buffers[i], bufSize << 10);
4513 kfree (sound_buffers);
4514 sound_buffers = 0;
4515 goto out_of_memory;
4519 sq_setup(numBufs, bufSize << 10, sound_buffers);
4521 /* Set default settings. */
4522 sq_init();
4524 /* Set up /dev/sndstat. */
4525 state_init();
4527 /* Set up /dev/mixer. */
4528 mixer_init();
4530 if (!sound.mach.irqinit()) {
4531 printk("DMA sound driver: Interrupt initialization failed\n");
4532 return;
4534 #ifdef MODULE
4535 irq_installed = 1;
4536 #endif
4538 printk("DMA sound driver installed, using %d buffers of %dk.\n", numBufs,
4539 bufSize);
4541 return;
4545 #define MAXARGS 8 /* Should be sufficient for now */
4547 __initfunc(void dmasound_setup(char *str, int *ints))
4549 /* check the bootstrap parameter for "dmasound=" */
4551 switch (ints[0]) {
4552 case 3:
4553 if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
4554 printk("dmasound_setup: illegal catch radius, using default = %d\n", catchRadius);
4555 else
4556 catchRadius = ints[3];
4557 /* fall through */
4558 case 2:
4559 if (ints[1] < MIN_BUFFERS)
4560 printk("dmasound_setup: illegal number of buffers, using default = %d\n", numBufs);
4561 else
4562 numBufs = ints[1];
4563 if (ints[2] < MIN_BUFSIZE || ints[2] > MAX_BUFSIZE)
4564 printk("dmasound_setup: illegal buffer size, using default = %d\n", bufSize);
4565 else
4566 bufSize = ints[2];
4567 break;
4568 case 0:
4569 break;
4570 default:
4571 printk("dmasound_setup: illegal number of arguments\n");
4576 #ifdef MODULE
4578 int init_module(void)
4580 dmasound_init();
4581 return 0;
4585 void cleanup_module(void)
4587 int i;
4589 if (irq_installed) {
4590 sound_silence();
4591 sound.mach.irqcleanup();
4594 if (sound_buffers) {
4595 for (i = 0; i < numBufs; i++)
4596 sound.mach.dma_free(sound_buffers[i], bufSize << 10);
4597 kfree(sound_buffers);
4600 if (mixer_unit >= 0)
4601 unregister_sound_mixer(mixer_unit);
4602 if (state_unit >= 0)
4603 unregister_sound_special(state_unit);
4604 if (sq_unit >= 0)
4605 unregister_sound_dsp(sq_unit);
4608 #endif /* MODULE */