Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/*
 **********************************************************************
 *     cardwo.c - PCM output HAL for emu10k1 driver
 *     Copyright 1999, 2000 Creative Labs, Inc.
 *
 **********************************************************************
 *
 *     Date                 Author          Summary of changes
 *     ----                 ------          ------------------
 *     October 20, 1999     Bertrand Lee    base code release
 *
 **********************************************************************
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
 *     the License, or (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public
 *     License along with this program; if not, write to the Free
 *     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 *     USA.
 *
 **********************************************************************
 */

#include <linux/poll.h>
#include "hwaccess.h"
#include "8010.h"
#include "voicemgr.h"
#include "cardwo.h"
#include "audio.h"

static u32 samplerate_to_linearpitch(u32 samplingrate)
{
	samplingrate = (samplingrate << 8) / 375;
	return (samplingrate >> 1) + (samplingrate & 1);
}

static void query_format(struct emu10k1_wavedevice *wave_dev, struct wave_format *wave_fmt)
{
	int i, j, do_passthrough = 0, is_ac3 = 0;
	struct emu10k1_card *card = wave_dev->card;
	struct woinst *woinst = wave_dev->woinst;

	if ((wave_fmt->channels > 2) && (wave_fmt->id != AFMT_S16_LE) && (wave_fmt->id != AFMT_U8))
		wave_fmt->channels = 2;

	if ((wave_fmt->channels < 1) || (wave_fmt->channels > WAVEOUT_MAXVOICES))
		wave_fmt->channels = 2;

	if (wave_fmt->channels == 2)
		woinst->num_voices = 1;
	else
		woinst->num_voices = wave_fmt->channels;

	if (wave_fmt->samplingrate >= 0x2ee00)
		wave_fmt->samplingrate = 0x2ee00;

	wave_fmt->passthrough = 0;
	do_passthrough = is_ac3 = 0;

	if (card->pt.selected)
		do_passthrough = 1;

	switch (wave_fmt->id) {
	case AFMT_S16_LE:
		wave_fmt->bitsperchannel = 16;
		break;
	case AFMT_U8:
		wave_fmt->bitsperchannel = 8;
		break;
	case AFMT_AC3:
		do_passthrough = 1;
		is_ac3 = 1;
		break;
	default:
		wave_fmt->id = AFMT_S16_LE;
		wave_fmt->bitsperchannel = 16;
		break;
	}	
	if (do_passthrough) {
		i = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.intr_gpr_name);
		j = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.enable_gpr_name);
		/* currently only one waveout instance may use pass-through */
		if (i < 0 || j < 0 || woinst->state != WAVE_STATE_CLOSED || 
		    card->pt.state != PT_STATE_INACTIVE ||
		    (wave_fmt->samplingrate != 48000 && !is_ac3) ||
		    (wave_fmt->samplingrate != 48000 && !is_ac3)) {
			DPF(2, "unable to set pass-through mode\n");
		} else {
			wave_fmt->samplingrate = 48000;
			wave_fmt->channels = 2;
			wave_fmt->passthrough = 1;
			card->pt.intr_gpr = i;
			card->pt.enable_gpr = j;
			card->pt.state = PT_STATE_INACTIVE;
			card->pt.pos_gpr = emu10k1_find_control_gpr(&card->mgr, card->pt.patch_name, card->pt.pos_gpr_name);
			DPD(2, "is_ac3 is %d\n", is_ac3);
			card->pt.ac3data = is_ac3;
	                wave_fmt->bitsperchannel = 16;
		}
	}

	wave_fmt->bytesperchannel = wave_fmt->bitsperchannel >> 3;

	if (wave_fmt->channels == 2)
		wave_fmt->bytespervoicesample = wave_fmt->channels * wave_fmt->bytesperchannel;
	else
		wave_fmt->bytespervoicesample = wave_fmt->bytesperchannel;

	wave_fmt->bytespersample = wave_fmt->channels * wave_fmt->bytesperchannel;
	wave_fmt->bytespersec = wave_fmt->bytespersample * wave_fmt->samplingrate;
}

