ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / seq / seq_midi.c
1 /*
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>
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 /* 
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)
26 */
27
28
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 <asm/semaphore.h>
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
42 MODULE_AUTHOR("Frank van de Pol <fvdpol@coil.demon.nl>, Jaroslav Kysela <perex@suse.cz>");
43 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
44 MODULE_LICENSE("GPL");
45 MODULE_CLASSES("{sound}");
46 MODULE_SUPPORTED_DEVICE("sound");
47 int output_buffer_size = PAGE_SIZE;
48 MODULE_PARM(output_buffer_size, "i");
49 MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
50 int input_buffer_size = PAGE_SIZE;
51 MODULE_PARM(input_buffer_size, "i");
52 MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
53
54 /* data for this midi synth driver */
55 typedef struct {
56         snd_card_t *card;
57         int device;
58         int subdevice;
59         snd_rawmidi_file_t input_rfile;
60         snd_rawmidi_file_t output_rfile;
61         int seq_client;
62         int seq_port;
63         snd_midi_event_t *parser;
64 } seq_midisynth_t;
65
66 typedef struct {
67         int seq_client;
68         int num_ports;
69         int ports_per_device[SNDRV_RAWMIDI_DEVICES];
70         seq_midisynth_t *ports[SNDRV_RAWMIDI_DEVICES];
71 } seq_midisynth_client_t;
72
73 static seq_midisynth_client_t *synths[SNDRV_CARDS];
74 static DECLARE_MUTEX(register_mutex);
75
76 /* handle rawmidi input event (MIDI v1.0 stream) */
77 static void snd_midi_input_event(snd_rawmidi_substream_t * substream)
78 {
79         snd_rawmidi_runtime_t *runtime;
80         seq_midisynth_t *msynth;
81         snd_seq_event_t ev;
82         char buf[16], *pbuf;
83         long res, count;
84
85         if (substream == NULL)
86                 return;
87         runtime = substream->runtime;
88         msynth = (seq_midisynth_t *) runtime->private_data;
89         if (msynth == NULL)
90                 return;
91         memset(&ev, 0, sizeof(ev));
92         while (runtime->avail > 0) {
93                 res = snd_rawmidi_kernel_read(substream, buf, sizeof(buf));
94                 if (res <= 0)
95                         continue;
96                 if (msynth->parser == NULL)
97                         continue;
98                 pbuf = buf;
99                 while (res > 0) {
100                         count = snd_midi_event_encode(msynth->parser, pbuf, res, &ev);
101                         if (count < 0)
102                                 break;
103                         pbuf += count;
104                         res -= count;
105                         if (ev.type != SNDRV_SEQ_EVENT_NONE) {
106                                 ev.source.port = msynth->seq_port;
107                                 ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
108                                 snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
109                                 /* clear event and reset header */
110                                 memset(&ev, 0, sizeof(ev));
111                         }
112                 }
113         }
114 }
115
116 static int dump_midi(snd_rawmidi_substream_t *substream, const char *buf, int count)
117 {
118         snd_rawmidi_runtime_t *runtime;
119         int tmp;
120
121         snd_assert(substream != NULL || buf != NULL, return -EINVAL);
122         runtime = substream->runtime;
123         if ((tmp = runtime->avail) < count) {
124                 snd_printd("warning, output event was lost (count = %i, available = %i)\n", count, tmp);
125                 return -ENOMEM;
126         }
127         if (snd_rawmidi_kernel_write(substream, buf, count) < count)
128                 return -EINVAL;
129         return 0;
130 }
131
132 static int event_process_midi(snd_seq_event_t * ev, int direct,
133                               void *private_data, int atomic, int hop)
134 {
135         seq_midisynth_t *msynth = (seq_midisynth_t *) private_data;
136         unsigned char msg[10];  /* buffer for constructing midi messages */
137         snd_rawmidi_substream_t *substream;
138         int res;
139
140         snd_assert(msynth != NULL, return -EINVAL);
141         substream = msynth->output_rfile.output;
142         if (substream == NULL)
143                 return -ENODEV;
144         if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {        /* special case, to save space */
145                 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
146                         /* invalid event */
147                         snd_printd("seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
148                         return 0;
149                 }
150                 res = snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)dump_midi, substream);
151                 snd_midi_event_reset_decode(msynth->parser);
152                 if (res < 0)
153                         return res;
154         } else {
155                 if (msynth->parser == NULL)
156                         return -EIO;
157                 res = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
158                 if (res < 0)
159                         return res;
160                 if ((res = dump_midi(substream, msg, res)) < 0) {
161                         snd_midi_event_reset_decode(msynth->parser);
162                         return res;
163                 }
164         }
165         return 0;
166 }
167
168
169 static int snd_seq_midisynth_new(seq_midisynth_t *msynth,
170                                  snd_card_t *card,
171                                  int device,
172                                  int subdevice)
173 {
174         if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
175                 return -ENOMEM;
176         msynth->card = card;
177         msynth->device = device;
178         msynth->subdevice = subdevice;
179         return 0;
180 }
181
182 /* open associated midi device for input */
183 static int midisynth_subscribe(void *private_data, snd_seq_port_subscribe_t *info)
184 {
185         int err;
186         seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
187         snd_rawmidi_runtime_t *runtime;
188         snd_rawmidi_params_t params;
189
190         /* open midi port */
191         if ((err = snd_rawmidi_kernel_open(msynth->card->number, msynth->device, msynth->subdevice, SNDRV_RAWMIDI_LFLG_INPUT, &msynth->input_rfile)) < 0) {
192                 snd_printd("midi input open failed!!!\n");
193                 return err;
194         }
195         runtime = msynth->input_rfile.input->runtime;
196         memset(&params, 0, sizeof(params));
197         params.avail_min = 1;
198         params.buffer_size = input_buffer_size;
199         if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, &params)) < 0) {
200                 snd_rawmidi_kernel_release(&msynth->input_rfile);
201                 return err;
202         }
203         snd_midi_event_reset_encode(msynth->parser);
204         runtime->event = snd_midi_input_event;
205         runtime->private_data = msynth;
206         snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
207         return 0;
208 }
209
210 /* close associated midi device for input */
211 static int midisynth_unsubscribe(void *private_data, snd_seq_port_subscribe_t *info)
212 {
213         int err;
214         seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
215
216         snd_assert(msynth->input_rfile.input != NULL, return -EINVAL);
217         err = snd_rawmidi_kernel_release(&msynth->input_rfile);
218         return err;
219 }
220
221 /* open associated midi device for output */
222 static int midisynth_use(void *private_data, snd_seq_port_subscribe_t *info)
223 {
224         int err;
225         seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
226         snd_rawmidi_params_t params;
227
228         /* open midi port */
229         if ((err = snd_rawmidi_kernel_open(msynth->card->number, msynth->device, msynth->subdevice, SNDRV_RAWMIDI_LFLG_OUTPUT, &msynth->output_rfile)) < 0) {
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 */
245 static int midisynth_unuse(void *private_data, snd_seq_port_subscribe_t *info)
246 {
247         seq_midisynth_t *msynth = (seq_midisynth_t *)private_data;
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 */
258 static void snd_seq_midisynth_delete(seq_midisynth_t *msynth)
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
272 /* set our client name */
273 static int set_client_name(seq_midisynth_client_t *client, snd_card_t *card,
274                            snd_rawmidi_info_t *rmidi)
275 {
276         snd_seq_client_info_t cinfo;
277         const char *name;
278
279         memset(&cinfo, 0, sizeof(cinfo));
280         cinfo.client = client->seq_client;
281         cinfo.type = KERNEL_CLIENT;
282         name = rmidi->name[0] ? (const char *)rmidi->name : "External MIDI";
283         snprintf(cinfo.name, sizeof(cinfo.name), "%s - Rawmidi %d", name, card->number);
284         return snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
285 }
286
287 /* register new midi synth port */
288 int
289 snd_seq_midisynth_register_port(snd_seq_device_t *dev)
290 {
291         seq_midisynth_client_t *client;
292         seq_midisynth_t *msynth, *ms;
293         snd_seq_port_info_t port;
294         snd_rawmidi_info_t info;
295         int newclient = 0;
296         unsigned int p, ports;
297         snd_seq_client_callback_t callbacks;
298         snd_seq_port_callback_t pcallbacks;
299         snd_card_t *card = dev->card;
300         int device = dev->device;
301         unsigned int input_count = 0, output_count = 0;
302
303         snd_assert(card != NULL && device >= 0 && device < SNDRV_RAWMIDI_DEVICES, return -EINVAL);
304         info.device = device;
305         info.stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
306         info.subdevice = 0;
307         if (snd_rawmidi_info_select(card, &info) >= 0)
308                 output_count = info.subdevices_count;
309         info.stream = SNDRV_RAWMIDI_STREAM_INPUT;
310         if (snd_rawmidi_info_select(card, &info) >= 0) {
311                 input_count = info.subdevices_count;
312         }
313         ports = output_count;
314         if (ports < input_count)
315                 ports = input_count;
316         if (ports == 0)
317                 return -ENODEV;
318         if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
319                 ports = 256 / SNDRV_RAWMIDI_DEVICES;
320
321         down(&register_mutex);
322         client = synths[card->number];
323         if (client == NULL) {
324                 newclient = 1;
325                 client = snd_kcalloc(sizeof(seq_midisynth_client_t), GFP_KERNEL);
326                 if (client == NULL) {
327                         up(&register_mutex);
328                         return -ENOMEM;
329                 }
330                 memset(&callbacks, 0, sizeof(callbacks));
331                 callbacks.private_data = client;
332                 callbacks.allow_input = callbacks.allow_output = 1;
333                 client->seq_client = snd_seq_create_kernel_client(card, 0, &callbacks);
334                 if (client->seq_client < 0) {
335                         kfree(client);
336                         up(&register_mutex);
337                         return -ENOMEM;
338                 }
339                 set_client_name(client, card, &info);
340         } else if (device == 0)
341                 set_client_name(client, card, &info); /* use the first device's name */
342
343         msynth = snd_kcalloc(sizeof(seq_midisynth_t) * ports, GFP_KERNEL);
344         if (msynth == NULL)
345                 goto __nomem;
346
347         for (p = 0; p < ports; p++) {
348                 ms = &msynth[p];
349
350                 if (snd_seq_midisynth_new(ms, card, device, p) < 0)
351                         goto __nomem;
352
353                 /* declare port */
354                 memset(&port, 0, sizeof(port));
355                 port.addr.client = client->seq_client;
356                 port.addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
357                 port.flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
358                 memset(&info, 0, sizeof(info));
359                 info.device = device;
360                 if (p < output_count)
361                         info.stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
362                 else
363                         info.stream = SNDRV_RAWMIDI_STREAM_INPUT;
364                 info.subdevice = p;
365                 if (snd_rawmidi_info_select(card, &info) >= 0)
366                         strcpy(port.name, info.subname);
367                 if (! port.name[0]) {
368                         if (info.name[0]) {
369                                 if (ports > 1)
370                                         snprintf(port.name, sizeof(port.name), "%s-%d", info.name, p);
371                                 else
372                                         snprintf(port.name, sizeof(port.name), "%s", info.name);
373                         } else {
374                                 /* last resort */
375                                 if (ports > 1)
376                                         sprintf(port.name, "MIDI %d-%d-%d", card->number, device, p);
377                                 else
378                                         sprintf(port.name, "MIDI %d-%d", card->number, device);
379                         }
380                 }
381                 if ((info.flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
382                         port.capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
383                 if ((info.flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
384                         port.capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
385                 if ((port.capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
386                     info.flags & SNDRV_RAWMIDI_INFO_DUPLEX)
387                         port.capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
388                 port.type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC;
389                 port.midi_channels = 16;
390                 memset(&pcallbacks, 0, sizeof(pcallbacks));
391                 pcallbacks.owner = THIS_MODULE;
392                 pcallbacks.private_data = ms;
393                 pcallbacks.subscribe = midisynth_subscribe;
394                 pcallbacks.unsubscribe = midisynth_unsubscribe;
395                 pcallbacks.use = midisynth_use;
396                 pcallbacks.unuse = midisynth_unuse;
397                 pcallbacks.event_input = event_process_midi;
398                 port.kernel = &pcallbacks;
399                 if (snd_seq_kernel_client_ctl(client->seq_client, SNDRV_SEQ_IOCTL_CREATE_PORT, &port)<0)
400                         goto __nomem;
401                 ms->seq_client = client->seq_client;
402                 ms->seq_port = port.addr.port;
403         }
404         client->ports_per_device[device] = ports;
405         client->ports[device] = msynth;
406         client->num_ports++;
407         if (newclient)
408                 synths[card->number] = client;
409         up(&register_mutex);
410         return 0;       /* success */
411
412       __nomem:
413         if (msynth != NULL) {
414                 for (p = 0; p < ports; p++)
415                         snd_seq_midisynth_delete(&msynth[p]);
416                 kfree(msynth);
417         }
418         if (newclient) {
419                 snd_seq_delete_kernel_client(client->seq_client);
420                 kfree(client);
421         }
422         up(&register_mutex);
423         return -ENOMEM;
424 }
425
426 /* release midi synth port */
427 int
428 snd_seq_midisynth_unregister_port(snd_seq_device_t *dev)
429 {
430         seq_midisynth_client_t *client;
431         seq_midisynth_t *msynth;
432         snd_card_t *card = dev->card;
433         int device = dev->device, p, ports;
434         
435         down(&register_mutex);
436         client = synths[card->number];
437         if (client == NULL || client->ports[device] == NULL) {
438                 up(&register_mutex);
439                 return -ENODEV;
440         }
441         ports = client->ports_per_device[device];
442         client->ports_per_device[device] = 0;
443         msynth = client->ports[device];
444         client->ports[device] = NULL;
445         snd_runtime_check(msynth != NULL || ports <= 0, goto __skip);
446         for (p = 0; p < ports; p++)
447                 snd_seq_midisynth_delete(&msynth[p]);
448         kfree(msynth);
449       __skip:
450         client->num_ports--;
451         if (client->num_ports <= 0) {
452                 snd_seq_delete_kernel_client(client->seq_client);
453                 synths[card->number] = NULL;
454                 kfree(client);
455         }
456         up(&register_mutex);
457         return 0;
458 }
459
460
461 static int __init alsa_seq_midi_init(void)
462 {
463         static snd_seq_dev_ops_t ops = {
464                 snd_seq_midisynth_register_port,
465                 snd_seq_midisynth_unregister_port,
466         };
467         memset(&synths, 0, sizeof(synths));
468         snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH, &ops, 0);
469         return 0;
470 }
471
472 static void __exit alsa_seq_midi_exit(void)
473 {
474         snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_MIDISYNTH);
475 }
476
477 module_init(alsa_seq_midi_init)
478 module_exit(alsa_seq_midi_exit)