fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / input / serio / serio.c
index 6f00319..f0ce822 100644 (file)
@@ -1,11 +1,9 @@
-/*
- * $Id: serio.c,v 1.15 2002/01/22 21:12:03 vojtech Exp $
- *
- *  Copyright (c) 1999-2001 Vojtech Pavlik
- */
-
 /*
  *  The Serio abstraction module
+ *
+ *  Copyright (c) 1999-2004 Vojtech Pavlik
+ *  Copyright (c) 2004 Dmitry Torokhov
+ *  Copyright (c) 2003 Daniele Bellucci
  */
 
 /*
  * Should you need to contact me, the author, you can do so either by
  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
- *
- * Changes:
- * 20 Jul. 2003    Daniele Bellucci <bellucda@tiscali.it>
- *                 Minor cleanups.
  */
 
 #include <linux/stddef.h>
 #include <linux/serio.h>
 #include <linux/errno.h>
 #include <linux/wait.h>
-#include <linux/completion.h>
 #include <linux/sched.h>
-#include <linux/smp_lock.h>
-#include <linux/suspend.h>
 #include <linux/slab.h>
+#include <linux/kthread.h>
+#include <linux/mutex.h>
+#include <linux/freezer.h>
 
 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 MODULE_DESCRIPTION("Serio abstraction core");
 MODULE_LICENSE("GPL");
 
 EXPORT_SYMBOL(serio_interrupt);
-EXPORT_SYMBOL(serio_register_port);
-EXPORT_SYMBOL(serio_register_port_delayed);
 EXPORT_SYMBOL(__serio_register_port);
 EXPORT_SYMBOL(serio_unregister_port);
-EXPORT_SYMBOL(serio_unregister_port_delayed);
-EXPORT_SYMBOL(__serio_unregister_port);
-EXPORT_SYMBOL(serio_register_device);
-EXPORT_SYMBOL(serio_unregister_device);
+EXPORT_SYMBOL(serio_unregister_child_port);
+EXPORT_SYMBOL(serio_register_driver);
+EXPORT_SYMBOL(serio_unregister_driver);
 EXPORT_SYMBOL(serio_open);
 EXPORT_SYMBOL(serio_close);
 EXPORT_SYMBOL(serio_rescan);
 EXPORT_SYMBOL(serio_reconnect);
 
