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