blob: 8f00f07701c460437d848996dea949e89cb254e8 [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 |\
57 SNDRV_SEQ_PORT_TYPE_MIDI_XG |\
58 SNDRV_SEQ_PORT_TYPE_DIRECT_SAMPLE)
59
60/*
61 * Initialise the EMUX Synth by creating a client and registering
62 * a series of ports.
63 * Each of the ports will contain the 16 midi channels. Applications
64 * can connect to these ports to play midi data.
65 */
66int
Takashi Iwai03da3122005-11-17 14:24:47 +010067snd_emux_init_seq(struct snd_emux *emu, struct snd_card *card, int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 int i;
Takashi Iwai03da3122005-11-17 14:24:47 +010070 struct snd_seq_port_callback pinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 char tmpname[64];
72
Clemens Ladisch7b6d9242005-12-12 09:33:37 +010073 emu->client = snd_seq_create_kernel_client(card, index,
74 "%s WaveTable", emu->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (emu->client < 0) {
76 snd_printk("can't create client\n");
77 return -ENODEV;
78 }
79
80 if (emu->num_ports < 0) {
81 snd_printk("seqports must be greater than zero\n");
82 emu->num_ports = 1;
83 } else if (emu->num_ports >= SNDRV_EMUX_MAX_PORTS) {
84 snd_printk("too many ports."
85 "limited max. ports %d\n", SNDRV_EMUX_MAX_PORTS);
86 emu->num_ports = SNDRV_EMUX_MAX_PORTS;
87 }
88
89 memset(&pinfo, 0, sizeof(pinfo));
90 pinfo.owner = THIS_MODULE;
91 pinfo.use = snd_emux_use;
92 pinfo.unuse = snd_emux_unuse;
93 pinfo.event_input = snd_emux_event_input;
94
95 for (i = 0; i < emu->num_ports; i++) {
Takashi Iwai03da3122005-11-17 14:24:47 +010096 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 sprintf(tmpname, "%s Port %d", emu->name, i);
99 p = snd_emux_create_port(emu, tmpname, MIDI_CHANNELS,
100 0, &pinfo);
101 if (p == NULL) {
102 snd_printk("can't create port\n");
103 return -ENOMEM;
104 }
105
106 p->port_mode = SNDRV_EMUX_PORT_MODE_MIDI;
107 snd_emux_init_port(p);
108 emu->ports[i] = p->chset.port;
109 emu->portptrs[i] = p;
110 }
111
112 return 0;
113}
114
115
116/*
117 * Detach from the ports that were set up for this synthesizer and
118 * destroy the kernel client.
119 */
120void
Takashi Iwai03da3122005-11-17 14:24:47 +0100121snd_emux_detach_seq(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 if (emu->voices)
124 snd_emux_terminate_all(emu);
125
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100126 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (emu->client >= 0) {
128 snd_seq_delete_kernel_client(emu->client);
129 emu->client = -1;
130 }
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100131 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134
135/*
136 * create a sequencer port and channel_set
137 */
138
Takashi Iwai03da3122005-11-17 14:24:47 +0100139struct snd_emux_port *
140snd_emux_create_port(struct snd_emux *emu, char *name,
141 int max_channels, int oss_port,
142 struct snd_seq_port_callback *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Takashi Iwai03da3122005-11-17 14:24:47 +0100144 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int i, type, cap;
146
147 /* Allocate structures for this channel */
Takashi Iwai561b2202005-09-09 14:22:34 +0200148 if ((p = kzalloc(sizeof(*p), GFP_KERNEL)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 snd_printk("no memory\n");
150 return NULL;
151 }
Takashi Iwai03da3122005-11-17 14:24:47 +0100152 p->chset.channels = kcalloc(max_channels, sizeof(struct snd_midi_channel), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (p->chset.channels == NULL) {
154 snd_printk("no memory\n");
155 kfree(p);
156 return NULL;
157 }
158 for (i = 0; i < max_channels; i++)
159 p->chset.channels[i].number = i;
160 p->chset.private_data = p;
161 p->chset.max_channels = max_channels;
162 p->emu = emu;
163 p->chset.client = emu->client;
164#ifdef SNDRV_EMUX_USE_RAW_EFFECT
165 snd_emux_create_effect(p);
166#endif
167 callback->private_free = free_port;
168 callback->private_data = p;
169
170 cap = SNDRV_SEQ_PORT_CAP_WRITE;
171 if (oss_port) {
172 type = SNDRV_SEQ_PORT_TYPE_SPECIFIC;
173 } else {
174 type = DEFAULT_MIDI_TYPE;
175 cap |= SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
176 }
177
178 p->chset.port = snd_seq_event_port_attach(emu->client, callback,
179 cap, type, max_channels,
180 emu->max_voices, name);
181
182 return p;
183}
184
185
186/*
187 * release memory block for port
188 */
189static void
190free_port(void *private_data)
191{
Takashi Iwai03da3122005-11-17 14:24:47 +0100192 struct snd_emux_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 p = private_data;
195 if (p) {
196#ifdef SNDRV_EMUX_USE_RAW_EFFECT
197 snd_emux_delete_effect(p);
198#endif
199 kfree(p->chset.channels);
200 kfree(p);
201 }
202}
203
204
205#define DEFAULT_DRUM_FLAGS (1<<9)
206
207/*
208 * initialize the port specific parameters
209 */
210static void
Takashi Iwai03da3122005-11-17 14:24:47 +0100211snd_emux_init_port(struct snd_emux_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 p->drum_flags = DEFAULT_DRUM_FLAGS;
214 p->volume_atten = 0;
215
216 snd_emux_reset_port(p);
217}
218
219
220/*
221 * reset port
222 */
223void
Takashi Iwai03da3122005-11-17 14:24:47 +0100224snd_emux_reset_port(struct snd_emux_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int i;
227
228 /* stop all sounds */
229 snd_emux_sounds_off_all(port);
230
231 snd_midi_channel_set_clear(&port->chset);
232
233#ifdef SNDRV_EMUX_USE_RAW_EFFECT
234 snd_emux_clear_effect(port);
235#endif
236
237 /* set port specific control parameters */
238 port->ctrls[EMUX_MD_DEF_BANK] = 0;
239 port->ctrls[EMUX_MD_DEF_DRUM] = 0;
240 port->ctrls[EMUX_MD_REALTIME_PAN] = 1;
241
242 for (i = 0; i < port->chset.max_channels; i++) {
Takashi Iwai03da3122005-11-17 14:24:47 +0100243 struct snd_midi_channel *chan = port->chset.channels + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 chan->drum_channel = ((port->drum_flags >> i) & 1) ? 1 : 0;
245 }
246}
247
248
249/*
250 * input sequencer event
251 */
252int
Takashi Iwai03da3122005-11-17 14:24:47 +0100253snd_emux_event_input(struct snd_seq_event *ev, int direct, void *private_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int atomic, int hop)
255{
Takashi Iwai03da3122005-11-17 14:24:47 +0100256 struct snd_emux_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 port = private_data;
259 snd_assert(port != NULL && ev != NULL, return -EINVAL);
260
261 snd_midi_process_event(&emux_ops, ev, &port->chset);
262
263 return 0;
264}
265
266
267/*
268 * increment usage count
269 */
270int
Takashi Iwai03da3122005-11-17 14:24:47 +0100271snd_emux_inc_count(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 emu->used++;
274 if (!try_module_get(emu->ops.owner))
275 goto __error;
276 if (!try_module_get(emu->card->module)) {
277 module_put(emu->ops.owner);
278 __error:
279 emu->used--;
280 return 0;
281 }
282 return 1;
283}
284
285
286/*
287 * decrease usage count
288 */
289void
Takashi Iwai03da3122005-11-17 14:24:47 +0100290snd_emux_dec_count(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 module_put(emu->card->module);
293 emu->used--;
294 if (emu->used <= 0)
295 snd_emux_terminate_all(emu);
296 module_put(emu->ops.owner);
297}
298
299
300/*
301 * Routine that is called upon a first use of a particular port
302 */
303static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100304snd_emux_use(void *private_data, struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Takashi Iwai03da3122005-11-17 14:24:47 +0100306 struct snd_emux_port *p;
307 struct snd_emux *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 p = private_data;
310 snd_assert(p != NULL, return -EINVAL);
311 emu = p->emu;
312 snd_assert(emu != NULL, return -EINVAL);
313
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100314 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 snd_emux_init_port(p);
316 snd_emux_inc_count(emu);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100317 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
320
321/*
322 * Routine that is called upon the last unuse() of a particular port.
323 */
324static int
Takashi Iwai03da3122005-11-17 14:24:47 +0100325snd_emux_unuse(void *private_data, struct snd_seq_port_subscribe *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Takashi Iwai03da3122005-11-17 14:24:47 +0100327 struct snd_emux_port *p;
328 struct snd_emux *emu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 p = private_data;
331 snd_assert(p != NULL, return -EINVAL);
332 emu = p->emu;
333 snd_assert(emu != NULL, return -EINVAL);
334
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100335 mutex_lock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 snd_emux_sounds_off_all(p);
337 snd_emux_dec_count(emu);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100338 mutex_unlock(&emu->register_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return 0;
340}
341
342
343/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * attach virtual rawmidi devices
345 */
Takashi Iwai03da3122005-11-17 14:24:47 +0100346int snd_emux_init_virmidi(struct snd_emux *emu, struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int i;
349
350 emu->vmidi = NULL;
351 if (emu->midi_ports <= 0)
352 return 0;
353
Takashi Iwai03da3122005-11-17 14:24:47 +0100354 emu->vmidi = kcalloc(emu->midi_ports, sizeof(struct snd_rawmidi *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (emu->vmidi == NULL)
356 return -ENOMEM;
357
358 for (i = 0; i < emu->midi_ports; i++) {
Takashi Iwai03da3122005-11-17 14:24:47 +0100359 struct snd_rawmidi *rmidi;
360 struct snd_virmidi_dev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (snd_virmidi_new(card, emu->midi_devidx + i, &rmidi) < 0)
362 goto __error;
363 rdev = rmidi->private_data;
364 sprintf(rmidi->name, "%s Synth MIDI", emu->name);
365 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_ATTACH;
366 rdev->client = emu->client;
367 rdev->port = emu->ports[i];
368 if (snd_device_register(card, rmidi) < 0) {
369 snd_device_free(card, rmidi);
370 goto __error;
371 }
372 emu->vmidi[i] = rmidi;
373 //snd_printk("virmidi %d ok\n", i);
374 }
375 return 0;
376
377__error:
378 //snd_printk("error init..\n");
379 snd_emux_delete_virmidi(emu);
380 return -ENOMEM;
381}
382
Takashi Iwai03da3122005-11-17 14:24:47 +0100383int snd_emux_delete_virmidi(struct snd_emux *emu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 int i;
386
387 if (emu->vmidi == NULL)
388 return 0;
389
390 for (i = 0; i < emu->midi_ports; i++) {
391 if (emu->vmidi[i])
392 snd_device_free(emu->card, emu->vmidi[i]);
393 }
394 kfree(emu->vmidi);
395 emu->vmidi = NULL;
396 return 0;
397}