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