blob: bcc3e8e071229db29775632dc6a7380aeda0f6f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Uwe Zeisbergerf30c2262006-10-03 23:01:26 +02002 * sound/oss/dmabuf.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Al Virof23f6e02006-10-20 15:17:02 -040028#include <linux/mm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "sound_config.h"
31
32#define DMAP_FREE_ON_CLOSE 0
33#define DMAP_KEEP_ON_CLOSE 1
34extern int sound_dmap_flag;
35
36static void dma_reset_output(int dev);
37static void dma_reset_input(int dev);
38static int local_start_dma(struct audio_operations *adev, unsigned long physaddr, int count, int dma_mode);
39
40
41
42static int debugmem; /* switched off by default */
43static int dma_buffsize = DSP_BUFFSIZE;
44
45static long dmabuf_timeout(struct dma_buffparms *dmap)
46{
47 long tmout;
48
49 tmout = (dmap->fragment_size * HZ) / dmap->data_rate;
50 tmout += HZ / 5; /* Some safety distance */
51 if (tmout < (HZ / 2))
52 tmout = HZ / 2;
53 if (tmout > 20 * HZ)
54 tmout = 20 * HZ;
55 return tmout;
56}
57
58static int sound_alloc_dmap(struct dma_buffparms *dmap)
59{
60 char *start_addr, *end_addr;
61 int dma_pagesize;
62 int sz, size;
63 struct page *page;
64
65 dmap->mapping_flags &= ~DMA_MAP_MAPPED;
66
67 if (dmap->raw_buf != NULL)
68 return 0; /* Already done */
69 if (dma_buffsize < 4096)
70 dma_buffsize = 4096;
71 dma_pagesize = (dmap->dma < 4) ? (64 * 1024) : (128 * 1024);
72
73 /*
74 * Now check for the Cyrix problem.
75 */
76
77 if(isa_dma_bridge_buggy==2)
78 dma_pagesize=32768;
79
80 dmap->raw_buf = NULL;
81 dmap->buffsize = dma_buffsize;
82 if (dmap->buffsize > dma_pagesize)
83 dmap->buffsize = dma_pagesize;
84 start_addr = NULL;
85 /*
86 * Now loop until we get a free buffer. Try to get smaller buffer if
87 * it fails. Don't accept smaller than 8k buffer for performance
88 * reasons.
89 */
90 while (start_addr == NULL && dmap->buffsize > PAGE_SIZE) {
91 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
92 dmap->buffsize = PAGE_SIZE * (1 << sz);
93 start_addr = (char *) __get_free_pages(GFP_ATOMIC|GFP_DMA|__GFP_NOWARN, sz);
94 if (start_addr == NULL)
95 dmap->buffsize /= 2;
96 }
97
98 if (start_addr == NULL) {
99 printk(KERN_WARNING "Sound error: Couldn't allocate DMA buffer\n");
100 return -ENOMEM;
101 } else {
102 /* make some checks */
103 end_addr = start_addr + dmap->buffsize - 1;
104
105 if (debugmem)
106 printk(KERN_DEBUG "sound: start 0x%lx, end 0x%lx\n", (long) start_addr, (long) end_addr);
107
108 /* now check if it fits into the same dma-pagesize */
109
110 if (((long) start_addr & ~(dma_pagesize - 1)) != ((long) end_addr & ~(dma_pagesize - 1))
111 || end_addr >= (char *) (MAX_DMA_ADDRESS)) {
112 printk(KERN_ERR "sound: Got invalid address 0x%lx for %db DMA-buffer\n", (long) start_addr, dmap->buffsize);
113 return -EFAULT;
114 }
115 }
116 dmap->raw_buf = start_addr;
117 dmap->raw_buf_phys = virt_to_bus(start_addr);
118
119 for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
120 SetPageReserved(page);
121 return 0;
122}
123
124static void sound_free_dmap(struct dma_buffparms *dmap)
125{
126 int sz, size;
127 struct page *page;
128 unsigned long start_addr, end_addr;
129
130 if (dmap->raw_buf == NULL)
131 return;
132 if (dmap->mapping_flags & DMA_MAP_MAPPED)
133 return; /* Don't free mmapped buffer. Will use it next time */
134 for (sz = 0, size = PAGE_SIZE; size < dmap->buffsize; sz++, size <<= 1);
135
136 start_addr = (unsigned long) dmap->raw_buf;
137 end_addr = start_addr + dmap->buffsize;
138
139 for (page = virt_to_page(start_addr); page <= virt_to_page(end_addr); page++)
140 ClearPageReserved(page);
141
142 free_pages((unsigned long) dmap->raw_buf, sz);
143 dmap->raw_buf = NULL;
144}
145
146
147/* Intel version !!!!!!!!! */
148
149static int sound_start_dma(struct dma_buffparms *dmap, unsigned long physaddr, int count, int dma_mode)
150{
151 unsigned long flags;
152 int chan = dmap->dma;
153
154 /* printk( "Start DMA%d %d, %d\n", chan, (int)(physaddr-dmap->raw_buf_phys), count); */
155
156 flags = claim_dma_lock();
157 disable_dma(chan);
158 clear_dma_ff(chan);
159 set_dma_mode(chan, dma_mode);
160 set_dma_addr(chan, physaddr);
161 set_dma_count(chan, count);
162 enable_dma(chan);
163 release_dma_lock(flags);
164
165 return 0;
166}
167
168static void dma_init_buffers(struct dma_buffparms *dmap)
169{
170 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
171 dmap->byte_counter = 0;
172 dmap->max_byte_counter = 8000 * 60 * 60;
173 dmap->bytes_in_use = dmap->buffsize;
174
175 dmap->dma_mode = DMODE_NONE;
176 dmap->mapping_flags = 0;
177 dmap->neutral_byte = 0x80;
178 dmap->data_rate = 8000;
179 dmap->cfrag = -1;
180 dmap->closing = 0;
181 dmap->nbufs = 1;
182 dmap->flags = DMA_BUSY; /* Other flags off */
183}
184
185static int open_dmap(struct audio_operations *adev, int mode, struct dma_buffparms *dmap)
186{
187 int err;
188
189 if (dmap->flags & DMA_BUSY)
190 return -EBUSY;
191 if ((err = sound_alloc_dmap(dmap)) < 0)
192 return err;
193
194 if (dmap->raw_buf == NULL) {
195 printk(KERN_WARNING "Sound: DMA buffers not available\n");
196 return -ENOSPC; /* Memory allocation failed during boot */
197 }
198 if (dmap->dma >= 0 && sound_open_dma(dmap->dma, adev->name)) {
199 printk(KERN_WARNING "Unable to grab(2) DMA%d for the audio driver\n", dmap->dma);
200 return -EBUSY;
201 }
202 dma_init_buffers(dmap);
203 spin_lock_init(&dmap->lock);
204 dmap->open_mode = mode;
205 dmap->subdivision = dmap->underrun_count = 0;
206 dmap->fragment_size = 0;
207 dmap->max_fragments = 65536; /* Just a large value */
208 dmap->byte_counter = 0;
209 dmap->max_byte_counter = 8000 * 60 * 60;
210 dmap->applic_profile = APF_NORMAL;
211 dmap->needs_reorg = 1;
212 dmap->audio_callback = NULL;
213 dmap->callback_parm = 0;
214 return 0;
215}
216
217static void close_dmap(struct audio_operations *adev, struct dma_buffparms *dmap)
218{
219 unsigned long flags;
220
221 if (dmap->dma >= 0) {
222 sound_close_dma(dmap->dma);
223 flags=claim_dma_lock();
224 disable_dma(dmap->dma);
225 release_dma_lock(flags);
226 }
227 if (dmap->flags & DMA_BUSY)
228 dmap->dma_mode = DMODE_NONE;
229 dmap->flags &= ~DMA_BUSY;
230
231 if (sound_dmap_flag == DMAP_FREE_ON_CLOSE)
232 sound_free_dmap(dmap);
233}
234
235
236static unsigned int default_set_bits(int dev, unsigned int bits)
237{
238 mm_segment_t fs = get_fs();
239
240 set_fs(get_ds());
241 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SETFMT, (void __user *)&bits);
242 set_fs(fs);
243 return bits;
244}
245
246static int default_set_speed(int dev, int speed)
247{
248 mm_segment_t fs = get_fs();
249
250 set_fs(get_ds());
251 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_SPEED, (void __user *)&speed);
252 set_fs(fs);
253 return speed;
254}
255
256static short default_set_channels(int dev, short channels)
257{
258 int c = channels;
259 mm_segment_t fs = get_fs();
260
261 set_fs(get_ds());
262 audio_devs[dev]->d->ioctl(dev, SNDCTL_DSP_CHANNELS, (void __user *)&c);
263 set_fs(fs);
264 return c;
265}
266
267static void check_driver(struct audio_driver *d)
268{
269 if (d->set_speed == NULL)
270 d->set_speed = default_set_speed;
271 if (d->set_bits == NULL)
272 d->set_bits = default_set_bits;
273 if (d->set_channels == NULL)
274 d->set_channels = default_set_channels;
275}
276
277int DMAbuf_open(int dev, int mode)
278{
279 struct audio_operations *adev = audio_devs[dev];
280 int retval;
281 struct dma_buffparms *dmap_in = NULL;
282 struct dma_buffparms *dmap_out = NULL;
283
284 if (!adev)
285 return -ENXIO;
286 if (!(adev->flags & DMA_DUPLEX))
287 adev->dmap_in = adev->dmap_out;
288 check_driver(adev->d);
289
290 if ((retval = adev->d->open(dev, mode)) < 0)
291 return retval;
292 dmap_out = adev->dmap_out;
293 dmap_in = adev->dmap_in;
294 if (dmap_in == dmap_out)
295 adev->flags &= ~DMA_DUPLEX;
296
297 if (mode & OPEN_WRITE) {
298 if ((retval = open_dmap(adev, mode, dmap_out)) < 0) {
299 adev->d->close(dev);
300 return retval;
301 }
302 }
303 adev->enable_bits = mode;
304
305 if (mode == OPEN_READ || (mode != OPEN_WRITE && (adev->flags & DMA_DUPLEX))) {
306 if ((retval = open_dmap(adev, mode, dmap_in)) < 0) {
307 adev->d->close(dev);
308 if (mode & OPEN_WRITE)
309 close_dmap(adev, dmap_out);
310 return retval;
311 }
312 }
313 adev->open_mode = mode;
314 adev->go = 1;
315
316 adev->d->set_bits(dev, 8);
317 adev->d->set_channels(dev, 1);
318 adev->d->set_speed(dev, DSP_DEFAULT_SPEED);
319 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
320 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte,
321 adev->dmap_out->bytes_in_use);
322 return 0;
323}
324/* MUST not hold the spinlock */
325void DMAbuf_reset(int dev)
326{
327 if (audio_devs[dev]->open_mode & OPEN_WRITE)
328 dma_reset_output(dev);
329
330 if (audio_devs[dev]->open_mode & OPEN_READ)
331 dma_reset_input(dev);
332}
333
334static void dma_reset_output(int dev)
335{
336 struct audio_operations *adev = audio_devs[dev];
337 unsigned long flags,f ;
338 struct dma_buffparms *dmap = adev->dmap_out;
339
340 if (!(dmap->flags & DMA_STARTED)) /* DMA is not active */
341 return;
342
343 /*
344 * First wait until the current fragment has been played completely
345 */
346 spin_lock_irqsave(&dmap->lock,flags);
347 adev->dmap_out->flags |= DMA_SYNCING;
348
349 adev->dmap_out->underrun_count = 0;
350 if (!signal_pending(current) && adev->dmap_out->qlen &&
351 adev->dmap_out->underrun_count == 0){
352 spin_unlock_irqrestore(&dmap->lock,flags);
353 interruptible_sleep_on_timeout(&adev->out_sleeper,
354 dmabuf_timeout(dmap));
355 spin_lock_irqsave(&dmap->lock,flags);
356 }
357 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
358
359 /*
360 * Finally shut the device off
361 */
362 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_output)
363 adev->d->halt_io(dev);
364 else
365 adev->d->halt_output(dev);
366 adev->dmap_out->flags &= ~DMA_STARTED;
367
368 f=claim_dma_lock();
369 clear_dma_ff(dmap->dma);
370 disable_dma(dmap->dma);
371 release_dma_lock(f);
372
373 dmap->byte_counter = 0;
374 reorganize_buffers(dev, adev->dmap_out, 0);
375 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
376 spin_unlock_irqrestore(&dmap->lock,flags);
377}
378
379static void dma_reset_input(int dev)
380{
381 struct audio_operations *adev = audio_devs[dev];
382 unsigned long flags;
383 struct dma_buffparms *dmap = adev->dmap_in;
384
385 spin_lock_irqsave(&dmap->lock,flags);
386 if (!(adev->flags & DMA_DUPLEX) || !adev->d->halt_input)
387 adev->d->halt_io(dev);
388 else
389 adev->d->halt_input(dev);
390 adev->dmap_in->flags &= ~DMA_STARTED;
391
392 dmap->qlen = dmap->qhead = dmap->qtail = dmap->user_counter = 0;
393 dmap->byte_counter = 0;
394 reorganize_buffers(dev, adev->dmap_in, 1);
395 spin_unlock_irqrestore(&dmap->lock,flags);
396}
397/* MUST be called with holding the dmap->lock */
398void DMAbuf_launch_output(int dev, struct dma_buffparms *dmap)
399{
400 struct audio_operations *adev = audio_devs[dev];
401
402 if (!((adev->enable_bits * adev->go) & PCM_ENABLE_OUTPUT))
403 return; /* Don't start DMA yet */
404 dmap->dma_mode = DMODE_OUTPUT;
405
406 if (!(dmap->flags & DMA_ACTIVE) || !(adev->flags & DMA_AUTOMODE) || (dmap->flags & DMA_NODMA)) {
407 if (!(dmap->flags & DMA_STARTED)) {
408 reorganize_buffers(dev, dmap, 0);
409 if (adev->d->prepare_for_output(dev, dmap->fragment_size, dmap->nbufs))
410 return;
411 if (!(dmap->flags & DMA_NODMA))
412 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use,DMA_MODE_WRITE);
413 dmap->flags |= DMA_STARTED;
414 }
415 if (dmap->counts[dmap->qhead] == 0)
416 dmap->counts[dmap->qhead] = dmap->fragment_size;
417 dmap->dma_mode = DMODE_OUTPUT;
418 adev->d->output_block(dev, dmap->raw_buf_phys + dmap->qhead * dmap->fragment_size,
419 dmap->counts[dmap->qhead], 1);
420 if (adev->d->trigger)
421 adev->d->trigger(dev,adev->enable_bits * adev->go);
422 }
423 dmap->flags |= DMA_ACTIVE;
424}
425
426int DMAbuf_sync(int dev)
427{
428 struct audio_operations *adev = audio_devs[dev];
429 unsigned long flags;
430 int n = 0;
431 struct dma_buffparms *dmap;
432
433 if (!adev->go && !(adev->enable_bits & PCM_ENABLE_OUTPUT))
434 return 0;
435
436 if (adev->dmap_out->dma_mode == DMODE_OUTPUT) {
437 dmap = adev->dmap_out;
438 spin_lock_irqsave(&dmap->lock,flags);
439 if (dmap->qlen > 0 && !(dmap->flags & DMA_ACTIVE))
440 DMAbuf_launch_output(dev, dmap);
441 adev->dmap_out->flags |= DMA_SYNCING;
442 adev->dmap_out->underrun_count = 0;
Roel Kluin1afa6e22009-02-11 13:53:26 +0100443 while (!signal_pending(current) && n++ < adev->dmap_out->nbufs &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 adev->dmap_out->qlen && adev->dmap_out->underrun_count == 0) {
445 long t = dmabuf_timeout(dmap);
446 spin_unlock_irqrestore(&dmap->lock,flags);
447 /* FIXME: not safe may miss events */
448 t = interruptible_sleep_on_timeout(&adev->out_sleeper, t);
449 spin_lock_irqsave(&dmap->lock,flags);
450 if (!t) {
451 adev->dmap_out->flags &= ~DMA_SYNCING;
452 spin_unlock_irqrestore(&dmap->lock,flags);
453 return adev->dmap_out->qlen;
454 }
455 }
456 adev->dmap_out->flags &= ~(DMA_SYNCING | DMA_ACTIVE);
457
458 /*
459 * Some devices such as GUS have huge amount of on board RAM for the
460 * audio data. We have to wait until the device has finished playing.
461 */
462
463 /* still holding the lock */
464 if (adev->d->local_qlen) { /* Device has hidden buffers */
465 while (!signal_pending(current) &&
466 adev->d->local_qlen(dev)){
467 spin_unlock_irqrestore(&dmap->lock,flags);
468 interruptible_sleep_on_timeout(&adev->out_sleeper,
469 dmabuf_timeout(dmap));
470 spin_lock_irqsave(&dmap->lock,flags);
471 }
472 }
473 spin_unlock_irqrestore(&dmap->lock,flags);
474 }
475 adev->dmap_out->dma_mode = DMODE_NONE;
476 return adev->dmap_out->qlen;
477}
478
479int DMAbuf_release(int dev, int mode)
480{
481 struct audio_operations *adev = audio_devs[dev];
482 struct dma_buffparms *dmap;
483 unsigned long flags;
484
485 dmap = adev->dmap_out;
486 if (adev->open_mode & OPEN_WRITE)
487 adev->dmap_out->closing = 1;
488
489 if (adev->open_mode & OPEN_READ){
490 adev->dmap_in->closing = 1;
491 dmap = adev->dmap_in;
492 }
493 if (adev->open_mode & OPEN_WRITE)
494 if (!(adev->dmap_out->mapping_flags & DMA_MAP_MAPPED))
495 if (!signal_pending(current) && (adev->dmap_out->dma_mode == DMODE_OUTPUT))
496 DMAbuf_sync(dev);
497 if (adev->dmap_out->dma_mode == DMODE_OUTPUT)
498 memset(adev->dmap_out->raw_buf, adev->dmap_out->neutral_byte, adev->dmap_out->bytes_in_use);
499
500 DMAbuf_reset(dev);
501 spin_lock_irqsave(&dmap->lock,flags);
502 adev->d->close(dev);
503
504 if (adev->open_mode & OPEN_WRITE)
505 close_dmap(adev, adev->dmap_out);
506
507 if (adev->open_mode == OPEN_READ ||
508 (adev->open_mode != OPEN_WRITE &&
509 (adev->flags & DMA_DUPLEX)))
510 close_dmap(adev, adev->dmap_in);
511 adev->open_mode = 0;
512 spin_unlock_irqrestore(&dmap->lock,flags);
513 return 0;
514}
515/* called with dmap->lock dold */
516int DMAbuf_activate_recording(int dev, struct dma_buffparms *dmap)
517{
518 struct audio_operations *adev = audio_devs[dev];
519 int err;
520
521 if (!(adev->open_mode & OPEN_READ))
522 return 0;
523 if (!(adev->enable_bits & PCM_ENABLE_INPUT))
524 return 0;
525 if (dmap->dma_mode == DMODE_OUTPUT) { /* Direction change */
526 /* release lock - it's not recursive */
527 spin_unlock_irq(&dmap->lock);
528 DMAbuf_sync(dev);
529 DMAbuf_reset(dev);
530 spin_lock_irq(&dmap->lock);
531 dmap->dma_mode = DMODE_NONE;
532 }
533 if (!dmap->dma_mode) {
534 reorganize_buffers(dev, dmap, 1);
535 if ((err = adev->d->prepare_for_input(dev,
536 dmap->fragment_size, dmap->nbufs)) < 0)
537 return err;
538 dmap->dma_mode = DMODE_INPUT;
539 }
540 if (!(dmap->flags & DMA_ACTIVE)) {
541 if (dmap->needs_reorg)
542 reorganize_buffers(dev, dmap, 0);
543 local_start_dma(adev, dmap->raw_buf_phys, dmap->bytes_in_use, DMA_MODE_READ);
544 adev->d->start_input(dev, dmap->raw_buf_phys + dmap->qtail * dmap->fragment_size,
545 dmap->fragment_size, 0);
546 dmap->flags |= DMA_ACTIVE;
547 if (adev->d->trigger)
548 adev->d->trigger(dev, adev->enable_bits * adev->go);
549 }
550 return 0;
551}
Adrian Bunk04187262006-06-30 18:23:04 +0200552/* acquires lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553int DMAbuf_getrdbuffer(int dev, char **buf, int *len, int dontblock)
554{
555 struct audio_operations *adev = audio_devs[dev];
556 unsigned long flags;
557 int err = 0, n = 0;
558 struct dma_buffparms *dmap = adev->dmap_in;
559 int go;
560
561 if (!(adev->open_mode & OPEN_READ))
562 return -EIO;
563 spin_lock_irqsave(&dmap->lock,flags);
564 if (dmap->needs_reorg)
565 reorganize_buffers(dev, dmap, 0);
566 if (adev->dmap_in->mapping_flags & DMA_MAP_MAPPED) {
567/* printk(KERN_WARNING "Sound: Can't read from mmapped device (1)\n");*/
568 spin_unlock_irqrestore(&dmap->lock,flags);
569 return -EINVAL;
570 } else while (dmap->qlen <= 0 && n++ < 10) {
571 long timeout = MAX_SCHEDULE_TIMEOUT;
572 if (!(adev->enable_bits & PCM_ENABLE_INPUT) || !adev->go) {
573 spin_unlock_irqrestore(&dmap->lock,flags);
574 return -EAGAIN;
575 }
576 if ((err = DMAbuf_activate_recording(dev, dmap)) < 0) {
577 spin_unlock_irqrestore(&dmap->lock,flags);
578 return err;
579 }
580 /* Wait for the next block */
581
582 if (dontblock) {
583 spin_unlock_irqrestore(&dmap->lock,flags);
584 return -EAGAIN;
585 }
586 if ((go = adev->go))
587 timeout = dmabuf_timeout(dmap);
588
589 spin_unlock_irqrestore(&dmap->lock,flags);
590 timeout = interruptible_sleep_on_timeout(&adev->in_sleeper,
591 timeout);
592 if (!timeout) {
593 /* FIXME: include device name */
594 err = -EIO;
595 printk(KERN_WARNING "Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
596 dma_reset_input(dev);
597 } else
598 err = -EINTR;
599 spin_lock_irqsave(&dmap->lock,flags);
600 }
601 spin_unlock_irqrestore(&dmap->lock,flags);
602
603 if (dmap->qlen <= 0)
604 return err ? err : -EINTR;
605 *buf = &dmap->raw_buf[dmap->qhead * dmap->fragment_size + dmap->counts[dmap->qhead]];
606 *len = dmap->fragment_size - dmap->counts[dmap->qhead];
607
608 return dmap->qhead;
609}
610
611int DMAbuf_rmchars(int dev, int buff_no, int c)
612{
613 struct audio_operations *adev = audio_devs[dev];
614 struct dma_buffparms *dmap = adev->dmap_in;
615 int p = dmap->counts[dmap->qhead] + c;
616
617 if (dmap->mapping_flags & DMA_MAP_MAPPED)
618 {
619/* printk("Sound: Can't read from mmapped device (2)\n");*/
620 return -EINVAL;
621 }
622 else if (dmap->qlen <= 0)
623 return -EIO;
624 else if (p >= dmap->fragment_size) { /* This buffer is completely empty */
625 dmap->counts[dmap->qhead] = 0;
626 dmap->qlen--;
627 dmap->qhead = (dmap->qhead + 1) % dmap->nbufs;
628 }
629 else dmap->counts[dmap->qhead] = p;
630
631 return 0;
632}
633/* MUST be called with dmap->lock hold */
634int DMAbuf_get_buffer_pointer(int dev, struct dma_buffparms *dmap, int direction)
635{
636 /*
637 * Try to approximate the active byte position of the DMA pointer within the
638 * buffer area as well as possible.
639 */
640
641 int pos;
642 unsigned long f;
643
644 if (!(dmap->flags & DMA_ACTIVE))
645 pos = 0;
646 else {
647 int chan = dmap->dma;
648
649 f=claim_dma_lock();
650 clear_dma_ff(chan);
651
652 if(!isa_dma_bridge_buggy)
653 disable_dma(dmap->dma);
654
655 pos = get_dma_residue(chan);
656
657 pos = dmap->bytes_in_use - pos;
658
659 if (!(dmap->mapping_flags & DMA_MAP_MAPPED)) {
660 if (direction == DMODE_OUTPUT) {
661 if (dmap->qhead == 0)
662 if (pos > dmap->fragment_size)
663 pos = 0;
664 } else {
665 if (dmap->qtail == 0)
666 if (pos > dmap->fragment_size)
667 pos = 0;
668 }
669 }
670 if (pos < 0)
671 pos = 0;
672 if (pos >= dmap->bytes_in_use)
673 pos = 0;
674
675 if(!isa_dma_bridge_buggy)
676 enable_dma(dmap->dma);
677
678 release_dma_lock(f);
679 }
680 /* printk( "%04x ", pos); */
681
682 return pos;
683}
684
685/*
686 * DMAbuf_start_devices() is called by the /dev/music driver to start
687 * one or more audio devices at desired moment.
688 */
689
690void DMAbuf_start_devices(unsigned int devmask)
691{
692 struct audio_operations *adev;
693 int dev;
694
695 for (dev = 0; dev < num_audiodevs; dev++) {
696 if (!(devmask & (1 << dev)))
697 continue;
698 if (!(adev = audio_devs[dev]))
699 continue;
700 if (adev->open_mode == 0)
701 continue;
702 if (adev->go)
703 continue;
704 /* OK to start the device */
705 adev->go = 1;
706 if (adev->d->trigger)
707 adev->d->trigger(dev,adev->enable_bits * adev->go);
708 }
709}
710/* via poll called without a lock ?*/
711int DMAbuf_space_in_queue(int dev)
712{
713 struct audio_operations *adev = audio_devs[dev];
714 int len, max, tmp;
715 struct dma_buffparms *dmap = adev->dmap_out;
716 int lim = dmap->nbufs;
717
718 if (lim < 2)
719 lim = 2;
720
721 if (dmap->qlen >= lim) /* No space at all */
722 return 0;
723
724 /*
725 * Verify that there are no more pending buffers than the limit
726 * defined by the process.
727 */
728
729 max = dmap->max_fragments;
730 if (max > lim)
731 max = lim;
732 len = dmap->qlen;
733
734 if (adev->d->local_qlen) {
735 tmp = adev->d->local_qlen(dev);
736 if (tmp && len)
737 tmp--; /* This buffer has been counted twice */
738 len += tmp;
739 }
740 if (dmap->byte_counter % dmap->fragment_size) /* There is a partial fragment */
741 len = len + 1;
742
743 if (len >= max)
744 return 0;
745 return max - len;
746}
747/* MUST not hold the spinlock - this function may sleep */
748static int output_sleep(int dev, int dontblock)
749{
750 struct audio_operations *adev = audio_devs[dev];
751 int err = 0;
752 struct dma_buffparms *dmap = adev->dmap_out;
753 long timeout;
754 long timeout_value;
755
756 if (dontblock)
757 return -EAGAIN;
758 if (!(adev->enable_bits & PCM_ENABLE_OUTPUT))
759 return -EAGAIN;
760
761 /*
762 * Wait for free space
763 */
764 if (signal_pending(current))
765 return -EINTR;
766 timeout = (adev->go && !(dmap->flags & DMA_NOTIMEOUT));
767 if (timeout)
768 timeout_value = dmabuf_timeout(dmap);
769 else
770 timeout_value = MAX_SCHEDULE_TIMEOUT;
771 timeout_value = interruptible_sleep_on_timeout(&adev->out_sleeper,
772 timeout_value);
773 if (timeout != MAX_SCHEDULE_TIMEOUT && !timeout_value) {
774 printk(KERN_WARNING "Sound: DMA (output) timed out - IRQ/DRQ config error?\n");
775 dma_reset_output(dev);
776 } else {
777 if (signal_pending(current))
778 err = -EINTR;
779 }
780 return err;
781}
782/* called with the lock held */
783static 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
Roel Kluin0d52cea2008-04-18 12:25:41 +0200799 active_offs = max(DMAbuf_get_buffer_pointer(dev, dmap, DMODE_OUTPUT), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Check for pointer wrapping situation */
Roel Kluin0d52cea2008-04-18 12:25:41 +0200801 if (active_offs >= dmap->bytes_in_use)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 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}
Adrian Bunk04187262006-06-30 18:23:04 +0200826/* acquires lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827int 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}
Adrian Bunk04187262006-06-30 18:23:04 +0200860/* has to acquire dmap->lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861int 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
916int 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}
Adrian Bunkece7f772006-10-04 02:17:31 -0700931EXPORT_SYMBOL(DMAbuf_start_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933static 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
951static 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*/
961static 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 */
1029void 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}
Adrian Bunkece7f772006-10-04 02:17:31 -07001061EXPORT_SYMBOL(DMAbuf_outputintr);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/* called with dmap->lock held in irq context */
1064static 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 */
1130void 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}
Adrian Bunkece7f772006-10-04 02:17:31 -07001162EXPORT_SYMBOL(DMAbuf_inputintr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 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 */
1198static 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
1227static 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
1248unsigned 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
1255void 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}