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 <linux/init.h>
33 #include <linux/interrupt.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/slab.h>
37 #include <linux/module.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 bool 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 address */
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 channel
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 struct emu10k1x_voice
{
199 struct emu10k1x
*emu
;
203 struct emu10k1x_pcm
*epcm
;
206 struct emu10k1x_pcm
{
207 struct emu10k1x
*emu
;
208 struct snd_pcm_substream
*substream
;
209 struct emu10k1x_voice
*voice
;
210 unsigned short running
;
213 struct emu10k1x_midi
{
214 struct emu10k1x
*emu
;
215 struct snd_rawmidi
*rmidi
;
216 struct snd_rawmidi_substream
*substream_input
;
217 struct snd_rawmidi_substream
*substream_output
;
218 unsigned int midi_mode
;
219 spinlock_t input_lock
;
220 spinlock_t output_lock
;
221 spinlock_t open_lock
;
222 int tx_enable
, rx_enable
;
225 void (*interrupt
)(struct emu10k1x
*emu
, unsigned int status
);
228 // definition of the chip-specific record
230 struct snd_card
*card
;
234 struct resource
*res_port
;
237 unsigned char revision
; /* chip revision */
238 unsigned int serial
; /* serial number */
239 unsigned short model
; /* subsystem id */
242 spinlock_t voice_lock
;
244 struct snd_ac97
*ac97
;
247 struct emu10k1x_voice voices
[3];
248 struct emu10k1x_voice capture_voice
;
249 u32 spdif_bits
[3]; // SPDIF out setup
251 struct snd_dma_buffer dma_buffer
;
253 struct emu10k1x_midi midi
;
256 /* hardware definition */
257 static struct snd_pcm_hardware snd_emu10k1x_playback_hw
= {
258 .info
= (SNDRV_PCM_INFO_MMAP
|
259 SNDRV_PCM_INFO_INTERLEAVED
|
260 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
261 SNDRV_PCM_INFO_MMAP_VALID
),
262 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
263 .rates
= SNDRV_PCM_RATE_48000
,
268 .buffer_bytes_max
= (32*1024),
269 .period_bytes_min
= 64,
270 .period_bytes_max
= (16*1024),
276 static struct snd_pcm_hardware snd_emu10k1x_capture_hw
= {
277 .info
= (SNDRV_PCM_INFO_MMAP
|
278 SNDRV_PCM_INFO_INTERLEAVED
|
279 SNDRV_PCM_INFO_BLOCK_TRANSFER
|
280 SNDRV_PCM_INFO_MMAP_VALID
),
281 .formats
= SNDRV_PCM_FMTBIT_S16_LE
,
282 .rates
= SNDRV_PCM_RATE_48000
,
287 .buffer_bytes_max
= (32*1024),
288 .period_bytes_min
= 64,
289 .period_bytes_max
= (16*1024),
295 static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x
* emu
,
300 unsigned int regptr
, val
;
302 regptr
= (reg
<< 16) | chn
;
304 spin_lock_irqsave(&emu
->emu_lock
, flags
);
305 outl(regptr
, emu
->port
+ PTR
);
306 val
= inl(emu
->port
+ DATA
);
307 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
311 static void snd_emu10k1x_ptr_write(struct emu10k1x
*emu
,
319 regptr
= (reg
<< 16) | chn
;
321 spin_lock_irqsave(&emu
->emu_lock
, flags
);
322 outl(regptr
, emu
->port
+ PTR
);
323 outl(data
, emu
->port
+ DATA
);
324 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
327 static void snd_emu10k1x_intr_enable(struct emu10k1x
*emu
, unsigned int intrenb
)
330 unsigned int intr_enable
;
332 spin_lock_irqsave(&emu
->emu_lock
, flags
);
333 intr_enable
= inl(emu
->port
+ INTE
) | intrenb
;
334 outl(intr_enable
, emu
->port
+ INTE
);
335 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
338 static void snd_emu10k1x_intr_disable(struct emu10k1x
*emu
, unsigned int intrenb
)
341 unsigned int intr_enable
;
343 spin_lock_irqsave(&emu
->emu_lock
, flags
);
344 intr_enable
= inl(emu
->port
+ INTE
) & ~intrenb
;
345 outl(intr_enable
, emu
->port
+ INTE
);
346 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
349 static void snd_emu10k1x_gpio_write(struct emu10k1x
*emu
, unsigned int value
)
353 spin_lock_irqsave(&emu
->emu_lock
, flags
);
354 outl(value
, emu
->port
+ GPIO
);
355 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
358 static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime
*runtime
)
360 kfree(runtime
->private_data
);
363 static void snd_emu10k1x_pcm_interrupt(struct emu10k1x
*emu
, struct emu10k1x_voice
*voice
)
365 struct emu10k1x_pcm
*epcm
;
367 if ((epcm
= voice
->epcm
) == NULL
)
369 if (epcm
->substream
== NULL
)
372 snd_printk(KERN_INFO
"IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
373 epcm
->substream
->ops
->pointer(epcm
->substream
),
374 snd_pcm_lib_period_bytes(epcm
->substream
),
375 snd_pcm_lib_buffer_bytes(epcm
->substream
));
377 snd_pcm_period_elapsed(epcm
->substream
);
381 static int snd_emu10k1x_playback_open(struct snd_pcm_substream
*substream
)
383 struct emu10k1x
*chip
= snd_pcm_substream_chip(substream
);
384 struct emu10k1x_pcm
*epcm
;
385 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
388 if ((err
= snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
)) < 0) {
391 if ((err
= snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 64)) < 0)
394 epcm
= kzalloc(sizeof(*epcm
), GFP_KERNEL
);
398 epcm
->substream
= substream
;
400 runtime
->private_data
= epcm
;
401 runtime
->private_free
= snd_emu10k1x_pcm_free_substream
;
403 runtime
->hw
= snd_emu10k1x_playback_hw
;
409 static int snd_emu10k1x_playback_close(struct snd_pcm_substream
*substream
)
414 /* hw_params callback */
415 static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream
*substream
,
416 struct snd_pcm_hw_params
*hw_params
)
418 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
419 struct emu10k1x_pcm
*epcm
= runtime
->private_data
;
422 epcm
->voice
= &epcm
->emu
->voices
[substream
->pcm
->device
];
423 epcm
->voice
->use
= 1;
424 epcm
->voice
->epcm
= epcm
;
427 return snd_pcm_lib_malloc_pages(substream
,
428 params_buffer_bytes(hw_params
));
431 /* hw_free callback */
432 static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream
*substream
)
434 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
435 struct emu10k1x_pcm
*epcm
;
437 if (runtime
->private_data
== NULL
)
440 epcm
= runtime
->private_data
;
443 epcm
->voice
->use
= 0;
444 epcm
->voice
->epcm
= NULL
;
448 return snd_pcm_lib_free_pages(substream
);
451 /* prepare callback */
452 static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream
*substream
)
454 struct emu10k1x
*emu
= snd_pcm_substream_chip(substream
);
455 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
456 struct emu10k1x_pcm
*epcm
= runtime
->private_data
;
457 int voice
= epcm
->voice
->number
;
458 u32
*table_base
= (u32
*)(emu
->dma_buffer
.area
+1024*voice
);
459 u32 period_size_bytes
= frames_to_bytes(runtime
, runtime
->period_size
);
462 for(i
= 0; i
< runtime
->periods
; i
++) {
463 *table_base
++=runtime
->dma_addr
+(i
*period_size_bytes
);
464 *table_base
++=period_size_bytes
<<16;
467 snd_emu10k1x_ptr_write(emu
, PLAYBACK_LIST_ADDR
, voice
, emu
->dma_buffer
.addr
+1024*voice
);
468 snd_emu10k1x_ptr_write(emu
, PLAYBACK_LIST_SIZE
, voice
, (runtime
->periods
- 1) << 19);
469 snd_emu10k1x_ptr_write(emu
, PLAYBACK_LIST_PTR
, voice
, 0);
470 snd_emu10k1x_ptr_write(emu
, PLAYBACK_POINTER
, voice
, 0);
471 snd_emu10k1x_ptr_write(emu
, PLAYBACK_UNKNOWN1
, voice
, 0);
472 snd_emu10k1x_ptr_write(emu
, PLAYBACK_UNKNOWN2
, voice
, 0);
473 snd_emu10k1x_ptr_write(emu
, PLAYBACK_DMA_ADDR
, voice
, runtime
->dma_addr
);
475 snd_emu10k1x_ptr_write(emu
, PLAYBACK_PERIOD_SIZE
, voice
, frames_to_bytes(runtime
, runtime
->period_size
)<<16);
480 /* trigger callback */
481 static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream
*substream
,
484 struct emu10k1x
*emu
= snd_pcm_substream_chip(substream
);
485 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
486 struct emu10k1x_pcm
*epcm
= runtime
->private_data
;
487 int channel
= epcm
->voice
->number
;
490 // snd_printk(KERN_INFO "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n", (int)emu, cmd, (int)substream->ops->pointer(substream));
493 case SNDRV_PCM_TRIGGER_START
:
494 if(runtime
->periods
== 2)
495 snd_emu10k1x_intr_enable(emu
, (INTE_CH_0_LOOP
| INTE_CH_0_HALF_LOOP
) << channel
);
497 snd_emu10k1x_intr_enable(emu
, INTE_CH_0_LOOP
<< channel
);
499 snd_emu10k1x_ptr_write(emu
, TRIGGER_CHANNEL
, 0, snd_emu10k1x_ptr_read(emu
, TRIGGER_CHANNEL
, 0)|(TRIGGER_CHANNEL_0
<<channel
));
501 case SNDRV_PCM_TRIGGER_STOP
:
503 snd_emu10k1x_intr_disable(emu
, (INTE_CH_0_LOOP
| INTE_CH_0_HALF_LOOP
) << channel
);
504 snd_emu10k1x_ptr_write(emu
, TRIGGER_CHANNEL
, 0, snd_emu10k1x_ptr_read(emu
, TRIGGER_CHANNEL
, 0) & ~(TRIGGER_CHANNEL_0
<<channel
));
513 /* pointer callback */
514 static snd_pcm_uframes_t
515 snd_emu10k1x_pcm_pointer(struct snd_pcm_substream
*substream
)
517 struct emu10k1x
*emu
= snd_pcm_substream_chip(substream
);
518 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
519 struct emu10k1x_pcm
*epcm
= runtime
->private_data
;
520 int channel
= epcm
->voice
->number
;
521 snd_pcm_uframes_t ptr
= 0, ptr1
= 0, ptr2
= 0,ptr3
= 0,ptr4
= 0;
526 ptr3
= snd_emu10k1x_ptr_read(emu
, PLAYBACK_LIST_PTR
, channel
);
527 ptr1
= snd_emu10k1x_ptr_read(emu
, PLAYBACK_POINTER
, channel
);
528 ptr4
= snd_emu10k1x_ptr_read(emu
, PLAYBACK_LIST_PTR
, channel
);
530 if(ptr4
== 0 && ptr1
== frames_to_bytes(runtime
, runtime
->buffer_size
))
534 ptr1
= snd_emu10k1x_ptr_read(emu
, PLAYBACK_POINTER
, channel
);
535 ptr2
= bytes_to_frames(runtime
, ptr1
);
536 ptr2
+= (ptr4
>> 3) * runtime
->period_size
;
539 if (ptr
>= runtime
->buffer_size
)
540 ptr
-= runtime
->buffer_size
;
546 static struct snd_pcm_ops snd_emu10k1x_playback_ops
= {
547 .open
= snd_emu10k1x_playback_open
,
548 .close
= snd_emu10k1x_playback_close
,
549 .ioctl
= snd_pcm_lib_ioctl
,
550 .hw_params
= snd_emu10k1x_pcm_hw_params
,
551 .hw_free
= snd_emu10k1x_pcm_hw_free
,
552 .prepare
= snd_emu10k1x_pcm_prepare
,
553 .trigger
= snd_emu10k1x_pcm_trigger
,
554 .pointer
= snd_emu10k1x_pcm_pointer
,
557 /* open_capture callback */
558 static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream
*substream
)
560 struct emu10k1x
*chip
= snd_pcm_substream_chip(substream
);
561 struct emu10k1x_pcm
*epcm
;
562 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
565 if ((err
= snd_pcm_hw_constraint_integer(runtime
, SNDRV_PCM_HW_PARAM_PERIODS
)) < 0)
567 if ((err
= snd_pcm_hw_constraint_step(runtime
, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 64)) < 0)
570 epcm
= kzalloc(sizeof(*epcm
), GFP_KERNEL
);
575 epcm
->substream
= substream
;
577 runtime
->private_data
= epcm
;
578 runtime
->private_free
= snd_emu10k1x_pcm_free_substream
;
580 runtime
->hw
= snd_emu10k1x_capture_hw
;
586 static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream
*substream
)
591 /* hw_params callback */
592 static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream
*substream
,
593 struct snd_pcm_hw_params
*hw_params
)
595 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
596 struct emu10k1x_pcm
*epcm
= runtime
->private_data
;
599 if (epcm
->emu
->capture_voice
.use
)
601 epcm
->voice
= &epcm
->emu
->capture_voice
;
602 epcm
->voice
->epcm
= epcm
;
603 epcm
->voice
->use
= 1;
606 return snd_pcm_lib_malloc_pages(substream
,
607 params_buffer_bytes(hw_params
));
610 /* hw_free callback */
611 static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream
*substream
)
613 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
615 struct emu10k1x_pcm
*epcm
;
617 if (runtime
->private_data
== NULL
)
619 epcm
= runtime
->private_data
;
622 epcm
->voice
->use
= 0;
623 epcm
->voice
->epcm
= NULL
;
627 return snd_pcm_lib_free_pages(substream
);
630 /* prepare capture callback */
631 static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream
*substream
)
633 struct emu10k1x
*emu
= snd_pcm_substream_chip(substream
);
634 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
636 snd_emu10k1x_ptr_write(emu
, CAPTURE_DMA_ADDR
, 0, runtime
->dma_addr
);
637 snd_emu10k1x_ptr_write(emu
, CAPTURE_BUFFER_SIZE
, 0, frames_to_bytes(runtime
, runtime
->buffer_size
)<<16); // buffer size in bytes
638 snd_emu10k1x_ptr_write(emu
, CAPTURE_POINTER
, 0, 0);
639 snd_emu10k1x_ptr_write(emu
, CAPTURE_UNKNOWN
, 0, 0);
644 /* trigger_capture callback */
645 static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream
*substream
,
648 struct emu10k1x
*emu
= snd_pcm_substream_chip(substream
);
649 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
650 struct emu10k1x_pcm
*epcm
= runtime
->private_data
;
654 case SNDRV_PCM_TRIGGER_START
:
655 snd_emu10k1x_intr_enable(emu
, INTE_CAP_0_LOOP
|
656 INTE_CAP_0_HALF_LOOP
);
657 snd_emu10k1x_ptr_write(emu
, TRIGGER_CHANNEL
, 0, snd_emu10k1x_ptr_read(emu
, TRIGGER_CHANNEL
, 0)|TRIGGER_CAPTURE
);
660 case SNDRV_PCM_TRIGGER_STOP
:
662 snd_emu10k1x_intr_disable(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
));
673 /* pointer_capture callback */
674 static snd_pcm_uframes_t
675 snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream
*substream
)
677 struct emu10k1x
*emu
= snd_pcm_substream_chip(substream
);
678 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
679 struct emu10k1x_pcm
*epcm
= runtime
->private_data
;
680 snd_pcm_uframes_t ptr
;
685 ptr
= bytes_to_frames(runtime
, snd_emu10k1x_ptr_read(emu
, CAPTURE_POINTER
, 0));
686 if (ptr
>= runtime
->buffer_size
)
687 ptr
-= runtime
->buffer_size
;
692 static struct snd_pcm_ops snd_emu10k1x_capture_ops
= {
693 .open
= snd_emu10k1x_pcm_open_capture
,
694 .close
= snd_emu10k1x_pcm_close_capture
,
695 .ioctl
= snd_pcm_lib_ioctl
,
696 .hw_params
= snd_emu10k1x_pcm_hw_params_capture
,
697 .hw_free
= snd_emu10k1x_pcm_hw_free_capture
,
698 .prepare
= snd_emu10k1x_pcm_prepare_capture
,
699 .trigger
= snd_emu10k1x_pcm_trigger_capture
,
700 .pointer
= snd_emu10k1x_pcm_pointer_capture
,
703 static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97
*ac97
,
706 struct emu10k1x
*emu
= ac97
->private_data
;
710 spin_lock_irqsave(&emu
->emu_lock
, flags
);
711 outb(reg
, emu
->port
+ AC97ADDRESS
);
712 val
= inw(emu
->port
+ AC97DATA
);
713 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
717 static void snd_emu10k1x_ac97_write(struct snd_ac97
*ac97
,
718 unsigned short reg
, unsigned short val
)
720 struct emu10k1x
*emu
= ac97
->private_data
;
723 spin_lock_irqsave(&emu
->emu_lock
, flags
);
724 outb(reg
, emu
->port
+ AC97ADDRESS
);
725 outw(val
, emu
->port
+ AC97DATA
);
726 spin_unlock_irqrestore(&emu
->emu_lock
, flags
);
729 static int snd_emu10k1x_ac97(struct emu10k1x
*chip
)
731 struct snd_ac97_bus
*pbus
;
732 struct snd_ac97_template ac97
;
734 static struct snd_ac97_bus_ops ops
= {
735 .write
= snd_emu10k1x_ac97_write
,
736 .read
= snd_emu10k1x_ac97_read
,
739 if ((err
= snd_ac97_bus(chip
->card
, 0, &ops
, NULL
, &pbus
)) < 0)
741 pbus
->no_vra
= 1; /* we don't need VRA */
743 memset(&ac97
, 0, sizeof(ac97
));
744 ac97
.private_data
= chip
;
745 ac97
.scaps
= AC97_SCAP_NO_SPDIF
;
746 return snd_ac97_mixer(pbus
, &ac97
, &chip
->ac97
);
749 static int snd_emu10k1x_free(struct emu10k1x
*chip
)
751 snd_emu10k1x_ptr_write(chip
, TRIGGER_CHANNEL
, 0, 0);
752 // disable interrupts
753 outl(0, chip
->port
+ INTE
);
755 outl(HCFG_LOCKSOUNDCACHE
, chip
->port
+ HCFG
);
757 /* release the irq */
759 free_irq(chip
->irq
, chip
);
761 // release the i/o port
762 release_and_free_resource(chip
->res_port
);
765 if (chip
->dma_buffer
.area
) {
766 snd_dma_free_pages(&chip
->dma_buffer
);
769 pci_disable_device(chip
->pci
);
776 static int snd_emu10k1x_dev_free(struct snd_device
*device
)
778 struct emu10k1x
*chip
= device
->device_data
;
779 return snd_emu10k1x_free(chip
);
782 static irqreturn_t
snd_emu10k1x_interrupt(int irq
, void *dev_id
)
786 struct emu10k1x
*chip
= dev_id
;
787 struct emu10k1x_voice
*pvoice
= chip
->voices
;
791 status
= inl(chip
->port
+ IPR
);
797 if (status
& (IPR_CAP_0_LOOP
| IPR_CAP_0_HALF_LOOP
)) {
798 struct emu10k1x_voice
*cap_voice
= &chip
->capture_voice
;
800 snd_emu10k1x_pcm_interrupt(chip
, cap_voice
);
802 snd_emu10k1x_intr_disable(chip
,
804 INTE_CAP_0_HALF_LOOP
);
807 mask
= IPR_CH_0_LOOP
|IPR_CH_0_HALF_LOOP
;
808 for (i
= 0; i
< 3; i
++) {
811 snd_emu10k1x_pcm_interrupt(chip
, pvoice
);
813 snd_emu10k1x_intr_disable(chip
, mask
);
819 if (status
& (IPR_MIDITRANSBUFEMPTY
|IPR_MIDIRECVBUFEMPTY
)) {
820 if (chip
->midi
.interrupt
)
821 chip
->midi
.interrupt(chip
, status
);
823 snd_emu10k1x_intr_disable(chip
, INTE_MIDITXENABLE
|INTE_MIDIRXENABLE
);
826 // acknowledge the interrupt if necessary
827 outl(status
, chip
->port
+ IPR
);
829 // snd_printk(KERN_INFO "interrupt %08x\n", status);
833 static const struct snd_pcm_chmap_elem surround_map
[] = {
835 .map
= { SNDRV_CHMAP_RL
, SNDRV_CHMAP_RR
} },
839 static const struct snd_pcm_chmap_elem clfe_map
[] = {
841 .map
= { SNDRV_CHMAP_FC
, SNDRV_CHMAP_LFE
} },
845 static int snd_emu10k1x_pcm(struct emu10k1x
*emu
, int device
, struct snd_pcm
**rpcm
)
848 const struct snd_pcm_chmap_elem
*map
= NULL
;
857 if ((err
= snd_pcm_new(emu
->card
, "emu10k1x", device
, 1, capture
, &pcm
)) < 0)
860 pcm
->private_data
= emu
;
864 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_emu10k1x_playback_ops
);
865 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_emu10k1x_capture_ops
);
869 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_emu10k1x_playback_ops
);
876 strcpy(pcm
->name
, "EMU10K1X Front");
877 map
= snd_pcm_std_chmaps
;
880 strcpy(pcm
->name
, "EMU10K1X Rear");
884 strcpy(pcm
->name
, "EMU10K1X Center/LFE");
890 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
891 snd_dma_pci_data(emu
->pci
),
894 err
= snd_pcm_add_chmap_ctls(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, map
, 2,
905 static int snd_emu10k1x_create(struct snd_card
*card
,
907 struct emu10k1x
**rchip
)
909 struct emu10k1x
*chip
;
912 static struct snd_device_ops ops
= {
913 .dev_free
= snd_emu10k1x_dev_free
,
918 if ((err
= pci_enable_device(pci
)) < 0)
920 if (pci_set_dma_mask(pci
, DMA_BIT_MASK(28)) < 0 ||
921 pci_set_consistent_dma_mask(pci
, DMA_BIT_MASK(28)) < 0) {
922 snd_printk(KERN_ERR
"error to set 28bit mask DMA\n");
923 pci_disable_device(pci
);
927 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
929 pci_disable_device(pci
);
937 spin_lock_init(&chip
->emu_lock
);
938 spin_lock_init(&chip
->voice_lock
);
940 chip
->port
= pci_resource_start(pci
, 0);
941 if ((chip
->res_port
= request_region(chip
->port
, 8,
942 "EMU10K1X")) == NULL
) {
943 snd_printk(KERN_ERR
"emu10k1x: cannot allocate the port 0x%lx\n", chip
->port
);
944 snd_emu10k1x_free(chip
);
948 if (request_irq(pci
->irq
, snd_emu10k1x_interrupt
,
949 IRQF_SHARED
, KBUILD_MODNAME
, chip
)) {
950 snd_printk(KERN_ERR
"emu10k1x: cannot grab irq %d\n", pci
->irq
);
951 snd_emu10k1x_free(chip
);
954 chip
->irq
= pci
->irq
;
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
);
963 /* read revision & serial */
964 chip
->revision
= pci
->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
);
970 outl(0, chip
->port
+ INTE
);
972 for(ch
= 0; ch
< 3; ch
++) {
973 chip
->voices
[ch
].emu
= chip
;
974 chip
->voices
[ch
].number
= ch
;
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)
986 * Emphasis = 0 (None)
987 * CP = 1 (Copyright unasserted)
988 * AN = 0 (Audio data)
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
);
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
1014 outl(HCFG_LOCKSOUNDCACHE
|HCFG_AUDIOENABLE
, chip
->port
+HCFG
);
1016 if ((err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
,
1018 snd_emu10k1x_free(chip
);
1025 static void snd_emu10k1x_proc_reg_read(struct snd_info_entry
*entry
,
1026 struct snd_info_buffer
*buffer
)
1028 struct emu10k1x
*emu
= entry
->private_data
;
1029 unsigned long value
,value1
,value2
;
1030 unsigned long flags
;
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
);
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
);
1048 snd_iprintf(buffer
, "%02X: %08lX\n", i
, value
);
1053 static void snd_emu10k1x_proc_reg_write(struct snd_info_entry
*entry
,
1054 struct snd_info_buffer
*buffer
)
1056 struct emu10k1x
*emu
= entry
->private_data
;
1058 unsigned int reg
, channel_id
, val
;
1060 while (!snd_info_get_line(buffer
, line
, sizeof(line
))) {
1061 if (sscanf(line
, "%x %x %x", ®
, &channel_id
, &val
) != 3)
1064 if (reg
< 0x49 && val
<= 0xffffffff && channel_id
<= 2)
1065 snd_emu10k1x_ptr_write(emu
, reg
, channel_id
, val
);
1069 static int snd_emu10k1x_proc_init(struct emu10k1x
*emu
)
1071 struct snd_info_entry
*entry
;
1073 if(! snd_card_proc_new(emu
->card
, "emu10k1x_regs", &entry
)) {
1074 snd_info_set_text_ops(entry
, emu
, snd_emu10k1x_proc_reg_read
);
1075 entry
->c
.text
.write
= snd_emu10k1x_proc_reg_write
;
1076 entry
->mode
|= S_IWUSR
;
1077 entry
->private_data
= emu
;
1083 #define snd_emu10k1x_shared_spdif_info snd_ctl_boolean_mono_info
1085 static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol
*kcontrol
,
1086 struct snd_ctl_elem_value
*ucontrol
)
1088 struct emu10k1x
*emu
= snd_kcontrol_chip(kcontrol
);
1090 ucontrol
->value
.integer
.value
[0] = (snd_emu10k1x_ptr_read(emu
, SPDIF_SELECT
, 0) == 0x700) ? 0 : 1;
1095 static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol
*kcontrol
,
1096 struct snd_ctl_elem_value
*ucontrol
)
1098 struct emu10k1x
*emu
= snd_kcontrol_chip(kcontrol
);
1102 val
= ucontrol
->value
.integer
.value
[0] ;
1105 // enable spdif output
1106 snd_emu10k1x_ptr_write(emu
, SPDIF_SELECT
, 0, 0x000);
1107 snd_emu10k1x_ptr_write(emu
, ROUTING
, 0, 0x700);
1108 snd_emu10k1x_gpio_write(emu
, 0x1000);
1110 // disable spdif output
1111 snd_emu10k1x_ptr_write(emu
, SPDIF_SELECT
, 0, 0x700);
1112 snd_emu10k1x_ptr_write(emu
, ROUTING
, 0, 0x1003F);
1113 snd_emu10k1x_gpio_write(emu
, 0x1080);
1118 static struct snd_kcontrol_new snd_emu10k1x_shared_spdif
=
1120 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
1121 .name
= "Analog/Digital Output Jack",
1122 .info
= snd_emu10k1x_shared_spdif_info
,
1123 .get
= snd_emu10k1x_shared_spdif_get
,
1124 .put
= snd_emu10k1x_shared_spdif_put
1127 static int snd_emu10k1x_spdif_info(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
1129 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
1134 static int snd_emu10k1x_spdif_get(struct snd_kcontrol
*kcontrol
,
1135 struct snd_ctl_elem_value
*ucontrol
)
1137 struct emu10k1x
*emu
= snd_kcontrol_chip(kcontrol
);
1138 unsigned int idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
1140 ucontrol
->value
.iec958
.status
[0] = (emu
->spdif_bits
[idx
] >> 0) & 0xff;
1141 ucontrol
->value
.iec958
.status
[1] = (emu
->spdif_bits
[idx
] >> 8) & 0xff;
1142 ucontrol
->value
.iec958
.status
[2] = (emu
->spdif_bits
[idx
] >> 16) & 0xff;
1143 ucontrol
->value
.iec958
.status
[3] = (emu
->spdif_bits
[idx
] >> 24) & 0xff;
1147 static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol
*kcontrol
,
1148 struct snd_ctl_elem_value
*ucontrol
)
1150 ucontrol
->value
.iec958
.status
[0] = 0xff;
1151 ucontrol
->value
.iec958
.status
[1] = 0xff;
1152 ucontrol
->value
.iec958
.status
[2] = 0xff;
1153 ucontrol
->value
.iec958
.status
[3] = 0xff;
1157 static int snd_emu10k1x_spdif_put(struct snd_kcontrol
*kcontrol
,
1158 struct snd_ctl_elem_value
*ucontrol
)
1160 struct emu10k1x
*emu
= snd_kcontrol_chip(kcontrol
);
1161 unsigned int idx
= snd_ctl_get_ioffidx(kcontrol
, &ucontrol
->id
);
1165 val
= (ucontrol
->value
.iec958
.status
[0] << 0) |
1166 (ucontrol
->value
.iec958
.status
[1] << 8) |
1167 (ucontrol
->value
.iec958
.status
[2] << 16) |
1168 (ucontrol
->value
.iec958
.status
[3] << 24);
1169 change
= val
!= emu
->spdif_bits
[idx
];
1171 snd_emu10k1x_ptr_write(emu
, SPCS0
+ idx
, 0, val
);
1172 emu
->spdif_bits
[idx
] = val
;
1177 static struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control
=
1179 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1180 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1181 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,MASK
),
1183 .info
= snd_emu10k1x_spdif_info
,
1184 .get
= snd_emu10k1x_spdif_get_mask
1187 static struct snd_kcontrol_new snd_emu10k1x_spdif_control
=
1189 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1190 .name
= SNDRV_CTL_NAME_IEC958("",PLAYBACK
,DEFAULT
),
1192 .info
= snd_emu10k1x_spdif_info
,
1193 .get
= snd_emu10k1x_spdif_get
,
1194 .put
= snd_emu10k1x_spdif_put
1197 static int snd_emu10k1x_mixer(struct emu10k1x
*emu
)
1200 struct snd_kcontrol
*kctl
;
1201 struct snd_card
*card
= emu
->card
;
1203 if ((kctl
= snd_ctl_new1(&snd_emu10k1x_spdif_mask_control
, emu
)) == NULL
)
1205 if ((err
= snd_ctl_add(card
, kctl
)))
1207 if ((kctl
= snd_ctl_new1(&snd_emu10k1x_shared_spdif
, emu
)) == NULL
)
1209 if ((err
= snd_ctl_add(card
, kctl
)))
1211 if ((kctl
= snd_ctl_new1(&snd_emu10k1x_spdif_control
, emu
)) == NULL
)
1213 if ((err
= snd_ctl_add(card
, kctl
)))
1219 #define EMU10K1X_MIDI_MODE_INPUT (1<<0)
1220 #define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
1222 static inline unsigned char mpu401_read(struct emu10k1x
*emu
, struct emu10k1x_midi
*mpu
, int idx
)
1224 return (unsigned char)snd_emu10k1x_ptr_read(emu
, mpu
->port
+ idx
, 0);
1227 static inline void mpu401_write(struct emu10k1x
*emu
, struct emu10k1x_midi
*mpu
, int data
, int idx
)
1229 snd_emu10k1x_ptr_write(emu
, mpu
->port
+ idx
, 0, data
);
1232 #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
1233 #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
1234 #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
1235 #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
1237 #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1238 #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
1240 #define MPU401_RESET 0xff
1241 #define MPU401_ENTER_UART 0x3f
1242 #define MPU401_ACK 0xfe
1244 static void mpu401_clear_rx(struct emu10k1x
*emu
, struct emu10k1x_midi
*mpu
)
1246 int timeout
= 100000;
1247 for (; timeout
> 0 && mpu401_input_avail(emu
, mpu
); timeout
--)
1248 mpu401_read_data(emu
, mpu
);
1249 #ifdef CONFIG_SND_DEBUG
1251 snd_printk(KERN_ERR
"cmd: clear rx timeout (status = 0x%x)\n", mpu401_read_stat(emu
, mpu
));
1259 static void do_emu10k1x_midi_interrupt(struct emu10k1x
*emu
,
1260 struct emu10k1x_midi
*midi
, unsigned int status
)
1264 if (midi
->rmidi
== NULL
) {
1265 snd_emu10k1x_intr_disable(emu
, midi
->tx_enable
| midi
->rx_enable
);
1269 spin_lock(&midi
->input_lock
);
1270 if ((status
& midi
->ipr_rx
) && mpu401_input_avail(emu
, midi
)) {
1271 if (!(midi
->midi_mode
& EMU10K1X_MIDI_MODE_INPUT
)) {
1272 mpu401_clear_rx(emu
, midi
);
1274 byte
= mpu401_read_data(emu
, midi
);
1275 if (midi
->substream_input
)
1276 snd_rawmidi_receive(midi
->substream_input
, &byte
, 1);
1279 spin_unlock(&midi
->input_lock
);
1281 spin_lock(&midi
->output_lock
);
1282 if ((status
& midi
->ipr_tx
) && mpu401_output_ready(emu
, midi
)) {
1283 if (midi
->substream_output
&&
1284 snd_rawmidi_transmit(midi
->substream_output
, &byte
, 1) == 1) {
1285 mpu401_write_data(emu
, midi
, byte
);
1287 snd_emu10k1x_intr_disable(emu
, midi
->tx_enable
);
1290 spin_unlock(&midi
->output_lock
);
1293 static void snd_emu10k1x_midi_interrupt(struct emu10k1x
*emu
, unsigned int status
)
1295 do_emu10k1x_midi_interrupt(emu
, &emu
->midi
, status
);
1298 static int snd_emu10k1x_midi_cmd(struct emu10k1x
* emu
,
1299 struct emu10k1x_midi
*midi
, unsigned char cmd
, int ack
)
1301 unsigned long flags
;
1304 spin_lock_irqsave(&midi
->input_lock
, flags
);
1305 mpu401_write_data(emu
, midi
, 0x00);
1306 /* mpu401_clear_rx(emu, midi); */
1308 mpu401_write_cmd(emu
, midi
, cmd
);
1312 while (!ok
&& timeout
-- > 0) {
1313 if (mpu401_input_avail(emu
, midi
)) {
1314 if (mpu401_read_data(emu
, midi
) == MPU401_ACK
)
1318 if (!ok
&& mpu401_read_data(emu
, midi
) == MPU401_ACK
)
1323 spin_unlock_irqrestore(&midi
->input_lock
, flags
);
1325 snd_printk(KERN_ERR
"midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1327 mpu401_read_stat(emu
, midi
),
1328 mpu401_read_data(emu
, midi
));
1334 static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream
*substream
)
1336 struct emu10k1x
*emu
;
1337 struct emu10k1x_midi
*midi
= substream
->rmidi
->private_data
;
1338 unsigned long flags
;
1341 if (snd_BUG_ON(!emu
))
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 if (snd_emu10k1x_midi_cmd(emu
, midi
, MPU401_RESET
, 1))
1350 if (snd_emu10k1x_midi_cmd(emu
, midi
, MPU401_ENTER_UART
, 1))
1353 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
1361 static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream
*substream
)
1363 struct emu10k1x
*emu
;
1364 struct emu10k1x_midi
*midi
= substream
->rmidi
->private_data
;
1365 unsigned long flags
;
1368 if (snd_BUG_ON(!emu
))
1370 spin_lock_irqsave(&midi
->open_lock
, flags
);
1371 midi
->midi_mode
|= EMU10K1X_MIDI_MODE_OUTPUT
;
1372 midi
->substream_output
= substream
;
1373 if (!(midi
->midi_mode
& EMU10K1X_MIDI_MODE_INPUT
)) {
1374 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
1375 if (snd_emu10k1x_midi_cmd(emu
, midi
, MPU401_RESET
, 1))
1377 if (snd_emu10k1x_midi_cmd(emu
, midi
, MPU401_ENTER_UART
, 1))
1380 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
1388 static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream
*substream
)
1390 struct emu10k1x
*emu
;
1391 struct emu10k1x_midi
*midi
= substream
->rmidi
->private_data
;
1392 unsigned long flags
;
1396 if (snd_BUG_ON(!emu
))
1398 spin_lock_irqsave(&midi
->open_lock
, flags
);
1399 snd_emu10k1x_intr_disable(emu
, midi
->rx_enable
);
1400 midi
->midi_mode
&= ~EMU10K1X_MIDI_MODE_INPUT
;
1401 midi
->substream_input
= NULL
;
1402 if (!(midi
->midi_mode
& EMU10K1X_MIDI_MODE_OUTPUT
)) {
1403 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
1404 err
= snd_emu10k1x_midi_cmd(emu
, midi
, MPU401_RESET
, 0);
1406 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
1411 static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream
*substream
)
1413 struct emu10k1x
*emu
;
1414 struct emu10k1x_midi
*midi
= substream
->rmidi
->private_data
;
1415 unsigned long flags
;
1419 if (snd_BUG_ON(!emu
))
1421 spin_lock_irqsave(&midi
->open_lock
, flags
);
1422 snd_emu10k1x_intr_disable(emu
, midi
->tx_enable
);
1423 midi
->midi_mode
&= ~EMU10K1X_MIDI_MODE_OUTPUT
;
1424 midi
->substream_output
= NULL
;
1425 if (!(midi
->midi_mode
& EMU10K1X_MIDI_MODE_INPUT
)) {
1426 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
1427 err
= snd_emu10k1x_midi_cmd(emu
, midi
, MPU401_RESET
, 0);
1429 spin_unlock_irqrestore(&midi
->open_lock
, flags
);
1434 static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1436 struct emu10k1x
*emu
;
1437 struct emu10k1x_midi
*midi
= substream
->rmidi
->private_data
;
1439 if (snd_BUG_ON(!emu
))
1443 snd_emu10k1x_intr_enable(emu
, midi
->rx_enable
);
1445 snd_emu10k1x_intr_disable(emu
, midi
->rx_enable
);
1448 static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
1450 struct emu10k1x
*emu
;
1451 struct emu10k1x_midi
*midi
= substream
->rmidi
->private_data
;
1452 unsigned long flags
;
1455 if (snd_BUG_ON(!emu
))
1462 /* try to send some amount of bytes here before interrupts */
1463 spin_lock_irqsave(&midi
->output_lock
, flags
);
1465 if (mpu401_output_ready(emu
, midi
)) {
1466 if (!(midi
->midi_mode
& EMU10K1X_MIDI_MODE_OUTPUT
) ||
1467 snd_rawmidi_transmit(substream
, &byte
, 1) != 1) {
1469 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
1472 mpu401_write_data(emu
, midi
, byte
);
1478 spin_unlock_irqrestore(&midi
->output_lock
, flags
);
1479 snd_emu10k1x_intr_enable(emu
, midi
->tx_enable
);
1481 snd_emu10k1x_intr_disable(emu
, midi
->tx_enable
);
1489 static struct snd_rawmidi_ops snd_emu10k1x_midi_output
=
1491 .open
= snd_emu10k1x_midi_output_open
,
1492 .close
= snd_emu10k1x_midi_output_close
,
1493 .trigger
= snd_emu10k1x_midi_output_trigger
,
1496 static struct snd_rawmidi_ops snd_emu10k1x_midi_input
=
1498 .open
= snd_emu10k1x_midi_input_open
,
1499 .close
= snd_emu10k1x_midi_input_close
,
1500 .trigger
= snd_emu10k1x_midi_input_trigger
,
1503 static void snd_emu10k1x_midi_free(struct snd_rawmidi
*rmidi
)
1505 struct emu10k1x_midi
*midi
= rmidi
->private_data
;
1506 midi
->interrupt
= NULL
;
1510 static int emu10k1x_midi_init(struct emu10k1x
*emu
,
1511 struct emu10k1x_midi
*midi
, int device
,
1514 struct snd_rawmidi
*rmidi
;
1517 if ((err
= snd_rawmidi_new(emu
->card
, name
, device
, 1, 1, &rmidi
)) < 0)
1520 spin_lock_init(&midi
->open_lock
);
1521 spin_lock_init(&midi
->input_lock
);
1522 spin_lock_init(&midi
->output_lock
);
1523 strcpy(rmidi
->name
, name
);
1524 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
, &snd_emu10k1x_midi_output
);
1525 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
, &snd_emu10k1x_midi_input
);
1526 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
|
1527 SNDRV_RAWMIDI_INFO_INPUT
|
1528 SNDRV_RAWMIDI_INFO_DUPLEX
;
1529 rmidi
->private_data
= midi
;
1530 rmidi
->private_free
= snd_emu10k1x_midi_free
;
1531 midi
->rmidi
= rmidi
;
1535 static int snd_emu10k1x_midi(struct emu10k1x
*emu
)
1537 struct emu10k1x_midi
*midi
= &emu
->midi
;
1540 if ((err
= emu10k1x_midi_init(emu
, midi
, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1543 midi
->tx_enable
= INTE_MIDITXENABLE
;
1544 midi
->rx_enable
= INTE_MIDIRXENABLE
;
1545 midi
->port
= MUDATA
;
1546 midi
->ipr_tx
= IPR_MIDITRANSBUFEMPTY
;
1547 midi
->ipr_rx
= IPR_MIDIRECVBUFEMPTY
;
1548 midi
->interrupt
= snd_emu10k1x_midi_interrupt
;
1552 static int snd_emu10k1x_probe(struct pci_dev
*pci
,
1553 const struct pci_device_id
*pci_id
)
1556 struct snd_card
*card
;
1557 struct emu10k1x
*chip
;
1560 if (dev
>= SNDRV_CARDS
)
1567 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
, 0, &card
);
1571 if ((err
= snd_emu10k1x_create(card
, pci
, &chip
)) < 0) {
1572 snd_card_free(card
);
1576 if ((err
= snd_emu10k1x_pcm(chip
, 0, NULL
)) < 0) {
1577 snd_card_free(card
);
1580 if ((err
= snd_emu10k1x_pcm(chip
, 1, NULL
)) < 0) {
1581 snd_card_free(card
);
1584 if ((err
= snd_emu10k1x_pcm(chip
, 2, NULL
)) < 0) {
1585 snd_card_free(card
);
1589 if ((err
= snd_emu10k1x_ac97(chip
)) < 0) {
1590 snd_card_free(card
);
1594 if ((err
= snd_emu10k1x_mixer(chip
)) < 0) {
1595 snd_card_free(card
);
1599 if ((err
= snd_emu10k1x_midi(chip
)) < 0) {
1600 snd_card_free(card
);
1604 snd_emu10k1x_proc_init(chip
);
1606 strcpy(card
->driver
, "EMU10K1X");
1607 strcpy(card
->shortname
, "Dell Sound Blaster Live!");
1608 sprintf(card
->longname
, "%s at 0x%lx irq %i",
1609 card
->shortname
, chip
->port
, chip
->irq
);
1611 snd_card_set_dev(card
, &pci
->dev
);
1613 if ((err
= snd_card_register(card
)) < 0) {
1614 snd_card_free(card
);
1618 pci_set_drvdata(pci
, card
);
1623 static void snd_emu10k1x_remove(struct pci_dev
*pci
)
1625 snd_card_free(pci_get_drvdata(pci
));
1629 static DEFINE_PCI_DEVICE_TABLE(snd_emu10k1x_ids
) = {
1630 { PCI_VDEVICE(CREATIVE
, 0x0006), 0 }, /* Dell OEM version (EMU10K1) */
1633 MODULE_DEVICE_TABLE(pci
, snd_emu10k1x_ids
);
1635 // pci_driver definition
1636 static struct pci_driver emu10k1x_driver
= {
1637 .name
= KBUILD_MODNAME
,
1638 .id_table
= snd_emu10k1x_ids
,
1639 .probe
= snd_emu10k1x_probe
,
1640 .remove
= snd_emu10k1x_remove
,
1643 module_pci_driver(emu10k1x_driver
);