xref: /linux/sound/pci/emu10k1/emu10k1x.c (revision 746680ec6696585e30db3e18c93a63df9cbec39c)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) by Francisco Moraes <fmoraes@nc.rr.com>
4  *  Driver EMU10K1X chips
5  *
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>
8  *
9  *  BUGS:
10  *    --
11  *
12  *  TODO:
13  *
14  *  Chips (SB0200 model):
15  *    - EMU10K1X-DBQ
16  *    - STAC 9708T
17  */
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/string.h>
24 #include <linux/module.h>
25 #include <sound/core.h>
26 #include <sound/initval.h>
27 #include <sound/pcm.h>
28 #include <sound/ac97_codec.h>
29 #include <sound/info.h>
30 #include <sound/rawmidi.h>
31 
32 MODULE_AUTHOR("Francisco Moraes <fmoraes@nc.rr.com>");
33 MODULE_DESCRIPTION("EMU10K1X");
34 MODULE_LICENSE("GPL");
35 
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;
40 
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.");
47 
48 
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 /************************************************************************************************/
53 
54 #define PTR			0x00		/* Indexed register set pointer register	*/
55 						/* NOTE: The CHANNELNUM and ADDRESS words can	*/
56 						/* be modified independently of each other.	*/
57 
58 #define DATA			0x04		/* Indexed register set data register		*/
59 
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                    */
69 
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                    */
77 
78 #define HCFG			0x14		/* Hardware config register			*/
79 
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.   */
86 
87 
88 #define AC97DATA		0x1c		/* AC97 register set data register (16 bit)	*/
89 
90 #define AC97ADDRESS		0x1e		/* AC97 register set address register (8 bit)	*/
91 
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.
100 						 */
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
108 
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
114 
115 /* From 0x20 - 0x3f, last samples played on each channel */
116 
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                      */
122 
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
129 
130 #define SPCS0			0x42		/* SPDIF output Channel Status 0 register	*/
131 
132 #define SPCS1			0x43		/* SPDIF output Channel Status 1 register	*/
133 
134 #define SPCS2			0x44		/* SPDIF output Channel Status 2 register	*/
135 
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)	*/
159 
160 #define SPDIF_SELECT		0x45		/* Enables SPDIF or Analogue outputs 0-Analogue, 0x700-SPDIF */
161 
162 /* This is the MPU port on the card                      					*/
163 #define MUDATA		0x47
164 #define MUCMD		0x48
165 #define MUSTAT		MUCMD
166 
167 /* From 0x50 - 0x5f, last samples captured */
168 
169 /*
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
179  * REAR OUT.
180  * The center/lfe channel has no volume control and cannot be muted during
181  * playback.
182  */
183 
184 struct emu10k1x_voice {
185 	struct emu10k1x *emu;
186 	int number;
187 	int use;
188 
189 	struct emu10k1x_pcm *epcm;
190 };
191 
192 struct emu10k1x_pcm {
193 	struct emu10k1x *emu;
194 	struct snd_pcm_substream *substream;
195 	struct emu10k1x_voice *voice;
196 	unsigned short running;
197 };
198 
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;
209 	int port;
210 	int ipr_tx, ipr_rx;
211 	void (*interrupt)(struct emu10k1x *emu, unsigned int status);
212 };
213 
214 // definition of the chip-specific record
215 struct emu10k1x {
216 	struct snd_card *card;
217 	struct pci_dev *pci;
218 
219 	unsigned long port;
220 	int irq;
221 
222 	unsigned char revision;		/* chip revision */
223 	unsigned int serial;            /* serial number */
224 	unsigned short model;		/* subsystem id */
225 
226 	spinlock_t emu_lock;
227 	spinlock_t voice_lock;
228 
229 	struct snd_ac97 *ac97;
230 	struct snd_pcm *pcm;
231 
232 	struct emu10k1x_voice voices[3];
233 	struct emu10k1x_voice capture_voice;
234 	u32 spdif_bits[3]; // SPDIF out setup
235 
236 	struct snd_dma_buffer *dma_buffer;
237 
238 	struct emu10k1x_midi midi;
239 };
240 
241 /* hardware definition */
242 static const struct snd_pcm_hardware snd_emu10k1x_playback_hw = {
243 	.info =			(SNDRV_PCM_INFO_MMAP |
244 				 SNDRV_PCM_INFO_INTERLEAVED |
245 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
246 				 SNDRV_PCM_INFO_MMAP_VALID),
247 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
248 	.rates =		SNDRV_PCM_RATE_48000,
249 	.rate_min =		48000,
250 	.rate_max =		48000,
251 	.channels_min =		2,
252 	.channels_max =		2,
253 	.buffer_bytes_max =	(32*1024),
254 	.period_bytes_min =	64,
255 	.period_bytes_max =	(16*1024),
256 	.periods_min =		2,
257 	.periods_max =		8,
258 	.fifo_size =		0,
259 };
260 
261 static const struct snd_pcm_hardware snd_emu10k1x_capture_hw = {
262 	.info =			(SNDRV_PCM_INFO_MMAP |
263 				 SNDRV_PCM_INFO_INTERLEAVED |
264 				 SNDRV_PCM_INFO_BLOCK_TRANSFER |
265 				 SNDRV_PCM_INFO_MMAP_VALID),
266 	.formats =		SNDRV_PCM_FMTBIT_S16_LE,
267 	.rates =		SNDRV_PCM_RATE_48000,
268 	.rate_min =		48000,
269 	.rate_max =		48000,
270 	.channels_min =		2,
271 	.channels_max =		2,
272 	.buffer_bytes_max =	(32*1024),
273 	.period_bytes_min =	64,
274 	.period_bytes_max =	(16*1024),
275 	.periods_min =		2,
276 	.periods_max =		2,
277 	.fifo_size =		0,
278 };
279 
280 static unsigned int snd_emu10k1x_ptr_read(struct emu10k1x * emu,
281 					  unsigned int reg,
282 					  unsigned int chn)
283 {
284 	unsigned long flags;
285 	unsigned int regptr, val;
286 
287 	regptr = (reg << 16) | chn;
288 
289 	spin_lock_irqsave(&emu->emu_lock, flags);
290 	outl(regptr, emu->port + PTR);
291 	val = inl(emu->port + DATA);
292 	spin_unlock_irqrestore(&emu->emu_lock, flags);
293 	return val;
294 }
295 
296 static void snd_emu10k1x_ptr_write(struct emu10k1x *emu,
297 				   unsigned int reg,
298 				   unsigned int chn,
299 				   unsigned int data)
300 {
301 	unsigned int regptr;
302 	unsigned long flags;
303 
304 	regptr = (reg << 16) | chn;
305 
306 	spin_lock_irqsave(&emu->emu_lock, flags);
307 	outl(regptr, emu->port + PTR);
308 	outl(data, emu->port + DATA);
309 	spin_unlock_irqrestore(&emu->emu_lock, flags);
310 }
311 
312 static void snd_emu10k1x_intr_enable(struct emu10k1x *emu, unsigned int intrenb)
313 {
314 	unsigned long flags;
315 	unsigned int intr_enable;
316 
317 	spin_lock_irqsave(&emu->emu_lock, flags);
318 	intr_enable = inl(emu->port + INTE) | intrenb;
319 	outl(intr_enable, emu->port + INTE);
320 	spin_unlock_irqrestore(&emu->emu_lock, flags);
321 }
322 
323 static void snd_emu10k1x_intr_disable(struct emu10k1x *emu, unsigned int intrenb)
324 {
325 	unsigned long flags;
326 	unsigned int intr_enable;
327 
328 	spin_lock_irqsave(&emu->emu_lock, flags);
329 	intr_enable = inl(emu->port + INTE) & ~intrenb;
330 	outl(intr_enable, emu->port + INTE);
331 	spin_unlock_irqrestore(&emu->emu_lock, flags);
332 }
333 
334 static void snd_emu10k1x_gpio_write(struct emu10k1x *emu, unsigned int value)
335 {
336 	unsigned long flags;
337 
338 	spin_lock_irqsave(&emu->emu_lock, flags);
339 	outl(value, emu->port + GPIO);
340 	spin_unlock_irqrestore(&emu->emu_lock, flags);
341 }
342 
343 static void snd_emu10k1x_pcm_free_substream(struct snd_pcm_runtime *runtime)
344 {
345 	kfree(runtime->private_data);
346 }
347 
348 static void snd_emu10k1x_pcm_interrupt(struct emu10k1x *emu, struct emu10k1x_voice *voice)
349 {
350 	struct emu10k1x_pcm *epcm;
351 
352 	epcm = voice->epcm;
353 	if (!epcm)
354 		return;
355 	if (epcm->substream == NULL)
356 		return;
357 #if 0
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));
363 #endif
364 	snd_pcm_period_elapsed(epcm->substream);
365 }
366 
367 /* open callback */
368 static int snd_emu10k1x_playback_open(struct snd_pcm_substream *substream)
369 {
370 	struct emu10k1x *chip = snd_pcm_substream_chip(substream);
371 	struct emu10k1x_pcm *epcm;
372 	struct snd_pcm_runtime *runtime = substream->runtime;
373 	int err;
374 
375 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
376 	if (err < 0)
377 		return err;
378 	err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
379 	if (err < 0)
380                 return err;
381 
382 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
383 	if (epcm == NULL)
384 		return -ENOMEM;
385 	epcm->emu = chip;
386 	epcm->substream = substream;
387 
388 	runtime->private_data = epcm;
389 	runtime->private_free = snd_emu10k1x_pcm_free_substream;
390 
391 	runtime->hw = snd_emu10k1x_playback_hw;
392 
393 	return 0;
394 }
395 
396 /* close callback */
397 static int snd_emu10k1x_playback_close(struct snd_pcm_substream *substream)
398 {
399 	return 0;
400 }
401 
402 /* hw_params callback */
403 static int snd_emu10k1x_pcm_hw_params(struct snd_pcm_substream *substream,
404 				      struct snd_pcm_hw_params *hw_params)
405 {
406 	struct snd_pcm_runtime *runtime = substream->runtime;
407 	struct emu10k1x_pcm *epcm = runtime->private_data;
408 
409 	if (! epcm->voice) {
410 		epcm->voice = &epcm->emu->voices[substream->pcm->device];
411 		epcm->voice->use = 1;
412 		epcm->voice->epcm = epcm;
413 	}
414 
415 	return 0;
416 }
417 
418 /* hw_free callback */
419 static int snd_emu10k1x_pcm_hw_free(struct snd_pcm_substream *substream)
420 {
421 	struct snd_pcm_runtime *runtime = substream->runtime;
422 	struct emu10k1x_pcm *epcm;
423 
424 	if (runtime->private_data == NULL)
425 		return 0;
426 
427 	epcm = runtime->private_data;
428 
429 	if (epcm->voice) {
430 		epcm->voice->use = 0;
431 		epcm->voice->epcm = NULL;
432 		epcm->voice = NULL;
433 	}
434 
435 	return 0;
436 }
437 
438 /* prepare callback */
439 static int snd_emu10k1x_pcm_prepare(struct snd_pcm_substream *substream)
440 {
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);
447 	int i;
448 
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;
452 	}
453 
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);
461 
462 	snd_emu10k1x_ptr_write(emu, PLAYBACK_PERIOD_SIZE, voice, frames_to_bytes(runtime, runtime->period_size)<<16);
463 
464 	return 0;
465 }
466 
467 /* trigger callback */
468 static int snd_emu10k1x_pcm_trigger(struct snd_pcm_substream *substream,
469 				    int cmd)
470 {
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;
475 	int result = 0;
476 
477 	/*
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));
481 	*/
482 
483 	switch (cmd) {
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);
487 		else
488 			snd_emu10k1x_intr_enable(emu, INTE_CH_0_LOOP << channel);
489 		epcm->running = 1;
490 		snd_emu10k1x_ptr_write(emu, TRIGGER_CHANNEL, 0, snd_emu10k1x_ptr_read(emu, TRIGGER_CHANNEL, 0)|(TRIGGER_CHANNEL_0<<channel));
491 		break;
492 	case SNDRV_PCM_TRIGGER_STOP:
493 		epcm->running = 0;
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));
496 		break;
497 	default:
498 		result = -EINVAL;
499 		break;
500 	}
501 	return result;
502 }
503 
504 /* pointer callback */
505 static snd_pcm_uframes_t
506 snd_emu10k1x_pcm_pointer(struct snd_pcm_substream *substream)
507 {
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;
513 
514 	if (!epcm->running)
515 		return 0;
516 
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);
520 
521 	if(ptr4 == 0 && ptr1 == frames_to_bytes(runtime, runtime->buffer_size))
522 		return 0;
523 
524 	if (ptr3 != ptr4)
525 		ptr1 = snd_emu10k1x_ptr_read(emu, PLAYBACK_POINTER, channel);
526 	ptr2 = bytes_to_frames(runtime, ptr1);
527 	ptr2 += (ptr4 >> 3) * runtime->period_size;
528 	ptr = ptr2;
529 
530 	if (ptr >= runtime->buffer_size)
531 		ptr -= runtime->buffer_size;
532 
533 	return ptr;
534 }
535 
536 /* operators */
537 static const struct snd_pcm_ops snd_emu10k1x_playback_ops = {
538 	.open =        snd_emu10k1x_playback_open,
539 	.close =       snd_emu10k1x_playback_close,
540 	.hw_params =   snd_emu10k1x_pcm_hw_params,
541 	.hw_free =     snd_emu10k1x_pcm_hw_free,
542 	.prepare =     snd_emu10k1x_pcm_prepare,
543 	.trigger =     snd_emu10k1x_pcm_trigger,
544 	.pointer =     snd_emu10k1x_pcm_pointer,
545 };
546 
547 /* open_capture callback */
548 static int snd_emu10k1x_pcm_open_capture(struct snd_pcm_substream *substream)
549 {
550 	struct emu10k1x *chip = snd_pcm_substream_chip(substream);
551 	struct emu10k1x_pcm *epcm;
552 	struct snd_pcm_runtime *runtime = substream->runtime;
553 	int err;
554 
555 	err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
556 	if (err < 0)
557 		return err;
558 	err = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
559 	if (err < 0)
560 		return err;
561 
562 	epcm = kzalloc(sizeof(*epcm), GFP_KERNEL);
563 	if (epcm == NULL)
564 		return -ENOMEM;
565 
566 	epcm->emu = chip;
567 	epcm->substream = substream;
568 
569 	runtime->private_data = epcm;
570 	runtime->private_free = snd_emu10k1x_pcm_free_substream;
571 
572 	runtime->hw = snd_emu10k1x_capture_hw;
573 
574 	return 0;
575 }
576 
577 /* close callback */
578 static int snd_emu10k1x_pcm_close_capture(struct snd_pcm_substream *substream)
579 {
580 	return 0;
581 }
582 
583 /* hw_params callback */
584 static int snd_emu10k1x_pcm_hw_params_capture(struct snd_pcm_substream *substream,
585 					      struct snd_pcm_hw_params *hw_params)
586 {
587 	struct snd_pcm_runtime *runtime = substream->runtime;
588 	struct emu10k1x_pcm *epcm = runtime->private_data;
589 
590 	if (! epcm->voice) {
591 		if (epcm->emu->capture_voice.use)
592 			return -EBUSY;
593 		epcm->voice = &epcm->emu->capture_voice;
594 		epcm->voice->epcm = epcm;
595 		epcm->voice->use = 1;
596 	}
597 
598 	return 0;
599 }
600 
601 /* hw_free callback */
602 static int snd_emu10k1x_pcm_hw_free_capture(struct snd_pcm_substream *substream)
603 {
604 	struct snd_pcm_runtime *runtime = substream->runtime;
605 
606 	struct emu10k1x_pcm *epcm;
607 
608 	if (runtime->private_data == NULL)
609 		return 0;
610 	epcm = runtime->private_data;
611 
612 	if (epcm->voice) {
613 		epcm->voice->use = 0;
614 		epcm->voice->epcm = NULL;
615 		epcm->voice = NULL;
616 	}
617 
618 	return 0;
619 }
620 
621 /* prepare capture callback */
622 static int snd_emu10k1x_pcm_prepare_capture(struct snd_pcm_substream *substream)
623 {
624 	struct emu10k1x *emu = snd_pcm_substream_chip(substream);
625 	struct snd_pcm_runtime *runtime = substream->runtime;
626 
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);
631 
632 	return 0;
633 }
634 
635 /* trigger_capture callback */
636 static int snd_emu10k1x_pcm_trigger_capture(struct snd_pcm_substream *substream,
637 					    int cmd)
638 {
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;
642 	int result = 0;
643 
644 	switch (cmd) {
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);
649 		epcm->running = 1;
650 		break;
651 	case SNDRV_PCM_TRIGGER_STOP:
652 		epcm->running = 0;
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));
656 		break;
657 	default:
658 		result = -EINVAL;
659 		break;
660 	}
661 	return result;
662 }
663 
664 /* pointer_capture callback */
665 static snd_pcm_uframes_t
666 snd_emu10k1x_pcm_pointer_capture(struct snd_pcm_substream *substream)
667 {
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;
672 
673 	if (!epcm->running)
674 		return 0;
675 
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;
679 
680 	return ptr;
681 }
682 
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 	.hw_params =   snd_emu10k1x_pcm_hw_params_capture,
687 	.hw_free =     snd_emu10k1x_pcm_hw_free_capture,
688 	.prepare =     snd_emu10k1x_pcm_prepare_capture,
689 	.trigger =     snd_emu10k1x_pcm_trigger_capture,
690 	.pointer =     snd_emu10k1x_pcm_pointer_capture,
691 };
692 
693 static unsigned short snd_emu10k1x_ac97_read(struct snd_ac97 *ac97,
694 					     unsigned short reg)
695 {
696 	struct emu10k1x *emu = ac97->private_data;
697 	unsigned long flags;
698 	unsigned short val;
699 
700 	spin_lock_irqsave(&emu->emu_lock, flags);
701 	outb(reg, emu->port + AC97ADDRESS);
702 	val = inw(emu->port + AC97DATA);
703 	spin_unlock_irqrestore(&emu->emu_lock, flags);
704 	return val;
705 }
706 
707 static void snd_emu10k1x_ac97_write(struct snd_ac97 *ac97,
708 				    unsigned short reg, unsigned short val)
709 {
710 	struct emu10k1x *emu = ac97->private_data;
711 	unsigned long flags;
712 
713 	spin_lock_irqsave(&emu->emu_lock, flags);
714 	outb(reg, emu->port + AC97ADDRESS);
715 	outw(val, emu->port + AC97DATA);
716 	spin_unlock_irqrestore(&emu->emu_lock, flags);
717 }
718 
719 static int snd_emu10k1x_ac97(struct emu10k1x *chip)
720 {
721 	struct snd_ac97_bus *pbus;
722 	struct snd_ac97_template ac97;
723 	int err;
724 	static const struct snd_ac97_bus_ops ops = {
725 		.write = snd_emu10k1x_ac97_write,
726 		.read = snd_emu10k1x_ac97_read,
727 	};
728 
729 	err = snd_ac97_bus(chip->card, 0, &ops, NULL, &pbus);
730 	if (err < 0)
731 		return err;
732 	pbus->no_vra = 1; /* we don't need VRA */
733 
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);
738 }
739 
740 static void snd_emu10k1x_free(struct snd_card *card)
741 {
742 	struct emu10k1x *chip = card->private_data;
743 
744 	snd_emu10k1x_ptr_write(chip, TRIGGER_CHANNEL, 0, 0);
745 	// disable interrupts
746 	outl(0, chip->port + INTE);
747 	// disable audio
748 	outl(HCFG_LOCKSOUNDCACHE, chip->port + HCFG);
749 }
750 
751 static irqreturn_t snd_emu10k1x_interrupt(int irq, void *dev_id)
752 {
753 	unsigned int status;
754 
755 	struct emu10k1x *chip = dev_id;
756 	struct emu10k1x_voice *pvoice = chip->voices;
757 	int i;
758 	int mask;
759 
760 	status = inl(chip->port + IPR);
761 
762 	if (! status)
763 		return IRQ_NONE;
764 
765 	// capture interrupt
766 	if (status & (IPR_CAP_0_LOOP | IPR_CAP_0_HALF_LOOP)) {
767 		struct emu10k1x_voice *cap_voice = &chip->capture_voice;
768 		if (cap_voice->use)
769 			snd_emu10k1x_pcm_interrupt(chip, cap_voice);
770 		else
771 			snd_emu10k1x_intr_disable(chip,
772 						  INTE_CAP_0_LOOP |
773 						  INTE_CAP_0_HALF_LOOP);
774 	}
775 
776 	mask = IPR_CH_0_LOOP|IPR_CH_0_HALF_LOOP;
777 	for (i = 0; i < 3; i++) {
778 		if (status & mask) {
779 			if (pvoice->use)
780 				snd_emu10k1x_pcm_interrupt(chip, pvoice);
781 			else
782 				snd_emu10k1x_intr_disable(chip, mask);
783 		}
784 		pvoice++;
785 		mask <<= 1;
786 	}
787 
788 	if (status & (IPR_MIDITRANSBUFEMPTY|IPR_MIDIRECVBUFEMPTY)) {
789 		if (chip->midi.interrupt)
790 			chip->midi.interrupt(chip, status);
791 		else
792 			snd_emu10k1x_intr_disable(chip, INTE_MIDITXENABLE|INTE_MIDIRXENABLE);
793 	}
794 
795 	// acknowledge the interrupt if necessary
796 	outl(status, chip->port + IPR);
797 
798 	/* dev_dbg(chip->card->dev, "interrupt %08x\n", status); */
799 	return IRQ_HANDLED;
800 }
801 
802 static const struct snd_pcm_chmap_elem surround_map[] = {
803 	{ .channels = 2,
804 	  .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
805 	{ }
806 };
807 
808 static const struct snd_pcm_chmap_elem clfe_map[] = {
809 	{ .channels = 2,
810 	  .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } },
811 	{ }
812 };
813 
814 static int snd_emu10k1x_pcm(struct emu10k1x *emu, int device)
815 {
816 	struct snd_pcm *pcm;
817 	const struct snd_pcm_chmap_elem *map = NULL;
818 	int err;
819 	int capture = 0;
820 
821 	if (device == 0)
822 		capture = 1;
823 
824 	err = snd_pcm_new(emu->card, "emu10k1x", device, 1, capture, &pcm);
825 	if (err < 0)
826 		return err;
827 
828 	pcm->private_data = emu;
829 
830 	switch(device) {
831 	case 0:
832 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
833 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_emu10k1x_capture_ops);
834 		break;
835 	case 1:
836 	case 2:
837 		snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_emu10k1x_playback_ops);
838 		break;
839 	}
840 
841 	pcm->info_flags = 0;
842 	switch(device) {
843 	case 0:
844 		strscpy(pcm->name, "EMU10K1X Front");
845 		map = snd_pcm_std_chmaps;
846 		break;
847 	case 1:
848 		strscpy(pcm->name, "EMU10K1X Rear");
849 		map = surround_map;
850 		break;
851 	case 2:
852 		strscpy(pcm->name, "EMU10K1X Center/LFE");
853 		map = clfe_map;
854 		break;
855 	}
856 	emu->pcm = pcm;
857 
858 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
859 				       &emu->pci->dev, 32*1024, 32*1024);
860 
861 	return snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, 2,
862 				     1 << 2, NULL);
863 }
864 
865 static int snd_emu10k1x_create(struct snd_card *card,
866 			       struct pci_dev *pci)
867 {
868 	struct emu10k1x *chip = card->private_data;
869 	int err;
870 	int ch;
871 
872 	err = pcim_enable_device(pci);
873 	if (err < 0)
874 		return err;
875 
876 	if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(28)) < 0) {
877 		dev_err(card->dev, "error to set 28bit mask DMA\n");
878 		return -ENXIO;
879 	}
880 
881 	chip->card = card;
882 	chip->pci = pci;
883 	chip->irq = -1;
884 
885 	spin_lock_init(&chip->emu_lock);
886 	spin_lock_init(&chip->voice_lock);
887 
888 	err = pcim_request_all_regions(pci, "EMU10K1X");
889 	if (err < 0)
890 		return err;
891 	chip->port = pci_resource_start(pci, 0);
892 
893 	if (devm_request_irq(&pci->dev, pci->irq, snd_emu10k1x_interrupt,
894 			     IRQF_SHARED, KBUILD_MODNAME, chip)) {
895 		dev_err(card->dev, "cannot grab irq %d\n", pci->irq);
896 		return -EBUSY;
897 	}
898 	chip->irq = pci->irq;
899 	card->sync_irq = chip->irq;
900 	card->private_free = snd_emu10k1x_free;
901 
902 	chip->dma_buffer = snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV,
903 						4 * 1024);
904 	if (!chip->dma_buffer)
905 		return -ENOMEM;
906 
907 	pci_set_master(pci);
908 	/* read revision & serial */
909 	chip->revision = pci->revision;
910 	pci_read_config_dword(pci, PCI_SUBSYSTEM_VENDOR_ID, &chip->serial);
911 	pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &chip->model);
912 	dev_info(card->dev, "Model %04x Rev %08x Serial %08x\n", chip->model,
913 		   chip->revision, chip->serial);
914 
915 	outl(0, chip->port + INTE);
916 
917 	for(ch = 0; ch < 3; ch++) {
918 		chip->voices[ch].emu = chip;
919 		chip->voices[ch].number = ch;
920 	}
921 
922 	/*
923 	 *  Init to 0x02109204 :
924 	 *  Clock accuracy    = 0     (1000ppm)
925 	 *  Sample Rate       = 2     (48kHz)
926 	 *  Audio Channel     = 1     (Left of 2)
927 	 *  Source Number     = 0     (Unspecified)
928 	 *  Generation Status = 1     (Original for Cat Code 12)
929 	 *  Cat Code          = 12    (Digital Signal Mixer)
930 	 *  Mode              = 0     (Mode 0)
931 	 *  Emphasis          = 0     (None)
932 	 *  CP                = 1     (Copyright unasserted)
933 	 *  AN                = 0     (Audio data)
934 	 *  P                 = 0     (Consumer)
935 	 */
936 	snd_emu10k1x_ptr_write(chip, SPCS0, 0,
937 			       chip->spdif_bits[0] =
938 			       SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
939 			       SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
940 			       SPCS_GENERATIONSTATUS | 0x00001200 |
941 			       0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
942 	snd_emu10k1x_ptr_write(chip, SPCS1, 0,
943 			       chip->spdif_bits[1] =
944 			       SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
945 			       SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
946 			       SPCS_GENERATIONSTATUS | 0x00001200 |
947 			       0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
948 	snd_emu10k1x_ptr_write(chip, SPCS2, 0,
949 			       chip->spdif_bits[2] =
950 			       SPCS_CLKACCY_1000PPM | SPCS_SAMPLERATE_48 |
951 			       SPCS_CHANNELNUM_LEFT | SPCS_SOURCENUM_UNSPEC |
952 			       SPCS_GENERATIONSTATUS | 0x00001200 |
953 			       0x00000000 | SPCS_EMPHASIS_NONE | SPCS_COPYRIGHT);
954 
955 	snd_emu10k1x_ptr_write(chip, SPDIF_SELECT, 0, 0x700); // disable SPDIF
956 	snd_emu10k1x_ptr_write(chip, ROUTING, 0, 0x1003F); // routing
957 	snd_emu10k1x_gpio_write(chip, 0x1080); // analog mode
958 
959 	outl(HCFG_LOCKSOUNDCACHE|HCFG_AUDIOENABLE, chip->port+HCFG);
960 
961 	return 0;
962 }
963 
964 static void snd_emu10k1x_proc_reg_read(struct snd_info_entry *entry,
965 				       struct snd_info_buffer *buffer)
966 {
967 	struct emu10k1x *emu = entry->private_data;
968 	unsigned long value,value1,value2;
969 	unsigned long flags;
970 	int i;
971 
972 	snd_iprintf(buffer, "Registers:\n\n");
973 	for(i = 0; i < 0x20; i+=4) {
974 		spin_lock_irqsave(&emu->emu_lock, flags);
975 		value = inl(emu->port + i);
976 		spin_unlock_irqrestore(&emu->emu_lock, flags);
977 		snd_iprintf(buffer, "Register %02X: %08lX\n", i, value);
978 	}
979 	snd_iprintf(buffer, "\nRegisters\n\n");
980 	for(i = 0; i <= 0x48; i++) {
981 		value = snd_emu10k1x_ptr_read(emu, i, 0);
982 		if(i < 0x10 || (i >= 0x20 && i < 0x40)) {
983 			value1 = snd_emu10k1x_ptr_read(emu, i, 1);
984 			value2 = snd_emu10k1x_ptr_read(emu, i, 2);
985 			snd_iprintf(buffer, "%02X: %08lX %08lX %08lX\n", i, value, value1, value2);
986 		} else {
987 			snd_iprintf(buffer, "%02X: %08lX\n", i, value);
988 		}
989 	}
990 }
991 
992 static void snd_emu10k1x_proc_reg_write(struct snd_info_entry *entry,
993 					struct snd_info_buffer *buffer)
994 {
995 	struct emu10k1x *emu = entry->private_data;
996 	char line[64];
997 	unsigned int reg, channel_id , val;
998 
999 	while (!snd_info_get_line(buffer, line, sizeof(line))) {
1000 		if (sscanf(line, "%x %x %x", &reg, &channel_id, &val) != 3)
1001 			continue;
1002 
1003 		if (reg < 0x49 && channel_id <= 2)
1004 			snd_emu10k1x_ptr_write(emu, reg, channel_id, val);
1005 	}
1006 }
1007 
1008 static int snd_emu10k1x_proc_init(struct emu10k1x *emu)
1009 {
1010 	snd_card_rw_proc_new(emu->card, "emu10k1x_regs", emu,
1011 			     snd_emu10k1x_proc_reg_read,
1012 			     snd_emu10k1x_proc_reg_write);
1013 	return 0;
1014 }
1015 
1016 #define snd_emu10k1x_shared_spdif_info	snd_ctl_boolean_mono_info
1017 
1018 static int snd_emu10k1x_shared_spdif_get(struct snd_kcontrol *kcontrol,
1019 					 struct snd_ctl_elem_value *ucontrol)
1020 {
1021 	struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1022 
1023 	ucontrol->value.integer.value[0] = (snd_emu10k1x_ptr_read(emu, SPDIF_SELECT, 0) == 0x700) ? 0 : 1;
1024 
1025 	return 0;
1026 }
1027 
1028 static int snd_emu10k1x_shared_spdif_put(struct snd_kcontrol *kcontrol,
1029 					 struct snd_ctl_elem_value *ucontrol)
1030 {
1031 	struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1032 	unsigned int val;
1033 
1034 	val = ucontrol->value.integer.value[0] ;
1035 
1036 	if (val) {
1037 		// enable spdif output
1038 		snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x000);
1039 		snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x700);
1040 		snd_emu10k1x_gpio_write(emu, 0x1000);
1041 	} else {
1042 		// disable spdif output
1043 		snd_emu10k1x_ptr_write(emu, SPDIF_SELECT, 0, 0x700);
1044 		snd_emu10k1x_ptr_write(emu, ROUTING, 0, 0x1003F);
1045 		snd_emu10k1x_gpio_write(emu, 0x1080);
1046 	}
1047 	return 0;
1048 }
1049 
1050 static const struct snd_kcontrol_new snd_emu10k1x_shared_spdif =
1051 {
1052 	.iface =	SNDRV_CTL_ELEM_IFACE_MIXER,
1053 	.name =		"Analog/Digital Output Jack",
1054 	.info =		snd_emu10k1x_shared_spdif_info,
1055 	.get =		snd_emu10k1x_shared_spdif_get,
1056 	.put =		snd_emu10k1x_shared_spdif_put
1057 };
1058 
1059 static int snd_emu10k1x_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1060 {
1061 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1062 	uinfo->count = 1;
1063 	return 0;
1064 }
1065 
1066 static int snd_emu10k1x_spdif_get(struct snd_kcontrol *kcontrol,
1067 				  struct snd_ctl_elem_value *ucontrol)
1068 {
1069 	struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1070 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1071 
1072 	ucontrol->value.iec958.status[0] = (emu->spdif_bits[idx] >> 0) & 0xff;
1073 	ucontrol->value.iec958.status[1] = (emu->spdif_bits[idx] >> 8) & 0xff;
1074 	ucontrol->value.iec958.status[2] = (emu->spdif_bits[idx] >> 16) & 0xff;
1075 	ucontrol->value.iec958.status[3] = (emu->spdif_bits[idx] >> 24) & 0xff;
1076 	return 0;
1077 }
1078 
1079 static int snd_emu10k1x_spdif_get_mask(struct snd_kcontrol *kcontrol,
1080 				       struct snd_ctl_elem_value *ucontrol)
1081 {
1082 	ucontrol->value.iec958.status[0] = 0xff;
1083 	ucontrol->value.iec958.status[1] = 0xff;
1084 	ucontrol->value.iec958.status[2] = 0xff;
1085 	ucontrol->value.iec958.status[3] = 0xff;
1086 	return 0;
1087 }
1088 
1089 static int snd_emu10k1x_spdif_put(struct snd_kcontrol *kcontrol,
1090 				  struct snd_ctl_elem_value *ucontrol)
1091 {
1092 	struct emu10k1x *emu = snd_kcontrol_chip(kcontrol);
1093 	unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
1094 	int change;
1095 	unsigned int val;
1096 
1097 	val = (ucontrol->value.iec958.status[0] << 0) |
1098 		(ucontrol->value.iec958.status[1] << 8) |
1099 		(ucontrol->value.iec958.status[2] << 16) |
1100 		(ucontrol->value.iec958.status[3] << 24);
1101 	change = val != emu->spdif_bits[idx];
1102 	if (change) {
1103 		snd_emu10k1x_ptr_write(emu, SPCS0 + idx, 0, val);
1104 		emu->spdif_bits[idx] = val;
1105 	}
1106 	return change;
1107 }
1108 
1109 static const struct snd_kcontrol_new snd_emu10k1x_spdif_mask_control =
1110 {
1111 	.access =	SNDRV_CTL_ELEM_ACCESS_READ,
1112 	.iface =        SNDRV_CTL_ELEM_IFACE_PCM,
1113 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
1114 	.count =	3,
1115 	.info =         snd_emu10k1x_spdif_info,
1116 	.get =          snd_emu10k1x_spdif_get_mask
1117 };
1118 
1119 static const struct snd_kcontrol_new snd_emu10k1x_spdif_control =
1120 {
1121 	.iface =	SNDRV_CTL_ELEM_IFACE_PCM,
1122 	.name =         SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1123 	.count =	3,
1124 	.info =         snd_emu10k1x_spdif_info,
1125 	.get =          snd_emu10k1x_spdif_get,
1126 	.put =          snd_emu10k1x_spdif_put
1127 };
1128 
1129 static int snd_emu10k1x_mixer(struct emu10k1x *emu)
1130 {
1131 	int err;
1132 	struct snd_kcontrol *kctl;
1133 	struct snd_card *card = emu->card;
1134 
1135 	kctl = snd_ctl_new1(&snd_emu10k1x_spdif_mask_control, emu);
1136 	if (!kctl)
1137 		return -ENOMEM;
1138 	err = snd_ctl_add(card, kctl);
1139 	if (err)
1140 		return err;
1141 	kctl = snd_ctl_new1(&snd_emu10k1x_shared_spdif, emu);
1142 	if (!kctl)
1143 		return -ENOMEM;
1144 	err = snd_ctl_add(card, kctl);
1145 	if (err)
1146 		return err;
1147 	kctl = snd_ctl_new1(&snd_emu10k1x_spdif_control, emu);
1148 	if (!kctl)
1149 		return -ENOMEM;
1150 	err = snd_ctl_add(card, kctl);
1151 	if (err)
1152 		return err;
1153 
1154 	return 0;
1155 }
1156 
1157 #define EMU10K1X_MIDI_MODE_INPUT	(1<<0)
1158 #define EMU10K1X_MIDI_MODE_OUTPUT	(1<<1)
1159 
1160 static inline unsigned char mpu401_read(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int idx)
1161 {
1162 	return (unsigned char)snd_emu10k1x_ptr_read(emu, mpu->port + idx, 0);
1163 }
1164 
1165 static inline void mpu401_write(struct emu10k1x *emu, struct emu10k1x_midi *mpu, int data, int idx)
1166 {
1167 	snd_emu10k1x_ptr_write(emu, mpu->port + idx, 0, data);
1168 }
1169 
1170 #define mpu401_write_data(emu, mpu, data)	mpu401_write(emu, mpu, data, 0)
1171 #define mpu401_write_cmd(emu, mpu, data)	mpu401_write(emu, mpu, data, 1)
1172 #define mpu401_read_data(emu, mpu)		mpu401_read(emu, mpu, 0)
1173 #define mpu401_read_stat(emu, mpu)		mpu401_read(emu, mpu, 1)
1174 
1175 #define mpu401_input_avail(emu,mpu)	(!(mpu401_read_stat(emu,mpu) & 0x80))
1176 #define mpu401_output_ready(emu,mpu)	(!(mpu401_read_stat(emu,mpu) & 0x40))
1177 
1178 #define MPU401_RESET		0xff
1179 #define MPU401_ENTER_UART	0x3f
1180 #define MPU401_ACK		0xfe
1181 
1182 static void mpu401_clear_rx(struct emu10k1x *emu, struct emu10k1x_midi *mpu)
1183 {
1184 	int timeout = 100000;
1185 	for (; timeout > 0 && mpu401_input_avail(emu, mpu); timeout--)
1186 		mpu401_read_data(emu, mpu);
1187 #ifdef CONFIG_SND_DEBUG
1188 	if (timeout <= 0)
1189 		dev_err(emu->card->dev,
1190 			"cmd: clear rx timeout (status = 0x%x)\n",
1191 			mpu401_read_stat(emu, mpu));
1192 #endif
1193 }
1194 
1195 /*
1196 
1197  */
1198 
1199 static void do_emu10k1x_midi_interrupt(struct emu10k1x *emu,
1200 				       struct emu10k1x_midi *midi, unsigned int status)
1201 {
1202 	unsigned char byte;
1203 
1204 	if (midi->rmidi == NULL) {
1205 		snd_emu10k1x_intr_disable(emu, midi->tx_enable | midi->rx_enable);
1206 		return;
1207 	}
1208 
1209 	spin_lock(&midi->input_lock);
1210 	if ((status & midi->ipr_rx) && mpu401_input_avail(emu, midi)) {
1211 		if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1212 			mpu401_clear_rx(emu, midi);
1213 		} else {
1214 			byte = mpu401_read_data(emu, midi);
1215 			if (midi->substream_input)
1216 				snd_rawmidi_receive(midi->substream_input, &byte, 1);
1217 		}
1218 	}
1219 	spin_unlock(&midi->input_lock);
1220 
1221 	spin_lock(&midi->output_lock);
1222 	if ((status & midi->ipr_tx) && mpu401_output_ready(emu, midi)) {
1223 		if (midi->substream_output &&
1224 		    snd_rawmidi_transmit(midi->substream_output, &byte, 1) == 1) {
1225 			mpu401_write_data(emu, midi, byte);
1226 		} else {
1227 			snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1228 		}
1229 	}
1230 	spin_unlock(&midi->output_lock);
1231 }
1232 
1233 static void snd_emu10k1x_midi_interrupt(struct emu10k1x *emu, unsigned int status)
1234 {
1235 	do_emu10k1x_midi_interrupt(emu, &emu->midi, status);
1236 }
1237 
1238 static int snd_emu10k1x_midi_cmd(struct emu10k1x * emu,
1239 				  struct emu10k1x_midi *midi, unsigned char cmd, int ack)
1240 {
1241 	unsigned long flags;
1242 	int timeout, ok;
1243 
1244 	spin_lock_irqsave(&midi->input_lock, flags);
1245 	mpu401_write_data(emu, midi, 0x00);
1246 	/* mpu401_clear_rx(emu, midi); */
1247 
1248 	mpu401_write_cmd(emu, midi, cmd);
1249 	if (ack) {
1250 		ok = 0;
1251 		timeout = 10000;
1252 		while (!ok && timeout-- > 0) {
1253 			if (mpu401_input_avail(emu, midi)) {
1254 				if (mpu401_read_data(emu, midi) == MPU401_ACK)
1255 					ok = 1;
1256 			}
1257 		}
1258 		if (!ok && mpu401_read_data(emu, midi) == MPU401_ACK)
1259 			ok = 1;
1260 	} else {
1261 		ok = 1;
1262 	}
1263 	spin_unlock_irqrestore(&midi->input_lock, flags);
1264 	if (!ok) {
1265 		dev_err(emu->card->dev,
1266 			"midi_cmd: 0x%x failed at 0x%lx (status = 0x%x, data = 0x%x)!!!\n",
1267 			   cmd, emu->port,
1268 			   mpu401_read_stat(emu, midi),
1269 			   mpu401_read_data(emu, midi));
1270 		return 1;
1271 	}
1272 	return 0;
1273 }
1274 
1275 static int snd_emu10k1x_midi_input_open(struct snd_rawmidi_substream *substream)
1276 {
1277 	struct emu10k1x *emu;
1278 	struct emu10k1x_midi *midi = substream->rmidi->private_data;
1279 	unsigned long flags;
1280 
1281 	emu = midi->emu;
1282 	if (snd_BUG_ON(!emu))
1283 		return -ENXIO;
1284 	spin_lock_irqsave(&midi->open_lock, flags);
1285 	midi->midi_mode |= EMU10K1X_MIDI_MODE_INPUT;
1286 	midi->substream_input = substream;
1287 	if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1288 		spin_unlock_irqrestore(&midi->open_lock, flags);
1289 		if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1290 			goto error_out;
1291 		if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1292 			goto error_out;
1293 	} else {
1294 		spin_unlock_irqrestore(&midi->open_lock, flags);
1295 	}
1296 	return 0;
1297 
1298 error_out:
1299 	return -EIO;
1300 }
1301 
1302 static int snd_emu10k1x_midi_output_open(struct snd_rawmidi_substream *substream)
1303 {
1304 	struct emu10k1x *emu;
1305 	struct emu10k1x_midi *midi = substream->rmidi->private_data;
1306 	unsigned long flags;
1307 
1308 	emu = midi->emu;
1309 	if (snd_BUG_ON(!emu))
1310 		return -ENXIO;
1311 	spin_lock_irqsave(&midi->open_lock, flags);
1312 	midi->midi_mode |= EMU10K1X_MIDI_MODE_OUTPUT;
1313 	midi->substream_output = substream;
1314 	if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1315 		spin_unlock_irqrestore(&midi->open_lock, flags);
1316 		if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 1))
1317 			goto error_out;
1318 		if (snd_emu10k1x_midi_cmd(emu, midi, MPU401_ENTER_UART, 1))
1319 			goto error_out;
1320 	} else {
1321 		spin_unlock_irqrestore(&midi->open_lock, flags);
1322 	}
1323 	return 0;
1324 
1325 error_out:
1326 	return -EIO;
1327 }
1328 
1329 static int snd_emu10k1x_midi_input_close(struct snd_rawmidi_substream *substream)
1330 {
1331 	struct emu10k1x *emu;
1332 	struct emu10k1x_midi *midi = substream->rmidi->private_data;
1333 	unsigned long flags;
1334 	int err = 0;
1335 
1336 	emu = midi->emu;
1337 	if (snd_BUG_ON(!emu))
1338 		return -ENXIO;
1339 	spin_lock_irqsave(&midi->open_lock, flags);
1340 	snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1341 	midi->midi_mode &= ~EMU10K1X_MIDI_MODE_INPUT;
1342 	midi->substream_input = NULL;
1343 	if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT)) {
1344 		spin_unlock_irqrestore(&midi->open_lock, flags);
1345 		err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1346 	} else {
1347 		spin_unlock_irqrestore(&midi->open_lock, flags);
1348 	}
1349 	return err;
1350 }
1351 
1352 static int snd_emu10k1x_midi_output_close(struct snd_rawmidi_substream *substream)
1353 {
1354 	struct emu10k1x *emu;
1355 	struct emu10k1x_midi *midi = substream->rmidi->private_data;
1356 	unsigned long flags;
1357 	int err = 0;
1358 
1359 	emu = midi->emu;
1360 	if (snd_BUG_ON(!emu))
1361 		return -ENXIO;
1362 	spin_lock_irqsave(&midi->open_lock, flags);
1363 	snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1364 	midi->midi_mode &= ~EMU10K1X_MIDI_MODE_OUTPUT;
1365 	midi->substream_output = NULL;
1366 	if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_INPUT)) {
1367 		spin_unlock_irqrestore(&midi->open_lock, flags);
1368 		err = snd_emu10k1x_midi_cmd(emu, midi, MPU401_RESET, 0);
1369 	} else {
1370 		spin_unlock_irqrestore(&midi->open_lock, flags);
1371 	}
1372 	return err;
1373 }
1374 
1375 static void snd_emu10k1x_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1376 {
1377 	struct emu10k1x *emu;
1378 	struct emu10k1x_midi *midi = substream->rmidi->private_data;
1379 	emu = midi->emu;
1380 	if (snd_BUG_ON(!emu))
1381 		return;
1382 
1383 	if (up)
1384 		snd_emu10k1x_intr_enable(emu, midi->rx_enable);
1385 	else
1386 		snd_emu10k1x_intr_disable(emu, midi->rx_enable);
1387 }
1388 
1389 static void snd_emu10k1x_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
1390 {
1391 	struct emu10k1x *emu;
1392 	struct emu10k1x_midi *midi = substream->rmidi->private_data;
1393 	unsigned long flags;
1394 
1395 	emu = midi->emu;
1396 	if (snd_BUG_ON(!emu))
1397 		return;
1398 
1399 	if (up) {
1400 		int max = 4;
1401 		unsigned char byte;
1402 
1403 		/* try to send some amount of bytes here before interrupts */
1404 		spin_lock_irqsave(&midi->output_lock, flags);
1405 		while (max > 0) {
1406 			if (mpu401_output_ready(emu, midi)) {
1407 				if (!(midi->midi_mode & EMU10K1X_MIDI_MODE_OUTPUT) ||
1408 				    snd_rawmidi_transmit(substream, &byte, 1) != 1) {
1409 					/* no more data */
1410 					spin_unlock_irqrestore(&midi->output_lock, flags);
1411 					return;
1412 				}
1413 				mpu401_write_data(emu, midi, byte);
1414 				max--;
1415 			} else {
1416 				break;
1417 			}
1418 		}
1419 		spin_unlock_irqrestore(&midi->output_lock, flags);
1420 		snd_emu10k1x_intr_enable(emu, midi->tx_enable);
1421 	} else {
1422 		snd_emu10k1x_intr_disable(emu, midi->tx_enable);
1423 	}
1424 }
1425 
1426 /*
1427 
1428  */
1429 
1430 static const struct snd_rawmidi_ops snd_emu10k1x_midi_output =
1431 {
1432 	.open =		snd_emu10k1x_midi_output_open,
1433 	.close =	snd_emu10k1x_midi_output_close,
1434 	.trigger =	snd_emu10k1x_midi_output_trigger,
1435 };
1436 
1437 static const struct snd_rawmidi_ops snd_emu10k1x_midi_input =
1438 {
1439 	.open =		snd_emu10k1x_midi_input_open,
1440 	.close =	snd_emu10k1x_midi_input_close,
1441 	.trigger =	snd_emu10k1x_midi_input_trigger,
1442 };
1443 
1444 static void snd_emu10k1x_midi_free(struct snd_rawmidi *rmidi)
1445 {
1446 	struct emu10k1x_midi *midi = rmidi->private_data;
1447 	midi->interrupt = NULL;
1448 	midi->rmidi = NULL;
1449 }
1450 
1451 static int emu10k1x_midi_init(struct emu10k1x *emu,
1452 			      struct emu10k1x_midi *midi, int device,
1453 			      char *name)
1454 {
1455 	struct snd_rawmidi *rmidi;
1456 	int err;
1457 
1458 	err = snd_rawmidi_new(emu->card, name, device, 1, 1, &rmidi);
1459 	if (err < 0)
1460 		return err;
1461 	midi->emu = emu;
1462 	spin_lock_init(&midi->open_lock);
1463 	spin_lock_init(&midi->input_lock);
1464 	spin_lock_init(&midi->output_lock);
1465 	strscpy(rmidi->name, name);
1466 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_emu10k1x_midi_output);
1467 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_emu10k1x_midi_input);
1468 	rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT |
1469 	                     SNDRV_RAWMIDI_INFO_INPUT |
1470 	                     SNDRV_RAWMIDI_INFO_DUPLEX;
1471 	rmidi->private_data = midi;
1472 	rmidi->private_free = snd_emu10k1x_midi_free;
1473 	midi->rmidi = rmidi;
1474 	return 0;
1475 }
1476 
1477 static int snd_emu10k1x_midi(struct emu10k1x *emu)
1478 {
1479 	struct emu10k1x_midi *midi = &emu->midi;
1480 	int err;
1481 
1482 	err = emu10k1x_midi_init(emu, midi, 0, "EMU10K1X MPU-401 (UART)");
1483 	if (err < 0)
1484 		return err;
1485 
1486 	midi->tx_enable = INTE_MIDITXENABLE;
1487 	midi->rx_enable = INTE_MIDIRXENABLE;
1488 	midi->port = MUDATA;
1489 	midi->ipr_tx = IPR_MIDITRANSBUFEMPTY;
1490 	midi->ipr_rx = IPR_MIDIRECVBUFEMPTY;
1491 	midi->interrupt = snd_emu10k1x_midi_interrupt;
1492 	return 0;
1493 }
1494 
1495 static int __snd_emu10k1x_probe(struct pci_dev *pci,
1496 				const struct pci_device_id *pci_id)
1497 {
1498 	static int dev;
1499 	struct snd_card *card;
1500 	struct emu10k1x *chip;
1501 	int err;
1502 
1503 	if (dev >= SNDRV_CARDS)
1504 		return -ENODEV;
1505 	if (!enable[dev]) {
1506 		dev++;
1507 		return -ENOENT;
1508 	}
1509 
1510 	err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1511 				sizeof(*chip), &card);
1512 	if (err < 0)
1513 		return err;
1514 	chip = card->private_data;
1515 
1516 	err = snd_emu10k1x_create(card, pci);
1517 	if (err < 0)
1518 		return err;
1519 
1520 	err = snd_emu10k1x_pcm(chip, 0);
1521 	if (err < 0)
1522 		return err;
1523 	err = snd_emu10k1x_pcm(chip, 1);
1524 	if (err < 0)
1525 		return err;
1526 	err = snd_emu10k1x_pcm(chip, 2);
1527 	if (err < 0)
1528 		return err;
1529 
1530 	err = snd_emu10k1x_ac97(chip);
1531 	if (err < 0)
1532 		return err;
1533 
1534 	err = snd_emu10k1x_mixer(chip);
1535 	if (err < 0)
1536 		return err;
1537 
1538 	err = snd_emu10k1x_midi(chip);
1539 	if (err < 0)
1540 		return err;
1541 
1542 	snd_emu10k1x_proc_init(chip);
1543 
1544 	strscpy(card->driver, "EMU10K1X");
1545 	strscpy(card->shortname, "Dell Sound Blaster Live!");
1546 	sprintf(card->longname, "%s at 0x%lx irq %i",
1547 		card->shortname, chip->port, chip->irq);
1548 
1549 	err = snd_card_register(card);
1550 	if (err < 0)
1551 		return err;
1552 
1553 	pci_set_drvdata(pci, card);
1554 	dev++;
1555 	return 0;
1556 }
1557 
1558 static int snd_emu10k1x_probe(struct pci_dev *pci,
1559 			      const struct pci_device_id *pci_id)
1560 {
1561 	return snd_card_free_on_error(&pci->dev, __snd_emu10k1x_probe(pci, pci_id));
1562 }
1563 
1564 // PCI IDs
1565 static const struct pci_device_id snd_emu10k1x_ids[] = {
1566 	{ PCI_VDEVICE(CREATIVE, 0x0006), 0 },	/* Dell OEM version (EMU10K1) */
1567 	{ 0, }
1568 };
1569 MODULE_DEVICE_TABLE(pci, snd_emu10k1x_ids);
1570 
1571 // pci_driver definition
1572 static struct pci_driver emu10k1x_driver = {
1573 	.name = KBUILD_MODNAME,
1574 	.id_table = snd_emu10k1x_ids,
1575 	.probe = snd_emu10k1x_probe,
1576 };
1577 
1578 module_pci_driver(emu10k1x_driver);
1579