File Index
Files and directories
Sign in
FILE

emu10k1x.c

File preview with syntax highlighting.

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