| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * sound/opl3.c | 
 | 3 |  * | 
 | 4 |  * A low level driver for Yamaha YM3812 and OPL-3 -chips | 
 | 5 |  * | 
 | 6 |  * | 
 | 7 |  * Copyright (C) by Hannu Savolainen 1993-1997 | 
 | 8 |  * | 
 | 9 |  * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL) | 
 | 10 |  * Version 2 (June 1991). See the "COPYING" file distributed with this software | 
 | 11 |  * for more info. | 
 | 12 |  * | 
 | 13 |  * | 
 | 14 |  * Changes | 
 | 15 |  *	Thomas Sailer   	ioctl code reworked (vmalloc/vfree removed) | 
 | 16 |  *	Alan Cox		modularisation, fixed sound_mem allocs. | 
 | 17 |  *	Christoph Hellwig	Adapted to module_init/module_exit | 
 | 18 |  *	Arnaldo C. de Melo	get rid of check_region, use request_region for | 
 | 19 |  *				OPL4, release it on exit, some cleanups. | 
 | 20 |  * | 
 | 21 |  * Status | 
 | 22 |  *	Believed to work. Badly needs rewriting a bit to support multiple | 
 | 23 |  *	OPL3 devices. | 
 | 24 |  */ | 
 | 25 |  | 
 | 26 | #include <linux/init.h> | 
 | 27 | #include <linux/module.h> | 
 | 28 | #include <linux/delay.h> | 
 | 29 |  | 
 | 30 | /* | 
 | 31 |  * Major improvements to the FM handling 30AUG92 by Rob Hooft, | 
 | 32 |  * hooft@chem.ruu.nl | 
 | 33 |  */ | 
 | 34 |  | 
 | 35 | #include "sound_config.h" | 
 | 36 |  | 
 | 37 | #include "opl3.h" | 
 | 38 | #include "opl3_hw.h" | 
 | 39 |  | 
 | 40 | #define MAX_VOICE	18 | 
 | 41 | #define OFFS_4OP	11 | 
 | 42 |  | 
 | 43 | struct voice_info | 
 | 44 | { | 
 | 45 | 	unsigned char   keyon_byte; | 
 | 46 | 	long            bender; | 
 | 47 | 	long            bender_range; | 
 | 48 | 	unsigned long   orig_freq; | 
 | 49 | 	unsigned long   current_freq; | 
 | 50 | 	int             volume; | 
 | 51 | 	int             mode; | 
 | 52 | 	int             panning;	/* 0xffff means not set */ | 
 | 53 | }; | 
 | 54 |  | 
 | 55 | typedef struct opl_devinfo | 
 | 56 | { | 
 | 57 | 	int             base; | 
 | 58 | 	int             left_io, right_io; | 
 | 59 | 	int             nr_voice; | 
 | 60 | 	int             lv_map[MAX_VOICE]; | 
 | 61 |  | 
 | 62 | 	struct voice_info voc[MAX_VOICE]; | 
 | 63 | 	struct voice_alloc_info *v_alloc; | 
 | 64 | 	struct channel_info *chn_info; | 
 | 65 |  | 
 | 66 | 	struct sbi_instrument i_map[SBFM_MAXINSTR]; | 
 | 67 | 	struct sbi_instrument *act_i[MAX_VOICE]; | 
 | 68 |  | 
 | 69 | 	struct synth_info fm_info; | 
 | 70 |  | 
 | 71 | 	int             busy; | 
 | 72 | 	int             model; | 
 | 73 | 	unsigned char   cmask; | 
 | 74 |  | 
 | 75 | 	int             is_opl4; | 
 | 76 | 	int            *osp; | 
 | 77 | } opl_devinfo; | 
 | 78 |  | 
 | 79 | static struct opl_devinfo *devc = NULL; | 
 | 80 |  | 
 | 81 | static int      detected_model; | 
 | 82 |  | 
 | 83 | static int      store_instr(int instr_no, struct sbi_instrument *instr); | 
 | 84 | static void     freq_to_fnum(int freq, int *block, int *fnum); | 
 | 85 | static void     opl3_command(int io_addr, unsigned int addr, unsigned int val); | 
 | 86 | static int      opl3_kill_note(int dev, int voice, int note, int velocity); | 
 | 87 |  | 
 | 88 | static void enter_4op_mode(void) | 
 | 89 | { | 
 | 90 | 	int i; | 
 | 91 | 	static int v4op[MAX_VOICE] = { | 
 | 92 | 		0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17 | 
 | 93 | 	}; | 
 | 94 |  | 
 | 95 | 	devc->cmask = 0x3f;	/* Connect all possible 4 OP voice operators */ | 
 | 96 | 	opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x3f); | 
 | 97 |  | 
 | 98 | 	for (i = 0; i < 3; i++) | 
 | 99 | 		pv_map[i].voice_mode = 4; | 
 | 100 | 	for (i = 3; i < 6; i++) | 
 | 101 | 		pv_map[i].voice_mode = 0; | 
 | 102 |  | 
 | 103 | 	for (i = 9; i < 12; i++) | 
 | 104 | 		pv_map[i].voice_mode = 4; | 
 | 105 | 	for (i = 12; i < 15; i++) | 
 | 106 | 		pv_map[i].voice_mode = 0; | 
 | 107 |  | 
 | 108 | 	for (i = 0; i < 12; i++) | 
 | 109 | 		devc->lv_map[i] = v4op[i]; | 
 | 110 | 	devc->v_alloc->max_voice = devc->nr_voice = 12; | 
 | 111 | } | 
 | 112 |  | 
 | 113 | static int opl3_ioctl(int dev, unsigned int cmd, void __user * arg) | 
 | 114 | { | 
 | 115 | 	struct sbi_instrument ins; | 
 | 116 | 	 | 
 | 117 | 	switch (cmd) { | 
 | 118 | 		case SNDCTL_FM_LOAD_INSTR: | 
 | 119 | 			printk(KERN_WARNING "Warning: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n"); | 
 | 120 | 			if (copy_from_user(&ins, arg, sizeof(ins))) | 
 | 121 | 				return -EFAULT; | 
 | 122 | 			if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) { | 
 | 123 | 				printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel); | 
 | 124 | 				return -EINVAL; | 
 | 125 | 			} | 
 | 126 | 			return store_instr(ins.channel, &ins); | 
 | 127 |  | 
 | 128 | 		case SNDCTL_SYNTH_INFO: | 
 | 129 | 			devc->fm_info.nr_voices = (devc->nr_voice == 12) ? 6 : devc->nr_voice; | 
 | 130 | 			if (copy_to_user(arg, &devc->fm_info, sizeof(devc->fm_info))) | 
 | 131 | 				return -EFAULT; | 
 | 132 | 			return 0; | 
 | 133 |  | 
 | 134 | 		case SNDCTL_SYNTH_MEMAVL: | 
 | 135 | 			return 0x7fffffff; | 
 | 136 |  | 
 | 137 | 		case SNDCTL_FM_4OP_ENABLE: | 
 | 138 | 			if (devc->model == 2) | 
 | 139 | 				enter_4op_mode(); | 
 | 140 | 			return 0; | 
 | 141 |  | 
 | 142 | 		default: | 
 | 143 | 			return -EINVAL; | 
 | 144 | 	} | 
 | 145 | } | 
 | 146 |  | 
 | 147 | int opl3_detect(int ioaddr, int *osp) | 
 | 148 | { | 
 | 149 | 	/* | 
 | 150 | 	 * This function returns 1 if the FM chip is present at the given I/O port | 
 | 151 | 	 * The detection algorithm plays with the timer built in the FM chip and | 
 | 152 | 	 * looks for a change in the status register. | 
 | 153 | 	 * | 
 | 154 | 	 * Note! The timers of the FM chip are not connected to AdLib (and compatible) | 
 | 155 | 	 * boards. | 
 | 156 | 	 * | 
 | 157 | 	 * Note2! The chip is initialized if detected. | 
 | 158 | 	 */ | 
 | 159 |  | 
 | 160 | 	unsigned char stat1, signature; | 
 | 161 | 	int i; | 
 | 162 |  | 
 | 163 | 	if (devc != NULL) | 
 | 164 | 	{ | 
 | 165 | 		printk(KERN_ERR "opl3: Only one OPL3 supported.\n"); | 
 | 166 | 		return 0; | 
 | 167 | 	} | 
 | 168 |  | 
 | 169 | 	devc = (struct opl_devinfo *)kmalloc(sizeof(*devc), GFP_KERNEL); | 
 | 170 |  | 
 | 171 | 	if (devc == NULL) | 
 | 172 | 	{ | 
 | 173 | 		printk(KERN_ERR "opl3: Can't allocate memory for the device control " | 
 | 174 | 			"structure \n "); | 
 | 175 | 		return 0; | 
 | 176 | 	} | 
 | 177 |  | 
 | 178 | 	memset(devc, 0, sizeof(*devc)); | 
 | 179 | 	strcpy(devc->fm_info.name, "OPL2"); | 
 | 180 |  | 
 | 181 | 	if (!request_region(ioaddr, 4, devc->fm_info.name)) { | 
 | 182 | 		printk(KERN_WARNING "opl3: I/O port 0x%x already in use\n", ioaddr); | 
 | 183 | 		goto cleanup_devc; | 
 | 184 | 	} | 
 | 185 |  | 
 | 186 | 	devc->osp = osp; | 
 | 187 | 	devc->base = ioaddr; | 
 | 188 |  | 
 | 189 | 	/* Reset timers 1 and 2 */ | 
 | 190 | 	opl3_command(ioaddr, TIMER_CONTROL_REGISTER, TIMER1_MASK | TIMER2_MASK); | 
 | 191 |  | 
 | 192 | 	/* Reset the IRQ of the FM chip */ | 
 | 193 | 	opl3_command(ioaddr, TIMER_CONTROL_REGISTER, IRQ_RESET); | 
 | 194 |  | 
 | 195 | 	signature = stat1 = inb(ioaddr);	/* Status register */ | 
 | 196 |  | 
 | 197 | 	if (signature != 0x00 && signature != 0x06 && signature != 0x02 && | 
 | 198 | 		signature != 0x0f) | 
 | 199 | 	{ | 
 | 200 | 		MDB(printk(KERN_INFO "OPL3 not detected %x\n", signature)); | 
 | 201 | 		goto cleanup_region; | 
 | 202 | 	} | 
 | 203 |  | 
 | 204 | 	if (signature == 0x06)		/* OPL2 */ | 
 | 205 | 	{ | 
 | 206 | 		detected_model = 2; | 
 | 207 | 	} | 
 | 208 | 	else if (signature == 0x00 || signature == 0x0f)	/* OPL3 or OPL4 */ | 
 | 209 | 	{ | 
 | 210 | 		unsigned char tmp; | 
 | 211 |  | 
 | 212 | 		detected_model = 3; | 
 | 213 |  | 
 | 214 | 		/* | 
 | 215 | 		 * Detect availability of OPL4 (_experimental_). Works probably | 
 | 216 | 		 * only after a cold boot. In addition the OPL4 port | 
 | 217 | 		 * of the chip may not be connected to the PC bus at all. | 
 | 218 | 		 */ | 
 | 219 |  | 
 | 220 | 		opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0x00); | 
 | 221 | 		opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, OPL3_ENABLE | OPL4_ENABLE); | 
 | 222 |  | 
 | 223 | 		if ((tmp = inb(ioaddr)) == 0x02)	/* Have a OPL4 */ | 
 | 224 | 		{ | 
 | 225 | 			detected_model = 4; | 
 | 226 | 		} | 
 | 227 |  | 
 | 228 | 		if (request_region(ioaddr - 8, 2, "OPL4"))	/* OPL4 port was free */ | 
 | 229 | 		{ | 
 | 230 | 			int tmp; | 
 | 231 |  | 
 | 232 | 			outb((0x02), ioaddr - 8);	/* Select OPL4 ID register */ | 
 | 233 | 			udelay(10); | 
 | 234 | 			tmp = inb(ioaddr - 7);		/* Read it */ | 
 | 235 | 			udelay(10); | 
 | 236 |  | 
 | 237 | 			if (tmp == 0x20)	/* OPL4 should return 0x20 here */ | 
 | 238 | 			{ | 
 | 239 | 				detected_model = 4; | 
 | 240 | 				outb((0xF8), ioaddr - 8);	/* Select OPL4 FM mixer control */ | 
 | 241 | 				udelay(10); | 
 | 242 | 				outb((0x1B), ioaddr - 7);	/* Write value */ | 
 | 243 | 				udelay(10); | 
 | 244 | 			} | 
 | 245 | 			else | 
 | 246 | 			{ /* release OPL4 port */ | 
 | 247 | 				release_region(ioaddr - 8, 2); | 
 | 248 | 				detected_model = 3; | 
 | 249 | 			} | 
 | 250 | 		} | 
 | 251 | 		opl3_command(ioaddr + 2, OPL3_MODE_REGISTER, 0); | 
 | 252 | 	} | 
 | 253 | 	for (i = 0; i < 9; i++) | 
 | 254 | 		opl3_command(ioaddr, KEYON_BLOCK + i, 0);	/* | 
 | 255 | 								 * Note off | 
 | 256 | 								 */ | 
 | 257 |  | 
 | 258 | 	opl3_command(ioaddr, TEST_REGISTER, ENABLE_WAVE_SELECT); | 
 | 259 | 	opl3_command(ioaddr, PERCOSSION_REGISTER, 0x00);	/* | 
 | 260 | 								 * Melodic mode. | 
 | 261 | 								 */ | 
 | 262 | 	return 1; | 
 | 263 | cleanup_region: | 
 | 264 | 	release_region(ioaddr, 4); | 
 | 265 | cleanup_devc: | 
 | 266 | 	kfree(devc); | 
 | 267 | 	devc = NULL; | 
 | 268 | 	return 0; | 
 | 269 | } | 
 | 270 |  | 
 | 271 | static int opl3_kill_note  (int devno, int voice, int note, int velocity) | 
 | 272 | { | 
 | 273 | 	 struct physical_voice_info *map; | 
 | 274 |  | 
 | 275 | 	 if (voice < 0 || voice >= devc->nr_voice) | 
 | 276 | 		 return 0; | 
 | 277 |  | 
 | 278 | 	 devc->v_alloc->map[voice] = 0; | 
 | 279 |  | 
 | 280 | 	 map = &pv_map[devc->lv_map[voice]]; | 
 | 281 | 	 DEB(printk("Kill note %d\n", voice)); | 
 | 282 |  | 
 | 283 | 	 if (map->voice_mode == 0) | 
 | 284 | 		 return 0; | 
 | 285 |  | 
 | 286 | 	 opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, devc->voc[voice].keyon_byte & ~0x20); | 
 | 287 | 	 devc->voc[voice].keyon_byte = 0; | 
 | 288 | 	 devc->voc[voice].bender = 0; | 
 | 289 | 	 devc->voc[voice].volume = 64; | 
 | 290 | 	 devc->voc[voice].panning = 0xffff;	/* Not set */ | 
 | 291 | 	 devc->voc[voice].bender_range = 200; | 
 | 292 | 	 devc->voc[voice].orig_freq = 0; | 
 | 293 | 	 devc->voc[voice].current_freq = 0; | 
 | 294 | 	 devc->voc[voice].mode = 0; | 
 | 295 | 	 return 0; | 
 | 296 | } | 
 | 297 |  | 
 | 298 | #define HIHAT			0 | 
 | 299 | #define CYMBAL			1 | 
 | 300 | #define TOMTOM			2 | 
 | 301 | #define SNARE			3 | 
 | 302 | #define BDRUM			4 | 
 | 303 | #define UNDEFINED		TOMTOM | 
 | 304 | #define DEFAULT			TOMTOM | 
 | 305 |  | 
 | 306 | static int store_instr(int instr_no, struct sbi_instrument *instr) | 
 | 307 | { | 
 | 308 | 	if (instr->key != FM_PATCH && (instr->key != OPL3_PATCH || devc->model != 2)) | 
 | 309 | 		printk(KERN_WARNING "FM warning: Invalid patch format field (key) 0x%x\n", instr->key); | 
 | 310 | 	memcpy((char *) &(devc->i_map[instr_no]), (char *) instr, sizeof(*instr)); | 
 | 311 | 	return 0; | 
 | 312 | } | 
 | 313 |  | 
 | 314 | static int opl3_set_instr  (int dev, int voice, int instr_no) | 
 | 315 | { | 
 | 316 | 	if (voice < 0 || voice >= devc->nr_voice) | 
 | 317 | 		return 0; | 
 | 318 | 	if (instr_no < 0 || instr_no >= SBFM_MAXINSTR) | 
 | 319 | 		instr_no = 0;	/* Acoustic piano (usually) */ | 
 | 320 |  | 
 | 321 | 	devc->act_i[voice] = &devc->i_map[instr_no]; | 
 | 322 | 	return 0; | 
 | 323 | } | 
 | 324 |  | 
 | 325 | /* | 
 | 326 |  * The next table looks magical, but it certainly is not. Its values have | 
 | 327 |  * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception | 
 | 328 |  * for i=0. This log-table converts a linear volume-scaling (0..127) to a | 
 | 329 |  * logarithmic scaling as present in the FM-synthesizer chips. so :    Volume | 
 | 330 |  * 64 =  0 db = relative volume  0 and:    Volume 32 = -6 db = relative | 
 | 331 |  * volume -8 it was implemented as a table because it is only 128 bytes and | 
 | 332 |  * it saves a lot of log() calculations. (RH) | 
 | 333 |  */ | 
 | 334 |  | 
 | 335 | static char fm_volume_table[128] = | 
 | 336 | { | 
 | 337 | 	-64, -48, -40, -35, -32, -29, -27, -26, | 
 | 338 | 	-24, -23, -21, -20, -19, -18, -18, -17, | 
 | 339 | 	-16, -15, -15, -14, -13, -13, -12, -12, | 
 | 340 | 	-11, -11, -10, -10, -10, -9, -9, -8, | 
 | 341 | 	-8, -8, -7, -7, -7, -6, -6, -6, | 
 | 342 | 	-5, -5, -5, -5, -4, -4, -4, -4, | 
 | 343 | 	-3, -3, -3, -3, -2, -2, -2, -2, | 
 | 344 | 	-2, -1, -1, -1, -1, 0, 0, 0, | 
 | 345 | 	0, 0, 0, 1, 1, 1, 1, 1, | 
 | 346 | 	1, 2, 2, 2, 2, 2, 2, 2, | 
 | 347 | 	3, 3, 3, 3, 3, 3, 3, 4, | 
 | 348 | 	4, 4, 4, 4, 4, 4, 4, 5, | 
 | 349 | 	5, 5, 5, 5, 5, 5, 5, 5, | 
 | 350 | 	6, 6, 6, 6, 6, 6, 6, 6, | 
 | 351 | 	6, 7, 7, 7, 7, 7, 7, 7, | 
 | 352 | 	7, 7, 7, 8, 8, 8, 8, 8 | 
 | 353 | }; | 
 | 354 |  | 
 | 355 | static void calc_vol(unsigned char *regbyte, int volume, int main_vol) | 
 | 356 | { | 
 | 357 | 	int level = (~*regbyte & 0x3f); | 
 | 358 |  | 
 | 359 | 	if (main_vol > 127) | 
 | 360 | 		main_vol = 127; | 
 | 361 | 	volume = (volume * main_vol) / 127; | 
 | 362 |  | 
 | 363 | 	if (level) | 
 | 364 | 		level += fm_volume_table[volume]; | 
 | 365 |  | 
 | 366 | 	if (level > 0x3f) | 
 | 367 | 		level = 0x3f; | 
 | 368 | 	if (level < 0) | 
 | 369 | 		level = 0; | 
 | 370 |  | 
 | 371 | 	*regbyte = (*regbyte & 0xc0) | (~level & 0x3f); | 
 | 372 | } | 
 | 373 |  | 
 | 374 | static void set_voice_volume(int voice, int volume, int main_vol) | 
 | 375 | { | 
 | 376 | 	unsigned char vol1, vol2, vol3, vol4; | 
 | 377 | 	struct sbi_instrument *instr; | 
 | 378 | 	struct physical_voice_info *map; | 
 | 379 |  | 
 | 380 | 	if (voice < 0 || voice >= devc->nr_voice) | 
 | 381 | 		return; | 
 | 382 |  | 
 | 383 | 	map = &pv_map[devc->lv_map[voice]]; | 
 | 384 | 	instr = devc->act_i[voice]; | 
 | 385 |  | 
 | 386 | 	if (!instr) | 
 | 387 | 		instr = &devc->i_map[0]; | 
 | 388 |  | 
 | 389 | 	if (instr->channel < 0) | 
 | 390 | 		return; | 
 | 391 |  | 
 | 392 | 	if (devc->voc[voice].mode == 0) | 
 | 393 | 		return; | 
 | 394 |  | 
 | 395 | 	if (devc->voc[voice].mode == 2) | 
 | 396 | 	{ | 
 | 397 | 		vol1 = instr->operators[2]; | 
 | 398 | 		vol2 = instr->operators[3]; | 
 | 399 | 		if ((instr->operators[10] & 0x01)) | 
 | 400 | 		{ | 
 | 401 | 			calc_vol(&vol1, volume, main_vol); | 
 | 402 | 			calc_vol(&vol2, volume, main_vol); | 
 | 403 | 		} | 
 | 404 | 		else | 
 | 405 | 		{ | 
 | 406 | 			calc_vol(&vol2, volume, main_vol); | 
 | 407 | 		} | 
 | 408 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1); | 
 | 409 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2); | 
 | 410 | 	} | 
 | 411 | 	else | 
 | 412 | 	{	/* | 
 | 413 | 		 * 4 OP voice | 
 | 414 | 		 */ | 
 | 415 | 		int connection; | 
 | 416 |  | 
 | 417 | 		vol1 = instr->operators[2]; | 
 | 418 | 		vol2 = instr->operators[3]; | 
 | 419 | 		vol3 = instr->operators[OFFS_4OP + 2]; | 
 | 420 | 		vol4 = instr->operators[OFFS_4OP + 3]; | 
 | 421 |  | 
 | 422 | 		/* | 
 | 423 | 		 * The connection method for 4 OP devc->voc is defined by the rightmost | 
 | 424 | 		 * bits at the offsets 10 and 10+OFFS_4OP | 
 | 425 | 		 */ | 
 | 426 |  | 
 | 427 | 		connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01); | 
 | 428 |  | 
 | 429 | 		switch (connection) | 
 | 430 | 		{ | 
 | 431 | 			case 0: | 
 | 432 | 				calc_vol(&vol4, volume, main_vol); | 
 | 433 | 				break; | 
 | 434 |  | 
 | 435 | 			case 1: | 
 | 436 | 				calc_vol(&vol2, volume, main_vol); | 
 | 437 | 				calc_vol(&vol4, volume, main_vol); | 
 | 438 | 				break; | 
 | 439 |  | 
 | 440 | 			case 2: | 
 | 441 | 				calc_vol(&vol1, volume, main_vol); | 
 | 442 | 				calc_vol(&vol4, volume, main_vol); | 
 | 443 | 				break; | 
 | 444 |  | 
 | 445 | 			case 3: | 
 | 446 | 				calc_vol(&vol1, volume, main_vol); | 
 | 447 | 				calc_vol(&vol3, volume, main_vol); | 
 | 448 | 				calc_vol(&vol4, volume, main_vol); | 
 | 449 | 				break; | 
 | 450 |  | 
 | 451 | 			default: | 
 | 452 | 				; | 
 | 453 | 		} | 
 | 454 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], vol1); | 
 | 455 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], vol2); | 
 | 456 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], vol3); | 
 | 457 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], vol4); | 
 | 458 | 	} | 
 | 459 | } | 
 | 460 |  | 
 | 461 | static int opl3_start_note (int dev, int voice, int note, int volume) | 
 | 462 | { | 
 | 463 | 	unsigned char data, fpc; | 
 | 464 | 	int block, fnum, freq, voice_mode, pan; | 
 | 465 | 	struct sbi_instrument *instr; | 
 | 466 | 	struct physical_voice_info *map; | 
 | 467 |  | 
 | 468 | 	if (voice < 0 || voice >= devc->nr_voice) | 
 | 469 | 		return 0; | 
 | 470 |  | 
 | 471 | 	map = &pv_map[devc->lv_map[voice]]; | 
 | 472 | 	pan = devc->voc[voice].panning; | 
 | 473 |  | 
 | 474 | 	if (map->voice_mode == 0) | 
 | 475 | 		return 0; | 
 | 476 |  | 
 | 477 | 	if (note == 255)	/* | 
 | 478 | 				 * Just change the volume | 
 | 479 | 				 */ | 
 | 480 | 	{ | 
 | 481 | 		set_voice_volume(voice, volume, devc->voc[voice].volume); | 
 | 482 | 		return 0; | 
 | 483 | 	} | 
 | 484 |  | 
 | 485 | 	/* | 
 | 486 | 	 * Kill previous note before playing | 
 | 487 | 	 */ | 
 | 488 | 	 | 
 | 489 | 	opl3_command(map->ioaddr, KSL_LEVEL + map->op[1], 0xff);	/* | 
 | 490 | 									 * Carrier | 
 | 491 | 									 * volume to | 
 | 492 | 									 * min | 
 | 493 | 									 */ | 
 | 494 | 	opl3_command(map->ioaddr, KSL_LEVEL + map->op[0], 0xff);	/* | 
 | 495 | 									 * Modulator | 
 | 496 | 									 * volume to | 
 | 497 | 									 */ | 
 | 498 |  | 
 | 499 | 	if (map->voice_mode == 4) | 
 | 500 | 	{ | 
 | 501 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[2], 0xff); | 
 | 502 | 		opl3_command(map->ioaddr, KSL_LEVEL + map->op[3], 0xff); | 
 | 503 | 	} | 
 | 504 |  | 
 | 505 | 	opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, 0x00);	/* | 
 | 506 | 									 * Note | 
 | 507 | 									 * off | 
 | 508 | 									 */ | 
 | 509 |  | 
 | 510 | 	instr = devc->act_i[voice]; | 
 | 511 | 	 | 
 | 512 | 	if (!instr) | 
 | 513 | 		instr = &devc->i_map[0]; | 
 | 514 |  | 
 | 515 | 	if (instr->channel < 0) | 
 | 516 | 	{ | 
 | 517 | 		printk(KERN_WARNING "opl3: Initializing voice %d with undefined instrument\n", voice); | 
 | 518 | 		return 0; | 
 | 519 | 	} | 
 | 520 |  | 
 | 521 | 	if (map->voice_mode == 2 && instr->key == OPL3_PATCH) | 
 | 522 | 		return 0;	/* | 
 | 523 | 				 * Cannot play | 
 | 524 | 				 */ | 
 | 525 |  | 
 | 526 | 	voice_mode = map->voice_mode; | 
 | 527 |  | 
 | 528 | 	if (voice_mode == 4) | 
 | 529 | 	{ | 
 | 530 | 		int voice_shift; | 
 | 531 |  | 
 | 532 | 		voice_shift = (map->ioaddr == devc->left_io) ? 0 : 3; | 
 | 533 | 		voice_shift += map->voice_num; | 
 | 534 |  | 
 | 535 | 		if (instr->key != OPL3_PATCH)	/* | 
 | 536 | 						 * Just 2 OP patch | 
 | 537 | 						 */ | 
 | 538 | 		{ | 
 | 539 | 			voice_mode = 2; | 
 | 540 | 			devc->cmask &= ~(1 << voice_shift); | 
 | 541 | 		} | 
 | 542 | 		else | 
 | 543 | 		{ | 
 | 544 | 			devc->cmask |= (1 << voice_shift); | 
 | 545 | 		} | 
 | 546 |  | 
 | 547 | 		opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask); | 
 | 548 | 	} | 
 | 549 |  | 
 | 550 | 	/* | 
 | 551 | 	 * Set Sound Characteristics | 
 | 552 | 	 */ | 
 | 553 | 	 | 
 | 554 | 	opl3_command(map->ioaddr, AM_VIB + map->op[0], instr->operators[0]); | 
 | 555 | 	opl3_command(map->ioaddr, AM_VIB + map->op[1], instr->operators[1]); | 
 | 556 |  | 
 | 557 | 	/* | 
 | 558 | 	 * Set Attack/Decay | 
 | 559 | 	 */ | 
 | 560 | 	 | 
 | 561 | 	opl3_command(map->ioaddr, ATTACK_DECAY + map->op[0], instr->operators[4]); | 
 | 562 | 	opl3_command(map->ioaddr, ATTACK_DECAY + map->op[1], instr->operators[5]); | 
 | 563 |  | 
 | 564 | 	/* | 
 | 565 | 	 * Set Sustain/Release | 
 | 566 | 	 */ | 
 | 567 | 	 | 
 | 568 | 	opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[0], instr->operators[6]); | 
 | 569 | 	opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[1], instr->operators[7]); | 
 | 570 |  | 
 | 571 | 	/* | 
 | 572 | 	 * Set Wave Select | 
 | 573 | 	 */ | 
 | 574 |  | 
 | 575 | 	opl3_command(map->ioaddr, WAVE_SELECT + map->op[0], instr->operators[8]); | 
 | 576 | 	opl3_command(map->ioaddr, WAVE_SELECT + map->op[1], instr->operators[9]); | 
 | 577 |  | 
 | 578 | 	/* | 
 | 579 | 	 * Set Feedback/Connection | 
 | 580 | 	 */ | 
 | 581 | 	 | 
 | 582 | 	fpc = instr->operators[10]; | 
 | 583 |  | 
 | 584 | 	if (pan != 0xffff) | 
 | 585 | 	{ | 
 | 586 | 		fpc &= ~STEREO_BITS; | 
 | 587 | 		if (pan < -64) | 
 | 588 | 			fpc |= VOICE_TO_LEFT; | 
 | 589 | 		else | 
 | 590 | 			if (pan > 64) | 
 | 591 | 				fpc |= VOICE_TO_RIGHT; | 
 | 592 | 			else | 
 | 593 | 				fpc |= (VOICE_TO_LEFT | VOICE_TO_RIGHT); | 
 | 594 | 	} | 
 | 595 |  | 
 | 596 | 	if (!(fpc & 0x30)) | 
 | 597 | 		fpc |= 0x30;	/* | 
 | 598 | 				 * Ensure that at least one chn is enabled | 
 | 599 | 				 */ | 
 | 600 | 	opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num, fpc); | 
 | 601 |  | 
 | 602 | 	/* | 
 | 603 | 	 * If the voice is a 4 OP one, initialize the operators 3 and 4 also | 
 | 604 | 	 */ | 
 | 605 |  | 
 | 606 | 	if (voice_mode == 4) | 
 | 607 | 	{ | 
 | 608 | 		/* | 
 | 609 | 		 * Set Sound Characteristics | 
 | 610 | 		 */ | 
 | 611 | 	 | 
 | 612 | 		opl3_command(map->ioaddr, AM_VIB + map->op[2], instr->operators[OFFS_4OP + 0]); | 
 | 613 | 		opl3_command(map->ioaddr, AM_VIB + map->op[3], instr->operators[OFFS_4OP + 1]); | 
 | 614 |  | 
 | 615 | 		/* | 
 | 616 | 		 * Set Attack/Decay | 
 | 617 | 		 */ | 
 | 618 | 		 | 
 | 619 | 		opl3_command(map->ioaddr, ATTACK_DECAY + map->op[2], instr->operators[OFFS_4OP + 4]); | 
 | 620 | 		opl3_command(map->ioaddr, ATTACK_DECAY + map->op[3], instr->operators[OFFS_4OP + 5]); | 
 | 621 |  | 
 | 622 | 		/* | 
 | 623 | 		 * Set Sustain/Release | 
 | 624 | 		 */ | 
 | 625 | 		 | 
 | 626 | 		opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[2], instr->operators[OFFS_4OP + 6]); | 
 | 627 | 		opl3_command(map->ioaddr, SUSTAIN_RELEASE + map->op[3], instr->operators[OFFS_4OP + 7]); | 
 | 628 |  | 
 | 629 | 		/* | 
 | 630 | 		 * Set Wave Select | 
 | 631 | 		 */ | 
 | 632 | 		 | 
 | 633 | 		opl3_command(map->ioaddr, WAVE_SELECT + map->op[2], instr->operators[OFFS_4OP + 8]); | 
 | 634 | 		opl3_command(map->ioaddr, WAVE_SELECT + map->op[3], instr->operators[OFFS_4OP + 9]); | 
 | 635 |  | 
 | 636 | 		/* | 
 | 637 | 		 * Set Feedback/Connection | 
 | 638 | 		 */ | 
 | 639 | 		 | 
 | 640 | 		fpc = instr->operators[OFFS_4OP + 10]; | 
 | 641 | 		if (!(fpc & 0x30)) | 
 | 642 | 			 fpc |= 0x30;	/* | 
 | 643 | 					 * Ensure that at least one chn is enabled | 
 | 644 | 					 */ | 
 | 645 | 		opl3_command(map->ioaddr, FEEDBACK_CONNECTION + map->voice_num + 3, fpc); | 
 | 646 | 	} | 
 | 647 |  | 
 | 648 | 	devc->voc[voice].mode = voice_mode; | 
 | 649 | 	set_voice_volume(voice, volume, devc->voc[voice].volume); | 
 | 650 |  | 
 | 651 | 	freq = devc->voc[voice].orig_freq = note_to_freq(note) / 1000; | 
 | 652 |  | 
 | 653 | 	/* | 
 | 654 | 	 * Since the pitch bender may have been set before playing the note, we | 
 | 655 | 	 * have to calculate the bending now. | 
 | 656 | 	 */ | 
 | 657 |  | 
 | 658 | 	freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0); | 
 | 659 | 	devc->voc[voice].current_freq = freq; | 
 | 660 |  | 
 | 661 | 	freq_to_fnum(freq, &block, &fnum); | 
 | 662 |  | 
 | 663 | 	/* | 
 | 664 | 	 * Play note | 
 | 665 | 	 */ | 
 | 666 |  | 
 | 667 | 	data = fnum & 0xff;	/* | 
 | 668 | 				 * Least significant bits of fnumber | 
 | 669 | 				 */ | 
 | 670 | 	opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data); | 
 | 671 |  | 
 | 672 | 	data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3); | 
 | 673 | 		 devc->voc[voice].keyon_byte = data; | 
 | 674 | 	opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data); | 
 | 675 | 	if (voice_mode == 4) | 
 | 676 | 		opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num + 3, data); | 
 | 677 |  | 
 | 678 | 	return 0; | 
 | 679 | } | 
 | 680 |  | 
 | 681 | static void freq_to_fnum    (int freq, int *block, int *fnum) | 
 | 682 | { | 
 | 683 | 	int f, octave; | 
 | 684 |  | 
 | 685 | 	/* | 
 | 686 | 	 * Converts the note frequency to block and fnum values for the FM chip | 
 | 687 | 	 */ | 
 | 688 | 	/* | 
 | 689 | 	 * First try to compute the block -value (octave) where the note belongs | 
 | 690 | 	 */ | 
 | 691 |  | 
 | 692 | 	f = freq; | 
 | 693 |  | 
 | 694 | 	octave = 5; | 
 | 695 |  | 
 | 696 | 	if (f == 0) | 
 | 697 | 		octave = 0; | 
 | 698 | 	else if (f < 261) | 
 | 699 | 	{ | 
 | 700 | 		while (f < 261) | 
 | 701 | 		{ | 
 | 702 | 			octave--; | 
 | 703 | 			f <<= 1; | 
 | 704 | 		} | 
 | 705 | 	} | 
 | 706 | 	else if (f > 493) | 
 | 707 | 	{ | 
 | 708 | 		while (f > 493) | 
 | 709 | 		{ | 
 | 710 | 			 octave++; | 
 | 711 | 			 f >>= 1; | 
 | 712 | 		} | 
 | 713 | 	} | 
 | 714 |  | 
 | 715 | 	if (octave > 7) | 
 | 716 | 		octave = 7; | 
 | 717 |  | 
 | 718 | 	*fnum = freq * (1 << (20 - octave)) / 49716; | 
 | 719 | 	*block = octave; | 
 | 720 | } | 
 | 721 |  | 
 | 722 | static void opl3_command    (int io_addr, unsigned int addr, unsigned int val) | 
 | 723 | { | 
 | 724 | 	 int i; | 
 | 725 |  | 
 | 726 | 	/* | 
 | 727 | 	 * The original 2-OP synth requires a quite long delay after writing to a | 
 | 728 | 	 * register. The OPL-3 survives with just two INBs | 
 | 729 | 	 */ | 
 | 730 |  | 
 | 731 | 	outb(((unsigned char) (addr & 0xff)), io_addr); | 
 | 732 |  | 
 | 733 | 	if (devc->model != 2) | 
 | 734 | 		udelay(10); | 
 | 735 | 	else | 
 | 736 | 		for (i = 0; i < 2; i++) | 
 | 737 | 			inb(io_addr); | 
 | 738 |  | 
 | 739 | 	outb(((unsigned char) (val & 0xff)), io_addr + 1); | 
 | 740 |  | 
 | 741 | 	if (devc->model != 2) | 
 | 742 | 		udelay(30); | 
 | 743 | 	else | 
 | 744 | 		for (i = 0; i < 2; i++) | 
 | 745 | 			inb(io_addr); | 
 | 746 | } | 
 | 747 |  | 
 | 748 | static void opl3_reset(int devno) | 
 | 749 | { | 
 | 750 | 	int i; | 
 | 751 |  | 
 | 752 | 	for (i = 0; i < 18; i++) | 
 | 753 | 		devc->lv_map[i] = i; | 
 | 754 |  | 
 | 755 | 	for (i = 0; i < devc->nr_voice; i++) | 
 | 756 | 	{ | 
 | 757 | 		opl3_command(pv_map[devc->lv_map[i]].ioaddr, | 
 | 758 | 			KSL_LEVEL + pv_map[devc->lv_map[i]].op[0], 0xff); | 
 | 759 |  | 
 | 760 | 		opl3_command(pv_map[devc->lv_map[i]].ioaddr, | 
 | 761 | 			KSL_LEVEL + pv_map[devc->lv_map[i]].op[1], 0xff); | 
 | 762 |  | 
 | 763 | 		if (pv_map[devc->lv_map[i]].voice_mode == 4) | 
 | 764 | 		{ | 
 | 765 | 			opl3_command(pv_map[devc->lv_map[i]].ioaddr, | 
 | 766 | 				KSL_LEVEL + pv_map[devc->lv_map[i]].op[2], 0xff); | 
 | 767 |  | 
 | 768 | 			opl3_command(pv_map[devc->lv_map[i]].ioaddr, | 
 | 769 | 				KSL_LEVEL + pv_map[devc->lv_map[i]].op[3], 0xff); | 
 | 770 | 		} | 
 | 771 |  | 
 | 772 | 		opl3_kill_note(devno, i, 0, 64); | 
 | 773 | 	} | 
 | 774 |  | 
 | 775 | 	if (devc->model == 2) | 
 | 776 | 	{ | 
 | 777 | 		devc->v_alloc->max_voice = devc->nr_voice = 18; | 
 | 778 |  | 
 | 779 | 		for (i = 0; i < 18; i++) | 
 | 780 | 			pv_map[i].voice_mode = 2; | 
 | 781 |  | 
 | 782 | 	} | 
 | 783 | } | 
 | 784 |  | 
 | 785 | static int opl3_open(int dev, int mode) | 
 | 786 | { | 
 | 787 | 	int i; | 
 | 788 |  | 
 | 789 | 	if (devc->busy) | 
 | 790 | 		return -EBUSY; | 
 | 791 | 	devc->busy = 1; | 
 | 792 |  | 
 | 793 | 	devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9; | 
 | 794 | 	devc->v_alloc->timestamp = 0; | 
 | 795 |  | 
 | 796 | 	for (i = 0; i < 18; i++) | 
 | 797 | 	{ | 
 | 798 | 		devc->v_alloc->map[i] = 0; | 
 | 799 | 		devc->v_alloc->alloc_times[i] = 0; | 
 | 800 | 	} | 
 | 801 |  | 
 | 802 | 	devc->cmask = 0x00;	/* | 
 | 803 | 				 * Just 2 OP mode | 
 | 804 | 				 */ | 
 | 805 | 	if (devc->model == 2) | 
 | 806 | 		opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, devc->cmask); | 
 | 807 | 	return 0; | 
 | 808 | } | 
 | 809 |  | 
 | 810 | static void opl3_close(int dev) | 
 | 811 | { | 
 | 812 | 	devc->busy = 0; | 
 | 813 | 	devc->v_alloc->max_voice = devc->nr_voice = (devc->model == 2) ? 18 : 9; | 
 | 814 |  | 
 | 815 | 	devc->fm_info.nr_drums = 0; | 
 | 816 | 	devc->fm_info.perc_mode = 0; | 
 | 817 |  | 
 | 818 | 	opl3_reset(dev); | 
 | 819 | } | 
 | 820 |  | 
 | 821 | static void opl3_hw_control(int dev, unsigned char *event) | 
 | 822 | { | 
 | 823 | } | 
 | 824 |  | 
 | 825 | static int opl3_load_patch(int dev, int format, const char __user *addr, | 
 | 826 | 		int offs, int count, int pmgr_flag) | 
 | 827 | { | 
 | 828 | 	struct sbi_instrument ins; | 
 | 829 |  | 
 | 830 | 	if (count <sizeof(ins)) | 
 | 831 | 	{ | 
 | 832 | 		printk(KERN_WARNING "FM Error: Patch record too short\n"); | 
 | 833 | 		return -EINVAL; | 
 | 834 | 	} | 
 | 835 |  | 
 | 836 | 	/* | 
 | 837 | 	 * What the fuck is going on here?  We leave junk in the beginning | 
 | 838 | 	 * of ins and then check the field pretty close to that beginning? | 
 | 839 | 	 */ | 
 | 840 | 	if(copy_from_user(&((char *) &ins)[offs], addr + offs, sizeof(ins) - offs)) | 
 | 841 | 		return -EFAULT; | 
 | 842 |  | 
 | 843 | 	if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR) | 
 | 844 | 	{ | 
 | 845 | 		printk(KERN_WARNING "FM Error: Invalid instrument number %d\n", ins.channel); | 
 | 846 | 		return -EINVAL; | 
 | 847 | 	} | 
 | 848 | 	ins.key = format; | 
 | 849 |  | 
 | 850 | 	return store_instr(ins.channel, &ins); | 
 | 851 | } | 
 | 852 |  | 
 | 853 | static void opl3_panning(int dev, int voice, int value) | 
 | 854 | { | 
 | 855 | 	devc->voc[voice].panning = value; | 
 | 856 | } | 
 | 857 |  | 
 | 858 | static void opl3_volume_method(int dev, int mode) | 
 | 859 | { | 
 | 860 | } | 
 | 861 |  | 
 | 862 | #define SET_VIBRATO(cell) { \ | 
 | 863 | 	tmp = instr->operators[(cell-1)+(((cell-1)/2)*OFFS_4OP)]; \ | 
 | 864 | 	if (pressure > 110) \ | 
 | 865 | 		tmp |= 0x40;		/* Vibrato on */ \ | 
 | 866 | 	opl3_command (map->ioaddr, AM_VIB + map->op[cell-1], tmp);} | 
 | 867 |  | 
 | 868 | static void opl3_aftertouch(int dev, int voice, int pressure) | 
 | 869 | { | 
 | 870 | 	int tmp; | 
 | 871 | 	struct sbi_instrument *instr; | 
 | 872 | 	struct physical_voice_info *map; | 
 | 873 |  | 
 | 874 | 	if (voice < 0 || voice >= devc->nr_voice) | 
 | 875 | 		return; | 
 | 876 |  | 
 | 877 | 	map = &pv_map[devc->lv_map[voice]]; | 
 | 878 |  | 
 | 879 | 	DEB(printk("Aftertouch %d\n", voice)); | 
 | 880 |  | 
 | 881 | 	if (map->voice_mode == 0) | 
 | 882 | 		return; | 
 | 883 |  | 
 | 884 | 	/* | 
 | 885 | 	 * Adjust the amount of vibrato depending the pressure | 
 | 886 | 	 */ | 
 | 887 |  | 
 | 888 | 	instr = devc->act_i[voice]; | 
 | 889 |  | 
 | 890 | 	if (!instr) | 
 | 891 | 		instr = &devc->i_map[0]; | 
 | 892 |  | 
 | 893 | 	if (devc->voc[voice].mode == 4) | 
 | 894 | 	{ | 
 | 895 | 		int connection = ((instr->operators[10] & 0x01) << 1) | (instr->operators[10 + OFFS_4OP] & 0x01); | 
 | 896 |  | 
 | 897 | 		switch (connection) | 
 | 898 | 		{ | 
 | 899 | 			case 0: | 
 | 900 | 				SET_VIBRATO(4); | 
 | 901 | 				break; | 
 | 902 |  | 
 | 903 | 			case 1: | 
 | 904 | 				SET_VIBRATO(2); | 
 | 905 | 				SET_VIBRATO(4); | 
 | 906 | 				break; | 
 | 907 |  | 
 | 908 | 			case 2: | 
 | 909 | 				SET_VIBRATO(1); | 
 | 910 | 				SET_VIBRATO(4); | 
 | 911 | 				break; | 
 | 912 |  | 
 | 913 | 			case 3: | 
 | 914 | 				SET_VIBRATO(1); | 
 | 915 | 				SET_VIBRATO(3); | 
 | 916 | 				SET_VIBRATO(4); | 
 | 917 | 				break; | 
 | 918 |  | 
 | 919 | 		} | 
 | 920 | 		/* | 
 | 921 | 		 * Not implemented yet | 
 | 922 | 		 */ | 
 | 923 | 	} | 
 | 924 | 	else | 
 | 925 | 	{ | 
 | 926 | 		SET_VIBRATO(1); | 
 | 927 |  | 
 | 928 | 		if ((instr->operators[10] & 0x01))	/* | 
 | 929 | 							 * Additive synthesis | 
 | 930 | 							 */ | 
 | 931 | 			SET_VIBRATO(2); | 
 | 932 | 	} | 
 | 933 | } | 
 | 934 |  | 
 | 935 | #undef SET_VIBRATO | 
 | 936 |  | 
 | 937 | static void bend_pitch(int dev, int voice, int value) | 
 | 938 | { | 
 | 939 | 	unsigned char data; | 
 | 940 | 	int block, fnum, freq; | 
 | 941 | 	struct physical_voice_info *map; | 
 | 942 |  | 
 | 943 | 	map = &pv_map[devc->lv_map[voice]]; | 
 | 944 |  | 
 | 945 | 	if (map->voice_mode == 0) | 
 | 946 | 		return; | 
 | 947 |  | 
 | 948 | 	devc->voc[voice].bender = value; | 
 | 949 | 	if (!value) | 
 | 950 | 		return; | 
 | 951 | 	if (!(devc->voc[voice].keyon_byte & 0x20)) | 
 | 952 | 		return;	/* | 
 | 953 | 			 * Not keyed on | 
 | 954 | 			 */ | 
 | 955 |  | 
 | 956 | 	freq = compute_finetune(devc->voc[voice].orig_freq, devc->voc[voice].bender, devc->voc[voice].bender_range, 0); | 
 | 957 | 	devc->voc[voice].current_freq = freq; | 
 | 958 |  | 
 | 959 | 	freq_to_fnum(freq, &block, &fnum); | 
 | 960 |  | 
 | 961 | 	data = fnum & 0xff;	/* | 
 | 962 | 				 * Least significant bits of fnumber | 
 | 963 | 				 */ | 
 | 964 | 	opl3_command(map->ioaddr, FNUM_LOW + map->voice_num, data); | 
 | 965 |  | 
 | 966 | 	data = 0x20 | ((block & 0x7) << 2) | ((fnum >> 8) & 0x3); | 
 | 967 | 	devc->voc[voice].keyon_byte = data; | 
 | 968 | 	opl3_command(map->ioaddr, KEYON_BLOCK + map->voice_num, data); | 
 | 969 | } | 
 | 970 |  | 
 | 971 | static void opl3_controller (int dev, int voice, int ctrl_num, int value) | 
 | 972 | { | 
 | 973 | 	if (voice < 0 || voice >= devc->nr_voice) | 
 | 974 | 		return; | 
 | 975 |  | 
 | 976 | 	switch (ctrl_num) | 
 | 977 | 	{ | 
 | 978 | 		case CTRL_PITCH_BENDER: | 
 | 979 | 			bend_pitch(dev, voice, value); | 
 | 980 | 			break; | 
 | 981 |  | 
 | 982 | 		case CTRL_PITCH_BENDER_RANGE: | 
 | 983 | 			devc->voc[voice].bender_range = value; | 
 | 984 | 			break; | 
 | 985 |  | 
 | 986 | 		case CTL_MAIN_VOLUME: | 
 | 987 | 			devc->voc[voice].volume = value / 128; | 
 | 988 | 			break; | 
 | 989 |  | 
 | 990 | 		case CTL_PAN: | 
 | 991 | 			devc->voc[voice].panning = (value * 2) - 128; | 
 | 992 | 			break; | 
 | 993 | 	} | 
 | 994 | } | 
 | 995 |  | 
 | 996 | static void opl3_bender(int dev, int voice, int value) | 
 | 997 | { | 
 | 998 | 	if (voice < 0 || voice >= devc->nr_voice) | 
 | 999 | 		return; | 
 | 1000 |  | 
 | 1001 | 	bend_pitch(dev, voice, value - 8192); | 
 | 1002 | } | 
 | 1003 |  | 
 | 1004 | static int opl3_alloc_voice(int dev, int chn, int note, struct voice_alloc_info *alloc) | 
 | 1005 | { | 
 | 1006 | 	int i, p, best, first, avail, best_time = 0x7fffffff; | 
 | 1007 | 	struct sbi_instrument *instr; | 
 | 1008 | 	int is4op; | 
 | 1009 | 	int instr_no; | 
 | 1010 |  | 
 | 1011 | 	if (chn < 0 || chn > 15) | 
 | 1012 | 		instr_no = 0; | 
 | 1013 | 	else | 
 | 1014 | 		instr_no = devc->chn_info[chn].pgm_num; | 
 | 1015 |  | 
 | 1016 | 	instr = &devc->i_map[instr_no]; | 
 | 1017 | 	if (instr->channel < 0 ||	/* Instrument not loaded */ | 
 | 1018 | 		devc->nr_voice != 12)	/* Not in 4 OP mode */ | 
 | 1019 | 		is4op = 0; | 
 | 1020 | 	else if (devc->nr_voice == 12)	/* 4 OP mode */ | 
 | 1021 | 		is4op = (instr->key == OPL3_PATCH); | 
 | 1022 | 	else | 
 | 1023 | 		is4op = 0; | 
 | 1024 |  | 
 | 1025 | 	if (is4op) | 
 | 1026 | 	{ | 
 | 1027 | 		first = p = 0; | 
 | 1028 | 		avail = 6; | 
 | 1029 | 	} | 
 | 1030 | 	else | 
 | 1031 | 	{ | 
 | 1032 | 		if (devc->nr_voice == 12)	/* 4 OP mode. Use the '2 OP only' operators first */ | 
 | 1033 | 			first = p = 6; | 
 | 1034 | 		else | 
 | 1035 | 			first = p = 0; | 
 | 1036 | 		avail = devc->nr_voice; | 
 | 1037 | 	} | 
 | 1038 |  | 
 | 1039 | 	/* | 
 | 1040 | 	 *    Now try to find a free voice | 
 | 1041 | 	 */ | 
 | 1042 | 	best = first; | 
 | 1043 |  | 
 | 1044 | 	for (i = 0; i < avail; i++) | 
 | 1045 | 	{ | 
 | 1046 | 		if (alloc->map[p] == 0) | 
 | 1047 | 		{ | 
 | 1048 | 			return p; | 
 | 1049 | 		} | 
 | 1050 | 		if (alloc->alloc_times[p] < best_time)		/* Find oldest playing note */ | 
 | 1051 | 		{ | 
 | 1052 | 			best_time = alloc->alloc_times[p]; | 
 | 1053 | 			best = p; | 
 | 1054 | 		} | 
 | 1055 | 		p = (p + 1) % avail; | 
 | 1056 | 	} | 
 | 1057 |  | 
 | 1058 | 	/* | 
 | 1059 | 	 *    Insert some kind of priority mechanism here. | 
 | 1060 | 	 */ | 
 | 1061 |  | 
 | 1062 | 	if (best < 0) | 
 | 1063 | 		best = 0; | 
 | 1064 | 	if (best > devc->nr_voice) | 
 | 1065 | 		best -= devc->nr_voice; | 
 | 1066 |  | 
 | 1067 | 	return best;	/* All devc->voc in use. Select the first one. */ | 
 | 1068 | } | 
 | 1069 |  | 
 | 1070 | static void opl3_setup_voice(int dev, int voice, int chn) | 
 | 1071 | { | 
 | 1072 | 	struct channel_info *info = | 
 | 1073 | 	&synth_devs[dev]->chn_info[chn]; | 
 | 1074 |  | 
 | 1075 | 	opl3_set_instr(dev, voice, info->pgm_num); | 
 | 1076 |  | 
 | 1077 | 	devc->voc[voice].bender = 0; | 
 | 1078 | 	devc->voc[voice].bender_range = info->bender_range; | 
 | 1079 | 	devc->voc[voice].volume = info->controllers[CTL_MAIN_VOLUME]; | 
 | 1080 | 	devc->voc[voice].panning = (info->controllers[CTL_PAN] * 2) - 128; | 
 | 1081 | } | 
 | 1082 |  | 
 | 1083 | static struct synth_operations opl3_operations = | 
 | 1084 | { | 
 | 1085 | 	.owner		= THIS_MODULE, | 
 | 1086 | 	.id		= "OPL", | 
 | 1087 | 	.info		= NULL, | 
 | 1088 | 	.midi_dev	= 0, | 
 | 1089 | 	.synth_type	= SYNTH_TYPE_FM, | 
 | 1090 | 	.synth_subtype	= FM_TYPE_ADLIB, | 
 | 1091 | 	.open		= opl3_open, | 
 | 1092 | 	.close		= opl3_close, | 
 | 1093 | 	.ioctl		= opl3_ioctl, | 
 | 1094 | 	.kill_note	= opl3_kill_note, | 
 | 1095 | 	.start_note	= opl3_start_note, | 
 | 1096 | 	.set_instr	= opl3_set_instr, | 
 | 1097 | 	.reset		= opl3_reset, | 
 | 1098 | 	.hw_control	= opl3_hw_control, | 
 | 1099 | 	.load_patch	= opl3_load_patch, | 
 | 1100 | 	.aftertouch	= opl3_aftertouch, | 
 | 1101 | 	.controller	= opl3_controller, | 
 | 1102 | 	.panning	= opl3_panning, | 
 | 1103 | 	.volume_method	= opl3_volume_method, | 
 | 1104 | 	.bender		= opl3_bender, | 
 | 1105 | 	.alloc_voice	= opl3_alloc_voice, | 
 | 1106 | 	.setup_voice	= opl3_setup_voice | 
 | 1107 | }; | 
 | 1108 |  | 
 | 1109 | int opl3_init(int ioaddr, int *osp, struct module *owner) | 
 | 1110 | { | 
 | 1111 | 	int i; | 
 | 1112 | 	int me; | 
 | 1113 |  | 
 | 1114 | 	if (devc == NULL) | 
 | 1115 | 	{ | 
 | 1116 | 		printk(KERN_ERR "opl3: Device control structure not initialized.\n"); | 
 | 1117 | 		return -1; | 
 | 1118 | 	} | 
 | 1119 |  | 
 | 1120 | 	if ((me = sound_alloc_synthdev()) == -1) | 
 | 1121 | 	{ | 
 | 1122 | 		printk(KERN_WARNING "opl3: Too many synthesizers\n"); | 
 | 1123 | 		return -1; | 
 | 1124 | 	} | 
 | 1125 |  | 
 | 1126 | 	devc->nr_voice = 9; | 
 | 1127 |  | 
 | 1128 | 	devc->fm_info.device = 0; | 
 | 1129 | 	devc->fm_info.synth_type = SYNTH_TYPE_FM; | 
 | 1130 | 	devc->fm_info.synth_subtype = FM_TYPE_ADLIB; | 
 | 1131 | 	devc->fm_info.perc_mode = 0; | 
 | 1132 | 	devc->fm_info.nr_voices = 9; | 
 | 1133 | 	devc->fm_info.nr_drums = 0; | 
 | 1134 | 	devc->fm_info.instr_bank_size = SBFM_MAXINSTR; | 
 | 1135 | 	devc->fm_info.capabilities = 0; | 
 | 1136 | 	devc->left_io = ioaddr; | 
 | 1137 | 	devc->right_io = ioaddr + 2; | 
 | 1138 |  | 
 | 1139 | 	if (detected_model <= 2) | 
 | 1140 | 		devc->model = 1; | 
 | 1141 | 	else | 
 | 1142 | 	{ | 
 | 1143 | 		devc->model = 2; | 
 | 1144 | 		if (detected_model == 4) | 
 | 1145 | 			devc->is_opl4 = 1; | 
 | 1146 | 	} | 
 | 1147 |  | 
 | 1148 | 	opl3_operations.info = &devc->fm_info; | 
 | 1149 |  | 
 | 1150 | 	synth_devs[me] = &opl3_operations; | 
 | 1151 |  | 
 | 1152 | 	if (owner) | 
 | 1153 | 		synth_devs[me]->owner = owner; | 
 | 1154 | 	 | 
 | 1155 | 	sequencer_init(); | 
 | 1156 | 	devc->v_alloc = &opl3_operations.alloc; | 
 | 1157 | 	devc->chn_info = &opl3_operations.chn_info[0]; | 
 | 1158 |  | 
 | 1159 | 	if (devc->model == 2) | 
 | 1160 | 	{ | 
 | 1161 | 		if (devc->is_opl4)  | 
 | 1162 | 			strcpy(devc->fm_info.name, "Yamaha OPL4/OPL3 FM"); | 
 | 1163 | 		else  | 
 | 1164 | 			strcpy(devc->fm_info.name, "Yamaha OPL3"); | 
 | 1165 |  | 
 | 1166 | 		devc->v_alloc->max_voice = devc->nr_voice = 18; | 
 | 1167 | 		devc->fm_info.nr_drums = 0; | 
 | 1168 | 		devc->fm_info.synth_subtype = FM_TYPE_OPL3; | 
 | 1169 | 		devc->fm_info.capabilities |= SYNTH_CAP_OPL3; | 
 | 1170 |  | 
 | 1171 | 		for (i = 0; i < 18; i++) | 
 | 1172 | 		{ | 
 | 1173 | 			if (pv_map[i].ioaddr == USE_LEFT) | 
 | 1174 | 				pv_map[i].ioaddr = devc->left_io; | 
 | 1175 | 			else | 
 | 1176 | 				pv_map[i].ioaddr = devc->right_io; | 
 | 1177 | 		} | 
 | 1178 | 		opl3_command(devc->right_io, OPL3_MODE_REGISTER, OPL3_ENABLE); | 
 | 1179 | 		opl3_command(devc->right_io, CONNECTION_SELECT_REGISTER, 0x00); | 
 | 1180 | 	} | 
 | 1181 | 	else | 
 | 1182 | 	{ | 
 | 1183 | 		strcpy(devc->fm_info.name, "Yamaha OPL2"); | 
 | 1184 | 		devc->v_alloc->max_voice = devc->nr_voice = 9; | 
 | 1185 | 		devc->fm_info.nr_drums = 0; | 
 | 1186 |  | 
 | 1187 | 		for (i = 0; i < 18; i++) | 
 | 1188 | 			pv_map[i].ioaddr = devc->left_io; | 
 | 1189 | 	}; | 
 | 1190 | 	conf_printf2(devc->fm_info.name, ioaddr, 0, -1, -1); | 
 | 1191 |  | 
 | 1192 | 	for (i = 0; i < SBFM_MAXINSTR; i++) | 
 | 1193 | 		devc->i_map[i].channel = -1; | 
 | 1194 |  | 
 | 1195 | 	return me; | 
 | 1196 | } | 
 | 1197 |  | 
 | 1198 | EXPORT_SYMBOL(opl3_init); | 
 | 1199 | EXPORT_SYMBOL(opl3_detect); | 
 | 1200 |  | 
 | 1201 | static int me; | 
 | 1202 |  | 
 | 1203 | static int io = -1; | 
 | 1204 |  | 
 | 1205 | module_param(io, int, 0); | 
 | 1206 |  | 
 | 1207 | static int __init init_opl3 (void) | 
 | 1208 | { | 
 | 1209 | 	printk(KERN_INFO "YM3812 and OPL-3 driver Copyright (C) by Hannu Savolainen, Rob Hooft 1993-1996\n"); | 
 | 1210 |  | 
 | 1211 | 	if (io != -1)	/* User loading pure OPL3 module */ | 
 | 1212 | 	{ | 
 | 1213 | 		if (!opl3_detect(io, NULL)) | 
 | 1214 | 		{ | 
 | 1215 | 			return -ENODEV; | 
 | 1216 | 		} | 
 | 1217 |  | 
 | 1218 | 		me = opl3_init(io, NULL, THIS_MODULE); | 
 | 1219 | 	} | 
 | 1220 |  | 
 | 1221 | 	return 0; | 
 | 1222 | } | 
 | 1223 |  | 
 | 1224 | static void __exit cleanup_opl3(void) | 
 | 1225 | { | 
 | 1226 | 	if (devc && io != -1) | 
 | 1227 | 	{ | 
 | 1228 | 		if (devc->base) { | 
 | 1229 | 			release_region(devc->base,4); | 
 | 1230 | 			if (devc->is_opl4) | 
 | 1231 | 				release_region(devc->base - 8, 2); | 
 | 1232 | 		} | 
 | 1233 | 		kfree(devc); | 
 | 1234 | 		devc = NULL; | 
 | 1235 | 		sound_unload_synthdev(me); | 
 | 1236 | 	} | 
 | 1237 | } | 
 | 1238 |  | 
 | 1239 | module_init(init_opl3); | 
 | 1240 | module_exit(cleanup_opl3); | 
 | 1241 |  | 
 | 1242 | #ifndef MODULE | 
 | 1243 | static int __init setup_opl3(char *str) | 
 | 1244 | { | 
 | 1245 |         /* io  */ | 
 | 1246 | 	int ints[2]; | 
 | 1247 | 	 | 
 | 1248 | 	str = get_options(str, ARRAY_SIZE(ints), ints); | 
 | 1249 | 	 | 
 | 1250 | 	io = ints[1]; | 
 | 1251 |  | 
 | 1252 | 	return 1; | 
 | 1253 | } | 
 | 1254 |  | 
 | 1255 | __setup("opl3=", setup_opl3); | 
 | 1256 | #endif | 
 | 1257 | MODULE_LICENSE("GPL"); |