Merge branch 'fix/asoc' into for-linus
[safe/jmp/linux-2.6] / sound / core / rawmidi.c
index ede0a60..002777b 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Abstract layer for MIDI v1.0 stream
- *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
+ *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  *
  *
  *   This program is free software; you can redistribute it and/or modify
  *
  */
 
-#include <sound/driver.h>
 #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>
 #include <sound/minors.h>
 #include <sound/initval.h>
 
-MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
+MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
 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_param_array(midi_map, int, NULL, 0444);
 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
@@ -54,11 +52,19 @@ 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 int snd_rawmidi_dev_unregister(struct snd_device *device);
 
-static struct snd_rawmidi *snd_rawmidi_devices[SNDRV_CARDS * SNDRV_RAWMIDI_DEVICES];
+static LIST_HEAD(snd_rawmidi_devices);
+static DEFINE_MUTEX(register_mutex);
+
+static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
+{
+       struct snd_rawmidi *rawmidi;
 
-static DECLARE_MUTEX(register_mutex);
+       list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
+               if (rawmidi->card == card && rawmidi->device == device)
+                       return rawmidi;
+       return NULL;
+}
 
 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
 {
@@ -142,8 +148,10 @@ static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
 
 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
 {
+       if (!substream->opened)
+               return;
        if (up) {
-               tasklet_hi_schedule(&substream->runtime->tasklet);
+               tasklet_schedule(&substream->runtime->tasklet);
        } else {
                tasklet_kill(&substream->runtime->tasklet);
                substream->ops->trigger(substream, 0);
@@ -152,6 +160,8 @@ static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *subs
 
 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
 {
+       if (!substream->opened)
+               return;
        substream->ops->trigger(substream, up);
        if (!up && substream->runtime->event)
                tasklet_kill(&substream->runtime->tasklet);
@@ -214,7 +224,7 @@ int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
        return 0;
 }
 
-int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
+int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
                            int mode, struct snd_rawmidi_file * rfile)
 {
        struct snd_rawmidi *rmidi;
@@ -225,7 +235,9 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
 
        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;
@@ -235,7 +247,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;
@@ -345,7 +357,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;
@@ -360,7 +372,7 @@ int snd_rawmidi_kernel_open(int cardnum, int device, int subdevice,
                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;
 }
@@ -368,39 +380,28 @@ 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;
        struct snd_card *card;
-       int device, subdevice;
+       int subdevice;
        unsigned short fflags;
        int err;
        struct snd_rawmidi *rmidi;
        struct snd_rawmidi_file *rawmidi_file;
        wait_queue_t wait;
-       struct list_head *list;
        struct snd_ctl_file *kctl;
 
        if (maj == snd_major) {
-               cardnum = SNDRV_MINOR_CARD(iminor(inode));
-               cardnum %= SNDRV_CARDS;
-               device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_RAWMIDI;
-               device %= SNDRV_MINOR_RAWMIDIS;
+               rmidi = snd_lookup_minor_data(iminor(inode),
+                                             SNDRV_DEVICE_TYPE_RAWMIDI);
 #ifdef CONFIG_SND_OSSEMUL
        } else if (maj == 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];
+               rmidi = snd_lookup_oss_minor_data(iminor(inode),
+                                                 SNDRV_OSS_DEVICE_TYPE_MIDI);
 #endif
        } 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;
@@ -418,19 +419,20 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
        }
        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);
-               list_for_each(list, &card->ctl_files) {
-                       kctl = snd_ctl_file(list);
+               read_lock(&card->ctl_files_rwlock);
+               list_for_each_entry(kctl, &card->ctl_files, 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);
+               read_unlock(&card->ctl_files_rwlock);
+               err = snd_rawmidi_kernel_open(rmidi->card, rmidi->device,
+                                             subdevice, fflags, rawmidi_file);
                if (err >= 0)
                        break;
                if (err == -EAGAIN) {
@@ -441,9 +443,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;
@@ -462,7 +464,7 @@ static int snd_rawmidi_open(struct inode *inode, struct file *file)
                snd_card_file_remove(card, file);
                kfree(rawmidi_file);
        }
-       up(&rmidi->open_mutex);
+       mutex_unlock(&rmidi->open_mutex);
        return err;
 }
 
