powerpc: Fix build warning of the defconfigs
[zen-stable.git] / sound / oss / dmabuf.c
blobbcc3e8e071229db29775632dc6a7380aeda0f6f5
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 <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)
47 long tmout;
49 tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
50 tmout += HZ / 5; /* Some safety distance */
51 if (tmout < (HZ / 2))
52 tmout = HZ / 2;
53 if (tmout > 20 * HZ)
54 tmout = 20 * HZ;
55 return tmout;
58 static int sound_alloc_dmap(struct dma_buffparms *dmap)
60 char *start_addr, *end_addr;
61 int dma_pagesize;
62 int sz, size;
63 struct page *page;
65 dmap->mapping_flags &= ~DMA_MAP_MAPPED;
67 if (dmap->raw_buf != NULL)
68 return 0; /* Already done */
69 if (dma_buffsize < 4096)
70 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)
78 dma_pagesize=32768;
80 dmap->raw_buf = NULL;
81 dmap->buffsize = dma_buffsize;
82 if (dmap->buffsize > dma_pagesize)
83 dmap->buffsize = dma_pagesize;
84 start_addr = NULL;
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
88 * reasons.
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)
95 dmap->buffsize /= 2;
98 if (start_addr == NULL) {
99 printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
100 return -ENOMEM;
101 } else {
102 /* make some checks */
103 end_addr = start_addr + dmap->buffsize - 1;
105 if (debugmem)
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);
113 return -EFAULT;
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);
121 return 0;
124 static void sound_free_dmap(struct dma_buffparms *dmap)
126 int sz, size;
127 struct page *page;
128 unsigned long start_addr, end_addr;
130 if (dmap->raw_buf == NULL)
131 return;
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)
151 unsigned long flags;
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();
157 disable_dma(chan);
158 clear_dma_ff(chan);
159 set_dma_mode(chan, dma_mode);
160 set_dma_addr(chan, physaddr);
161 set_dma_count(chan, count);
162 enable_dma(chan);
163 release_dma_lock(flags);
165 return 0;
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;
179 dmap->cfrag = -1;
180 dmap->closing = 0;
181 dmap->nbufs = 1;
182 dmap->flags = DMA_BUSY; /* Other flags off */
185 static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
187 int err;
189 if (dmap->flags & DMA_BUSY)
190 return -EBUSY;
191 if ((err = sound_alloc_dmap(dmap)) < 0)
192 return err;
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);
200 return -EBUSY;
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;
214 return 0;
217 static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
219 unsigned long flags;
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();
240 set_fs(get_ds());
241 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
242 set_fs(fs);
243 return bits;
246 static int default_set_speed(int dev, int speed)
248 mm_segment_t fs = get_fs();
250 set_fs(get_ds());
251 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
252 set_fs(fs);
253 return speed;
256 static short default_set_channels(int dev, short channels)
258 int c = channels;
259 mm_segment_t fs = get_fs();
261 set_fs(get_ds());
262 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
263 set_fs(fs);
264 return 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];
280 int retval;
281 struct dma_buffparms *dmap_in = NULL;
282 struct dma_buffparms *dmap_out = NULL;
284 if (!adev)
285 return -ENXIO;
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)
291 return retval;
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) {
299 adev->d->close(dev);
300 return retval;
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) {
307 adev->d->close(dev);
308 if (mode & OPEN_WRITE)
309 close_dmap(adev, dmap_out);
310 return retval;
313 adev->open_mode = mode;
314 adev->go = 1;
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);
322 return 0;
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 */
341 return;
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);
364 else
365 adev->d->halt_output(dev);
366 adev->dmap_out->flags &= ~DMA_STARTED;
368 f=claim_dma_lock();
369 clear_dma_ff(dmap->dma);
370 disable_dma(dmap->dma);
371 release_dma_lock(f);
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];
382 unsigned long flags;
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);
388 else
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))
410 return;
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];
429 unsigned long flags;
430 int n = 0;
431 struct dma_buffparms *dmap;
433 if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
434 return 0;
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);
450 if (!t) {
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;
483 unsigned long flags;
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))
496 DMAbuf_sync(dev);
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);
500 DMAbuf_reset(dev);
501 spin_lock_irqsave(&dmap->lock,flags);
502 adev->d->close(dev);
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);
511 adev->open_mode = 0;
512 spin_unlock_irqrestore(&dmap->lock,flags);
513 return 0;
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];
519 int err;
521 if (!(adev->open_mode & OPEN_READ))
522 return 0;
523 if (!(adev->enable_bits & PCM_ENABLE_INPUT))
524 return 0;
525 if (dmap->dma_mode == DMODE_OUTPUT) { /* Direction change */
526 /* release lock - it's not recursive */
527 spin_unlock_irq(&dmap->lock);
528 DMAbuf_sync(dev);
529 DMAbuf_reset(dev);
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)
537 return err;
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);
550 return 0;
552 /* acquires lock */
553 int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
555 struct audio_operations *adev = audio_devs[dev];
556 unsigned long flags;
557 int err = 0, n = 0;
558 struct dma_buffparms *dmap = adev->dmap_in;
559 int go;
561 if (!(adev->open_mode & OPEN_READ))
562 return -EIO;
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);
569 return -EINVAL;
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);
574 return -EAGAIN;
576 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
577 spin_unlock_irqrestore(&dmap->lock,flags);
578 return err;
580 /* Wait for the next block */
582 if (dontblock) {
583 spin_unlock_irqrestore(&dmap->lock,flags);
584 return -EAGAIN;
586 if ((go = adev->go))
587 timeout = dmabuf_timeout(dmap);
589 spin_unlock_irqrestore(&dmap->lock,flags);
590 timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
591 timeout);
592 if (!timeout) {
593 /* FIXME: include device name */
594 err = -EIO;
595 printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
596 dma_reset_input(dev);
597 } else
598 err = -EINTR;
599 spin_lock_irqsave(&dmap->lock,flags);
601 spin_unlock_irqrestore(&dmap->lock,flags);
603 if (dmap->qlen <= 0)
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];
608 return 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");*/
620 return -EINVAL;
622 else if (dmap->qlen <= 0)
623 return -EIO;
624 else if (p >= dmap->fragment_size) { /* This buffer is completely empty */
625 dmap->counts[dmap->qhead] = 0;
626 dmap->qlen--;
627 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
629 else dmap->counts[dmap->qhead] = p;
631 return 0;
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.
641 int pos;
642 unsigned long f;
644 if (!(dmap->flags & DMA_ACTIVE))
645 pos = 0;
646 else {
647 int chan = dmap->dma;
649 f=claim_dma_lock();
650 clear_dma_ff(chan);
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)
663 pos = 0;
664 } else {
665 if (dmap->qtail == 0)
666 if (pos > dmap->fragment_size)
667 pos = 0;
670 if (pos < 0)
671 pos = 0;
672 if (pos >= dmap->bytes_in_use)
673 pos = 0;
675 if(!isa_dma_bridge_buggy)
676 enable_dma(dmap->dma);
678 release_dma_lock(f);
680 /* printk( "%04x ", pos); */
682 return 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;
693 int dev;
695 for (dev = 0; dev < num_audiodevs; dev++) {
696 if (!(devmask & (1 << dev)))
697 continue;
698 if (!(adev = audio_devs[dev]))
699 continue;
700 if (adev->open_mode == 0)
701 continue;
702 if (adev->go)
703 continue;
704 /* OK to start the device */
705 adev->go = 1;
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];
714 int len, max, tmp;
715 struct dma_buffparms *dmap = adev->dmap_out;
716 int lim = dmap->nbufs;
718 if (lim < 2)
719 lim = 2;
721 if (dmap->qlen >= lim) /* No space at all */
722 return 0;
725 * Verify that there are no more pending buffers than the limit
726 * defined by the process.
729 max = dmap->max_fragments;
730 if (max > lim)
731 max = lim;
732 len = dmap->qlen;
734 if (adev->d->local_qlen) {
735 tmp = adev->d->local_qlen(dev);
736 if (tmp && len)
737 tmp--; /* This buffer has been counted twice */
738 len += tmp;
740 if (dmap->byte_counter % dmap->fragment_size) /* There is a partial fragment */
741 len = len + 1;
743 if (len >= max)
744 return 0;
745 return max - len;
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];
751 int err = 0;
752 struct dma_buffparms *dmap = adev->dmap_out;
753 long timeout;
754 long timeout_value;
756 if (dontblock)
757 return -EAGAIN;
758 if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
759 return -EAGAIN;
762 * Wait for free space
764 if (signal_pending(current))
765 return -EINTR;
766 timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
767 if (timeout)
768 timeout_value = dmabuf_timeout(dmap);
769 else
770 timeout_value = MAX_SCHEDULE_TIMEOUT;
771 timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
772 timeout_value);
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);
776 } else {
777 if (signal_pending(current))
778 err = -EINTR;
780 return err;
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;
788 long len, offs;
789 int maxfrags;
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)
794 return 0;
796 #ifdef BE_CONSERVATIVE
797 active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
798 #else
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)
802 active_offs = 0;
803 active_offs += dmap->byte_counter;
804 #endif
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);
810 return 0;
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;
818 if (len < 0) {
819 return 0;
821 if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
822 len = (maxfrags * dmap->fragment_size) - occupied_bytes;
823 *size = len & ~SAMPLE_ROUNDUP;
824 return (*size > 0);
826 /* acquires lock */
827 int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
829 struct audio_operations *adev = audio_devs[dev];
830 unsigned long flags;
831 int err = -EIO;
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");*/
836 return -EINVAL;
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);
844 DMAbuf_reset(dev);
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) {
852 return err;
854 spin_lock_irqsave(&dmap->lock,flags);
857 spin_unlock_irqrestore(&dmap->lock,flags);
858 return 0;
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;
865 unsigned long ptr;
866 unsigned long end_ptr, p;
867 int post;
868 unsigned long flags;
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;
875 dmap->cfrag = -1;
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;
895 dmap->qlen++;
896 ptr += dmap->fragment_size;
899 dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
902 * Let the low level driver perform some postprocessing to
903 * the written data.
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);
913 return 0;
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");
924 return 0;
926 if (dmap->dma < 0)
927 return 0;
928 sound_start_dma(dmap, physaddr, count, dma_mode);
929 return count;
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");
940 return 0;
942 if (dmap->flags & DMA_NODMA)
943 return 1;
944 if (dmap->dma < 0)
945 return 0;
946 sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
947 dmap->flags |= DMA_STARTED;
948 return count;
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;
965 int this_fragment;
967 if (dmap->raw_buf == NULL) {
968 printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
969 return;
971 if (dmap->mapping_flags & DMA_MAP_MAPPED) { /* Virtual memory mapped access */
972 /* mmapped 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);
989 return;
992 dmap->qlen--;
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
1010 * dmap->qlen < 0
1013 while (dmap->qlen <= -dmap->closing) {
1014 dmap->underrun_count++;
1015 dmap->qlen++;
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;
1024 if (dmap->qlen > 0)
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;
1038 unsigned long f;
1040 f=claim_dma_lock();
1042 if(!isa_dma_bridge_buggy)
1043 disable_dma(dmap->dma);
1044 clear_dma_ff(chan);
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)
1052 pos = 0;
1053 n = 0;
1054 while (dmap->qhead != pos && n++ < dmap->nbufs)
1055 do_outputintr(dev, notify_only);
1057 else
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");
1071 return;
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;
1084 dmap->qlen++;
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) {
1104 dmap->qlen++;
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;
1123 if (dmap->qlen > 0)
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;
1140 unsigned long f;
1142 f=claim_dma_lock();
1143 if(!isa_dma_bridge_buggy)
1144 disable_dma(dmap->dma);
1145 clear_dma_ff(chan);
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)
1153 pos = 0;
1155 n = 0;
1156 while (dmap->qtail != pos && ++n < dmap->nbufs)
1157 do_inputintr(dev);
1158 } else
1159 do_inputintr(dev);
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;
1179 } else {
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))
1204 return 0;
1205 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1206 if (dmap->qlen)
1207 return POLLIN | POLLRDNORM;
1208 return 0;
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);
1220 return 0;
1222 if (!dmap->qlen)
1223 return 0;
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))
1233 return 0;
1234 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1235 if (dmap->qlen)
1236 return POLLOUT | POLLWRNORM;
1237 return 0;
1239 if (dmap->dma_mode == DMODE_INPUT)
1240 return 0;
1241 if (dmap->dma_mode == DMODE_NONE)
1242 return POLLOUT | POLLWRNORM;
1243 if (!DMAbuf_space_in_queue(dev))
1244 return 0;
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 */
1259 if (!adev)
1260 return;
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);