58838f7c95f05e2afb73a390aded0b3c3f019d41
[safe/jmp/linux-2.6] / sound / synth / emux / emux_seq.c
1 /*
2  *  Midi Sequencer interface routines.
3  *
4  *  Copyright (C) 1999 Steve Ratcliffe
5  *  Copyright (c) 1999-2000 Takashi Iwai <tiwai@suse.de>
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include "emux_voice.h"
23 #include <linux/slab.h>
24
25
26 /* Prototypes for static functions */
27 static void free_port(void *private);
28 static void snd_emux_init_port(struct snd_emux_port *p);
29 static int snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info);
30 static int snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info);
31
32 /*
33  * MIDI emulation operators
34  */
35 static struct snd_midi_op emux_ops = {
36         snd_emux_note_on,
37         snd_emux_note_off,
38         snd_emux_key_press,
39         snd_emux_terminate_note,
40         snd_emux_control,
41         snd_emux_nrpn,
42         snd_emux_sysex,
43 };
44
45
46 /*
47  * number of MIDI channels
48  */
49 #define MIDI_CHANNELS           16
50
51 /*
52  * type flags for MIDI sequencer port
53  */
54 #define DEFAULT_MIDI_TYPE       (SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |\
55                                  SNDRV_SEQ_PORT_TYPE_MIDI_GM |\
56                                  SNDRV_SEQ_PORT_TYPE_MIDI_GS |\
57                                  SNDRV_SEQ_PORT_TYPE_MIDI_XG)
58
59 /*
60  * Initialise the EMUX Synth by creating a client and registering
61  * a series of ports.
62  * Each of the ports will contain the 16 midi channels.  Applications
63  * can connect to these ports to play midi data.
64  */
65 int
66 snd_emux_init_seq(struct snd_emux *emu, struct snd_card *card, int index)
67 {
68         int  i;
69         struct snd_seq_port_callback pinfo;
70         char tmpname[64];
71
72         emu->client = snd_seq_create_kernel_client(card, index,
73                                                    "%s WaveTable", emu->name);
74         if (emu->client < 0) {
75                 snd_printk("can't create client\n");
76                 return -ENODEV;
77         }
78
79         if (emu->num_ports < 0) {
80                 snd_printk("seqports must be greater than zero\n");
81                 emu->num_ports = 1;
82         } else if (emu->num_ports >= SNDRV_EMUX_MAX_PORTS) {
83                 snd_printk("too many ports."
84                            "limited max. ports %d\n", SNDRV_EMUX_MAX_PORTS);
85                 emu->num_ports = SNDRV_EMUX_MAX_PORTS;
86         }
87
88         memset(&pinfo, 0, sizeof(pinfo));
89         pinfo.owner = THIS_MODULE;
90         pinfo.use = snd_emux_use;
91         pinfo.unuse = snd_emux_unuse;
92         pinfo.event_input = snd_emux_event_input;
93
94         for (i = 0; i < emu->num_ports; i++) {
95                 struct snd_emux_port *p;
96
97                 sprintf(tmpname, "%s Port %d", emu->name, i);
98                 p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS,
99                                          0, &pinfo);
100                 if (p == NULL) {
101                         snd_printk("can't create port\n");
102                         return -ENOMEM;
103                 }
104
105                 p->port_mode =  SNDRV_EMUX_PORT_MODE_MIDI;
106                 snd_emux_init_port(p);
107                 emu->ports[i] = p->chset.port;
108                 emu->portptrs[i] = p;
109         }
110
111         return 0;
112 }
113
114
115 /*
116  * Detach from the ports that were set up for this synthesizer and
117  * destroy the kernel client.
118  */
119 void
120 snd_emux_detach_seq(struct snd_emux *emu)
121 {
122         if (emu->voices)
123                 snd_emux_terminate_all(emu);
124                 
125         mutex_lock(&emu->register_mutex);
126         if (emu->client >= 0) {
127                 snd_seq_delete_kernel_client(emu->client);
128                 emu->client = -1;
129         }
130         mutex_unlock(&emu->register_mutex);
131 }
132
133
134 /*
135  * create a sequencer port and channel_set
136  */
137
138 struct snd_emux_port *
139 snd_emux_create_port(struct snd_emux *emu, char *name,
140                      int max_channels, int oss_port,
141                      struct snd_seq_port_callback *callback)
142 {
143         struct snd_emux_port *p;
144         int i, type, cap;
145
146         /* Allocate structures for this channel */
147         if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
148                 snd_printk("no memory\n");
149                 return NULL;
150         }
151         p->chset.channels = kcalloc(max_channels, sizeof(struct snd_midi_channel), GFP_KERNEL);
152         if (p->chset.channels == NULL) {
153                 snd_printk("no memory\n");
154                 kfree(p);
155                 return NULL;
156         }
157         for (i = 0; i < max_channels; i++)
158                 p->chset.channels[i].number = i;
159         p->chset.private_data = p;
160         p->chset.max_channels = max_channels;
161         p->emu = emu;
162         p->chset.client = emu->client;
163 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
164         snd_emux_create_effect(p);
165 #endif
166         callback->private_free = free_port;
167         callback->private_data = p;
168
169         cap = SNDRV_SEQ_PORT_CAP_WRITE;
170         if (oss_port) {
171                 type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
172         } else {
173                 type = DEFAULT_MIDI_TYPE;
174                 cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
175         }
176
177         p->chset.port = snd_seq_event_port_attach(emu->client, callback,
178                                                   cap, type, max_channels,
179                                                   emu->max_voices, name);
180
181         return p;
182 }
183
184
185 /*
186  * release memory block for port
187  */
188 static void
189 free_port(void *private_data)
190 {
191         struct snd_emux_port *p;
192
193         p = private_data;
194         if (p) {
195 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
196                 snd_emux_delete_effect(p);
197 #endif
198                 kfree(p->chset.channels);
199                 kfree(p);
200         }
201 }
202
203
204 #define DEFAULT_DRUM_FLAGS      (1<<9)
205
206 /*
207  * initialize the port specific parameters
208  */
209 static void
210 snd_emux_init_port(struct snd_emux_port *p)
211 {
212         p->drum_flags = DEFAULT_DRUM_FLAGS;
213         p->volume_atten = 0;
214
215         snd_emux_reset_port(p);
216 }
217
218
219 /*
220  * reset port
221  */
222 void
223 snd_emux_reset_port(struct snd_emux_port *port)
224 {
225         int i;
226
227         /* stop all sounds */
228         snd_emux_sounds_off_all(port);
229
230         snd_midi_channel_set_clear(&port->chset);
231
232 #ifdef SNDRV_EMUX_USE_RAW_EFFECT
233         snd_emux_clear_effect(port);
234 #endif
235
236         /* set port specific control parameters */
237         port->ctrls[EMUX_MD_DEF_BANK] = 0;
238         port->ctrls[EMUX_MD_DEF_DRUM] = 0;
239         port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
240
241         for (i = 0; i < port->chset.max_channels; i++) {
242                 struct snd_midi_channel *chan = port->chset.channels + i;
243                 chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
244         }
245 }
246
247
248 /*
249  * input sequencer event
250  */
251 int
252 snd_emux_event_input(struct snd_seq_event *ev, int direct, void *private_data,
253                      int atomic, int hop)
254 {
255         struct snd_emux_port *port;
256
257         port = private_data;
258         snd_assert(port != NULL && ev != NULL, return -EINVAL);
259
260         snd_midi_process_event(&emux_ops, ev, &port->chset);
261
262         return 0;
263 }
264
265
266 /*
267  * increment usage count
268  */
269 int
270 snd_emux_inc_count(struct snd_emux *emu)
271 {
272         emu->used++;
273         if (!try_module_get(emu->ops.owner))
274                 goto __error;
275         if (!try_module_get(emu->card->module)) {
276                 module_put(emu->ops.owner);
277               __error:
278                 emu->used--;
279                 return 0;
280         }
281         return 1;
282 }
283
284
285 /*
286  * decrease usage count
287  */
288 void
289 snd_emux_dec_count(struct snd_emux *emu)
290 {
291         module_put(emu->card->module);
292         emu->used--;
293         if (emu->used <= 0)
294                 snd_emux_terminate_all(emu);
295         module_put(emu->ops.owner);
296 }
297
298
299 /*
300  * Routine that is called upon a first use of a particular port
301  */
302 static int
303 snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info)
304 {
305         struct snd_emux_port *p;
306         struct snd_emux *emu;
307
308         p = private_data;
309         snd_assert(p != NULL, return -EINVAL);
310         emu = p->emu;
311         snd_assert(emu != NULL, return -EINVAL);
312
313         mutex_lock(&emu->register_mutex);
314         snd_emux_init_port(p);
315         snd_emux_inc_count(emu);
316         mutex_unlock(&emu->register_mutex);
317         return 0;
318 }
319
320 /*
321  * Routine that is called upon the last unuse() of a particular port.
322  */
323 static int
324 snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info)
325 {
326         struct snd_emux_port *p;
327         struct snd_emux *emu;
328
329         p = private_data;
330         snd_assert(p != NULL, return -EINVAL);
331         emu = p->emu;
332         snd_assert(emu != NULL, return -EINVAL);
333
334         mutex_lock(&emu->register_mutex);
335         snd_emux_sounds_off_all(p);
336         snd_emux_dec_count(emu);
337         mutex_unlock(&emu->register_mutex);
338         return 0;
339 }
340
341
342 /*
343  * attach virtual rawmidi devices
344  */
345 int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card)
346 {
347         int i;
348
349         emu->vmidi = NULL;
350         if (emu->midi_ports <= 0)
351                 return 0;
352
353         emu->vmidi = kcalloc(emu->midi_ports, sizeof(struct snd_rawmidi *), GFP_KERNEL);
354         if (emu->vmidi == NULL)
355                 return -ENOMEM;
356
357         for (i = 0; i < emu->midi_ports; i++) {
358                 struct snd_rawmidi *rmidi;
359                 struct snd_virmidi_dev *rdev;
360                 if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
361                         goto __error;
362                 rdev = rmidi->private_data;
363                 sprintf(rmidi->name, "%s Synth MIDI", emu->name);
364                 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
365                 rdev->client = emu->client;
366                 rdev->port = emu->ports[i];
367                 if (snd_device_register(card, rmidi) < 0) {
368                         snd_device_free(card, rmidi);
369                         goto __error;
370                 }
371                 emu->vmidi[i] = rmidi;
372                 //snd_printk("virmidi %d ok\n", i);
373         }
374         return 0;
375
376 __error:
377         //snd_printk("error init..\n");
378         snd_emux_delete_virmidi(emu);
379         return -ENOMEM;
380 }
381
382 int snd_emux_delete_virmidi(struct snd_emux *emu)
383 {
384         int i;
385
386         if (emu->vmidi == NULL)
387                 return 0;
388
389         for (i = 0; i < emu->midi_ports; i++) {
390                 if (emu->vmidi[i])
391                         snd_device_free(emu->card, emu->vmidi[i]);
392         }
393         kfree(emu->vmidi);
394         emu->vmidi = NULL;
395         return 0;
396 }