blob: fdae5d7f421ff76c119606ca04e909f58f76be0d [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040028#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <sound/initval.h>
30
31MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
32MODULE_LICENSE("GPL");
33MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
34
Rusty Russella67ff6a2011-12-15 13:49:36 +103035bool use_internal_drums = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036module_param(use_internal_drums, bool, 0444);
37MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
38
Takashi Iwai5b1646a2005-11-17 14:13:14 +010039int snd_opl3_synth_use_inc(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
41 if (!try_module_get(opl3->card->module))
42 return -EFAULT;
43 return 0;
44
45}
46
Takashi Iwai5b1646a2005-11-17 14:13:14 +010047void snd_opl3_synth_use_dec(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 module_put(opl3->card->module);
50}
51
Takashi Iwai5b1646a2005-11-17 14:13:14 +010052int snd_opl3_synth_setup(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 int idx;
Takashi Iwai05c1afe2007-10-30 11:59:15 +010055 struct snd_hwdep *hwdep = opl3->hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Takashi Iwai05c1afe2007-10-30 11:59:15 +010057 mutex_lock(&hwdep->open_mutex);
58 if (hwdep->used) {
59 mutex_unlock(&hwdep->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return -EBUSY;
61 }
Takashi Iwai05c1afe2007-10-30 11:59:15 +010062 hwdep->used++;
63 mutex_unlock(&hwdep->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 snd_opl3_reset(opl3);
66
67 for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
68 opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
69 opl3->voices[idx].time = 0;
70 opl3->voices[idx].keyon_reg = 0x00;
71 }
72 opl3->use_time = 0;
73 opl3->connection_reg = 0x00;
74 if (opl3->hardware >= OPL3_HW_OPL3) {
75 /* Clear 4-op connections */
76 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
77 opl3->connection_reg);
78 opl3->max_voices = MAX_OPL3_VOICES;
79 }
80 return 0;
81}
82
Takashi Iwai5b1646a2005-11-17 14:13:14 +010083void snd_opl3_synth_cleanup(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 unsigned long flags;
Takashi Iwai04576522007-10-30 12:43:40 +010086 struct snd_hwdep *hwdep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 /* Stop system timer */
89 spin_lock_irqsave(&opl3->sys_timer_lock, flags);
90 if (opl3->sys_timer_status) {
91 del_timer(&opl3->tlist);
92 opl3->sys_timer_status = 0;
93 }
94 spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
95
96 snd_opl3_reset(opl3);
Takashi Iwai05c1afe2007-10-30 11:59:15 +010097 hwdep = opl3->hwdep;
98 mutex_lock(&hwdep->open_mutex);
99 hwdep->used--;
100 mutex_unlock(&hwdep->open_mutex);
101 wake_up(&hwdep->open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100104static int snd_opl3_synth_use(void *private_data, struct snd_seq_port_subscribe * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100106 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int err;
108
109 if ((err = snd_opl3_synth_setup(opl3)) < 0)
110 return err;
111
112 if (use_internal_drums) {
113 /* Percussion mode */
114 opl3->voices[6].state = opl3->voices[7].state =
115 opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
116 snd_opl3_load_drums(opl3);
117 opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
118 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
119 } else {
120 opl3->drum_reg = 0x00;
121 }
122
123 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
124 if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
125 return err;
126 }
127 opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
128 return 0;
129}
130
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100131static int snd_opl3_synth_unuse(void *private_data, struct snd_seq_port_subscribe * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100133 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 snd_opl3_synth_cleanup(opl3);
136
137 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
138 snd_opl3_synth_use_dec(opl3);
139 return 0;
140}
141
142/*
143 * MIDI emulation operators
144 */
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100145struct snd_midi_op opl3_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 .note_on = snd_opl3_note_on,
147 .note_off = snd_opl3_note_off,
148 .key_press = snd_opl3_key_press,
149 .note_terminate = snd_opl3_terminate_note,
150 .control = snd_opl3_control,
151 .nrpn = snd_opl3_nrpn,
152 .sysex = snd_opl3_sysex,
153};
154
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100155static int snd_opl3_synth_event_input(struct snd_seq_event * ev, int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 void *private_data, int atomic, int hop)
157{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100158 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Takashi Iwai224a0332007-10-30 11:49:22 +0100160 snd_midi_process_event(&opl3_ops, ev, opl3->chset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return 0;
162}
163
164/* ------------------------------ */
165
166static void snd_opl3_synth_free_port(void *private_data)
167{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100168 struct snd_opl3 *opl3 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 snd_midi_channel_free_set(opl3->chset);
171}
172
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100173static int snd_opl3_synth_create_port(struct snd_opl3 * opl3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100175 struct snd_seq_port_callback callbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 char name[32];
177 int voices, opl_ver;
178
179 voices = (opl3->hardware < OPL3_HW_OPL3) ?
180 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
181 opl3->chset = snd_midi_channel_alloc_set(16);
182 if (opl3->chset == NULL)
183 return -ENOMEM;
184 opl3->chset->private_data = opl3;
185
186 memset(&callbacks, 0, sizeof(callbacks));
187 callbacks.owner = THIS_MODULE;
188 callbacks.use = snd_opl3_synth_use;
189 callbacks.unuse = snd_opl3_synth_unuse;
190 callbacks.event_input = snd_opl3_synth_event_input;
191 callbacks.private_free = snd_opl3_synth_free_port;
192 callbacks.private_data = opl3;
193
194 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
195 sprintf(name, "OPL%i FM Port", opl_ver);
196
197 opl3->chset->client = opl3->seq_client;
198 opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
199 SNDRV_SEQ_PORT_CAP_WRITE |
200 SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
201 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
202 SNDRV_SEQ_PORT_TYPE_MIDI_GM |
Clemens Ladisch450047a2006-05-02 16:08:41 +0200203 SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE |
204 SNDRV_SEQ_PORT_TYPE_HARDWARE |
205 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 16, voices,
207 name);
208 if (opl3->chset->port < 0) {
Dave Jones5e315e92006-03-06 14:03:37 +0100209 int port;
210 port = opl3->chset->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 snd_midi_channel_free_set(opl3->chset);
Dave Jones5e315e92006-03-06 14:03:37 +0100212 return port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 return 0;
215}
216
217/* ------------------------------ */
218
Takashi Iwai05662202015-02-12 13:43:22 +0100219static int snd_opl3_seq_probe(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Takashi Iwai05662202015-02-12 13:43:22 +0100221 struct snd_seq_device *dev = to_seq_dev(_dev);
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100222 struct snd_opl3 *opl3;
Dave Jones5e315e92006-03-06 14:03:37 +0100223 int client, err;
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100224 char name[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int opl_ver;
226
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100227 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (opl3 == NULL)
229 return -EINVAL;
230
231 spin_lock_init(&opl3->voice_lock);
232
233 opl3->seq_client = -1;
234
235 /* allocate new client */
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100236 opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
237 sprintf(name, "OPL%i FM synth", opl_ver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 client = opl3->seq_client =
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100239 snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num,
240 name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (client < 0)
242 return client;
243
Dave Jones5e315e92006-03-06 14:03:37 +0100244 if ((err = snd_opl3_synth_create_port(opl3)) < 0) {
245 snd_seq_delete_kernel_client(client);
246 opl3->seq_client = -1;
247 return err;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* setup system timer */
Takashi Iwaif5d5f752015-01-19 11:28:54 +0100251 setup_timer(&opl3->tlist, snd_opl3_timer_func, (unsigned long) opl3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 spin_lock_init(&opl3->sys_timer_lock);
253 opl3->sys_timer_status = 0;
254
255#ifdef CONFIG_SND_SEQUENCER_OSS
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100256 snd_opl3_init_seq_oss(opl3, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#endif
258 return 0;
259}
260
Takashi Iwai05662202015-02-12 13:43:22 +0100261static int snd_opl3_seq_remove(struct device *_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Takashi Iwai05662202015-02-12 13:43:22 +0100263 struct snd_seq_device *dev = to_seq_dev(_dev);
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100264 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Takashi Iwai5b1646a2005-11-17 14:13:14 +0100266 opl3 = *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (opl3 == NULL)
268 return -EINVAL;
269
270#ifdef CONFIG_SND_SEQUENCER_OSS
271 snd_opl3_free_seq_oss(opl3);
272#endif
273 if (opl3->seq_client >= 0) {
274 snd_seq_delete_kernel_client(opl3->seq_client);
275 opl3->seq_client = -1;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return 0;
278}
279
Takashi Iwai05662202015-02-12 13:43:22 +0100280static struct snd_seq_driver opl3_seq_driver = {
281 .driver = {
282 .name = KBUILD_MODNAME,
283 .probe = snd_opl3_seq_probe,
284 .remove = snd_opl3_seq_remove,
285 },
286 .id = SNDRV_SEQ_DEV_ID_OPL3,
287 .argsize = sizeof(struct snd_opl3 *),
288};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Takashi Iwai05662202015-02-12 13:43:22 +0100290module_snd_seq_driver(opl3_seq_driver);