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