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

📄 vx_pcm.c

📁 鼎力推荐!本程序是基于嵌入式LUNUX系统开发的源程序代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Driver for Digigram VX soundcards * * PCM part * * Copyright (c) 2002,2003 by Takashi Iwai <tiwai@suse.de> * *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA * * * STRATEGY *  for playback, we send series of "chunks", which size is equal with the *  IBL size, typically 126 samples.  at each end of chunk, the end-of-buffer *  interrupt is notified, and the interrupt handler will feed the next chunk. * *  the current position is calculated from the sample count RMH. *  pipe->transferred is the counter of data which has been already transferred. *  if this counter reaches to the period size, snd_pcm_period_elapsed() will *  be issued. * *  for capture, the situation is much easier. *  to get a low latency response, we'll check the capture streams at each *  interrupt (capture stream has no EOB notification).  if the pending *  data is accumulated to the period size, snd_pcm_period_elapsed() is *  called and the pointer is updated. * *  the current point of read buffer is kept in pipe->hw_ptr.  note that *  this is in bytes. * * * TODO *  - linked trigger for full-duplex mode. *  - scheduled action on the stream. */#include <sound/driver.h>#include <linux/slab.h>#include <linux/vmalloc.h>#include <linux/delay.h>#include <sound/core.h>#include <sound/asoundef.h>#include <sound/pcm.h>#include <sound/vx_core.h>#include "vx_cmd.h"/* * we use a vmalloc'ed (sg-)buffer *//* get the physical page pointer on the given offset */static struct page *snd_pcm_get_vmalloc_page(snd_pcm_substream_t *subs, unsigned long offset){	void *pageptr = subs->runtime->dma_area + offset;	return vmalloc_to_page(pageptr);}/* * allocate a buffer via vmalloc_32(). * called from hw_params * NOTE: this may be called not only once per pcm open! */static int snd_pcm_alloc_vmalloc_buffer(snd_pcm_substream_t *subs, size_t size){	snd_pcm_runtime_t *runtime = subs->runtime;	if (runtime->dma_area) {		/* already allocated */		if (runtime->dma_bytes >= size)			return 0; /* already enough large */		vfree_nocheck(runtime->dma_area); /* bypass the memory wrapper */	}	runtime->dma_area = vmalloc_32(size);	if (! runtime->dma_area)		return -ENOMEM;	memset(runtime->dma_area, 0, size);	runtime->dma_bytes = size;	return 1; /* changed */}/* * free the buffer. * called from hw_free callback * NOTE: this may be called not only once per pcm open! */static int snd_pcm_free_vmalloc_buffer(snd_pcm_substream_t *subs){	snd_pcm_runtime_t *runtime = subs->runtime;	if (runtime->dma_area) {		vfree_nocheck(runtime->dma_area); /* bypass the memory wrapper */		runtime->dma_area = NULL;	}	return 0;}/* * read three pending pcm bytes via inb() */static void vx_pcm_read_per_bytes(vx_core_t *chip, snd_pcm_runtime_t *runtime, vx_pipe_t *pipe){	int offset = pipe->hw_ptr;	unsigned char *buf = (unsigned char *)(runtime->dma_area + offset);	*buf++ = vx_inb(chip, RXH);	if (++offset >= pipe->buffer_bytes) {		offset = 0;		buf = (unsigned char *)runtime->dma_area;	}	*buf++ = vx_inb(chip, RXM);	if (++offset >= pipe->buffer_bytes) {		offset = 0;		buf = (unsigned char *)runtime->dma_area;	}	*buf++ = vx_inb(chip, RXL);	if (++offset >= pipe->buffer_bytes) {		offset = 0;		buf = (unsigned char *)runtime->dma_area;	}	pipe->hw_ptr = offset;}/* * vx_set_pcx_time - convert from the PC time to the RMH status time. * @pc_time: the pointer for the PC-time to set * @dsp_time: the pointer for RMH status time array */static void vx_set_pcx_time(vx_core_t *chip, pcx_time_t *pc_time, unsigned int *dsp_time){	dsp_time[0] = (unsigned int)((*pc_time) >> 24) & PCX_TIME_HI_MASK;	dsp_time[1] = (unsigned int)(*pc_time) &  MASK_DSP_WORD;}/* * vx_set_differed_time - set the differed time if specified * @rmh: the rmh record to modify * @pipe: the pipe to be checked * * if the pipe is programmed with the differed time, set the DSP time * on the rmh and changes its command length. * * returns the increase of the command length. */static int vx_set_differed_time(vx_core_t *chip, struct vx_rmh *rmh, vx_pipe_t *pipe){	/* Update The length added to the RMH command by the timestamp */	if (! (pipe->differed_type & DC_DIFFERED_DELAY))		return 0;			/* Set the T bit */	rmh->Cmd[0] |= DSP_DIFFERED_COMMAND_MASK;	/* Time stamp is the 1st following parameter */	vx_set_pcx_time(chip, &pipe->pcx_time, &rmh->Cmd[1]);	/* Add the flags to a notified differed command */	if (pipe->differed_type & DC_NOTIFY_DELAY)		rmh->Cmd[1] |= NOTIFY_MASK_TIME_HIGH ;	/* Add the flags to a multiple differed command */	if (pipe->differed_type & DC_MULTIPLE_DELAY)		rmh->Cmd[1] |= MULTIPLE_MASK_TIME_HIGH;	/* Add the flags to a stream-time differed command */	if (pipe->differed_type & DC_STREAM_TIME_DELAY)		rmh->Cmd[1] |= STREAM_MASK_TIME_HIGH;			rmh->LgCmd += 2;	return 2;}/* * vx_set_stream_format - send the stream format command * @pipe: the affected pipe * @data: format bitmask */static int vx_set_stream_format(vx_core_t *chip, vx_pipe_t *pipe, unsigned int data){	struct vx_rmh rmh;	vx_init_rmh(&rmh, pipe->is_capture ?		    CMD_FORMAT_STREAM_IN : CMD_FORMAT_STREAM_OUT);	rmh.Cmd[0] |= pipe->number << FIELD_SIZE;        /* Command might be longer since we may have to add a timestamp */	vx_set_differed_time(chip, &rmh, pipe);	rmh.Cmd[rmh.LgCmd] = (data & 0xFFFFFF00) >> 8;	rmh.Cmd[rmh.LgCmd + 1] = (data & 0xFF) << 16 /*| (datal & 0xFFFF00) >> 8*/;	rmh.LgCmd += 2;    	return vx_send_msg(chip, &rmh);}/* * vx_set_format - set the format of a pipe * @pipe: the affected pipe * @runtime: pcm runtime instance to be referred * * returns 0 if successful, or a negative error code. */static int vx_set_format(vx_core_t *chip, vx_pipe_t *pipe,			 snd_pcm_runtime_t *runtime){	unsigned int header = HEADER_FMT_BASE;	if (runtime->channels == 1)		header |= HEADER_FMT_MONO;	if (snd_pcm_format_little_endian(runtime->format))		header |= HEADER_FMT_INTEL;	if (runtime->rate < 32000 && runtime->rate > 11025)		header |= HEADER_FMT_UPTO32;	else if (runtime->rate <= 11025)		header |= HEADER_FMT_UPTO11;	switch (snd_pcm_format_physical_width(runtime->format)) {	// case 8: break;	case 16: header |= HEADER_FMT_16BITS; break;	case 24: header |= HEADER_FMT_24BITS; break;	default : 		snd_BUG();		return -EINVAL;        };	return vx_set_stream_format(chip, pipe, header);}/* * set / query the IBL size */static int vx_set_ibl(vx_core_t *chip, struct vx_ibl_info *info){	int err;	struct vx_rmh rmh;	vx_init_rmh(&rmh, CMD_IBL);	rmh.Cmd[0] |= info->size & 0x03ffff;	err = vx_send_msg(chip, &rmh);	if (err < 0)		return err;	info->size = rmh.Stat[0];	info->max_size = rmh.Stat[1];	info->min_size = rmh.Stat[2];	info->granularity = rmh.Stat[3];	snd_printdd(KERN_DEBUG "vx_set_ibl: size = %d, max = %d, min = %d, gran = %d\n",		   info->size, info->max_size, info->min_size, info->granularity);	return 0;}/* * vx_get_pipe_state - get the state of a pipe * @pipe: the pipe to be checked * @state: the pointer for the returned state * * checks the state of a given pipe, and stores the state (1 = running, * 0 = paused) on the given pointer. * * called from trigger callback only */static int vx_get_pipe_state(vx_core_t *chip, vx_pipe_t *pipe, int *state){	int err;	struct vx_rmh rmh;	vx_init_rmh(&rmh, CMD_PIPE_STATE);	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);	err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 	if (! err)		*state = (rmh.Stat[0] & (1 << pipe->number)) ? 1 : 0;	return err;}/* * vx_query_hbuffer_size - query available h-buffer size in bytes * @pipe: the pipe to be checked * * return the available size on h-buffer in bytes, * or a negative error code. * * NOTE: calling this function always switches to the stream mode. *       you'll need to disconnect the host to get back to the *       normal mode. */static int vx_query_hbuffer_size(vx_core_t *chip, vx_pipe_t *pipe){	int result;	struct vx_rmh rmh;	vx_init_rmh(&rmh, CMD_SIZE_HBUFFER);	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);	if (pipe->is_capture)		rmh.Cmd[0] |= 0x00000001;	result = vx_send_msg(chip, &rmh);	if (! result)		result = rmh.Stat[0] & 0xffff;	return result;}/* * vx_pipe_can_start - query whether a pipe is ready for start * @pipe: the pipe to be checked * * return 1 if ready, 0 if not ready, and negative value on error. * * called from trigger callback only */static int vx_pipe_can_start(vx_core_t *chip, vx_pipe_t *pipe){	int err;	struct vx_rmh rmh;        	vx_init_rmh(&rmh, CMD_CAN_START_PIPE);	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);	rmh.Cmd[0] |= 1;	err = vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ 	if (! err) {		if (rmh.Stat[0])			err = 1;	}	return err;}/* * vx_conf_pipe - tell the pipe to stand by and wait for IRQA. * @pipe: the pipe to be configured */static int vx_conf_pipe(vx_core_t *chip, vx_pipe_t *pipe){	struct vx_rmh rmh;	vx_init_rmh(&rmh, CMD_CONF_PIPE);	if (pipe->is_capture)		rmh.Cmd[0] |= COMMAND_RECORD_MASK;	rmh.Cmd[1] = 1 << pipe->number;	return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */}/* * vx_send_irqa - trigger IRQA */static int vx_send_irqa(vx_core_t *chip){	struct vx_rmh rmh;	vx_init_rmh(&rmh, CMD_SEND_IRQA);	return vx_send_msg_nolock(chip, &rmh); /* no lock needed for trigger */ }#define MAX_WAIT_FOR_DSP        250/* * vx boards do not support inter-card sync, besides * only 126 samples require to be prepared before a pipe can start */#define CAN_START_DELAY         2	/* wait 2ms only before asking if the pipe is ready*/#define WAIT_STATE_DELAY        2	/* wait 2ms after irqA was requested and check if the pipe state toggled*//* * vx_toggle_pipe - start / pause a pipe * @pipe: the pipe to be triggered * @state: start = 1, pause = 0 * * called from trigger callback only * */static int vx_toggle_pipe(vx_core_t *chip, vx_pipe_t *pipe, int state){	int err, i, cur_state;	/* Check the pipe is not already in the requested state */	if (vx_get_pipe_state(chip, pipe, &cur_state) < 0)		return -EBADFD;	if (state == cur_state)		return 0;	/* If a start is requested, ask the DSP to get prepared	 * and wait for a positive acknowledge (when there are	 * enough sound buffer for this pipe)	 */	if (state) {		for (i = 0 ; i < MAX_WAIT_FOR_DSP; i++) {			err = vx_pipe_can_start(chip, pipe);			if (err > 0)				break;			/* Wait for a few, before asking again			 * to avoid flooding the DSP with our requests			 */			mdelay(1);		}	}    	if ((err = vx_conf_pipe(chip, pipe)) < 0)		return err;	if ((err = vx_send_irqa(chip)) < 0)		return err;    	/* If it completes successfully, wait for the pipes	 * reaching the expected state before returning	 * Check one pipe only (since they are synchronous)	 */	for (i = 0; i < MAX_WAIT_FOR_DSP; i++) {		err = vx_get_pipe_state(chip, pipe, &cur_state);		if (err < 0 || cur_state == state)			break;		err = -EIO;		mdelay(1);	}	return err < 0 ? -EIO : 0;}    /* * vx_stop_pipe - stop a pipe * @pipe: the pipe to be stopped * * called from trigger callback only */static int vx_stop_pipe(vx_core_t *chip, vx_pipe_t *pipe){	struct vx_rmh rmh;	vx_init_rmh(&rmh, CMD_STOP_PIPE);	vx_set_pipe_cmd_params(&rmh, pipe->is_capture, pipe->number, 0);

⌨️ 快捷键说明

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