1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Abstract layer for MIDI v1.0 stream 4 * Copyright (c) by Jaroslav Kysela <perex@perex.cz> 5 */ 6 7 #include <sound/core.h> 8 #include <linux/major.h> 9 #include <linux/init.h> 10 #include <linux/sched/signal.h> 11 #include <linux/slab.h> 12 #include <linux/time.h> 13 #include <linux/wait.h> 14 #include <linux/mutex.h> 15 #include <linux/module.h> 16 #include <linux/delay.h> 17 #include <linux/mm.h> 18 #include <linux/nospec.h> 19 #include <sound/rawmidi.h> 20 #include <sound/info.h> 21 #include <sound/control.h> 22 #include <sound/minors.h> 23 #include <sound/initval.h> 24 #include <sound/ump.h> 25 26 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); 27 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA."); 28 MODULE_LICENSE("GPL"); 29 30 #ifdef CONFIG_SND_OSSEMUL 31 static int midi_map[SNDRV_CARDS]; 32 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1}; 33 module_param_array(midi_map, int, NULL, 0444); 34 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device."); 35 module_param_array(amidi_map, int, NULL, 0444); 36 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device."); 37 #endif /* CONFIG_SND_OSSEMUL */ 38 39 static int snd_rawmidi_dev_free(struct snd_device *device); 40 static int snd_rawmidi_dev_register(struct snd_device *device); 41 static int snd_rawmidi_dev_disconnect(struct snd_device *device); 42 43 static LIST_HEAD(snd_rawmidi_devices); 44 static DEFINE_MUTEX(register_mutex); 45 46 #define rmidi_err(rmidi, fmt, args...) \ 47 dev_err((rmidi)->dev, fmt, ##args) 48 #define rmidi_warn(rmidi, fmt, args...) \ 49 dev_warn((rmidi)->dev, fmt, ##args) 50 #define rmidi_dbg(rmidi, fmt, args...) \ 51 dev_dbg((rmidi)->dev, fmt, ##args) 52 53 struct snd_rawmidi_status32 { 54 s32 stream; 55 s32 tstamp_sec; /* Timestamp */ 56 s32 tstamp_nsec; 57 u32 avail; /* available bytes */ 58 u32 xruns; /* count of overruns since last status (in bytes) */ 59 unsigned char reserved[16]; /* reserved for future use */ 60 }; 61 62 #define SNDRV_RAWMIDI_IOCTL_STATUS32 _IOWR('W', 0x20, struct snd_rawmidi_status32) 63 64 struct snd_rawmidi_status64 { 65 int stream; 66 u8 rsvd[4]; /* alignment */ 67 s64 tstamp_sec; /* Timestamp */ 68 s64 tstamp_nsec; 69 size_t avail; /* available bytes */ 70 size_t xruns; /* count of overruns since last status (in bytes) */ 71 unsigned char reserved[16]; /* reserved for future use */ 72 }; 73 74 #define SNDRV_RAWMIDI_IOCTL_STATUS64 _IOWR('W', 0x20, struct snd_rawmidi_status64) 75 76 #define rawmidi_is_ump(rmidi) \ 77 (IS_ENABLED(CONFIG_SND_UMP) && ((rmidi)->info_flags & SNDRV_RAWMIDI_INFO_UMP)) 78 79 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device) 80 { 81 struct snd_rawmidi *rawmidi; 82 83 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list) 84 if (rawmidi->card == card && rawmidi->device == device) 85 return rawmidi; 86 return NULL; 87 } 88 89 static inline unsigned short snd_rawmidi_file_flags(struct file *file) 90 { 91 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) { 92 case FMODE_WRITE: 93 return SNDRV_RAWMIDI_LFLG_OUTPUT; 94 case FMODE_READ: 95 return SNDRV_RAWMIDI_LFLG_INPUT; 96 default: 97 return SNDRV_RAWMIDI_LFLG_OPEN; 98 } 99 } 100 101 static inline bool __snd_rawmidi_ready(struct snd_rawmidi_runtime *runtime) 102 { 103 return runtime->avail >= runtime->avail_min; 104 } 105 106 static bool snd_rawmidi_ready(struct snd_rawmidi_substream *substream) 107 { 108 guard(spinlock_irqsave)(&substream->lock); 109 return __snd_rawmidi_ready(substream->runtime); 110 } 111 112 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream, 113 size_t count) 114 { 115 struct snd_rawmidi_runtime *runtime = substream->runtime; 116 117 return runtime->avail >= runtime->avail_min && 118 (!substream->append || runtime->avail >= count); 119 } 120 121 static void snd_rawmidi_input_event_work(struct work_struct *work) 122 { 123 struct snd_rawmidi_runtime *runtime = 124 container_of(work, struct snd_rawmidi_runtime, event_work); 125 126 if (runtime->event) 127 runtime->event(runtime->substream); 128 } 129 130 /* buffer refcount management: call with substream->lock held */ 131 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime) 132 { 133 runtime->buffer_ref++; 134 } 135 136 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime) 137 { 138 runtime->buffer_ref--; 139 } 140 141 static void snd_rawmidi_buffer_ref_sync(struct snd_rawmidi_substream *substream) 142 { 143 int loop = HZ; 144 145 spin_lock_irq(&substream->lock); 146 while (substream->runtime->buffer_ref) { 147 spin_unlock_irq(&substream->lock); 148 if (!--loop) { 149 rmidi_err(substream->rmidi, "Buffer ref sync timeout\n"); 150 return; 151 } 152 schedule_timeout_uninterruptible(1); 153 spin_lock_irq(&substream->lock); 154 } 155 spin_unlock_irq(&substream->lock); 156 } 157 158 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream) 159 { 160 struct snd_rawmidi_runtime *runtime; 161 162 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); 163 if (!runtime) 164 return -ENOMEM; 165 runtime->substream = substream; 166 init_waitqueue_head(&runtime->sleep); 167 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work); 168 runtime->event = NULL; 169 runtime->buffer_size = PAGE_SIZE; 170 runtime->avail_min = 1; 171 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT) 172 runtime->avail = 0; 173 else 174 runtime->avail = runtime->buffer_size; 175 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL); 176 if (!runtime->buffer) { 177 kfree(runtime); 178 return -ENOMEM; 179 } 180 runtime->appl_ptr = runtime->hw_ptr = 0; 181 substream->runtime = runtime; 182 if (rawmidi_is_ump(substream->rmidi)) 183 runtime->align = 3; 184 return 0; 185 } 186 187 /* get the current alignment (either 0 or 3) */ 188 static inline int get_align(struct snd_rawmidi_runtime *runtime) 189 { 190 if (IS_ENABLED(CONFIG_SND_UMP)) 191 return runtime->align; 192 else 193 return 0; 194 } 195 196 /* get the trimmed size with the current alignment */ 197 #define get_aligned_size(runtime, size) ((size) & ~get_align(runtime)) 198 199 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream) 200 { 201 struct snd_rawmidi_runtime *runtime = substream->runtime; 202 203 kvfree(runtime->buffer); 204 kfree(runtime); 205 substream->runtime = NULL; 206 return 0; 207 } 208 209 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) 210 { 211 if (!substream->opened) 212 return; 213 substream->ops->trigger(substream, up); 214 } 215 216 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) 217 { 218 if (!substream->opened) 219 return; 220 substream->ops->trigger(substream, up); 221 if (!up) 222 cancel_work_sync(&substream->runtime->event_work); 223 } 224 225 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime, 226 bool is_input) 227 { 228 runtime->drain = 0; 229 runtime->appl_ptr = runtime->hw_ptr = 0; 230 runtime->avail = is_input ? 0 : runtime->buffer_size; 231 } 232 233 static void reset_runtime_ptrs(struct snd_rawmidi_substream *substream, 234 bool is_input) 235 { 236 guard(spinlock_irqsave)(&substream->lock); 237 if (substream->opened && substream->runtime) 238 __reset_runtime_ptrs(substream->runtime, is_input); 239 } 240 241 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream) 242 { 243 snd_rawmidi_output_trigger(substream, 0); 244 reset_runtime_ptrs(substream, false); 245 return 0; 246 } 247 EXPORT_SYMBOL(snd_rawmidi_drop_output); 248 249 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream) 250 { 251 int err = 0; 252 long timeout; 253 struct snd_rawmidi_runtime *runtime; 254 255 scoped_guard(spinlock_irq, &substream->lock) { 256 runtime = substream->runtime; 257 if (!substream->opened || !runtime || !runtime->buffer) 258 return -EINVAL; 259 snd_rawmidi_buffer_ref(runtime); 260 runtime->drain = 1; 261 } 262 263 timeout = wait_event_interruptible_timeout(runtime->sleep, 264 (runtime->avail >= runtime->buffer_size), 265 10*HZ); 266 267 scoped_guard(spinlock_irq, &substream->lock) { 268 if (signal_pending(current)) 269 err = -ERESTARTSYS; 270 if (runtime->avail < runtime->buffer_size && !timeout) { 271 rmidi_warn(substream->rmidi, 272 "rawmidi drain error (avail = %li, buffer_size = %li)\n", 273 (long)runtime->avail, (long)runtime->buffer_size); 274 err = -EIO; 275 } 276 runtime->drain = 0; 277 } 278 279 if (err != -ERESTARTSYS) { 280 /* we need wait a while to make sure that Tx FIFOs are empty */ 281 if (substream->ops->drain) 282 substream->ops->drain(substream); 283 else 284 msleep(50); 285 snd_rawmidi_drop_output(substream); 286 } 287 288 scoped_guard(spinlock_irq, &substream->lock) 289 snd_rawmidi_buffer_unref(runtime); 290 291 return err; 292 } 293 EXPORT_SYMBOL(snd_rawmidi_drain_output); 294 295 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream) 296 { 297 snd_rawmidi_input_trigger(substream, 0); 298 reset_runtime_ptrs(substream, true); 299 return 0; 300 } 301 EXPORT_SYMBOL(snd_rawmidi_drain_input); 302 303 /* look for an available substream for the given stream direction; 304 * if a specific subdevice is given, try to assign it 305 */ 306 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice, 307 int stream, int mode, 308 struct snd_rawmidi_substream **sub_ret) 309 { 310 struct snd_rawmidi_substream *substream; 311 struct snd_rawmidi_str *s = &rmidi->streams[stream]; 312 static const unsigned int info_flags[2] = { 313 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT, 314 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT, 315 }; 316 317 if (!(rmidi->info_flags & info_flags[stream])) 318 return -ENXIO; 319 if (subdevice >= 0 && subdevice >= s->substream_count) 320 return -ENODEV; 321 322 list_for_each_entry(substream, &s->substreams, list) { 323 if (substream->opened) { 324 if (stream == SNDRV_RAWMIDI_STREAM_INPUT || 325 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) || 326 !substream->append) 327 continue; 328 } 329 if (subdevice < 0 || subdevice == substream->number) { 330 *sub_ret = substream; 331 return 0; 332 } 333 } 334 return -EAGAIN; 335 } 336 337 /* open and do ref-counting for the given substream */ 338 static int open_substream(struct snd_rawmidi *rmidi, 339 struct snd_rawmidi_substream *substream, 340 int mode) 341 { 342 int err; 343 344 if (substream->use_count == 0) { 345 err = snd_rawmidi_runtime_create(substream); 346 if (err < 0) 347 return err; 348 err = substream->ops->open(substream); 349 if (err < 0) { 350 snd_rawmidi_runtime_free(substream); 351 return err; 352 } 353 guard(spinlock_irq)(&substream->lock); 354 substream->opened = 1; 355 substream->active_sensing = 0; 356 if (mode & SNDRV_RAWMIDI_LFLG_APPEND) 357 substream->append = 1; 358 substream->pid = get_pid(task_pid(current)); 359 rmidi->streams[substream->stream].substream_opened++; 360 } 361 substream->use_count++; 362 return 0; 363 } 364 365 static void close_substream(struct snd_rawmidi *rmidi, 366 struct snd_rawmidi_substream *substream, 367 int cleanup); 368 369 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode, 370 struct snd_rawmidi_file *rfile) 371 { 372 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL; 373 int err; 374 375 rfile->input = rfile->output = NULL; 376 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) { 377 err = assign_substream(rmidi, subdevice, 378 SNDRV_RAWMIDI_STREAM_INPUT, 379 mode, &sinput); 380 if (err < 0) 381 return err; 382 } 383 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) { 384 err = assign_substream(rmidi, subdevice, 385 SNDRV_RAWMIDI_STREAM_OUTPUT, 386 mode, &soutput); 387 if (err < 0) 388 return err; 389 } 390 391 if (sinput) { 392 err = open_substream(rmidi, sinput, mode); 393 if (err < 0) 394 return err; 395 } 396 if (soutput) { 397 err = open_substream(rmidi, soutput, mode); 398 if (err < 0) { 399 if (sinput) 400 close_substream(rmidi, sinput, 0); 401 return err; 402 } 403 } 404 405 rfile->rmidi = rmidi; 406 rfile->input = sinput; 407 rfile->output = soutput; 408 return 0; 409 } 410 411 /* called from sound/core/seq/seq_midi.c */ 412 int snd_rawmidi_kernel_open(struct snd_rawmidi *rmidi, int subdevice, 413 int mode, struct snd_rawmidi_file *rfile) 414 { 415 int err; 416 417 if (snd_BUG_ON(!rfile)) 418 return -EINVAL; 419 if (!try_module_get(rmidi->card->module)) 420 return -ENXIO; 421 422 guard(mutex)(&rmidi->open_mutex); 423 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile); 424 if (err < 0) 425 module_put(rmidi->card->module); 426 return err; 427 } 428 EXPORT_SYMBOL(snd_rawmidi_kernel_open); 429 430 static int snd_rawmidi_open(struct inode *inode, struct file *file) 431 { 432 int maj = imajor(inode); 433 struct snd_card *card; 434 int subdevice; 435 unsigned short fflags; 436 int err; 437 struct snd_rawmidi *rmidi; 438 struct snd_rawmidi_file *rawmidi_file = NULL; 439 wait_queue_entry_t wait; 440 441 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK)) 442 return -EINVAL; /* invalid combination */ 443 444 err = stream_open(inode, file); 445 if (err < 0) 446 return err; 447 448 if (maj == snd_major) { 449 rmidi = snd_lookup_minor_data(iminor(inode), 450 SNDRV_DEVICE_TYPE_RAWMIDI); 451 #ifdef CONFIG_SND_OSSEMUL 452 } else if (maj == SOUND_MAJOR) { 453 rmidi = snd_lookup_oss_minor_data(iminor(inode), 454 SNDRV_OSS_DEVICE_TYPE_MIDI); 455 #endif 456 } else 457 return -ENXIO; 458 459 if (rmidi == NULL) 460 return -ENODEV; 461 462 if (!try_module_get(rmidi->card->module)) { 463 snd_card_unref(rmidi->card); 464 return -ENXIO; 465 } 466 467 mutex_lock(&rmidi->open_mutex); 468 card = rmidi->card; 469 err = snd_card_file_add(card, file); 470 if (err < 0) 471 goto __error_card; 472 fflags = snd_rawmidi_file_flags(file); 473 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */ 474 fflags |= SNDRV_RAWMIDI_LFLG_APPEND; 475 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL); 476 if (rawmidi_file == NULL) { 477 err = -ENOMEM; 478 goto __error; 479 } 480 rawmidi_file->user_pversion = 0; 481 init_waitqueue_entry(&wait, current); 482 add_wait_queue(&rmidi->open_wait, &wait); 483 while (1) { 484 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI); 485 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file); 486 if (err >= 0) 487 break; 488 if (err == -EAGAIN) { 489 if (file->f_flags & O_NONBLOCK) { 490 err = -EBUSY; 491 break; 492 } 493 } else 494 break; 495 set_current_state(TASK_INTERRUPTIBLE); 496 mutex_unlock(&rmidi->open_mutex); 497 schedule(); 498 mutex_lock(&rmidi->open_mutex); 499 if (rmidi->card->shutdown) { 500 err = -ENODEV; 501 break; 502 } 503 if (signal_pending(current)) { 504 err = -ERESTARTSYS; 505 break; 506 } 507 } 508 remove_wait_queue(&rmidi->open_wait, &wait); 509 if (err < 0) { 510 kfree(rawmidi_file); 511 goto __error; 512 } 513 #ifdef CONFIG_SND_OSSEMUL 514 if (rawmidi_file->input && rawmidi_file->input->runtime) 515 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR); 516 if (rawmidi_file->output && rawmidi_file->output->runtime) 517 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR); 518 #endif 519 file->private_data = rawmidi_file; 520 mutex_unlock(&rmidi->open_mutex); 521 snd_card_unref(rmidi->card); 522 return 0; 523 524 __error: 525 snd_card_file_remove(card, file); 526 __error_card: 527 mutex_unlock(&rmidi->open_mutex); 528 module_put(rmidi->card->module); 529 snd_card_unref(rmidi->card); 530 return err; 531 } 532 533 static void close_substream(struct snd_rawmidi *rmidi, 534 struct snd_rawmidi_substream *substream, 535 int cleanup) 536 { 537 if (--substream->use_count) 538 return; 539 540 if (cleanup) { 541 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT) 542 snd_rawmidi_input_trigger(substream, 0); 543 else { 544 if (substream->active_sensing) { 545 unsigned char buf = 0xfe; 546 /* sending single active sensing message 547 * to shut the device up 548 */ 549 snd_rawmidi_kernel_write(substream, &buf, 1); 550 } 551 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS) 552 snd_rawmidi_output_trigger(substream, 0); 553 } 554 snd_rawmidi_buffer_ref_sync(substream); 555 } 556 scoped_guard(spinlock_irq, &substream->lock) { 557 substream->opened = 0; 558 substream->append = 0; 559 } 560 substream->ops->close(substream); 561 if (substream->runtime->private_free) 562 substream->runtime->private_free(substream); 563 snd_rawmidi_runtime_free(substream); 564 put_pid(substream->pid); 565 substream->pid = NULL; 566 rmidi->streams[substream->stream].substream_opened--; 567 } 568 569 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile) 570 { 571 struct snd_rawmidi *rmidi; 572 573 rmidi = rfile->rmidi; 574 guard(mutex)(&rmidi->open_mutex); 575 if (rfile->input) { 576 close_substream(rmidi, rfile->input, 1); 577 rfile->input = NULL; 578 } 579 if (rfile->output) { 580 close_substream(rmidi, rfile->output, 1); 581 rfile->output = NULL; 582 } 583 rfile->rmidi = NULL; 584 wake_up(&rmidi->open_wait); 585 } 586 587 /* called from sound/core/seq/seq_midi.c */ 588 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile) 589 { 590 struct snd_rawmidi *rmidi; 591 592 if (snd_BUG_ON(!rfile)) 593 return -ENXIO; 594 595 rmidi = rfile->rmidi; 596 rawmidi_release_priv(rfile); 597 module_put(rmidi->card->module); 598 return 0; 599 } 600 EXPORT_SYMBOL(snd_rawmidi_kernel_release); 601 602 static int snd_rawmidi_release(struct inode *inode, struct file *file) 603 { 604 struct snd_rawmidi_file *rfile; 605 struct snd_rawmidi *rmidi; 606 struct module *module; 607 608 rfile = file->private_data; 609 rmidi = rfile->rmidi; 610 rawmidi_release_priv(rfile); 611 kfree(rfile); 612 module = rmidi->card->module; 613 snd_card_file_remove(rmidi->card, file); 614 module_put(module); 615 return 0; 616 } 617 618 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream, 619 struct snd_rawmidi_info *info) 620 { 621 struct snd_rawmidi *rmidi; 622 623 if (substream == NULL) 624 return -ENODEV; 625 rmidi = substream->rmidi; 626 memset(info, 0, sizeof(*info)); 627 info->card = rmidi->card->number; 628 info->device = rmidi->device; 629 info->subdevice = substream->number; 630 info->stream = substream->stream; 631 info->flags = rmidi->info_flags; 632 if (substream->inactive) 633 info->flags |= SNDRV_RAWMIDI_INFO_STREAM_INACTIVE; 634 strcpy(info->id, rmidi->id); 635 strcpy(info->name, rmidi->name); 636 strcpy(info->subname, substream->name); 637 info->subdevices_count = substream->pstr->substream_count; 638 info->subdevices_avail = (substream->pstr->substream_count - 639 substream->pstr->substream_opened); 640 info->tied_device = rmidi->tied_device; 641 return 0; 642 } 643 644 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream, 645 struct snd_rawmidi_info __user *_info) 646 { 647 struct snd_rawmidi_info info; 648 int err; 649 650 err = snd_rawmidi_info(substream, &info); 651 if (err < 0) 652 return err; 653 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info))) 654 return -EFAULT; 655 return 0; 656 } 657 658 static int __snd_rawmidi_info_select(struct snd_card *card, 659 struct snd_rawmidi_info *info) 660 { 661 struct snd_rawmidi *rmidi; 662 struct snd_rawmidi_str *pstr; 663 struct snd_rawmidi_substream *substream; 664 665 rmidi = snd_rawmidi_search(card, info->device); 666 if (!rmidi) 667 return -ENXIO; 668 if (info->stream < 0 || info->stream > 1) 669 return -EINVAL; 670 info->stream = array_index_nospec(info->stream, 2); 671 pstr = &rmidi->streams[info->stream]; 672 if (pstr->substream_count == 0) 673 return -ENOENT; 674 if (info->subdevice >= pstr->substream_count) 675 return -ENXIO; 676 list_for_each_entry(substream, &pstr->substreams, list) { 677 if ((unsigned int)substream->number == info->subdevice) 678 return snd_rawmidi_info(substream, info); 679 } 680 return -ENXIO; 681 } 682 683 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info) 684 { 685 guard(mutex)(®ister_mutex); 686 return __snd_rawmidi_info_select(card, info); 687 } 688 EXPORT_SYMBOL(snd_rawmidi_info_select); 689 690 static int snd_rawmidi_info_select_user(struct snd_card *card, 691 struct snd_rawmidi_info __user *_info) 692 { 693 int err; 694 struct snd_rawmidi_info info; 695 696 if (get_user(info.device, &_info->device)) 697 return -EFAULT; 698 if (get_user(info.stream, &_info->stream)) 699 return -EFAULT; 700 if (get_user(info.subdevice, &_info->subdevice)) 701 return -EFAULT; 702 err = snd_rawmidi_info_select(card, &info); 703 if (err < 0) 704 return err; 705 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info))) 706 return -EFAULT; 707 return 0; 708 } 709 710 static int resize_runtime_buffer(struct snd_rawmidi_substream *substream, 711 struct snd_rawmidi_params *params, 712 bool is_input) 713 { 714 struct snd_rawmidi_runtime *runtime = substream->runtime; 715 char *newbuf, *oldbuf; 716 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK; 717 718 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) 719 return -EINVAL; 720 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP && (params->buffer_size & 0x1f) != 0) 721 return -EINVAL; 722 if (params->avail_min < 1 || params->avail_min > params->buffer_size) 723 return -EINVAL; 724 if (params->buffer_size & get_align(runtime)) 725 return -EINVAL; 726 if (params->buffer_size != runtime->buffer_size) { 727 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL); 728 if (!newbuf) 729 return -ENOMEM; 730 spin_lock_irq(&substream->lock); 731 if (runtime->buffer_ref) { 732 spin_unlock_irq(&substream->lock); 733 kvfree(newbuf); 734 return -EBUSY; 735 } 736 oldbuf = runtime->buffer; 737 runtime->buffer = newbuf; 738 runtime->buffer_size = params->buffer_size; 739 __reset_runtime_ptrs(runtime, is_input); 740 spin_unlock_irq(&substream->lock); 741 kvfree(oldbuf); 742 } 743 runtime->avail_min = params->avail_min; 744 return 0; 745 } 746 747 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream, 748 struct snd_rawmidi_params *params) 749 { 750 int err; 751 752 snd_rawmidi_drain_output(substream); 753 guard(mutex)(&substream->rmidi->open_mutex); 754 if (substream->append && substream->use_count > 1) 755 return -EBUSY; 756 err = resize_runtime_buffer(substream, params, false); 757 if (!err) 758 substream->active_sensing = !params->no_active_sensing; 759 return err; 760 } 761 EXPORT_SYMBOL(snd_rawmidi_output_params); 762 763 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream, 764 struct snd_rawmidi_params *params) 765 { 766 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK; 767 unsigned int clock_type = params->mode & SNDRV_RAWMIDI_MODE_CLOCK_MASK; 768 int err; 769 770 snd_rawmidi_drain_input(substream); 771 guard(mutex)(&substream->rmidi->open_mutex); 772 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_NONE && clock_type != SNDRV_RAWMIDI_MODE_CLOCK_NONE) 773 err = -EINVAL; 774 else if (clock_type > SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW) 775 err = -EINVAL; 776 else if (framing > SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) 777 err = -EINVAL; 778 else 779 err = resize_runtime_buffer(substream, params, true); 780 781 if (!err) { 782 substream->framing = framing; 783 substream->clock_type = clock_type; 784 } 785 return 0; 786 } 787 EXPORT_SYMBOL(snd_rawmidi_input_params); 788 789 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream, 790 struct snd_rawmidi_status64 *status) 791 { 792 struct snd_rawmidi_runtime *runtime = substream->runtime; 793 794 memset(status, 0, sizeof(*status)); 795 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT; 796 guard(spinlock_irq)(&substream->lock); 797 status->avail = runtime->avail; 798 return 0; 799 } 800 801 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream, 802 struct snd_rawmidi_status64 *status) 803 { 804 struct snd_rawmidi_runtime *runtime = substream->runtime; 805 806 memset(status, 0, sizeof(*status)); 807 status->stream = SNDRV_RAWMIDI_STREAM_INPUT; 808 guard(spinlock_irq)(&substream->lock); 809 status->avail = runtime->avail; 810 status->xruns = runtime->xruns; 811 runtime->xruns = 0; 812 return 0; 813 } 814 815 static int snd_rawmidi_ioctl_status32(struct snd_rawmidi_file *rfile, 816 struct snd_rawmidi_status32 __user *argp) 817 { 818 int err = 0; 819 struct snd_rawmidi_status32 __user *status = argp; 820 struct snd_rawmidi_status32 status32; 821 struct snd_rawmidi_status64 status64; 822 823 if (copy_from_user(&status32, argp, 824 sizeof(struct snd_rawmidi_status32))) 825 return -EFAULT; 826 827 switch (status32.stream) { 828 case SNDRV_RAWMIDI_STREAM_OUTPUT: 829 if (rfile->output == NULL) 830 return -EINVAL; 831 err = snd_rawmidi_output_status(rfile->output, &status64); 832 break; 833 case SNDRV_RAWMIDI_STREAM_INPUT: 834 if (rfile->input == NULL) 835 return -EINVAL; 836 err = snd_rawmidi_input_status(rfile->input, &status64); 837 break; 838 default: 839 return -EINVAL; 840 } 841 if (err < 0) 842 return err; 843 844 status32 = (struct snd_rawmidi_status32) { 845 .stream = status64.stream, 846 .tstamp_sec = status64.tstamp_sec, 847 .tstamp_nsec = status64.tstamp_nsec, 848 .avail = status64.avail, 849 .xruns = status64.xruns, 850 }; 851 852 if (copy_to_user(status, &status32, sizeof(*status))) 853 return -EFAULT; 854 855 return 0; 856 } 857 858 static int snd_rawmidi_ioctl_status64(struct snd_rawmidi_file *rfile, 859 struct snd_rawmidi_status64 __user *argp) 860 { 861 int err = 0; 862 struct snd_rawmidi_status64 status; 863 864 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status64))) 865 return -EFAULT; 866 867 switch (status.stream) { 868 case SNDRV_RAWMIDI_STREAM_OUTPUT: 869 if (rfile->output == NULL) 870 return -EINVAL; 871 err = snd_rawmidi_output_status(rfile->output, &status); 872 break; 873 case SNDRV_RAWMIDI_STREAM_INPUT: 874 if (rfile->input == NULL) 875 return -EINVAL; 876 err = snd_rawmidi_input_status(rfile->input, &status); 877 break; 878 default: 879 return -EINVAL; 880 } 881 if (err < 0) 882 return err; 883 if (copy_to_user(argp, &status, 884 sizeof(struct snd_rawmidi_status64))) 885 return -EFAULT; 886 return 0; 887 } 888 889 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 890 { 891 struct snd_rawmidi_file *rfile; 892 struct snd_rawmidi *rmidi; 893 void __user *argp = (void __user *)arg; 894 895 rfile = file->private_data; 896 if (((cmd >> 8) & 0xff) != 'W') 897 return -ENOTTY; 898 switch (cmd) { 899 case SNDRV_RAWMIDI_IOCTL_PVERSION: 900 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0; 901 case SNDRV_RAWMIDI_IOCTL_INFO: 902 { 903 int stream; 904 struct snd_rawmidi_info __user *info = argp; 905 906 if (get_user(stream, &info->stream)) 907 return -EFAULT; 908 switch (stream) { 909 case SNDRV_RAWMIDI_STREAM_INPUT: 910 return snd_rawmidi_info_user(rfile->input, info); 911 case SNDRV_RAWMIDI_STREAM_OUTPUT: 912 return snd_rawmidi_info_user(rfile->output, info); 913 default: 914 return -EINVAL; 915 } 916 } 917 case SNDRV_RAWMIDI_IOCTL_USER_PVERSION: 918 if (get_user(rfile->user_pversion, (unsigned int __user *)arg)) 919 return -EFAULT; 920 return 0; 921 922 case SNDRV_RAWMIDI_IOCTL_PARAMS: 923 { 924 struct snd_rawmidi_params params; 925 926 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params))) 927 return -EFAULT; 928 if (rfile->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 2)) { 929 params.mode = 0; 930 memset(params.reserved, 0, sizeof(params.reserved)); 931 } 932 switch (params.stream) { 933 case SNDRV_RAWMIDI_STREAM_OUTPUT: 934 if (rfile->output == NULL) 935 return -EINVAL; 936 return snd_rawmidi_output_params(rfile->output, ¶ms); 937 case SNDRV_RAWMIDI_STREAM_INPUT: 938 if (rfile->input == NULL) 939 return -EINVAL; 940 return snd_rawmidi_input_params(rfile->input, ¶ms); 941 default: 942 return -EINVAL; 943 } 944 } 945 case SNDRV_RAWMIDI_IOCTL_STATUS32: 946 return snd_rawmidi_ioctl_status32(rfile, argp); 947 case SNDRV_RAWMIDI_IOCTL_STATUS64: 948 return snd_rawmidi_ioctl_status64(rfile, argp); 949 case SNDRV_RAWMIDI_IOCTL_DROP: 950 { 951 int val; 952 953 if (get_user(val, (int __user *) argp)) 954 return -EFAULT; 955 switch (val) { 956 case SNDRV_RAWMIDI_STREAM_OUTPUT: 957 if (rfile->output == NULL) 958 return -EINVAL; 959 return snd_rawmidi_drop_output(rfile->output); 960 default: 961 return -EINVAL; 962 } 963 } 964 case SNDRV_RAWMIDI_IOCTL_DRAIN: 965 { 966 int val; 967 968 if (get_user(val, (int __user *) argp)) 969 return -EFAULT; 970 switch (val) { 971 case SNDRV_RAWMIDI_STREAM_OUTPUT: 972 if (rfile->output == NULL) 973 return -EINVAL; 974 return snd_rawmidi_drain_output(rfile->output); 975 case SNDRV_RAWMIDI_STREAM_INPUT: 976 if (rfile->input == NULL) 977 return -EINVAL; 978 return snd_rawmidi_drain_input(rfile->input); 979 default: 980 return -EINVAL; 981 } 982 } 983 default: 984 rmidi = rfile->rmidi; 985 if (rmidi->ops && rmidi->ops->ioctl) 986 return rmidi->ops->ioctl(rmidi, cmd, argp); 987 rmidi_dbg(rmidi, "rawmidi: unknown command = 0x%x\n", cmd); 988 } 989 return -ENOTTY; 990 } 991 992 /* ioctl to find the next device; either legacy or UMP depending on @find_ump */ 993 static int snd_rawmidi_next_device(struct snd_card *card, int __user *argp, 994 bool find_ump) 995 996 { 997 struct snd_rawmidi *rmidi; 998 int device; 999 bool is_ump; 1000 1001 if (get_user(device, argp)) 1002 return -EFAULT; 1003 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */ 1004 device = SNDRV_RAWMIDI_DEVICES - 1; 1005 scoped_guard(mutex, ®ister_mutex) { 1006 device = device < 0 ? 0 : device + 1; 1007 for (; device < SNDRV_RAWMIDI_DEVICES; device++) { 1008 rmidi = snd_rawmidi_search(card, device); 1009 if (!rmidi) 1010 continue; 1011 is_ump = rawmidi_is_ump(rmidi); 1012 if (find_ump == is_ump) 1013 break; 1014 } 1015 if (device == SNDRV_RAWMIDI_DEVICES) 1016 device = -1; 1017 } 1018 if (put_user(device, argp)) 1019 return -EFAULT; 1020 return 0; 1021 } 1022 1023 #if IS_ENABLED(CONFIG_SND_UMP) 1024 /* inquiry of UMP endpoint and block info via control API */ 1025 static int snd_rawmidi_call_ump_ioctl(struct snd_card *card, int cmd, 1026 void __user *argp) 1027 { 1028 struct snd_ump_endpoint_info __user *info = argp; 1029 struct snd_rawmidi *rmidi; 1030 int device; 1031 1032 if (get_user(device, &info->device)) 1033 return -EFAULT; 1034 guard(mutex)(®ister_mutex); 1035 rmidi = snd_rawmidi_search(card, device); 1036 if (rmidi && rmidi->ops && rmidi->ops->ioctl) 1037 return rmidi->ops->ioctl(rmidi, cmd, argp); 1038 else 1039 return -ENXIO; 1040 } 1041 #endif 1042 1043 static int snd_rawmidi_control_ioctl(struct snd_card *card, 1044 struct snd_ctl_file *control, 1045 unsigned int cmd, 1046 unsigned long arg) 1047 { 1048 void __user *argp = (void __user *)arg; 1049 1050 switch (cmd) { 1051 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE: 1052 return snd_rawmidi_next_device(card, argp, false); 1053 #if IS_ENABLED(CONFIG_SND_UMP) 1054 case SNDRV_CTL_IOCTL_UMP_NEXT_DEVICE: 1055 return snd_rawmidi_next_device(card, argp, true); 1056 case SNDRV_CTL_IOCTL_UMP_ENDPOINT_INFO: 1057 return snd_rawmidi_call_ump_ioctl(card, SNDRV_UMP_IOCTL_ENDPOINT_INFO, argp); 1058 case SNDRV_CTL_IOCTL_UMP_BLOCK_INFO: 1059 return snd_rawmidi_call_ump_ioctl(card, SNDRV_UMP_IOCTL_BLOCK_INFO, argp); 1060 #endif 1061 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE: 1062 { 1063 int val; 1064 1065 if (get_user(val, (int __user *)argp)) 1066 return -EFAULT; 1067 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val; 1068 return 0; 1069 } 1070 case SNDRV_CTL_IOCTL_RAWMIDI_INFO: 1071 return snd_rawmidi_info_select_user(card, argp); 1072 } 1073 return -ENOIOCTLCMD; 1074 } 1075 1076 static int receive_with_tstamp_framing(struct snd_rawmidi_substream *substream, 1077 const unsigned char *buffer, int src_count, const struct timespec64 *tstamp) 1078 { 1079 struct snd_rawmidi_runtime *runtime = substream->runtime; 1080 struct snd_rawmidi_framing_tstamp *dest_ptr; 1081 struct snd_rawmidi_framing_tstamp frame = { .tv_sec = tstamp->tv_sec, .tv_nsec = tstamp->tv_nsec }; 1082 int orig_count = src_count; 1083 int frame_size = sizeof(struct snd_rawmidi_framing_tstamp); 1084 int align = get_align(runtime); 1085 1086 BUILD_BUG_ON(frame_size != 0x20); 1087 if (snd_BUG_ON((runtime->hw_ptr & 0x1f) != 0)) 1088 return -EINVAL; 1089 1090 while (src_count > align) { 1091 if ((int)(runtime->buffer_size - runtime->avail) < frame_size) { 1092 runtime->xruns += src_count; 1093 break; 1094 } 1095 if (src_count >= SNDRV_RAWMIDI_FRAMING_DATA_LENGTH) 1096 frame.length = SNDRV_RAWMIDI_FRAMING_DATA_LENGTH; 1097 else { 1098 frame.length = get_aligned_size(runtime, src_count); 1099 if (!frame.length) 1100 break; 1101 memset(frame.data, 0, SNDRV_RAWMIDI_FRAMING_DATA_LENGTH); 1102 } 1103 memcpy(frame.data, buffer, frame.length); 1104 buffer += frame.length; 1105 src_count -= frame.length; 1106 dest_ptr = (struct snd_rawmidi_framing_tstamp *) (runtime->buffer + runtime->hw_ptr); 1107 *dest_ptr = frame; 1108 runtime->avail += frame_size; 1109 runtime->hw_ptr += frame_size; 1110 runtime->hw_ptr %= runtime->buffer_size; 1111 } 1112 return orig_count - src_count; 1113 } 1114 1115 static struct timespec64 get_framing_tstamp(struct snd_rawmidi_substream *substream) 1116 { 1117 struct timespec64 ts64 = {0, 0}; 1118 1119 switch (substream->clock_type) { 1120 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW: 1121 ktime_get_raw_ts64(&ts64); 1122 break; 1123 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC: 1124 ktime_get_ts64(&ts64); 1125 break; 1126 case SNDRV_RAWMIDI_MODE_CLOCK_REALTIME: 1127 ktime_get_real_ts64(&ts64); 1128 break; 1129 } 1130 return ts64; 1131 } 1132 1133 /** 1134 * snd_rawmidi_receive - receive the input data from the device 1135 * @substream: the rawmidi substream 1136 * @buffer: the buffer pointer 1137 * @count: the data size to read 1138 * 1139 * Reads the data from the internal buffer. 1140 * 1141 * Return: The size of read data, or a negative error code on failure. 1142 */ 1143 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, 1144 const unsigned char *buffer, int count) 1145 { 1146 struct timespec64 ts64 = get_framing_tstamp(substream); 1147 int result = 0, count1; 1148 struct snd_rawmidi_runtime *runtime; 1149 1150 guard(spinlock_irqsave)(&substream->lock); 1151 if (!substream->opened) 1152 return -EBADFD; 1153 runtime = substream->runtime; 1154 if (!runtime || !runtime->buffer) { 1155 rmidi_dbg(substream->rmidi, 1156 "snd_rawmidi_receive: input is not active!!!\n"); 1157 return -EINVAL; 1158 } 1159 1160 count = get_aligned_size(runtime, count); 1161 if (!count) 1162 return result; 1163 1164 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) { 1165 result = receive_with_tstamp_framing(substream, buffer, count, &ts64); 1166 } else if (count == 1) { /* special case, faster code */ 1167 substream->bytes++; 1168 if (runtime->avail < runtime->buffer_size) { 1169 runtime->buffer[runtime->hw_ptr++] = buffer[0]; 1170 runtime->hw_ptr %= runtime->buffer_size; 1171 runtime->avail++; 1172 result++; 1173 } else { 1174 runtime->xruns++; 1175 } 1176 } else { 1177 substream->bytes += count; 1178 count1 = runtime->buffer_size - runtime->hw_ptr; 1179 if (count1 > count) 1180 count1 = count; 1181 if (count1 > (int)(runtime->buffer_size - runtime->avail)) 1182 count1 = runtime->buffer_size - runtime->avail; 1183 count1 = get_aligned_size(runtime, count1); 1184 if (!count1) 1185 return result; 1186 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1); 1187 runtime->hw_ptr += count1; 1188 runtime->hw_ptr %= runtime->buffer_size; 1189 runtime->avail += count1; 1190 count -= count1; 1191 result += count1; 1192 if (count > 0) { 1193 buffer += count1; 1194 count1 = count; 1195 if (count1 > (int)(runtime->buffer_size - runtime->avail)) { 1196 count1 = runtime->buffer_size - runtime->avail; 1197 runtime->xruns += count - count1; 1198 } 1199 if (count1 > 0) { 1200 memcpy(runtime->buffer, buffer, count1); 1201 runtime->hw_ptr = count1; 1202 runtime->avail += count1; 1203 result += count1; 1204 } 1205 } 1206 } 1207 if (result > 0) { 1208 if (runtime->event) 1209 schedule_work(&runtime->event_work); 1210 else if (__snd_rawmidi_ready(runtime)) 1211 wake_up(&runtime->sleep); 1212 } 1213 return result; 1214 } 1215 EXPORT_SYMBOL(snd_rawmidi_receive); 1216 1217 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream, 1218 unsigned char __user *userbuf, 1219 unsigned char *kernelbuf, long count) 1220 { 1221 unsigned long flags; 1222 long result = 0, count1; 1223 struct snd_rawmidi_runtime *runtime = substream->runtime; 1224 unsigned long appl_ptr; 1225 int err = 0; 1226 1227 spin_lock_irqsave(&substream->lock, flags); 1228 snd_rawmidi_buffer_ref(runtime); 1229 while (count > 0 && runtime->avail) { 1230 count1 = runtime->buffer_size - runtime->appl_ptr; 1231 if (count1 > count) 1232 count1 = count; 1233 if (count1 > (int)runtime->avail) 1234 count1 = runtime->avail; 1235 1236 /* update runtime->appl_ptr before unlocking for userbuf */ 1237 appl_ptr = runtime->appl_ptr; 1238 runtime->appl_ptr += count1; 1239 runtime->appl_ptr %= runtime->buffer_size; 1240 runtime->avail -= count1; 1241 1242 if (kernelbuf) 1243 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1); 1244 if (userbuf) { 1245 spin_unlock_irqrestore(&substream->lock, flags); 1246 if (copy_to_user(userbuf + result, 1247 runtime->buffer + appl_ptr, count1)) 1248 err = -EFAULT; 1249 spin_lock_irqsave(&substream->lock, flags); 1250 if (err) 1251 goto out; 1252 } 1253 result += count1; 1254 count -= count1; 1255 } 1256 out: 1257 snd_rawmidi_buffer_unref(runtime); 1258 spin_unlock_irqrestore(&substream->lock, flags); 1259 return result > 0 ? result : err; 1260 } 1261 1262 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream, 1263 unsigned char *buf, long count) 1264 { 1265 snd_rawmidi_input_trigger(substream, 1); 1266 return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count); 1267 } 1268 EXPORT_SYMBOL(snd_rawmidi_kernel_read); 1269 1270 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, 1271 loff_t *offset) 1272 { 1273 long result; 1274 int count1; 1275 struct snd_rawmidi_file *rfile; 1276 struct snd_rawmidi_substream *substream; 1277 struct snd_rawmidi_runtime *runtime; 1278 1279 rfile = file->private_data; 1280 substream = rfile->input; 1281 if (substream == NULL) 1282 return -EIO; 1283 runtime = substream->runtime; 1284 snd_rawmidi_input_trigger(substream, 1); 1285 result = 0; 1286 while (count > 0) { 1287 spin_lock_irq(&substream->lock); 1288 while (!__snd_rawmidi_ready(runtime)) { 1289 wait_queue_entry_t wait; 1290 1291 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) { 1292 spin_unlock_irq(&substream->lock); 1293 return result > 0 ? result : -EAGAIN; 1294 } 1295 init_waitqueue_entry(&wait, current); 1296 add_wait_queue(&runtime->sleep, &wait); 1297 set_current_state(TASK_INTERRUPTIBLE); 1298 spin_unlock_irq(&substream->lock); 1299 schedule(); 1300 remove_wait_queue(&runtime->sleep, &wait); 1301 if (rfile->rmidi->card->shutdown) 1302 return -ENODEV; 1303 if (signal_pending(current)) 1304 return result > 0 ? result : -ERESTARTSYS; 1305 spin_lock_irq(&substream->lock); 1306 if (!runtime->avail) { 1307 spin_unlock_irq(&substream->lock); 1308 return result > 0 ? result : -EIO; 1309 } 1310 } 1311 spin_unlock_irq(&substream->lock); 1312 count1 = snd_rawmidi_kernel_read1(substream, 1313 (unsigned char __user *)buf, 1314 NULL/*kernelbuf*/, 1315 count); 1316 if (count1 < 0) 1317 return result > 0 ? result : count1; 1318 result += count1; 1319 buf += count1; 1320 count -= count1; 1321 } 1322 return result; 1323 } 1324 1325 /** 1326 * snd_rawmidi_transmit_empty - check whether the output buffer is empty 1327 * @substream: the rawmidi substream 1328 * 1329 * Return: 1 if the internal output buffer is empty, 0 if not. 1330 */ 1331 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream) 1332 { 1333 struct snd_rawmidi_runtime *runtime; 1334 1335 guard(spinlock_irqsave)(&substream->lock); 1336 runtime = substream->runtime; 1337 if (!substream->opened || !runtime || !runtime->buffer) { 1338 rmidi_dbg(substream->rmidi, 1339 "snd_rawmidi_transmit_empty: output is not active!!!\n"); 1340 return 1; 1341 } 1342 return (runtime->avail >= runtime->buffer_size); 1343 } 1344 EXPORT_SYMBOL(snd_rawmidi_transmit_empty); 1345 1346 /* 1347 * __snd_rawmidi_transmit_peek - copy data from the internal buffer 1348 * @substream: the rawmidi substream 1349 * @buffer: the buffer pointer 1350 * @count: data size to transfer 1351 * 1352 * This is a variant of snd_rawmidi_transmit_peek() without spinlock. 1353 */ 1354 static int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream, 1355 unsigned char *buffer, int count) 1356 { 1357 int result, count1; 1358 struct snd_rawmidi_runtime *runtime = substream->runtime; 1359 1360 if (runtime->buffer == NULL) { 1361 rmidi_dbg(substream->rmidi, 1362 "snd_rawmidi_transmit_peek: output is not active!!!\n"); 1363 return -EINVAL; 1364 } 1365 result = 0; 1366 if (runtime->avail >= runtime->buffer_size) { 1367 /* warning: lowlevel layer MUST trigger down the hardware */ 1368 goto __skip; 1369 } 1370 if (count == 1) { /* special case, faster code */ 1371 *buffer = runtime->buffer[runtime->hw_ptr]; 1372 result++; 1373 } else { 1374 count1 = runtime->buffer_size - runtime->hw_ptr; 1375 if (count1 > count) 1376 count1 = count; 1377 if (count1 > (int)(runtime->buffer_size - runtime->avail)) 1378 count1 = runtime->buffer_size - runtime->avail; 1379 count1 = get_aligned_size(runtime, count1); 1380 if (!count1) 1381 goto __skip; 1382 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1); 1383 count -= count1; 1384 result += count1; 1385 if (count > 0) { 1386 if (count > (int)(runtime->buffer_size - runtime->avail - count1)) 1387 count = runtime->buffer_size - runtime->avail - count1; 1388 count = get_aligned_size(runtime, count); 1389 if (!count) 1390 goto __skip; 1391 memcpy(buffer + count1, runtime->buffer, count); 1392 result += count; 1393 } 1394 } 1395 __skip: 1396 return result; 1397 } 1398 1399 /** 1400 * snd_rawmidi_transmit_peek - copy data from the internal buffer 1401 * @substream: the rawmidi substream 1402 * @buffer: the buffer pointer 1403 * @count: data size to transfer 1404 * 1405 * Copies data from the internal output buffer to the given buffer. 1406 * 1407 * Call this in the interrupt handler when the midi output is ready, 1408 * and call snd_rawmidi_transmit_ack() after the transmission is 1409 * finished. 1410 * 1411 * Return: The size of copied data, or a negative error code on failure. 1412 */ 1413 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream, 1414 unsigned char *buffer, int count) 1415 { 1416 guard(spinlock_irqsave)(&substream->lock); 1417 if (!substream->opened || !substream->runtime) 1418 return -EBADFD; 1419 return __snd_rawmidi_transmit_peek(substream, buffer, count); 1420 } 1421 EXPORT_SYMBOL(snd_rawmidi_transmit_peek); 1422 1423 /* 1424 * __snd_rawmidi_transmit_ack - acknowledge the transmission 1425 * @substream: the rawmidi substream 1426 * @count: the transferred count 1427 * 1428 * This is a variant of __snd_rawmidi_transmit_ack() without spinlock. 1429 */ 1430 static int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, 1431 int count) 1432 { 1433 struct snd_rawmidi_runtime *runtime = substream->runtime; 1434 1435 if (runtime->buffer == NULL) { 1436 rmidi_dbg(substream->rmidi, 1437 "snd_rawmidi_transmit_ack: output is not active!!!\n"); 1438 return -EINVAL; 1439 } 1440 snd_BUG_ON(runtime->avail + count > runtime->buffer_size); 1441 count = get_aligned_size(runtime, count); 1442 runtime->hw_ptr += count; 1443 runtime->hw_ptr %= runtime->buffer_size; 1444 runtime->avail += count; 1445 substream->bytes += count; 1446 if (count > 0) { 1447 if (runtime->drain || __snd_rawmidi_ready(runtime)) 1448 wake_up(&runtime->sleep); 1449 } 1450 return count; 1451 } 1452 1453 /** 1454 * snd_rawmidi_transmit_ack - acknowledge the transmission 1455 * @substream: the rawmidi substream 1456 * @count: the transferred count 1457 * 1458 * Advances the hardware pointer for the internal output buffer with 1459 * the given size and updates the condition. 1460 * Call after the transmission is finished. 1461 * 1462 * Return: The advanced size if successful, or a negative error code on failure. 1463 */ 1464 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count) 1465 { 1466 guard(spinlock_irqsave)(&substream->lock); 1467 if (!substream->opened || !substream->runtime) 1468 return -EBADFD; 1469 return __snd_rawmidi_transmit_ack(substream, count); 1470 } 1471 EXPORT_SYMBOL(snd_rawmidi_transmit_ack); 1472 1473 /** 1474 * snd_rawmidi_transmit - copy from the buffer to the device 1475 * @substream: the rawmidi substream 1476 * @buffer: the buffer pointer 1477 * @count: the data size to transfer 1478 * 1479 * Copies data from the buffer to the device and advances the pointer. 1480 * 1481 * Return: The copied size if successful, or a negative error code on failure. 1482 */ 1483 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream, 1484 unsigned char *buffer, int count) 1485 { 1486 guard(spinlock_irqsave)(&substream->lock); 1487 if (!substream->opened) 1488 return -EBADFD; 1489 count = __snd_rawmidi_transmit_peek(substream, buffer, count); 1490 if (count <= 0) 1491 return count; 1492 return __snd_rawmidi_transmit_ack(substream, count); 1493 } 1494 EXPORT_SYMBOL(snd_rawmidi_transmit); 1495 1496 /** 1497 * snd_rawmidi_proceed - Discard the all pending bytes and proceed 1498 * @substream: rawmidi substream 1499 * 1500 * Return: the number of discarded bytes 1501 */ 1502 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream) 1503 { 1504 struct snd_rawmidi_runtime *runtime; 1505 int count = 0; 1506 1507 guard(spinlock_irqsave)(&substream->lock); 1508 runtime = substream->runtime; 1509 if (substream->opened && runtime && 1510 runtime->avail < runtime->buffer_size) { 1511 count = runtime->buffer_size - runtime->avail; 1512 __snd_rawmidi_transmit_ack(substream, count); 1513 } 1514 return count; 1515 } 1516 EXPORT_SYMBOL(snd_rawmidi_proceed); 1517 1518 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream, 1519 const unsigned char __user *userbuf, 1520 const unsigned char *kernelbuf, 1521 long count) 1522 { 1523 unsigned long flags; 1524 long count1, result; 1525 struct snd_rawmidi_runtime *runtime = substream->runtime; 1526 unsigned long appl_ptr; 1527 1528 if (!kernelbuf && !userbuf) 1529 return -EINVAL; 1530 if (snd_BUG_ON(!runtime->buffer)) 1531 return -EINVAL; 1532 1533 result = 0; 1534 spin_lock_irqsave(&substream->lock, flags); 1535 if (substream->append) { 1536 if ((long)runtime->avail < count) { 1537 spin_unlock_irqrestore(&substream->lock, flags); 1538 return -EAGAIN; 1539 } 1540 } 1541 snd_rawmidi_buffer_ref(runtime); 1542 while (count > 0 && runtime->avail > 0) { 1543 count1 = runtime->buffer_size - runtime->appl_ptr; 1544 if (count1 > count) 1545 count1 = count; 1546 if (count1 > (long)runtime->avail) 1547 count1 = runtime->avail; 1548 1549 /* update runtime->appl_ptr before unlocking for userbuf */ 1550 appl_ptr = runtime->appl_ptr; 1551 runtime->appl_ptr += count1; 1552 runtime->appl_ptr %= runtime->buffer_size; 1553 runtime->avail -= count1; 1554 1555 if (kernelbuf) 1556 memcpy(runtime->buffer + appl_ptr, 1557 kernelbuf + result, count1); 1558 else if (userbuf) { 1559 spin_unlock_irqrestore(&substream->lock, flags); 1560 if (copy_from_user(runtime->buffer + appl_ptr, 1561 userbuf + result, count1)) { 1562 spin_lock_irqsave(&substream->lock, flags); 1563 result = result > 0 ? result : -EFAULT; 1564 goto __end; 1565 } 1566 spin_lock_irqsave(&substream->lock, flags); 1567 } 1568 result += count1; 1569 count -= count1; 1570 } 1571 __end: 1572 count1 = runtime->avail < runtime->buffer_size; 1573 snd_rawmidi_buffer_unref(runtime); 1574 spin_unlock_irqrestore(&substream->lock, flags); 1575 if (count1) 1576 snd_rawmidi_output_trigger(substream, 1); 1577 return result; 1578 } 1579 1580 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream, 1581 const unsigned char *buf, long count) 1582 { 1583 return snd_rawmidi_kernel_write1(substream, NULL, buf, count); 1584 } 1585 EXPORT_SYMBOL(snd_rawmidi_kernel_write); 1586 1587 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, 1588 size_t count, loff_t *offset) 1589 { 1590 long result, timeout; 1591 int count1; 1592 struct snd_rawmidi_file *rfile; 1593 struct snd_rawmidi_runtime *runtime; 1594 struct snd_rawmidi_substream *substream; 1595 1596 rfile = file->private_data; 1597 substream = rfile->output; 1598 runtime = substream->runtime; 1599 /* we cannot put an atomic message to our buffer */ 1600 if (substream->append && count > runtime->buffer_size) 1601 return -EIO; 1602 result = 0; 1603 while (count > 0) { 1604 spin_lock_irq(&substream->lock); 1605 while (!snd_rawmidi_ready_append(substream, count)) { 1606 wait_queue_entry_t wait; 1607 1608 if (file->f_flags & O_NONBLOCK) { 1609 spin_unlock_irq(&substream->lock); 1610 return result > 0 ? result : -EAGAIN; 1611 } 1612 init_waitqueue_entry(&wait, current); 1613 add_wait_queue(&runtime->sleep, &wait); 1614 set_current_state(TASK_INTERRUPTIBLE); 1615 spin_unlock_irq(&substream->lock); 1616 timeout = schedule_timeout(30 * HZ); 1617 remove_wait_queue(&runtime->sleep, &wait); 1618 if (rfile->rmidi->card->shutdown) 1619 return -ENODEV; 1620 if (signal_pending(current)) 1621 return result > 0 ? result : -ERESTARTSYS; 1622 spin_lock_irq(&substream->lock); 1623 if (!runtime->avail && !timeout) { 1624 spin_unlock_irq(&substream->lock); 1625 return result > 0 ? result : -EIO; 1626 } 1627 } 1628 spin_unlock_irq(&substream->lock); 1629 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count); 1630 if (count1 < 0) 1631 return result > 0 ? result : count1; 1632 result += count1; 1633 buf += count1; 1634 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK)) 1635 break; 1636 count -= count1; 1637 } 1638 if (file->f_flags & O_DSYNC) { 1639 spin_lock_irq(&substream->lock); 1640 while (runtime->avail != runtime->buffer_size) { 1641 wait_queue_entry_t wait; 1642 unsigned int last_avail = runtime->avail; 1643 1644 init_waitqueue_entry(&wait, current); 1645 add_wait_queue(&runtime->sleep, &wait); 1646 set_current_state(TASK_INTERRUPTIBLE); 1647 spin_unlock_irq(&substream->lock); 1648 timeout = schedule_timeout(30 * HZ); 1649 remove_wait_queue(&runtime->sleep, &wait); 1650 if (signal_pending(current)) 1651 return result > 0 ? result : -ERESTARTSYS; 1652 if (runtime->avail == last_avail && !timeout) 1653 return result > 0 ? result : -EIO; 1654 spin_lock_irq(&substream->lock); 1655 } 1656 spin_unlock_irq(&substream->lock); 1657 } 1658 return result; 1659 } 1660 1661 static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait) 1662 { 1663 struct snd_rawmidi_file *rfile; 1664 struct snd_rawmidi_runtime *runtime; 1665 __poll_t mask; 1666 1667 rfile = file->private_data; 1668 if (rfile->input != NULL) { 1669 runtime = rfile->input->runtime; 1670 snd_rawmidi_input_trigger(rfile->input, 1); 1671 poll_wait(file, &runtime->sleep, wait); 1672 } 1673 if (rfile->output != NULL) { 1674 runtime = rfile->output->runtime; 1675 poll_wait(file, &runtime->sleep, wait); 1676 } 1677 mask = 0; 1678 if (rfile->input != NULL) { 1679 if (snd_rawmidi_ready(rfile->input)) 1680 mask |= EPOLLIN | EPOLLRDNORM; 1681 } 1682 if (rfile->output != NULL) { 1683 if (snd_rawmidi_ready(rfile->output)) 1684 mask |= EPOLLOUT | EPOLLWRNORM; 1685 } 1686 return mask; 1687 } 1688 1689 /* 1690 */ 1691 #ifdef CONFIG_COMPAT 1692 #include "rawmidi_compat.c" 1693 #else 1694 #define snd_rawmidi_ioctl_compat NULL 1695 #endif 1696 1697 /* 1698 */ 1699 1700 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry, 1701 struct snd_info_buffer *buffer) 1702 { 1703 struct snd_rawmidi *rmidi; 1704 struct snd_rawmidi_substream *substream; 1705 struct snd_rawmidi_runtime *runtime; 1706 unsigned long buffer_size, avail, xruns; 1707 unsigned int clock_type; 1708 static const char *clock_names[4] = { "none", "realtime", "monotonic", "monotonic raw" }; 1709 1710 rmidi = entry->private_data; 1711 snd_iprintf(buffer, "%s\n\n", rmidi->name); 1712 if (IS_ENABLED(CONFIG_SND_UMP)) 1713 snd_iprintf(buffer, "Type: %s\n", 1714 rawmidi_is_ump(rmidi) ? "UMP" : "Legacy"); 1715 if (rmidi->ops && rmidi->ops->proc_read) 1716 rmidi->ops->proc_read(entry, buffer); 1717 guard(mutex)(&rmidi->open_mutex); 1718 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) { 1719 list_for_each_entry(substream, 1720 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams, 1721 list) { 1722 snd_iprintf(buffer, 1723 "Output %d\n" 1724 " Tx bytes : %lu\n", 1725 substream->number, 1726 (unsigned long) substream->bytes); 1727 if (substream->opened) { 1728 snd_iprintf(buffer, 1729 " Owner PID : %d\n", 1730 pid_vnr(substream->pid)); 1731 runtime = substream->runtime; 1732 scoped_guard(spinlock_irq, &substream->lock) { 1733 buffer_size = runtime->buffer_size; 1734 avail = runtime->avail; 1735 } 1736 snd_iprintf(buffer, 1737 " Mode : %s\n" 1738 " Buffer size : %lu\n" 1739 " Avail : %lu\n", 1740 runtime->oss ? "OSS compatible" : "native", 1741 buffer_size, avail); 1742 } 1743 } 1744 } 1745 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) { 1746 list_for_each_entry(substream, 1747 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams, 1748 list) { 1749 snd_iprintf(buffer, 1750 "Input %d\n" 1751 " Rx bytes : %lu\n", 1752 substream->number, 1753 (unsigned long) substream->bytes); 1754 if (substream->opened) { 1755 snd_iprintf(buffer, 1756 " Owner PID : %d\n", 1757 pid_vnr(substream->pid)); 1758 runtime = substream->runtime; 1759 scoped_guard(spinlock_irq, &substream->lock) { 1760 buffer_size = runtime->buffer_size; 1761 avail = runtime->avail; 1762 xruns = runtime->xruns; 1763 } 1764 snd_iprintf(buffer, 1765 " Buffer size : %lu\n" 1766 " Avail : %lu\n" 1767 " Overruns : %lu\n", 1768 buffer_size, avail, xruns); 1769 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) { 1770 clock_type = substream->clock_type >> SNDRV_RAWMIDI_MODE_CLOCK_SHIFT; 1771 if (!snd_BUG_ON(clock_type >= ARRAY_SIZE(clock_names))) 1772 snd_iprintf(buffer, 1773 " Framing : tstamp\n" 1774 " Clock type : %s\n", 1775 clock_names[clock_type]); 1776 } 1777 } 1778 } 1779 } 1780 } 1781 1782 /* 1783 * Register functions 1784 */ 1785 1786 static const struct file_operations snd_rawmidi_f_ops = { 1787 .owner = THIS_MODULE, 1788 .read = snd_rawmidi_read, 1789 .write = snd_rawmidi_write, 1790 .open = snd_rawmidi_open, 1791 .release = snd_rawmidi_release, 1792 .poll = snd_rawmidi_poll, 1793 .unlocked_ioctl = snd_rawmidi_ioctl, 1794 .compat_ioctl = snd_rawmidi_ioctl_compat, 1795 }; 1796 1797 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi, 1798 struct snd_rawmidi_str *stream, 1799 int direction, 1800 int count) 1801 { 1802 struct snd_rawmidi_substream *substream; 1803 int idx; 1804 1805 for (idx = 0; idx < count; idx++) { 1806 substream = kzalloc(sizeof(*substream), GFP_KERNEL); 1807 if (!substream) 1808 return -ENOMEM; 1809 substream->stream = direction; 1810 substream->number = idx; 1811 substream->rmidi = rmidi; 1812 substream->pstr = stream; 1813 spin_lock_init(&substream->lock); 1814 list_add_tail(&substream->list, &stream->substreams); 1815 stream->substream_count++; 1816 } 1817 return 0; 1818 } 1819 1820 /* used for both rawmidi and ump */ 1821 int snd_rawmidi_init(struct snd_rawmidi *rmidi, 1822 struct snd_card *card, char *id, int device, 1823 int output_count, int input_count, 1824 unsigned int info_flags) 1825 { 1826 int err; 1827 static const struct snd_device_ops ops = { 1828 .dev_free = snd_rawmidi_dev_free, 1829 .dev_register = snd_rawmidi_dev_register, 1830 .dev_disconnect = snd_rawmidi_dev_disconnect, 1831 }; 1832 1833 rmidi->card = card; 1834 rmidi->device = device; 1835 mutex_init(&rmidi->open_mutex); 1836 init_waitqueue_head(&rmidi->open_wait); 1837 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams); 1838 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams); 1839 rmidi->info_flags = info_flags; 1840 1841 if (id != NULL) 1842 strscpy(rmidi->id, id, sizeof(rmidi->id)); 1843 1844 err = snd_device_alloc(&rmidi->dev, card); 1845 if (err < 0) 1846 return err; 1847 if (rawmidi_is_ump(rmidi)) 1848 dev_set_name(rmidi->dev, "umpC%iD%i", card->number, device); 1849 else 1850 dev_set_name(rmidi->dev, "midiC%iD%i", card->number, device); 1851 1852 err = snd_rawmidi_alloc_substreams(rmidi, 1853 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], 1854 SNDRV_RAWMIDI_STREAM_INPUT, 1855 input_count); 1856 if (err < 0) 1857 return err; 1858 err = snd_rawmidi_alloc_substreams(rmidi, 1859 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], 1860 SNDRV_RAWMIDI_STREAM_OUTPUT, 1861 output_count); 1862 if (err < 0) 1863 return err; 1864 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops); 1865 if (err < 0) 1866 return err; 1867 return 0; 1868 } 1869 EXPORT_SYMBOL_GPL(snd_rawmidi_init); 1870 1871 /** 1872 * snd_rawmidi_new - create a rawmidi instance 1873 * @card: the card instance 1874 * @id: the id string 1875 * @device: the device index 1876 * @output_count: the number of output streams 1877 * @input_count: the number of input streams 1878 * @rrawmidi: the pointer to store the new rawmidi instance 1879 * 1880 * Creates a new rawmidi instance. 1881 * Use snd_rawmidi_set_ops() to set the operators to the new instance. 1882 * 1883 * Return: Zero if successful, or a negative error code on failure. 1884 */ 1885 int snd_rawmidi_new(struct snd_card *card, char *id, int device, 1886 int output_count, int input_count, 1887 struct snd_rawmidi **rrawmidi) 1888 { 1889 struct snd_rawmidi *rmidi; 1890 int err; 1891 1892 if (rrawmidi) 1893 *rrawmidi = NULL; 1894 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL); 1895 if (!rmidi) 1896 return -ENOMEM; 1897 err = snd_rawmidi_init(rmidi, card, id, device, 1898 output_count, input_count, 0); 1899 if (err < 0) { 1900 snd_rawmidi_free(rmidi); 1901 return err; 1902 } 1903 if (rrawmidi) 1904 *rrawmidi = rmidi; 1905 return 0; 1906 } 1907 EXPORT_SYMBOL(snd_rawmidi_new); 1908 1909 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream) 1910 { 1911 struct snd_rawmidi_substream *substream; 1912 1913 while (!list_empty(&stream->substreams)) { 1914 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list); 1915 list_del(&substream->list); 1916 kfree(substream); 1917 } 1918 } 1919 1920 /* called from ump.c, too */ 1921 int snd_rawmidi_free(struct snd_rawmidi *rmidi) 1922 { 1923 if (!rmidi) 1924 return 0; 1925 1926 snd_info_free_entry(rmidi->proc_entry); 1927 rmidi->proc_entry = NULL; 1928 if (rmidi->ops && rmidi->ops->dev_unregister) 1929 rmidi->ops->dev_unregister(rmidi); 1930 1931 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]); 1932 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]); 1933 if (rmidi->private_free) 1934 rmidi->private_free(rmidi); 1935 put_device(rmidi->dev); 1936 kfree(rmidi); 1937 return 0; 1938 } 1939 EXPORT_SYMBOL_GPL(snd_rawmidi_free); 1940 1941 static int snd_rawmidi_dev_free(struct snd_device *device) 1942 { 1943 struct snd_rawmidi *rmidi = device->device_data; 1944 1945 return snd_rawmidi_free(rmidi); 1946 } 1947 1948 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 1949 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device) 1950 { 1951 struct snd_rawmidi *rmidi = device->private_data; 1952 1953 rmidi->seq_dev = NULL; 1954 } 1955 #endif 1956 1957 static int snd_rawmidi_dev_register(struct snd_device *device) 1958 { 1959 int err; 1960 struct snd_info_entry *entry; 1961 char name[16]; 1962 struct snd_rawmidi *rmidi = device->device_data; 1963 1964 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES) 1965 return -ENOMEM; 1966 err = 0; 1967 scoped_guard(mutex, ®ister_mutex) { 1968 if (snd_rawmidi_search(rmidi->card, rmidi->device)) 1969 err = -EBUSY; 1970 else 1971 list_add_tail(&rmidi->list, &snd_rawmidi_devices); 1972 } 1973 if (err < 0) 1974 return err; 1975 1976 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI, 1977 rmidi->card, rmidi->device, 1978 &snd_rawmidi_f_ops, rmidi, rmidi->dev); 1979 if (err < 0) { 1980 rmidi_err(rmidi, "unable to register\n"); 1981 goto error; 1982 } 1983 if (rmidi->ops && rmidi->ops->dev_register) { 1984 err = rmidi->ops->dev_register(rmidi); 1985 if (err < 0) 1986 goto error_unregister; 1987 } 1988 #ifdef CONFIG_SND_OSSEMUL 1989 rmidi->ossreg = 0; 1990 if (!rawmidi_is_ump(rmidi) && 1991 (int)rmidi->device == midi_map[rmidi->card->number]) { 1992 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, 1993 rmidi->card, 0, &snd_rawmidi_f_ops, 1994 rmidi) < 0) { 1995 rmidi_err(rmidi, 1996 "unable to register OSS rawmidi device %i:%i\n", 1997 rmidi->card->number, 0); 1998 } else { 1999 rmidi->ossreg++; 2000 #ifdef SNDRV_OSS_INFO_DEV_MIDI 2001 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name); 2002 #endif 2003 } 2004 } 2005 if (!rawmidi_is_ump(rmidi) && 2006 (int)rmidi->device == amidi_map[rmidi->card->number]) { 2007 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, 2008 rmidi->card, 1, &snd_rawmidi_f_ops, 2009 rmidi) < 0) { 2010 rmidi_err(rmidi, 2011 "unable to register OSS rawmidi device %i:%i\n", 2012 rmidi->card->number, 1); 2013 } else { 2014 rmidi->ossreg++; 2015 } 2016 } 2017 #endif /* CONFIG_SND_OSSEMUL */ 2018 sprintf(name, "midi%d", rmidi->device); 2019 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root); 2020 if (entry) { 2021 entry->private_data = rmidi; 2022 entry->c.text.read = snd_rawmidi_proc_info_read; 2023 if (snd_info_register(entry) < 0) { 2024 snd_info_free_entry(entry); 2025 entry = NULL; 2026 } 2027 } 2028 rmidi->proc_entry = entry; 2029 #if IS_ENABLED(CONFIG_SND_SEQUENCER) 2030 /* no own registration mechanism? */ 2031 if (!rmidi->ops || !rmidi->ops->dev_register) { 2032 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) { 2033 rmidi->seq_dev->private_data = rmidi; 2034 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free; 2035 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device); 2036 snd_device_register(rmidi->card, rmidi->seq_dev); 2037 } 2038 } 2039 #endif 2040 return 0; 2041 2042 error_unregister: 2043 snd_unregister_device(rmidi->dev); 2044 error: 2045 scoped_guard(mutex, ®ister_mutex) 2046 list_del(&rmidi->list); 2047 return err; 2048 } 2049 2050 static int snd_rawmidi_dev_disconnect(struct snd_device *device) 2051 { 2052 struct snd_rawmidi *rmidi = device->device_data; 2053 int dir; 2054 2055 guard(mutex)(®ister_mutex); 2056 guard(mutex)(&rmidi->open_mutex); 2057 wake_up(&rmidi->open_wait); 2058 list_del_init(&rmidi->list); 2059 for (dir = 0; dir < 2; dir++) { 2060 struct snd_rawmidi_substream *s; 2061 2062 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) { 2063 if (s->runtime) 2064 wake_up(&s->runtime->sleep); 2065 } 2066 } 2067 2068 #ifdef CONFIG_SND_OSSEMUL 2069 if (rmidi->ossreg) { 2070 if ((int)rmidi->device == midi_map[rmidi->card->number]) { 2071 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0); 2072 #ifdef SNDRV_OSS_INFO_DEV_MIDI 2073 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number); 2074 #endif 2075 } 2076 if ((int)rmidi->device == amidi_map[rmidi->card->number]) 2077 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1); 2078 rmidi->ossreg = 0; 2079 } 2080 #endif /* CONFIG_SND_OSSEMUL */ 2081 snd_unregister_device(rmidi->dev); 2082 return 0; 2083 } 2084 2085 /** 2086 * snd_rawmidi_set_ops - set the rawmidi operators 2087 * @rmidi: the rawmidi instance 2088 * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX 2089 * @ops: the operator table 2090 * 2091 * Sets the rawmidi operators for the given stream direction. 2092 */ 2093 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream, 2094 const struct snd_rawmidi_ops *ops) 2095 { 2096 struct snd_rawmidi_substream *substream; 2097 2098 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list) 2099 substream->ops = ops; 2100 } 2101 EXPORT_SYMBOL(snd_rawmidi_set_ops); 2102 2103 /* 2104 * ENTRY functions 2105 */ 2106 2107 static int __init alsa_rawmidi_init(void) 2108 { 2109 2110 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl); 2111 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl); 2112 #ifdef CONFIG_SND_OSSEMUL 2113 { int i; 2114 /* check device map table */ 2115 for (i = 0; i < SNDRV_CARDS; i++) { 2116 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) { 2117 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n", 2118 i, midi_map[i]); 2119 midi_map[i] = 0; 2120 } 2121 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) { 2122 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n", 2123 i, amidi_map[i]); 2124 amidi_map[i] = 1; 2125 } 2126 } 2127 } 2128 #endif /* CONFIG_SND_OSSEMUL */ 2129 return 0; 2130 } 2131 2132 static void __exit alsa_rawmidi_exit(void) 2133 { 2134 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl); 2135 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl); 2136 } 2137 2138 module_init(alsa_rawmidi_init) 2139 module_exit(alsa_rawmidi_exit) 2140