fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / sound / core / rawmidi.c
index 81be9e5..0f055bf 100644 (file)
 #include <sound/core.h>
 #include <linux/major.h>
 #include <linux/init.h>
+#include <linux/smp_lock.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/time.h>
 #include <linux/wait.h>
+#include <linux/mutex.h>
+#include <linux/moduleparam.h>
+#include <linux/delay.h>
+#include <linux/wait.h>
 #include <sound/rawmidi.h>
 #include <sound/info.h>
 #include <sound/control.h>
@@ -38,25 +43,34 @@ MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
 MODULE_LICENSE("GPL");
 
 #ifdef CONFIG_SND_OSSEMUL
-static int midi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
+static int midi_map[SNDRV_CARDS];
 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
-MODULE_PARM(midi_map, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(midi_map, int, NULL, 0444);
 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
-MODULE_PARM_SYNTAX(midi_map, "default:0,skill:advanced");
-MODULE_PARM(amidi_map, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
+module_param_array(amidi_map, int, NULL, 0444);
 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
-MODULE_PARM_SYNTAX(amidi_map, "default:1,skill:advanced");
 #endif /* CONFIG_SND_OSSEMUL */
 
-static int snd_rawmidi_free(snd_rawmidi_t *rawmidi);
-static int snd_rawmidi_dev_free(snd_device_t *device);
-static int snd_rawmidi_dev_register(snd_device_t *device);
-static int snd_rawmidi_dev_disconnect(snd_device_t *device);
-static int snd_rawmidi_dev_unregister(snd_device_t *device);
+static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
+static int snd_rawmidi_dev_free(struct snd_device *device);
+static int snd_rawmidi_dev_register(struct snd_device *device);
+static int snd_rawmidi_dev_disconnect(struct snd_device *device);
+
+static LIST_HEAD(snd_rawmidi_devices);
+static DEFINE_MUTEX(register_mutex);
 
-snd_rawmidi_t *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES];
+static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
+{
+       struct list_head *p;
+       struct snd_rawmidi *rawmidi;
 
-static DECLARE_MUTEX(register_mutex);
+       list_for_each(p, &snd_rawmidi_devices) {
+               rawmidi = list_entry(p, struct snd_rawmidi, list);
+               if (rawmidi->card == card && rawmidi->device == device)
+                       return rawmidi;
+       }
+       return NULL;
+}
 
 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
 {
@@ -70,24 +84,48 @@ static inline unsigned short snd_rawmidi_file_flags(struct file *file)
        }
 }
 
-static inline int snd_rawmidi_ready(snd_rawmidi_substream_t * substream)
+static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
        return runtime->avail >= runtime->avail_min;
 }
 
-static inline int snd_rawmidi_ready_append(snd_rawmidi_substream_t * substream, size_t count)
+static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
+                                          size_t count)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
        return runtime->avail >= runtime->avail_min &&
               (!substream->append || runtime->avail >= count);
 }
 
-static int snd_rawmidi_init(snd_rawmidi_substream_t *substream)
+static void snd_rawmidi_input_event_tasklet(unsigned long data)
+{
+       struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
+       substream->runtime->event(substream);
+}
+
+static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_substream *substream = (struct snd_rawmidi_substream *)data;
+       substream->ops->trigger(substream, 1);
+}
+
+static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
+{
+       struct snd_rawmidi_runtime *runtime;
+
+       if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
+               return -ENOMEM;
        spin_lock_init(&runtime->lock);
        init_waitqueue_head(&runtime->sleep);
+       if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
+               tasklet_init(&runtime->tasklet,
+                            snd_rawmidi_input_event_tasklet,
+                            (unsigned long)substream);
+       else
+               tasklet_init(&runtime->tasklet,
+                            snd_rawmidi_output_trigger_tasklet,
+                            (unsigned long)substream);
        runtime->event = NULL;
        runtime->buffer_size = PAGE_SIZE;
        runtime->avail_min = 1;
@@ -95,94 +133,113 @@ static int snd_rawmidi_init(snd_rawmidi_substream_t *substream)
                runtime->avail = 0;
        else
                runtime->avail = runtime->buffer_size;
-       if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL)
+       if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
+               kfree(runtime);
                return -ENOMEM;
+       }
        runtime->appl_ptr = runtime->hw_ptr = 0;
+       substream->runtime = runtime;
        return 0;
 }
 
-static int snd_rawmidi_done_buffer(snd_rawmidi_runtime_t *runtime)
+static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
 {
-       if (runtime->buffer) {
-               kfree(runtime->buffer);
-               runtime->buffer = NULL;
-       }
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
+
+       kfree(runtime->buffer);
+       kfree(runtime);
+       substream->runtime = NULL;
        return 0;
 }
 
-int snd_rawmidi_drop_output(snd_rawmidi_substream_t * substream)
+static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
+{
+       if (up) {
+               tasklet_hi_schedule(&substream->runtime->tasklet);
+       } else {
+               tasklet_kill(&substream->runtime->tasklet);
+               substream->ops->trigger(substream, 0);
+       }
+}
+
+static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
+{
+       substream->ops->trigger(substream, up);
+       if (!up && substream->runtime->event)
+               tasklet_kill(&substream->runtime->tasklet);
+}
+
+int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       unsigned long flags;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
-       substream->ops->trigger(substream, 0);
-       runtime->trigger = 0;
+       snd_rawmidi_output_trigger(substream, 0);
        runtime->drain = 0;
-       /* interrupts are not enabled at this moment,
-          so spinlock is not required */
+       spin_lock_irqsave(&runtime->lock, flags);
        runtime->appl_ptr = runtime->hw_ptr = 0;
        runtime->avail = runtime->buffer_size;
+       spin_unlock_irqrestore(&runtime->lock, flags);
        return 0;
 }
 
-int snd_rawmidi_drain_output(snd_rawmidi_substream_t * substream)
+int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
 {
        int err;
        long timeout;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        err = 0;
        runtime->drain = 1;
-       while (runtime->avail < runtime->buffer_size) {
-               timeout = interruptible_sleep_on_timeout(&runtime->sleep, 10 * HZ);
-               if (signal_pending(current)) {
-                       err = -ERESTARTSYS;
-                       break;
-               }
-               if (runtime->avail < runtime->buffer_size && !timeout) {
-                       snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
-                       err = -EIO;
-                       break;
-               }
+       timeout = wait_event_interruptible_timeout(runtime->sleep,
+                               (runtime->avail >= runtime->buffer_size),
+                               10*HZ);
+       if (signal_pending(current))
+               err = -ERESTARTSYS;
+       if (runtime->avail < runtime->buffer_size && !timeout) {
+               snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
+               err = -EIO;
        }
        runtime->drain = 0;
        if (err != -ERESTARTSYS) {
                /* we need wait a while to make sure that Tx FIFOs are empty */
                if (substream->ops->drain)
                        substream->ops->drain(substream);
-               else {
-                       set_current_state(TASK_UNINTERRUPTIBLE);
-                       schedule_timeout(HZ / 20);
-               }
+               else
+                       msleep(50);
                snd_rawmidi_drop_output(substream);
        }
        return err;
 }
 
-int snd_rawmidi_drain_input(snd_rawmidi_substream_t * substream)
+int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       unsigned long flags;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
-       substream->ops->trigger(substream, 0);
-       runtime->trigger = 0;
+       snd_rawmidi_input_trigger(substream, 0);
        runtime->drain = 0;
-       /* interrupts aren't enabled at this moment, so spinlock isn't needed */
+       spin_lock_irqsave(&runtime->lock, flags);
        runtime->appl_ptr = runtime->hw_ptr = 0;
        runtime->avail = 0;
+       spin_unlock_irqrestore(&runtime->lock, flags);
        return 0;
 }
 
-int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
-                           int mode, snd_rawmidi_file_t * rfile)
+int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
+                           int mode, struct snd_rawmidi_file * rfile)
 {
-       snd_rawmidi_t *rmidi;
+       struct snd_rawmidi *rmidi;
        struct list_head *list1, *list2;
-       snd_rawmidi_substream_t *sinput, *soutput;
-       snd_rawmidi_runtime_t *input = NULL, *output = NULL;
+       struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
+       struct snd_rawmidi_runtime *input = NULL, *output = NULL;
        int err;
 
        if (rfile)
                rfile->input = rfile->output = NULL;
-       rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
+       mutex_lock(&register_mutex);
+       rmidi = snd_rawmidi_search(card, device);
+       mutex_unlock(&register_mutex);
        if (rmidi == NULL) {
                err = -ENODEV;
                goto __error1;
@@ -192,7 +249,7 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
                goto __error1;
        }
        if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
-               down(&rmidi->open_mutex);
+               mutex_lock(&rmidi->open_mutex);
        if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
                if (!(rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT)) {
                        err = -ENXIO;
@@ -233,7 +290,7 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
                        }
                        break;
                }
-               sinput = list_entry(list1, snd_rawmidi_substream_t, list);
+               sinput = list_entry(list1, struct snd_rawmidi_substream, list);
                if ((mode & SNDRV_RAWMIDI_LFLG_INPUT) && sinput->opened)
                        goto __nexti;
                if (subdevice < 0 || (subdevice >= 0 && subdevice == sinput->number))
@@ -251,7 +308,7 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
                        }
                        break;
                }
-               soutput = list_entry(list2, snd_rawmidi_substream_t, list);
+               soutput = list_entry(list2, struct snd_rawmidi_substream, list);
                if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
                        if (mode & SNDRV_RAWMIDI_LFLG_APPEND) {
                                if (soutput->opened && !soutput->append)
@@ -267,20 +324,11 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
                list2 = list2->next;
        }
        if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
-               input = snd_kcalloc(sizeof(snd_rawmidi_runtime_t), GFP_KERNEL);
-               if (input == NULL) {
-                       err = -ENOMEM;
+               if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
                        goto __error;
-               }
-               sinput->runtime = input;
-               if (snd_rawmidi_init(sinput) < 0) {
-                       err = -ENOMEM;
+               input = sinput->runtime;
+               if ((err = sinput->ops->open(sinput)) < 0)
                        goto __error;
-               }
-               if ((err = sinput->ops->open(sinput)) < 0) {
-                       sinput->runtime = NULL;
-                       goto __error;
-               }
                sinput->opened = 1;
                rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
        } else {
@@ -289,26 +337,15 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
        if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
                if (soutput->opened)
                        goto __skip_output;
-               output = snd_kcalloc(sizeof(snd_rawmidi_runtime_t), GFP_KERNEL);
-               if (output == NULL) {
-                       err = -ENOMEM;
-                       goto __error;
-               }
-               soutput->runtime = output;
-               if (snd_rawmidi_init(soutput) < 0) {
-                       if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
+               if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
+                       if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
                                sinput->ops->close(sinput);
-                               sinput->runtime = NULL;
-                       }
-                       err = -ENOMEM;
                        goto __error;
                }
+               output = soutput->runtime;
                if ((err = soutput->ops->open(soutput)) < 0) {
-                       if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
+                       if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
                                sinput->ops->close(sinput);
-                               sinput->runtime = NULL;
-                       }
-                       soutput->runtime = NULL;
                        goto __error;
                }
              __skip_output:
@@ -322,7 +359,7 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
                soutput = NULL;
        }
        if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
-               up(&rmidi->open_mutex);
+               mutex_unlock(&rmidi->open_mutex);
        if (rfile) {
                rfile->rmidi = rmidi;
                rfile->input = sinput;
@@ -331,17 +368,13 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
        return 0;
 
       __error:
-       if (input != NULL) {
-               snd_rawmidi_done_buffer(input);
-               kfree(input);
-       }
-       if (output != NULL) {
-               snd_rawmidi_done_buffer(output);
-               kfree(output);
-       }
+       if (input != NULL)
+               snd_rawmidi_runtime_free(sinput);
+       if (output != NULL)
+               snd_rawmidi_runtime_free(soutput);
        module_put(rmidi->card->module);
        if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
-               up(&rmidi->open_mutex);
+               mutex_unlock(&rmidi->open_mutex);
       __error1:
        return err;
 }
@@ -349,43 +382,29 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
 static int snd_rawmidi_open(struct inode *inode, struct file *file)
 {
        int maj = imajor(inode);
-       int cardnum;
-       snd_card_t *card;
-       int device, subdevice;
+       struct snd_card *card;
+       int subdevice;
        unsigned short fflags;
        int err;
-       snd_rawmidi_t *rmidi;
-       snd_rawmidi_file_t *rawmidi_file;
+       struct snd_rawmidi *rmidi;
+       struct snd_rawmidi_file *rawmidi_file;
        wait_queue_t wait;
        struct list_head *list;
-       snd_ctl_file_t *kctl;
-
-       switch (maj) {
-       case CONFIG_SND_MAJOR:
-               cardnum = SNDRV_MINOR_CARD(iminor(inode));
-               cardnum %= SNDRV_CARDS;
-               device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_RAWMIDI;
-               device %= SNDRV_MINOR_RAWMIDIS;
-               break;
+       struct snd_ctl_file *kctl;
+
+       if (maj == snd_major) {
+               rmidi = snd_lookup_minor_data(iminor(inode),
+                                             SNDRV_DEVICE_TYPE_RAWMIDI);
 #ifdef CONFIG_SND_OSSEMUL
-       case SOUND_MAJOR:
-               cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
-               cardnum %= SNDRV_CARDS;
-               device = SNDRV_MINOR_OSS_DEVICE(iminor(inode)) == SNDRV_MINOR_OSS_MIDI ?
-                       midi_map[cardnum] : amidi_map[cardnum];
-               break;
+       } else if (maj == SOUND_MAJOR) {
+               rmidi = snd_lookup_oss_minor_data(iminor(inode),
+                                                 SNDRV_OSS_DEVICE_TYPE_MIDI);
 #endif
-       default:
+       } else
                return -ENXIO;
-       }
 
-       rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
        if (rmidi == NULL)
                return -ENODEV;
-#ifdef CONFIG_SND_OSSEMUL
-       if (maj == SOUND_MAJOR && !rmidi->ossreg)
-               return -ENXIO;
-#endif
        if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK)) 
                return -EINVAL;         /* invalid combination */
        card = rmidi->card;
@@ -393,17 +412,17 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
        if (err < 0)
                return -ENODEV;
        fflags = snd_rawmidi_file_flags(file);
-       if ((file->f_flags & O_APPEND) || maj != CONFIG_SND_MAJOR) /* OSS emul? */
+       if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
                fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
        fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
-       rawmidi_file = snd_magic_kmalloc(snd_rawmidi_file_t, 0, GFP_KERNEL);
+       rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
        if (rawmidi_file == NULL) {
                snd_card_file_remove(card, file);
                return -ENOMEM;
        }
        init_waitqueue_entry(&wait, current);
        add_wait_queue(&rmidi->open_wait, &wait);
-       down(&rmidi->open_mutex);
+       mutex_lock(&rmidi->open_mutex);
        while (1) {
                subdevice = -1;
                down_read(&card->controls_rwsem);
@@ -411,11 +430,13 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
                        kctl = snd_ctl_file(list);
                        if (kctl->pid == current->pid) {
                                subdevice = kctl->prefer_rawmidi_subdevice;
-                               break;
+                               if (subdevice != -1)
+                                       break;
                        }
                }
                up_read(&card->controls_rwsem);
-               err = snd_rawmidi_kernel_open(cardnum, device, subdevice, fflags, rawmidi_file);
+               err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
+                                             subdevice, fflags, rawmidi_file);
                if (err >= 0)
                        break;
                if (err == -EAGAIN) {
@@ -426,9 +447,9 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
                } else
                        break;
                set_current_state(TASK_INTERRUPTIBLE);
-               up(&rmidi->open_mutex);
+               mutex_unlock(&rmidi->open_mutex);
                schedule();
-               down(&rmidi->open_mutex);
+               mutex_lock(&rmidi->open_mutex);
                if (signal_pending(current)) {
                        err = -ERESTARTSYS;
                        break;
@@ -445,34 +466,31 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
                file->private_data = rawmidi_file;
        } else {
                snd_card_file_remove(card, file);
-               snd_magic_kfree(rawmidi_file);
+               kfree(rawmidi_file);
        }
-       up(&rmidi->open_mutex);
+       mutex_unlock(&rmidi->open_mutex);
        return err;
 }
 
-int snd_rawmidi_kernel_release(snd_rawmidi_file_t * rfile)
+int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
 {
-       snd_rawmidi_t *rmidi;
-       snd_rawmidi_substream_t *substream;
-       snd_rawmidi_runtime_t *runtime;
+       struct snd_rawmidi *rmidi;
+       struct snd_rawmidi_substream *substream;
+       struct snd_rawmidi_runtime *runtime;
 
        snd_assert(rfile != NULL, return -ENXIO);
        snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
        rmidi = rfile->rmidi;
-       down(&rmidi->open_mutex);
+       mutex_lock(&rmidi->open_mutex);
        if (rfile->input != NULL) {
                substream = rfile->input;
                rfile->input = NULL;
                runtime = substream->runtime;
-               runtime->trigger = 0;
-               substream->ops->trigger(substream, 0);
+               snd_rawmidi_input_trigger(substream, 0);
                substream->ops->close(substream);
-               snd_rawmidi_done_buffer(runtime);
                if (runtime->private_free != NULL)
                        runtime->private_free(substream);
-               kfree(runtime);
-               substream->runtime = NULL;
+               snd_rawmidi_runtime_free(substream);
                substream->opened = 0;
                rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
        }
@@ -487,41 +505,40 @@ int snd_rawmidi_kernel_release(snd_rawmidi_file_t * rfile)
                                snd_rawmidi_kernel_write(substream, &buf, 1);
                        }
                        if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
-                               substream->ops->trigger(substream, 0);
+                               snd_rawmidi_output_trigger(substream, 0);
                        substream->ops->close(substream);
-                       snd_rawmidi_done_buffer(runtime);
                        if (runtime->private_free != NULL)
                                runtime->private_free(substream);
-                       kfree(runtime);
-                       substream->runtime = NULL;
+                       snd_rawmidi_runtime_free(substream);
                        substream->opened = 0;
                        substream->append = 0;
                }
                rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
        }
-       up(&rmidi->open_mutex);
+       mutex_unlock(&rmidi->open_mutex);
        module_put(rmidi->card->module);
        return 0;
 }
 
 static int snd_rawmidi_release(struct inode *inode, struct file *file)
 {
-       snd_rawmidi_file_t *rfile;
-       snd_rawmidi_t *rmidi;
+       struct snd_rawmidi_file *rfile;
+       struct snd_rawmidi *rmidi;
        int err;
 
-       rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
+       rfile = file->private_data;
        err = snd_rawmidi_kernel_release(rfile);
        rmidi = rfile->rmidi;
        wake_up(&rmidi->open_wait);
-       snd_magic_kfree(rfile);
+       kfree(rfile);
        snd_card_file_remove(rmidi->card, file);
        return err;
 }
 
-int snd_rawmidi_info(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t *info)
+static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
+                           struct snd_rawmidi_info *info)
 {
-       snd_rawmidi_t *rmidi;
+       struct snd_rawmidi *rmidi;
        
        if (substream == NULL)
                return -ENODEV;
@@ -541,26 +558,30 @@ int snd_rawmidi_info(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t *inf
        return 0;
 }
 
-static int snd_rawmidi_info_user(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t * _info)
+static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
+                                struct snd_rawmidi_info __user * _info)
 {
-       snd_rawmidi_info_t info;
+       struct snd_rawmidi_info info;
        int err;
        if ((err = snd_rawmidi_info(substream, &info)) < 0)
                return err;
-       if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
+       if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
                return -EFAULT;
        return 0;
 }
 
