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