patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / drivers / opl4 / opl4_seq.c
1 /*
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>
37 #include <sound/initval.h>
38
39 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
40 MODULE_DESCRIPTION("OPL4 wavetable synth driver");
41 MODULE_LICENSE("Dual BSD/GPL");
42 MODULE_CLASSES("{sound}");
43
44 int volume_boost = 8;
45
46 module_param(volume_boost, int, 0644);
47 MODULE_PARM_DESC(volume_boost, "Additional volume for OPL4 wavetable sounds.");
48 MODULE_PARM_SYNTAX(volume_boost, "default:8");
49
50 static int snd_opl4_seq_use_inc(opl4_t *opl4)
51 {
52         if (!try_module_get(opl4->card->module))
53                 return -EFAULT;
54         return 0;
55 }
56
57 static void snd_opl4_seq_use_dec(opl4_t *opl4)
58 {
59         module_put(opl4->card->module);
60 }
61
62 static int snd_opl4_seq_use(void *private_data, snd_seq_port_subscribe_t *info)
63 {
64         opl4_t *opl4 = snd_magic_cast(opl4_t, private_data, return -ENXIO);
65         int err;
66
67         down(&opl4->access_mutex);
68
69         if (opl4->used) {
70                 up(&opl4->access_mutex);
71                 return -EBUSY;
72         }
73         opl4->used++;
74
75         if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
76                 err = snd_opl4_seq_use_inc(opl4);
77                 if (err < 0) {
78                         up(&opl4->access_mutex);
79                         return err;
80                 }
81         }
82
83         up(&opl4->access_mutex);
84
85         snd_opl4_synth_reset(opl4);
86         return 0;
87 }
88
89 static int snd_opl4_seq_unuse(void *private_data, snd_seq_port_subscribe_t *info)
90 {
91         opl4_t *opl4 = snd_magic_cast(opl4_t, private_data, return -ENXIO);
92
93         snd_opl4_synth_shutdown(opl4);
94
95         down(&opl4->access_mutex);
96         opl4->used--;
97         up(&opl4->access_mutex);
98
99         if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
100                 snd_opl4_seq_use_dec(opl4);
101         return 0;
102 }
103
104 static snd_midi_op_t opl4_ops = {
105         .note_on =              snd_opl4_note_on,
106         .note_off =             snd_opl4_note_off,
107         .note_terminate =       snd_opl4_terminate_note,
108         .control =              snd_opl4_control,
109         .sysex =                snd_opl4_sysex,
110 };
111
112 static int snd_opl4_seq_event_input(snd_seq_event_t *ev, int direct,
113                                     void *private_data, int atomic, int hop)
114 {
115         opl4_t *opl4 = snd_magic_cast(opl4_t, private_data, return -ENXIO);
116
117         snd_midi_process_event(&opl4_ops, ev, opl4->chset);
118         return 0;
119 }
120
121 static void snd_opl4_seq_free_port(void *private_data)
122 {
123         opl4_t *opl4 = snd_magic_cast(opl4_t, private_data, return);
124
125         snd_midi_channel_free_set(opl4->chset);
126 }
127
128 static int snd_opl4_seq_new_device(snd_seq_device_t *dev)
129 {
130         opl4_t *opl4;
131         int client;
132         snd_seq_client_callback_t callbacks;
133         snd_seq_client_info_t cinfo;
134         snd_seq_port_callback_t pcallbacks;
135
136         opl4 = *(opl4_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
137         if (!opl4)
138                 return -EINVAL;
139
140         if (snd_yrw801_detect(opl4) < 0)
141                 return -ENODEV;
142
143         opl4->chset = snd_midi_channel_alloc_set(16);
144         if (!opl4->chset)
145                 return -ENOMEM;
146         opl4->chset->private_data = opl4;
147
148         /* allocate new client */
149         memset(&callbacks, 0, sizeof(callbacks));
150         callbacks.private_data = opl4;
151         callbacks.allow_output = callbacks.allow_input = 1;
152         client = snd_seq_create_kernel_client(opl4->card, opl4->seq_dev_num, &callbacks);
153         if (client < 0) {
154                 snd_midi_channel_free_set(opl4->chset);
155                 return client;
156         }
157         opl4->seq_client = client;
158         opl4->chset->client = client;
159
160         /* change name of client */
161         memset(&cinfo, 0, sizeof(cinfo));
162         cinfo.client = client;
163         cinfo.type = KERNEL_CLIENT;
164         strcpy(cinfo.name, "OPL4 Wavetable");
165         snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
166
167         /* create new port */
168         memset(&pcallbacks, 0, sizeof(pcallbacks));
169         pcallbacks.owner = THIS_MODULE;
170         pcallbacks.use = snd_opl4_seq_use;
171         pcallbacks.unuse = snd_opl4_seq_unuse;
172         pcallbacks.event_input = snd_opl4_seq_event_input;
173         pcallbacks.private_free = snd_opl4_seq_free_port;
174         pcallbacks.private_data = opl4;
175
176         opl4->chset->port = snd_seq_event_port_attach(client, &pcallbacks,
177                                                       SNDRV_SEQ_PORT_CAP_WRITE |
178                                                       SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
179                                                       SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
180                                                       SNDRV_SEQ_PORT_TYPE_MIDI_GM,
181                                                       16, 24,
182                                                       "OPL4 Wavetable Port");
183         if (opl4->chset->port < 0) {
184                 int err = opl4->chset->port;
185                 snd_midi_channel_free_set(opl4->chset);
186                 snd_seq_delete_kernel_client(client);
187                 opl4->seq_client = -1;
188                 return err;
189         }
190         return 0;
191 }
192
193 static int snd_opl4_seq_delete_device(snd_seq_device_t *dev)
194 {
195         opl4_t *opl4;
196
197         opl4 = *(opl4_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
198         if (!opl4)
199                 return -EINVAL;
200
201         if (opl4->seq_client >= 0) {
202                 snd_seq_delete_kernel_client(opl4->seq_client);
203                 opl4->seq_client = -1;
204         }
205         return 0;
206 }
207
208 static int __init alsa_opl4_synth_init(void)
209 {
210         static snd_seq_dev_ops_t ops = {
211                 snd_opl4_seq_new_device,
212                 snd_opl4_seq_delete_device
213         };
214
215         return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL4, &ops,
216                                               sizeof(opl4_t*));
217 }
218
219 static void __exit alsa_opl4_synth_exit(void)
220 {
221         snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL4);
222 }
223
224 module_init(alsa_opl4_synth_init)
225 module_exit(alsa_opl4_synth_exit)