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