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 
samplerate_to_linearpitch(u32 samplingrate)39 static u32 samplerate_to_linearpitch(u32 samplingrate)
40 {
41 	samplingrate = (samplingrate << 8) / 375;
42 	return (samplingrate >> 1) + (samplingrate & 1);
43 }
44 
query_format(struct emu10k1_wavedevice * wave_dev,struct wave_format * wave_fmt)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 	wave_fmt->bytespersample = wave_fmt->channels * wave_fmt->bytesperchannel;
112 	wave_fmt->bytespersec = wave_fmt->bytespersample * wave_fmt->samplingrate;
113 
114 	if (wave_fmt->channels == 2)
115 		wave_fmt->bytespervoicesample = wave_fmt->channels * wave_fmt->bytesperchannel;
116 	else
117 		wave_fmt->bytespervoicesample = wave_fmt->bytesperchannel;
118 }
119 
get_voice(struct emu10k1_card * card,struct woinst * woinst,unsigned int voicenum)120 static int get_voice(struct emu10k1_card *card, struct woinst *woinst, unsigned int voicenum)
121 {
122 	struct emu_voice *voice = &woinst->voice[voicenum];
123 
124 	/* Allocate voices here, if no voices available, return error. */
125 
126 	voice->usage = VOICE_USAGE_PLAYBACK;
127 
128 	voice->flags = 0;
129 
130 	if (woinst->format.channels == 2)
131 		voice->flags |= VOICE_FLAGS_STEREO;
132 
133 	if (woinst->format.bitsperchannel == 16)
134 		voice->flags |= VOICE_FLAGS_16BIT;
135 
136 	if (emu10k1_voice_alloc(card, voice) < 0) {
137 		voice->usage = VOICE_USAGE_FREE;
138 		return -1;
139 	}
140 
141 	/* Calculate pitch */
142 	voice->initial_pitch = (u16) (srToPitch(woinst->format.samplingrate) >> 8);
143 	voice->pitch_target = samplerate_to_linearpitch(woinst->format.samplingrate);
144 
145 	DPD(2, "Initial pitch --> %#x\n", voice->initial_pitch);
146 
147 	voice->startloop = (voice->mem.emupageindex << 12) /
148 	 woinst->format.bytespervoicesample;
149 	voice->endloop = voice->startloop + woinst->buffer.size / woinst->format.bytespervoicesample;
150 	voice->start = voice->startloop;
151 
152 	if (voice->flags & VOICE_FLAGS_STEREO) {
153 		voice->params[0].send_a = card->waveout.send_a[1];
154 		voice->params[0].send_b = card->waveout.send_b[1];
155 		voice->params[0].send_c = card->waveout.send_c[1];
156 		voice->params[0].send_d = card->waveout.send_d[1];
157 
158 		if (woinst->device)
159 			voice->params[0].send_routing = 0x7654;
160 		else
161 			voice->params[0].send_routing = card->waveout.send_routing[1];
162 
163 		voice->params[0].volume_target = 0xffff;
164 		voice->params[0].initial_fc = 0xff;
165 		voice->params[0].initial_attn = 0x00;
166 		voice->params[0].byampl_env_sustain = 0x7f;
167 		voice->params[0].byampl_env_decay = 0x7f;
168 
169 		voice->params[1].send_a = card->waveout.send_a[2];
170 		voice->params[1].send_b = card->waveout.send_b[2];
171 		voice->params[1].send_c = card->waveout.send_c[2];
172 		voice->params[1].send_d = card->waveout.send_d[2];
173 
174 		if (woinst->device)
175 			voice->params[1].send_routing = 0x7654;
176 		else
177 			voice->params[1].send_routing = card->waveout.send_routing[2];
178 
179 		voice->params[1].volume_target = 0xffff;
180 		voice->params[1].initial_fc = 0xff;
181 		voice->params[1].initial_attn = 0x00;
182 		voice->params[1].byampl_env_sustain = 0x7f;
183 		voice->params[1].byampl_env_decay = 0x7f;
184 	} else {
185 		if (woinst->num_voices > 1) {
186 			voice->params[0].send_a = 0xff;
187 			voice->params[0].send_b = 0;
188 			voice->params[0].send_c = 0;
189 			voice->params[0].send_d = 0;
190 
191 			voice->params[0].send_routing =
192 			 0xfff0 + card->mchannel_fx + voicenum;
193 		} else {
194 			voice->params[0].send_a = card->waveout.send_a[0];
195 			voice->params[0].send_b = card->waveout.send_b[0];
196 			voice->params[0].send_c = card->waveout.send_c[0];
197 			voice->params[0].send_d = card->waveout.send_d[0];
198 
199 			if (woinst->device)
200 				voice->params[0].send_routing = 0x7654;
201 			else
202 				voice->params[0].send_routing = card->waveout.send_routing[0];
203 		}
204 
205 		voice->params[0].volume_target = 0xffff;
206 		voice->params[0].initial_fc = 0xff;
207 		voice->params[0].initial_attn = 0x00;
208 		voice->params[0].byampl_env_sustain = 0x7f;
209 		voice->params[0].byampl_env_decay = 0x7f;
210 	}
211 
212 	DPD(2, "voice: startloop=%#x, endloop=%#x\n", voice->startloop, voice->endloop);
213 
214 	emu10k1_voice_playback_setup(voice);
215 
216 	return 0;
217 }
218 
emu10k1_waveout_open(struct emu10k1_wavedevice * wave_dev)219 int emu10k1_waveout_open(struct emu10k1_wavedevice *wave_dev)
220 {
221 	struct emu10k1_card *card = wave_dev->card;
222 	struct woinst *woinst = wave_dev->woinst;
223 	struct waveout_buffer *buffer = &woinst->buffer;
224 	unsigned int voicenum;
225 	u16 delay;
226 
227 	DPF(2, "emu10k1_waveout_open()\n");
228 
229 	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
230 		if (emu10k1_voice_alloc_buffer(card, &woinst->voice[voicenum].mem, woinst->buffer.pages) < 0) {
231 			ERROR();
232 			emu10k1_waveout_close(wave_dev);
233 			return -1;
234 		}
235 
236 		if (get_voice(card, woinst, voicenum) < 0) {
237 			ERROR();
238 			emu10k1_waveout_close(wave_dev);
239 			return -1;
240 		}
241 	}
242 
243 	buffer->fill_silence = 0;
244 	buffer->silence_bytes = 0;
245 	buffer->silence_pos = 0;
246 	buffer->hw_pos = 0;
247 	buffer->free_bytes = woinst->buffer.size;
248 
249 	delay = (48000 * woinst->buffer.fragment_size) /
250 		 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
251 
252 	emu10k1_timer_install(card, &woinst->timer, delay);
253 
254 	woinst->state = WAVE_STATE_OPEN;
255 
256 	return 0;
257 }
258 
emu10k1_waveout_close(struct emu10k1_wavedevice * wave_dev)259 void emu10k1_waveout_close(struct emu10k1_wavedevice *wave_dev)
260 {
261 	struct emu10k1_card *card = wave_dev->card;
262 	struct woinst *woinst = wave_dev->woinst;
263 	unsigned int voicenum;
264 
265 	DPF(2, "emu10k1_waveout_close()\n");
266 
267 	emu10k1_waveout_stop(wave_dev);
268 
269 	emu10k1_timer_uninstall(card, &woinst->timer);
270 
271 	for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
272 		emu10k1_voice_free(&woinst->voice[voicenum]);
273 		emu10k1_voice_free_buffer(card, &woinst->voice[voicenum].mem);
274 	}
275 
276 	woinst->state = WAVE_STATE_CLOSED;
277 }
278 
emu10k1_waveout_start(struct emu10k1_wavedevice * wave_dev)279 void emu10k1_waveout_start(struct emu10k1_wavedevice *wave_dev)
280 {
281 	struct emu10k1_card *card = wave_dev->card;
282 	struct woinst *woinst = wave_dev->woinst;
283 
284 	DPF(2, "emu10k1_waveout_start()\n");
285 
286 	/* Actual start */
287 	emu10k1_voices_start(woinst->voice, woinst->num_voices, woinst->total_played);
288 
289 	emu10k1_timer_enable(card, &woinst->timer);
290 
291 	woinst->state |= WAVE_STATE_STARTED;
292 }
293 
emu10k1_waveout_setformat(struct emu10k1_wavedevice * wave_dev,struct wave_format * format)294 int emu10k1_waveout_setformat(struct emu10k1_wavedevice *wave_dev, struct wave_format *format)
295 {
296 	struct emu10k1_card *card = wave_dev->card;
297 	struct woinst *woinst = wave_dev->woinst;
298 	unsigned int voicenum;
299 	u16 delay;
300 
301 	DPF(2, "emu10k1_waveout_setformat()\n");
302 
303 	if (woinst->state & WAVE_STATE_STARTED)
304 		return -1;
305 
306 	query_format(wave_dev, format);
307 
308 	if (woinst->format.samplingrate != format->samplingrate ||
309 	    woinst->format.channels != format->channels ||
310 	    woinst->format.bitsperchannel != format->bitsperchannel) {
311 
312 		woinst->format = *format;
313 
314 		if (woinst->state == WAVE_STATE_CLOSED)
315 			return 0;
316 
317 		emu10k1_timer_uninstall(card, &woinst->timer);
318 
319 		for (voicenum = 0; voicenum < woinst->num_voices; voicenum++) {
320 			emu10k1_voice_free(&woinst->voice[voicenum]);
321 
322 			if (get_voice(card, woinst, voicenum) < 0) {
323 				ERROR();
324 				emu10k1_waveout_close(wave_dev);
325 				return -1;
326 			}
327 		}
328 
329 		delay = (48000 * woinst->buffer.fragment_size) /
330 			 (woinst->format.samplingrate * woinst->format.bytespervoicesample);
331 
332 		emu10k1_timer_install(card, &woinst->timer, delay);
333 	}
334 
335 	return 0;
336 }
337 
emu10k1_waveout_stop(struct emu10k1_wavedevice * wave_dev)338 void emu10k1_waveout_stop(struct emu10k1_wavedevice *wave_dev)
339 {
340 	struct emu10k1_card *card = wave_dev->card;
341 	struct woinst *woinst = wave_dev->woinst;
342 
343 	DPF(2, "emu10k1_waveout_stop()\n");
344 
345 	if (!(woinst->state & WAVE_STATE_STARTED))
346 		return;
347 
348 	emu10k1_timer_disable(card, &woinst->timer);
349 
350 	/* Stop actual voices */
351 	emu10k1_voices_stop(woinst->voice, woinst->num_voices);
352 
353 	emu10k1_waveout_update(woinst);
354 
355 	woinst->state &= ~WAVE_STATE_STARTED;
356 }
357 
358 /**
359  * emu10k1_waveout_getxfersize -
360  *
361  * gives the total free bytes on the voice buffer, including silence bytes
362  * (basically: total_free_bytes = free_bytes + silence_bytes).
363  *
364  */
emu10k1_waveout_getxfersize(struct woinst * woinst,u32 * total_free_bytes)365 void emu10k1_waveout_getxfersize(struct woinst *woinst, u32 *total_free_bytes)
366 {
367 	struct waveout_buffer *buffer = &woinst->buffer;
368 	int pending_bytes;
369 
370 	if (woinst->mmapped) {
371 		*total_free_bytes = buffer->free_bytes;
372 		return;
373 	}
374 
375 	pending_bytes = buffer->size - buffer->free_bytes;
376 
377 	buffer->fill_silence = (pending_bytes < (signed) buffer->fragment_size * 2) ? 1 : 0;
378 
379 	if (pending_bytes > (signed) buffer->silence_bytes) {
380 		*total_free_bytes = (buffer->free_bytes + buffer->silence_bytes);
381 	} else {
382 		*total_free_bytes = buffer->size;
383 		buffer->silence_bytes = pending_bytes;
384 		if (pending_bytes < 0) {
385 			buffer->silence_pos = buffer->hw_pos;
386 			buffer->silence_bytes = 0;
387 			buffer->free_bytes = buffer->size;
388 			DPF(1, "buffer underrun\n");
389 		}
390 	}
391 }
392 
393 /**
394  * copy_block -
395  *
396  * copies a block of pcm data to a voice buffer.
397  * Notice that the voice buffer is actually a set of disjointed memory pages.
398  *
399  */
copy_block(void ** dst,u32 str,u8 * src,u32 len)400 static void copy_block(void **dst, u32 str, u8 *src, u32 len)
401 {
402 	unsigned int pg;
403 	unsigned int pgoff;
404 	unsigned int k;
405 
406 	pg = str / PAGE_SIZE;
407 	pgoff = str % PAGE_SIZE;
408 
409 	if (len > PAGE_SIZE - pgoff) {
410 		k = PAGE_SIZE - pgoff;
411 		__copy_from_user((u8 *)dst[pg] + pgoff, src, k);
412 		len -= k;
413 		while (len > PAGE_SIZE) {
414 			__copy_from_user(dst[++pg], src + k, PAGE_SIZE);
415 			k += PAGE_SIZE;
416 			len -= PAGE_SIZE;
417 		}
418 		__copy_from_user(dst[++pg], src + k, len);
419 
420 	} else
421 		__copy_from_user((u8 *)dst[pg] + pgoff, src, len);
422 }
423 
424 /**
425  * copy_ilv_block -
426  *
427  * copies a block of pcm data containing n interleaved channels to n mono voice buffers.
428  * Notice that the voice buffer is actually a set of disjointed memory pages.
429  *
430  */
copy_ilv_block(struct woinst * woinst,u32 str,u8 * src,u32 len)431 static void copy_ilv_block(struct woinst *woinst, u32 str, u8 *src, u32 len)
432 {
433         unsigned int pg;
434 	unsigned int pgoff;
435 	unsigned int voice_num;
436 	struct emu_voice *voice = woinst->voice;
437 
438 	pg = str / PAGE_SIZE;
439 	pgoff = str % PAGE_SIZE;
440 
441 	while (len) {
442 		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++) {
443 			__copy_from_user((u8 *)(voice[voice_num].mem.addr[pg]) + pgoff, src, woinst->format.bytespervoicesample);
444 			src += woinst->format.bytespervoicesample;
445 		}
446 
447 		len -= woinst->format.bytespervoicesample;
448 
449 		pgoff += woinst->format.bytespervoicesample;
450 		if (pgoff >= PAGE_SIZE) {
451 			pgoff = 0;
452 			pg++;
453 		}
454 	}
455 }
456 
457 /**
458  * fill_block -
459  *
460  * fills a set voice buffers with a block of a given sample.
461  *
462  */
fill_block(struct woinst * woinst,u32 str,u8 data,u32 len)463 static void fill_block(struct woinst *woinst, u32 str, u8 data, u32 len)
464 {
465 	unsigned int pg;
466 	unsigned int pgoff;
467 	unsigned int voice_num;
468         struct emu_voice *voice = woinst->voice;
469 	unsigned int  k;
470 
471 	pg = str / PAGE_SIZE;
472 	pgoff = str % PAGE_SIZE;
473 
474 	if (len > PAGE_SIZE - pgoff) {
475 		k = PAGE_SIZE - pgoff;
476 		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
477 			memset((u8 *)voice[voice_num].mem.addr[pg] + pgoff, data, k);
478 		len -= k;
479 		while (len > PAGE_SIZE) {
480 			pg++;
481 			for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
482 				memset(voice[voice_num].mem.addr[pg], data, PAGE_SIZE);
483 
484 			len -= PAGE_SIZE;
485 		}
486 		pg++;
487 		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
488 			memset(voice[voice_num].mem.addr[pg], data, len);
489 
490 	} else {
491 		for (voice_num = 0; voice_num < woinst->num_voices; voice_num++)
492 			memset((u8 *)voice[voice_num].mem.addr[pg] + pgoff, data, len);
493 	}
494 }
495 
496 /**
497  * emu10k1_waveout_xferdata -
498  *
499  * copies pcm data to the voice buffer. Silence samples
500  * previously added to the buffer are overwritten.
501  *
502  */
emu10k1_waveout_xferdata(struct woinst * woinst,u8 * data,u32 * size)503 void emu10k1_waveout_xferdata(struct woinst *woinst, u8 *data, u32 *size)
504 {
505 	struct waveout_buffer *buffer = &woinst->buffer;
506 	struct voice_mem *mem = &woinst->voice[0].mem;
507 	u32 sizetocopy, sizetocopy_now, start;
508 	unsigned long flags;
509 
510 	sizetocopy = min_t(u32, buffer->size, *size);
511 	*size = sizetocopy;
512 
513 	if (!sizetocopy)
514 		return;
515 
516 	spin_lock_irqsave(&woinst->lock, flags);
517 	start = (buffer->size + buffer->silence_pos - buffer->silence_bytes) % buffer->size;
518 
519 	if (sizetocopy > buffer->silence_bytes) {
520 		buffer->silence_pos += sizetocopy - buffer->silence_bytes;
521 		buffer->free_bytes -= sizetocopy - buffer->silence_bytes;
522 		buffer->silence_bytes = 0;
523 	} else
524 		buffer->silence_bytes -= sizetocopy;
525 
526 	spin_unlock_irqrestore(&woinst->lock, flags);
527 
528 	sizetocopy_now = buffer->size - start;
529 	if (sizetocopy > sizetocopy_now) {
530 		sizetocopy -= sizetocopy_now;
531 		if (woinst->num_voices > 1) {
532 			copy_ilv_block(woinst, start, data, sizetocopy_now);
533 			copy_ilv_block(woinst, 0, data + sizetocopy_now * woinst->num_voices, sizetocopy);
534 		} else {
535 			copy_block(mem->addr, start, data, sizetocopy_now);
536 			copy_block(mem->addr, 0, data + sizetocopy_now, sizetocopy);
537 		}
538 	} else {
539 		if (woinst->num_voices > 1)
540 			copy_ilv_block(woinst, start, data, sizetocopy);
541 		else
542 			copy_block(mem->addr, start, data, sizetocopy);
543 	}
544 }
545 
546 /**
547  * emu10k1_waveout_fillsilence -
548  *
549  * adds samples of silence to the voice buffer so that we
550  * don't loop over stale pcm data.
551  *
552  */
emu10k1_waveout_fillsilence(struct woinst * woinst)553 void emu10k1_waveout_fillsilence(struct woinst *woinst)
554 {
555 	struct waveout_buffer *buffer = &woinst->buffer;
556 	u32 sizetocopy, sizetocopy_now, start;
557 	u8 filldata;
558 	unsigned long flags;
559 
560 	sizetocopy = buffer->fragment_size;
561 
562 	if (woinst->format.bitsperchannel == 16)
563 		filldata = 0x00;
564 	else
565 		filldata = 0x80;
566 
567 	spin_lock_irqsave(&woinst->lock, flags);
568 	buffer->silence_bytes += sizetocopy;
569 	buffer->free_bytes -= sizetocopy;
570 	buffer->silence_pos %= buffer->size;
571 	start = buffer->silence_pos;
572 	buffer->silence_pos += sizetocopy;
573 	spin_unlock_irqrestore(&woinst->lock, flags);
574 
575 	sizetocopy_now = buffer->size - start;
576 
577 	if (sizetocopy > sizetocopy_now) {
578 		sizetocopy -= sizetocopy_now;
579 		fill_block(woinst, start, filldata, sizetocopy_now);
580 		fill_block(woinst, 0, filldata, sizetocopy);
581 	} else {
582 		fill_block(woinst, start, filldata, sizetocopy);
583 	}
584 }
585 
586 /**
587  * emu10k1_waveout_update -
588  *
589  * updates the position of the voice buffer hardware pointer (hw_pos)
590  * and the number of free bytes on the buffer (free_bytes).
591  * The free bytes _don't_ include silence bytes that may have been
592  * added to the buffer.
593  *
594  */
emu10k1_waveout_update(struct woinst * woinst)595 void emu10k1_waveout_update(struct woinst *woinst)
596 {
597 	u32 hw_pos;
598 	u32 diff;
599 
600 	/* There is no actual start yet */
601 	if (!(woinst->state & WAVE_STATE_STARTED)) {
602 		hw_pos = woinst->buffer.hw_pos;
603 	} else {
604 		/* hw_pos in sample units */
605 		hw_pos = sblive_readptr(woinst->voice[0].card, CCCA_CURRADDR, woinst->voice[0].num);
606 
607 		if(hw_pos < woinst->voice[0].start)
608 			hw_pos += woinst->buffer.size / woinst->format.bytespervoicesample - woinst->voice[0].start;
609 		else
610 			hw_pos -= woinst->voice[0].start;
611 
612 		hw_pos *= woinst->format.bytespervoicesample;
613 	}
614 
615 	diff = (woinst->buffer.size + hw_pos - woinst->buffer.hw_pos) % woinst->buffer.size;
616 	woinst->total_played += diff;
617 	woinst->buffer.free_bytes += diff;
618 	woinst->buffer.hw_pos = hw_pos;
619 }
620