-int snd_rawmidi_info_select(snd_card_t *card, snd_rawmidi_info_t *info)
+int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
 {
-       snd_rawmidi_t *rmidi;
-       snd_rawmidi_str_t *pstr;
-       snd_rawmidi_substream_t *substream;
+       struct snd_rawmidi *rmidi;
+       struct snd_rawmidi_str *pstr;
+       struct snd_rawmidi_substream *substream;
        struct list_head *list;
-       if (info->device >= SNDRV_RAWMIDI_DEVICES)
+
+       mutex_lock(&register_mutex);
+       rmidi = snd_rawmidi_search(card, info->device);
+       mutex_unlock(&register_mutex);
+       if (!rmidi)
                return -ENXIO;
-       rmidi = snd_rawmidi_devices[card->number * SNDRV_RAWMIDI_DEVICES + info->device];
        if (info->stream < 0 || info->stream > 1)
                return -EINVAL;
        pstr = &rmidi->streams[info->stream];
@@ -569,18 +590,18 @@ int snd_rawmidi_info_select(snd_card_t *card, snd_rawmidi_info_t *info)
        if (info->subdevice >= pstr->substream_count)
                return -ENXIO;
        list_for_each(list, &pstr->substreams) {
-               substream = list_entry(list, snd_rawmidi_substream_t, list);
+               substream = list_entry(list, struct snd_rawmidi_substream, list);
                if ((unsigned int)substream->number == info->subdevice)
                        return snd_rawmidi_info(substream, info);
        }
        return -ENXIO;
 }
 
-static int snd_rawmidi_info_select_user(snd_card_t *card,
-                                       snd_rawmidi_info_t *_info)
+static int snd_rawmidi_info_select_user(struct snd_card *card,
+                                       struct snd_rawmidi_info __user *_info)
 {
        int err;
-       snd_rawmidi_info_t info;
+       struct snd_rawmidi_info info;
        if (get_user(info.device, &_info->device))
                return -EFAULT;
        if (get_user(info.stream, &_info->stream))
@@ -589,16 +610,16 @@ static int snd_rawmidi_info_select_user(snd_card_t *card,
                return -EFAULT;
        if ((err = snd_rawmidi_info_select(card, &info)) < 0)
                return err;
-       if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
+       if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
                return -EFAULT;
        return 0;
 }
 
-int snd_rawmidi_output_params(snd_rawmidi_substream_t * substream,
-                             snd_rawmidi_params_t * params)
+int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
+                             struct snd_rawmidi_params * params)
 {
        char *newbuf;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
        
        if (substream->append && substream->use_count > 1)
                return -EBUSY;
@@ -610,22 +631,24 @@ int snd_rawmidi_output_params(snd_rawmidi_substream_t * substream,
                return -EINVAL;
        }
        if (params->buffer_size != runtime->buffer_size) {
-               if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
+               newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
+               if (!newbuf)
                        return -ENOMEM;
                kfree(runtime->buffer);
                runtime->buffer = newbuf;
                runtime->buffer_size = params->buffer_size;
+               runtime->avail = runtime->buffer_size;
        }
        runtime->avail_min = params->avail_min;
        substream->active_sensing = !params->no_active_sensing;
        return 0;
 }
 
-int snd_rawmidi_input_params(snd_rawmidi_substream_t * substream,
-                            snd_rawmidi_params_t * params)
+int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
+                            struct snd_rawmidi_params * params)
 {
        char *newbuf;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        snd_rawmidi_drain_input(substream);
        if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
@@ -635,7 +658,8 @@ int snd_rawmidi_input_params(snd_rawmidi_substream_t * substream,
                return -EINVAL;
        }
        if (params->buffer_size != runtime->buffer_size) {
-               if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
+               newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
+               if (!newbuf)
                        return -ENOMEM;
                kfree(runtime->buffer);
                runtime->buffer = newbuf;
@@ -645,10 +669,10 @@ int snd_rawmidi_input_params(snd_rawmidi_substream_t * substream,
        return 0;
 }
 
-static int snd_rawmidi_output_status(snd_rawmidi_substream_t * substream,
-                                    snd_rawmidi_status_t * status)
+static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
+                                    struct snd_rawmidi_status * status)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        memset(status, 0, sizeof(*status));
        status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
@@ -658,10 +682,10 @@ static int snd_rawmidi_output_status(snd_rawmidi_substream_t * substream,
        return 0;
 }
 
-static int snd_rawmidi_input_status(snd_rawmidi_substream_t * substream,
-                                   snd_rawmidi_status_t * status)
+static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
+                                   struct snd_rawmidi_status * status)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        memset(status, 0, sizeof(*status));
        status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
@@ -673,21 +697,21 @@ static int snd_rawmidi_input_status(snd_rawmidi_substream_t * substream,
        return 0;
 }
 
-static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
-                            unsigned int cmd, unsigned long arg)
+static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
-       snd_rawmidi_file_t *rfile;
+       struct snd_rawmidi_file *rfile;
+       void __user *argp = (void __user *)arg;
 