/**
 * alloc_buffer -
 *
 * allocates the memory buffer for a voice. Two page tables are kept for each buffer.
 * One (dma_handle) keeps track of the host memory pages used and the other (virtualpagetable)
 * is passed to the device so that it can do DMA to host memory. 
 *
 */
static int alloc_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer, unsigned int voicenum)
{
	u32 pageindex, pagecount;
	unsigned long busaddx;
	int i;

	DPD(2, "requested pages is: %d\n", buffer->pages);

	if ((buffer->mem[voicenum].emupageindex =
	 emu10k1_addxmgr_alloc(buffer->pages * PAGE_SIZE, card)) < 0)
		return -1;

	/* Fill in virtual memory table */
	for (pagecount = 0; pagecount < buffer->pages; pagecount++) {
		if ((buffer->mem[voicenum].addr[pagecount] =
		 pci_alloc_consistent(card->pci_dev, PAGE_SIZE,
		 &buffer->mem[voicenum].dma_handle[pagecount])) == NULL) {
			buffer->pages = pagecount;
			return -1;
		}

		DPD(2, "Virtual Addx: %p\n", buffer->mem[voicenum].addr[pagecount]);

		for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
			busaddx = buffer->mem[voicenum].dma_handle[pagecount] + i * EMUPAGESIZE;

			DPD(3, "Bus Addx: %#lx\n", busaddx);

			pageindex = buffer->mem[voicenum].emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;

			((u32 *) card->virtualpagetable.addr)[pageindex] = cpu_to_le32((busaddx * 2) | pageindex);
		}
	}

	return 0;
}

/**
 * free_buffer -
 *
 * frees the memory buffer for a voice.
 */
static void free_buffer(struct emu10k1_card *card, struct waveout_buffer *buffer, unsigned int voicenum)
{
	u32 pagecount, pageindex;
	int i;

	if (buffer->mem[voicenum].emupageindex < 0)
		return;

	for (pagecount = 0; pagecount < buffer->pages; pagecount++) {
		pci_free_consistent(card->pci_dev, PAGE_SIZE,
		 buffer->mem[voicenum].addr[pagecount],
		 buffer->mem[voicenum].dma_handle[pagecount]);

		for (i = 0; i < PAGE_SIZE / EMUPAGESIZE; i++) {
			pageindex = buffer->mem[voicenum].emupageindex + pagecount * PAGE_SIZE / EMUPAGESIZE + i;
			((u32 *) card->virtualpagetable.addr)[pageindex] =
			 cpu_to_le32((card->silentpage.dma_handle * 2) | pageindex);
		}
	}

	emu10k1_addxmgr_free(card, buffer->mem[voicenum].emupageindex);
	buffer->mem[voicenum].emupageindex = -1;
}

static int get_voice(struct emu10k1_card *card, struct woinst *woinst, unsigned int voicenum)
{
	struct emu_voice *voice = &woinst->voice[voicenum];
	/* Allocate voices here, if no voices available, return error.
	 * Init voice_allocdesc first.*/

	voice->usage = VOICE_USAGE_PLAYBACK;

	voice->flags = 0;

	if (woinst->format.channels == 2)
		voice->flags |= VOICE_FLAGS_STEREO;

	if (woinst->format.bitsperchannel == 16)
		voice->flags |= VOICE_FLAGS_16BIT;

	if (emu10k1_voice_alloc(card, voice) < 0) {
		voice->usage = VOICE_USAGE_FREE;
		return -1;
	}

	/* Calculate pitch */
	voice->initial_pitch = (u16) (srToPitch(woinst->format.samplingrate) >> 8);
	voice->pitch_target = samplerate_to_linearpitch(woinst->format.samplingrate);

	DPD(2, "Initial pitch --> %#x\n", voice->initial_pitch);

	voice->startloop = (woinst->buffer.mem[voicenum].emupageindex << 12) /
	 woinst->format.bytespervoicesample;
	voice->endloop = voice->startloop + woinst->buffer.size / woinst->format.bytespervoicesample;
	voice->start = voice->startloop;

	if (voice->flags & VOICE_FLAGS_STEREO) {
		voice->params[0].send_a = card->waveout.send_a[1];
		voice->params[0].send_b = card->waveout.send_b[1];
		voice->params[0].send_c = card->waveout.send_c[1];
		voice->params[0].send_d = card->waveout.send_d[1];

		if (woinst->device)
			voice->params[0].send_routing = 0x7654;
		else
			voice->params[0].send_routing = card->waveout.send_routing[1];

		voice->params[0].volume_target = 0xffff;
		voice->params[0].initial_fc = 0xff;
		voice->params[0].initial_attn = 0x00;
		voice->params[0].byampl_env_sustain = 0x7f;
		voice->params[0].byampl_env_decay = 0x7f;

		voice->params[1].send_a = card->waveout.send_a[2];
		voice->params[1].send_b = card->waveout.send_b[2];
		voice->params[1].send_c = card->waveout.send_c[2];
		voice->params[1].send_d = card->waveout.send_d[2];

		if (woinst->device)
			voice->params[1].send_routing = 0x7654;
		else
			voice->params[1].send_routing = card->waveout.send_routing[2];

		voice->params[1].volume_target = 0xffff;
		voice->params[1].initial_fc = 0xff;
		voice->params[1].initial_attn = 0x00;
		voice->params[1].byampl_env_sustain = 0x7f;
		voice->params[1].byampl_env_decay = 0x7f;
	} else {
		if (woinst->num_voices > 1) {
			voice->params[0].send_a = 0xff;
			voice->params[0].send_b = 0;
			voice->params[0].send_c = 0;
			voice->params[0].send_d = 0;

			voice->params[0].send_routing =
			 0xfff0 + card->mchannel_fx + voicenum;
		} else {
			voice->params[0].send_a = card->waveout.send_a[0];
			voice->params[0].send_b = card->waveout.send_b[0];
			voice->params[0].send_c = card->waveout.send_c[0];
			voice->params[0].send_d = card->waveout.send_d[0];

			if (woinst->device)
				voice->params[0].send_routing = 0x7654;
			else
				voice->params[0].send_routing = card->waveout.send_routing[0];
		}	

		voice->params[0].volume_target = 0xffff;
		voice->params[0].initial_fc = 0xff;
		voice->params[0].initial_attn = 0x00;
		voice->params[0].byampl_env_sustain = 0x7f;
		voice->params[0].byampl_env_decay = 0x7f;
	}

	DPD(2, "voice: startloop=%#x, endloop=%#x\n", voice->startloop, voice->endloop);

	emu10k1_voice_playback_setup(voice);

	return 0;
}

int emu10k1_waveout_open(struct emu10k1_wavedevice *wave_dev)
{
	struct emu10k1_card *card = wave_dev->card;
	struct woinst *woinst = wave_dev->woinst;
	struct waveout_buffer *buffer = &woinst->buffer;
	unsigned int voicenum;
	u32 delay;

	DPF(2, "emu10k1_waveout_open()\n");

	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
		if (alloc_buffer(card, buffer, voicenum) < 0) {
			ERROR();
			emu10k1_waveout_close(wave_dev);
			return -1;
		}

		if (get_voice(card, woinst, voicenum) < 0) {
			ERROR();
			emu10k1_waveout_close(wave_dev);
			return -1;
		}
	}

	buffer->fill_silence = 0;
	buffer->silence_bytes = 0;
	buffer->silence_pos = 0;
	buffer->hw_pos = 0;
	buffer->free_bytes = woinst->buffer.size;

	delay = (48000 * woinst->buffer.fragment_size) /
		 (woinst->format.samplingrate * woinst->format.bytespervoicesample);

	emu10k1_timer_install(card, &woinst->timer, delay / 2);

	woinst->state = WAVE_STATE_OPEN;

	return 0;
}

