xref: /linux/sound/isa/sb/emu8000_pcm.c (revision 9ce7677cfd7cd871adb457c80bea3b581b839641)
1 /*
2  * pcm emulation on emu8000 wavetable
3  *
4  *  Copyright (C) 2002 Takashi Iwai <tiwai@suse.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  */
20 
21 #include "emu8000_local.h"
22 #include <linux/init.h>
23 #include <sound/initval.h>
24 #include <sound/pcm.h>
25 
26 /*
27  * define the following if you want to use this pcm with non-interleaved mode
28  */
29 /* #define USE_NONINTERLEAVE */
30 
31 /* NOTE: for using the non-interleaved mode with alsa-lib, you have to set
32  * mmap_emulation flag to 1 in your .asoundrc, such like
33  *
34  *	pcm.emu8k {
35  *		type plug
36  *		slave.pcm {
37  *			type hw
38  *			card 0
39  *			device 1
40  *			mmap_emulation 1
41  *		}
42  *	}
43  *
44  * besides, for the time being, the non-interleaved mode doesn't work well on
45  * alsa-lib...
46  */
47 
48 
49 typedef struct snd_emu8k_pcm emu8k_pcm_t;
50 
51 struct snd_emu8k_pcm {
52 	emu8000_t *emu;
53 	snd_pcm_substream_t *substream;
54 
55 	unsigned int allocated_bytes;
56 	snd_util_memblk_t *block;
57 	unsigned int offset;
58 	unsigned int buf_size;
59 	unsigned int period_size;
60 	unsigned int loop_start[2];
61 	unsigned int pitch;
62 	int panning[2];
63 	int last_ptr;
64 	int period_pos;
65 	int voices;
66 	unsigned int dram_opened: 1;
67 	unsigned int running: 1;
68 	unsigned int timer_running: 1;
69 	struct timer_list timer;
70 	spinlock_t timer_lock;
71 };
72 
73 #define LOOP_BLANK_SIZE		8
74 
75 
76 /*
77  * open up channels for the simultaneous data transfer and playback
78  */
79 static int
80 emu8k_open_dram_for_pcm(emu8000_t *emu, int channels)
81 {
82 	int i;
83 
84 	/* reserve up to 2 voices for playback */
85 	snd_emux_lock_voice(emu->emu, 0);
86 	if (channels > 1)
87 		snd_emux_lock_voice(emu->emu, 1);
88 
89 	/* reserve 28 voices for loading */
90 	for (i = channels + 1; i < EMU8000_DRAM_VOICES; i++) {
91 		unsigned int mode = EMU8000_RAM_WRITE;
92 		snd_emux_lock_voice(emu->emu, i);
93 #ifndef USE_NONINTERLEAVE
94 		if (channels > 1 && (i & 1) != 0)
95 			mode |= EMU8000_RAM_RIGHT;
96 #endif
97 		snd_emu8000_dma_chan(emu, i, mode);
98 	}
99 
100 	/* assign voice 31 and 32 to ROM */
101 	EMU8000_VTFT_WRITE(emu, 30, 0);
102 	EMU8000_PSST_WRITE(emu, 30, 0x1d8);
103 	EMU8000_CSL_WRITE(emu, 30, 0x1e0);
104 	EMU8000_CCCA_WRITE(emu, 30, 0x1d8);
105 	EMU8000_VTFT_WRITE(emu, 31, 0);
106 	EMU8000_PSST_WRITE(emu, 31, 0x1d8);
107 	EMU8000_CSL_WRITE(emu, 31, 0x1e0);
108 	EMU8000_CCCA_WRITE(emu, 31, 0x1d8);
109 
110 	return 0;
111 }
112 
113 /*
114  */
115 static void
116 snd_emu8000_write_wait(emu8000_t *emu, int can_schedule)
117 {
118 	while ((EMU8000_SMALW_READ(emu) & 0x80000000) != 0) {
119 		if (can_schedule) {
120 			schedule_timeout_interruptible(1);
121 			if (signal_pending(current))
122 				break;
123 		}
124 	}
125 }
126 
127 /*
128  * close all channels
129  */
130 static void
131 emu8k_close_dram(emu8000_t *emu)
132 {
133 	int i;
134 
135 	for (i = 0; i < 2; i++)
136 		snd_emux_unlock_voice(emu->emu, i);
137 	for (; i < EMU8000_DRAM_VOICES; i++) {
138 		snd_emu8000_dma_chan(emu, i, EMU8000_RAM_CLOSE);
139 		snd_emux_unlock_voice(emu->emu, i);
140 	}
141 }
142 
143 /*
144  * convert Hz to AWE32 rate offset (see emux/soundfont.c)
145  */
146 
147 #define OFFSET_SAMPLERATE	1011119		/* base = 44100 */
148 #define SAMPLERATE_RATIO	4096
149 
150 static int calc_rate_offset(int hz)
151 {
152 	return snd_sf_linear_to_log(hz, OFFSET_SAMPLERATE, SAMPLERATE_RATIO);
153 }
154 
155 
156 /*
157  */
158 
159 static snd_pcm_hardware_t emu8k_pcm_hw = {
160 #ifdef USE_NONINTERLEAVE
161 	.info =			SNDRV_PCM_INFO_NONINTERLEAVED,
162 #else
163 	.info =			SNDRV_PCM_INFO_INTERLEAVED,
164 #endif
165 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
166 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
167 	.rate_min =		4000,
168 	.rate_max =		48000,
169 	.channels_min =		1,
170 	.channels_max =		2,
171 	.buffer_bytes_max =	(128*1024),
172 	.period_bytes_min =	1024,
173 	.period_bytes_max =	(128*1024),
174 	.periods_min =		2,
175 	.periods_max =		1024,
176 	.fifo_size =		0,
177 
178 };
179 
180 /*
181  * get the current position at the given channel from CCCA register
182  */
183 static inline int emu8k_get_curpos(emu8k_pcm_t *rec, int ch)
184 {
185 	int val = EMU8000_CCCA_READ(rec->emu, ch) & 0xfffffff;
186 	val -= rec->loop_start[ch] - 1;
187 	return val;
188 }
189 
190 
191 /*
192  * timer interrupt handler
193  * check the current position and update the period if necessary.
194  */
195 static void emu8k_pcm_timer_func(unsigned long data)
196 {
197 	emu8k_pcm_t *rec = (emu8k_pcm_t *)data;
198 	int ptr, delta;
199 
200 	spin_lock(&rec->timer_lock);
201 	/* update the current pointer */
202 	ptr = emu8k_get_curpos(rec, 0);
203 	if (ptr < rec->last_ptr)
204 		delta = ptr + rec->buf_size - rec->last_ptr;
205 	else
206 		delta = ptr - rec->last_ptr;
207 	rec->period_pos += delta;
208 	rec->last_ptr = ptr;
209 
210 	/* reprogram timer */
211 	rec->timer.expires = jiffies + 1;
212 	add_timer(&rec->timer);
213 
214 	/* update period */
215 	if (rec->period_pos >= (int)rec->period_size) {
216 		rec->period_pos %= rec->period_size;
217 		spin_unlock(&rec->timer_lock);
218 		snd_pcm_period_elapsed(rec->substream);
219 		return;
220 	}
221 	spin_unlock(&rec->timer_lock);
222 }
223 
224 
225 /*
226  * open pcm
227  * creating an instance here
228  */
229 static int emu8k_pcm_open(snd_pcm_substream_t *subs)
230 {
231 	emu8000_t *emu = snd_pcm_substream_chip(subs);
232 	emu8k_pcm_t *rec;
233 	snd_pcm_runtime_t *runtime = subs->runtime;
234 
235 	rec = kzalloc(sizeof(*rec), GFP_KERNEL);
236 	if (! rec)
237 		return -ENOMEM;
238 
239 	rec->emu = emu;
240 	rec->substream = subs;
241 	runtime->private_data = rec;
242 
243 	spin_lock_init(&rec->timer_lock);
244 	init_timer(&rec->timer);
245 	rec->timer.function = emu8k_pcm_timer_func;
246 	rec->timer.data = (unsigned long)rec;
247 
248 	runtime->hw = emu8k_pcm_hw;
249 	runtime->hw.buffer_bytes_max = emu->mem_size - LOOP_BLANK_SIZE * 3;
250 	runtime->hw.period_bytes_max = runtime->hw.buffer_bytes_max / 2;
251 
252 	/* use timer to update periods.. (specified in msec) */
253 	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_TIME,
254 				     (1000000 + HZ - 1) / HZ, UINT_MAX);
255 
256 	return 0;
257 }
258 
259 static int emu8k_pcm_close(snd_pcm_substream_t *subs)
260 {
261 	emu8k_pcm_t *rec = subs->runtime->private_data;
262 	kfree(rec);
263 	subs->runtime->private_data = NULL;
264 	return 0;
265 }
266 
267 /*
268  * calculate pitch target
269  */
270 static int calc_pitch_target(int pitch)
271 {
272 	int ptarget = 1 << (pitch >> 12);
273 	if (pitch & 0x800) ptarget += (ptarget * 0x102e) / 0x2710;
274 	if (pitch & 0x400) ptarget += (ptarget * 0x764) / 0x2710;
275 	if (pitch & 0x200) ptarget += (ptarget * 0x389) / 0x2710;
276 	ptarget += (ptarget >> 1);
277 	if (ptarget > 0xffff) ptarget = 0xffff;
278 	return ptarget;
279 }
280 
281 /*
282  * set up the voice
283  */
284 static void setup_voice(emu8k_pcm_t *rec, int ch)
285 {
286 	emu8000_t *hw = rec->emu;
287 	unsigned int temp;
288 
289 	/* channel to be silent and idle */
290 	EMU8000_DCYSUSV_WRITE(hw, ch, 0x0080);
291 	EMU8000_VTFT_WRITE(hw, ch, 0x0000FFFF);
292 	EMU8000_CVCF_WRITE(hw, ch, 0x0000FFFF);
293 	EMU8000_PTRX_WRITE(hw, ch, 0);
294 	EMU8000_CPF_WRITE(hw, ch, 0);
295 
296 	/* pitch offset */
297 	EMU8000_IP_WRITE(hw, ch, rec->pitch);
298 	/* set envelope parameters */
299 	EMU8000_ENVVAL_WRITE(hw, ch, 0x8000);
300 	EMU8000_ATKHLD_WRITE(hw, ch, 0x7f7f);
301 	EMU8000_DCYSUS_WRITE(hw, ch, 0x7f7f);
302 	EMU8000_ENVVOL_WRITE(hw, ch, 0x8000);
303 	EMU8000_ATKHLDV_WRITE(hw, ch, 0x7f7f);
304 	/* decay/sustain parameter for volume envelope is used
305 	   for triggerg the voice */
306 	/* modulation envelope heights */
307 	EMU8000_PEFE_WRITE(hw, ch, 0x0);
308 	/* lfo1/2 delay */
309 	EMU8000_LFO1VAL_WRITE(hw, ch, 0x8000);
310 	EMU8000_LFO2VAL_WRITE(hw, ch, 0x8000);
311 	/* lfo1 pitch & cutoff shift */
312 	EMU8000_FMMOD_WRITE(hw, ch, 0);
313 	/* lfo1 volume & freq */
314 	EMU8000_TREMFRQ_WRITE(hw, ch, 0);
315 	/* lfo2 pitch & freq */
316 	EMU8000_FM2FRQ2_WRITE(hw, ch, 0);
317 	/* pan & loop start */
318 	temp = rec->panning[ch];
319 	temp = (temp <<24) | ((unsigned int)rec->loop_start[ch] - 1);
320 	EMU8000_PSST_WRITE(hw, ch, temp);
321 	/* chorus & loop end (chorus 8bit, MSB) */
322 	temp = 0; // chorus
323 	temp = (temp << 24) | ((unsigned int)rec->loop_start[ch] + rec->buf_size - 1);
324 	EMU8000_CSL_WRITE(hw, ch, temp);
325 	/* Q & current address (Q 4bit value, MSB) */
326 	temp = 0; // filterQ
327 	temp = (temp << 28) | ((unsigned int)rec->loop_start[ch] - 1);
328 	EMU8000_CCCA_WRITE(hw, ch, temp);
329 	/* clear unknown registers */
330 	EMU8000_00A0_WRITE(hw, ch, 0);
331 	EMU8000_0080_WRITE(hw, ch, 0);
332 }
333 
334 /*
335  * trigger the voice
336  */
337 static void start_voice(emu8k_pcm_t *rec, int ch)
338 {
339 	unsigned long flags;
340 	emu8000_t *hw = rec->emu;
341 	unsigned int temp, aux;
342 	int pt = calc_pitch_target(rec->pitch);
343 
344 	/* cutoff and volume */
345 	EMU8000_IFATN_WRITE(hw, ch, 0xff00);
346 	EMU8000_VTFT_WRITE(hw, ch, 0xffff);
347 	EMU8000_CVCF_WRITE(hw, ch, 0xffff);
348 	/* trigger envelope */
349 	EMU8000_DCYSUSV_WRITE(hw, ch, 0x7f7f);
350 	/* set reverb and pitch target */
351 	temp = 0; // reverb
352 	if (rec->panning[ch] == 0)
353 		aux = 0xff;
354 	else
355 		aux = (-rec->panning[ch]) & 0xff;
356 	temp = (temp << 8) | (pt << 16) | aux;
357 	EMU8000_PTRX_WRITE(hw, ch, temp);
358 	EMU8000_CPF_WRITE(hw, ch, pt << 16);
359 
360 	/* start timer */
361 	spin_lock_irqsave(&rec->timer_lock, flags);
362 	if (! rec->timer_running) {
363 		rec->timer.expires = jiffies + 1;
364 		add_timer(&rec->timer);
365 		rec->timer_running = 1;
366 	}
367 	spin_unlock_irqrestore(&rec->timer_lock, flags);
368 }
369 
370 /*
371  * stop the voice immediately
372  */
373 static void stop_voice(emu8k_pcm_t *rec, int ch)
374 {
375 	unsigned long flags;
376 	emu8000_t *hw = rec->emu;
377 
378 	EMU8000_DCYSUSV_WRITE(hw, ch, 0x807F);
379 
380 	/* stop timer */
381 	spin_lock_irqsave(&rec->timer_lock, flags);
382 	if (rec->timer_running) {
383 		del_timer(&rec->timer);
384 		rec->timer_running = 0;
385 	}
386 	spin_unlock_irqrestore(&rec->timer_lock, flags);
387 }
388 
389 static int emu8k_pcm_trigger(snd_pcm_substream_t *subs, int cmd)
390 {
391 	emu8k_pcm_t *rec = subs->runtime->private_data;
392 	int ch;
393 
394 	switch (cmd) {
395 	case SNDRV_PCM_TRIGGER_START:
396 		for (ch = 0; ch < rec->voices; ch++)
397 			start_voice(rec, ch);
398 		rec->running = 1;
399 		break;
400 	case SNDRV_PCM_TRIGGER_STOP:
401 		rec->running = 0;
402 		for (ch = 0; ch < rec->voices; ch++)
403 			stop_voice(rec, ch);
404 		break;
405 	default:
406 		return -EINVAL;
407 	}
408 	return 0;
409 }
410 
411 
412 /*
413  * copy / silence ops
414  */
415 
416 /*
417  * this macro should be inserted in the copy/silence loops
418  * to reduce the latency.  without this, the system will hang up
419  * during the whole loop.
420  */
421 #define CHECK_SCHEDULER() \
422 do { \
423 	cond_resched();\
424 	if (signal_pending(current))\
425 		return -EAGAIN;\
426 } while (0)
427 
428 
429 #ifdef USE_NONINTERLEAVE
430 /* copy one channel block */
431 static int emu8k_transfer_block(emu8000_t *emu, int offset, unsigned short *buf, int count)
432 {
433 	EMU8000_SMALW_WRITE(emu, offset);
434 	while (count > 0) {
435 		unsigned short sval;
436 		CHECK_SCHEDULER();
437 		get_user(sval, buf);
438 		EMU8000_SMLD_WRITE(emu, sval);
439 		buf++;
440 		count--;
441 	}
442 	return 0;
443 }
444 
445 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
446 			  int voice,
447 			  snd_pcm_uframes_t pos,
448 			  void *src,
449 			  snd_pcm_uframes_t count)
450 {
451 	emu8k_pcm_t *rec = subs->runtime->private_data;
452 	emu8000_t *emu = rec->emu;
453 
454 	snd_emu8000_write_wait(emu, 1);
455 	if (voice == -1) {
456 		unsigned short *buf = src;
457 		int i, err;
458 		count /= rec->voices;
459 		for (i = 0; i < rec->voices; i++) {
460 			err = emu8k_transfer_block(emu, pos + rec->loop_start[i], buf, count);
461 			if (err < 0)
462 				return err;
463 			buf += count;
464 		}
465 		return 0;
466 	} else {
467 		return emu8k_transfer_block(emu, pos + rec->loop_start[voice], src, count);
468 	}
469 }
470 
471 /* make a channel block silence */
472 static int emu8k_silence_block(emu8000_t *emu, int offset, int count)
473 {
474 	EMU8000_SMALW_WRITE(emu, offset);
475 	while (count > 0) {
476 		CHECK_SCHEDULER();
477 		EMU8000_SMLD_WRITE(emu, 0);
478 		count--;
479 	}
480 	return 0;
481 }
482 
483 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
484 			     int voice,
485 			     snd_pcm_uframes_t pos,
486 			     snd_pcm_uframes_t count)
487 {
488 	emu8k_pcm_t *rec = subs->runtime->private_data;
489 	emu8000_t *emu = rec->emu;
490 
491 	snd_emu8000_write_wait(emu, 1);
492 	if (voice == -1 && rec->voices == 1)
493 		voice = 0;
494 	if (voice == -1) {
495 		int err;
496 		err = emu8k_silence_block(emu, pos + rec->loop_start[0], count / 2);
497 		if (err < 0)
498 			return err;
499 		return emu8k_silence_block(emu, pos + rec->loop_start[1], count / 2);
500 	} else {
501 		return emu8k_silence_block(emu, pos + rec->loop_start[voice], count);
502 	}
503 }
504 
505 #else /* interleave */
506 
507 /*
508  * copy the interleaved data can be done easily by using
509  * DMA "left" and "right" channels on emu8k engine.
510  */
511 static int emu8k_pcm_copy(snd_pcm_substream_t *subs,
512 			  int voice,
513 			  snd_pcm_uframes_t pos,
514 			  void __user *src,
515 			  snd_pcm_uframes_t count)
516 {
517 	emu8k_pcm_t *rec = subs->runtime->private_data;
518 	emu8000_t *emu = rec->emu;
519 	unsigned short __user *buf = src;
520 
521 	snd_emu8000_write_wait(emu, 1);
522 	EMU8000_SMALW_WRITE(emu, pos + rec->loop_start[0]);
523 	if (rec->voices > 1)
524 		EMU8000_SMARW_WRITE(emu, pos + rec->loop_start[1]);
525 
526 	while (count-- > 0) {
527 		unsigned short sval;
528 		CHECK_SCHEDULER();
529 		get_user(sval, buf);
530 		EMU8000_SMLD_WRITE(emu, sval);
531 		buf++;
532 		if (rec->voices > 1) {
533 			CHECK_SCHEDULER();
534 			get_user(sval, buf);
535 			EMU8000_SMRD_WRITE(emu, sval);
536 			buf++;
537 		}
538 	}
539 	return 0;
540 }
541 
542 static int emu8k_pcm_silence(snd_pcm_substream_t *subs,
543 			     int voice,
544 			     snd_pcm_uframes_t pos,
545 			     snd_pcm_uframes_t count)
546 {
547 	emu8k_pcm_t *rec = subs->runtime->private_data;
548 	emu8000_t *emu = rec->emu;
549 
550 	snd_emu8000_write_wait(emu, 1);
551 	EMU8000_SMALW_WRITE(emu, rec->loop_start[0] + pos);
552 	if (rec->voices > 1)
553 		EMU8000_SMARW_WRITE(emu, rec->loop_start[1] + pos);
554 	while (count-- > 0) {
555 		CHECK_SCHEDULER();
556 		EMU8000_SMLD_WRITE(emu, 0);
557 		if (rec->voices > 1) {
558 			CHECK_SCHEDULER();
559 			EMU8000_SMRD_WRITE(emu, 0);
560 		}
561 	}
562 	return 0;
563 }
564 #endif
565 
566 
567 /*
568  * allocate a memory block
569  */
570 static int emu8k_pcm_hw_params(snd_pcm_substream_t *subs,
571 			       snd_pcm_hw_params_t *hw_params)
572 {
573 	emu8k_pcm_t *rec = subs->runtime->private_data;
574 
575 	if (rec->block) {
576 		/* reallocation - release the old block */
577 		snd_util_mem_free(rec->emu->memhdr, rec->block);
578 		rec->block = NULL;
579 	}
580 
581 	rec->allocated_bytes = params_buffer_bytes(hw_params) + LOOP_BLANK_SIZE * 4;
582 	rec->block = snd_util_mem_alloc(rec->emu->memhdr, rec->allocated_bytes);
583 	if (! rec->block)
584 		return -ENOMEM;
585 	rec->offset = EMU8000_DRAM_OFFSET + (rec->block->offset >> 1); /* in word */
586 	/* at least dma_bytes must be set for non-interleaved mode */
587 	subs->dma_buffer.bytes = params_buffer_bytes(hw_params);
588 
589 	return 0;
590 }
591 
592 /*
593  * free the memory block
594  */
595 static int emu8k_pcm_hw_free(snd_pcm_substream_t *subs)
596 {
597 	emu8k_pcm_t *rec = subs->runtime->private_data;
598 
599 	if (rec->block) {
600 		int ch;
601 		for (ch = 0; ch < rec->voices; ch++)
602 			stop_voice(rec, ch); // to be sure
603 		if (rec->dram_opened)
604 			emu8k_close_dram(rec->emu);
605 		snd_util_mem_free(rec->emu->memhdr, rec->block);
606 		rec->block = NULL;
607 	}
608 	return 0;
609 }
610 
611 /*
612  */
613 static int emu8k_pcm_prepare(snd_pcm_substream_t *subs)
614 {
615 	emu8k_pcm_t *rec = subs->runtime->private_data;
616 
617 	rec->pitch = 0xe000 + calc_rate_offset(subs->runtime->rate);
618 	rec->last_ptr = 0;
619 	rec->period_pos = 0;
620 
621 	rec->buf_size = subs->runtime->buffer_size;
622 	rec->period_size = subs->runtime->period_size;
623 	rec->voices = subs->runtime->channels;
624 	rec->loop_start[0] = rec->offset + LOOP_BLANK_SIZE;
625 	if (rec->voices > 1)
626 		rec->loop_start[1] = rec->loop_start[0] + rec->buf_size + LOOP_BLANK_SIZE;
627 	if (rec->voices > 1) {
628 		rec->panning[0] = 0xff;
629 		rec->panning[1] = 0x00;
630 	} else
631 		rec->panning[0] = 0x80;
632 
633 	if (! rec->dram_opened) {
634 		int err, i, ch;
635 
636 		snd_emux_terminate_all(rec->emu->emu);
637 		if ((err = emu8k_open_dram_for_pcm(rec->emu, rec->voices)) != 0)
638 			return err;
639 		rec->dram_opened = 1;
640 
641 		/* clear loop blanks */
642 		snd_emu8000_write_wait(rec->emu, 0);
643 		EMU8000_SMALW_WRITE(rec->emu, rec->offset);
644 		for (i = 0; i < LOOP_BLANK_SIZE; i++)
645 			EMU8000_SMLD_WRITE(rec->emu, 0);
646 		for (ch = 0; ch < rec->voices; ch++) {
647 			EMU8000_SMALW_WRITE(rec->emu, rec->loop_start[ch] + rec->buf_size);
648 			for (i = 0; i < LOOP_BLANK_SIZE; i++)
649 				EMU8000_SMLD_WRITE(rec->emu, 0);
650 		}
651 	}
652 
653 	setup_voice(rec, 0);
654 	if (rec->voices > 1)
655 		setup_voice(rec, 1);
656 	return 0;
657 }
658 
659 static snd_pcm_uframes_t emu8k_pcm_pointer(snd_pcm_substream_t *subs)
660 {
661 	emu8k_pcm_t *rec = subs->runtime->private_data;
662 	if (rec->running)
663 		return emu8k_get_curpos(rec, 0);
664 	return 0;
665 }
666 
667 
668 static snd_pcm_ops_t emu8k_pcm_ops = {
669 	.open =		emu8k_pcm_open,
670 	.close =	emu8k_pcm_close,
671 	.ioctl =	snd_pcm_lib_ioctl,
672 	.hw_params =	emu8k_pcm_hw_params,
673 	.hw_free =	emu8k_pcm_hw_free,
674 	.prepare =	emu8k_pcm_prepare,
675 	.trigger =	emu8k_pcm_trigger,
676 	.pointer =	emu8k_pcm_pointer,
677 	.copy =		emu8k_pcm_copy,
678 	.silence =	emu8k_pcm_silence,
679 };
680 
681 
682 static void snd_emu8000_pcm_free(snd_pcm_t *pcm)
683 {
684 	emu8000_t *emu = pcm->private_data;
685 	emu->pcm = NULL;
686 }
687 
688 int snd_emu8000_pcm_new(snd_card_t *card, emu8000_t *emu, int index)
689 {
690 	snd_pcm_t *pcm;
691 	int err;
692 
693 	if ((err = snd_pcm_new(card, "Emu8000 PCM", index, 1, 0, &pcm)) < 0)
694 		return err;
695 	pcm->private_data = emu;
696 	pcm->private_free = snd_emu8000_pcm_free;
697 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &emu8k_pcm_ops);
698 	emu->pcm = pcm;
699 
700 	snd_device_register(card, pcm);
701 
702 	return 0;
703 }
704