+/*
+ * serio_mutex protects entire serio subsystem and is taken every time
+ * serio port or driver registrered or unregistered.
+ */
+static DEFINE_MUTEX(serio_mutex);
+
+static LIST_HEAD(serio_list);
+
+static struct bus_type serio_bus;
+
+static void serio_add_port(struct serio *serio);
+static void serio_reconnect_port(struct serio *serio);
+static void serio_disconnect_port(struct serio *serio);
+static void serio_attach_driver(struct serio_driver *drv);
+
+static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
+{
+       int retval;
+
+       mutex_lock(&serio->drv_mutex);
+       retval = drv->connect(serio, drv);
+       mutex_unlock(&serio->drv_mutex);
+
+       return retval;
+}
+
+static int serio_reconnect_driver(struct serio *serio)
+{
+       int retval = -1;
+
+       mutex_lock(&serio->drv_mutex);
+       if (serio->drv && serio->drv->reconnect)
+               retval = serio->drv->reconnect(serio);
+       mutex_unlock(&serio->drv_mutex);
+
+       return retval;
+}
+
+static void serio_disconnect_driver(struct serio *serio)
+{
+       mutex_lock(&serio->drv_mutex);
+       if (serio->drv)
+               serio->drv->disconnect(serio);
+       mutex_unlock(&serio->drv_mutex);
+}
+
+static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
+{
+       while (ids->type || ids->proto) {
+               if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
+                   (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
+                   (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
+                   (ids->id == SERIO_ANY || ids->id == serio->id.id))
+                       return 1;
+               ids++;
+       }
+       return 0;
+}
+
+/*
+ * Basic serio -> driver core mappings
+ */
+
+static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
+{
+       int error;
+
+       down_write(&serio_bus.subsys.rwsem);
+
+       if (serio_match_port(drv->id_table, serio)) {
+               serio->dev.driver = &drv->driver;
+               if (serio_connect_driver(serio, drv)) {
+                       serio->dev.driver = NULL;
+                       goto out;
+               }
+               error = device_bind_driver(&serio->dev);
+               if (error) {
+                       printk(KERN_WARNING
+                               "serio: device_bind_driver() failed "
+                               "for %s (%s) and %s, error: %d\n",
+                               serio->phys, serio->name,
+                               drv->description, error);
+                       serio_disconnect_driver(serio);
+                       serio->dev.driver = NULL;
+                       goto out;
+               }
+       }
+ out:
+       up_write(&serio_bus.subsys.rwsem);
+}
+
+static void serio_release_driver(struct serio *serio)
+{
+       down_write(&serio_bus.subsys.rwsem);
+       device_release_driver(&serio->dev);
+       up_write(&serio_bus.subsys.rwsem);
+}
+
+static void serio_find_driver(struct serio *serio)
+{
+       int error;
+
+       down_write(&serio_bus.subsys.rwsem);
+       error = device_attach(&serio->dev);
+       if (error < 0)
+               printk(KERN_WARNING
+                       "serio: device_attach() failed for %s (%s), error: %d\n",
+                       serio->phys, serio->name, error);
+       up_write(&serio_bus.subsys.rwsem);
+}
+
+
+/*
+ * Serio event processing.
+ */
+
+enum serio_event_type {
+       SERIO_RESCAN_PORT,
+       SERIO_RECONNECT_PORT,
+       SERIO_REGISTER_PORT,
+       SERIO_ATTACH_DRIVER,
+};
+
 struct serio_event {
-       int type;
-       struct serio *serio;
+       enum serio_event_type type;
+       void *object;
+       struct module *owner;
        struct list_head node;
 };
 
-static DECLARE_MUTEX(serio_sem);
-static LIST_HEAD(serio_list);
-static LIST_HEAD(serio_dev_list);
+static DEFINE_SPINLOCK(serio_event_lock);      /* protects serio_event_list */
 static LIST_HEAD(serio_event_list);
-static int serio_pid;
+static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
+static struct task_struct *serio_task;
 
-static void serio_find_dev(struct serio *serio)
+static int serio_queue_event(void *object, struct module *owner,
+                            enum serio_event_type event_type)
 {
-       struct serio_dev *dev;
-
-       list_for_each_entry(dev, &serio_dev_list, node) {
-               if (serio->dev)
+       unsigned long flags;
+       struct serio_event *event;
+       int retval = 0;
+
+       spin_lock_irqsave(&serio_event_lock, flags);
+
+       /*
+        * Scan event list for the other events for the same serio port,
+        * starting with the most recent one. If event is the same we
+        * do not need add new one. If event is of different type we
+        * need to add this event and should not look further because
+        * we need to preseve sequence of distinct events.
+        */
+       list_for_each_entry_reverse(event, &serio_event_list, node) {
+               if (event->object == object) {
+                       if (event->type == event_type)
+                               goto out;
                        break;
-               if (dev->connect)
-                       dev->connect(serio, dev);
+               }
+       }
+
+       event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
+       if (!event) {
+               printk(KERN_ERR
+                       "serio: Not enough memory to queue event %d\n",
+                       event_type);
+               retval = -ENOMEM;
+               goto out;
+       }
+
+       if (!try_module_get(owner)) {
+               printk(KERN_WARNING
+                       "serio: Can't get module reference, dropping event %d\n",
+                       event_type);
+               kfree(event);
+               retval = -EINVAL;
+               goto out;
        }
+
+       event->type = event_type;
+       event->object = object;
+       event->owner = owner;
+
+       list_add_tail(&event->node, &serio_event_list);
+       wake_up(&serio_wait);
+
+out:
+       spin_unlock_irqrestore(&serio_event_lock, flags);
+       return retval;
 }
 
-#define SERIO_RESCAN           1
-#define SERIO_RECONNECT                2
-#define SERIO_REGISTER_PORT    3
-#define SERIO_UNREGISTER_PORT  4
+static void serio_free_event(struct serio_event *event)
+{
+       module_put(event->owner);
+       kfree(event);
+}
 
-static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
-static DECLARE_COMPLETION(serio_exited);
+static void serio_remove_duplicate_events(struct serio_event *event)
+{
+       struct list_head *node, *next;
+       struct serio_event *e;
+       unsigned long flags;
+
+       spin_lock_irqsave(&serio_event_lock, flags);
 
-static void serio_invalidate_pending_events(struct serio *serio)
+       list_for_each_safe(node, next, &serio_event_list) {
+               e = list_entry(node, struct serio_event, node);
+               if (event->object == e->object) {
+                       /*
+                        * If this event is of different type we should not
+                        * look further - we only suppress duplicate events
+                        * that were sent back-to-back.
+                        */
+                       if (event->type != e->type)
+                               break;
+
+                       list_del_init(node);
+                       serio_free_event(e);
+               }
+       }
+
+       spin_unlock_irqrestore(&serio_event_lock, flags);
+}
+
+
+static struct serio_event *serio_get_event(void)
 {
        struct serio_event *event;
+       struct list_head *node;
+       unsigned long flags;
+
+       spin_lock_irqsave(&serio_event_lock, flags);
 
-       list_for_each_entry(event, &serio_event_list, node)
-               if (event->serio == serio)
-                       event->serio = NULL;
+       if (list_empty(&serio_event_list)) {
+               spin_unlock_irqrestore(&serio_event_lock, flags);
+               return NULL;
+       }
+
+       node = serio_event_list.next;
+       event = list_entry(node, struct serio_event, node);
+       list_del_init(node);
+
+       spin_unlock_irqrestore(&serio_event_lock, flags);
+
+       return event;
 }
 
-void serio_handle_events(void)
+static void serio_handle_event(void)
 {
-       struct list_head *node, *next;
        struct serio_event *event;
 
-       list_for_each_safe(node, next, &serio_event_list) {
-               event = container_of(node, struct serio_event, node);
+       mutex_lock(&serio_mutex);
 
-               down(&serio_sem);
-               if (event->serio == NULL)
-                       goto event_done;
+       /*
+        * Note that we handle only one event here to give swsusp
+        * a chance to freeze kseriod thread. Serio events should
+        * be pretty rare so we are not concerned about taking
+        * performance hit.
+        */
+       if ((event = serio_get_event())) {
 
                switch (event->type) {
-                       case SERIO_REGISTER_PORT :
-                               __serio_register_port(event->serio);
+                       case SERIO_REGISTER_PORT:
+                               serio_add_port(event->object);
                                break;
 
-                       case SERIO_UNREGISTER_PORT :
-                               __serio_unregister_port(event->serio);
+                       case SERIO_RECONNECT_PORT:
+                               serio_reconnect_port(event->object);
                                break;
 
-                       case SERIO_RECONNECT :
-                               if (event->serio->dev && event->serio->dev->reconnect)
-                                       if (event->serio->dev->reconnect(event->serio) == 0)
-                                               break;
-                               /* reconnect failed - fall through to rescan */
+                       case SERIO_RESCAN_PORT:
+                               serio_disconnect_port(event->object);
+                               serio_find_driver(event->object);
+                               break;
 
-                       case SERIO_RESCAN :
-                               if (event->serio->dev && event->serio->dev->disconnect)
-                                       event->serio->dev->disconnect(event->serio);
-                               serio_find_dev(event->serio);
+                       case SERIO_ATTACH_DRIVER:
+                               serio_attach_driver(event->object);
                                break;
+
                        default:
                                break;
                }
-event_done:
-               up(&serio_sem);
-               list_del_init(node);
-               kfree(event);
+
+               serio_remove_duplicate_events(event);
+               serio_free_event(event);
        }
+
+       mutex_unlock(&serio_mutex);
 }
 
-static int serio_thread(void *nothing)
+/*
+ * Remove all events that have been submitted for a given serio port.
+ */
+static void serio_remove_pending_events(struct serio *serio)
 {
-       lock_kernel();
-       daemonize("kseriod");
-       allow_signal(SIGTERM);
+       struct list_head *node, *next;
+       struct serio_event *event;
+       unsigned long flags;
 
-       do {
-               serio_handle_events();
-               wait_event_interruptible(serio_wait, !list_empty(&serio_event_list));
-               if (current->flags & PF_FREEZE)
-                       refrigerator(PF_FREEZE);
-       } while (!signal_pending(current));
+       spin_lock_irqsave(&serio_event_lock, flags);
 
-       printk(KERN_DEBUG "serio: kseriod exiting\n");
+       list_for_each_safe(node, next, &serio_event_list) {
+               event = list_entry(node, struct serio_event, node);
+               if (event->object == serio) {
+                       list_del_init(node);
+                       serio_free_event(event);
+               }
+       }
 
-       unlock_kernel();
-       complete_and_exit(&serio_exited, 0);
+       spin_unlock_irqrestore(&serio_event_lock, flags);
 }
 
-static void serio_queue_event(struct serio *serio, int event_type)
+/*
+ * Destroy child serio port (if any) that has not been fully registered yet.
+ *
+ * Note that we rely on the fact that port can have only one child and therefore
+ * only one child registration request can be pending. Additionally, children
+ * are registered by driver's connect() handler so there can't be a grandchild
+ * pending registration together with a child.
+ */
+static struct serio *serio_get_pending_child(struct serio *parent)
 {
        struct serio_event *event;
+       struct serio *serio, *child = NULL;
+       unsigned long flags;
 
-       if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
-               event->type = event_type;
-               event->serio = serio;
+       spin_lock_irqsave(&serio_event_lock, flags);
 
-               list_add_tail(&event->node, &serio_event_list);
-               wake_up(&serio_wait);
+       list_for_each_entry(event, &serio_event_list, node) {
+               if (event->type == SERIO_REGISTER_PORT) {
+                       serio = event->object;
+                       if (serio->parent == parent) {
+                               child = serio;
+                               break;
+                       }
+               }
        }
+
+       spin_unlock_irqrestore(&serio_event_lock, flags);
+       return child;
 }
 
-void serio_rescan(struct serio *serio)
+static int serio_thread(void *nothing)
 {
-       serio_queue_event(serio, SERIO_RESCAN);
+       do {
+               serio_handle_event();
+               wait_event_interruptible(serio_wait,
+                       kthread_should_stop() || !list_empty(&serio_event_list));
+               try_to_freeze();
+       } while (!kthread_should_stop());
+
+       printk(KERN_DEBUG "serio: kseriod exiting\n");
+       return 0;
 }
 
-void serio_reconnect(struct serio *serio)
+
+/*
+ * Serio port operations
+ */
+
+static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
 {
-       serio_queue_event(serio, SERIO_RECONNECT);
+       struct serio *serio = to_serio_port(dev);
+       return sprintf(buf, "%s\n", serio->name);
 }
 
-irqreturn_t serio_interrupt(struct serio *serio,
-               unsigned char data, unsigned int flags, struct pt_regs *regs)
+static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
 {
-       irqreturn_t ret = IRQ_NONE;
+       struct serio *serio = to_serio_port(dev);
+
+       return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
+                       serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
+}
 
-        if (serio->dev && serio->dev->interrupt) {
-                ret = serio->dev->interrupt(serio, data, flags, regs);
+static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct serio *serio = to_serio_port(dev);
+       return sprintf(buf, "%02x\n", serio->id.type);
+}
+
+static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct serio *serio = to_serio_port(dev);
+       return sprintf(buf, "%02x\n", serio->id.proto);
+}
+
+static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct serio *serio = to_serio_port(dev);
+       return sprintf(buf, "%02x\n", serio->id.id);
+}
+
+static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct serio *serio = to_serio_port(dev);
+       return sprintf(buf, "%02x\n", serio->id.extra);
+}
+
+static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
+static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
+static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
+static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
+
+static struct attribute *serio_device_id_attrs[] = {
+       &dev_attr_type.attr,
+       &dev_attr_proto.attr,
+       &dev_attr_id.attr,
+       &dev_attr_extra.attr,
+       NULL
+};
+
+static struct attribute_group serio_id_attr_group = {
+       .name   = "id",
+       .attrs  = serio_device_id_attrs,
+};
+
+static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       struct serio *serio = to_serio_port(dev);
+       struct device_driver *drv;
+       int retval;
+
+       retval = mutex_lock_interruptible(&serio_mutex);
+       if (retval)
+               return retval;
+
+       retval = count;
+       if (!strncmp(buf, "none", count)) {
+               serio_disconnect_port(serio);
+       } else if (!strncmp(buf, "reconnect", count)) {
+               serio_reconnect_port(serio);
+       } else if (!strncmp(buf, "rescan", count)) {
+               serio_disconnect_port(serio);
+               serio_find_driver(serio);
+       } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
+               serio_disconnect_port(serio);
+               serio_bind_driver(serio, to_serio_driver(drv));
+               put_driver(drv);
        } else {
-               if (!flags) {
-                       if ((serio->type == SERIO_8042 ||
-                               serio->type == SERIO_8042_XL) && (data != 0xaa))
-                                       return ret;
-                       serio_rescan(serio);
-                       ret = IRQ_HANDLED;
-               }
+               retval = -EINVAL;
        }
-       return ret;
+
+       mutex_unlock(&serio_mutex);
+
+       return retval;
+}
+
+static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       struct serio *serio = to_serio_port(dev);
+       return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
 }
 
-void serio_register_port(struct serio *serio)
+static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
 {
-       down(&serio_sem);
-       __serio_register_port(serio);
-       up(&serio_sem);
+       struct serio *serio = to_serio_port(dev);
+       int retval;
+
+       retval = count;
+       if (!strncmp(buf, "manual", count)) {
+               serio->manual_bind = 1;
+       } else if (!strncmp(buf, "auto", count)) {
+               serio->manual_bind = 0;
+       } else {
+               retval = -EINVAL;
+       }
+
+       return retval;
+}
+
+static struct device_attribute serio_device_attrs[] = {
+       __ATTR(description, S_IRUGO, serio_show_description, NULL),
+       __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
+       __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
+       __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
+       __ATTR_NULL
+};
+
+
+static void serio_release_port(struct device *dev)
+{
+       struct serio *serio = to_serio_port(dev);
+
+       kfree(serio);
+       module_put(THIS_MODULE);
 }
 
 /*
- * Submits register request to kseriod for subsequent execution.
- * Can be used when it is not obvious whether the serio_sem is
- * taken or not and when delayed execution is feasible.
+ * Prepare serio port for registration.
  */
