blob: 6fd60b7e5805532625e77b653553b21cc743c1da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
30MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
31MODULE_LICENSE("GPL");
32MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
33
34int use_internal_drums = 0;
35module_param(use_internal_drums, bool, 0444);
36MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
37
Takashi Iwai5b1646a2005-11-17 14:13:14 +010038int snd_opl3_synth_use_inc(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 if (!try_module_get(opl3->card->module))
41 return -EFAULT;
42 return 0;
43
44}
45
Takashi Iwai5b1646a2005-11-17 14:13:14 +010046void snd_opl3_synth_use_dec(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 module_put(opl3->card->module);
49}
50
Takashi Iwai5b1646a2005-11-17 14:13:14 +010051int snd_opl3_synth_setup(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 int idx;
Takashi Iwai05c1afe2007-10-30 11:59:15 +010054 struct snd_hwdep *hwdep = opl3->hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Takashi Iwai05c1afe2007-10-30 11:59:15 +010056 mutex_lock(&hwdep->open_mutex);
57 if (hwdep->used) {
58 mutex_unlock(&hwdep->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -EBUSY;
60 }
Takashi Iwai05c1afe2007-10-30 11:59:15 +010061 hwdep->used++;
62 mutex_unlock(&hwdep->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 snd_opl3_reset(opl3);
65
66 for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
67 opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
68 opl3->voices[idx].time = 0;
69 opl3->voices[idx].keyon_reg = 0x00;
70 }
71 opl3->use_time = 0;
72 opl3->connection_reg = 0x00;
73 if (opl3->hardware >= OPL3_HW_OPL3) {
74 /* Clear 4-op connections */
75 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
76 opl3->connection_reg);
77 opl3->max_voices = MAX_OPL3_VOICES;
78 }
79 return 0;
80}
81
Takashi Iwai5b1646a2005-11-17 14:13:14 +010082void snd_opl3_synth_cleanup(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 unsigned long flags;
85
86 /* Stop system timer */
87 spin_lock_irqsave(&opl3->sys_timer_lock, flags);
88 if (opl3->sys_timer_status) {
89 del_timer(&opl3->tlist);
90 opl3->sys_timer_status = 0;
91 }
92 spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
93
94 snd_opl3_reset(opl3);
Takashi Iwai05c1afe2007-10-30 11:59:15 +010095 hwdep = opl3->hwdep;
96 mutex_lock(&hwdep->open_mutex);
97 hwdep->used--;
98 mutex_unlock(&hwdep->open_mutex);
99 wake_up(&hwdep->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100102static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100104 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int err;
106
107 if ((err = snd_opl3_synth_setup(opl3)) < 0)
108 return err;
109
110 if (use_internal_drums) {
111 /* Percussion mode */
112 opl3->voices[6].state = opl3->voices[7].state =
113 opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
114 snd_opl3_load_drums(opl3);
115 opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
116 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
117 } else {
118 opl3->drum_reg = 0x00;
119 }
120
121 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
122 if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
123 return err;
124 }
125 opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
126 return 0;
127}
128
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100129static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100131 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 snd_opl3_synth_cleanup(opl3);
134
135 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
136 snd_opl3_synth_use_dec(opl3);
137 return 0;
138}
139
140/*
141 * MIDI emulation operators
142 */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100143struct snd_midi_op opl3_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .note_on = snd_opl3_note_on,
145 .note_off = snd_opl3_note_off,
146 .key_press = snd_opl3_key_press,
147 .note_terminate = snd_opl3_terminate_note,
148 .control = snd_opl3_control,
149 .nrpn = snd_opl3_nrpn,
150 .sysex = snd_opl3_sysex,
151};
152
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100153static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 void *private_data, int atomic, int hop)
155{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100156 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Takashi Iwai224a0332007-10-30 11:49:22 +0100158 snd_midi_process_event(&opl3_ops, ev, opl3->chset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
160}
161
162/* ------------------------------ */
163
164static void snd_opl3_synth_free_port(void *private_data)
165{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100166 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 snd_midi_channel_free_set(opl3->chset);
169}
170
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100171static int snd_opl3_synth_create_port(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100173 struct snd_seq_port_callback callbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 char name[32];
175 int voices, opl_ver;
176
177 voices = (opl3->hardware < OPL3_HW_OPL3) ?
178 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
179 opl3->chset = snd_midi_channel_alloc_set(16);
180 if (opl3->chset == NULL)
181 return -ENOMEM;
182 opl3->chset->private_data = opl3;
183
184 memset(&callbacks, 0, sizeof(callbacks));
185 callbacks.owner = THIS_MODULE;
186 callbacks.use = snd_opl3_synth_use;
187 callbacks.unuse = snd_opl3_synth_unuse;
188 callbacks.event_input = snd_opl3_synth_event_input;
189 callbacks.private_free = snd_opl3_synth_free_port;
190 callbacks.private_data = opl3;
191
192 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
193 sprintf(name, "OPL%i FM Port", opl_ver);
194
195 opl3->chset->client = opl3->seq_client;
196 opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
197 SNDRV_SEQ_PORT_CAP_WRITE |
198 SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
199 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
200 SNDRV_SEQ_PORT_TYPE_MIDI_GM |
Clemens Ladisch450047a2006-05-02 16:08:41 +0200201 SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
202 SNDRV_SEQ_PORT_TYPE_HARDWARE |
203 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 16, voices,
205 name);
206 if (opl3->chset->port < 0) {
Dave Jones5e315e92006-03-06 14:03:37 +0100207 int port;
208 port = opl3->chset->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 snd_midi_channel_free_set(opl3->chset);
Dave Jones5e315e92006-03-06 14:03:37 +0100210 return port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212 return 0;
213}
214
215/* ------------------------------ */
216
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100217static int snd_opl3_seq_new_device(struct snd_seq_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100219 struct snd_opl3 *opl3;
Dave Jones5e315e92006-03-06 14:03:37 +0100220 int client, err;
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100221 char name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int opl_ver;
223
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100224 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 if (opl3 == NULL)
226 return -EINVAL;
227
228 spin_lock_init(&opl3->voice_lock);
229
230 opl3->seq_client = -1;
231
232 /* allocate new client */
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100233 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
234 sprintf(name, "OPL%i FM synth", opl_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 client = opl3->seq_client =
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100236 snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num,
237 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (client < 0)
239 return client;
240
Dave Jones5e315e92006-03-06 14:03:37 +0100241 if ((err = snd_opl3_synth_create_port(opl3)) < 0) {
242 snd_seq_delete_kernel_client(client);
243 opl3->seq_client = -1;
244 return err;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* setup system timer */
248 init_timer(&opl3->tlist);
249 opl3->tlist.function = snd_opl3_timer_func;
250 opl3->tlist.data = (unsigned long) opl3;
251 spin_lock_init(&opl3->sys_timer_lock);
252 opl3->sys_timer_status = 0;
253
254#ifdef CONFIG_SND_SEQUENCER_OSS
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100255 snd_opl3_init_seq_oss(opl3, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#endif
257 return 0;
258}
259
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100260static int snd_opl3_seq_delete_device(struct snd_seq_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100262 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100264 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (opl3 == NULL)
266 return -EINVAL;
267
268#ifdef CONFIG_SND_SEQUENCER_OSS
269 snd_opl3_free_seq_oss(opl3);
270#endif
271 if (opl3->seq_client >= 0) {
272 snd_seq_delete_kernel_client(opl3->seq_client);
273 opl3->seq_client = -1;
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 0;
276}
277
278static int __init alsa_opl3_seq_init(void)
279{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100280 static struct snd_seq_dev_ops ops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 {
282 snd_opl3_seq_new_device,
283 snd_opl3_seq_delete_device
284 };
285
286 return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL3, &ops,
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100287 sizeof(struct snd_opl3 *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static void __exit alsa_opl3_seq_exit(void)
291{
292 snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL3);
293}
294
295module_init(alsa_opl3_seq_init)
296module_exit(alsa_opl3_seq_exit)