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