-       rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
+       rfile = file->private_data;
        if (((cmd >> 8) & 0xff) != 'W')
                return -ENOTTY;
        switch (cmd) {
        case SNDRV_RAWMIDI_IOCTL_PVERSION:
-               return put_user(SNDRV_RAWMIDI_VERSION, (int *)arg) ? -EFAULT : 0;
+               return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
        case SNDRV_RAWMIDI_IOCTL_INFO:
        {
-               snd_rawmidi_stream_t stream;
-               snd_rawmidi_info_t *info = (snd_rawmidi_info_t *) arg;
+               int stream;
+               struct snd_rawmidi_info __user *info = argp;
                if (get_user(stream, &info->stream))
                        return -EFAULT;
                switch (stream) {
@@ -701,8 +725,8 @@ static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
        }
        case SNDRV_RAWMIDI_IOCTL_PARAMS:
        {
-               snd_rawmidi_params_t params;
-               if (copy_from_user(&params, (snd_rawmidi_params_t *) arg, sizeof(snd_rawmidi_params_t)))
+               struct snd_rawmidi_params params;
+               if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
                        return -EFAULT;
                switch (params.stream) {
                case SNDRV_RAWMIDI_STREAM_OUTPUT:
@@ -720,8 +744,8 @@ static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
        case SNDRV_RAWMIDI_IOCTL_STATUS:
        {
                int err = 0;
-               snd_rawmidi_status_t status;
-               if (copy_from_user(&status, (snd_rawmidi_status_t *) arg, sizeof(snd_rawmidi_status_t)))
+               struct snd_rawmidi_status status;
+               if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
                        return -EFAULT;
                switch (status.stream) {
                case SNDRV_RAWMIDI_STREAM_OUTPUT:
@@ -739,14 +763,14 @@ static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
                }
                if (err < 0)
                        return err;
-               if (copy_to_user((snd_rawmidi_status_t *) arg, &status, sizeof(snd_rawmidi_status_t)))
+               if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
                        return -EFAULT;
                return 0;
        }
        case SNDRV_RAWMIDI_IOCTL_DROP:
        {
                int val;
-               if (get_user(val, (long *) arg))
+               if (get_user(val, (int __user *) argp))
                        return -EFAULT;
                switch (val) {
                case SNDRV_RAWMIDI_STREAM_OUTPUT:
@@ -760,7 +784,7 @@ static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
        case SNDRV_RAWMIDI_IOCTL_DRAIN:
        {
                int val;
-               if (get_user(val, (long *) arg))
+               if (get_user(val, (int __user *) argp))
                        return -EFAULT;
                switch (val) {
                case SNDRV_RAWMIDI_STREAM_OUTPUT:
@@ -783,28 +807,31 @@ static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
        return -ENOTTY;
 }
 
-int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
-                             unsigned int cmd, unsigned long arg)
+static int snd_rawmidi_control_ioctl(struct snd_card *card,
+                                    struct snd_ctl_file *control,
+                                    unsigned int cmd,
+                                    unsigned long arg)
 {
-       unsigned int tmp;
+       void __user *argp = (void __user *)arg;
 
-       tmp = card->number * SNDRV_RAWMIDI_DEVICES;
        switch (cmd) {
        case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
        {
                int device;
                
-               if (get_user(device, (int *)arg))
+               if (get_user(device, (int __user *)argp))
                        return -EFAULT;
+               mutex_lock(&register_mutex);
                device = device < 0 ? 0 : device + 1;
                while (device < SNDRV_RAWMIDI_DEVICES) {
-                       if (snd_rawmidi_devices[tmp + device])
+                       if (snd_rawmidi_search(card, device))
                                break;
                        device++;
                }
                if (device == SNDRV_RAWMIDI_DEVICES)
                        device = -1;
-               if (put_user(device, (int *)arg))
+               mutex_unlock(&register_mutex);
+               if (put_user(device, (int __user *)argp))
                        return -EFAULT;
                return 0;
        }
@@ -812,13 +839,13 @@ int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
        {
                int val;
                
-               if (get_user(val, (int *)arg))
+               if (get_user(val, (int __user *)argp))
                        return -EFAULT;
                control->prefer_rawmidi_subdevice = val;
                return 0;
        }
        case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
-               return snd_rawmidi_info_select_user(card, (snd_rawmidi_info_t *)arg);
+               return snd_rawmidi_info_select_user(card, argp);
        }
        return -ENOIOCTLCMD;
 }
@@ -833,11 +860,12 @@ int snd_rawmidi_control_ioctl(snd_card_t * card, snd_ctl_file_t * control,
  *
  * Returns the size of read data, or a negative error code on failure.
  */
-int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
+int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
+                       const unsigned char *buffer, int count)
 {
        unsigned long flags;
        int result = 0, count1;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        if (runtime->buffer == NULL) {
                snd_printd("snd_rawmidi_receive: input is not active!!!\n");
@@ -872,7 +900,7 @@ int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char
                        count1 = count;
                        if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
                                count1 = runtime->buffer_size - runtime->avail;
-                               runtime->xruns = count - count1;
+                               runtime->xruns += count - count1;
                        }
                        if (count1 > 0) {
                                memcpy(runtime->buffer, buffer, count1);
@@ -882,22 +910,22 @@ int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char
                        }
                }
        }
-       if (result > 0 && runtime->event == NULL) {
-               if (snd_rawmidi_ready(substream))
+       if (result > 0) {
+               if (runtime->event)
+                       tasklet_hi_schedule(&runtime->tasklet);
+               else if (snd_rawmidi_ready(substream))
                        wake_up(&runtime->sleep);
        }
        spin_unlock_irqrestore(&runtime->lock, flags);
-       if (result > 0 && runtime->event)
-               runtime->event(substream);
        return result;
 }
 
-static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
+static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
                                     unsigned char *buf, long count, int kernel)
 {
        unsigned long flags;
        long result = 0, count1;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        while (count > 0 && runtime->avail) {
                count1 = runtime->buffer_size - runtime->appl_ptr;
@@ -910,7 +938,8 @@ static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
                        memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
                } else {
                        spin_unlock_irqrestore(&runtime->lock, flags);
-                       if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
+                       if (copy_to_user((char __user *)buf + result,
+                                        runtime->buffer + runtime->appl_ptr, count1)) {
                                return result > 0 ? result : -EFAULT;
                        }
                        spin_lock_irqsave(&runtime->lock, flags);
@@ -925,28 +954,28 @@ static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
        return result;
 }
 
-long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
+long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
+                            unsigned char *buf, long count)
 {
-       substream->runtime->trigger = 1;
-       substream->ops->trigger(substream, 1);
+       snd_rawmidi_input_trigger(substream, 1);
        return snd_rawmidi_kernel_read1(substream, buf, count, 1);
 }
 
-static ssize_t snd_rawmidi_read(struct file *file, char *buf, size_t count, loff_t *offset)
+static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
+                               loff_t *offset)
 {
        long result;
        int count1;
-       snd_rawmidi_file_t *rfile;
-       snd_rawmidi_substream_t *substream;
-       snd_rawmidi_runtime_t *runtime;
+       struct snd_rawmidi_file *rfile;
+       struct snd_rawmidi_substream *substream;
+       struct snd_rawmidi_runtime *runtime;
 
-       rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
+       rfile = file->private_data;
        substream = rfile->input;
        if (substream == NULL)
                return -EIO;
        runtime = substream->runtime;
-       runtime->trigger = 1;
-       substream->ops->trigger(substream, 1);
+       snd_rawmidi_input_trigger(substream, 1);
        result = 0;
        while (count > 0) {
                spin_lock_irq(&runtime->lock);
@@ -969,7 +998,11 @@ static ssize_t snd_rawmidi_read(struct file *file, char *buf, size_t count, loff
                        spin_lock_irq(&runtime->lock);
                }
                spin_unlock_irq(&runtime->lock);
-               count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
+               count1 = snd_rawmidi_kernel_read1(substream,
+                                                 (unsigned char __force *)buf,
+                                                 count, 0);
+               if (count1 < 0)
+                       return result > 0 ? result : count1;
                result += count1;
                buf += count1;
                count -= count1;
@@ -983,9 +1016,9 @@ static ssize_t snd_rawmidi_read(struct file *file, char *buf, size_t count, loff
  * 
  * Returns 1 if the internal output buffer is empty, 0 if not.
  */
-int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
+int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
        int result;
        unsigned long flags;
 
@@ -995,8 +1028,6 @@ int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
        }
        spin_lock_irqsave(&runtime->lock, flags);
        result = runtime->avail >= runtime->buffer_size;
-       if (result)
-               runtime->trigger = 1;
        spin_unlock_irqrestore(&runtime->lock, flags);
        return result;          
 }
@@ -1015,11 +1046,12 @@ int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
  *
  * Returns the size of copied data, or a negative error code on failure.
  */
-int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
+int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
+                             unsigned char *buffer, int count)
 {
        unsigned long flags;
        int result, count1;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        if (runtime->buffer == NULL) {
                snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
@@ -1029,7 +1061,6 @@ int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char
        spin_lock_irqsave(&runtime->lock, flags);
        if (runtime->avail >= runtime->buffer_size) {
                /* warning: lowlevel layer MUST trigger down the hardware */
-               runtime->trigger = 0;
                goto __skip;
        }
        if (count == 1) {       /* special case, faster code */
@@ -1044,8 +1075,12 @@ int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char
                memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
                count -= count1;
                result += count1;
-               if (count > 0)
+               if (count > 0) {
+                       if (count > (int)(runtime->buffer_size - runtime->avail - count1))
+                               count = runtime->buffer_size - runtime->avail - count1;
                        memcpy(buffer + count1, runtime->buffer, count);
+                       result += count;
+               }
        }
       __skip:
        spin_unlock_irqrestore(&runtime->lock, flags);
@@ -1063,10 +1098,10 @@ int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char
  *
  * Returns the advanced size if successful, or a negative error code on failure.
  */
-int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
+int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
 {
        unsigned long flags;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        if (runtime->buffer == NULL) {
                snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
@@ -1078,29 +1113,26 @@ int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
        runtime->hw_ptr %= runtime->buffer_size;
        runtime->avail += count;
        substream->bytes += count;
-       if (runtime->drain)
-               wake_up(&runtime->sleep);
-       else
-               if (count > 0 && runtime->event == NULL)
-                       if (snd_rawmidi_ready(substream))
-                               wake_up(&runtime->sleep);
+       if (count > 0) {
+               if (runtime->drain || snd_rawmidi_ready(substream))
+                       wake_up(&runtime->sleep);
+       }
        spin_unlock_irqrestore(&runtime->lock, flags);
-       if (count > 0 && runtime->event)
-               runtime->event(substream);
        return count;
 }
 
 /**
  * snd_rawmidi_transmit - copy from the buffer to the device
  * @substream: the rawmidi substream
- * @buf: the buffer pointer
+ * @buffer: the buffer pointer
  * @count: the data size to transfer
  * 
  * Copies data from the buffer to the device and advances the pointer.
  *
  * Returns the copied size if successful, or a negative error code on failure.
  */
-int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
+int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
+                        unsigned char *buffer, int count)
 {
        count = snd_rawmidi_transmit_peek(substream, buffer, count);
        if (count < 0)
@@ -1108,11 +1140,12 @@ int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buf
        return snd_rawmidi_transmit_ack(substream, count);
 }
 
-static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
+static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
+                                     const unsigned char *buf, long count, int kernel)
 {
        unsigned long flags;
        long count1, result;
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       struct snd_rawmidi_runtime *runtime = substream->runtime;
 
        snd_assert(buf != NULL, return -EINVAL);
        snd_assert(runtime->buffer != NULL, return -EINVAL);
@@ -1135,7 +1168,8 @@ static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const
                        memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
                } else {
                        spin_unlock_irqrestore(&runtime->lock, flags);
-                       if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
+                       if (copy_from_user(runtime->buffer + runtime->appl_ptr,
+                                          (char __user *)buf, count1)) {
                                spin_lock_irqsave(&runtime->lock, flags);
                                result = result > 0 ? result : -EFAULT;
                                goto __end;
@@ -1150,29 +1184,29 @@ static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const
                count -= count1;
        }
       __end:
-       if (result > 0)
-               runtime->trigger = 1;
        count1 = runtime->avail < runtime->buffer_size;
        spin_unlock_irqrestore(&runtime->lock, flags);
        if (count1)
-               substream->ops->trigger(substream, 1);
+               snd_rawmidi_output_trigger(substream, 1);
        return result;
 }
 
-long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
+long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
+                             const unsigned char *buf, long count)
 {
        return snd_rawmidi_kernel_write1(substream, buf, count, 1);
 }
 
-static ssize_t snd_rawmidi_write(struct file *file, const char *buf, size_t count, loff_t *offset)
+static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
+                                size_t count, loff_t *offset)
 {
        long result, timeout;
        int count1;
-       snd_rawmidi_file_t *rfile;
-       snd_rawmidi_runtime_t *runtime;
-       snd_rawmidi_substream_t *substream;
+       struct snd_rawmidi_file *rfile;
+       struct snd_rawmidi_runtime *runtime;
+       struct snd_rawmidi_substream *substream;
 
-       rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return -ENXIO);
+       rfile = file->private_data;
        substream = rfile->output;
        runtime = substream->runtime;
        /* we cannot put an atomic message to our buffer */
@@ -1200,16 +1234,18 @@ static ssize_t snd_rawmidi_write(struct file *file, const char *buf, size_t coun
                        spin_lock_irq(&runtime->lock);
                }
                spin_unlock_irq(&runtime->lock);
-               count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
+               count1 = snd_rawmidi_kernel_write1(substream,
+                                                  (unsigned char __force *)buf,
+                                                  count, 0);
                if (count1 < 0)
-                       continue;
+                       return result > 0 ? result : count1;
                result += count1;
                buf += count1;
                if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
                        break;
                count -= count1;
        }
-       while (file->f_flags & O_SYNC) {
+       if (file->f_flags & O_SYNC) {
                spin_lock_irq(&runtime->lock);
                while (runtime->avail != runtime->buffer_size) {
                        wait_queue_t wait;
@@ -1233,15 +1269,14 @@ static ssize_t snd_rawmidi_write(struct file *file, const char *buf, size_t coun
 
 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
 {
-       snd_rawmidi_file_t *rfile;
-       snd_rawmidi_runtime_t *runtime;
+       struct snd_rawmidi_file *rfile;
+       struct snd_rawmidi_runtime *runtime;
        unsigned int mask;
 
-       rfile = snd_magic_cast(snd_rawmidi_file_t, file->private_data, return 0);
+       rfile = file->private_data;
        if (rfile->input != NULL) {
                runtime = rfile->input->runtime;
-               runtime->trigger = 1;
-               rfile->input->ops->trigger(rfile->input, 1);
+               snd_rawmidi_input_trigger(rfile->input, 1);
                poll_wait(file, &runtime->sleep, wait);
        }
        if (rfile->output != NULL) {
@@ -1260,24 +1295,32 @@ static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
        return mask;
 }
 
+/*
+ */
+#ifdef CONFIG_COMPAT
+#include "rawmidi_compat.c"
+#else
+#define snd_rawmidi_ioctl_compat       NULL
+#endif
+
 /*
 
  */
 
-static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
-                                      snd_info_buffer_t * buffer)
+static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
+                                      struct snd_info_buffer *buffer)
 {
-       snd_rawmidi_t *rmidi;
-       snd_rawmidi_substream_t *substream;
-       snd_rawmidi_runtime_t *runtime;
+       struct snd_rawmidi *rmidi;
+       struct snd_rawmidi_substream *substream;
+       struct snd_rawmidi_runtime *runtime;
        struct list_head *list;
 
-       rmidi = snd_magic_cast(snd_rawmidi_t, entry->private_data, return);
+       rmidi = entry->private_data;
        snd_iprintf(buffer, "%s\n\n", rmidi->name);
-       down(&rmidi->open_mutex);
+       mutex_lock(&rmidi->open_mutex);
        if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
                list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams) {
-                       substream = list_entry(list, snd_rawmidi_substream_t, list);
+                       substream = list_entry(list, struct snd_rawmidi_substream, list);
                        snd_iprintf(buffer,
                                    "Output %d\n"
                                    "  Tx bytes     : %lu\n",
@@ -1297,7 +1340,7 @@ static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
        }
        if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
                list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
-                       substream = list_entry(list, snd_rawmidi_substream_t, list);
+                       substream = list_entry(list, struct snd_rawmidi_substream, list);
                        snd_iprintf(buffer,
                                    "Input %d\n"
                                    "  Rx bytes     : %lu\n",
@@ -1315,7 +1358,7 @@ static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
                        }
                }
        }
-       up(&rmidi->open_mutex);
+       mutex_unlock(&rmidi->open_mutex);
 }
 
 /*
@@ -1330,28 +1373,24 @@ static struct file_operations snd_rawmidi_f_ops =
        .open =         snd_rawmidi_open,
        .release =      snd_rawmidi_release,
        .poll =         snd_rawmidi_poll,
-       .ioctl =        snd_rawmidi_ioctl,
-};
-
-static snd_minor_t snd_rawmidi_reg =
-{
-       .comment =      "raw midi",
-       .f_ops =        &snd_rawmidi_f_ops,
+       .unlocked_ioctl =       snd_rawmidi_ioctl,
+       .compat_ioctl = snd_rawmidi_ioctl_compat,
 };
 
-static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
-                                       snd_rawmidi_str_t *stream,
+static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
+                                       struct snd_rawmidi_str *stream,
                                        int direction,
                                        int count)
 {
-       snd_rawmidi_substream_t *substream;
+       struct snd_rawmidi_substream *substream;
        int idx;
 
-       INIT_LIST_HEAD(&stream->substreams);
        for (idx = 0; idx < count; idx++) {
-               substream = snd_kcalloc(sizeof(snd_rawmidi_substream_t), GFP_KERNEL);
-               if (substream == NULL)
+               substream = kzalloc(sizeof(*substream), GFP_KERNEL);
+               if (substream == NULL) {
+                       snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
                        return -ENOMEM;
+               }
                substream->stream = direction;
                substream->number = idx;
                substream->rmidi = rmidi;
@@ -1376,36 +1415,46 @@ static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
  *
  * Returns zero if successful, or a negative error code on failure.
  */
-int snd_rawmidi_new(snd_card_t * card, char *id, int device,
+int snd_rawmidi_new(struct snd_card *card, char *id, int device,
                    int output_count, int input_count,
-                   snd_rawmidi_t ** rrawmidi)
+                   struct snd_rawmidi ** rrawmidi)
 {
-       snd_rawmidi_t *rmidi;
+       struct snd_rawmidi *rmidi;
        int err;
-       static snd_device_ops_t ops = {
+       static struct snd_device_ops ops = {
                .dev_free = snd_rawmidi_dev_free,
                .dev_register = snd_rawmidi_dev_register,
                .dev_disconnect = snd_rawmidi_dev_disconnect,
-               .dev_unregister = snd_rawmidi_dev_unregister
        };
 
        snd_assert(rrawmidi != NULL, return -EINVAL);
        *rrawmidi = NULL;
        snd_assert(card != NULL, return -ENXIO);
-       rmidi = snd_magic_kcalloc(snd_rawmidi_t, 0, GFP_KERNEL);
-       if (rmidi == NULL)
+       rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
+       if (rmidi == NULL) {
+               snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
                return -ENOMEM;
+       }
        rmidi->card = card;
        rmidi->device = device;
-       init_MUTEX(&rmidi->open_mutex);
+       mutex_init(&rmidi->open_mutex);
        init_waitqueue_head(&rmidi->open_wait);
+       INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
+       INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
+
        if (id != NULL)
                strlcpy(rmidi->id, id, sizeof(rmidi->id));
-       if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT], SNDRV_RAWMIDI_STREAM_INPUT, input_count)) < 0) {
+       if ((err = snd_rawmidi_alloc_substreams(rmidi,
+                                               &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
+                                               SNDRV_RAWMIDI_STREAM_INPUT,
+                                               input_count)) < 0) {
                snd_rawmidi_free(rmidi);
                return err;
        }
-       if ((err = snd_rawmidi_alloc_substreams(rmidi, &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT], SNDRV_RAWMIDI_STREAM_OUTPUT, output_count)) < 0) {
+       if ((err = snd_rawmidi_alloc_substreams(rmidi,
+                                               &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
+                                               SNDRV_RAWMIDI_STREAM_OUTPUT,
+                                               output_count)) < 0) {
                snd_rawmidi_free(rmidi);
                return err;
        }
@@ -1417,79 +1466,87 @@ int snd_rawmidi_new(snd_card_t * card, char *id, int device,
        return 0;
 }
 
-static void snd_rawmidi_free_substreams(snd_rawmidi_str_t *stream)
+static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
 {
-       snd_rawmidi_substream_t *substream;
+       struct snd_rawmidi_substream *substream;
 
        while (!list_empty(&stream->substreams)) {
-               substream = list_entry(stream->substreams.next, snd_rawmidi_substream_t, list);
+               substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
                list_del(&substream->list);
                kfree(substream);
        }
 }
 
-static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
+static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
 {
        snd_assert(rmidi != NULL, return -ENXIO);       
+
+       snd_info_free_entry(rmidi->proc_entry);
+       rmidi->proc_entry = NULL;
+       mutex_lock(&register_mutex);
+       if (rmidi->ops && rmidi->ops->dev_unregister)
+               rmidi->ops->dev_unregister(rmidi);
+       mutex_unlock(&register_mutex);
+
        snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
        snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
        if (rmidi->private_free)
                rmidi->private_free(rmidi);
-       snd_magic_kfree(rmidi);
+       kfree(rmidi);
        return 0;
 }
 
-static int snd_rawmidi_dev_free(snd_device_t *device)
+static int snd_rawmidi_dev_free(struct snd_device *device)
 {
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
+       struct snd_rawmidi *rmidi = device->device_data;
        return snd_rawmidi_free(rmidi);
 }
 
 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
-static void snd_rawmidi_dev_seq_free(snd_seq_device_t *device)
+static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
 {
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->private_data, return);
+       struct snd_rawmidi *rmidi = device->private_data;
        rmidi->seq_dev = NULL;
 }
 #endif
 
