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 */
snd_virmidi_init_event(struct snd_virmidi * vmidi,struct snd_seq_event * ev)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() */
dump_to_rawmidi(void * ptr,void * buf,int count)67 static int dump_to_rawmidi(void *ptr, void *buf, int count)
68 {
69 return snd_rawmidi_receive(ptr, buf, count);
70 }
71
snd_virmidi_dev_receive_event(struct snd_virmidi_dev * rdev,struct snd_seq_event * ev,bool atomic)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 */
snd_virmidi_event_input(struct snd_seq_event * ev,int direct,void * private_data,int atomic,int hop)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 */
snd_virmidi_input_trigger(struct snd_rawmidi_substream * substream,int up)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 */
snd_vmidi_output_work(struct work_struct * work)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 */
snd_virmidi_output_trigger(struct snd_rawmidi_substream * substream,int up)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 */
snd_virmidi_input_open(struct snd_rawmidi_substream * substream)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 */
snd_virmidi_output_open(struct snd_rawmidi_substream * substream)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 */
snd_virmidi_input_close(struct snd_rawmidi_substream * substream)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 */
snd_virmidi_output_close(struct snd_rawmidi_substream * substream)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 * drain output work queue
274 */
snd_virmidi_output_drain(struct snd_rawmidi_substream * substream)275 static void snd_virmidi_output_drain(struct snd_rawmidi_substream *substream)
276 {
277 struct snd_virmidi *vmidi = substream->runtime->private_data;
278
279 flush_work(&vmidi->output_work);
280 }
281
282 /*
283 * subscribe callback - allow output to rawmidi device
284 */
snd_virmidi_subscribe(void * private_data,struct snd_seq_port_subscribe * info)285 static int snd_virmidi_subscribe(void *private_data,
286 struct snd_seq_port_subscribe *info)
287 {
288 struct snd_virmidi_dev *rdev;
289
290 rdev = private_data;
291 if (!try_module_get(rdev->card->module))
292 return -EFAULT;
293 rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
294 return 0;
295 }
296
297 /*
298 * unsubscribe callback - disallow output to rawmidi device
299 */
snd_virmidi_unsubscribe(void * private_data,struct snd_seq_port_subscribe * info)300 static int snd_virmidi_unsubscribe(void *private_data,
301 struct snd_seq_port_subscribe *info)
302 {
303 struct snd_virmidi_dev *rdev;
304
305 rdev = private_data;
306 rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
307 module_put(rdev->card->module);
308 return 0;
309 }
310
311
312 /*
313 * use callback - allow input to rawmidi device
314 */
snd_virmidi_use(void * private_data,struct snd_seq_port_subscribe * info)315 static int snd_virmidi_use(void *private_data,
316 struct snd_seq_port_subscribe *info)
317 {
318 struct snd_virmidi_dev *rdev;
319
320 rdev = private_data;
321 if (!try_module_get(rdev->card->module))
322 return -EFAULT;
323 rdev->flags |= SNDRV_VIRMIDI_USE;
324 return 0;
325 }
326
327 /*
328 * unuse callback - disallow input to rawmidi device
329 */
snd_virmidi_unuse(void * private_data,struct snd_seq_port_subscribe * info)330 static int snd_virmidi_unuse(void *private_data,
331 struct snd_seq_port_subscribe *info)
332 {
333 struct snd_virmidi_dev *rdev;
334
335 rdev = private_data;
336 rdev->flags &= ~SNDRV_VIRMIDI_USE;
337 module_put(rdev->card->module);
338 return 0;
339 }
340
341
342 /*
343 * Register functions
344 */
345
346 static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
347 .open = snd_virmidi_input_open,
348 .close = snd_virmidi_input_close,
349 .trigger = snd_virmidi_input_trigger,
350 };
351
352 static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
353 .open = snd_virmidi_output_open,
354 .close = snd_virmidi_output_close,
355 .trigger = snd_virmidi_output_trigger,
356 .drain = snd_virmidi_output_drain,
357 };
358
359 /*
360 * create a sequencer client and a port
361 */
snd_virmidi_dev_attach_seq(struct snd_virmidi_dev * rdev)362 static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
363 {
364 int client;
365 struct snd_seq_port_callback pcallbacks;
366 struct snd_seq_port_info *pinfo;
367 int err;
368
369 if (rdev->client >= 0)
370 return 0;
371
372 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
373 if (!pinfo) {
374 err = -ENOMEM;
375 goto __error;
376 }
377
378 client = snd_seq_create_kernel_client(rdev->card, rdev->device,
379 "%s %d-%d", rdev->rmidi->name,
380 rdev->card->number,
381 rdev->device);
382 if (client < 0) {
383 err = client;
384 goto __error;
385 }
386 rdev->client = client;
387
388 /* create a port */
389 pinfo->addr.client = client;
390 sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
391 /* set all capabilities */
392 pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
393 pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
394 pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
395 pinfo->direction = SNDRV_SEQ_PORT_DIR_BIDIRECTION;
396 pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
397 | SNDRV_SEQ_PORT_TYPE_SOFTWARE
398 | SNDRV_SEQ_PORT_TYPE_PORT;
399 pinfo->midi_channels = 16;
400 memset(&pcallbacks, 0, sizeof(pcallbacks));
401 pcallbacks.owner = THIS_MODULE;
402 pcallbacks.private_data = rdev;
403 pcallbacks.subscribe = snd_virmidi_subscribe;
404 pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
405 pcallbacks.use = snd_virmidi_use;
406 pcallbacks.unuse = snd_virmidi_unuse;
407 pcallbacks.event_input = snd_virmidi_event_input;
408 pinfo->kernel = &pcallbacks;
409 err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
410 if (err < 0) {
411 snd_seq_delete_kernel_client(client);
412 rdev->client = -1;
413 goto __error;
414 }
415
416 rdev->port = pinfo->addr.port;
417 err = 0; /* success */
418
419 __error:
420 kfree(pinfo);
421 return err;
422 }
423
424
425 /*
426 * release the sequencer client
427 */
snd_virmidi_dev_detach_seq(struct snd_virmidi_dev * rdev)428 static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
429 {
430 if (rdev->client >= 0) {
431 snd_seq_delete_kernel_client(rdev->client);
432 rdev->client = -1;
433 }
434 }
435
436 /*
437 * register the device
438 */
snd_virmidi_dev_register(struct snd_rawmidi * rmidi)439 static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
440 {
441 struct snd_virmidi_dev *rdev = rmidi->private_data;
442 int err;
443
444 switch (rdev->seq_mode) {
445 case SNDRV_VIRMIDI_SEQ_DISPATCH:
446 err = snd_virmidi_dev_attach_seq(rdev);
447 if (err < 0)
448 return err;
449 break;
450 case SNDRV_VIRMIDI_SEQ_ATTACH:
451 if (rdev->client == 0)
452 return -EINVAL;
453 /* should check presence of port more strictly.. */
454 break;
455 default:
456 pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
457 return -EINVAL;
458 }
459 return 0;
460 }
461
462
463 /*
464 * unregister the device
465 */
snd_virmidi_dev_unregister(struct snd_rawmidi * rmidi)466 static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
467 {
468 struct snd_virmidi_dev *rdev = rmidi->private_data;
469
470 if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
471 snd_virmidi_dev_detach_seq(rdev);
472 return 0;
473 }
474
475 /*
476 *
477 */
478 static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
479 .dev_register = snd_virmidi_dev_register,
480 .dev_unregister = snd_virmidi_dev_unregister,
481 };
482
483 /*
484 * free device
485 */
snd_virmidi_free(struct snd_rawmidi * rmidi)486 static void snd_virmidi_free(struct snd_rawmidi *rmidi)
487 {
488 struct snd_virmidi_dev *rdev = rmidi->private_data;
489 kfree(rdev);
490 }
491
492 /*
493 * create a new device
494 *
495 */
496 /* exported */
snd_virmidi_new(struct snd_card * card,int device,struct snd_rawmidi ** rrmidi)497 int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
498 {
499 struct snd_rawmidi *rmidi;
500 struct snd_virmidi_dev *rdev;
501 int err;
502
503 *rrmidi = NULL;
504 err = snd_rawmidi_new(card, "VirMidi", device,
505 16, /* may be configurable */
506 16, /* may be configurable */
507 &rmidi);
508 if (err < 0)
509 return err;
510 strcpy(rmidi->name, rmidi->id);
511 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
512 if (rdev == NULL) {
513 snd_device_free(card, rmidi);
514 return -ENOMEM;
515 }
516 rdev->card = card;
517 rdev->rmidi = rmidi;
518 rdev->device = device;
519 rdev->client = -1;
520 init_rwsem(&rdev->filelist_sem);
521 rwlock_init(&rdev->filelist_lock);
522 INIT_LIST_HEAD(&rdev->filelist);
523 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
524 rmidi->private_data = rdev;
525 rmidi->private_free = snd_virmidi_free;
526 rmidi->ops = &snd_virmidi_global_ops;
527 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
528 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
529 rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
530 SNDRV_RAWMIDI_INFO_OUTPUT |
531 SNDRV_RAWMIDI_INFO_DUPLEX;
532 *rrmidi = rmidi;
533 return 0;
534 }
535 EXPORT_SYMBOL(snd_virmidi_new);
536