- Peter Anvin: more P4 configuration parsing
[davej-history.git] / drivers / sound / dmasound / dmasound_paula.c
blobd76c079371d23fad509fd26626a75f3b0d3a2339
2 /*
3 * linux/drivers/sound/dmasound/dmasound_paula.c
5 * Amiga `Paula' DMA Sound Driver
7 * See linux/drivers/sound/dmasound/dmasound_core.c for copyright and credits
8 */
11 #include <linux/module.h>
12 #include <linux/config.h>
13 #include <linux/mm.h>
14 #include <linux/init.h>
15 #include <linux/ioport.h>
16 #include <linux/soundcard.h>
18 #include <asm/uaccess.h>
19 #include <asm/setup.h>
20 #include <asm/amigahw.h>
21 #include <asm/amigaints.h>
22 #include <asm/machdep.h>
24 #include "dmasound.h"
28 * The minimum period for audio depends on htotal (for OCS/ECS/AGA)
29 * (Imported from arch/m68k/amiga/amisound.c)
32 extern volatile u_short amiga_audio_min_period;
36 * amiga_mksound() should be able to restore the period after beeping
37 * (Imported from arch/m68k/amiga/amisound.c)
40 extern u_short amiga_audio_period;
44 * Audio DMA masks
47 #define AMI_AUDIO_OFF (DMAF_AUD0 | DMAF_AUD1 | DMAF_AUD2 | DMAF_AUD3)
48 #define AMI_AUDIO_8 (DMAF_SETCLR | DMAF_MASTER | DMAF_AUD0 | DMAF_AUD1)
49 #define AMI_AUDIO_14 (AMI_AUDIO_8 | DMAF_AUD2 | DMAF_AUD3)
53 * Helper pointers for 16(14)-bit sound
56 static int write_sq_block_size_half, write_sq_block_size_quarter;
59 /*** Low level stuff *********************************************************/
62 static void AmiOpen(void);
63 static void AmiRelease(void);
64 static void *AmiAlloc(unsigned int size, int flags);
65 static void AmiFree(void *obj, unsigned int size);
66 static int AmiIrqInit(void);
67 #ifdef MODULE
68 static void AmiIrqCleanUp(void);
69 #endif
70 static void AmiSilence(void);
71 static void AmiInit(void);
72 static int AmiSetFormat(int format);
73 static int AmiSetVolume(int volume);
74 static int AmiSetTreble(int treble);
75 static void AmiPlayNextFrame(int index);
76 static void AmiPlay(void);
77 static void AmiInterrupt(int irq, void *dummy, struct pt_regs *fp);
79 #ifdef CONFIG_HEARTBEAT
82 * Heartbeat interferes with sound since the 7 kHz low-pass filter and the
83 * power LED are controlled by the same line.
86 #ifdef CONFIG_APUS
87 #define mach_heartbeat ppc_md.heartbeat
88 #endif
90 static void (*saved_heartbeat)(int) = NULL;
92 static inline void disable_heartbeat(void)
94 if (mach_heartbeat) {
95 saved_heartbeat = mach_heartbeat;
96 mach_heartbeat = NULL;
98 AmiSetTreble(dmasound.treble);
101 static inline void enable_heartbeat(void)
103 if (saved_heartbeat)
104 mach_heartbeat = saved_heartbeat;
106 #else /* !CONFIG_HEARTBEAT */
107 #define disable_heartbeat() do { } while (0)
108 #define enable_heartbeat() do { } while (0)
109 #endif /* !CONFIG_HEARTBEAT */
112 /*** Mid level stuff *********************************************************/
114 static void AmiMixerInit(void);
115 static int AmiMixerIoctl(u_int cmd, u_long arg);
116 static void AmiWriteSqSetup(void);
117 static int AmiStateInfo(char *buffer);
120 /*** Translations ************************************************************/
122 /* ++TeSche: radically changed for new expanding purposes...
124 * These two routines now deal with copying/expanding/translating the samples
125 * from user space into our buffer at the right frequency. They take care about
126 * how much data there's actually to read, how much buffer space there is and
127 * to convert samples into the right frequency/encoding. They will only work on
128 * complete samples so it may happen they leave some bytes in the input stream
129 * if the user didn't write a multiple of the current sample size. They both
130 * return the number of bytes they've used from both streams so you may detect
131 * such a situation. Luckily all programs should be able to cope with that.
133 * I think I've optimized anything as far as one can do in plain C, all
134 * variables should fit in registers and the loops are really short. There's
135 * one loop for every possible situation. Writing a more generalized and thus
136 * parameterized loop would only produce slower code. Feel free to optimize
137 * this in assembler if you like. :)
139 * I think these routines belong here because they're not yet really hardware
140 * independent, especially the fact that the Falcon can play 16bit samples
141 * only in stereo is hardcoded in both of them!
143 * ++geert: split in even more functions (one per format)
148 * Native format
151 static ssize_t ami_ct_s8(const u_char *userPtr, size_t userCount,
152 u_char frame[], ssize_t *frameUsed, ssize_t frameLeft)
154 ssize_t count, used;
156 if (!dmasound.soft.stereo) {
157 void *p = &frame[*frameUsed];
158 count = min(userCount, frameLeft) & ~1;
159 used = count;
160 if (copy_from_user(p, userPtr, count))
161 return -EFAULT;
162 } else {
163 u_char *left = &frame[*frameUsed>>1];
164 u_char *right = left+write_sq_block_size_half;
165 count = min(userCount, frameLeft)>>1 & ~1;
166 used = count*2;
167 while (count > 0) {
168 if (get_user(*left++, userPtr++)
169 || get_user(*right++, userPtr++))
170 return -EFAULT;
171 count--;
174 *frameUsed += used;
175 return used;
180 * Copy and convert 8 bit data
183 #define GENERATE_AMI_CT8(funcname, convsample) \
184 static ssize_t funcname(const u_char *userPtr, size_t userCount, \
185 u_char frame[], ssize_t *frameUsed, \
186 ssize_t frameLeft) \
188 ssize_t count, used; \
190 if (!dmasound.soft.stereo) { \
191 u_char *p = &frame[*frameUsed]; \
192 count = min(userCount, frameLeft) & ~1; \
193 used = count; \
194 while (count > 0) { \
195 u_char data; \
196 if (get_user(data, userPtr++)) \
197 return -EFAULT; \
198 *p++ = convsample(data); \
199 count--; \
201 } else { \
202 u_char *left = &frame[*frameUsed>>1]; \
203 u_char *right = left+write_sq_block_size_half; \
204 count = min(userCount, frameLeft)>>1 & ~1; \
205 used = count*2; \
206 while (count > 0) { \
207 u_char data; \
208 if (get_user(data, userPtr++)) \
209 return -EFAULT; \
210 *left++ = convsample(data); \
211 if (get_user(data, userPtr++)) \
212 return -EFAULT; \
213 *right++ = convsample(data); \
214 count--; \
217 *frameUsed += used; \
218 return used; \
221 #define AMI_CT_ULAW(x) (dmasound_ulaw2dma8[(x)])
222 #define AMI_CT_ALAW(x) (dmasound_alaw2dma8[(x)])
223 #define AMI_CT_U8(x) ((x) ^ 0x80)
225 GENERATE_AMI_CT8(ami_ct_ulaw, AMI_CT_ULAW)
226 GENERATE_AMI_CT8(ami_ct_alaw, AMI_CT_ALAW)
227 GENERATE_AMI_CT8(ami_ct_u8, AMI_CT_U8)
231 * Copy and convert 16 bit data
234 #define GENERATE_AMI_CT_16(funcname, convsample) \
235 static ssize_t funcname(const u_char *userPtr, size_t userCount, \
236 u_char frame[], ssize_t *frameUsed, \
237 ssize_t frameLeft) \
239 ssize_t count, used; \
240 u_short data; \
242 if (!dmasound.soft.stereo) { \
243 u_char *high = &frame[*frameUsed>>1]; \
244 u_char *low = high+write_sq_block_size_half; \
245 count = min(userCount, frameLeft)>>1 & ~1; \
246 used = count*2; \
247 while (count > 0) { \
248 if (get_user(data, ((u_short *)userPtr)++)) \
249 return -EFAULT; \
250 data = convsample(data); \
251 *high++ = data>>8; \
252 *low++ = (data>>2) & 0x3f; \
253 count--; \
255 } else { \
256 u_char *lefth = &frame[*frameUsed>>2]; \
257 u_char *leftl = lefth+write_sq_block_size_quarter; \
258 u_char *righth = lefth+write_sq_block_size_half; \
259 u_char *rightl = righth+write_sq_block_size_quarter; \
260 count = min(userCount, frameLeft)>>2 & ~1; \
261 used = count*4; \
262 while (count > 0) { \
263 if (get_user(data, ((u_short *)userPtr)++)) \
264 return -EFAULT; \
265 data = convsample(data); \
266 *lefth++ = data>>8; \
267 *leftl++ = (data>>2) & 0x3f; \
268 if (get_user(data, ((u_short *)userPtr)++)) \
269 return -EFAULT; \
270 data = convsample(data); \
271 *righth++ = data>>8; \
272 *rightl++ = (data>>2) & 0x3f; \
273 count--; \
276 *frameUsed += used; \
277 return used; \
280 #define AMI_CT_S16BE(x) (x)
281 #define AMI_CT_U16BE(x) ((x) ^ 0x8000)
282 #define AMI_CT_S16LE(x) (le2be16((x)))
283 #define AMI_CT_U16LE(x) (le2be16((x)) ^ 0x8000)
285 GENERATE_AMI_CT_16(ami_ct_s16be, AMI_CT_S16BE)
286 GENERATE_AMI_CT_16(ami_ct_u16be, AMI_CT_U16BE)
287 GENERATE_AMI_CT_16(ami_ct_s16le, AMI_CT_S16LE)
288 GENERATE_AMI_CT_16(ami_ct_u16le, AMI_CT_U16LE)
291 static TRANS transAmiga = {
292 ct_ulaw: ami_ct_ulaw,
293 ct_alaw: ami_ct_alaw,
294 ct_s8: ami_ct_s8,
295 ct_u8: ami_ct_u8,
296 ct_s16be: ami_ct_s16be,
297 ct_u16be: ami_ct_u16be,
298 ct_s16le: ami_ct_s16le,
299 ct_u16le: ami_ct_u16le,
302 /*** Low level stuff *********************************************************/
305 static void AmiOpen(void)
307 MOD_INC_USE_COUNT;
310 static void AmiRelease(void)
312 MOD_DEC_USE_COUNT;
315 static inline void StopDMA(void)
317 custom.aud[0].audvol = custom.aud[1].audvol = 0;
318 custom.aud[2].audvol = custom.aud[3].audvol = 0;
319 custom.dmacon = AMI_AUDIO_OFF;
320 enable_heartbeat();
323 static void *AmiAlloc(unsigned int size, int flags)
325 return amiga_chip_alloc((long)size, "dmasound [Paula]");
328 static void AmiFree(void *obj, unsigned int size)
330 amiga_chip_free (obj);
333 static int __init AmiIrqInit(void)
335 /* turn off DMA for audio channels */
336 StopDMA();
338 /* Register interrupt handler. */
339 if (request_irq(IRQ_AMIGA_AUD0, AmiInterrupt, 0, "DMA sound",
340 AmiInterrupt))
341 return 0;
342 return 1;
345 #ifdef MODULE
346 static void AmiIrqCleanUp(void)
348 /* turn off DMA for audio channels */
349 StopDMA();
350 /* release the interrupt */
351 free_irq(IRQ_AMIGA_AUD0, AmiInterrupt);
353 #endif /* MODULE */
355 static void AmiSilence(void)
357 /* turn off DMA for audio channels */
358 StopDMA();
362 static void AmiInit(void)
364 int period, i;
366 AmiSilence();
368 if (dmasound.soft.speed)
369 period = amiga_colorclock/dmasound.soft.speed-1;
370 else
371 period = amiga_audio_min_period;
372 dmasound.hard = dmasound.soft;
373 dmasound.trans_write = &transAmiga;
375 if (period < amiga_audio_min_period) {
376 /* we would need to squeeze the sound, but we won't do that */
377 period = amiga_audio_min_period;
378 } else if (period > 65535) {
379 period = 65535;
381 dmasound.hard.speed = amiga_colorclock/(period+1);
383 for (i = 0; i < 4; i++)
384 custom.aud[i].audper = period;
385 amiga_audio_period = period;
389 static int AmiSetFormat(int format)
391 int size;
393 /* Amiga sound DMA supports 8bit and 16bit (pseudo 14 bit) modes */
395 switch (format) {
396 case AFMT_QUERY:
397 return dmasound.soft.format;
398 case AFMT_MU_LAW:
399 case AFMT_A_LAW:
400 case AFMT_U8:
401 case AFMT_S8:
402 size = 8;
403 break;
404 case AFMT_S16_BE:
405 case AFMT_U16_BE:
406 case AFMT_S16_LE:
407 case AFMT_U16_LE:
408 size = 16;
409 break;
410 default: /* :-) */
411 size = 8;
412 format = AFMT_S8;
415 dmasound.soft.format = format;
416 dmasound.soft.size = size;
417 if (dmasound.minDev == SND_DEV_DSP) {
418 dmasound.dsp.format = format;
419 dmasound.dsp.size = dmasound.soft.size;
421 AmiInit();
423 return format;
427 #define VOLUME_VOXWARE_TO_AMI(v) \
428 (((v) < 0) ? 0 : ((v) > 100) ? 64 : ((v) * 64)/100)
429 #define VOLUME_AMI_TO_VOXWARE(v) ((v)*100/64)
431 static int AmiSetVolume(int volume)
433 dmasound.volume_left = VOLUME_VOXWARE_TO_AMI(volume & 0xff);
434 custom.aud[0].audvol = dmasound.volume_left;
435 dmasound.volume_right = VOLUME_VOXWARE_TO_AMI((volume & 0xff00) >> 8);
436 custom.aud[1].audvol = dmasound.volume_right;
437 if (dmasound.hard.size == 16) {
438 if (dmasound.volume_left == 64 && dmasound.volume_right == 64) {
439 custom.aud[2].audvol = 1;
440 custom.aud[3].audvol = 1;
441 } else {
442 custom.aud[2].audvol = 0;
443 custom.aud[3].audvol = 0;
446 return VOLUME_AMI_TO_VOXWARE(dmasound.volume_left) |
447 (VOLUME_AMI_TO_VOXWARE(dmasound.volume_right) << 8);
450 static int AmiSetTreble(int treble)
452 dmasound.treble = treble;
453 if (treble < 50)
454 ciaa.pra &= ~0x02;
455 else
456 ciaa.pra |= 0x02;
457 return treble;
461 #define AMI_PLAY_LOADED 1
462 #define AMI_PLAY_PLAYING 2
463 #define AMI_PLAY_MASK 3
466 static void AmiPlayNextFrame(int index)
468 u_char *start, *ch0, *ch1, *ch2, *ch3;
469 u_long size;
471 /* used by AmiPlay() if all doubts whether there really is something
472 * to be played are already wiped out.
474 start = write_sq.buffers[write_sq.front];
475 size = (write_sq.count == index ? write_sq.rear_size
476 : write_sq.block_size)>>1;
478 if (dmasound.hard.stereo) {
479 ch0 = start;
480 ch1 = start+write_sq_block_size_half;
481 size >>= 1;
482 } else {
483 ch0 = start;
484 ch1 = start;
487 disable_heartbeat();
488 custom.aud[0].audvol = dmasound.volume_left;
489 custom.aud[1].audvol = dmasound.volume_right;
490 if (dmasound.hard.size == 8) {
491 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
492 custom.aud[0].audlen = size;
493 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
494 custom.aud[1].audlen = size;
495 custom.dmacon = AMI_AUDIO_8;
496 } else {
497 size >>= 1;
498 custom.aud[0].audlc = (u_short *)ZTWO_PADDR(ch0);
499 custom.aud[0].audlen = size;
500 custom.aud[1].audlc = (u_short *)ZTWO_PADDR(ch1);
501 custom.aud[1].audlen = size;
502 if (dmasound.volume_left == 64 && dmasound.volume_right == 64) {
503 /* We can play pseudo 14-bit only with the maximum volume */
504 ch3 = ch0+write_sq_block_size_quarter;
505 ch2 = ch1+write_sq_block_size_quarter;
506 custom.aud[2].audvol = 1; /* we are being affected by the beeps */
507 custom.aud[3].audvol = 1; /* restoring volume here helps a bit */
508 custom.aud[2].audlc = (u_short *)ZTWO_PADDR(ch2);
509 custom.aud[2].audlen = size;
510 custom.aud[3].audlc = (u_short *)ZTWO_PADDR(ch3);
511 custom.aud[3].audlen = size;
512 custom.dmacon = AMI_AUDIO_14;
513 } else {
514 custom.aud[2].audvol = 0;
515 custom.aud[3].audvol = 0;
516 custom.dmacon = AMI_AUDIO_8;
519 write_sq.front = (write_sq.front+1) % write_sq.max_count;
520 write_sq.active |= AMI_PLAY_LOADED;
524 static void AmiPlay(void)
526 int minframes = 1;
528 custom.intena = IF_AUD0;
530 if (write_sq.active & AMI_PLAY_LOADED) {
531 /* There's already a frame loaded */
532 custom.intena = IF_SETCLR | IF_AUD0;
533 return;
536 if (write_sq.active & AMI_PLAY_PLAYING)
537 /* Increase threshold: frame 1 is already being played */
538 minframes = 2;
540 if (write_sq.count < minframes) {
541 /* Nothing to do */
542 custom.intena = IF_SETCLR | IF_AUD0;
543 return;
546 if (write_sq.count <= minframes &&
547 write_sq.rear_size < write_sq.block_size && !write_sq.syncing) {
548 /* hmmm, the only existing frame is not
549 * yet filled and we're not syncing?
551 custom.intena = IF_SETCLR | IF_AUD0;
552 return;
555 AmiPlayNextFrame(minframes);
557 custom.intena = IF_SETCLR | IF_AUD0;
561 static void AmiInterrupt(int irq, void *dummy, struct pt_regs *fp)
563 int minframes = 1;
565 custom.intena = IF_AUD0;
567 if (!write_sq.active) {
568 /* Playing was interrupted and sq_reset() has already cleared
569 * the sq variables, so better don't do anything here.
571 WAKE_UP(write_sq.sync_queue);
572 return;
575 if (write_sq.active & AMI_PLAY_PLAYING) {
576 /* We've just finished a frame */
577 write_sq.count--;
578 WAKE_UP(write_sq.action_queue);
581 if (write_sq.active & AMI_PLAY_LOADED)
582 /* Increase threshold: frame 1 is already being played */
583 minframes = 2;
585 /* Shift the flags */
586 write_sq.active = (write_sq.active<<1) & AMI_PLAY_MASK;
588 if (!write_sq.active)
589 /* No frame is playing, disable audio DMA */
590 StopDMA();
592 custom.intena = IF_SETCLR | IF_AUD0;
594 if (write_sq.count >= minframes)
595 /* Try to play the next frame */
596 AmiPlay();
598 if (!write_sq.active)
599 /* Nothing to play anymore.
600 Wake up a process waiting for audio output to drain. */
601 WAKE_UP(write_sq.sync_queue);
604 /*** Mid level stuff *********************************************************/
608 * /dev/mixer abstraction
611 static void __init AmiMixerInit(void)
613 dmasound.volume_left = 64;
614 dmasound.volume_right = 64;
615 custom.aud[0].audvol = dmasound.volume_left;
616 custom.aud[3].audvol = 1; /* For pseudo 14bit */
617 custom.aud[1].audvol = dmasound.volume_right;
618 custom.aud[2].audvol = 1; /* For pseudo 14bit */
619 dmasound.treble = 50;
622 static int AmiMixerIoctl(u_int cmd, u_long arg)
624 int data;
625 switch (cmd) {
626 case SOUND_MIXER_READ_DEVMASK:
627 return IOCTL_OUT(arg, SOUND_MASK_VOLUME | SOUND_MASK_TREBLE);
628 case SOUND_MIXER_READ_RECMASK:
629 return IOCTL_OUT(arg, 0);
630 case SOUND_MIXER_READ_STEREODEVS:
631 return IOCTL_OUT(arg, SOUND_MASK_VOLUME);
632 case SOUND_MIXER_READ_VOLUME:
633 return IOCTL_OUT(arg,
634 VOLUME_AMI_TO_VOXWARE(dmasound.volume_left) |
635 VOLUME_AMI_TO_VOXWARE(dmasound.volume_right) << 8);
636 case SOUND_MIXER_WRITE_VOLUME:
637 IOCTL_IN(arg, data);
638 return IOCTL_OUT(arg, dmasound_set_volume(data));
639 case SOUND_MIXER_READ_TREBLE:
640 return IOCTL_OUT(arg, dmasound.treble);
641 case SOUND_MIXER_WRITE_TREBLE:
642 IOCTL_IN(arg, data);
643 return IOCTL_OUT(arg, dmasound_set_treble(data));
645 return -EINVAL;
649 static void AmiWriteSqSetup(void)
651 write_sq_block_size_half = write_sq.block_size>>1;
652 write_sq_block_size_quarter = write_sq_block_size_half>>1;
656 static int AmiStateInfo(char *buffer)
658 int len = 0;
659 len += sprintf(buffer+len, "\tsound.volume_left = %d [0...64]\n",
660 dmasound.volume_left);
661 len += sprintf(buffer+len, "\tsound.volume_right = %d [0...64]\n",
662 dmasound.volume_right);
663 return len;
667 /*** Machine definitions *****************************************************/
670 static MACHINE machAmiga = {
671 name: "Amiga",
672 name2: "AMIGA",
673 open: AmiOpen,
674 release: AmiRelease,
675 dma_alloc: AmiAlloc,
676 dma_free: AmiFree,
677 irqinit: AmiIrqInit,
678 #ifdef MODULE
679 irqcleanup: AmiIrqCleanUp,
680 #endif /* MODULE */
681 init: AmiInit,
682 silence: AmiSilence,
683 setFormat: AmiSetFormat,
684 setVolume: AmiSetVolume,
685 setTreble: AmiSetTreble,
686 play: AmiPlay,
687 mixer_init: AmiMixerInit,
688 mixer_ioctl: AmiMixerIoctl,
689 write_sq_setup: AmiWriteSqSetup,
690 state_info: AmiStateInfo,
691 min_dsp_speed: 8000
695 /*** Config & Setup **********************************************************/
698 int __init dmasound_paula_init(void)
700 int err;
702 if (MACH_IS_AMIGA && AMIGAHW_PRESENT(AMI_AUDIO)) {
703 if (!request_mem_region(CUSTOM_PHYSADDR+0xa0, 0x40,
704 "dmasound [Paula]"))
705 return -EBUSY;
706 dmasound.mach = machAmiga;
707 err = dmasound_init();
708 if (err)
709 release_mem_region(CUSTOM_PHYSADDR+0xa0, 0x40);
710 return err;
711 } else
712 return -ENODEV;
715 static void __exit dmasound_paula_cleanup(void)
717 dmasound_deinit();
718 release_mem_region(CUSTOM_PHYSADDR+0xa0, 0x40);
721 module_init(dmasound_paula_init);
722 module_exit(dmasound_paula_cleanup);