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

📄 opl3.c

📁 opl3的驱动程序
💻 C
📖 第 1 页 / 共 5 页
字号:
     }
     
     int opl3_detect(int ioaddr, int *osp)
     {
     	/*
     	 * This function returns 1 if the FM chip is present at the given I/O port
     	 * The detection algorithm plays with the timer built in the FM chip and
     	 * looks for a change in the status register.
     	 *
     	 * Note! The timers of the FM chip are not connected to AdLib (and compatible)
     	 * boards.
     	 *
     	 * Note2! The chip is initialized if detected.
     	 */
     
     	unsigned char stat1, signature;
     	int i;
     
     	if (devc != NULL)
     	{
     		printk(KERN_ERR "opl3: Only one OPL3 supported.\n");
     		return 0;
     	}
     
     	devc = (struct opl_devinfo *)kmalloc(sizeof(*devc), GFP_KERNEL);
     
     	if (devc == NULL)
     	{
     		printk(KERN_ERR "opl3: Can't allocate memory for the device control "
     			"structure \n ");
     		return 0;
     	}
     
     	memset(devc, 0, sizeof(*devc));
     	strcpy(devc->fm_info.name, "OPL2");
     
     	if (!request_region(ioaddr, 4, devc->fm_info.name)) {
     		printk(KERN_WARNING "opl3: I/O port 0x%x already in use\n", ioaddr);
     		goto cleanup_devc;
     	}
     
     	devc->osp = osp;
     	devc->base = ioaddr;
     
     	/* Reset timers 1 and 2 */
     	opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK);
     
     	/* Reset the IRQ of the FM chip */
     	opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET);
     
     	signature = stat1 = inb(ioaddr);	/* Status register */
     
     	if (signature != 0x00 && signature != 0x06 && signature != 0x02 &&
     		signature != 0x0f)
     	{
     		MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature));
     		goto cleanup_region;
     	}
     
     	if (signature == 0x06)		/* OPL2 */
     	{
     		detected_model = 2;
     	}
     	else if (signature == 0x00 || signature == 0x0f)	/* OPL3 or OPL4 */
     	{
     		unsigned char tmp;
     
     		detected_model = 3;
     
     		/*
     		 * Detect availability of OPL4 (_experimental_). Works probably
     		 * only after a cold boot. In addition the OPL4 port
     		 * of the chip may not be connected to the PC bus at all.
     		 */
     
     		opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00);
     		opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE);
     
     		if ((tmp = inb(ioaddr)) == 0x02)	/* Have a OPL4 */
     		{
     			detected_model = 4;
     		}
     
     		if (request_region(ioaddr - 8, 2, "OPL4"))	/* OPL4 port was free */
     		{
     			int tmp;
     
     			outb((0x02), ioaddr - 8);	/* Select OPL4 ID register */
     			udelay(10);
     			tmp = inb(ioaddr - 7);		/* Read it */
     			udelay(10);
     
     			if (tmp == 0x20)	/* OPL4 should return 0x20 here */
     			{
     				detected_model = 4;
     				outb((0xF8), ioaddr - 8);	/* Select OPL4 FM mixer control */
     				udelay(10);
     				outb((0x1B), ioaddr - 7);	/* Write value */
     				udelay(10);
     			}
     			else
     			{ /* release OPL4 port */
     				release_region(ioaddr - 8, 2);
     				detected_model = 3;
     			}
     		}
     		opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0);
     	}
     	for (i = 0; i < 9; i++)
     		opl3_command(ioaddr, KEYON_BLOCK + i, 0);	/*
     								 * Note off
     								 */
     
     	opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT);
     	opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00);	/*
     								 * Melodic mode.
     								 */
     	return 1;
     cleanup_region:
     	release_region(ioaddr, 4);
     cleanup_devc:
     	kfree(devc);
     	devc = NULL;
     	return 0;
     }
     
     static int opl3_kill_note  (int devno, int voice, int note, int velocity)
     {
     	 struct physical_voice_info *map;
     
     	 if (voice < 0 || voice >= devc->nr_voice)
     		 return 0;
     
     	 devc->v_alloc->map[voice] = 0;
     
     	 map = &pv_map[devc->lv_map[voice]];
     	 DEB(printk("Kill note %d\n", voice));
     
     	 if (map->voice_mode == 0)
     		 return 0;
     
     	 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20);
     	 devc->voc[voice].keyon_byte = 0;
     	 devc->voc[voice].bender = 0;
     	 devc->voc[voice].volume = 64;
     	 devc->voc[voice].panning = 0xffff;	/* Not set */
     	 devc->voc[voice].bender_range = 200;
     	 devc->voc[voice].orig_freq = 0;
     	 devc->voc[voice].current_freq = 0;
     	 devc->voc[voice].mode = 0;
     	 return 0;
     }
     
     #define HIHAT			0
     #define CYMBAL			1
     #define TOMTOM			2
     #define SNARE			3
     #define BDRUM			4
     #define UNDEFINED		TOMTOM
     #define DEFAULT			TOMTOM
     
     static int store_instr(int instr_no, struct sbi_instrument *instr)
     {
     	if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2))
     		printk(KERN_WARNING "FM warning: Invalid patch format field (key) 0x%x\n", instr->key);
     	memcpy((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof(*instr));
     	return 0;
     }
     
     static int opl3_set_instr  (int dev, int voice, int instr_no)
     {
     	if (voice < 0 || voice >= devc->nr_voice)
     		return 0;
     	if (instr_no < 0 || instr_no >= SBFM_MAXINSTR)
     		instr_no = 0;	/* Acoustic piano (usually) */
     
     	devc->act_i[voice] = &devc->i_map[instr_no];
     	return 0;
     }
     
     /*
      * The next table looks magical, but it certainly is not. Its values have
      * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
      * for i=0. This log-table converts a linear volume-scaling (0..127) to a
      * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume
      * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative
      * volume -8 it was implemented as a table because it is only 128 bytes and
      * it saves a lot of log() calculations. (RH)
      */
     
     static char fm_volume_table[128] =
     {
     	-64, -48, -40, -35, -32, -29, -27, -26,
     	-24, -23, -21, -20, -19, -18, -18, -17,
     	-16, -15, -15, -14, -13, -13, -12, -12,
     	-11, -11, -10, -10, -10, -9, -9, -8,
     	-8, -8, -7, -7, -7, -6, -6, -6,
     	-5, -5, -5, -5, -4, -4, -4, -4,
     	-3, -3, -3, -3, -2, -2, -2, -2,
     	-2, -1, -1, -1, -1, 0, 0, 0,
     	0, 0, 0, 1, 1, 1, 1, 1,
     	1, 2, 2, 2, 2, 2, 2, 2,
     	3, 3, 3, 3, 3, 3, 3, 4,
     	4, 4, 4, 4, 4, 4, 4, 5,
     	5, 5, 5, 5, 5, 5, 5, 5,
     	6, 6, 6, 6, 6, 6, 6, 6,
     	6, 7, 7, 7, 7, 7, 7, 7,
     	7, 7, 7, 8, 8, 8, 8, 8
     };
     
     static void calc_vol(unsigned char *regbyte, int volume, int main_vol)
     {
     	int level = (~*regbyte & 0x3f);
     
     	if (main_vol > 127)
     		main_vol = 127;
     	volume = (volume * main_vol) / 127;
     
     	if (level)
     		level += fm_volume_table[volume];
     
     	if (level > 0x3f)
     		level = 0x3f;
     	if (level < 0)
     		level = 0;
     
     	*regbyte = (*regbyte & 0xc0) | (~level & 0x3f);
     }
     
     static void set_voice_volume(int voice, int volume, int main_vol)
     {
     	unsigned char vol1, vol2, vol3, vol4;
     	struct sbi_instrument *instr;
     	struct physical_voice_info *map;
     
     	if (voice < 0 || voice >= devc->nr_voice)
     		return;
     
     	map = &pv_map[devc->lv_map[voice]];
     	instr = devc->act_i[voice];
     
     	if (!instr)
     		instr = &devc->i_map[0];
     
     	if (instr->channel < 0)
     		return;
     
     	if (devc->voc[voice].mode == 0)
     		return;
     
     	if (devc->voc[voice].mode == 2)
     	{
     		vol1 = instr->operators[2];
     		vol2 = instr->operators[3];
     		if ((instr->operators[10] & 0x01))
     		{
     			calc_vol(&vol1, volume, main_vol);
     			calc_vol(&vol2, volume, main_vol);
     		}
     		else
     		{
     			calc_vol(&vol2, volume, main_vol);
     		}
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
     	}
     	else
     	{	/*
     		 * 4 OP voice
     		 */
     		int connection;
     
     		vol1 = instr->operators[2];
     		vol2 = instr->operators[3];
     		vol3 = instr->operators[OFFS_4OP + 2];
     		vol4 = instr->operators[OFFS_4OP + 3];
     
     		/*
     		 * The connection method for 4 OP devc->voc is defined by the rightmost
     		 * bits at the offsets 10 and 10+OFFS_4OP
     		 */
     
     		connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01);
     
     		switch (connection)
     		{
     			case 0:
     				calc_vol(&vol4, volume, main_vol);
     				break;
     
     			case 1:
     				calc_vol(&vol2, volume, main_vol);
     				calc_vol(&vol4, volume, main_vol);
     				break;
     
     			case 2:
     				calc_vol(&vol1, volume, main_vol);
     				calc_vol(&vol4, volume, main_vol);
     				break;
     
     			case 3:
     				calc_vol(&vol1, volume, main_vol);
     				calc_vol(&vol3, volume, main_vol);
     				calc_vol(&vol4, volume, main_vol);
     				break;
     
     			default:
     				;
     		}
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1);
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2);
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3);
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4);
     	}
     }
     
     static int opl3_start_note (int dev, int voice, int note, int volume)
     {
     	unsigned char data, fpc;
     	int block, fnum, freq, voice_mode, pan;
     	struct sbi_instrument *instr;
     	struct physical_voice_info *map;
     
     	if (voice < 0 || voice >= devc->nr_voice)
     		return 0;
     
     	map = &pv_map[devc->lv_map[voice]];
     	pan = devc->voc[voice].panning;
     
     	if (map->voice_mode == 0)
     		return 0;
     
     	if (note == 255)	/*
     				 * Just change the volume
     				 */
     	{
     		set_voice_volume(voice, volume, devc->voc[voice].volume);
     		return 0;
     	}
     
     	/*
     	 * Kill previous note before playing
     	 */
     	
     	opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff);	/*
     									 * Carrier
     									 * volume to
     									 * min
     									 */
     	opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff);	/*
     									 * Modulator
     									 * volume to
     									 */
     
     	if (map->voice_mode == 4)
     	{
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff);
     		opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff);
     	}
     
     	opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);	/*
     									 * Note
     									 * off
     									 */
     
     	instr = devc->act_i[voice];
     	
     	if (!instr)
     		instr = &devc->i_map[0];
     
     	if (instr->channel < 0)
     	{
     		printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice);
     		return 0;
     	}
     
     	if (map->voice_mode == 2 && instr->key == OPL3_PATCH)
     		return 0;	/*
     				 * Cannot play
     				 */
     
     	voice_mode = map->voice_mode;
     
     	if (voice_mode == 4)
     	{
     		int voice_shift;
     
     		voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3;
     		voice_shift += map->voice_num;
     
     		if (instr->key != OPL3_PATCH)	/*
     						 * Just 2 OP patch
     						 */
     		{
     			voice_mode = 2;
     			devc->cmask &= ~(1 << voice_shift);
     		}
     		else
     		{
     			devc->cmask |= (1 << voice_shift);
     		}
     
     		opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask);
     	}
     
     	/*
     	 * Set Sound Characteristics
     	 */
     	
     	opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]);
     	opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]);
     
     	/*
     	 * Set Attack/Decay
     	 */
     	
     	opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]);
     	opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]);
     
     	/*
     	 * Set Sustain/Release
     	 */
     	
     	opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]);
     	opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]);
     
     	/*
     	 * Set Wave Select
     	 */
     
     	opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]);
     	opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]);
     
     	/*
     	 * Set Feedback/Connection
     	 */
     	
     	fpc = instr->operators[10];
     
     	if (pan != 0xffff)
     	{
     		fpc &= ~STEREO_BITS;
     		if (pan < -64)
     			fpc |= VOICE_TO_LEFT;
     		else
     			if (pan > 64)
     				fpc |= VOICE_TO_RIGHT;
     			else
     				fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT);
     	}
     
     	if (!(fpc & 0x30))
     		fpc |= 0x30;	/*
     				 * Ensure that at least one chn is enabled
     				 */
     	opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc);
     
     	/*
     	 * If the voice is a 4 OP one, initialize the operators 3 and 4 also
     	 */
     
     	if (voice_mode == 4)
     	{
     		/*
     		 * Set Sound Characteristics
     		 */

⌨️ 快捷键说明

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