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/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>
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!}");
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;
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.");
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 /************************************************************************************************/
68 #define PTR 0x00 /* Indexed register set pointer register */
69 /* NOTE: The CHANNELNUM and ADDRESS words can */
70 /* be modified independently of each other. */
72 #define DATA 0x04 /* Indexed register set data register */
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 */
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 */
92 #define HCFG 0x14 /* Hardware config register */
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. */
102 #define AC97DATA 0x1c /* AC97 register set data register (16 bit) */
104 #define AC97ADDRESS 0x1e /* AC97 register set address register (8 bit) */
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.
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
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
129 /* From 0x20 - 0x3f, last samples played on each channel */
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 */
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
144 #define SPCS0 0x42 /* SPDIF output Channel Status 0 register */
146 #define SPCS1 0x43 /* SPDIF output Channel Status 1 register */
148 #define SPCS2 0x44 /* SPDIF output Channel Status 2 register */
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) */
174 #define SPDIF_SELECT 0x45 /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
176 /* This is the MPU port on the card */
181 /* From 0x50 - 0x5f, last samples captured */
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
194 * The center/lfe channel has no volume control and cannot be muted during
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;
202 struct snd_emu10k1x_voice {
207 emu10k1x_pcm_t *epcm;
210 struct snd_emu10k1x_pcm {
212 snd_pcm_substream_t *substream;
213 emu10k1x_voice_t *voice;
214 unsigned short running;
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;
229 void (*interrupt)(emu10k1x_t *emu, unsigned int status);
232 // definition of the chip-specific record
233 struct snd_emu10k1x {
238 struct resource *res_port;
241 unsigned int revision; /* chip revision */
242 unsigned int serial; /* serial number */
243 unsigned short model; /* subsystem id */
246 spinlock_t voice_lock;
251 emu10k1x_voice_t voices[3];
252 emu10k1x_voice_t capture_voice;
253 u32 spdif_bits[3]; // SPDIF out setup
255 struct snd_dma_buffer dma_buffer;
257 emu10k1x_midi_t midi;
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,
272 .buffer_bytes_max = (32*1024),
273 .period_bytes_min = 64,
274 .period_bytes_max = (16*1024),
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,
291 .buffer_bytes_max = (32*1024),
292 .period_bytes_min = 64,
293 .period_bytes_max = (16*1024),
299 static unsigned int snd_emu10k1x_ptr_read(emu10k1x_t * emu,
304 unsigned int regptr, val;
306 regptr = (reg << 16) | chn;
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);
315 static void snd_emu10k1x_ptr_write(emu10k1x_t *emu,
323 regptr = (reg << 16) | chn;
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);
331 static void snd_emu10k1x_intr_enable(emu10k1x_t *emu, unsigned int intrenb)
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);
342 static void snd_emu10k1x_intr_disable(emu10k1x_t *emu, unsigned int intrenb)
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);
353 static void snd_emu10k1x_gpio_write(emu10k1x_t *emu, unsigned int value)
357 spin_lock_irqsave(&emu->emu_lock, flags);
358 outl(value, emu->port + GPIO);
359 spin_unlock_irqrestore(&emu->emu_lock, flags);
362 static void snd_emu10k1x_pcm_free_substream(snd_pcm_runtime_t *runtime)
364 kfree(runtime->private_data);
367 static void snd_emu10k1x_pcm_interrupt(emu10k1x_t *emu, emu10k1x_voice_t *voice)
369 emu10k1x_pcm_t *epcm;
371 if ((epcm = voice->epcm) == NULL)
373 if (epcm->substream == NULL)
376 snd_printk(KERN_INFO "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
377 epcm->substream->ops->pointer(epcm->substream),
378 snd_pcm_lib_period_bytes(epcm->substream),
379 snd_pcm_lib_buffer_bytes(epcm->substream));
381 snd_pcm_period_elapsed(epcm->substream);
385 static int snd_emu10k1x_playback_open(snd_pcm_substream_t *substream)
387 emu10k1x_t *chip = snd_pcm_substream_chip(substream);
388 emu10k1x_pcm_t *epcm;
389 snd_pcm_runtime_t *runtime = substream->runtime;
392 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
395 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
398 epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
402 epcm->substream = substream;
404 runtime->private_data = epcm;
405 runtime->private_free = snd_emu10k1x_pcm_free_substream;
407 runtime->hw = snd_emu10k1x_playback_hw;
413 static int snd_emu10k1x_playback_close(snd_pcm_substream_t *substream)
418 /* hw_params callback */
419 static int snd_emu10k1x_pcm_hw_params(snd_pcm_substream_t *substream,
420 snd_pcm_hw_params_t * hw_params)
422 snd_pcm_runtime_t *runtime = substream->runtime;
423 emu10k1x_pcm_t *epcm = runtime->private_data;
426 epcm->voice = &epcm->emu->voices[substream->pcm->device];
427 epcm->voice->use = 1;
428 epcm->voice->epcm = epcm;
431 return snd_pcm_lib_malloc_pages(substream,
432 params_buffer_bytes(hw_params));
435 /* hw_free callback */
436 static int snd_emu10k1x_pcm_hw_free(snd_pcm_substream_t *substream)
438 snd_pcm_runtime_t *runtime = substream->runtime;
439 emu10k1x_pcm_t *epcm;
441 if (runtime->private_data == NULL)
444 epcm = runtime->private_data;
447 epcm->voice->use = 0;
448 epcm->voice->epcm = NULL;
452 return snd_pcm_lib_free_pages(substream);
455 /* prepare callback */
456 static int snd_emu10k1x_pcm_prepare(snd_pcm_substream_t *substream)
458 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
459 snd_pcm_runtime_t *runtime = substream->runtime;
460 emu10k1x_pcm_t *epcm = runtime->private_data;
461 int voice = epcm->voice->number;
462 u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
463 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
466 for(i=0; i < runtime->periods; i++) {
467 *table_base++=runtime->dma_addr+(i*period_size_bytes);
468 *table_base++=period_size_bytes<<16;
471 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
472 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
473 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
474 snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
475 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
476 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
477 snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
479 snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
484 /* trigger callback */
485 static int snd_emu10k1x_pcm_trigger(snd_pcm_substream_t *substream,
488 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
489 snd_pcm_runtime_t *runtime = substream->runtime;
490 emu10k1x_pcm_t *epcm = runtime->private_data;
491 int channel = epcm->voice->number;
494 // snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
497 case SNDRV_PCM_TRIGGER_START:
498 if(runtime->periods == 2)
499 snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
501 snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
503 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
505 case SNDRV_PCM_TRIGGER_STOP:
507 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
508 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
517 /* pointer callback */
518 static snd_pcm_uframes_t
519 snd_emu10k1x_pcm_pointer(snd_pcm_substream_t *substream)
521 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
522 snd_pcm_runtime_t *runtime = substream->runtime;
523 emu10k1x_pcm_t *epcm = runtime->private_data;
524 int channel = epcm->voice->number;
525 snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
530 ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
531 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
532 ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
534 if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
538 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
539 ptr2 = bytes_to_frames(runtime, ptr1);
540 ptr2 += (ptr4 >> 3) * runtime->period_size;
543 if (ptr >= runtime->buffer_size)
544 ptr -= runtime->buffer_size;
550 static snd_pcm_ops_t snd_emu10k1x_playback_ops = {
551 .open = snd_emu10k1x_playback_open,
552 .close = snd_emu10k1x_playback_close,
553 .ioctl = snd_pcm_lib_ioctl,
554 .hw_params = snd_emu10k1x_pcm_hw_params,
555 .hw_free = snd_emu10k1x_pcm_hw_free,
556 .prepare = snd_emu10k1x_pcm_prepare,
557 .trigger = snd_emu10k1x_pcm_trigger,
558 .pointer = snd_emu10k1x_pcm_pointer,
561 /* open_capture callback */
562 static int snd_emu10k1x_pcm_open_capture(snd_pcm_substream_t *substream)
564 emu10k1x_t *chip = snd_pcm_substream_chip(substream);
565 emu10k1x_pcm_t *epcm;
566 snd_pcm_runtime_t *runtime = substream->runtime;
569 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
571 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
574 epcm = kcalloc(1, sizeof(*epcm), GFP_KERNEL);
579 epcm->substream = substream;
581 runtime->private_data = epcm;
582 runtime->private_free = snd_emu10k1x_pcm_free_substream;
584 runtime->hw = snd_emu10k1x_capture_hw;
590 static int snd_emu10k1x_pcm_close_capture(snd_pcm_substream_t *substream)
595 /* hw_params callback */
596 static int snd_emu10k1x_pcm_hw_params_capture(snd_pcm_substream_t *substream,
597 snd_pcm_hw_params_t * hw_params)
599 snd_pcm_runtime_t *runtime = substream->runtime;
600 emu10k1x_pcm_t *epcm = runtime->private_data;
603 if (epcm->emu->capture_voice.use)
605 epcm->voice = &epcm->emu->capture_voice;
606 epcm->voice->epcm = epcm;
607 epcm->voice->use = 1;
610 return snd_pcm_lib_malloc_pages(substream,
611 params_buffer_bytes(hw_params));
614 /* hw_free callback */
615 static int snd_emu10k1x_pcm_hw_free_capture(snd_pcm_substream_t *substream)
617 snd_pcm_runtime_t *runtime = substream->runtime;
619 emu10k1x_pcm_t *epcm;
621 if (runtime->private_data == NULL)
623 epcm = runtime->private_data;
626 epcm->voice->use = 0;
627 epcm->voice->epcm = NULL;
631 return snd_pcm_lib_free_pages(substream);
634 /* prepare capture callback */
635 static int snd_emu10k1x_pcm_prepare_capture(snd_pcm_substream_t *substream)
637 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
638 snd_pcm_runtime_t *runtime = substream->runtime;
640 snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
641 snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
642 snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
643 snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
648 /* trigger_capture callback */
649 static int snd_emu10k1x_pcm_trigger_capture(snd_pcm_substream_t *substream,
652 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
653 snd_pcm_runtime_t *runtime = substream->runtime;
654 emu10k1x_pcm_t *epcm = runtime->private_data;
658 case SNDRV_PCM_TRIGGER_START:
659 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
660 INTE_CAP_0_HALF_LOOP);
661 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
664 case SNDRV_PCM_TRIGGER_STOP:
666 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
667 INTE_CAP_0_HALF_LOOP);
668 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
677 /* pointer_capture callback */
678 static snd_pcm_uframes_t
679 snd_emu10k1x_pcm_pointer_capture(snd_pcm_substream_t *substream)
681 emu10k1x_t *emu = snd_pcm_substream_chip(substream);
682 snd_pcm_runtime_t *runtime = substream->runtime;
683 emu10k1x_pcm_t *epcm = runtime->private_data;
684 snd_pcm_uframes_t ptr;
689 ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
690 if (ptr >= runtime->buffer_size)
691 ptr -= runtime->buffer_size;
696 static snd_pcm_ops_t snd_emu10k1x_capture_ops = {
697 .open = snd_emu10k1x_pcm_open_capture,
698 .close = snd_emu10k1x_pcm_close_capture,
699 .ioctl = snd_pcm_lib_ioctl,
700 .hw_params = snd_emu10k1x_pcm_hw_params_capture,
701 .hw_free = snd_emu10k1x_pcm_hw_free_capture,
702 .prepare = snd_emu10k1x_pcm_prepare_capture,
703 .trigger = snd_emu10k1x_pcm_trigger_capture,
704 .pointer = snd_emu10k1x_pcm_pointer_capture,
707 static unsigned short snd_emu10k1x_ac97_read(ac97_t *ac97,
710 emu10k1x_t *emu = ac97->private_data;
714 spin_lock_irqsave(&emu->emu_lock, flags);
715 outb(reg, emu->port + AC97ADDRESS);
716 val = inw(emu->port + AC97DATA);
717 spin_unlock_irqrestore(&emu->emu_lock, flags);
721 static void snd_emu10k1x_ac97_write(ac97_t *ac97,
722 unsigned short reg, unsigned short val)
724 emu10k1x_t *emu = ac97->private_data;
727 spin_lock_irqsave(&emu->emu_lock, flags);
728 outb(reg, emu->port + AC97ADDRESS);
729 outw(val, emu->port + AC97DATA);
730 spin_unlock_irqrestore(&emu->emu_lock, flags);
733 static int snd_emu10k1x_ac97(emu10k1x_t *chip)
736 ac97_template_t ac97;
738 static ac97_bus_ops_t ops = {
739 .write = snd_emu10k1x_ac97_write,
740 .read = snd_emu10k1x_ac97_read,
743 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
745 pbus->no_vra = 1; /* we don't need VRA */
747 memset(&ac97, 0, sizeof(ac97));
748 ac97.private_data = chip;
749 ac97.scaps = AC97_SCAP_NO_SPDIF;
750 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
753 static int snd_emu10k1x_free(emu10k1x_t *chip)
755 snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
756 // disable interrupts
757 outl(0, chip->port + INTE);
759 outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
761 // release the i/o port
762 if (chip->res_port) {
763 release_resource(chip->res_port);
764 kfree_nocheck(chip->res_port);
768 free_irq(chip->irq, (void *)chip);
771 if (chip->dma_buffer.area) {
772 snd_dma_free_pages(&chip->dma_buffer);
775 pci_disable_device(chip->pci);
782 static int snd_emu10k1x_dev_free(snd_device_t *device)
784 emu10k1x_t *chip = device->device_data;
785 return snd_emu10k1x_free(chip);
788 static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id,
789 struct pt_regs *regs)
793 emu10k1x_t *chip = dev_id;
794 emu10k1x_voice_t *pvoice = chip->voices;
798 status = inl(chip->port + IPR);
802 if(status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
803 emu10k1x_voice_t *pvoice = &chip->capture_voice;
805 snd_emu10k1x_pcm_interrupt(chip, pvoice);
807 snd_emu10k1x_intr_disable(chip,
809 INTE_CAP_0_HALF_LOOP);
812 mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
813 for(i = 0; i < 3; i++) {
816 snd_emu10k1x_pcm_interrupt(chip, pvoice);
818 snd_emu10k1x_intr_disable(chip, mask);
824 if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
825 if (chip->midi.interrupt)
826 chip->midi.interrupt(chip, status);
828 snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
831 // acknowledge the interrupt if necessary
833 outl(status, chip->port+IPR);
835 // snd_printk(KERN_INFO "interrupt %08x\n", status);
841 static void snd_emu10k1x_pcm_free(snd_pcm_t *pcm)
843 emu10k1x_t *emu = pcm->private_data;
845 snd_pcm_lib_preallocate_free_for_all(pcm);
848 static int __devinit snd_emu10k1x_pcm(emu10k1x_t *emu, int device, snd_pcm_t **rpcm)
859 if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
862 pcm->private_data = emu;
863 pcm->private_free = snd_emu10k1x_pcm_free;
867 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
868 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
872 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
877 pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
880 strcpy(pcm->name, "EMU10K1X Front");
883 strcpy(pcm->name, "EMU10K1X Rear");
886 strcpy(pcm->name, "EMU10K1X Center/LFE");
891 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
892 snd_dma_pci_data(emu->pci),
901 static int __devinit snd_emu10k1x_create(snd_card_t *card,
908 static snd_device_ops_t ops = {
909 .dev_free = snd_emu10k1x_dev_free,
914 if ((err = pci_enable_device(pci)) < 0)
916 if (pci_set_dma_mask(pci, 0x0fffffff) < 0 ||
917 pci_set_consistent_dma_mask(pci, 0x0fffffff) < 0) {
918 snd_printk(KERN_ERR "error to set 28bit mask DMA\n");
919 pci_disable_device(pci);
923 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
925 pci_disable_device(pci);
933 spin_lock_init(&chip->emu_lock);
934 spin_lock_init(&chip->voice_lock);
936 chip->port = pci_resource_start(pci, 0);
937 if ((chip->res_port = request_region(chip->port, 8,
938 "EMU10K1X")) == NULL) {
939 snd_printk(KERN_ERR "emu10k1x: cannot allocate the port 0x%lx\n", chip->port);
940 snd_emu10k1x_free(chip);
944 if (request_irq(pci->irq, snd_emu10k1x_interrupt,
945 SA_INTERRUPT|SA_SHIRQ, "EMU10K1X",
947 snd_printk(KERN_ERR "emu10k1x: cannot grab irq %d\n", pci->irq);
948 snd_emu10k1x_free(chip);
951 chip->irq = pci->irq;
953 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
954 4 * 1024, &chip->dma_buffer) < 0) {
955 snd_emu10k1x_free(chip);
960 /* read revision & serial */
961 pci_read_config_byte(pci, PCI_REVISION_ID, (char *)&chip->revision);
962 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
963 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
964 snd_printk(KERN_INFO "Model %04x Rev %08x Serial %08x\n", chip->model,
965 chip->revision, chip->serial);
967 outl(0, chip->port + INTE);
969 for(ch = 0; ch < 3; ch++) {
970 chip->voices[ch].emu = chip;
971 chip->voices[ch].number = ch;
975 * Init to 0x02109204 :
976 * Clock accuracy = 0 (1000ppm)
977 * Sample Rate = 2 (48kHz)
978 * Audio Channel = 1 (Left of 2)
979 * Source Number = 0 (Unspecified)
980 * Generation Status = 1 (Original for Cat Code 12)
981 * Cat Code = 12 (Digital Signal Mixer)
983 * Emphasis = 0 (None)
984 * CP = 1 (Copyright unasserted)
985 * AN = 0 (Audio data)
988 snd_emu10k1x_ptr_write(chip, SPCS0, 0,
989 chip->spdif_bits[0] =
990 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
991 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
992 SPCS_GENERATIONSTATUS | 0x00001200 |
993 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
994 snd_emu10k1x_ptr_write(chip, SPCS1, 0,
995 chip->spdif_bits[1] =
996 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
997 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
998 SPCS_GENERATIONSTATUS | 0x00001200 |
999 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1000 snd_emu10k1x_ptr_write(chip, SPCS2, 0,
1001 chip->spdif_bits[2] =
1002 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
1003 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
1004 SPCS_GENERATIONSTATUS | 0x00001200 |
1005 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
1007 snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
1008 snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
1009 snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
1011 outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
1013 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1015 snd_emu10k1x_free(chip);
1022 static void snd_emu10k1x_proc_reg_read(snd_info_entry_t *entry,
1023 snd_info_buffer_t * buffer)
1025 emu10k1x_t *emu = entry->private_data;
1026 unsigned long value,value1,value2;
1027 unsigned long flags;
1030 snd_iprintf(buffer, "Registers:\n\n");
1031 for(i = 0; i < 0x20; i+=4) {
1032 spin_lock_irqsave(&emu->emu_lock, flags);
1033 value = inl(emu->port + i);
1034 spin_unlock_irqrestore(&emu->emu_lock, flags);
1035 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
1037 snd_iprintf(buffer, "\nRegisters\n\n");
1038 for(i = 0; i <= 0x48; i++) {
1039 value = snd_emu10k1x_ptr_read(emu, i, 0);
1040 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
1041 value1 = snd_emu10k1x_ptr_read(emu, i, 1);
1042 value2 = snd_emu10k1x_ptr_read(emu, i, 2);
1043 snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
1045 snd_iprintf(buffer, "%02X: %08lX\n", i, value);
1050 static void snd_emu10k1x_proc_reg_write(snd_info_entry_t *entry,
1051 snd_info_buffer_t *buffer)
1053 emu10k1x_t *emu = entry->private_data;
1055 unsigned int reg, channel_id , val;
1057 while (!snd_info_get_line(buffer, line, sizeof(line))) {
1058 if (sscanf(line, "%x %x %x", ®, &channel_id, &val) != 3)
1061 if ((reg < 0x49) && (reg >=0) && (val <= 0xffffffff)
1062 && (channel_id >=0) && (channel_id <= 2) )
1063 snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1067 static int __devinit snd_emu10k1x_proc_init(emu10k1x_t * emu)
1069 snd_info_entry_t *entry;
1071 if(! snd_card_proc_new(emu->card, "emu10k1x_regs", &entry)) {
1072 snd_info_set_text_ops(entry, emu, 1024, snd_emu10k1x_proc_reg_read);
1073 entry->c.text.write_size = 64;
1074 entry->c.text.write = snd_emu10k1x_proc_reg_write;
1075 entry->mode |= S_IWUSR;
1076 entry->private_data = emu;
1082 static int snd_emu10k1x_shared_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1084 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1086 uinfo->value.integer.min = 0;
1087 uinfo->value.integer.max = 1;
1091 static int snd_emu10k1x_shared_spdif_get(snd_kcontrol_t * kcontrol,
1092 snd_ctl_elem_value_t * ucontrol)
1094 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1096 ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1101 static int snd_emu10k1x_shared_spdif_put(snd_kcontrol_t * kcontrol,
1102 snd_ctl_elem_value_t * ucontrol)
1104 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1108 val = ucontrol->value.integer.value[0] ;
1111 // enable spdif output
1112 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1113 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1114 snd_emu10k1x_gpio_write(emu, 0x1000);
1116 // disable spdif output
1117 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1118 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1119 snd_emu10k1x_gpio_write(emu, 0x1080);
1124 static snd_kcontrol_new_t snd_emu10k1x_shared_spdif __devinitdata =
1126 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1127 .name = "Analog/Digital Output Jack",
1128 .info = snd_emu10k1x_shared_spdif_info,
1129 .get = snd_emu10k1x_shared_spdif_get,
1130 .put = snd_emu10k1x_shared_spdif_put
1133 static int snd_emu10k1x_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1135 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1140 static int snd_emu10k1x_spdif_get(snd_kcontrol_t * kcontrol,
1141 snd_ctl_elem_value_t * ucontrol)
1143 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1144 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1146 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1147 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1148 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1149 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1153 static int snd_emu10k1x_spdif_get_mask(snd_kcontrol_t * kcontrol,
1154 snd_ctl_elem_value_t * ucontrol)
1156 ucontrol->value.iec958.status[0] = 0xff;
1157 ucontrol->value.iec958.status[1] = 0xff;
1158 ucontrol->value.iec958.status[2] = 0xff;
1159 ucontrol->value.iec958.status[3] = 0xff;
1163 static int snd_emu10k1x_spdif_put(snd_kcontrol_t * kcontrol,
1164 snd_ctl_elem_value_t * ucontrol)
1166 emu10k1x_t *emu = snd_kcontrol_chip(kcontrol);
1167 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1171 val = (ucontrol->value.iec958.status[0] << 0) |
1172 (ucontrol->value.iec958.status[1] << 8) |
1173 (ucontrol->value.iec958.status[2] << 16) |
1174 (ucontrol->value.iec958.status[3] << 24);
1175 change = val != emu->spdif_bits[idx];
1177 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1178 emu->spdif_bits[idx] = val;
1183 static snd_kcontrol_new_t snd_emu10k1x_spdif_mask_control =
1185 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1186 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1187 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1189 .info = snd_emu10k1x_spdif_info,
1190 .get = snd_emu10k1x_spdif_get_mask
1193 static snd_kcontrol_new_t snd_emu10k1x_spdif_control =
1195 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1196 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1198 .info = snd_emu10k1x_spdif_info,
1199 .get = snd_emu10k1x_spdif_get,
1200 .put = snd_emu10k1x_spdif_put
1203 static int __devinit snd_emu10k1x_mixer(emu10k1x_t *emu)
1206 snd_kcontrol_t *kctl;
1207 snd_card_t *card = emu->card;
1209 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
1211 if ((err = snd_ctl_add(card, kctl)))
1213 if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
1215 if ((err = snd_ctl_add(card, kctl)))
1217 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
1219 if ((err = snd_ctl_add(card, kctl)))
1225 #define EMU10K1X_MIDI_MODE_INPUT (1<<0)
1226 #define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
1228 static inline unsigned char mpu401_read(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int idx)
1230 return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1233 static inline void mpu401_write(emu10k1x_t *emu, emu10k1x_midi_t *mpu, int data, int idx)
1235 snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1238 #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
1239 #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
1240 #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
1241 #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
1243 #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1244 #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
1246 #define MPU401_RESET 0xff
1247 #define MPU401_ENTER_UART 0x3f
1248 #define MPU401_ACK 0xfe
1250 static void mpu401_clear_rx(emu10k1x_t *emu, emu10k1x_midi_t *mpu)
1252 int timeout = 100000;
1253 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1254 mpu401_read_data(emu, mpu);
1255 #ifdef CONFIG_SND_DEBUG
1257 snd_printk(KERN_ERR "cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu, mpu));
1265 static void do_emu10k1x_midi_interrupt(emu10k1x_t *emu, emu10k1x_midi_t *midi, unsigned int status)
1269 if (midi->rmidi == NULL) {
1270 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1274 spin_lock(&midi->input_lock);
1275 if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1276 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1277 mpu401_clear_rx(emu, midi);
1279 byte = mpu401_read_data(emu, midi);
1280 if (midi->substream_input)
1281 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1284 spin_unlock(&midi->input_lock);
1286 spin_lock(&midi->output_lock);
1287 if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1288 if (midi->substream_output &&
1289 snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1290 mpu401_write_data(emu, midi, byte);
1292 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1295 spin_unlock(&midi->output_lock);
1298 static void snd_emu10k1x_midi_interrupt(emu10k1x_t *emu, unsigned int status)
1300 do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1303 static void snd_emu10k1x_midi_cmd(emu10k1x_t * emu, emu10k1x_midi_t *midi, unsigned char cmd, int ack)
1305 unsigned long flags;
1308 spin_lock_irqsave(&midi->input_lock, flags);
1309 mpu401_write_data(emu, midi, 0x00);
1310 /* mpu401_clear_rx(emu, midi); */
1312 mpu401_write_cmd(emu, midi, cmd);
1316 while (!ok && timeout-- > 0) {
1317 if (mpu401_input_avail(emu, midi)) {
1318 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1322 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1327 spin_unlock_irqrestore(&midi->input_lock, flags);
1329 snd_printk(KERN_ERR "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1331 mpu401_read_stat(emu, midi),
1332 mpu401_read_data(emu, midi));
1335 static int snd_emu10k1x_midi_input_open(snd_rawmidi_substream_t * substream)
1338 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1339 unsigned long flags;
1342 snd_assert(emu, return -ENXIO);
1343 spin_lock_irqsave(&midi->open_lock, flags);
1344 midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1345 midi->substream_input = substream;
1346 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1347 spin_unlock_irqrestore(&midi->open_lock, flags);
1348 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1349 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1351 spin_unlock_irqrestore(&midi->open_lock, flags);
1356 static int snd_emu10k1x_midi_output_open(snd_rawmidi_substream_t * substream)
1359 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1360 unsigned long flags;
1363 snd_assert(emu, return -ENXIO);
1364 spin_lock_irqsave(&midi->open_lock, flags);
1365 midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1366 midi->substream_output = substream;
1367 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1368 spin_unlock_irqrestore(&midi->open_lock, flags);
1369 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1);
1370 snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1);
1372 spin_unlock_irqrestore(&midi->open_lock, flags);
1377 static int snd_emu10k1x_midi_input_close(snd_rawmidi_substream_t * substream)
1380 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1381 unsigned long flags;
1384 snd_assert(emu, return -ENXIO);
1385 spin_lock_irqsave(&midi->open_lock, flags);
1386 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1387 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1388 midi->substream_input = NULL;
1389 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1390 spin_unlock_irqrestore(&midi->open_lock, flags);
1391 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1393 spin_unlock_irqrestore(&midi->open_lock, flags);
1398 static int snd_emu10k1x_midi_output_close(snd_rawmidi_substream_t * substream)
1401 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1402 unsigned long flags;
1405 snd_assert(emu, return -ENXIO);
1406 spin_lock_irqsave(&midi->open_lock, flags);
1407 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1408 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1409 midi->substream_output = NULL;
1410 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1411 spin_unlock_irqrestore(&midi->open_lock, flags);
1412 snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1414 spin_unlock_irqrestore(&midi->open_lock, flags);
1419 static void snd_emu10k1x_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
1422 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1424 snd_assert(emu, return);
1427 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1429 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1432 static void snd_emu10k1x_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
1435 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)substream->rmidi->private_data;
1436 unsigned long flags;
1439 snd_assert(emu, return);
1445 /* try to send some amount of bytes here before interrupts */
1446 spin_lock_irqsave(&midi->output_lock, flags);
1448 if (mpu401_output_ready(emu, midi)) {
1449 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1450 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1452 spin_unlock_irqrestore(&midi->output_lock, flags);
1455 mpu401_write_data(emu, midi, byte);
1461 spin_unlock_irqrestore(&midi->output_lock, flags);
1462 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1464 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1472 static snd_rawmidi_ops_t snd_emu10k1x_midi_output =
1474 .open = snd_emu10k1x_midi_output_open,
1475 .close = snd_emu10k1x_midi_output_close,
1476 .trigger = snd_emu10k1x_midi_output_trigger,
1479 static snd_rawmidi_ops_t snd_emu10k1x_midi_input =
1481 .open = snd_emu10k1x_midi_input_open,
1482 .close = snd_emu10k1x_midi_input_close,
1483 .trigger = snd_emu10k1x_midi_input_trigger,
1486 static void snd_emu10k1x_midi_free(snd_rawmidi_t *rmidi)
1488 emu10k1x_midi_t *midi = (emu10k1x_midi_t *)rmidi->private_data;
1489 midi->interrupt = NULL;
1493 static int __devinit emu10k1x_midi_init(emu10k1x_t *emu, emu10k1x_midi_t *midi, int device, char *name)
1495 snd_rawmidi_t *rmidi;
1498 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
1501 spin_lock_init(&midi->open_lock);
1502 spin_lock_init(&midi->input_lock);
1503 spin_lock_init(&midi->output_lock);
1504 strcpy(rmidi->name, name);
1505 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1506 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1507 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1508 SNDRV_RAWMIDI_INFO_INPUT |
1509 SNDRV_RAWMIDI_INFO_DUPLEX;
1510 rmidi->private_data = midi;
1511 rmidi->private_free = snd_emu10k1x_midi_free;
1512 midi->rmidi = rmidi;
1516 static int __devinit snd_emu10k1x_midi(emu10k1x_t *emu)
1518 emu10k1x_midi_t *midi = &emu->midi;
1521 if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1524 midi->tx_enable = INTE_MIDITXENABLE;
1525 midi->rx_enable = INTE_MIDIRXENABLE;
1526 midi->port = MUDATA;
1527 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1528 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1529 midi->interrupt = snd_emu10k1x_midi_interrupt;
1533 static int __devinit snd_emu10k1x_probe(struct pci_dev *pci,
1534 const struct pci_device_id *pci_id)
1541 if (dev >= SNDRV_CARDS)
1548 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1552 if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
1553 snd_card_free(card);
1557 if ((err = snd_emu10k1x_pcm(chip, 0, NULL)) < 0) {
1558 snd_card_free(card);
1561 if ((err = snd_emu10k1x_pcm(chip, 1, NULL)) < 0) {
1562 snd_card_free(card);
1565 if ((err = snd_emu10k1x_pcm(chip, 2, NULL)) < 0) {
1566 snd_card_free(card);
1570 if ((err = snd_emu10k1x_ac97(chip)) < 0) {
1571 snd_card_free(card);
1575 if ((err = snd_emu10k1x_mixer(chip)) < 0) {
1576 snd_card_free(card);
1580 if ((err = snd_emu10k1x_midi(chip)) < 0) {
1581 snd_card_free(card);
1585 snd_emu10k1x_proc_init(chip);
1587 strcpy(card->driver, "EMU10K1X");
1588 strcpy(card->shortname, "Dell Sound Blaster Live!");
1589 sprintf(card->longname, "%s at 0x%lx irq %i",
1590 card->shortname, chip->port, chip->irq);
1592 if ((err = snd_card_register(card)) < 0) {
1593 snd_card_free(card);
1597 pci_set_drvdata(pci, card);
1602 static void __devexit snd_emu10k1x_remove(struct pci_dev *pci)
1604 snd_card_free(pci_get_drvdata(pci));
1605 pci_set_drvdata(pci, NULL);
1609 static struct pci_device_id snd_emu10k1x_ids[] = {
1610 { 0x1102, 0x0006, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, /* Dell OEM version (EMU10K1) */
1613 MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1615 // pci_driver definition
1616 static struct pci_driver driver = {
1618 .id_table = snd_emu10k1x_ids,
1619 .probe = snd_emu10k1x_probe,
1620 .remove = __devexit_p(snd_emu10k1x_remove),
1623 // initialization of the module
1624 static int __init alsa_card_emu10k1x_init(void)
1628 if ((err = pci_register_driver(&driver)) > 0)
1634 // clean up the module
1635 static void __exit alsa_card_emu10k1x_exit(void)
1637 pci_unregister_driver(&driver);
1640 module_init(alsa_card_emu10k1x_init)
1641 module_exit(alsa_card_emu10k1x_exit)