Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  The Serio abstraction module
0004  *
0005  *  Copyright (c) 1999-2004 Vojtech Pavlik
0006  *  Copyright (c) 2004 Dmitry Torokhov
0007  *  Copyright (c) 2003 Daniele Bellucci
0008  */
0009 
0010 /*
0011  */
0012 
0013 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0014 
0015 #include <linux/stddef.h>
0016 #include <linux/module.h>
0017 #include <linux/serio.h>
0018 #include <linux/errno.h>
0019 #include <linux/sched.h>
0020 #include <linux/slab.h>
0021 #include <linux/workqueue.h>
0022 #include <linux/mutex.h>
0023 
0024 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
0025 MODULE_DESCRIPTION("Serio abstraction core");
0026 MODULE_LICENSE("GPL");
0027 
0028 /*
0029  * serio_mutex protects entire serio subsystem and is taken every time
0030  * serio port or driver registered or unregistered.
0031  */
0032 static DEFINE_MUTEX(serio_mutex);
0033 
0034 static LIST_HEAD(serio_list);
0035 
0036 static void serio_add_port(struct serio *serio);
0037 static int serio_reconnect_port(struct serio *serio);
0038 static void serio_disconnect_port(struct serio *serio);
0039 static void serio_reconnect_subtree(struct serio *serio);
0040 static void serio_attach_driver(struct serio_driver *drv);
0041 
0042 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
0043 {
0044     int retval;
0045 
0046     mutex_lock(&serio->drv_mutex);
0047     retval = drv->connect(serio, drv);
0048     mutex_unlock(&serio->drv_mutex);
0049 
0050     return retval;
0051 }
0052 
0053 static int serio_reconnect_driver(struct serio *serio)
0054 {
0055     int retval = -1;
0056 
0057     mutex_lock(&serio->drv_mutex);
0058     if (serio->drv && serio->drv->reconnect)
0059         retval = serio->drv->reconnect(serio);
0060     mutex_unlock(&serio->drv_mutex);
0061 
0062     return retval;
0063 }
0064 
0065 static void serio_disconnect_driver(struct serio *serio)
0066 {
0067     mutex_lock(&serio->drv_mutex);
0068     if (serio->drv)
0069         serio->drv->disconnect(serio);
0070     mutex_unlock(&serio->drv_mutex);
0071 }
0072 
0073 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
0074 {
0075     while (ids->type || ids->proto) {
0076         if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
0077             (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
0078             (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
0079             (ids->id == SERIO_ANY || ids->id == serio->id.id))
0080             return 1;
0081         ids++;
0082     }
0083     return 0;
0084 }
0085 
0086 /*
0087  * Basic serio -> driver core mappings
0088  */
0089 
0090 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
0091 {
0092     int error;
0093 
0094     if (serio_match_port(drv->id_table, serio)) {
0095 
0096         serio->dev.driver = &drv->driver;
0097         if (serio_connect_driver(serio, drv)) {
0098             serio->dev.driver = NULL;
0099             return -ENODEV;
0100         }
0101 
0102         error = device_bind_driver(&serio->dev);
0103         if (error) {
0104             dev_warn(&serio->dev,
0105                  "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
0106                  serio->phys, serio->name,
0107                  drv->description, error);
0108             serio_disconnect_driver(serio);
0109             serio->dev.driver = NULL;
0110             return error;
0111         }
0112     }
0113     return 0;
0114 }
0115 
0116 static void serio_find_driver(struct serio *serio)
0117 {
0118     int error;
0119 
0120     error = device_attach(&serio->dev);
0121     if (error < 0 && error != -EPROBE_DEFER)
0122         dev_warn(&serio->dev,
0123              "device_attach() failed for %s (%s), error: %d\n",
0124              serio->phys, serio->name, error);
0125 }
0126 
0127 
0128 /*
0129  * Serio event processing.
0130  */
0131 
0132 enum serio_event_type {
0133     SERIO_RESCAN_PORT,
0134     SERIO_RECONNECT_PORT,
0135     SERIO_RECONNECT_SUBTREE,
0136     SERIO_REGISTER_PORT,
0137     SERIO_ATTACH_DRIVER,
0138 };
0139 
0140 struct serio_event {
0141     enum serio_event_type type;
0142     void *object;
0143     struct module *owner;
0144     struct list_head node;
0145 };
0146 
0147 static DEFINE_SPINLOCK(serio_event_lock);   /* protects serio_event_list */
0148 static LIST_HEAD(serio_event_list);
0149 
0150 static struct serio_event *serio_get_event(void)
0151 {
0152     struct serio_event *event = NULL;
0153     unsigned long flags;
0154 
0155     spin_lock_irqsave(&serio_event_lock, flags);
0156 
0157     if (!list_empty(&serio_event_list)) {
0158         event = list_first_entry(&serio_event_list,
0159                      struct serio_event, node);
0160         list_del_init(&event->node);
0161     }
0162 
0163     spin_unlock_irqrestore(&serio_event_lock, flags);
0164     return event;
0165 }
0166 
0167 static void serio_free_event(struct serio_event *event)
0168 {
0169     module_put(event->owner);
0170     kfree(event);
0171 }
0172 
0173 static void serio_remove_duplicate_events(void *object,
0174                       enum serio_event_type type)
0175 {
0176     struct serio_event *e, *next;
0177     unsigned long flags;
0178 
0179     spin_lock_irqsave(&serio_event_lock, flags);
0180 
0181     list_for_each_entry_safe(e, next, &serio_event_list, node) {
0182         if (object == e->object) {
0183             /*
0184              * If this event is of different type we should not
0185              * look further - we only suppress duplicate events
0186              * that were sent back-to-back.
0187              */
0188             if (type != e->type)
0189                 break;
0190 
0191             list_del_init(&e->node);
0192             serio_free_event(e);
0193         }
0194     }
0195 
0196     spin_unlock_irqrestore(&serio_event_lock, flags);
0197 }
0198 
0199 static void serio_handle_event(struct work_struct *work)
0200 {
0201     struct serio_event *event;
0202 
0203     mutex_lock(&serio_mutex);
0204 
0205     while ((event = serio_get_event())) {
0206 
0207         switch (event->type) {
0208 
0209         case SERIO_REGISTER_PORT:
0210             serio_add_port(event->object);
0211             break;
0212 
0213         case SERIO_RECONNECT_PORT:
0214             serio_reconnect_port(event->object);
0215             break;
0216 
0217         case SERIO_RESCAN_PORT:
0218             serio_disconnect_port(event->object);
0219             serio_find_driver(event->object);
0220             break;
0221 
0222         case SERIO_RECONNECT_SUBTREE:
0223             serio_reconnect_subtree(event->object);
0224             break;
0225 
0226         case SERIO_ATTACH_DRIVER:
0227             serio_attach_driver(event->object);
0228             break;
0229         }
0230 
0231         serio_remove_duplicate_events(event->object, event->type);
0232         serio_free_event(event);
0233     }
0234 
0235     mutex_unlock(&serio_mutex);
0236 }
0237 
0238 static DECLARE_WORK(serio_event_work, serio_handle_event);
0239 
0240 static int serio_queue_event(void *object, struct module *owner,
0241                  enum serio_event_type event_type)
0242 {
0243     unsigned long flags;
0244     struct serio_event *event;
0245     int retval = 0;
0246 
0247     spin_lock_irqsave(&serio_event_lock, flags);
0248 
0249     /*
0250      * Scan event list for the other events for the same serio port,
0251      * starting with the most recent one. If event is the same we
0252      * do not need add new one. If event is of different type we
0253      * need to add this event and should not look further because
0254      * we need to preseve sequence of distinct events.
0255      */
0256     list_for_each_entry_reverse(event, &serio_event_list, node) {
0257         if (event->object == object) {
0258             if (event->type == event_type)
0259                 goto out;
0260             break;
0261         }
0262     }
0263 
0264     event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
0265     if (!event) {
0266         pr_err("Not enough memory to queue event %d\n", event_type);
0267         retval = -ENOMEM;
0268         goto out;
0269     }
0270 
0271     if (!try_module_get(owner)) {
0272         pr_warn("Can't get module reference, dropping event %d\n",
0273             event_type);
0274         kfree(event);
0275         retval = -EINVAL;
0276         goto out;
0277     }
0278 
0279     event->type = event_type;
0280     event->object = object;
0281     event->owner = owner;
0282 
0283     list_add_tail(&event->node, &serio_event_list);
0284     queue_work(system_long_wq, &serio_event_work);
0285 
0286 out:
0287     spin_unlock_irqrestore(&serio_event_lock, flags);
0288     return retval;
0289 }
0290 
0291 /*
0292  * Remove all events that have been submitted for a given
0293  * object, be it serio port or driver.
0294  */
0295 static void serio_remove_pending_events(void *object)
0296 {
0297     struct serio_event *event, *next;
0298     unsigned long flags;
0299 
0300     spin_lock_irqsave(&serio_event_lock, flags);
0301 
0302     list_for_each_entry_safe(event, next, &serio_event_list, node) {
0303         if (event->object == object) {
0304             list_del_init(&event->node);
0305             serio_free_event(event);
0306         }
0307     }
0308 
0309     spin_unlock_irqrestore(&serio_event_lock, flags);
0310 }
0311 
0312 /*
0313  * Locate child serio port (if any) that has not been fully registered yet.
0314  *
0315  * Children are registered by driver's connect() handler so there can't be a
0316  * grandchild pending registration together with a child.
0317  */
0318 static struct serio *serio_get_pending_child(struct serio *parent)
0319 {
0320     struct serio_event *event;
0321     struct serio *serio, *child = NULL;
0322     unsigned long flags;
0323 
0324     spin_lock_irqsave(&serio_event_lock, flags);
0325 
0326     list_for_each_entry(event, &serio_event_list, node) {
0327         if (event->type == SERIO_REGISTER_PORT) {
0328             serio = event->object;
0329             if (serio->parent == parent) {
0330                 child = serio;
0331                 break;
0332             }
0333         }
0334     }
0335 
0336     spin_unlock_irqrestore(&serio_event_lock, flags);
0337     return child;
0338 }
0339 
0340 /*
0341  * Serio port operations
0342  */
0343 
0344 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
0345 {
0346     struct serio *serio = to_serio_port(dev);
0347     return sprintf(buf, "%s\n", serio->name);
0348 }
0349 
0350 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
0351 {
0352     struct serio *serio = to_serio_port(dev);
0353 
0354     return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
0355             serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
0356 }
0357 
0358 static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
0359 {
0360     struct serio *serio = to_serio_port(dev);
0361     return sprintf(buf, "%02x\n", serio->id.type);
0362 }
0363 
0364 static ssize_t proto_show(struct device *dev, struct device_attribute *attr, char *buf)
0365 {
0366     struct serio *serio = to_serio_port(dev);
0367     return sprintf(buf, "%02x\n", serio->id.proto);
0368 }
0369 
0370 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
0371 {
0372     struct serio *serio = to_serio_port(dev);
0373     return sprintf(buf, "%02x\n", serio->id.id);
0374 }
0375 
0376 static ssize_t extra_show(struct device *dev, struct device_attribute *attr, char *buf)
0377 {
0378     struct serio *serio = to_serio_port(dev);
0379     return sprintf(buf, "%02x\n", serio->id.extra);
0380 }
0381 
0382 static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
0383 {
0384     struct serio *serio = to_serio_port(dev);
0385     struct device_driver *drv;
0386     int error;
0387 
0388     error = mutex_lock_interruptible(&serio_mutex);
0389     if (error)
0390         return error;
0391 
0392     if (!strncmp(buf, "none", count)) {
0393         serio_disconnect_port(serio);
0394     } else if (!strncmp(buf, "reconnect", count)) {
0395         serio_reconnect_subtree(serio);
0396     } else if (!strncmp(buf, "rescan", count)) {
0397         serio_disconnect_port(serio);
0398         serio_find_driver(serio);
0399         serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
0400     } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
0401         serio_disconnect_port(serio);
0402         error = serio_bind_driver(serio, to_serio_driver(drv));
0403         serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
0404     } else {
0405         error = -EINVAL;
0406     }
0407 
0408     mutex_unlock(&serio_mutex);
0409 
0410     return error ? error : count;
0411 }
0412 
0413 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
0414 {
0415     struct serio *serio = to_serio_port(dev);
0416     return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
0417 }
0418 
0419 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
0420 {
0421     struct serio *serio = to_serio_port(dev);
0422     int retval;
0423 
0424     retval = count;
0425     if (!strncmp(buf, "manual", count)) {
0426         serio->manual_bind = true;
0427     } else if (!strncmp(buf, "auto", count)) {
0428         serio->manual_bind = false;
0429     } else {
0430         retval = -EINVAL;
0431     }
0432 
0433     return retval;
0434 }
0435 
0436 static ssize_t firmware_id_show(struct device *dev, struct device_attribute *attr, char *buf)
0437 {
0438     struct serio *serio = to_serio_port(dev);
0439 
0440     return sprintf(buf, "%s\n", serio->firmware_id);
0441 }
0442 
0443 static DEVICE_ATTR_RO(type);
0444 static DEVICE_ATTR_RO(proto);
0445 static DEVICE_ATTR_RO(id);
0446 static DEVICE_ATTR_RO(extra);
0447 
0448 static struct attribute *serio_device_id_attrs[] = {
0449     &dev_attr_type.attr,
0450     &dev_attr_proto.attr,
0451     &dev_attr_id.attr,
0452     &dev_attr_extra.attr,
0453     NULL
0454 };
0455 
0456 static const struct attribute_group serio_id_attr_group = {
0457     .name   = "id",
0458     .attrs  = serio_device_id_attrs,
0459 };
0460 
0461 static DEVICE_ATTR_RO(modalias);
0462 static DEVICE_ATTR_WO(drvctl);
0463 static DEVICE_ATTR(description, S_IRUGO, serio_show_description, NULL);
0464 static DEVICE_ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode);
0465 static DEVICE_ATTR_RO(firmware_id);
0466 
0467 static struct attribute *serio_device_attrs[] = {
0468     &dev_attr_modalias.attr,
0469     &dev_attr_description.attr,
0470     &dev_attr_drvctl.attr,
0471     &dev_attr_bind_mode.attr,
0472     &dev_attr_firmware_id.attr,
0473     NULL
0474 };
0475 
0476 static const struct attribute_group serio_device_attr_group = {
0477     .attrs  = serio_device_attrs,
0478 };
0479 
0480 static const struct attribute_group *serio_device_attr_groups[] = {
0481     &serio_id_attr_group,
0482     &serio_device_attr_group,
0483     NULL
0484 };
0485 
0486 static void serio_release_port(struct device *dev)
0487 {
0488     struct serio *serio = to_serio_port(dev);
0489 
0490     kfree(serio);
0491     module_put(THIS_MODULE);
0492 }
0493 
0494 /*
0495  * Prepare serio port for registration.
0496  */
0497 static void serio_init_port(struct serio *serio)
0498 {
0499     static atomic_t serio_no = ATOMIC_INIT(-1);
0500 
0501     __module_get(THIS_MODULE);
0502 
0503     INIT_LIST_HEAD(&serio->node);
0504     INIT_LIST_HEAD(&serio->child_node);
0505     INIT_LIST_HEAD(&serio->children);
0506     spin_lock_init(&serio->lock);
0507     mutex_init(&serio->drv_mutex);
0508     device_initialize(&serio->dev);
0509     dev_set_name(&serio->dev, "serio%lu",
0510              (unsigned long)atomic_inc_return(&serio_no));
0511     serio->dev.bus = &serio_bus;
0512     serio->dev.release = serio_release_port;
0513     serio->dev.groups = serio_device_attr_groups;
0514     if (serio->parent) {
0515         serio->dev.parent = &serio->parent->dev;
0516         serio->depth = serio->parent->depth + 1;
0517     } else
0518         serio->depth = 0;
0519     lockdep_set_subclass(&serio->lock, serio->depth);
0520 }
0521 
0522 /*
0523  * Complete serio port registration.
0524  * Driver core will attempt to find appropriate driver for the port.
0525  */
0526 static void serio_add_port(struct serio *serio)
0527 {
0528     struct serio *parent = serio->parent;
0529     int error;
0530 
0531     if (parent) {
0532         serio_pause_rx(parent);
0533         list_add_tail(&serio->child_node, &parent->children);
0534         serio_continue_rx(parent);
0535     }
0536 
0537     list_add_tail(&serio->node, &serio_list);
0538 
0539     if (serio->start)
0540         serio->start(serio);
0541 
0542     error = device_add(&serio->dev);
0543     if (error)
0544         dev_err(&serio->dev,
0545             "device_add() failed for %s (%s), error: %d\n",
0546             serio->phys, serio->name, error);
0547 }
0548 
0549 /*
0550  * serio_destroy_port() completes unregistration process and removes
0551  * port from the system
0552  */
0553 static void serio_destroy_port(struct serio *serio)
0554 {
0555     struct serio *child;
0556 
0557     while ((child = serio_get_pending_child(serio)) != NULL) {
0558         serio_remove_pending_events(child);
0559         put_device(&child->dev);
0560     }
0561 
0562     if (serio->stop)
0563         serio->stop(serio);
0564 
0565     if (serio->parent) {
0566         serio_pause_rx(serio->parent);
0567         list_del_init(&serio->child_node);
0568         serio_continue_rx(serio->parent);
0569         serio->parent = NULL;
0570     }
0571 
0572     if (device_is_registered(&serio->dev))
0573         device_del(&serio->dev);
0574 
0575     list_del_init(&serio->node);
0576     serio_remove_pending_events(serio);
0577     put_device(&serio->dev);
0578 }
0579 
0580 /*
0581  * Reconnect serio port (re-initialize attached device).
0582  * If reconnect fails (old device is no longer attached or
0583  * there was no device to begin with) we do full rescan in
0584  * hope of finding a driver for the port.
0585  */
0586 static int serio_reconnect_port(struct serio *serio)
0587 {
0588     int error = serio_reconnect_driver(serio);
0589 
0590     if (error) {
0591         serio_disconnect_port(serio);
0592         serio_find_driver(serio);
0593     }
0594 
0595     return error;
0596 }
0597 
0598 /*
0599  * Reconnect serio port and all its children (re-initialize attached
0600  * devices).
0601  */
0602 static void serio_reconnect_subtree(struct serio *root)
0603 {
0604     struct serio *s = root;
0605     int error;
0606 
0607     do {
0608         error = serio_reconnect_port(s);
0609         if (!error) {
0610             /*
0611              * Reconnect was successful, move on to do the
0612              * first child.
0613              */
0614             if (!list_empty(&s->children)) {
0615                 s = list_first_entry(&s->children,
0616                              struct serio, child_node);
0617                 continue;
0618             }
0619         }
0620 
0621         /*
0622          * Either it was a leaf node or reconnect failed and it
0623          * became a leaf node. Continue reconnecting starting with
0624          * the next sibling of the parent node.
0625          */
0626         while (s != root) {
0627             struct serio *parent = s->parent;
0628 
0629             if (!list_is_last(&s->child_node, &parent->children)) {
0630                 s = list_entry(s->child_node.next,
0631                            struct serio, child_node);
0632                 break;
0633             }
0634 
0635             s = parent;
0636         }
0637     } while (s != root);
0638 }
0639 
0640 /*
0641  * serio_disconnect_port() unbinds a port from its driver. As a side effect
0642  * all children ports are unbound and destroyed.
0643  */
0644 static void serio_disconnect_port(struct serio *serio)
0645 {
0646     struct serio *s = serio;
0647 
0648     /*
0649      * Children ports should be disconnected and destroyed
0650      * first; we travel the tree in depth-first order.
0651      */
0652     while (!list_empty(&serio->children)) {
0653 
0654         /* Locate a leaf */
0655         while (!list_empty(&s->children))
0656             s = list_first_entry(&s->children,
0657                          struct serio, child_node);
0658 
0659         /*
0660          * Prune this leaf node unless it is the one we
0661          * started with.
0662          */
0663         if (s != serio) {
0664             struct serio *parent = s->parent;
0665 
0666             device_release_driver(&s->dev);
0667             serio_destroy_port(s);
0668 
0669             s = parent;
0670         }
0671     }
0672 
0673     /*
0674      * OK, no children left, now disconnect this port.
0675      */
0676     device_release_driver(&serio->dev);
0677 }
0678 
0679 void serio_rescan(struct serio *serio)
0680 {
0681     serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
0682 }
0683 EXPORT_SYMBOL(serio_rescan);
0684 
0685 void serio_reconnect(struct serio *serio)
0686 {
0687     serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
0688 }
0689 EXPORT_SYMBOL(serio_reconnect);
0690 
0691 /*
0692  * Submits register request to kseriod for subsequent execution.
0693  * Note that port registration is always asynchronous.
0694  */
0695 void __serio_register_port(struct serio *serio, struct module *owner)
0696 {
0697     serio_init_port(serio);
0698     serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
0699 }
0700 EXPORT_SYMBOL(__serio_register_port);
0701 
0702 /*
0703  * Synchronously unregisters serio port.
0704  */
0705 void serio_unregister_port(struct serio *serio)
0706 {
0707     mutex_lock(&serio_mutex);
0708     serio_disconnect_port(serio);
0709     serio_destroy_port(serio);
0710     mutex_unlock(&serio_mutex);
0711 }
0712 EXPORT_SYMBOL(serio_unregister_port);
0713 
0714 /*
0715  * Safely unregisters children ports if they are present.
0716  */
0717 void serio_unregister_child_port(struct serio *serio)
0718 {
0719     struct serio *s, *next;
0720 
0721     mutex_lock(&serio_mutex);
0722     list_for_each_entry_safe(s, next, &serio->children, child_node) {
0723         serio_disconnect_port(s);
0724         serio_destroy_port(s);
0725     }
0726     mutex_unlock(&serio_mutex);
0727 }
0728 EXPORT_SYMBOL(serio_unregister_child_port);
0729 
0730 
0731 /*
0732  * Serio driver operations
0733  */
0734 
0735 static ssize_t description_show(struct device_driver *drv, char *buf)
0736 {
0737     struct serio_driver *driver = to_serio_driver(drv);
0738     return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
0739 }
0740 static DRIVER_ATTR_RO(description);
0741 
0742 static ssize_t bind_mode_show(struct device_driver *drv, char *buf)
0743 {
0744     struct serio_driver *serio_drv = to_serio_driver(drv);
0745     return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
0746 }
0747 
0748 static ssize_t bind_mode_store(struct device_driver *drv, const char *buf, size_t count)
0749 {
0750     struct serio_driver *serio_drv = to_serio_driver(drv);
0751     int retval;
0752 
0753     retval = count;
0754     if (!strncmp(buf, "manual", count)) {
0755         serio_drv->manual_bind = true;
0756     } else if (!strncmp(buf, "auto", count)) {
0757         serio_drv->manual_bind = false;
0758     } else {
0759         retval = -EINVAL;
0760     }
0761 
0762     return retval;
0763 }
0764 static DRIVER_ATTR_RW(bind_mode);
0765 
0766 static struct attribute *serio_driver_attrs[] = {
0767     &driver_attr_description.attr,
0768     &driver_attr_bind_mode.attr,
0769     NULL,
0770 };
0771 ATTRIBUTE_GROUPS(serio_driver);
0772 
0773 static int serio_driver_probe(struct device *dev)
0774 {
0775     struct serio *serio = to_serio_port(dev);
0776     struct serio_driver *drv = to_serio_driver(dev->driver);
0777 
0778     return serio_connect_driver(serio, drv);
0779 }
0780 
0781 static void serio_driver_remove(struct device *dev)
0782 {
0783     struct serio *serio = to_serio_port(dev);
0784 
0785     serio_disconnect_driver(serio);
0786 }
0787 
0788 static void serio_cleanup(struct serio *serio)
0789 {
0790     mutex_lock(&serio->drv_mutex);
0791     if (serio->drv && serio->drv->cleanup)
0792         serio->drv->cleanup(serio);
0793     mutex_unlock(&serio->drv_mutex);
0794 }
0795 
0796 static void serio_shutdown(struct device *dev)
0797 {
0798     struct serio *serio = to_serio_port(dev);
0799 
0800     serio_cleanup(serio);
0801 }
0802 
0803 static void serio_attach_driver(struct serio_driver *drv)
0804 {
0805     int error;
0806 
0807     error = driver_attach(&drv->driver);
0808     if (error)
0809         pr_warn("driver_attach() failed for %s with error %d\n",
0810             drv->driver.name, error);
0811 }
0812 
0813 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
0814 {
0815     bool manual_bind = drv->manual_bind;
0816     int error;
0817 
0818     drv->driver.bus = &serio_bus;
0819     drv->driver.owner = owner;
0820     drv->driver.mod_name = mod_name;
0821 
0822     /*
0823      * Temporarily disable automatic binding because probing
0824      * takes long time and we are better off doing it in kseriod
0825      */
0826     drv->manual_bind = true;
0827 
0828     error = driver_register(&drv->driver);
0829     if (error) {
0830         pr_err("driver_register() failed for %s, error: %d\n",
0831             drv->driver.name, error);
0832         return error;
0833     }
0834 
0835     /*
0836      * Restore original bind mode and let kseriod bind the
0837      * driver to free ports
0838      */
0839     if (!manual_bind) {
0840         drv->manual_bind = false;
0841         error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
0842         if (error) {
0843             driver_unregister(&drv->driver);
0844             return error;
0845         }
0846     }
0847 
0848     return 0;
0849 }
0850 EXPORT_SYMBOL(__serio_register_driver);
0851 
0852 void serio_unregister_driver(struct serio_driver *drv)
0853 {
0854     struct serio *serio;
0855 
0856     mutex_lock(&serio_mutex);
0857 
0858     drv->manual_bind = true;    /* so serio_find_driver ignores it */
0859     serio_remove_pending_events(drv);
0860 
0861 start_over:
0862     list_for_each_entry(serio, &serio_list, node) {
0863         if (serio->drv == drv) {
0864             serio_disconnect_port(serio);
0865             serio_find_driver(serio);
0866             /* we could've deleted some ports, restart */
0867             goto start_over;
0868         }
0869     }
0870 
0871     driver_unregister(&drv->driver);
0872     mutex_unlock(&serio_mutex);
0873 }
0874 EXPORT_SYMBOL(serio_unregister_driver);
0875 
0876 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
0877 {
0878     serio_pause_rx(serio);
0879     serio->drv = drv;
0880     serio_continue_rx(serio);
0881 }
0882 
0883 static int serio_bus_match(struct device *dev, struct device_driver *drv)
0884 {
0885     struct serio *serio = to_serio_port(dev);
0886     struct serio_driver *serio_drv = to_serio_driver(drv);
0887 
0888     if (serio->manual_bind || serio_drv->manual_bind)
0889         return 0;
0890 
0891     return serio_match_port(serio_drv->id_table, serio);
0892 }
0893 
0894 #define SERIO_ADD_UEVENT_VAR(fmt, val...)               \
0895     do {                                \
0896         int err = add_uevent_var(env, fmt, val);        \
0897         if (err)                        \
0898             return err;                 \
0899     } while (0)
0900 
0901 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
0902 {
0903     struct serio *serio;
0904 
0905     if (!dev)
0906         return -ENODEV;
0907 
0908     serio = to_serio_port(dev);
0909 
0910     SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
0911     SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
0912     SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
0913     SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
0914 
0915     SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
0916                 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
0917 
0918     if (serio->firmware_id[0])
0919         SERIO_ADD_UEVENT_VAR("SERIO_FIRMWARE_ID=%s",
0920                      serio->firmware_id);
0921 
0922     return 0;
0923 }
0924 #undef SERIO_ADD_UEVENT_VAR
0925 
0926 #ifdef CONFIG_PM
0927 static int serio_suspend(struct device *dev)
0928 {
0929     struct serio *serio = to_serio_port(dev);
0930 
0931     serio_cleanup(serio);
0932 
0933     return 0;
0934 }
0935 
0936 static int serio_resume(struct device *dev)
0937 {
0938     struct serio *serio = to_serio_port(dev);
0939     int error = -ENOENT;
0940 
0941     mutex_lock(&serio->drv_mutex);
0942     if (serio->drv && serio->drv->fast_reconnect) {
0943         error = serio->drv->fast_reconnect(serio);
0944         if (error && error != -ENOENT)
0945             dev_warn(dev, "fast reconnect failed with error %d\n",
0946                  error);
0947     }
0948     mutex_unlock(&serio->drv_mutex);
0949 
0950     if (error) {
0951         /*
0952          * Driver reconnect can take a while, so better let
0953          * kseriod deal with it.
0954          */
0955         serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
0956     }
0957 
0958     return 0;
0959 }
0960 
0961 static const struct dev_pm_ops serio_pm_ops = {
0962     .suspend    = serio_suspend,
0963     .resume     = serio_resume,
0964     .poweroff   = serio_suspend,
0965     .restore    = serio_resume,
0966 };
0967 #endif /* CONFIG_PM */
0968 
0969 /* called from serio_driver->connect/disconnect methods under serio_mutex */
0970 int serio_open(struct serio *serio, struct serio_driver *drv)
0971 {
0972     serio_set_drv(serio, drv);
0973 
0974     if (serio->open && serio->open(serio)) {
0975         serio_set_drv(serio, NULL);
0976         return -1;
0977     }
0978     return 0;
0979 }
0980 EXPORT_SYMBOL(serio_open);
0981 
0982 /* called from serio_driver->connect/disconnect methods under serio_mutex */
0983 void serio_close(struct serio *serio)
0984 {
0985     if (serio->close)
0986         serio->close(serio);
0987 
0988     serio_set_drv(serio, NULL);
0989 }
0990 EXPORT_SYMBOL(serio_close);
0991 
0992 irqreturn_t serio_interrupt(struct serio *serio,
0993         unsigned char data, unsigned int dfl)
0994 {
0995     unsigned long flags;
0996     irqreturn_t ret = IRQ_NONE;
0997 
0998     spin_lock_irqsave(&serio->lock, flags);
0999 
1000         if (likely(serio->drv)) {
1001                 ret = serio->drv->interrupt(serio, data, dfl);
1002     } else if (!dfl && device_is_registered(&serio->dev)) {
1003         serio_rescan(serio);
1004         ret = IRQ_HANDLED;
1005     }
1006 
1007     spin_unlock_irqrestore(&serio->lock, flags);
1008 
1009     return ret;
1010 }
1011 EXPORT_SYMBOL(serio_interrupt);
1012 
1013 struct bus_type serio_bus = {
1014     .name       = "serio",
1015     .drv_groups = serio_driver_groups,
1016     .match      = serio_bus_match,
1017     .uevent     = serio_uevent,
1018     .probe      = serio_driver_probe,
1019     .remove     = serio_driver_remove,
1020     .shutdown   = serio_shutdown,
1021 #ifdef CONFIG_PM
1022     .pm     = &serio_pm_ops,
1023 #endif
1024 };
1025 EXPORT_SYMBOL(serio_bus);
1026 
1027 static int __init serio_init(void)
1028 {
1029     int error;
1030 
1031     error = bus_register(&serio_bus);
1032     if (error) {
1033         pr_err("Failed to register serio bus, error: %d\n", error);
1034         return error;
1035     }
1036 
1037     return 0;
1038 }
1039 
1040 static void __exit serio_exit(void)
1041 {
1042     bus_unregister(&serio_bus);
1043 
1044     /*
1045      * There should not be any outstanding events but work may
1046      * still be scheduled so simply cancel it.
1047      */
1048     cancel_work_sync(&serio_event_work);
1049 }
1050 
1051 subsys_initcall(serio_init);
1052 module_exit(serio_exit);