asm-generic: merge branch 'master' of torvalds/linux-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / oss / dmabuf.c
blob1bfcf7e8854632a7b395f9143b9fb3c3d0408545
1 /*
2 * sound/oss/dmabuf.c
4 * The DMA buffer manager for digitized voice applications
5 */
6 /*
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
11 * for more info.
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
16 * 12k or so
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
28 #include <linux/mm.h>
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)
46 long tmout;
48 tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
49 tmout += HZ / 5; /* Some safety distance */
50 if (tmout < (HZ / 2))
51 tmout = HZ / 2;
52 if (tmout > 20 * HZ)
53 tmout = 20 * HZ;
54 return tmout;
57 static int sound_alloc_dmap(struct dma_buffparms *dmap)
59 char *start_addr, *end_addr;
60 int dma_pagesize;
61 int sz, size;
62 struct page *page;
64 dmap->mapping_flags &= ~DMA_MAP_MAPPED;
66 if (dmap->raw_buf != NULL)
67 return 0; /* Already done */
68 if (dma_buffsize < 4096)
69 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)
77 dma_pagesize=32768;
79 dmap->raw_buf = NULL;
80 dmap->buffsize = dma_buffsize;
81 if (dmap->buffsize > dma_pagesize)
82 dmap->buffsize = dma_pagesize;
83 start_addr = NULL;
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
87 * reasons.
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)
94 dmap->buffsize /= 2;
97 if (start_addr == NULL) {
98 printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
99 return -ENOMEM;
100 } else {
101 /* make some checks */
102 end_addr = start_addr + dmap->buffsize - 1;
104 if (debugmem)
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);
112 return -EFAULT;
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);
120 return 0;
123 static void sound_free_dmap(struct dma_buffparms *dmap)
125 int sz, size;
126 struct page *page;
127 unsigned long start_addr, end_addr;
129 if (dmap->raw_buf == NULL)
130 return;
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)
150 unsigned long flags;
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();
156 disable_dma(chan);
157 clear_dma_ff(chan);
158 set_dma_mode(chan, dma_mode);
159 set_dma_addr(chan, physaddr);
160 set_dma_count(chan, count);
161 enable_dma(chan);
162 release_dma_lock(flags);
164 return 0;
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;
178 dmap->cfrag = -1;
179 dmap->closing = 0;
180 dmap->nbufs = 1;
181 dmap->flags = DMA_BUSY; /* Other flags off */
184 static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
186 int err;
188 if (dmap->flags & DMA_BUSY)
189 return -EBUSY;
190 if ((err = sound_alloc_dmap(dmap)) < 0)
191 return err;
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);
199 return -EBUSY;
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;
213 return 0;
216 static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
218 unsigned long flags;
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();
239 set_fs(get_ds());
240 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
241 set_fs(fs);
242 return bits;
245 static int default_set_speed(int dev, int speed)
247 mm_segment_t fs = get_fs();
249 set_fs(get_ds());
250 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
251 set_fs(fs);
252 return speed;
255 static short default_set_channels(int dev, short channels)
257 int c = channels;
258 mm_segment_t fs = get_fs();
260 set_fs(get_ds());
261 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
262 set_fs(fs);
263 return 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];
279 int retval;
280 struct dma_buffparms *dmap_in = NULL;
281 struct dma_buffparms *dmap_out = NULL;
283 if (!adev)
284 return -ENXIO;
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)
290 return retval;
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) {
298 adev->d->close(dev);
299 return retval;
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) {
306 adev->d->close(dev);
307 if (mode & OPEN_WRITE)
308 close_dmap(adev, dmap_out);
309 return retval;
312 adev->open_mode = mode;
313 adev->go = 1;
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);
321 return 0;
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 */
340 return;
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);
363 else
364 adev->d->halt_output(dev);
365 adev->dmap_out->flags &= ~DMA_STARTED;
367 f=claim_dma_lock();
368 clear_dma_ff(dmap->dma);
369 disable_dma(dmap->dma);
370 release_dma_lock(f);
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];
381 unsigned long flags;
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);
387 else
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))
409 return;
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];
428 unsigned long flags;
429 int n = 0;
430 struct dma_buffparms *dmap;
432 if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
433 return 0;
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);
449 if (!t) {
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;
482 unsigned long flags;
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))
495 DMAbuf_sync(dev);
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);
499 DMAbuf_reset(dev);
500 spin_lock_irqsave(&dmap->lock,flags);
501 adev->d->close(dev);
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);
510 adev->open_mode = 0;
511 spin_unlock_irqrestore(&dmap->lock,flags);
512 return 0;
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];
518 int err;
520 if (!(adev->open_mode & OPEN_READ))
521 return 0;
522 if (!(adev->enable_bits & PCM_ENABLE_INPUT))
523 return 0;
524 if (dmap->dma_mode == DMODE_OUTPUT) { /* Direction change */
525 /* release lock - it's not recursive */
526 spin_unlock_irq(&dmap->lock);
527 DMAbuf_sync(dev);
528 DMAbuf_reset(dev);
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)
536 return err;
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);
549 return 0;
551 /* acquires lock */
552 int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
554 struct audio_operations *adev = audio_devs[dev];
555 unsigned long flags;
556 int err = 0, n = 0;
557 struct dma_buffparms *dmap = adev->dmap_in;
558 int go;
560 if (!(adev->open_mode & OPEN_READ))
561 return -EIO;
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);
568 return -EINVAL;
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);
573 return -EAGAIN;
575 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
576 spin_unlock_irqrestore(&dmap->lock,flags);
577 return err;
579 /* Wait for the next block */
581 if (dontblock) {
582 spin_unlock_irqrestore(&dmap->lock,flags);
583 return -EAGAIN;
585 if ((go = adev->go))
586 timeout = dmabuf_timeout(dmap);
588 spin_unlock_irqrestore(&dmap->lock,flags);
589 timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
590 timeout);
591 if (!timeout) {
592 /* FIXME: include device name */
593 err = -EIO;
594 printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
595 dma_reset_input(dev);
596 } else
597 err = -EINTR;
598 spin_lock_irqsave(&dmap->lock,flags);
600 spin_unlock_irqrestore(&dmap->lock,flags);
602 if (dmap->qlen <= 0)
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];
607 return 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");*/
619 return -EINVAL;
621 else if (dmap->qlen <= 0)
622 return -EIO;
623 else if (p >= dmap->fragment_size) { /* This buffer is completely empty */
624 dmap->counts[dmap->qhead] = 0;
625 dmap->qlen--;
626 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
628 else dmap->counts[dmap->qhead] = p;
630 return 0;
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.
640 int pos;
641 unsigned long f;
643 if (!(dmap->flags & DMA_ACTIVE))
644 pos = 0;
645 else {
646 int chan = dmap->dma;
648 f=claim_dma_lock();
649 clear_dma_ff(chan);
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)
662 pos = 0;
663 } else {
664 if (dmap->qtail == 0)
665 if (pos > dmap->fragment_size)
666 pos = 0;
669 if (pos < 0)
670 pos = 0;
671 if (pos >= dmap->bytes_in_use)
672 pos = 0;
674 if(!isa_dma_bridge_buggy)
675 enable_dma(dmap->dma);
677 release_dma_lock(f);
679 /* printk( "%04x ", pos); */
681 return 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;
692 int dev;
694 for (dev = 0; dev < num_audiodevs; dev++) {
695 if (!(devmask & (1 << dev)))
696 continue;
697 if (!(adev = audio_devs[dev]))
698 continue;
699 if (adev->open_mode == 0)
700 continue;
701 if (adev->go)
702 continue;
703 /* OK to start the device */
704 adev->go = 1;
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];
713 int len, max, tmp;
714 struct dma_buffparms *dmap = adev->dmap_out;
715 int lim = dmap->nbufs;
717 if (lim < 2)
718 lim = 2;
720 if (dmap->qlen >= lim) /* No space at all */
721 return 0;
724 * Verify that there are no more pending buffers than the limit
725 * defined by the process.
728 max = dmap->max_fragments;
729 if (max > lim)
730 max = lim;
731 len = dmap->qlen;
733 if (adev->d->local_qlen) {
734 tmp = adev->d->local_qlen(dev);
735 if (tmp && len)
736 tmp--; /* This buffer has been counted twice */
737 len += tmp;
739 if (dmap->byte_counter % dmap->fragment_size) /* There is a partial fragment */
740 len = len + 1;
742 if (len >= max)
743 return 0;
744 return max - len;
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];
750 int err = 0;
751 struct dma_buffparms *dmap = adev->dmap_out;
752 long timeout;
753 long timeout_value;
755 if (dontblock)
756 return -EAGAIN;
757 if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
758 return -EAGAIN;
761 * Wait for free space
763 if (signal_pending(current))
764 return -EINTR;
765 timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
766 if (timeout)
767 timeout_value = dmabuf_timeout(dmap);
768 else
769 timeout_value = MAX_SCHEDULE_TIMEOUT;
770 timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
771 timeout_value);
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);
775 } else {
776 if (signal_pending(current))
777 err = -EINTR;
779 return err;
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;
787 long len, offs;
788 int maxfrags;
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)
793 return 0;
795 #ifdef BE_CONSERVATIVE
796 active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
797 #else
798 active_offs = max(DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT), 0);
799 /* Check for pointer wrapping situation */
800 if (active_offs >= dmap->bytes_in_use)
801 active_offs = 0;
802 active_offs += dmap->byte_counter;
803 #endif
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);
809 return 0;
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;
817 if (len < 0) {
818 return 0;
820 if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
821 len = (maxfrags * dmap->fragment_size) - occupied_bytes;
822 *size = len & ~SAMPLE_ROUNDUP;
823 return (*size > 0);
825 /* acquires lock */
826 int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
828 struct audio_operations *adev = audio_devs[dev];
829 unsigned long flags;
830 int err = -EIO;
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");*/
835 return -EINVAL;
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);
843 DMAbuf_reset(dev);
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) {
851 return err;
853 spin_lock_irqsave(&dmap->lock,flags);
856 spin_unlock_irqrestore(&dmap->lock,flags);
857 return 0;
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;
864 unsigned long ptr;
865 unsigned long end_ptr, p;
866 int post;
867 unsigned long flags;
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;
874 dmap->cfrag = -1;
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;
894 dmap->qlen++;
895 ptr += dmap->fragment_size;
898 dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
901 * Let the low level driver perform some postprocessing to
902 * the written data.
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);
912 return 0;
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");
923 return 0;
925 if (dmap->dma < 0)
926 return 0;
927 sound_start_dma(dmap, physaddr, count, dma_mode);
928 return count;
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");
939 return 0;
941 if (dmap->flags & DMA_NODMA)
942 return 1;
943 if (dmap->dma < 0)
944 return 0;
945 sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
946 dmap->flags |= DMA_STARTED;
947 return count;
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;
964 int this_fragment;
966 if (dmap->raw_buf == NULL) {
967 printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
968 return;
970 if (dmap->mapping_flags & DMA_MAP_MAPPED) { /* Virtual memory mapped access */
971 /* mmapped 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);
988 return;
991 dmap->qlen--;
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
1009 * dmap->qlen < 0
1012 while (dmap->qlen <= -dmap->closing) {
1013 dmap->underrun_count++;
1014 dmap->qlen++;
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;
1023 if (dmap->qlen > 0)
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;
1037 unsigned long f;
1039 f=claim_dma_lock();
1041 if(!isa_dma_bridge_buggy)
1042 disable_dma(dmap->dma);
1043 clear_dma_ff(chan);
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)
1051 pos = 0;
1052 n = 0;
1053 while (dmap->qhead != pos && n++ < dmap->nbufs)
1054 do_outputintr(dev, notify_only);
1056 else
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");
1070 return;
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;
1083 dmap->qlen++;
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) {
1103 dmap->qlen++;
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;
1122 if (dmap->qlen > 0)
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;
1139 unsigned long f;
1141 f=claim_dma_lock();
1142 if(!isa_dma_bridge_buggy)
1143 disable_dma(dmap->dma);
1144 clear_dma_ff(chan);
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)
1152 pos = 0;
1154 n = 0;
1155 while (dmap->qtail != pos && ++n < dmap->nbufs)
1156 do_inputintr(dev);
1157 } else
1158 do_inputintr(dev);
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;
1178 } else {
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))
1203 return 0;
1204 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1205 if (dmap->qlen)
1206 return POLLIN | POLLRDNORM;
1207 return 0;
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);
1219 return 0;
1221 if (!dmap->qlen)
1222 return 0;
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))
1232 return 0;
1233 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1234 if (dmap->qlen)
1235 return POLLOUT | POLLWRNORM;
1236 return 0;
1238 if (dmap->dma_mode == DMODE_INPUT)
1239 return 0;
1240 if (dmap->dma_mode == DMODE_NONE)
1241 return POLLOUT | POLLWRNORM;
1242 if (!DMAbuf_space_in_queue(dev))
1243 return 0;
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 */
1258 if (!adev)
1259 return;
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);