~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/oss/dmabuf.c

Version: ~ [ linux-5.12-rc1 ] ~ [ linux-5.11.2 ] ~ [ linux-5.10.19 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.101 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.177 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.222 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.258 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.258 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp