| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  Copyright (c) by Uros Bizjak <uros@kss-loka.si> | 
 | 3 |  * | 
 | 4 |  *  Midi Sequencer interface routines for OPL2/OPL3/OPL4 FM | 
 | 5 |  * | 
 | 6 |  *  OPL2/3 FM instrument loader: | 
 | 7 |  *   alsa-tools/seq/sbiload/ | 
 | 8 |  * | 
 | 9 |  *   This program is free software; you can redistribute it and/or modify | 
 | 10 |  *   it under the terms of the GNU General Public License as published by | 
 | 11 |  *   the Free Software Foundation; either version 2 of the License, or | 
 | 12 |  *   (at your option) any later version. | 
 | 13 |  * | 
 | 14 |  *   This program is distributed in the hope that it will be useful, | 
 | 15 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 16 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 17 |  *   GNU General Public License for more details. | 
 | 18 |  * | 
 | 19 |  *   You should have received a copy of the GNU General Public License | 
 | 20 |  *   along with this program; if not, write to the Free Software | 
 | 21 |  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA | 
 | 22 |  * | 
 | 23 |  */ | 
 | 24 |  | 
 | 25 | #include "opl3_voice.h" | 
 | 26 | #include <linux/init.h> | 
 | 27 | #include <linux/moduleparam.h> | 
 | 28 | #include <sound/initval.h> | 
 | 29 |  | 
 | 30 | MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>"); | 
 | 31 | MODULE_LICENSE("GPL"); | 
 | 32 | MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth"); | 
 | 33 |  | 
 | 34 | int use_internal_drums = 0; | 
 | 35 | module_param(use_internal_drums, bool, 0444); | 
 | 36 | MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums."); | 
 | 37 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 38 | int snd_opl3_synth_use_inc(struct snd_opl3 * opl3) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { | 
 | 40 | 	if (!try_module_get(opl3->card->module)) | 
 | 41 | 		return -EFAULT; | 
 | 42 | 	return 0; | 
 | 43 |  | 
 | 44 | } | 
 | 45 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 46 | void snd_opl3_synth_use_dec(struct snd_opl3 * opl3) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { | 
 | 48 | 	module_put(opl3->card->module); | 
 | 49 | } | 
 | 50 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 51 | int snd_opl3_synth_setup(struct snd_opl3 * opl3) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { | 
 | 53 | 	int idx; | 
 | 54 |  | 
 | 55 | 	down(&opl3->access_mutex); | 
 | 56 | 	if (opl3->used) { | 
 | 57 | 		up(&opl3->access_mutex); | 
 | 58 | 		return -EBUSY; | 
 | 59 | 	} | 
 | 60 | 	opl3->used++; | 
 | 61 | 	up(&opl3->access_mutex); | 
 | 62 |  | 
 | 63 | 	snd_opl3_reset(opl3); | 
 | 64 |  | 
 | 65 | 	for (idx = 0; idx < MAX_OPL3_VOICES; idx++) { | 
 | 66 | 		opl3->voices[idx].state = SNDRV_OPL3_ST_OFF; | 
 | 67 | 		opl3->voices[idx].time = 0; | 
 | 68 | 		opl3->voices[idx].keyon_reg = 0x00; | 
 | 69 | 	} | 
 | 70 | 	opl3->use_time = 0; | 
 | 71 | 	opl3->connection_reg = 0x00; | 
 | 72 | 	if (opl3->hardware >= OPL3_HW_OPL3) { | 
 | 73 | 		/* Clear 4-op connections */ | 
 | 74 | 		opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, | 
 | 75 | 				 opl3->connection_reg); | 
 | 76 | 		opl3->max_voices = MAX_OPL3_VOICES; | 
 | 77 | 	} | 
 | 78 | 	return 0; | 
 | 79 | } | 
 | 80 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 81 | void snd_opl3_synth_cleanup(struct snd_opl3 * opl3) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { | 
 | 83 | 	unsigned long flags; | 
 | 84 |  | 
 | 85 | 	/* Stop system timer */ | 
 | 86 | 	spin_lock_irqsave(&opl3->sys_timer_lock, flags); | 
 | 87 | 	if (opl3->sys_timer_status) { | 
 | 88 | 		del_timer(&opl3->tlist); | 
 | 89 | 		opl3->sys_timer_status = 0; | 
 | 90 | 	} | 
 | 91 | 	spin_unlock_irqrestore(&opl3->sys_timer_lock, flags); | 
 | 92 |  | 
 | 93 | 	snd_opl3_reset(opl3); | 
 | 94 | 	down(&opl3->access_mutex); | 
 | 95 | 	opl3->used--; | 
 | 96 | 	up(&opl3->access_mutex); | 
 | 97 | } | 
 | 98 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 99 | static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 101 | 	struct snd_opl3 *opl3 = private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | 	int err; | 
 | 103 |  | 
 | 104 | 	if ((err = snd_opl3_synth_setup(opl3)) < 0) | 
 | 105 | 		return err; | 
 | 106 |  | 
 | 107 | 	if (use_internal_drums) { | 
 | 108 | 		/* Percussion mode */ | 
 | 109 | 		opl3->voices[6].state = opl3->voices[7].state =  | 
 | 110 | 			opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL; | 
 | 111 | 		snd_opl3_load_drums(opl3); | 
 | 112 | 		opl3->drum_reg = OPL3_PERCUSSION_ENABLE; | 
 | 113 | 		opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg); | 
 | 114 | 	} else { | 
 | 115 | 		opl3->drum_reg = 0x00; | 
 | 116 | 	} | 
 | 117 |  | 
 | 118 | 	if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) { | 
 | 119 | 		if ((err = snd_opl3_synth_use_inc(opl3)) < 0) | 
 | 120 | 			return err; | 
 | 121 | 	} | 
 | 122 | 	opl3->synth_mode = SNDRV_OPL3_MODE_SEQ; | 
 | 123 | 	return 0; | 
 | 124 | } | 
 | 125 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 126 | static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 128 | 	struct snd_opl3 *opl3 = private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 |  | 
 | 130 | 	snd_opl3_synth_cleanup(opl3); | 
 | 131 |  | 
 | 132 | 	if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) | 
 | 133 | 		snd_opl3_synth_use_dec(opl3); | 
 | 134 | 	return 0; | 
 | 135 | } | 
 | 136 |  | 
 | 137 | /* | 
 | 138 |  * MIDI emulation operators | 
 | 139 |  */ | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 140 | struct snd_midi_op opl3_ops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | 	.note_on =		snd_opl3_note_on, | 
 | 142 | 	.note_off =		snd_opl3_note_off, | 
 | 143 | 	.key_press =		snd_opl3_key_press, | 
 | 144 | 	.note_terminate =	snd_opl3_terminate_note, | 
 | 145 | 	.control =		snd_opl3_control, | 
 | 146 | 	.nrpn =			snd_opl3_nrpn, | 
 | 147 | 	.sysex =		snd_opl3_sysex, | 
 | 148 | }; | 
 | 149 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 150 | static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | 				      void *private_data, int atomic, int hop) | 
 | 152 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 153 | 	struct snd_opl3 *opl3 = private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 |  | 
 | 155 | 	if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN && | 
 | 156 | 	    ev->type <= SNDRV_SEQ_EVENT_INSTR_CHANGE) { | 
 | 157 | 		if (direct) { | 
 | 158 | 			snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, ev, | 
 | 159 | 					    opl3->seq_client, atomic, hop); | 
 | 160 | 		} | 
 | 161 | 	} else { | 
 | 162 | 		snd_midi_process_event(&opl3_ops, ev, opl3->chset); | 
 | 163 | 	} | 
 | 164 | 	return 0; | 
 | 165 | } | 
 | 166 |  | 
 | 167 | /* ------------------------------ */ | 
 | 168 |  | 
 | 169 | static void snd_opl3_synth_free_port(void *private_data) | 
 | 170 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 171 | 	struct snd_opl3 *opl3 = private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 |  | 
 | 173 | 	snd_midi_channel_free_set(opl3->chset); | 
 | 174 | } | 
 | 175 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 176 | static int snd_opl3_synth_create_port(struct snd_opl3 * opl3) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 178 | 	struct snd_seq_port_callback callbacks; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | 	char name[32]; | 
 | 180 | 	int voices, opl_ver; | 
 | 181 |  | 
 | 182 | 	voices = (opl3->hardware < OPL3_HW_OPL3) ? | 
 | 183 | 		MAX_OPL2_VOICES : MAX_OPL3_VOICES; | 
 | 184 | 	opl3->chset = snd_midi_channel_alloc_set(16); | 
 | 185 | 	if (opl3->chset == NULL) | 
 | 186 | 		return -ENOMEM; | 
 | 187 | 	opl3->chset->private_data = opl3; | 
 | 188 |  | 
 | 189 | 	memset(&callbacks, 0, sizeof(callbacks)); | 
 | 190 | 	callbacks.owner = THIS_MODULE; | 
 | 191 | 	callbacks.use = snd_opl3_synth_use; | 
 | 192 | 	callbacks.unuse = snd_opl3_synth_unuse; | 
 | 193 | 	callbacks.event_input = snd_opl3_synth_event_input; | 
 | 194 | 	callbacks.private_free = snd_opl3_synth_free_port; | 
 | 195 | 	callbacks.private_data = opl3; | 
 | 196 |  | 
 | 197 | 	opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8; | 
 | 198 | 	sprintf(name, "OPL%i FM Port", opl_ver); | 
 | 199 |  | 
 | 200 | 	opl3->chset->client = opl3->seq_client; | 
 | 201 | 	opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks, | 
 | 202 | 						      SNDRV_SEQ_PORT_CAP_WRITE | | 
 | 203 | 						      SNDRV_SEQ_PORT_CAP_SUBS_WRITE, | 
 | 204 | 						      SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | | 
 | 205 | 						      SNDRV_SEQ_PORT_TYPE_MIDI_GM | | 
 | 206 | 						      SNDRV_SEQ_PORT_TYPE_SYNTH, | 
 | 207 | 						      16, voices, | 
 | 208 | 						      name); | 
 | 209 | 	if (opl3->chset->port < 0) { | 
 | 210 | 		snd_midi_channel_free_set(opl3->chset); | 
 | 211 | 		return opl3->chset->port; | 
 | 212 | 	} | 
 | 213 | 	return 0; | 
 | 214 | } | 
 | 215 |  | 
 | 216 | /* ------------------------------ */ | 
 | 217 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 218 | static int snd_opl3_seq_new_device(struct snd_seq_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 220 | 	struct snd_opl3 *opl3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | 	int client; | 
| Clemens Ladisch | 7b6d924 | 2005-12-12 09:33:37 +0100 | [diff] [blame] | 222 | 	char name[32]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | 	int opl_ver; | 
 | 224 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 225 | 	opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | 	if (opl3 == NULL) | 
 | 227 | 		return -EINVAL; | 
 | 228 |  | 
 | 229 | 	spin_lock_init(&opl3->voice_lock); | 
 | 230 |  | 
 | 231 | 	opl3->seq_client = -1; | 
 | 232 |  | 
 | 233 | 	/* allocate new client */ | 
| Clemens Ladisch | 7b6d924 | 2005-12-12 09:33:37 +0100 | [diff] [blame] | 234 | 	opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8; | 
 | 235 | 	sprintf(name, "OPL%i FM synth", opl_ver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | 	client = opl3->seq_client = | 
| Clemens Ladisch | 7b6d924 | 2005-12-12 09:33:37 +0100 | [diff] [blame] | 237 | 		snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num, | 
 | 238 | 					     name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | 	if (client < 0) | 
 | 240 | 		return client; | 
 | 241 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | 	snd_opl3_synth_create_port(opl3); | 
 | 243 |  | 
 | 244 | 	/* initialize instrument list */ | 
 | 245 | 	opl3->ilist = snd_seq_instr_list_new(); | 
 | 246 | 	if (opl3->ilist == NULL) { | 
 | 247 | 		snd_seq_delete_kernel_client(client); | 
 | 248 | 		opl3->seq_client = -1; | 
 | 249 | 		return -ENOMEM; | 
 | 250 | 	} | 
 | 251 | 	opl3->ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT; | 
 | 252 | 	snd_seq_fm_init(&opl3->fm_ops, NULL); | 
 | 253 |  | 
 | 254 | 	/* setup system timer */ | 
 | 255 | 	init_timer(&opl3->tlist); | 
 | 256 | 	opl3->tlist.function = snd_opl3_timer_func; | 
 | 257 | 	opl3->tlist.data = (unsigned long) opl3; | 
 | 258 | 	spin_lock_init(&opl3->sys_timer_lock); | 
 | 259 | 	opl3->sys_timer_status = 0; | 
 | 260 |  | 
 | 261 | #ifdef CONFIG_SND_SEQUENCER_OSS | 
| Clemens Ladisch | 7b6d924 | 2005-12-12 09:33:37 +0100 | [diff] [blame] | 262 | 	snd_opl3_init_seq_oss(opl3, name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | #endif | 
 | 264 | 	return 0; | 
 | 265 | } | 
 | 266 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 267 | static int snd_opl3_seq_delete_device(struct snd_seq_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 269 | 	struct snd_opl3 *opl3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 |  | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 271 | 	opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | 	if (opl3 == NULL) | 
 | 273 | 		return -EINVAL; | 
 | 274 |  | 
 | 275 | #ifdef CONFIG_SND_SEQUENCER_OSS | 
 | 276 | 	snd_opl3_free_seq_oss(opl3); | 
 | 277 | #endif | 
 | 278 | 	if (opl3->seq_client >= 0) { | 
 | 279 | 		snd_seq_delete_kernel_client(opl3->seq_client); | 
 | 280 | 		opl3->seq_client = -1; | 
 | 281 | 	} | 
 | 282 | 	if (opl3->ilist) | 
 | 283 | 		snd_seq_instr_list_free(&opl3->ilist); | 
 | 284 | 	return 0; | 
 | 285 | } | 
 | 286 |  | 
 | 287 | static int __init alsa_opl3_seq_init(void) | 
 | 288 | { | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 289 | 	static struct snd_seq_dev_ops ops = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 	{ | 
 | 291 | 		snd_opl3_seq_new_device, | 
 | 292 | 		snd_opl3_seq_delete_device | 
 | 293 | 	}; | 
 | 294 |  | 
 | 295 | 	return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL3, &ops, | 
| Takashi Iwai | 5b1646a | 2005-11-17 14:13:14 +0100 | [diff] [blame] | 296 | 					      sizeof(struct snd_opl3 *)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } | 
 | 298 |  | 
 | 299 | static void __exit alsa_opl3_seq_exit(void) | 
 | 300 | { | 
 | 301 | 	snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL3); | 
 | 302 | } | 
 | 303 |  | 
 | 304 | module_init(alsa_opl3_seq_init) | 
 | 305 | module_exit(alsa_opl3_seq_exit) |