ac97_pcm.c
来自「优龙2410linux2.6.8内核源代码」· C语言 代码 · 共 616 行 · 第 1/2 页
C
616 行
es = ac97->regs[AC97_EXTENDED_STATUS] &= ~AC97_EI_DACS_SLOT_MASK; switch (ac97->addr) { case 1: case 2: es |= (1<<AC97_EI_DACS_SLOT_SHIFT); break; case 3: es |= (2<<AC97_EI_DACS_SLOT_SHIFT); break; } snd_ac97_write_cache(ac97, AC97_EXTENDED_STATUS, es); } switch (ac97->addr) { case 0: slots |= (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT); if (ac97->scaps & AC97_SCAP_SURROUND_DAC) slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT); if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE); if (ac97->ext_id & AC97_EI_SPDIF) { if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC)) *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT); else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)) *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1); else *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2); } *rate_table = 0; break; case 1: case 2: slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT); if (ac97->scaps & AC97_SCAP_SURROUND_DAC) slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE); if (ac97->ext_id & AC97_EI_SPDIF) { if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC)) *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1); else *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2); } *rate_table = 1; break; case 3: slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE); if (ac97->ext_id & AC97_EI_SPDIF) *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2); *rate_table = 2; break; } return slots; } else { unsigned short slots; slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT); if (ac97->scaps & AC97_SCAP_SURROUND_DAC) slots |= (1<<AC97_SLOT_PCM_SLEFT)|(1<<AC97_SLOT_PCM_SRIGHT); if (ac97->scaps & AC97_SCAP_CENTER_LFE_DAC) slots |= (1<<AC97_SLOT_PCM_CENTER)|(1<<AC97_SLOT_LFE); if (ac97->ext_id & AC97_EI_SPDIF) { if (!(ac97->scaps & AC97_SCAP_SURROUND_DAC)) *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT)|(1<<AC97_SLOT_SPDIF_RIGHT); else if (!(ac97->scaps & AC97_SCAP_CENTER_LFE_DAC)) *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT1)|(1<<AC97_SLOT_SPDIF_RIGHT1); else *spdif_slots = (1<<AC97_SLOT_SPDIF_LEFT2)|(1<<AC97_SLOT_SPDIF_RIGHT2); } *rate_table = 0; return slots; }}static unsigned short get_cslots(ac97_t *ac97){ unsigned short slots; if (!ac97_is_audio(ac97)) return 0; slots = (1<<AC97_SLOT_PCM_LEFT)|(1<<AC97_SLOT_PCM_RIGHT); slots |= (1<<AC97_SLOT_MIC); return slots;}static unsigned int get_rates(struct ac97_pcm *pcm, unsigned int cidx, unsigned short slots, int dbl){ int i, idx; unsigned int rates = ~0; unsigned char reg; for (i = 3; i < 12; i++) { if (!(slots & (1 << i))) continue; reg = get_slot_reg(pcm, cidx, i, dbl); switch (reg) { case AC97_PCM_FRONT_DAC_RATE: idx = AC97_RATES_FRONT_DAC; break; case AC97_PCM_SURR_DAC_RATE: idx = AC97_RATES_SURR_DAC; break; case AC97_PCM_LFE_DAC_RATE: idx = AC97_RATES_LFE_DAC; break; case AC97_PCM_LR_ADC_RATE: idx = AC97_RATES_ADC; break; case AC97_PCM_MIC_ADC_RATE: idx = AC97_RATES_MIC_ADC; break; default: idx = AC97_RATES_SPDIF; break; } rates &= pcm->r[dbl].codec[cidx]->rates[idx]; } return rates;}/** * snd_ac97_pcm_assign - assign AC97 slots to given PCM streams * @bus: the ac97 bus instance * @pcms_count: count of PCMs to be assigned * @pcms: PCMs to be assigned * * It assigns available AC97 slots for given PCMs. If none or only * some slots are available, pcm->xxx.slots and pcm->xxx.rslots[] members * are reduced and might be zero. */int snd_ac97_pcm_assign(ac97_bus_t *bus, unsigned short pcms_count, const struct ac97_pcm *pcms){ int i, j, k; const struct ac97_pcm *pcm; struct ac97_pcm *rpcms, *rpcm; unsigned short avail_slots[2][4]; unsigned char rate_table[2][4]; unsigned short tmp, slots; unsigned short spdif_slots[4]; unsigned int rates; ac97_t *codec; rpcms = snd_kcalloc(sizeof(struct ac97_pcm) * pcms_count, GFP_KERNEL); if (rpcms == NULL) return -ENOMEM; memset(avail_slots, 0, sizeof(avail_slots)); memset(rate_table, 0, sizeof(rate_table)); memset(spdif_slots, 0, sizeof(spdif_slots)); for (i = 0; i < 4; i++) { codec = bus->codec[i]; if (!codec) continue; avail_slots[0][i] = get_pslots(codec, &rate_table[0][i], &spdif_slots[i]); avail_slots[1][i] = get_cslots(codec); if (!(codec->scaps & AC97_SCAP_INDEP_SDIN)) { for (j = 0; j < i; j++) { if (bus->codec[j]) avail_slots[1][i] &= ~avail_slots[1][j]; } } } /* FIXME: add double rate allocation */ /* first step - exclusive devices */ for (i = 0; i < pcms_count; i++) { pcm = &pcms[i]; rpcm = &rpcms[i]; /* low-level driver thinks that it's more clever */ if (pcm->copy_flag) { *rpcm = *pcm; continue; } rpcm->stream = pcm->stream; rpcm->exclusive = pcm->exclusive; rpcm->spdif = pcm->spdif; rpcm->private_value = pcm->private_value; rpcm->bus = bus; rpcm->rates = ~0; slots = pcm->r[0].slots; for (j = 0; j < 4 && slots; j++) { if (!bus->codec[j]) continue; rates = ~0; if (pcm->spdif && pcm->stream == 0) tmp = spdif_slots[j]; else tmp = avail_slots[pcm->stream][j]; if (pcm->exclusive) { /* exclusive access */ tmp &= slots; for (k = 0; k < i; k++) { if (rpcm->stream == rpcms[k].stream) tmp &= ~rpcms[k].r[0].rslots[j]; } } else { /* non-exclusive access */ tmp &= pcm->r[0].slots; } if (tmp) { rpcm->r[0].rslots[j] = tmp; rpcm->r[0].codec[j] = bus->codec[j]; rpcm->r[0].rate_table[j] = rate_table[pcm->stream][j]; rates = get_rates(rpcm, j, tmp, 0); if (pcm->exclusive) avail_slots[pcm->stream][j] &= ~tmp; } slots &= ~tmp; rpcm->r[0].slots |= tmp; rpcm->rates &= rates; } if (rpcm->rates == ~0) rpcm->rates = 0; /* not used */ } bus->pcms_count = pcms_count; bus->pcms = rpcms; return 0;}/** * snd_ac97_pcm_open - opens the given AC97 pcm * @pcm: the ac97 pcm instance * @rate: rate in Hz, if codec does not support VRA, this value must be 48000Hz * @cfg: output stream characteristics * @slots: a subset of allocated slots (snd_ac97_pcm_assign) for this pcm * * It locks the specified slots and sets the given rate to AC97 registers. */int snd_ac97_pcm_open(struct ac97_pcm *pcm, unsigned int rate, enum ac97_pcm_cfg cfg, unsigned short slots){ ac97_bus_t *bus; int i, cidx, r = 0, ok_flag; unsigned int reg_ok = 0; unsigned char reg; int err = 0; if (rate > 48000) /* FIXME: add support for double rate */ return -EINVAL; bus = pcm->bus; if (cfg == AC97_PCM_CFG_SPDIF) { int err; for (cidx = 0; cidx < 4; cidx++) if (bus->codec[cidx] && (bus->codec[cidx]->ext_id & AC97_EI_SPDIF)) { err = set_spdif_rate(bus->codec[cidx], rate); if (err < 0) return err; } } spin_lock_irq(&pcm->bus->bus_lock); for (i = 3; i < 12; i++) { if (!(slots & (1 << i))) continue; ok_flag = 0; for (cidx = 0; cidx < 4; cidx++) { if (bus->used_slots[pcm->stream][cidx] & (1 << i)) { spin_unlock_irq(&pcm->bus->bus_lock); err = -EBUSY; goto error; } if (pcm->r[r].rslots[cidx] & (1 << i)) { bus->used_slots[pcm->stream][cidx] |= (1 << i); ok_flag++; } } if (!ok_flag) { spin_unlock_irq(&pcm->bus->bus_lock); snd_printk(KERN_ERR "cannot find configuration for AC97 slot %i\n", i); err = -EAGAIN; goto error; } } spin_unlock_irq(&pcm->bus->bus_lock); for (i = 3; i < 12; i++) { if (!(slots & (1 << i))) continue; for (cidx = 0; cidx < 4; cidx++) { if (pcm->r[r].rslots[cidx] & (1 << i)) { reg = get_slot_reg(pcm, cidx, i, r); if (reg == 0xff) { snd_printk(KERN_ERR "invalid AC97 slot %i?\n", i); continue; } if (reg_ok & (1 << (reg - AC97_PCM_FRONT_DAC_RATE))) continue; //printk(KERN_DEBUG "setting ac97 reg 0x%x to rate %d\n", reg, rate); err = snd_ac97_set_rate(pcm->r[r].codec[cidx], reg, rate); if (err < 0) snd_printk(KERN_ERR "error in snd_ac97_set_rate: cidx=%d, reg=0x%x, rate=%d, err=%d\n", cidx, reg, rate, err); else reg_ok |= (1 << (reg - AC97_PCM_FRONT_DAC_RATE)); } } } pcm->aslots = slots; return 0; error: pcm->aslots = slots; snd_ac97_pcm_close(pcm); return err;}/** * snd_ac97_pcm_close - closes the given AC97 pcm * @pcm: the ac97 pcm instance * * It frees the locked AC97 slots. */int snd_ac97_pcm_close(struct ac97_pcm *pcm){ ac97_bus_t *bus; unsigned short slots = pcm->aslots; int i, cidx; bus = pcm->bus; spin_lock_irq(&pcm->bus->bus_lock); for (i = 3; i < 12; i++) { if (!(slots & (1 << i))) continue; for (cidx = 0; cidx < 4; cidx++) bus->used_slots[pcm->stream][cidx] &= ~(1 << i); } pcm->aslots = 0; spin_unlock_irq(&pcm->bus->bus_lock); return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?