2 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
3 * Driver EMU10K1X chips
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>
13 * Chips (SB0200 model):
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.
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.
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
32 #include <sound/driver.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
35 #include <linux/pci.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/slab.h>
38 #include <linux/moduleparam.h>
39 #include <linux/dma-mapping.h>
40 #include <sound/core.h>
41 #include <sound/initval.h>
42 #include <sound/pcm.h>
43 #include <sound/ac97_codec.h>
44 #include <sound/info.h>
45 #include <sound/rawmidi.h>
47 MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
48 MODULE_DESCRIPTION("EMU10K1X");
49 MODULE_LICENSE("GPL");
50 MODULE_SUPPORTED_DEVICE("{{Dell Creative Labs,SB Live!}");
52 // module parameters (see "Module Parameters")
53 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
54 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
55 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
57 module_param_array(index, int, NULL, 0444);
58 MODULE_PARM_DESC(index, "Index value for the EMU10K1X soundcard.");
59 module_param_array(id, charp, NULL, 0444);
60 MODULE_PARM_DESC(id, "ID string for the EMU10K1X soundcard.");
61 module_param_array(enable, bool, NULL, 0444);
62 MODULE_PARM_DESC(enable, "Enable the EMU10K1X soundcard.");
65 // some definitions were borrowed from emu10k1 driver as they seem to be the same
66 /************************************************************************************************/
67 /* PCI function 0 registers, address = <val> + PCIBASE0 */
68 /************************************************************************************************/
70 #define PTR 0x00 /* Indexed register set pointer register */
71 /* NOTE: The CHANNELNUM and ADDRESS words can */
72 /* be modified independently of each other. */
74 #define DATA 0x04 /* Indexed register set data register */
76 #define IPR 0x08 /* Global interrupt pending register */
77 /* Clear pending interrupts by writing a 1 to */
78 /* the relevant bits and zero to the other bits */
79 #define IPR_MIDITRANSBUFEMPTY 0x00000001 /* MIDI UART transmit buffer empty */
80 #define IPR_MIDIRECVBUFEMPTY 0x00000002 /* MIDI UART receive buffer empty */
81 #define IPR_CH_0_LOOP 0x00000800 /* Channel 0 loop */
82 #define IPR_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
83 #define IPR_CAP_0_LOOP 0x00080000 /* Channel capture loop */
84 #define IPR_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
86 #define INTE 0x0c /* Interrupt enable register */
87 #define INTE_MIDITXENABLE 0x00000001 /* Enable MIDI transmit-buffer-empty interrupts */
88 #define INTE_MIDIRXENABLE 0x00000002 /* Enable MIDI receive-buffer-empty interrupts */
89 #define INTE_CH_0_LOOP 0x00000800 /* Channel 0 loop */
90 #define INTE_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
91 #define INTE_CAP_0_LOOP 0x00080000 /* Channel capture loop */
92 #define INTE_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
94 #define HCFG 0x14 /* Hardware config register */
96 #define HCFG_LOCKSOUNDCACHE 0x00000008 /* 1 = Cancel bustmaster accesses to soundcache */
97 /* NOTE: This should generally never be used. */
98 #define HCFG_AUDIOENABLE 0x00000001 /* 0 = CODECs transmit zero-valued samples */
99 /* Should be set to 1 when the EMU10K1 is */
100 /* completely initialized. */
101 #define GPIO 0x18 /* Defaults: 00001080-Analog, 00001000-SPDIF. */
104 #define AC97DATA 0x1c /* AC97 register set data register (16 bit) */
106 #define AC97ADDRESS 0x1e /* AC97 register set address register (8 bit) */
108 /********************************************************************************************************/
109 /* Emu10k1x pointer-offset register set, accessed through the PTR and DATA registers */
110 /********************************************************************************************************/
111 #define PLAYBACK_LIST_ADDR 0x00 /* Base DMA address of a list of pointers to each period/size */
112 /* One list entry: 4 bytes for DMA address,
113 * 4 bytes for period_size << 16.
114 * One list entry is 8 bytes long.
115 * One list entry for each period in the buffer.
117 #define PLAYBACK_LIST_SIZE 0x01 /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000 */
118 #define PLAYBACK_LIST_PTR 0x02 /* Pointer to the current period being played */
119 #define PLAYBACK_DMA_ADDR 0x04 /* Playback DMA addresss */
120 #define PLAYBACK_PERIOD_SIZE 0x05 /* Playback period size */
121 #define PLAYBACK_POINTER 0x06 /* Playback period pointer. Sample currently in DAC */
122 #define PLAYBACK_UNKNOWN1 0x07
123 #define PLAYBACK_UNKNOWN2 0x08
125 /* Only one capture channel supported */
126 #define CAPTURE_DMA_ADDR 0x10 /* Capture DMA address */
127 #define CAPTURE_BUFFER_SIZE 0x11 /* Capture buffer size */
128 #define CAPTURE_POINTER 0x12 /* Capture buffer pointer. Sample currently in ADC */
129 #define CAPTURE_UNKNOWN 0x13
131 /* From 0x20 - 0x3f, last samples played on each channel */
133 #define TRIGGER_CHANNEL 0x40 /* Trigger channel playback */
134 #define TRIGGER_CHANNEL_0 0x00000001 /* Trigger channel 0 */
135 #define TRIGGER_CHANNEL_1 0x00000002 /* Trigger channel 1 */
136 #define TRIGGER_CHANNEL_2 0x00000004 /* Trigger channel 2 */
137 #define TRIGGER_CAPTURE 0x00000100 /* Trigger capture channel */
139 #define ROUTING 0x41 /* Setup sound routing ? */
140 #define ROUTING_FRONT_LEFT 0x00000001
141 #define ROUTING_FRONT_RIGHT 0x00000002
142 #define ROUTING_REAR_LEFT 0x00000004
143 #define ROUTING_REAR_RIGHT 0x00000008
144 #define ROUTING_CENTER_LFE 0x00010000
146 #define SPCS0 0x42 /* SPDIF output Channel Status 0 register */
148 #define SPCS1 0x43 /* SPDIF output Channel Status 1 register */
150 #define SPCS2 0x44 /* SPDIF output Channel Status 2 register */
152 #define SPCS_CLKACCYMASK 0x30000000 /* Clock accuracy */
153 #define SPCS_CLKACCY_1000PPM 0x00000000 /* 1000 parts per million */
154 #define SPCS_CLKACCY_50PPM 0x10000000 /* 50 parts per million */
155 #define SPCS_CLKACCY_VARIABLE 0x20000000 /* Variable accuracy */
156 #define SPCS_SAMPLERATEMASK 0x0f000000 /* Sample rate */
157 #define SPCS_SAMPLERATE_44 0x00000000 /* 44.1kHz sample rate */
158 #define SPCS_SAMPLERATE_48 0x02000000 /* 48kHz sample rate */
159 #define SPCS_SAMPLERATE_32 0x03000000 /* 32kHz sample rate */
160 #define SPCS_CHANNELNUMMASK 0x00f00000 /* Channel number */
161 #define SPCS_CHANNELNUM_UNSPEC 0x00000000 /* Unspecified channel number */
162 #define SPCS_CHANNELNUM_LEFT 0x00100000 /* Left channel */
163 #define SPCS_CHANNELNUM_RIGHT 0x00200000 /* Right channel */
164 #define SPCS_SOURCENUMMASK 0x000f0000 /* Source number */
165 #define SPCS_SOURCENUM_UNSPEC 0x00000000 /* Unspecified source number */
166 #define SPCS_GENERATIONSTATUS 0x00008000 /* Originality flag (see IEC-958 spec) */
167 #define SPCS_CATEGORYCODEMASK 0x00007f00 /* Category code (see IEC-958 spec) */
168 #define SPCS_MODEMASK 0x000000c0 /* Mode (see IEC-958 spec) */
169 #define SPCS_EMPHASISMASK 0x00000038 /* Emphasis */
170 #define SPCS_EMPHASIS_NONE 0x00000000 /* No emphasis */
171 #define SPCS_EMPHASIS_50_15 0x00000008 /* 50/15 usec 2 channel */
172 #define SPCS_COPYRIGHT 0x00000004 /* Copyright asserted flag -- do not modify */
173 #define SPCS_NOTAUDIODATA 0x00000002 /* 0 = Digital audio, 1 = not audio */
174 #define SPCS_PROFESSIONAL 0x00000001 /* 0 = Consumer (IEC-958), 1 = pro (AES3-1992) */
176 #define SPDIF_SELECT 0x45 /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
178 /* This is the MPU port on the card */
183 /* From 0x50 - 0x5f, last samples captured */
186 * The hardware has 3 channels for playback and 1 for capture.
187 * - channel 0 is the front channel
188 * - channel 1 is the rear channel
189 * - channel 2 is the center/lfe chanel
190 * Volume is controlled by the AC97 for the front and rear channels by
191 * the PCM Playback Volume, Sigmatel Surround Playback Volume and
192 * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
193 * the front/rear channel mixing in the REAR OUT jack. When using the
194 * 4-Speaker Stereo, both front and rear channels will be mixed in the
196 * The center/lfe channel has no volume control and cannot be muted during
200 struct emu10k1x_voice {
201 struct emu10k1x *emu;
205 struct emu10k1x_pcm *epcm;
208 struct emu10k1x_pcm {
209 struct emu10k1x *emu;
210 struct snd_pcm_substream *substream;
211 struct emu10k1x_voice *voice;
212 unsigned short running;
215 struct emu10k1x_midi {
216 struct emu10k1x *emu;
217 struct snd_rawmidi *rmidi;
218 struct snd_rawmidi_substream *substream_input;
219 struct snd_rawmidi_substream *substream_output;
220 unsigned int midi_mode;
221 spinlock_t input_lock;
222 spinlock_t output_lock;
223 spinlock_t open_lock;
224 int tx_enable, rx_enable;
227 void (*interrupt)(struct emu10k1x *emu, unsigned int status);
230 // definition of the chip-specific record
232 struct snd_card *card;
236 struct resource *res_port;
239 unsigned int revision; /* chip revision */
240 unsigned int serial; /* serial number */
241 unsigned short model; /* subsystem id */
244 spinlock_t voice_lock;
246 struct snd_ac97 *ac97;
249 struct emu10k1x_voice voices[3];
250 struct emu10k1x_voice capture_voice;
251 u32 spdif_bits[3]; // SPDIF out setup
253 struct snd_dma_buffer dma_buffer;
255 struct emu10k1x_midi midi;
258 /* hardware definition */
259 static struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
260 .info = (SNDRV_PCM_INFO_MMAP |
261 SNDRV_PCM_INFO_INTERLEAVED |
262 SNDRV_PCM_INFO_BLOCK_TRANSFER |
263 SNDRV_PCM_INFO_MMAP_VALID),
264 .formats = SNDRV_PCM_FMTBIT_S16_LE,
265 .rates = SNDRV_PCM_RATE_48000,
270 .buffer_bytes_max = (32*1024),
271 .period_bytes_min = 64,
272 .period_bytes_max = (16*1024),
278 static struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
279 .info = (SNDRV_PCM_INFO_MMAP |
280 SNDRV_PCM_INFO_INTERLEAVED |
281 SNDRV_PCM_INFO_BLOCK_TRANSFER |
282 SNDRV_PCM_INFO_MMAP_VALID),
283 .formats = SNDRV_PCM_FMTBIT_S16_LE,
284 .rates = SNDRV_PCM_RATE_48000,
289 .buffer_bytes_max = (32*1024),
290 .period_bytes_min = 64,
291 .period_bytes_max = (16*1024),
297 static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu,
302 unsigned int regptr, val;
304 regptr = (reg << 16) | chn;
306 spin_lock_irqsave(&emu->emu_lock, flags);
307 outl(regptr, emu->port + PTR);
308 val = inl(emu->port + DATA);
309 spin_unlock_irqrestore(&emu->emu_lock, flags);
313 static void snd_emu10k1x_ptr_write(struct emu10k1x *emu,
321 regptr = (reg << 16) | chn;
323 spin_lock_irqsave(&emu->emu_lock, flags);
324 outl(regptr, emu->port + PTR);
325 outl(data, emu->port + DATA);
326 spin_unlock_irqrestore(&emu->emu_lock, flags);
329 static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
334 spin_lock_irqsave(&emu->emu_lock, flags);
335 enable = inl(emu->port + INTE) | intrenb;
336 outl(enable, emu->port + INTE);
337 spin_unlock_irqrestore(&emu->emu_lock, flags);
340 static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
345 spin_lock_irqsave(&emu->emu_lock, flags);
346 enable = inl(emu->port + INTE) & ~intrenb;
347 outl(enable, emu->port + INTE);
348 spin_unlock_irqrestore(&emu->emu_lock, flags);
351 static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
355 spin_lock_irqsave(&emu->emu_lock, flags);
356 outl(value, emu->port + GPIO);
357 spin_unlock_irqrestore(&emu->emu_lock, flags);
360 static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
362 kfree(runtime->private_data);
365 static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
367 struct emu10k1x_pcm *epcm;
369 if ((epcm = voice->epcm) == NULL)
371 if (epcm->substream == NULL)
374 snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
375 epcm->substream->ops->pointer(epcm->substream),
376 snd_pcm_lib_period_bytes(epcm->substream),
377 snd_pcm_lib_buffer_bytes(epcm->substream));
379 snd_pcm_period_elapsed(epcm->substream);
383 static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
385 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
386 struct emu10k1x_pcm *epcm;
387 struct snd_pcm_runtime *runtime = substream->runtime;
390 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
393 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
396 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
400 epcm->substream = substream;
402 runtime->private_data = epcm;
403 runtime->private_free = snd_emu10k1x_pcm_free_substream;
405 runtime->hw = snd_emu10k1x_playback_hw;
411 static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
416 /* hw_params callback */
417 static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
418 struct snd_pcm_hw_params *hw_params)
420 struct snd_pcm_runtime *runtime = substream->runtime;
421 struct emu10k1x_pcm *epcm = runtime->private_data;
424 epcm->voice = &epcm->emu->voices[substream->pcm->device];
425 epcm->voice->use = 1;
426 epcm->voice->epcm = epcm;
429 return snd_pcm_lib_malloc_pages(substream,
430 params_buffer_bytes(hw_params));
433 /* hw_free callback */
434 static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
436 struct snd_pcm_runtime *runtime = substream->runtime;
437 struct emu10k1x_pcm *epcm;
439 if (runtime->private_data == NULL)
442 epcm = runtime->private_data;
445 epcm->voice->use = 0;
446 epcm->voice->epcm = NULL;
450 return snd_pcm_lib_free_pages(substream);
453 /* prepare callback */
454 static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
456 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
457 struct snd_pcm_runtime *runtime = substream->runtime;
458 struct emu10k1x_pcm *epcm = runtime->private_data;
459 int voice = epcm->voice->number;
460 u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
461 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
464 for(i=0; i < runtime->periods; i++) {
465 *table_base++=runtime->dma_addr+(i*period_size_bytes);
466 *table_base++=period_size_bytes<<16;
469 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
470 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
471 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
472 snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
473 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
474 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
475 snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
477 snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
482 /* trigger callback */
483 static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
486 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
487 struct snd_pcm_runtime *runtime = substream->runtime;
488 struct emu10k1x_pcm *epcm = runtime->private_data;
489 int channel = epcm->voice->number;
492 // snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
495 case SNDRV_PCM_TRIGGER_START:
496 if(runtime->periods == 2)
497 snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
499 snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
501 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
503 case SNDRV_PCM_TRIGGER_STOP:
505 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
506 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
515 /* pointer callback */
516 static snd_pcm_uframes_t
517 snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
519 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
520 struct snd_pcm_runtime *runtime = substream->runtime;
521 struct emu10k1x_pcm *epcm = runtime->private_data;
522 int channel = epcm->voice->number;
523 snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
528 ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
529 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
530 ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
532 if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
536 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
537 ptr2 = bytes_to_frames(runtime, ptr1);
538 ptr2 += (ptr4 >> 3) * runtime->period_size;
541 if (ptr >= runtime->buffer_size)
542 ptr -= runtime->buffer_size;
548 static struct snd_pcm_ops snd_emu10k1x_playback_ops = {
549 .open = snd_emu10k1x_playback_open,
550 .close = snd_emu10k1x_playback_close,
551 .ioctl = snd_pcm_lib_ioctl,
552 .hw_params = snd_emu10k1x_pcm_hw_params,
553 .hw_free = snd_emu10k1x_pcm_hw_free,
554 .prepare = snd_emu10k1x_pcm_prepare,
555 .trigger = snd_emu10k1x_pcm_trigger,
556 .pointer = snd_emu10k1x_pcm_pointer,
559 /* open_capture callback */
560 static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
562 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
563 struct emu10k1x_pcm *epcm;
564 struct snd_pcm_runtime *runtime = substream->runtime;
567 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
569 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
572 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
577 epcm->substream = substream;
579 runtime->private_data = epcm;
580 runtime->private_free = snd_emu10k1x_pcm_free_substream;
582 runtime->hw = snd_emu10k1x_capture_hw;
588 static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
593 /* hw_params callback */
594 static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
595 struct snd_pcm_hw_params *hw_params)
597 struct snd_pcm_runtime *runtime = substream->runtime;
598 struct emu10k1x_pcm *epcm = runtime->private_data;
601 if (epcm->emu->capture_voice.use)
603 epcm->voice = &epcm->emu->capture_voice;
604 epcm->voice->epcm = epcm;
605 epcm->voice->use = 1;
608 return snd_pcm_lib_malloc_pages(substream,
609 params_buffer_bytes(hw_params));
612 /* hw_free callback */
613 static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
615 struct snd_pcm_runtime *runtime = substream->runtime;
617 struct emu10k1x_pcm *epcm;
619 if (runtime->private_data == NULL)
621 epcm = runtime->private_data;
624 epcm->voice->use = 0;
625 epcm->voice->epcm = NULL;
629 return snd_pcm_lib_free_pages(substream);
632 /* prepare capture callback */
633 static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
635 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
636 struct snd_pcm_runtime *runtime = substream->runtime;
638 snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
639 snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
640 snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
641 snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
646 /* trigger_capture callback */
647 static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
650 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
651 struct snd_pcm_runtime *runtime = substream->runtime;
652 struct emu10k1x_pcm *epcm = runtime->private_data;
656 case SNDRV_PCM_TRIGGER_START:
657 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
658 INTE_CAP_0_HALF_LOOP);
659 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
662 case SNDRV_PCM_TRIGGER_STOP:
664 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
665 INTE_CAP_0_HALF_LOOP);
666 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
675 /* pointer_capture callback */
676 static snd_pcm_uframes_t
677 snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
679 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
680 struct snd_pcm_runtime *runtime = substream->runtime;
681 struct emu10k1x_pcm *epcm = runtime->private_data;
682 snd_pcm_uframes_t ptr;
687 ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
688 if (ptr >= runtime->buffer_size)
689 ptr -= runtime->buffer_size;
694 static struct snd_pcm_ops snd_emu10k1x_capture_ops = {
695 .open = snd_emu10k1x_pcm_open_capture,
696 .close = snd_emu10k1x_pcm_close_capture,
697 .ioctl = snd_pcm_lib_ioctl,
698 .hw_params = snd_emu10k1x_pcm_hw_params_capture,
699 .hw_free = snd_emu10k1x_pcm_hw_free_capture,
700 .prepare = snd_emu10k1x_pcm_prepare_capture,
701 .trigger = snd_emu10k1x_pcm_trigger_capture,
702 .pointer = snd_emu10k1x_pcm_pointer_capture,
705 static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
708 struct emu10k1x *emu = ac97->private_data;
712 spin_lock_irqsave(&emu->emu_lock, flags);
713 outb(reg, emu->port + AC97ADDRESS);
714 val = inw(emu->port + AC97DATA);
715 spin_unlock_irqrestore(&emu->emu_lock, flags);
719 static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
720 unsigned short reg, unsigned short val)
722 struct emu10k1x *emu = ac97->private_data;
725 spin_lock_irqsave(&emu->emu_lock, flags);
726 outb(reg, emu->port + AC97ADDRESS);
727 outw(val, emu->port + AC97DATA);
728 spin_unlock_irqrestore(&emu->emu_lock, flags);
731 static int snd_emu10k1x_ac97(struct emu10k1x *chip)
733 struct snd_ac97_bus *pbus;
734 struct snd_ac97_template ac97;
736 static struct snd_ac97_bus_ops ops = {
737 .write = snd_emu10k1x_ac97_write,
738 .read = snd_emu10k1x_ac97_read,
741 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
743 pbus->no_vra = 1; /* we don't need VRA */
745 memset(&ac97, 0, sizeof(ac97));
746 ac97.private_data = chip;
747 ac97.scaps = AC97_SCAP_NO_SPDIF;
748 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
751 static int snd_emu10k1x_free(struct emu10k1x *chip)
753 snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
754 // disable interrupts
755 outl(0, chip->port + INTE);
757 outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
759 // release the i/o port
760 release_and_free_resource(chip->res_port);
764 free_irq(chip->irq, (void *)chip);
767 if (chip->dma_buffer.area) {
768 snd_dma_free_pages(&chip->dma_buffer);
771 pci_disable_device(chip->pci);
778 static int snd_emu10k1x_dev_free(struct snd_device *device)
780 struct emu10k1x *chip = device->device_data;
781 return snd_emu10k1x_free(chip);
784 static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id,
785 struct pt_regs *regs)
789 struct emu10k1x *chip = dev_id;
790 struct emu10k1x_voice *pvoice = chip->voices;
794 status = inl(chip->port + IPR);
800 if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
801 struct emu10k1x_voice *pvoice = &chip->capture_voice;
803 snd_emu10k1x_pcm_interrupt(chip, pvoice);
805 snd_emu10k1x_intr_disable(chip,
807 INTE_CAP_0_HALF_LOOP);
810 mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
811 for (i = 0; i < 3; i++) {
814 snd_emu10k1x_pcm_interrupt(chip, pvoice);
816 snd_emu10k1x_intr_disable(chip, mask);
822 if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
823 if (chip->midi.interrupt)
824 chip->midi.interrupt(chip, status);
826 snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
829 // acknowledge the interrupt if necessary
830 outl(status, chip->port + IPR);
832 // snd_printk(KERN_INFO "interrupt %08x\n", status);
836 static int __devinit snd_emu10k1x_pcm(struct emu10k1x *emu, int device, struct snd_pcm **rpcm)
847 if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
850 pcm->private_data = emu;
854 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
855 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
859 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
864 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
867 strcpy(pcm->name, "EMU10K1X Front");
870 strcpy(pcm->name, "EMU10K1X Rear");
873 strcpy(pcm->name, "EMU10K1X Center/LFE");
878 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
879 snd_dma_pci_data(emu->pci),
888 static int __devinit snd_emu10k1x_create(struct snd_card *card,
890 struct emu10k1x **rchip)
892 struct emu10k1x *chip;
895 static struct snd_device_ops ops = {
896 .dev_free = snd_emu10k1x_dev_free,
901 if ((err = pci_enable_device(pci)) < 0)
903 if (pci_set_dma_mask(pci, DMA_28BIT_MASK) < 0 ||
904 pci_set_consistent_dma_mask(pci, DMA_28BIT_MASK) < 0) {
905 snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
906 pci_disable_device(pci);
910 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
912 pci_disable_device(pci);
920 spin_lock_init(&chip->emu_lock);
921 spin_lock_init(&chip->voice_lock);
923 chip->port = pci_resource_start(pci, 0);
924 if ((chip->res_port = request_region(chip->port, 8,
925 "EMU10K1X")) == NULL) {
926 snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
927 snd_emu10k1x_free(chip);
931 if (request_irq(pci->irq, snd_emu10k1x_interrupt,
932 SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
934 snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
935 snd_emu10k1x_free(chip);
938 chip->irq = pci->irq;
940 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
941 4 * 1024, &chip->dma_buffer) < 0) {
942 snd_emu10k1x_free(chip);
947 /* read revision & serial */
948 pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
949 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
950 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
951 snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
952 chip->revision, chip->serial);
954 outl(0, chip->port + INTE);
956 for(ch = 0; ch < 3; ch++) {
957 chip->voices[ch].emu = chip;
958 chip->voices[ch].number = ch;
962 * Init to 0x02109204 :
963 * Clock accuracy = 0 (1000ppm)
964 * Sample Rate = 2 (48kHz)
965 * Audio Channel = 1 (Left of 2)
966 * Source Number = 0 (Unspecified)
967 * Generation Status = 1 (Original for Cat Code 12)
968 * Cat Code = 12 (Digital Signal Mixer)
970 * Emphasis = 0 (None)
971 * CP = 1 (Copyright unasserted)
972 * AN = 0 (Audio data)
975 snd_emu10k1x_ptr_write(chip, SPCS0, 0,
976 chip->spdif_bits[0] =
977 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
978 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
979 SPCS_GENERATIONSTATUS | 0x00001200 |
980 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
981 snd_emu10k1x_ptr_write(chip, SPCS1, 0,
982 chip->spdif_bits[1] =
983 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
984 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
985 SPCS_GENERATIONSTATUS | 0x00001200 |
986 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
987 snd_emu10k1x_ptr_write(chip, SPCS2, 0,
988 chip->spdif_bits[2] =
989 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
990 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
991 SPCS_GENERATIONSTATUS | 0x00001200 |
992 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
994 snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
995 snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
996 snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
998 outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1000 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1002 snd_emu10k1x_free(chip);
1009 static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
1010 struct snd_info_buffer *buffer)
1012 struct emu10k1x *emu = entry->private_data;
1013 unsigned long value,value1,value2;
1014 unsigned long flags;
1017 snd_iprintf(buffer, "Registers:\n\n");
1018 for(i = 0; i < 0x20; i+=4) {
1019 spin_lock_irqsave(&emu->emu_lock, flags);
1020 value = inl(emu->port + i);
1021 spin_unlock_irqrestore(&emu->emu_lock, flags);
1022 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
1024 snd_iprintf(buffer, "\nRegisters\n\n");
1025 for(i = 0; i <= 0x48; i++) {
1026 value = snd_emu10k1x_ptr_read(emu, i, 0);
1027 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
1028 value1 = snd_emu10k1x_ptr_read(emu, i, 1);
1029 value2 = snd_emu10k1x_ptr_read(emu, i, 2);
1030 snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
1032 snd_iprintf(buffer, "%02X: %08lX\n", i, value);
1037 static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
1038 struct snd_info_buffer *buffer)
1040 struct emu10k1x *emu = entry->private_data;
1042 unsigned int reg, channel_id , val;
1044 while (!snd_info_get_line(buffer, line, sizeof(line))) {
1045 if (sscanf(line, "%x %x %x", ®, &channel_id, &val) != 3)
1048 if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff)
1049 && (channel_id >=0) && (channel_id <= 2) )
1050 snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1054 static int __devinit snd_emu10k1x_proc_init(struct emu10k1x * emu)
1056 struct snd_info_entry *entry;
1058 if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
1059 snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
1060 entry->c.text.write_size = 64;
1061 entry->c.text.write = snd_emu10k1x_proc_reg_write;
1062 entry->mode |= S_IWUSR;
1063 entry->private_data = emu;
1069 static int snd_emu10k1x_shared_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1071 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1073 uinfo->value.integer.min = 0;
1074 uinfo->value.integer.max = 1;
1078 static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
1079 struct snd_ctl_elem_value *ucontrol)
1081 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1083 ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1088 static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
1089 struct snd_ctl_elem_value *ucontrol)
1091 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1095 val = ucontrol->value.integer.value[0] ;
1098 // enable spdif output
1099 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1100 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1101 snd_emu10k1x_gpio_write(emu, 0x1000);
1103 // disable spdif output
1104 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1105 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1106 snd_emu10k1x_gpio_write(emu, 0x1080);
1111 static struct snd_kcontrol_new snd_emu10k1x_shared_spdif __devinitdata =
1113 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1114 .name = "Analog/Digital Output Jack",
1115 .info = snd_emu10k1x_shared_spdif_info,
1116 .get = snd_emu10k1x_shared_spdif_get,
1117 .put = snd_emu10k1x_shared_spdif_put
1120 static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1122 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1127 static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
1128 struct snd_ctl_elem_value *ucontrol)
1130 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1131 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1133 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1134 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1135 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1136 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1140 static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
1141 struct snd_ctl_elem_value *ucontrol)
1143 ucontrol->value.iec958.status[0] = 0xff;
1144 ucontrol->value.iec958.status[1] = 0xff;
1145 ucontrol->value.iec958.status[2] = 0xff;
1146 ucontrol->value.iec958.status[3] = 0xff;
1150 static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
1151 struct snd_ctl_elem_value *ucontrol)
1153 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1154 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1158 val = (ucontrol->value.iec958.status[0] << 0) |
1159 (ucontrol->value.iec958.status[1] << 8) |
1160 (ucontrol->value.iec958.status[2] << 16) |
1161 (ucontrol->value.iec958.status[3] << 24);
1162 change = val != emu->spdif_bits[idx];
1164 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1165 emu->spdif_bits[idx] = val;
1170 static struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
1172 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1173 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1174 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1176 .info = snd_emu10k1x_spdif_info,
1177 .get = snd_emu10k1x_spdif_get_mask
1180 static struct snd_kcontrol_new snd_emu10k1x_spdif_control =
1182 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1183 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1185 .info = snd_emu10k1x_spdif_info,
1186 .get = snd_emu10k1x_spdif_get,
1187 .put = snd_emu10k1x_spdif_put
1190 static int __devinit snd_emu10k1x_mixer(struct emu10k1x *emu)
1193 struct snd_kcontrol *kctl;
1194 struct snd_card *card = emu->card;
1196 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
1198 if ((err = snd_ctl_add(card, kctl)))
1200 if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
1202 if ((err = snd_ctl_add(card, kctl)))
1204 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
1206 if ((err = snd_ctl_add(card, kctl)))
1212 #define EMU10K1X_MIDI_MODE_INPUT (1<<0)
1213 #define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
1215 static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
1217 return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1220 static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
1222 snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1225 #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
1226 #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
1227 #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
1228 #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
1230 #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1231 #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
1233 #define MPU401_RESET 0xff
1234 #define MPU401_ENTER_UART 0x3f
1235 #define MPU401_ACK 0xfe
1237 static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
1239 int timeout = 100000;
1240 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1241 mpu401_read_data(emu, mpu);
1242 #ifdef CONFIG_SND_DEBUG
1244 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
1252 static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
1253 struct emu10k1x_midi *midi, unsigned int status)
1257 if (midi->rmidi == NULL) {
1258 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1262 spin_lock(&midi->input_lock);
1263 if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1264 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1265 mpu401_clear_rx(emu, midi);
1267 byte = mpu401_read_data(emu, midi);
1268 if (midi->substream_input)
1269 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1272 spin_unlock(&midi->input_lock);
1274 spin_lock(&midi->output_lock);
1275 if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1276 if (midi->substream_output &&
1277 snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1278 mpu401_write_data(emu, midi, byte);
1280 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1283 spin_unlock(&midi->output_lock);
1286 static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
1288 do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1291 static void snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
1292 struct emu10k1x_midi *midi, unsigned char cmd, int ack)
1294 unsigned long flags;
1297 spin_lock_irqsave(&midi->input_lock, flags);
1298 mpu401_write_data(emu, midi, 0x00);
1299 /* mpu401_clear_rx(emu, midi); */
1301 mpu401_write_cmd(emu, midi, cmd);
1305 while (!ok && timeout-- > 0) {
1306 if (mpu401_input_avail(emu, midi)) {
1307 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1311 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1316 spin_unlock_irqrestore(&midi->input_lock, flags);
1318 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1320 mpu401_read_stat(emu, midi),
1321 mpu401_read_data(emu, midi));
1324 static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
1326 struct emu10k1x *emu;
1327 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1328 unsigned long flags;
1331 snd_assert(emu, return -ENXIO);
1332 spin_lock_irqsave(&midi->open_lock, flags);
1333 midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1334 midi->substream_input = substream;
1335 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1336 spin_unlock_irqrestore(&midi->open_lock, flags);
1337 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1338 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1340 spin_unlock_irqrestore(&midi->open_lock, flags);
1345 static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
1347 struct emu10k1x *emu;
1348 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1349 unsigned long flags;
1352 snd_assert(emu, return -ENXIO);
1353 spin_lock_irqsave(&midi->open_lock, flags);
1354 midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1355 midi->substream_output = substream;
1356 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1357 spin_unlock_irqrestore(&midi->open_lock, flags);
1358 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1359 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1361 spin_unlock_irqrestore(&midi->open_lock, flags);
1366 static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
1368 struct emu10k1x *emu;
1369 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1370 unsigned long flags;
1373 snd_assert(emu, return -ENXIO);
1374 spin_lock_irqsave(&midi->open_lock, flags);
1375 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1376 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1377 midi->substream_input = NULL;
1378 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1379 spin_unlock_irqrestore(&midi->open_lock, flags);
1380 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1382 spin_unlock_irqrestore(&midi->open_lock, flags);
1387 static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
1389 struct emu10k1x *emu;
1390 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1391 unsigned long flags;
1394 snd_assert(emu, return -ENXIO);
1395 spin_lock_irqsave(&midi->open_lock, flags);
1396 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1397 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1398 midi->substream_output = NULL;
1399 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1400 spin_unlock_irqrestore(&midi->open_lock, flags);
1401 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1403 spin_unlock_irqrestore(&midi->open_lock, flags);
1408 static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1410 struct emu10k1x *emu;
1411 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1413 snd_assert(emu, return);
1416 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1418 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1421 static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1423 struct emu10k1x *emu;
1424 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1425 unsigned long flags;
1428 snd_assert(emu, return);
1434 /* try to send some amount of bytes here before interrupts */
1435 spin_lock_irqsave(&midi->output_lock, flags);
1437 if (mpu401_output_ready(emu, midi)) {
1438 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1439 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1441 spin_unlock_irqrestore(&midi->output_lock, flags);
1444 mpu401_write_data(emu, midi, byte);
1450 spin_unlock_irqrestore(&midi->output_lock, flags);
1451 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1453 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1461 static struct snd_rawmidi_ops snd_emu10k1x_midi_output =
1463 .open = snd_emu10k1x_midi_output_open,
1464 .close = snd_emu10k1x_midi_output_close,
1465 .trigger = snd_emu10k1x_midi_output_trigger,
1468 static struct snd_rawmidi_ops snd_emu10k1x_midi_input =
1470 .open = snd_emu10k1x_midi_input_open,
1471 .close = snd_emu10k1x_midi_input_close,
1472 .trigger = snd_emu10k1x_midi_input_trigger,
1475 static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
1477 struct emu10k1x_midi *midi = rmidi->private_data;
1478 midi->interrupt = NULL;
1482 static int __devinit emu10k1x_midi_init(struct emu10k1x *emu,
1483 struct emu10k1x_midi *midi, int device, char *name)
1485 struct snd_rawmidi *rmidi;
1488 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
1491 spin_lock_init(&midi->open_lock);
1492 spin_lock_init(&midi->input_lock);
1493 spin_lock_init(&midi->output_lock);
1494 strcpy(rmidi->name, name);
1495 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1496 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1497 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1498 SNDRV_RAWMIDI_INFO_INPUT |
1499 SNDRV_RAWMIDI_INFO_DUPLEX;
1500 rmidi->private_data = midi;
1501 rmidi->private_free = snd_emu10k1x_midi_free;
1502 midi->rmidi = rmidi;
1506 static int __devinit snd_emu10k1x_midi(struct emu10k1x *emu)
1508 struct emu10k1x_midi *midi = &emu->midi;
1511 if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1514 midi->tx_enable = INTE_MIDITXENABLE;
1515 midi->rx_enable = INTE_MIDIRXENABLE;
1516 midi->port = MUDATA;
1517 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1518 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1519 midi->interrupt = snd_emu10k1x_midi_interrupt;
1523 static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
1524 const struct pci_device_id *pci_id)
1527 struct snd_card *card;
1528 struct emu10k1x *chip;
1531 if (dev >= SNDRV_CARDS)
1538 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1542 if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
1543 snd_card_free(card);
1547 if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
1548 snd_card_free(card);
1551 if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
1552 snd_card_free(card);
1555 if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
1556 snd_card_free(card);
1560 if ((err = snd_emu10k1x_ac97(chip)) < 0) {
1561 snd_card_free(card);
1565 if ((err = snd_emu10k1x_mixer(chip)) < 0) {
1566 snd_card_free(card);
1570 if ((err = snd_emu10k1x_midi(chip)) < 0) {
1571 snd_card_free(card);
1575 snd_emu10k1x_proc_init(chip);
1577 strcpy(card->driver, "EMU10K1X");
1578 strcpy(card->shortname, "Dell Sound Blaster Live!");
1579 sprintf(card->longname, "%s at 0x%lx irq %i",
1580 card->shortname, chip->port, chip->irq);
1582 if ((err = snd_card_register(card)) < 0) {
1583 snd_card_free(card);
1587 pci_set_drvdata(pci, card);
1592 static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
1594 snd_card_free(pci_get_drvdata(pci));
1595 pci_set_drvdata(pci, NULL);
1599 static struct pci_device_id snd_emu10k1x_ids[] = {
1600 { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Dell OEM version (EMU10K1) */
1603 MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1605 // pci_driver definition
1606 static struct pci_driver driver = {
1608 .id_table = snd_emu10k1x_ids,
1609 .probe = snd_emu10k1x_probe,
1610 .remove = __devexit_p(snd_emu10k1x_remove),
1613 // initialization of the module
1614 static int __init alsa_card_emu10k1x_init(void)
1618 if ((err = pci_register_driver(&driver)) > 0)
1624 // clean up the module
1625 static void __exit alsa_card_emu10k1x_exit(void)
1627 pci_unregister_driver(&driver);
1630 module_init(alsa_card_emu10k1x_init)
1631 module_exit(alsa_card_emu10k1x_exit)