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