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 */ 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 */ 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 */ 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 */ 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 */ 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 while (!snd_rawmidi_transmit_empty(substream)) 153 snd_rawmidi_transmit_ack(substream, 1); 154 return; 155 } 156 157 while (READ_ONCE(vmidi->trigger)) { 158 if (snd_rawmidi_transmit(substream, &input, 1) != 1) 159 break; 160 if (!snd_midi_event_encode_byte(vmidi->parser, input, 161 &vmidi->event)) 162 continue; 163 if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) { 164 ret = snd_seq_kernel_client_dispatch(vmidi->client, 165 &vmidi->event, 166 false, 0); 167 vmidi->event.type = SNDRV_SEQ_EVENT_NONE; 168 if (ret < 0) 169 break; 170 } 171 /* rawmidi input might be huge, allow to have a break */ 172 cond_resched(); 173 } 174 } 175 176 /* 177 * trigger rawmidi stream for output 178 */ 179 static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) 180 { 181 struct snd_virmidi *vmidi = substream->runtime->private_data; 182 183 WRITE_ONCE(vmidi->trigger, !!up); 184 if (up) 185 queue_work(system_highpri_wq, &vmidi->output_work); 186 } 187 188 /* 189 * open rawmidi handle for input 190 */ 191 static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream) 192 { 193 struct snd_virmidi_dev *rdev = substream->rmidi->private_data; 194 struct snd_rawmidi_runtime *runtime = substream->runtime; 195 struct snd_virmidi *vmidi; 196 197 vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL); 198 if (vmidi == NULL) 199 return -ENOMEM; 200 vmidi->substream = substream; 201 if (snd_midi_event_new(0, &vmidi->parser) < 0) { 202 kfree(vmidi); 203 return -ENOMEM; 204 } 205 vmidi->seq_mode = rdev->seq_mode; 206 vmidi->client = rdev->client; 207 vmidi->port = rdev->port; 208 runtime->private_data = vmidi; 209 down_write(&rdev->filelist_sem); 210 write_lock_irq(&rdev->filelist_lock); 211 list_add_tail(&vmidi->list, &rdev->filelist); 212 write_unlock_irq(&rdev->filelist_lock); 213 up_write(&rdev->filelist_sem); 214 vmidi->rdev = rdev; 215 return 0; 216 } 217 218 /* 219 * open rawmidi handle for output 220 */ 221 static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream) 222 { 223 struct snd_virmidi_dev *rdev = substream->rmidi->private_data; 224 struct snd_rawmidi_runtime *runtime = substream->runtime; 225 struct snd_virmidi *vmidi; 226 227 vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL); 228 if (vmidi == NULL) 229 return -ENOMEM; 230 vmidi->substream = substream; 231 if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) { 232 kfree(vmidi); 233 return -ENOMEM; 234 } 235 vmidi->seq_mode = rdev->seq_mode; 236 vmidi->client = rdev->client; 237 vmidi->port = rdev->port; 238 snd_virmidi_init_event(vmidi, &vmidi->event); 239 vmidi->rdev = rdev; 240 INIT_WORK(&vmidi->output_work, snd_vmidi_output_work); 241 runtime->private_data = vmidi; 242 return 0; 243 } 244 245 /* 246 * close rawmidi handle for input 247 */ 248 static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream) 249 { 250 struct snd_virmidi_dev *rdev = substream->rmidi->private_data; 251 struct snd_virmidi *vmidi = substream->runtime->private_data; 252 253 down_write(&rdev->filelist_sem); 254 write_lock_irq(&rdev->filelist_lock); 255 list_del(&vmidi->list); 256 write_unlock_irq(&rdev->filelist_lock); 257 up_write(&rdev->filelist_sem); 258 snd_midi_event_free(vmidi->parser); 259 substream->runtime->private_data = NULL; 260 kfree(vmidi); 261 return 0; 262 } 263 264 /* 265 * close rawmidi handle for output 266 */ 267 static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream) 268 { 269 struct snd_virmidi *vmidi = substream->runtime->private_data; 270 271 WRITE_ONCE(vmidi->trigger, false); /* to be sure */ 272 cancel_work_sync(&vmidi->output_work); 273 snd_midi_event_free(vmidi->parser); 274 substream->runtime->private_data = NULL; 275 kfree(vmidi); 276 return 0; 277 } 278 279 /* 280 * subscribe callback - allow output to rawmidi device 281 */ 282 static int snd_virmidi_subscribe(void *private_data, 283 struct snd_seq_port_subscribe *info) 284 { 285 struct snd_virmidi_dev *rdev; 286 287 rdev = private_data; 288 if (!try_module_get(rdev->card->module)) 289 return -EFAULT; 290 rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE; 291 return 0; 292 } 293 294 /* 295 * unsubscribe callback - disallow output to rawmidi device 296 */ 297 static int snd_virmidi_unsubscribe(void *private_data, 298 struct snd_seq_port_subscribe *info) 299 { 300 struct snd_virmidi_dev *rdev; 301 302 rdev = private_data; 303 rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE; 304 module_put(rdev->card->module); 305 return 0; 306 } 307 308 309 /* 310 * use callback - allow input to rawmidi device 311 */ 312 static int snd_virmidi_use(void *private_data, 313 struct snd_seq_port_subscribe *info) 314 { 315 struct snd_virmidi_dev *rdev; 316 317 rdev = private_data; 318 if (!try_module_get(rdev->card->module)) 319 return -EFAULT; 320 rdev->flags |= SNDRV_VIRMIDI_USE; 321 return 0; 322 } 323 324 /* 325 * unuse callback - disallow input to rawmidi device 326 */ 327 static int snd_virmidi_unuse(void *private_data, 328 struct snd_seq_port_subscribe *info) 329 { 330 struct snd_virmidi_dev *rdev; 331 332 rdev = private_data; 333 rdev->flags &= ~SNDRV_VIRMIDI_USE; 334 module_put(rdev->card->module); 335 return 0; 336 } 337 338 339 /* 340 * Register functions 341 */ 342 343 static const struct snd_rawmidi_ops snd_virmidi_input_ops = { 344 .open = snd_virmidi_input_open, 345 .close = snd_virmidi_input_close, 346 .trigger = snd_virmidi_input_trigger, 347 }; 348 349 static const struct snd_rawmidi_ops snd_virmidi_output_ops = { 350 .open = snd_virmidi_output_open, 351 .close = snd_virmidi_output_close, 352 .trigger = snd_virmidi_output_trigger, 353 }; 354 355 /* 356 * create a sequencer client and a port 357 */ 358 static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev) 359 { 360 int client; 361 struct snd_seq_port_callback pcallbacks; 362 struct snd_seq_port_info *pinfo; 363 int err; 364 365 if (rdev->client >= 0) 366 return 0; 367 368 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL); 369 if (!pinfo) { 370 err = -ENOMEM; 371 goto __error; 372 } 373 374 client = snd_seq_create_kernel_client(rdev->card, rdev->device, 375 "%s %d-%d", rdev->rmidi->name, 376 rdev->card->number, 377 rdev->device); 378 if (client < 0) { 379 err = client; 380 goto __error; 381 } 382 rdev->client = client; 383 384 /* create a port */ 385 pinfo->addr.client = client; 386 sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device); 387 /* set all capabilities */ 388 pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE; 389 pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ; 390 pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX; 391 pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC 392 | SNDRV_SEQ_PORT_TYPE_SOFTWARE 393 | SNDRV_SEQ_PORT_TYPE_PORT; 394 pinfo->midi_channels = 16; 395 memset(&pcallbacks, 0, sizeof(pcallbacks)); 396 pcallbacks.owner = THIS_MODULE; 397 pcallbacks.private_data = rdev; 398 pcallbacks.subscribe = snd_virmidi_subscribe; 399 pcallbacks.unsubscribe = snd_virmidi_unsubscribe; 400 pcallbacks.use = snd_virmidi_use; 401 pcallbacks.unuse = snd_virmidi_unuse; 402 pcallbacks.event_input = snd_virmidi_event_input; 403 pinfo->kernel = &pcallbacks; 404 err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo); 405 if (err < 0) { 406 snd_seq_delete_kernel_client(client); 407 rdev->client = -1; 408 goto __error; 409 } 410 411 rdev->port = pinfo->addr.port; 412 err = 0; /* success */ 413 414 __error: 415 kfree(pinfo); 416 return err; 417 } 418 419 420 /* 421 * release the sequencer client 422 */ 423 static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev) 424 { 425 if (rdev->client >= 0) { 426 snd_seq_delete_kernel_client(rdev->client); 427 rdev->client = -1; 428 } 429 } 430 431 /* 432 * register the device 433 */ 434 static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi) 435 { 436 struct snd_virmidi_dev *rdev = rmidi->private_data; 437 int err; 438 439 switch (rdev->seq_mode) { 440 case SNDRV_VIRMIDI_SEQ_DISPATCH: 441 err = snd_virmidi_dev_attach_seq(rdev); 442 if (err < 0) 443 return err; 444 break; 445 case SNDRV_VIRMIDI_SEQ_ATTACH: 446 if (rdev->client == 0) 447 return -EINVAL; 448 /* should check presence of port more strictly.. */ 449 break; 450 default: 451 pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode); 452 return -EINVAL; 453 } 454 return 0; 455 } 456 457 458 /* 459 * unregister the device 460 */ 461 static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi) 462 { 463 struct snd_virmidi_dev *rdev = rmidi->private_data; 464 465 if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH) 466 snd_virmidi_dev_detach_seq(rdev); 467 return 0; 468 } 469 470 /* 471 * 472 */ 473 static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = { 474 .dev_register = snd_virmidi_dev_register, 475 .dev_unregister = snd_virmidi_dev_unregister, 476 }; 477 478 /* 479 * free device 480 */ 481 static void snd_virmidi_free(struct snd_rawmidi *rmidi) 482 { 483 struct snd_virmidi_dev *rdev = rmidi->private_data; 484 kfree(rdev); 485 } 486 487 /* 488 * create a new device 489 * 490 */ 491 /* exported */ 492 int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi) 493 { 494 struct snd_rawmidi *rmidi; 495 struct snd_virmidi_dev *rdev; 496 int err; 497 498 *rrmidi = NULL; 499 if ((err = snd_rawmidi_new(card, "VirMidi", device, 500 16, /* may be configurable */ 501 16, /* may be configurable */ 502 &rmidi)) < 0) 503 return err; 504 strcpy(rmidi->name, rmidi->id); 505 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL); 506 if (rdev == NULL) { 507 snd_device_free(card, rmidi); 508 return -ENOMEM; 509 } 510 rdev->card = card; 511 rdev->rmidi = rmidi; 512 rdev->device = device; 513 rdev->client = -1; 514 init_rwsem(&rdev->filelist_sem); 515 rwlock_init(&rdev->filelist_lock); 516 INIT_LIST_HEAD(&rdev->filelist); 517 rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH; 518 rmidi->private_data = rdev; 519 rmidi->private_free = snd_virmidi_free; 520 rmidi->ops = &snd_virmidi_global_ops; 521 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops); 522 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops); 523 rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT | 524 SNDRV_RAWMIDI_INFO_OUTPUT | 525 SNDRV_RAWMIDI_INFO_DUPLEX; 526 *rrmidi = rmidi; 527 return 0; 528 } 529 EXPORT_SYMBOL(snd_virmidi_new); 530