⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hal2.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* *  Driver for A2 audio system used in SGI machines *  Copyright (c) 2001, 2002, 2003 Ladislav Michl <ladis@linux-mips.org> *   *  Based on Ulf Carlsson's code. * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License version 2 as  *  published by the Free Software Foundation. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *  Supported devices: *  /dev/dsp    standard dsp device, (mostly) OSS compatible *  /dev/mixer	standard mixer device, (mostly) OSS compatible * */#include <linux/kernel.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/poll.h>#include <linux/interrupt.h>#include <linux/dma-mapping.h>#include <linux/sound.h>#include <linux/soundcard.h>#include <linux/mutex.h>#include <asm/io.h>#include <asm/sgi/hpc3.h>#include <asm/sgi/ip22.h>#include "hal2.h"#if 0#define DEBUG(args...)		printk(args)#else#define DEBUG(args...)#endif#if 0 #define DEBUG_MIX(args...)	printk(args)#else#define DEBUG_MIX(args...)#endif/* * Before touching these look how it works. It is a bit unusual I know, * but it helps to keep things simple. This driver is considered complete * and I won't add any new features although hardware has many cool * capabilities. * (Historical note: HAL2 driver was first written by Ulf Carlsson - ALSA * 0.3 running with 2.2.x kernel. Then ALSA changed completely and it * seemed easier to me to write OSS driver from scratch - this one. Now * when ALSA is official part of 2.6 kernel it's time to write ALSA driver * using (hopefully) final version of ALSA interface) */#define H2_BLOCK_SIZE	1024#define H2_ADC_BUFSIZE	8192#define H2_DAC_BUFSIZE	16834struct hal2_pbus {	struct hpc3_pbus_dmacregs *pbus;	int pbusnr;	unsigned int ctrl;		/* Current state of pbus->pbdma_ctrl */};struct hal2_desc {	struct hpc_dma_desc desc;	u32 cnt;			/* don't touch, it is also padding */};struct hal2_codec {	unsigned char *buffer;	struct hal2_desc *desc;	int desc_count;	int tail, head;			/* tail index, head index */	struct hal2_pbus pbus;	unsigned int format;		/* Audio data format */	int voices;			/* mono/stereo */	unsigned int sample_rate;	unsigned int master;		/* Master frequency */	unsigned short mod;		/* MOD value */	unsigned short inc;		/* INC value */	wait_queue_head_t dma_wait;	spinlock_t lock;	struct mutex sem;	int usecount;			/* recording and playback are					 * independent */};#define H2_MIX_OUTPUT_ATT	0#define H2_MIX_INPUT_GAIN	1#define H2_MIXERS		2struct hal2_mixer {	int modcnt;	unsigned int master;	unsigned int volume[H2_MIXERS];};struct hal2_card {	int dev_dsp;			/* audio device */	int dev_mixer;			/* mixer device */	int dev_midi;			/* midi device */	struct hal2_ctl_regs *ctl_regs;	/* HAL2 ctl registers */	struct hal2_aes_regs *aes_regs;	/* HAL2 aes registers */	struct hal2_vol_regs *vol_regs;	/* HAL2 vol registers */	struct hal2_syn_regs *syn_regs;	/* HAL2 syn registers */	struct hal2_codec dac;	struct hal2_codec adc;	struct hal2_mixer mixer;};#define H2_INDIRECT_WAIT(regs)	while (regs->isr & H2_ISR_TSTATUS);#define H2_READ_ADDR(addr)	(addr | (1<<7))#define H2_WRITE_ADDR(addr)	(addr)static char *hal2str = "HAL2";/* * I doubt anyone has a machine with two HAL2 cards. It's possible to * have two HPC's, so it is probably possible to have two HAL2 cards. * Try to deal with it, but note that it is not tested. */#define MAXCARDS	2static struct hal2_card* hal2_card[MAXCARDS];static const struct {	unsigned char idx:4, avail:1;} mixtable[SOUND_MIXER_NRDEVICES] = {	[SOUND_MIXER_PCM]	= { H2_MIX_OUTPUT_ATT, 1 },	/* voice */	[SOUND_MIXER_MIC]	= { H2_MIX_INPUT_GAIN, 1 },	/* mic */};#define H2_SUPPORTED_FORMATS	(AFMT_S16_LE | AFMT_S16_BE)static inline void hal2_isr_write(struct hal2_card *hal2, u16 val){	hal2->ctl_regs->isr = val;}static inline u16 hal2_isr_look(struct hal2_card *hal2){	return hal2->ctl_regs->isr;}static inline u16 hal2_rev_look(struct hal2_card *hal2){	return hal2->ctl_regs->rev;}#ifdef HAL2_DUMP_REGSstatic u16 hal2_i_look16(struct hal2_card *hal2, u16 addr){	struct hal2_ctl_regs *regs = hal2->ctl_regs;	regs->iar = H2_READ_ADDR(addr);	H2_INDIRECT_WAIT(regs);	return regs->idr0;}#endifstatic u32 hal2_i_look32(struct hal2_card *hal2, u16 addr){	u32 ret;	struct hal2_ctl_regs *regs = hal2->ctl_regs;	regs->iar = H2_READ_ADDR(addr);	H2_INDIRECT_WAIT(regs);	ret = regs->idr0 & 0xffff;	regs->iar = H2_READ_ADDR(addr | 0x1);	H2_INDIRECT_WAIT(regs);	ret |= (regs->idr0 & 0xffff) << 16;	return ret;}static void hal2_i_write16(struct hal2_card *hal2, u16 addr, u16 val){	struct hal2_ctl_regs *regs = hal2->ctl_regs;	regs->idr0 = val;	regs->idr1 = 0;	regs->idr2 = 0;	regs->idr3 = 0;	regs->iar = H2_WRITE_ADDR(addr);	H2_INDIRECT_WAIT(regs);}static void hal2_i_write32(struct hal2_card *hal2, u16 addr, u32 val){	struct hal2_ctl_regs *regs = hal2->ctl_regs;	regs->idr0 = val & 0xffff;	regs->idr1 = val >> 16;	regs->idr2 = 0;	regs->idr3 = 0;	regs->iar = H2_WRITE_ADDR(addr);	H2_INDIRECT_WAIT(regs);}static void hal2_i_setbit16(struct hal2_card *hal2, u16 addr, u16 bit){	struct hal2_ctl_regs *regs = hal2->ctl_regs;	regs->iar = H2_READ_ADDR(addr);	H2_INDIRECT_WAIT(regs);	regs->idr0 = (regs->idr0 & 0xffff) | bit;	regs->idr1 = 0;	regs->idr2 = 0;	regs->idr3 = 0;	regs->iar = H2_WRITE_ADDR(addr);	H2_INDIRECT_WAIT(regs);}static void hal2_i_setbit32(struct hal2_card *hal2, u16 addr, u32 bit){	u32 tmp;	struct hal2_ctl_regs *regs = hal2->ctl_regs;	regs->iar = H2_READ_ADDR(addr);	H2_INDIRECT_WAIT(regs);	tmp = (regs->idr0 & 0xffff) | (regs->idr1 << 16) | bit;	regs->idr0 = tmp & 0xffff;	regs->idr1 = tmp >> 16;	regs->idr2 = 0;	regs->idr3 = 0;	regs->iar = H2_WRITE_ADDR(addr);	H2_INDIRECT_WAIT(regs);}static void hal2_i_clearbit16(struct hal2_card *hal2, u16 addr, u16 bit){	struct hal2_ctl_regs *regs = hal2->ctl_regs;	regs->iar = H2_READ_ADDR(addr);	H2_INDIRECT_WAIT(regs);	regs->idr0 = (regs->idr0 & 0xffff) & ~bit;	regs->idr1 = 0;	regs->idr2 = 0;	regs->idr3 = 0;	regs->iar = H2_WRITE_ADDR(addr);	H2_INDIRECT_WAIT(regs);}#if 0static void hal2_i_clearbit32(struct hal2_card *hal2, u16 addr, u32 bit){	u32 tmp;	hal2_ctl_regs_t *regs = hal2->ctl_regs;	regs->iar = H2_READ_ADDR(addr);	H2_INDIRECT_WAIT(regs);	tmp = ((regs->idr0 & 0xffff) | (regs->idr1 << 16)) & ~bit;	regs->idr0 = tmp & 0xffff;	regs->idr1 = tmp >> 16;	regs->idr2 = 0;	regs->idr3 = 0;	regs->iar = H2_WRITE_ADDR(addr);	H2_INDIRECT_WAIT(regs);}#endif#ifdef HAL2_DUMP_REGSstatic void hal2_dump_regs(struct hal2_card *hal2){	DEBUG("isr: %08hx ", hal2_isr_look(hal2));	DEBUG("rev: %08hx\n", hal2_rev_look(hal2));	DEBUG("relay: %04hx\n", hal2_i_look16(hal2, H2I_RELAY_C));	DEBUG("port en: %04hx ", hal2_i_look16(hal2, H2I_DMA_PORT_EN));	DEBUG("dma end: %04hx ", hal2_i_look16(hal2, H2I_DMA_END));	DEBUG("dma drv: %04hx\n", hal2_i_look16(hal2, H2I_DMA_DRV));	DEBUG("syn ctl: %04hx ", hal2_i_look16(hal2, H2I_SYNTH_C));	DEBUG("aesrx ctl: %04hx ", hal2_i_look16(hal2, H2I_AESRX_C));	DEBUG("aestx ctl: %04hx ", hal2_i_look16(hal2, H2I_AESTX_C));	DEBUG("dac ctl1: %04hx ", hal2_i_look16(hal2, H2I_ADC_C1));	DEBUG("dac ctl2: %08x ", hal2_i_look32(hal2, H2I_ADC_C2));	DEBUG("adc ctl1: %04hx ", hal2_i_look16(hal2, H2I_DAC_C1));	DEBUG("adc ctl2: %08x ", hal2_i_look32(hal2, H2I_DAC_C2));	DEBUG("syn map: %04hx\n", hal2_i_look16(hal2, H2I_SYNTH_MAP_C));	DEBUG("bres1 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES1_C1));	DEBUG("bres1 ctl2: %04x ", hal2_i_look32(hal2, H2I_BRES1_C2));	DEBUG("bres2 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES2_C1));	DEBUG("bres2 ctl2: %04x ", hal2_i_look32(hal2, H2I_BRES2_C2));	DEBUG("bres3 ctl1: %04hx ", hal2_i_look16(hal2, H2I_BRES3_C1));	DEBUG("bres3 ctl2: %04x\n", hal2_i_look32(hal2, H2I_BRES3_C2));}#endifstatic struct hal2_card* hal2_dsp_find_card(int minor){	int i;	for (i = 0; i < MAXCARDS; i++)		if (hal2_card[i] != NULL && hal2_card[i]->dev_dsp == minor)			return hal2_card[i];	return NULL;}static struct hal2_card* hal2_mixer_find_card(int minor){	int i;	for (i = 0; i < MAXCARDS; i++)		if (hal2_card[i] != NULL && hal2_card[i]->dev_mixer == minor)			return hal2_card[i];	return NULL;}static void hal2_inc_head(struct hal2_codec *codec){	codec->head++;	if (codec->head == codec->desc_count)		codec->head = 0;}static void hal2_inc_tail(struct hal2_codec *codec){	codec->tail++;	if (codec->tail == codec->desc_count)		codec->tail = 0;}static void hal2_dac_interrupt(struct hal2_codec *dac){	int running;	spin_lock(&dac->lock);	/* if tail buffer contains zero samples DMA stream was already	 * stopped */	running = dac->desc[dac->tail].cnt;	dac->desc[dac->tail].cnt = 0;	dac->desc[dac->tail].desc.cntinfo = HPCDMA_XIE | HPCDMA_EOX;	/* we just proccessed empty buffer, don't update tail pointer */	if (running)		hal2_inc_tail(dac);	spin_unlock(&dac->lock);	wake_up(&dac->dma_wait);}static void hal2_adc_interrupt(struct hal2_codec *adc){	int running;	spin_lock(&adc->lock);	/* if head buffer contains nonzero samples DMA stream was already	 * stopped */	running = !adc->desc[adc->head].cnt;	adc->desc[adc->head].cnt = H2_BLOCK_SIZE;	adc->desc[adc->head].desc.cntinfo = HPCDMA_XIE | HPCDMA_EOR;	/* we just proccessed empty buffer, don't update head pointer */	if (running)		hal2_inc_head(adc);	spin_unlock(&adc->lock);	wake_up(&adc->dma_wait);}static irqreturn_t hal2_interrupt(int irq, void *dev_id){	struct hal2_card *hal2 = dev_id;	irqreturn_t ret = IRQ_NONE;	/* decide what caused this interrupt */	if (hal2->dac.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT) {		hal2_dac_interrupt(&hal2->dac);		ret = IRQ_HANDLED;	}	if (hal2->adc.pbus.pbus->pbdma_ctrl & HPC3_PDMACTRL_INT) {		hal2_adc_interrupt(&hal2->adc);		ret = IRQ_HANDLED;	}	return ret;}static int hal2_compute_rate(struct hal2_codec *codec, unsigned int rate){	unsigned short mod;		DEBUG("rate: %d\n", rate);		if (rate < 4000) rate = 4000;	else if (rate > 48000) rate = 48000;	if (44100 % rate < 48000 % rate) {		mod = 4 * 44100 / rate;		codec->master = 44100;	} else {		mod = 4 * 48000 / rate;		codec->master = 48000;	}	codec->inc = 4;	codec->mod = mod;	rate = 4 * codec->master / mod;	DEBUG("real_rate: %d\n", rate);	return rate;}static void hal2_set_dac_rate(struct hal2_card *hal2){	unsigned int master = hal2->dac.master;	int inc = hal2->dac.inc;	int mod = hal2->dac.mod;	DEBUG("master: %d inc: %d mod: %d\n", master, inc, mod);		hal2_i_write16(hal2, H2I_BRES1_C1, (master == 44100) ? 1 : 0);	hal2_i_write32(hal2, H2I_BRES1_C2, ((0xffff & (inc - mod - 1)) << 16) | inc);}static void hal2_set_adc_rate(struct hal2_card *hal2){	unsigned int master = hal2->adc.master;	int inc = hal2->adc.inc;	int mod = hal2->adc.mod;	DEBUG("master: %d inc: %d mod: %d\n", master, inc, mod);		hal2_i_write16(hal2, H2I_BRES2_C1, (master == 44100) ? 1 : 0);	hal2_i_write32(hal2, H2I_BRES2_C2, ((0xffff & (inc - mod - 1)) << 16) | inc);}static void hal2_setup_dac(struct hal2_card *hal2){	unsigned int fifobeg, fifoend, highwater, sample_size;	struct hal2_pbus *pbus = &hal2->dac.pbus;	DEBUG("hal2_setup_dac\n");		/* Now we set up some PBUS information. The PBUS needs information about	 * what portion of the fifo it will use. If it's receiving or	 * transmitting, and finally whether the stream is little endian or big	 * endian. The information is written later, on the start call.	 */	sample_size = 2 * hal2->dac.voices;	/* Fifo should be set to hold exactly four samples. Highwater mark	 * should be set to two samples. */	highwater = (sample_size * 2) >> 1;	/* halfwords */	fifobeg = 0;				/* playback is first */	fifoend = (sample_size * 4) >> 3;	/* doublewords */	pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_LD |		     (highwater << 8) | (fifobeg << 16) | (fifoend << 24) |		     (hal2->dac.format & AFMT_S16_LE ? HPC3_PDMACTRL_SEL : 0);	/* We disable everything before we do anything at all */	pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;	hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECTX);	/* Setup the HAL2 for playback */	hal2_set_dac_rate(hal2);	/* Set endianess */	if (hal2->dac.format & AFMT_S16_LE)		hal2_i_setbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECTX);	else		hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECTX);	/* Set DMA bus */	hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));	/* We are using 1st Bresenham clock generator for playback */	hal2_i_write16(hal2, H2I_DAC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)			| (1 << H2I_C1_CLKID_SHIFT)			| (hal2->dac.voices << H2I_C1_DATAT_SHIFT));}static void hal2_setup_adc(struct hal2_card *hal2){	unsigned int fifobeg, fifoend, highwater, sample_size;	struct hal2_pbus *pbus = &hal2->adc.pbus;	DEBUG("hal2_setup_adc\n");	sample_size = 2 * hal2->adc.voices;	highwater = (sample_size * 2) >> 1;		/* halfwords */	fifobeg = (4 * 4) >> 3;				/* record is second */	fifoend = (4 * 4 + sample_size * 4) >> 3;	/* doublewords */	pbus->ctrl = HPC3_PDMACTRL_RT | HPC3_PDMACTRL_RCV | HPC3_PDMACTRL_LD | 		     (highwater << 8) | (fifobeg << 16) | (fifoend << 24) |		     (hal2->adc.format & AFMT_S16_LE ? HPC3_PDMACTRL_SEL : 0);	pbus->pbus->pbdma_ctrl = HPC3_PDMACTRL_LD;	hal2_i_clearbit16(hal2, H2I_DMA_PORT_EN, H2I_DMA_PORT_EN_CODECR);	/* Setup the HAL2 for record */	hal2_set_adc_rate(hal2);	/* Set endianess */	if (hal2->adc.format & AFMT_S16_LE)		hal2_i_setbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECR);	else		hal2_i_clearbit16(hal2, H2I_DMA_END, H2I_DMA_END_CODECR);	/* Set DMA bus */	hal2_i_setbit16(hal2, H2I_DMA_DRV, (1 << pbus->pbusnr));	/* We are using 2nd Bresenham clock generator for record */	hal2_i_write16(hal2, H2I_ADC_C1, (pbus->pbusnr << H2I_C1_DMA_SHIFT)			| (2 << H2I_C1_CLKID_SHIFT)			| (hal2->adc.voices << H2I_C1_DATAT_SHIFT));}static dma_addr_t hal2_desc_addr(struct hal2_codec *codec, int i){	if (--i < 0)		i = codec->desc_count - 1;	return codec->desc[i].desc.pnext;}static void hal2_start_dac(struct hal2_card *hal2){	struct hal2_codec *dac = &hal2->dac;	struct hal2_pbus *pbus = &dac->pbus;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -