Lines Matching +full:signal +full:- +full:guard
1 // SPDX-License-Identifier: GPL-2.0-or-later
10 #include <linux/sched/signal.h>
32 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
47 dev_err((rmidi)->dev, fmt, ##args)
49 dev_warn((rmidi)->dev, fmt, ##args)
51 dev_dbg((rmidi)->dev, fmt, ##args)
77 (IS_ENABLED(CONFIG_SND_UMP) && ((rmidi)->info_flags & SNDRV_RAWMIDI_INFO_UMP))
84 if (rawmidi->card == card && rawmidi->device == device)
91 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
103 return runtime->avail >= runtime->avail_min;
108 guard(spinlock_irqsave)(&substream->lock);
109 return __snd_rawmidi_ready(substream->runtime);
115 struct snd_rawmidi_runtime *runtime = substream->runtime;
117 return runtime->avail >= runtime->avail_min &&
118 (!substream->append || runtime->avail >= count);
126 if (runtime->event)
127 runtime->event(runtime->substream);
130 /* buffer refcount management: call with substream->lock held */
133 runtime->buffer_ref++;
138 runtime->buffer_ref--;
145 spin_lock_irq(&substream->lock);
146 while (substream->runtime->buffer_ref) {
147 spin_unlock_irq(&substream->lock);
148 if (!--loop) {
149 rmidi_err(substream->rmidi, "Buffer ref sync timeout\n");
153 spin_lock_irq(&substream->lock);
155 spin_unlock_irq(&substream->lock);
164 return -ENOMEM;
165 runtime->substream = substream;
166 init_waitqueue_head(&runtime->sleep);
167 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
168 runtime->event = NULL;
169 runtime->buffer_size = PAGE_SIZE;
170 runtime->avail_min = 1;
171 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
172 runtime->avail = 0;
174 runtime->avail = runtime->buffer_size;
175 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
176 if (!runtime->buffer) {
178 return -ENOMEM;
180 runtime->appl_ptr = runtime->hw_ptr = 0;
181 substream->runtime = runtime;
182 if (rawmidi_is_ump(substream->rmidi))
183 runtime->align = 3;
191 return runtime->align;
201 struct snd_rawmidi_runtime *runtime = substream->runtime;
203 kvfree(runtime->buffer);
205 substream->runtime = NULL;
211 if (!substream->opened)
213 substream->ops->trigger(substream, up);
218 if (!substream->opened)
220 substream->ops->trigger(substream, up);
222 cancel_work_sync(&substream->runtime->event_work);
228 runtime->drain = 0;
229 runtime->appl_ptr = runtime->hw_ptr = 0;
230 runtime->avail = is_input ? 0 : runtime->buffer_size;
236 guard(spinlock_irqsave)(&substream->lock);
237 if (substream->opened && substream->runtime)
238 __reset_runtime_ptrs(substream->runtime, is_input);
255 scoped_guard(spinlock_irq, &substream->lock) {
256 runtime = substream->runtime;
257 if (!substream->opened || !runtime || !runtime->buffer)
258 return -EINVAL;
260 runtime->drain = 1;
263 timeout = wait_event_interruptible_timeout(runtime->sleep,
264 (runtime->avail >= runtime->buffer_size),
267 scoped_guard(spinlock_irq, &substream->lock) {
269 err = -ERESTARTSYS;
270 if (runtime->avail < runtime->buffer_size && !timeout) {
271 rmidi_warn(substream->rmidi,
273 (long)runtime->avail, (long)runtime->buffer_size);
274 err = -EIO;
276 runtime->drain = 0;
279 if (err != -ERESTARTSYS) {
281 if (substream->ops->drain)
282 substream->ops->drain(substream);
288 scoped_guard(spinlock_irq, &substream->lock)
311 struct snd_rawmidi_str *s = &rmidi->streams[stream];
317 if (!(rmidi->info_flags & info_flags[stream]))
318 return -ENXIO;
319 if (subdevice >= 0 && subdevice >= s->substream_count)
320 return -ENODEV;
322 list_for_each_entry(substream, &s->substreams, list) {
323 if (substream->opened) {
326 !substream->append)
329 if (subdevice < 0 || subdevice == substream->number) {
334 return -EAGAIN;
337 /* open and do ref-counting for the given substream */
344 if (substream->use_count == 0) {
348 err = substream->ops->open(substream);
353 guard(spinlock_irq)(&substream->lock);
354 substream->opened = 1;
355 substream->active_sensing = 0;
357 substream->append = 1;
358 substream->pid = get_pid(task_pid(current));
359 rmidi->streams[substream->stream].substream_opened++;
361 substream->use_count++;
375 rfile->input = rfile->output = NULL;
405 rfile->rmidi = rmidi;
406 rfile->input = sinput;
407 rfile->output = soutput;
418 return -EINVAL;
419 if (!try_module_get(rmidi->card->module))
420 return -ENXIO;
422 guard(mutex)(&rmidi->open_mutex);
425 module_put(rmidi->card->module);
441 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
442 return -EINVAL; /* invalid combination */
457 return -ENXIO;
460 return -ENODEV;
462 if (!try_module_get(rmidi->card->module)) {
463 snd_card_unref(rmidi->card);
464 return -ENXIO;
467 mutex_lock(&rmidi->open_mutex);
468 card = rmidi->card;
473 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
477 err = -ENOMEM;
480 rawmidi_file->user_pversion = 0;
482 add_wait_queue(&rmidi->open_wait, &wait);
488 if (err == -EAGAIN) {
489 if (file->f_flags & O_NONBLOCK) {
490 err = -EBUSY;
496 mutex_unlock(&rmidi->open_mutex);
498 mutex_lock(&rmidi->open_mutex);
499 if (rmidi->card->shutdown) {
500 err = -ENODEV;
504 err = -ERESTARTSYS;
508 remove_wait_queue(&rmidi->open_wait, &wait);
514 if (rawmidi_file->input && rawmidi_file->input->runtime)
515 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
516 if (rawmidi_file->output && rawmidi_file->output->runtime)
517 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
519 file->private_data = rawmidi_file;
520 mutex_unlock(&rmidi->open_mutex);
521 snd_card_unref(rmidi->card);
527 mutex_unlock(&rmidi->open_mutex);
528 module_put(rmidi->card->module);
529 snd_card_unref(rmidi->card);
537 if (--substream->use_count)
541 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
544 if (substream->active_sensing) {
551 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
556 scoped_guard(spinlock_irq, &substream->lock) {
557 substream->opened = 0;
558 substream->append = 0;
560 substream->ops->close(substream);
561 if (substream->runtime->private_free)
562 substream->runtime->private_free(substream);
564 put_pid(substream->pid);
565 substream->pid = NULL;
566 rmidi->streams[substream->stream].substream_opened--;
573 rmidi = rfile->rmidi;
574 guard(mutex)(&rmidi->open_mutex);
575 if (rfile->input) {
576 close_substream(rmidi, rfile->input, 1);
577 rfile->input = NULL;
579 if (rfile->output) {
580 close_substream(rmidi, rfile->output, 1);
581 rfile->output = NULL;
583 rfile->rmidi = NULL;
584 wake_up(&rmidi->open_wait);
593 return -ENXIO;
595 rmidi = rfile->rmidi;
597 module_put(rmidi->card->module);
608 rfile = file->private_data;
609 rmidi = rfile->rmidi;
612 module = rmidi->card->module;
613 snd_card_file_remove(rmidi->card, file);
624 return -ENODEV;
625 rmidi = substream->rmidi;
627 info->card = rmidi->card->number;
628 info->device = rmidi->device;
629 info->subdevice = substream->number;
630 info->stream = substream->stream;
631 info->flags = rmidi->info_flags;
632 strcpy(info->id, rmidi->id);
633 strcpy(info->name, rmidi->name);
634 strcpy(info->subname, substream->name);
635 info->subdevices_count = substream->pstr->substream_count;
636 info->subdevices_avail = (substream->pstr->substream_count -
637 substream->pstr->substream_opened);
651 return -EFAULT;
662 rmidi = snd_rawmidi_search(card, info->device);
664 return -ENXIO;
665 if (info->stream < 0 || info->stream > 1)
666 return -EINVAL;
667 info->stream = array_index_nospec(info->stream, 2);
668 pstr = &rmidi->streams[info->stream];
669 if (pstr->substream_count == 0)
670 return -ENOENT;
671 if (info->subdevice >= pstr->substream_count)
672 return -ENXIO;
673 list_for_each_entry(substream, &pstr->substreams, list) {
674 if ((unsigned int)substream->number == info->subdevice)
677 return -ENXIO;
682 guard(mutex)(®ister_mutex);
693 if (get_user(info.device, &_info->device))
694 return -EFAULT;
695 if (get_user(info.stream, &_info->stream))
696 return -EFAULT;
697 if (get_user(info.subdevice, &_info->subdevice))
698 return -EFAULT;
703 return -EFAULT;
711 struct snd_rawmidi_runtime *runtime = substream->runtime;
713 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
715 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
716 return -EINVAL;
717 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP && (params->buffer_size & 0x1f) != 0)
718 return -EINVAL;
719 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
720 return -EINVAL;
721 if (params->buffer_size & get_align(runtime))
722 return -EINVAL;
723 if (params->buffer_size != runtime->buffer_size) {
724 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
726 return -ENOMEM;
727 guard(spinlock_irq)(&substream->lock);
728 if (runtime->buffer_ref) {
730 return -EBUSY;
732 oldbuf = runtime->buffer;
733 runtime->buffer = newbuf;
734 runtime->buffer_size = params->buffer_size;
738 runtime->avail_min = params->avail_min;
748 guard(mutex)(&substream->rmidi->open_mutex);
749 if (substream->append && substream->use_count > 1)
750 return -EBUSY;
753 substream->active_sensing = !params->no_active_sensing;
761 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
762 unsigned int clock_type = params->mode & SNDRV_RAWMIDI_MODE_CLOCK_MASK;
766 guard(mutex)(&substream->rmidi->open_mutex);
768 err = -EINVAL;
770 err = -EINVAL;
772 err = -EINVAL;
777 substream->framing = framing;
778 substream->clock_type = clock_type;
787 struct snd_rawmidi_runtime *runtime = substream->runtime;
790 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
791 guard(spinlock_irq)(&substream->lock);
792 status->avail = runtime->avail;
799 struct snd_rawmidi_runtime *runtime = substream->runtime;
802 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
803 guard(spinlock_irq)(&substream->lock);
804 status->avail = runtime->avail;
805 status->xruns = runtime->xruns;
806 runtime->xruns = 0;
820 return -EFAULT;
824 if (rfile->output == NULL)
825 return -EINVAL;
826 err = snd_rawmidi_output_status(rfile->output, &status64);
829 if (rfile->input == NULL)
830 return -EINVAL;
831 err = snd_rawmidi_input_status(rfile->input, &status64);
834 return -EINVAL;
848 return -EFAULT;
860 return -EFAULT;
864 if (rfile->output == NULL)
865 return -EINVAL;
866 err = snd_rawmidi_output_status(rfile->output, &status);
869 if (rfile->input == NULL)
870 return -EINVAL;
871 err = snd_rawmidi_input_status(rfile->input, &status);
874 return -EINVAL;
880 return -EFAULT;
890 rfile = file->private_data;
892 return -ENOTTY;
895 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
901 if (get_user(stream, &info->stream))
902 return -EFAULT;
905 return snd_rawmidi_info_user(rfile->input, info);
907 return snd_rawmidi_info_user(rfile->output, info);
909 return -EINVAL;
913 if (get_user(rfile->user_pversion, (unsigned int __user *)arg))
914 return -EFAULT;
922 return -EFAULT;
923 if (rfile->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 2)) {
929 if (rfile->output == NULL)
930 return -EINVAL;
931 return snd_rawmidi_output_params(rfile->output, ¶ms);
933 if (rfile->input == NULL)
934 return -EINVAL;
935 return snd_rawmidi_input_params(rfile->input, ¶ms);
937 return -EINVAL;
949 return -EFAULT;
952 if (rfile->output == NULL)
953 return -EINVAL;
954 return snd_rawmidi_drop_output(rfile->output);
956 return -EINVAL;
964 return -EFAULT;
967 if (rfile->output == NULL)
968 return -EINVAL;
969 return snd_rawmidi_drain_output(rfile->output);
971 if (rfile->input == NULL)
972 return -EINVAL;
973 return snd_rawmidi_drain_input(rfile->input);
975 return -EINVAL;
979 rmidi = rfile->rmidi;
980 if (rmidi->ops && rmidi->ops->ioctl)
981 return rmidi->ops->ioctl(rmidi, cmd, argp);
984 return -ENOTTY;
997 return -EFAULT;
998 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
999 device = SNDRV_RAWMIDI_DEVICES - 1;
1011 device = -1;
1014 return -EFAULT;
1027 if (get_user(device, &info->device))
1028 return -EFAULT;
1029 guard(mutex)(®ister_mutex);
1031 if (rmidi && rmidi->ops && rmidi->ops->ioctl)
1032 return rmidi->ops->ioctl(rmidi, cmd, argp);
1034 return -ENXIO;
1061 return -EFAULT;
1062 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
1068 return -ENOIOCTLCMD;
1074 struct snd_rawmidi_runtime *runtime = substream->runtime;
1076 struct snd_rawmidi_framing_tstamp frame = { .tv_sec = tstamp->tv_sec, .tv_nsec = tstamp->tv_nsec };
1082 if (snd_BUG_ON((runtime->hw_ptr & 0x1f) != 0))
1083 return -EINVAL;
1086 if ((int)(runtime->buffer_size - runtime->avail) < frame_size) {
1087 runtime->xruns += src_count;
1100 src_count -= frame.length;
1101 dest_ptr = (struct snd_rawmidi_framing_tstamp *) (runtime->buffer + runtime->hw_ptr);
1103 runtime->avail += frame_size;
1104 runtime->hw_ptr += frame_size;
1105 runtime->hw_ptr %= runtime->buffer_size;
1107 return orig_count - src_count;
1114 switch (substream->clock_type) {
1129 * snd_rawmidi_receive - receive the input data from the device
1145 guard(spinlock_irqsave)(&substream->lock);
1146 if (!substream->opened)
1147 return -EBADFD;
1148 runtime = substream->runtime;
1149 if (!runtime || !runtime->buffer) {
1150 rmidi_dbg(substream->rmidi,
1152 return -EINVAL;
1159 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1162 substream->bytes++;
1163 if (runtime->avail < runtime->buffer_size) {
1164 runtime->buffer[runtime->hw_ptr++] = buffer[0];
1165 runtime->hw_ptr %= runtime->buffer_size;
1166 runtime->avail++;
1169 runtime->xruns++;
1172 substream->bytes += count;
1173 count1 = runtime->buffer_size - runtime->hw_ptr;
1176 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1177 count1 = runtime->buffer_size - runtime->avail;
1181 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
1182 runtime->hw_ptr += count1;
1183 runtime->hw_ptr %= runtime->buffer_size;
1184 runtime->avail += count1;
1185 count -= count1;
1190 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
1191 count1 = runtime->buffer_size - runtime->avail;
1192 runtime->xruns += count - count1;
1195 memcpy(runtime->buffer, buffer, count1);
1196 runtime->hw_ptr = count1;
1197 runtime->avail += count1;
1203 if (runtime->event)
1204 schedule_work(&runtime->event_work);
1206 wake_up(&runtime->sleep);
1218 struct snd_rawmidi_runtime *runtime = substream->runtime;
1222 spin_lock_irqsave(&substream->lock, flags);
1224 while (count > 0 && runtime->avail) {
1225 count1 = runtime->buffer_size - runtime->appl_ptr;
1228 if (count1 > (int)runtime->avail)
1229 count1 = runtime->avail;
1231 /* update runtime->appl_ptr before unlocking for userbuf */
1232 appl_ptr = runtime->appl_ptr;
1233 runtime->appl_ptr += count1;
1234 runtime->appl_ptr %= runtime->buffer_size;
1235 runtime->avail -= count1;
1238 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
1240 spin_unlock_irqrestore(&substream->lock, flags);
1242 runtime->buffer + appl_ptr, count1))
1243 err = -EFAULT;
1244 spin_lock_irqsave(&substream->lock, flags);
1249 count -= count1;
1253 spin_unlock_irqrestore(&substream->lock, flags);
1274 rfile = file->private_data;
1275 substream = rfile->input;
1277 return -EIO;
1278 runtime = substream->runtime;
1282 spin_lock_irq(&substream->lock);
1286 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1287 spin_unlock_irq(&substream->lock);
1288 return result > 0 ? result : -EAGAIN;
1291 add_wait_queue(&runtime->sleep, &wait);
1293 spin_unlock_irq(&substream->lock);
1295 remove_wait_queue(&runtime->sleep, &wait);
1296 if (rfile->rmidi->card->shutdown)
1297 return -ENODEV;
1299 return result > 0 ? result : -ERESTARTSYS;
1300 spin_lock_irq(&substream->lock);
1301 if (!runtime->avail) {
1302 spin_unlock_irq(&substream->lock);
1303 return result > 0 ? result : -EIO;
1306 spin_unlock_irq(&substream->lock);
1315 count -= count1;
1321 * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1330 guard(spinlock_irqsave)(&substream->lock);
1331 runtime = substream->runtime;
1332 if (!substream->opened || !runtime || !runtime->buffer) {
1333 rmidi_dbg(substream->rmidi,
1337 return (runtime->avail >= runtime->buffer_size);
1342 * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1353 struct snd_rawmidi_runtime *runtime = substream->runtime;
1355 if (runtime->buffer == NULL) {
1356 rmidi_dbg(substream->rmidi,
1358 return -EINVAL;
1361 if (runtime->avail >= runtime->buffer_size) {
1366 *buffer = runtime->buffer[runtime->hw_ptr];
1369 count1 = runtime->buffer_size - runtime->hw_ptr;
1372 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1373 count1 = runtime->buffer_size - runtime->avail;
1377 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1378 count -= count1;
1381 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1382 count = runtime->buffer_size - runtime->avail - count1;
1386 memcpy(buffer + count1, runtime->buffer, count);
1395 * snd_rawmidi_transmit_peek - copy data from the internal buffer
1411 guard(spinlock_irqsave)(&substream->lock);
1412 if (!substream->opened || !substream->runtime)
1413 return -EBADFD;
1419 * __snd_rawmidi_transmit_ack - acknowledge the transmission
1428 struct snd_rawmidi_runtime *runtime = substream->runtime;
1430 if (runtime->buffer == NULL) {
1431 rmidi_dbg(substream->rmidi,
1433 return -EINVAL;
1435 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1437 runtime->hw_ptr += count;
1438 runtime->hw_ptr %= runtime->buffer_size;
1439 runtime->avail += count;
1440 substream->bytes += count;
1442 if (runtime->drain || __snd_rawmidi_ready(runtime))
1443 wake_up(&runtime->sleep);
1449 * snd_rawmidi_transmit_ack - acknowledge the transmission
1461 guard(spinlock_irqsave)(&substream->lock);
1462 if (!substream->opened || !substream->runtime)
1463 return -EBADFD;
1469 * snd_rawmidi_transmit - copy from the buffer to the device
1481 guard(spinlock_irqsave)(&substream->lock);
1482 if (!substream->opened)
1483 return -EBADFD;
1492 * snd_rawmidi_proceed - Discard the all pending bytes and proceed
1502 guard(spinlock_irqsave)(&substream->lock);
1503 runtime = substream->runtime;
1504 if (substream->opened && runtime &&
1505 runtime->avail < runtime->buffer_size) {
1506 count = runtime->buffer_size - runtime->avail;
1520 struct snd_rawmidi_runtime *runtime = substream->runtime;
1524 return -EINVAL;
1525 if (snd_BUG_ON(!runtime->buffer))
1526 return -EINVAL;
1529 spin_lock_irqsave(&substream->lock, flags);
1530 if (substream->append) {
1531 if ((long)runtime->avail < count) {
1532 spin_unlock_irqrestore(&substream->lock, flags);
1533 return -EAGAIN;
1537 while (count > 0 && runtime->avail > 0) {
1538 count1 = runtime->buffer_size - runtime->appl_ptr;
1541 if (count1 > (long)runtime->avail)
1542 count1 = runtime->avail;
1544 /* update runtime->appl_ptr before unlocking for userbuf */
1545 appl_ptr = runtime->appl_ptr;
1546 runtime->appl_ptr += count1;
1547 runtime->appl_ptr %= runtime->buffer_size;
1548 runtime->avail -= count1;
1551 memcpy(runtime->buffer + appl_ptr,
1554 spin_unlock_irqrestore(&substream->lock, flags);
1555 if (copy_from_user(runtime->buffer + appl_ptr,
1557 spin_lock_irqsave(&substream->lock, flags);
1558 result = result > 0 ? result : -EFAULT;
1561 spin_lock_irqsave(&substream->lock, flags);
1564 count -= count1;
1567 count1 = runtime->avail < runtime->buffer_size;
1569 spin_unlock_irqrestore(&substream->lock, flags);
1591 rfile = file->private_data;
1592 substream = rfile->output;
1593 runtime = substream->runtime;
1595 if (substream->append && count > runtime->buffer_size)
1596 return -EIO;
1599 spin_lock_irq(&substream->lock);
1603 if (file->f_flags & O_NONBLOCK) {
1604 spin_unlock_irq(&substream->lock);
1605 return result > 0 ? result : -EAGAIN;
1608 add_wait_queue(&runtime->sleep, &wait);
1610 spin_unlock_irq(&substream->lock);
1612 remove_wait_queue(&runtime->sleep, &wait);
1613 if (rfile->rmidi->card->shutdown)
1614 return -ENODEV;
1616 return result > 0 ? result : -ERESTARTSYS;
1617 spin_lock_irq(&substream->lock);
1618 if (!runtime->avail && !timeout) {
1619 spin_unlock_irq(&substream->lock);
1620 return result > 0 ? result : -EIO;
1623 spin_unlock_irq(&substream->lock);
1629 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1631 count -= count1;
1633 if (file->f_flags & O_DSYNC) {
1634 spin_lock_irq(&substream->lock);
1635 while (runtime->avail != runtime->buffer_size) {
1637 unsigned int last_avail = runtime->avail;
1640 add_wait_queue(&runtime->sleep, &wait);
1642 spin_unlock_irq(&substream->lock);
1644 remove_wait_queue(&runtime->sleep, &wait);
1646 return result > 0 ? result : -ERESTARTSYS;
1647 if (runtime->avail == last_avail && !timeout)
1648 return result > 0 ? result : -EIO;
1649 spin_lock_irq(&substream->lock);
1651 spin_unlock_irq(&substream->lock);
1662 rfile = file->private_data;
1663 if (rfile->input != NULL) {
1664 runtime = rfile->input->runtime;
1665 snd_rawmidi_input_trigger(rfile->input, 1);
1666 poll_wait(file, &runtime->sleep, wait);
1668 if (rfile->output != NULL) {
1669 runtime = rfile->output->runtime;
1670 poll_wait(file, &runtime->sleep, wait);
1673 if (rfile->input != NULL) {
1674 if (snd_rawmidi_ready(rfile->input))
1677 if (rfile->output != NULL) {
1678 if (snd_rawmidi_ready(rfile->output))
1705 rmidi = entry->private_data;
1706 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1710 if (rmidi->ops && rmidi->ops->proc_read)
1711 rmidi->ops->proc_read(entry, buffer);
1712 guard(mutex)(&rmidi->open_mutex);
1713 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1715 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1720 substream->number,
1721 (unsigned long) substream->bytes);
1722 if (substream->opened) {
1725 pid_vnr(substream->pid));
1726 runtime = substream->runtime;
1727 scoped_guard(spinlock_irq, &substream->lock) {
1728 buffer_size = runtime->buffer_size;
1729 avail = runtime->avail;
1735 runtime->oss ? "OSS compatible" : "native",
1740 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1742 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1747 substream->number,
1748 (unsigned long) substream->bytes);
1749 if (substream->opened) {
1752 pid_vnr(substream->pid));
1753 runtime = substream->runtime;
1754 scoped_guard(spinlock_irq, &substream->lock) {
1755 buffer_size = runtime->buffer_size;
1756 avail = runtime->avail;
1757 xruns = runtime->xruns;
1764 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1765 clock_type = substream->clock_type >> SNDRV_RAWMIDI_MODE_CLOCK_SHIFT;
1803 return -ENOMEM;
1804 substream->stream = direction;
1805 substream->number = idx;
1806 substream->rmidi = rmidi;
1807 substream->pstr = stream;
1808 spin_lock_init(&substream->lock);
1809 list_add_tail(&substream->list, &stream->substreams);
1810 stream->substream_count++;
1828 rmidi->card = card;
1829 rmidi->device = device;
1830 mutex_init(&rmidi->open_mutex);
1831 init_waitqueue_head(&rmidi->open_wait);
1832 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1833 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1834 rmidi->info_flags = info_flags;
1837 strscpy(rmidi->id, id, sizeof(rmidi->id));
1839 err = snd_device_alloc(&rmidi->dev, card);
1843 dev_set_name(rmidi->dev, "umpC%iD%i", card->number, device);
1845 dev_set_name(rmidi->dev, "midiC%iD%i", card->number, device);
1848 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1854 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1867 * snd_rawmidi_new - create a rawmidi instance
1891 return -ENOMEM;
1908 while (!list_empty(&stream->substreams)) {
1909 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1910 list_del(&substream->list);
1921 snd_info_free_entry(rmidi->proc_entry);
1922 rmidi->proc_entry = NULL;
1923 if (rmidi->ops && rmidi->ops->dev_unregister)
1924 rmidi->ops->dev_unregister(rmidi);
1926 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1927 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1928 if (rmidi->private_free)
1929 rmidi->private_free(rmidi);
1930 put_device(rmidi->dev);
1938 struct snd_rawmidi *rmidi = device->device_data;
1946 struct snd_rawmidi *rmidi = device->private_data;
1948 rmidi->seq_dev = NULL;
1957 struct snd_rawmidi *rmidi = device->device_data;
1959 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1960 return -ENOMEM;
1963 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1964 err = -EBUSY;
1966 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1972 rmidi->card, rmidi->device,
1973 &snd_rawmidi_f_ops, rmidi, rmidi->dev);
1978 if (rmidi->ops && rmidi->ops->dev_register) {
1979 err = rmidi->ops->dev_register(rmidi);
1984 rmidi->ossreg = 0;
1986 (int)rmidi->device == midi_map[rmidi->card->number]) {
1988 rmidi->card, 0, &snd_rawmidi_f_ops,
1992 rmidi->card->number, 0);
1994 rmidi->ossreg++;
1996 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
2001 (int)rmidi->device == amidi_map[rmidi->card->number]) {
2003 rmidi->card, 1, &snd_rawmidi_f_ops,
2007 rmidi->card->number, 1);
2009 rmidi->ossreg++;
2013 sprintf(name, "midi%d", rmidi->device);
2014 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
2016 entry->private_data = rmidi;
2017 entry->c.text.read = snd_rawmidi_proc_info_read;
2023 rmidi->proc_entry = entry;
2026 if (!rmidi->ops || !rmidi->ops->dev_register) {
2027 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
2028 rmidi->seq_dev->private_data = rmidi;
2029 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
2030 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
2031 snd_device_register(rmidi->card, rmidi->seq_dev);
2038 snd_unregister_device(rmidi->dev);
2041 list_del(&rmidi->list);
2047 struct snd_rawmidi *rmidi = device->device_data;
2050 guard(mutex)(®ister_mutex);
2051 guard(mutex)(&rmidi->open_mutex);
2052 wake_up(&rmidi->open_wait);
2053 list_del_init(&rmidi->list);
2057 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
2058 if (s->runtime)
2059 wake_up(&s->runtime->sleep);
2064 if (rmidi->ossreg) {
2065 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
2066 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
2068 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
2071 if ((int)rmidi->device == amidi_map[rmidi->card->number])
2072 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
2073 rmidi->ossreg = 0;
2076 snd_unregister_device(rmidi->dev);
2081 * snd_rawmidi_set_ops - set the rawmidi operators
2093 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
2094 substream->ops = ops;