-void serio_register_port_delayed(struct serio *serio)
+static void serio_init_port(struct serio *serio)
 {
-       serio_queue_event(serio, SERIO_REGISTER_PORT);
+       static atomic_t serio_no = ATOMIC_INIT(0);
+
+       __module_get(THIS_MODULE);
+
+       INIT_LIST_HEAD(&serio->node);
+       spin_lock_init(&serio->lock);
+       mutex_init(&serio->drv_mutex);
+       device_initialize(&serio->dev);
+       snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
+                "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
+       serio->dev.bus = &serio_bus;
+       serio->dev.release = serio_release_port;
+       if (serio->parent) {
+               serio->dev.parent = &serio->parent->dev;
+               serio->depth = serio->parent->depth + 1;
+       } else
+               serio->depth = 0;
+       lockdep_set_subclass(&serio->lock, serio->depth);
 }
 
 /*
- * Should only be called directly if serio_sem has already been taken,
- * for example when unregistering a serio from other input device's
- * connect() function.
+ * Complete serio port registration.
+ * Driver core will attempt to find appropriate driver for the port.
  */
-void __serio_register_port(struct serio *serio)
+static void serio_add_port(struct serio *serio)
 {
+       int error;
+
+       if (serio->parent) {
+               serio_pause_rx(serio->parent);
+               serio->parent->child = serio;
+               serio_continue_rx(serio->parent);
+       }
+
        list_add_tail(&serio->node, &serio_list);
-       serio_find_dev(serio);
+       if (serio->start)
+               serio->start(serio);
+       error = device_add(&serio->dev);
+       if (error)
+               printk(KERN_ERR
+                       "serio: device_add() failed for %s (%s), error: %d\n",
+                       serio->phys, serio->name, error);
+       else {
+               serio->registered = 1;
+               error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
+               if (error)
+                       printk(KERN_ERR
+                               "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
+                               serio->phys, serio->name, error);
+       }
+}
+
+/*
+ * serio_destroy_port() completes deregistration process and removes
+ * port from the system
+ */
+static void serio_destroy_port(struct serio *serio)
+{
+       struct serio *child;
+
+       child = serio_get_pending_child(serio);
+       if (child) {
+               serio_remove_pending_events(child);
+               put_device(&child->dev);
+       }
+
+       if (serio->stop)
+               serio->stop(serio);
+
+       if (serio->parent) {
+               serio_pause_rx(serio->parent);
+               serio->parent->child = NULL;
+               serio_continue_rx(serio->parent);
+               serio->parent = NULL;
+       }
+
+       if (serio->registered) {
+               sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
+               device_del(&serio->dev);
+               serio->registered = 0;
+       }
+
+       list_del_init(&serio->node);
+       serio_remove_pending_events(serio);
+       put_device(&serio->dev);
+}
+
+/*
+ * Reconnect serio port and all its children (re-initialize attached devices)
+ */
+static void serio_reconnect_port(struct serio *serio)
+{
+       do {
+               if (serio_reconnect_driver(serio)) {
+                       serio_disconnect_port(serio);
+                       serio_find_driver(serio);
+                       /* Ok, old children are now gone, we are done */
+                       break;
+               }
+               serio = serio->child;
+       } while (serio);
+}
+
+/*
+ * serio_disconnect_port() unbinds a port from its driver. As a side effect
+ * all child ports are unbound and destroyed.
+ */
+static void serio_disconnect_port(struct serio *serio)
+{
+       struct serio *s, *parent;
+
+       if (serio->child) {
+               /*
+                * Children ports should be disconnected and destroyed
+                * first, staring with the leaf one, since we don't want
+                * to do recursion
+                */
+               for (s = serio; s->child; s = s->child)
+                       /* empty */;
+
+               do {
+                       parent = s->parent;
+
+                       serio_release_driver(s);
+                       serio_destroy_port(s);
+               } while ((s = parent) != serio);
+       }
+
+       /*
+        * Ok, no children left, now disconnect this port
+        */
+       serio_release_driver(serio);
 }
 
