blob: ca5f7effb4df48815c2367bd8473f1e1024fefdc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
27static void free_port(void *private);
Takashi Iwai03da3122005-11-17 14:24:47 +010028static void snd_emux_init_port(struct snd_emux_port *p);
29static int snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info);
30static int snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * MIDI emulation operators
34 */
Takashi Iwai03da3122005-11-17 14:24:47 +010035static struct snd_midi_op emux_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 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 |\
Clemens Ladisch450047a2006-05-02 16:08:41 +020057 SNDRV_SEQ_PORT_TYPE_MIDI_XG |\
58 SNDRV_SEQ_PORT_TYPE_HARDWARE |\
59 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * Initialise the EMUX Synth by creating a client and registering
63 * a series of ports.
64 * Each of the ports will contain the 16 midi channels. Applications
65 * can connect to these ports to play midi data.
66 */
67int
Takashi Iwai03da3122005-11-17 14:24:47 +010068snd_emux_init_seq(struct snd_emux *emu, struct snd_card *card, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 int i;
Takashi Iwai03da3122005-11-17 14:24:47 +010071 struct snd_seq_port_callback pinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 char tmpname[64];
73
Clemens Ladisch7b6d9242005-12-12 09:33:37 +010074 emu->client = snd_seq_create_kernel_client(card, index,
75 "%s WaveTable", emu->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (emu->client < 0) {
Takashi Iwai42b01582009-02-05 16:01:46 +010077 snd_printk(KERN_ERR "can't create client\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return -ENODEV;
79 }
80
81 if (emu->num_ports < 0) {
Takashi Iwai42b01582009-02-05 16:01:46 +010082 snd_printk(KERN_WARNING "seqports must be greater than zero\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 emu->num_ports = 1;
84 } else if (emu->num_ports >= SNDRV_EMUX_MAX_PORTS) {
Takashi Iwai42b01582009-02-05 16:01:46 +010085 snd_printk(KERN_WARNING "too many ports."
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 "limited max. ports %d\n", SNDRV_EMUX_MAX_PORTS);
87 emu->num_ports = SNDRV_EMUX_MAX_PORTS;
88 }
89
90 memset(&pinfo, 0, sizeof(pinfo));
91 pinfo.owner = THIS_MODULE;
92 pinfo.use = snd_emux_use;
93 pinfo.unuse = snd_emux_unuse;
94 pinfo.event_input = snd_emux_event_input;
95
96 for (i = 0; i < emu->num_ports; i++) {
Takashi Iwai03da3122005-11-17 14:24:47 +010097 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 sprintf(tmpname, "%s Port %d", emu->name, i);
100 p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS,
101 0, &pinfo);
102 if (p == NULL) {
Takashi Iwai42b01582009-02-05 16:01:46 +0100103 snd_printk(KERN_ERR "can't create port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return -ENOMEM;
105 }
106
107 p->port_mode = SNDRV_EMUX_PORT_MODE_MIDI;
108 snd_emux_init_port(p);
109 emu->ports[i] = p->chset.port;
110 emu->portptrs[i] = p;
111 }
112
113 return 0;
114}
115
116
117/*
118 * Detach from the ports that were set up for this synthesizer and
119 * destroy the kernel client.
120 */
121void
Takashi Iwai03da3122005-11-17 14:24:47 +0100122snd_emux_detach_seq(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 if (emu->voices)
125 snd_emux_terminate_all(emu);
126
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100127 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 if (emu->client >= 0) {
129 snd_seq_delete_kernel_client(emu->client);
130 emu->client = -1;
131 }
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100132 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135
136/*
137 * create a sequencer port and channel_set
138 */
139
Takashi Iwai03da3122005-11-17 14:24:47 +0100140struct snd_emux_port *
141snd_emux_create_port(struct snd_emux *emu, char *name,
142 int max_channels, int oss_port,
143 struct snd_seq_port_callback *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Takashi Iwai03da3122005-11-17 14:24:47 +0100145 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int i, type, cap;
147
148 /* Allocate structures for this channel */
Takashi Iwai561b2202005-09-09 14:22:34 +0200149 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
Takashi Iwai42b01582009-02-05 16:01:46 +0100150 snd_printk(KERN_ERR "no memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return NULL;
152 }
Takashi Iwai03da3122005-11-17 14:24:47 +0100153 p->chset.channels = kcalloc(max_channels, sizeof(struct snd_midi_channel), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (p->chset.channels == NULL) {
Takashi Iwai42b01582009-02-05 16:01:46 +0100155 snd_printk(KERN_ERR "no memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 kfree(p);
157 return NULL;
158 }
159 for (i = 0; i < max_channels; i++)
160 p->chset.channels[i].number = i;
161 p->chset.private_data = p;
162 p->chset.max_channels = max_channels;
163 p->emu = emu;
164 p->chset.client = emu->client;
165#ifdef SNDRV_EMUX_USE_RAW_EFFECT
166 snd_emux_create_effect(p);
167#endif
168 callback->private_free = free_port;
169 callback->private_data = p;
170
171 cap = SNDRV_SEQ_PORT_CAP_WRITE;
172 if (oss_port) {
173 type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
174 } else {
175 type = DEFAULT_MIDI_TYPE;
176 cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
177 }
178
179 p->chset.port = snd_seq_event_port_attach(emu->client, callback,
180 cap, type, max_channels,
181 emu->max_voices, name);
182
183 return p;
184}
185
186
187/*
188 * release memory block for port
189 */
190static void
191free_port(void *private_data)
192{
Takashi Iwai03da3122005-11-17 14:24:47 +0100193 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 p = private_data;
196 if (p) {
197#ifdef SNDRV_EMUX_USE_RAW_EFFECT
198 snd_emux_delete_effect(p);
199#endif
200 kfree(p->chset.channels);
201 kfree(p);
202 }
203}
204
205
206#define DEFAULT_DRUM_FLAGS (1<<9)
207
208/*
209 * initialize the port specific parameters
210 */
211static void
Takashi Iwai03da3122005-11-17 14:24:47 +0100212snd_emux_init_port(struct snd_emux_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 p->drum_flags = DEFAULT_DRUM_FLAGS;
215 p->volume_atten = 0;
216
217 snd_emux_reset_port(p);
218}
219
220
221/*
222 * reset port
223 */
224void
Takashi Iwai03da3122005-11-17 14:24:47 +0100225snd_emux_reset_port(struct snd_emux_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 int i;
228
229 /* stop all sounds */
230 snd_emux_sounds_off_all(port);
231
232 snd_midi_channel_set_clear(&port->chset);
233
234#ifdef SNDRV_EMUX_USE_RAW_EFFECT
235 snd_emux_clear_effect(port);
236#endif
237
238 /* set port specific control parameters */
239 port->ctrls[EMUX_MD_DEF_BANK] = 0;
240 port->ctrls[EMUX_MD_DEF_DRUM] = 0;
241 port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
242
243 for (i = 0; i < port->chset.max_channels; i++) {
Takashi Iwai03da3122005-11-17 14:24:47 +0100244 struct snd_midi_channel *chan = port->chset.channels + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
246 }
247}
248
249
250/*
251 * input sequencer event
252 */
253int
Takashi Iwai03da3122005-11-17 14:24:47 +0100254snd_emux_event_input(struct snd_seq_event *ev, int direct, void *private_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 int atomic, int hop)
256{
Takashi Iwai03da3122005-11-17 14:24:47 +0100257 struct snd_emux_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 port = private_data;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200260 if (snd_BUG_ON(!port || !ev))
261 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 snd_midi_process_event(&emux_ops, ev, &port->chset);
264
265 return 0;
266}
267
268
269/*
270 * increment usage count
271 */
272int
Takashi Iwai03da3122005-11-17 14:24:47 +0100273snd_emux_inc_count(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 emu->used++;
276 if (!try_module_get(emu->ops.owner))
277 goto __error;
278 if (!try_module_get(emu->card->module)) {
279 module_put(emu->ops.owner);
280 __error:
281 emu->used--;
282 return 0;
283 }
284 return 1;
285}
286
287
288/*
289 * decrease usage count
290 */
291void
Takashi Iwai03da3122005-11-17 14:24:47 +0100292snd_emux_dec_count(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 module_put(emu->card->module);
295 emu->used--;
296 if (emu->used <= 0)
297 snd_emux_terminate_all(emu);
298 module_put(emu->ops.owner);
299}
300
301
302/*
303 * Routine that is called upon a first use of a particular port
304 */
305static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100306snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Takashi Iwai03da3122005-11-17 14:24:47 +0100308 struct snd_emux_port *p;
309 struct snd_emux *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 p = private_data;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200312 if (snd_BUG_ON(!p))
313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 emu = p->emu;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200315 if (snd_BUG_ON(!emu))
316 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100318 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 snd_emux_init_port(p);
320 snd_emux_inc_count(emu);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100321 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return 0;
323}
324
325/*
326 * Routine that is called upon the last unuse() of a particular port.
327 */
328static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100329snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Takashi Iwai03da3122005-11-17 14:24:47 +0100331 struct snd_emux_port *p;
332 struct snd_emux *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 p = private_data;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200335 if (snd_BUG_ON(!p))
336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 emu = p->emu;
Takashi Iwai5e246b82008-08-08 17:12:47 +0200338 if (snd_BUG_ON(!emu))
339 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100341 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 snd_emux_sounds_off_all(p);
343 snd_emux_dec_count(emu);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100344 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346}
347
348
349/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * attach virtual rawmidi devices
351 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100352int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int i;
355
356 emu->vmidi = NULL;
357 if (emu->midi_ports <= 0)
358 return 0;
359
Takashi Iwai03da3122005-11-17 14:24:47 +0100360 emu->vmidi = kcalloc(emu->midi_ports, sizeof(struct snd_rawmidi *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (emu->vmidi == NULL)
362 return -ENOMEM;
363
364 for (i = 0; i < emu->midi_ports; i++) {
Takashi Iwai03da3122005-11-17 14:24:47 +0100365 struct snd_rawmidi *rmidi;
366 struct snd_virmidi_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
368 goto __error;
369 rdev = rmidi->private_data;
370 sprintf(rmidi->name, "%s Synth MIDI", emu->name);
371 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
372 rdev->client = emu->client;
373 rdev->port = emu->ports[i];
374 if (snd_device_register(card, rmidi) < 0) {
375 snd_device_free(card, rmidi);
376 goto __error;
377 }
378 emu->vmidi[i] = rmidi;
Takashi Iwai42b01582009-02-05 16:01:46 +0100379 /* snd_printk(KERN_DEBUG "virmidi %d ok\n", i); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 return 0;
382
383__error:
Takashi Iwai42b01582009-02-05 16:01:46 +0100384 /* snd_printk(KERN_DEBUG "error init..\n"); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 snd_emux_delete_virmidi(emu);
386 return -ENOMEM;
387}
388
Takashi Iwai03da3122005-11-17 14:24:47 +0100389int snd_emux_delete_virmidi(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 int i;
392
393 if (emu->vmidi == NULL)
394 return 0;
395
396 for (i = 0; i < emu->midi_ports; i++) {
397 if (emu->vmidi[i])
398 snd_device_free(emu->card, emu->vmidi[i]);
399 }
400 kfree(emu->vmidi);
401 emu->vmidi = NULL;
402 return 0;
403}