@@ -472,10 +474,10 @@ int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
        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);
+       if (snd_BUG_ON(!rfile))
+               return -ENXIO;
        rmidi = rfile->rmidi;
-       down(&rmidi->open_mutex);
+       mutex_lock(&rmidi->open_mutex);
        if (rfile->input != NULL) {
                substream = rfile->input;
                rfile->input = NULL;
@@ -509,7 +511,7 @@ int snd_rawmidi_kernel_release(struct snd_rawmidi_file * rfile)
                }
                rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
        }
-       up(&rmidi->open_mutex);
+       mutex_unlock(&rmidi->open_mutex);
        module_put(rmidi->card->module);
        return 0;
 }
@@ -529,8 +531,8 @@ static int snd_rawmidi_release(struct inode *inode, struct file *file)
        return err;
 }
 
-int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
-                    struct snd_rawmidi_info *info)
+static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
+                           struct snd_rawmidi_info *info)
 {
        struct snd_rawmidi *rmidi;
        
@@ -569,10 +571,12 @@ int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info
        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];
@@ -580,8 +584,7 @@ int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info
                return -ENOENT;
        if (info->subdevice >= pstr->substream_count)
                return -ENXIO;
-       list_for_each(list, &pstr->substreams) {
-               substream = list_entry(list, struct snd_rawmidi_substream, list);
+       list_for_each_entry(substream, &pstr->substreams, list) {
                if ((unsigned int)substream->number == info->subdevice)
                        return snd_rawmidi_info(substream, info);
        }
@@ -622,11 +625,13 @@ int snd_rawmidi_output_params(struct snd_rawmidi_substream *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;
@@ -647,7 +652,8 @@ int snd_rawmidi_input_params(struct snd_rawmidi_substream *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;
@@ -801,9 +807,7 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
                                     unsigned long arg)
 {
        void __user *argp = (void __user *)arg;
-       unsigned int tmp;
 
-       tmp = card->number * SNDRV_RAWMIDI_DEVICES;
        switch (cmd) {
        case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
        {
@@ -811,14 +815,16 @@ static int snd_rawmidi_control_ioctl(struct snd_card *card,
                
                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;
+               mutex_unlock(&register_mutex);
                if (put_user(device, (int __user *)argp))
                        return -EFAULT;
                return 0;
@@ -855,6 +861,8 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
        int result = 0, count1;
        struct snd_rawmidi_runtime *runtime = substream->runtime;
 
+       if (!substream->opened)
+               return -EBADFD;
        if (runtime->buffer == NULL) {
                snd_printd("snd_rawmidi_receive: input is not active!!!\n");
                return -EINVAL;
@@ -900,7 +908,7 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
        }
        if (result > 0) {
                if (runtime->event)
-                       tasklet_hi_schedule(&runtime->tasklet);
+                       tasklet_schedule(&runtime->tasklet);
                else if (snd_rawmidi_ready(substream))
                        wake_up(&runtime->sleep);
        }
@@ -909,7 +917,8 @@ int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
 }
 
 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
-                                    unsigned char *buf, long count, int kernel)
+                                    unsigned char __user *userbuf,
+                                    unsigned char *kernelbuf, long count)
 {
        unsigned long flags;
        long result = 0, count1;
@@ -922,11 +931,11 @@ static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
                spin_lock_irqsave(&runtime->lock, flags);
                if (count1 > (int)runtime->avail)
                        count1 = runtime->avail;
-               if (kernel) {
-                       memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
-               } else {
+               if (kernelbuf)
+                       memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
+               if (userbuf) {
                        spin_unlock_irqrestore(&runtime->lock, flags);
-                       if (copy_to_user((char __user *)buf + result,
+                       if (copy_to_user(userbuf + result,
                                         runtime->buffer + runtime->appl_ptr, count1)) {
                                return result > 0 ? result : -EFAULT;
                        }
@@ -946,7 +955,7 @@ long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
                             unsigned char *buf, long count)
 {
        snd_rawmidi_input_trigger(substream, 1);
-       return snd_rawmidi_kernel_read1(substream, buf, count, 1);
+       return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
 }
 
 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
@@ -987,8 +996,9 @@ static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t coun
                }
                spin_unlock_irq(&runtime->lock);
                count1 = snd_rawmidi_kernel_read1(substream,
-                                                 (unsigned char __force *)buf,
-                                                 count, 0);
+                                                 (unsigned char __user *)buf,
+                                                 NULL/*kernelbuf*/,
+                                                 count);
                if (count1 < 0)
                        return result > 0 ? result : count1;
                result += count1;
@@ -1096,7 +1106,7 @@ int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
                return -EINVAL;
        }
        spin_lock_irqsave(&runtime->lock, flags);
-       snd_assert(runtime->avail + count <= runtime->buffer_size, );
+       snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
        runtime->hw_ptr += count;
        runtime->hw_ptr %= runtime->buffer_size;
        runtime->avail += count;
@@ -1122,6 +1132,8 @@ int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
                         unsigned char *buffer, int count)
 {
+       if (!substream->opened)
+               return -EBADFD;
        count = snd_rawmidi_transmit_peek(substream, buffer, count);
        if (count < 0)
                return count;
@@ -1129,14 +1141,18 @@ int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
 }
 
 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
-                                     const unsigned char *buf, long count, int kernel)
+                                     const unsigned char __user *userbuf,
+                                     const unsigned char *kernelbuf,
+                                     long count)
 {
        unsigned long flags;
        long count1, result;
        struct snd_rawmidi_runtime *runtime = substream->runtime;
 
-       snd_assert(buf != NULL, return -EINVAL);
-       snd_assert(runtime->buffer != NULL, return -EINVAL);
+       if (snd_BUG_ON(!kernelbuf && !userbuf))
+               return -EINVAL;
+       if (snd_BUG_ON(!runtime->buffer))
+               return -EINVAL;
 
        result = 0;
        spin_lock_irqsave(&runtime->lock, flags);
@@ -1152,12 +1168,13 @@ static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
                        count1 = count;
                if (count1 > (long)runtime->avail)
                        count1 = runtime->avail;
-               if (kernel) {
-                       memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
-               } else {
+               if (kernelbuf)
+                       memcpy(runtime->buffer + runtime->appl_ptr,
+                              kernelbuf + result, count1);
+               else if (userbuf) {
                        spin_unlock_irqrestore(&runtime->lock, flags);
                        if (copy_from_user(runtime->buffer + runtime->appl_ptr,
-                                          (char __user *)buf, count1)) {
+                                          userbuf + result, count1)) {
                                spin_lock_irqsave(&runtime->lock, flags);
                                result = result > 0 ? result : -EFAULT;
                                goto __end;
@@ -1168,7 +1185,6 @@ static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
                runtime->appl_ptr %= runtime->buffer_size;
                runtime->avail -= count1;
                result += count1;
-               buf += count1;
                count -= count1;
        }
       __end:
@@ -1182,7 +1198,7 @@ static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
 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);
+       return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
 }
 
 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
@@ -1222,9 +1238,7 @@ static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
                        spin_lock_irq(&runtime->lock);
                }
                spin_unlock_irq(&runtime->lock);
