Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Abstract layer for MIDI v1.0 stream |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <sound/core.h> |
| 23 | #include <linux/major.h> |
| 24 | #include <linux/init.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 25 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/time.h> |
| 28 | #include <linux/wait.h> |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 29 | #include <linux/mutex.h> |
Paul Gortmaker | 65a7721 | 2011-07-15 13:13:37 -0400 | [diff] [blame] | 30 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/delay.h> |
Takashi Iwai | ef4db23 | 2018-07-17 23:12:33 +0200 | [diff] [blame] | 32 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 39 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA."); |
| 41 | MODULE_LICENSE("GPL"); |
| 42 | |
| 43 | #ifdef CONFIG_SND_OSSEMUL |
Takashi Iwai | 6581f4e | 2006-05-17 17:14:51 +0200 | [diff] [blame] | 44 | static int midi_map[SNDRV_CARDS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 57 | static LIST_HEAD(snd_rawmidi_devices); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 58 | static DEFINE_MUTEX(register_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 60 | #define rmidi_err(rmidi, fmt, args...) \ |
Takashi Iwai | 07cc3e8 | 2015-01-30 15:31:52 +0100 | [diff] [blame] | 61 | dev_err(&(rmidi)->dev, fmt, ##args) |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 62 | #define rmidi_warn(rmidi, fmt, args...) \ |
Takashi Iwai | 07cc3e8 | 2015-01-30 15:31:52 +0100 | [diff] [blame] | 63 | dev_warn(&(rmidi)->dev, fmt, ##args) |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 64 | #define rmidi_dbg(rmidi, fmt, args...) \ |
Takashi Iwai | 07cc3e8 | 2015-01-30 15:31:52 +0100 | [diff] [blame] | 65 | dev_dbg(&(rmidi)->dev, fmt, ##args) |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 66 | |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 67 | static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device) |
| 68 | { |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 69 | struct snd_rawmidi *rawmidi; |
| 70 | |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 71 | list_for_each_entry(rawmidi, &snd_rawmidi_devices, list) |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 72 | if (rawmidi->card == card && rawmidi->device == device) |
| 73 | return rawmidi; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 74 | return NULL; |
| 75 | } |
| 76 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 89 | static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 91 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | return runtime->avail >= runtime->avail_min; |
| 94 | } |
| 95 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 96 | static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream, |
| 97 | size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 99 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return runtime->avail >= runtime->avail_min && |
| 102 | (!substream->append || runtime->avail >= count); |
| 103 | } |
| 104 | |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 105 | static void snd_rawmidi_input_event_work(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | { |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 107 | struct snd_rawmidi_runtime *runtime = |
| 108 | container_of(work, struct snd_rawmidi_runtime, event_work); |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 109 | |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 110 | if (runtime->event) |
| 111 | runtime->event(runtime->substream); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 114 | static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 116 | struct snd_rawmidi_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 118 | runtime = kzalloc(sizeof(*runtime), GFP_KERNEL); |
| 119 | if (!runtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | return -ENOMEM; |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 121 | runtime->substream = substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | spin_lock_init(&runtime->lock); |
| 123 | init_waitqueue_head(&runtime->sleep); |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 124 | INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 5a7b44a | 2018-09-03 15:16:43 +0200 | [diff] [blame] | 132 | runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL); |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 133 | if (!runtime->buffer) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | kfree(runtime); |
| 135 | return -ENOMEM; |
| 136 | } |
| 137 | runtime->appl_ptr = runtime->hw_ptr = 0; |
| 138 | substream->runtime = runtime; |
| 139 | return 0; |
| 140 | } |
| 141 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 142 | static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 144 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Takashi Iwai | ef4db23 | 2018-07-17 23:12:33 +0200 | [diff] [blame] | 146 | kvfree(runtime->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | kfree(runtime); |
| 148 | substream->runtime = NULL; |
| 149 | return 0; |
| 150 | } |
| 151 | |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 152 | static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Takashi Iwai | 219df32 | 2008-11-03 08:17:05 +0100 | [diff] [blame] | 154 | if (!substream->opened) |
| 155 | return; |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 156 | substream->ops->trigger(substream, up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 159 | static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | { |
Takashi Iwai | 219df32 | 2008-11-03 08:17:05 +0100 | [diff] [blame] | 161 | if (!substream->opened) |
| 162 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | substream->ops->trigger(substream, up); |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 164 | if (!up) |
| 165 | cancel_work_sync(&substream->runtime->event_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | { |
| 179 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | spin_lock_irqsave(&runtime->lock, flags); |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 182 | __reset_runtime_ptrs(runtime, is_input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | spin_unlock_irqrestore(&runtime->lock, flags); |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 192 | EXPORT_SYMBOL(snd_rawmidi_drop_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 194 | int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
| 196 | int err; |
| 197 | long timeout; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 198 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 208 | rmidi_warn(substream->rmidi, |
| 209 | "rawmidi drain error (avail = %li, buffer_size = %li)\n", |
| 210 | (long)runtime->avail, (long)runtime->buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 224 | EXPORT_SYMBOL(snd_rawmidi_drain_output); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 226 | int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | snd_rawmidi_input_trigger(substream, 0); |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 229 | reset_runtime_ptrs(substream->runtime, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return 0; |
| 231 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 232 | EXPORT_SYMBOL(snd_rawmidi_drain_input); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 252 | |
| 253 | list_for_each_entry(substream, &s->substreams, list) { |
| 254 | if (substream->opened) { |
| 255 | if (stream == SNDRV_RAWMIDI_STREAM_INPUT || |
Clemens Ladisch | 16fb109 | 2009-10-21 09:10:16 +0200 | [diff] [blame] | 256 | !(mode & SNDRV_RAWMIDI_LFLG_APPEND) || |
| 257 | !substream->append) |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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 | |
Clemens Ladisch | 8579d2d | 2009-10-21 09:09:38 +0200 | [diff] [blame] | 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); |
Clemens Ladisch | b7fe750 | 2009-10-21 09:11:43 +0200 | [diff] [blame] | 280 | if (err < 0) { |
| 281 | snd_rawmidi_runtime_free(substream); |
Clemens Ladisch | 8579d2d | 2009-10-21 09:09:38 +0200 | [diff] [blame] | 282 | return err; |
Clemens Ladisch | b7fe750 | 2009-10-21 09:11:43 +0200 | [diff] [blame] | 283 | } |
Clemens Ladisch | 8579d2d | 2009-10-21 09:09:38 +0200 | [diff] [blame] | 284 | substream->opened = 1; |
Clemens Ladisch | 2d4b842 | 2009-07-13 13:52:46 +0200 | [diff] [blame] | 285 | substream->active_sensing = 0; |
Clemens Ladisch | 8579d2d | 2009-10-21 09:09:38 +0200 | [diff] [blame] | 286 | if (mode & SNDRV_RAWMIDI_LFLG_APPEND) |
| 287 | substream->append = 1; |
Clemens Ladisch | 7584af1 | 2009-11-10 10:14:04 +0100 | [diff] [blame] | 288 | substream->pid = get_pid(task_pid(current)); |
Clemens Ladisch | 91d12c4 | 2009-10-21 09:12:26 +0200 | [diff] [blame] | 289 | rmidi->streams[substream->stream].substream_opened++; |
Clemens Ladisch | 8579d2d | 2009-10-21 09:09:38 +0200 | [diff] [blame] | 290 | } |
| 291 | substream->use_count++; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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) |
Clemens Ladisch | b7fe750 | 2009-10-21 09:11:43 +0200 | [diff] [blame] | 311 | return err; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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) |
Clemens Ladisch | b7fe750 | 2009-10-21 09:11:43 +0200 | [diff] [blame] | 318 | return err; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | if (sinput) { |
| 322 | err = open_substream(rmidi, sinput, mode); |
| 323 | if (err < 0) |
Clemens Ladisch | b7fe750 | 2009-10-21 09:11:43 +0200 | [diff] [blame] | 324 | return err; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 325 | } |
| 326 | if (soutput) { |
| 327 | err = open_substream(rmidi, soutput, mode); |
| 328 | if (err < 0) { |
| 329 | if (sinput) |
| 330 | close_substream(rmidi, sinput, 0); |
Clemens Ladisch | b7fe750 | 2009-10-21 09:11:43 +0200 | [diff] [blame] | 331 | return err; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | rfile->rmidi = rmidi; |
| 336 | rfile->input = sinput; |
| 337 | rfile->output = soutput; |
| 338 | return 0; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /* called from sound/core/seq/seq_midi.c */ |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 342 | int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice, |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 343 | int mode, struct snd_rawmidi_file *rfile) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 345 | struct snd_rawmidi *rmidi; |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 346 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 348 | if (snd_BUG_ON(!rfile)) |
| 349 | return -EINVAL; |
| 350 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 351 | mutex_lock(®ister_mutex); |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 352 | rmidi = snd_rawmidi_search(card, device); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 353 | if (!rmidi) |
| 354 | err = -ENODEV; |
| 355 | else if (!try_module_get(rmidi->card->module)) |
| 356 | err = -ENXIO; |
Takashi Iwai | f9d2028 | 2009-02-11 14:55:59 +0100 | [diff] [blame] | 357 | mutex_unlock(®ister_mutex); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 358 | if (err < 0) |
| 359 | return err; |
Takashi Iwai | f9d2028 | 2009-02-11 14:55:59 +0100 | [diff] [blame] | 360 | |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | return err; |
| 367 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 368 | EXPORT_SYMBOL(snd_rawmidi_kernel_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | static int snd_rawmidi_open(struct inode *inode, struct file *file) |
| 371 | { |
| 372 | int maj = imajor(inode); |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 373 | struct snd_card *card; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 374 | int subdevice; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | unsigned short fflags; |
| 376 | int err; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 377 | struct snd_rawmidi *rmidi; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 378 | struct snd_rawmidi_file *rawmidi_file = NULL; |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 379 | wait_queue_entry_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 381 | if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK)) |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 382 | return -EINVAL; /* invalid combination */ |
| 383 | |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 384 | err = nonseekable_open(inode, file); |
| 385 | if (err < 0) |
| 386 | return err; |
| 387 | |
Clemens Ladisch | f190286 | 2005-10-24 17:05:03 +0200 | [diff] [blame] | 388 | if (maj == snd_major) { |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 389 | rmidi = snd_lookup_minor_data(iminor(inode), |
| 390 | SNDRV_DEVICE_TYPE_RAWMIDI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | #ifdef CONFIG_SND_OSSEMUL |
Clemens Ladisch | f190286 | 2005-10-24 17:05:03 +0200 | [diff] [blame] | 392 | } else if (maj == SOUND_MAJOR) { |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 393 | rmidi = snd_lookup_oss_minor_data(iminor(inode), |
| 394 | SNDRV_OSS_DEVICE_TYPE_MIDI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | #endif |
Clemens Ladisch | f190286 | 2005-10-24 17:05:03 +0200 | [diff] [blame] | 396 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (rmidi == NULL) |
| 400 | return -ENODEV; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 401 | |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 402 | if (!try_module_get(rmidi->card->module)) { |
| 403 | snd_card_unref(rmidi->card); |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 404 | return -ENXIO; |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 405 | } |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 406 | |
| 407 | mutex_lock(&rmidi->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | card = rmidi->card; |
| 409 | err = snd_card_file_add(card, file); |
| 410 | if (err < 0) |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 411 | goto __error_card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | fflags = snd_rawmidi_file_flags(file); |
Clemens Ladisch | f190286 | 2005-10-24 17:05:03 +0200 | [diff] [blame] | 413 | if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | fflags |= SNDRV_RAWMIDI_LFLG_APPEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL); |
| 416 | if (rawmidi_file == NULL) { |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 417 | err = -ENOMEM; |
| 418 | goto __error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | init_waitqueue_entry(&wait, current); |
| 421 | add_wait_queue(&rmidi->open_wait, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | while (1) { |
Takashi Iwai | 23c18d4 | 2014-02-19 14:30:29 +0100 | [diff] [blame] | 423 | subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI); |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 424 | err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 435 | mutex_unlock(&rmidi->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | schedule(); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 437 | mutex_lock(&rmidi->open_mutex); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 438 | if (rmidi->card->shutdown) { |
| 439 | err = -ENODEV; |
| 440 | break; |
| 441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | if (signal_pending(current)) { |
| 443 | err = -ERESTARTSYS; |
| 444 | break; |
| 445 | } |
| 446 | } |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 447 | remove_wait_queue(&rmidi->open_wait, &wait); |
| 448 | if (err < 0) { |
| 449 | kfree(rawmidi_file); |
| 450 | goto __error; |
| 451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 458 | file->private_data = rawmidi_file; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 459 | mutex_unlock(&rmidi->open_mutex); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 460 | snd_card_unref(rmidi->card); |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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); |
Takashi Iwai | a0830db | 2012-10-16 13:05:59 +0200 | [diff] [blame] | 468 | snd_card_unref(rmidi->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | return err; |
| 470 | } |
| 471 | |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 472 | static void close_substream(struct snd_rawmidi *rmidi, |
| 473 | struct snd_rawmidi_substream *substream, |
| 474 | int cleanup) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | { |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 476 | if (--substream->use_count) |
| 477 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 479 | if (cleanup) { |
| 480 | if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT) |
| 481 | snd_rawmidi_input_trigger(substream, 0); |
| 482 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | if (substream->active_sensing) { |
| 484 | unsigned char buf = 0xfe; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 485 | /* sending single active sensing message |
| 486 | * to shut the device up |
| 487 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | snd_rawmidi_kernel_write(substream, &buf, 1); |
| 489 | } |
| 490 | if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS) |
| 491 | snd_rawmidi_output_trigger(substream, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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; |
Clemens Ladisch | 7584af1 | 2009-11-10 10:14:04 +0100 | [diff] [blame] | 500 | put_pid(substream->pid); |
| 501 | substream->pid = NULL; |
Clemens Ladisch | 91d12c4 | 2009-10-21 09:12:26 +0200 | [diff] [blame] | 502 | rmidi->streams[substream->stream].substream_opened--; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 520 | mutex_unlock(&rmidi->open_mutex); |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 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; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 531 | |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 532 | rmidi = rfile->rmidi; |
| 533 | rawmidi_release_priv(rfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | module_put(rmidi->card->module); |
| 535 | return 0; |
| 536 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 537 | EXPORT_SYMBOL(snd_rawmidi_kernel_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
| 539 | static int snd_rawmidi_release(struct inode *inode, struct file *file) |
| 540 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 541 | struct snd_rawmidi_file *rfile; |
| 542 | struct snd_rawmidi *rmidi; |
Clemens Ladisch | aa73aec | 2010-10-15 12:06:18 +0200 | [diff] [blame] | 543 | struct module *module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
| 545 | rfile = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | rmidi = rfile->rmidi; |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 547 | rawmidi_release_priv(rfile); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | kfree(rfile); |
Clemens Ladisch | aa73aec | 2010-10-15 12:06:18 +0200 | [diff] [blame] | 549 | module = rmidi->card->module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | snd_card_file_remove(rmidi->card, file); |
Clemens Ladisch | aa73aec | 2010-10-15 12:06:18 +0200 | [diff] [blame] | 551 | module_put(module); |
Takashi Iwai | 9a1b64c | 2009-02-11 17:03:49 +0100 | [diff] [blame] | 552 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Adrian Bunk | 1861204 | 2005-11-23 13:14:50 +0100 | [diff] [blame] | 555 | static int snd_rawmidi_info(struct snd_rawmidi_substream *substream, |
| 556 | struct snd_rawmidi_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 558 | struct snd_rawmidi *rmidi; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 578 | static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream, |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 579 | struct snd_rawmidi_info __user *_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 581 | struct snd_rawmidi_info info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | int err; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 583 | |
| 584 | err = snd_rawmidi_info(substream, &info); |
| 585 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | return err; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 587 | if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | return -EFAULT; |
| 589 | return 0; |
| 590 | } |
| 591 | |
Takashi Iwai | c1cfd90 | 2017-12-14 16:44:12 +0100 | [diff] [blame] | 592 | static int __snd_rawmidi_info_select(struct snd_card *card, |
| 593 | struct snd_rawmidi_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 595 | struct snd_rawmidi *rmidi; |
| 596 | struct snd_rawmidi_str *pstr; |
| 597 | struct snd_rawmidi_substream *substream; |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 598 | |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 599 | rmidi = snd_rawmidi_search(card, info->device); |
Clemens Ladisch | a106cd3 | 2005-11-20 13:59:56 +0100 | [diff] [blame] | 600 | if (!rmidi) |
| 601 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 609 | list_for_each_entry(substream, &pstr->substreams, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if ((unsigned int)substream->number == info->subdevice) |
| 611 | return snd_rawmidi_info(substream, info); |
| 612 | } |
| 613 | return -ENXIO; |
| 614 | } |
Takashi Iwai | c1cfd90 | 2017-12-14 16:44:12 +0100 | [diff] [blame] | 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 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 625 | EXPORT_SYMBOL(snd_rawmidi_info_select); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 627 | static int snd_rawmidi_info_select_user(struct snd_card *card, |
| 628 | struct snd_rawmidi_info __user *_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
| 630 | int err; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 631 | struct snd_rawmidi_info info; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 639 | err = snd_rawmidi_info_select(card, &info); |
| 640 | if (err < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | return err; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 642 | if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | return -EFAULT; |
| 644 | return 0; |
| 645 | } |
| 646 | |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 647 | static int resize_runtime_buffer(struct snd_rawmidi_runtime *runtime, |
| 648 | struct snd_rawmidi_params *params, |
| 649 | bool is_input) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | { |
Takashi Iwai | 39675f7 | 2018-07-17 17:26:43 +0200 | [diff] [blame] | 651 | char *newbuf, *oldbuf; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 652 | |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 653 | if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | return -EINVAL; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 655 | if (params->avail_min < 1 || params->avail_min > params->buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (params->buffer_size != runtime->buffer_size) { |
Takashi Iwai | 5a7b44a | 2018-09-03 15:16:43 +0200 | [diff] [blame] | 658 | newbuf = kvzalloc(params->buffer_size, GFP_KERNEL); |
Jesper Juhl | 4fa95ef | 2006-03-28 01:56:54 -0800 | [diff] [blame] | 659 | if (!newbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | return -ENOMEM; |
Takashi Iwai | 39675f7 | 2018-07-17 17:26:43 +0200 | [diff] [blame] | 661 | spin_lock_irq(&runtime->lock); |
| 662 | oldbuf = runtime->buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | runtime->buffer = newbuf; |
| 664 | runtime->buffer_size = params->buffer_size; |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 665 | __reset_runtime_ptrs(runtime, is_input); |
Takashi Iwai | 39675f7 | 2018-07-17 17:26:43 +0200 | [diff] [blame] | 666 | spin_unlock_irq(&runtime->lock); |
Takashi Iwai | ef4db23 | 2018-07-17 23:12:33 +0200 | [diff] [blame] | 667 | kvfree(oldbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | runtime->avail_min = params->avail_min; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | return 0; |
| 671 | } |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 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 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 682 | EXPORT_SYMBOL(snd_rawmidi_output_params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 684 | int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream, |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 685 | struct snd_rawmidi_params *params) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | snd_rawmidi_drain_input(substream); |
Takashi Iwai | f5beb59 | 2018-07-17 23:07:29 +0200 | [diff] [blame] | 688 | return resize_runtime_buffer(substream->runtime, params, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 690 | EXPORT_SYMBOL(snd_rawmidi_input_params); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 692 | static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream, |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 693 | struct snd_rawmidi_status *status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 695 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 705 | static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream, |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 706 | struct snd_rawmidi_status *status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 708 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 722 | struct snd_rawmidi_file *rfile; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 733 | int stream; |
| 734 | struct snd_rawmidi_info __user *info = argp; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 735 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 749 | struct snd_rawmidi_params params; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 750 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 751 | if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 769 | struct snd_rawmidi_status status; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 770 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 771 | if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 789 | if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | return -EFAULT; |
| 791 | return 0; |
| 792 | } |
| 793 | case SNDRV_RAWMIDI_IOCTL_DROP: |
| 794 | { |
| 795 | int val; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | default: |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 828 | rmidi_dbg(rfile->rmidi, |
| 829 | "rawmidi: unknown command = 0x%x\n", cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | return -ENOTTY; |
| 832 | } |
| 833 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 834 | static int snd_rawmidi_control_ioctl(struct snd_card *card, |
| 835 | struct snd_ctl_file *control, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | unsigned int cmd, |
| 837 | unsigned long arg) |
| 838 | { |
| 839 | void __user *argp = (void __user *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | switch (cmd) { |
| 842 | case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE: |
| 843 | { |
| 844 | int device; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 845 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | if (get_user(device, (int __user *)argp)) |
| 847 | return -EFAULT; |
Dan Carpenter | a7a13d0 | 2010-09-09 00:11:41 +0200 | [diff] [blame] | 848 | if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */ |
| 849 | device = SNDRV_RAWMIDI_DEVICES - 1; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 850 | mutex_lock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | device = device < 0 ? 0 : device + 1; |
| 852 | while (device < SNDRV_RAWMIDI_DEVICES) { |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 853 | if (snd_rawmidi_search(card, device)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | break; |
| 855 | device++; |
| 856 | } |
| 857 | if (device == SNDRV_RAWMIDI_DEVICES) |
| 858 | device = -1; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 859 | mutex_unlock(®ister_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | if (get_user(val, (int __user *)argp)) |
| 869 | return -EFAULT; |
Takashi Iwai | 23c18d4 | 2014-02-19 14:30:29 +0100 | [diff] [blame] | 870 | control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 887 | * Return: The size of read data, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | */ |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 889 | int snd_rawmidi_receive(struct snd_rawmidi_substream *substream, |
| 890 | const unsigned char *buffer, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | { |
| 892 | unsigned long flags; |
| 893 | int result = 0, count1; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 894 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Takashi Iwai | 219df32 | 2008-11-03 08:17:05 +0100 | [diff] [blame] | 896 | if (!substream->opened) |
| 897 | return -EBADFD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | if (runtime->buffer == NULL) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 899 | rmidi_dbg(substream->rmidi, |
| 900 | "snd_rawmidi_receive: input is not active!!!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Takashi Iwai | b3c705a | 2011-06-14 14:37:06 +0200 | [diff] [blame] | 944 | schedule_work(&runtime->event_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | else if (snd_rawmidi_ready(substream)) |
| 946 | wake_up(&runtime->sleep); |
| 947 | } |
| 948 | spin_unlock_irqrestore(&runtime->lock, flags); |
| 949 | return result; |
| 950 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 951 | EXPORT_SYMBOL(snd_rawmidi_receive); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 953 | static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream, |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 954 | unsigned char __user *userbuf, |
| 955 | unsigned char *kernelbuf, long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | { |
| 957 | unsigned long flags; |
| 958 | long result = 0, count1; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 959 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 960 | unsigned long appl_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 962 | spin_lock_irqsave(&runtime->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | while (count > 0 && runtime->avail) { |
| 964 | count1 = runtime->buffer_size - runtime->appl_ptr; |
| 965 | if (count1 > count) |
| 966 | count1 = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | if (count1 > (int)runtime->avail) |
| 968 | count1 = runtime->avail; |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 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 | |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 976 | if (kernelbuf) |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 977 | memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1); |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 978 | if (userbuf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | spin_unlock_irqrestore(&runtime->lock, flags); |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 980 | if (copy_to_user(userbuf + result, |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 981 | runtime->buffer + appl_ptr, count1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | return result > 0 ? result : -EFAULT; |
| 983 | } |
| 984 | spin_lock_irqsave(&runtime->lock, flags); |
| 985 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | result += count1; |
| 987 | count -= count1; |
| 988 | } |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 989 | spin_unlock_irqrestore(&runtime->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | return result; |
| 991 | } |
| 992 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 993 | long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream, |
| 994 | unsigned char *buf, long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | { |
| 996 | snd_rawmidi_input_trigger(substream, 1); |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 997 | return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 999 | EXPORT_SYMBOL(snd_rawmidi_kernel_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1001 | static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, |
| 1002 | loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
| 1004 | long result; |
| 1005 | int count1; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1006 | struct snd_rawmidi_file *rfile; |
| 1007 | struct snd_rawmidi_substream *substream; |
| 1008 | struct snd_rawmidi_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1020 | wait_queue_entry_t wait; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1021 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1032 | if (rfile->rmidi->card->shutdown) |
| 1033 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Clemens Ladisch | 4d23359 | 2005-09-05 10:35:20 +0200 | [diff] [blame] | 1041 | count1 = snd_rawmidi_kernel_read1(substream, |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 1042 | (unsigned char __user *)buf, |
| 1043 | NULL/*kernelbuf*/, |
| 1044 | count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1057 | * |
| 1058 | * Return: 1 if the internal output buffer is empty, 0 if not. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | */ |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1060 | int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1062 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | int result; |
| 1064 | unsigned long flags; |
| 1065 | |
| 1066 | if (runtime->buffer == NULL) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 1067 | rmidi_dbg(substream->rmidi, |
| 1068 | "snd_rawmidi_transmit_empty: output is not active!!!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1074 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 1076 | EXPORT_SYMBOL(snd_rawmidi_transmit_empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | |
| 1078 | /** |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 1079 | * __snd_rawmidi_transmit_peek - copy data from the internal buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | * @substream: the rawmidi substream |
| 1081 | * @buffer: the buffer pointer |
| 1082 | * @count: data size to transfer |
| 1083 | * |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 1084 | * This is a variant of snd_rawmidi_transmit_peek() without spinlock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | */ |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 1086 | int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream, |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1087 | unsigned char *buffer, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | int result, count1; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1090 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
| 1092 | if (runtime->buffer == NULL) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 1093 | rmidi_dbg(substream->rmidi, |
| 1094 | "snd_rawmidi_transmit_peek: output is not active!!!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | return -EINVAL; |
| 1096 | } |
| 1097 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | spin_unlock_irqrestore(&runtime->lock, flags); |
| 1150 | return result; |
| 1151 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 1152 | EXPORT_SYMBOL(snd_rawmidi_transmit_peek); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | |
| 1154 | /** |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 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 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | * snd_rawmidi_transmit_ack - acknowledge the transmission |
| 1185 | * @substream: the rawmidi substream |
Masanari Iida | 0a11458 | 2014-02-09 00:47:36 +0900 | [diff] [blame] | 1186 | * @count: the transferred count |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1192 | * Return: The advanced size if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | */ |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1194 | int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1196 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 1197 | int result; |
| 1198 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | spin_lock_irqsave(&runtime->lock, flags); |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 1201 | result = __snd_rawmidi_transmit_ack(substream, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | spin_unlock_irqrestore(&runtime->lock, flags); |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 1203 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 1205 | EXPORT_SYMBOL(snd_rawmidi_transmit_ack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
| 1207 | /** |
| 1208 | * snd_rawmidi_transmit - copy from the buffer to the device |
| 1209 | * @substream: the rawmidi substream |
Takashi Iwai | df8db93 | 2005-09-07 13:38:19 +0200 | [diff] [blame] | 1210 | * @buffer: the buffer pointer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | * @count: the data size to transfer |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1212 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | * Copies data from the buffer to the device and advances the pointer. |
| 1214 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1215 | * Return: The copied size if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | */ |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1217 | int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream, |
| 1218 | unsigned char *buffer, int count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | { |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 1220 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
| 1221 | int result; |
| 1222 | unsigned long flags; |
| 1223 | |
| 1224 | spin_lock_irqsave(&runtime->lock, flags); |
Takashi Iwai | 219df32 | 2008-11-03 08:17:05 +0100 | [diff] [blame] | 1225 | if (!substream->opened) |
Takashi Iwai | 06ab300 | 2016-01-31 11:57:41 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 1237 | EXPORT_SYMBOL(snd_rawmidi_transmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | |
Takashi Iwai | 6aea570 | 2018-09-13 08:20:43 +0200 | [diff] [blame] | 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 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1261 | static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream, |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 1262 | const unsigned char __user *userbuf, |
| 1263 | const unsigned char *kernelbuf, |
| 1264 | long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | { |
| 1266 | unsigned long flags; |
| 1267 | long count1, result; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1268 | struct snd_rawmidi_runtime *runtime = substream->runtime; |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 1269 | unsigned long appl_ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
Takashi Iwai | cc85f7a | 2016-02-01 12:04:55 +0100 | [diff] [blame] | 1271 | if (!kernelbuf && !userbuf) |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1272 | return -EINVAL; |
| 1273 | if (snd_BUG_ON(!runtime->buffer)) |
| 1274 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 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 | |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 1297 | if (kernelbuf) |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 1298 | memcpy(runtime->buffer + appl_ptr, |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 1299 | kernelbuf + result, count1); |
| 1300 | else if (userbuf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | spin_unlock_irqrestore(&runtime->lock, flags); |
Takashi Iwai | 81f5775 | 2016-02-03 14:41:22 +0100 | [diff] [blame] | 1302 | if (copy_from_user(runtime->buffer + appl_ptr, |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 1303 | userbuf + result, count1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | result += count1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1321 | long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream, |
| 1322 | const unsigned char *buf, long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | { |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 1324 | return snd_rawmidi_kernel_write1(substream, NULL, buf, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 1326 | EXPORT_SYMBOL(snd_rawmidi_kernel_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1328 | static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, |
| 1329 | size_t count, loff_t *offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | { |
| 1331 | long result, timeout; |
| 1332 | int count1; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1333 | struct snd_rawmidi_file *rfile; |
| 1334 | struct snd_rawmidi_runtime *runtime; |
| 1335 | struct snd_rawmidi_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1347 | wait_queue_entry_t wait; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1359 | if (rfile->rmidi->card->shutdown) |
| 1360 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Marcin Åšlusarz | 17596a8 | 2008-01-09 17:56:07 +0100 | [diff] [blame] | 1368 | count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 1377 | if (file->f_flags & O_DSYNC) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | spin_lock_irq(&runtime->lock); |
| 1379 | while (runtime->avail != runtime->buffer_size) { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 1380 | wait_queue_entry_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | unsigned int last_avail = runtime->avail; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1400 | static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1402 | struct snd_rawmidi_file *rfile; |
| 1403 | struct snd_rawmidi_runtime *runtime; |
Al Viro | 680ef72 | 2017-07-02 23:27:36 -0400 | [diff] [blame] | 1404 | __poll_t mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1419 | mask |= EPOLLIN | EPOLLRDNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1420 | } |
| 1421 | if (rfile->output != NULL) { |
| 1422 | if (snd_rawmidi_ready(rfile->output)) |
Linus Torvalds | a9a0884 | 2018-02-11 14:34:03 -0800 | [diff] [blame] | 1423 | mask |= EPOLLOUT | EPOLLWRNORM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | */ |
| 1438 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1439 | static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry, |
| 1440 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1442 | struct snd_rawmidi *rmidi; |
| 1443 | struct snd_rawmidi_substream *substream; |
| 1444 | struct snd_rawmidi_runtime *runtime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
| 1446 | rmidi = entry->private_data; |
| 1447 | snd_iprintf(buffer, "%s\n\n", rmidi->name); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1448 | mutex_lock(&rmidi->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1450 | list_for_each_entry(substream, |
| 1451 | &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams, |
| 1452 | list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Clemens Ladisch | 7584af1 | 2009-11-10 10:14:04 +0100 | [diff] [blame] | 1459 | snd_iprintf(buffer, |
| 1460 | " Owner PID : %d\n", |
| 1461 | pid_vnr(substream->pid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1474 | list_for_each_entry(substream, |
| 1475 | &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams, |
| 1476 | list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Clemens Ladisch | 7584af1 | 2009-11-10 10:14:04 +0100 | [diff] [blame] | 1483 | snd_iprintf(buffer, |
| 1484 | " Owner PID : %d\n", |
| 1485 | pid_vnr(substream->pid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1497 | mutex_unlock(&rmidi->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | /* |
| 1501 | * Register functions |
| 1502 | */ |
| 1503 | |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1504 | static const struct file_operations snd_rawmidi_f_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | .owner = THIS_MODULE, |
| 1506 | .read = snd_rawmidi_read, |
| 1507 | .write = snd_rawmidi_write, |
| 1508 | .open = snd_rawmidi_open, |
| 1509 | .release = snd_rawmidi_release, |
Takashi Iwai | 02f4865 | 2010-04-13 11:49:04 +0200 | [diff] [blame] | 1510 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | .poll = snd_rawmidi_poll, |
| 1512 | .unlocked_ioctl = snd_rawmidi_ioctl, |
| 1513 | .compat_ioctl = snd_rawmidi_ioctl_compat, |
| 1514 | }; |
| 1515 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1516 | static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi, |
| 1517 | struct snd_rawmidi_str *stream, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | int direction, |
| 1519 | int count) |
| 1520 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1521 | struct snd_rawmidi_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | int idx; |
| 1523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | for (idx = 0; idx < count; idx++) { |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1525 | substream = kzalloc(sizeof(*substream), GFP_KERNEL); |
Takashi Iwai | ec0e993 | 2015-03-10 15:42:14 +0100 | [diff] [blame] | 1526 | if (!substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Takashi Iwai | aee5012 | 2015-01-29 17:55:52 +0100 | [diff] [blame] | 1538 | static void release_rawmidi_device(struct device *dev) |
| 1539 | { |
| 1540 | kfree(container_of(dev, struct snd_rawmidi, dev)); |
| 1541 | } |
| 1542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Yacine Belkadi | eb7c06e | 2013-03-11 22:05:14 +0100 | [diff] [blame] | 1555 | * Return: Zero if successful, or a negative error code on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | */ |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1557 | int snd_rawmidi_new(struct snd_card *card, char *id, int device, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | int output_count, int input_count, |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1559 | struct snd_rawmidi **rrawmidi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1561 | struct snd_rawmidi *rmidi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | int err; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1563 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | .dev_free = snd_rawmidi_dev_free, |
| 1565 | .dev_register = snd_rawmidi_dev_register, |
| 1566 | .dev_disconnect = snd_rawmidi_dev_disconnect, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | }; |
| 1568 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1569 | if (snd_BUG_ON(!card)) |
| 1570 | return -ENXIO; |
| 1571 | if (rrawmidi) |
| 1572 | *rrawmidi = NULL; |
Takashi Iwai | ca2c096 | 2005-09-09 14:20:23 +0200 | [diff] [blame] | 1573 | rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL); |
Takashi Iwai | ec0e993 | 2015-03-10 15:42:14 +0100 | [diff] [blame] | 1574 | if (!rmidi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | return -ENOMEM; |
| 1576 | rmidi->card = card; |
| 1577 | rmidi->device = device; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1578 | mutex_init(&rmidi->open_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | init_waitqueue_head(&rmidi->open_wait); |
Akinobu Mita | c13893d | 2006-11-23 12:02:33 +0100 | [diff] [blame] | 1580 | INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams); |
| 1581 | INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams); |
| 1582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (id != NULL) |
| 1584 | strlcpy(rmidi->id, id, sizeof(rmidi->id)); |
Takashi Iwai | aee5012 | 2015-01-29 17:55:52 +0100 | [diff] [blame] | 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 | |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1590 | err = snd_rawmidi_alloc_substreams(rmidi, |
| 1591 | &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], |
| 1592 | SNDRV_RAWMIDI_STREAM_INPUT, |
| 1593 | input_count); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1594 | if (err < 0) |
| 1595 | goto error; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1596 | err = snd_rawmidi_alloc_substreams(rmidi, |
| 1597 | &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], |
| 1598 | SNDRV_RAWMIDI_STREAM_OUTPUT, |
| 1599 | output_count); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1600 | if (err < 0) |
| 1601 | goto error; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1602 | err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1603 | if (err < 0) |
| 1604 | goto error; |
| 1605 | |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1606 | if (rrawmidi) |
| 1607 | *rrawmidi = rmidi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1608 | return 0; |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1609 | |
| 1610 | error: |
| 1611 | snd_rawmidi_free(rmidi); |
| 1612 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 1614 | EXPORT_SYMBOL(snd_rawmidi_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1616 | static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1618 | struct snd_rawmidi_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
| 1620 | while (!list_empty(&stream->substreams)) { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1621 | substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | list_del(&substream->list); |
| 1623 | kfree(substream); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1627 | static int snd_rawmidi_free(struct snd_rawmidi *rmidi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
Takashi Iwai | 7eaa943 | 2008-08-08 17:09:09 +0200 | [diff] [blame] | 1629 | if (!rmidi) |
| 1630 | return 0; |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Takashi Iwai | aee5012 | 2015-01-29 17:55:52 +0100 | [diff] [blame] | 1643 | put_device(&rmidi->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | return 0; |
| 1645 | } |
| 1646 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1647 | static int snd_rawmidi_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1649 | struct snd_rawmidi *rmidi = device->device_data; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1650 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | return snd_rawmidi_free(rmidi); |
| 1652 | } |
| 1653 | |
Takashi Iwai | 111b0cd | 2017-06-09 15:11:58 +0200 | [diff] [blame] | 1654 | #if IS_ENABLED(CONFIG_SND_SEQUENCER) |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1655 | static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1657 | struct snd_rawmidi *rmidi = device->private_data; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | rmidi->seq_dev = NULL; |
| 1660 | } |
| 1661 | #endif |
| 1662 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1663 | static int snd_rawmidi_dev_register(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | { |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1665 | int err; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1666 | struct snd_info_entry *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | char name[16]; |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1668 | struct snd_rawmidi *rmidi = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | |
| 1670 | if (rmidi->device >= SNDRV_RAWMIDI_DEVICES) |
| 1671 | return -ENOMEM; |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1672 | err = 0; |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1673 | mutex_lock(®ister_mutex); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1674 | if (snd_rawmidi_search(rmidi->card, rmidi->device)) |
| 1675 | err = -EBUSY; |
| 1676 | else |
| 1677 | list_add_tail(&rmidi->list, &snd_rawmidi_devices); |
Takashi Iwai | 816f318 | 2016-08-30 14:45:46 +0200 | [diff] [blame] | 1678 | mutex_unlock(®ister_mutex); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1679 | if (err < 0) |
| 1680 | return err; |
| 1681 | |
Takashi Iwai | 40a4b26 | 2015-01-30 08:34:58 +0100 | [diff] [blame] | 1682 | err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI, |
| 1683 | rmidi->card, rmidi->device, |
| 1684 | &snd_rawmidi_f_ops, rmidi, &rmidi->dev); |
Takashi Iwai | aee5012 | 2015-01-29 17:55:52 +0100 | [diff] [blame] | 1685 | if (err < 0) { |
| 1686 | rmidi_err(rmidi, "unable to register\n"); |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1687 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | } |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 1689 | if (rmidi->ops && rmidi->ops->dev_register) { |
| 1690 | err = rmidi->ops->dev_register(rmidi); |
| 1691 | if (err < 0) |
| 1692 | goto error_unregister; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1698 | rmidi->card, 0, &snd_rawmidi_f_ops, |
Takashi Iwai | 80d7d77 | 2014-02-04 13:51:45 +0100 | [diff] [blame] | 1699 | rmidi) < 0) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 1700 | rmidi_err(rmidi, |
| 1701 | "unable to register OSS rawmidi device %i:%i\n", |
| 1702 | rmidi->card->number, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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, |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1712 | rmidi->card, 1, &snd_rawmidi_f_ops, |
Takashi Iwai | 80d7d77 | 2014-02-04 13:51:45 +0100 | [diff] [blame] | 1713 | rmidi) < 0) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 1714 | rmidi_err(rmidi, |
| 1715 | "unable to register OSS rawmidi device %i:%i\n", |
| 1716 | rmidi->card->number, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | } else { |
| 1718 | rmidi->ossreg++; |
| 1719 | } |
| 1720 | } |
| 1721 | #endif /* CONFIG_SND_OSSEMUL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 111b0cd | 2017-06-09 15:11:58 +0200 | [diff] [blame] | 1733 | #if IS_ENABLED(CONFIG_SND_SEQUENCER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Takashi Iwai | 7fdc9b0 | 2018-07-17 22:45:50 +0200 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
| 1753 | |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1754 | static int snd_rawmidi_dev_disconnect(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1756 | struct snd_rawmidi *rmidi = device->device_data; |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1757 | int dir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1759 | mutex_lock(®ister_mutex); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1760 | mutex_lock(&rmidi->open_mutex); |
| 1761 | wake_up(&rmidi->open_wait); |
Clemens Ladisch | f87135f | 2005-11-20 14:06:59 +0100 | [diff] [blame] | 1762 | list_del_init(&rmidi->list); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1763 | for (dir = 0; dir < 2; dir++) { |
| 1764 | struct snd_rawmidi_substream *s; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1765 | |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1766 | list_for_each_entry(s, &rmidi->streams[dir].substreams, list) { |
| 1767 | if (s->runtime) |
| 1768 | wake_up(&s->runtime->sleep); |
| 1769 | } |
| 1770 | } |
| 1771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Takashi Iwai | 40a4b26 | 2015-01-30 08:34:58 +0100 | [diff] [blame] | 1785 | snd_unregister_device(&rmidi->dev); |
Takashi Iwai | 0914f79 | 2012-10-16 16:43:39 +0200 | [diff] [blame] | 1786 | mutex_unlock(&rmidi->open_mutex); |
Ingo Molnar | 1a60d4c | 2006-01-16 16:29:08 +0100 | [diff] [blame] | 1787 | mutex_unlock(®ister_mutex); |
Takashi Iwai | c461482 | 2006-06-23 14:38:23 +0200 | [diff] [blame] | 1788 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1799 | void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream, |
Takashi Iwai | 6ba79b8 | 2017-01-05 17:01:14 +0100 | [diff] [blame] | 1800 | const struct snd_rawmidi_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | { |
Takashi Iwai | 48c9d41 | 2005-11-17 13:56:51 +0100 | [diff] [blame] | 1802 | struct snd_rawmidi_substream *substream; |
Takashi Iwai | 5bed913 | 2018-07-17 22:32:52 +0200 | [diff] [blame] | 1803 | |
Johannes Berg | 9244b2c | 2006-10-05 16:02:22 +0200 | [diff] [blame] | 1804 | list_for_each_entry(substream, &rmidi->streams[stream].substreams, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | substream->ops = ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | } |
Takashi Iwai | 6776a5d | 2014-02-27 16:00:17 +0100 | [diff] [blame] | 1807 | EXPORT_SYMBOL(snd_rawmidi_set_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 1823 | pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n", |
| 1824 | i, midi_map[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | midi_map[i] = 0; |
| 1826 | } |
| 1827 | if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) { |
Takashi Iwai | ca20d29 | 2014-02-04 18:21:39 +0100 | [diff] [blame] | 1828 | pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n", |
| 1829 | i, amidi_map[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |