1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
5 * Midi Sequencer interface routines for OPL2/OPL3/OPL4 FM
7 * OPL2/3 FM instrument loader:
8 * alsa-tools/seq/sbiload/
11 #include "opl3_voice.h"
12 #include <linux/init.h>
13 #include <linux/moduleparam.h>
14 #include <linux/module.h>
15 #include <sound/initval.h>
17 MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
18 MODULE_LICENSE("GPL");
19 MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
21 bool use_internal_drums = 0;
22 module_param(use_internal_drums, bool, 0444);
23 MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
25 int snd_opl3_synth_use_inc(struct snd_opl3 * opl3)
27 if (!try_module_get(opl3->card->module))
33 void snd_opl3_synth_use_dec(struct snd_opl3 * opl3)
35 module_put(opl3->card->module);
38 int snd_opl3_synth_setup(struct snd_opl3 * opl3)
41 struct snd_hwdep *hwdep = opl3->hwdep;
43 mutex_lock(&hwdep->open_mutex);
45 mutex_unlock(&hwdep->open_mutex);
49 mutex_unlock(&hwdep->open_mutex);
53 for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
54 opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
55 opl3->voices[idx].time = 0;
56 opl3->voices[idx].keyon_reg = 0x00;
59 opl3->connection_reg = 0x00;
60 if (opl3->hardware >= OPL3_HW_OPL3) {
61 /* Clear 4-op connections */
62 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
63 opl3->connection_reg);
64 opl3->max_voices = MAX_OPL3_VOICES;
69 void snd_opl3_synth_cleanup(struct snd_opl3 * opl3)
72 struct snd_hwdep *hwdep;
74 /* Stop system timer */
75 spin_lock_irqsave(&opl3->sys_timer_lock, flags);
76 if (opl3->sys_timer_status) {
77 del_timer(&opl3->tlist);
78 opl3->sys_timer_status = 0;
80 spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
84 mutex_lock(&hwdep->open_mutex);
86 mutex_unlock(&hwdep->open_mutex);
87 wake_up(&hwdep->open_wait);
90 static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
92 struct snd_opl3 *opl3 = private_data;
95 err = snd_opl3_synth_setup(opl3);
99 if (use_internal_drums) {
100 /* Percussion mode */
101 opl3->voices[6].state = opl3->voices[7].state =
102 opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
103 snd_opl3_load_drums(opl3);
104 opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
105 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
107 opl3->drum_reg = 0x00;
110 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
111 err = snd_opl3_synth_use_inc(opl3);
115 opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
119 static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
121 struct snd_opl3 *opl3 = private_data;
123 snd_opl3_synth_cleanup(opl3);
125 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
126 snd_opl3_synth_use_dec(opl3);
131 * MIDI emulation operators
133 const struct snd_midi_op opl3_ops = {
134 .note_on = snd_opl3_note_on,
135 .note_off = snd_opl3_note_off,
136 .key_press = snd_opl3_key_press,
137 .note_terminate = snd_opl3_terminate_note,
138 .control = snd_opl3_control,
139 .nrpn = snd_opl3_nrpn,
140 .sysex = snd_opl3_sysex,
143 static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct,
144 void *private_data, int atomic, int hop)
146 struct snd_opl3 *opl3 = private_data;
148 snd_midi_process_event(&opl3_ops, ev, opl3->chset);
152 /* ------------------------------ */
154 static void snd_opl3_synth_free_port(void *private_data)
156 struct snd_opl3 *opl3 = private_data;
158 snd_midi_channel_free_set(opl3->chset);
161 static int snd_opl3_synth_create_port(struct snd_opl3 * opl3)
163 struct snd_seq_port_callback callbacks;
167 voices = (opl3->hardware < OPL3_HW_OPL3) ?
168 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
169 opl3->chset = snd_midi_channel_alloc_set(16);
170 if (opl3->chset == NULL)
172 opl3->chset->private_data = opl3;
174 memset(&callbacks, 0, sizeof(callbacks));
175 callbacks.owner = THIS_MODULE;
176 callbacks.use = snd_opl3_synth_use;
177 callbacks.unuse = snd_opl3_synth_unuse;
178 callbacks.event_input = snd_opl3_synth_event_input;
179 callbacks.private_free = snd_opl3_synth_free_port;
180 callbacks.private_data = opl3;
182 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
183 sprintf(name, "OPL%i FM Port", opl_ver);
185 opl3->chset->client = opl3->seq_client;
186 opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
187 SNDRV_SEQ_PORT_CAP_WRITE |
188 SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
189 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
190 SNDRV_SEQ_PORT_TYPE_MIDI_GM |
191 SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
192 SNDRV_SEQ_PORT_TYPE_HARDWARE |
193 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
196 if (opl3->chset->port < 0) {
198 port = opl3->chset->port;
199 snd_midi_channel_free_set(opl3->chset);
205 /* ------------------------------ */
207 static int snd_opl3_seq_probe(struct device *_dev)
209 struct snd_seq_device *dev = to_seq_dev(_dev);
210 struct snd_opl3 *opl3;
215 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
219 spin_lock_init(&opl3->voice_lock);
221 opl3->seq_client = -1;
223 /* allocate new client */
224 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
225 sprintf(name, "OPL%i FM synth", opl_ver);
226 client = opl3->seq_client =
227 snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num,
232 err = snd_opl3_synth_create_port(opl3);
234 snd_seq_delete_kernel_client(client);
235 opl3->seq_client = -1;
239 /* setup system timer */
240 timer_setup(&opl3->tlist, snd_opl3_timer_func, 0);
241 spin_lock_init(&opl3->sys_timer_lock);
242 opl3->sys_timer_status = 0;
244 #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
245 snd_opl3_init_seq_oss(opl3, name);
250 static int snd_opl3_seq_remove(struct device *_dev)
252 struct snd_seq_device *dev = to_seq_dev(_dev);
253 struct snd_opl3 *opl3;
255 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
259 #if IS_ENABLED(CONFIG_SND_SEQUENCER_OSS)
260 snd_opl3_free_seq_oss(opl3);
262 if (opl3->seq_client >= 0) {
263 snd_seq_delete_kernel_client(opl3->seq_client);
264 opl3->seq_client = -1;
269 static struct snd_seq_driver opl3_seq_driver = {
271 .name = KBUILD_MODNAME,
272 .probe = snd_opl3_seq_probe,
273 .remove = snd_opl3_seq_remove,
275 .id = SNDRV_SEQ_DEV_ID_OPL3,
276 .argsize = sizeof(struct snd_opl3 *),
279 module_snd_seq_driver(opl3_seq_driver);