]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/core/seq/seq_midi.c
[ALSA] Remove sound/driver.h
[net-next-2.6.git] / sound / core / seq / seq_midi.c
CommitLineData
1da177e4
LT
1/*
2 * Generic MIDI synth driver for ALSA sequencer
3 * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
c1017a4c 4 * Jaroslav Kysela <perex@perex.cz>
1da177e4
LT
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22/*
23Possible options for midisynth module:
24 - automatic opening of midi ports on first received event or subscription
25 (close will be performed when client leaves)
26*/
27
28
1da177e4
LT
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/errno.h>
32#include <linux/string.h>
33#include <linux/moduleparam.h>
1a60d4c5 34#include <linux/mutex.h>
1da177e4
LT
35#include <sound/core.h>
36#include <sound/rawmidi.h>
37#include <sound/seq_kernel.h>
38#include <sound/seq_device.h>
39#include <sound/seq_midi_event.h>
40#include <sound/initval.h>
41
c1017a4c 42MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@perex.cz>");
1da177e4
LT
43MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
44MODULE_LICENSE("GPL");
45static int output_buffer_size = PAGE_SIZE;
46module_param(output_buffer_size, int, 0644);
47MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
48static int input_buffer_size = PAGE_SIZE;
49module_param(input_buffer_size, int, 0644);
50MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
51
52/* data for this midi synth driver */
c7e0b5bf
TI
53struct seq_midisynth {
54 struct snd_card *card;
1da177e4
LT
55 int device;
56 int subdevice;
c7e0b5bf
TI
57 struct snd_rawmidi_file input_rfile;
58 struct snd_rawmidi_file output_rfile;
1da177e4
LT
59 int seq_client;
60 int seq_port;
c7e0b5bf
TI
61 struct snd_midi_event *parser;
62};
1da177e4 63
c7e0b5bf 64struct seq_midisynth_client {
1da177e4
LT
65 int seq_client;
66 int num_ports;
67 int ports_per_device[SNDRV_RAWMIDI_DEVICES];
c7e0b5bf
TI
68 struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES];
69};
1da177e4 70
c7e0b5bf 71static struct seq_midisynth_client *synths[SNDRV_CARDS];
1a60d4c5 72static DEFINE_MUTEX(register_mutex);
1da177e4
LT
73
74/* handle rawmidi input event (MIDI v1.0 stream) */
c7e0b5bf 75static void snd_midi_input_event(struct snd_rawmidi_substream *substream)
1da177e4 76{
c7e0b5bf
TI
77 struct snd_rawmidi_runtime *runtime;
78 struct seq_midisynth *msynth;
79 struct snd_seq_event ev;
1da177e4
LT
80 char buf[16], *pbuf;
81 long res, count;
82
83 if (substream == NULL)
84 return;
85 runtime = substream->runtime;
c7e0b5bf 86 msynth = runtime->private_data;
1da177e4
LT
87 if (msynth == NULL)
88 return;
89 memset(&ev, 0, sizeof(ev));
90 while (runtime->avail > 0) {
91 res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
92 if (res <= 0)
93 continue;
94 if (msynth->parser == NULL)
95 continue;
96 pbuf = buf;
97 while (res > 0) {
98 count = snd_midi_event_encode(msynth->parser, pbuf, res, &ev);
99 if (count < 0)
100 break;
101 pbuf += count;
102 res -= count;
103 if (ev.type != SNDRV_SEQ_EVENT_NONE) {
104 ev.source.port = msynth->seq_port;
105 ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
106 snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
107 /* clear event and reset header */
108 memset(&ev, 0, sizeof(ev));
109 }
110 }
111 }
112}
113
c7e0b5bf 114static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count)
1da177e4 115{
c7e0b5bf 116 struct snd_rawmidi_runtime *runtime;
1da177e4
LT
117 int tmp;
118
119 snd_assert(substream != NULL || buf != NULL, return -EINVAL);
120 runtime = substream->runtime;
121 if ((tmp = runtime->avail) < count) {
122 snd_printd("warning, output event was lost (count = %i, available = %i)\n", count, tmp);
123 return -ENOMEM;
124 }
125 if (snd_rawmidi_kernel_write(substream, buf, count) < count)
126 return -EINVAL;
127 return 0;
128}
129
c7e0b5bf 130static int event_process_midi(struct snd_seq_event *ev, int direct,
1da177e4
LT
131 void *private_data, int atomic, int hop)
132{
c7e0b5bf 133 struct seq_midisynth *msynth = private_data;
1da177e4 134 unsigned char msg[10]; /* buffer for constructing midi messages */
c7e0b5bf 135 struct snd_rawmidi_substream *substream;
d06e4c40 136 int len;
1da177e4
LT
137
138 snd_assert(msynth != NULL, return -EINVAL);
139 substream = msynth->output_rfile.output;
140 if (substream == NULL)
141 return -ENODEV;
142 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) { /* special case, to save space */
143 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
144 /* invalid event */
145 snd_printd("seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
146 return 0;
147 }
d06e4c40 148 snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
1da177e4 149 snd_midi_event_reset_decode(msynth->parser);
1da177e4
LT
150 } else {
151 if (msynth->parser == NULL)
152 return -EIO;
d06e4c40
CL
153 len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
154 if (len < 0)
155 return 0;
156 if (dump_midi(substream, msg, len) < 0)
1da177e4 157 snd_midi_event_reset_decode(msynth->parser);
1da177e4
LT
158 }
159 return 0;
160}
161
162
c7e0b5bf
TI
163static int snd_seq_midisynth_new(struct seq_midisynth *msynth,
164 struct snd_card *card,
1da177e4
LT
165 int device,
166 int subdevice)
167{
168 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
169 return -ENOMEM;
170 msynth->card = card;
171 msynth->device = device;
172 msynth->subdevice = subdevice;
173 return 0;
174}
175
176/* open associated midi device for input */
c7e0b5bf 177static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info)
1da177e4
LT
178{
179 int err;
c7e0b5bf
TI
180 struct seq_midisynth *msynth = private_data;
181 struct snd_rawmidi_runtime *runtime;
182 struct snd_rawmidi_params params;
1da177e4
LT
183
184 /* open midi port */
f87135f5
CL
185 if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
186 msynth->subdevice,
187 SNDRV_RAWMIDI_LFLG_INPUT,
188 &msynth->input_rfile)) < 0) {
1da177e4
LT
189 snd_printd("midi input open failed!!!\n");
190 return err;
191 }
192 runtime = msynth->input_rfile.input->runtime;
193 memset(&params, 0, sizeof(params));
194 params.avail_min = 1;
195 params.buffer_size = input_buffer_size;
196 if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, &params)) < 0) {
197 snd_rawmidi_kernel_release(&msynth->input_rfile);
198 return err;
199 }
200 snd_midi_event_reset_encode(msynth->parser);
201 runtime->event = snd_midi_input_event;
202 runtime->private_data = msynth;
203 snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
204 return 0;
205}
206
207/* close associated midi device for input */
c7e0b5bf 208static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info)
1da177e4
LT
209{
210 int err;
c7e0b5bf 211 struct seq_midisynth *msynth = private_data;
1da177e4
LT
212
213 snd_assert(msynth->input_rfile.input != NULL, return -EINVAL);
214 err = snd_rawmidi_kernel_release(&msynth->input_rfile);
215 return err;
216}
217
218/* open associated midi device for output */
c7e0b5bf 219static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info)
1da177e4
LT
220{
221 int err;
c7e0b5bf
TI
222 struct seq_midisynth *msynth = private_data;
223 struct snd_rawmidi_params params;
1da177e4
LT
224
225 /* open midi port */
f87135f5
CL
226 if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
227 msynth->subdevice,
228 SNDRV_RAWMIDI_LFLG_OUTPUT,
229 &msynth->output_rfile)) < 0) {
1da177e4
LT
230 snd_printd("midi output open failed!!!\n");
231 return err;
232 }
233 memset(&params, 0, sizeof(params));
234 params.avail_min = 1;
235 params.buffer_size = output_buffer_size;
236 if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, &params)) < 0) {
237 snd_rawmidi_kernel_release(&msynth->output_rfile);
238 return err;
239 }
240 snd_midi_event_reset_decode(msynth->parser);
241 return 0;
242}
243
244/* close associated midi device for output */
c7e0b5bf 245static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
1da177e4 246{
c7e0b5bf 247 struct seq_midisynth *msynth = private_data;
1da177e4
LT
248 unsigned char buf = 0xff; /* MIDI reset */
249
250 snd_assert(msynth->output_rfile.output != NULL, return -EINVAL);
251 /* sending single MIDI reset message to shut the device up */
252 snd_rawmidi_kernel_write(msynth->output_rfile.output, &buf, 1);
253 snd_rawmidi_drain_output(msynth->output_rfile.output);
254 return snd_rawmidi_kernel_release(&msynth->output_rfile);
255}
256
257/* delete given midi synth port */
c7e0b5bf 258static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
1da177e4
LT
259{
260 if (msynth == NULL)
261 return;
262
263 if (msynth->seq_client > 0) {
264 /* delete port */
265 snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
266 }
267
268 if (msynth->parser)
269 snd_midi_event_free(msynth->parser);
270}
271
1da177e4
LT
272/* register new midi synth port */
273static int
c7e0b5bf 274snd_seq_midisynth_register_port(struct snd_seq_device *dev)
1da177e4 275{
c7e0b5bf
TI
276 struct seq_midisynth_client *client;
277 struct seq_midisynth *msynth, *ms;
278 struct snd_seq_port_info *port;
279 struct snd_rawmidi_info *info;
a7b928ac 280 struct snd_rawmidi *rmidi = dev->private_data;
1da177e4
LT
281 int newclient = 0;
282 unsigned int p, ports;
c7e0b5bf
TI
283 struct snd_seq_port_callback pcallbacks;
284 struct snd_card *card = dev->card;
1da177e4
LT
285 int device = dev->device;
286 unsigned int input_count = 0, output_count = 0;
287
288 snd_assert(card != NULL && device >= 0 && device < SNDRV_RAWMIDI_DEVICES, return -EINVAL);
289 info = kmalloc(sizeof(*info), GFP_KERNEL);
290 if (! info)
291 return -ENOMEM;
292 info->device = device;
293 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
294 info->subdevice = 0;
295 if (snd_rawmidi_info_select(card, info) >= 0)
296 output_count = info->subdevices_count;
297 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
298 if (snd_rawmidi_info_select(card, info) >= 0) {
299 input_count = info->subdevices_count;
300 }
301 ports = output_count;
302 if (ports < input_count)
303 ports = input_count;
304 if (ports == 0) {
305 kfree(info);
306 return -ENODEV;
307 }
308 if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
309 ports = 256 / SNDRV_RAWMIDI_DEVICES;
310
1a60d4c5 311 mutex_lock(&register_mutex);
1da177e4
LT
312 client = synths[card->number];
313 if (client == NULL) {
314 newclient = 1;
ecca82b4 315 client = kzalloc(sizeof(*client), GFP_KERNEL);
1da177e4 316 if (client == NULL) {
1a60d4c5 317 mutex_unlock(&register_mutex);
1da177e4
LT
318 kfree(info);
319 return -ENOMEM;
320 }
7b6d9245
CL
321 client->seq_client =
322 snd_seq_create_kernel_client(
78fc030b
AH
323 card, 0, "%s", card->shortname[0] ?
324 (const char *)card->shortname : "External MIDI");
1da177e4
LT
325 if (client->seq_client < 0) {
326 kfree(client);
1a60d4c5 327 mutex_unlock(&register_mutex);
1da177e4
LT
328 kfree(info);
329 return -ENOMEM;
330 }
7b6d9245 331 }
1da177e4 332
c7e0b5bf 333 msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
1da177e4
LT
334 port = kmalloc(sizeof(*port), GFP_KERNEL);
335 if (msynth == NULL || port == NULL)
336 goto __nomem;
337
338 for (p = 0; p < ports; p++) {
339 ms = &msynth[p];
340
341 if (snd_seq_midisynth_new(ms, card, device, p) < 0)
342 goto __nomem;
343
344 /* declare port */
345 memset(port, 0, sizeof(*port));
346 port->addr.client = client->seq_client;
347 port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
348 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
349 memset(info, 0, sizeof(*info));
350 info->device = device;
351 if (p < output_count)
352 info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
353 else
354 info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
355 info->subdevice = p;
356 if (snd_rawmidi_info_select(card, info) >= 0)
357 strcpy(port->name, info->subname);
358 if (! port->name[0]) {
359 if (info->name[0]) {
360 if (ports > 1)
361 snprintf(port->name, sizeof(port->name), "%s-%d", info->name, p);
362 else
363 snprintf(port->name, sizeof(port->name), "%s", info->name);
364 } else {
365 /* last resort */
366 if (ports > 1)
367 sprintf(port->name, "MIDI %d-%d-%d", card->number, device, p);
368 else
369 sprintf(port->name, "MIDI %d-%d", card->number, device);
370 }
371 }
372 if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
373 port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
374 if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
375 port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
376 if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
377 info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
378 port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
450047a7
CL
379 port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
380 | SNDRV_SEQ_PORT_TYPE_HARDWARE
381 | SNDRV_SEQ_PORT_TYPE_PORT;
1da177e4
LT
382 port->midi_channels = 16;
383 memset(&pcallbacks, 0, sizeof(pcallbacks));
384 pcallbacks.owner = THIS_MODULE;
385 pcallbacks.private_data = ms;
386 pcallbacks.subscribe = midisynth_subscribe;
387 pcallbacks.unsubscribe = midisynth_unsubscribe;
388 pcallbacks.use = midisynth_use;
389 pcallbacks.unuse = midisynth_unuse;
390 pcallbacks.event_input = event_process_midi;
391 port->kernel = &pcallbacks;
a7b928ac
CL
392 if (rmidi->ops && rmidi->ops->get_port_info)
393 rmidi->ops->get_port_info(rmidi, p, port);
1da177e4
LT
394 if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, port)<0)
395 goto __nomem;
396 ms->seq_client = client->seq_client;
397 ms->seq_port = port->addr.port;
398 }
399 client->ports_per_device[device] = ports;
400 client->ports[device] = msynth;
401 client->num_ports++;
402 if (newclient)
403 synths[card->number] = client;
1a60d4c5 404 mutex_unlock(&register_mutex);
51f633da
TI
405 kfree(info);
406 kfree(port);
1da177e4
LT
407 return 0; /* success */
408
409 __nomem:
410 if (msynth != NULL) {
411 for (p = 0; p < ports; p++)
412 snd_seq_midisynth_delete(&msynth[p]);
413 kfree(msynth);
414 }
415 if (newclient) {
416 snd_seq_delete_kernel_client(client->seq_client);
417 kfree(client);
418 }
419 kfree(info);
420 kfree(port);
1a60d4c5 421 mutex_unlock(&register_mutex);
1da177e4
LT
422 return -ENOMEM;
423}
424
425/* release midi synth port */
426static int
c7e0b5bf 427snd_seq_midisynth_unregister_port(struct snd_seq_device *dev)
1da177e4 428{
c7e0b5bf
TI
429 struct seq_midisynth_client *client;
430 struct seq_midisynth *msynth;
431 struct snd_card *card = dev->card;
1da177e4
LT
432 int device = dev->device, p, ports;
433
1a60d4c5 434 mutex_lock(&register_mutex);
1da177e4
LT
435 client = synths[card->number];
436 if (client == NULL || client->ports[device] == NULL) {
1a60d4c5 437 mutex_unlock(&register_mutex);
1da177e4
LT
438 return -ENODEV;
439 }
440 ports = client->ports_per_device[device];
441 client->ports_per_device[device] = 0;
442 msynth = client->ports[device];
443 client->ports[device] = NULL;
1da177e4
LT
444 for (p = 0; p < ports; p++)
445 snd_seq_midisynth_delete(&msynth[p]);
446 kfree(msynth);
1da177e4
LT
447 client->num_ports--;
448 if (client->num_ports <= 0) {
449 snd_seq_delete_kernel_client(client->seq_client);
450 synths[card->number] = NULL;
451 kfree(client);
452 }
1a60d4c5 453 mutex_unlock(&register_mutex);
1da177e4
LT
454 return 0;
455}
456
457
458static int __init alsa_seq_midi_init(void)
459{
c7e0b5bf 460 static struct snd_seq_dev_ops ops = {
1da177e4
LT
461 snd_seq_midisynth_register_port,
462 snd_seq_midisynth_unregister_port,
463 };
464 memset(&synths, 0, sizeof(synths));
465 snd_seq_autoload_lock();
466 snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH, &ops, 0);
467 snd_seq_autoload_unlock();
468 return 0;
469}
470
471static void __exit alsa_seq_midi_exit(void)
472{
473 snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH);
474}
475
476module_init(alsa_seq_midi_init)
477module_exit(alsa_seq_midi_exit)