1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
4 * Driver EMU10K1X chips
6 * Parts of this code were adapted from audigyls.c driver which is
7 * Copyright (c) by James Courtier-Dutton <James@superbug.demon.co.uk>
14 * Chips (SB0200 model):
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/pci.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <sound/core.h>
25 #include <sound/initval.h>
26 #include <sound/pcm.h>
27 #include <sound/ac97_codec.h>
28 #include <sound/info.h>
29 #include <sound/rawmidi.h>
31 MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
32 MODULE_DESCRIPTION("EMU10K1X");
33 MODULE_LICENSE("GPL");
34 MODULE_SUPPORTED_DEVICE("{{Dell Creative Labs,SB Live!}");
36 // module parameters (see "Module Parameters")
37 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
38 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
39 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
41 module_param_array(index, int, NULL, 0444);
42 MODULE_PARM_DESC(index, "Index value for the EMU10K1X soundcard.");
43 module_param_array(id, charp, NULL, 0444);
44 MODULE_PARM_DESC(id, "ID string for the EMU10K1X soundcard.");
45 module_param_array(enable, bool, NULL, 0444);
46 MODULE_PARM_DESC(enable, "Enable the EMU10K1X soundcard.");
49 // some definitions were borrowed from emu10k1 driver as they seem to be the same
50 /************************************************************************************************/
51 /* PCI function 0 registers, address = <val> + PCIBASE0 */
52 /************************************************************************************************/
54 #define PTR 0x00 /* Indexed register set pointer register */
55 /* NOTE: The CHANNELNUM and ADDRESS words can */
56 /* be modified independently of each other. */
58 #define DATA 0x04 /* Indexed register set data register */
60 #define IPR 0x08 /* Global interrupt pending register */
61 /* Clear pending interrupts by writing a 1 to */
62 /* the relevant bits and zero to the other bits */
63 #define IPR_MIDITRANSBUFEMPTY 0x00000001 /* MIDI UART transmit buffer empty */
64 #define IPR_MIDIRECVBUFEMPTY 0x00000002 /* MIDI UART receive buffer empty */
65 #define IPR_CH_0_LOOP 0x00000800 /* Channel 0 loop */
66 #define IPR_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
67 #define IPR_CAP_0_LOOP 0x00080000 /* Channel capture loop */
68 #define IPR_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
70 #define INTE 0x0c /* Interrupt enable register */
71 #define INTE_MIDITXENABLE 0x00000001 /* Enable MIDI transmit-buffer-empty interrupts */
72 #define INTE_MIDIRXENABLE 0x00000002 /* Enable MIDI receive-buffer-empty interrupts */
73 #define INTE_CH_0_LOOP 0x00000800 /* Channel 0 loop */
74 #define INTE_CH_0_HALF_LOOP 0x00000100 /* Channel 0 half loop */
75 #define INTE_CAP_0_LOOP 0x00080000 /* Channel capture loop */
76 #define INTE_CAP_0_HALF_LOOP 0x00010000 /* Channel capture half loop */
78 #define HCFG 0x14 /* Hardware config register */
80 #define HCFG_LOCKSOUNDCACHE 0x00000008 /* 1 = Cancel bustmaster accesses to soundcache */
81 /* NOTE: This should generally never be used. */
82 #define HCFG_AUDIOENABLE 0x00000001 /* 0 = CODECs transmit zero-valued samples */
83 /* Should be set to 1 when the EMU10K1 is */
84 /* completely initialized. */
85 #define GPIO 0x18 /* Defaults: 00001080-Analog, 00001000-SPDIF. */
88 #define AC97DATA 0x1c /* AC97 register set data register (16 bit) */
90 #define AC97ADDRESS 0x1e /* AC97 register set address register (8 bit) */
92 /********************************************************************************************************/
93 /* Emu10k1x pointer-offset register set, accessed through the PTR and DATA registers */
94 /********************************************************************************************************/
95 #define PLAYBACK_LIST_ADDR 0x00 /* Base DMA address of a list of pointers to each period/size */
96 /* One list entry: 4 bytes for DMA address,
97 * 4 bytes for period_size << 16.
98 * One list entry is 8 bytes long.
99 * One list entry for each period in the buffer.
101 #define PLAYBACK_LIST_SIZE 0x01 /* Size of list in bytes << 16. E.g. 8 periods -> 0x00380000 */
102 #define PLAYBACK_LIST_PTR 0x02 /* Pointer to the current period being played */
103 #define PLAYBACK_DMA_ADDR 0x04 /* Playback DMA address */
104 #define PLAYBACK_PERIOD_SIZE 0x05 /* Playback period size */
105 #define PLAYBACK_POINTER 0x06 /* Playback period pointer. Sample currently in DAC */
106 #define PLAYBACK_UNKNOWN1 0x07
107 #define PLAYBACK_UNKNOWN2 0x08
109 /* Only one capture channel supported */
110 #define CAPTURE_DMA_ADDR 0x10 /* Capture DMA address */
111 #define CAPTURE_BUFFER_SIZE 0x11 /* Capture buffer size */
112 #define CAPTURE_POINTER 0x12 /* Capture buffer pointer. Sample currently in ADC */
113 #define CAPTURE_UNKNOWN 0x13
115 /* From 0x20 - 0x3f, last samples played on each channel */
117 #define TRIGGER_CHANNEL 0x40 /* Trigger channel playback */
118 #define TRIGGER_CHANNEL_0 0x00000001 /* Trigger channel 0 */
119 #define TRIGGER_CHANNEL_1 0x00000002 /* Trigger channel 1 */
120 #define TRIGGER_CHANNEL_2 0x00000004 /* Trigger channel 2 */
121 #define TRIGGER_CAPTURE 0x00000100 /* Trigger capture channel */
123 #define ROUTING 0x41 /* Setup sound routing ? */
124 #define ROUTING_FRONT_LEFT 0x00000001
125 #define ROUTING_FRONT_RIGHT 0x00000002
126 #define ROUTING_REAR_LEFT 0x00000004
127 #define ROUTING_REAR_RIGHT 0x00000008
128 #define ROUTING_CENTER_LFE 0x00010000
130 #define SPCS0 0x42 /* SPDIF output Channel Status 0 register */
132 #define SPCS1 0x43 /* SPDIF output Channel Status 1 register */
134 #define SPCS2 0x44 /* SPDIF output Channel Status 2 register */
136 #define SPCS_CLKACCYMASK 0x30000000 /* Clock accuracy */
137 #define SPCS_CLKACCY_1000PPM 0x00000000 /* 1000 parts per million */
138 #define SPCS_CLKACCY_50PPM 0x10000000 /* 50 parts per million */
139 #define SPCS_CLKACCY_VARIABLE 0x20000000 /* Variable accuracy */
140 #define SPCS_SAMPLERATEMASK 0x0f000000 /* Sample rate */
141 #define SPCS_SAMPLERATE_44 0x00000000 /* 44.1kHz sample rate */
142 #define SPCS_SAMPLERATE_48 0x02000000 /* 48kHz sample rate */
143 #define SPCS_SAMPLERATE_32 0x03000000 /* 32kHz sample rate */
144 #define SPCS_CHANNELNUMMASK 0x00f00000 /* Channel number */
145 #define SPCS_CHANNELNUM_UNSPEC 0x00000000 /* Unspecified channel number */
146 #define SPCS_CHANNELNUM_LEFT 0x00100000 /* Left channel */
147 #define SPCS_CHANNELNUM_RIGHT 0x00200000 /* Right channel */
148 #define SPCS_SOURCENUMMASK 0x000f0000 /* Source number */
149 #define SPCS_SOURCENUM_UNSPEC 0x00000000 /* Unspecified source number */
150 #define SPCS_GENERATIONSTATUS 0x00008000 /* Originality flag (see IEC-958 spec) */
151 #define SPCS_CATEGORYCODEMASK 0x00007f00 /* Category code (see IEC-958 spec) */
152 #define SPCS_MODEMASK 0x000000c0 /* Mode (see IEC-958 spec) */
153 #define SPCS_EMPHASISMASK 0x00000038 /* Emphasis */
154 #define SPCS_EMPHASIS_NONE 0x00000000 /* No emphasis */
155 #define SPCS_EMPHASIS_50_15 0x00000008 /* 50/15 usec 2 channel */
156 #define SPCS_COPYRIGHT 0x00000004 /* Copyright asserted flag -- do not modify */
157 #define SPCS_NOTAUDIODATA 0x00000002 /* 0 = Digital audio, 1 = not audio */
158 #define SPCS_PROFESSIONAL 0x00000001 /* 0 = Consumer (IEC-958), 1 = pro (AES3-1992) */
160 #define SPDIF_SELECT 0x45 /* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
162 /* This is the MPU port on the card */
167 /* From 0x50 - 0x5f, last samples captured */
170 * The hardware has 3 channels for playback and 1 for capture.
171 * - channel 0 is the front channel
172 * - channel 1 is the rear channel
173 * - channel 2 is the center/lfe channel
174 * Volume is controlled by the AC97 for the front and rear channels by
175 * the PCM Playback Volume, Sigmatel Surround Playback Volume and
176 * Surround Playback Volume. The Sigmatel 4-Speaker Stereo switch affects
177 * the front/rear channel mixing in the REAR OUT jack. When using the
178 * 4-Speaker Stereo, both front and rear channels will be mixed in the
180 * The center/lfe channel has no volume control and cannot be muted during
184 struct emu10k1x_voice {
185 struct emu10k1x *emu;
189 struct emu10k1x_pcm *epcm;
192 struct emu10k1x_pcm {
193 struct emu10k1x *emu;
194 struct snd_pcm_substream *substream;
195 struct emu10k1x_voice *voice;
196 unsigned short running;
199 struct emu10k1x_midi {
200 struct emu10k1x *emu;
201 struct snd_rawmidi *rmidi;
202 struct snd_rawmidi_substream *substream_input;
203 struct snd_rawmidi_substream *substream_output;
204 unsigned int midi_mode;
205 spinlock_t input_lock;
206 spinlock_t output_lock;
207 spinlock_t open_lock;
208 int tx_enable, rx_enable;
211 void (*interrupt)(struct emu10k1x *emu, unsigned int status);
214 // definition of the chip-specific record
216 struct snd_card *card;
220 struct resource *res_port;
223 unsigned char revision; /* chip revision */
224 unsigned int serial; /* serial number */
225 unsigned short model; /* subsystem id */
228 spinlock_t voice_lock;
230 struct snd_ac97 *ac97;
233 struct emu10k1x_voice voices[3];
234 struct emu10k1x_voice capture_voice;
235 u32 spdif_bits[3]; // SPDIF out setup
237 struct snd_dma_buffer dma_buffer;
239 struct emu10k1x_midi midi;
242 /* hardware definition */
243 static const struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
244 .info = (SNDRV_PCM_INFO_MMAP |
245 SNDRV_PCM_INFO_INTERLEAVED |
246 SNDRV_PCM_INFO_BLOCK_TRANSFER |
247 SNDRV_PCM_INFO_MMAP_VALID),
248 .formats = SNDRV_PCM_FMTBIT_S16_LE,
249 .rates = SNDRV_PCM_RATE_48000,
254 .buffer_bytes_max = (32*1024),
255 .period_bytes_min = 64,
256 .period_bytes_max = (16*1024),
262 static const struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
263 .info = (SNDRV_PCM_INFO_MMAP |
264 SNDRV_PCM_INFO_INTERLEAVED |
265 SNDRV_PCM_INFO_BLOCK_TRANSFER |
266 SNDRV_PCM_INFO_MMAP_VALID),
267 .formats = SNDRV_PCM_FMTBIT_S16_LE,
268 .rates = SNDRV_PCM_RATE_48000,
273 .buffer_bytes_max = (32*1024),
274 .period_bytes_min = 64,
275 .period_bytes_max = (16*1024),
281 static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu,
286 unsigned int regptr, val;
288 regptr = (reg << 16) | chn;
290 spin_lock_irqsave(&emu->emu_lock, flags);
291 outl(regptr, emu->port + PTR);
292 val = inl(emu->port + DATA);
293 spin_unlock_irqrestore(&emu->emu_lock, flags);
297 static void snd_emu10k1x_ptr_write(struct emu10k1x *emu,
305 regptr = (reg << 16) | chn;
307 spin_lock_irqsave(&emu->emu_lock, flags);
308 outl(regptr, emu->port + PTR);
309 outl(data, emu->port + DATA);
310 spin_unlock_irqrestore(&emu->emu_lock, flags);
313 static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
316 unsigned int intr_enable;
318 spin_lock_irqsave(&emu->emu_lock, flags);
319 intr_enable = inl(emu->port + INTE) | intrenb;
320 outl(intr_enable, emu->port + INTE);
321 spin_unlock_irqrestore(&emu->emu_lock, flags);
324 static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
327 unsigned int intr_enable;
329 spin_lock_irqsave(&emu->emu_lock, flags);
330 intr_enable = inl(emu->port + INTE) & ~intrenb;
331 outl(intr_enable, emu->port + INTE);
332 spin_unlock_irqrestore(&emu->emu_lock, flags);
335 static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
339 spin_lock_irqsave(&emu->emu_lock, flags);
340 outl(value, emu->port + GPIO);
341 spin_unlock_irqrestore(&emu->emu_lock, flags);
344 static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
346 kfree(runtime->private_data);
349 static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
351 struct emu10k1x_pcm *epcm;
353 if ((epcm = voice->epcm) == NULL)
355 if (epcm->substream == NULL)
358 dev_info(emu->card->dev,
359 "IRQ: position = 0x%x, period = 0x%x, size = 0x%x\n",
360 epcm->substream->ops->pointer(epcm->substream),
361 snd_pcm_lib_period_bytes(epcm->substream),
362 snd_pcm_lib_buffer_bytes(epcm->substream));
364 snd_pcm_period_elapsed(epcm->substream);
368 static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
370 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
371 struct emu10k1x_pcm *epcm;
372 struct snd_pcm_runtime *runtime = substream->runtime;
375 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0) {
378 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
381 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
385 epcm->substream = substream;
387 runtime->private_data = epcm;
388 runtime->private_free = snd_emu10k1x_pcm_free_substream;
390 runtime->hw = snd_emu10k1x_playback_hw;
396 static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
401 /* hw_params callback */
402 static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
403 struct snd_pcm_hw_params *hw_params)
405 struct snd_pcm_runtime *runtime = substream->runtime;
406 struct emu10k1x_pcm *epcm = runtime->private_data;
409 epcm->voice = &epcm->emu->voices[substream->pcm->device];
410 epcm->voice->use = 1;
411 epcm->voice->epcm = epcm;
414 return snd_pcm_lib_malloc_pages(substream,
415 params_buffer_bytes(hw_params));
418 /* hw_free callback */
419 static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
421 struct snd_pcm_runtime *runtime = substream->runtime;
422 struct emu10k1x_pcm *epcm;
424 if (runtime->private_data == NULL)
427 epcm = runtime->private_data;
430 epcm->voice->use = 0;
431 epcm->voice->epcm = NULL;
435 return snd_pcm_lib_free_pages(substream);
438 /* prepare callback */
439 static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
441 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
442 struct snd_pcm_runtime *runtime = substream->runtime;
443 struct emu10k1x_pcm *epcm = runtime->private_data;
444 int voice = epcm->voice->number;
445 u32 *table_base = (u32 *)(emu->dma_buffer.area+1024*voice);
446 u32 period_size_bytes = frames_to_bytes(runtime, runtime->period_size);
449 for(i = 0; i < runtime->periods; i++) {
450 *table_base++=runtime->dma_addr+(i*period_size_bytes);
451 *table_base++=period_size_bytes<<16;
454 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_ADDR, voice, emu->dma_buffer.addr+1024*voice);
455 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_SIZE, voice, (runtime->periods - 1) << 19);
456 snd_emu10k1x_ptr_write(emu, PLAYBACK_LIST_PTR, voice, 0);
457 snd_emu10k1x_ptr_write(emu, PLAYBACK_POINTER, voice, 0);
458 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN1, voice, 0);
459 snd_emu10k1x_ptr_write(emu, PLAYBACK_UNKNOWN2, voice, 0);
460 snd_emu10k1x_ptr_write(emu, PLAYBACK_DMA_ADDR, voice, runtime->dma_addr);
462 snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
467 /* trigger callback */
468 static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
471 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
472 struct snd_pcm_runtime *runtime = substream->runtime;
473 struct emu10k1x_pcm *epcm = runtime->private_data;
474 int channel = epcm->voice->number;
478 dev_dbg(emu->card->dev,
479 "trigger - emu10k1x = 0x%x, cmd = %i, pointer = %d\n",
480 (int)emu, cmd, (int)substream->ops->pointer(substream));
484 case SNDRV_PCM_TRIGGER_START:
485 if(runtime->periods == 2)
486 snd_emu10k1x_intr_enable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
488 snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
490 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
492 case SNDRV_PCM_TRIGGER_STOP:
494 snd_emu10k1x_intr_disable(emu, (INTE_CH_0_LOOP | INTE_CH_0_HALF_LOOP) << channel);
495 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CHANNEL_0<<channel));
504 /* pointer callback */
505 static snd_pcm_uframes_t
506 snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
508 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
509 struct snd_pcm_runtime *runtime = substream->runtime;
510 struct emu10k1x_pcm *epcm = runtime->private_data;
511 int channel = epcm->voice->number;
512 snd_pcm_uframes_t ptr = 0, ptr1 = 0, ptr2= 0,ptr3 = 0,ptr4 = 0;
517 ptr3 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
518 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
519 ptr4 = snd_emu10k1x_ptr_read(emu, PLAYBACK_LIST_PTR, channel);
521 if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
525 ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
526 ptr2 = bytes_to_frames(runtime, ptr1);
527 ptr2 += (ptr4 >> 3) * runtime->period_size;
530 if (ptr >= runtime->buffer_size)
531 ptr -= runtime->buffer_size;
537 static const struct snd_pcm_ops snd_emu10k1x_playback_ops = {
538 .open = snd_emu10k1x_playback_open,
539 .close = snd_emu10k1x_playback_close,
540 .ioctl = snd_pcm_lib_ioctl,
541 .hw_params = snd_emu10k1x_pcm_hw_params,
542 .hw_free = snd_emu10k1x_pcm_hw_free,
543 .prepare = snd_emu10k1x_pcm_prepare,
544 .trigger = snd_emu10k1x_pcm_trigger,
545 .pointer = snd_emu10k1x_pcm_pointer,
548 /* open_capture callback */
549 static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
551 struct emu10k1x *chip = snd_pcm_substream_chip(substream);
552 struct emu10k1x_pcm *epcm;
553 struct snd_pcm_runtime *runtime = substream->runtime;
556 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
558 if ((err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64)) < 0)
561 epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
566 epcm->substream = substream;
568 runtime->private_data = epcm;
569 runtime->private_free = snd_emu10k1x_pcm_free_substream;
571 runtime->hw = snd_emu10k1x_capture_hw;
577 static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
582 /* hw_params callback */
583 static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
584 struct snd_pcm_hw_params *hw_params)
586 struct snd_pcm_runtime *runtime = substream->runtime;
587 struct emu10k1x_pcm *epcm = runtime->private_data;
590 if (epcm->emu->capture_voice.use)
592 epcm->voice = &epcm->emu->capture_voice;
593 epcm->voice->epcm = epcm;
594 epcm->voice->use = 1;
597 return snd_pcm_lib_malloc_pages(substream,
598 params_buffer_bytes(hw_params));
601 /* hw_free callback */
602 static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
604 struct snd_pcm_runtime *runtime = substream->runtime;
606 struct emu10k1x_pcm *epcm;
608 if (runtime->private_data == NULL)
610 epcm = runtime->private_data;
613 epcm->voice->use = 0;
614 epcm->voice->epcm = NULL;
618 return snd_pcm_lib_free_pages(substream);
621 /* prepare capture callback */
622 static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
624 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
625 struct snd_pcm_runtime *runtime = substream->runtime;
627 snd_emu10k1x_ptr_write(emu, CAPTURE_DMA_ADDR, 0, runtime->dma_addr);
628 snd_emu10k1x_ptr_write(emu, CAPTURE_BUFFER_SIZE, 0, frames_to_bytes(runtime, runtime->buffer_size)<<16); // buffer size in bytes
629 snd_emu10k1x_ptr_write(emu, CAPTURE_POINTER, 0, 0);
630 snd_emu10k1x_ptr_write(emu, CAPTURE_UNKNOWN, 0, 0);
635 /* trigger_capture callback */
636 static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
639 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
640 struct snd_pcm_runtime *runtime = substream->runtime;
641 struct emu10k1x_pcm *epcm = runtime->private_data;
645 case SNDRV_PCM_TRIGGER_START:
646 snd_emu10k1x_intr_enable(emu, INTE_CAP_0_LOOP |
647 INTE_CAP_0_HALF_LOOP);
648 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|TRIGGER_CAPTURE);
651 case SNDRV_PCM_TRIGGER_STOP:
653 snd_emu10k1x_intr_disable(emu, INTE_CAP_0_LOOP |
654 INTE_CAP_0_HALF_LOOP);
655 snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0) & ~(TRIGGER_CAPTURE));
664 /* pointer_capture callback */
665 static snd_pcm_uframes_t
666 snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
668 struct emu10k1x *emu = snd_pcm_substream_chip(substream);
669 struct snd_pcm_runtime *runtime = substream->runtime;
670 struct emu10k1x_pcm *epcm = runtime->private_data;
671 snd_pcm_uframes_t ptr;
676 ptr = bytes_to_frames(runtime, snd_emu10k1x_ptr_read(emu, CAPTURE_POINTER, 0));
677 if (ptr >= runtime->buffer_size)
678 ptr -= runtime->buffer_size;
683 static const struct snd_pcm_ops snd_emu10k1x_capture_ops = {
684 .open = snd_emu10k1x_pcm_open_capture,
685 .close = snd_emu10k1x_pcm_close_capture,
686 .ioctl = snd_pcm_lib_ioctl,
687 .hw_params = snd_emu10k1x_pcm_hw_params_capture,
688 .hw_free = snd_emu10k1x_pcm_hw_free_capture,
689 .prepare = snd_emu10k1x_pcm_prepare_capture,
690 .trigger = snd_emu10k1x_pcm_trigger_capture,
691 .pointer = snd_emu10k1x_pcm_pointer_capture,
694 static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
697 struct emu10k1x *emu = ac97->private_data;
701 spin_lock_irqsave(&emu->emu_lock, flags);
702 outb(reg, emu->port + AC97ADDRESS);
703 val = inw(emu->port + AC97DATA);
704 spin_unlock_irqrestore(&emu->emu_lock, flags);
708 static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
709 unsigned short reg, unsigned short val)
711 struct emu10k1x *emu = ac97->private_data;
714 spin_lock_irqsave(&emu->emu_lock, flags);
715 outb(reg, emu->port + AC97ADDRESS);
716 outw(val, emu->port + AC97DATA);
717 spin_unlock_irqrestore(&emu->emu_lock, flags);
720 static int snd_emu10k1x_ac97(struct emu10k1x *chip)
722 struct snd_ac97_bus *pbus;
723 struct snd_ac97_template ac97;
725 static struct snd_ac97_bus_ops ops = {
726 .write = snd_emu10k1x_ac97_write,
727 .read = snd_emu10k1x_ac97_read,
730 if ((err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus)) < 0)
732 pbus->no_vra = 1; /* we don't need VRA */
734 memset(&ac97, 0, sizeof(ac97));
735 ac97.private_data = chip;
736 ac97.scaps = AC97_SCAP_NO_SPDIF;
737 return snd_ac97_mixer(pbus, &ac97, &chip->ac97);
740 static int snd_emu10k1x_free(struct emu10k1x *chip)
742 snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
743 // disable interrupts
744 outl(0, chip->port + INTE);
746 outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
748 /* release the irq */
750 free_irq(chip->irq, chip);
752 // release the i/o port
753 release_and_free_resource(chip->res_port);
756 if (chip->dma_buffer.area) {
757 snd_dma_free_pages(&chip->dma_buffer);
760 pci_disable_device(chip->pci);
767 static int snd_emu10k1x_dev_free(struct snd_device *device)
769 struct emu10k1x *chip = device->device_data;
770 return snd_emu10k1x_free(chip);
773 static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id)
777 struct emu10k1x *chip = dev_id;
778 struct emu10k1x_voice *pvoice = chip->voices;
782 status = inl(chip->port + IPR);
788 if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
789 struct emu10k1x_voice *cap_voice = &chip->capture_voice;
791 snd_emu10k1x_pcm_interrupt(chip, cap_voice);
793 snd_emu10k1x_intr_disable(chip,
795 INTE_CAP_0_HALF_LOOP);
798 mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
799 for (i = 0; i < 3; i++) {
802 snd_emu10k1x_pcm_interrupt(chip, pvoice);
804 snd_emu10k1x_intr_disable(chip, mask);
810 if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
811 if (chip->midi.interrupt)
812 chip->midi.interrupt(chip, status);
814 snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
817 // acknowledge the interrupt if necessary
818 outl(status, chip->port + IPR);
820 /* dev_dbg(chip->card->dev, "interrupt %08x\n", status); */
824 static const struct snd_pcm_chmap_elem surround_map[] = {
826 .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
830 static const struct snd_pcm_chmap_elem clfe_map[] = {
832 .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
836 static int snd_emu10k1x_pcm(struct emu10k1x *emu, int device)
839 const struct snd_pcm_chmap_elem *map = NULL;
846 if ((err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm)) < 0)
849 pcm->private_data = emu;
853 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
854 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
858 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
865 strcpy(pcm->name, "EMU10K1X Front");
866 map = snd_pcm_std_chmaps;
869 strcpy(pcm->name, "EMU10K1X Rear");
873 strcpy(pcm->name, "EMU10K1X Center/LFE");
879 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
880 snd_dma_pci_data(emu->pci),
883 return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, 2,
887 static int snd_emu10k1x_create(struct snd_card *card,
889 struct emu10k1x **rchip)
891 struct emu10k1x *chip;
894 static struct snd_device_ops ops = {
895 .dev_free = snd_emu10k1x_dev_free,
900 if ((err = pci_enable_device(pci)) < 0)
902 if (pci_set_dma_mask(pci, DMA_BIT_MASK(28)) < 0 ||
903 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(28)) < 0) {
904 dev_err(card->dev, "error to set 28bit mask DMA\n");
905 pci_disable_device(pci);
909 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
911 pci_disable_device(pci);
919 spin_lock_init(&chip->emu_lock);
920 spin_lock_init(&chip->voice_lock);
922 chip->port = pci_resource_start(pci, 0);
923 if ((chip->res_port = request_region(chip->port, 8,
924 "EMU10K1X")) == NULL) {
925 dev_err(card->dev, "cannot allocate the port 0x%lx\n",
927 snd_emu10k1x_free(chip);
931 if (request_irq(pci->irq, snd_emu10k1x_interrupt,
932 IRQF_SHARED, KBUILD_MODNAME, chip)) {
933 dev_err(card->dev, "cannot grab irq %d\n", pci->irq);
934 snd_emu10k1x_free(chip);
937 chip->irq = pci->irq;
939 if(snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
940 4 * 1024, &chip->dma_buffer) < 0) {
941 snd_emu10k1x_free(chip);
946 /* read revision & serial */
947 chip->revision = pci->revision;
948 pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
949 pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
950 dev_info(card->dev, "Model %04x Rev %08x Serial %08x\n", chip->model,
951 chip->revision, chip->serial);
953 outl(0, chip->port + INTE);
955 for(ch = 0; ch < 3; ch++) {
956 chip->voices[ch].emu = chip;
957 chip->voices[ch].number = ch;
961 * Init to 0x02109204 :
962 * Clock accuracy = 0 (1000ppm)
963 * Sample Rate = 2 (48kHz)
964 * Audio Channel = 1 (Left of 2)
965 * Source Number = 0 (Unspecified)
966 * Generation Status = 1 (Original for Cat Code 12)
967 * Cat Code = 12 (Digital Signal Mixer)
969 * Emphasis = 0 (None)
970 * CP = 1 (Copyright unasserted)
971 * AN = 0 (Audio data)
974 snd_emu10k1x_ptr_write(chip, SPCS0, 0,
975 chip->spdif_bits[0] =
976 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
977 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
978 SPCS_GENERATIONSTATUS | 0x00001200 |
979 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
980 snd_emu10k1x_ptr_write(chip, SPCS1, 0,
981 chip->spdif_bits[1] =
982 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
983 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
984 SPCS_GENERATIONSTATUS | 0x00001200 |
985 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
986 snd_emu10k1x_ptr_write(chip, SPCS2, 0,
987 chip->spdif_bits[2] =
988 SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
989 SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
990 SPCS_GENERATIONSTATUS | 0x00001200 |
991 0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
993 snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
994 snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
995 snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
997 outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
999 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
1001 snd_emu10k1x_free(chip);
1008 static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
1009 struct snd_info_buffer *buffer)
1011 struct emu10k1x *emu = entry->private_data;
1012 unsigned long value,value1,value2;
1013 unsigned long flags;
1016 snd_iprintf(buffer, "Registers:\n\n");
1017 for(i = 0; i < 0x20; i+=4) {
1018 spin_lock_irqsave(&emu->emu_lock, flags);
1019 value = inl(emu->port + i);
1020 spin_unlock_irqrestore(&emu->emu_lock, flags);
1021 snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
1023 snd_iprintf(buffer, "\nRegisters\n\n");
1024 for(i = 0; i <= 0x48; i++) {
1025 value = snd_emu10k1x_ptr_read(emu, i, 0);
1026 if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
1027 value1 = snd_emu10k1x_ptr_read(emu, i, 1);
1028 value2 = snd_emu10k1x_ptr_read(emu, i, 2);
1029 snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
1031 snd_iprintf(buffer, "%02X: %08lX\n", i, value);
1036 static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
1037 struct snd_info_buffer *buffer)
1039 struct emu10k1x *emu = entry->private_data;
1041 unsigned int reg, channel_id , val;
1043 while (!snd_info_get_line(buffer, line, sizeof(line))) {
1044 if (sscanf(line, "%x %x %x", ®, &channel_id, &val) != 3)
1047 if (reg < 0x49 && val <= 0xffffffff && channel_id <= 2)
1048 snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1052 static int snd_emu10k1x_proc_init(struct emu10k1x *emu)
1054 snd_card_rw_proc_new(emu->card, "emu10k1x_regs", emu,
1055 snd_emu10k1x_proc_reg_read,
1056 snd_emu10k1x_proc_reg_write);
1060 #define snd_emu10k1x_shared_spdif_info snd_ctl_boolean_mono_info
1062 static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
1063 struct snd_ctl_elem_value *ucontrol)
1065 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1067 ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1072 static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
1073 struct snd_ctl_elem_value *ucontrol)
1075 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1078 val = ucontrol->value.integer.value[0] ;
1081 // enable spdif output
1082 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1083 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1084 snd_emu10k1x_gpio_write(emu, 0x1000);
1086 // disable spdif output
1087 snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1088 snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1089 snd_emu10k1x_gpio_write(emu, 0x1080);
1094 static const struct snd_kcontrol_new snd_emu10k1x_shared_spdif =
1096 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1097 .name = "Analog/Digital Output Jack",
1098 .info = snd_emu10k1x_shared_spdif_info,
1099 .get = snd_emu10k1x_shared_spdif_get,
1100 .put = snd_emu10k1x_shared_spdif_put
1103 static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1105 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1110 static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
1111 struct snd_ctl_elem_value *ucontrol)
1113 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1114 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1116 ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1117 ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1118 ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1119 ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1123 static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
1124 struct snd_ctl_elem_value *ucontrol)
1126 ucontrol->value.iec958.status[0] = 0xff;
1127 ucontrol->value.iec958.status[1] = 0xff;
1128 ucontrol->value.iec958.status[2] = 0xff;
1129 ucontrol->value.iec958.status[3] = 0xff;
1133 static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
1134 struct snd_ctl_elem_value *ucontrol)
1136 struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1137 unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1141 val = (ucontrol->value.iec958.status[0] << 0) |
1142 (ucontrol->value.iec958.status[1] << 8) |
1143 (ucontrol->value.iec958.status[2] << 16) |
1144 (ucontrol->value.iec958.status[3] << 24);
1145 change = val != emu->spdif_bits[idx];
1147 snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1148 emu->spdif_bits[idx] = val;
1153 static const struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
1155 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1156 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1157 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1159 .info = snd_emu10k1x_spdif_info,
1160 .get = snd_emu10k1x_spdif_get_mask
1163 static const struct snd_kcontrol_new snd_emu10k1x_spdif_control =
1165 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1166 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1168 .info = snd_emu10k1x_spdif_info,
1169 .get = snd_emu10k1x_spdif_get,
1170 .put = snd_emu10k1x_spdif_put
1173 static int snd_emu10k1x_mixer(struct emu10k1x *emu)
1176 struct snd_kcontrol *kctl;
1177 struct snd_card *card = emu->card;
1179 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu)) == NULL)
1181 if ((err = snd_ctl_add(card, kctl)))
1183 if ((kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu)) == NULL)
1185 if ((err = snd_ctl_add(card, kctl)))
1187 if ((kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu)) == NULL)
1189 if ((err = snd_ctl_add(card, kctl)))
1195 #define EMU10K1X_MIDI_MODE_INPUT (1<<0)
1196 #define EMU10K1X_MIDI_MODE_OUTPUT (1<<1)
1198 static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
1200 return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1203 static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
1205 snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1208 #define mpu401_write_data(emu, mpu, data) mpu401_write(emu, mpu, data, 0)
1209 #define mpu401_write_cmd(emu, mpu, data) mpu401_write(emu, mpu, data, 1)
1210 #define mpu401_read_data(emu, mpu) mpu401_read(emu, mpu, 0)
1211 #define mpu401_read_stat(emu, mpu) mpu401_read(emu, mpu, 1)
1213 #define mpu401_input_avail(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x80))
1214 #define mpu401_output_ready(emu,mpu) (!(mpu401_read_stat(emu,mpu) & 0x40))
1216 #define MPU401_RESET 0xff
1217 #define MPU401_ENTER_UART 0x3f
1218 #define MPU401_ACK 0xfe
1220 static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
1222 int timeout = 100000;
1223 for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1224 mpu401_read_data(emu, mpu);
1225 #ifdef CONFIG_SND_DEBUG
1227 dev_err(emu->card->dev,
1228 "cmd: clear rx timeout (status = 0x%x)\n",
1229 mpu401_read_stat(emu, mpu));
1237 static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
1238 struct emu10k1x_midi *midi, unsigned int status)
1242 if (midi->rmidi == NULL) {
1243 snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1247 spin_lock(&midi->input_lock);
1248 if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1249 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1250 mpu401_clear_rx(emu, midi);
1252 byte = mpu401_read_data(emu, midi);
1253 if (midi->substream_input)
1254 snd_rawmidi_receive(midi->substream_input, &byte, 1);
1257 spin_unlock(&midi->input_lock);
1259 spin_lock(&midi->output_lock);
1260 if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1261 if (midi->substream_output &&
1262 snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1263 mpu401_write_data(emu, midi, byte);
1265 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1268 spin_unlock(&midi->output_lock);
1271 static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
1273 do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1276 static int snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
1277 struct emu10k1x_midi *midi, unsigned char cmd, int ack)
1279 unsigned long flags;
1282 spin_lock_irqsave(&midi->input_lock, flags);
1283 mpu401_write_data(emu, midi, 0x00);
1284 /* mpu401_clear_rx(emu, midi); */
1286 mpu401_write_cmd(emu, midi, cmd);
1290 while (!ok && timeout-- > 0) {
1291 if (mpu401_input_avail(emu, midi)) {
1292 if (mpu401_read_data(emu, midi) == MPU401_ACK)
1296 if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1301 spin_unlock_irqrestore(&midi->input_lock, flags);
1303 dev_err(emu->card->dev,
1304 "midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1306 mpu401_read_stat(emu, midi),
1307 mpu401_read_data(emu, midi));
1313 static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
1315 struct emu10k1x *emu;
1316 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1317 unsigned long flags;
1320 if (snd_BUG_ON(!emu))
1322 spin_lock_irqsave(&midi->open_lock, flags);
1323 midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1324 midi->substream_input = substream;
1325 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1326 spin_unlock_irqrestore(&midi->open_lock, flags);
1327 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1329 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1332 spin_unlock_irqrestore(&midi->open_lock, flags);
1340 static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
1342 struct emu10k1x *emu;
1343 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1344 unsigned long flags;
1347 if (snd_BUG_ON(!emu))
1349 spin_lock_irqsave(&midi->open_lock, flags);
1350 midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1351 midi->substream_output = substream;
1352 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1353 spin_unlock_irqrestore(&midi->open_lock, flags);
1354 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1356 if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1359 spin_unlock_irqrestore(&midi->open_lock, flags);
1367 static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
1369 struct emu10k1x *emu;
1370 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1371 unsigned long flags;
1375 if (snd_BUG_ON(!emu))
1377 spin_lock_irqsave(&midi->open_lock, flags);
1378 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1379 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1380 midi->substream_input = NULL;
1381 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1382 spin_unlock_irqrestore(&midi->open_lock, flags);
1383 err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1385 spin_unlock_irqrestore(&midi->open_lock, flags);
1390 static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
1392 struct emu10k1x *emu;
1393 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1394 unsigned long flags;
1398 if (snd_BUG_ON(!emu))
1400 spin_lock_irqsave(&midi->open_lock, flags);
1401 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1402 midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1403 midi->substream_output = NULL;
1404 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1405 spin_unlock_irqrestore(&midi->open_lock, flags);
1406 err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1408 spin_unlock_irqrestore(&midi->open_lock, flags);
1413 static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1415 struct emu10k1x *emu;
1416 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1418 if (snd_BUG_ON(!emu))
1422 snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1424 snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1427 static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1429 struct emu10k1x *emu;
1430 struct emu10k1x_midi *midi = substream->rmidi->private_data;
1431 unsigned long flags;
1434 if (snd_BUG_ON(!emu))
1441 /* try to send some amount of bytes here before interrupts */
1442 spin_lock_irqsave(&midi->output_lock, flags);
1444 if (mpu401_output_ready(emu, midi)) {
1445 if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1446 snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1448 spin_unlock_irqrestore(&midi->output_lock, flags);
1451 mpu401_write_data(emu, midi, byte);
1457 spin_unlock_irqrestore(&midi->output_lock, flags);
1458 snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1460 snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1468 static const struct snd_rawmidi_ops snd_emu10k1x_midi_output =
1470 .open = snd_emu10k1x_midi_output_open,
1471 .close = snd_emu10k1x_midi_output_close,
1472 .trigger = snd_emu10k1x_midi_output_trigger,
1475 static const struct snd_rawmidi_ops snd_emu10k1x_midi_input =
1477 .open = snd_emu10k1x_midi_input_open,
1478 .close = snd_emu10k1x_midi_input_close,
1479 .trigger = snd_emu10k1x_midi_input_trigger,
1482 static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
1484 struct emu10k1x_midi *midi = rmidi->private_data;
1485 midi->interrupt = NULL;
1489 static int emu10k1x_midi_init(struct emu10k1x *emu,
1490 struct emu10k1x_midi *midi, int device,
1493 struct snd_rawmidi *rmidi;
1496 if ((err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi)) < 0)
1499 spin_lock_init(&midi->open_lock);
1500 spin_lock_init(&midi->input_lock);
1501 spin_lock_init(&midi->output_lock);
1502 strcpy(rmidi->name, name);
1503 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1504 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1505 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1506 SNDRV_RAWMIDI_INFO_INPUT |
1507 SNDRV_RAWMIDI_INFO_DUPLEX;
1508 rmidi->private_data = midi;
1509 rmidi->private_free = snd_emu10k1x_midi_free;
1510 midi->rmidi = rmidi;
1514 static int snd_emu10k1x_midi(struct emu10k1x *emu)
1516 struct emu10k1x_midi *midi = &emu->midi;
1519 if ((err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)")) < 0)
1522 midi->tx_enable = INTE_MIDITXENABLE;
1523 midi->rx_enable = INTE_MIDIRXENABLE;
1524 midi->port = MUDATA;
1525 midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1526 midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1527 midi->interrupt = snd_emu10k1x_midi_interrupt;
1531 static int snd_emu10k1x_probe(struct pci_dev *pci,
1532 const struct pci_device_id *pci_id)
1535 struct snd_card *card;
1536 struct emu10k1x *chip;
1539 if (dev >= SNDRV_CARDS)
1546 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1551 if ((err = snd_emu10k1x_create(card, pci, &chip)) < 0) {
1552 snd_card_free(card);
1556 if ((err = snd_emu10k1x_pcm(chip, 0)) < 0) {
1557 snd_card_free(card);
1560 if ((err = snd_emu10k1x_pcm(chip, 1)) < 0) {
1561 snd_card_free(card);
1564 if ((err = snd_emu10k1x_pcm(chip, 2)) < 0) {
1565 snd_card_free(card);
1569 if ((err = snd_emu10k1x_ac97(chip)) < 0) {
1570 snd_card_free(card);
1574 if ((err = snd_emu10k1x_mixer(chip)) < 0) {
1575 snd_card_free(card);
1579 if ((err = snd_emu10k1x_midi(chip)) < 0) {
1580 snd_card_free(card);
1584 snd_emu10k1x_proc_init(chip);
1586 strcpy(card->driver, "EMU10K1X");
1587 strcpy(card->shortname, "Dell Sound Blaster Live!");
1588 sprintf(card->longname, "%s at 0x%lx irq %i",
1589 card->shortname, chip->port, chip->irq);
1591 if ((err = snd_card_register(card)) < 0) {
1592 snd_card_free(card);
1596 pci_set_drvdata(pci, card);
1601 static void snd_emu10k1x_remove(struct pci_dev *pci)
1603 snd_card_free(pci_get_drvdata(pci));
1607 static const struct pci_device_id snd_emu10k1x_ids[] = {
1608 { PCI_VDEVICE(CREATIVE, 0x0006), 0 }, /* Dell OEM version (EMU10K1) */
1611 MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1613 // pci_driver definition
1614 static struct pci_driver emu10k1x_driver = {
1615 .name = KBUILD_MODNAME,
1616 .id_table = snd_emu10k1x_ids,
1617 .probe = snd_emu10k1x_probe,
1618 .remove = snd_emu10k1x_remove,
1621 module_pci_driver(emu10k1x_driver);