+/*
+ * 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,
+};
+