void emu10k1_waveout_close(struct emu10k1_wavedevice *wave_dev)
{
	struct emu10k1_card *card = wave_dev->card;
	struct woinst *woinst = wave_dev->woinst;
	unsigned int voicenum;

	DPF(2, "emu10k1_waveout_close()\n");

	emu10k1_waveout_stop(wave_dev);

	emu10k1_timer_uninstall(card, &woinst->timer);

	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
		emu10k1_voice_free(&woinst->voice[voicenum]);
		free_buffer(card, &woinst->buffer, voicenum);
	}

	woinst->state = WAVE_STATE_CLOSED;
}

void emu10k1_waveout_start(struct emu10k1_wavedevice *wave_dev)
{
	struct emu10k1_card *card = wave_dev->card;
	struct woinst *woinst = wave_dev->woinst;

	DPF(2, "emu10k1_waveout_start()\n");

	/* Actual start */
	emu10k1_voices_start(woinst->voice, woinst->num_voices, woinst->total_played);

	emu10k1_timer_enable(card, &woinst->timer);

	woinst->state |= WAVE_STATE_STARTED;
}

int emu10k1_waveout_setformat(struct emu10k1_wavedevice *wave_dev, struct wave_format *format)
{
	struct emu10k1_card *card = wave_dev->card;
	struct woinst *woinst = wave_dev->woinst;
	unsigned int voicenum;
	u32 delay;

	DPF(2, "emu10k1_waveout_setformat()\n");

	if (woinst->state & WAVE_STATE_STARTED)
		return -1;

	query_format(wave_dev, format);

	if (woinst->format.samplingrate != format->samplingrate ||
	    woinst->format.channels != format->channels ||
	    woinst->format.bitsperchannel != format->bitsperchannel) {

		woinst->format = *format;

		if (woinst->state == WAVE_STATE_CLOSED)
			return 0;

		emu10k1_timer_uninstall(card, &woinst->timer);

		for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
			emu10k1_voice_free(&woinst->voice[voicenum]);

			if (get_voice(card, woinst, voicenum) < 0) {
				ERROR();
				emu10k1_waveout_close(wave_dev);
				return -1;
			}
		}

		delay = (48000 * woinst->buffer.fragment_size) /
			 (woinst->format.samplingrate * woinst->format.bytespervoicesample);

		emu10k1_timer_install(card, &woinst->timer, delay / 2);
	}

	return 0;
}

void emu10k1_waveout_stop(struct emu10k1_wavedevice *wave_dev)
{
	struct emu10k1_card *card = wave_dev->card;
	struct woinst *woinst = wave_dev->woinst;

	DPF(2, "emu10k1_waveout_stop()\n");

	if (!(woinst->state & WAVE_STATE_STARTED))
		return;

	emu10k1_timer_disable(card, &woinst->timer);

	/* Stop actual voices */
	emu10k1_voices_stop(woinst->voice, woinst->num_voices);

	emu10k1_waveout_update(woinst);

	woinst->state &= ~WAVE_STATE_STARTED;
}

/**
 * emu10k1_waveout_getxfersize -
 *
 * gives the total free bytes on the voice buffer, including silence bytes
 * (basically: total_free_bytes = free_bytes + silence_bytes).
 *
 */
void emu10k1_waveout_getxfersize(struct woinst *woinst, u32 *total_free_bytes)
{
	struct waveout_buffer *buffer = &woinst->buffer;
	int pending_bytes;

	if (woinst->mmapped) {
		*total_free_bytes = buffer->free_bytes;
		return;
	}

	pending_bytes = buffer->size - buffer->free_bytes;

	buffer->fill_silence = (pending_bytes < (signed) buffer->fragment_size) ? 1 : 0;

	if (pending_bytes > (signed) buffer->silence_bytes) {
		*total_free_bytes = (buffer->free_bytes + buffer->silence_bytes);
	} else {
		*total_free_bytes = buffer->size;
		buffer->silence_bytes = pending_bytes;
		if (pending_bytes < 0) {
			buffer->silence_pos = buffer->hw_pos;
			buffer->silence_bytes = 0;
			buffer->free_bytes = buffer->size;
			DPF(1, "buffer underrun\n");
		}
	}
}

