GNU Linux-libre 4.19.314-gnu1
[releases.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@perex.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 <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/module.h>
34 #include <linux/mutex.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@perex.cz>");
43 MODULE_DESCRIPTION("Advanced Linux Sound Architecture sequencer MIDI synth.");
44 MODULE_LICENSE("GPL");
45 static int output_buffer_size = PAGE_SIZE;
46 module_param(output_buffer_size, int, 0644);
47 MODULE_PARM_DESC(output_buffer_size, "Output buffer size in bytes.");
48 static int input_buffer_size = PAGE_SIZE;
49 module_param(input_buffer_size, int, 0644);
50 MODULE_PARM_DESC(input_buffer_size, "Input buffer size in bytes.");
51
52 /* data for this midi synth driver */
53 struct seq_midisynth {
54         struct snd_card *card;
55         int device;
56         int subdevice;
57         struct snd_rawmidi_file input_rfile;
58         struct snd_rawmidi_file output_rfile;
59         int seq_client;
60         int seq_port;
61         struct snd_midi_event *parser;
62 };
63
64 struct seq_midisynth_client {
65         int seq_client;
66         int num_ports;
67         int ports_per_device[SNDRV_RAWMIDI_DEVICES];
68         struct seq_midisynth *ports[SNDRV_RAWMIDI_DEVICES];
69 };
70
71 static struct seq_midisynth_client *synths[SNDRV_CARDS];
72 static DEFINE_MUTEX(register_mutex);
73
74 /* handle rawmidi input event (MIDI v1.0 stream) */
75 static void snd_midi_input_event(struct snd_rawmidi_substream *substream)
76 {
77         struct snd_rawmidi_runtime *runtime;
78         struct seq_midisynth *msynth;
79         struct snd_seq_event ev;
80         char buf[16], *pbuf;
81         long res;
82
83         if (substream == NULL)
84                 return;
85         runtime = substream->runtime;
86         msynth = runtime->private_data;
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                         if (!snd_midi_event_encode_byte(msynth->parser,
99                                                         *pbuf++, &ev))
100                                 continue;
101                         ev.source.port = msynth->seq_port;
102                         ev.dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
103                         snd_seq_kernel_client_dispatch(msynth->seq_client, &ev, 1, 0);
104                         /* clear event and reset header */
105                         memset(&ev, 0, sizeof(ev));
106                 }
107         }
108 }
109
110 static int dump_midi(struct snd_rawmidi_substream *substream, const char *buf, int count)
111 {
112         struct snd_rawmidi_runtime *runtime;
113         int tmp;
114
115         if (snd_BUG_ON(!substream || !buf))
116                 return -EINVAL;
117         runtime = substream->runtime;
118         if ((tmp = runtime->avail) < count) {
119                 if (printk_ratelimit())
120                         pr_err("ALSA: seq_midi: MIDI output buffer overrun\n");
121                 return -ENOMEM;
122         }
123         if (snd_rawmidi_kernel_write(substream, buf, count) < count)
124                 return -EINVAL;
125         return 0;
126 }
127
128 /* callback for snd_seq_dump_var_event(), bridging to dump_midi() */
129 static int __dump_midi(void *ptr, void *buf, int count)
130 {
131         return dump_midi(ptr, buf, count);
132 }
133
134 static int event_process_midi(struct snd_seq_event *ev, int direct,
135                               void *private_data, int atomic, int hop)
136 {
137         struct seq_midisynth *msynth = private_data;
138         unsigned char msg[10];  /* buffer for constructing midi messages */
139         struct snd_rawmidi_substream *substream;
140         int len;
141
142         if (snd_BUG_ON(!msynth))
143                 return -EINVAL;
144         substream = msynth->output_rfile.output;
145         if (substream == NULL)
146                 return -ENODEV;
147         if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {        /* special case, to save space */
148                 if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE) {
149                         /* invalid event */
150                         pr_debug("ALSA: seq_midi: invalid sysex event flags = 0x%x\n", ev->flags);
151                         return 0;
152                 }
153                 snd_seq_dump_var_event(ev, __dump_midi, substream);
154                 snd_midi_event_reset_decode(msynth->parser);
155         } else {
156                 if (msynth->parser == NULL)
157                         return -EIO;
158                 len = snd_midi_event_decode(msynth->parser, msg, sizeof(msg), ev);
159                 if (len < 0)
160                         return 0;
161                 if (dump_midi(substream, msg, len) < 0)
162                         snd_midi_event_reset_decode(msynth->parser);
163         }
164         return 0;
165 }
166
167
168 static int snd_seq_midisynth_new(struct seq_midisynth *msynth,
169                                  struct snd_card *card,
170                                  int device,
171                                  int subdevice)
172 {
173         if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &msynth->parser) < 0)
174                 return -ENOMEM;
175         msynth->card = card;
176         msynth->device = device;
177         msynth->subdevice = subdevice;
178         return 0;
179 }
180
181 /* open associated midi device for input */
182 static int midisynth_subscribe(void *private_data, struct snd_seq_port_subscribe *info)
183 {
184         int err;
185         struct seq_midisynth *msynth = private_data;
186         struct snd_rawmidi_runtime *runtime;
187         struct snd_rawmidi_params params;
188
189         /* open midi port */
190         if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
191                                            msynth->subdevice,
192                                            SNDRV_RAWMIDI_LFLG_INPUT,
193                                            &msynth->input_rfile)) < 0) {
194                 pr_debug("ALSA: seq_midi: midi input open failed!!!\n");
195                 return err;
196         }
197         runtime = msynth->input_rfile.input->runtime;
198         memset(&params, 0, sizeof(params));
199         params.avail_min = 1;
200         params.buffer_size = input_buffer_size;
201         if ((err = snd_rawmidi_input_params(msynth->input_rfile.input, &params)) < 0) {
202                 snd_rawmidi_kernel_release(&msynth->input_rfile);
203                 return err;
204         }
205         snd_midi_event_reset_encode(msynth->parser);
206         runtime->event = snd_midi_input_event;
207         runtime->private_data = msynth;
208         snd_rawmidi_kernel_read(msynth->input_rfile.input, NULL, 0);
209         return 0;
210 }
211
212 /* close associated midi device for input */
213 static int midisynth_unsubscribe(void *private_data, struct snd_seq_port_subscribe *info)
214 {
215         int err;
216         struct seq_midisynth *msynth = private_data;
217
218         if (snd_BUG_ON(!msynth->input_rfile.input))
219                 return -EINVAL;
220         err = snd_rawmidi_kernel_release(&msynth->input_rfile);
221         return err;
222 }
223
224 /* open associated midi device for output */
225 static int midisynth_use(void *private_data, struct snd_seq_port_subscribe *info)
226 {
227         int err;
228         struct seq_midisynth *msynth = private_data;
229         struct snd_rawmidi_params params;
230
231         /* open midi port */
232         if ((err = snd_rawmidi_kernel_open(msynth->card, msynth->device,
233                                            msynth->subdevice,
234                                            SNDRV_RAWMIDI_LFLG_OUTPUT,
235                                            &msynth->output_rfile)) < 0) {
236                 pr_debug("ALSA: seq_midi: midi output open failed!!!\n");
237                 return err;
238         }
239         memset(&params, 0, sizeof(params));
240         params.avail_min = 1;
241         params.buffer_size = output_buffer_size;
242         params.no_active_sensing = 1;
243         if ((err = snd_rawmidi_output_params(msynth->output_rfile.output, &params)) < 0) {
244                 snd_rawmidi_kernel_release(&msynth->output_rfile);
245                 return err;
246         }
247         snd_midi_event_reset_decode(msynth->parser);
248         return 0;
249 }
250
251 /* close associated midi device for output */
252 static int midisynth_unuse(void *private_data, struct snd_seq_port_subscribe *info)
253 {
254         struct seq_midisynth *msynth = private_data;
255
256         if (snd_BUG_ON(!msynth->output_rfile.output))
257                 return -EINVAL;
258         snd_rawmidi_drain_output(msynth->output_rfile.output);
259         return snd_rawmidi_kernel_release(&msynth->output_rfile);
260 }
261
262 /* delete given midi synth port */
263 static void snd_seq_midisynth_delete(struct seq_midisynth *msynth)
264 {
265         if (msynth == NULL)
266                 return;
267
268         if (msynth->seq_client > 0) {
269                 /* delete port */
270                 snd_seq_event_port_detach(msynth->seq_client, msynth->seq_port);
271         }
272
273         snd_midi_event_free(msynth->parser);
274 }
275
276 /* register new midi synth port */
277 static int
278 snd_seq_midisynth_probe(struct device *_dev)
279 {
280         struct snd_seq_device *dev = to_seq_dev(_dev);
281         struct seq_midisynth_client *client;
282         struct seq_midisynth *msynth, *ms;
283         struct snd_seq_port_info *port;
284         struct snd_rawmidi_info *info;
285         struct snd_rawmidi *rmidi = dev->private_data;
286         int newclient = 0;
287         unsigned int p, ports;
288         struct snd_seq_port_callback pcallbacks;
289         struct snd_card *card = dev->card;
290         int device = dev->device;
291         unsigned int input_count = 0, output_count = 0;
292
293         if (snd_BUG_ON(!card || device < 0 || device >= SNDRV_RAWMIDI_DEVICES))
294                 return -EINVAL;
295         info = kmalloc(sizeof(*info), GFP_KERNEL);
296         if (! info)
297                 return -ENOMEM;
298         info->device = device;
299         info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
300         info->subdevice = 0;
301         if (snd_rawmidi_info_select(card, info) >= 0)
302                 output_count = info->subdevices_count;
303         info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
304         if (snd_rawmidi_info_select(card, info) >= 0) {
305                 input_count = info->subdevices_count;
306         }
307         ports = output_count;
308         if (ports < input_count)
309                 ports = input_count;
310         if (ports == 0) {
311                 kfree(info);
312                 return -ENODEV;
313         }
314         if (ports > (256 / SNDRV_RAWMIDI_DEVICES))
315                 ports = 256 / SNDRV_RAWMIDI_DEVICES;
316
317         mutex_lock(&register_mutex);
318         client = synths[card->number];
319         if (client == NULL) {
320                 newclient = 1;
321                 client = kzalloc(sizeof(*client), GFP_KERNEL);
322                 if (client == NULL) {
323                         mutex_unlock(&register_mutex);
324                         kfree(info);
325                         return -ENOMEM;
326                 }
327                 client->seq_client =
328                         snd_seq_create_kernel_client(
329                                 card, 0, "%s", card->shortname[0] ?
330                                 (const char *)card->shortname : "External MIDI");
331                 if (client->seq_client < 0) {
332                         kfree(client);
333                         mutex_unlock(&register_mutex);
334                         kfree(info);
335                         return -ENOMEM;
336                 }
337         }
338
339         msynth = kcalloc(ports, sizeof(struct seq_midisynth), GFP_KERNEL);
340         port = kmalloc(sizeof(*port), GFP_KERNEL);
341         if (msynth == NULL || port == NULL)
342                 goto __nomem;
343
344         for (p = 0; p < ports; p++) {
345                 ms = &msynth[p];
346
347                 if (snd_seq_midisynth_new(ms, card, device, p) < 0)
348                         goto __nomem;
349
350                 /* declare port */
351                 memset(port, 0, sizeof(*port));
352                 port->addr.client = client->seq_client;
353                 port->addr.port = device * (256 / SNDRV_RAWMIDI_DEVICES) + p;
354                 port->flags = SNDRV_SEQ_PORT_FLG_GIVEN_PORT;
355                 memset(info, 0, sizeof(*info));
356                 info->device = device;
357                 if (p < output_count)
358                         info->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
359                 else
360                         info->stream = SNDRV_RAWMIDI_STREAM_INPUT;
361                 info->subdevice = p;
362                 if (snd_rawmidi_info_select(card, info) >= 0)
363                         strcpy(port->name, info->subname);
364                 if (! port->name[0]) {
365                         if (info->name[0]) {
366                                 if (ports > 1)
367                                         snprintf(port->name, sizeof(port->name), "%s-%u", info->name, p);
368                                 else
369                                         snprintf(port->name, sizeof(port->name), "%s", info->name);
370                         } else {
371                                 /* last resort */
372                                 if (ports > 1)
373                                         sprintf(port->name, "MIDI %d-%d-%u", card->number, device, p);
374                                 else
375                                         sprintf(port->name, "MIDI %d-%d", card->number, device);
376                         }
377                 }
378                 if ((info->flags & SNDRV_RAWMIDI_INFO_OUTPUT) && p < output_count)
379                         port->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
380                 if ((info->flags & SNDRV_RAWMIDI_INFO_INPUT) && p < input_count)
381                         port->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
382                 if ((port->capability & (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ)) == (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_READ) &&
383                     info->flags & SNDRV_RAWMIDI_INFO_DUPLEX)
384                         port->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
385                 port->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
386                         | SNDRV_SEQ_PORT_TYPE_HARDWARE
387                         | SNDRV_SEQ_PORT_TYPE_PORT;
388                 port->midi_channels = 16;
389                 memset(&pcallbacks, 0, sizeof(pcallbacks));
390                 pcallbacks.owner = THIS_MODULE;
391                 pcallbacks.private_data = ms;
392                 pcallbacks.subscribe = midisynth_subscribe;
393                 pcallbacks.unsubscribe = midisynth_unsubscribe;
394                 pcallbacks.use = midisynth_use;
395                 pcallbacks.unuse = midisynth_unuse;
396                 pcallbacks.event_input = event_process_midi;
397                 port->kernel = &pcallbacks;
398                 if (rmidi->ops && rmidi->ops->get_port_info)
399                         rmidi->ops->get_port_info(rmidi, p, port);
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         mutex_unlock(&register_mutex);
411         kfree(info);
412         kfree(port);
413         return 0;       /* success */
414
415       __nomem:
416         if (msynth != NULL) {
417                 for (p = 0; p < ports; p++)
418                         snd_seq_midisynth_delete(&msynth[p]);
419                 kfree(msynth);
420         }
421         if (newclient) {
422                 snd_seq_delete_kernel_client(client->seq_client);
423                 kfree(client);
424         }
425         kfree(info);
426         kfree(port);
427         mutex_unlock(&register_mutex);
428         return -ENOMEM;
429 }
430
431 /* release midi synth port */
432 static int
433 snd_seq_midisynth_remove(struct device *_dev)
434 {
435         struct snd_seq_device *dev = to_seq_dev(_dev);
436         struct seq_midisynth_client *client;
437         struct seq_midisynth *msynth;
438         struct snd_card *card = dev->card;
439         int device = dev->device, p, ports;
440         
441         mutex_lock(&register_mutex);
442         client = synths[card->number];
443         if (client == NULL || client->ports[device] == NULL) {
444                 mutex_unlock(&register_mutex);
445                 return -ENODEV;
446         }
447         ports = client->ports_per_device[device];
448         client->ports_per_device[device] = 0;
449         msynth = client->ports[device];
450         client->ports[device] = NULL;
451         for (p = 0; p < ports; p++)
452                 snd_seq_midisynth_delete(&msynth[p]);
453         kfree(msynth);
454         client->num_ports--;
455         if (client->num_ports <= 0) {
456                 snd_seq_delete_kernel_client(client->seq_client);
457                 synths[card->number] = NULL;
458                 kfree(client);
459         }
460         mutex_unlock(&register_mutex);
461         return 0;
462 }
463
464 static struct snd_seq_driver seq_midisynth_driver = {
465         .driver = {
466                 .name = KBUILD_MODNAME,
467                 .probe = snd_seq_midisynth_probe,
468                 .remove = snd_seq_midisynth_remove,
469         },
470         .id = SNDRV_SEQ_DEV_ID_MIDISYNTH,
471         .argsize = 0,
472 };
473
474 module_snd_seq_driver(seq_midisynth_driver);