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