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