File: /usr/src/linux/drivers/sound/emu10k1/cardwo.c

1     /*
2      **********************************************************************
3      *     cardwo.c - PCM output HAL for emu10k1 driver
4      *     Copyright 1999, 2000 Creative Labs, Inc.
5      *
6      **********************************************************************
7      *
8      *     Date                 Author          Summary of changes
9      *     ----                 ------          ------------------
10      *     October 20, 1999     Bertrand Lee    base code release
11      *
12      **********************************************************************
13      *
14      *     This program is free software; you can redistribute it and/or
15      *     modify it under the terms of the GNU General Public License as
16      *     published by the Free Software Foundation; either version 2 of
17      *     the License, or (at your option) any later version.
18      *
19      *     This program is distributed in the hope that it will be useful,
20      *     but WITHOUT ANY WARRANTY; without even the implied warranty of
21      *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22      *     GNU General Public License for more details.
23      *
24      *     You should have received a copy of the GNU General Public
25      *     License along with this program; if not, write to the Free
26      *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
27      *     USA.
28      *
29      **********************************************************************
30      */
31     
32     #include <linux/poll.h>
33     #include "hwaccess.h"
34     #include "8010.h"
35     #include "voicemgr.h"
36     #include "cardwo.h"
37     #include "audio.h"
38     
39     static u32 samplerate_to_linearpitch(u32 samplingrate)
40     {
41     	samplingrate = (samplingrate << 8) / 375;
42     	return (samplingrate >> 1) + (samplingrate & 1);
43     }
44     
45     static void query_format(struct emu10k1_wavedevice *wave_dev, struct wave_format *wave_fmt)
46     {
47     	int i, j, do_passthrough = 0, is_ac3 = 0;
48     	struct emu10k1_card *card = wave_dev->card;
49     	struct woinst *woinst = wave_dev->woinst;
50     
51     	if ((wave_fmt->channels > 2) && (wave_fmt->id != AFMT_S16_LE) && (wave_fmt->id != AFMT_U8))
52     		wave_fmt->channels = 2;
53     
54     	if ((wave_fmt->channels < 1) || (wave_fmt->channels > WAVEOUT_MAXVOICES))
55     		wave_fmt->channels = 2;
56     
57     	if (wave_fmt->channels == 2)
58     		woinst->num_voices = 1;
59     	else
60     		woinst->num_voices = wave_fmt->channels;
61     
62     	if (wave_fmt->samplingrate >= 0x2ee00)
63     		wave_fmt->samplingrate = 0x2ee00;
64     
65     	wave_fmt->passthrough = 0;
66     	do_passthrough = is_ac3 = 0;
67     
68     	if (card->pt.selected)
69     		do_passthrough = 1;
70     
71     	switch (wave_fmt->id) {
72     	case AFMT_S16_LE:
73     		wave_fmt->bitsperchannel = 16;
74     		break;
75     	case AFMT_U8:
76     		wave_fmt->bitsperchannel = 8;
77     		break;
78     	case AFMT_AC3:
79     		do_passthrough = 1;
80     		is_ac3 = 1;
81     		break;
82     	default:
83     		wave_fmt->id = AFMT_S16_LE;
84     		wave_fmt->bitsperchannel = 16;
85     		break;
86     	}	
87     	if (do_passthrough) {
88     		i = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.intr_gpr_name);
89     		j = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.enable_gpr_name);
90     		/* currently only one waveout instance may use pass-through */
91     		if (i < 0 || j < 0 || woinst->state != WAVE_STATE_CLOSED || 
92     		    card->pt.state != PT_STATE_INACTIVE ||
93     		    (wave_fmt->samplingrate != 48000 && !is_ac3) ||
94     		    (wave_fmt->samplingrate != 48000 && !is_ac3)) {
95     			DPF(2, "unable to set pass-through mode\n");
96     		} else {
97     			wave_fmt->samplingrate = 48000;
98     			wave_fmt->channels = 2;
99     			wave_fmt->passthrough = 1;
100     			card->pt.intr_gpr = i;
101     			card->pt.enable_gpr = j;
102     			card->pt.state = PT_STATE_INACTIVE;
103     			card->pt.pos_gpr = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.pos_gpr_name);
104     			DPD(2, "is_ac3 is %d\n", is_ac3);
105     			card->pt.ac3data = is_ac3;
106     	                wave_fmt->bitsperchannel = 16;
107     		}
108     	}
109     
110     	wave_fmt->bytesperchannel = wave_fmt->bitsperchannel >> 3;
111     
112     	if (wave_fmt->channels == 2)
113     		wave_fmt->bytespervoicesample = wave_fmt->channels * wave_fmt->bytesperchannel;
114     	else
115     		wave_fmt->bytespervoicesample = wave_fmt->bytesperchannel;
116     
117     	wave_fmt->bytespersample = wave_fmt->channels * wave_fmt->bytesperchannel;
118     	wave_fmt->bytespersec = wave_fmt->bytespersample * wave_fmt->samplingrate;
119     }
120     
121     /**
122      * alloc_buffer -
123      *
124      * allocates the memory buffer for a voice. Two page tables are kept for each buffer.
125      * One (dma_handle) keeps track of the host memory pages used and the other (virtualpagetable)
126      * is passed to the device so that it can do DMA to host memory. 
127      *
128      */
129     static int alloc_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer, unsigned int voicenum)
130     {
131     	u32 pageindex, pagecount;
132     	unsigned long busaddx;
133     	int i;
134     
135     	DPD(2, "requested pages is: %d\n", buffer->pages);
136     
137     	if ((buffer->mem[voicenum].emupageindex =
138     	 emu10k1_addxmgr_alloc(buffer->pages * PAGE_SIZE, card)) < 0)
139     		return -1;
140     
141     	/* Fill in virtual memory table */
142     	for (pagecount = 0; pagecount < buffer->pages; pagecount++) {
143     		if ((buffer->mem[voicenum].addr[pagecount] =
144     		 pci_alloc_consistent(card->pci_dev, PAGE_SIZE,
145     		 &buffer->mem[voicenum].dma_handle[pagecount])) == NULL) {
146     			buffer->pages = pagecount;
147     			return -1;
148     		}
149     
150     		DPD(2, "Virtual Addx: %p\n", buffer->mem[voicenum].addr[pagecount]);
151     
152     		for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
153     			busaddx = buffer->mem[voicenum].dma_handle[pagecount] + i * EMUPAGESIZE;
154     
155     			DPD(3, "Bus Addx: %#lx\n", busaddx);
156     
157     			pageindex = buffer->mem[voicenum].emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
158     
159     			((u32 *) card->virtualpagetable.addr)[pageindex] = cpu_to_le32((busaddx * 2) | pageindex);
160     		}
161     	}
162     
163     	return 0;
164     }
165     
166     /**
167      * free_buffer -
168      *
169      * frees the memory buffer for a voice.
170      */
171     static void free_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer, unsigned int voicenum)
172     {
173     	u32 pagecount, pageindex;
174     	int i;
175     
176     	if (buffer->mem[voicenum].emupageindex < 0)
177     		return;
178     
179     	for (pagecount = 0; pagecount < buffer->pages; pagecount++) {
180     		pci_free_consistent(card->pci_dev, PAGE_SIZE,
181     		 buffer->mem[voicenum].addr[pagecount],
182     		 buffer->mem[voicenum].dma_handle[pagecount]);
183     
184     		for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
185     			pageindex = buffer->mem[voicenum].emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
186     			((u32 *) card->virtualpagetable.addr)[pageindex] =
187     			 cpu_to_le32((card->silentpage.dma_handle * 2) | pageindex);
188     		}
189     	}
190     
191     	emu10k1_addxmgr_free(card, buffer->mem[voicenum].emupageindex);
192     	buffer->mem[voicenum].emupageindex = -1;
193     }
194     
195     static int get_voice(struct emu10k1_card *card, struct woinst *woinst, unsigned int voicenum)
196     {
197     	struct emu_voice *voice = &woinst->voice[voicenum];
198     	/* Allocate voices here, if no voices available, return error.
199     	 * Init voice_allocdesc first.*/
200     
201     	voice->usage = VOICE_USAGE_PLAYBACK;
202     
203     	voice->flags = 0;
204     
205     	if (woinst->format.channels == 2)
206     		voice->flags |= VOICE_FLAGS_STEREO;
207     
208     	if (woinst->format.bitsperchannel == 16)
209     		voice->flags |= VOICE_FLAGS_16BIT;
210     
211     	if (emu10k1_voice_alloc(card, voice) < 0) {
212     		voice->usage = VOICE_USAGE_FREE;
213     		return -1;
214     	}
215     
216     	/* Calculate pitch */
217     	voice->initial_pitch = (u16) (srToPitch(woinst->format.samplingrate) >> 8);
218     	voice->pitch_target = samplerate_to_linearpitch(woinst->format.samplingrate);
219     
220     	DPD(2, "Initial pitch --> %#x\n", voice->initial_pitch);
221     
222     	voice->startloop = (woinst->buffer.mem[voicenum].emupageindex << 12) /
223     	 woinst->format.bytespervoicesample;
224     	voice->endloop = voice->startloop + woinst->buffer.size / woinst->format.bytespervoicesample;
225     	voice->start = voice->startloop;
226     
227     	if (voice->flags & VOICE_FLAGS_STEREO) {
228     		voice->params[0].send_a = card->waveout.send_a[1];
229     		voice->params[0].send_b = card->waveout.send_b[1];
230     		voice->params[0].send_c = card->waveout.send_c[1];
231     		voice->params[0].send_d = card->waveout.send_d[1];
232     
233     		if (woinst->device)
234     			voice->params[0].send_routing = 0x7654;
235     		else
236     			voice->params[0].send_routing = card->waveout.send_routing[1];
237     
238     		voice->params[0].volume_target = 0xffff;
239     		voice->params[0].initial_fc = 0xff;
240     		voice->params[0].initial_attn = 0x00;
241     		voice->params[0].byampl_env_sustain = 0x7f;
242     		voice->params[0].byampl_env_decay = 0x7f;
243     
244     		voice->params[1].send_a = card->waveout.send_a[2];
245     		voice->params[1].send_b = card->waveout.send_b[2];
246     		voice->params[1].send_c = card->waveout.send_c[2];
247     		voice->params[1].send_d = card->waveout.send_d[2];
248     
249     		if (woinst->device)
250     			voice->params[1].send_routing = 0x7654;
251     		else
252     			voice->params[1].send_routing = card->waveout.send_routing[2];
253     
254     		voice->params[1].volume_target = 0xffff;
255     		voice->params[1].initial_fc = 0xff;
256     		voice->params[1].initial_attn = 0x00;
257     		voice->params[1].byampl_env_sustain = 0x7f;
258     		voice->params[1].byampl_env_decay = 0x7f;
259     	} else {
260     		if (woinst->num_voices > 1) {
261     			voice->params[0].send_a = 0xff;
262     			voice->params[0].send_b = 0;
263     			voice->params[0].send_c = 0;
264     			voice->params[0].send_d = 0;
265     
266     			voice->params[0].send_routing =
267     			 0xfff0 + card->mchannel_fx + voicenum;
268     		} else {
269     			voice->params[0].send_a = card->waveout.send_a[0];
270     			voice->params[0].send_b = card->waveout.send_b[0];
271     			voice->params[0].send_c = card->waveout.send_c[0];
272     			voice->params[0].send_d = card->waveout.send_d[0];
273     
274     			if (woinst->device)
275     				voice->params[0].send_routing = 0x7654;
276     			else
277     				voice->params[0].send_routing = card->waveout.send_routing[0];
278     		}	
279     
280     		voice->params[0].volume_target = 0xffff;
281     		voice->params[0].initial_fc = 0xff;
282     		voice->params[0].initial_attn = 0x00;
283     		voice->params[0].byampl_env_sustain = 0x7f;
284     		voice->params[0].byampl_env_decay = 0x7f;
285     	}
286     
287     	DPD(2, "voice: startloop=%#x, endloop=%#x\n", voice->startloop, voice->endloop);
288     
289     	emu10k1_voice_playback_setup(voice);
290     
291     	return 0;
292     }
293     
294     int emu10k1_waveout_open(struct emu10k1_wavedevice *wave_dev)
295     {
296     	struct emu10k1_card *card = wave_dev->card;
297     	struct woinst *woinst = wave_dev->woinst;
298     	struct waveout_buffer *buffer = &woinst->buffer;
299     	unsigned int voicenum;
300     	u32 delay;
301     
302     	DPF(2, "emu10k1_waveout_open()\n");
303     
304     	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
305     		if (alloc_buffer(card, buffer, voicenum) < 0) {
306     			ERROR();
307     			emu10k1_waveout_close(wave_dev);
308     			return -1;
309     		}
310     
311     		if (get_voice(card, woinst, voicenum) < 0) {
312     			ERROR();
313     			emu10k1_waveout_close(wave_dev);
314     			return -1;
315     		}
316     	}
317     
318     	buffer->fill_silence = 0;
319     	buffer->silence_bytes = 0;
320     	buffer->silence_pos = 0;
321     	buffer->hw_pos = 0;
322     	buffer->free_bytes = woinst->buffer.size;
323     
324     	delay = (48000 * woinst->buffer.fragment_size) /
325     		 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
326     
327     	emu10k1_timer_install(card, &woinst->timer, delay / 2);
328     
329     	woinst->state = WAVE_STATE_OPEN;
330     
331     	return 0;
332     }
333     
334     void emu10k1_waveout_close(struct emu10k1_wavedevice *wave_dev)
335     {
336     	struct emu10k1_card *card = wave_dev->card;
337     	struct woinst *woinst = wave_dev->woinst;
338     	unsigned int voicenum;
339     
340     	DPF(2, "emu10k1_waveout_close()\n");
341     
342     	emu10k1_waveout_stop(wave_dev);
343     
344     	emu10k1_timer_uninstall(card, &woinst->timer);
345     
346     	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
347     		emu10k1_voice_free(&woinst->voice[voicenum]);
348     		free_buffer(card, &woinst->buffer, voicenum);
349     	}
350     
351     	woinst->state = WAVE_STATE_CLOSED;
352     }
353     
354     void emu10k1_waveout_start(struct emu10k1_wavedevice *wave_dev)
355     {
356     	struct emu10k1_card *card = wave_dev->card;
357     	struct woinst *woinst = wave_dev->woinst;
358     
359     	DPF(2, "emu10k1_waveout_start()\n");
360     
361     	/* Actual start */
362     	emu10k1_voices_start(woinst->voice, woinst->num_voices, woinst->total_played);
363     
364     	emu10k1_timer_enable(card, &woinst->timer);
365     
366     	woinst->state |= WAVE_STATE_STARTED;
367     }
368     
369     int emu10k1_waveout_setformat(struct emu10k1_wavedevice *wave_dev, struct wave_format *format)
370     {
371     	struct emu10k1_card *card = wave_dev->card;
372     	struct woinst *woinst = wave_dev->woinst;
373     	unsigned int voicenum;
374     	u32 delay;
375     
376     	DPF(2, "emu10k1_waveout_setformat()\n");
377     
378     	if (woinst->state & WAVE_STATE_STARTED)
379     		return -1;
380     
381     	query_format(wave_dev, format);
382     
383     	if (woinst->format.samplingrate != format->samplingrate ||
384     	    woinst->format.channels != format->channels ||
385     	    woinst->format.bitsperchannel != format->bitsperchannel) {
386     
387     		woinst->format = *format;
388     
389     		if (woinst->state == WAVE_STATE_CLOSED)
390     			return 0;
391     
392     		emu10k1_timer_uninstall(card, &woinst->timer);
393     
394     		for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
395     			emu10k1_voice_free(&woinst->voice[voicenum]);
396     
397     			if (get_voice(card, woinst, voicenum) < 0) {
398     				ERROR();
399     				emu10k1_waveout_close(wave_dev);
400     				return -1;
401     			}
402     		}
403     
404     		delay = (48000 * woinst->buffer.fragment_size) /
405     			 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
406     
407     		emu10k1_timer_install(card, &woinst->timer, delay / 2);
408     	}
409     
410     	return 0;
411     }
412     
413     void emu10k1_waveout_stop(struct emu10k1_wavedevice *wave_dev)
414     {
415     	struct emu10k1_card *card = wave_dev->card;
416     	struct woinst *woinst = wave_dev->woinst;
417     
418     	DPF(2, "emu10k1_waveout_stop()\n");
419     
420     	if (!(woinst->state & WAVE_STATE_STARTED))
421     		return;
422     
423     	emu10k1_timer_disable(card, &woinst->timer);
424     
425     	/* Stop actual voices */
426     	emu10k1_voices_stop(woinst->voice, woinst->num_voices);
427     
428     	emu10k1_waveout_update(woinst);
429     
430     	woinst->state &= ~WAVE_STATE_STARTED;
431     }
432     
433     /**
434      * emu10k1_waveout_getxfersize -
435      *
436      * gives the total free bytes on the voice buffer, including silence bytes
437      * (basically: total_free_bytes = free_bytes + silence_bytes).
438      *
439      */
440     void emu10k1_waveout_getxfersize(struct woinst *woinst, u32 *total_free_bytes)
441     {
442     	struct waveout_buffer *buffer = &woinst->buffer;
443     	int pending_bytes;
444     
445     	if (woinst->mmapped) {
446     		*total_free_bytes = buffer->free_bytes;
447     		return;
448     	}
449     
450     	pending_bytes = buffer->size - buffer->free_bytes;
451     
452     	buffer->fill_silence = (pending_bytes < (signed) buffer->fragment_size) ? 1 : 0;
453     
454     	if (pending_bytes > (signed) buffer->silence_bytes) {
455     		*total_free_bytes = (buffer->free_bytes + buffer->silence_bytes);
456     	} else {
457     		*total_free_bytes = buffer->size;
458     		buffer->silence_bytes = pending_bytes;
459     		if (pending_bytes < 0) {
460     			buffer->silence_pos = buffer->hw_pos;
461     			buffer->silence_bytes = 0;
462     			buffer->free_bytes = buffer->size;
463     			DPF(1, "buffer underrun\n");
464     		}
465     	}
466     }
467     
468     /**
469      * copy_block -
470      *
471      * copies a block of pcm data to a voice buffer.
472      * Notice that the voice buffer is actually a set of disjointed memory pages.
473      *
474      */
475     static void copy_block(void **dst, u32 str, u8 *src, u32 len)
476     {
477     	unsigned int pg;
478     	unsigned int pgoff;
479     	unsigned int k;
480     
481     	pg = str / PAGE_SIZE;
482     	pgoff = str % PAGE_SIZE;
483     
484     	if (len > PAGE_SIZE - pgoff) {
485     		k = PAGE_SIZE - pgoff;
486     		__copy_from_user((u8 *)dst[pg] + pgoff, src, k);
487     		len -= k;
488     		while (len > PAGE_SIZE) {
489     			__copy_from_user(dst[++pg], src + k, PAGE_SIZE);
490     			k += PAGE_SIZE;
491     			len -= PAGE_SIZE;
492     		}
493     		__copy_from_user(dst[++pg], src + k, len);
494     
495     	} else
496     		__copy_from_user((u8 *)dst[pg] + pgoff, src, len);
497     }
498     
499     /**
500      * copy_ilv_block -
501      *
502      * copies a block of pcm data containing n interleaved channels to n mono voice buffers.
503      * Notice that the voice buffer is actually a set of disjointed memory pages.
504      *
505      */
506     static void copy_ilv_block(struct woinst *woinst, u32 str, u8 *src, u32 len) 
507     {
508             unsigned int pg;
509     	unsigned int pgoff;
510     	unsigned int voice_num;
511     	struct waveout_mem *mem = woinst->buffer.mem;
512     
513     	pg = str / PAGE_SIZE;
514     	pgoff = str % PAGE_SIZE;
515     
516     	while (len) { 
517     		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++) {
518     			__copy_from_user((u8 *)(mem[voice_num].addr[pg]) + pgoff, src, woinst->format.bytespervoicesample);
519     			src += woinst->format.bytespervoicesample;
520     		}
521     
522     		len -= woinst->format.bytespervoicesample;
523     
524     		pgoff += woinst->format.bytespervoicesample;
525     		if (pgoff >= PAGE_SIZE) {
526     			pgoff = 0;
527     			pg++;
528     		}
529     	}
530     }
531     
532     /**
533      * fill_block -
534      *
535      * fills a set voice buffers with a block of a given sample.
536      *
537      */
538     static void fill_block(struct woinst *woinst, u32 str, u8 data, u32 len)
539     {
540     	unsigned int pg;
541     	unsigned int pgoff;
542     	unsigned int voice_num;
543             struct waveout_mem *mem = woinst->buffer.mem;
544     	unsigned int  k;
545     
546     	pg = str / PAGE_SIZE;
547     	pgoff = str % PAGE_SIZE;
548     
549     	if (len > PAGE_SIZE - pgoff) {
550     		k = PAGE_SIZE - pgoff;
551     		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
552     			memset((u8 *)mem[voice_num].addr[pg] + pgoff, data, k);
553     		len -= k;
554     		while (len > PAGE_SIZE) {
555     			pg++;
556     			for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
557     				memset(mem[voice_num].addr[pg], data, PAGE_SIZE);
558     
559     			len -= PAGE_SIZE;
560     		}
561     		pg++;
562     		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
563     			memset(mem[voice_num].addr[pg], data, len);
564     
565     	} else {
566     		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
567     			memset((u8 *)mem[voice_num].addr[pg] + pgoff, data, len);
568     	}
569     }
570     
571     /**
572      * emu10k1_waveout_xferdata -
573      *
574      * copies pcm data to the voice buffer. Silence samples
575      * previously added to the buffer are overwritten.
576      *
577      */
578     void emu10k1_waveout_xferdata(struct woinst *woinst, u8 *data, u32 *size)
579     {
580     	struct waveout_buffer *buffer = &woinst->buffer;
581     	u32 sizetocopy, sizetocopy_now, start;
582     	unsigned long flags;
583     
584     	sizetocopy = min_t(u32, buffer->size, *size);
585     	*size = sizetocopy;
586     
587     	if (!sizetocopy)
588     		return;
589     	
590     	spin_lock_irqsave(&woinst->lock, flags);
591     	start = (buffer->size + buffer->silence_pos - buffer->silence_bytes) % buffer->size;
592     
593     	if (sizetocopy > buffer->silence_bytes) {
594     		buffer->silence_pos += sizetocopy - buffer->silence_bytes;
595     		buffer->free_bytes -= sizetocopy - buffer->silence_bytes;
596     		buffer->silence_bytes = 0;
597     	} else
598     		buffer->silence_bytes -= sizetocopy;
599     
600     	spin_unlock_irqrestore(&woinst->lock, flags);
601     
602     	sizetocopy_now = buffer->size - start;
603     	if (sizetocopy > sizetocopy_now) {
604     		sizetocopy -= sizetocopy_now;
605     		if (woinst->num_voices > 1) {
606     			copy_ilv_block(woinst, start, data, sizetocopy_now);
607     			copy_ilv_block(woinst, 0, data + sizetocopy_now * woinst->num_voices, sizetocopy);
608     		} else {
609     			copy_block(buffer->mem[0].addr, start, data, sizetocopy_now);
610     			copy_block(buffer->mem[0].addr, 0, data + sizetocopy_now, sizetocopy);
611     		}
612     	} else {
613     		if (woinst->num_voices > 1)
614     			copy_ilv_block(woinst, start, data, sizetocopy);
615     		else
616     			copy_block(buffer->mem[0].addr, start, data, sizetocopy);
617     	}
618     }
619     
620     /**
621      * emu10k1_waveout_fillsilence -
622      *
623      * adds samples of silence to the voice buffer so that we
624      * don't loop over stale pcm data.
625      *
626      */
627     void emu10k1_waveout_fillsilence(struct woinst *woinst)
628     {
629     	struct waveout_buffer *buffer = &woinst->buffer;
630     	u32 sizetocopy, sizetocopy_now, start;
631     	u8 filldata;
632     	unsigned long flags;
633     
634     	sizetocopy = buffer->fragment_size;
635     
636     	if (woinst->format.bitsperchannel == 16)
637     		filldata = 0x00;
638     	else
639     		filldata = 0x80;
640     
641     	spin_lock_irqsave(&woinst->lock, flags);
642     	buffer->silence_bytes += sizetocopy;
643     	buffer->free_bytes -= sizetocopy;
644     	buffer->silence_pos %= buffer->size;
645     	start = buffer->silence_pos;
646     	buffer->silence_pos += sizetocopy;
647     	spin_unlock_irqrestore(&woinst->lock, flags);
648     
649     	sizetocopy_now = buffer->size - start;
650     
651     	if (sizetocopy > sizetocopy_now) {
652     		sizetocopy -= sizetocopy_now;
653     		fill_block(woinst, start, filldata, sizetocopy_now);
654     		fill_block(woinst, 0, filldata, sizetocopy);
655     	} else {
656     		fill_block(woinst, start, filldata, sizetocopy);
657     	}
658     }
659     
660     /**
661      * emu10k1_waveout_update -
662      *
663      * updates the position of the voice buffer hardware pointer (hw_pos)
664      * and the number of free bytes on the buffer (free_bytes).
665      * The free bytes _don't_ include silence bytes that may have been
666      * added to the buffer.
667      *
668      */
669     void emu10k1_waveout_update(struct woinst *woinst)
670     {
671     	u32 hw_pos;
672     	u32 diff;
673     	
674     	/* There is no actual start yet */
675     	if (!(woinst->state & WAVE_STATE_STARTED)) {
676     		hw_pos = woinst->buffer.hw_pos;
677     	} else {
678     		/* hw_pos in sample units */
679     		hw_pos = sblive_readptr(woinst->voice[0].card, CCCA_CURRADDR, woinst->voice[0].num);
680     
681     		if(hw_pos < woinst->voice[0].start)
682     			hw_pos += woinst->buffer.size / woinst->format.bytespervoicesample - woinst->voice[0].start;
683     		else
684     			hw_pos -= woinst->voice[0].start;
685     
686     		hw_pos *= woinst->format.bytespervoicesample;
687     	}
688     
689     	diff = (woinst->buffer.size + hw_pos - woinst->buffer.hw_pos) % woinst->buffer.size;
690     	woinst->total_played += diff;
691     	woinst->buffer.free_bytes += diff;
692     	woinst->buffer.hw_pos = hw_pos;
693     }
694