Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  The Serio abstraction module
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (c) 1999-2004 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  Copyright (c) 2004 Dmitry Torokhov
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  Copyright (c) 2003 Daniele Bellucci
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/serio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) MODULE_DESCRIPTION("Serio abstraction core");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * serio_mutex protects entire serio subsystem and is taken every time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  * serio port or driver registered or unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) static DEFINE_MUTEX(serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) static LIST_HEAD(serio_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) static void serio_add_port(struct serio *serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) static int serio_reconnect_port(struct serio *serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) static void serio_disconnect_port(struct serio *serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) static void serio_reconnect_subtree(struct serio *serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) static void serio_attach_driver(struct serio_driver *drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 	mutex_lock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 	retval = drv->connect(serio, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	mutex_unlock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) static int serio_reconnect_driver(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	int retval = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	mutex_lock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	if (serio->drv && serio->drv->reconnect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 		retval = serio->drv->reconnect(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	mutex_unlock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) static void serio_disconnect_driver(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	mutex_lock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	if (serio->drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 		serio->drv->disconnect(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	mutex_unlock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	while (ids->type || ids->proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		    (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		    (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 		    (ids->id == SERIO_ANY || ids->id == serio->id.id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 			return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		ids++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * Basic serio -> driver core mappings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	if (serio_match_port(drv->id_table, serio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 		serio->dev.driver = &drv->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 		if (serio_connect_driver(serio, drv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 			serio->dev.driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 		error = device_bind_driver(&serio->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 			dev_warn(&serio->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 				 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 				 serio->phys, serio->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 				 drv->description, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 			serio_disconnect_driver(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 			serio->dev.driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) static void serio_find_driver(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	error = device_attach(&serio->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	if (error < 0 && error != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 		dev_warn(&serio->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 			 "device_attach() failed for %s (%s), error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 			 serio->phys, serio->name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  * Serio event processing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) enum serio_event_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	SERIO_RESCAN_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	SERIO_RECONNECT_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	SERIO_RECONNECT_SUBTREE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	SERIO_REGISTER_PORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	SERIO_ATTACH_DRIVER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) struct serio_event {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	enum serio_event_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	void *object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	struct module *owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) static DEFINE_SPINLOCK(serio_event_lock);	/* protects serio_event_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) static LIST_HEAD(serio_event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) static struct serio_event *serio_get_event(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	struct serio_event *event = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	spin_lock_irqsave(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	if (!list_empty(&serio_event_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		event = list_first_entry(&serio_event_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 					 struct serio_event, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 		list_del_init(&event->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	spin_unlock_irqrestore(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	return event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) static void serio_free_event(struct serio_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	module_put(event->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	kfree(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) static void serio_remove_duplicate_events(void *object,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 					  enum serio_event_type type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	struct serio_event *e, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	spin_lock_irqsave(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	list_for_each_entry_safe(e, next, &serio_event_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		if (object == e->object) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 			 * If this event is of different type we should not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 			 * look further - we only suppress duplicate events
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			 * that were sent back-to-back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 			if (type != e->type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 			list_del_init(&e->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 			serio_free_event(e);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	spin_unlock_irqrestore(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) static void serio_handle_event(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	struct serio_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	mutex_lock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	while ((event = serio_get_event())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 		switch (event->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		case SERIO_REGISTER_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 			serio_add_port(event->object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		case SERIO_RECONNECT_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 			serio_reconnect_port(event->object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		case SERIO_RESCAN_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			serio_disconnect_port(event->object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			serio_find_driver(event->object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		case SERIO_RECONNECT_SUBTREE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 			serio_reconnect_subtree(event->object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		case SERIO_ATTACH_DRIVER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 			serio_attach_driver(event->object);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 		serio_remove_duplicate_events(event->object, event->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		serio_free_event(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	mutex_unlock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) static DECLARE_WORK(serio_event_work, serio_handle_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) static int serio_queue_event(void *object, struct module *owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			     enum serio_event_type event_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	struct serio_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	spin_lock_irqsave(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	 * Scan event list for the other events for the same serio port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	 * starting with the most recent one. If event is the same we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	 * do not need add new one. If event is of different type we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	 * need to add this event and should not look further because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	 * we need to preseve sequence of distinct events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	list_for_each_entry_reverse(event, &serio_event_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		if (event->object == object) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 			if (event->type == event_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	if (!event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		pr_err("Not enough memory to queue event %d\n", event_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	if (!try_module_get(owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		pr_warn("Can't get module reference, dropping event %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			event_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		kfree(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	event->type = event_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	event->object = object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	event->owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	list_add_tail(&event->node, &serio_event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	queue_work(system_long_wq, &serio_event_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	spin_unlock_irqrestore(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292)  * Remove all events that have been submitted for a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293)  * object, be it serio port or driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) static void serio_remove_pending_events(void *object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	struct serio_event *event, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	spin_lock_irqsave(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	list_for_each_entry_safe(event, next, &serio_event_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		if (event->object == object) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 			list_del_init(&event->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 			serio_free_event(event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	spin_unlock_irqrestore(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313)  * Locate child serio port (if any) that has not been fully registered yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  * Children are registered by driver's connect() handler so there can't be a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  * grandchild pending registration together with a child.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) static struct serio *serio_get_pending_child(struct serio *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct serio_event *event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	struct serio *serio, *child = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	spin_lock_irqsave(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	list_for_each_entry(event, &serio_event_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		if (event->type == SERIO_REGISTER_PORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			serio = event->object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 			if (serio->parent == parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 				child = serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	spin_unlock_irqrestore(&serio_event_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  * Serio port operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	return sprintf(buf, "%s\n", serio->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	return sprintf(buf, "%02x\n", serio->id.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) static ssize_t proto_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	return sprintf(buf, "%02x\n", serio->id.proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	return sprintf(buf, "%02x\n", serio->id.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) static ssize_t extra_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	return sprintf(buf, "%02x\n", serio->id.extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct device_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	error = mutex_lock_interruptible(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	if (!strncmp(buf, "none", count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		serio_disconnect_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	} else if (!strncmp(buf, "reconnect", count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		serio_reconnect_subtree(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	} else if (!strncmp(buf, "rescan", count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		serio_disconnect_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		serio_find_driver(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 		serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	} else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		serio_disconnect_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		error = serio_bind_driver(serio, to_serio_driver(drv));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	mutex_unlock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	return error ? error : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	retval = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (!strncmp(buf, "manual", count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		serio->manual_bind = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	} else if (!strncmp(buf, "auto", count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		serio->manual_bind = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) static ssize_t firmware_id_show(struct device *dev, struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	return sprintf(buf, "%s\n", serio->firmware_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) static DEVICE_ATTR_RO(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) static DEVICE_ATTR_RO(proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) static DEVICE_ATTR_RO(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) static DEVICE_ATTR_RO(extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) static struct attribute *serio_device_id_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	&dev_attr_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	&dev_attr_proto.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	&dev_attr_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	&dev_attr_extra.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) static const struct attribute_group serio_id_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	.name	= "id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	.attrs	= serio_device_id_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) static DEVICE_ATTR_RO(modalias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) static DEVICE_ATTR_WO(drvctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) static DEVICE_ATTR(description, S_IRUGO, serio_show_description, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) static DEVICE_ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) static DEVICE_ATTR_RO(firmware_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) static struct attribute *serio_device_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	&dev_attr_modalias.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	&dev_attr_description.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	&dev_attr_drvctl.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	&dev_attr_bind_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	&dev_attr_firmware_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) static const struct attribute_group serio_device_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	.attrs	= serio_device_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) static const struct attribute_group *serio_device_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	&serio_id_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	&serio_device_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) static void serio_release_port(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	kfree(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  * Prepare serio port for registration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) static void serio_init_port(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	static atomic_t serio_no = ATOMIC_INIT(-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	__module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	INIT_LIST_HEAD(&serio->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	INIT_LIST_HEAD(&serio->child_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	INIT_LIST_HEAD(&serio->children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	spin_lock_init(&serio->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	mutex_init(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	device_initialize(&serio->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	dev_set_name(&serio->dev, "serio%lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		     (unsigned long)atomic_inc_return(&serio_no));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	serio->dev.bus = &serio_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	serio->dev.release = serio_release_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	serio->dev.groups = serio_device_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (serio->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		serio->dev.parent = &serio->parent->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		serio->depth = serio->parent->depth + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		serio->depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	lockdep_set_subclass(&serio->lock, serio->depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  * Complete serio port registration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  * Driver core will attempt to find appropriate driver for the port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) static void serio_add_port(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct serio *parent = serio->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		serio_pause_rx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		list_add_tail(&serio->child_node, &parent->children);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		serio_continue_rx(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	list_add_tail(&serio->node, &serio_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	if (serio->start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		serio->start(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	error = device_add(&serio->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		dev_err(&serio->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			"device_add() failed for %s (%s), error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 			serio->phys, serio->name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  * serio_destroy_port() completes unregistration process and removes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  * port from the system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) static void serio_destroy_port(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	struct serio *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	while ((child = serio_get_pending_child(serio)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		serio_remove_pending_events(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		put_device(&child->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	if (serio->stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		serio->stop(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	if (serio->parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 		serio_pause_rx(serio->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		list_del_init(&serio->child_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		serio_continue_rx(serio->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		serio->parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (device_is_registered(&serio->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		device_del(&serio->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	list_del_init(&serio->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	serio_remove_pending_events(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	put_device(&serio->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581)  * Reconnect serio port (re-initialize attached device).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582)  * If reconnect fails (old device is no longer attached or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583)  * there was no device to begin with) we do full rescan in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584)  * hope of finding a driver for the port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) static int serio_reconnect_port(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	int error = serio_reconnect_driver(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		serio_disconnect_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		serio_find_driver(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599)  * Reconnect serio port and all its children (re-initialize attached
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600)  * devices).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) static void serio_reconnect_subtree(struct serio *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	struct serio *s = root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		error = serio_reconnect_port(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 		if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 			 * Reconnect was successful, move on to do the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 			 * first child.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			if (!list_empty(&s->children)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 				s = list_first_entry(&s->children,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 						     struct serio, child_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 		 * Either it was a leaf node or reconnect failed and it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		 * became a leaf node. Continue reconnecting starting with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		 * the next sibling of the parent node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		while (s != root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 			struct serio *parent = s->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 			if (!list_is_last(&s->child_node, &parent->children)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 				s = list_entry(s->child_node.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 					       struct serio, child_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 			s = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	} while (s != root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641)  * serio_disconnect_port() unbinds a port from its driver. As a side effect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642)  * all children ports are unbound and destroyed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) static void serio_disconnect_port(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	struct serio *s = serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	 * Children ports should be disconnected and destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	 * first; we travel the tree in depth-first order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	while (!list_empty(&serio->children)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		/* Locate a leaf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 		while (!list_empty(&s->children))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 			s = list_first_entry(&s->children,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 					     struct serio, child_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		 * Prune this leaf node unless it is the one we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		 * started with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		if (s != serio) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 			struct serio *parent = s->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			device_release_driver(&s->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			serio_destroy_port(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			s = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	 * OK, no children left, now disconnect this port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	device_release_driver(&serio->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) void serio_rescan(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) EXPORT_SYMBOL(serio_rescan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) void serio_reconnect(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) EXPORT_SYMBOL(serio_reconnect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692)  * Submits register request to kseriod for subsequent execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693)  * Note that port registration is always asynchronous.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) void __serio_register_port(struct serio *serio, struct module *owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	serio_init_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) EXPORT_SYMBOL(__serio_register_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703)  * Synchronously unregisters serio port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) void serio_unregister_port(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	mutex_lock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	serio_disconnect_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	serio_destroy_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	mutex_unlock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) EXPORT_SYMBOL(serio_unregister_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715)  * Safely unregisters children ports if they are present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) void serio_unregister_child_port(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	struct serio *s, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	mutex_lock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	list_for_each_entry_safe(s, next, &serio->children, child_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		serio_disconnect_port(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		serio_destroy_port(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	mutex_unlock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) EXPORT_SYMBOL(serio_unregister_child_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732)  * Serio driver operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) static ssize_t description_show(struct device_driver *drv, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	struct serio_driver *driver = to_serio_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) static DRIVER_ATTR_RO(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) static ssize_t bind_mode_show(struct device_driver *drv, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	struct serio_driver *serio_drv = to_serio_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) static ssize_t bind_mode_store(struct device_driver *drv, const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	struct serio_driver *serio_drv = to_serio_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	retval = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	if (!strncmp(buf, "manual", count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		serio_drv->manual_bind = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	} else if (!strncmp(buf, "auto", count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		serio_drv->manual_bind = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static DRIVER_ATTR_RW(bind_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) static struct attribute *serio_driver_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	&driver_attr_description.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	&driver_attr_bind_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) ATTRIBUTE_GROUPS(serio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) static int serio_driver_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	struct serio_driver *drv = to_serio_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	return serio_connect_driver(serio, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) static int serio_driver_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	serio_disconnect_driver(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) static void serio_cleanup(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	mutex_lock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (serio->drv && serio->drv->cleanup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		serio->drv->cleanup(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	mutex_unlock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) static void serio_shutdown(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	serio_cleanup(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) static void serio_attach_driver(struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	error = driver_attach(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		pr_warn("driver_attach() failed for %s with error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 			drv->driver.name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	bool manual_bind = drv->manual_bind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	drv->driver.bus = &serio_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	drv->driver.owner = owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	drv->driver.mod_name = mod_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	 * Temporarily disable automatic binding because probing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	 * takes long time and we are better off doing it in kseriod
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	drv->manual_bind = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	error = driver_register(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		pr_err("driver_register() failed for %s, error: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			drv->driver.name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	 * Restore original bind mode and let kseriod bind the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	 * driver to free ports
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	if (!manual_bind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		drv->manual_bind = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			driver_unregister(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) EXPORT_SYMBOL(__serio_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) void serio_unregister_driver(struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	struct serio *serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	mutex_lock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	drv->manual_bind = true;	/* so serio_find_driver ignores it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	serio_remove_pending_events(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) start_over:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	list_for_each_entry(serio, &serio_list, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		if (serio->drv == drv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			serio_disconnect_port(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			serio_find_driver(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			/* we could've deleted some ports, restart */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			goto start_over;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	driver_unregister(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	mutex_unlock(&serio_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) EXPORT_SYMBOL(serio_unregister_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	serio_pause_rx(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	serio->drv = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	serio_continue_rx(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) static int serio_bus_match(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	struct serio_driver *serio_drv = to_serio_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	if (serio->manual_bind || serio_drv->manual_bind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	return serio_match_port(serio_drv->id_table, serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) #define SERIO_ADD_UEVENT_VAR(fmt, val...)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	do {								\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		int err = add_uevent_var(env, fmt, val);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		if (err)						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			return err;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	struct serio *serio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 				serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	if (serio->firmware_id[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		SERIO_ADD_UEVENT_VAR("SERIO_FIRMWARE_ID=%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 				     serio->firmware_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) #undef SERIO_ADD_UEVENT_VAR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) static int serio_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	serio_cleanup(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) static int serio_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	struct serio *serio = to_serio_port(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	int error = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	mutex_lock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (serio->drv && serio->drv->fast_reconnect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		error = serio->drv->fast_reconnect(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		if (error && error != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			dev_warn(dev, "fast reconnect failed with error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 				 error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	mutex_unlock(&serio->drv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		 * Driver reconnect can take a while, so better let
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		 * kseriod deal with it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) static const struct dev_pm_ops serio_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	.suspend	= serio_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	.resume		= serio_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	.poweroff	= serio_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	.restore	= serio_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) /* called from serio_driver->connect/disconnect methods under serio_mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) int serio_open(struct serio *serio, struct serio_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	serio_set_drv(serio, drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	if (serio->open && serio->open(serio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		serio_set_drv(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) EXPORT_SYMBOL(serio_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) /* called from serio_driver->connect/disconnect methods under serio_mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) void serio_close(struct serio *serio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	if (serio->close)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		serio->close(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	serio_set_drv(serio, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) EXPORT_SYMBOL(serio_close);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) irqreturn_t serio_interrupt(struct serio *serio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		unsigned char data, unsigned int dfl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	irqreturn_t ret = IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	spin_lock_irqsave(&serio->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)         if (likely(serio->drv)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)                 ret = serio->drv->interrupt(serio, data, dfl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	} else if (!dfl && device_is_registered(&serio->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		serio_rescan(serio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 		ret = IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	spin_unlock_irqrestore(&serio->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) EXPORT_SYMBOL(serio_interrupt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct bus_type serio_bus = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	.name		= "serio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	.drv_groups	= serio_driver_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	.match		= serio_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	.uevent		= serio_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	.probe		= serio_driver_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	.remove		= serio_driver_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	.shutdown	= serio_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	.pm		= &serio_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) EXPORT_SYMBOL(serio_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) static int __init serio_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	error = bus_register(&serio_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		pr_err("Failed to register serio bus, error: %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) static void __exit serio_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	bus_unregister(&serio_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	 * There should not be any outstanding events but work may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	 * still be scheduled so simply cancel it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	cancel_work_sync(&serio_event_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) subsys_initcall(serio_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) module_exit(serio_exit);