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