[PATCH] Fix sequencer missing negative bound check
[pandora-kernel.git] / sound / oss / dmabuf.c
1 /*
2  * sound/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 "sound_config.h"
29
30 #define DMAP_FREE_ON_CLOSE      0
31 #define DMAP_KEEP_ON_CLOSE      1
32 extern int sound_dmap_flag;
33
34 static void dma_reset_output(int dev);
35 static void dma_reset_input(int dev);
36 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode);
37
38
39
40 static int debugmem;            /* switched off by default */
41 static int dma_buffsize = DSP_BUFFSIZE;
42
43 static long dmabuf_timeout(struct dma_buffparms *dmap)
44 {
45         long tmout;
46
47         tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
48         tmout += HZ / 5;        /* Some safety distance */
49         if (tmout < (HZ / 2))
50                 tmout = HZ / 2;
51         if (tmout > 20 * HZ)
52                 tmout = 20 * HZ;
53         return tmout;
54 }
55
56 static int sound_alloc_dmap(struct dma_buffparms *dmap)
57 {
58         char *start_addr, *end_addr;
59         int dma_pagesize;
60         int sz, size;
61         struct page *page;
62
63         dmap->mapping_flags &= ~DMA_MAP_MAPPED;
64
65         if (dmap->raw_buf != NULL)
66                 return 0;       /* Already done */
67         if (dma_buffsize < 4096)
68                 dma_buffsize = 4096;
69         dma_pagesize = (dmap->dma < 4) ? (64 * 1024) : (128 * 1024);
70         
71         /*
72          *      Now check for the Cyrix problem.
73          */
74          
75         if(isa_dma_bridge_buggy==2)
76                 dma_pagesize=32768;
77          
78         dmap->raw_buf = NULL;
79         dmap->buffsize = dma_buffsize;
80         if (dmap->buffsize > dma_pagesize)
81                 dmap->buffsize = dma_pagesize;
82         start_addr = NULL;
83         /*
84          * Now loop until we get a free buffer. Try to get smaller buffer if
85          * it fails. Don't accept smaller than 8k buffer for performance
86          * reasons.
87          */
88         while (start_addr == NULL && dmap->buffsize > PAGE_SIZE) {
89                 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
90                 dmap->buffsize = PAGE_SIZE * (1 << sz);
91                 start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA|__GFP_NOWARN, sz);
92                 if (start_addr == NULL)
93                         dmap->buffsize /= 2;
94         }
95
96         if (start_addr == NULL) {
97                 printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
98                 return -ENOMEM;
99         } else {
100                 /* make some checks */
101                 end_addr = start_addr + dmap->buffsize - 1;
102
103                 if (debugmem)
104                         printk(KERN_DEBUG "sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr);
105                 
106                 /* now check if it fits into the same dma-pagesize */
107
108                 if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
109                     || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
110                         printk(KERN_ERR "sound: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, dmap->buffsize);
111                         return -EFAULT;
112                 }
113         }
114         dmap->raw_buf = start_addr;
115         dmap->raw_buf_phys = virt_to_bus(start_addr);
116
117         for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
118                 SetPageReserved(page);
119         return 0;
120 }
121
122 static void sound_free_dmap(struct dma_buffparms *dmap)
123 {
124         int sz, size;
125         struct page *page;
126         unsigned long start_addr, end_addr;
127
128         if (dmap->raw_buf == NULL)
129                 return;
130         if (dmap->mapping_flags & DMA_MAP_MAPPED)
131                 return;         /* Don't free mmapped buffer. Will use it next time */
132         for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
133
134         start_addr = (unsigned long) dmap->raw_buf;
135         end_addr = start_addr + dmap->buffsize;
136
137         for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
138                 ClearPageReserved(page);
139
140         free_pages((unsigned long) dmap->raw_buf, sz);
141         dmap->raw_buf = NULL;
142 }
143
144
145 /* Intel version !!!!!!!!! */
146
147 static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
148 {
149         unsigned long flags;
150         int chan = dmap->dma;
151
152         /* printk( "Start DMA%d %d, %d\n",  chan,  (int)(physaddr-dmap->raw_buf_phys),  count); */
153
154         flags = claim_dma_lock();
155         disable_dma(chan);
156         clear_dma_ff(chan);
157         set_dma_mode(chan, dma_mode);
158         set_dma_addr(chan, physaddr);
159         set_dma_count(chan, count);
160         enable_dma(chan);
161         release_dma_lock(flags);
162
163         return 0;
164 }
165
166 static void dma_init_buffers(struct dma_buffparms *dmap)
167 {
168         dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
169         dmap->byte_counter = 0;
170         dmap->max_byte_counter = 8000 * 60 * 60;
171         dmap->bytes_in_use = dmap->buffsize;
172
173         dmap->dma_mode = DMODE_NONE;
174         dmap->mapping_flags = 0;
175         dmap->neutral_byte = 0x80;
176         dmap->data_rate = 8000;
177         dmap->cfrag = -1;
178         dmap->closing = 0;
179         dmap->nbufs = 1;
180         dmap->flags = DMA_BUSY; /* Other flags off */
181 }
182
183 static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
184 {
185         int err;
186         
187         if (dmap->flags & DMA_BUSY)
188                 return -EBUSY;
189         if ((err = sound_alloc_dmap(dmap)) < 0)
190                 return err;
191
192         if (dmap->raw_buf == NULL) {
193                 printk(KERN_WARNING "Sound: DMA buffers not available\n");
194                 return -ENOSPC; /* Memory allocation failed during boot */
195         }
196         if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
197                 printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
198                 return -EBUSY;
199         }
200         dma_init_buffers(dmap);
201         spin_lock_init(&dmap->lock);
202         dmap->open_mode = mode;
203         dmap->subdivision = dmap->underrun_count = 0;
204         dmap->fragment_size = 0;
205         dmap->max_fragments = 65536;    /* Just a large value */
206         dmap->byte_counter = 0;
207         dmap->max_byte_counter = 8000 * 60 * 60;
208         dmap->applic_profile = APF_NORMAL;
209         dmap->needs_reorg = 1;
210         dmap->audio_callback = NULL;
211         dmap->callback_parm = 0;
212         return 0;
213 }
214
215 static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
216 {
217         unsigned long flags;
218         
219         if (dmap->dma >= 0) {
220                 sound_close_dma(dmap->dma);
221                 flags=claim_dma_lock();
222                 disable_dma(dmap->dma);
223                 release_dma_lock(flags);
224         }
225         if (dmap->flags & DMA_BUSY)
226                 dmap->dma_mode = DMODE_NONE;
227         dmap->flags &= ~DMA_BUSY;
228         
229         if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
230                 sound_free_dmap(dmap);
231 }
232
233
234 static unsigned int default_set_bits(int dev, unsigned int bits)
235 {
236         mm_segment_t fs = get_fs();
237
238         set_fs(get_ds());
239         audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
240         set_fs(fs);
241         return bits;
242 }
243
244 static int default_set_speed(int dev, int speed)
245 {
246         mm_segment_t fs = get_fs();
247
248         set_fs(get_ds());
249         audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
250         set_fs(fs);
251         return speed;
252 }
253
254 static short default_set_channels(int dev, short channels)
255 {
256         int c = channels;
257         mm_segment_t fs = get_fs();
258
259         set_fs(get_ds());
260         audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
261         set_fs(fs);
262         return c;
263 }
264
265 static void check_driver(struct audio_driver *d)
266 {
267         if (d->set_speed == NULL)
268                 d->set_speed = default_set_speed;
269         if (d->set_bits == NULL)
270                 d->set_bits = default_set_bits;
271         if (d->set_channels == NULL)
272                 d->set_channels = default_set_channels;
273 }
274
275 int DMAbuf_open(int dev, int mode)
276 {
277         struct audio_operations *adev = audio_devs[dev];
278         int retval;
279         struct dma_buffparms *dmap_in = NULL;
280         struct dma_buffparms *dmap_out = NULL;
281
282         if (!adev)
283                   return -ENXIO;
284         if (!(adev->flags & DMA_DUPLEX))
285                 adev->dmap_in = adev->dmap_out;
286         check_driver(adev->d);
287
288         if ((retval = adev->d->open(dev, mode)) < 0)
289                 return retval;
290         dmap_out = adev->dmap_out;
291         dmap_in = adev->dmap_in;
292         if (dmap_in == dmap_out)
293                 adev->flags &= ~DMA_DUPLEX;
294
295         if (mode & OPEN_WRITE) {
296                 if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
297                         adev->d->close(dev);
298                         return retval;
299                 }
300         }
301         adev->enable_bits = mode;
302
303         if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
304                 if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
305                         adev->d->close(dev);
306                         if (mode & OPEN_WRITE)
307                                 close_dmap(adev, dmap_out);
308                         return retval;
309                 }
310         }
311         adev->open_mode = mode;
312         adev->go = 1;
313
314         adev->d->set_bits(dev, 8);
315         adev->d->set_channels(dev, 1);
316         adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
317         if (adev->dmap_out->dma_mode == DMODE_OUTPUT) 
318                 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
319                        adev->dmap_out->bytes_in_use);
320         return 0;
321 }
322 /* MUST not hold the spinlock */
323 void DMAbuf_reset(int dev)
324 {
325         if (audio_devs[dev]->open_mode & OPEN_WRITE)
326                 dma_reset_output(dev);
327
328         if (audio_devs[dev]->open_mode & OPEN_READ)
329                 dma_reset_input(dev);
330 }
331
332 static void dma_reset_output(int dev)
333 {
334         struct audio_operations *adev = audio_devs[dev];
335         unsigned long flags,f ;
336         struct dma_buffparms *dmap = adev->dmap_out;
337
338         if (!(dmap->flags & DMA_STARTED))       /* DMA is not active */
339                 return;
340
341         /*
342          *      First wait until the current fragment has been played completely
343          */
344         spin_lock_irqsave(&dmap->lock,flags);
345         adev->dmap_out->flags |= DMA_SYNCING;
346
347         adev->dmap_out->underrun_count = 0;
348         if (!signal_pending(current) && adev->dmap_out->qlen && 
349             adev->dmap_out->underrun_count == 0){
350                 spin_unlock_irqrestore(&dmap->lock,flags);
351                 interruptible_sleep_on_timeout(&adev->out_sleeper,
352                                                dmabuf_timeout(dmap));
353                 spin_lock_irqsave(&dmap->lock,flags);
354         }
355         adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
356
357         /*
358          *      Finally shut the device off
359          */
360         if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_output)
361                 adev->d->halt_io(dev);
362         else
363                 adev->d->halt_output(dev);
364         adev->dmap_out->flags &= ~DMA_STARTED;
365         
366         f=claim_dma_lock();
367         clear_dma_ff(dmap->dma);
368         disable_dma(dmap->dma);
369         release_dma_lock(f);
370         
371         dmap->byte_counter = 0;
372         reorganize_buffers(dev, adev->dmap_out, 0);
373         dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
374         spin_unlock_irqrestore(&dmap->lock,flags);
375 }
376
377 static void dma_reset_input(int dev)
378 {
379         struct audio_operations *adev = audio_devs[dev];
380         unsigned long flags;
381         struct dma_buffparms *dmap = adev->dmap_in;
382
383         spin_lock_irqsave(&dmap->lock,flags);
384         if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_input)
385                 adev->d->halt_io(dev);
386         else
387                 adev->d->halt_input(dev);
388         adev->dmap_in->flags &= ~DMA_STARTED;
389
390         dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
391         dmap->byte_counter = 0;
392         reorganize_buffers(dev, adev->dmap_in, 1);
393         spin_unlock_irqrestore(&dmap->lock,flags);
394 }
395 /* MUST be called with holding the dmap->lock */
396 void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
397 {
398         struct audio_operations *adev = audio_devs[dev];
399
400         if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
401                 return;         /* Don't start DMA yet */
402         dmap->dma_mode = DMODE_OUTPUT;
403
404         if (!(dmap->flags & DMA_ACTIVE) || !(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
405                 if (!(dmap->flags & DMA_STARTED)) {
406                         reorganize_buffers(dev, dmap, 0);
407                         if (adev->d->prepare_for_output(dev, dmap->fragment_size, dmap->nbufs))
408                                 return;
409                         if (!(dmap->flags & DMA_NODMA))
410                                 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_WRITE);
411                         dmap->flags |= DMA_STARTED;
412                 }
413                 if (dmap->counts[dmap->qhead] == 0)
414                         dmap->counts[dmap->qhead] = dmap->fragment_size;
415                 dmap->dma_mode = DMODE_OUTPUT;
416                 adev->d->output_block(dev, dmap->raw_buf_phys + dmap->qhead * dmap->fragment_size,
417                                       dmap->counts[dmap->qhead], 1);
418                 if (adev->d->trigger)
419                         adev->d->trigger(dev,adev->enable_bits * adev->go);
420         }
421         dmap->flags |= DMA_ACTIVE;
422 }
423
424 int DMAbuf_sync(int dev)
425 {
426         struct audio_operations *adev = audio_devs[dev];
427         unsigned long flags;
428         int n = 0;
429         struct dma_buffparms *dmap;
430
431         if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
432                 return 0;
433
434         if (adev->dmap_out->dma_mode == DMODE_OUTPUT) {
435                 dmap = adev->dmap_out;
436                 spin_lock_irqsave(&dmap->lock,flags);
437                 if (dmap->qlen > 0 && !(dmap->flags & DMA_ACTIVE))
438                         DMAbuf_launch_output(dev, dmap);
439                 adev->dmap_out->flags |= DMA_SYNCING;
440                 adev->dmap_out->underrun_count = 0;
441                 while (!signal_pending(current) && n++ <= adev->dmap_out->nbufs && 
442                        adev->dmap_out->qlen && adev->dmap_out->underrun_count == 0) {
443                         long t = dmabuf_timeout(dmap);
444                         spin_unlock_irqrestore(&dmap->lock,flags);
445                         /* FIXME: not safe may miss events */
446                         t = interruptible_sleep_on_timeout(&adev->out_sleeper, t);
447                         spin_lock_irqsave(&dmap->lock,flags);
448                         if (!t) {
449                                 adev->dmap_out->flags &= ~DMA_SYNCING;
450                                 spin_unlock_irqrestore(&dmap->lock,flags);
451                                 return adev->dmap_out->qlen;
452                         }
453                 }
454                 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
455                 
456                 /*
457                  * Some devices such as GUS have huge amount of on board RAM for the
458                  * audio data. We have to wait until the device has finished playing.
459                  */
460
461                 /* still holding the lock */
462                 if (adev->d->local_qlen) {   /* Device has hidden buffers */
463                         while (!signal_pending(current) &&
464                                adev->d->local_qlen(dev)){
465                                 spin_unlock_irqrestore(&dmap->lock,flags);
466                                 interruptible_sleep_on_timeout(&adev->out_sleeper,
467                                                                dmabuf_timeout(dmap));
468                                 spin_lock_irqsave(&dmap->lock,flags);
469                         }
470                 }
471                 spin_unlock_irqrestore(&dmap->lock,flags);
472         }
473         adev->dmap_out->dma_mode = DMODE_NONE;
474         return adev->dmap_out->qlen;
475 }
476
477 int DMAbuf_release(int dev, int mode)
478 {
479         struct audio_operations *adev = audio_devs[dev];
480         struct dma_buffparms *dmap;
481         unsigned long flags;
482
483         dmap = adev->dmap_out;
484         if (adev->open_mode & OPEN_WRITE)
485                 adev->dmap_out->closing = 1;
486
487         if (adev->open_mode & OPEN_READ){
488                 adev->dmap_in->closing = 1;
489                 dmap = adev->dmap_in;
490         }
491         if (adev->open_mode & OPEN_WRITE)
492                 if (!(adev->dmap_out->mapping_flags & DMA_MAP_MAPPED))
493                         if (!signal_pending(current) && (adev->dmap_out->dma_mode == DMODE_OUTPUT))
494                                 DMAbuf_sync(dev);
495         if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
496                 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte, adev->dmap_out->bytes_in_use);
497
498         DMAbuf_reset(dev);
499         spin_lock_irqsave(&dmap->lock,flags);
500         adev->d->close(dev);
501
502         if (adev->open_mode & OPEN_WRITE)
503                 close_dmap(adev, adev->dmap_out);
504
505         if (adev->open_mode == OPEN_READ ||
506             (adev->open_mode != OPEN_WRITE &&
507              (adev->flags & DMA_DUPLEX)))
508                 close_dmap(adev, adev->dmap_in);
509         adev->open_mode = 0;
510         spin_unlock_irqrestore(&dmap->lock,flags);
511         return 0;
512 }
513 /* called with dmap->lock dold */
514 int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
515 {
516         struct audio_operations *adev = audio_devs[dev];
517         int  err;
518
519         if (!(adev->open_mode & OPEN_READ))
520                 return 0;
521         if (!(adev->enable_bits & PCM_ENABLE_INPUT))
522                 return 0;
523         if (dmap->dma_mode == DMODE_OUTPUT) {   /* Direction change */
524                 /* release lock - it's not recursive */
525                 spin_unlock_irq(&dmap->lock);
526                 DMAbuf_sync(dev);
527                 DMAbuf_reset(dev);
528                 spin_lock_irq(&dmap->lock);
529                 dmap->dma_mode = DMODE_NONE;
530         }
531         if (!dmap->dma_mode) {
532                 reorganize_buffers(dev, dmap, 1);
533                 if ((err = adev->d->prepare_for_input(dev,
534                                 dmap->fragment_size, dmap->nbufs)) < 0)
535                         return err;
536                 dmap->dma_mode = DMODE_INPUT;
537         }
538         if (!(dmap->flags & DMA_ACTIVE)) {
539                 if (dmap->needs_reorg)
540                         reorganize_buffers(dev, dmap, 0);
541                 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
542                 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
543                                      dmap->fragment_size, 0);
544                 dmap->flags |= DMA_ACTIVE;
545                 if (adev->d->trigger)
546                         adev->d->trigger(dev, adev->enable_bits * adev->go);
547         }
548         return 0;
549 }
550 /* aquires lock */
551 int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
552 {
553         struct audio_operations *adev = audio_devs[dev];
554         unsigned long flags;
555         int err = 0, n = 0;
556         struct dma_buffparms *dmap = adev->dmap_in;
557         int go;
558
559         if (!(adev->open_mode & OPEN_READ))
560                 return -EIO;
561         spin_lock_irqsave(&dmap->lock,flags);
562         if (dmap->needs_reorg)
563                 reorganize_buffers(dev, dmap, 0);
564         if (adev->dmap_in->mapping_flags & DMA_MAP_MAPPED) {
565 /*                printk(KERN_WARNING "Sound: Can't read from mmapped device (1)\n");*/
566                   spin_unlock_irqrestore(&dmap->lock,flags);
567                   return -EINVAL;
568         } else while (dmap->qlen <= 0 && n++ < 10) {
569                 long timeout = MAX_SCHEDULE_TIMEOUT;
570                 if (!(adev->enable_bits & PCM_ENABLE_INPUT) || !adev->go) {
571                         spin_unlock_irqrestore(&dmap->lock,flags);
572                         return -EAGAIN;
573                 }
574                 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
575                         spin_unlock_irqrestore(&dmap->lock,flags);
576                         return err;
577                 }
578                 /* Wait for the next block */
579
580                 if (dontblock) {
581                         spin_unlock_irqrestore(&dmap->lock,flags);
582                         return -EAGAIN;
583                 }
584                 if ((go = adev->go))
585                         timeout = dmabuf_timeout(dmap);
586
587                 spin_unlock_irqrestore(&dmap->lock,flags);
588                 timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
589                                                          timeout);
590                 if (!timeout) {
591                         /* FIXME: include device name */
592                         err = -EIO;
593                         printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
594                         dma_reset_input(dev);
595                 } else
596                         err = -EINTR;
597                 spin_lock_irqsave(&dmap->lock,flags);
598         }
599         spin_unlock_irqrestore(&dmap->lock,flags);
600
601         if (dmap->qlen <= 0)
602                 return err ? err : -EINTR;
603         *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
604         *len = dmap->fragment_size - dmap->counts[dmap->qhead];
605
606         return dmap->qhead;
607 }
608
609 int DMAbuf_rmchars(int dev, int buff_no, int c)
610 {
611         struct audio_operations *adev = audio_devs[dev];
612         struct dma_buffparms *dmap = adev->dmap_in;
613         int p = dmap->counts[dmap->qhead] + c;
614
615         if (dmap->mapping_flags & DMA_MAP_MAPPED)
616         {
617 /*                printk("Sound: Can't read from mmapped device (2)\n");*/
618                 return -EINVAL;
619         }
620         else if (dmap->qlen <= 0)
621                 return -EIO;
622         else if (p >= dmap->fragment_size) {  /* This buffer is completely empty */
623                 dmap->counts[dmap->qhead] = 0;
624                 dmap->qlen--;
625                 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
626         }
627         else dmap->counts[dmap->qhead] = p;
628
629         return 0;
630 }
631 /* MUST be called with dmap->lock hold */
632 int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
633 {
634         /*
635          *      Try to approximate the active byte position of the DMA pointer within the
636          *      buffer area as well as possible.
637          */
638
639         int pos;
640         unsigned long f;
641
642         if (!(dmap->flags & DMA_ACTIVE))
643                 pos = 0;
644         else {
645                 int chan = dmap->dma;
646                 
647                 f=claim_dma_lock();
648                 clear_dma_ff(chan);
649                 
650                 if(!isa_dma_bridge_buggy)
651                         disable_dma(dmap->dma);
652                 
653                 pos = get_dma_residue(chan);
654                 
655                 pos = dmap->bytes_in_use - pos;
656
657                 if (!(dmap->mapping_flags & DMA_MAP_MAPPED)) {
658                         if (direction == DMODE_OUTPUT) {
659                                 if (dmap->qhead == 0)
660                                         if (pos > dmap->fragment_size)
661                                                 pos = 0;
662                         } else {
663                                 if (dmap->qtail == 0)
664                                         if (pos > dmap->fragment_size)
665                                                 pos = 0;
666                         }
667                 }
668                 if (pos < 0)
669                         pos = 0;
670                 if (pos >= dmap->bytes_in_use)
671                         pos = 0;
672                 
673                 if(!isa_dma_bridge_buggy)
674                         enable_dma(dmap->dma);
675                         
676                 release_dma_lock(f);
677         }
678         /* printk( "%04x ",  pos); */
679
680         return pos;
681 }
682
683 /*
684  *      DMAbuf_start_devices() is called by the /dev/music driver to start
685  *      one or more audio devices at desired moment.
686  */
687
688 void DMAbuf_start_devices(unsigned int devmask)
689 {
690         struct audio_operations *adev;
691         int dev;
692
693         for (dev = 0; dev < num_audiodevs; dev++) {
694                 if (!(devmask & (1 << dev)))
695                         continue;
696                 if (!(adev = audio_devs[dev]))
697                         continue;
698                 if (adev->open_mode == 0)
699                         continue;
700                 if (adev->go)
701                         continue;
702                 /* OK to start the device */
703                 adev->go = 1;
704                 if (adev->d->trigger)
705                         adev->d->trigger(dev,adev->enable_bits * adev->go);
706         }
707 }
708 /* via poll called without a lock ?*/
709 int DMAbuf_space_in_queue(int dev)
710 {
711         struct audio_operations *adev = audio_devs[dev];
712         int len, max, tmp;
713         struct dma_buffparms *dmap = adev->dmap_out;
714         int lim = dmap->nbufs;
715
716         if (lim < 2)
717                 lim = 2;
718
719         if (dmap->qlen >= lim)  /* No space at all */
720                 return 0;
721
722         /*
723          *      Verify that there are no more pending buffers than the limit
724          *      defined by the process.
725          */
726
727         max = dmap->max_fragments;
728         if (max > lim)
729                 max = lim;
730         len = dmap->qlen;
731
732         if (adev->d->local_qlen) {
733                 tmp = adev->d->local_qlen(dev);
734                 if (tmp && len)
735                         tmp--;  /* This buffer has been counted twice */
736                 len += tmp;
737         }
738         if (dmap->byte_counter % dmap->fragment_size)   /* There is a partial fragment */
739                 len = len + 1;
740
741         if (len >= max)
742                 return 0;
743         return max - len;
744 }
745 /* MUST not hold the spinlock  - this function may sleep */
746 static int output_sleep(int dev, int dontblock)
747 {
748         struct audio_operations *adev = audio_devs[dev];
749         int err = 0;
750         struct dma_buffparms *dmap = adev->dmap_out;
751         long timeout;
752         long timeout_value;
753
754         if (dontblock)
755                 return -EAGAIN;
756         if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
757                 return -EAGAIN;
758
759         /*
760          * Wait for free space
761          */
762         if (signal_pending(current))
763                 return -EINTR;
764         timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
765         if (timeout) 
766                 timeout_value = dmabuf_timeout(dmap);
767         else
768                 timeout_value = MAX_SCHEDULE_TIMEOUT;
769         timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
770                                                        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 = DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT);
798         /* Check for pointer wrapping situation */
799         if (active_offs < 0 || 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 /* aquires 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 aquire 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
930 static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode)
931 {
932         struct dma_buffparms *dmap = (dma_mode == DMA_MODE_WRITE) ? adev->dmap_out : adev->dmap_in;
933
934         if (dmap->raw_buf == NULL) {
935                 printk(KERN_ERR "sound: DMA buffer(2) == NULL\n");
936                 printk(KERN_ERR "Device %s, chn=%s\n", adev->name, (dmap == adev->dmap_out) ? "out" : "in");
937                 return 0;
938         }
939         if (dmap->flags & DMA_NODMA)
940                 return 1;
941         if (dmap->dma < 0)
942                 return 0;
943         sound_start_dma(dmap, dmap->raw_buf_phys, dmap->bytes_in_use, dma_mode | DMA_AUTOINIT);
944         dmap->flags |= DMA_STARTED;
945         return count;
946 }
947
948 static void finish_output_interrupt(int dev, struct dma_buffparms *dmap)
949 {
950         struct audio_operations *adev = audio_devs[dev];
951
952         if (dmap->audio_callback != NULL)
953                 dmap->audio_callback(dev, dmap->callback_parm);
954         wake_up(&adev->out_sleeper);
955         wake_up(&adev->poll_sleeper);
956 }
957 /* called with dmap->lock held in irq context*/
958 static void do_outputintr(int dev, int dummy)
959 {
960         struct audio_operations *adev = audio_devs[dev];
961         struct dma_buffparms *dmap = adev->dmap_out;
962         int this_fragment;
963
964         if (dmap->raw_buf == NULL) {
965                 printk(KERN_ERR "Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev);
966                 return;
967         }
968         if (dmap->mapping_flags & DMA_MAP_MAPPED) {     /* Virtual memory mapped access */
969                 /* mmapped access */
970                 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
971                 if (dmap->qhead == 0) {     /* Wrapped */
972                         dmap->byte_counter += dmap->bytes_in_use;
973                         if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
974                                 long decr = dmap->byte_counter;
975                                 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
976                                 decr -= dmap->byte_counter;
977                                 dmap->user_counter -= decr;
978                         }
979                 }
980                 dmap->qlen++;   /* Yes increment it (don't decrement) */
981                 if (!(adev->flags & DMA_AUTOMODE))
982                         dmap->flags &= ~DMA_ACTIVE;
983                 dmap->counts[dmap->qhead] = dmap->fragment_size;
984                 DMAbuf_launch_output(dev, dmap);
985                 finish_output_interrupt(dev, dmap);
986                 return;
987         }
988
989         dmap->qlen--;
990         this_fragment = dmap->qhead;
991         dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
992
993         if (dmap->qhead == 0) { /* Wrapped */
994                 dmap->byte_counter += dmap->bytes_in_use;
995                 if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
996                         long decr = dmap->byte_counter;
997                         dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use);
998                         decr -= dmap->byte_counter;
999                         dmap->user_counter -= decr;
1000                 }
1001         }
1002         if (!(adev->flags & DMA_AUTOMODE))
1003                 dmap->flags &= ~DMA_ACTIVE;
1004                 
1005         /*
1006          *      This is  dmap->qlen <= 0 except when closing when
1007          *      dmap->qlen < 0
1008          */
1009          
1010         while (dmap->qlen <= -dmap->closing) {
1011                 dmap->underrun_count++;
1012                 dmap->qlen++;
1013                 if ((dmap->flags & DMA_DIRTY) && dmap->applic_profile != APF_CPUINTENS) {
1014                         dmap->flags &= ~DMA_DIRTY;
1015                         memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
1016                                adev->dmap_out->buffsize);
1017                 }
1018                 dmap->user_counter += dmap->fragment_size;
1019                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1020         }
1021         if (dmap->qlen > 0)
1022                 DMAbuf_launch_output(dev, dmap);
1023         finish_output_interrupt(dev, dmap);
1024 }
1025 /* called in irq context */
1026 void DMAbuf_outputintr(int dev, int notify_only)
1027 {
1028         struct audio_operations *adev = audio_devs[dev];
1029         unsigned long flags;
1030         struct dma_buffparms *dmap = adev->dmap_out;
1031
1032         spin_lock_irqsave(&dmap->lock,flags);
1033         if (!(dmap->flags & DMA_NODMA)) {
1034                 int chan = dmap->dma, pos, n;
1035                 unsigned long f;
1036                 
1037                 f=claim_dma_lock();
1038                 
1039                 if(!isa_dma_bridge_buggy)
1040                         disable_dma(dmap->dma);
1041                 clear_dma_ff(chan);
1042                 pos = dmap->bytes_in_use - get_dma_residue(chan);
1043                 if(!isa_dma_bridge_buggy)
1044                         enable_dma(dmap->dma);
1045                 release_dma_lock(f);
1046                 
1047                 pos = pos / dmap->fragment_size;        /* Actual qhead */
1048                 if (pos < 0 || pos >= dmap->nbufs)
1049                         pos = 0;
1050                 n = 0;
1051                 while (dmap->qhead != pos && n++ < dmap->nbufs)
1052                         do_outputintr(dev, notify_only);
1053         }
1054         else
1055                 do_outputintr(dev, notify_only);
1056         spin_unlock_irqrestore(&dmap->lock,flags);
1057 }
1058 /* called with dmap->lock held in irq context */
1059 static void do_inputintr(int dev)
1060 {
1061         struct audio_operations *adev = audio_devs[dev];
1062         struct dma_buffparms *dmap = adev->dmap_in;
1063
1064         if (dmap->raw_buf == NULL) {
1065                 printk(KERN_ERR "Sound: Fatal error. Audio interrupt after freeing buffers.\n");
1066                 return;
1067         }
1068         if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1069                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1070                 if (dmap->qtail == 0) {         /* Wrapped */
1071                         dmap->byte_counter += dmap->bytes_in_use;
1072                         if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
1073                                 long decr = dmap->byte_counter;
1074                                 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1075                                 decr -= dmap->byte_counter;
1076                                 dmap->user_counter -= decr;
1077                         }
1078                 }
1079                 dmap->qlen++;
1080
1081                 if (!(adev->flags & DMA_AUTOMODE)) {
1082                         if (dmap->needs_reorg)
1083                                 reorganize_buffers(dev, dmap, 0);
1084                         local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_READ);
1085                         adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
1086                                              dmap->fragment_size, 1);
1087                         if (adev->d->trigger)
1088                                 adev->d->trigger(dev, adev->enable_bits * adev->go);
1089                 }
1090                 dmap->flags |= DMA_ACTIVE;
1091         } else if (dmap->qlen >= (dmap->nbufs - 1)) {
1092                 printk(KERN_WARNING "Sound: Recording overrun\n");
1093                 dmap->underrun_count++;
1094
1095                 /* Just throw away the oldest fragment but keep the engine running */
1096                 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
1097                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1098         } else if (dmap->qlen >= 0 && dmap->qlen < dmap->nbufs) {
1099                 dmap->qlen++;
1100                 dmap->qtail = (dmap->qtail + 1) % dmap->nbufs;
1101                 if (dmap->qtail == 0) {         /* Wrapped */
1102                         dmap->byte_counter += dmap->bytes_in_use;
1103                         if (dmap->byte_counter >= dmap->max_byte_counter) {     /* Overflow */
1104                                 long decr = dmap->byte_counter;
1105                                 dmap->byte_counter = (dmap->byte_counter % dmap->bytes_in_use) + dmap->bytes_in_use;
1106                                 decr -= dmap->byte_counter;
1107                                 dmap->user_counter -= decr;
1108                         }
1109                 }
1110         }
1111         if (!(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
1112                 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
1113                 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size, dmap->fragment_size, 1);
1114                 if (adev->d->trigger)
1115                         adev->d->trigger(dev,adev->enable_bits * adev->go);
1116         }
1117         dmap->flags |= DMA_ACTIVE;
1118         if (dmap->qlen > 0)
1119         {
1120                 wake_up(&adev->in_sleeper);
1121                 wake_up(&adev->poll_sleeper);
1122         }
1123 }
1124 /* called in irq context */
1125 void DMAbuf_inputintr(int dev)
1126 {
1127         struct audio_operations *adev = audio_devs[dev];
1128         struct dma_buffparms *dmap = adev->dmap_in;
1129         unsigned long flags;
1130
1131         spin_lock_irqsave(&dmap->lock,flags);
1132
1133         if (!(dmap->flags & DMA_NODMA)) {
1134                 int chan = dmap->dma, pos, n;
1135                 unsigned long f;
1136                 
1137                 f=claim_dma_lock();
1138                 if(!isa_dma_bridge_buggy)
1139                         disable_dma(dmap->dma);
1140                 clear_dma_ff(chan);
1141                 pos = dmap->bytes_in_use - get_dma_residue(chan);
1142                 if(!isa_dma_bridge_buggy)
1143                         enable_dma(dmap->dma);
1144                 release_dma_lock(f);
1145
1146                 pos = pos / dmap->fragment_size;        /* Actual qhead */
1147                 if (pos < 0 || pos >= dmap->nbufs)
1148                         pos = 0;
1149
1150                 n = 0;
1151                 while (dmap->qtail != pos && ++n < dmap->nbufs)
1152                         do_inputintr(dev);
1153         } else
1154                 do_inputintr(dev);
1155         spin_unlock_irqrestore(&dmap->lock,flags);
1156 }
1157
1158 int DMAbuf_open_dma(int dev)
1159 {
1160         /*
1161          *    NOTE!  This routine opens only the primary DMA channel (output).
1162          */
1163         struct audio_operations *adev = audio_devs[dev];
1164         int err;
1165
1166         if ((err = open_dmap(adev, OPEN_READWRITE, adev->dmap_out)) < 0)
1167                 return -EBUSY;
1168         dma_init_buffers(adev->dmap_out);
1169         adev->dmap_out->flags |= DMA_ALLOC_DONE;
1170         adev->dmap_out->fragment_size = adev->dmap_out->buffsize;
1171
1172         if (adev->dmap_out->dma >= 0) {
1173                 unsigned long flags;
1174
1175                 flags=claim_dma_lock();
1176                 clear_dma_ff(adev->dmap_out->dma);
1177                 disable_dma(adev->dmap_out->dma);
1178                 release_dma_lock(flags);
1179         }
1180         return 0;
1181 }
1182
1183 void DMAbuf_close_dma(int dev)
1184 {
1185         close_dmap(audio_devs[dev], audio_devs[dev]->dmap_out);
1186 }
1187
1188 void DMAbuf_init(int dev, int dma1, int dma2)
1189 {
1190         struct audio_operations *adev = audio_devs[dev];
1191         /*
1192          * NOTE! This routine could be called several times.
1193          */
1194
1195         /* drag in audio_syms.o */
1196         {
1197                 extern char audio_syms_symbol;
1198                 audio_syms_symbol = 0;
1199         }
1200
1201         if (adev && adev->dmap_out == NULL) {
1202                 if (adev->d == NULL)
1203                         panic("OSS: audio_devs[%d]->d == NULL\n", dev);
1204
1205                 if (adev->parent_dev) {  /* Use DMA map of the parent dev */
1206                         int parent = adev->parent_dev - 1;
1207                         adev->dmap_out = audio_devs[parent]->dmap_out;
1208                         adev->dmap_in = audio_devs[parent]->dmap_in;
1209                 } else {
1210                         adev->dmap_out = adev->dmap_in = &adev->dmaps[0];
1211                         adev->dmap_out->dma = dma1;
1212                         if (adev->flags & DMA_DUPLEX) {
1213                                 adev->dmap_in = &adev->dmaps[1];
1214                                 adev->dmap_in->dma = dma2;
1215                         }
1216                 }
1217                 /* Persistent DMA buffers allocated here */
1218                 if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1219                         if (adev->dmap_in->raw_buf == NULL)
1220                                 sound_alloc_dmap(adev->dmap_in);
1221                         if (adev->dmap_out->raw_buf == NULL)
1222                                 sound_alloc_dmap(adev->dmap_out);
1223                 }
1224         }
1225 }
1226
1227 /* No kernel lock - DMAbuf_activate_recording protected by global cli/sti */
1228 static unsigned int poll_input(struct file * file, int dev, poll_table *wait)
1229 {
1230         struct audio_operations *adev = audio_devs[dev];
1231         struct dma_buffparms *dmap = adev->dmap_in;
1232
1233         if (!(adev->open_mode & OPEN_READ))
1234                 return 0;
1235         if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1236                 if (dmap->qlen)
1237                         return POLLIN | POLLRDNORM;
1238                 return 0;
1239         }
1240         if (dmap->dma_mode != DMODE_INPUT) {
1241                 if (dmap->dma_mode == DMODE_NONE &&
1242                     adev->enable_bits & PCM_ENABLE_INPUT &&
1243                     !dmap->qlen && adev->go) {
1244                         unsigned long flags;
1245                         
1246                         spin_lock_irqsave(&dmap->lock,flags);
1247                         DMAbuf_activate_recording(dev, dmap);
1248                         spin_unlock_irqrestore(&dmap->lock,flags);
1249                 }
1250                 return 0;
1251         }
1252         if (!dmap->qlen)
1253                 return 0;
1254         return POLLIN | POLLRDNORM;
1255 }
1256
1257 static unsigned int poll_output(struct file * file, int dev, poll_table *wait)
1258 {
1259         struct audio_operations *adev = audio_devs[dev];
1260         struct dma_buffparms *dmap = adev->dmap_out;
1261         
1262         if (!(adev->open_mode & OPEN_WRITE))
1263                 return 0;
1264         if (dmap->mapping_flags & DMA_MAP_MAPPED) {
1265                 if (dmap->qlen)
1266                         return POLLOUT | POLLWRNORM;
1267                 return 0;
1268         }
1269         if (dmap->dma_mode == DMODE_INPUT)
1270                 return 0;
1271         if (dmap->dma_mode == DMODE_NONE)
1272                 return POLLOUT | POLLWRNORM;
1273         if (!DMAbuf_space_in_queue(dev))
1274                 return 0;
1275         return POLLOUT | POLLWRNORM;
1276 }
1277
1278 unsigned int DMAbuf_poll(struct file * file, int dev, poll_table *wait)
1279 {
1280         struct audio_operations *adev = audio_devs[dev];
1281         poll_wait(file, &adev->poll_sleeper, wait);
1282         return poll_input(file, dev, wait) | poll_output(file, dev, wait);
1283 }
1284
1285 void DMAbuf_deinit(int dev)
1286 {
1287         struct audio_operations *adev = audio_devs[dev];
1288         /* This routine is called when driver is being unloaded */
1289         if (!adev)
1290                 return;
1291
1292         /* Persistent DMA buffers deallocated here */
1293         if (sound_dmap_flag == DMAP_KEEP_ON_CLOSE) {
1294                 sound_free_dmap(adev->dmap_out);
1295                 if (adev->flags & DMA_DUPLEX)
1296                         sound_free_dmap(adev->dmap_in);
1297         }
1298 }