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