patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / drivers / opl3 / opl3_seq.c
1 /*
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
30 MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
31 MODULE_LICENSE("GPL");
32 MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
33 MODULE_CLASSES("{sound}");
34
35 int use_internal_drums = 0;
36 module_param(use_internal_drums, bool, 0444);
37 MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
38
39 int snd_opl3_synth_use_inc(opl3_t * opl3)
40 {
41         if (!try_module_get(opl3->card->module))
42                 return -EFAULT;
43         return 0;
44
45 }
46
47 void snd_opl3_synth_use_dec(opl3_t * opl3)
48 {
49         module_put(opl3->card->module);
50 }
51
52 int snd_opl3_synth_setup(opl3_t * opl3)
53 {
54         int idx;
55
56         down(&opl3->access_mutex);
57         if (opl3->used) {
58                 up(&opl3->access_mutex);
59                 return -EBUSY;
60         }
61         opl3->used++;
62         up(&opl3->access_mutex);
63
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
82 void snd_opl3_synth_cleanup(opl3_t * opl3)
83 {
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);
95         down(&opl3->access_mutex);
96         opl3->used--;
97         up(&opl3->access_mutex);
98 }
99
100 int snd_opl3_synth_use(void *private_data, snd_seq_port_subscribe_t * info)
101 {
102         opl3_t *opl3 = snd_magic_cast(opl3_t, private_data, return -ENXIO);
103         int err;
104
105         if ((err = snd_opl3_synth_setup(opl3)) < 0)
106                 return err;
107
108         if (use_internal_drums) {
109                 /* Percussion mode */
110                 opl3->voices[6].state = opl3->voices[7].state = 
111                         opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
112                 snd_opl3_load_drums(opl3);
113                 opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
114                 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
115         } else {
116                 opl3->drum_reg = 0x00;
117         }
118
119         if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
120                 if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
121                         return err;
122         }
123         opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
124         return 0;
125 }
126
127 int snd_opl3_synth_unuse(void *private_data, snd_seq_port_subscribe_t * info)
128 {
129         opl3_t *opl3 = snd_magic_cast(opl3_t, private_data, return -ENXIO);
130
131         snd_opl3_synth_cleanup(opl3);
132
133         if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
134                 snd_opl3_synth_use_dec(opl3);
135         return 0;
136 }
137
138 /*
139  * MIDI emulation operators
140  */
141 snd_midi_op_t opl3_ops = {
142         .note_on =              snd_opl3_note_on,
143         .note_off =             snd_opl3_note_off,
144         .key_press =            snd_opl3_key_press,
145         .note_terminate =       snd_opl3_terminate_note,
146         .control =              snd_opl3_control,
147         .nrpn =                 snd_opl3_nrpn,
148         .sysex =                snd_opl3_sysex,
149 };
150
151 static int snd_opl3_synth_event_input(snd_seq_event_t * ev, int direct,
152                                       void *private_data, int atomic, int hop)
153 {
154         opl3_t *opl3 = snd_magic_cast(opl3_t, private_data, return -EINVAL);
155
156         if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN &&
157             ev->type <= SNDRV_SEQ_EVENT_INSTR_CHANGE) {
158                 if (direct) {
159                         snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, ev,
160                                             opl3->seq_client, atomic, hop);
161                 }
162         } else {
163                 snd_midi_process_event(&opl3_ops, ev, opl3->chset);
164         }
165         return 0;
166 }
167
168 /* ------------------------------ */
169
170 static void snd_opl3_synth_free_port(void *private_data)
171 {
172         opl3_t *opl3 = snd_magic_cast(opl3_t, private_data, return);
173
174         snd_midi_channel_free_set(opl3->chset);
175 }
176
177 static int snd_opl3_synth_create_port(opl3_t * opl3)
178 {
179         snd_seq_port_callback_t callbacks;
180         char name[32];
181         int voices, opl_ver;
182
183         voices = (opl3->hardware < OPL3_HW_OPL3) ?
184                 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
185         opl3->chset = snd_midi_channel_alloc_set(16);
186         if (opl3->chset == NULL)
187                 return -ENOMEM;
188         opl3->chset->private_data = opl3;
189
190         memset(&callbacks, 0, sizeof(callbacks));
191         callbacks.owner = THIS_MODULE;
192         callbacks.use = snd_opl3_synth_use;
193         callbacks.unuse = snd_opl3_synth_unuse;
194         callbacks.event_input = snd_opl3_synth_event_input;
195         callbacks.private_free = snd_opl3_synth_free_port;
196         callbacks.private_data = opl3;
197
198         opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
199         sprintf(name, "OPL%i FM Port", opl_ver);
200
201         opl3->chset->client = opl3->seq_client;
202         opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
203                                                       SNDRV_SEQ_PORT_CAP_WRITE |
204                                                       SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
205                                                       SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
206                                                       SNDRV_SEQ_PORT_TYPE_MIDI_GM |
207                                                       SNDRV_SEQ_PORT_TYPE_SYNTH,
208                                                       16, voices,
209                                                       name);
210         if (opl3->chset->port < 0) {
211                 snd_midi_channel_free_set(opl3->chset);
212                 return opl3->chset->port;
213         }
214         return 0;
215 }
216
217 /* ------------------------------ */
218
219 static int snd_opl3_seq_new_device(snd_seq_device_t *dev)
220 {
221         opl3_t *opl3;
222         int client;
223         snd_seq_client_callback_t callbacks;
224         snd_seq_client_info_t cinfo;
225         int opl_ver;
226
227         opl3 = *(opl3_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
228         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 */
236         memset(&callbacks, 0, sizeof(callbacks));
237         callbacks.private_data = opl3;
238         callbacks.allow_output = callbacks.allow_input = 1;
239         client = opl3->seq_client =
240             snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num, &callbacks);
241         if (client < 0)
242                 return client;
243
244         /* change name of client */
245         memset(&cinfo, 0, sizeof(cinfo));
246         cinfo.client = client;
247         cinfo.type = KERNEL_CLIENT;
248         opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
249         sprintf(cinfo.name, "OPL%i FM synth", opl_ver);
250         snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
251
252         snd_opl3_synth_create_port(opl3);
253
254         /* initialize instrument list */
255         opl3->ilist = snd_seq_instr_list_new();
256         if (opl3->ilist == NULL) {
257                 snd_seq_delete_kernel_client(client);
258                 opl3->seq_client = -1;
259                 return -ENOMEM;
260         }
261         opl3->ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT;
262         snd_seq_fm_init(&opl3->fm_ops, NULL);
263
264         /* setup system timer */
265         init_timer(&opl3->tlist);
266         opl3->tlist.function = snd_opl3_timer_func;
267         opl3->tlist.data = (unsigned long) opl3;
268         spin_lock_init(&opl3->sys_timer_lock);
269         opl3->sys_timer_status = 0;
270
271 #ifdef CONFIG_SND_SEQUENCER_OSS
272         snd_opl3_init_seq_oss(opl3, cinfo.name);
273 #endif
274         return 0;
275 }
276
277 static int snd_opl3_seq_delete_device(snd_seq_device_t *dev)
278 {
279         opl3_t *opl3;
280
281         opl3 = *(opl3_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
282         if (opl3 == NULL)
283                 return -EINVAL;
284
285 #ifdef CONFIG_SND_SEQUENCER_OSS
286         snd_opl3_free_seq_oss(opl3);
287 #endif
288         if (opl3->seq_client >= 0) {
289                 snd_seq_delete_kernel_client(opl3->seq_client);
290                 opl3->seq_client = -1;
291         }
292         if (opl3->ilist)
293                 snd_seq_instr_list_free(&opl3->ilist);
294         return 0;
295 }
296
297 static int __init alsa_opl3_seq_init(void)
298 {
299         static snd_seq_dev_ops_t ops =
300         {
301                 snd_opl3_seq_new_device,
302                 snd_opl3_seq_delete_device
303         };
304
305         return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL3, &ops,
306                                               sizeof(opl3_t*));
307 }
308
309 static void __exit alsa_opl3_seq_exit(void)
310 {
311         snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL3);
312 }
313
314 module_init(alsa_opl3_seq_init)
315 module_exit(alsa_opl3_seq_exit)