1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * OSS compatible sequencer driver 4 * 5 * synth device handlers 6 * 7 * Copyright (C) 1998,99 Takashi Iwai <[email protected]> 8 */ 9 10 #include "seq_oss_synth.h" 11 #include "seq_oss_midi.h" 12 #include "../seq_lock.h" 13 #include <linux/init.h> 14 #include <linux/module.h> 15 #include <linux/slab.h> 16 #include <linux/nospec.h> 17 18 /* 19 * constants 20 */ 21 #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30 22 #define MAX_SYSEX_BUFLEN 128 23 24 25 /* 26 * definition of synth info records 27 */ 28 29 /* sysex buffer */ 30 struct seq_oss_synth_sysex { 31 int len; 32 int skip; 33 unsigned char buf[MAX_SYSEX_BUFLEN]; 34 }; 35 36 /* synth info */ 37 struct seq_oss_synth { 38 int seq_device; 39 40 /* for synth_info */ 41 int synth_type; 42 int synth_subtype; 43 int nr_voices; 44 45 char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME]; 46 struct snd_seq_oss_callback oper; 47 48 int opened; 49 50 void *private_data; 51 snd_use_lock_t use_lock; 52 }; 53 54 55 /* 56 * device table 57 */ 58 static int max_synth_devs; 59 static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS]; 60 static struct seq_oss_synth midi_synth_dev = { 61 .seq_device = -1, 62 .synth_type = SYNTH_TYPE_MIDI, 63 .synth_subtype = 0, 64 .nr_voices = 16, 65 .name = "MIDI", 66 }; 67 68 static DEFINE_SPINLOCK(register_lock); 69 static DEFINE_MUTEX(sysex_mutex); 70 71 /* 72 * prototypes 73 */ 74 static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev); 75 static void reset_channels(struct seq_oss_synthinfo *info); 76 77 /* 78 * global initialization 79 */ 80 void __init 81 snd_seq_oss_synth_init(void) 82 { 83 snd_use_lock_init(&midi_synth_dev.use_lock); 84 } 85 86 /* 87 * registration of the synth device 88 */ 89 int 90 snd_seq_oss_synth_probe(struct device *_dev) 91 { 92 struct snd_seq_device *dev = to_seq_dev(_dev); 93 int i; 94 struct seq_oss_synth *rec; 95 struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev); 96 unsigned long flags; 97 98 rec = kzalloc(sizeof(*rec), GFP_KERNEL); 99 if (!rec) 100 return -ENOMEM; 101 rec->seq_device = -1; 102 rec->synth_type = reg->type; 103 rec->synth_subtype = reg->subtype; 104 rec->nr_voices = reg->nvoices; 105 rec->oper = reg->oper; 106 rec->private_data = reg->private_data; 107 rec->opened = 0; 108 snd_use_lock_init(&rec->use_lock); 109 110 /* copy and truncate the name of synth device */ 111 strscpy(rec->name, dev->name, sizeof(rec->name)); 112 113 /* registration */ 114 spin_lock_irqsave(®ister_lock, flags); 115 for (i = 0; i < max_synth_devs; i++) { 116 if (synth_devs[i] == NULL) 117 break; 118 } 119 if (i >= max_synth_devs) { 120 if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) { 121 spin_unlock_irqrestore(®ister_lock, flags); 122 pr_err("ALSA: seq_oss: no more synth slot\n"); 123 kfree(rec); 124 return -ENOMEM; 125 } 126 max_synth_devs++; 127 } 128 rec->seq_device = i; 129 synth_devs[i] = rec; 130 spin_unlock_irqrestore(®ister_lock, flags); 131 dev->driver_data = rec; 132 #ifdef SNDRV_OSS_INFO_DEV_SYNTH 133 if (i < SNDRV_CARDS) 134 snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name); 135 #endif 136 return 0; 137 } 138 139 140 int 141 snd_seq_oss_synth_remove(struct device *_dev) 142 { 143 struct snd_seq_device *dev = to_seq_dev(_dev); 144 int index; 145 struct seq_oss_synth *rec = dev->driver_data; 146 unsigned long flags; 147 148 spin_lock_irqsave(®ister_lock, flags); 149 for (index = 0; index < max_synth_devs; index++) { 150 if (synth_devs[index] == rec) 151 break; 152 } 153 if (index >= max_synth_devs) { 154 spin_unlock_irqrestore(®ister_lock, flags); 155 pr_err("ALSA: seq_oss: can't unregister synth\n"); 156 return -EINVAL; 157 } 158 synth_devs[index] = NULL; 159 if (index == max_synth_devs - 1) { 160 for (index--; index >= 0; index--) { 161 if (synth_devs[index]) 162 break; 163 } 164 max_synth_devs = index + 1; 165 } 166 spin_unlock_irqrestore(®ister_lock, flags); 167 #ifdef SNDRV_OSS_INFO_DEV_SYNTH 168 if (rec->seq_device < SNDRV_CARDS) 169 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device); 170 #endif 171 172 snd_use_lock_sync(&rec->use_lock); 173 kfree(rec); 174 175 return 0; 176 } 177 178 179 /* 180 */ 181 static struct seq_oss_synth * 182 get_sdev(int dev) 183 { 184 struct seq_oss_synth *rec; 185 unsigned long flags; 186 187 spin_lock_irqsave(®ister_lock, flags); 188 rec = synth_devs[dev]; 189 if (rec) 190 snd_use_lock_use(&rec->use_lock); 191 spin_unlock_irqrestore(®ister_lock, flags); 192 return rec; 193 } 194 195 196 /* 197 * set up synth tables 198 */ 199 200 void 201 snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp) 202 { 203 int i; 204 struct seq_oss_synth *rec; 205 struct seq_oss_synthinfo *info; 206 207 dp->max_synthdev = max_synth_devs; 208 dp->synth_opened = 0; 209 memset(dp->synths, 0, sizeof(dp->synths)); 210 for (i = 0; i < dp->max_synthdev; i++) { 211 rec = get_sdev(i); 212 if (rec == NULL) 213 continue; 214 if (rec->oper.open == NULL || rec->oper.close == NULL) { 215 snd_use_lock_free(&rec->use_lock); 216 continue; 217 } 218 info = &dp->synths[i]; 219 info->arg.app_index = dp->port; 220 info->arg.file_mode = dp->file_mode; 221 info->arg.seq_mode = dp->seq_mode; 222 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) 223 info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS; 224 else 225 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS; 226 info->opened = 0; 227 if (!try_module_get(rec->oper.owner)) { 228 snd_use_lock_free(&rec->use_lock); 229 continue; 230 } 231 if (rec->oper.open(&info->arg, rec->private_data) < 0) { 232 module_put(rec->oper.owner); 233 snd_use_lock_free(&rec->use_lock); 234 continue; 235 } 236 info->nr_voices = rec->nr_voices; 237 if (info->nr_voices > 0) { 238 info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL); 239 if (!info->ch) { 240 rec->oper.close(&info->arg); 241 module_put(rec->oper.owner); 242 snd_use_lock_free(&rec->use_lock); 243 continue; 244 } 245 reset_channels(info); 246 } 247 info->opened++; 248 rec->opened++; 249 dp->synth_opened++; 250 snd_use_lock_free(&rec->use_lock); 251 } 252 } 253 254 255 /* 256 * set up synth tables for MIDI emulation - /dev/music mode only 257 */ 258 259 void 260 snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp) 261 { 262 int i; 263 264 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) 265 return; 266 267 for (i = 0; i < dp->max_mididev; i++) { 268 struct seq_oss_synthinfo *info; 269 info = &dp->synths[dp->max_synthdev]; 270 if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0) 271 continue; 272 info->arg.app_index = dp->port; 273 info->arg.file_mode = dp->file_mode; 274 info->arg.seq_mode = dp->seq_mode; 275 info->arg.private_data = info; 276 info->is_midi = 1; 277 info->midi_mapped = i; 278 info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS; 279 snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr); 280 info->opened = 1; 281 midi_synth_dev.opened++; 282 dp->max_synthdev++; 283 if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) 284 break; 285 } 286 } 287 288 289 /* 290 * clean up synth tables 291 */ 292 293 void 294 snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp) 295 { 296 int i; 297 struct seq_oss_synth *rec; 298 struct seq_oss_synthinfo *info; 299 300 if (snd_BUG_ON(dp->max_synthdev > SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)) 301 return; 302 for (i = 0; i < dp->max_synthdev; i++) { 303 info = &dp->synths[i]; 304 if (! info->opened) 305 continue; 306 if (info->is_midi) { 307 if (midi_synth_dev.opened > 0) { 308 snd_seq_oss_midi_close(dp, info->midi_mapped); 309 midi_synth_dev.opened--; 310 } 311 } else { 312 rec = get_sdev(i); 313 if (rec == NULL) 314 continue; 315 if (rec->opened > 0) { 316 rec->oper.close(&info->arg); 317 module_put(rec->oper.owner); 318 rec->opened = 0; 319 } 320 snd_use_lock_free(&rec->use_lock); 321 } 322 kfree(info->sysex); 323 info->sysex = NULL; 324 kfree(info->ch); 325 info->ch = NULL; 326 } 327 dp->synth_opened = 0; 328 dp->max_synthdev = 0; 329 } 330 331 static struct seq_oss_synthinfo * 332 get_synthinfo_nospec(struct seq_oss_devinfo *dp, int dev) 333 { 334 if (dev < 0 || dev >= dp->max_synthdev) 335 return NULL; 336 dev = array_index_nospec(dev, SNDRV_SEQ_OSS_MAX_SYNTH_DEVS); 337 return &dp->synths[dev]; 338 } 339 340 /* 341 * return synth device information pointer 342 */ 343 static struct seq_oss_synth * 344 get_synthdev(struct seq_oss_devinfo *dp, int dev) 345 { 346 struct seq_oss_synth *rec; 347 struct seq_oss_synthinfo *info = get_synthinfo_nospec(dp, dev); 348 349 if (!info) 350 return NULL; 351 if (!info->opened) 352 return NULL; 353 if (info->is_midi) { 354 rec = &midi_synth_dev; 355 snd_use_lock_use(&rec->use_lock); 356 } else { 357 rec = get_sdev(dev); 358 if (!rec) 359 return NULL; 360 } 361 if (! rec->opened) { 362 snd_use_lock_free(&rec->use_lock); 363 return NULL; 364 } 365 return rec; 366 } 367 368 369 /* 370 * reset note and velocity on each channel. 371 */ 372 static void 373 reset_channels(struct seq_oss_synthinfo *info) 374 { 375 int i; 376 if (info->ch == NULL || ! info->nr_voices) 377 return; 378 for (i = 0; i < info->nr_voices; i++) { 379 info->ch[i].note = -1; 380 info->ch[i].vel = 0; 381 } 382 } 383 384 385 /* 386 * reset synth device: 387 * call reset callback. if no callback is defined, send a heartbeat 388 * event to the corresponding port. 389 */ 390 void 391 snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev) 392 { 393 struct seq_oss_synth *rec; 394 struct seq_oss_synthinfo *info; 395 396 info = get_synthinfo_nospec(dp, dev); 397 if (!info || !info->opened) 398 return; 399 if (info->sysex) 400 info->sysex->len = 0; /* reset sysex */ 401 reset_channels(info); 402 if (info->is_midi) { 403 if (midi_synth_dev.opened <= 0) 404 return; 405 snd_seq_oss_midi_reset(dp, info->midi_mapped); 406 /* reopen the device */ 407 snd_seq_oss_midi_close(dp, dev); 408 if (snd_seq_oss_midi_open(dp, info->midi_mapped, 409 dp->file_mode) < 0) { 410 midi_synth_dev.opened--; 411 info->opened = 0; 412 kfree(info->sysex); 413 info->sysex = NULL; 414 kfree(info->ch); 415 info->ch = NULL; 416 } 417 return; 418 } 419 420 rec = get_sdev(dev); 421 if (rec == NULL) 422 return; 423 if (rec->oper.reset) { 424 rec->oper.reset(&info->arg); 425 } else { 426 struct snd_seq_event ev; 427 memset(&ev, 0, sizeof(ev)); 428 snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client, 429 info->arg.addr.port); 430 ev.type = SNDRV_SEQ_EVENT_RESET; 431 snd_seq_oss_dispatch(dp, &ev, 0, 0); 432 } 433 snd_use_lock_free(&rec->use_lock); 434 } 435 436 437 /* 438 * load a patch record: 439 * call load_patch callback function 440 */ 441 int 442 snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt, 443 const char __user *buf, int p, int c) 444 { 445 struct seq_oss_synth *rec; 446 struct seq_oss_synthinfo *info; 447 int rc; 448 449 info = get_synthinfo_nospec(dp, dev); 450 if (!info) 451 return -ENXIO; 452 453 if (info->is_midi) 454 return 0; 455 rec = get_synthdev(dp, dev); 456 if (!rec) 457 return -ENXIO; 458 459 if (rec->oper.load_patch == NULL) 460 rc = -ENXIO; 461 else 462 rc = rec->oper.load_patch(&info->arg, fmt, buf, p, c); 463 snd_use_lock_free(&rec->use_lock); 464 return rc; 465 } 466 467 /* 468 * check if the device is valid synth device and return the synth info 469 */ 470 struct seq_oss_synthinfo * 471 snd_seq_oss_synth_info(struct seq_oss_devinfo *dp, int dev) 472 { 473 struct seq_oss_synth *rec; 474 475 rec = get_synthdev(dp, dev); 476 if (rec) { 477 snd_use_lock_free(&rec->use_lock); 478 return get_synthinfo_nospec(dp, dev); 479 } 480 return NULL; 481 } 482 483 484 /* 485 * receive OSS 6 byte sysex packet: 486 * the full sysex message will be sent if it reaches to the end of data 487 * (0xff). 488 */ 489 int 490 snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev) 491 { 492 int i, send; 493 unsigned char *dest; 494 struct seq_oss_synth_sysex *sysex; 495 struct seq_oss_synthinfo *info; 496 497 info = snd_seq_oss_synth_info(dp, dev); 498 if (!info) 499 return -ENXIO; 500 501 guard(mutex)(&sysex_mutex); 502 sysex = info->sysex; 503 if (sysex == NULL) { 504 sysex = kzalloc(sizeof(*sysex), GFP_KERNEL); 505 if (sysex == NULL) 506 return -ENOMEM; 507 info->sysex = sysex; 508 } 509 510 send = 0; 511 dest = sysex->buf + sysex->len; 512 /* copy 6 byte packet to the buffer */ 513 for (i = 0; i < 6; i++) { 514 if (buf[i] == 0xff) { 515 send = 1; 516 break; 517 } 518 dest[i] = buf[i]; 519 sysex->len++; 520 if (sysex->len >= MAX_SYSEX_BUFLEN) { 521 sysex->len = 0; 522 sysex->skip = 1; 523 break; 524 } 525 } 526 527 if (sysex->len && send) { 528 if (sysex->skip) { 529 sysex->skip = 0; 530 sysex->len = 0; 531 return -EINVAL; /* skip */ 532 } 533 /* copy the data to event record and send it */ 534 ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE; 535 if (snd_seq_oss_synth_addr(dp, dev, ev)) 536 return -EINVAL; 537 ev->data.ext.len = sysex->len; 538 ev->data.ext.ptr = sysex->buf; 539 sysex->len = 0; 540 return 0; 541 } 542 543 return -EINVAL; /* skip */ 544 } 545 546 /* 547 * fill the event source/destination addresses 548 */ 549 int 550 snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev) 551 { 552 struct seq_oss_synthinfo *info = snd_seq_oss_synth_info(dp, dev); 553 554 if (!info) 555 return -EINVAL; 556 snd_seq_oss_fill_addr(dp, ev, info->arg.addr.client, 557 info->arg.addr.port); 558 return 0; 559 } 560 561 562 /* 563 * OSS compatible ioctl 564 */ 565 int 566 snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr) 567 { 568 struct seq_oss_synth *rec; 569 struct seq_oss_synthinfo *info; 570 int rc; 571 572 info = get_synthinfo_nospec(dp, dev); 573 if (!info || info->is_midi) 574 return -ENXIO; 575 rec = get_synthdev(dp, dev); 576 if (!rec) 577 return -ENXIO; 578 if (rec->oper.ioctl == NULL) 579 rc = -ENXIO; 580 else 581 rc = rec->oper.ioctl(&info->arg, cmd, addr); 582 snd_use_lock_free(&rec->use_lock); 583 return rc; 584 } 585 586 587 /* 588 * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME 589 */ 590 int 591 snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev) 592 { 593 struct seq_oss_synthinfo *info; 594 595 info = snd_seq_oss_synth_info(dp, dev); 596 if (!info || info->is_midi) 597 return -ENXIO; 598 ev->type = SNDRV_SEQ_EVENT_OSS; 599 memcpy(ev->data.raw8.d, data, 8); 600 return snd_seq_oss_synth_addr(dp, dev, ev); 601 } 602 603 604 /* 605 * create OSS compatible synth_info record 606 */ 607 int 608 snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf) 609 { 610 struct seq_oss_synth *rec; 611 struct seq_oss_synthinfo *info = get_synthinfo_nospec(dp, dev); 612 613 if (!info) 614 return -ENXIO; 615 616 if (info->is_midi) { 617 struct midi_info minf; 618 if (snd_seq_oss_midi_make_info(dp, info->midi_mapped, &minf)) 619 return -ENXIO; 620 inf->synth_type = SYNTH_TYPE_MIDI; 621 inf->synth_subtype = 0; 622 inf->nr_voices = 16; 623 inf->device = dev; 624 strscpy(inf->name, minf.name, sizeof(inf->name)); 625 } else { 626 rec = get_synthdev(dp, dev); 627 if (!rec) 628 return -ENXIO; 629 inf->synth_type = rec->synth_type; 630 inf->synth_subtype = rec->synth_subtype; 631 inf->nr_voices = rec->nr_voices; 632 inf->device = dev; 633 strscpy(inf->name, rec->name, sizeof(inf->name)); 634 snd_use_lock_free(&rec->use_lock); 635 } 636 return 0; 637 } 638 639 640 #ifdef CONFIG_SND_PROC_FS 641 /* 642 * proc interface 643 */ 644 void 645 snd_seq_oss_synth_info_read(struct snd_info_buffer *buf) 646 { 647 int i; 648 struct seq_oss_synth *rec; 649 650 snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs); 651 for (i = 0; i < max_synth_devs; i++) { 652 snd_iprintf(buf, "\nsynth %d: ", i); 653 rec = get_sdev(i); 654 if (rec == NULL) { 655 snd_iprintf(buf, "*empty*\n"); 656 continue; 657 } 658 snd_iprintf(buf, "[%s]\n", rec->name); 659 snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n", 660 rec->synth_type, rec->synth_subtype, 661 rec->nr_voices); 662 snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n", 663 enabled_str((long)rec->oper.ioctl), 664 enabled_str((long)rec->oper.load_patch)); 665 snd_use_lock_free(&rec->use_lock); 666 } 667 } 668 #endif /* CONFIG_SND_PROC_FS */ 669