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