vserver 2.0 rc7
[linux-2.6.git] / sound / core / rawmidi.c
index df904d1..edba411 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/moduleparam.h>
+#include <linux/delay.h>
+#include <linux/wait.h>
 #include <sound/rawmidi.h>
 #include <sound/info.h>
 #include <sound/control.h>
@@ -41,13 +44,10 @@ MODULE_LICENSE("GPL");
 #ifdef CONFIG_SND_OSSEMUL
 static int midi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 0};
 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
-static int boot_devs;
-module_param_array(midi_map, int, boot_devs, 0444);
+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_param_array(amidi_map, int, boot_devs, 0444);
+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);
@@ -56,7 +56,7 @@ 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);
 
-snd_rawmidi_t *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES];
+static snd_rawmidi_t *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES];
 
 static DECLARE_MUTEX(register_mutex);
 
@@ -85,11 +85,34 @@ static inline int snd_rawmidi_ready_append(snd_rawmidi_substream_t * substream,
               (!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)
 {
-       snd_rawmidi_runtime_t *runtime = substream->runtime;
+       snd_rawmidi_substream_t *substream = (snd_rawmidi_substream_t *)data;
+       substream->runtime->event(substream);
+}
+
+static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
+{
+       snd_rawmidi_substream_t *substream = (snd_rawmidi_substream_t *)data;
+       substream->ops->trigger(substream, 1);
+}
+
+static int snd_rawmidi_runtime_create(snd_rawmidi_substream_t * substream)
+{
+       snd_rawmidi_runtime_t *runtime;
+
+       if ((runtime = kcalloc(1, 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;
@@ -97,32 +120,53 @@ 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(snd_rawmidi_substream_t * substream)
 {
-       if (runtime->buffer) {
-               kfree(runtime->buffer);
-               runtime->buffer = NULL;
-       }
+       snd_rawmidi_runtime_t *runtime = substream->runtime;
+
+       kfree(runtime->buffer);
+       kfree(runtime);
+       substream->runtime = NULL;
        return 0;
 }
 
+static inline void snd_rawmidi_output_trigger(snd_rawmidi_substream_t * 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(snd_rawmidi_substream_t * substream, int up)
+{
+       substream->ops->trigger(substream, up);
+       if (!up && substream->runtime->event)
+               tasklet_kill(&substream->runtime->tasklet);
+}
+
 int snd_rawmidi_drop_output(snd_rawmidi_substream_t * substream)
 {
+       unsigned long flags;
        snd_rawmidi_runtime_t *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;
 }
 
@@ -134,27 +178,22 @@ int snd_rawmidi_drain_output(snd_rawmidi_substream_t * substream)
 
        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;
@@ -162,14 +201,15 @@ int snd_rawmidi_drain_output(snd_rawmidi_substream_t * substream)
 
 int snd_rawmidi_drain_input(snd_rawmidi_substream_t * substream)
 {
+       unsigned long flags;
        snd_rawmidi_runtime_t *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;
 }
 
@@ -178,7 +218,7 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
 {
        snd_rawmidi_t *rmidi;
        struct list_head *list1, *list2;
-       snd_rawmidi_substream_t *sinput, *soutput;
+       snd_rawmidi_substream_t *sinput = NULL, *soutput = NULL;
        snd_rawmidi_runtime_t *input = NULL, *output = NULL;
        int err;
 
@@ -269,20 +309,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 {
@@ -291,26 +322,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:
@@ -333,14 +353,10 @@ 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);
@@ -398,7 +414,7 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
        if ((file->f_flags & O_APPEND) || maj != CONFIG_SND_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;
@@ -447,7 +463,7 @@ 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);
        return err;
@@ -467,14 +483,11 @@ int snd_rawmidi_kernel_release(snd_rawmidi_file_t * rfile)
                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--;
        }
@@ -489,13 +502,11 @@ 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;
                }
@@ -512,11 +523,11 @@ static int snd_rawmidi_release(struct inode *inode, struct file *file)
        snd_rawmidi_t *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;
 }
@@ -675,13 +686,12 @@ 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;
        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) {
@@ -749,7 +759,7 @@ static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
        case SNDRV_RAWMIDI_IOCTL_DROP:
        {
                int val;
-               if (get_user(val, (long __user *) argp))
+               if (get_user(val, (int __user *) argp))
                        return -EFAULT;
                switch (val) {
                case SNDRV_RAWMIDI_STREAM_OUTPUT:
@@ -763,7 +773,7 @@ static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
        case SNDRV_RAWMIDI_IOCTL_DRAIN:
        {
                int val;
-               if (get_user(val, (long __user *) argp))
+               if (get_user(val, (int __user *) argp))
                        return -EFAULT;
                switch (val) {
                case SNDRV_RAWMIDI_STREAM_OUTPUT:
@@ -786,8 +796,10 @@ 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(snd_card_t * card,
+                                    snd_ctl_file_t * control,
+                                    unsigned int cmd,
+                                    unsigned long arg)
 {
        void __user *argp = (void __user *)arg;
        unsigned int tmp;
@@ -876,7 +888,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);
@@ -886,13 +898,13 @@ 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;
 }
 
@@ -914,7 +926,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);
@@ -931,8 +944,7 @@ static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
 
 long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *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);
 }
 
@@ -944,13 +956,12 @@ static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t coun
        snd_rawmidi_substream_t *substream;
        snd_rawmidi_runtime_t *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);
@@ -973,7 +984,9 @@ static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t coun
                        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 *)buf, count, 0);
+               if (count1 < 0)
+                       return result > 0 ? result : count1;
                result += count1;
                buf += count1;
                count -= count1;
@@ -999,8 +1012,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;          
 }
@@ -1033,7 +1044,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 */
@@ -1048,8 +1058,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);
@@ -1082,15 +1096,11 @@ 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;
 }
 
@@ -1139,7 +1149,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;
@@ -1154,12 +1165,10 @@ 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;
 }
 
@@ -1176,7 +1185,7 @@ static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size
        snd_rawmidi_runtime_t *runtime;
        snd_rawmidi_substream_t *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 */
@@ -1204,16 +1213,16 @@ static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size
                        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 *)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;
@@ -1241,11 +1250,10 @@ static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
        snd_rawmidi_runtime_t *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) {
@@ -1264,6 +1272,14 @@ 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
+
 /*
 
  */
@@ -1276,7 +1292,7 @@ static void snd_rawmidi_proc_info_read(snd_info_entry_t *entry,
        snd_rawmidi_runtime_t *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);
        if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
@@ -1334,7 +1350,8 @@ static struct file_operations snd_rawmidi_f_ops =
        .open =         snd_rawmidi_open,
        .release =      snd_rawmidi_release,
        .poll =         snd_rawmidi_poll,
-       .ioctl =        snd_rawmidi_ioctl,
+       .unlocked_ioctl =       snd_rawmidi_ioctl,
+       .compat_ioctl = snd_rawmidi_ioctl_compat,
 };
 
 static snd_minor_t snd_rawmidi_reg =
@@ -1353,7 +1370,7 @@ static int snd_rawmidi_alloc_substreams(snd_rawmidi_t *rmidi,
 
        INIT_LIST_HEAD(&stream->substreams);
        for (idx = 0; idx < count; idx++) {
-               substream = snd_kcalloc(sizeof(snd_rawmidi_substream_t), GFP_KERNEL);
+               substream = kcalloc(1, sizeof(*substream), GFP_KERNEL);
                if (substream == NULL)
                        return -ENOMEM;
                substream->stream = direction;
@@ -1396,7 +1413,7 @@ int snd_rawmidi_new(snd_card_t * card, char *id, int device,
        snd_assert(rrawmidi != NULL, return -EINVAL);
        *rrawmidi = NULL;
        snd_assert(card != NULL, return -ENXIO);
-       rmidi = snd_magic_kcalloc(snd_rawmidi_t, 0, GFP_KERNEL);
+       rmidi = kcalloc(1, sizeof(*rmidi), GFP_KERNEL);
        if (rmidi == NULL)
                return -ENOMEM;
        rmidi->card = card;
@@ -1439,20 +1456,20 @@ static int snd_rawmidi_free(snd_rawmidi_t *rmidi)
        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)
 {
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
+       snd_rawmidi_t *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)
 {
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->private_data, return);
+       snd_rawmidi_t *rmidi = device->private_data;
        rmidi->seq_dev = NULL;
 }
 #endif
@@ -1462,7 +1479,7 @@ static int snd_rawmidi_dev_register(snd_device_t *device)
        int idx, err;
        snd_info_entry_t *entry;
        char name[16];
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
+       snd_rawmidi_t *rmidi = device->device_data;
 
        if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
                return -ENOMEM;
@@ -1539,7 +1556,7 @@ static int snd_rawmidi_dev_register(snd_device_t *device)
 
 static int snd_rawmidi_dev_disconnect(snd_device_t *device)
 {
-       snd_rawmidi_t *rmidi = snd_magic_cast(snd_rawmidi_t, device->device_data, return -ENXIO);
+       snd_rawmidi_t *rmidi = device->device_data;
        int idx;
 
        down(&register_mutex);
@@ -1552,7 +1569,7 @@ static int snd_rawmidi_dev_disconnect(snd_device_t *device)
 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_rawmidi_t *rmidi = device->device_data;
 
        snd_assert(rmidi != NULL, return -ENXIO);
        down(&register_mutex);
@@ -1615,6 +1632,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 */
@@ -1636,6 +1654,7 @@ 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)