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