-               count1 = snd_rawmidi_kernel_write1(substream,
-                                                  (unsigned char __force *)buf,
-                                                  count, 0);
+               count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
                if (count1 < 0)
                        return result > 0 ? result : count1;
                result += count1;
@@ -1301,14 +1315,14 @@ static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
        struct snd_rawmidi *rmidi;
        struct snd_rawmidi_substream *substream;
        struct snd_rawmidi_runtime *runtime;
-       struct list_head *list;
 
        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, struct snd_rawmidi_substream, list);
+               list_for_each_entry(substream,
+                                   &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
+                                   list) {
                        snd_iprintf(buffer,
                                    "Output %d\n"
                                    "  Tx bytes     : %lu\n",
@@ -1327,8 +1341,9 @@ static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
                }
        }
        if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
-               list_for_each(list, &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams) {
-                       substream = list_entry(list, struct snd_rawmidi_substream, list);
+               list_for_each_entry(substream,
+                                   &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
+                                   list) {
                        snd_iprintf(buffer,
                                    "Input %d\n"
                                    "  Rx bytes     : %lu\n",
@@ -1346,14 +1361,14 @@ static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
                        }
                }
        }
-       up(&rmidi->open_mutex);
+       mutex_unlock(&rmidi->open_mutex);
 }
 
 /*
  *  Register functions
  */
 
