Merge tag 'critical_fix_for_3.9' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6.git] / sound / oss / dmabuf.c
bloba59c88818f48a2dbf1f79e9ac4a627ee81f6cb26
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 = dma_map_single(NULL, start_addr, dmap->buffsize, DMA_BIDIRECTIONAL);
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 dma_unmap_single(NULL, dmap->raw_buf_phys, dmap->buffsize, DMA_BIDIRECTIONAL);
143 free_pages((unsigned long) dmap->raw_buf, sz);
144 dmap->raw_buf = NULL;
148 /* Intel version !!!!!!!!! */
150 static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
152 unsigned long flags;
153 int chan = dmap->dma;
155 /* printk( "Start DMA%d %d, %d\n", chan, (int)(physaddr-dmap->raw_buf_phys), count); */
157 flags = claim_dma_lock();
158 disable_dma(chan);
159 clear_dma_ff(chan);
160 set_dma_mode(chan, dma_mode);
161 set_dma_addr(chan, physaddr);
162 set_dma_count(chan, count);
163 enable_dma(chan);
164 release_dma_lock(flags);
166 return 0;
169 static void dma_init_buffers(struct dma_buffparms *dmap)
171 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
172 dmap->byte_counter = 0;
173 dmap->max_byte_counter = 8000 * 60 * 60;
174 dmap->bytes_in_use = dmap->buffsize;
176 dmap->dma_mode = DMODE_NONE;
177 dmap->mapping_flags = 0;
178 dmap->neutral_byte = 0x80;
179 dmap->data_rate = 8000;
180 dmap->cfrag = -1;
181 dmap->closing = 0;
182 dmap->nbufs = 1;
183 dmap->flags = DMA_BUSY; /* Other flags off */
186 static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
188 int err;
190 if (dmap->flags & DMA_BUSY)
191 return -EBUSY;
192 if ((err = sound_alloc_dmap(dmap)) < 0)
193 return err;
195 if (dmap->raw_buf == NULL) {
196 printk(KERN_WARNING "Sound: DMA buffers not available\n");
197 return -ENOSPC; /* Memory allocation failed during boot */
199 if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
200 printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
201 return -EBUSY;
203 dma_init_buffers(dmap);
204 spin_lock_init(&dmap->lock);
205 dmap->open_mode = mode;
206 dmap->subdivision = dmap->underrun_count = 0;
207 dmap->fragment_size = 0;
208 dmap->max_fragments = 65536; /* Just a large value */
209 dmap->byte_counter = 0;
210 dmap->max_byte_counter = 8000 * 60 * 60;
211 dmap->applic_profile = APF_NORMAL;
212 dmap->needs_reorg = 1;
213 dmap->audio_callback = NULL;
214 dmap->callback_parm = 0;
215 return 0;
218 static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
220 unsigned long flags;
222 if (dmap->dma >= 0) {
223 sound_close_dma(dmap->dma);
224 flags=claim_dma_lock();
225 disable_dma(dmap->dma);
226 release_dma_lock(flags);
228 if (dmap->flags & DMA_BUSY)
229 dmap->dma_mode = DMODE_NONE;
230 dmap->flags &= ~DMA_BUSY;
232 if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
233 sound_free_dmap(dmap);
237 static unsigned int default_set_bits(int dev, unsigned int bits)
239 mm_segment_t fs = get_fs();
241 set_fs(get_ds());
242 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
243 set_fs(fs);
244 return bits;
247 static int default_set_speed(int dev, int speed)
249 mm_segment_t fs = get_fs();
251 set_fs(get_ds());
252 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
253 set_fs(fs);
254 return speed;
257 static short default_set_channels(int dev, short channels)
259 int c = channels;
260 mm_segment_t fs = get_fs();
262 set_fs(get_ds());
263 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
264 set_fs(fs);
265 return c;
268 static void check_driver(struct audio_driver *d)
270 if (d->set_speed == NULL)
271 d->set_speed = default_set_speed;
272 if (d->set_bits == NULL)
273 d->set_bits = default_set_bits;
274 if (d->set_channels == NULL)
275 d->set_channels = default_set_channels;
278 int DMAbuf_open(int dev, int mode)
280 struct audio_operations *adev = audio_devs[dev];
281 int retval;
282 struct dma_buffparms *dmap_in = NULL;
283 struct dma_buffparms *dmap_out = NULL;
285 if (!adev)
286 return -ENXIO;
287 if (!(adev->flags & DMA_DUPLEX))
288 adev->dmap_in = adev->dmap_out;
289 check_driver(adev->d);
291 if ((retval = adev->d->open(dev, mode)) < 0)
292 return retval;
293 dmap_out = adev->dmap_out;
294 dmap_in = adev->dmap_in;
295 if (dmap_in == dmap_out)
296 adev->flags &= ~DMA_DUPLEX;
298 if (mode & OPEN_WRITE) {
299 if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
300 adev->d->close(dev);
301 return retval;
304 adev->enable_bits = mode;
306 if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
307 if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
308 adev->d->close(dev);
309 if (mode & OPEN_WRITE)
310 close_dmap(adev, dmap_out);
311 return retval;
314 adev->open_mode = mode;
315 adev->go = 1;
317 adev->d->set_bits(dev, 8);
318 adev->d->set_channels(dev, 1);
319 adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
320 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
321 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
322 adev->dmap_out->bytes_in_use);
323 return 0;
325 /* MUST not hold the spinlock */
326 void DMAbuf_reset(int dev)
328 if (audio_devs[dev]->open_mode & OPEN_WRITE)
329 dma_reset_output(dev);
331 if (audio_devs[dev]->open_mode & OPEN_READ)
332 dma_reset_input(dev);
335 static void dma_reset_output(int dev)
337 struct audio_operations *adev = audio_devs[dev];
338 unsigned long flags,f ;
339 struct dma_buffparms *dmap = adev->dmap_out;
341 if (!(dmap->flags & DMA_STARTED)) /* DMA is not active */
342 return;
345 * First wait until the current fragment has been played completely
347 spin_lock_irqsave(&dmap->lock,flags);
348 adev->dmap_out->flags |= DMA_SYNCING;
350 adev->dmap_out->underrun_count = 0;
351 if (!signal_pending(current) && adev->dmap_out->qlen &&
352 adev->dmap_out->underrun_count == 0){
353 spin_unlock_irqrestore(&dmap->lock,flags);
354 interruptible_sleep_on_timeout(&adev->out_sleeper,
355 dmabuf_timeout(dmap));
356 spin_lock_irqsave(&dmap->lock,flags);
358 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
361 * Finally shut the device off
363 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_output)
364 adev->d->halt_io(dev);
365 else
366 adev->d->halt_output(dev);
367 adev->dmap_out->flags &= ~DMA_STARTED;
369 f=claim_dma_lock();
370 clear_dma_ff(dmap->dma);
371 disable_dma(dmap->dma);
372 release_dma_lock(f);
374 dmap->byte_counter = 0;
375 reorganize_buffers(dev, adev->dmap_out, 0);
376 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
377 spin_unlock_irqrestore(&dmap->lock,flags);
380 static void dma_reset_input(int dev)
382 struct audio_operations *adev = audio_devs[dev];
383 unsigned long flags;
384 struct dma_buffparms *dmap = adev->dmap_in;
386 spin_lock_irqsave(&dmap->lock,flags);
387 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_input)
388 adev->d->halt_io(dev);
389 else
390 adev->d->halt_input(dev);
391 adev->dmap_in->flags &= ~DMA_STARTED;
393 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
394 dmap->byte_counter = 0;
395 reorganize_buffers(dev, adev->dmap_in, 1);
396 spin_unlock_irqrestore(&dmap->lock,flags);
398 /* MUST be called with holding the dmap->lock */
399 void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
401 struct audio_operations *adev = audio_devs[dev];
403 if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
404 return; /* Don't start DMA yet */
405 dmap->dma_mode = DMODE_OUTPUT;
407 if (!(dmap->flags & DMA_ACTIVE) || !(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
408 if (!(dmap->flags & DMA_STARTED)) {
409 reorganize_buffers(dev, dmap, 0);
410 if (adev->d->prepare_for_output(dev, dmap->fragment_size, dmap->nbufs))
411 return;
412 if (!(dmap->flags & DMA_NODMA))
413 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_WRITE);
414 dmap->flags |= DMA_STARTED;
416 if (dmap->counts[dmap->qhead] == 0)
417 dmap->counts[dmap->qhead] = dmap->fragment_size;
418 dmap->dma_mode = DMODE_OUTPUT;
419 adev->d->output_block(dev, dmap->raw_buf_phys + dmap->qhead * dmap->fragment_size,
420 dmap->counts[dmap->qhead], 1);
421 if (adev->d->trigger)
422 adev->d->trigger(dev,adev->enable_bits * adev->go);
424 dmap->flags |= DMA_ACTIVE;
427 int DMAbuf_sync(int dev)
429 struct audio_operations *adev = audio_devs[dev];
430 unsigned long flags;
431 int n = 0;
432 struct dma_buffparms *dmap;
434 if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
435 return 0;
437 if (adev->dmap_out->dma_mode == DMODE_OUTPUT) {
438 dmap = adev->dmap_out;
439 spin_lock_irqsave(&dmap->lock,flags);
440 if (dmap->qlen > 0 && !(dmap->flags & DMA_ACTIVE))
441 DMAbuf_launch_output(dev, dmap);
442 adev->dmap_out->flags |= DMA_SYNCING;
443 adev->dmap_out->underrun_count = 0;
444 while (!signal_pending(current) && n++ < adev->dmap_out->nbufs &&
445 adev->dmap_out->qlen && adev->dmap_out->underrun_count == 0) {
446 long t = dmabuf_timeout(dmap);
447 spin_unlock_irqrestore(&dmap->lock,flags);
448 /* FIXME: not safe may miss events */
449 t = interruptible_sleep_on_timeout(&adev->out_sleeper, t);
450 spin_lock_irqsave(&dmap->lock,flags);
451 if (!t) {
452 adev->dmap_out->flags &= ~DMA_SYNCING;
453 spin_unlock_irqrestore(&dmap->lock,flags);
454 return adev->dmap_out->qlen;
457 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
460 * Some devices such as GUS have huge amount of on board RAM for the
461 * audio data. We have to wait until the device has finished playing.
464 /* still holding the lock */
465 if (adev->d->local_qlen) { /* Device has hidden buffers */
466 while (!signal_pending(current) &&
467 adev->d->local_qlen(dev)){
468 spin_unlock_irqrestore(&dmap->lock,flags);
469 interruptible_sleep_on_timeout(&adev->out_sleeper,
470 dmabuf_timeout(dmap));
471 spin_lock_irqsave(&dmap->lock,flags);
474 spin_unlock_irqrestore(&dmap->lock,flags);
476 adev->dmap_out->dma_mode = DMODE_NONE;
477 return adev->dmap_out->qlen;
480 int DMAbuf_release(int dev, int mode)
482 struct audio_operations *adev = audio_devs[dev];
483 struct dma_buffparms *dmap;
484 unsigned long flags;
486 dmap = adev->dmap_out;
487 if (adev->open_mode & OPEN_WRITE)
488 adev->dmap_out->closing = 1;
490 if (adev->open_mode & OPEN_READ){
491 adev->dmap_in->closing = 1;
492 dmap = adev->dmap_in;
494 if (adev->open_mode & OPEN_WRITE)
495 if (!(adev->dmap_out->mapping_flags & DMA_MAP_MAPPED))
496 if (!signal_pending(current) && (adev->dmap_out->dma_mode == DMODE_OUTPUT))
497 DMAbuf_sync(dev);
498 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
499 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte, adev->dmap_out->bytes_in_use);
501 DMAbuf_reset(dev);
502 spin_lock_irqsave(&dmap->lock,flags);
503 adev->d->close(dev);
505 if (adev->open_mode & OPEN_WRITE)
506 close_dmap(adev, adev->dmap_out);
508 if (adev->open_mode == OPEN_READ ||
509 (adev->open_mode != OPEN_WRITE &&
510 (adev->flags & DMA_DUPLEX)))
511 close_dmap(adev, adev->dmap_in);
512 adev->open_mode = 0;
513 spin_unlock_irqrestore(&dmap->lock,flags);
514 return 0;
516 /* called with dmap->lock dold */
517 int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
519 struct audio_operations *adev = audio_devs[dev];
520 int err;
522 if (!(adev->open_mode & OPEN_READ))
523 return 0;
524 if (!(adev->enable_bits & PCM_ENABLE_INPUT))
525 return 0;
526 if (dmap->dma_mode == DMODE_OUTPUT) { /* Direction change */
527 /* release lock - it's not recursive */
528 spin_unlock_irq(&dmap->lock);
529 DMAbuf_sync(dev);
530 DMAbuf_reset(dev);
531 spin_lock_irq(&dmap->lock);
532 dmap->dma_mode = DMODE_NONE;
534 if (!dmap->dma_mode) {
535 reorganize_buffers(dev, dmap, 1);
536 if ((err = adev->d->prepare_for_input(dev,
537 dmap->fragment_size, dmap->nbufs)) < 0)
538 return err;
539 dmap->dma_mode = DMODE_INPUT;
541 if (!(dmap->flags & DMA_ACTIVE)) {
542 if (dmap->needs_reorg)
543 reorganize_buffers(dev, dmap, 0);
544 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
545 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
546 dmap->fragment_size, 0);
547 dmap->flags |= DMA_ACTIVE;
548 if (adev->d->trigger)
549 adev->d->trigger(dev, adev->enable_bits * adev->go);
551 return 0;
553 /* acquires lock */
554 int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
556 struct audio_operations *adev = audio_devs[dev];
557 unsigned long flags;
558 int err = 0, n = 0;
559 struct dma_buffparms *dmap = adev->dmap_in;
560 int go;
562 if (!(adev->open_mode & OPEN_READ))
563 return -EIO;
564 spin_lock_irqsave(&dmap->lock,flags);
565 if (dmap->needs_reorg)
566 reorganize_buffers(dev, dmap, 0);
567 if (adev->dmap_in->mapping_flags & DMA_MAP_MAPPED) {
568 /* printk(KERN_WARNING "Sound: Can't read from mmapped device (1)\n");*/
569 spin_unlock_irqrestore(&dmap->lock,flags);
570 return -EINVAL;
571 } else while (dmap->qlen <= 0 && n++ < 10) {
572 long timeout = MAX_SCHEDULE_TIMEOUT;
573 if (!(adev->enable_bits & PCM_ENABLE_INPUT) || !adev->go) {
574 spin_unlock_irqrestore(&dmap->lock,flags);
575 return -EAGAIN;
577 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
578 spin_unlock_irqrestore(&dmap->lock,flags);
579 return err;
581 /* Wait for the next block */
583 if (dontblock) {
584 spin_unlock_irqrestore(&dmap->lock,flags);
585 return -EAGAIN;
587 if ((go = adev->go))
588 timeout = dmabuf_timeout(dmap);
590 spin_unlock_irqrestore(&dmap->lock,flags);
591 timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
592 timeout);
593 if (!timeout) {
594 /* FIXME: include device name */
595 err = -EIO;
596 printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
597 dma_reset_input(dev);
598 } else
599 err = -EINTR;
600 spin_lock_irqsave(&dmap->lock,flags);
602 spin_unlock_irqrestore(&dmap->lock,flags);
604 if (dmap->qlen <= 0)
605 return err ? err : -EINTR;
606 *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
607 *len = dmap->fragment_size - dmap->counts[dmap->qhead];
609 return dmap->qhead;
612 int DMAbuf_rmchars(int dev, int buff_no, int c)
614 struct audio_operations *adev = audio_devs[dev];
615 struct dma_buffparms *dmap = adev->dmap_in;
616 int p = dmap->counts[dmap->qhead] + c;
618 if (dmap->mapping_flags & DMA_MAP_MAPPED)
620 /* printk("Sound: Can't read from mmapped device (2)\n");*/
621 return -EINVAL;
623 else if (dmap->qlen <= 0)
624 return -EIO;
625 else if (p >= dmap->fragment_size) { /* This buffer is completely empty */
626 dmap->counts[dmap->qhead] = 0;
627 dmap->qlen--;
628 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
630 else dmap->counts[dmap->qhead] = p;
632 return 0;
634 /* MUST be called with dmap->lock hold */
635 int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
638 * Try to approximate the active byte position of the DMA pointer within the
639 * buffer area as well as possible.
642 int pos;
643 unsigned long f;
645 if (!(dmap->flags & DMA_ACTIVE))
646 pos = 0;
647 else {
648 int chan = dmap->dma;
650 f=claim_dma_lock();
651 clear_dma_ff(chan);
653 if(!isa_dma_bridge_buggy)
654 disable_dma(dmap->dma);
656 pos = get_dma_residue(chan);
658 pos = dmap->bytes_in_use - pos;
660 if (!(dmap->mapping_flags & DMA_MAP_MAPPED)) {
661 if (direction == DMODE_OUTPUT) {
662 if (dmap->qhead == 0)
663 if (pos > dmap->fragment_size)
664 pos = 0;
665 } else {
666 if (dmap->qtail == 0)
667 if (pos > dmap->fragment_size)
668 pos = 0;
671 if (pos < 0)
672 pos = 0;
673 if (pos >= dmap->bytes_in_use)
674 pos = 0;
676 if(!isa_dma_bridge_buggy)
677 enable_dma(dmap->dma);
679 release_dma_lock(f);
681 /* printk( "%04x ", pos); */
683 return pos;
687 * DMAbuf_start_devices() is called by the /dev/music driver to start
688 * one or more audio devices at desired moment.
691 void DMAbuf_start_devices(unsigned int devmask)
693 struct audio_operations *adev;
694 int dev;
696 for (dev = 0; dev < num_audiodevs; dev++) {
697 if (!(devmask & (1 << dev)))
698 continue;
699 if (!(adev = audio_devs[dev]))
700 continue;
701 if (adev->open_mode == 0)
702 continue;
703 if (adev->go)
704 continue;
705 /* OK to start the device */
706 adev->go = 1;
707 if (adev->d->trigger)
708 adev->d->trigger(dev,adev->enable_bits * adev->go);
711 /* via poll called without a lock ?*/
712 int DMAbuf_space_in_queue(int dev)
714 struct audio_operations *adev = audio_devs[dev];
715 int len, max, tmp;
716 struct dma_buffparms *dmap = adev->dmap_out;
717 int lim = dmap->nbufs;
719 if (lim < 2)
720 lim = 2;
722 if (dmap->qlen >= lim) /* No space at all */
723 return 0;
726 * Verify that there are no more pending buffers than the limit
727 * defined by the process.
730 max = dmap->max_fragments;
731 if (max > lim)
732 max = lim;
733 len = dmap->qlen;
735 if (adev->d->local_qlen) {
736 tmp = adev->d->local_qlen(dev);
737 if (tmp && len)
738 tmp--; /* This buffer has been counted twice */
739 len += tmp;
741 if (dmap->byte_counter % dmap->fragment_size) /* There is a partial fragment */
742 len = len + 1;
744 if (len >= max)
745 return 0;
746 return max - len;
748 /* MUST not hold the spinlock - this function may sleep */
749 static int output_sleep(int dev, int dontblock)
751 struct audio_operations *adev = audio_devs[dev];
752 int err = 0;
753 struct dma_buffparms *dmap = adev->dmap_out;
754 long timeout;
755 long timeout_value;
757 if (dontblock)
758 return -EAGAIN;
759 if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
760 return -EAGAIN;
763 * Wait for free space
765 if (signal_pending(current))
766 return -EINTR;
767 timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
768 if (timeout)
769 timeout_value = dmabuf_timeout(dmap);
770 else
771 timeout_value = MAX_SCHEDULE_TIMEOUT;
772 timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
773 timeout_value);
774 if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
775 printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
776 dma_reset_output(dev);
777 } else {
778 if (signal_pending(current))
779 err = -EINTR;
781 return err;
783 /* called with the lock held */
784 static int find_output_space(int dev, char **buf, int *size)
786 struct audio_operations *adev = audio_devs[dev];
787 struct dma_buffparms *dmap = adev->dmap_out;
788 unsigned long active_offs;
789 long len, offs;
790 int maxfrags;
791 int occupied_bytes = (dmap->user_counter % dmap->fragment_size);
793 *buf = dmap->raw_buf;
794 if (!(maxfrags = DMAbuf_space_in_queue(dev)) && !occupied_bytes)
795 return 0;
797 #ifdef BE_CONSERVATIVE
798 active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
799 #else
800 active_offs = max(DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT), 0);
801 /* Check for pointer wrapping situation */
802 if (active_offs >= dmap->bytes_in_use)
803 active_offs = 0;
804 active_offs += dmap->byte_counter;
805 #endif
807 offs = (dmap->user_counter % dmap->bytes_in_use) & ~SAMPLE_ROUNDUP;
808 if (offs < 0 || offs >= dmap->bytes_in_use) {
809 printk(KERN_ERR "Sound: Got unexpected offs %ld. Giving up.\n", offs);
810 printk("Counter = %ld, bytes=%d\n", dmap->user_counter, dmap->bytes_in_use);
811 return 0;
813 *buf = dmap->raw_buf + offs;
815 len = active_offs + dmap->bytes_in_use - dmap->user_counter; /* Number of unused bytes in buffer */
817 if ((offs + len) > dmap->bytes_in_use)
818 len = dmap->bytes_in_use - offs;
819 if (len < 0) {
820 return 0;
822 if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
823 len = (maxfrags * dmap->fragment_size) - occupied_bytes;
824 *size = len & ~SAMPLE_ROUNDUP;
825 return (*size > 0);
827 /* acquires lock */
828 int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
830 struct audio_operations *adev = audio_devs[dev];
831 unsigned long flags;
832 int err = -EIO;
833 struct dma_buffparms *dmap = adev->dmap_out;
835 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
836 /* printk(KERN_DEBUG "Sound: Can't write to mmapped device (3)\n");*/
837 return -EINVAL;
839 spin_lock_irqsave(&dmap->lock,flags);
840 if (dmap->needs_reorg)
841 reorganize_buffers(dev, dmap, 0);
843 if (dmap->dma_mode == DMODE_INPUT) { /* Direction change */
844 spin_unlock_irqrestore(&dmap->lock,flags);
845 DMAbuf_reset(dev);
846 spin_lock_irqsave(&dmap->lock,flags);
848 dmap->dma_mode = DMODE_OUTPUT;
850 while (find_output_space(dev, buf, size) <= 0) {
851 spin_unlock_irqrestore(&dmap->lock,flags);
852 if ((err = output_sleep(dev, dontblock)) < 0) {
853 return err;
855 spin_lock_irqsave(&dmap->lock,flags);
858 spin_unlock_irqrestore(&dmap->lock,flags);
859 return 0;
861 /* has to acquire dmap->lock */
862 int DMAbuf_move_wrpointer(int dev, int l)
864 struct audio_operations *adev = audio_devs[dev];
865 struct dma_buffparms *dmap = adev->dmap_out;
866 unsigned long ptr;
867 unsigned long end_ptr, p;
868 int post;
869 unsigned long flags;
871 spin_lock_irqsave(&dmap->lock,flags);
872 post= (dmap->flags & DMA_POST);
873 ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
875 dmap->flags &= ~DMA_POST;
876 dmap->cfrag = -1;
877 dmap->user_counter += l;
878 dmap->flags |= DMA_DIRTY;
880 if (dmap->byte_counter >= dmap->max_byte_counter) {
881 /* Wrap the byte counters */
882 long decr = dmap->byte_counter;
883 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
884 decr -= dmap->byte_counter;
885 dmap->user_counter -= decr;
887 end_ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
889 p = (dmap->user_counter - 1) % dmap->bytes_in_use;
890 dmap->neutral_byte = dmap->raw_buf[p];
892 /* Update the fragment based bookkeeping too */
893 while (ptr < end_ptr) {
894 dmap->counts[dmap->qtail] = dmap->fragment_size;
895 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
896 dmap->qlen++;
897 ptr += dmap->fragment_size;
900 dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
903 * Let the low level driver perform some postprocessing to
904 * the written data.
906 if (adev->d->postprocess_write)
907 adev->d->postprocess_write(dev);
909 if (!(dmap->flags & DMA_ACTIVE))
910 if (dmap->qlen > 1 || (dmap->qlen > 0 && (post || dmap->qlen >= dmap->nbufs - 1)))
911 DMAbuf_launch_output(dev, dmap);
913 spin_unlock_irqrestore(&dmap->lock,flags);
914 return 0;
917 int DMAbuf_start_dma(int dev, unsigned long physaddr, int count, int dma_mode)
919 struct audio_operations *adev = audio_devs[dev];
920 struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
922 if (dmap->raw_buf == NULL) {
923 printk(KERN_ERR "sound: DMA buffer(1) == NULL\n");
924 printk("Device %d, chn=%s\n", dev, (dmap == adev->dmap_out) ? "out" : "in");
925 return 0;
927 if (dmap->dma < 0)
928 return 0;
929 sound_start_dma(dmap, physaddr, count, dma_mode);
930 return count;
932 EXPORT_SYMBOL(DMAbuf_start_dma);
934 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode)
936 struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
938 if (dmap->raw_buf == NULL) {
939 printk(KERN_ERR "sound: DMA buffer(2) == NULL\n");
940 printk(KERN_ERR "Device %s, chn=%s\n", adev->name, (dmap == adev->dmap_out) ? "out" : "in");
941 return 0;
943 if (dmap->flags & DMA_NODMA)
944 return 1;
945 if (dmap->dma < 0)
946 return 0;
947 sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
948 dmap->flags |= DMA_STARTED;
949 return count;
952 static void finish_output_interrupt(int dev, struct dma_buffparms *dmap)
954 struct audio_operations *adev = audio_devs[dev];
956 if (dmap->audio_callback != NULL)
957 dmap->audio_callback(dev, dmap->callback_parm);
958 wake_up(&adev->out_sleeper);
959 wake_up(&adev->poll_sleeper);
961 /* called with dmap->lock held in irq context*/
962 static void do_outputintr(int dev, int dummy)
964 struct audio_operations *adev = audio_devs[dev];
965 struct dma_buffparms *dmap = adev->dmap_out;
966 int this_fragment;
968 if (dmap->raw_buf == NULL) {
969 printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
970 return;
972 if (dmap->mapping_flags & DMA_MAP_MAPPED) { /* Virtual memory mapped access */
973 /* mmapped access */
974 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
975 if (dmap->qhead == 0) { /* Wrapped */
976 dmap->byte_counter += dmap->bytes_in_use;
977 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
978 long decr = dmap->byte_counter;
979 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
980 decr -= dmap->byte_counter;
981 dmap->user_counter -= decr;
984 dmap->qlen++; /* Yes increment it (don't decrement) */
985 if (!(adev->flags & DMA_AUTOMODE))
986 dmap->flags &= ~DMA_ACTIVE;
987 dmap->counts[dmap->qhead] = dmap->fragment_size;
988 DMAbuf_launch_output(dev, dmap);
989 finish_output_interrupt(dev, dmap);
990 return;
993 dmap->qlen--;
994 this_fragment = dmap->qhead;
995 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
997 if (dmap->qhead == 0) { /* Wrapped */
998 dmap->byte_counter += dmap->bytes_in_use;
999 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
1000 long decr = dmap->byte_counter;
1001 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
1002 decr -= dmap->byte_counter;
1003 dmap->user_counter -= decr;
1006 if (!(adev->flags & DMA_AUTOMODE))
1007 dmap->flags &= ~DMA_ACTIVE;
1010 * This is dmap->qlen <= 0 except when closing when
1011 * dmap->qlen < 0
1014 while (dmap->qlen <= -dmap->closing) {
1015 dmap->underrun_count++;
1016 dmap->qlen++;
1017 if ((dmap->flags & DMA_DIRTY) && dmap->applic_profile != APF_CPUINTENS) {
1018 dmap->flags &= ~DMA_DIRTY;
1019 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
1020 adev->dmap_out->buffsize);
1022 dmap->user_counter += dmap->fragment_size;
1023 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1025 if (dmap->qlen > 0)
1026 DMAbuf_launch_output(dev, dmap);
1027 finish_output_interrupt(dev, dmap);
1029 /* called in irq context */
1030 void DMAbuf_outputintr(int dev, int notify_only)
1032 struct audio_operations *adev = audio_devs[dev];
1033 unsigned long flags;
1034 struct dma_buffparms *dmap = adev->dmap_out;
1036 spin_lock_irqsave(&dmap->lock,flags);
1037 if (!(dmap->flags & DMA_NODMA)) {
1038 int chan = dmap->dma, pos, n;
1039 unsigned long f;
1041 f=claim_dma_lock();
1043 if(!isa_dma_bridge_buggy)
1044 disable_dma(dmap->dma);
1045 clear_dma_ff(chan);
1046 pos = dmap->bytes_in_use - get_dma_residue(chan);
1047 if(!isa_dma_bridge_buggy)
1048 enable_dma(dmap->dma);
1049 release_dma_lock(f);
1051 pos = pos / dmap->fragment_size; /* Actual qhead */
1052 if (pos < 0 || pos >= dmap->nbufs)
1053 pos = 0;
1054 n = 0;
1055 while (dmap->qhead != pos && n++ < dmap->nbufs)
1056 do_outputintr(dev, notify_only);
1058 else
1059 do_outputintr(dev, notify_only);
1060 spin_unlock_irqrestore(&dmap->lock,flags);
1062 EXPORT_SYMBOL(DMAbuf_outputintr);
1064 /* called with dmap->lock held in irq context */
1065 static void do_inputintr(int dev)
1067 struct audio_operations *adev = audio_devs[dev];
1068 struct dma_buffparms *dmap = adev->dmap_in;
1070 if (dmap->raw_buf == NULL) {
1071 printk(KERN_ERR "Sound: Fatal error. Audio interrupt after freeing buffers.\n");
1072 return;
1074 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1075 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1076 if (dmap->qtail == 0) { /* Wrapped */
1077 dmap->byte_counter += dmap->bytes_in_use;
1078 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
1079 long decr = dmap->byte_counter;
1080 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1081 decr -= dmap->byte_counter;
1082 dmap->user_counter -= decr;
1085 dmap->qlen++;
1087 if (!(adev->flags & DMA_AUTOMODE)) {
1088 if (dmap->needs_reorg)
1089 reorganize_buffers(dev, dmap, 0);
1090 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_READ);
1091 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
1092 dmap->fragment_size, 1);
1093 if (adev->d->trigger)
1094 adev->d->trigger(dev, adev->enable_bits * adev->go);
1096 dmap->flags |= DMA_ACTIVE;
1097 } else if (dmap->qlen >= (dmap->nbufs - 1)) {
1098 printk(KERN_WARNING "Sound: Recording overrun\n");
1099 dmap->underrun_count++;
1101 /* Just throw away the oldest fragment but keep the engine running */
1102 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1103 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1104 } else if (dmap->qlen >= 0 && dmap->qlen < dmap->nbufs) {
1105 dmap->qlen++;
1106 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1107 if (dmap->qtail == 0) { /* Wrapped */
1108 dmap->byte_counter += dmap->bytes_in_use;
1109 if (dmap->byte_counter >= dmap->max_byte_counter) { /* Overflow */
1110 long decr = dmap->byte_counter;
1111 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1112 decr -= dmap->byte_counter;
1113 dmap->user_counter -= decr;
1117 if (!(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
1118 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
1119 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size, dmap->fragment_size, 1);
1120 if (adev->d->trigger)
1121 adev->d->trigger(dev,adev->enable_bits * adev->go);
1123 dmap->flags |= DMA_ACTIVE;
1124 if (dmap->qlen > 0)
1126 wake_up(&adev->in_sleeper);
1127 wake_up(&adev->poll_sleeper);
1130 /* called in irq context */
1131 void DMAbuf_inputintr(int dev)
1133 struct audio_operations *adev = audio_devs[dev];
1134 struct dma_buffparms *dmap = adev->dmap_in;
1135 unsigned long flags;
1137 spin_lock_irqsave(&dmap->lock,flags);
1139 if (!(dmap->flags & DMA_NODMA)) {
1140 int chan = dmap->dma, pos, n;
1141 unsigned long f;
1143 f=claim_dma_lock();
1144 if(!isa_dma_bridge_buggy)
1145 disable_dma(dmap->dma);
1146 clear_dma_ff(chan);
1147 pos = dmap->bytes_in_use - get_dma_residue(chan);
1148 if(!isa_dma_bridge_buggy)
1149 enable_dma(dmap->dma);
1150 release_dma_lock(f);
1152 pos = pos / dmap->fragment_size; /* Actual qhead */
1153 if (pos < 0 || pos >= dmap->nbufs)
1154 pos = 0;
1156 n = 0;
1157 while (dmap->qtail != pos && ++n < dmap->nbufs)
1158 do_inputintr(dev);
1159 } else
1160 do_inputintr(dev);
1161 spin_unlock_irqrestore(&dmap->lock,flags);
1163 EXPORT_SYMBOL(DMAbuf_inputintr);
1165 void DMAbuf_init(int dev, int dma1, int dma2)
1167 struct audio_operations *adev = audio_devs[dev];
1169 * NOTE! This routine could be called several times.
1172 if (adev && adev->dmap_out == NULL) {
1173 if (adev->d == NULL)
1174 panic("OSS: audio_devs[%d]->d == NULL\n", dev);
1176 if (adev->parent_dev) { /* Use DMA map of the parent dev */
1177 int parent = adev->parent_dev - 1;
1178 adev->dmap_out = audio_devs[parent]->dmap_out;
1179 adev->dmap_in = audio_devs[parent]->dmap_in;
1180 } else {
1181 adev->dmap_out = adev->dmap_in = &adev->dmaps[0];
1182 adev->dmap_out->dma = dma1;
1183 if (adev->flags & DMA_DUPLEX) {
1184 adev->dmap_in = &adev->dmaps[1];
1185 adev->dmap_in->dma = dma2;
1188 /* Persistent DMA buffers allocated here */
1189 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1190 if (adev->dmap_in->raw_buf == NULL)
1191 sound_alloc_dmap(adev->dmap_in);
1192 if (adev->dmap_out->raw_buf == NULL)
1193 sound_alloc_dmap(adev->dmap_out);
1198 /* No kernel lock - DMAbuf_activate_recording protected by global cli/sti */
1199 static unsigned int poll_input(struct file * file, int dev, poll_table *wait)
1201 struct audio_operations *adev = audio_devs[dev];
1202 struct dma_buffparms *dmap = adev->dmap_in;
1204 if (!(adev->open_mode & OPEN_READ))
1205 return 0;
1206 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1207 if (dmap->qlen)
1208 return POLLIN | POLLRDNORM;
1209 return 0;
1211 if (dmap->dma_mode != DMODE_INPUT) {
1212 if (dmap->dma_mode == DMODE_NONE &&
1213 adev->enable_bits & PCM_ENABLE_INPUT &&
1214 !dmap->qlen && adev->go) {
1215 unsigned long flags;
1217 spin_lock_irqsave(&dmap->lock,flags);
1218 DMAbuf_activate_recording(dev, dmap);
1219 spin_unlock_irqrestore(&dmap->lock,flags);
1221 return 0;
1223 if (!dmap->qlen)
1224 return 0;
1225 return POLLIN | POLLRDNORM;
1228 static unsigned int poll_output(struct file * file, int dev, poll_table *wait)
1230 struct audio_operations *adev = audio_devs[dev];
1231 struct dma_buffparms *dmap = adev->dmap_out;
1233 if (!(adev->open_mode & OPEN_WRITE))
1234 return 0;
1235 if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1236 if (dmap->qlen)
1237 return POLLOUT | POLLWRNORM;
1238 return 0;
1240 if (dmap->dma_mode == DMODE_INPUT)
1241 return 0;
1242 if (dmap->dma_mode == DMODE_NONE)
1243 return POLLOUT | POLLWRNORM;
1244 if (!DMAbuf_space_in_queue(dev))
1245 return 0;
1246 return POLLOUT | POLLWRNORM;
1249 unsigned int DMAbuf_poll(struct file * file, int dev, poll_table *wait)
1251 struct audio_operations *adev = audio_devs[dev];
1252 poll_wait(file, &adev->poll_sleeper, wait);
1253 return poll_input(file, dev, wait) | poll_output(file, dev, wait);
1256 void DMAbuf_deinit(int dev)
1258 struct audio_operations *adev = audio_devs[dev];
1259 /* This routine is called when driver is being unloaded */
1260 if (!adev)
1261 return;
1263 /* Persistent DMA buffers deallocated here */
1264 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1265 sound_free_dmap(adev->dmap_out);
1266 if (adev->flags & DMA_DUPLEX)
1267 sound_free_dmap(adev->dmap_in);