2 * Generic MIDI synth driver for ALSA sequencer
3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
4 * Jaroslav Kysela <perex@suse.cz>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 Possible options for midisynth module:
24 - automatic opening of midi ports on first received event or subscription
25 (close will be performed when client leaves)
29 #include <sound/driver.h>
30 #include <linux/init.h>
31 #include <linux/slab.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/moduleparam.h>
35 #include <asm/semaphore.h>
36 #include <sound/core.h>
37 #include <sound/rawmidi.h>
38 #include <sound/seq_kernel.h>
39 #include <sound/seq_device.h>
40 #include <sound/seq_midi_event.h>
41 #include <sound/initval.h>
43 MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@suse.cz>");
44 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
45 MODULE_LICENSE("GPL");
46 static int output_buffer_size
= PAGE_SIZE
;
47 module_param(output_buffer_size
, int, 0644);
48 MODULE_PARM_DESC(output_buffer_size
, "Output buffer size in bytes.");
49 static int input_buffer_size
= PAGE_SIZE
;
50 module_param(input_buffer_size
, int, 0644);
51 MODULE_PARM_DESC(input_buffer_size
, "Input buffer size in bytes.");
53 /* data for this midi synth driver */
54 struct seq_midisynth
{
55 struct snd_card
*card
;
58 struct snd_rawmidi_file input_rfile
;
59 struct snd_rawmidi_file output_rfile
;
62 struct snd_midi_event
*parser
;
65 struct seq_midisynth_client
{
68 int ports_per_device
[SNDRV_RAWMIDI_DEVICES
];
69 struct seq_midisynth
*ports
[SNDRV_RAWMIDI_DEVICES
];
72 static struct seq_midisynth_client
*synths
[SNDRV_CARDS
];
73 static DECLARE_MUTEX(register_mutex
);
75 /* handle rawmidi input event (MIDI v1.0 stream) */
76 static void snd_midi_input_event(struct snd_rawmidi_substream
*substream
)
78 struct snd_rawmidi_runtime
*runtime
;
79 struct seq_midisynth
*msynth
;
80 struct snd_seq_event ev
;
84 if (substream
== NULL
)
86 runtime
= substream
->runtime
;
87 msynth
= runtime
->private_data
;
90 memset(&ev
, 0, sizeof(ev
));
91 while (runtime
->avail
> 0) {
92 res
= snd_rawmidi_kernel_read(substream
, buf
, sizeof(buf
));
95 if (msynth
->parser
== NULL
)
99 count
= snd_midi_event_encode(msynth
->parser
, pbuf
, res
, &ev
);
104 if (ev
.type
!= SNDRV_SEQ_EVENT_NONE
) {
105 ev
.source
.port
= msynth
->seq_port
;
106 ev
.dest
.client
= SNDRV_SEQ_ADDRESS_SUBSCRIBERS
;
107 snd_seq_kernel_client_dispatch(msynth
->seq_client
, &ev
, 1, 0);
108 /* clear event and reset header */
109 memset(&ev
, 0, sizeof(ev
));
115 static int dump_midi(struct snd_rawmidi_substream
*substream
, const char *buf
, int count
)
117 struct snd_rawmidi_runtime
*runtime
;
120 snd_assert(substream
!= NULL
|| buf
!= NULL
, return -EINVAL
);
121 runtime
= substream
->runtime
;
122 if ((tmp
= runtime
->avail
) < count
) {
123 snd_printd("warning, output event was lost (count = %i, available = %i)\n", count
, tmp
);
126 if (snd_rawmidi_kernel_write(substream
, buf
, count
) < count
)
131 static int event_process_midi(struct snd_seq_event
*ev
, int direct
,
132 void *private_data
, int atomic
, int hop
)
134 struct seq_midisynth
*msynth
= private_data
;
135 unsigned char msg
[10]; /* buffer for constructing midi messages */
136 struct snd_rawmidi_substream
*substream
;
139 snd_assert(msynth
!= NULL
, return -EINVAL
);
140 substream
= msynth
->output_rfile
.output
;
141 if (substream
== NULL
)
143 if (ev
->type
== SNDRV_SEQ_EVENT_SYSEX
) { /* special case, to save space */
144 if ((ev
->flags
& SNDRV_SEQ_EVENT_LENGTH_MASK
) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE
) {
146 snd_printd("seq_midi: invalid sysex event flags = 0x%x\n", ev
->flags
);
149 snd_seq_dump_var_event(ev
, (snd_seq_dump_func_t
)dump_midi
, substream
);
150 snd_midi_event_reset_decode(msynth
->parser
);
152 if (msynth
->parser
== NULL
)
154 len
= snd_midi_event_decode(msynth
->parser
, msg
, sizeof(msg
), ev
);
157 if (dump_midi(substream
, msg
, len
) < 0)
158 snd_midi_event_reset_decode(msynth
->parser
);
164 static int snd_seq_midisynth_new(struct seq_midisynth
*msynth
,
165 struct snd_card
*card
,
169 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF
, &msynth
->parser
) < 0)
172 msynth
->device
= device
;
173 msynth
->subdevice
= subdevice
;
177 /* open associated midi device for input */
178 static int midisynth_subscribe(void *private_data
, struct snd_seq_port_subscribe
*info
)
181 struct seq_midisynth
*msynth
= private_data
;
182 struct snd_rawmidi_runtime
*runtime
;
183 struct snd_rawmidi_params params
;
186 if ((err
= snd_rawmidi_kernel_open(msynth
->card
, msynth
->device
,
188 SNDRV_RAWMIDI_LFLG_INPUT
,
189 &msynth
->input_rfile
)) < 0) {
190 snd_printd("midi input open failed!!!\n");
193 runtime
= msynth
->input_rfile
.input
->runtime
;
194 memset(¶ms
, 0, sizeof(params
));
195 params
.avail_min
= 1;
196 params
.buffer_size
= input_buffer_size
;
197 if ((err
= snd_rawmidi_input_params(msynth
->input_rfile
.input
, ¶ms
)) < 0) {
198 snd_rawmidi_kernel_release(&msynth
->input_rfile
);
201 snd_midi_event_reset_encode(msynth
->parser
);
202 runtime
->event
= snd_midi_input_event
;
203 runtime
->private_data
= msynth
;
204 snd_rawmidi_kernel_read(msynth
->input_rfile
.input
, NULL
, 0);
208 /* close associated midi device for input */
209 static int midisynth_unsubscribe(void *private_data
, struct snd_seq_port_subscribe
*info
)
212 struct seq_midisynth
*msynth
= private_data
;
214 snd_assert(msynth
->input_rfile
.input
!= NULL
, return -EINVAL
);
215 err
= snd_rawmidi_kernel_release(&msynth
->input_rfile
);
219 /* open associated midi device for output */
220 static int midisynth_use(void *private_data
, struct snd_seq_port_subscribe
*info
)
223 struct seq_midisynth
*msynth
= private_data
;
224 struct snd_rawmidi_params params
;
227 if ((err
= snd_rawmidi_kernel_open(msynth
->card
, msynth
->device
,
229 SNDRV_RAWMIDI_LFLG_OUTPUT
,
230 &msynth
->output_rfile
)) < 0) {
231 snd_printd("midi output open failed!!!\n");
234 memset(¶ms
, 0, sizeof(params
));
235 params
.avail_min
= 1;
236 params
.buffer_size
= output_buffer_size
;
237 if ((err
= snd_rawmidi_output_params(msynth
->output_rfile
.output
, ¶ms
)) < 0) {
238 snd_rawmidi_kernel_release(&msynth
->output_rfile
);
241 snd_midi_event_reset_decode(msynth
->parser
);
245 /* close associated midi device for output */
246 static int midisynth_unuse(void *private_data
, struct snd_seq_port_subscribe
*info
)
248 struct seq_midisynth
*msynth
= private_data
;
249 unsigned char buf
= 0xff; /* MIDI reset */
251 snd_assert(msynth
->output_rfile
.output
!= NULL
, return -EINVAL
);
252 /* sending single MIDI reset message to shut the device up */
253 snd_rawmidi_kernel_write(msynth
->output_rfile
.output
, &buf
, 1);
254 snd_rawmidi_drain_output(msynth
->output_rfile
.output
);
255 return snd_rawmidi_kernel_release(&msynth
->output_rfile
);
258 /* delete given midi synth port */
259 static void snd_seq_midisynth_delete(struct seq_midisynth
*msynth
)
264 if (msynth
->seq_client
> 0) {
266 snd_seq_event_port_detach(msynth
->seq_client
, msynth
->seq_port
);
270 snd_midi_event_free(msynth
->parser
);
273 /* set our client name */
274 static int set_client_name(struct seq_midisynth_client
*client
, struct snd_card
*card
,
275 struct snd_rawmidi_info
*rmidi
)
277 struct snd_seq_client_info cinfo
;
280 memset(&cinfo
, 0, sizeof(cinfo
));
281 cinfo
.client
= client
->seq_client
;
282 cinfo
.type
= KERNEL_CLIENT
;
283 name
= rmidi
->name
[0] ? (const char *)rmidi
->name
: "External MIDI";
284 strlcpy(cinfo
.name
, name
, sizeof(cinfo
.name
));
285 return snd_seq_kernel_client_ctl(client
->seq_client
, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO
, &cinfo
);
288 /* register new midi synth port */
290 snd_seq_midisynth_register_port(struct snd_seq_device
*dev
)
292 struct seq_midisynth_client
*client
;
293 struct seq_midisynth
*msynth
, *ms
;
294 struct snd_seq_port_info
*port
;
295 struct snd_rawmidi_info
*info
;
297 unsigned int p
, ports
;
298 struct snd_seq_client_callback callbacks
;
299 struct snd_seq_port_callback pcallbacks
;
300 struct snd_card
*card
= dev
->card
;
301 int device
= dev
->device
;
302 unsigned int input_count
= 0, output_count
= 0;
304 snd_assert(card
!= NULL
&& device
>= 0 && device
< SNDRV_RAWMIDI_DEVICES
, return -EINVAL
);
305 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
308 info
->device
= device
;
309 info
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
311 if (snd_rawmidi_info_select(card
, info
) >= 0)
312 output_count
= info
->subdevices_count
;
313 info
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
314 if (snd_rawmidi_info_select(card
, info
) >= 0) {
315 input_count
= info
->subdevices_count
;
317 ports
= output_count
;
318 if (ports
< input_count
)
324 if (ports
> (256 / SNDRV_RAWMIDI_DEVICES
))
325 ports
= 256 / SNDRV_RAWMIDI_DEVICES
;
327 down(®ister_mutex
);
328 client
= synths
[card
->number
];
329 if (client
== NULL
) {
331 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
332 if (client
== NULL
) {
337 memset(&callbacks
, 0, sizeof(callbacks
));
338 callbacks
.private_data
= client
;
339 callbacks
.allow_input
= callbacks
.allow_output
= 1;
340 client
->seq_client
= snd_seq_create_kernel_client(card
, 0, &callbacks
);
341 if (client
->seq_client
< 0) {
347 set_client_name(client
, card
, info
);
348 } else if (device
== 0)
349 set_client_name(client
, card
, info
); /* use the first device's name */
351 msynth
= kcalloc(ports
, sizeof(struct seq_midisynth
), GFP_KERNEL
);
352 port
= kmalloc(sizeof(*port
), GFP_KERNEL
);
353 if (msynth
== NULL
|| port
== NULL
)
356 for (p
= 0; p
< ports
; p
++) {
359 if (snd_seq_midisynth_new(ms
, card
, device
, p
) < 0)
363 memset(port
, 0, sizeof(*port
));
364 port
->addr
.client
= client
->seq_client
;
365 port
->addr
.port
= device
* (256 / SNDRV_RAWMIDI_DEVICES
) + p
;
366 port
->flags
= SNDRV_SEQ_PORT_FLG_GIVEN_PORT
;
367 memset(info
, 0, sizeof(*info
));
368 info
->device
= device
;
369 if (p
< output_count
)
370 info
->stream
= SNDRV_RAWMIDI_STREAM_OUTPUT
;
372 info
->stream
= SNDRV_RAWMIDI_STREAM_INPUT
;
374 if (snd_rawmidi_info_select(card
, info
) >= 0)
375 strcpy(port
->name
, info
->subname
);
376 if (! port
->name
[0]) {
379 snprintf(port
->name
, sizeof(port
->name
), "%s-%d", info
->name
, p
);
381 snprintf(port
->name
, sizeof(port
->name
), "%s", info
->name
);
385 sprintf(port
->name
, "MIDI %d-%d-%d", card
->number
, device
, p
);
387 sprintf(port
->name
, "MIDI %d-%d", card
->number
, device
);
390 if ((info
->flags
& SNDRV_RAWMIDI_INFO_OUTPUT
) && p
< output_count
)
391 port
->capability
|= SNDRV_SEQ_PORT_CAP_WRITE
| SNDRV_SEQ_PORT_CAP_SYNC_WRITE
| SNDRV_SEQ_PORT_CAP_SUBS_WRITE
;
392 if ((info
->flags
& SNDRV_RAWMIDI_INFO_INPUT
) && p
< input_count
)
393 port
->capability
|= SNDRV_SEQ_PORT_CAP_READ
| SNDRV_SEQ_PORT_CAP_SYNC_READ
| SNDRV_SEQ_PORT_CAP_SUBS_READ
;
394 if ((port
->capability
& (SNDRV_SEQ_PORT_CAP_WRITE
|SNDRV_SEQ_PORT_CAP_READ
)) == (SNDRV_SEQ_PORT_CAP_WRITE
|SNDRV_SEQ_PORT_CAP_READ
) &&
395 info
->flags
& SNDRV_RAWMIDI_INFO_DUPLEX
)
396 port
->capability
|= SNDRV_SEQ_PORT_CAP_DUPLEX
;
397 port
->type
= SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
;
398 port
->midi_channels
= 16;
399 memset(&pcallbacks
, 0, sizeof(pcallbacks
));
400 pcallbacks
.owner
= THIS_MODULE
;
401 pcallbacks
.private_data
= ms
;
402 pcallbacks
.subscribe
= midisynth_subscribe
;
403 pcallbacks
.unsubscribe
= midisynth_unsubscribe
;
404 pcallbacks
.use
= midisynth_use
;
405 pcallbacks
.unuse
= midisynth_unuse
;
406 pcallbacks
.event_input
= event_process_midi
;
407 port
->kernel
= &pcallbacks
;
408 if (snd_seq_kernel_client_ctl(client
->seq_client
, SNDRV_SEQ_IOCTL_CREATE_PORT
, port
)<0)
410 ms
->seq_client
= client
->seq_client
;
411 ms
->seq_port
= port
->addr
.port
;
413 client
->ports_per_device
[device
] = ports
;
414 client
->ports
[device
] = msynth
;
417 synths
[card
->number
] = client
;
421 return 0; /* success */
424 if (msynth
!= NULL
) {
425 for (p
= 0; p
< ports
; p
++)
426 snd_seq_midisynth_delete(&msynth
[p
]);
430 snd_seq_delete_kernel_client(client
->seq_client
);
439 /* release midi synth port */
441 snd_seq_midisynth_unregister_port(struct snd_seq_device
*dev
)
443 struct seq_midisynth_client
*client
;
444 struct seq_midisynth
*msynth
;
445 struct snd_card
*card
= dev
->card
;
446 int device
= dev
->device
, p
, ports
;
448 down(®ister_mutex
);
449 client
= synths
[card
->number
];
450 if (client
== NULL
|| client
->ports
[device
] == NULL
) {
454 ports
= client
->ports_per_device
[device
];
455 client
->ports_per_device
[device
] = 0;
456 msynth
= client
->ports
[device
];
457 client
->ports
[device
] = NULL
;
458 for (p
= 0; p
< ports
; p
++)
459 snd_seq_midisynth_delete(&msynth
[p
]);
462 if (client
->num_ports
<= 0) {
463 snd_seq_delete_kernel_client(client
->seq_client
);
464 synths
[card
->number
] = NULL
;
472 static int __init
alsa_seq_midi_init(void)
474 static struct snd_seq_dev_ops ops
= {
475 snd_seq_midisynth_register_port
,
476 snd_seq_midisynth_unregister_port
,
478 memset(&synths
, 0, sizeof(synths
));
479 snd_seq_autoload_lock();
480 snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH
, &ops
, 0);
481 snd_seq_autoload_unlock();
485 static void __exit
alsa_seq_midi_exit(void)
487 snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH
);
490 module_init(alsa_seq_midi_init
)
491 module_exit(alsa_seq_midi_exit
)