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