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

📄 mixart.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * Driver for Digigram miXart soundcards * * main file with alsa callbacks * * Copyright (c) 2003 by Digigram <alsa@digigram.com> * *   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 */#include <sound/driver.h>#include <linux/init.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/moduleparam.h>#include <sound/core.h>#include <sound/initval.h>#include <sound/info.h>#include <sound/control.h>#include <sound/pcm.h>#include <sound/pcm_params.h>#include "mixart.h"#include "mixart_hwdep.h"#include "mixart_core.h"#include "mixart_mixer.h"#define CARD_NAME "miXart"MODULE_AUTHOR("Digigram <alsa@digigram.com>");MODULE_DESCRIPTION("Digigram " CARD_NAME);MODULE_LICENSE("GPL");MODULE_SUPPORTED_DEVICE("{{Digigram," CARD_NAME "}}");static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;             /* Index 0-MAX */static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;              /* ID for this card */static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;     /* Enable this card */module_param_array(index, int, NULL, 0444);MODULE_PARM_DESC(index, "Index value for Digigram " CARD_NAME " soundcard.");module_param_array(id, charp, NULL, 0444);MODULE_PARM_DESC(id, "ID string for Digigram " CARD_NAME " soundcard.");module_param_array(enable, bool, NULL, 0444);MODULE_PARM_DESC(enable, "Enable Digigram " CARD_NAME " soundcard.");/* */static struct pci_device_id snd_mixart_ids[] = {	{ 0x1057, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* MC8240 */	{ 0, }};MODULE_DEVICE_TABLE(pci, snd_mixart_ids);static int mixart_set_pipe_state(mixart_mgr_t *mgr, mixart_pipe_t* pipe, int start){	mixart_group_state_req_t group_state;	mixart_group_state_resp_t group_state_resp;	mixart_msg_t request;	int err;	u32 system_msg_uid;	switch(pipe->status) {	case PIPE_RUNNING:	case PIPE_CLOCK_SET:		if(start) return 0; /* already started */		break;	case PIPE_STOPPED:		if(!start) return 0; /* already stopped */		break;	default:		snd_printk(KERN_ERR "error mixart_set_pipe_state called with wrong pipe->status!\n");		return -EINVAL;      /* function called with wrong pipe status */	}	system_msg_uid = 0x12345678; /* the event ! (take care: the MSB and two LSB's have to be 0) */	/* wait on the last MSG_SYSTEM_SEND_SYNCHRO_CMD command to be really finished */	request.message_id = MSG_SYSTEM_WAIT_SYNCHRO_CMD;	request.uid = (mixart_uid_t){0,0};	request.data = &system_msg_uid;	request.size = sizeof(system_msg_uid);	err = snd_mixart_send_msg_wait_notif(mgr, &request, system_msg_uid);	if(err) {		snd_printk(KERN_ERR "error : MSG_SYSTEM_WAIT_SYNCHRO_CMD was not notified !\n");		return err;	}	/* start or stop the pipe (1 pipe) */	memset(&group_state, 0, sizeof(group_state));	group_state.pipe_count = 1;	group_state.pipe_uid[0] = pipe->group_uid;	if(start)		request.message_id = MSG_STREAM_START_STREAM_GRP_PACKET;	else		request.message_id = MSG_STREAM_STOP_STREAM_GRP_PACKET;	request.uid = pipe->group_uid; /*(mixart_uid_t){0,0};*/	request.data = &group_state;	request.size = sizeof(group_state);	err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);	if (err < 0 || group_state_resp.txx_status != 0) {		snd_printk(KERN_ERR "error MSG_STREAM_ST***_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status);		return -EINVAL;	}	if(start) {		u32 stat;		group_state.pipe_count = 0; /* in case of start same command once again with pipe_count=0 */		err = snd_mixart_send_msg(mgr, &request, sizeof(group_state_resp), &group_state_resp);		if (err < 0 || group_state_resp.txx_status != 0) {			snd_printk(KERN_ERR "error MSG_STREAM_START_STREAM_GRP_PACKET err=%x stat=%x !\n", err, group_state_resp.txx_status); 			return -EINVAL;		}		/* in case of start send a synchro top */		request.message_id = MSG_SYSTEM_SEND_SYNCHRO_CMD;		request.uid = (mixart_uid_t){0,0};		request.data = NULL;		request.size = 0;		err = snd_mixart_send_msg(mgr, &request, sizeof(stat), &stat);		if (err < 0 || stat != 0) {			snd_printk(KERN_ERR "error MSG_SYSTEM_SEND_SYNCHRO_CMD err=%x stat=%x !\n", err, stat);			return -EINVAL;		}		pipe->status = PIPE_RUNNING;	}	else /* !start */		pipe->status = PIPE_STOPPED;	return 0;}static int mixart_set_clock(mixart_mgr_t *mgr, mixart_pipe_t *pipe, unsigned int rate){	mixart_msg_t request;	mixart_clock_properties_t clock_properties;	mixart_clock_properties_resp_t clock_prop_resp;	int err;	switch(pipe->status) {	case PIPE_CLOCK_SET:		break;	case PIPE_RUNNING:		if(rate != 0)			break;	default:		if(rate == 0)			return 0; /* nothing to do */		else {			snd_printk(KERN_ERR "error mixart_set_clock(%d) called with wrong pipe->status !\n", rate);			return -EINVAL;		}	}	memset(&clock_properties, 0, sizeof(clock_properties));	clock_properties.clock_generic_type = (rate != 0) ? CGT_INTERNAL_CLOCK : CGT_NO_CLOCK;	clock_properties.clock_mode = CM_STANDALONE;	clock_properties.frequency = rate;	clock_properties.nb_callers = 1; /* only one entry in uid_caller ! */	clock_properties.uid_caller[0] = pipe->group_uid;	snd_printdd("mixart_set_clock to %d kHz\n", rate);	request.message_id = MSG_CLOCK_SET_PROPERTIES;	request.uid = mgr->uid_console_manager;	request.data = &clock_properties;	request.size = sizeof(clock_properties);	err = snd_mixart_send_msg(mgr, &request, sizeof(clock_prop_resp), &clock_prop_resp);	if (err < 0 || clock_prop_resp.status != 0 || clock_prop_resp.clock_mode != CM_STANDALONE) {		snd_printk(KERN_ERR "error MSG_CLOCK_SET_PROPERTIES err=%x stat=%x mod=%x !\n", err, clock_prop_resp.status, clock_prop_resp.clock_mode);		return -EINVAL;	}	if(rate)  pipe->status = PIPE_CLOCK_SET;	else      pipe->status = PIPE_RUNNING;	return 0;}/* *  Allocate or reference output pipe for analog IOs (pcmp0/1) */mixart_pipe_t* snd_mixart_add_ref_pipe( mixart_t *chip, int pcm_number, int capture, int monitoring){	int stream_count;	mixart_pipe_t *pipe;	mixart_msg_t request;	if(capture) {		if (pcm_number == MIXART_PCM_ANALOG) {			pipe = &(chip->pipe_in_ana);  /* analog inputs */		} else {			pipe = &(chip->pipe_in_dig); /* digital inputs */		}		request.message_id = MSG_STREAM_ADD_OUTPUT_GROUP;		stream_count = MIXART_CAPTURE_STREAMS;	} else {		if (pcm_number == MIXART_PCM_ANALOG) {			pipe = &(chip->pipe_out_ana);  /* analog outputs */		} else {			pipe = &(chip->pipe_out_dig);  /* digital outputs */		}		request.message_id = MSG_STREAM_ADD_INPUT_GROUP;		stream_count = MIXART_PLAYBACK_STREAMS;	}	/* a new stream is opened and there are already all streams in use */	if( (monitoring == 0) && (pipe->references >= stream_count) ) {		return NULL;	}	/* pipe is not yet defined */	if( pipe->status == PIPE_UNDEFINED ) {		int err, i;		struct {			mixart_streaming_group_req_t sgroup_req;			mixart_streaming_group_t sgroup_resp;		} *buf;		snd_printdd("add_ref_pipe audio chip(%d) pcm(%d)\n", chip->chip_idx, pcm_number);		buf = kmalloc(sizeof(*buf), GFP_KERNEL);		if (!buf)			return NULL;		request.uid = (mixart_uid_t){0,0};      /* should be StreamManagerUID, but zero is OK if there is only one ! */		request.data = &buf->sgroup_req;		request.size = sizeof(buf->sgroup_req);		memset(&buf->sgroup_req, 0, sizeof(buf->sgroup_req));		buf->sgroup_req.stream_count = stream_count;		buf->sgroup_req.channel_count = 2;		buf->sgroup_req.latency = 256;		buf->sgroup_req.connector = pipe->uid_left_connector;  /* the left connector */		for (i=0; i<stream_count; i++) {			int j;			struct mixart_flowinfo *flowinfo;			struct mixart_bufferinfo *bufferinfo;						/* we don't yet know the format, so config 16 bit pcm audio for instance */			buf->sgroup_req.stream_info[i].size_max_byte_frame = 1024;			buf->sgroup_req.stream_info[i].size_max_sample_frame = 256;			buf->sgroup_req.stream_info[i].nb_bytes_max_per_sample = MIXART_FLOAT_P__4_0_TO_HEX; /* is 4.0f */			/* find the right bufferinfo_array */			j = (chip->chip_idx * MIXART_MAX_STREAM_PER_CARD) + (pcm_number * (MIXART_PLAYBACK_STREAMS + MIXART_CAPTURE_STREAMS)) + i;			if(capture) j += MIXART_PLAYBACK_STREAMS; /* in the array capture is behind playback */			buf->sgroup_req.flow_entry[i] = j;			flowinfo = (struct mixart_flowinfo *)chip->mgr->flowinfo.area;			flowinfo[j].bufferinfo_array_phy_address = (u32)chip->mgr->bufferinfo.addr + (j * sizeof(mixart_bufferinfo_t));			flowinfo[j].bufferinfo_count = 1;               /* 1 will set the miXart to ring-buffer mode ! */			bufferinfo = (struct mixart_bufferinfo *)chip->mgr->bufferinfo.area;			bufferinfo[j].buffer_address = 0;               /* buffer is not yet allocated */			bufferinfo[j].available_length = 0;             /* buffer is not yet allocated */			/* construct the identifier of the stream buffer received in the interrupts ! */			bufferinfo[j].buffer_id = (chip->chip_idx << MIXART_NOTIFY_CARD_OFFSET) + (pcm_number << MIXART_NOTIFY_PCM_OFFSET ) + i;			if(capture) {				bufferinfo[j].buffer_id |= MIXART_NOTIFY_CAPT_MASK;			}		}		err = snd_mixart_send_msg(chip->mgr, &request, sizeof(buf->sgroup_resp), &buf->sgroup_resp);		if((err < 0) || (buf->sgroup_resp.status != 0)) {			snd_printk(KERN_ERR "error MSG_STREAM_ADD_**PUT_GROUP err=%x stat=%x !\n", err, buf->sgroup_resp.status);			kfree(buf);			return NULL;		}		pipe->group_uid = buf->sgroup_resp.group;     /* id of the pipe, as returned by embedded */		pipe->stream_count = buf->sgroup_resp.stream_count;		/* pipe->stream_uid[i] = buf->sgroup_resp.stream[i].stream_uid; */		pipe->status = PIPE_STOPPED;		kfree(buf);	}	if(monitoring)	pipe->monitoring = 1;	else		pipe->references++;	return pipe;}int snd_mixart_kill_ref_pipe( mixart_mgr_t *mgr, mixart_pipe_t *pipe, int monitoring){	int err = 0;	if(pipe->status == PIPE_UNDEFINED)		return 0;	if(monitoring)		pipe->monitoring = 0;	else		pipe->references--;	if((pipe->references <= 0) && (pipe->monitoring == 0)) {		mixart_msg_t request;		mixart_delete_group_resp_t delete_resp;		/* release the clock */		err = mixart_set_clock( mgr, pipe, 0);		if( err < 0 ) {			snd_printk(KERN_ERR "mixart_set_clock(0) return error!\n");		}		/* stop the pipe */		err = mixart_set_pipe_state(mgr, pipe, 0);		if( err < 0 ) {			snd_printk(KERN_ERR "error stopping pipe!\n");		}		request.message_id = MSG_STREAM_DELETE_GROUP;		request.uid = (mixart_uid_t){0,0};		request.data = &pipe->group_uid;            /* the streaming group ! */		request.size = sizeof(pipe->group_uid);		/* delete the pipe */		err = snd_mixart_send_msg(mgr, &request, sizeof(delete_resp), &delete_resp);		if ((err < 0) || (delete_resp.status != 0)) {			snd_printk(KERN_ERR "error MSG_STREAM_DELETE_GROUP err(%x), status(%x)\n", err, delete_resp.status);		}		pipe->group_uid = (mixart_uid_t){0,0};		pipe->stream_count = 0;		pipe->status = PIPE_UNDEFINED;	}	return err;}static int mixart_set_stream_state(mixart_stream_t *stream, int start){	mixart_t *chip;	mixart_stream_state_req_t stream_state_req;	mixart_msg_t request;	if(!stream->substream)		return -EINVAL;	memset(&stream_state_req, 0, sizeof(stream_state_req));	stream_state_req.stream_count = 1;	stream_state_req.stream_info.stream_desc.uid_pipe = stream->pipe->group_uid;	stream_state_req.stream_info.stream_desc.stream_idx = stream->substream->number;	if (stream->substream->stream == SNDRV_PCM_STREAM_PLAYBACK)		request.message_id = start ? MSG_STREAM_START_INPUT_STAGE_PACKET : MSG_STREAM_STOP_INPUT_STAGE_PACKET;	else		request.message_id = start ? MSG_STREAM_START_OUTPUT_STAGE_PACKET : MSG_STREAM_STOP_OUTPUT_STAGE_PACKET;	request.uid = (mixart_uid_t){0,0};	request.data = &stream_state_req;	request.size = sizeof(stream_state_req);	stream->abs_period_elapsed = 0;            /* reset stream pos      */	stream->buf_periods = 0;	stream->buf_period_frag = 0;	chip = snd_pcm_substream_chip(stream->substream);	return snd_mixart_send_msg_nonblock(chip->mgr, &request);}/* *  Trigger callback */static int snd_mixart_trigger(snd_pcm_substream_t *subs, int cmd){	mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;	switch (cmd) {	case SNDRV_PCM_TRIGGER_START:		snd_printdd("SNDRV_PCM_TRIGGER_START\n");		/* START_STREAM */		if( mixart_set_stream_state(stream, 1) )			return -EINVAL;		stream->status = MIXART_STREAM_STATUS_RUNNING;		break;	case SNDRV_PCM_TRIGGER_STOP:		/* STOP_STREAM */		if( mixart_set_stream_state(stream, 0) )			return -EINVAL;		stream->status = MIXART_STREAM_STATUS_OPEN;		snd_printdd("SNDRV_PCM_TRIGGER_STOP\n");		break;	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:		/* TODO */		stream->status = MIXART_STREAM_STATUS_PAUSE;		snd_printdd("SNDRV_PCM_PAUSE_PUSH\n");		break;	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:		/* TODO */		stream->status = MIXART_STREAM_STATUS_RUNNING;		snd_printdd("SNDRV_PCM_PAUSE_RELEASE\n");		break;	default:		return -EINVAL;	}	return 0;}static int mixart_sync_nonblock_events(mixart_mgr_t *mgr){	unsigned long timeout = jiffies + HZ;	while (atomic_read(&mgr->msg_processed) > 0) {		if (time_after(jiffies, timeout)) {			snd_printk(KERN_ERR "mixart: cannot process nonblock events!\n");			return -EBUSY;		}		schedule_timeout_uninterruptible(1);	}	return 0;}/* *  prepare callback for all pcms */static int snd_mixart_prepare(snd_pcm_substream_t *subs){	mixart_t *chip = snd_pcm_substream_chip(subs);	mixart_stream_t *stream = (mixart_stream_t*)subs->runtime->private_data;	/* TODO de fa鏾n non bloquante, r閍ppliquer les hw_params (rate, bits, codec) */	snd_printdd("snd_mixart_prepare\n");	mixart_sync_nonblock_events(chip->mgr);	/* only the first stream can choose the sample rate */	/* the further opened streams will be limited to its frequency (see open) */	if(chip->mgr->ref_count_rate == 1)		chip->mgr->sample_rate = subs->runtime->rate;	/* set the clock only once (first stream) on the same pipe */	if(stream->pipe->references == 1) {		if( mixart_set_clock(chip->mgr, stream->pipe, subs->runtime->rate) )			return -EINVAL;

⌨️ 快捷键说明

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