/**
 * copy_block -
 *
 * copies a block of pcm data to a voice buffer.
 * Notice that the voice buffer is actually a set of disjointed memory pages.
 *
 */
static void copy_block(void **dst, u32 str, u8 *src, u32 len)
{
	unsigned int pg;
	unsigned int pgoff;
	unsigned int k;

	pg = str / PAGE_SIZE;
	pgoff = str % PAGE_SIZE;

	if (len > PAGE_SIZE - pgoff) {
		k = PAGE_SIZE - pgoff;
		__copy_from_user((u8 *)dst[pg] + pgoff, src, k);
		len -= k;
		while (len > PAGE_SIZE) {
			__copy_from_user(dst[++pg], src + k, PAGE_SIZE);
			k += PAGE_SIZE;
			len -= PAGE_SIZE;
		}
		__copy_from_user(dst[++pg], src + k, len);

	} else
		__copy_from_user((u8 *)dst[pg] + pgoff, src, len);
}

/**
 * copy_ilv_block -
 *
 * copies a block of pcm data containing n interleaved channels to n mono voice buffers.
 * Notice that the voice buffer is actually a set of disjointed memory pages.
 *
 */
static void copy_ilv_block(struct woinst *woinst, u32 str, u8 *src, u32 len) 
{
        unsigned int pg;
	unsigned int pgoff;
	unsigned int voice_num;
	struct waveout_mem *mem = woinst->buffer.mem;

	pg = str / PAGE_SIZE;
	pgoff = str % PAGE_SIZE;

	while (len) { 
		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++) {
			__copy_from_user((u8 *)(mem[voice_num].addr[pg]) + pgoff, src, woinst->format.bytespervoicesample);
			src += woinst->format.bytespervoicesample;
		}

		len -= woinst->format.bytespervoicesample;

		pgoff += woinst->format.bytespervoicesample;
		if (pgoff >= PAGE_SIZE) {
			pgoff = 0;
			pg++;
		}
	}
}

/**
 * fill_block -
 *
 * fills a set voice buffers with a block of a given sample.
 *
 */
static void fill_block(struct woinst *woinst, u32 str, u8 data, u32 len)
{
	unsigned int pg;
	unsigned int pgoff;
	unsigned int voice_num;
        struct waveout_mem *mem = woinst->buffer.mem;
	unsigned int  k;

	pg = str / PAGE_SIZE;
	pgoff = str % PAGE_SIZE;

	if (len > PAGE_SIZE - pgoff) {
		k = PAGE_SIZE - pgoff;
		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
			memset((u8 *)mem[voice_num].addr[pg] + pgoff, data, k);
		len -= k;
		while (len > PAGE_SIZE) {
			pg++;
			for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
				memset(mem[voice_num].addr[pg], data, PAGE_SIZE);

			len -= PAGE_SIZE;
		}
		pg++;
		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
			memset(mem[voice_num].addr[pg], data, len);

	} else {
		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
			memset((u8 *)mem[voice_num].addr[pg] + pgoff, data, len);
	}
}

/**
 * emu10k1_waveout_xferdata -
 *
 * copies pcm data to the voice buffer. Silence samples
 * previously added to the buffer are overwritten.
 *
 */
void emu10k1_waveout_xferdata(struct woinst *woinst, u8 *data, u32 *size)
{
	struct waveout_buffer *buffer = &woinst->buffer;
	u32 sizetocopy, sizetocopy_now, start;
	unsigned long flags;

	sizetocopy = min_t(u32, buffer->size, *size);
	*size = sizetocopy;

	if (!sizetocopy)
		return;
	
	spin_lock_irqsave(&woinst->lock, flags);
	start = (buffer->size + buffer->silence_pos - buffer->silence_bytes) % buffer->size;

	if (sizetocopy > buffer->silence_bytes) {
		buffer->silence_pos += sizetocopy - buffer->silence_bytes;
		buffer->free_bytes -= sizetocopy - buffer->silence_bytes;
		buffer->silence_bytes = 0;
	} else
		buffer->silence_bytes -= sizetocopy;

	spin_unlock_irqrestore(&woinst->lock, flags);

	sizetocopy_now = buffer->size - start;
	if (sizetocopy > sizetocopy_now) {
		sizetocopy -= sizetocopy_now;
		if (woinst->num_voices > 1) {
			copy_ilv_block(woinst, start, data, sizetocopy_now);
			copy_ilv_block(woinst, 0, data + sizetocopy_now * woinst->num_voices, sizetocopy);
		} else {
			copy_block(buffer->mem[0].addr, start, data, sizetocopy_now);
			copy_block(buffer->mem[0].addr, 0, data + sizetocopy_now, sizetocopy);
		}
	} else {
		if (woinst->num_voices > 1)
			copy_ilv_block(woinst, start, data, sizetocopy);
		else
			copy_block(buffer->mem[0].addr, start, data, sizetocopy);
	}
}

/**
 * emu10k1_waveout_fillsilence -
 *
 * adds samples of silence to the voice buffer so that we
 * don't loop over stale pcm data.
 *
 */
void emu10k1_waveout_fillsilence(struct woinst *woinst)
{
	struct waveout_buffer *buffer = &woinst->buffer;
	u32 sizetocopy, sizetocopy_now, start;
	u8 filldata;
	unsigned long flags;

	sizetocopy = buffer->fragment_size;

	if (woinst->format.bitsperchannel == 16)
		filldata = 0x00;
	else
		filldata = 0x80;

	spin_lock_irqsave(&woinst->lock, flags);
	buffer->silence_bytes += sizetocopy;
	buffer->free_bytes -= sizetocopy;
	buffer->silence_pos %= buffer->size;
	start = buffer->silence_pos;
	buffer->silence_pos += sizetocopy;
	spin_unlock_irqrestore(&woinst->lock, flags);

	sizetocopy_now = buffer->size - start;

	if (sizetocopy > sizetocopy_now) {
		sizetocopy -= sizetocopy_now;
		fill_block(woinst, start, filldata, sizetocopy_now);
		fill_block(woinst, 0, filldata, sizetocopy);
	} else {
		fill_block(woinst, start, filldata, sizetocopy);
	}
}

/**
 * emu10k1_waveout_update -
 *
 * updates the position of the voice buffer hardware pointer (hw_pos)
 * and the number of free bytes on the buffer (free_bytes).
 * The free bytes _don't_ include silence bytes that may have been
 * added to the buffer.
 *
 */
void emu10k1_waveout_update(struct woinst *woinst)
{
	u32 hw_pos;
	u32 diff;
	
	/* There is no actual start yet */
	if (!(woinst->state & WAVE_STATE_STARTED)) {
		hw_pos = woinst->buffer.hw_pos;
	} else {
		/* hw_pos in sample units */
		hw_pos = sblive_readptr(woinst->voice[0].card, CCCA_CURRADDR, woinst->voice[0].num);

		if(hw_pos < woinst->voice[0].start)
			hw_pos += woinst->buffer.size / woinst->format.bytespervoicesample - woinst->voice[0].start;
		else
			hw_pos -= woinst->voice[0].start;

		hw_pos *= woinst->format.bytespervoicesample;
	}

	diff = (woinst->buffer.size + hw_pos - woinst->buffer.hw_pos) % woinst->buffer.size;
	woinst->total_played += diff;
	woinst->buffer.free_bytes += diff;
	woinst->buffer.hw_pos = hw_pos;
}