-static int snd_rawmidi_dev_register(snd_device_t *device)
+static int snd_rawmidi_dev_register(struct snd_device *device)
 {
-       int idx, err;
-       snd_info_entry_t *entry;
+       int err;
+       struct snd_info_entry *entry;
        char name[16];
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
+       struct snd_rawmidi *rmidi = device->device_data;
 
        if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
                return -ENOMEM;
-       down(&register_mutex);
-       idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
-       if (snd_rawmidi_devices[idx] != NULL) {
-               up(&register_mutex);
+       mutex_lock(&register_mutex);
+       if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
+               mutex_unlock(&register_mutex);
                return -EBUSY;
        }
-       snd_rawmidi_devices[idx] = rmidi;
+       list_add_tail(&rmidi->list, &snd_rawmidi_devices);
        sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
        if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
                                       rmidi->card, rmidi->device,
-                                      &snd_rawmidi_reg, name)) < 0) {
+                                      &snd_rawmidi_f_ops, rmidi, name)) < 0) {
                snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
-               snd_rawmidi_devices[idx] = NULL;
-               up(&register_mutex);
+               list_del(&rmidi->list);
+               mutex_unlock(&register_mutex);
                return err;
        }
        if (rmidi->ops && rmidi->ops->dev_register &&
            (err = rmidi->ops->dev_register(rmidi)) < 0) {
                snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
-               snd_rawmidi_devices[idx] = NULL;
-               up(&register_mutex);
+               list_del(&rmidi->list);
+               mutex_unlock(&register_mutex);
                return err;
        }
 #ifdef CONFIG_SND_OSSEMUL
        rmidi->ossreg = 0;
        if ((int)rmidi->device == midi_map[rmidi->card->number]) {
                if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
-                                           rmidi->card, 0, &snd_rawmidi_reg, name) < 0) {
+                                           rmidi->card, 0, &snd_rawmidi_f_ops,
+                                           rmidi, name) < 0) {
                        snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
                } else {
                        rmidi->ossreg++;
@@ -1500,19 +1557,19 @@ static int snd_rawmidi_dev_register(snd_device_t *device)
        }
        if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
                if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