+void serio_rescan(struct serio *serio)
+{
+       serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
+}
+
+void serio_reconnect(struct serio *serio)
+{
+       serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
+}
+
+/*
+ * Submits register request to kseriod for subsequent execution.
+ * Note that port registration is always asynchronous.
+ */
+void __serio_register_port(struct serio *serio, struct module *owner)
+{
+       serio_init_port(serio);
+       serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
+}
+
+/*
+ * Synchronously unregisters serio port.
+ */
 void serio_unregister_port(struct serio *serio)
 {
-       down(&serio_sem);
-       __serio_unregister_port(serio);
-       up(&serio_sem);
+       mutex_lock(&serio_mutex);
+       serio_disconnect_port(serio);
+       serio_destroy_port(serio);
+       mutex_unlock(&serio_mutex);
 }
 
 /*
- * Submits unregister request to kseriod for subsequent execution.
- * Can be used when it is not obvious whether the serio_sem is
- * taken or not and when delayed execution is feasible.
+ * Safely unregisters child port if one is present.
  */
-void serio_unregister_port_delayed(struct serio *serio)
+void serio_unregister_child_port(struct serio *serio)
 {
-       serio_queue_event(serio, SERIO_UNREGISTER_PORT);
+       mutex_lock(&serio_mutex);
+       if (serio->child) {
+               serio_disconnect_port(serio->child);
+               serio_destroy_port(serio->child);
+       }
+       mutex_unlock(&serio_mutex);
 }
 
+
 /*
- * Should only be called directly if serio_sem has already been taken,
- * for example when unregistering a serio from other input device's
- * disconnect() function.
+ * Serio driver operations
  */
-void __serio_unregister_port(struct serio *serio)
+
+static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
 {
-       serio_invalidate_pending_events(serio);
-       list_del_init(&serio->node);
-       if (serio->dev && serio->dev->disconnect)
-               serio->dev->disconnect(serio);
+       struct serio_driver *driver = to_serio_driver(drv);
+       return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
 }
 
-void serio_register_device(struct serio_dev *dev)
+static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
 {
-       struct serio *serio;
-       down(&serio_sem);
-       list_add_tail(&dev->node, &serio_dev_list);
-       list_for_each_entry(serio, &serio_list, node)
-               if (!serio->dev && dev->connect)
-                       dev->connect(serio, dev);
-       up(&serio_sem);
+       struct serio_driver *serio_drv = to_serio_driver(drv);
+       return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
+}
+
+static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
+{
+       struct serio_driver *serio_drv = to_serio_driver(drv);
+       int retval;
+
+       retval = count;
+       if (!strncmp(buf, "manual", count)) {
+               serio_drv->manual_bind = 1;
+       } else if (!strncmp(buf, "auto", count)) {
+               serio_drv->manual_bind = 0;
+       } else {
+               retval = -EINVAL;
+       }
+
+       return retval;
+}
+
+
+static struct driver_attribute serio_driver_attrs[] = {
+       __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
+       __ATTR(bind_mode, S_IWUSR | S_IRUGO,
+               serio_driver_show_bind_mode, serio_driver_set_bind_mode),
+       __ATTR_NULL
+};
+
+static int serio_driver_probe(struct device *dev)
+{
+       struct serio *serio = to_serio_port(dev);
+       struct serio_driver *drv = to_serio_driver(dev->driver);
+
+       return serio_connect_driver(serio, drv);
 }
 
-void serio_unregister_device(struct serio_dev *dev)
+static int serio_driver_remove(struct device *dev)
+{
+       struct serio *serio = to_serio_port(dev);
+
+       serio_disconnect_driver(serio);
+       return 0;
+}
+
+static void serio_attach_driver(struct serio_driver *drv)
+{
+       int error;
+
+       error = driver_attach(&drv->driver);
+       if (error)
+               printk(KERN_WARNING
+                       "serio: driver_attach() failed for %s with error %d\n",
+                       drv->driver.name, error);
+}
+
+int serio_register_driver(struct serio_driver *drv)
+{
+       int manual_bind = drv->manual_bind;
+       int error;
+
+       drv->driver.bus = &serio_bus;
+
+       /*
+        * Temporarily disable automatic binding because probing
+        * takes long time and we are better off doing it in kseriod
+        */
+       drv->manual_bind = 1;
+
+       error = driver_register(&drv->driver);
+       if (error) {
+               printk(KERN_ERR
+                       "serio: driver_register() failed for %s, error: %d\n",
+                       drv->driver.name, error);
+               return error;
+       }
+
+       /*
+        * Restore original bind mode and let kseriod bind the
+        * driver to free ports
+        */
+       if (!manual_bind) {
+               drv->manual_bind = 0;
+               error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
+               if (error) {
+                       driver_unregister(&drv->driver);
+                       return error;
+               }
+       }
+
+       return 0;
+}
+
+void serio_unregister_driver(struct serio_driver *drv)
 {
        struct serio *serio;
 
-       down(&serio_sem);
-       list_del_init(&dev->node);
+       mutex_lock(&serio_mutex);
+       drv->manual_bind = 1;   /* so serio_find_driver ignores it */
 
+start_over:
        list_for_each_entry(serio, &serio_list, node) {
-               if (serio->dev == dev && dev->disconnect)
-                       dev->disconnect(serio);
-               serio_find_dev(serio);
+               if (serio->drv == drv) {
+                       serio_disconnect_port(serio);
+                       serio_find_driver(serio);
+                       /* we could've deleted some ports, restart */
+                       goto start_over;
+               }
+       }
+
+       driver_unregister(&drv->driver);
+       mutex_unlock(&serio_mutex);
+}
+
+static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
+{
+       serio_pause_rx(serio);
+       serio->drv = drv;
+       serio_continue_rx(serio);
+}
+
+static int serio_bus_match(struct device *dev, struct device_driver *drv)
+{
+       struct serio *serio = to_serio_port(dev);
+       struct serio_driver *serio_drv = to_serio_driver(drv);
+
+       if (serio->manual_bind || serio_drv->manual_bind)
+               return 0;
+
+       return serio_match_port(serio_drv->id_table, serio);
+}
+
+#ifdef CONFIG_HOTPLUG
+
+#define SERIO_ADD_UEVENT_VAR(fmt, val...)                              \
+       do {                                                            \
+               int err = add_uevent_var(envp, num_envp, &i,    \
+                                       buffer, buffer_size, &len,      \
+                                       fmt, val);                      \
+               if (err)                                                \
+                       return err;                                     \
+       } while (0)
+
+static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
+{
+       struct serio *serio;
+       int i = 0;
+       int len = 0;
+
+       if (!dev)
+               return -ENODEV;
+
+       serio = to_serio_port(dev);
+
+       SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
+       SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
+       SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
+       SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
+       SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
+                               serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
+       envp[i] = NULL;
+
+       return 0;
+}
+#undef SERIO_ADD_UEVENT_VAR
+
+#else
+
+static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
+{
+       return -ENODEV;
+}
+
+#endif /* CONFIG_HOTPLUG */
+
+static int serio_resume(struct device *dev)
+{
+       struct serio *serio = to_serio_port(dev);
+
+       if (serio_reconnect_driver(serio)) {
+               /*
+                * Driver re-probing can take a while, so better let kseriod
+                * deal with it.
+                */
+               serio_rescan(serio);
        }
-       up(&serio_sem);
+
+       return 0;
 }
 
-/* called from serio_dev->connect/disconnect methods under serio_sem */
-int serio_open(struct serio *serio, struct serio_dev *dev)
+/* called from serio_driver->connect/disconnect methods under serio_mutex */
+int serio_open(struct serio *serio, struct serio_driver *drv)
 {
-       serio->dev = dev;
+       serio_set_drv(serio, drv);
+
        if (serio->open && serio->open(serio)) {
-               serio->dev = NULL;
+               serio_set_drv(serio, NULL);
                return -1;
        }
        return 0;
 }
 
-/* called from serio_dev->connect/disconnect methods under serio_sem */
+/* called from serio_driver->connect/disconnect methods under serio_mutex */
 void serio_close(struct serio *serio)
 {
        if (serio->close)
                serio->close(serio);
-       serio->dev = NULL;
+
+       serio_set_drv(serio, NULL);
 }
 
-static int __init serio_init(void)
+irqreturn_t serio_interrupt(struct serio *serio,
+               unsigned char data, unsigned int dfl)
 {
-       int pid;
+       unsigned long flags;
+       irqreturn_t ret = IRQ_NONE;
 
-       pid = kernel_thread(serio_thread, NULL, CLONE_KERNEL);
+       spin_lock_irqsave(&serio->lock, flags);
 
-       if (!pid) {
-               printk(KERN_WARNING "serio: Failed to start kseriod\n");
-               return -1;
+        if (likely(serio->drv)) {
+                ret = serio->drv->interrupt(serio, data, dfl);
+       } else if (!dfl && serio->registered) {
+               serio_rescan(serio);
+               ret = IRQ_HANDLED;
+       }
+
+       spin_unlock_irqrestore(&serio->lock, flags);
+
+       return ret;
+}
+
+static struct bus_type serio_bus = {
+       .name           = "serio",
+       .dev_attrs      = serio_device_attrs,
+       .drv_attrs      = serio_driver_attrs,
+       .match          = serio_bus_match,
+       .uevent         = serio_uevent,
+       .probe          = serio_driver_probe,
+       .remove         = serio_driver_remove,
+       .resume         = serio_resume,
+};
+
+static int __init serio_init(void)
+{
+       int error;
+
+       error = bus_register(&serio_bus);
+       if (error) {
+               printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
+               return error;
        }
 
-       serio_pid = pid;
+       serio_task = kthread_run(serio_thread, NULL, "kseriod");
+       if (IS_ERR(serio_task)) {
+               bus_unregister(&serio_bus);
+               error = PTR_ERR(serio_task);
+               printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
+               return error;
+       }
 
        return 0;
 }
 
 static void __exit serio_exit(void)
 {
-       kill_proc(serio_pid, SIGTERM, 1);
-       wait_for_completion(&serio_exited);
+       bus_unregister(&serio_bus);
+       kthread_stop(serio_task);
 }
 
-module_init(serio_init);
+subsys_initcall(serio_init);
 module_exit(serio_exit);