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