-                                           rmidi->card, 1, &snd_rawmidi_reg, name) < 0) {
+                                           rmidi->card, 1, &snd_rawmidi_f_ops,
+                                           rmidi, name) < 0) {
                        snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
                } else {
                        rmidi->ossreg++;
                }
        }
 #endif /* CONFIG_SND_OSSEMUL */
-       up(&register_mutex);
+       mutex_unlock(&register_mutex);
        sprintf(name, "midi%d", rmidi->device);
        entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
        if (entry) {
                entry->private_data = rmidi;
-               entry->c.text.read_size = 1024;
                entry->c.text.read = snd_rawmidi_proc_info_read;
                if (snd_info_register(entry) < 0) {
                        snd_info_free_entry(entry);
@@ -1533,31 +1590,12 @@ static int snd_rawmidi_dev_register(snd_device_t *device)
        return 0;
 }
 
-static int snd_rawmidi_dev_disconnect(snd_device_t *device)
+static int snd_rawmidi_dev_disconnect(struct snd_device *device)
 {
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
-       int idx;
+       struct snd_rawmidi *rmidi = device->device_data;
 
-       down(&register_mutex);
-       idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
-       snd_rawmidi_devices[idx] = NULL;
-       up(&register_mutex);
-       return 0;
-}
-
-static int snd_rawmidi_dev_unregister(snd_device_t *device)
-{
-       int idx;
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
-
-       snd_assert(rmidi != NULL, return -ENXIO);
-       down(&register_mutex);
-       idx = (rmidi->card->number * SNDRV_RAWMIDI_DEVICES) + rmidi->device;
-       snd_rawmidi_devices[idx] = NULL;
-       if (rmidi->proc_entry) {
-               snd_info_unregister(rmidi->proc_entry);
-               rmidi->proc_entry = NULL;
-       }
+       mutex_lock(&register_mutex);
+       list_del_init(&rmidi->list);
 #ifdef CONFIG_SND_OSSEMUL
        if (rmidi->ossreg) {
                if ((int)rmidi->device == midi_map[rmidi->card->number]) {
@@ -1571,17 +1609,9 @@ static int snd_rawmidi_dev_unregister(snd_device_t *device)
                rmidi->ossreg = 0;
        }
 #endif /* CONFIG_SND_OSSEMUL */
-       if (rmidi->ops && rmidi->ops->dev_unregister)
-               rmidi->ops->dev_unregister(rmidi);
        snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
-       up(&register_mutex);
-#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
-       if (rmidi->seq_dev) {
-               snd_device_free(rmidi->card, rmidi->seq_dev);
-               rmidi->seq_dev = NULL;
-       }
-#endif
-       return snd_rawmidi_free(rmidi);
+       mutex_unlock(&register_mutex);
+       return 0;
 }
 
 /**
@@ -1592,13 +1622,14 @@ static int snd_rawmidi_dev_unregister(snd_device_t *device)
  *
  * Sets the rawmidi operators for the given stream direction.
  */
-void snd_rawmidi_set_ops(snd_rawmidi_t *rmidi, int stream, snd_rawmidi_ops_t *ops)
+void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
+                        struct snd_rawmidi_ops *ops)
 {
        struct list_head *list;
-       snd_rawmidi_substream_t *substream;
+       struct snd_rawmidi_substream *substream;
        
        list_for_each(list, &rmidi->streams[stream].substreams) {
-               substream = list_entry(list, snd_rawmidi_substream_t, list);
+               substream = list_entry(list, struct snd_rawmidi_substream, list);
                substream->ops = ops;
        }
 }
@@ -1611,6 +1642,7 @@ static int __init alsa_rawmidi_init(void)
 {
 
        snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
+       snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
 #ifdef CONFIG_SND_OSSEMUL
        { int i;
        /* check device map table */
@@ -1632,31 +1664,12 @@ static int __init alsa_rawmidi_init(void)
 static void __exit alsa_rawmidi_exit(void)
 {
        snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
+       snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
 }
 
 module_init(alsa_rawmidi_init)
 module_exit(alsa_rawmidi_exit)
 
-#ifndef MODULE
-#ifdef CONFIG_SND_OSSEMUL
-/* format is: snd-rawmidi=midi_map,amidi_map */
-
-static int __init alsa_rawmidi_setup(char *str)
-{
-       static unsigned __initdata nr_dev = 0;
-
-       if (nr_dev >= SNDRV_CARDS)
-               return 0;
-       (void)(get_option(&str,&midi_map[nr_dev]) == 2 &&
-              get_option(&str,&amidi_map[nr_dev]) == 2);
-       nr_dev++;
-       return 1;
-}
-
-__setup("snd-rawmidi=", alsa_rawmidi_setup);
-#endif /* CONFIG_SND_OSSEMUL */
-#endif /* ifndef MODULE */
-
 EXPORT_SYMBOL(snd_rawmidi_output_params);
 EXPORT_SYMBOL(snd_rawmidi_input_params);
 EXPORT_SYMBOL(snd_rawmidi_drop_output);
@@ -1669,7 +1682,6 @@ EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
 EXPORT_SYMBOL(snd_rawmidi_transmit);
 EXPORT_SYMBOL(snd_rawmidi_new);
 EXPORT_SYMBOL(snd_rawmidi_set_ops);
-EXPORT_SYMBOL(snd_rawmidi_info);
 EXPORT_SYMBOL(snd_rawmidi_info_select);
 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
 EXPORT_SYMBOL(snd_rawmidi_kernel_release);