📄 ali5451.c
字号:
} if (!(codec->synth.chmap & (1 << idx))) { codec->synth.chmap |= 1 << idx; codec->synth.chcnt++; snd_ali_printk("alloc_pcm_channel no. %d.\n",idx); return idx; } return -1;}static int snd_ali_find_free_channel(struct snd_ali * codec, int rec){ int idx; int result = -1; snd_ali_printk("find_free_channel: for %s\n",rec ? "rec" : "pcm"); /* recording */ if (rec) { if (codec->spdif_support && (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_IN_SUPPORT)) idx = ALI_SPDIF_IN_CHANNEL; else idx = ALI_PCM_IN_CHANNEL; result = snd_ali_alloc_pcm_channel(codec, idx); if (result >= 0) return result; else { snd_printk(KERN_ERR "ali_find_free_channel: " "record channel is busy now.\n"); return -1; } } /* playback... */ if (codec->spdif_support && (inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)) & ALI_SPDIF_OUT_CH_ENABLE)) { idx = ALI_SPDIF_OUT_CHANNEL; result = snd_ali_alloc_pcm_channel(codec, idx); if (result >= 0) return result; else snd_printk(KERN_ERR "ali_find_free_channel: " "S/PDIF out channel is in busy now.\n"); } for (idx = 0; idx < ALI_CHANNELS; idx++) { result = snd_ali_alloc_pcm_channel(codec, idx); if (result >= 0) return result; } snd_printk(KERN_ERR "ali_find_free_channel: no free channels.\n"); return -1;}static void snd_ali_free_channel_pcm(struct snd_ali *codec, int channel){ unsigned int idx = channel & 0x0000001f; snd_ali_printk("free_channel_pcm channel=%d\n",channel); if (channel < 0 || channel >= ALI_CHANNELS) return; if (!(codec->synth.chmap & (1 << idx))) { snd_printk(KERN_ERR "ali_free_channel_pcm: " "channel %d is not in use.\n", channel); return; } else { codec->synth.chmap &= ~(1 << idx); codec->synth.chcnt--; }}#if 0 /* not used */static void snd_ali_start_voice(struct snd_ali *codec, unsigned int channel){ unsigned int mask = 1 << (channel & 0x1f); snd_ali_printk("start_voice: channel=%d\n",channel); outl(mask, ALI_REG(codec,codec->chregs.regs.start));}#endifstatic void snd_ali_stop_voice(struct snd_ali *codec, unsigned int channel){ unsigned int mask = 1 << (channel & 0x1f); snd_ali_printk("stop_voice: channel=%d\n",channel); outl(mask, ALI_REG(codec, codec->chregs.regs.stop));}/* * S/PDIF Part */static void snd_ali_delay(struct snd_ali *codec,int interval){ unsigned long begintimer,currenttimer; begintimer = inl(ALI_REG(codec, ALI_STIMER)); currenttimer = inl(ALI_REG(codec, ALI_STIMER)); while (currenttimer < begintimer + interval) { if (snd_ali_stimer_ready(codec) < 0) break; currenttimer = inl(ALI_REG(codec, ALI_STIMER)); cpu_relax(); }}static void snd_ali_detect_spdif_rate(struct snd_ali *codec){ u16 wval; u16 count = 0; u8 bval, R1 = 0, R2; bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1)); bval |= 0x1F; outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL + 1)); while ((R1 < 0x0b || R1 > 0x0e) && R1 != 0x12 && count <= 50000) { count ++; snd_ali_delay(codec, 6); bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL + 1)); R1 = bval & 0x1F; } if (count > 50000) { snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n"); return; } for (count = 0; count <= 50000; count++) { snd_ali_delay(codec, 6); bval = inb(ALI_REG(codec,ALI_SPDIF_CTRL + 1)); R2 = bval & 0x1F; if (R2 != R1) R1 = R2; else break; } if (count > 50000) { snd_printk(KERN_ERR "ali_detect_spdif_rate: timeout!\n"); return; } if (R2 >= 0x0b && R2 <= 0x0e) { wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2)); wval &= 0xe0f0; wval |= (0x09 << 8) | 0x05; outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2)); bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)) & 0xf0; outb(bval | 0x02, ALI_REG(codec, ALI_SPDIF_CS + 3)); } else if (R2 == 0x12) { wval = inw(ALI_REG(codec, ALI_SPDIF_CTRL + 2)); wval &= 0xe0f0; wval |= (0x0e << 8) | 0x08; outw(wval, ALI_REG(codec, ALI_SPDIF_CTRL + 2)); bval = inb(ALI_REG(codec,ALI_SPDIF_CS + 3)) & 0xf0; outb(bval | 0x03, ALI_REG(codec, ALI_SPDIF_CS + 3)); }}static unsigned int snd_ali_get_spdif_in_rate(struct snd_ali *codec){ u32 dwRate; u8 bval; bval = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); bval &= 0x7f; bval |= 0x40; outb(bval, ALI_REG(codec, ALI_SPDIF_CTRL)); snd_ali_detect_spdif_rate(codec); bval = inb(ALI_REG(codec, ALI_SPDIF_CS + 3)); bval &= 0x0f; switch (bval) { case 0: dwRate = 44100; break; case 1: dwRate = 48000; break; case 2: dwRate = 32000; break; default: dwRate = 0; break; } return dwRate;}static void snd_ali_enable_spdif_in(struct snd_ali *codec){ unsigned int dwVal; dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)); dwVal |= ALI_SPDIF_IN_SUPPORT; outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); dwVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); dwVal |= 0x02; outb(dwVal, ALI_REG(codec, ALI_SPDIF_CTRL)); snd_ali_enable_special_channel(codec, ALI_SPDIF_IN_CHANNEL);}static void snd_ali_disable_spdif_in(struct snd_ali *codec){ unsigned int dwVal; dwVal = inl(ALI_REG(codec, ALI_GLOBAL_CONTROL)); dwVal &= ~ALI_SPDIF_IN_SUPPORT; outl(dwVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); snd_ali_disable_special_channel(codec, ALI_SPDIF_IN_CHANNEL); }static void snd_ali_set_spdif_out_rate(struct snd_ali *codec, unsigned int rate){ unsigned char bVal; unsigned int dwRate; switch (rate) { case 32000: dwRate = 0x300; break; case 48000: dwRate = 0x200; break; default: dwRate = 0; break; } bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); bVal &= (unsigned char)(~(1<<6)); bVal |= 0x80; /* select right */ outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL)); outb(dwRate | 0x20, ALI_REG(codec, ALI_SPDIF_CS + 2)); bVal &= ~0x80; /* select left */ outb(bVal, ALI_REG(codec, ALI_SPDIF_CTRL)); outw(rate | 0x10, ALI_REG(codec, ALI_SPDIF_CS + 2));}static void snd_ali_enable_spdif_out(struct snd_ali *codec){ unsigned short wVal; unsigned char bVal; struct pci_dev *pci_dev; pci_dev = codec->pci_m1533; if (pci_dev == NULL) return; pci_read_config_byte(pci_dev, 0x61, &bVal); bVal |= 0x40; pci_write_config_byte(pci_dev, 0x61, bVal); pci_read_config_byte(pci_dev, 0x7d, &bVal); bVal |= 0x01; pci_write_config_byte(pci_dev, 0x7d, bVal); pci_read_config_byte(pci_dev, 0x7e, &bVal); bVal &= (~0x20); bVal |= 0x10; pci_write_config_byte(pci_dev, 0x7e, bVal); bVal = inb(ALI_REG(codec, ALI_SCTRL)); outb(bVal | ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL)); bVal = inb(ALI_REG(codec, ALI_SPDIF_CTRL)); outb(bVal & ALI_SPDIF_OUT_CH_STATUS, ALI_REG(codec, ALI_SPDIF_CTRL)); wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL)); wVal |= ALI_SPDIF_OUT_SEL_PCM; outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); snd_ali_disable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);}static void snd_ali_enable_spdif_chnout(struct snd_ali *codec){ unsigned short wVal; wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL)); wVal &= ~ALI_SPDIF_OUT_SEL_PCM; outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL));/* wVal = inw(ALI_REG(codec, ALI_SPDIF_CS)); if (flag & ALI_SPDIF_OUT_NON_PCM) wVal |= 0x0002; else wVal &= (~0x0002); outw(wVal, ALI_REG(codec, ALI_SPDIF_CS));*/ snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);}static void snd_ali_disable_spdif_chnout(struct snd_ali *codec){ unsigned short wVal; wVal = inw(ALI_REG(codec, ALI_GLOBAL_CONTROL)); wVal |= ALI_SPDIF_OUT_SEL_PCM; outw(wVal, ALI_REG(codec, ALI_GLOBAL_CONTROL)); snd_ali_enable_special_channel(codec, ALI_SPDIF_OUT_CHANNEL);}static void snd_ali_disable_spdif_out(struct snd_ali *codec){ unsigned char bVal; bVal = inb(ALI_REG(codec, ALI_SCTRL)); outb(bVal & ~ALI_SPDIF_OUT_ENABLE, ALI_REG(codec, ALI_SCTRL)); snd_ali_disable_spdif_chnout(codec);}static void snd_ali_update_ptr(struct snd_ali *codec, int channel){ struct snd_ali_voice *pvoice; struct snd_pcm_runtime *runtime; struct snd_ali_channel_control *pchregs; unsigned int old, mask;#ifdef ALI_DEBUG unsigned int temp, cspf;#endif pchregs = &(codec->chregs); /* check if interrupt occurred for channel */ old = pchregs->data.aint; mask = 1U << (channel & 0x1f); if (!(old & mask)) return; pvoice = &codec->synth.voices[channel]; runtime = pvoice->substream->runtime; udelay(100); spin_lock(&codec->reg_lock); if (pvoice->pcm && pvoice->substream) { /* pcm interrupt */#ifdef ALI_DEBUG outb((u8)(pvoice->number), ALI_REG(codec, ALI_GC_CIR)); temp = inw(ALI_REG(codec, ALI_CSO_ALPHA_FMS + 2)); cspf = (inl(ALI_REG(codec, ALI_CSPF)) & mask) == mask;#endif if (pvoice->running) { snd_ali_printk("update_ptr: cso=%4.4x cspf=%d.\n", (u16)temp, cspf); spin_unlock(&codec->reg_lock); snd_pcm_period_elapsed(pvoice->substream); spin_lock(&codec->reg_lock); } else { snd_ali_stop_voice(codec, channel); snd_ali_disable_voice_irq(codec, channel); } } else if (codec->synth.voices[channel].synth) { /* synth interrupt */ } else if (codec->synth.voices[channel].midi) { /* midi interrupt */ } else { /* unknown interrupt */ snd_ali_stop_voice(codec, channel); snd_ali_disable_voice_irq(codec, channel); } spin_unlock(&codec->reg_lock); outl(mask,ALI_REG(codec,pchregs->regs.aint)); pchregs->data.aint = old & (~mask);}static irqreturn_t snd_ali_card_interrupt(int irq, void *dev_id){ struct snd_ali *codec = dev_id; int channel; unsigned int audio_int; struct snd_ali_channel_control *pchregs; if (codec == NULL || !codec->hw_initialized) return IRQ_NONE; audio_int = inl(ALI_REG(codec, ALI_MISCINT)); if (!audio_int) return IRQ_NONE; pchregs = &(codec->chregs); if (audio_int & ADDRESS_IRQ) { /* get interrupt status for all channels */ pchregs->data.aint = inl(ALI_REG(codec, pchregs->regs.aint)); for (channel = 0; channel < ALI_CHANNELS; channel++) snd_ali_update_ptr(codec, channel); } outl((TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), ALI_REG(codec, ALI_MISCINT)); return IRQ_HANDLED;}static struct snd_ali_voice *snd_ali_alloc_voice(struct snd_ali * codec, int type, int rec, int channel){ struct snd_ali_voice *pvoice; int idx; snd_ali_printk("alloc_voice: type=%d rec=%d\n", type, rec); spin_lock_irq(&codec->voice_alloc); if (type == SNDRV_ALI_VOICE_TYPE_PCM) { idx = channel > 0 ? snd_ali_alloc_pcm_channel(codec, channel) : snd_ali_find_free_channel(codec,rec); if (idx < 0) { snd_printk(KERN_ERR "ali_alloc_voice: err.\n"); spin_unlock_irq(&codec->voice_alloc); return NULL; } pvoice = &(codec->synth.voices[idx]); pvoice->codec = codec; pvoice->use = 1; pvoice->pcm = 1; pvoice->mode = rec; spin_unlock_irq(&codec->voice_alloc); return pvoice; } spin_unlock_irq(&codec->voice_alloc); return NULL;}static void snd_ali_free_voice(struct snd_ali * codec, struct snd_ali_voice *pvoice){ void (*private_free)(void *); void *private_data; snd_ali_printk("free_voice: channel=%d\n",pvoice->number); if (pvoice == NULL || !pvoice->use) return; snd_ali_clear_voices(codec, pvoice->number, pvoice->number); spin_lock_irq(&codec->voice_alloc); private_free = pvoice->private_free; private_data = pvoice->private_data; pvoice->private_free = NULL; pvoice->private_data = NULL; if (pvoice->pcm) snd_ali_free_channel_pcm(codec, pvoice->number); pvoice->use = pvoice->pcm = pvoice->synth = 0; pvoice->substream = NULL; spin_unlock_irq(&codec->voice_alloc); if (private_free) private_free(private_data);}static void snd_ali_clear_voices(struct snd_ali *codec, unsigned int v_min, unsigned int v_max){ unsigned int i; for (i = v_min; i <= v_max; i++) { snd_ali_stop_voice(codec, i); snd_ali_disable_voice_irq(codec, i); }}static void snd_ali_write_voice_regs(struct snd_ali *codec, unsigned int Channel, unsigned int LBA, unsigned int CSO, unsigned int ESO, unsigned int DELTA, unsigned int ALPHA_FMS, unsigned int GVSEL, unsigned int PAN, unsigned int VOL, unsigned int CTRL, unsigned int EC){ unsigned int ctlcmds[4]; outb((unsigned char)(Channel & 0x001f), ALI_REG(codec, ALI_GC_CIR)); ctlcmds[0] = (CSO << 16) | (ALPHA_FMS & 0x0000ffff); ctlcmds[1] = LBA; ctlcmds[2] = (ESO << 16) | (DELTA & 0x0ffff); ctlcmds[3] = (GVSEL << 31) | ((PAN & 0x0000007f) << 24) | ((VOL & 0x000000ff) << 16) | ((CTRL & 0x0000000f) << 12) | (EC & 0x00000fff); outb(Channel, ALI_REG(codec, ALI_GC_CIR)); outl(ctlcmds[0], ALI_REG(codec, ALI_CSO_ALPHA_FMS)); outl(ctlcmds[1], ALI_REG(codec, ALI_LBA)); outl(ctlcmds[2], ALI_REG(codec, ALI_ESO_DELTA)); outl(ctlcmds[3], ALI_REG(codec, ALI_GVSEL_PAN_VOC_CTRL_EC)); outl(0x30000000, ALI_REG(codec, ALI_EBUF1)); /* Still Mode */ outl(0x30000000, ALI_REG(codec, ALI_EBUF2)); /* Still Mode */}static unsigned int snd_ali_convert_rate(unsigned int rate, int rec){ unsigned int delta; if (rate < 4000) rate = 4000; if (rate > 48000) rate = 48000; if (rec) { if (rate == 44100) delta = 0x116a; else if (rate == 8000) delta = 0x6000; else if (rate == 48000) delta = 0x1000; else delta = ((48000 << 12) / rate) & 0x0000ffff; } else { if (rate == 44100) delta = 0xeb3; else if (rate == 8000) delta = 0x2ab; else if (rate == 48000) delta = 0x1000; else delta = (((rate << 12) + rate) / 48000) & 0x0000ffff; } return delta;}static unsigned int snd_ali_control_mode(struct snd_pcm_substream *substream){ unsigned int CTRL; struct snd_pcm_runtime *runtime = substream->runtime; /* set ctrl mode CTRL default: 8-bit (unsigned) mono, loop mode enabled */ CTRL = 0x00000001; if (snd_pcm_format_width(runtime->format) == 16) CTRL |= 0x00000008; /* 16-bit data */ if (!snd_pcm_format_unsigned(runtime->format)) CTRL |= 0x00000002; /* signed data */ if (runtime->channels > 1) CTRL |= 0x00000004; /* stereo data */ return CTRL;}/* * PCM part */static int snd_ali_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_ali *codec = snd_pcm_substream_chip(substream); struct snd_pcm_substream *s; unsigned int what, whati, capture_flag; struct snd_ali_voice *pvoice, *evoice; unsigned int val; int do_start; switch (cmd) { case SNDRV_PCM_TRIGGER_START: case SNDRV_PCM_TRIGGER_RESUME: do_start = 1; break; case SNDRV_PCM_TRIGGER_STOP: case SNDRV_PCM_TRIGGER_SUSPEND: do_start = 0; break; default: return -EINVAL; } what = whati = capture_flag = 0; snd_pcm_group_for_each_entry(s, substream) { if ((struct snd_ali *) snd_pcm_substream_chip(s) == codec) { pvoice = s->runtime->private_data; evoice = pvoice->extra; what |= 1 << (pvoice->number & 0x1f); if (evoice == NULL) whati |= 1 << (pvoice->number & 0x1f); else { whati |= 1 << (evoice->number & 0x1f); what |= 1 << (evoice->number & 0x1f); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -