]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - drivers/uio/uio.c
uio: fix allocating minor id for uio device
[linux-2.6.git] / drivers / uio / uio.c
index 865f32b63b5c0d2d468348f77a1c2c8c07d34879..d2efe823c20dd7fc317a1ffc0e12c283138e531d 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright(C) 2005, Benedikt Spranger <b.spranger@linutronix.de>
  * Copyright(C) 2005, Thomas Gleixner <tglx@linutronix.de>
- * Copyright(C) 2006, Hans J. Koch <hjk@linutronix.de>
+ * Copyright(C) 2006, Hans J. Koch <hjk@hansjkoch.de>
  * Copyright(C) 2006, Greg Kroah-Hartman <greg@kroah.com>
  *
  * Userspace IO
 #include <linux/init.h>
 #include <linux/poll.h>
 #include <linux/device.h>
+#include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/idr.h>
+#include <linux/sched.h>
 #include <linux/string.h>
 #include <linux/kobject.h>
+#include <linux/cdev.h>
 #include <linux/uio_driver.h>
 
-#define UIO_MAX_DEVICES 255
+#define UIO_MAX_DEVICES                (1U << MINORBITS)
 
 struct uio_device {
        struct module           *owner;
@@ -34,109 +37,225 @@ struct uio_device {
        wait_queue_head_t       wait;
        int                     vma_count;
        struct uio_info         *info;
-       struct kset             map_attr_kset;
+       struct kobject          *map_dir;
+       struct kobject          *portio_dir;
 };
 
 static int uio_major;
+static struct cdev *uio_cdev;
 static DEFINE_IDR(uio_idr);
-static struct file_operations uio_fops;
+static const struct file_operations uio_fops;
 
-/* UIO class infrastructure */
-static struct uio_class {
-       struct kref kref;
-       struct class *class;
-} *uio_class;
+/* Protect idr accesses */
+static DEFINE_MUTEX(minor_lock);
 
 /*
  * attributes
  */
 
-static struct attribute attr_addr = {
-       .name  = "addr",
-       .mode  = S_IRUGO,
+struct uio_map {
+       struct kobject kobj;
+       struct uio_mem *mem;
 };
+#define to_map(map) container_of(map, struct uio_map, kobj)
+
+static ssize_t map_name_show(struct uio_mem *mem, char *buf)
+{
+       if (unlikely(!mem->name))
+               mem->name = "";
+
+       return sprintf(buf, "%s\n", mem->name);
+}
+
+static ssize_t map_addr_show(struct uio_mem *mem, char *buf)
+{
+       return sprintf(buf, "0x%lx\n", mem->addr);
+}
 
-static struct attribute attr_size = {
-       .name  = "size",
-       .mode  = S_IRUGO,
+static ssize_t map_size_show(struct uio_mem *mem, char *buf)
+{
+       return sprintf(buf, "0x%lx\n", mem->size);
+}
+
+static ssize_t map_offset_show(struct uio_mem *mem, char *buf)
+{
+       return sprintf(buf, "0x%lx\n", mem->addr & ~PAGE_MASK);
+}
+
+struct map_sysfs_entry {
+       struct attribute attr;
+       ssize_t (*show)(struct uio_mem *, char *);
+       ssize_t (*store)(struct uio_mem *, const char *, size_t);
 };
 
-static struct attribute* map_attrs[] = {
-       &attr_addr, &attr_size, NULL
+static struct map_sysfs_entry name_attribute =
+       __ATTR(name, S_IRUGO, map_name_show, NULL);
+static struct map_sysfs_entry addr_attribute =
+       __ATTR(addr, S_IRUGO, map_addr_show, NULL);
+static struct map_sysfs_entry size_attribute =
+       __ATTR(size, S_IRUGO, map_size_show, NULL);
+static struct map_sysfs_entry offset_attribute =
+       __ATTR(offset, S_IRUGO, map_offset_show, NULL);
+
+static struct attribute *attrs[] = {
+       &name_attribute.attr,
+       &addr_attribute.attr,
+       &size_attribute.attr,
+       &offset_attribute.attr,
+       NULL,   /* need to NULL terminate the list of attributes */
 };
 
-static ssize_t map_attr_show(struct kobject *kobj, struct attribute *attr,
+static void map_release(struct kobject *kobj)
+{
+       struct uio_map *map = to_map(kobj);
+       kfree(map);
+}
+
+static ssize_t map_type_show(struct kobject *kobj, struct attribute *attr,
                             char *buf)
 {
-       struct uio_mem *mem = container_of(kobj, struct uio_mem, kobj);
+       struct uio_map *map = to_map(kobj);
+       struct uio_mem *mem = map->mem;
+       struct map_sysfs_entry *entry;
 
-       if (strncmp(attr->name,"addr",4) == 0)
-               return sprintf(buf, "0x%lx\n", mem->addr);
+       entry = container_of(attr, struct map_sysfs_entry, attr);
 
-       if (strncmp(attr->name,"size",4) == 0)
-               return sprintf(buf, "0x%lx\n", mem->size);
+       if (!entry->show)
+               return -EIO;
+
+       return entry->show(mem, buf);
+}
+
+static const struct sysfs_ops map_sysfs_ops = {
+       .show = map_type_show,
+};
+
+static struct kobj_type map_attr_type = {
+       .release        = map_release,
+       .sysfs_ops      = &map_sysfs_ops,
+       .default_attrs  = attrs,
+};
+
+struct uio_portio {
+       struct kobject kobj;
+       struct uio_port *port;
+};
+#define to_portio(portio) container_of(portio, struct uio_portio, kobj)
+
+static ssize_t portio_name_show(struct uio_port *port, char *buf)
+{
+       if (unlikely(!port->name))
+               port->name = "";
 
-       return -ENODEV;
+       return sprintf(buf, "%s\n", port->name);
 }
 
-static void map_attr_release(struct kobject *kobj)
+static ssize_t portio_start_show(struct uio_port *port, char *buf)
 {
-       /* TODO ??? */
+       return sprintf(buf, "0x%lx\n", port->start);
 }
 
-static struct sysfs_ops map_attr_ops = {
-       .show  = map_attr_show,
+static ssize_t portio_size_show(struct uio_port *port, char *buf)
+{
+       return sprintf(buf, "0x%lx\n", port->size);
+}
+
+static ssize_t portio_porttype_show(struct uio_port *port, char *buf)
+{
+       const char *porttypes[] = {"none", "x86", "gpio", "other"};
+
+       if ((port->porttype < 0) || (port->porttype > UIO_PORT_OTHER))
+               return -EINVAL;
+
+       return sprintf(buf, "port_%s\n", porttypes[port->porttype]);
+}
+
+struct portio_sysfs_entry {
+       struct attribute attr;
+       ssize_t (*show)(struct uio_port *, char *);
+       ssize_t (*store)(struct uio_port *, const char *, size_t);
 };
 
-static struct kobj_type map_attr_type = {
-       .release        = map_attr_release,
-       .sysfs_ops      = &map_attr_ops,
-       .default_attrs  = map_attrs,
+static struct portio_sysfs_entry portio_name_attribute =
+       __ATTR(name, S_IRUGO, portio_name_show, NULL);
+static struct portio_sysfs_entry portio_start_attribute =
+       __ATTR(start, S_IRUGO, portio_start_show, NULL);
+static struct portio_sysfs_entry portio_size_attribute =
+       __ATTR(size, S_IRUGO, portio_size_show, NULL);
+static struct portio_sysfs_entry portio_porttype_attribute =
+       __ATTR(porttype, S_IRUGO, portio_porttype_show, NULL);
+
+static struct attribute *portio_attrs[] = {
+       &portio_name_attribute.attr,
+       &portio_start_attribute.attr,
+       &portio_size_attribute.attr,
+       &portio_porttype_attribute.attr,
+       NULL,
+};
+
+static void portio_release(struct kobject *kobj)
+{
+       struct uio_portio *portio = to_portio(kobj);
+       kfree(portio);
+}
+
+static ssize_t portio_type_show(struct kobject *kobj, struct attribute *attr,
+                            char *buf)
+{
+       struct uio_portio *portio = to_portio(kobj);
+       struct uio_port *port = portio->port;
+       struct portio_sysfs_entry *entry;
+
+       entry = container_of(attr, struct portio_sysfs_entry, attr);
+
+       if (!entry->show)
+               return -EIO;
+
+       return entry->show(port, buf);
+}
+
+static const struct sysfs_ops portio_sysfs_ops = {
+       .show = portio_type_show,
+};
+
+static struct kobj_type portio_attr_type = {
+       .release        = portio_release,
+       .sysfs_ops      = &portio_sysfs_ops,
+       .default_attrs  = portio_attrs,
 };
 
 static ssize_t show_name(struct device *dev,
                         struct device_attribute *attr, char *buf)
 {
        struct uio_device *idev = dev_get_drvdata(dev);
-       if (idev)
-               return sprintf(buf, "%s\n", idev->info->name);
-       else
-               return -ENODEV;
+       return sprintf(buf, "%s\n", idev->info->name);
 }
-static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
 
 static ssize_t show_version(struct device *dev,
                            struct device_attribute *attr, char *buf)
 {
        struct uio_device *idev = dev_get_drvdata(dev);
-       if (idev)
-               return sprintf(buf, "%s\n", idev->info->version);
-       else
-               return -ENODEV;
+       return sprintf(buf, "%s\n", idev->info->version);
 }
-static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
 
 static ssize_t show_event(struct device *dev,
                          struct device_attribute *attr, char *buf)
 {
        struct uio_device *idev = dev_get_drvdata(dev);
-       if (idev)
-               return sprintf(buf, "%u\n",
-                               (unsigned int)atomic_read(&idev->event));
-       else
-               return -ENODEV;
+       return sprintf(buf, "%u\n", (unsigned int)atomic_read(&idev->event));
 }
-static DEVICE_ATTR(event, S_IRUGO, show_event, NULL);
 
-static struct attribute *uio_attrs[] = {
-       &dev_attr_name.attr,
-       &dev_attr_version.attr,
-       &dev_attr_event.attr,
-       NULL,
+static struct device_attribute uio_class_attributes[] = {
+       __ATTR(name, S_IRUGO, show_name, NULL),
+       __ATTR(version, S_IRUGO, show_version, NULL),
+       __ATTR(event, S_IRUGO, show_event, NULL),
+       {}
 };
 
-static struct attribute_group uio_attr_grp = {
-       .attrs = uio_attrs,
+/* UIO class infrastructure */
+static struct class uio_class = {
+       .name = "uio",
+       .dev_attrs = uio_class_attributes,
 };
 
 /*
@@ -145,13 +264,13 @@ static struct attribute_group uio_attr_grp = {
 static int uio_dev_add_attributes(struct uio_device *idev)
 {
        int ret;
-       int mi;
+       int mi, pi;
        int map_found = 0;
+       int portio_found = 0;
        struct uio_mem *mem;
-
-       ret = sysfs_create_group(&idev->dev->kobj, &uio_attr_grp);
-       if (ret)
-               goto err_group;
+       struct uio_map *map;
+       struct uio_port *port;
+       struct uio_portio *portio;
 
        for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
                mem = &idev->info->mem[mi];
@@ -159,54 +278,96 @@ static int uio_dev_add_attributes(struct uio_device *idev)
                        break;
                if (!map_found) {
                        map_found = 1;
-                       kobject_set_name(&idev->map_attr_kset.kobj,"maps");
-                       idev->map_attr_kset.ktype = &map_attr_type;
-                       idev->map_attr_kset.kobj.parent = &idev->dev->kobj;
-                       ret = kset_register(&idev->map_attr_kset);
-                       if (ret)
-                               goto err_remove_group;
+                       idev->map_dir = kobject_create_and_add("maps",
+                                                       &idev->dev->kobj);
+                       if (!idev->map_dir)
+                               goto err_map;
+               }
+               map = kzalloc(sizeof(*map), GFP_KERNEL);
+               if (!map)
+                       goto err_map;
+               kobject_init(&map->kobj, &map_attr_type);
+               map->mem = mem;
+               mem->map = map;
+               ret = kobject_add(&map->kobj, idev->map_dir, "map%d", mi);
+               if (ret)
+                       goto err_map;
+               ret = kobject_uevent(&map->kobj, KOBJ_ADD);
+               if (ret)
+                       goto err_map;
+       }
+
+       for (pi = 0; pi < MAX_UIO_PORT_REGIONS; pi++) {
+               port = &idev->info->port[pi];
+               if (port->size == 0)
+                       break;
+               if (!portio_found) {
+                       portio_found = 1;
+                       idev->portio_dir = kobject_create_and_add("portio",
+                                                       &idev->dev->kobj);
+                       if (!idev->portio_dir)
+                               goto err_portio;
                }
-               kobject_init(&mem->kobj);
-               kobject_set_name(&mem->kobj,"map%d",mi);
-               mem->kobj.parent = &idev->map_attr_kset.kobj;
-               mem->kobj.kset = &idev->map_attr_kset;
-               ret = kobject_add(&mem->kobj);
+               portio = kzalloc(sizeof(*portio), GFP_KERNEL);
+               if (!portio)
+                       goto err_portio;
+               kobject_init(&portio->kobj, &portio_attr_type);
+               portio->port = port;
+               port->portio = portio;
+               ret = kobject_add(&portio->kobj, idev->portio_dir,
+                                                       "port%d", pi);
+               if (ret)
+                       goto err_portio;
+               ret = kobject_uevent(&portio->kobj, KOBJ_ADD);
                if (ret)
-                       goto err_remove_maps;
+                       goto err_portio;
        }
 
        return 0;
 
-err_remove_maps:
+err_portio:
+       for (pi--; pi >= 0; pi--) {
+               port = &idev->info->port[pi];
+               portio = port->portio;
+               kobject_put(&portio->kobj);
+       }
+       kobject_put(idev->portio_dir);
+err_map:
        for (mi--; mi>=0; mi--) {
                mem = &idev->info->mem[mi];
-               kobject_unregister(&mem->kobj);
+               map = mem->map;
+               kobject_put(&map->kobj);
        }
-       kset_unregister(&idev->map_attr_kset); /* Needed ? */
-err_remove_group:
-       sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
-err_group:
+       kobject_put(idev->map_dir);
        dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
        return ret;
 }
 
 static void uio_dev_del_attributes(struct uio_device *idev)
 {
-       int mi;
+       int i;
        struct uio_mem *mem;
-       for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
-               mem = &idev->info->mem[mi];
+       struct uio_port *port;
+
+       for (i = 0; i < MAX_UIO_MAPS; i++) {
+               mem = &idev->info->mem[i];
                if (mem->size == 0)
                        break;
-               kobject_unregister(&mem->kobj);
+               kobject_put(&mem->map->kobj);
        }
-       kset_unregister(&idev->map_attr_kset);
-       sysfs_remove_group(&idev->dev->kobj, &uio_attr_grp);
+       kobject_put(idev->map_dir);
+
+       for (i = 0; i < MAX_UIO_PORT_REGIONS; i++) {
+               port = &idev->info->port[i];
+               if (port->size == 0)
+                       break;
+               kobject_put(&port->portio->kobj);
+       }
+       kobject_put(idev->portio_dir);
 }
 
 static int uio_get_minor(struct uio_device *idev)
 {
-       static DEFINE_MUTEX(minor_lock);
        int retval = -ENOMEM;
        int id;
 
@@ -220,7 +381,13 @@ static int uio_get_minor(struct uio_device *idev)
                        retval = -ENOMEM;
                goto exit;
        }
-       idev->minor = id & MAX_ID_MASK;
+       if (id < UIO_MAX_DEVICES) {
+               idev->minor = id;
+       } else {
+               dev_err(idev->dev, "too many uio devices\n");
+               retval = -EINVAL;
+               idr_remove(&uio_idr, id);
+       }
 exit:
        mutex_unlock(&minor_lock);
        return retval;
@@ -228,7 +395,9 @@ exit:
 
 static void uio_free_minor(struct uio_device *idev)
 {
+       mutex_lock(&minor_lock);
        idr_remove(&uio_idr, idev->minor);
+       mutex_unlock(&minor_lock);
 }
 
 /**
@@ -272,28 +441,43 @@ static int uio_open(struct inode *inode, struct file *filep)
        struct uio_listener *listener;
        int ret = 0;
 
+       mutex_lock(&minor_lock);
        idev = idr_find(&uio_idr, iminor(inode));
-       if (!idev)
-               return -ENODEV;
+       mutex_unlock(&minor_lock);
+       if (!idev) {
+               ret = -ENODEV;
+               goto out;
+       }
+
+       if (!try_module_get(idev->owner)) {
+               ret = -ENODEV;
+               goto out;
+       }
 
        listener = kmalloc(sizeof(*listener), GFP_KERNEL);
-       if (!listener)
-               return -ENOMEM;
+       if (!listener) {
+               ret = -ENOMEM;
+               goto err_alloc_listener;
+       }
 
        listener->dev = idev;
        listener->event_count = atomic_read(&idev->event);
        filep->private_data = listener;
 
        if (idev->info->open) {
-               if (!try_module_get(idev->owner))
-                       return -ENODEV;
                ret = idev->info->open(idev->info, inode);
-               module_put(idev->owner);
+               if (ret)
+                       goto err_infoopen;
        }
+       return 0;
 
-       if (ret)
-               kfree(listener);
+err_infoopen:
+       kfree(listener);
 
+err_alloc_listener:
+       module_put(idev->owner);
+
+out:
        return ret;
 }
 
@@ -311,14 +495,10 @@ static int uio_release(struct inode *inode, struct file *filep)
        struct uio_listener *listener = filep->private_data;
        struct uio_device *idev = listener->dev;
 
-       if (idev->info->release) {
-               if (!try_module_get(idev->owner))
-                       return -ENODEV;
+       if (idev->info->release)
                ret = idev->info->release(idev->info, inode);
-               module_put(idev->owner);
-       }
-       if (filep->f_flags & FASYNC)
-               ret = uio_fasync(-1, filep, 0);
+
+       module_put(idev->owner);
        kfree(listener);
        return ret;
 }
@@ -328,7 +508,7 @@ static unsigned int uio_poll(struct file *filep, poll_table *wait)
        struct uio_listener *listener = filep->private_data;
        struct uio_device *idev = listener->dev;
 
-       if (idev->info->irq == UIO_IRQ_NONE)
+       if (!idev->info->irq)
                return -EIO;
 
        poll_wait(filep, &idev->wait, wait);
@@ -346,7 +526,7 @@ static ssize_t uio_read(struct file *filep, char __user *buf,
        ssize_t retval;
        s32 event_count;
 
-       if (idev->info->irq == UIO_IRQ_NONE)
+       if (!idev->info->irq)
                return -EIO;
 
        if (count != sizeof(s32))
@@ -386,16 +566,39 @@ static ssize_t uio_read(struct file *filep, char __user *buf,
        return retval;
 }
 
+static ssize_t uio_write(struct file *filep, const char __user *buf,
+                       size_t count, loff_t *ppos)
+{
+       struct uio_listener *listener = filep->private_data;
+       struct uio_device *idev = listener->dev;
+       ssize_t retval;
+       s32 irq_on;
+
+       if (!idev->info->irq)
+               return -EIO;
+
+       if (count != sizeof(s32))
+               return -EINVAL;
+
+       if (!idev->info->irqcontrol)
+               return -ENOSYS;
+
+       if (copy_from_user(&irq_on, buf, count))
+               return -EFAULT;
+
+       retval = idev->info->irqcontrol(idev->info, irq_on);
+
+       return retval ? retval : sizeof(s32);
+}
+
 static int uio_find_mem_index(struct vm_area_struct *vma)
 {
-       int mi;
        struct uio_device *idev = vma->vm_private_data;
 
-       for (mi = 0; mi < MAX_UIO_MAPS; mi++) {
-               if (idev->info->mem[mi].size == 0)
+       if (vma->vm_pgoff < MAX_UIO_MAPS) {
+               if (idev->info->mem[vma->vm_pgoff].size == 0)
                        return -1;
-               if (vma->vm_pgoff == mi)
-                       return mi;
+               return (int)vma->vm_pgoff;
        }
        return -1;
 }
@@ -412,30 +615,36 @@ static void uio_vma_close(struct vm_area_struct *vma)
        idev->vma_count--;
 }
 
-static struct page *uio_vma_nopage(struct vm_area_struct *vma,
-                                  unsigned long address, int *type)
+static int uio_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
 {
        struct uio_device *idev = vma->vm_private_data;
-       struct page* page = NOPAGE_SIGBUS;
+       struct page *page;
+       unsigned long offset;
 
        int mi = uio_find_mem_index(vma);
        if (mi < 0)
-               return page;
+               return VM_FAULT_SIGBUS;
+
+       /*
+        * We need to subtract mi because userspace uses offset = N*PAGE_SIZE
+        * to use mem[N].
+        */
+       offset = (vmf->pgoff - mi) << PAGE_SHIFT;
 
        if (idev->info->mem[mi].memtype == UIO_MEM_LOGICAL)
-               page = virt_to_page(idev->info->mem[mi].addr);
+               page = virt_to_page(idev->info->mem[mi].addr + offset);
        else
-               page = vmalloc_to_page((void*)idev->info->mem[mi].addr);
+               page = vmalloc_to_page((void *)idev->info->mem[mi].addr
+                                                       + offset);
        get_page(page);
-       if (type)
-               *type = VM_FAULT_MINOR;
-       return page;
+       vmf->page = page;
+       return 0;
 }
 
-static struct vm_operations_struct uio_vm_ops = {
+static const struct vm_operations_struct uio_vm_ops = {
        .open = uio_vma_open,
        .close = uio_vma_close,
-       .nopage = uio_vma_nopage,
+       .fault = uio_vma_fault,
 };
 
 static int uio_mmap_physical(struct vm_area_struct *vma)
@@ -447,6 +656,8 @@ static int uio_mmap_physical(struct vm_area_struct *vma)
 
        vma->vm_flags |= VM_IO | VM_RESERVED;
 
+       vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
        return remap_pfn_range(vma,
                               vma->vm_start,
                               idev->info->mem[mi].addr >> PAGE_SHIFT,
@@ -480,15 +691,13 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
                return -EINVAL;
 
        requested_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
-       actual_pages = (idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
+       actual_pages = ((idev->info->mem[mi].addr & ~PAGE_MASK)
+                       + idev->info->mem[mi].size + PAGE_SIZE -1) >> PAGE_SHIFT;
        if (requested_pages > actual_pages)
                return -EINVAL;
 
        if (idev->info->mmap) {
-               if (!try_module_get(idev->owner))
-                       return -ENODEV;
                ret = idev->info->mmap(idev->info, vma);
-               module_put(idev->owner);
                return ret;
        }
 
@@ -503,80 +712,86 @@ static int uio_mmap(struct file *filep, struct vm_area_struct *vma)
        }
 }
 
-static struct file_operations uio_fops = {
+static const struct file_operations uio_fops = {
        .owner          = THIS_MODULE,
        .open           = uio_open,
        .release        = uio_release,
        .read           = uio_read,
+       .write          = uio_write,
        .mmap           = uio_mmap,
        .poll           = uio_poll,
        .fasync         = uio_fasync,
+       .llseek         = noop_llseek,
 };
 
 static int uio_major_init(void)
 {
-       uio_major = register_chrdev(0, "uio", &uio_fops);
-       if (uio_major < 0)
-               return uio_major;
-       return 0;
+       static const char name[] = "uio";
+       struct cdev *cdev = NULL;
+       dev_t uio_dev = 0;
+       int result;
+
+       result = alloc_chrdev_region(&uio_dev, 0, UIO_MAX_DEVICES, name);
+       if (result)
+               goto out;
+
+       result = -ENOMEM;
+       cdev = cdev_alloc();
+       if (!cdev)
+               goto out_unregister;
+
+       cdev->owner = THIS_MODULE;
+       cdev->ops = &uio_fops;
+       kobject_set_name(&cdev->kobj, "%s", name);
+
+       result = cdev_add(cdev, uio_dev, UIO_MAX_DEVICES);
+       if (result)
+               goto out_put;
+
+       uio_major = MAJOR(uio_dev);
+       uio_cdev = cdev;
+       result = 0;
+out:
+       return result;
+out_put:
+       kobject_put(&cdev->kobj);
+out_unregister:
+       unregister_chrdev_region(uio_dev, UIO_MAX_DEVICES);
+       goto out;
 }
 
 static void uio_major_cleanup(void)
 {
-       unregister_chrdev(uio_major, "uio");
+       unregister_chrdev_region(MKDEV(uio_major, 0), UIO_MAX_DEVICES);
+       cdev_del(uio_cdev);
 }
 
 static int init_uio_class(void)
 {
-       int ret = 0;
-
-       if (uio_class != NULL) {
-               kref_get(&uio_class->kref);
-               goto exit;
-       }
+       int ret;
 
        /* This is the first time in here, set everything up properly */
        ret = uio_major_init();
        if (ret)
                goto exit;
 
-       uio_class = kzalloc(sizeof(*uio_class), GFP_KERNEL);
-       if (!uio_class) {
-               ret = -ENOMEM;
-               goto err_kzalloc;
-       }
-
-       kref_init(&uio_class->kref);
-       uio_class->class = class_create(THIS_MODULE, "uio");
-       if (IS_ERR(uio_class->class)) {
-               ret = IS_ERR(uio_class->class);
-               printk(KERN_ERR "class_create failed for uio\n");
-               goto err_class_create;
+       ret = class_register(&uio_class);
+       if (ret) {
+               printk(KERN_ERR "class_register failed for uio\n");
+               goto err_class_register;
        }
        return 0;
 
-err_class_create:
-       kfree(uio_class);
-       uio_class = NULL;
-err_kzalloc:
+err_class_register:
        uio_major_cleanup();
 exit:
        return ret;
 }
 
-static void release_uio_class(struct kref *kref)
+static void release_uio_class(void)
 {
-       /* Ok, we cheat as we know we only have one uio_class */
-       class_destroy(uio_class->class);
-       kfree(uio_class);
+       class_unregister(&uio_class);
        uio_major_cleanup();
-       uio_class = NULL;
-}
-
-static void uio_class_destroy(void)
-{
-       if (uio_class)
-               kref_put(&uio_class->kref, release_uio_class);
 }
 
 /**
@@ -599,10 +814,6 @@ int __uio_register_device(struct module *owner,
 
        info->uio_dev = NULL;
 
-       ret = init_uio_class();
-       if (ret)
-               return ret;
-
        idev = kzalloc(sizeof(*idev), GFP_KERNEL);
        if (!idev) {
                ret = -ENOMEM;
@@ -618,15 +829,14 @@ int __uio_register_device(struct module *owner,
        if (ret)
                goto err_get_minor;
 
-       idev->dev = device_create(uio_class->class, parent,
-                                 MKDEV(uio_major, idev->minor),
+       idev->dev = device_create(&uio_class, parent,
+                                 MKDEV(uio_major, idev->minor), idev,
                                  "uio%d", idev->minor);
        if (IS_ERR(idev->dev)) {
                printk(KERN_ERR "UIO: device register failed\n");
                ret = PTR_ERR(idev->dev);
                goto err_device_create;
        }
-       dev_set_drvdata(idev->dev, idev);
 
        ret = uio_dev_add_attributes(idev);
        if (ret)
@@ -634,9 +844,9 @@ int __uio_register_device(struct module *owner,
 
        info->uio_dev = idev;
 
-       if (idev->info->irq >= 0) {
-               ret = request_irq(idev->info->irq, uio_interrupt,
-                                 idev->info->irq_flags, idev->info->name, idev);
+       if (info->irq && (info->irq != UIO_IRQ_CUSTOM)) {
+               ret = request_irq(info->irq, uio_interrupt,
+                                 info->irq_flags, info->name, idev);
                if (ret)
                        goto err_request_irq;
        }
@@ -646,13 +856,12 @@ int __uio_register_device(struct module *owner,
 err_request_irq:
        uio_dev_del_attributes(idev);
 err_uio_dev_add_attributes:
-       device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
+       device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
 err_device_create:
        uio_free_minor(idev);
 err_get_minor:
        kfree(idev);
 err_kzalloc:
-       uio_class_destroy();
        return ret;
 }
 EXPORT_SYMBOL_GPL(__uio_register_device);
@@ -673,15 +882,13 @@ void uio_unregister_device(struct uio_info *info)
 
        uio_free_minor(idev);
 
-       if (info->irq >= 0)
+       if (info->irq && (info->irq != UIO_IRQ_CUSTOM))
                free_irq(info->irq, idev);
 
        uio_dev_del_attributes(idev);
 
-       dev_set_drvdata(idev->dev, NULL);
-       device_destroy(uio_class->class, MKDEV(uio_major, idev->minor));
+       device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
        kfree(idev);
-       uio_class_destroy();
 
        return;
 }
@@ -689,11 +896,12 @@ EXPORT_SYMBOL_GPL(uio_unregister_device);
 
 static int __init uio_init(void)
 {
-       return 0;
+       return init_uio_class();
 }
 
 static void __exit uio_exit(void)
 {
+       release_uio_class();
 }
 
 module_init(uio_init)