-static struct file_operations snd_rawmidi_f_ops =
+static const struct file_operations snd_rawmidi_f_ops =
 {
        .owner =        THIS_MODULE,
        .read =         snd_rawmidi_read,
@@ -1365,12 +1380,6 @@ static struct file_operations snd_rawmidi_f_ops =
        .compat_ioctl = snd_rawmidi_ioctl_compat,
 };
 
-static struct snd_minor snd_rawmidi_reg =
-{
-       .comment =      "raw midi",
-       .f_ops =        &snd_rawmidi_f_ops,
-};
-
 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
                                        struct snd_rawmidi_str *stream,
                                        int direction,
@@ -1379,11 +1388,12 @@ static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
        struct snd_rawmidi_substream *substream;
        int idx;
 
-       INIT_LIST_HEAD(&stream->substreams);
        for (idx = 0; idx < count; idx++) {
                substream = kzalloc(sizeof(*substream), GFP_KERNEL);
-               if (substream == NULL)
+               if (substream == NULL) {
+                       snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
                        return -ENOMEM;
+               }
                substream->stream = direction;
                substream->number = idx;
                substream->rmidi = rmidi;
@@ -1418,26 +1428,37 @@ int snd_rawmidi_new(struct snd_card *card, char *id, int device,
                .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);
+       if (snd_BUG_ON(!card))
+               return -ENXIO;
+       if (rrawmidi)
+               *rrawmidi = NULL;
        rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
-       if (rmidi == NULL)
+       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;
        }
@@ -1445,7 +1466,8 @@ int snd_rawmidi_new(struct snd_card *card, char *id, int device,
                snd_rawmidi_free(rmidi);
                return err;
        }
-       *rrawmidi = rmidi;
+       if (rrawmidi)
+               *rrawmidi = rmidi;
        return 0;
 }
 
@@ -1462,7 +1484,16 @@ static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
 
 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
 {
-       snd_assert(rmidi != NULL, return -ENXIO);       
+       if (!rmidi)
+               return 0;
+
+       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)
@@ -1487,41 +1518,41 @@ static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
 
 static int snd_rawmidi_dev_register(struct snd_device *device)
 {
-       int idx, err;
+       int err;
        struct snd_info_entry *entry;
        char name[16];
        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++;
@@ -1532,19 +1563,19 @@ static int snd_rawmidi_dev_register(struct snd_device *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);
@@ -1568,28 +1599,9 @@ static int snd_rawmidi_dev_register(struct snd_device *device)
 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
 {
        struct snd_rawmidi *rmidi = device->device_data;
-       int idx;
-
-       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(struct snd_device *device)
-{
-       int idx;
-       struct snd_rawmidi *rmidi = device->device_data;
 
-       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]) {
@@ -1603,17 +1615,9 @@ static int snd_rawmidi_dev_unregister(struct snd_device *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;
 }
 
 /**
@@ -1627,13 +1631,10 @@ static int snd_rawmidi_dev_unregister(struct snd_device *device)
 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
                         struct snd_rawmidi_ops *ops)
 {
-       struct list_head *list;
        struct snd_rawmidi_substream *substream;
        
-       list_for_each(list, &rmidi->streams[stream].substreams) {
-               substream = list_entry(list, struct snd_rawmidi_substream, list);
+       list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
                substream->ops = ops;
-       }
 }
 
 /*
@@ -1684,7 +1685,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);