1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * The Serio abstraction module
5 * Copyright (c) 1999-2004 Vojtech Pavlik
6 * Copyright (c) 2004 Dmitry Torokhov
7 * Copyright (c) 2003 Daniele Bellucci
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/stddef.h>
13 #include <linux/module.h>
14 #include <linux/serio.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19 #include <linux/mutex.h>
22 MODULE_DESCRIPTION("Serio abstraction core");
23 MODULE_LICENSE("GPL");
26 * serio_mutex protects entire serio subsystem and is taken every time
27 * serio port or driver registered or unregistered.
29 static DEFINE_MUTEX(serio_mutex);
31 static LIST_HEAD(serio_list);
33 static void serio_add_port(struct serio *serio);
34 static int serio_reconnect_port(struct serio *serio);
35 static void serio_disconnect_port(struct serio *serio);
36 static void serio_reconnect_subtree(struct serio *serio);
37 static void serio_attach_driver(struct serio_driver *drv);
39 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
41 guard(mutex)(&serio->drv_mutex);
43 return drv->connect(serio, drv);
46 static int serio_reconnect_driver(struct serio *serio)
48 guard(mutex)(&serio->drv_mutex);
50 if (serio->drv && serio->drv->reconnect)
51 return serio->drv->reconnect(serio);
56 static void serio_disconnect_driver(struct serio *serio)
58 guard(mutex)(&serio->drv_mutex);
61 serio->drv->disconnect(serio);
64 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
66 while (ids->type || ids->proto) {
67 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
68 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
69 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
70 (ids->id == SERIO_ANY || ids->id == serio->id.id))
78 * Basic serio -> driver core mappings
81 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
85 if (serio_match_port(drv->id_table, serio)) {
87 serio->dev.driver = &drv->driver;
88 if (serio_connect_driver(serio, drv)) {
89 serio->dev.driver = NULL;
93 error = device_bind_driver(&serio->dev);
96 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
97 serio->phys, serio->name,
98 drv->description, error);
99 serio_disconnect_driver(serio);
100 serio->dev.driver = NULL;
107 static void serio_find_driver(struct serio *serio)
111 error = device_attach(&serio->dev);
112 if (error < 0 && error != -EPROBE_DEFER)
113 dev_warn(&serio->dev,
114 "device_attach() failed for %s (%s), error: %d\n",
115 serio->phys, serio->name, error);
120 * Serio event processing.
123 enum serio_event_type {
125 SERIO_RECONNECT_PORT,
126 SERIO_RECONNECT_SUBTREE,
132 enum serio_event_type type;
134 struct module *owner;
135 struct list_head node;
138 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
139 static LIST_HEAD(serio_event_list);
141 static struct serio_event *serio_get_event(void)
143 struct serio_event *event = NULL;
145 guard(spinlock_irqsave)(&serio_event_lock);
147 if (!list_empty(&serio_event_list)) {
148 event = list_first_entry(&serio_event_list,
149 struct serio_event, node);
150 list_del_init(&event->node);
156 static void serio_free_event(struct serio_event *event)
158 module_put(event->owner);
162 static void serio_remove_duplicate_events(void *object,
163 enum serio_event_type type)
165 struct serio_event *e, *next;
167 guard(spinlock_irqsave)(&serio_event_lock);
169 list_for_each_entry_safe(e, next, &serio_event_list, node) {
170 if (object == e->object) {
172 * If this event is of different type we should not
173 * look further - we only suppress duplicate events
174 * that were sent back-to-back.
179 list_del_init(&e->node);
185 static void serio_handle_event(struct work_struct *work)
187 struct serio_event *event;
189 guard(mutex)(&serio_mutex);
191 while ((event = serio_get_event())) {
193 switch (event->type) {
195 case SERIO_REGISTER_PORT:
196 serio_add_port(event->object);
199 case SERIO_RECONNECT_PORT:
200 serio_reconnect_port(event->object);
203 case SERIO_RESCAN_PORT:
204 serio_disconnect_port(event->object);
205 serio_find_driver(event->object);
208 case SERIO_RECONNECT_SUBTREE:
209 serio_reconnect_subtree(event->object);
212 case SERIO_ATTACH_DRIVER:
213 serio_attach_driver(event->object);
217 serio_remove_duplicate_events(event->object, event->type);
218 serio_free_event(event);
222 static DECLARE_WORK(serio_event_work, serio_handle_event);
224 static int serio_queue_event(void *object, struct module *owner,
225 enum serio_event_type event_type)
227 struct serio_event *event;
229 guard(spinlock_irqsave)(&serio_event_lock);
232 * Scan event list for the other events for the same serio port,
233 * starting with the most recent one. If event is the same we
234 * do not need add new one. If event is of different type we
235 * need to add this event and should not look further because
236 * we need to preseve sequence of distinct events.
238 list_for_each_entry_reverse(event, &serio_event_list, node) {
239 if (event->object == object) {
240 if (event->type == event_type)
246 event = kmalloc(sizeof(*event), GFP_ATOMIC);
248 pr_err("Not enough memory to queue event %d\n", event_type);
252 if (!try_module_get(owner)) {
253 pr_warn("Can't get module reference, dropping event %d\n",
259 event->type = event_type;
260 event->object = object;
261 event->owner = owner;
263 list_add_tail(&event->node, &serio_event_list);
264 queue_work(system_long_wq, &serio_event_work);
270 * Remove all events that have been submitted for a given
271 * object, be it serio port or driver.
273 static void serio_remove_pending_events(void *object)
275 struct serio_event *event, *next;
277 guard(spinlock_irqsave)(&serio_event_lock);
279 list_for_each_entry_safe(event, next, &serio_event_list, node) {
280 if (event->object == object) {
281 list_del_init(&event->node);
282 serio_free_event(event);
288 * Locate child serio port (if any) that has not been fully registered yet.
290 * Children are registered by driver's connect() handler so there can't be a
291 * grandchild pending registration together with a child.
293 static struct serio *serio_get_pending_child(struct serio *parent)
295 struct serio_event *event;
298 guard(spinlock_irqsave)(&serio_event_lock);
300 list_for_each_entry(event, &serio_event_list, node) {
301 if (event->type == SERIO_REGISTER_PORT) {
302 serio = event->object;
303 if (serio->parent == parent)
312 * Serio port operations
315 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
317 struct serio *serio = to_serio_port(dev);
318 return sprintf(buf, "%s\n", serio->name);
321 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
323 struct serio *serio = to_serio_port(dev);
325 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
326 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
329 static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
331 struct serio *serio = to_serio_port(dev);
332 return sprintf(buf, "%02x\n", serio->id.type);
335 static ssize_t proto_show(struct device *dev, struct device_attribute *attr, char *buf)
337 struct serio *serio = to_serio_port(dev);
338 return sprintf(buf, "%02x\n", serio->id.proto);
341 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
343 struct serio *serio = to_serio_port(dev);
344 return sprintf(buf, "%02x\n", serio->id.id);
347 static ssize_t extra_show(struct device *dev, struct device_attribute *attr, char *buf)
349 struct serio *serio = to_serio_port(dev);
350 return sprintf(buf, "%02x\n", serio->id.extra);
353 static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
355 struct serio *serio = to_serio_port(dev);
356 struct device_driver *drv;
359 scoped_cond_guard(mutex_intr, return -EINTR, &serio_mutex) {
360 if (!strncmp(buf, "none", count)) {
361 serio_disconnect_port(serio);
362 } else if (!strncmp(buf, "reconnect", count)) {
363 serio_reconnect_subtree(serio);
364 } else if (!strncmp(buf, "rescan", count)) {
365 serio_disconnect_port(serio);
366 serio_find_driver(serio);
367 serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
368 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
369 serio_disconnect_port(serio);
370 error = serio_bind_driver(serio, to_serio_driver(drv));
371 serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
382 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
384 struct serio *serio = to_serio_port(dev);
385 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
388 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
390 struct serio *serio = to_serio_port(dev);
394 if (!strncmp(buf, "manual", count)) {
395 serio->manual_bind = true;
396 } else if (!strncmp(buf, "auto", count)) {
397 serio->manual_bind = false;
405 static ssize_t firmware_id_show(struct device *dev, struct device_attribute *attr, char *buf)
407 struct serio *serio = to_serio_port(dev);
409 return sprintf(buf, "%s\n", serio->firmware_id);
412 static DEVICE_ATTR_RO(type);
413 static DEVICE_ATTR_RO(proto);
414 static DEVICE_ATTR_RO(id);
415 static DEVICE_ATTR_RO(extra);
417 static struct attribute *serio_device_id_attrs[] = {
419 &dev_attr_proto.attr,
421 &dev_attr_extra.attr,
425 static const struct attribute_group serio_id_attr_group = {
427 .attrs = serio_device_id_attrs,
430 static DEVICE_ATTR_RO(modalias);
431 static DEVICE_ATTR_WO(drvctl);
432 static DEVICE_ATTR(description, S_IRUGO, serio_show_description, NULL);
433 static DEVICE_ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode);
434 static DEVICE_ATTR_RO(firmware_id);
436 static struct attribute *serio_device_attrs[] = {
437 &dev_attr_modalias.attr,
438 &dev_attr_description.attr,
439 &dev_attr_drvctl.attr,
440 &dev_attr_bind_mode.attr,
441 &dev_attr_firmware_id.attr,
445 static const struct attribute_group serio_device_attr_group = {
446 .attrs = serio_device_attrs,
449 static const struct attribute_group *serio_device_attr_groups[] = {
450 &serio_id_attr_group,
451 &serio_device_attr_group,
455 static void serio_release_port(struct device *dev)
457 struct serio *serio = to_serio_port(dev);
460 module_put(THIS_MODULE);
464 * Prepare serio port for registration.
466 static void serio_init_port(struct serio *serio)
468 static atomic_t serio_no = ATOMIC_INIT(-1);
470 __module_get(THIS_MODULE);
472 INIT_LIST_HEAD(&serio->node);
473 INIT_LIST_HEAD(&serio->child_node);
474 INIT_LIST_HEAD(&serio->children);
475 spin_lock_init(&serio->lock);
476 mutex_init(&serio->drv_mutex);
477 device_initialize(&serio->dev);
478 dev_set_name(&serio->dev, "serio%lu",
479 (unsigned long)atomic_inc_return(&serio_no));
480 serio->dev.bus = &serio_bus;
481 serio->dev.release = serio_release_port;
482 serio->dev.groups = serio_device_attr_groups;
484 serio->dev.parent = &serio->parent->dev;
485 serio->depth = serio->parent->depth + 1;
488 lockdep_set_subclass(&serio->lock, serio->depth);
492 * Complete serio port registration.
493 * Driver core will attempt to find appropriate driver for the port.
495 static void serio_add_port(struct serio *serio)
497 struct serio *parent = serio->parent;
501 guard(serio_pause_rx)(parent);
503 list_add_tail(&serio->child_node, &parent->children);
506 list_add_tail(&serio->node, &serio_list);
511 error = device_add(&serio->dev);
514 "device_add() failed for %s (%s), error: %d\n",
515 serio->phys, serio->name, error);
519 * serio_destroy_port() completes unregistration process and removes
520 * port from the system
522 static void serio_destroy_port(struct serio *serio)
526 while ((child = serio_get_pending_child(serio)) != NULL) {
527 serio_remove_pending_events(child);
528 put_device(&child->dev);
535 guard(serio_pause_rx)(serio->parent);
537 list_del_init(&serio->child_node);
538 serio->parent = NULL;
541 if (device_is_registered(&serio->dev))
542 device_del(&serio->dev);
544 list_del_init(&serio->node);
545 serio_remove_pending_events(serio);
546 put_device(&serio->dev);
550 * Reconnect serio port (re-initialize attached device).
551 * If reconnect fails (old device is no longer attached or
552 * there was no device to begin with) we do full rescan in
553 * hope of finding a driver for the port.
555 static int serio_reconnect_port(struct serio *serio)
557 int error = serio_reconnect_driver(serio);
560 serio_disconnect_port(serio);
561 serio_find_driver(serio);
568 * Reconnect serio port and all its children (re-initialize attached
571 static void serio_reconnect_subtree(struct serio *root)
573 struct serio *s = root;
577 error = serio_reconnect_port(s);
580 * Reconnect was successful, move on to do the
583 if (!list_empty(&s->children)) {
584 s = list_first_entry(&s->children,
585 struct serio, child_node);
591 * Either it was a leaf node or reconnect failed and it
592 * became a leaf node. Continue reconnecting starting with
593 * the next sibling of the parent node.
596 struct serio *parent = s->parent;
598 if (!list_is_last(&s->child_node, &parent->children)) {
599 s = list_entry(s->child_node.next,
600 struct serio, child_node);
610 * serio_disconnect_port() unbinds a port from its driver. As a side effect
611 * all children ports are unbound and destroyed.
613 static void serio_disconnect_port(struct serio *serio)
615 struct serio *s = serio;
618 * Children ports should be disconnected and destroyed
619 * first; we travel the tree in depth-first order.
621 while (!list_empty(&serio->children)) {
624 while (!list_empty(&s->children))
625 s = list_first_entry(&s->children,
626 struct serio, child_node);
629 * Prune this leaf node unless it is the one we
633 struct serio *parent = s->parent;
635 device_release_driver(&s->dev);
636 serio_destroy_port(s);
643 * OK, no children left, now disconnect this port.
645 device_release_driver(&serio->dev);
648 void serio_rescan(struct serio *serio)
650 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
652 EXPORT_SYMBOL(serio_rescan);
654 void serio_reconnect(struct serio *serio)
656 serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
658 EXPORT_SYMBOL(serio_reconnect);
661 * Submits register request to kseriod for subsequent execution.
662 * Note that port registration is always asynchronous.
664 void __serio_register_port(struct serio *serio, struct module *owner)
666 serio_init_port(serio);
667 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
669 EXPORT_SYMBOL(__serio_register_port);
672 * Synchronously unregisters serio port.
674 void serio_unregister_port(struct serio *serio)
676 guard(mutex)(&serio_mutex);
678 serio_disconnect_port(serio);
679 serio_destroy_port(serio);
681 EXPORT_SYMBOL(serio_unregister_port);
684 * Safely unregisters children ports if they are present.
686 void serio_unregister_child_port(struct serio *serio)
688 struct serio *s, *next;
690 guard(mutex)(&serio_mutex);
692 list_for_each_entry_safe(s, next, &serio->children, child_node) {
693 serio_disconnect_port(s);
694 serio_destroy_port(s);
697 EXPORT_SYMBOL(serio_unregister_child_port);
701 * Serio driver operations
704 static ssize_t description_show(struct device_driver *drv, char *buf)
706 struct serio_driver *driver = to_serio_driver(drv);
707 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
709 static DRIVER_ATTR_RO(description);
711 static ssize_t bind_mode_show(struct device_driver *drv, char *buf)
713 struct serio_driver *serio_drv = to_serio_driver(drv);
714 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
717 static ssize_t bind_mode_store(struct device_driver *drv, const char *buf, size_t count)
719 struct serio_driver *serio_drv = to_serio_driver(drv);
723 if (!strncmp(buf, "manual", count)) {
724 serio_drv->manual_bind = true;
725 } else if (!strncmp(buf, "auto", count)) {
726 serio_drv->manual_bind = false;
733 static DRIVER_ATTR_RW(bind_mode);
735 static struct attribute *serio_driver_attrs[] = {
736 &driver_attr_description.attr,
737 &driver_attr_bind_mode.attr,
740 ATTRIBUTE_GROUPS(serio_driver);
742 static int serio_driver_probe(struct device *dev)
744 struct serio *serio = to_serio_port(dev);
745 struct serio_driver *drv = to_serio_driver(dev->driver);
747 return serio_connect_driver(serio, drv);
750 static void serio_driver_remove(struct device *dev)
752 struct serio *serio = to_serio_port(dev);
754 serio_disconnect_driver(serio);
757 static void serio_cleanup(struct serio *serio)
759 guard(mutex)(&serio->drv_mutex);
761 if (serio->drv && serio->drv->cleanup)
762 serio->drv->cleanup(serio);
765 static void serio_shutdown(struct device *dev)
767 struct serio *serio = to_serio_port(dev);
769 serio_cleanup(serio);
772 static void serio_attach_driver(struct serio_driver *drv)
776 error = driver_attach(&drv->driver);
778 pr_warn("driver_attach() failed for %s with error %d\n",
779 drv->driver.name, error);
782 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
784 bool manual_bind = drv->manual_bind;
787 drv->driver.bus = &serio_bus;
788 drv->driver.owner = owner;
789 drv->driver.mod_name = mod_name;
792 * Temporarily disable automatic binding because probing
793 * takes long time and we are better off doing it in kseriod
795 drv->manual_bind = true;
797 error = driver_register(&drv->driver);
799 pr_err("driver_register() failed for %s, error: %d\n",
800 drv->driver.name, error);
805 * Restore original bind mode and let kseriod bind the
806 * driver to free ports
809 drv->manual_bind = false;
810 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
812 driver_unregister(&drv->driver);
819 EXPORT_SYMBOL(__serio_register_driver);
821 void serio_unregister_driver(struct serio_driver *drv)
825 guard(mutex)(&serio_mutex);
827 drv->manual_bind = true; /* so serio_find_driver ignores it */
828 serio_remove_pending_events(drv);
831 list_for_each_entry(serio, &serio_list, node) {
832 if (serio->drv == drv) {
833 serio_disconnect_port(serio);
834 serio_find_driver(serio);
835 /* we could've deleted some ports, restart */
840 driver_unregister(&drv->driver);
842 EXPORT_SYMBOL(serio_unregister_driver);
844 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
846 guard(serio_pause_rx)(serio);
851 static int serio_bus_match(struct device *dev, const struct device_driver *drv)
853 struct serio *serio = to_serio_port(dev);
854 const struct serio_driver *serio_drv = to_serio_driver(drv);
856 if (serio->manual_bind || serio_drv->manual_bind)
859 return serio_match_port(serio_drv->id_table, serio);
862 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
864 int err = add_uevent_var(env, fmt, val); \
869 static int serio_uevent(const struct device *dev, struct kobj_uevent_env *env)
871 const struct serio *serio;
876 serio = to_serio_port(dev);
878 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
879 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
880 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
881 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
883 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
884 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
886 if (serio->firmware_id[0])
887 SERIO_ADD_UEVENT_VAR("SERIO_FIRMWARE_ID=%s",
892 #undef SERIO_ADD_UEVENT_VAR
895 static int serio_suspend(struct device *dev)
897 struct serio *serio = to_serio_port(dev);
899 serio_cleanup(serio);
904 static int serio_resume(struct device *dev)
906 struct serio *serio = to_serio_port(dev);
909 scoped_guard(mutex, &serio->drv_mutex) {
910 if (serio->drv && serio->drv->fast_reconnect) {
911 error = serio->drv->fast_reconnect(serio);
912 if (error && error != -ENOENT)
913 dev_warn(dev, "fast reconnect failed with error %d\n",
920 * Driver reconnect can take a while, so better let
921 * kseriod deal with it.
923 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
929 static const struct dev_pm_ops serio_pm_ops = {
930 .suspend = serio_suspend,
931 .resume = serio_resume,
932 .poweroff = serio_suspend,
933 .restore = serio_resume,
935 #endif /* CONFIG_PM */
937 /* called from serio_driver->connect/disconnect methods under serio_mutex */
938 int serio_open(struct serio *serio, struct serio_driver *drv)
940 serio_set_drv(serio, drv);
942 if (serio->open && serio->open(serio)) {
943 serio_set_drv(serio, NULL);
948 EXPORT_SYMBOL(serio_open);
950 /* called from serio_driver->connect/disconnect methods under serio_mutex */
951 void serio_close(struct serio *serio)
956 serio_set_drv(serio, NULL);
958 EXPORT_SYMBOL(serio_close);
960 irqreturn_t serio_interrupt(struct serio *serio,
961 unsigned char data, unsigned int dfl)
963 guard(spinlock_irqsave)(&serio->lock);
965 if (likely(serio->drv))
966 return serio->drv->interrupt(serio, data, dfl);
968 if (!dfl && device_is_registered(&serio->dev)) {
975 EXPORT_SYMBOL(serio_interrupt);
977 const struct bus_type serio_bus = {
979 .drv_groups = serio_driver_groups,
980 .match = serio_bus_match,
981 .uevent = serio_uevent,
982 .probe = serio_driver_probe,
983 .remove = serio_driver_remove,
984 .shutdown = serio_shutdown,
989 EXPORT_SYMBOL(serio_bus);
991 static int __init serio_init(void)
995 error = bus_register(&serio_bus);
997 pr_err("Failed to register serio bus, error: %d\n", error);
1004 static void __exit serio_exit(void)
1006 bus_unregister(&serio_bus);
1009 * There should not be any outstanding events but work may
1010 * still be scheduled so simply cancel it.
1012 cancel_work_sync(&serio_event_work);
1015 subsys_initcall(serio_init);
1016 module_exit(serio_exit);