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 "sound_config.h"
31 #define DMAP_FREE_ON_CLOSE 0
32 #define DMAP_KEEP_ON_CLOSE 1
33 extern int sound_dmap_flag;
35 static void dma_reset_output(int dev);
36 static void dma_reset_input(int dev);
37 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode);
41 static int debugmem; /* switched off by default */
42 static int dma_buffsize = DSP_BUFFSIZE;
44 static long dmabuf_timeout(struct dma_buffparms *dmap)
48 tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
49 tmout += HZ / 5; /* Some safety distance */
57 static int sound_alloc_dmap(struct dma_buffparms *dmap)
59 char *start_addr, *end_addr;
64 dmap->mapping_flags &= ~DMA_MAP_MAPPED;
66 if (dmap->raw_buf != NULL)
67 return 0; /* Already done */
68 if (dma_buffsize < 4096)
70 dma_pagesize = (dmap->dma < 4) ? (64 * 1024) : (128 * 1024);
73 * Now check for the Cyrix problem.
76 if(isa_dma_bridge_buggy==2)
80 dmap->buffsize = dma_buffsize;
81 if (dmap->buffsize > dma_pagesize)
82 dmap->buffsize = dma_pagesize;
85 * Now loop until we get a free buffer. Try to get smaller buffer if
86 * it fails. Don't accept smaller than 8k buffer for performance
89 while (start_addr == NULL && dmap->buffsize > PAGE_SIZE) {
90 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
91 dmap->buffsize = PAGE_SIZE * (1 << sz);
92 start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA|__GFP_NOWARN, sz);
93 if (start_addr == NULL)
97 if (start_addr == NULL) {
98 printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
101 /* make some checks */
102 end_addr = start_addr + dmap->buffsize - 1;
105 printk(KERN_DEBUG "sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr);
107 /* now check if it fits into the same dma-pagesize */
109 if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
110 || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
111 printk(KERN_ERR "sound: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, dmap->buffsize);
115 dmap->raw_buf = start_addr;
116 dmap->raw_buf_phys = virt_to_bus(start_addr);
118 for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
119 SetPageReserved(page);
123 static void sound_free_dmap(struct dma_buffparms *dmap)
127 unsigned long start_addr, end_addr;
129 if (dmap->raw_buf == NULL)
131 if (dmap->mapping_flags & DMA_MAP_MAPPED)
132 return; /* Don't free mmapped buffer. Will use it next time */
133 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
135 start_addr = (unsigned long) dmap->raw_buf;
136 end_addr = start_addr + dmap->buffsize;
138 for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
139 ClearPageReserved(page);
141 free_pages((unsigned long) dmap->raw_buf, sz);
142 dmap->raw_buf = NULL;
146 /* Intel version !!!!!!!!! */
148 static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
151 int chan = dmap->dma;
153 /* printk( "Start DMA%d %d, %d\n", chan, (int)(physaddr-dmap->raw_buf_phys), count); */
155 flags = claim_dma_lock();
158 set_dma_mode(chan, dma_mode);
159 set_dma_addr(chan, physaddr);
160 set_dma_count(chan, count);
162 release_dma_lock(flags);
167 static void dma_init_buffers(struct dma_buffparms *dmap)
169 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
170 dmap->byte_counter = 0;
171 dmap->max_byte_counter = 8000 * 60 * 60;
172 dmap->bytes_in_use = dmap->buffsize;
174 dmap->dma_mode = DMODE_NONE;
175 dmap->mapping_flags = 0;
176 dmap->neutral_byte = 0x80;
177 dmap->data_rate = 8000;
181 dmap->flags = DMA_BUSY; /* Other flags off */
184 static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
188 if (dmap->flags & DMA_BUSY)
190 if ((err = sound_alloc_dmap(dmap)) < 0)
193 if (dmap->raw_buf == NULL) {
194 printk(KERN_WARNING "Sound: DMA buffers not available\n");
195 return -ENOSPC; /* Memory allocation failed during boot */
197 if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
198 printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
201 dma_init_buffers(dmap);
202 spin_lock_init(&dmap->lock);
203 dmap->open_mode = mode;
204 dmap->subdivision = dmap->underrun_count = 0;
205 dmap->fragment_size = 0;
206 dmap->max_fragments = 65536; /* Just a large value */
207 dmap->byte_counter = 0;
208 dmap->max_byte_counter = 8000 * 60 * 60;
209 dmap->applic_profile = APF_NORMAL;
210 dmap->needs_reorg = 1;
211 dmap->audio_callback = NULL;
212 dmap->callback_parm = 0;
216 static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
220 if (dmap->dma >= 0) {
221 sound_close_dma(dmap->dma);
222 flags=claim_dma_lock();
223 disable_dma(dmap->dma);
224 release_dma_lock(flags);
226 if (dmap->flags & DMA_BUSY)
227 dmap->dma_mode = DMODE_NONE;
228 dmap->flags &= ~DMA_BUSY;
230 if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
231 sound_free_dmap(dmap);
235 static unsigned int default_set_bits(int dev, unsigned int bits)
237 mm_segment_t fs = get_fs();
240 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
245 static int default_set_speed(int dev, int speed)
247 mm_segment_t fs = get_fs();
250 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
255 static short default_set_channels(int dev, short channels)
258 mm_segment_t fs = get_fs();
261 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
266 static void check_driver(struct audio_driver *d)
268 if (d->set_speed == NULL)
269 d->set_speed = default_set_speed;
270 if (d->set_bits == NULL)
271 d->set_bits = default_set_bits;
272 if (d->set_channels == NULL)
273 d->set_channels = default_set_channels;
276 int DMAbuf_open(int dev, int mode)
278 struct audio_operations *adev = audio_devs[dev];
280 struct dma_buffparms *dmap_in = NULL;
281 struct dma_buffparms *dmap_out = NULL;
285 if (!(adev->flags & DMA_DUPLEX))
286 adev->dmap_in = adev->dmap_out;
287 check_driver(adev->d);
289 if ((retval = adev->d->open(dev, mode)) < 0)
291 dmap_out = adev->dmap_out;
292 dmap_in = adev->dmap_in;
293 if (dmap_in == dmap_out)
294 adev->flags &= ~DMA_DUPLEX;
296 if (mode & OPEN_WRITE) {
297 if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
302 adev->enable_bits = mode;
304 if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
305 if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
307 if (mode & OPEN_WRITE)
308 close_dmap(adev, dmap_out);
312 adev->open_mode = mode;
315 adev->d->set_bits(dev, 8);
316 adev->d->set_channels(dev, 1);
317 adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
318 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
319 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
320 adev->dmap_out->bytes_in_use);
323 /* MUST not hold the spinlock */
324 void DMAbuf_reset(int dev)
326 if (audio_devs[dev]->open_mode & OPEN_WRITE)
327 dma_reset_output(dev);
329 if (audio_devs[dev]->open_mode & OPEN_READ)
330 dma_reset_input(dev);
333 static void dma_reset_output(int dev)
335 struct audio_operations *adev = audio_devs[dev];
336 unsigned long flags,f ;
337 struct dma_buffparms *dmap = adev->dmap_out;
339 if (!(dmap->flags & DMA_STARTED)) /* DMA is not active */
343 * First wait until the current fragment has been played completely
345 spin_lock_irqsave(&dmap->lock,flags);
346 adev->dmap_out->flags |= DMA_SYNCING;
348 adev->dmap_out->underrun_count = 0;
349 if (!signal_pending(current) && adev->dmap_out->qlen &&
350 adev->dmap_out->underrun_count == 0){
351 spin_unlock_irqrestore(&dmap->lock,flags);
352 interruptible_sleep_on_timeout(&adev->out_sleeper,
353 dmabuf_timeout(dmap));
354 spin_lock_irqsave(&dmap->lock,flags);
356 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
359 * Finally shut the device off
361 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_output)
362 adev->d->halt_io(dev);
364 adev->d->halt_output(dev);
365 adev->dmap_out->flags &= ~DMA_STARTED;
368 clear_dma_ff(dmap->dma);
369 disable_dma(dmap->dma);
372 dmap->byte_counter = 0;
373 reorganize_buffers(dev, adev->dmap_out, 0);
374 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
375 spin_unlock_irqrestore(&dmap->lock,flags);
378 static void dma_reset_input(int dev)
380 struct audio_operations *adev = audio_devs[dev];
382 struct dma_buffparms *dmap = adev->dmap_in;
384 spin_lock_irqsave(&dmap->lock,flags);
385 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_input)
386 adev->d->halt_io(dev);
388 adev->d->halt_input(dev);
389 adev->dmap_in->flags &= ~DMA_STARTED;
391 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
392 dmap->byte_counter = 0;
393 reorganize_buffers(dev, adev->dmap_in, 1);
394 spin_unlock_irqrestore(&dmap->lock,flags);
396 /* MUST be called with holding the dmap->lock */
397 void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
399 struct audio_operations *adev = audio_devs[dev];
401 if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
402 return; /* Don't start DMA yet */
403 dmap->dma_mode = DMODE_OUTPUT;
405 if (!(dmap->flags & DMA_ACTIVE) || !(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
406 if (!(dmap->flags & DMA_STARTED)) {
407 reorganize_buffers(dev, dmap, 0);
408 if (adev->d->prepare_for_output(dev, dmap->fragment_size, dmap->nbufs))
410 if (!(dmap->flags & DMA_NODMA))
411 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_WRITE);
412 dmap->flags |= DMA_STARTED;
414 if (dmap->counts[dmap->qhead] == 0)
415 dmap->counts[dmap->qhead] = dmap->fragment_size;
416 dmap->dma_mode = DMODE_OUTPUT;
417 adev->d->output_block(dev, dmap->raw_buf_phys + dmap->qhead * dmap->fragment_size,
418 dmap->counts[dmap->qhead], 1);
419 if (adev->d->trigger)
420 adev->d->trigger(dev,adev->enable_bits * adev->go);
422 dmap->flags |= DMA_ACTIVE;
425 int DMAbuf_sync(int dev)
427 struct audio_operations *adev = audio_devs[dev];
430 struct dma_buffparms *dmap;
432 if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
435 if (adev->dmap_out->dma_mode == DMODE_OUTPUT) {
436 dmap = adev->dmap_out;
437 spin_lock_irqsave(&dmap->lock,flags);
438 if (dmap->qlen > 0 && !(dmap->flags & DMA_ACTIVE))
439 DMAbuf_launch_output(dev, dmap);
440 adev->dmap_out->flags |= DMA_SYNCING;
441 adev->dmap_out->underrun_count = 0;
442 while (!signal_pending(current) && n++ <= adev->dmap_out->nbufs &&
443 adev->dmap_out->qlen && adev->dmap_out->underrun_count == 0) {
444 long t = dmabuf_timeout(dmap);
445 spin_unlock_irqrestore(&dmap->lock,flags);
446 /* FIXME: not safe may miss events */
447 t = interruptible_sleep_on_timeout(&adev->out_sleeper, t);
448 spin_lock_irqsave(&dmap->lock,flags);
450 adev->dmap_out->flags &= ~DMA_SYNCING;
451 spin_unlock_irqrestore(&dmap->lock,flags);
452 return adev->dmap_out->qlen;
455 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
458 * Some devices such as GUS have huge amount of on board RAM for the
459 * audio data. We have to wait until the device has finished playing.
462 /* still holding the lock */
463 if (adev->d->local_qlen) { /* Device has hidden buffers */
464 while (!signal_pending(current) &&
465 adev->d->local_qlen(dev)){
466 spin_unlock_irqrestore(&dmap->lock,flags);
467 interruptible_sleep_on_timeout(&adev->out_sleeper,
468 dmabuf_timeout(dmap));
469 spin_lock_irqsave(&dmap->lock,flags);
472 spin_unlock_irqrestore(&dmap->lock,flags);
474 adev->dmap_out->dma_mode = DMODE_NONE;
475 return adev->dmap_out->qlen;
478 int DMAbuf_release(int dev, int mode)
480 struct audio_operations *adev = audio_devs[dev];
481 struct dma_buffparms *dmap;
484 dmap = adev->dmap_out;
485 if (adev->open_mode & OPEN_WRITE)
486 adev->dmap_out->closing = 1;
488 if (adev->open_mode & OPEN_READ){
489 adev->dmap_in->closing = 1;
490 dmap = adev->dmap_in;
492 if (adev->open_mode & OPEN_WRITE)
493 if (!(adev->dmap_out->mapping_flags & DMA_MAP_MAPPED))
494 if (!signal_pending(current) && (adev->dmap_out->dma_mode == DMODE_OUTPUT))
496 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
497 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte, adev->dmap_out->bytes_in_use);
500 spin_lock_irqsave(&dmap->lock,flags);
503 if (adev->open_mode & OPEN_WRITE)
504 close_dmap(adev, adev->dmap_out);
506 if (adev->open_mode == OPEN_READ ||
507 (adev->open_mode != OPEN_WRITE &&
508 (adev->flags & DMA_DUPLEX)))
509 close_dmap(adev, adev->dmap_in);
511 spin_unlock_irqrestore(&dmap->lock,flags);
514 /* called with dmap->lock dold */
515 int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
517 struct audio_operations *adev = audio_devs[dev];
520 if (!(adev->open_mode & OPEN_READ))
522 if (!(adev->enable_bits & PCM_ENABLE_INPUT))
524 if (dmap->dma_mode == DMODE_OUTPUT) { /* Direction change */
525 /* release lock - it's not recursive */
526 spin_unlock_irq(&dmap->lock);
529 spin_lock_irq(&dmap->lock);
530 dmap->dma_mode = DMODE_NONE;
532 if (!dmap->dma_mode) {
533 reorganize_buffers(dev, dmap, 1);
534 if ((err = adev->d->prepare_for_input(dev,
535 dmap->fragment_size, dmap->nbufs)) < 0)
537 dmap->dma_mode = DMODE_INPUT;
539 if (!(dmap->flags & DMA_ACTIVE)) {
540 if (dmap->needs_reorg)
541 reorganize_buffers(dev, dmap, 0);
542 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
543 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
544 dmap->fragment_size, 0);
545 dmap->flags |= DMA_ACTIVE;
546 if (adev->d->trigger)
547 adev->d->trigger(dev, adev->enable_bits * adev->go);
552 int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
554 struct audio_operations *adev = audio_devs[dev];
557 struct dma_buffparms *dmap = adev->dmap_in;
560 if (!(adev->open_mode & OPEN_READ))
562 spin_lock_irqsave(&dmap->lock,flags);
563 if (dmap->needs_reorg)
564 reorganize_buffers(dev, dmap, 0);
565 if (adev->dmap_in->mapping_flags & DMA_MAP_MAPPED) {
566 /* printk(KERN_WARNING "Sound: Can't read from mmapped device (1)\n");*/
567 spin_unlock_irqrestore(&dmap->lock,flags);
569 } else while (dmap->qlen <= 0 && n++ < 10) {
570 long timeout = MAX_SCHEDULE_TIMEOUT;
571 if (!(adev->enable_bits & PCM_ENABLE_INPUT) || !adev->go) {
572 spin_unlock_irqrestore(&dmap->lock,flags);
575 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
576 spin_unlock_irqrestore(&dmap->lock,flags);
579 /* Wait for the next block */
582 spin_unlock_irqrestore(&dmap->lock,flags);
586 timeout = dmabuf_timeout(dmap);
588 spin_unlock_irqrestore(&dmap->lock,flags);
589 timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
592 /* FIXME: include device name */
594 printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
595 dma_reset_input(dev);
598 spin_lock_irqsave(&dmap->lock,flags);
600 spin_unlock_irqrestore(&dmap->lock,flags);
603 return err ? err : -EINTR;
604 *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
605 *len = dmap->fragment_size - dmap->counts[dmap->qhead];
610 int DMAbuf_rmchars(int dev, int buff_no, int c)
612 struct audio_operations *adev = audio_devs[dev];
613 struct dma_buffparms *dmap = adev->dmap_in;
614 int p = dmap->counts[dmap->qhead] + c;
616 if (dmap->mapping_flags & DMA_MAP_MAPPED)
618 /* printk("Sound: Can't read from mmapped device (2)\n");*/
621 else if (dmap->qlen <= 0)
623 else if (p >= dmap->fragment_size) { /* This buffer is completely empty */
624 dmap->counts[dmap->qhead] = 0;
626 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
628 else dmap->counts[dmap->qhead] = p;
632 /* MUST be called with dmap->lock hold */
633 int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
636 * Try to approximate the active byte position of the DMA pointer within the
637 * buffer area as well as possible.
643 if (!(dmap->flags & DMA_ACTIVE))
646 int chan = dmap->dma;
651 if(!isa_dma_bridge_buggy)
652 disable_dma(dmap->dma);
654 pos = get_dma_residue(chan);
656 pos = dmap->bytes_in_use - pos;
658 if (!(dmap->mapping_flags & DMA_MAP_MAPPED)) {
659 if (direction == DMODE_OUTPUT) {
660 if (dmap->qhead == 0)
661 if (pos > dmap->fragment_size)
664 if (dmap->qtail == 0)
665 if (pos > dmap->fragment_size)
671 if (pos >= dmap->bytes_in_use)
674 if(!isa_dma_bridge_buggy)
675 enable_dma(dmap->dma);
679 /* printk( "%04x ", pos); */
685 * DMAbuf_start_devices() is called by the /dev/music driver to start
686 * one or more audio devices at desired moment.
689 void DMAbuf_start_devices(unsigned int devmask)
691 struct audio_operations *adev;
694 for (dev = 0; dev < num_audiodevs; dev++) {
695 if (!(devmask & (1 << dev)))
697 if (!(adev = audio_devs[dev]))
699 if (adev->open_mode == 0)
703 /* OK to start the device */
705 if (adev->d->trigger)
706 adev->d->trigger(dev,adev->enable_bits * adev->go);
709 /* via poll called without a lock ?*/
710 int DMAbuf_space_in_queue(int dev)
712 struct audio_operations *adev = audio_devs[dev];
714 struct dma_buffparms *dmap = adev->dmap_out;
715 int lim = dmap->nbufs;
720 if (dmap->qlen >= lim) /* No space at all */
724 * Verify that there are no more pending buffers than the limit
725 * defined by the process.
728 max = dmap->max_fragments;
733 if (adev->d->local_qlen) {
734 tmp = adev->d->local_qlen(dev);
736 tmp--; /* This buffer has been counted twice */
739 if (dmap->byte_counter % dmap->fragment_size) /* There is a partial fragment */
746 /* MUST not hold the spinlock - this function may sleep */
747 static int output_sleep(int dev, int dontblock)
749 struct audio_operations *adev = audio_devs[dev];
751 struct dma_buffparms *dmap = adev->dmap_out;
757 if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
761 * Wait for free space
763 if (signal_pending(current))
765 timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
767 timeout_value = dmabuf_timeout(dmap);
769 timeout_value = MAX_SCHEDULE_TIMEOUT;
770 timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
772 if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
773 printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
774 dma_reset_output(dev);
776 if (signal_pending(current))
781 /* called with the lock held */
782 static int find_output_space(int dev, char **buf, int *size)
784 struct audio_operations *adev = audio_devs[dev];
785 struct dma_buffparms *dmap = adev->dmap_out;
786 unsigned long active_offs;
789 int occupied_bytes = (dmap->user_counter % dmap->fragment_size);
791 *buf = dmap->raw_buf;
792 if (!(maxfrags = DMAbuf_space_in_queue(dev)) && !occupied_bytes)
795 #ifdef BE_CONSERVATIVE
796 active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
798 active_offs = DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT);
799 /* Check for pointer wrapping situation */
800 if (active_offs < 0 || active_offs >= dmap->bytes_in_use)
802 active_offs += dmap->byte_counter;
805 offs = (dmap->user_counter % dmap->bytes_in_use) & ~SAMPLE_ROUNDUP;
806 if (offs < 0 || offs >= dmap->bytes_in_use) {
807 printk(KERN_ERR "Sound: Got unexpected offs %ld. Giving up.\n", offs);
808 printk("Counter = %ld, bytes=%d\n", dmap->user_counter, dmap->bytes_in_use);
811 *buf = dmap->raw_buf + offs;
813 len = active_offs + dmap->bytes_in_use - dmap->user_counter; /* Number of unused bytes in buffer */
815 if ((offs + len) > dmap->bytes_in_use)
816 len = dmap->bytes_in_use - offs;
820 if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
821 len = (maxfrags * dmap->fragment_size) - occupied_bytes;
822 *size = len & ~SAMPLE_ROUNDUP;
826 int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
828 struct audio_operations *adev = audio_devs[dev];
831 struct dma_buffparms *dmap = adev->dmap_out;
833 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
834 /* printk(KERN_DEBUG "Sound: Can't write to mmapped device (3)\n");*/
837 spin_lock_irqsave(&dmap->lock,flags);
838 if (dmap->needs_reorg)
839 reorganize_buffers(dev, dmap, 0);
841 if (dmap->dma_mode == DMODE_INPUT) { /* Direction change */
842 spin_unlock_irqrestore(&dmap->lock,flags);
844 spin_lock_irqsave(&dmap->lock,flags);
846 dmap->dma_mode = DMODE_OUTPUT;
848 while (find_output_space(dev, buf, size) <= 0) {
849 spin_unlock_irqrestore(&dmap->lock,flags);
850 if ((err = output_sleep(dev, dontblock)) < 0) {
853 spin_lock_irqsave(&dmap->lock,flags);
856 spin_unlock_irqrestore(&dmap->lock,flags);
859 /* has to acquire dmap->lock */
860 int DMAbuf_move_wrpointer(int dev, int l)
862 struct audio_operations *adev = audio_devs[dev];
863 struct dma_buffparms *dmap = adev->dmap_out;
865 unsigned long end_ptr, p;
869 spin_lock_irqsave(&dmap->lock,flags);
870 post= (dmap->flags & DMA_POST);
871 ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
873 dmap->flags &= ~DMA_POST;
875 dmap->user_counter += l;
876 dmap->flags |= DMA_DIRTY;
878 if (dmap->byte_counter >= dmap->max_byte_counter) {
879 /* Wrap the byte counters */
880 long decr = dmap->byte_counter;
881 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
882 decr -= dmap->byte_counter;
883 dmap->user_counter -= decr;
885 end_ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
887 p = (dmap->user_counter - 1) % dmap->bytes_in_use;
888 dmap->neutral_byte = dmap->raw_buf[p];
890 /* Update the fragment based bookkeeping too */
891 while (ptr < end_ptr) {
892 dmap->counts[dmap->qtail] = dmap->fragment_size;
893 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
895 ptr += dmap->fragment_size;
898 dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
901 * Let the low level driver perform some postprocessing to
904 if (adev->d->postprocess_write)
905 adev->d->postprocess_write(dev);
907 if (!(dmap->flags & DMA_ACTIVE))
908 if (dmap->qlen > 1 || (dmap->qlen > 0 && (post || dmap->qlen >= dmap->nbufs - 1)))
909 DMAbuf_launch_output(dev, dmap);
911 spin_unlock_irqrestore(&dmap->lock,flags);
915 int DMAbuf_start_dma(int dev, unsigned long physaddr, int count, int dma_mode)
917 struct audio_operations *adev = audio_devs[dev];
918 struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
920 if (dmap->raw_buf == NULL) {
921 printk(KERN_ERR "sound: DMA buffer(1) == NULL\n");
922 printk("Device %d, chn=%s\n", dev, (dmap == adev->dmap_out) ? "out" : "in");
927 sound_start_dma(dmap, physaddr, count, dma_mode);
930 EXPORT_SYMBOL(DMAbuf_start_dma);
932 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode)
934 struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
936 if (dmap->raw_buf == NULL) {
937 printk(KERN_ERR "sound: DMA buffer(2) == NULL\n");
938 printk(KERN_ERR "Device %s, chn=%s\n", adev->name, (dmap == adev->dmap_out) ? "out" : "in");
941 if (dmap->flags & DMA_NODMA)
945 sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
946 dmap->flags |= DMA_STARTED;
950 static void finish_output_interrupt(int dev, struct dma_buffparms *dmap)
952 struct audio_operations *adev = audio_devs[dev];
954 if (dmap->audio_callback != NULL)
955 dmap->audio_callback(dev, dmap->callback_parm);
956 wake_up(&adev->out_sleeper);
957 wake_up(&adev->poll_sleeper);
959 /* called with dmap->lock held in irq context*/
960 static void do_outputintr(int dev, int dummy)
962 struct audio_operations *adev = audio_devs[dev];
963 struct dma_buffparms *dmap = adev->dmap_out;
966 if (dmap->raw_buf == NULL) {
967 printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
970 if (dmap->mapping_flags & DMA_MAP_MAPPED) { /* Virtual memory mapped access */
972 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
973 if (dmap->qhead == 0) { /* Wrapped */
974 dmap->byte_counter += dmap->bytes_in_use;
975 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
976 long decr = dmap->byte_counter;
977 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
978 decr -= dmap->byte_counter;
979 dmap->user_counter -= decr;
982 dmap->qlen++; /* Yes increment it (don't decrement) */
983 if (!(adev->flags & DMA_AUTOMODE))
984 dmap->flags &= ~DMA_ACTIVE;
985 dmap->counts[dmap->qhead] = dmap->fragment_size;
986 DMAbuf_launch_output(dev, dmap);
987 finish_output_interrupt(dev, dmap);
992 this_fragment = dmap->qhead;
993 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
995 if (dmap->qhead == 0) { /* Wrapped */
996 dmap->byte_counter += dmap->bytes_in_use;
997 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
998 long decr = dmap->byte_counter;
999 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
1000 decr -= dmap->byte_counter;
1001 dmap->user_counter -= decr;
1004 if (!(adev->flags & DMA_AUTOMODE))
1005 dmap->flags &= ~DMA_ACTIVE;
1008 * This is dmap->qlen <= 0 except when closing when
1012 while (dmap->qlen <= -dmap->closing) {
1013 dmap->underrun_count++;
1015 if ((dmap->flags & DMA_DIRTY) && dmap->applic_profile != APF_CPUINTENS) {
1016 dmap->flags &= ~DMA_DIRTY;
1017 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
1018 adev->dmap_out->buffsize);
1020 dmap->user_counter += dmap->fragment_size;
1021 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1024 DMAbuf_launch_output(dev, dmap);
1025 finish_output_interrupt(dev, dmap);
1027 /* called in irq context */
1028 void DMAbuf_outputintr(int dev, int notify_only)
1030 struct audio_operations *adev = audio_devs[dev];
1031 unsigned long flags;
1032 struct dma_buffparms *dmap = adev->dmap_out;
1034 spin_lock_irqsave(&dmap->lock,flags);
1035 if (!(dmap->flags & DMA_NODMA)) {
1036 int chan = dmap->dma, pos, n;
1041 if(!isa_dma_bridge_buggy)
1042 disable_dma(dmap->dma);
1044 pos = dmap->bytes_in_use - get_dma_residue(chan);
1045 if(!isa_dma_bridge_buggy)
1046 enable_dma(dmap->dma);
1047 release_dma_lock(f);
1049 pos = pos / dmap->fragment_size; /* Actual qhead */
1050 if (pos < 0 || pos >= dmap->nbufs)
1053 while (dmap->qhead != pos && n++ < dmap->nbufs)
1054 do_outputintr(dev, notify_only);
1057 do_outputintr(dev, notify_only);
1058 spin_unlock_irqrestore(&dmap->lock,flags);
1060 EXPORT_SYMBOL(DMAbuf_outputintr);
1062 /* called with dmap->lock held in irq context */
1063 static void do_inputintr(int dev)
1065 struct audio_operations *adev = audio_devs[dev];
1066 struct dma_buffparms *dmap = adev->dmap_in;
1068 if (dmap->raw_buf == NULL) {
1069 printk(KERN_ERR "Sound: Fatal error. Audio interrupt after freeing buffers.\n");
1072 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1073 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1074 if (dmap->qtail == 0) { /* Wrapped */
1075 dmap->byte_counter += dmap->bytes_in_use;
1076 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
1077 long decr = dmap->byte_counter;
1078 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1079 decr -= dmap->byte_counter;
1080 dmap->user_counter -= decr;
1085 if (!(adev->flags & DMA_AUTOMODE)) {
1086 if (dmap->needs_reorg)
1087 reorganize_buffers(dev, dmap, 0);
1088 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_READ);
1089 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
1090 dmap->fragment_size, 1);
1091 if (adev->d->trigger)
1092 adev->d->trigger(dev, adev->enable_bits * adev->go);
1094 dmap->flags |= DMA_ACTIVE;
1095 } else if (dmap->qlen >= (dmap->nbufs - 1)) {
1096 printk(KERN_WARNING "Sound: Recording overrun\n");
1097 dmap->underrun_count++;
1099 /* Just throw away the oldest fragment but keep the engine running */
1100 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1101 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1102 } else if (dmap->qlen >= 0 && dmap->qlen < dmap->nbufs) {
1104 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1105 if (dmap->qtail == 0) { /* Wrapped */
1106 dmap->byte_counter += dmap->bytes_in_use;
1107 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
1108 long decr = dmap->byte_counter;
1109 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1110 decr -= dmap->byte_counter;
1111 dmap->user_counter -= decr;
1115 if (!(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
1116 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
1117 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size, dmap->fragment_size, 1);
1118 if (adev->d->trigger)
1119 adev->d->trigger(dev,adev->enable_bits * adev->go);
1121 dmap->flags |= DMA_ACTIVE;
1124 wake_up(&adev->in_sleeper);
1125 wake_up(&adev->poll_sleeper);
1128 /* called in irq context */
1129 void DMAbuf_inputintr(int dev)
1131 struct audio_operations *adev = audio_devs[dev];
1132 struct dma_buffparms *dmap = adev->dmap_in;
1133 unsigned long flags;
1135 spin_lock_irqsave(&dmap->lock,flags);
1137 if (!(dmap->flags & DMA_NODMA)) {
1138 int chan = dmap->dma, pos, n;
1142 if(!isa_dma_bridge_buggy)
1143 disable_dma(dmap->dma);
1145 pos = dmap->bytes_in_use - get_dma_residue(chan);
1146 if(!isa_dma_bridge_buggy)
1147 enable_dma(dmap->dma);
1148 release_dma_lock(f);
1150 pos = pos / dmap->fragment_size; /* Actual qhead */
1151 if (pos < 0 || pos >= dmap->nbufs)
1155 while (dmap->qtail != pos && ++n < dmap->nbufs)
1159 spin_unlock_irqrestore(&dmap->lock,flags);
1161 EXPORT_SYMBOL(DMAbuf_inputintr);
1163 void DMAbuf_init(int dev, int dma1, int dma2)
1165 struct audio_operations *adev = audio_devs[dev];
1167 * NOTE! This routine could be called several times.
1170 if (adev && adev->dmap_out == NULL) {
1171 if (adev->d == NULL)
1172 panic("OSS: audio_devs[%d]->d == NULL\n", dev);
1174 if (adev->parent_dev) { /* Use DMA map of the parent dev */
1175 int parent = adev->parent_dev - 1;
1176 adev->dmap_out = audio_devs[parent]->dmap_out;
1177 adev->dmap_in = audio_devs[parent]->dmap_in;
1179 adev->dmap_out = adev->dmap_in = &adev->dmaps[0];
1180 adev->dmap_out->dma = dma1;
1181 if (adev->flags & DMA_DUPLEX) {
1182 adev->dmap_in = &adev->dmaps[1];
1183 adev->dmap_in->dma = dma2;
1186 /* Persistent DMA buffers allocated here */
1187 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1188 if (adev->dmap_in->raw_buf == NULL)
1189 sound_alloc_dmap(adev->dmap_in);
1190 if (adev->dmap_out->raw_buf == NULL)
1191 sound_alloc_dmap(adev->dmap_out);
1196 /* No kernel lock - DMAbuf_activate_recording protected by global cli/sti */
1197 static unsigned int poll_input(struct file * file, int dev, poll_table *wait)
1199 struct audio_operations *adev = audio_devs[dev];
1200 struct dma_buffparms *dmap = adev->dmap_in;
1202 if (!(adev->open_mode & OPEN_READ))
1204 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1206 return POLLIN | POLLRDNORM;
1209 if (dmap->dma_mode != DMODE_INPUT) {
1210 if (dmap->dma_mode == DMODE_NONE &&
1211 adev->enable_bits & PCM_ENABLE_INPUT &&
1212 !dmap->qlen && adev->go) {
1213 unsigned long flags;
1215 spin_lock_irqsave(&dmap->lock,flags);
1216 DMAbuf_activate_recording(dev, dmap);
1217 spin_unlock_irqrestore(&dmap->lock,flags);
1223 return POLLIN | POLLRDNORM;
1226 static unsigned int poll_output(struct file * file, int dev, poll_table *wait)
1228 struct audio_operations *adev = audio_devs[dev];
1229 struct dma_buffparms *dmap = adev->dmap_out;
1231 if (!(adev->open_mode & OPEN_WRITE))
1233 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1235 return POLLOUT | POLLWRNORM;
1238 if (dmap->dma_mode == DMODE_INPUT)
1240 if (dmap->dma_mode == DMODE_NONE)
1241 return POLLOUT | POLLWRNORM;
1242 if (!DMAbuf_space_in_queue(dev))
1244 return POLLOUT | POLLWRNORM;
1247 unsigned int DMAbuf_poll(struct file * file, int dev, poll_table *wait)
1249 struct audio_operations *adev = audio_devs[dev];
1250 poll_wait(file, &adev->poll_sleeper, wait);
1251 return poll_input(file, dev, wait) | poll_output(file, dev, wait);
1254 void DMAbuf_deinit(int dev)
1256 struct audio_operations *adev = audio_devs[dev];
1257 /* This routine is called when driver is being unloaded */
1261 /* Persistent DMA buffers deallocated here */
1262 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1263 sound_free_dmap(adev->dmap_out);
1264 if (adev->flags & DMA_DUPLEX)
1265 sound_free_dmap(adev->dmap_in);