4 * The DMA buffer manager for digitized voice applications
7 * Copyright (C) by Hannu Savolainen 1993-1997
9 * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
10 * Version 2 (June 1991). See the "COPYING" file distributed with this software
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
17 * Thomas Sailer : remove {in,out}_sleep_flag. It was used for the sleeper to
18 * determine if it was woken up by the expiring timeout or by
19 * an explicit wake_up. The return value from schedule_timeout
20 * can be used instead; if 0, the wakeup was due to the timeout.
22 * Rob Riggs Added persistent DMA buffers (1998/10/17)
25 #define BE_CONSERVATIVE
26 #define SAMPLE_ROUNDUP 0
29 #include <linux/gfp.h>
30 #include <linux/sched/signal.h>
32 #include "sound_config.h"
35 #define DMAP_FREE_ON_CLOSE 0
36 #define DMAP_KEEP_ON_CLOSE 1
37 extern int sound_dmap_flag
;
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
);
45 static int debugmem
; /* switched off by default */
46 static int dma_buffsize
= DSP_BUFFSIZE
;
48 static long dmabuf_timeout(struct dma_buffparms
*dmap
)
52 tmout
= (dmap
->fragment_size
* HZ
) / dmap
->data_rate
;
53 tmout
+= HZ
/ 5; /* Some safety distance */
61 static int sound_alloc_dmap(struct dma_buffparms
*dmap
)
63 char *start_addr
, *end_addr
;
68 dmap
->mapping_flags
&= ~DMA_MAP_MAPPED
;
70 if (dmap
->raw_buf
!= NULL
)
71 return 0; /* Already done */
72 if (dma_buffsize
< 4096)
74 dma_pagesize
= (dmap
->dma
< 4) ? (64 * 1024) : (128 * 1024);
77 * Now check for the Cyrix problem.
80 if(isa_dma_bridge_buggy
==2)
84 dmap
->buffsize
= dma_buffsize
;
85 if (dmap
->buffsize
> dma_pagesize
)
86 dmap
->buffsize
= dma_pagesize
;
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
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
)
101 if (start_addr
== NULL
) {
102 printk(KERN_WARNING
"Sound error: Couldn't allocate DMA buffer\n");
105 /* make some checks */
106 end_addr
= start_addr
+ dmap
->buffsize
- 1;
109 printk(KERN_DEBUG
"sound: start 0x%lx, end 0x%lx\n", (long) start_addr
, (long) end_addr
);
111 /* now check if it fits into the same dma-pagesize */
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
);
119 dmap
->raw_buf
= start_addr
;
120 dmap
->raw_buf_phys
= dma_map_single(NULL
, start_addr
, dmap
->buffsize
, DMA_BIDIRECTIONAL
);
122 for (page
= virt_to_page(start_addr
); page
<= virt_to_page(end_addr
); page
++)
123 SetPageReserved(page
);
127 static void sound_free_dmap(struct dma_buffparms
*dmap
)
131 unsigned long start_addr
, end_addr
;
133 if (dmap
->raw_buf
== NULL
)
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);
139 start_addr
= (unsigned long) dmap
->raw_buf
;
140 end_addr
= start_addr
+ dmap
->buffsize
;
142 for (page
= virt_to_page(start_addr
); page
<= virt_to_page(end_addr
); page
++)
143 ClearPageReserved(page
);
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
;
151 /* Intel version !!!!!!!!! */
153 static int sound_start_dma(struct dma_buffparms
*dmap
, unsigned long physaddr
, int count
, int dma_mode
)
156 int chan
= dmap
->dma
;
158 /* printk( "Start DMA%d %d, %d\n", chan, (int)(physaddr-dmap->raw_buf_phys), count); */
160 flags
= claim_dma_lock();
163 set_dma_mode(chan
, dma_mode
);
164 set_dma_addr(chan
, physaddr
);
165 set_dma_count(chan
, count
);
167 release_dma_lock(flags
);
172 static void dma_init_buffers(struct dma_buffparms
*dmap
)
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
;
179 dmap
->dma_mode
= DMODE_NONE
;
180 dmap
->mapping_flags
= 0;
181 dmap
->neutral_byte
= 0x80;
182 dmap
->data_rate
= 8000;
186 dmap
->flags
= DMA_BUSY
; /* Other flags off */
189 static int open_dmap(struct audio_operations
*adev
, int mode
, struct dma_buffparms
*dmap
)
193 if (dmap
->flags
& DMA_BUSY
)
195 if ((err
= sound_alloc_dmap(dmap
)) < 0)
198 if (dmap
->raw_buf
== NULL
) {
199 printk(KERN_WARNING
"Sound: DMA buffers not available\n");
200 return -ENOSPC
; /* Memory allocation failed during boot */
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
);
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;
221 static void close_dmap(struct audio_operations
*adev
, struct dma_buffparms
*dmap
)
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
);
231 if (dmap
->flags
& DMA_BUSY
)
232 dmap
->dma_mode
= DMODE_NONE
;
233 dmap
->flags
&= ~DMA_BUSY
;
235 if (sound_dmap_flag
== DMAP_FREE_ON_CLOSE
)
236 sound_free_dmap(dmap
);
240 static unsigned int default_set_bits(int dev
, unsigned int bits
)
242 mm_segment_t fs
= get_fs();
245 audio_devs
[dev
]->d
->ioctl(dev
, SNDCTL_DSP_SETFMT
, (void __user
*)&bits
);
250 static int default_set_speed(int dev
, int speed
)
252 mm_segment_t fs
= get_fs();
255 audio_devs
[dev
]->d
->ioctl(dev
, SNDCTL_DSP_SPEED
, (void __user
*)&speed
);
260 static short default_set_channels(int dev
, short channels
)
263 mm_segment_t fs
= get_fs();
266 audio_devs
[dev
]->d
->ioctl(dev
, SNDCTL_DSP_CHANNELS
, (void __user
*)&c
);
271 static void check_driver(struct audio_driver
*d
)
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
;
281 int DMAbuf_open(int dev
, int mode
)
283 struct audio_operations
*adev
= audio_devs
[dev
];
285 struct dma_buffparms
*dmap_in
= NULL
;
286 struct dma_buffparms
*dmap_out
= NULL
;
290 if (!(adev
->flags
& DMA_DUPLEX
))
291 adev
->dmap_in
= adev
->dmap_out
;
292 check_driver(adev
->d
);
294 if ((retval
= adev
->d
->open(dev
, mode
)) < 0)
296 dmap_out
= adev
->dmap_out
;
297 dmap_in
= adev
->dmap_in
;
298 if (dmap_in
== dmap_out
)
299 adev
->flags
&= ~DMA_DUPLEX
;
301 if (mode
& OPEN_WRITE
) {
302 if ((retval
= open_dmap(adev
, mode
, dmap_out
)) < 0) {
307 adev
->enable_bits
= mode
;
309 if (mode
== OPEN_READ
|| (mode
!= OPEN_WRITE
&& (adev
->flags
& DMA_DUPLEX
))) {
310 if ((retval
= open_dmap(adev
, mode
, dmap_in
)) < 0) {
312 if (mode
& OPEN_WRITE
)
313 close_dmap(adev
, dmap_out
);
317 adev
->open_mode
= mode
;
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
);
328 /* MUST not hold the spinlock */
329 void DMAbuf_reset(int dev
)
331 if (audio_devs
[dev
]->open_mode
& OPEN_WRITE
)
332 dma_reset_output(dev
);
334 if (audio_devs
[dev
]->open_mode
& OPEN_READ
)
335 dma_reset_input(dev
);
338 static void dma_reset_output(int dev
)
340 struct audio_operations
*adev
= audio_devs
[dev
];
341 unsigned long flags
,f
;
342 struct dma_buffparms
*dmap
= adev
->dmap_out
;
344 if (!(dmap
->flags
& DMA_STARTED
)) /* DMA is not active */
348 * First wait until the current fragment has been played completely
350 spin_lock_irqsave(&dmap
->lock
,flags
);
351 adev
->dmap_out
->flags
|= DMA_SYNCING
;
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
);
360 adev
->dmap_out
->flags
&= ~(DMA_SYNCING
| DMA_ACTIVE
);
363 * Finally shut the device off
365 if (!(adev
->flags
& DMA_DUPLEX
) || !adev
->d
->halt_output
)
366 adev
->d
->halt_io(dev
);
368 adev
->d
->halt_output(dev
);
369 adev
->dmap_out
->flags
&= ~DMA_STARTED
;
372 clear_dma_ff(dmap
->dma
);
373 disable_dma(dmap
->dma
);
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
);
382 static void dma_reset_input(int dev
)
384 struct audio_operations
*adev
= audio_devs
[dev
];
386 struct dma_buffparms
*dmap
= adev
->dmap_in
;
388 spin_lock_irqsave(&dmap
->lock
,flags
);
389 if (!(adev
->flags
& DMA_DUPLEX
) || !adev
->d
->halt_input
)
390 adev
->d
->halt_io(dev
);
392 adev
->d
->halt_input(dev
);
393 adev
->dmap_in
->flags
&= ~DMA_STARTED
;
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
);
400 /* MUST be called with holding the dmap->lock */
401 void DMAbuf_launch_output(int dev
, struct dma_buffparms
*dmap
)
403 struct audio_operations
*adev
= audio_devs
[dev
];
405 if (!((adev
->enable_bits
* adev
->go
) & PCM_ENABLE_OUTPUT
))
406 return; /* Don't start DMA yet */
407 dmap
->dma_mode
= DMODE_OUTPUT
;
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
))
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
;
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
);
426 dmap
->flags
|= DMA_ACTIVE
;
429 int DMAbuf_sync(int dev
)
431 struct audio_operations
*adev
= audio_devs
[dev
];
434 struct dma_buffparms
*dmap
;
436 if (!adev
->go
&& !(adev
->enable_bits
& PCM_ENABLE_OUTPUT
))
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
);
454 adev
->dmap_out
->flags
&= ~DMA_SYNCING
;
455 spin_unlock_irqrestore(&dmap
->lock
,flags
);
456 return adev
->dmap_out
->qlen
;
459 adev
->dmap_out
->flags
&= ~(DMA_SYNCING
| DMA_ACTIVE
);
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.
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
);
476 spin_unlock_irqrestore(&dmap
->lock
,flags
);
478 adev
->dmap_out
->dma_mode
= DMODE_NONE
;
479 return adev
->dmap_out
->qlen
;
482 int DMAbuf_release(int dev
, int mode
)
484 struct audio_operations
*adev
= audio_devs
[dev
];
485 struct dma_buffparms
*dmap
;
488 dmap
= adev
->dmap_out
;
489 if (adev
->open_mode
& OPEN_WRITE
)
490 adev
->dmap_out
->closing
= 1;
492 if (adev
->open_mode
& OPEN_READ
){
493 adev
->dmap_in
->closing
= 1;
494 dmap
= adev
->dmap_in
;
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
))
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
);
504 spin_lock_irqsave(&dmap
->lock
,flags
);
507 if (adev
->open_mode
& OPEN_WRITE
)
508 close_dmap(adev
, adev
->dmap_out
);
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
);
515 spin_unlock_irqrestore(&dmap
->lock
,flags
);
518 /* called with dmap->lock dold */
519 int DMAbuf_activate_recording(int dev
, struct dma_buffparms
*dmap
)
521 struct audio_operations
*adev
= audio_devs
[dev
];
524 if (!(adev
->open_mode
& OPEN_READ
))
526 if (!(adev
->enable_bits
& PCM_ENABLE_INPUT
))
528 if (dmap
->dma_mode
== DMODE_OUTPUT
) { /* Direction change */
529 /* release lock - it's not recursive */
530 spin_unlock_irq(&dmap
->lock
);
533 spin_lock_irq(&dmap
->lock
);
534 dmap
->dma_mode
= DMODE_NONE
;
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)
541 dmap
->dma_mode
= DMODE_INPUT
;
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
);
556 int DMAbuf_getrdbuffer(int dev
, char **buf
, int *len
, int dontblock
)
558 struct audio_operations
*adev
= audio_devs
[dev
];
561 struct dma_buffparms
*dmap
= adev
->dmap_in
;
563 if (!(adev
->open_mode
& OPEN_READ
))
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
);
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
);
578 if ((err
= DMAbuf_activate_recording(dev
, dmap
)) < 0) {
579 spin_unlock_irqrestore(&dmap
->lock
,flags
);
582 /* Wait for the next block */
585 spin_unlock_irqrestore(&dmap
->lock
,flags
);
589 timeout
= dmabuf_timeout(dmap
);
591 spin_unlock_irqrestore(&dmap
->lock
,flags
);
592 timeout
= oss_broken_sleep_on(&adev
->in_sleeper
, timeout
);
594 /* FIXME: include device name */
596 printk(KERN_WARNING
"Sound: DMA (input) timed out - IRQ/DRQ config error?\n");
597 dma_reset_input(dev
);
600 spin_lock_irqsave(&dmap
->lock
,flags
);
602 spin_unlock_irqrestore(&dmap
->lock
,flags
);
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
];
612 int DMAbuf_rmchars(int dev
, int buff_no
, int c
)
614 struct audio_operations
*adev
= audio_devs
[dev
];
615 struct dma_buffparms
*dmap
= adev
->dmap_in
;
616 int p
= dmap
->counts
[dmap
->qhead
] + c
;
618 if (dmap
->mapping_flags
& DMA_MAP_MAPPED
)
620 /* printk("Sound: Can't read from mmapped device (2)\n");*/
623 else if (dmap
->qlen
<= 0)
625 else if (p
>= dmap
->fragment_size
) { /* This buffer is completely empty */
626 dmap
->counts
[dmap
->qhead
] = 0;
628 dmap
->qhead
= (dmap
->qhead
+ 1) % dmap
->nbufs
;
630 else dmap
->counts
[dmap
->qhead
] = p
;
634 /* MUST be called with dmap->lock hold */
635 int DMAbuf_get_buffer_pointer(int dev
, struct dma_buffparms
*dmap
, int direction
)
638 * Try to approximate the active byte position of the DMA pointer within the
639 * buffer area as well as possible.
645 if (!(dmap
->flags
& DMA_ACTIVE
))
648 int chan
= dmap
->dma
;
653 if(!isa_dma_bridge_buggy
)
654 disable_dma(dmap
->dma
);
656 pos
= get_dma_residue(chan
);
658 pos
= dmap
->bytes_in_use
- pos
;
660 if (!(dmap
->mapping_flags
& DMA_MAP_MAPPED
)) {
661 if (direction
== DMODE_OUTPUT
) {
662 if (dmap
->qhead
== 0)
663 if (pos
> dmap
->fragment_size
)
666 if (dmap
->qtail
== 0)
667 if (pos
> dmap
->fragment_size
)
673 if (pos
>= dmap
->bytes_in_use
)
676 if(!isa_dma_bridge_buggy
)
677 enable_dma(dmap
->dma
);
681 /* printk( "%04x ", pos); */
687 * DMAbuf_start_devices() is called by the /dev/music driver to start
688 * one or more audio devices at desired moment.
691 void DMAbuf_start_devices(unsigned int devmask
)
693 struct audio_operations
*adev
;
696 for (dev
= 0; dev
< num_audiodevs
; dev
++) {
697 if (!(devmask
& (1 << dev
)))
699 if (!(adev
= audio_devs
[dev
]))
701 if (adev
->open_mode
== 0)
705 /* OK to start the device */
707 if (adev
->d
->trigger
)
708 adev
->d
->trigger(dev
,adev
->enable_bits
* adev
->go
);
711 /* via poll called without a lock ?*/
712 int DMAbuf_space_in_queue(int dev
)
714 struct audio_operations
*adev
= audio_devs
[dev
];
716 struct dma_buffparms
*dmap
= adev
->dmap_out
;
717 int lim
= dmap
->nbufs
;
722 if (dmap
->qlen
>= lim
) /* No space at all */
726 * Verify that there are no more pending buffers than the limit
727 * defined by the process.
730 max
= dmap
->max_fragments
;
735 if (adev
->d
->local_qlen
) {
736 tmp
= adev
->d
->local_qlen(dev
);
738 tmp
--; /* This buffer has been counted twice */
741 if (dmap
->byte_counter
% dmap
->fragment_size
) /* There is a partial fragment */
748 /* MUST not hold the spinlock - this function may sleep */
749 static int output_sleep(int dev
, int dontblock
)
751 struct audio_operations
*adev
= audio_devs
[dev
];
753 struct dma_buffparms
*dmap
= adev
->dmap_out
;
759 if (!(adev
->enable_bits
& PCM_ENABLE_OUTPUT
))
763 * Wait for free space
765 if (signal_pending(current
))
767 timeout
= (adev
->go
&& !(dmap
->flags
& DMA_NOTIMEOUT
));
769 timeout_value
= dmabuf_timeout(dmap
);
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
);
777 if (signal_pending(current
))
782 /* called with the lock held */
783 static int find_output_space(int dev
, char **buf
, int *size
)
785 struct audio_operations
*adev
= audio_devs
[dev
];
786 struct dma_buffparms
*dmap
= adev
->dmap_out
;
787 unsigned long active_offs
;
790 int occupied_bytes
= (dmap
->user_counter
% dmap
->fragment_size
);
792 *buf
= dmap
->raw_buf
;
793 if (!(maxfrags
= DMAbuf_space_in_queue(dev
)) && !occupied_bytes
)
796 #ifdef BE_CONSERVATIVE
797 active_offs
= dmap
->byte_counter
+ dmap
->qhead
* dmap
->fragment_size
;
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
)
803 active_offs
+= dmap
->byte_counter
;
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
);
812 *buf
= dmap
->raw_buf
+ offs
;
814 len
= active_offs
+ dmap
->bytes_in_use
- dmap
->user_counter
; /* Number of unused bytes in buffer */
816 if ((offs
+ len
) > dmap
->bytes_in_use
)
817 len
= dmap
->bytes_in_use
- offs
;
821 if (len
> ((maxfrags
* dmap
->fragment_size
) - occupied_bytes
))
822 len
= (maxfrags
* dmap
->fragment_size
) - occupied_bytes
;
823 *size
= len
& ~SAMPLE_ROUNDUP
;
827 int DMAbuf_getwrbuffer(int dev
, char **buf
, int *size
, int dontblock
)
829 struct audio_operations
*adev
= audio_devs
[dev
];
832 struct dma_buffparms
*dmap
= adev
->dmap_out
;
834 if (dmap
->mapping_flags
& DMA_MAP_MAPPED
) {
835 /* printk(KERN_DEBUG "Sound: Can't write to mmapped device (3)\n");*/
838 spin_lock_irqsave(&dmap
->lock
,flags
);
839 if (dmap
->needs_reorg
)
840 reorganize_buffers(dev
, dmap
, 0);
842 if (dmap
->dma_mode
== DMODE_INPUT
) { /* Direction change */
843 spin_unlock_irqrestore(&dmap
->lock
,flags
);
845 spin_lock_irqsave(&dmap
->lock
,flags
);
847 dmap
->dma_mode
= DMODE_OUTPUT
;
849 while (find_output_space(dev
, buf
, size
) <= 0) {
850 spin_unlock_irqrestore(&dmap
->lock
,flags
);
851 if ((err
= output_sleep(dev
, dontblock
)) < 0) {
854 spin_lock_irqsave(&dmap
->lock
,flags
);
857 spin_unlock_irqrestore(&dmap
->lock
,flags
);
860 /* has to acquire dmap->lock */
861 int DMAbuf_move_wrpointer(int dev
, int l
)
863 struct audio_operations
*adev
= audio_devs
[dev
];
864 struct dma_buffparms
*dmap
= adev
->dmap_out
;
866 unsigned long end_ptr
, p
;
870 spin_lock_irqsave(&dmap
->lock
,flags
);
871 post
= (dmap
->flags
& DMA_POST
);
872 ptr
= (dmap
->user_counter
/ dmap
->fragment_size
) * dmap
->fragment_size
;
874 dmap
->flags
&= ~DMA_POST
;
876 dmap
->user_counter
+= l
;
877 dmap
->flags
|= DMA_DIRTY
;
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
;
886 end_ptr
= (dmap
->user_counter
/ dmap
->fragment_size
) * dmap
->fragment_size
;
888 p
= (dmap
->user_counter
- 1) % dmap
->bytes_in_use
;
889 dmap
->neutral_byte
= dmap
->raw_buf
[p
];
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
;
896 ptr
+= dmap
->fragment_size
;
899 dmap
->counts
[dmap
->qtail
] = dmap
->user_counter
- ptr
;
902 * Let the low level driver perform some postprocessing to
905 if (adev
->d
->postprocess_write
)
906 adev
->d
->postprocess_write(dev
);
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
);
912 spin_unlock_irqrestore(&dmap
->lock
,flags
);
916 int DMAbuf_start_dma(int dev
, unsigned long physaddr
, int count
, int dma_mode
)
918 struct audio_operations
*adev
= audio_devs
[dev
];
919 struct dma_buffparms
*dmap
= (dma_mode
== DMA_MODE_WRITE
) ? adev
->dmap_out
: adev
->dmap_in
;
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");
928 sound_start_dma(dmap
, physaddr
, count
, dma_mode
);
931 EXPORT_SYMBOL(DMAbuf_start_dma
);
933 static int local_start_dma(struct audio_operations
*adev
, unsigned long physaddr
, int count
, int dma_mode
)
935 struct dma_buffparms
*dmap
= (dma_mode
== DMA_MODE_WRITE
) ? adev
->dmap_out
: adev
->dmap_in
;
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");
942 if (dmap
->flags
& DMA_NODMA
)
946 sound_start_dma(dmap
, dmap
->raw_buf_phys
, dmap
->bytes_in_use
, dma_mode
| DMA_AUTOINIT
);
947 dmap
->flags
|= DMA_STARTED
;
951 static void finish_output_interrupt(int dev
, struct dma_buffparms
*dmap
)
953 struct audio_operations
*adev
= audio_devs
[dev
];
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
);
960 /* called with dmap->lock held in irq context*/
961 static void do_outputintr(int dev
, int dummy
)
963 struct audio_operations
*adev
= audio_devs
[dev
];
964 struct dma_buffparms
*dmap
= adev
->dmap_out
;
967 if (dmap
->raw_buf
== NULL
) {
968 printk(KERN_ERR
"Sound: Error. Audio interrupt (%d) after freeing buffers.\n", dev
);
971 if (dmap
->mapping_flags
& DMA_MAP_MAPPED
) { /* Virtual memory mapped 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
;
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
);
993 this_fragment
= dmap
->qhead
;
994 dmap
->qhead
= (dmap
->qhead
+ 1) % dmap
->nbufs
;
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
;
1005 if (!(adev
->flags
& DMA_AUTOMODE
))
1006 dmap
->flags
&= ~DMA_ACTIVE
;
1009 * This is dmap->qlen <= 0 except when closing when
1013 while (dmap
->qlen
<= -dmap
->closing
) {
1014 dmap
->underrun_count
++;
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
);
1021 dmap
->user_counter
+= dmap
->fragment_size
;
1022 dmap
->qtail
= (dmap
->qtail
+ 1) % dmap
->nbufs
;
1025 DMAbuf_launch_output(dev
, dmap
);
1026 finish_output_interrupt(dev
, dmap
);
1028 /* called in irq context */
1029 void DMAbuf_outputintr(int dev
, int notify_only
)
1031 struct audio_operations
*adev
= audio_devs
[dev
];
1032 unsigned long flags
;
1033 struct dma_buffparms
*dmap
= adev
->dmap_out
;
1035 spin_lock_irqsave(&dmap
->lock
,flags
);
1036 if (!(dmap
->flags
& DMA_NODMA
)) {
1037 int chan
= dmap
->dma
, pos
, n
;
1042 if(!isa_dma_bridge_buggy
)
1043 disable_dma(dmap
->dma
);
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
);
1050 pos
= pos
/ dmap
->fragment_size
; /* Actual qhead */
1051 if (pos
< 0 || pos
>= dmap
->nbufs
)
1054 while (dmap
->qhead
!= pos
&& n
++ < dmap
->nbufs
)
1055 do_outputintr(dev
, notify_only
);
1058 do_outputintr(dev
, notify_only
);
1059 spin_unlock_irqrestore(&dmap
->lock
,flags
);
1061 EXPORT_SYMBOL(DMAbuf_outputintr
);
1063 /* called with dmap->lock held in irq context */
1064 static void do_inputintr(int dev
)
1066 struct audio_operations
*adev
= audio_devs
[dev
];
1067 struct dma_buffparms
*dmap
= adev
->dmap_in
;
1069 if (dmap
->raw_buf
== NULL
) {
1070 printk(KERN_ERR
"Sound: Fatal error. Audio interrupt after freeing buffers.\n");
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
;
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
);
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
++;
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
) {
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
;
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
);
1122 dmap
->flags
|= DMA_ACTIVE
;
1125 wake_up(&adev
->in_sleeper
);
1126 wake_up(&adev
->poll_sleeper
);
1129 /* called in irq context */
1130 void DMAbuf_inputintr(int dev
)
1132 struct audio_operations
*adev
= audio_devs
[dev
];
1133 struct dma_buffparms
*dmap
= adev
->dmap_in
;
1134 unsigned long flags
;
1136 spin_lock_irqsave(&dmap
->lock
,flags
);
1138 if (!(dmap
->flags
& DMA_NODMA
)) {
1139 int chan
= dmap
->dma
, pos
, n
;
1143 if(!isa_dma_bridge_buggy
)
1144 disable_dma(dmap
->dma
);
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
);
1151 pos
= pos
/ dmap
->fragment_size
; /* Actual qhead */
1152 if (pos
< 0 || pos
>= dmap
->nbufs
)
1156 while (dmap
->qtail
!= pos
&& ++n
< dmap
->nbufs
)
1160 spin_unlock_irqrestore(&dmap
->lock
,flags
);
1162 EXPORT_SYMBOL(DMAbuf_inputintr
);
1164 void DMAbuf_init(int dev
, int dma1
, int dma2
)
1166 struct audio_operations
*adev
= audio_devs
[dev
];
1168 * NOTE! This routine could be called several times.
1171 if (adev
&& adev
->dmap_out
== NULL
) {
1172 if (adev
->d
== NULL
)
1173 panic("OSS: audio_devs[%d]->d == NULL\n", dev
);
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
;
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
;
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
);
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
)
1200 struct audio_operations
*adev
= audio_devs
[dev
];
1201 struct dma_buffparms
*dmap
= adev
->dmap_in
;
1203 if (!(adev
->open_mode
& OPEN_READ
))
1205 if (dmap
->mapping_flags
& DMA_MAP_MAPPED
) {
1207 return POLLIN
| POLLRDNORM
;
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
;
1216 spin_lock_irqsave(&dmap
->lock
,flags
);
1217 DMAbuf_activate_recording(dev
, dmap
);
1218 spin_unlock_irqrestore(&dmap
->lock
,flags
);
1224 return POLLIN
| POLLRDNORM
;
1227 static unsigned int poll_output(struct file
* file
, int dev
, poll_table
*wait
)
1229 struct audio_operations
*adev
= audio_devs
[dev
];
1230 struct dma_buffparms
*dmap
= adev
->dmap_out
;
1232 if (!(adev
->open_mode
& OPEN_WRITE
))
1234 if (dmap
->mapping_flags
& DMA_MAP_MAPPED
) {
1236 return POLLOUT
| POLLWRNORM
;
1239 if (dmap
->dma_mode
== DMODE_INPUT
)
1241 if (dmap
->dma_mode
== DMODE_NONE
)
1242 return POLLOUT
| POLLWRNORM
;
1243 if (!DMAbuf_space_in_queue(dev
))
1245 return POLLOUT
| POLLWRNORM
;
1248 unsigned int DMAbuf_poll(struct file
* file
, int dev
, poll_table
*wait
)
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
);
1255 void DMAbuf_deinit(int dev
)
1257 struct audio_operations
*adev
= audio_devs
[dev
];
1258 /* This routine is called when driver is being unloaded */
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
);