GNU Linux-libre 5.10.217-gnu1
[releases.git] / sound / core / seq / seq_virmidi.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Virtual Raw MIDI client on Sequencer
4  *
5  *  Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>,
6  *                        Jaroslav Kysela <perex@perex.cz>
7  */
8
9 /*
10  * Virtual Raw MIDI client
11  *
12  * The virtual rawmidi client is a sequencer client which associate
13  * a rawmidi device file.  The created rawmidi device file can be
14  * accessed as a normal raw midi, but its MIDI source and destination
15  * are arbitrary.  For example, a user-client software synth connected
16  * to this port can be used as a normal midi device as well.
17  *
18  * The virtual rawmidi device accepts also multiple opens.  Each file
19  * has its own input buffer, so that no conflict would occur.  The drain
20  * of input/output buffer acts only to the local buffer.
21  *
22  */
23
24 #include <linux/init.h>
25 #include <linux/wait.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <sound/core.h>
29 #include <sound/rawmidi.h>
30 #include <sound/info.h>
31 #include <sound/control.h>
32 #include <sound/minors.h>
33 #include <sound/seq_kernel.h>
34 #include <sound/seq_midi_event.h>
35 #include <sound/seq_virmidi.h>
36
37 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
38 MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
39 MODULE_LICENSE("GPL");
40
41 /*
42  * initialize an event record
43  */
44 static void snd_virmidi_init_event(struct snd_virmidi *vmidi,
45                                    struct snd_seq_event *ev)
46 {
47         memset(ev, 0, sizeof(*ev));
48         ev->source.port = vmidi->port;
49         switch (vmidi->seq_mode) {
50         case SNDRV_VIRMIDI_SEQ_DISPATCH:
51                 ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
52                 break;
53         case SNDRV_VIRMIDI_SEQ_ATTACH:
54                 /* FIXME: source and destination are same - not good.. */
55                 ev->dest.client = vmidi->client;
56                 ev->dest.port = vmidi->port;
57                 break;
58         }
59         ev->type = SNDRV_SEQ_EVENT_NONE;
60 }
61
62 /*
63  * decode input event and put to read buffer of each opened file
64  */
65
66 /* callback for snd_seq_dump_var_event(), bridging to snd_rawmidi_receive() */
67 static int dump_to_rawmidi(void *ptr, void *buf, int count)
68 {
69         return snd_rawmidi_receive(ptr, buf, count);
70 }
71
72 static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
73                                          struct snd_seq_event *ev,
74                                          bool atomic)
75 {
76         struct snd_virmidi *vmidi;
77         unsigned char msg[4];
78         int len;
79
80         if (atomic)
81                 read_lock(&rdev->filelist_lock);
82         else
83                 down_read(&rdev->filelist_sem);
84         list_for_each_entry(vmidi, &rdev->filelist, list) {
85                 if (!READ_ONCE(vmidi->trigger))
86                         continue;
87                 if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
88                         if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
89                                 continue;
90                         snd_seq_dump_var_event(ev, dump_to_rawmidi, vmidi->substream);
91                         snd_midi_event_reset_decode(vmidi->parser);
92                 } else {
93                         len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
94                         if (len > 0)
95                                 snd_rawmidi_receive(vmidi->substream, msg, len);
96                 }
97         }
98         if (atomic)
99                 read_unlock(&rdev->filelist_lock);
100         else
101                 up_read(&rdev->filelist_sem);
102
103         return 0;
104 }
105
106 /*
107  * event handler of virmidi port
108  */
109 static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
110                                    void *private_data, int atomic, int hop)
111 {
112         struct snd_virmidi_dev *rdev;
113
114         rdev = private_data;
115         if (!(rdev->flags & SNDRV_VIRMIDI_USE))
116                 return 0; /* ignored */
117         return snd_virmidi_dev_receive_event(rdev, ev, atomic);
118 }
119
120 /*
121  * trigger rawmidi stream for input
122  */
123 static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
124 {
125         struct snd_virmidi *vmidi = substream->runtime->private_data;
126
127         WRITE_ONCE(vmidi->trigger, !!up);
128 }
129
130 /* process rawmidi bytes and send events;
131  * we need no lock here for vmidi->event since it's handled only in this work
132  */
133 static void snd_vmidi_output_work(struct work_struct *work)
134 {
135         struct snd_virmidi *vmidi;
136         struct snd_rawmidi_substream *substream;
137         unsigned char input;
138         int ret;
139
140         vmidi = container_of(work, struct snd_virmidi, output_work);
141         substream = vmidi->substream;
142
143         /* discard the outputs in dispatch mode unless subscribed */
144         if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
145             !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
146                 snd_rawmidi_proceed(substream);
147                 return;
148         }
149
150         while (READ_ONCE(vmidi->trigger)) {
151                 if (snd_rawmidi_transmit(substream, &input, 1) != 1)
152                         break;
153                 if (!snd_midi_event_encode_byte(vmidi->parser, input,
154                                                 &vmidi->event))
155                         continue;
156                 if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
157                         ret = snd_seq_kernel_client_dispatch(vmidi->client,
158                                                              &vmidi->event,
159                                                              false, 0);
160                         vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
161                         if (ret < 0)
162                                 break;
163                 }
164                 /* rawmidi input might be huge, allow to have a break */
165                 cond_resched();
166         }
167 }
168
169 /*
170  * trigger rawmidi stream for output
171  */
172 static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
173 {
174         struct snd_virmidi *vmidi = substream->runtime->private_data;
175
176         WRITE_ONCE(vmidi->trigger, !!up);
177         if (up)
178                 queue_work(system_highpri_wq, &vmidi->output_work);
179 }
180
181 /*
182  * open rawmidi handle for input
183  */
184 static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
185 {
186         struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
187         struct snd_rawmidi_runtime *runtime = substream->runtime;
188         struct snd_virmidi *vmidi;
189
190         vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
191         if (vmidi == NULL)
192                 return -ENOMEM;
193         vmidi->substream = substream;
194         if (snd_midi_event_new(0, &vmidi->parser) < 0) {
195                 kfree(vmidi);
196                 return -ENOMEM;
197         }
198         vmidi->seq_mode = rdev->seq_mode;
199         vmidi->client = rdev->client;
200         vmidi->port = rdev->port;       
201         runtime->private_data = vmidi;
202         down_write(&rdev->filelist_sem);
203         write_lock_irq(&rdev->filelist_lock);
204         list_add_tail(&vmidi->list, &rdev->filelist);
205         write_unlock_irq(&rdev->filelist_lock);
206         up_write(&rdev->filelist_sem);
207         vmidi->rdev = rdev;
208         return 0;
209 }
210
211 /*
212  * open rawmidi handle for output
213  */
214 static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
215 {
216         struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
217         struct snd_rawmidi_runtime *runtime = substream->runtime;
218         struct snd_virmidi *vmidi;
219
220         vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
221         if (vmidi == NULL)
222                 return -ENOMEM;
223         vmidi->substream = substream;
224         if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
225                 kfree(vmidi);
226                 return -ENOMEM;
227         }
228         vmidi->seq_mode = rdev->seq_mode;
229         vmidi->client = rdev->client;
230         vmidi->port = rdev->port;
231         snd_virmidi_init_event(vmidi, &vmidi->event);
232         vmidi->rdev = rdev;
233         INIT_WORK(&vmidi->output_work, snd_vmidi_output_work);
234         runtime->private_data = vmidi;
235         return 0;
236 }
237
238 /*
239  * close rawmidi handle for input
240  */
241 static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
242 {
243         struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
244         struct snd_virmidi *vmidi = substream->runtime->private_data;
245
246         down_write(&rdev->filelist_sem);
247         write_lock_irq(&rdev->filelist_lock);
248         list_del(&vmidi->list);
249         write_unlock_irq(&rdev->filelist_lock);
250         up_write(&rdev->filelist_sem);
251         snd_midi_event_free(vmidi->parser);
252         substream->runtime->private_data = NULL;
253         kfree(vmidi);
254         return 0;
255 }
256
257 /*
258  * close rawmidi handle for output
259  */
260 static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
261 {
262         struct snd_virmidi *vmidi = substream->runtime->private_data;
263
264         WRITE_ONCE(vmidi->trigger, false); /* to be sure */
265         cancel_work_sync(&vmidi->output_work);
266         snd_midi_event_free(vmidi->parser);
267         substream->runtime->private_data = NULL;
268         kfree(vmidi);
269         return 0;
270 }
271
272 /*
273  * subscribe callback - allow output to rawmidi device
274  */
275 static int snd_virmidi_subscribe(void *private_data,
276                                  struct snd_seq_port_subscribe *info)
277 {
278         struct snd_virmidi_dev *rdev;
279
280         rdev = private_data;
281         if (!try_module_get(rdev->card->module))
282                 return -EFAULT;
283         rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
284         return 0;
285 }
286
287 /*
288  * unsubscribe callback - disallow output to rawmidi device
289  */
290 static int snd_virmidi_unsubscribe(void *private_data,
291                                    struct snd_seq_port_subscribe *info)
292 {
293         struct snd_virmidi_dev *rdev;
294
295         rdev = private_data;
296         rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
297         module_put(rdev->card->module);
298         return 0;
299 }
300
301
302 /*
303  * use callback - allow input to rawmidi device
304  */
305 static int snd_virmidi_use(void *private_data,
306                            struct snd_seq_port_subscribe *info)
307 {
308         struct snd_virmidi_dev *rdev;
309
310         rdev = private_data;
311         if (!try_module_get(rdev->card->module))
312                 return -EFAULT;
313         rdev->flags |= SNDRV_VIRMIDI_USE;
314         return 0;
315 }
316
317 /*
318  * unuse callback - disallow input to rawmidi device
319  */
320 static int snd_virmidi_unuse(void *private_data,
321                              struct snd_seq_port_subscribe *info)
322 {
323         struct snd_virmidi_dev *rdev;
324
325         rdev = private_data;
326         rdev->flags &= ~SNDRV_VIRMIDI_USE;
327         module_put(rdev->card->module);
328         return 0;
329 }
330
331
332 /*
333  *  Register functions
334  */
335
336 static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
337         .open = snd_virmidi_input_open,
338         .close = snd_virmidi_input_close,
339         .trigger = snd_virmidi_input_trigger,
340 };
341
342 static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
343         .open = snd_virmidi_output_open,
344         .close = snd_virmidi_output_close,
345         .trigger = snd_virmidi_output_trigger,
346 };
347
348 /*
349  * create a sequencer client and a port
350  */
351 static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
352 {
353         int client;
354         struct snd_seq_port_callback pcallbacks;
355         struct snd_seq_port_info *pinfo;
356         int err;
357
358         if (rdev->client >= 0)
359                 return 0;
360
361         pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
362         if (!pinfo) {
363                 err = -ENOMEM;
364                 goto __error;
365         }
366
367         client = snd_seq_create_kernel_client(rdev->card, rdev->device,
368                                               "%s %d-%d", rdev->rmidi->name,
369                                               rdev->card->number,
370                                               rdev->device);
371         if (client < 0) {
372                 err = client;
373                 goto __error;
374         }
375         rdev->client = client;
376
377         /* create a port */
378         pinfo->addr.client = client;
379         sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
380         /* set all capabilities */
381         pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
382         pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
383         pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
384         pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
385                 | SNDRV_SEQ_PORT_TYPE_SOFTWARE
386                 | SNDRV_SEQ_PORT_TYPE_PORT;
387         pinfo->midi_channels = 16;
388         memset(&pcallbacks, 0, sizeof(pcallbacks));
389         pcallbacks.owner = THIS_MODULE;
390         pcallbacks.private_data = rdev;
391         pcallbacks.subscribe = snd_virmidi_subscribe;
392         pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
393         pcallbacks.use = snd_virmidi_use;
394         pcallbacks.unuse = snd_virmidi_unuse;
395         pcallbacks.event_input = snd_virmidi_event_input;
396         pinfo->kernel = &pcallbacks;
397         err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
398         if (err < 0) {
399                 snd_seq_delete_kernel_client(client);
400                 rdev->client = -1;
401                 goto __error;
402         }
403
404         rdev->port = pinfo->addr.port;
405         err = 0; /* success */
406
407  __error:
408         kfree(pinfo);
409         return err;
410 }
411
412
413 /*
414  * release the sequencer client
415  */
416 static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
417 {
418         if (rdev->client >= 0) {
419                 snd_seq_delete_kernel_client(rdev->client);
420                 rdev->client = -1;
421         }
422 }
423
424 /*
425  * register the device
426  */
427 static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
428 {
429         struct snd_virmidi_dev *rdev = rmidi->private_data;
430         int err;
431
432         switch (rdev->seq_mode) {
433         case SNDRV_VIRMIDI_SEQ_DISPATCH:
434                 err = snd_virmidi_dev_attach_seq(rdev);
435                 if (err < 0)
436                         return err;
437                 break;
438         case SNDRV_VIRMIDI_SEQ_ATTACH:
439                 if (rdev->client == 0)
440                         return -EINVAL;
441                 /* should check presence of port more strictly.. */
442                 break;
443         default:
444                 pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
445                 return -EINVAL;
446         }
447         return 0;
448 }
449
450
451 /*
452  * unregister the device
453  */
454 static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
455 {
456         struct snd_virmidi_dev *rdev = rmidi->private_data;
457
458         if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
459                 snd_virmidi_dev_detach_seq(rdev);
460         return 0;
461 }
462
463 /*
464  *
465  */
466 static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
467         .dev_register = snd_virmidi_dev_register,
468         .dev_unregister = snd_virmidi_dev_unregister,
469 };
470
471 /*
472  * free device
473  */
474 static void snd_virmidi_free(struct snd_rawmidi *rmidi)
475 {
476         struct snd_virmidi_dev *rdev = rmidi->private_data;
477         kfree(rdev);
478 }
479
480 /*
481  * create a new device
482  *
483  */
484 /* exported */
485 int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
486 {
487         struct snd_rawmidi *rmidi;
488         struct snd_virmidi_dev *rdev;
489         int err;
490         
491         *rrmidi = NULL;
492         if ((err = snd_rawmidi_new(card, "VirMidi", device,
493                                    16,  /* may be configurable */
494                                    16,  /* may be configurable */
495                                    &rmidi)) < 0)
496                 return err;
497         strcpy(rmidi->name, rmidi->id);
498         rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
499         if (rdev == NULL) {
500                 snd_device_free(card, rmidi);
501                 return -ENOMEM;
502         }
503         rdev->card = card;
504         rdev->rmidi = rmidi;
505         rdev->device = device;
506         rdev->client = -1;
507         init_rwsem(&rdev->filelist_sem);
508         rwlock_init(&rdev->filelist_lock);
509         INIT_LIST_HEAD(&rdev->filelist);
510         rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
511         rmidi->private_data = rdev;
512         rmidi->private_free = snd_virmidi_free;
513         rmidi->ops = &snd_virmidi_global_ops;
514         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
515         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
516         rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
517                             SNDRV_RAWMIDI_INFO_OUTPUT |
518                             SNDRV_RAWMIDI_INFO_DUPLEX;
519         *rrmidi = rmidi;
520         return 0;
521 }
522 EXPORT_SYMBOL(snd_virmidi_new);