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