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