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

📄 pcm_oss.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 5 页
字号:
/* *  Digital Audio (PCM) abstract layer / OSS compatible *  Copyright (c) by Jaroslav Kysela <perex@suse.cz> * * *   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 * */#if 0#define PLUGIN_DEBUG#endif#if 0#define OSS_DEBUG#endif#include <sound/driver.h>#include <linux/init.h>#include <linux/smp_lock.h>#include <linux/slab.h>#include <linux/time.h>#include <linux/vmalloc.h>#include <linux/moduleparam.h>#include <linux/string.h>#include <sound/core.h>#include <sound/minors.h>#include <sound/pcm.h>#include <sound/pcm_params.h>#include "pcm_plugin.h"#include <sound/info.h>#include <linux/soundcard.h>#include <sound/initval.h>#define OSS_ALSAEMULVER		_SIOR ('M', 249, int)static int dsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};static int adsp_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};static int nonblock_open = 1;MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Abramo Bagnara <abramo@alsa-project.org>");MODULE_DESCRIPTION("PCM OSS emulation for ALSA.");MODULE_LICENSE("GPL");module_param_array(dsp_map, int, NULL, 0444);MODULE_PARM_DESC(dsp_map, "PCM device number assigned to 1st OSS device.");module_param_array(adsp_map, int, NULL, 0444);MODULE_PARM_DESC(adsp_map, "PCM device number assigned to 2nd OSS device.");module_param(nonblock_open, bool, 0644);MODULE_PARM_DESC(nonblock_open, "Don't block opening busy PCM devices.");MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM);MODULE_ALIAS_SNDRV_MINOR(SNDRV_MINOR_OSS_PCM1);extern int snd_mixer_oss_ioctl_card(snd_card_t *card, unsigned int cmd, unsigned long arg);static int snd_pcm_oss_get_rate(snd_pcm_oss_file_t *pcm_oss_file);static int snd_pcm_oss_get_channels(snd_pcm_oss_file_t *pcm_oss_file);static int snd_pcm_oss_get_format(snd_pcm_oss_file_t *pcm_oss_file);static inline mm_segment_t snd_enter_user(void){	mm_segment_t fs = get_fs();	set_fs(get_ds());	return fs;}static inline void snd_leave_user(mm_segment_t fs){	set_fs(fs);}static int snd_pcm_oss_plugin_clear(snd_pcm_substream_t *substream){	snd_pcm_runtime_t *runtime = substream->runtime;	snd_pcm_plugin_t *plugin, *next;		plugin = runtime->oss.plugin_first;	while (plugin) {		next = plugin->next;		snd_pcm_plugin_free(plugin);		plugin = next;	}	runtime->oss.plugin_first = runtime->oss.plugin_last = NULL;	return 0;}static int snd_pcm_plugin_insert(snd_pcm_plugin_t *plugin){	snd_pcm_runtime_t *runtime = plugin->plug->runtime;	plugin->next = runtime->oss.plugin_first;	plugin->prev = NULL;	if (runtime->oss.plugin_first) {		runtime->oss.plugin_first->prev = plugin;		runtime->oss.plugin_first = plugin;	} else {		runtime->oss.plugin_last =		runtime->oss.plugin_first = plugin;	}	return 0;}int snd_pcm_plugin_append(snd_pcm_plugin_t *plugin){	snd_pcm_runtime_t *runtime = plugin->plug->runtime;	plugin->next = NULL;	plugin->prev = runtime->oss.plugin_last;	if (runtime->oss.plugin_last) {		runtime->oss.plugin_last->next = plugin;		runtime->oss.plugin_last = plugin;	} else {		runtime->oss.plugin_last =		runtime->oss.plugin_first = plugin;	}	return 0;}static long snd_pcm_oss_bytes(snd_pcm_substream_t *substream, long frames){	snd_pcm_runtime_t *runtime = substream->runtime;	long buffer_size = snd_pcm_lib_buffer_bytes(substream);	long bytes = frames_to_bytes(runtime, frames);	if (buffer_size == runtime->oss.buffer_bytes)		return bytes;#if BITS_PER_LONG >= 64	return runtime->oss.buffer_bytes * bytes / buffer_size;#else	{		u64 bsize = (u64)runtime->oss.buffer_bytes * (u64)bytes;		u32 rem;		div64_32(&bsize, buffer_size, &rem);		return (long)bsize;	}#endif}static long snd_pcm_alsa_frames(snd_pcm_substream_t *substream, long bytes){	snd_pcm_runtime_t *runtime = substream->runtime;	long buffer_size = snd_pcm_lib_buffer_bytes(substream);	if (buffer_size == runtime->oss.buffer_bytes)		return bytes_to_frames(runtime, bytes);	return bytes_to_frames(runtime, (buffer_size * bytes) / runtime->oss.buffer_bytes);}static int snd_pcm_oss_format_from(int format){	switch (format) {	case AFMT_MU_LAW:	return SNDRV_PCM_FORMAT_MU_LAW;	case AFMT_A_LAW:	return SNDRV_PCM_FORMAT_A_LAW;	case AFMT_IMA_ADPCM:	return SNDRV_PCM_FORMAT_IMA_ADPCM;	case AFMT_U8:		return SNDRV_PCM_FORMAT_U8;	case AFMT_S16_LE:	return SNDRV_PCM_FORMAT_S16_LE;	case AFMT_S16_BE:	return SNDRV_PCM_FORMAT_S16_BE;	case AFMT_S8:		return SNDRV_PCM_FORMAT_S8;	case AFMT_U16_LE:	return SNDRV_PCM_FORMAT_U16_LE;	case AFMT_U16_BE:	return SNDRV_PCM_FORMAT_U16_BE;	case AFMT_MPEG:		return SNDRV_PCM_FORMAT_MPEG;	default:		return SNDRV_PCM_FORMAT_U8;	}}static int snd_pcm_oss_format_to(int format){	switch (format) {	case SNDRV_PCM_FORMAT_MU_LAW:	return AFMT_MU_LAW;	case SNDRV_PCM_FORMAT_A_LAW:	return AFMT_A_LAW;	case SNDRV_PCM_FORMAT_IMA_ADPCM:	return AFMT_IMA_ADPCM;	case SNDRV_PCM_FORMAT_U8:		return AFMT_U8;	case SNDRV_PCM_FORMAT_S16_LE:	return AFMT_S16_LE;	case SNDRV_PCM_FORMAT_S16_BE:	return AFMT_S16_BE;	case SNDRV_PCM_FORMAT_S8:		return AFMT_S8;	case SNDRV_PCM_FORMAT_U16_LE:	return AFMT_U16_LE;	case SNDRV_PCM_FORMAT_U16_BE:	return AFMT_U16_BE;	case SNDRV_PCM_FORMAT_MPEG:		return AFMT_MPEG;	default:			return -EINVAL;	}}static int snd_pcm_oss_period_size(snd_pcm_substream_t *substream, 				   snd_pcm_hw_params_t *oss_params,				   snd_pcm_hw_params_t *slave_params){	size_t s;	size_t oss_buffer_size, oss_period_size, oss_periods;	size_t min_period_size, max_period_size;	snd_pcm_runtime_t *runtime = substream->runtime;	size_t oss_frame_size;	oss_frame_size = snd_pcm_format_physical_width(params_format(oss_params)) *			 params_channels(oss_params) / 8;	oss_buffer_size = snd_pcm_plug_client_size(substream,						   snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, NULL)) * oss_frame_size;	oss_buffer_size = 1 << ld2(oss_buffer_size);	if (atomic_read(&runtime->mmap_count)) {		if (oss_buffer_size > runtime->oss.mmap_bytes)			oss_buffer_size = runtime->oss.mmap_bytes;	}	if (substream->oss.setup &&	    substream->oss.setup->period_size > 16)		oss_period_size = substream->oss.setup->period_size;	else if (runtime->oss.fragshift) {		oss_period_size = 1 << runtime->oss.fragshift;		if (oss_period_size > oss_buffer_size / 2)			oss_period_size = oss_buffer_size / 2;	} else {		int sd;		size_t bytes_per_sec = params_rate(oss_params) * snd_pcm_format_physical_width(params_format(oss_params)) * params_channels(oss_params) / 8;		oss_period_size = oss_buffer_size;		do {			oss_period_size /= 2;		} while (oss_period_size > bytes_per_sec);		if (runtime->oss.subdivision == 0) {			sd = 4;			if (oss_period_size / sd > 4096)				sd *= 2;			if (oss_period_size / sd < 4096)				sd = 1;		} else			sd = runtime->oss.subdivision;		oss_period_size /= sd;		if (oss_period_size < 16)			oss_period_size = 16;	}	min_period_size = snd_pcm_plug_client_size(substream,						   snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));	min_period_size *= oss_frame_size;	min_period_size = 1 << (ld2(min_period_size - 1) + 1);	if (oss_period_size < min_period_size)		oss_period_size = min_period_size;	max_period_size = snd_pcm_plug_client_size(substream,						   snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIOD_SIZE, NULL));	max_period_size *= oss_frame_size;	max_period_size = 1 << ld2(max_period_size);	if (oss_period_size > max_period_size)		oss_period_size = max_period_size;	oss_periods = oss_buffer_size / oss_period_size;	if (substream->oss.setup) {		if (substream->oss.setup->periods > 1)			oss_periods = substream->oss.setup->periods;	}	s = snd_pcm_hw_param_value_max(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);	if (runtime->oss.maxfrags && s > runtime->oss.maxfrags)		s = runtime->oss.maxfrags;	if (oss_periods > s)		oss_periods = s;	s = snd_pcm_hw_param_value_min(slave_params, SNDRV_PCM_HW_PARAM_PERIODS, NULL);	if (s < 2)		s = 2;	if (oss_periods < s)		oss_periods = s;	while (oss_period_size * oss_periods > oss_buffer_size)		oss_period_size /= 2;	snd_assert(oss_period_size >= 16, return -EINVAL);	runtime->oss.period_bytes = oss_period_size;	runtime->oss.period_frames = 1;	runtime->oss.periods = oss_periods;	return 0;}static int choose_rate(snd_pcm_substream_t *substream,		       snd_pcm_hw_params_t *params, unsigned int best_rate){	snd_interval_t *it;	snd_pcm_hw_params_t *save;	unsigned int rate, prev;	save = kmalloc(sizeof(*save), GFP_KERNEL);	if (save == NULL)		return -ENOMEM;	*save = *params;	it = hw_param_interval(save, SNDRV_PCM_HW_PARAM_RATE);	/* try multiples of the best rate */	rate = best_rate;	for (;;) {		if (it->max < rate || (it->max == rate && it->openmax))			break;		if (it->min < rate || (it->min == rate && !it->openmin)) {			int ret;			ret = snd_pcm_hw_param_set(substream, params,						   SNDRV_PCM_HW_PARAM_RATE,						   rate, 0);			if (ret == (int)rate) {				kfree(save);				return rate;			}			*params = *save;		}		prev = rate;		rate += best_rate;		if (rate <= prev)			break;	}	/* not found, use the nearest rate */	kfree(save);	return snd_pcm_hw_param_near(substream, params, SNDRV_PCM_HW_PARAM_RATE, best_rate, NULL);}static int snd_pcm_oss_change_params(snd_pcm_substream_t *substream){	snd_pcm_runtime_t *runtime = substream->runtime;	snd_pcm_hw_params_t *params, *sparams;	snd_pcm_sw_params_t *sw_params;	ssize_t oss_buffer_size, oss_period_size;	size_t oss_frame_size;	int err;	int direct;	int format, sformat, n;	snd_mask_t sformat_mask;	snd_mask_t mask;	sw_params = kmalloc(sizeof(*sw_params), GFP_KERNEL);	params = kmalloc(sizeof(*params), GFP_KERNEL);	sparams = kmalloc(sizeof(*sparams), GFP_KERNEL);	if (!sw_params || !params || !sparams) {		snd_printd("No memory\n");		err = -ENOMEM;		goto failure;	}	if (atomic_read(&runtime->mmap_count)) {		direct = 1;	} else {		snd_pcm_oss_setup_t *setup = substream->oss.setup;		direct = (setup != NULL && setup->direct);	}	_snd_pcm_hw_params_any(sparams);	_snd_pcm_hw_param_setinteger(sparams, SNDRV_PCM_HW_PARAM_PERIODS);	_snd_pcm_hw_param_min(sparams, SNDRV_PCM_HW_PARAM_PERIODS, 2, 0);	snd_mask_none(&mask);	if (atomic_read(&runtime->mmap_count))		snd_mask_set(&mask, SNDRV_PCM_ACCESS_MMAP_INTERLEAVED);	else {		snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_INTERLEAVED);		if (!direct)			snd_mask_set(&mask, SNDRV_PCM_ACCESS_RW_NONINTERLEAVED);	}	err = snd_pcm_hw_param_mask(substream, sparams, SNDRV_PCM_HW_PARAM_ACCESS, &mask);	if (err < 0) {		snd_printd("No usable accesses\n");		err = -EINVAL;		goto failure;	}	choose_rate(substream, sparams, runtime->oss.rate);	snd_pcm_hw_param_near(substream, sparams, SNDRV_PCM_HW_PARAM_CHANNELS, runtime->oss.channels, NULL);	format = snd_pcm_oss_format_from(runtime->oss.format);	sformat_mask = *hw_param_mask(sparams, SNDRV_PCM_HW_PARAM_FORMAT);	if (direct)		sformat = format;	else		sformat = snd_pcm_plug_slave_format(format, &sformat_mask);	if (sformat < 0 || !snd_mask_test(&sformat_mask, sformat)) {		for (sformat = 0; sformat <= SNDRV_PCM_FORMAT_LAST; sformat++) {			if (snd_mask_test(&sformat_mask, sformat) &&			    snd_pcm_oss_format_to(sformat) >= 0)				break;		}		if (sformat > SNDRV_PCM_FORMAT_LAST) {			snd_printd("Cannot find a format!!!\n");			err = -EINVAL;			goto failure;		}	}	err = _snd_pcm_hw_param_set(sparams, SNDRV_PCM_HW_PARAM_FORMAT, sformat, 0);	snd_assert(err >= 0, goto failure);	if (direct) {		memcpy(params, sparams, sizeof(*params));	} else {		_snd_pcm_hw_params_any(params);		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_ACCESS,				      SNDRV_PCM_ACCESS_RW_INTERLEAVED, 0);		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_FORMAT,				      snd_pcm_oss_format_from(runtime->oss.format), 0);		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_CHANNELS,				      runtime->oss.channels, 0);		_snd_pcm_hw_param_set(params, SNDRV_PCM_HW_PARAM_RATE,				      runtime->oss.rate, 0);		pdprintf("client: access = %i, format = %i, channels = %i, rate = %i\n",			 params_access(params), params_format(params),			 params_channels(params), params_rate(params));	}	pdprintf("slave: access = %i, format = %i, channels = %i, rate = %i\n",		 params_access(sparams), params_format(sparams),		 params_channels(sparams), params_rate(sparams));	oss_frame_size = snd_pcm_format_physical_width(params_format(params)) *			 params_channels(params) / 8;	snd_pcm_oss_plugin_clear(substream);	if (!direct) {		/* add necessary plugins */		snd_pcm_oss_plugin_clear(substream);		if ((err = snd_pcm_plug_format_plugins(substream,						       params, 						       sparams)) < 0) {			snd_printd("snd_pcm_plug_format_plugins failed: %i\n", err);			snd_pcm_oss_plugin_clear(substream);			goto failure;		}		if (runtime->oss.plugin_first) {			snd_pcm_plugin_t *plugin;			if ((err = snd_pcm_plugin_build_io(substream, sparams, &plugin)) < 0) {				snd_printd("snd_pcm_plugin_build_io failed: %i\n", err);				snd_pcm_oss_plugin_clear(substream);				goto failure;			}			if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {				err = snd_pcm_plugin_append(plugin);			} else {				err = snd_pcm_plugin_insert(plugin);			}			if (err < 0) {				snd_pcm_oss_plugin_clear(substream);				goto failure;			}		}	}	err = snd_pcm_oss_period_size(substream, params, sparams);

⌨️ 快捷键说明

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