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