4 * The DMA buffer manager for digitized voice applications
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
13 * Thomas Sailer : moved several static variables into struct audio_operations
14 * (which is grossly misnamed btw.) because they have the same
15 * lifetime as the rest in there and dynamic allocation saves
17 * Thomas Sailer : remove {in,out}_sleep_flag. It was used for the sleeper to
18 * determine if it was woken up by the expiring timeout or by
19 * an explicit wake_up. The return value from schedule_timeout
20 * can be used instead; if 0, the wakeup was due to the timeout.
22 * Rob Riggs Added persistent DMA buffers (1998/10/17)
25 #define BE_CONSERVATIVE
26 #define SAMPLE_ROUNDUP 0
29 #include <linux/gfp.h>
30 #include "sound_config.h"
32 #define DMAP_FREE_ON_CLOSE 0
33 #define DMAP_KEEP_ON_CLOSE 1
34 extern int sound_dmap_flag;
36 static void dma_reset_output(int dev);
37 static void dma_reset_input(int dev);
38 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode);
42 static int debugmem; /* switched off by default */
43 static int dma_buffsize = DSP_BUFFSIZE;
45 static long dmabuf_timeout(struct dma_buffparms *dmap)
49 tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
50 tmout += HZ / 5; /* Some safety distance */
58 static int sound_alloc_dmap(struct dma_buffparms *dmap)
60 char *start_addr, *end_addr;
65 dmap->mapping_flags &= ~DMA_MAP_MAPPED;
67 if (dmap->raw_buf != NULL)
68 return 0; /* Already done */
69 if (dma_buffsize < 4096)
71 dma_pagesize = (dmap->dma < 4) ? (64 * 1024) : (128 * 1024);
74 * Now check for the Cyrix problem.
77 if(isa_dma_bridge_buggy==2)
81 dmap->buffsize = dma_buffsize;
82 if (dmap->buffsize > dma_pagesize)
83 dmap->buffsize = dma_pagesize;
86 * Now loop until we get a free buffer. Try to get smaller buffer if
87 * it fails. Don't accept smaller than 8k buffer for performance
90 while (start_addr == NULL && dmap->buffsize > PAGE_SIZE) {
91 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
92 dmap->buffsize = PAGE_SIZE * (1 << sz);
93 start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA|__GFP_NOWARN, sz);
94 if (start_addr == NULL)
98 if (start_addr == NULL) {
99 printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
102 /* make some checks */
103 end_addr = start_addr + dmap->buffsize - 1;
106 printk(KERN_DEBUG "sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr);
108 /* now check if it fits into the same dma-pagesize */
110 if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
111 || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
112 printk(KERN_ERR "sound: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, dmap->buffsize);
116 dmap->raw_buf = start_addr;
117 dmap->raw_buf_phys = virt_to_bus(start_addr);
119 for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
120 SetPageReserved(page);
124 static void sound_free_dmap(struct dma_buffparms *dmap)
128 unsigned long start_addr, end_addr;
130 if (dmap->raw_buf == NULL)
132 if (dmap->mapping_flags & DMA_MAP_MAPPED)
133 return; /* Don't free mmapped buffer. Will use it next time */
134 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
136 start_addr = (unsigned long) dmap->raw_buf;
137 end_addr = start_addr + dmap->buffsize;
139 for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
140 ClearPageReserved(page);
142 free_pages((unsigned long) dmap->raw_buf, sz);
143 dmap->raw_buf = NULL;
147 /* Intel version !!!!!!!!! */
149 static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
152 int chan = dmap->dma;
154 /* printk( "Start DMA%d %d, %d\n", chan, (int)(physaddr-dmap->raw_buf_phys), count); */
156 flags = claim_dma_lock();
159 set_dma_mode(chan, dma_mode);
160 set_dma_addr(chan, physaddr);
161 set_dma_count(chan, count);
163 release_dma_lock(flags);
168 static void dma_init_buffers(struct dma_buffparms *dmap)
170 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
171 dmap->byte_counter = 0;
172 dmap->max_byte_counter = 8000 * 60 * 60;
173 dmap->bytes_in_use = dmap->buffsize;
175 dmap->dma_mode = DMODE_NONE;
176 dmap->mapping_flags = 0;
177 dmap->neutral_byte = 0x80;
178 dmap->data_rate = 8000;
182 dmap->flags = DMA_BUSY; /* Other flags off */
185 static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
189 if (dmap->flags & DMA_BUSY)
191 if ((err = sound_alloc_dmap(dmap)) < 0)
194 if (dmap->raw_buf == NULL) {
195 printk(KERN_WARNING "Sound: DMA buffers not available\n");
196 return -ENOSPC; /* Memory allocation failed during boot */
198 if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
199 printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
202 dma_init_buffers(dmap);
203 spin_lock_init(&dmap->lock);
204 dmap->open_mode = mode;
205 dmap->subdivision = dmap->underrun_count = 0;
206 dmap->fragment_size = 0;
207 dmap->max_fragments = 65536; /* Just a large value */
208 dmap->byte_counter = 0;
209 dmap->max_byte_counter = 8000 * 60 * 60;
210 dmap->applic_profile = APF_NORMAL;
211 dmap->needs_reorg = 1;
212 dmap->audio_callback = NULL;
213 dmap->callback_parm = 0;
217 static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
221 if (dmap->dma >= 0) {
222 sound_close_dma(dmap->dma);
223 flags=claim_dma_lock();
224 disable_dma(dmap->dma);
225 release_dma_lock(flags);
227 if (dmap->flags & DMA_BUSY)
228 dmap->dma_mode = DMODE_NONE;
229 dmap->flags &= ~DMA_BUSY;
231 if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
232 sound_free_dmap(dmap);
236 static unsigned int default_set_bits(int dev, unsigned int bits)
238 mm_segment_t fs = get_fs();
241 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
246 static int default_set_speed(int dev, int speed)
248 mm_segment_t fs = get_fs();
251 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
256 static short default_set_channels(int dev, short channels)
259 mm_segment_t fs = get_fs();
262 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
267 static void check_driver(struct audio_driver *d)
269 if (d->set_speed == NULL)
270 d->set_speed = default_set_speed;
271 if (d->set_bits == NULL)
272 d->set_bits = default_set_bits;
273 if (d->set_channels == NULL)
274 d->set_channels = default_set_channels;
277 int DMAbuf_open(int dev, int mode)
279 struct audio_operations *adev = audio_devs[dev];
281 struct dma_buffparms *dmap_in = NULL;
282 struct dma_buffparms *dmap_out = NULL;
286 if (!(adev->flags & DMA_DUPLEX))
287 adev->dmap_in = adev->dmap_out;
288 check_driver(adev->d);
290 if ((retval = adev->d->open(dev, mode)) < 0)
292 dmap_out = adev->dmap_out;
293 dmap_in = adev->dmap_in;
294 if (dmap_in == dmap_out)
295 adev->flags &= ~DMA_DUPLEX;
297 if (mode & OPEN_WRITE) {
298 if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
303 adev->enable_bits = mode;
305 if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
306 if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
308 if (mode & OPEN_WRITE)
309 close_dmap(adev, dmap_out);
313 adev->open_mode = mode;
316 adev->d->set_bits(dev, 8);
317 adev->d->set_channels(dev, 1);
318 adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
319 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
320 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
321 adev->dmap_out->bytes_in_use);
324 /* MUST not hold the spinlock */
325 void DMAbuf_reset(int dev)
327 if (audio_devs[dev]->open_mode & OPEN_WRITE)
328 dma_reset_output(dev);
330 if (audio_devs[dev]->open_mode & OPEN_READ)
331 dma_reset_input(dev);
334 static void dma_reset_output(int dev)
336 struct audio_operations *adev = audio_devs[dev];
337 unsigned long flags,f ;
338 struct dma_buffparms *dmap = adev->dmap_out;
340 if (!(dmap->flags & DMA_STARTED)) /* DMA is not active */
344 * First wait until the current fragment has been played completely
346 spin_lock_irqsave(&dmap->lock,flags);
347 adev->dmap_out->flags |= DMA_SYNCING;
349 adev->dmap_out->underrun_count = 0;
350 if (!signal_pending(current) && adev->dmap_out->qlen &&
351 adev->dmap_out->underrun_count == 0){
352 spin_unlock_irqrestore(&dmap->lock,flags);
353 interruptible_sleep_on_timeout(&adev->out_sleeper,
354 dmabuf_timeout(dmap));
355 spin_lock_irqsave(&dmap->lock,flags);
357 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
360 * Finally shut the device off
362 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_output)
363 adev->d->halt_io(dev);
365 adev->d->halt_output(dev);
366 adev->dmap_out->flags &= ~DMA_STARTED;
369 clear_dma_ff(dmap->dma);
370 disable_dma(dmap->dma);
373 dmap->byte_counter = 0;
374 reorganize_buffers(dev, adev->dmap_out, 0);
375 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
376 spin_unlock_irqrestore(&dmap->lock,flags);
379 static void dma_reset_input(int dev)
381 struct audio_operations *adev = audio_devs[dev];
383 struct dma_buffparms *dmap = adev->dmap_in;
385 spin_lock_irqsave(&dmap->lock,flags);
386 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_input)
387 adev->d->halt_io(dev);
389 adev->d->halt_input(dev);
390 adev->dmap_in->flags &= ~DMA_STARTED;
392 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
393 dmap->byte_counter = 0;
394 reorganize_buffers(dev, adev->dmap_in, 1);
395 spin_unlock_irqrestore(&dmap->lock,flags);
397 /* MUST be called with holding the dmap->lock */
398 void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
400 struct audio_operations *adev = audio_devs[dev];
402 if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
403 return; /* Don't start DMA yet */
404 dmap->dma_mode = DMODE_OUTPUT;
406 if (!(dmap->flags & DMA_ACTIVE) || !(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
407 if (!(dmap->flags & DMA_STARTED)) {
408 reorganize_buffers(dev, dmap, 0);
409 if (adev->d->prepare_for_output(dev, dmap->fragment_size, dmap->nbufs))
411 if (!(dmap->flags & DMA_NODMA))
412 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_WRITE);
413 dmap->flags |= DMA_STARTED;
415 if (dmap->counts[dmap->qhead] == 0)
416 dmap->counts[dmap->qhead] = dmap->fragment_size;
417 dmap->dma_mode = DMODE_OUTPUT;
418 adev->d->output_block(dev, dmap->raw_buf_phys + dmap->qhead * dmap->fragment_size,
419 dmap->counts[dmap->qhead], 1);
420 if (adev->d->trigger)
421 adev->d->trigger(dev,adev->enable_bits * adev->go);
423 dmap->flags |= DMA_ACTIVE;
426 int DMAbuf_sync(int dev)
428 struct audio_operations *adev = audio_devs[dev];
431 struct dma_buffparms *dmap;
433 if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
436 if (adev->dmap_out->dma_mode == DMODE_OUTPUT) {
437 dmap = adev->dmap_out;
438 spin_lock_irqsave(&dmap->lock,flags);
439 if (dmap->qlen > 0 && !(dmap->flags & DMA_ACTIVE))
440 DMAbuf_launch_output(dev, dmap);
441 adev->dmap_out->flags |= DMA_SYNCING;
442 adev->dmap_out->underrun_count = 0;
443 while (!signal_pending(current) && n++ < adev->dmap_out->nbufs &&
444 adev->dmap_out->qlen && adev->dmap_out->underrun_count == 0) {
445 long t = dmabuf_timeout(dmap);
446 spin_unlock_irqrestore(&dmap->lock,flags);
447 /* FIXME: not safe may miss events */
448 t = interruptible_sleep_on_timeout(&adev->out_sleeper, t);
449 spin_lock_irqsave(&dmap->lock,flags);
451 adev->dmap_out->flags &= ~DMA_SYNCING;
452 spin_unlock_irqrestore(&dmap->lock,flags);
453 return adev->dmap_out->qlen;
456 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
459 * Some devices such as GUS have huge amount of on board RAM for the
460 * audio data. We have to wait until the device has finished playing.
463 /* still holding the lock */
464 if (adev->d->local_qlen) { /* Device has hidden buffers */
465 while (!signal_pending(current) &&
466 adev->d->local_qlen(dev)){
467 spin_unlock_irqrestore(&dmap->lock,flags);
468 interruptible_sleep_on_timeout(&adev->out_sleeper,
469 dmabuf_timeout(dmap));
470 spin_lock_irqsave(&dmap->lock,flags);
473 spin_unlock_irqrestore(&dmap->lock,flags);
475 adev->dmap_out->dma_mode = DMODE_NONE;
476 return adev->dmap_out->qlen;
479 int DMAbuf_release(int dev, int mode)
481 struct audio_operations *adev = audio_devs[dev];
482 struct dma_buffparms *dmap;
485 dmap = adev->dmap_out;
486 if (adev->open_mode & OPEN_WRITE)
487 adev->dmap_out->closing = 1;
489 if (adev->open_mode & OPEN_READ){
490 adev->dmap_in->closing = 1;
491 dmap = adev->dmap_in;
493 if (adev->open_mode & OPEN_WRITE)
494 if (!(adev->dmap_out->mapping_flags & DMA_MAP_MAPPED))
495 if (!signal_pending(current) && (adev->dmap_out->dma_mode == DMODE_OUTPUT))
497 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
498 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte, adev->dmap_out->bytes_in_use);
501 spin_lock_irqsave(&dmap->lock,flags);
504 if (adev->open_mode & OPEN_WRITE)
505 close_dmap(adev, adev->dmap_out);
507 if (adev->open_mode == OPEN_READ ||
508 (adev->open_mode != OPEN_WRITE &&
509 (adev->flags & DMA_DUPLEX)))
510 close_dmap(adev, adev->dmap_in);
512 spin_unlock_irqrestore(&dmap->lock,flags);
515 /* called with dmap->lock dold */
516 int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
518 struct audio_operations *adev = audio_devs[dev];
521 if (!(adev->open_mode & OPEN_READ))
523 if (!(adev->enable_bits & PCM_ENABLE_INPUT))
525 if (dmap->dma_mode == DMODE_OUTPUT) { /* Direction change */
526 /* release lock - it's not recursive */
527 spin_unlock_irq(&dmap->lock);
530 spin_lock_irq(&dmap->lock);
531 dmap->dma_mode = DMODE_NONE;
533 if (!dmap->dma_mode) {
534 reorganize_buffers(dev, dmap, 1);
535 if ((err = adev->d->prepare_for_input(dev,
536 dmap->fragment_size, dmap->nbufs)) < 0)
538 dmap->dma_mode = DMODE_INPUT;
540 if (!(dmap->flags & DMA_ACTIVE)) {
541 if (dmap->needs_reorg)
542 reorganize_buffers(dev, dmap, 0);
543 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
544 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
545 dmap->fragment_size, 0);
546 dmap->flags |= DMA_ACTIVE;
547 if (adev->d->trigger)
548 adev->d->trigger(dev, adev->enable_bits * adev->go);
553 int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
555 struct audio_operations *adev = audio_devs[dev];
558 struct dma_buffparms *dmap = adev->dmap_in;
561 if (!(adev->open_mode & OPEN_READ))
563 spin_lock_irqsave(&dmap->lock,flags);
564 if (dmap->needs_reorg)
565 reorganize_buffers(dev, dmap, 0);
566 if (adev->dmap_in->mapping_flags & DMA_MAP_MAPPED) {
567 /* printk(KERN_WARNING "Sound: Can't read from mmapped device (1)\n");*/
568 spin_unlock_irqrestore(&dmap->lock,flags);
570 } else while (dmap->qlen <= 0 && n++ < 10) {
571 long timeout = MAX_SCHEDULE_TIMEOUT;
572 if (!(adev->enable_bits & PCM_ENABLE_INPUT) || !adev->go) {
573 spin_unlock_irqrestore(&dmap->lock,flags);
576 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
577 spin_unlock_irqrestore(&dmap->lock,flags);
580 /* Wait for the next block */
583 spin_unlock_irqrestore(&dmap->lock,flags);
587 timeout = dmabuf_timeout(dmap);
589 spin_unlock_irqrestore(&dmap->lock,flags);
590 timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
593 /* FIXME: include device name */
595 printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
596 dma_reset_input(dev);
599 spin_lock_irqsave(&dmap->lock,flags);
601 spin_unlock_irqrestore(&dmap->lock,flags);
604 return err ? err : -EINTR;
605 *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
606 *len = dmap->fragment_size - dmap->counts[dmap->qhead];
611 int DMAbuf_rmchars(int dev, int buff_no, int c)
613 struct audio_operations *adev = audio_devs[dev];
614 struct dma_buffparms *dmap = adev->dmap_in;
615 int p = dmap->counts[dmap->qhead] + c;
617 if (dmap->mapping_flags & DMA_MAP_MAPPED)
619 /* printk("Sound: Can't read from mmapped device (2)\n");*/
622 else if (dmap->qlen <= 0)
624 else if (p >= dmap->fragment_size) { /* This buffer is completely empty */
625 dmap->counts[dmap->qhead] = 0;
627 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
629 else dmap->counts[dmap->qhead] = p;
633 /* MUST be called with dmap->lock hold */
634 int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
637 * Try to approximate the active byte position of the DMA pointer within the
638 * buffer area as well as possible.
644 if (!(dmap->flags & DMA_ACTIVE))
647 int chan = dmap->dma;
652 if(!isa_dma_bridge_buggy)
653 disable_dma(dmap->dma);
655 pos = get_dma_residue(chan);
657 pos = dmap->bytes_in_use - pos;
659 if (!(dmap->mapping_flags & DMA_MAP_MAPPED)) {
660 if (direction == DMODE_OUTPUT) {
661 if (dmap->qhead == 0)
662 if (pos > dmap->fragment_size)
665 if (dmap->qtail == 0)
666 if (pos > dmap->fragment_size)
672 if (pos >= dmap->bytes_in_use)
675 if(!isa_dma_bridge_buggy)
676 enable_dma(dmap->dma);
680 /* printk( "%04x ", pos); */
686 * DMAbuf_start_devices() is called by the /dev/music driver to start
687 * one or more audio devices at desired moment.
690 void DMAbuf_start_devices(unsigned int devmask)
692 struct audio_operations *adev;
695 for (dev = 0; dev < num_audiodevs; dev++) {
696 if (!(devmask & (1 << dev)))
698 if (!(adev = audio_devs[dev]))
700 if (adev->open_mode == 0)
704 /* OK to start the device */
706 if (adev->d->trigger)
707 adev->d->trigger(dev,adev->enable_bits * adev->go);
710 /* via poll called without a lock ?*/
711 int DMAbuf_space_in_queue(int dev)
713 struct audio_operations *adev = audio_devs[dev];
715 struct dma_buffparms *dmap = adev->dmap_out;
716 int lim = dmap->nbufs;
721 if (dmap->qlen >= lim) /* No space at all */
725 * Verify that there are no more pending buffers than the limit
726 * defined by the process.
729 max = dmap->max_fragments;
734 if (adev->d->local_qlen) {
735 tmp = adev->d->local_qlen(dev);
737 tmp--; /* This buffer has been counted twice */
740 if (dmap->byte_counter % dmap->fragment_size) /* There is a partial fragment */
747 /* MUST not hold the spinlock - this function may sleep */
748 static int output_sleep(int dev, int dontblock)
750 struct audio_operations *adev = audio_devs[dev];
752 struct dma_buffparms *dmap = adev->dmap_out;
758 if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
762 * Wait for free space
764 if (signal_pending(current))
766 timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
768 timeout_value = dmabuf_timeout(dmap);
770 timeout_value = MAX_SCHEDULE_TIMEOUT;
771 timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
773 if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
774 printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
775 dma_reset_output(dev);
777 if (signal_pending(current))
782 /* called with the lock held */
783 static int find_output_space(int dev, char **buf, int *size)
785 struct audio_operations *adev = audio_devs[dev];
786 struct dma_buffparms *dmap = adev->dmap_out;
787 unsigned long active_offs;
790 int occupied_bytes = (dmap->user_counter % dmap->fragment_size);
792 *buf = dmap->raw_buf;
793 if (!(maxfrags = DMAbuf_space_in_queue(dev)) && !occupied_bytes)
796 #ifdef BE_CONSERVATIVE
797 active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
799 active_offs = max(DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT), 0);
800 /* Check for pointer wrapping situation */
801 if (active_offs >= dmap->bytes_in_use)
803 active_offs += dmap->byte_counter;
806 offs = (dmap->user_counter % dmap->bytes_in_use) & ~SAMPLE_ROUNDUP;
807 if (offs < 0 || offs >= dmap->bytes_in_use) {
808 printk(KERN_ERR "Sound: Got unexpected offs %ld. Giving up.\n", offs);
809 printk("Counter = %ld, bytes=%d\n", dmap->user_counter, dmap->bytes_in_use);
812 *buf = dmap->raw_buf + offs;
814 len = active_offs + dmap->bytes_in_use - dmap->user_counter; /* Number of unused bytes in buffer */
816 if ((offs + len) > dmap->bytes_in_use)
817 len = dmap->bytes_in_use - offs;
821 if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
822 len = (maxfrags * dmap->fragment_size) - occupied_bytes;
823 *size = len & ~SAMPLE_ROUNDUP;
827 int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
829 struct audio_operations *adev = audio_devs[dev];
832 struct dma_buffparms *dmap = adev->dmap_out;
834 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
835 /* printk(KERN_DEBUG "Sound: Can't write to mmapped device (3)\n");*/
838 spin_lock_irqsave(&dmap->lock,flags);
839 if (dmap->needs_reorg)
840 reorganize_buffers(dev, dmap, 0);
842 if (dmap->dma_mode == DMODE_INPUT) { /* Direction change */
843 spin_unlock_irqrestore(&dmap->lock,flags);
845 spin_lock_irqsave(&dmap->lock,flags);
847 dmap->dma_mode = DMODE_OUTPUT;
849 while (find_output_space(dev, buf, size) <= 0) {
850 spin_unlock_irqrestore(&dmap->lock,flags);
851 if ((err = output_sleep(dev, dontblock)) < 0) {
854 spin_lock_irqsave(&dmap->lock,flags);
857 spin_unlock_irqrestore(&dmap->lock,flags);
860 /* has to acquire dmap->lock */
861 int DMAbuf_move_wrpointer(int dev, int l)
863 struct audio_operations *adev = audio_devs[dev];
864 struct dma_buffparms *dmap = adev->dmap_out;
866 unsigned long end_ptr, p;
870 spin_lock_irqsave(&dmap->lock,flags);
871 post= (dmap->flags & DMA_POST);
872 ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
874 dmap->flags &= ~DMA_POST;
876 dmap->user_counter += l;
877 dmap->flags |= DMA_DIRTY;
879 if (dmap->byte_counter >= dmap->max_byte_counter) {
880 /* Wrap the byte counters */
881 long decr = dmap->byte_counter;
882 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
883 decr -= dmap->byte_counter;
884 dmap->user_counter -= decr;
886 end_ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
888 p = (dmap->user_counter - 1) % dmap->bytes_in_use;
889 dmap->neutral_byte = dmap->raw_buf[p];
891 /* Update the fragment based bookkeeping too */
892 while (ptr < end_ptr) {
893 dmap->counts[dmap->qtail] = dmap->fragment_size;
894 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
896 ptr += dmap->fragment_size;
899 dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
902 * Let the low level driver perform some postprocessing to
905 if (adev->d->postprocess_write)
906 adev->d->postprocess_write(dev);
908 if (!(dmap->flags & DMA_ACTIVE))
909 if (dmap->qlen > 1 || (dmap->qlen > 0 && (post || dmap->qlen >= dmap->nbufs - 1)))
910 DMAbuf_launch_output(dev, dmap);
912 spin_unlock_irqrestore(&dmap->lock,flags);
916 int DMAbuf_start_dma(int dev, unsigned long physaddr, int count, int dma_mode)
918 struct audio_operations *adev = audio_devs[dev];
919 struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
921 if (dmap->raw_buf == NULL) {
922 printk(KERN_ERR "sound: DMA buffer(1) == NULL\n");
923 printk("Device %d, chn=%s\n", dev, (dmap == adev->dmap_out) ? "out" : "in");
928 sound_start_dma(dmap, physaddr, count, dma_mode);
931 EXPORT_SYMBOL(DMAbuf_start_dma);
933 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode)
935 struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
937 if (dmap->raw_buf == NULL) {
938 printk(KERN_ERR "sound: DMA buffer(2) == NULL\n");
939 printk(KERN_ERR "Device %s, chn=%s\n", adev->name, (dmap == adev->dmap_out) ? "out" : "in");
942 if (dmap->flags & DMA_NODMA)
946 sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
947 dmap->flags |= DMA_STARTED;
951 static void finish_output_interrupt(int dev, struct dma_buffparms *dmap)
953 struct audio_operations *adev = audio_devs[dev];
955 if (dmap->audio_callback != NULL)
956 dmap->audio_callback(dev, dmap->callback_parm);
957 wake_up(&adev->out_sleeper);
958 wake_up(&adev->poll_sleeper);
960 /* called with dmap->lock held in irq context*/
961 static void do_outputintr(int dev, int dummy)
963 struct audio_operations *adev = audio_devs[dev];
964 struct dma_buffparms *dmap = adev->dmap_out;
967 if (dmap->raw_buf == NULL) {
968 printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
971 if (dmap->mapping_flags & DMA_MAP_MAPPED) { /* Virtual memory mapped access */
973 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
974 if (dmap->qhead == 0) { /* Wrapped */
975 dmap->byte_counter += dmap->bytes_in_use;
976 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
977 long decr = dmap->byte_counter;
978 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
979 decr -= dmap->byte_counter;
980 dmap->user_counter -= decr;
983 dmap->qlen++; /* Yes increment it (don't decrement) */
984 if (!(adev->flags & DMA_AUTOMODE))
985 dmap->flags &= ~DMA_ACTIVE;
986 dmap->counts[dmap->qhead] = dmap->fragment_size;
987 DMAbuf_launch_output(dev, dmap);
988 finish_output_interrupt(dev, dmap);
993 this_fragment = dmap->qhead;
994 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
996 if (dmap->qhead == 0) { /* Wrapped */
997 dmap->byte_counter += dmap->bytes_in_use;
998 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
999 long decr = dmap->byte_counter;
1000 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
1001 decr -= dmap->byte_counter;
1002 dmap->user_counter -= decr;
1005 if (!(adev->flags & DMA_AUTOMODE))
1006 dmap->flags &= ~DMA_ACTIVE;
1009 * This is dmap->qlen <= 0 except when closing when
1013 while (dmap->qlen <= -dmap->closing) {
1014 dmap->underrun_count++;
1016 if ((dmap->flags & DMA_DIRTY) && dmap->applic_profile != APF_CPUINTENS) {
1017 dmap->flags &= ~DMA_DIRTY;
1018 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
1019 adev->dmap_out->buffsize);
1021 dmap->user_counter += dmap->fragment_size;
1022 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1025 DMAbuf_launch_output(dev, dmap);
1026 finish_output_interrupt(dev, dmap);
1028 /* called in irq context */
1029 void DMAbuf_outputintr(int dev, int notify_only)
1031 struct audio_operations *adev = audio_devs[dev];
1032 unsigned long flags;
1033 struct dma_buffparms *dmap = adev->dmap_out;
1035 spin_lock_irqsave(&dmap->lock,flags);
1036 if (!(dmap->flags & DMA_NODMA)) {
1037 int chan = dmap->dma, pos, n;
1042 if(!isa_dma_bridge_buggy)
1043 disable_dma(dmap->dma);
1045 pos = dmap->bytes_in_use - get_dma_residue(chan);
1046 if(!isa_dma_bridge_buggy)
1047 enable_dma(dmap->dma);
1048 release_dma_lock(f);
1050 pos = pos / dmap->fragment_size; /* Actual qhead */
1051 if (pos < 0 || pos >= dmap->nbufs)
1054 while (dmap->qhead != pos && n++ < dmap->nbufs)
1055 do_outputintr(dev, notify_only);
1058 do_outputintr(dev, notify_only);
1059 spin_unlock_irqrestore(&dmap->lock,flags);
1061 EXPORT_SYMBOL(DMAbuf_outputintr);
1063 /* called with dmap->lock held in irq context */
1064 static void do_inputintr(int dev)
1066 struct audio_operations *adev = audio_devs[dev];
1067 struct dma_buffparms *dmap = adev->dmap_in;
1069 if (dmap->raw_buf == NULL) {
1070 printk(KERN_ERR "Sound: Fatal error. Audio interrupt after freeing buffers.\n");
1073 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1074 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1075 if (dmap->qtail == 0) { /* Wrapped */
1076 dmap->byte_counter += dmap->bytes_in_use;
1077 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
1078 long decr = dmap->byte_counter;
1079 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1080 decr -= dmap->byte_counter;
1081 dmap->user_counter -= decr;
1086 if (!(adev->flags & DMA_AUTOMODE)) {
1087 if (dmap->needs_reorg)
1088 reorganize_buffers(dev, dmap, 0);
1089 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_READ);
1090 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
1091 dmap->fragment_size, 1);
1092 if (adev->d->trigger)
1093 adev->d->trigger(dev, adev->enable_bits * adev->go);
1095 dmap->flags |= DMA_ACTIVE;
1096 } else if (dmap->qlen >= (dmap->nbufs - 1)) {
1097 printk(KERN_WARNING "Sound: Recording overrun\n");
1098 dmap->underrun_count++;
1100 /* Just throw away the oldest fragment but keep the engine running */
1101 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1102 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1103 } else if (dmap->qlen >= 0 && dmap->qlen < dmap->nbufs) {
1105 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1106 if (dmap->qtail == 0) { /* Wrapped */
1107 dmap->byte_counter += dmap->bytes_in_use;
1108 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
1109 long decr = dmap->byte_counter;
1110 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1111 decr -= dmap->byte_counter;
1112 dmap->user_counter -= decr;
1116 if (!(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
1117 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
1118 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size, dmap->fragment_size, 1);
1119 if (adev->d->trigger)
1120 adev->d->trigger(dev,adev->enable_bits * adev->go);
1122 dmap->flags |= DMA_ACTIVE;
1125 wake_up(&adev->in_sleeper);
1126 wake_up(&adev->poll_sleeper);
1129 /* called in irq context */
1130 void DMAbuf_inputintr(int dev)
1132 struct audio_operations *adev = audio_devs[dev];
1133 struct dma_buffparms *dmap = adev->dmap_in;
1134 unsigned long flags;
1136 spin_lock_irqsave(&dmap->lock,flags);
1138 if (!(dmap->flags & DMA_NODMA)) {
1139 int chan = dmap->dma, pos, n;
1143 if(!isa_dma_bridge_buggy)
1144 disable_dma(dmap->dma);
1146 pos = dmap->bytes_in_use - get_dma_residue(chan);
1147 if(!isa_dma_bridge_buggy)
1148 enable_dma(dmap->dma);
1149 release_dma_lock(f);
1151 pos = pos / dmap->fragment_size; /* Actual qhead */
1152 if (pos < 0 || pos >= dmap->nbufs)
1156 while (dmap->qtail != pos && ++n < dmap->nbufs)
1160 spin_unlock_irqrestore(&dmap->lock,flags);
1162 EXPORT_SYMBOL(DMAbuf_inputintr);
1164 void DMAbuf_init(int dev, int dma1, int dma2)
1166 struct audio_operations *adev = audio_devs[dev];
1168 * NOTE! This routine could be called several times.
1171 if (adev && adev->dmap_out == NULL) {
1172 if (adev->d == NULL)
1173 panic("OSS: audio_devs[%d]->d == NULL\n", dev);
1175 if (adev->parent_dev) { /* Use DMA map of the parent dev */
1176 int parent = adev->parent_dev - 1;
1177 adev->dmap_out = audio_devs[parent]->dmap_out;
1178 adev->dmap_in = audio_devs[parent]->dmap_in;
1180 adev->dmap_out = adev->dmap_in = &adev->dmaps[0];
1181 adev->dmap_out->dma = dma1;
1182 if (adev->flags & DMA_DUPLEX) {
1183 adev->dmap_in = &adev->dmaps[1];
1184 adev->dmap_in->dma = dma2;
1187 /* Persistent DMA buffers allocated here */
1188 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1189 if (adev->dmap_in->raw_buf == NULL)
1190 sound_alloc_dmap(adev->dmap_in);
1191 if (adev->dmap_out->raw_buf == NULL)
1192 sound_alloc_dmap(adev->dmap_out);
1197 /* No kernel lock - DMAbuf_activate_recording protected by global cli/sti */
1198 static unsigned int poll_input(struct file * file, int dev, poll_table *wait)
1200 struct audio_operations *adev = audio_devs[dev];
1201 struct dma_buffparms *dmap = adev->dmap_in;
1203 if (!(adev->open_mode & OPEN_READ))
1205 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1207 return POLLIN | POLLRDNORM;
1210 if (dmap->dma_mode != DMODE_INPUT) {
1211 if (dmap->dma_mode == DMODE_NONE &&
1212 adev->enable_bits & PCM_ENABLE_INPUT &&
1213 !dmap->qlen && adev->go) {
1214 unsigned long flags;
1216 spin_lock_irqsave(&dmap->lock,flags);
1217 DMAbuf_activate_recording(dev, dmap);
1218 spin_unlock_irqrestore(&dmap->lock,flags);
1224 return POLLIN | POLLRDNORM;
1227 static unsigned int poll_output(struct file * file, int dev, poll_table *wait)
1229 struct audio_operations *adev = audio_devs[dev];
1230 struct dma_buffparms *dmap = adev->dmap_out;
1232 if (!(adev->open_mode & OPEN_WRITE))
1234 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1236 return POLLOUT | POLLWRNORM;
1239 if (dmap->dma_mode == DMODE_INPUT)
1241 if (dmap->dma_mode == DMODE_NONE)
1242 return POLLOUT | POLLWRNORM;
1243 if (!DMAbuf_space_in_queue(dev))
1245 return POLLOUT | POLLWRNORM;
1248 unsigned int DMAbuf_poll(struct file * file, int dev, poll_table *wait)
1250 struct audio_operations *adev = audio_devs[dev];
1251 poll_wait(file, &adev->poll_sleeper, wait);
1252 return poll_input(file, dev, wait) | poll_output(file, dev, wait);
1255 void DMAbuf_deinit(int dev)
1257 struct audio_operations *adev = audio_devs[dev];
1258 /* This routine is called when driver is being unloaded */
1262 /* Persistent DMA buffers deallocated here */
1263 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1264 sound_free_dmap(adev->dmap_out);
1265 if (adev->flags & DMA_DUPLEX)
1266 sound_free_dmap(adev->dmap_in);