Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetoot...
[firefly-linux-kernel-4.4.55.git] / sound / oss / dmabuf.c
1 /*
2  * sound/oss/dmabuf.c
3  *
4  * The DMA buffer manager for digitized voice applications
5  */
6 /*
7  * Copyright (C) by Hannu Savolainen 1993-1997
8  *
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.
12  *
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.
21  *
22  * Rob Riggs            Added persistent DMA buffers (1998/10/17)
23  */
24
25 #define BE_CONSERVATIVE
26 #define SAMPLE_ROUNDUP 0
27
28 #include <linux/mm.h>
29 #include <linux/gfp.h>
30 #include "sound_config.h"
31 #include "sleep.h"
32
33 #define DMAP_FREE_ON_CLOSE      0
34 #define DMAP_KEEP_ON_CLOSE      1
35 extern int sound_dmap_flag;
36
37 static void dma_reset_output(int dev);
38 static void dma_reset_input(int dev);
39 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode);
40
41
42
43 static int debugmem;            /* switched off by default */
44 static int dma_buffsize = DSP_BUFFSIZE;
45
46 static long dmabuf_timeout(struct dma_buffparms *dmap)
47 {
48         long tmout;
49
50         tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
51         tmout += HZ / 5;        /* Some safety distance */
52         if (tmout < (HZ / 2))
53                 tmout = HZ / 2;
54         if (tmout > 20 * HZ)
55                 tmout = 20 * HZ;
56         return tmout;
57 }
58
59 static int sound_alloc_dmap(struct dma_buffparms *dmap)
60 {
61         char *start_addr, *end_addr;
62         int dma_pagesize;
63         int sz, size;
64         struct page *page;
65
66         dmap->mapping_flags &= ~DMA_MAP_MAPPED;
67
68         if (dmap->raw_buf != NULL)
69                 return 0;       /* Already done */
70         if (dma_buffsize < 4096)
71                 dma_buffsize = 4096;
72         dma_pagesize = (dmap->dma < 4) ? (64 * 1024) : (128 * 1024);
73         
74         /*
75          *      Now check for the Cyrix problem.
76          */
77          
78         if(isa_dma_bridge_buggy==2)
79                 dma_pagesize=32768;
80          
81         dmap->raw_buf = NULL;
82         dmap->buffsize = dma_buffsize;
83         if (dmap->buffsize > dma_pagesize)
84                 dmap->buffsize = dma_pagesize;
85         start_addr = NULL;
86         /*
87          * Now loop until we get a free buffer. Try to get smaller buffer if
88          * it fails. Don't accept smaller than 8k buffer for performance
89          * reasons.
90          */
91         while (start_addr == NULL && dmap->buffsize > PAGE_SIZE) {
92                 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
93                 dmap->buffsize = PAGE_SIZE * (1 << sz);
94                 start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA|__GFP_NOWARN, sz);
95                 if (start_addr == NULL)
96                         dmap->buffsize /= 2;
97         }
98
99         if (start_addr == NULL) {
100                 printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
101                 return -ENOMEM;
102         } else {
103                 /* make some checks */
104                 end_addr = start_addr + dmap->buffsize - 1;
105
106                 if (debugmem)
107                         printk(KERN_DEBUG "sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr);
108                 
109                 /* now check if it fits into the same dma-pagesize */
110
111                 if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
112                     || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
113                         printk(KERN_ERR "sound: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, dmap->buffsize);
114                         return -EFAULT;
115                 }
116         }
117         dmap->raw_buf = start_addr;
118         dmap->raw_buf_phys = dma_map_single(NULL, start_addr, dmap->buffsize, DMA_BIDIRECTIONAL);
119
120         for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
121                 SetPageReserved(page);
122         return 0;
123 }
124
125 static void sound_free_dmap(struct dma_buffparms *dmap)
126 {
127         int sz, size;
128         struct page *page;
129         unsigned long start_addr, end_addr;
130
131         if (dmap->raw_buf == NULL)
132                 return;
133         if (dmap->mapping_flags & DMA_MAP_MAPPED)
134                 return;         /* Don't free mmapped buffer. Will use it next time */
135         for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
136
137         start_addr = (unsigned long) dmap->raw_buf;
138         end_addr = start_addr + dmap->buffsize;
139
140         for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
141                 ClearPageReserved(page);
142
143         dma_unmap_single(NULL, dmap->raw_buf_phys, dmap->buffsize, DMA_BIDIRECTIONAL);
144         free_pages((unsigned long) dmap->raw_buf, sz);
145         dmap->raw_buf = NULL;
146 }
147
148
149 /* Intel version !!!!!!!!! */
150
151 static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
152 {
153         unsigned long flags;
154         int chan = dmap->dma;
155
156         /* printk( "Start DMA%d %d, %d\n",  chan,  (int)(physaddr-dmap->raw_buf_phys),  count); */
157
158         flags = claim_dma_lock();
159         disable_dma(chan);
160         clear_dma_ff(chan);
161         set_dma_mode(chan, dma_mode);
162         set_dma_addr(chan, physaddr);
163         set_dma_count(chan, count);
164         enable_dma(chan);
165         release_dma_lock(flags);
166
167         return 0;
168 }
169
170 static void dma_init_buffers(struct dma_buffparms *dmap)
171 {
172         dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
173         dmap->byte_counter = 0;
174         dmap->max_byte_counter = 8000 * 60 * 60;
175         dmap->bytes_in_use = dmap->buffsize;
176
177         dmap->dma_mode = DMODE_NONE;
178         dmap->mapping_flags = 0;
179         dmap->neutral_byte = 0x80;
180         dmap->data_rate = 8000;
181         dmap->cfrag = -1;
182         dmap->closing = 0;
183         dmap->nbufs = 1;
184         dmap->flags = DMA_BUSY; /* Other flags off */
185 }
186
187 static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
188 {
189         int err;
190         
191         if (dmap->flags & DMA_BUSY)
192                 return -EBUSY;
193         if ((err = sound_alloc_dmap(dmap)) < 0)
194                 return err;
195
196         if (dmap->raw_buf == NULL) {
197                 printk(KERN_WARNING "Sound: DMA buffers not available\n");
198                 return -ENOSPC; /* Memory allocation failed during boot */
199         }
200         if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
201                 printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
202                 return -EBUSY;
203         }
204         dma_init_buffers(dmap);
205         spin_lock_init(&dmap->lock);
206         dmap->open_mode = mode;
207         dmap->subdivision = dmap->underrun_count = 0;
208         dmap->fragment_size = 0;
209         dmap->max_fragments = 65536;    /* Just a large value */
210         dmap->byte_counter = 0;
211         dmap->max_byte_counter = 8000 * 60 * 60;
212         dmap->applic_profile = APF_NORMAL;
213         dmap->needs_reorg = 1;
214         dmap->audio_callback = NULL;
215         dmap->callback_parm = 0;
216         return 0;
217 }
218
219 static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
220 {
221         unsigned long flags;
222         
223         if (dmap->dma >= 0) {
224                 sound_close_dma(dmap->dma);
225                 flags=claim_dma_lock();
226                 disable_dma(dmap->dma);
227                 release_dma_lock(flags);
228         }
229         if (dmap->flags & DMA_BUSY)
230                 dmap->dma_mode = DMODE_NONE;
231         dmap->flags &= ~DMA_BUSY;
232         
233         if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
234                 sound_free_dmap(dmap);
235 }
236
237
238 static unsigned int default_set_bits(int dev, unsigned int bits)
239 {
240         mm_segment_t fs = get_fs();
241
242         set_fs(get_ds());
243         audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
244         set_fs(fs);
245         return bits;
246 }
247
248 static int default_set_speed(int dev, int speed)
249 {
250         mm_segment_t fs = get_fs();
251
252         set_fs(get_ds());
253         audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
254         set_fs(fs);
255         return speed;
256 }
257
258 static short default_set_channels(int dev, short channels)
259 {
260         int c = channels;
261         mm_segment_t fs = get_fs();
262
263         set_fs(get_ds());
264         audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
265         set_fs(fs);
266         return c;
267 }
268
269 static void check_driver(struct audio_driver *d)
270 {
271         if (d->set_speed == NULL)
272                 d->set_speed = default_set_speed;
273         if (d->set_bits == NULL)
274                 d->set_bits = default_set_bits;
275         if (d->set_channels == NULL)
276                 d->set_channels = default_set_channels;
277 }
278
279 int DMAbuf_open(int dev, int mode)
280 {
281         struct audio_operations *adev = audio_devs[dev];
282         int retval;
283         struct dma_buffparms *dmap_in = NULL;
284         struct dma_buffparms *dmap_out = NULL;
285
286         if (!adev)
287                   return -ENXIO;
288         if (!(adev->flags & DMA_DUPLEX))
289                 adev->dmap_in = adev->dmap_out;
290         check_driver(adev->d);
291
292         if ((retval = adev->d->open(dev, mode)) < 0)
293                 return retval;
294         dmap_out = adev->dmap_out;
295         dmap_in = adev->dmap_in;
296         if (dmap_in == dmap_out)
297                 adev->flags &= ~DMA_DUPLEX;
298
299         if (mode & OPEN_WRITE) {
300                 if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
301                         adev->d->close(dev);
302                         return retval;
303                 }
304         }
305         adev->enable_bits = mode;
306
307         if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
308                 if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
309                         adev->d->close(dev);
310                         if (mode & OPEN_WRITE)
311                                 close_dmap(adev, dmap_out);
312                         return retval;
313                 }
314         }
315         adev->open_mode = mode;
316         adev->go = 1;
317
318         adev->d->set_bits(dev, 8);
319         adev->d->set_channels(dev, 1);
320         adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
321         if (adev->dmap_out->dma_mode == DMODE_OUTPUT) 
322                 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
323                        adev->dmap_out->bytes_in_use);
324         return 0;
325 }
326 /* MUST not hold the spinlock */
327 void DMAbuf_reset(int dev)
328 {
329         if (audio_devs[dev]->open_mode & OPEN_WRITE)
330                 dma_reset_output(dev);
331
332         if (audio_devs[dev]->open_mode & OPEN_READ)
333                 dma_reset_input(dev);
334 }
335
336 static void dma_reset_output(int dev)
337 {
338         struct audio_operations *adev = audio_devs[dev];
339         unsigned long flags,f ;
340         struct dma_buffparms *dmap = adev->dmap_out;
341
342         if (!(dmap->flags & DMA_STARTED))       /* DMA is not active */
343                 return;
344
345         /*
346          *      First wait until the current fragment has been played completely
347          */
348         spin_lock_irqsave(&dmap->lock,flags);
349         adev->dmap_out->flags |= DMA_SYNCING;
350
351         adev->dmap_out->underrun_count = 0;
352         if (!signal_pending(current) && adev->dmap_out->qlen && 
353             adev->dmap_out->underrun_count == 0){
354                 spin_unlock_irqrestore(&dmap->lock,flags);
355                 oss_broken_sleep_on(&adev->out_sleeper, dmabuf_timeout(dmap));
356                 spin_lock_irqsave(&dmap->lock,flags);
357         }
358         adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
359
360         /*
361          *      Finally shut the device off
362          */
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;
368         
369         f=claim_dma_lock();
370         clear_dma_ff(dmap->dma);
371         disable_dma(dmap->dma);
372         release_dma_lock(f);
373         
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);
378 }
379
380 static void dma_reset_input(int dev)
381 {
382         struct audio_operations *adev = audio_devs[dev];
383         unsigned long flags;
384         struct dma_buffparms *dmap = adev->dmap_in;
385
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;
392
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);
397 }
398 /* MUST be called with holding the dmap->lock */
399 void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
400 {
401         struct audio_operations *adev = audio_devs[dev];
402
403         if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
404                 return;         /* Don't start DMA yet */
405         dmap->dma_mode = DMODE_OUTPUT;
406
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;
415                 }
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);
423         }
424         dmap->flags |= DMA_ACTIVE;
425 }
426
427 int DMAbuf_sync(int dev)
428 {
429         struct audio_operations *adev = audio_devs[dev];
430         unsigned long flags;
431         int n = 0;
432         struct dma_buffparms *dmap;
433
434         if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
435                 return 0;
436
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 = oss_broken_sleep_on(&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;
455                         }
456                 }
457                 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
458                 
459                 /*
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.
462                  */
463
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                                 oss_broken_sleep_on(&adev->out_sleeper,
470                                                                dmabuf_timeout(dmap));
471                                 spin_lock_irqsave(&dmap->lock,flags);
472                         }
473                 }
474                 spin_unlock_irqrestore(&dmap->lock,flags);
475         }
476         adev->dmap_out->dma_mode = DMODE_NONE;
477         return adev->dmap_out->qlen;
478 }
479
480 int DMAbuf_release(int dev, int mode)
481 {
482         struct audio_operations *adev = audio_devs[dev];
483         struct dma_buffparms *dmap;
484         unsigned long flags;
485
486         dmap = adev->dmap_out;
487         if (adev->open_mode & OPEN_WRITE)
488                 adev->dmap_out->closing = 1;
489
490         if (adev->open_mode & OPEN_READ){
491                 adev->dmap_in->closing = 1;
492                 dmap = adev->dmap_in;
493         }
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);
500
501         DMAbuf_reset(dev);
502         spin_lock_irqsave(&dmap->lock,flags);
503         adev->d->close(dev);
504
505         if (adev->open_mode & OPEN_WRITE)
506                 close_dmap(adev, adev->dmap_out);
507
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;
515 }
516 /* called with dmap->lock dold */
517 int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
518 {
519         struct audio_operations *adev = audio_devs[dev];
520         int  err;
521
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;
533         }
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;
540         }
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);
550         }
551         return 0;
552 }
553 /* acquires lock */
554 int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
555 {
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
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;
575                 }
576                 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
577                         spin_unlock_irqrestore(&dmap->lock,flags);
578                         return err;
579                 }
580                 /* Wait for the next block */
581
582                 if (dontblock) {
583                         spin_unlock_irqrestore(&dmap->lock,flags);
584                         return -EAGAIN;
585                 }
586                 if (adev->go)
587                         timeout = dmabuf_timeout(dmap);
588
589                 spin_unlock_irqrestore(&dmap->lock,flags);
590                 timeout = oss_broken_sleep_on(&adev->in_sleeper, 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);
599         }
600         spin_unlock_irqrestore(&dmap->lock,flags);
601
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];
606
607         return dmap->qhead;
608 }
609
610 int DMAbuf_rmchars(int dev, int buff_no, int c)
611 {
612         struct audio_operations *adev = audio_devs[dev];
613         struct dma_buffparms *dmap = adev->dmap_in;
614         int p = dmap->counts[dmap->qhead] + c;
615
616         if (dmap->mapping_flags & DMA_MAP_MAPPED)
617         {
618 /*                printk("Sound: Can't read from mmapped device (2)\n");*/
619                 return -EINVAL;
620         }
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;
627         }
628         else dmap->counts[dmap->qhead] = p;
629
630         return 0;
631 }
632 /* MUST be called with dmap->lock hold */
633 int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
634 {
635         /*
636          *      Try to approximate the active byte position of the DMA pointer within the
637          *      buffer area as well as possible.
638          */
639
640         int pos;
641         unsigned long f;
642
643         if (!(dmap->flags & DMA_ACTIVE))
644                 pos = 0;
645         else {
646                 int chan = dmap->dma;
647                 
648                 f=claim_dma_lock();
649                 clear_dma_ff(chan);
650                 
651                 if(!isa_dma_bridge_buggy)
652                         disable_dma(dmap->dma);
653                 
654                 pos = get_dma_residue(chan);
655                 
656                 pos = dmap->bytes_in_use - pos;
657
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;
667                         }
668                 }
669                 if (pos < 0)
670                         pos = 0;
671                 if (pos >= dmap->bytes_in_use)
672                         pos = 0;
673                 
674                 if(!isa_dma_bridge_buggy)
675                         enable_dma(dmap->dma);
676                         
677                 release_dma_lock(f);
678         }
679         /* printk( "%04x ",  pos); */
680
681         return pos;
682 }
683
684 /*
685  *      DMAbuf_start_devices() is called by the /dev/music driver to start
686  *      one or more audio devices at desired moment.
687  */
688
689 void DMAbuf_start_devices(unsigned int devmask)
690 {
691         struct audio_operations *adev;
692         int dev;
693
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);
707         }
708 }
709 /* via poll called without a lock ?*/
710 int DMAbuf_space_in_queue(int dev)
711 {
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;
716
717         if (lim < 2)
718                 lim = 2;
719
720         if (dmap->qlen >= lim)  /* No space at all */
721                 return 0;
722
723         /*
724          *      Verify that there are no more pending buffers than the limit
725          *      defined by the process.
726          */
727
728         max = dmap->max_fragments;
729         if (max > lim)
730                 max = lim;
731         len = dmap->qlen;
732
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;
738         }
739         if (dmap->byte_counter % dmap->fragment_size)   /* There is a partial fragment */
740                 len = len + 1;
741
742         if (len >= max)
743                 return 0;
744         return max - len;
745 }
746 /* MUST not hold the spinlock  - this function may sleep */
747 static int output_sleep(int dev, int dontblock)
748 {
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;
754
755         if (dontblock)
756                 return -EAGAIN;
757         if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
758                 return -EAGAIN;
759
760         /*
761          * Wait for free space
762          */
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 = oss_broken_sleep_on(&adev->out_sleeper, timeout_value);
771         if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
772                 printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
773                 dma_reset_output(dev);
774         } else {
775                 if (signal_pending(current))
776                         err = -EINTR;
777         }
778         return err;
779 }
780 /* called with the lock held */
781 static int find_output_space(int dev, char **buf, int *size)
782 {
783         struct audio_operations *adev = audio_devs[dev];
784         struct dma_buffparms *dmap = adev->dmap_out;
785         unsigned long active_offs;
786         long len, offs;
787         int maxfrags;
788         int occupied_bytes = (dmap->user_counter % dmap->fragment_size);
789
790         *buf = dmap->raw_buf;
791         if (!(maxfrags = DMAbuf_space_in_queue(dev)) && !occupied_bytes)
792                 return 0;
793
794 #ifdef BE_CONSERVATIVE
795         active_offs = dmap->byte_counter + dmap->qhead * dmap->fragment_size;
796 #else
797         active_offs = max(DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT), 0);
798         /* Check for pointer wrapping situation */
799         if (active_offs >= dmap->bytes_in_use)
800                 active_offs = 0;
801         active_offs += dmap->byte_counter;
802 #endif
803
804         offs = (dmap->user_counter % dmap->bytes_in_use) & ~SAMPLE_ROUNDUP;
805         if (offs < 0 || offs >= dmap->bytes_in_use) {
806                 printk(KERN_ERR "Sound: Got unexpected offs %ld. Giving up.\n", offs);
807                 printk("Counter = %ld, bytes=%d\n", dmap->user_counter, dmap->bytes_in_use);
808                 return 0;
809         }
810         *buf = dmap->raw_buf + offs;
811
812         len = active_offs + dmap->bytes_in_use - dmap->user_counter;    /* Number of unused bytes in buffer */
813
814         if ((offs + len) > dmap->bytes_in_use)
815                 len = dmap->bytes_in_use - offs;
816         if (len < 0) {
817                 return 0;
818         }
819         if (len > ((maxfrags * dmap->fragment_size) - occupied_bytes))
820                 len = (maxfrags * dmap->fragment_size) - occupied_bytes;
821         *size = len & ~SAMPLE_ROUNDUP;
822         return (*size > 0);
823 }
824 /* acquires lock  */
825 int DMAbuf_getwrbuffer(int dev, char **buf, int *size, int dontblock)
826 {
827         struct audio_operations *adev = audio_devs[dev];
828         unsigned long flags;
829         int err = -EIO;
830         struct dma_buffparms *dmap = adev->dmap_out;
831
832         if (dmap->mapping_flags & DMA_MAP_MAPPED) {
833 /*              printk(KERN_DEBUG "Sound: Can't write to mmapped device (3)\n");*/
834                 return -EINVAL;
835         }
836         spin_lock_irqsave(&dmap->lock,flags);
837         if (dmap->needs_reorg)
838                 reorganize_buffers(dev, dmap, 0);
839
840         if (dmap->dma_mode == DMODE_INPUT) {    /* Direction change */
841                 spin_unlock_irqrestore(&dmap->lock,flags);
842                 DMAbuf_reset(dev);
843                 spin_lock_irqsave(&dmap->lock,flags);
844         }
845         dmap->dma_mode = DMODE_OUTPUT;
846
847         while (find_output_space(dev, buf, size) <= 0) {
848                 spin_unlock_irqrestore(&dmap->lock,flags);
849                 if ((err = output_sleep(dev, dontblock)) < 0) {
850                         return err;
851                 }
852                 spin_lock_irqsave(&dmap->lock,flags);
853         }
854
855         spin_unlock_irqrestore(&dmap->lock,flags);
856         return 0;
857 }
858 /* has to acquire dmap->lock */
859 int DMAbuf_move_wrpointer(int dev, int l)
860 {
861         struct audio_operations *adev = audio_devs[dev];
862         struct dma_buffparms *dmap = adev->dmap_out;
863         unsigned long ptr;
864         unsigned long end_ptr, p;
865         int post;
866         unsigned long flags;
867
868         spin_lock_irqsave(&dmap->lock,flags);
869         post= (dmap->flags & DMA_POST);
870         ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
871
872         dmap->flags &= ~DMA_POST;
873         dmap->cfrag = -1;
874         dmap->user_counter += l;
875         dmap->flags |= DMA_DIRTY;
876
877         if (dmap->byte_counter >= dmap->max_byte_counter) {
878                 /* Wrap the byte counters */
879                 long decr = dmap->byte_counter;
880                 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
881                 decr -= dmap->byte_counter;
882                 dmap->user_counter -= decr;
883         }
884         end_ptr = (dmap->user_counter / dmap->fragment_size) * dmap->fragment_size;
885
886         p = (dmap->user_counter - 1) % dmap->bytes_in_use;
887         dmap->neutral_byte = dmap->raw_buf[p];
888
889         /* Update the fragment based bookkeeping too */
890         while (ptr < end_ptr) {
891                 dmap->counts[dmap->qtail] = dmap->fragment_size;
892                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
893                 dmap->qlen++;
894                 ptr += dmap->fragment_size;
895         }
896
897         dmap->counts[dmap->qtail] = dmap->user_counter - ptr;
898
899         /*
900          *      Let the low level driver perform some postprocessing to
901          *      the written data.
902          */
903         if (adev->d->postprocess_write)
904                 adev->d->postprocess_write(dev);
905
906         if (!(dmap->flags & DMA_ACTIVE))
907                 if (dmap->qlen > 1 || (dmap->qlen > 0 && (post || dmap->qlen >= dmap->nbufs - 1)))
908                         DMAbuf_launch_output(dev, dmap);
909
910         spin_unlock_irqrestore(&dmap->lock,flags);
911         return 0;
912 }
913
914 int DMAbuf_start_dma(int dev, unsigned long physaddr, int count, int dma_mode)
915 {
916         struct audio_operations *adev = audio_devs[dev];
917         struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
918
919         if (dmap->raw_buf == NULL) {
920                 printk(KERN_ERR "sound: DMA buffer(1) == NULL\n");
921                 printk("Device %d, chn=%s\n", dev, (dmap == adev->dmap_out) ? "out" : "in");
922                 return 0;
923         }
924         if (dmap->dma < 0)
925                 return 0;
926         sound_start_dma(dmap, physaddr, count, dma_mode);
927         return count;
928 }
929 EXPORT_SYMBOL(DMAbuf_start_dma);
930
931 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode)
932 {
933         struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
934
935         if (dmap->raw_buf == NULL) {
936                 printk(KERN_ERR "sound: DMA buffer(2) == NULL\n");
937                 printk(KERN_ERR "Device %s, chn=%s\n", adev->name, (dmap == adev->dmap_out) ? "out" : "in");
938                 return 0;
939         }
940         if (dmap->flags & DMA_NODMA)
941                 return 1;
942         if (dmap->dma < 0)
943                 return 0;
944         sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
945         dmap->flags |= DMA_STARTED;
946         return count;
947 }
948
949 static void finish_output_interrupt(int dev, struct dma_buffparms *dmap)
950 {
951         struct audio_operations *adev = audio_devs[dev];
952
953         if (dmap->audio_callback != NULL)
954                 dmap->audio_callback(dev, dmap->callback_parm);
955         wake_up(&adev->out_sleeper);
956         wake_up(&adev->poll_sleeper);
957 }
958 /* called with dmap->lock held in irq context*/
959 static void do_outputintr(int dev, int dummy)
960 {
961         struct audio_operations *adev = audio_devs[dev];
962         struct dma_buffparms *dmap = adev->dmap_out;
963         int this_fragment;
964
965         if (dmap->raw_buf == NULL) {
966                 printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
967                 return;
968         }
969         if (dmap->mapping_flags & DMA_MAP_MAPPED) {     /* Virtual memory mapped access */
970                 /* mmapped access */
971                 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
972                 if (dmap->qhead == 0) {     /* Wrapped */
973                         dmap->byte_counter += dmap->bytes_in_use;
974                         if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
975                                 long decr = dmap->byte_counter;
976                                 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
977                                 decr -= dmap->byte_counter;
978                                 dmap->user_counter -= decr;
979                         }
980                 }
981                 dmap->qlen++;   /* Yes increment it (don't decrement) */
982                 if (!(adev->flags & DMA_AUTOMODE))
983                         dmap->flags &= ~DMA_ACTIVE;
984                 dmap->counts[dmap->qhead] = dmap->fragment_size;
985                 DMAbuf_launch_output(dev, dmap);
986                 finish_output_interrupt(dev, dmap);
987                 return;
988         }
989
990         dmap->qlen--;
991         this_fragment = dmap->qhead;
992         dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
993
994         if (dmap->qhead == 0) { /* Wrapped */
995                 dmap->byte_counter += dmap->bytes_in_use;
996                 if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
997                         long decr = dmap->byte_counter;
998                         dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
999                         decr -= dmap->byte_counter;
1000                         dmap->user_counter -= decr;
1001                 }
1002         }
1003         if (!(adev->flags & DMA_AUTOMODE))
1004                 dmap->flags &= ~DMA_ACTIVE;
1005                 
1006         /*
1007          *      This is  dmap->qlen <= 0 except when closing when
1008          *      dmap->qlen < 0
1009          */
1010          
1011         while (dmap->qlen <= -dmap->closing) {
1012                 dmap->underrun_count++;
1013                 dmap->qlen++;
1014                 if ((dmap->flags & DMA_DIRTY) && dmap->applic_profile != APF_CPUINTENS) {
1015                         dmap->flags &= ~DMA_DIRTY;
1016                         memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
1017                                adev->dmap_out->buffsize);
1018                 }
1019                 dmap->user_counter += dmap->fragment_size;
1020                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1021         }
1022         if (dmap->qlen > 0)
1023                 DMAbuf_launch_output(dev, dmap);
1024         finish_output_interrupt(dev, dmap);
1025 }
1026 /* called in irq context */
1027 void DMAbuf_outputintr(int dev, int notify_only)
1028 {
1029         struct audio_operations *adev = audio_devs[dev];
1030         unsigned long flags;
1031         struct dma_buffparms *dmap = adev->dmap_out;
1032
1033         spin_lock_irqsave(&dmap->lock,flags);
1034         if (!(dmap->flags & DMA_NODMA)) {
1035                 int chan = dmap->dma, pos, n;
1036                 unsigned long f;
1037                 
1038                 f=claim_dma_lock();
1039                 
1040                 if(!isa_dma_bridge_buggy)
1041                         disable_dma(dmap->dma);
1042                 clear_dma_ff(chan);
1043                 pos = dmap->bytes_in_use - get_dma_residue(chan);
1044                 if(!isa_dma_bridge_buggy)
1045                         enable_dma(dmap->dma);
1046                 release_dma_lock(f);
1047                 
1048                 pos = pos / dmap->fragment_size;        /* Actual qhead */
1049                 if (pos < 0 || pos >= dmap->nbufs)
1050                         pos = 0;
1051                 n = 0;
1052                 while (dmap->qhead != pos && n++ < dmap->nbufs)
1053                         do_outputintr(dev, notify_only);
1054         }
1055         else
1056                 do_outputintr(dev, notify_only);
1057         spin_unlock_irqrestore(&dmap->lock,flags);
1058 }
1059 EXPORT_SYMBOL(DMAbuf_outputintr);
1060
1061 /* called with dmap->lock held in irq context */
1062 static void do_inputintr(int dev)
1063 {
1064         struct audio_operations *adev = audio_devs[dev];
1065         struct dma_buffparms *dmap = adev->dmap_in;
1066
1067         if (dmap->raw_buf == NULL) {
1068                 printk(KERN_ERR "Sound: Fatal error. Audio interrupt after freeing buffers.\n");
1069                 return;
1070         }
1071         if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1072                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1073                 if (dmap->qtail == 0) {         /* Wrapped */
1074                         dmap->byte_counter += dmap->bytes_in_use;
1075                         if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
1076                                 long decr = dmap->byte_counter;
1077                                 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1078                                 decr -= dmap->byte_counter;
1079                                 dmap->user_counter -= decr;
1080                         }
1081                 }
1082                 dmap->qlen++;
1083
1084                 if (!(adev->flags & DMA_AUTOMODE)) {
1085                         if (dmap->needs_reorg)
1086                                 reorganize_buffers(dev, dmap, 0);
1087                         local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_READ);
1088                         adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
1089                                              dmap->fragment_size, 1);
1090                         if (adev->d->trigger)
1091                                 adev->d->trigger(dev, adev->enable_bits * adev->go);
1092                 }
1093                 dmap->flags |= DMA_ACTIVE;
1094         } else if (dmap->qlen >= (dmap->nbufs - 1)) {
1095                 printk(KERN_WARNING "Sound: Recording overrun\n");
1096                 dmap->underrun_count++;
1097
1098                 /* Just throw away the oldest fragment but keep the engine running */
1099                 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1100                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1101         } else if (dmap->qlen >= 0 && dmap->qlen < dmap->nbufs) {
1102                 dmap->qlen++;
1103                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1104                 if (dmap->qtail == 0) {         /* Wrapped */
1105                         dmap->byte_counter += dmap->bytes_in_use;
1106                         if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
1107                                 long decr = dmap->byte_counter;
1108                                 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1109                                 decr -= dmap->byte_counter;
1110                                 dmap->user_counter -= decr;
1111                         }
1112                 }
1113         }
1114         if (!(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
1115                 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
1116                 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size, dmap->fragment_size, 1);
1117                 if (adev->d->trigger)
1118                         adev->d->trigger(dev,adev->enable_bits * adev->go);
1119         }
1120         dmap->flags |= DMA_ACTIVE;
1121         if (dmap->qlen > 0)
1122         {
1123                 wake_up(&adev->in_sleeper);
1124                 wake_up(&adev->poll_sleeper);
1125         }
1126 }
1127 /* called in irq context */
1128 void DMAbuf_inputintr(int dev)
1129 {
1130         struct audio_operations *adev = audio_devs[dev];
1131         struct dma_buffparms *dmap = adev->dmap_in;
1132         unsigned long flags;
1133
1134         spin_lock_irqsave(&dmap->lock,flags);
1135
1136         if (!(dmap->flags & DMA_NODMA)) {
1137                 int chan = dmap->dma, pos, n;
1138                 unsigned long f;
1139                 
1140                 f=claim_dma_lock();
1141                 if(!isa_dma_bridge_buggy)
1142                         disable_dma(dmap->dma);
1143                 clear_dma_ff(chan);
1144                 pos = dmap->bytes_in_use - get_dma_residue(chan);
1145                 if(!isa_dma_bridge_buggy)
1146                         enable_dma(dmap->dma);
1147                 release_dma_lock(f);
1148
1149                 pos = pos / dmap->fragment_size;        /* Actual qhead */
1150                 if (pos < 0 || pos >= dmap->nbufs)
1151                         pos = 0;
1152
1153                 n = 0;
1154                 while (dmap->qtail != pos && ++n < dmap->nbufs)
1155                         do_inputintr(dev);
1156         } else
1157                 do_inputintr(dev);
1158         spin_unlock_irqrestore(&dmap->lock,flags);
1159 }
1160 EXPORT_SYMBOL(DMAbuf_inputintr);
1161
1162 void DMAbuf_init(int dev, int dma1, int dma2)
1163 {
1164         struct audio_operations *adev = audio_devs[dev];
1165         /*
1166          * NOTE! This routine could be called several times.
1167          */
1168
1169         if (adev && adev->dmap_out == NULL) {
1170                 if (adev->d == NULL)
1171                         panic("OSS: audio_devs[%d]->d == NULL\n", dev);
1172
1173                 if (adev->parent_dev) {  /* Use DMA map of the parent dev */
1174                         int parent = adev->parent_dev - 1;
1175                         adev->dmap_out = audio_devs[parent]->dmap_out;
1176                         adev->dmap_in = audio_devs[parent]->dmap_in;
1177                 } else {
1178                         adev->dmap_out = adev->dmap_in = &adev->dmaps[0];
1179                         adev->dmap_out->dma = dma1;
1180                         if (adev->flags & DMA_DUPLEX) {
1181                                 adev->dmap_in = &adev->dmaps[1];
1182                                 adev->dmap_in->dma = dma2;
1183                         }
1184                 }
1185                 /* Persistent DMA buffers allocated here */
1186                 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1187                         if (adev->dmap_in->raw_buf == NULL)
1188                                 sound_alloc_dmap(adev->dmap_in);
1189                         if (adev->dmap_out->raw_buf == NULL)
1190                                 sound_alloc_dmap(adev->dmap_out);
1191                 }
1192         }
1193 }
1194
1195 /* No kernel lock - DMAbuf_activate_recording protected by global cli/sti */
1196 static unsigned int poll_input(struct file * file, int dev, poll_table *wait)
1197 {
1198         struct audio_operations *adev = audio_devs[dev];
1199         struct dma_buffparms *dmap = adev->dmap_in;
1200
1201         if (!(adev->open_mode & OPEN_READ))
1202                 return 0;
1203         if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1204                 if (dmap->qlen)
1205                         return POLLIN | POLLRDNORM;
1206                 return 0;
1207         }
1208         if (dmap->dma_mode != DMODE_INPUT) {
1209                 if (dmap->dma_mode == DMODE_NONE &&
1210                     adev->enable_bits & PCM_ENABLE_INPUT &&
1211                     !dmap->qlen && adev->go) {
1212                         unsigned long flags;
1213                         
1214                         spin_lock_irqsave(&dmap->lock,flags);
1215                         DMAbuf_activate_recording(dev, dmap);
1216                         spin_unlock_irqrestore(&dmap->lock,flags);
1217                 }
1218                 return 0;
1219         }
1220         if (!dmap->qlen)
1221                 return 0;
1222         return POLLIN | POLLRDNORM;
1223 }
1224
1225 static unsigned int poll_output(struct file * file, int dev, poll_table *wait)
1226 {
1227         struct audio_operations *adev = audio_devs[dev];
1228         struct dma_buffparms *dmap = adev->dmap_out;
1229         
1230         if (!(adev->open_mode & OPEN_WRITE))
1231                 return 0;
1232         if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1233                 if (dmap->qlen)
1234                         return POLLOUT | POLLWRNORM;
1235                 return 0;
1236         }
1237         if (dmap->dma_mode == DMODE_INPUT)
1238                 return 0;
1239         if (dmap->dma_mode == DMODE_NONE)
1240                 return POLLOUT | POLLWRNORM;
1241         if (!DMAbuf_space_in_queue(dev))
1242                 return 0;
1243         return POLLOUT | POLLWRNORM;
1244 }
1245
1246 unsigned int DMAbuf_poll(struct file * file, int dev, poll_table *wait)
1247 {
1248         struct audio_operations *adev = audio_devs[dev];
1249         poll_wait(file, &adev->poll_sleeper, wait);
1250         return poll_input(file, dev, wait) | poll_output(file, dev, wait);
1251 }
1252
1253 void DMAbuf_deinit(int dev)
1254 {
1255         struct audio_operations *adev = audio_devs[dev];
1256         /* This routine is called when driver is being unloaded */
1257         if (!adev)
1258                 return;
1259
1260         /* Persistent DMA buffers deallocated here */
1261         if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1262                 sound_free_dmap(adev->dmap_out);
1263                 if (adev->flags & DMA_DUPLEX)
1264                         sound_free_dmap(adev->dmap_in);
1265         }
1266 }