[ALSA] Fix permissions in some /proc files
[linux-2.6.git] / sound / pci / emu10k1 / emu10k1x.c
1 /*
2  *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
3  *  Driver EMU10K1X chips
4  *
5  *  Parts of this code were adapted from audigyls.c driver which is
6  *  Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
7  *
8  *  BUGS:
9  *    --
10  *
11  *  TODO:
12  *
13  *  Chips (SB0200 model):
14  *    - EMU10K1X-DBQ
15  *    - STAC 9708T
16  *
17  *   This program is free software; you can redistribute it and/or modify
18  *   it under the terms of the GNU General Public License as published by
19  *   the Free Software Foundation; either version 2 of the License, or
20  *   (at your option) any later version.
21  *
22  *   This program is distributed in the hope that it will be useful,
23  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *   GNU General Public License for more details.
26  *
27  *   You should have received a copy of the GNU General Public License
28  *   along with this program; if not, write to the Free Software
29  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
30  *
31  */
32 #include <sound/driver.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/pci.h>
36 #include <linux/slab.h>
37 #include <linux/moduleparam.h>
38 #include <sound/core.h>
39 #include <sound/initval.h>
40 #include <sound/pcm.h>
41 #include <sound/ac97_codec.h>
42 #include <sound/info.h>
43 #include <sound/rawmidi.h>
44
45 MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
46 MODULE_DESCRIPTION("EMU10K1X");
47 MODULE_LICENSE("GPL");
48 MODULE_SUPPORTED_DEVICE("{{Dell Creative Labs,SB Live!}");
49
50 // module parameters (see "Module Parameters")
51 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
52 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
53 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
54
55 module_param_array(index, int, NULL, 0444);
56 MODULE_PARM_DESC(index, "Index value for the EMU10K1X soundcard.");
57 module_param_array(id, charp, NULL, 0444);
58 MODULE_PARM_DESC(id, "ID string for the EMU10K1X soundcard.");
59 module_param_array(enable, bool, NULL, 0444);
60 MODULE_PARM_DESC(enable, "Enable the EMU10K1X soundcard.");
61
62
63 // some definitions were borrowed from emu10k1 driver as they seem to be the same
64 /************************************************************************************************/
65 /* PCI function 0 registers, address = <val> + PCIBASE0                                         */
66 /************************************************************************************************/
67
68 #define PTR                     0x00            /* Indexed register set pointer register        */
69                                                 /* NOTE: The CHANNELNUM and ADDRESS words can   */
70                                                 /* be modified independently of each other.     */
71
72 #define DATA                    0x04            /* Indexed register set data register           */
73
74 #define IPR                     0x08            /* Global interrupt pending register            */
75                                                 /* Clear pending interrupts by writing a 1 to   */
76                                                 /* the relevant bits and zero to the other bits */
77 #define IPR_MIDITRANSBUFEMPTY   0x00000001      /* MIDI UART transmit buffer empty              */
78 #define IPR_MIDIRECVBUFEMPTY    0x00000002      /* MIDI UART receive buffer empty               */
79 #define IPR_CH_0_LOOP           0x00000800      /* Channel 0 loop                               */
80 #define IPR_CH_0_HALF_LOOP      0x00000100      /* Channel 0 half loop                          */
81 #define IPR_CAP_0_LOOP          0x00080000      /* Channel capture loop                         */
82 #define IPR_CAP_0_HALF_LOOP     0x00010000      /* Channel capture half loop                    */
83
84 #define INTE                    0x0c            /* Interrupt enable register                    */
85 #define INTE_MIDITXENABLE       0x00000001      /* Enable MIDI transmit-buffer-empty interrupts */
86 #define INTE_MIDIRXENABLE       0x00000002      /* Enable MIDI receive-buffer-empty interrupts  */
87 #define INTE_CH_0_LOOP          0x00000800      /* Channel 0 loop                               */
88 #define INTE_CH_0_HALF_LOOP     0x00000100      /* Channel 0 half loop                          */
89 #define INTE_CAP_0_LOOP         0x00080000      /* Channel capture loop                         */
90 #define INTE_CAP_0_HALF_LOOP    0x00010000      /* Channel capture half loop                    */
91
92 #define HCFG                    0x14            /* Hardware config register                     */
93
94 #define HCFG_LOCKSOUNDCACHE     0x00000008      /* 1 = Cancel bustmaster accesses to soundcache */
95                                                 /* NOTE: This should generally never be used.   */
96 #define HCFG_AUDIOENABLE        0x00000001      /* 0 = CODECs transmit zero-valued samples      */
97                                                 /* Should be set to 1 when the EMU10K1 is       */
98                                                 /* completely initialized.                      */
99 #define GPIO                    0x18            /* Defaults: 00001080-Analog, 00001000-SPDIF.   */
100
101
102 #define AC97DATA                0x1c            /* AC97 register set data register (16 bit)     */
103
104 #define AC97ADDRESS             0x1e            /* AC97 register set address register (8 bit)   */
105
106 /********************************************************************************************************/
107 /* Emu10k1x pointer-offset register set, accessed through the PTR and DATA registers                    */
108 /********************************************************************************************************/
109 #define PLAYBACK_LIST_ADDR      0x00            /* Base DMA address of a list of pointers to each period/size */
110                                                 /* One list entry: 4 bytes for DMA address, 
111                                                  * 4 bytes for period_size << 16.
112                                                  * One list entry is 8 bytes long.
113                                                  * One list entry for each period in the buffer.
114                                                  */
115 #define PLAYBACK_LIST_SIZE      0x01            /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000  */
116 #define PLAYBACK_LIST_PTR       0x02            /* Pointer to the current period being played */
117 #define PLAYBACK_DMA_ADDR       0x04            /* Playback DMA addresss */
118 #define PLAYBACK_PERIOD_SIZE    0x05            /* Playback period size */
119 #define PLAYBACK_POINTER        0x06            /* Playback period pointer. Sample currently in DAC */
120 #define PLAYBACK_UNKNOWN1       0x07
121 #define PLAYBACK_UNKNOWN2       0x08
122
123 /* Only one capture channel supported */
124 #define CAPTURE_DMA_ADDR        0x10            /* Capture DMA address */
125 #define CAPTURE_BUFFER_SIZE     0x11            /* Capture buffer size */
126 #define CAPTURE_POINTER         0x12            /* Capture buffer pointer. Sample currently in ADC */
127 #define CAPTURE_UNKNOWN         0x13
128
129 /* From 0x20 - 0x3f, last samples played on each channel */
130
131 #define TRIGGER_CHANNEL         0x40            /* Trigger channel playback                     */
132 #define TRIGGER_CHANNEL_0       0x00000001      /* Trigger channel 0                            */
133 #define TRIGGER_CHANNEL_1       0x00000002      /* Trigger channel 1                            */
134 #define TRIGGER_CHANNEL_2       0x00000004      /* Trigger channel 2                            */
135 #define TRIGGER_CAPTURE         0x00000100      /* Trigger capture channel                      */
136
137 #define ROUTING                 0x41            /* Setup sound routing ?                        */
138 #define ROUTING_FRONT_LEFT      0x00000001
139 #define ROUTING_FRONT_RIGHT     0x00000002
140 #define ROUTING_REAR_LEFT       0x00000004
141 #define ROUTING_REAR_RIGHT      0x00000008
142 #define ROUTING_CENTER_LFE      0x00010000
143
144 #define SPCS0                   0x42            /* SPDIF output Channel Status 0 register       */
145
146 #define SPCS1                   0x43            /* SPDIF output Channel Status 1 register       */
147
148 #define SPCS2                   0x44            /* SPDIF output Channel Status 2 register       */
149
150 #define SPCS_CLKACCYMASK        0x30000000      /* Clock accuracy                               */
151 #define SPCS_CLKACCY_1000PPM    0x00000000      /* 1000 parts per million                       */
152 #define SPCS_CLKACCY_50PPM      0x10000000      /* 50 parts per million                         */
153 #define SPCS_CLKACCY_VARIABLE   0x20000000      /* Variable accuracy                            */
154 #define SPCS_SAMPLERATEMASK     0x0f000000      /* Sample rate                                  */
155 #define SPCS_SAMPLERATE_44      0x00000000      /* 44.1kHz sample rate                          */
156 #define SPCS_SAMPLERATE_48      0x02000000      /* 48kHz sample rate                            */
157 #define SPCS_SAMPLERATE_32      0x03000000      /* 32kHz sample rate                            */
158 #define SPCS_CHANNELNUMMASK     0x00f00000      /* Channel number                               */
159 #define SPCS_CHANNELNUM_UNSPEC  0x00000000      /* Unspecified channel number                   */
160 #define SPCS_CHANNELNUM_LEFT    0x00100000      /* Left channel                                 */
161 #define SPCS_CHANNELNUM_RIGHT   0x00200000      /* Right channel                                */
162 #define SPCS_SOURCENUMMASK      0x000f0000      /* Source number                                */
163 #define SPCS_SOURCENUM_UNSPEC   0x00000000      /* Unspecified source number                    */
164 #define SPCS_GENERATIONSTATUS   0x00008000      /* Originality flag (see IEC-958 spec)          */
165 #define SPCS_CATEGORYCODEMASK   0x00007f00      /* Category code (see IEC-958 spec)             */
166 #define SPCS_MODEMASK           0x000000c0      /* Mode (see IEC-958 spec)                      */
167 #define SPCS_EMPHASISMASK       0x00000038      /* Emphasis                                     */
168 #define SPCS_EMPHASIS_NONE      0x00000000      /* No emphasis                                  */
169 #define SPCS_EMPHASIS_50_15     0x00000008      /* 50/15 usec 2 channel                         */
170 #define SPCS_COPYRIGHT          0x00000004      /* Copyright asserted flag -- do not modify     */
171 #define SPCS_NOTAUDIODATA       0x00000002      /* 0 = Digital audio, 1 = not audio             */
172 #define SPCS_PROFESSIONAL       0x00000001      /* 0 = Consumer (IEC-958), 1 = pro (AES3-1992)  */
173
174 #define SPDIF_SELECT            0x45            /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
175
176 /* This is the MPU port on the card                                                             */
177 #define MUDATA          0x47
178 #define MUCMD           0x48
179 #define MUSTAT          MUCMD
180
181 /* From 0x50 - 0x5f, last samples captured */
182
183 /**
184  * The hardware has 3 channels for playback and 1 for capture.
185  *  - channel 0 is the front channel
186  *  - channel 1 is the rear channel
187  *  - channel 2 is the center/lfe chanel
188  * Volume is controlled by the AC97 for the front and rear channels by
189  * the PCM Playback Volume, Sigmatel Surround Playback Volume and 
190  * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
191  * the front/rear channel mixing in the REAR OUT jack. When using the
192  * 4-Speaker Stereo, both front and rear channels will be mixed in the
193  * REAR OUT.
194  * The center/lfe channel has no volume control and cannot be muted during
195  * playback.
196  */
197
198 typedef struct snd_emu10k1x_voice emu10k1x_voice_t;
199 typedef struct snd_emu10k1x emu10k1x_t;
200 typedef struct snd_emu10k1x_pcm emu10k1x_pcm_t;
201
202 struct snd_emu10k1x_voice {
203         emu10k1x_t *emu;
204         int number;
205         int use;
206   
207         emu10k1x_pcm_t *epcm;
208 };
209
210 struct snd_emu10k1x_pcm {
211         emu10k1x_t *emu;
212         snd_pcm_substream_t *substream;
213         emu10k1x_voice_t *voice;
214         unsigned short running;
215 };
216
217 typedef struct {
218         struct snd_emu10k1x *emu;
219         snd_rawmidi_t *rmidi;
220         snd_rawmidi_substream_t *substream_input;
221         snd_rawmidi_substream_t *substream_output;
222         unsigned int midi_mode;
223         spinlock_t input_lock;
224         spinlock_t output_lock;
225         spinlock_t open_lock;
226         int tx_enable, rx_enable;
227         int port;
228         int ipr_tx, ipr_rx;
229         void (*interrupt)(emu10k1x_t *emu, unsigned int status);
230 } emu10k1x_midi_t;
231
232 // definition of the chip-specific record
233 struct snd_emu10k1x {
234         snd_card_t *card;
235         struct pci_dev *pci;
236
237         unsigned long port;
238         struct resource *res_port;
239         int irq;
240
241         unsigned int revision;          /* chip revision */
242         unsigned int serial;            /* serial number */
243         unsigned short model;           /* subsystem id */
244
245         spinlock_t emu_lock;
246         spinlock_t voice_lock;
247
248         ac97_t *ac97;
249         snd_pcm_t *pcm;
250
251         emu10k1x_voice_t voices[3];
252         emu10k1x_voice_t capture_voice;
253         u32 spdif_bits[3]; // SPDIF out setup
254
255         struct snd_dma_buffer dma_buffer;
256
257         emu10k1x_midi_t midi;
258 };
259
260 /* hardware definition */
261 static snd_pcm_hardware_t snd_emu10k1x_playback_hw = {
262         .info =                 (SNDRV_PCM_INFO_MMAP | 
263                                  SNDRV_PCM_INFO_INTERLEAVED |
264                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
265                                  SNDRV_PCM_INFO_MMAP_VALID),
266         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
267         .rates =                SNDRV_PCM_RATE_48000,
268         .rate_min =             48000,
269         .rate_max =             48000,
270         .channels_min =         2,
271         .channels_max =         2,
272         .buffer_bytes_max =     (32*1024),
273         .period_bytes_min =     64,
274         .period_bytes_max =     (16*1024),
275         .periods_min =          2,
276         .periods_max =          8,
277         .fifo_size =            0,
278 };
279
280 static snd_pcm_hardware_t snd_emu10k1x_capture_hw = {
281         .info =                 (SNDRV_PCM_INFO_MMAP | 
282                                  SNDRV_PCM_INFO_INTERLEAVED |
283                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
284                                  SNDRV_PCM_INFO_MMAP_VALID),
285         .formats =              SNDRV_PCM_FMTBIT_S16_LE,
286         .rates =                SNDRV_PCM_RATE_48000,
287         .rate_min =             48000,
288         .rate_max =             48000,
289         .channels_min =         2,
290         .channels_max =         2,
291         .buffer_bytes_max =     (32*1024),
292         .period_bytes_min =     64,
293         .period_bytes_max =     (16*1024),
294         .periods_min =          2,
295         .periods_max =          2,
296         .fifo_size =            0,
297 };
298
299 static unsigned int snd_emu10k1x_ptr_read(emu10k1x_t * emu, 
300                                           unsigned int reg, 
301                                           unsigned int chn)
302 {
303         unsigned long flags;
304         unsigned int regptr, val;
305   
306         regptr = (reg << 16) | chn;
307
308         spin_lock_irqsave(&emu->emu_lock, flags);
309         outl(regptr, emu->port + PTR);
310         val = inl(emu->port + DATA);
311         spin_unlock_irqrestore(&emu->emu_lock, flags);
312         return val;
313 }
314
315 static void snd_emu10k1x_ptr_write(emu10k1x_t *emu, 
316                                    unsigned int reg, 
317                                    unsigned int chn, 
318                                    unsigned int data)
319 {
320         unsigned int regptr;
321         unsigned long flags;
322
323         regptr = (reg << 16) | chn;
324
325         spin_lock_irqsave(&emu->emu_lock, flags);
326         outl(regptr, emu->port + PTR);
327         outl(data, emu->port + DATA);
328         spin_unlock_irqrestore(&emu->emu_lock, flags);
329 }
330
331 static void snd_emu10k1x_intr_enable(emu10k1x_t *emu, unsigned int intrenb)
332 {
333         unsigned long flags;
334         unsigned int enable;
335   
336         spin_lock_irqsave(&emu->emu_lock, flags);
337         enable = inl(emu->port + INTE) | intrenb;
338         outl(enable, emu->port + INTE);
339         spin_unlock_irqrestore(&emu->emu_lock, flags);
340 }
341
342 static void snd_emu10k1x_intr_disable(emu10k1x_t *emu, unsigned int intrenb)
343 {
344         unsigned long flags;
345         unsigned int enable;
346   
347         spin_lock_irqsave(&emu->emu_lock, flags);
348         enable = inl(emu->port + INTE) & ~intrenb;
349         outl(enable, emu->port + INTE);
350         spin_unlock_irqrestore(&emu->emu_lock, flags);
351 }
352
353 static void snd_emu10k1x_gpio_write(emu10k1x_t *emu, unsigned int value)
354 {
355         unsigned long flags;
356
357         spin_lock_irqsave(&emu->emu_lock, flags);
358         outl(value, emu->port + GPIO);
359         spin_unlock_irqrestore(&emu->emu_lock, flags);
360 }
361
362 static void snd_emu10k1x_pcm_free_substream(snd_pcm_runtime_t *runtime)
363 {
364         emu10k1x_pcm_t *epcm = runtime->private_data;
365   
366         if (epcm)
367                 kfree(epcm);
368 }
369
370 static void snd_emu10k1x_pcm_interrupt(emu10k1x_t *emu, emu10k1x_voice_t *voice)
371 {
372         emu10k1x_pcm_t *epcm;
373
374         if ((epcm = voice->epcm) == NULL)
375                 return;
376         if (epcm->substream == NULL)
377                 return;
378 #if 0
379         snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
380                    epcm->substream->ops->pointer(epcm->substream),
381                    snd_pcm_lib_period_bytes(epcm->substream),
382                    snd_pcm_lib_buffer_bytes(epcm->substream));
383 #endif
384         snd_pcm_period_elapsed(epcm->substream);
385 }
386
387 /* open callback */
388 static int snd_emu10k1x_playback_open(snd_pcm_substream_t *substream)
389 {
390         emu10k1x_t *chip = snd_pcm_substream_chip(substream);
391         emu10k1x_pcm_t *epcm;
392         snd_pcm_runtime_t *runtime = substream->runtime;
393         int err;
394
395         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
396                 return err;
397         }
398         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
399                 return err;
400
401         epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
402         if (epcm == NULL)
403                 return -ENOMEM;
404         epcm->emu = chip;
405         epcm->substream = substream;
406   
407         runtime->private_data = epcm;
408         runtime->private_free = snd_emu10k1x_pcm_free_substream;
409   
410         runtime->hw = snd_emu10k1x_playback_hw;
411
412         return 0;
413 }
414
415 /* close callback */
416 static int snd_emu10k1x_playback_close(snd_pcm_substream_t *substream)
417 {
418         return 0;
419 }
420
421 /* hw_params callback */
422 static int snd_emu10k1x_pcm_hw_params(snd_pcm_substream_t *substream,
423                                       snd_pcm_hw_params_t * hw_params)
424 {
425         snd_pcm_runtime_t *runtime = substream->runtime;
426         emu10k1x_pcm_t *epcm = runtime->private_data;
427
428         if (! epcm->voice) {
429                 epcm->voice = &epcm->emu->voices[substream->pcm->device];
430                 epcm->voice->use = 1;
431                 epcm->voice->epcm = epcm;
432         }
433
434         return snd_pcm_lib_malloc_pages(substream,
435                                         params_buffer_bytes(hw_params));
436 }
437
438 /* hw_free callback */
439 static int snd_emu10k1x_pcm_hw_free(snd_pcm_substream_t *substream)
440 {
441         snd_pcm_runtime_t *runtime = substream->runtime;
442         emu10k1x_pcm_t *epcm;
443
444         if (runtime->private_data == NULL)
445                 return 0;
446         
447         epcm = runtime->private_data;
448
449         if (epcm->voice) {
450                 epcm->voice->use = 0;
451                 epcm->voice->epcm = NULL;
452                 epcm->voice = NULL;
453         }
454
455         return snd_pcm_lib_free_pages(substream);
456 }
457
458 /* prepare callback */
459 static int snd_emu10k1x_pcm_prepare(snd_pcm_substream_t *substream)
460 {
461         emu10k1x_t *emu = snd_pcm_substream_chip(substream);
462         snd_pcm_runtime_t *runtime = substream->runtime;
463         emu10k1x_pcm_t *epcm = runtime->private_data;
464         int voice = epcm->voice->number;
465         u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
466         u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
467         int i;
468         
469         for(i=0; i < runtime->periods; i++) {
470                 *table_base++=runtime->dma_addr+(i*period_size_bytes);
471                 *table_base++=period_size_bytes<<16;
472         }
473
474         snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
475         snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
476         snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
477         snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
478         snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
479         snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
480         snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
481
482         snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
483
484         return 0;
485 }
486
487 /* trigger callback */
488 static int snd_emu10k1x_pcm_trigger(snd_pcm_substream_t *substream,
489                                     int cmd)
490 {
491         emu10k1x_t *emu = snd_pcm_substream_chip(substream);
492         snd_pcm_runtime_t *runtime = substream->runtime;
493         emu10k1x_pcm_t *epcm = runtime->private_data;
494         int channel = epcm->voice->number;
495         int result = 0;
496
497 //      snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
498
499         switch (cmd) {
500         case SNDRV_PCM_TRIGGER_START:
501                 if(runtime->periods == 2)
502                         snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
503                 else
504                         snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
505                 epcm->running = 1;
506                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
507                 break;
508         case SNDRV_PCM_TRIGGER_STOP:
509                 epcm->running = 0;
510                 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
511                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
512                 break;
513         default:
514                 result = -EINVAL;
515                 break;
516         }
517         return result;
518 }
519
520 /* pointer callback */
521 static snd_pcm_uframes_t
522 snd_emu10k1x_pcm_pointer(snd_pcm_substream_t *substream)
523 {
524         emu10k1x_t *emu = snd_pcm_substream_chip(substream);
525         snd_pcm_runtime_t *runtime = substream->runtime;
526         emu10k1x_pcm_t *epcm = runtime->private_data;
527         int channel = epcm->voice->number;
528         snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
529
530         if (!epcm->running)
531                 return 0;
532
533         ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
534         ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
535         ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
536
537         if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
538                 return 0;
539         
540         if (ptr3 != ptr4) 
541                 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
542         ptr2 = bytes_to_frames(runtime, ptr1);
543         ptr2 += (ptr4 >> 3) * runtime->period_size;
544         ptr = ptr2;
545
546         if (ptr >= runtime->buffer_size)
547                 ptr -= runtime->buffer_size;
548
549         return ptr;
550 }
551
552 /* operators */
553 static snd_pcm_ops_t snd_emu10k1x_playback_ops = {
554         .open =        snd_emu10k1x_playback_open,
555         .close =       snd_emu10k1x_playback_close,
556         .ioctl =       snd_pcm_lib_ioctl,
557         .hw_params =   snd_emu10k1x_pcm_hw_params,
558         .hw_free =     snd_emu10k1x_pcm_hw_free,
559         .prepare =     snd_emu10k1x_pcm_prepare,
560         .trigger =     snd_emu10k1x_pcm_trigger,
561         .pointer =     snd_emu10k1x_pcm_pointer,
562 };
563
564 /* open_capture callback */
565 static int snd_emu10k1x_pcm_open_capture(snd_pcm_substream_t *substream)
566 {
567         emu10k1x_t *chip = snd_pcm_substream_chip(substream);
568         emu10k1x_pcm_t *epcm;
569         snd_pcm_runtime_t *runtime = substream->runtime;
570         int err;
571
572         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
573                 return err;
574         if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
575                 return err;
576
577         epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
578         if (epcm == NULL)
579                 return -ENOMEM;
580
581         epcm->emu = chip;
582         epcm->substream = substream;
583
584         runtime->private_data = epcm;
585         runtime->private_free = snd_emu10k1x_pcm_free_substream;
586
587         runtime->hw = snd_emu10k1x_capture_hw;
588
589         return 0;
590 }
591
592 /* close callback */
593 static int snd_emu10k1x_pcm_close_capture(snd_pcm_substream_t *substream)
594 {
595         return 0;
596 }
597
598 /* hw_params callback */
599 static int snd_emu10k1x_pcm_hw_params_capture(snd_pcm_substream_t *substream,
600                                               snd_pcm_hw_params_t * hw_params)
601 {
602         snd_pcm_runtime_t *runtime = substream->runtime;
603         emu10k1x_pcm_t *epcm = runtime->private_data;
604
605         if (! epcm->voice) {
606                 if (epcm->emu->capture_voice.use)
607                         return -EBUSY;
608                 epcm->voice = &epcm->emu->capture_voice;
609                 epcm->voice->epcm = epcm;
610                 epcm->voice->use = 1;
611         }
612
613         return snd_pcm_lib_malloc_pages(substream,
614                                         params_buffer_bytes(hw_params));
615 }
616
617 /* hw_free callback */
618 static int snd_emu10k1x_pcm_hw_free_capture(snd_pcm_substream_t *substream)
619 {
620         snd_pcm_runtime_t *runtime = substream->runtime;
621
622         emu10k1x_pcm_t *epcm;
623
624         if (runtime->private_data == NULL)
625                 return 0;
626         epcm = runtime->private_data;
627
628         if (epcm->voice) {
629                 epcm->voice->use = 0;
630                 epcm->voice->epcm = NULL;
631                 epcm->voice = NULL;
632         }
633
634         return snd_pcm_lib_free_pages(substream);
635 }
636
637 /* prepare capture callback */
638 static int snd_emu10k1x_pcm_prepare_capture(snd_pcm_substream_t *substream)
639 {
640         emu10k1x_t *emu = snd_pcm_substream_chip(substream);
641         snd_pcm_runtime_t *runtime = substream->runtime;
642
643         snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
644         snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
645         snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
646         snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
647
648         return 0;
649 }
650
651 /* trigger_capture callback */
652 static int snd_emu10k1x_pcm_trigger_capture(snd_pcm_substream_t *substream,
653                                             int cmd)
654 {
655         emu10k1x_t *emu = snd_pcm_substream_chip(substream);
656         snd_pcm_runtime_t *runtime = substream->runtime;
657         emu10k1x_pcm_t *epcm = runtime->private_data;
658         int result = 0;
659
660         switch (cmd) {
661         case SNDRV_PCM_TRIGGER_START:
662                 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP | 
663                                          INTE_CAP_0_HALF_LOOP);
664                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
665                 epcm->running = 1;
666                 break;
667         case SNDRV_PCM_TRIGGER_STOP:
668                 epcm->running = 0;
669                 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP | 
670                                           INTE_CAP_0_HALF_LOOP);
671                 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
672                 break;
673         default:
674                 result = -EINVAL;
675                 break;
676         }
677         return result;
678 }
679
680 /* pointer_capture callback */
681 static snd_pcm_uframes_t
682 snd_emu10k1x_pcm_pointer_capture(snd_pcm_substream_t *substream)
683 {
684         emu10k1x_t *emu = snd_pcm_substream_chip(substream);
685         snd_pcm_runtime_t *runtime = substream->runtime;
686         emu10k1x_pcm_t *epcm = runtime->private_data;
687         snd_pcm_uframes_t ptr;
688
689         if (!epcm->running)
690                 return 0;
691
692         ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
693         if (ptr >= runtime->buffer_size)
694                 ptr -= runtime->buffer_size;
695
696         return ptr;
697 }
698
699 static snd_pcm_ops_t snd_emu10k1x_capture_ops = {
700         .open =        snd_emu10k1x_pcm_open_capture,
701         .close =       snd_emu10k1x_pcm_close_capture,
702         .ioctl =       snd_pcm_lib_ioctl,
703         .hw_params =   snd_emu10k1x_pcm_hw_params_capture,
704         .hw_free =     snd_emu10k1x_pcm_hw_free_capture,
705         .prepare =     snd_emu10k1x_pcm_prepare_capture,
706         .trigger =     snd_emu10k1x_pcm_trigger_capture,
707         .pointer =     snd_emu10k1x_pcm_pointer_capture,
708 };
709
710 static unsigned short snd_emu10k1x_ac97_read(ac97_t *ac97,
711                                              unsigned short reg)
712 {
713         emu10k1x_t *emu = ac97->private_data;
714         unsigned long flags;
715         unsigned short val;
716   
717         spin_lock_irqsave(&emu->emu_lock, flags);
718         outb(reg, emu->port + AC97ADDRESS);
719         val = inw(emu->port + AC97DATA);
720         spin_unlock_irqrestore(&emu->emu_lock, flags);
721         return val;
722 }
723
724 static void snd_emu10k1x_ac97_write(ac97_t *ac97,
725                                     unsigned short reg, unsigned short val)
726 {
727         emu10k1x_t *emu = ac97->private_data;
728         unsigned long flags;
729   
730         spin_lock_irqsave(&emu->emu_lock, flags);
731         outb(reg, emu->port + AC97ADDRESS);
732         outw(val, emu->port + AC97DATA);
733         spin_unlock_irqrestore(&emu->emu_lock, flags);
734 }
735
736 static int snd_emu10k1x_ac97(emu10k1x_t *chip)
737 {
738         ac97_bus_t *pbus;
739         ac97_template_t ac97;
740         int err;
741         static ac97_bus_ops_t ops = {
742                 .write = snd_emu10k1x_ac97_write,
743                 .read = snd_emu10k1x_ac97_read,
744         };
745   
746         if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
747                 return err;
748         pbus->no_vra = 1; /* we don't need VRA */
749
750         memset(&ac97, 0, sizeof(ac97));
751         ac97.private_data = chip;
752         ac97.scaps = AC97_SCAP_NO_SPDIF;
753         return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
754 }
755
756 static int snd_emu10k1x_free(emu10k1x_t *chip)
757 {
758         snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
759         // disable interrupts
760         outl(0, chip->port + INTE);
761         // disable audio
762         outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
763
764         // release the i/o port
765         if (chip->res_port) {
766                 release_resource(chip->res_port);
767                 kfree_nocheck(chip->res_port);
768         }
769         // release the irq
770         if (chip->irq >= 0)
771                 free_irq(chip->irq, (void *)chip);
772
773         // release the DMA
774         if (chip->dma_buffer.area) {
775                 snd_dma_free_pages(&chip->dma_buffer);
776         }
777
778         pci_disable_device(chip->pci);
779
780         // release the data
781         kfree(chip);
782         return 0;
783 }
784
785 static int snd_emu10k1x_dev_free(snd_device_t *device)
786 {
787         emu10k1x_t *chip = device->device_data;
788         return snd_emu10k1x_free(chip);
789 }
790
791 static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id,
792                                           struct pt_regs *regs)
793 {
794         unsigned int status;
795
796         emu10k1x_t *chip = dev_id;
797         emu10k1x_voice_t *pvoice = chip->voices;
798         int i;
799         int mask;
800
801         status = inl(chip->port + IPR);
802
803         if(status) {
804                 // capture interrupt
805                 if(status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
806                         emu10k1x_voice_t *pvoice = &chip->capture_voice;
807                         if(pvoice->use)
808                                 snd_emu10k1x_pcm_interrupt(chip, pvoice);
809                         else
810                                 snd_emu10k1x_intr_disable(chip, 
811                                                           INTE_CAP_0_LOOP |
812                                                           INTE_CAP_0_HALF_LOOP);
813                 }
814                 
815                 mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
816                 for(i = 0; i < 3; i++) {
817                         if(status & mask) {
818                                 if(pvoice->use)
819                                         snd_emu10k1x_pcm_interrupt(chip, pvoice);
820                                 else 
821                                         snd_emu10k1x_intr_disable(chip, mask);
822                         }
823                         pvoice++;
824                         mask <<= 1;
825                 }
826                 
827                 if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
828                         if (chip->midi.interrupt)
829                                 chip->midi.interrupt(chip, status);
830                         else
831                                 snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
832                 }
833                 
834                 // acknowledge the interrupt if necessary
835                 if(status)
836                         outl(status, chip->port+IPR);
837
838 //              snd_printk(KERN_INFO "interrupt %08x\n", status);
839         }
840
841         return IRQ_HANDLED;
842 }
843
844 static void snd_emu10k1x_pcm_free(snd_pcm_t *pcm)
845 {
846         emu10k1x_t *emu = pcm->private_data;
847         emu->pcm = NULL;
848         snd_pcm_lib_preallocate_free_for_all(pcm);
849 }
850
851 static int __devinit snd_emu10k1x_pcm(emu10k1x_t *emu, int device, snd_pcm_t **rpcm)
852 {
853         snd_pcm_t *pcm;
854         int err;
855         int capture = 0;
856   
857         if (rpcm)
858                 *rpcm = NULL;
859         if (device == 0)
860                 capture = 1;
861         
862         if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
863                 return err;
864   
865         pcm->private_data = emu;
866         pcm->private_free = snd_emu10k1x_pcm_free;
867         
868         switch(device) {
869         case 0:
870                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
871                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
872                 break;
873         case 1:
874         case 2:
875                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
876                 break;
877         }
878
879         pcm->info_flags = 0;
880         pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
881         switch(device) {
882         case 0:
883                 strcpy(pcm->name, "EMU10K1X Front");
884                 break;
885         case 1:
886                 strcpy(pcm->name, "EMU10K1X Rear");
887                 break;
888         case 2:
889                 strcpy(pcm->name, "EMU10K1X Center/LFE");
890                 break;
891         }
892         emu->pcm = pcm;
893
894         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
895                                               snd_dma_pci_data(emu->pci), 
896                                               32*1024, 32*1024);
897   
898         if (rpcm)
899                 *rpcm = pcm;
900   
901         return 0;
902 }
903
904 static int __devinit snd_emu10k1x_create(snd_card_t *card,
905                                          struct pci_dev *pci,
906                                          emu10k1x_t **rchip)
907 {
908         emu10k1x_t *chip;
909         int err;
910         int ch;
911         static snd_device_ops_t ops = {
912                 .dev_free = snd_emu10k1x_dev_free,
913         };
914   
915         *rchip = NULL;
916   
917         if ((err = pci_enable_device(pci)) < 0)
918                 return err;
919         if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
920             pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
921                 snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
922                 pci_disable_device(pci);
923                 return -ENXIO;
924         }
925   
926         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
927         if (chip == NULL) {
928                 pci_disable_device(pci);
929                 return -ENOMEM;
930         }
931   
932         chip->card = card;
933         chip->pci = pci;
934         chip->irq = -1;
935
936         spin_lock_init(&chip->emu_lock);
937         spin_lock_init(&chip->voice_lock);
938   
939         chip->port = pci_resource_start(pci, 0);
940         if ((chip->res_port = request_region(chip->port, 8,
941                                              "EMU10K1X")) == NULL) { 
942                 snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
943                 snd_emu10k1x_free(chip);
944                 return -EBUSY;
945         }
946
947         if (request_irq(pci->irq, snd_emu10k1x_interrupt,
948                         SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
949                         (void *)chip)) {
950                 snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
951                 snd_emu10k1x_free(chip);
952                 return -EBUSY;
953         }
954         chip->irq = pci->irq;
955   
956         if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
957                                4 * 1024, &chip->dma_buffer) < 0) {
958                 snd_emu10k1x_free(chip);
959                 return -ENOMEM;
960         }
961
962         pci_set_master(pci);
963         /* read revision & serial */
964         pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
965         pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
966         pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
967         snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
968                    chip->revision, chip->serial);
969
970         outl(0, chip->port + INTE);     
971
972         for(ch = 0; ch < 3; ch++) {
973                 chip->voices[ch].emu = chip;
974                 chip->voices[ch].number = ch;
975         }
976
977         /*
978          *  Init to 0x02109204 :
979          *  Clock accuracy    = 0     (1000ppm)
980          *  Sample Rate       = 2     (48kHz)
981          *  Audio Channel     = 1     (Left of 2)
982          *  Source Number     = 0     (Unspecified)
983          *  Generation Status = 1     (Original for Cat Code 12)
984          *  Cat Code          = 12    (Digital Signal Mixer)
985          *  Mode              = 0     (Mode 0)
986          *  Emphasis          = 0     (None)
987          *  CP                = 1     (Copyright unasserted)
988          *  AN                = 0     (Audio data)
989          *  P                 = 0     (Consumer)
990          */
991         snd_emu10k1x_ptr_write(chip, SPCS0, 0,
992                                chip->spdif_bits[0] = 
993                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
994                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
995                                SPCS_GENERATIONSTATUS | 0x00001200 |
996                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
997         snd_emu10k1x_ptr_write(chip, SPCS1, 0,
998                                chip->spdif_bits[1] = 
999                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1000                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1001                                SPCS_GENERATIONSTATUS | 0x00001200 |
1002                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1003         snd_emu10k1x_ptr_write(chip, SPCS2, 0,
1004                                chip->spdif_bits[2] = 
1005                                SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1006                                SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1007                                SPCS_GENERATIONSTATUS | 0x00001200 |
1008                                0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1009
1010         snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
1011         snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
1012         snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
1013
1014         outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1015
1016         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1017                                   chip, &ops)) < 0) {
1018                 snd_emu10k1x_free(chip);
1019                 return err;
1020         }
1021         *rchip = chip;
1022         return 0;
1023 }
1024
1025 static void snd_emu10k1x_proc_reg_read(snd_info_entry_t *entry, 
1026                                        snd_info_buffer_t * buffer)
1027 {
1028         emu10k1x_t *emu = entry->private_data;
1029         unsigned long value,value1,value2;
1030         unsigned long flags;
1031         int i;
1032
1033         snd_iprintf(buffer, "Registers:\n\n");
1034         for(i = 0; i < 0x20; i+=4) {
1035                 spin_lock_irqsave(&emu->emu_lock, flags);
1036                 value = inl(emu->port + i);
1037                 spin_unlock_irqrestore(&emu->emu_lock, flags);
1038                 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
1039         }
1040         snd_iprintf(buffer, "\nRegisters\n\n");
1041         for(i = 0; i <= 0x48; i++) {
1042                 value = snd_emu10k1x_ptr_read(emu, i, 0);
1043                 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
1044                         value1 = snd_emu10k1x_ptr_read(emu, i, 1);
1045                         value2 = snd_emu10k1x_ptr_read(emu, i, 2);
1046                         snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
1047                 } else {
1048                         snd_iprintf(buffer, "%02X: %08lX\n", i, value);
1049                 }
1050         }
1051 }
1052
1053 static void snd_emu10k1x_proc_reg_write(snd_info_entry_t *entry, 
1054                                         snd_info_buffer_t *buffer)
1055 {
1056         emu10k1x_t *emu = entry->private_data;
1057         char line[64];
1058         unsigned int reg, channel_id , val;
1059
1060         while (!snd_info_get_line(buffer, line, sizeof(line))) {
1061                 if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
1062                         continue;
1063
1064                 if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff) 
1065                     && (channel_id >=0) && (channel_id <= 2) )
1066                         snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1067         }
1068 }
1069
1070 static int __devinit snd_emu10k1x_proc_init(emu10k1x_t * emu)
1071 {
1072         snd_info_entry_t *entry;
1073         
1074         if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
1075                 snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
1076                 entry->c.text.write_size = 64;
1077                 entry->c.text.write = snd_emu10k1x_proc_reg_write;
1078                 entry->mode |= S_IWUSR;
1079                 entry->private_data = emu;
1080         }
1081         
1082         return 0;
1083 }
1084
1085 static int snd_emu10k1x_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1086 {
1087         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1088         uinfo->count = 1;
1089         uinfo->value.integer.min = 0;
1090         uinfo->value.integer.max = 1;
1091         return 0;
1092 }
1093
1094 static int snd_emu10k1x_shared_spdif_get(snd_kcontrol_t * kcontrol,
1095                                          snd_ctl_elem_value_t * ucontrol)
1096 {
1097         emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1098
1099         ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1100
1101         return 0;
1102 }
1103
1104 static int snd_emu10k1x_shared_spdif_put(snd_kcontrol_t * kcontrol,
1105                                          snd_ctl_elem_value_t * ucontrol)
1106 {
1107         emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1108         unsigned int val;
1109         int change = 0;
1110
1111         val = ucontrol->value.integer.value[0] ;
1112
1113         if (val) {
1114                 // enable spdif output
1115                 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1116                 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1117                 snd_emu10k1x_gpio_write(emu, 0x1000);
1118         } else {
1119                 // disable spdif output
1120                 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1121                 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1122                 snd_emu10k1x_gpio_write(emu, 0x1080);
1123         }
1124         return change;
1125 }
1126
1127 static snd_kcontrol_new_t snd_emu10k1x_shared_spdif __devinitdata =
1128 {
1129         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1130         .name =         "Analog/Digital Output Jack",
1131         .info =         snd_emu10k1x_shared_spdif_info,
1132         .get =          snd_emu10k1x_shared_spdif_get,
1133         .put =          snd_emu10k1x_shared_spdif_put
1134 };
1135
1136 static int snd_emu10k1x_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1137 {
1138         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1139         uinfo->count = 1;
1140         return 0;
1141 }
1142
1143 static int snd_emu10k1x_spdif_get(snd_kcontrol_t * kcontrol,
1144                                   snd_ctl_elem_value_t * ucontrol)
1145 {
1146         emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1147         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1148
1149         ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1150         ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1151         ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1152         ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1153         return 0;
1154 }
1155
1156 static int snd_emu10k1x_spdif_get_mask(snd_kcontrol_t * kcontrol,
1157                                        snd_ctl_elem_value_t * ucontrol)
1158 {
1159         ucontrol->value.iec958.status[0] = 0xff;
1160         ucontrol->value.iec958.status[1] = 0xff;
1161         ucontrol->value.iec958.status[2] = 0xff;
1162         ucontrol->value.iec958.status[3] = 0xff;
1163         return 0;
1164 }
1165
1166 static int snd_emu10k1x_spdif_put(snd_kcontrol_t * kcontrol,
1167                                   snd_ctl_elem_value_t * ucontrol)
1168 {
1169         emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1170         unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1171         int change;
1172         unsigned int val;
1173
1174         val = (ucontrol->value.iec958.status[0] << 0) |
1175                 (ucontrol->value.iec958.status[1] << 8) |
1176                 (ucontrol->value.iec958.status[2] << 16) |
1177                 (ucontrol->value.iec958.status[3] << 24);
1178         change = val != emu->spdif_bits[idx];
1179         if (change) {
1180                 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1181                 emu->spdif_bits[idx] = val;
1182         }
1183         return change;
1184 }
1185
1186 static snd_kcontrol_new_t snd_emu10k1x_spdif_mask_control =
1187 {
1188         .access =       SNDRV_CTL_ELEM_ACCESS_READ,
1189         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1190         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1191         .count =        3,
1192         .info =         snd_emu10k1x_spdif_info,
1193         .get =          snd_emu10k1x_spdif_get_mask
1194 };
1195
1196 static snd_kcontrol_new_t snd_emu10k1x_spdif_control =
1197 {
1198         .iface =        SNDRV_CTL_ELEM_IFACE_MIXER,
1199         .name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1200         .count =        3,
1201         .info =         snd_emu10k1x_spdif_info,
1202         .get =          snd_emu10k1x_spdif_get,
1203         .put =          snd_emu10k1x_spdif_put
1204 };
1205
1206 static int __devinit snd_emu10k1x_mixer(emu10k1x_t *emu)
1207 {
1208         int err;
1209         snd_kcontrol_t *kctl;
1210         snd_card_t *card = emu->card;
1211
1212         if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
1213                 return -ENOMEM;
1214         if ((err = snd_ctl_add(card, kctl)))
1215                 return err;
1216         if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
1217                 return -ENOMEM;
1218         if ((err = snd_ctl_add(card, kctl)))
1219                 return err;
1220         if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
1221                 return -ENOMEM;
1222         if ((err = snd_ctl_add(card, kctl)))
1223                 return err;
1224
1225         return 0;
1226 }
1227
1228 #define EMU10K1X_MIDI_MODE_INPUT        (1<<0)
1229 #define EMU10K1X_MIDI_MODE_OUTPUT       (1<<1)
1230
1231 static inline unsigned char mpu401_read(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int idx)
1232 {
1233         return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1234 }
1235
1236 static inline void mpu401_write(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int data, int idx)
1237 {
1238         snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1239 }
1240
1241 #define mpu401_write_data(emu, mpu, data)       mpu401_write(emu, mpu, data, 0)
1242 #define mpu401_write_cmd(emu, mpu, data)        mpu401_write(emu, mpu, data, 1)
1243 #define mpu401_read_data(emu, mpu)              mpu401_read(emu, mpu, 0)
1244 #define mpu401_read_stat(emu, mpu)              mpu401_read(emu, mpu, 1)
1245
1246 #define mpu401_input_avail(emu,mpu)     (!(mpu401_read_stat(emu,mpu) & 0x80))
1247 #define mpu401_output_ready(emu,mpu)    (!(mpu401_read_stat(emu,mpu) & 0x40))
1248
1249 #define MPU401_RESET            0xff
1250 #define MPU401_ENTER_UART       0x3f
1251 #define MPU401_ACK              0xfe
1252
1253 static void mpu401_clear_rx(emu10k1x_t *emu, emu10k1x_midi_t *mpu)
1254 {
1255         int timeout = 100000;
1256         for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1257                 mpu401_read_data(emu, mpu);
1258 #ifdef CONFIG_SND_DEBUG
1259         if (timeout <= 0)
1260                 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
1261 #endif
1262 }
1263
1264 /*
1265
1266  */
1267
1268 static void do_emu10k1x_midi_interrupt(emu10k1x_t *emu, emu10k1x_midi_t *midi, unsigned int status)
1269 {
1270         unsigned char byte;
1271
1272         if (midi->rmidi == NULL) {
1273                 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1274                 return;
1275         }
1276
1277         spin_lock(&midi->input_lock);
1278         if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1279                 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1280                         mpu401_clear_rx(emu, midi);
1281                 } else {
1282                         byte = mpu401_read_data(emu, midi);
1283                         if (midi->substream_input)
1284                                 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1285                 }
1286         }
1287         spin_unlock(&midi->input_lock);
1288
1289         spin_lock(&midi->output_lock);
1290         if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1291                 if (midi->substream_output &&
1292                     snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1293                         mpu401_write_data(emu, midi, byte);
1294                 } else {
1295                         snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1296                 }
1297         }
1298         spin_unlock(&midi->output_lock);
1299 }
1300
1301 static void snd_emu10k1x_midi_interrupt(emu10k1x_t *emu, unsigned int status)
1302 {
1303         do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1304 }
1305
1306 static void snd_emu10k1x_midi_cmd(emu10k1x_t * emu, emu10k1x_midi_t *midi, unsigned char cmd, int ack)
1307 {
1308         unsigned long flags;
1309         int timeout, ok;
1310
1311         spin_lock_irqsave(&midi->input_lock, flags);
1312         mpu401_write_data(emu, midi, 0x00);
1313         /* mpu401_clear_rx(emu, midi); */
1314
1315         mpu401_write_cmd(emu, midi, cmd);
1316         if (ack) {
1317                 ok = 0;
1318                 timeout = 10000;
1319                 while (!ok && timeout-- > 0) {
1320                         if (mpu401_input_avail(emu, midi)) {
1321                                 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1322                                         ok = 1;
1323                         }
1324                 }
1325                 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1326                         ok = 1;
1327         } else {
1328                 ok = 1;
1329         }
1330         spin_unlock_irqrestore(&midi->input_lock, flags);
1331         if (!ok)
1332                 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1333                            cmd, emu->port,
1334                            mpu401_read_stat(emu, midi),
1335                            mpu401_read_data(emu, midi));
1336 }
1337
1338 static int snd_emu10k1x_midi_input_open(snd_rawmidi_substream_t * substream)
1339 {
1340         emu10k1x_t *emu;
1341         emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1342         unsigned long flags;
1343         
1344         emu = midi->emu;
1345         snd_assert(emu, return -ENXIO);
1346         spin_lock_irqsave(&midi->open_lock, flags);
1347         midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1348         midi->substream_input = substream;
1349         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1350                 spin_unlock_irqrestore(&midi->open_lock, flags);
1351                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1352                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1353         } else {
1354                 spin_unlock_irqrestore(&midi->open_lock, flags);
1355         }
1356         return 0;
1357 }
1358
1359 static int snd_emu10k1x_midi_output_open(snd_rawmidi_substream_t * substream)
1360 {
1361         emu10k1x_t *emu;
1362         emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1363         unsigned long flags;
1364
1365         emu = midi->emu;
1366         snd_assert(emu, return -ENXIO);
1367         spin_lock_irqsave(&midi->open_lock, flags);
1368         midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1369         midi->substream_output = substream;
1370         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1371                 spin_unlock_irqrestore(&midi->open_lock, flags);
1372                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1373                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1374         } else {
1375                 spin_unlock_irqrestore(&midi->open_lock, flags);
1376         }
1377         return 0;
1378 }
1379
1380 static int snd_emu10k1x_midi_input_close(snd_rawmidi_substream_t * substream)
1381 {
1382         emu10k1x_t *emu;
1383         emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1384         unsigned long flags;
1385
1386         emu = midi->emu;
1387         snd_assert(emu, return -ENXIO);
1388         spin_lock_irqsave(&midi->open_lock, flags);
1389         snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1390         midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1391         midi->substream_input = NULL;
1392         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1393                 spin_unlock_irqrestore(&midi->open_lock, flags);
1394                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1395         } else {
1396                 spin_unlock_irqrestore(&midi->open_lock, flags);
1397         }
1398         return 0;
1399 }
1400
1401 static int snd_emu10k1x_midi_output_close(snd_rawmidi_substream_t * substream)
1402 {
1403         emu10k1x_t *emu;
1404         emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1405         unsigned long flags;
1406
1407         emu = midi->emu;
1408         snd_assert(emu, return -ENXIO);
1409         spin_lock_irqsave(&midi->open_lock, flags);
1410         snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1411         midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1412         midi->substream_output = NULL;
1413         if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1414                 spin_unlock_irqrestore(&midi->open_lock, flags);
1415                 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1416         } else {
1417                 spin_unlock_irqrestore(&midi->open_lock, flags);
1418         }
1419         return 0;
1420 }
1421
1422 static void snd_emu10k1x_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
1423 {
1424         emu10k1x_t *emu;
1425         emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1426         emu = midi->emu;
1427         snd_assert(emu, return);
1428
1429         if (up)
1430                 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1431         else
1432                 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1433 }
1434
1435 static void snd_emu10k1x_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
1436 {
1437         emu10k1x_t *emu;
1438         emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1439         unsigned long flags;
1440
1441         emu = midi->emu;
1442         snd_assert(emu, return);
1443
1444         if (up) {
1445                 int max = 4;
1446                 unsigned char byte;
1447         
1448                 /* try to send some amount of bytes here before interrupts */
1449                 spin_lock_irqsave(&midi->output_lock, flags);
1450                 while (max > 0) {
1451                         if (mpu401_output_ready(emu, midi)) {
1452                                 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1453                                     snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1454                                         /* no more data */
1455                                         spin_unlock_irqrestore(&midi->output_lock, flags);
1456                                         return;
1457                                 }
1458                                 mpu401_write_data(emu, midi, byte);
1459                                 max--;
1460                         } else {
1461                                 break;
1462                         }
1463                 }
1464                 spin_unlock_irqrestore(&midi->output_lock, flags);
1465                 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1466         } else {
1467                 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1468         }
1469 }
1470
1471 /*
1472
1473  */
1474
1475 static snd_rawmidi_ops_t snd_emu10k1x_midi_output =
1476 {
1477         .open =         snd_emu10k1x_midi_output_open,
1478         .close =        snd_emu10k1x_midi_output_close,
1479         .trigger =      snd_emu10k1x_midi_output_trigger,
1480 };
1481
1482 static snd_rawmidi_ops_t snd_emu10k1x_midi_input =
1483 {
1484         .open =         snd_emu10k1x_midi_input_open,
1485         .close =        snd_emu10k1x_midi_input_close,
1486         .trigger =      snd_emu10k1x_midi_input_trigger,
1487 };
1488
1489 static void snd_emu10k1x_midi_free(snd_rawmidi_t *rmidi)
1490 {
1491         emu10k1x_midi_t *midi = (emu10k1x_midi_t *)rmidi->private_data;
1492         midi->interrupt = NULL;
1493         midi->rmidi = NULL;
1494 }
1495
1496 static int __devinit emu10k1x_midi_init(emu10k1x_t *emu, emu10k1x_midi_t *midi, int device, char *name)
1497 {
1498         snd_rawmidi_t *rmidi;
1499         int err;
1500
1501         if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
1502                 return err;
1503         midi->emu = emu;
1504         spin_lock_init(&midi->open_lock);
1505         spin_lock_init(&midi->input_lock);
1506         spin_lock_init(&midi->output_lock);
1507         strcpy(rmidi->name, name);
1508         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1509         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1510         rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1511                              SNDRV_RAWMIDI_INFO_INPUT |
1512                              SNDRV_RAWMIDI_INFO_DUPLEX;
1513         rmidi->private_data = midi;
1514         rmidi->private_free = snd_emu10k1x_midi_free;
1515         midi->rmidi = rmidi;
1516         return 0;
1517 }
1518
1519 static int __devinit snd_emu10k1x_midi(emu10k1x_t *emu)
1520 {
1521         emu10k1x_midi_t *midi = &emu->midi;
1522         int err;
1523
1524         if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1525                 return err;
1526
1527         midi->tx_enable = INTE_MIDITXENABLE;
1528         midi->rx_enable = INTE_MIDIRXENABLE;
1529         midi->port = MUDATA;
1530         midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1531         midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1532         midi->interrupt = snd_emu10k1x_midi_interrupt;
1533         return 0;
1534 }
1535
1536 static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
1537                                         const struct pci_device_id *pci_id)
1538 {
1539         static int dev;
1540         snd_card_t *card;
1541         emu10k1x_t *chip;
1542         int err;
1543
1544         if (dev >= SNDRV_CARDS)
1545                 return -ENODEV;
1546         if (!enable[dev]) {
1547                 dev++;
1548                 return -ENOENT;
1549         }
1550
1551         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1552         if (card == NULL)
1553                 return -ENOMEM;
1554
1555         if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
1556                 snd_card_free(card);
1557                 return err;
1558         }
1559
1560         if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
1561                 snd_card_free(card);
1562                 return err;
1563         }
1564         if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
1565                 snd_card_free(card);
1566                 return err;
1567         }
1568         if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
1569                 snd_card_free(card);
1570                 return err;
1571         }
1572
1573         if ((err = snd_emu10k1x_ac97(chip)) < 0) {
1574                 snd_card_free(card);
1575                 return err;
1576         }
1577
1578         if ((err = snd_emu10k1x_mixer(chip)) < 0) {
1579                 snd_card_free(card);
1580                 return err;
1581         }
1582         
1583         if ((err = snd_emu10k1x_midi(chip)) < 0) {
1584                 snd_card_free(card);
1585                 return err;
1586         }
1587
1588         snd_emu10k1x_proc_init(chip);
1589
1590         strcpy(card->driver, "EMU10K1X");
1591         strcpy(card->shortname, "Dell Sound Blaster Live!");
1592         sprintf(card->longname, "%s at 0x%lx irq %i",
1593                 card->shortname, chip->port, chip->irq);
1594
1595         if ((err = snd_card_register(card)) < 0) {
1596                 snd_card_free(card);
1597                 return err;
1598         }
1599
1600         pci_set_drvdata(pci, card);
1601         dev++;
1602         return 0;
1603 }
1604
1605 static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
1606 {
1607         snd_card_free(pci_get_drvdata(pci));
1608         pci_set_drvdata(pci, NULL);
1609 }
1610
1611 // PCI IDs
1612 static struct pci_device_id snd_emu10k1x_ids[] = {
1613         { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },    /* Dell OEM version (EMU10K1) */
1614         { 0, }
1615 };
1616 MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1617
1618 // pci_driver definition
1619 static struct pci_driver driver = {
1620         .name = "EMU10K1X",
1621         .id_table = snd_emu10k1x_ids,
1622         .probe = snd_emu10k1x_probe,
1623         .remove = __devexit_p(snd_emu10k1x_remove),
1624 };
1625
1626 // initialization of the module
1627 static int __init alsa_card_emu10k1x_init(void)
1628 {
1629         int err;
1630
1631         if ((err = pci_register_driver(&driver)) > 0)
1632                 return err;
1633
1634         return 0;
1635 }
1636
1637 // clean up the module
1638 static void __exit alsa_card_emu10k1x_exit(void)
1639 {
1640         pci_unregister_driver(&driver);
1641 }
1642
1643 module_init(alsa_card_emu10k1x_init)
1644 module_exit(alsa_card_emu10k1x_exit)