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