blob: 99197699c55a63f50bd33f45d454a40f4dd5cf70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * OPL4 sequencer functions
3 *
4 * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * Alternatively, this software may be distributed and/or modified under the
17 * terms of the GNU General Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at your option) any later
19 * version.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include "opl4_local.h"
35#include <linux/init.h>
36#include <linux/moduleparam.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040037#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <sound/initval.h>
39
40MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
41MODULE_DESCRIPTION("OPL4 wavetable synth driver");
42MODULE_LICENSE("Dual BSD/GPL");
43
44int volume_boost = 8;
45
46module_param(volume_boost, int, 0644);
47MODULE_PARM_DESC(volume_boost, "Additional volume for OPL4 wavetable sounds.");
48
Takashi Iwaia42dd422005-11-17 14:13:47 +010049static int snd_opl4_seq_use_inc(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 if (!try_module_get(opl4->card->module))
52 return -EFAULT;
53 return 0;
54}
55
Takashi Iwaia42dd422005-11-17 14:13:47 +010056static void snd_opl4_seq_use_dec(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 module_put(opl4->card->module);
59}
60
Takashi Iwaia42dd422005-11-17 14:13:47 +010061static int snd_opl4_seq_use(void *private_data, struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Takashi Iwaia42dd422005-11-17 14:13:47 +010063 struct snd_opl4 *opl4 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int err;
65
Ingo Molnaref9f0a42006-01-16 16:31:42 +010066 mutex_lock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 if (opl4->used) {
Ingo Molnaref9f0a42006-01-16 16:31:42 +010069 mutex_unlock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -EBUSY;
71 }
72 opl4->used++;
73
74 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
75 err = snd_opl4_seq_use_inc(opl4);
76 if (err < 0) {
Ingo Molnaref9f0a42006-01-16 16:31:42 +010077 mutex_unlock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return err;
79 }
80 }
81
Ingo Molnaref9f0a42006-01-16 16:31:42 +010082 mutex_unlock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 snd_opl4_synth_reset(opl4);
85 return 0;
86}
87
Takashi Iwaia42dd422005-11-17 14:13:47 +010088static int snd_opl4_seq_unuse(void *private_data, struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Takashi Iwaia42dd422005-11-17 14:13:47 +010090 struct snd_opl4 *opl4 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 snd_opl4_synth_shutdown(opl4);
93
Ingo Molnaref9f0a42006-01-16 16:31:42 +010094 mutex_lock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 opl4->used--;
Ingo Molnaref9f0a42006-01-16 16:31:42 +010096 mutex_unlock(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
99 snd_opl4_seq_use_dec(opl4);
100 return 0;
101}
102
Takashi Iwaia42dd422005-11-17 14:13:47 +0100103static struct snd_midi_op opl4_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .note_on = snd_opl4_note_on,
105 .note_off = snd_opl4_note_off,
106 .note_terminate = snd_opl4_terminate_note,
107 .control = snd_opl4_control,
108 .sysex = snd_opl4_sysex,
109};
110
Takashi Iwaia42dd422005-11-17 14:13:47 +0100111static int snd_opl4_seq_event_input(struct snd_seq_event *ev, int direct,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 void *private_data, int atomic, int hop)
113{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100114 struct snd_opl4 *opl4 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 snd_midi_process_event(&opl4_ops, ev, opl4->chset);
117 return 0;
118}
119
120static void snd_opl4_seq_free_port(void *private_data)
121{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100122 struct snd_opl4 *opl4 = private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 snd_midi_channel_free_set(opl4->chset);
125}
126
Takashi Iwaia42dd422005-11-17 14:13:47 +0100127static int snd_opl4_seq_new_device(struct snd_seq_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100129 struct snd_opl4 *opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 int client;
Takashi Iwaia42dd422005-11-17 14:13:47 +0100131 struct snd_seq_port_callback pcallbacks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Takashi Iwaia42dd422005-11-17 14:13:47 +0100133 opl4 = *(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!opl4)
135 return -EINVAL;
136
137 if (snd_yrw801_detect(opl4) < 0)
138 return -ENODEV;
139
140 opl4->chset = snd_midi_channel_alloc_set(16);
141 if (!opl4->chset)
142 return -ENOMEM;
143 opl4->chset->private_data = opl4;
144
145 /* allocate new client */
Clemens Ladisch7b6d9242005-12-12 09:33:37 +0100146 client = snd_seq_create_kernel_client(opl4->card, opl4->seq_dev_num,
147 "OPL4 Wavetable");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (client < 0) {
149 snd_midi_channel_free_set(opl4->chset);
150 return client;
151 }
152 opl4->seq_client = client;
153 opl4->chset->client = client;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* create new port */
156 memset(&pcallbacks, 0, sizeof(pcallbacks));
157 pcallbacks.owner = THIS_MODULE;
158 pcallbacks.use = snd_opl4_seq_use;
159 pcallbacks.unuse = snd_opl4_seq_unuse;
160 pcallbacks.event_input = snd_opl4_seq_event_input;
161 pcallbacks.private_free = snd_opl4_seq_free_port;
162 pcallbacks.private_data = opl4;
163
164 opl4->chset->port = snd_seq_event_port_attach(client, &pcallbacks,
165 SNDRV_SEQ_PORT_CAP_WRITE |
166 SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
167 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
Clemens Ladisch450047a2006-05-02 16:08:41 +0200168 SNDRV_SEQ_PORT_TYPE_MIDI_GM |
169 SNDRV_SEQ_PORT_TYPE_HARDWARE |
170 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 16, 24,
172 "OPL4 Wavetable Port");
173 if (opl4->chset->port < 0) {
174 int err = opl4->chset->port;
175 snd_midi_channel_free_set(opl4->chset);
176 snd_seq_delete_kernel_client(client);
177 opl4->seq_client = -1;
178 return err;
179 }
180 return 0;
181}
182
Takashi Iwaia42dd422005-11-17 14:13:47 +0100183static int snd_opl4_seq_delete_device(struct snd_seq_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100185 struct snd_opl4 *opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Takashi Iwaia42dd422005-11-17 14:13:47 +0100187 opl4 = *(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (!opl4)
189 return -EINVAL;
190
191 if (opl4->seq_client >= 0) {
192 snd_seq_delete_kernel_client(opl4->seq_client);
193 opl4->seq_client = -1;
194 }
195 return 0;
196}
197
198static int __init alsa_opl4_synth_init(void)
199{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100200 static struct snd_seq_dev_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 snd_opl4_seq_new_device,
202 snd_opl4_seq_delete_device
203 };
204
205 return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL4, &ops,
Takashi Iwaia42dd422005-11-17 14:13:47 +0100206 sizeof(struct snd_opl4 *));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
209static void __exit alsa_opl4_synth_exit(void)
210{
211 snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL4);
212}
213
214module_init(alsa_opl4_synth_init)
215module_exit(alsa_opl4_synth_exit)