blob: 2e1fb0649260a5e79ade82f36995a63f9ebd383a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The Serio abstraction module
3 *
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
6 * Copyright (c) 2003 Daniele Bellucci
7 */
8
9/*
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Dmitry Torokhovcac91692010-01-05 17:56:04 -080025#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/stddef.h>
28#include <linux/module.h>
29#include <linux/serio.h>
30#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -080033#include <linux/workqueue.h>
Arjan van de Venc4e32e92006-02-19 00:21:55 -050034#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
37MODULE_DESCRIPTION("Serio abstraction core");
38MODULE_LICENSE("GPL");
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050041 * serio_mutex protects entire serio subsystem and is taken every time
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -080042 * serio port or driver registered or unregistered.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050044static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46static LIST_HEAD(serio_list);
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static void serio_add_port(struct serio *serio);
Shaohua Li6aabcdf2008-07-03 10:45:38 -040049static int serio_reconnect_port(struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static void serio_disconnect_port(struct serio *serio);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -070051static void serio_reconnect_subtree(struct serio *serio);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050052static void serio_attach_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds3c803e82005-06-27 17:49:45 -070054static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
55{
56 int retval;
57
Arjan van de Venc4e32e92006-02-19 00:21:55 -050058 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070059 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050060 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070061
62 return retval;
63}
64
65static int serio_reconnect_driver(struct serio *serio)
66{
67 int retval = -1;
68
Arjan van de Venc4e32e92006-02-19 00:21:55 -050069 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070070 if (serio->drv && serio->drv->reconnect)
71 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050072 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070073
74 return retval;
75}
76
77static void serio_disconnect_driver(struct serio *serio)
78{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050079 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070080 if (serio->drv)
81 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050082 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070083}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
86{
87 while (ids->type || ids->proto) {
88 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
89 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
90 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
91 (ids->id == SERIO_ANY || ids->id == serio->id.id))
92 return 1;
93 ids++;
94 }
95 return 0;
96}
97
98/*
99 * Basic serio -> driver core mappings
100 */
101
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400102static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400104 int error;
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (serio_match_port(drv->id_table, serio)) {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700109 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400111 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400113
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400114 error = device_bind_driver(&serio->dev);
115 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800116 dev_warn(&serio->dev,
117 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
118 serio->phys, serio->name,
119 drv->description, error);
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400120 serio_disconnect_driver(serio);
121 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400122 return error;
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static void serio_find_driver(struct serio *serio)
129{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400130 int error;
131
Randy Dunlap73b59a32006-07-19 01:14:55 -0400132 error = device_attach(&serio->dev);
Grygorii Strashko015bb5e2016-01-16 10:35:47 -0800133 if (error < 0 && error != -EPROBE_DEFER)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800134 dev_warn(&serio->dev,
135 "device_attach() failed for %s (%s), error: %d\n",
136 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139
140/*
141 * Serio event processing.
142 */
143
144enum serio_event_type {
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500145 SERIO_RESCAN_PORT,
146 SERIO_RECONNECT_PORT,
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700147 SERIO_RECONNECT_SUBTREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 SERIO_REGISTER_PORT,
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500149 SERIO_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152struct serio_event {
153 enum serio_event_type type;
154 void *object;
155 struct module *owner;
156 struct list_head node;
157};
158
159static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
160static LIST_HEAD(serio_event_list);
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -0800161
162static struct serio_event *serio_get_event(void)
163{
164 struct serio_event *event = NULL;
165 unsigned long flags;
166
167 spin_lock_irqsave(&serio_event_lock, flags);
168
169 if (!list_empty(&serio_event_list)) {
170 event = list_first_entry(&serio_event_list,
171 struct serio_event, node);
172 list_del_init(&event->node);
173 }
174
175 spin_unlock_irqrestore(&serio_event_lock, flags);
176 return event;
177}
178
179static void serio_free_event(struct serio_event *event)
180{
181 module_put(event->owner);
182 kfree(event);
183}
184
Duncan Laurie19e95542011-02-02 22:59:54 -0800185static void serio_remove_duplicate_events(void *object,
186 enum serio_event_type type)
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -0800187{
188 struct serio_event *e, *next;
189 unsigned long flags;
190
191 spin_lock_irqsave(&serio_event_lock, flags);
192
193 list_for_each_entry_safe(e, next, &serio_event_list, node) {
Duncan Laurie19e95542011-02-02 22:59:54 -0800194 if (object == e->object) {
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -0800195 /*
196 * If this event is of different type we should not
197 * look further - we only suppress duplicate events
198 * that were sent back-to-back.
199 */
Duncan Laurie19e95542011-02-02 22:59:54 -0800200 if (type != e->type)
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -0800201 break;
202
203 list_del_init(&e->node);
204 serio_free_event(e);
205 }
206 }
207
208 spin_unlock_irqrestore(&serio_event_lock, flags);
209}
210
211static void serio_handle_event(struct work_struct *work)
212{
213 struct serio_event *event;
214
215 mutex_lock(&serio_mutex);
216
217 while ((event = serio_get_event())) {
218
219 switch (event->type) {
220
221 case SERIO_REGISTER_PORT:
222 serio_add_port(event->object);
223 break;
224
225 case SERIO_RECONNECT_PORT:
226 serio_reconnect_port(event->object);
227 break;
228
229 case SERIO_RESCAN_PORT:
230 serio_disconnect_port(event->object);
231 serio_find_driver(event->object);
232 break;
233
234 case SERIO_RECONNECT_SUBTREE:
235 serio_reconnect_subtree(event->object);
236 break;
237
238 case SERIO_ATTACH_DRIVER:
239 serio_attach_driver(event->object);
240 break;
241 }
242
Duncan Laurie19e95542011-02-02 22:59:54 -0800243 serio_remove_duplicate_events(event->object, event->type);
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -0800244 serio_free_event(event);
245 }
246
247 mutex_unlock(&serio_mutex);
248}
249
250static DECLARE_WORK(serio_event_work, serio_handle_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500252static int serio_queue_event(void *object, struct module *owner,
253 enum serio_event_type event_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 unsigned long flags;
256 struct serio_event *event;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500257 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 spin_lock_irqsave(&serio_event_lock, flags);
260
261 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700262 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * starting with the most recent one. If event is the same we
264 * do not need add new one. If event is of different type we
265 * need to add this event and should not look further because
266 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 list_for_each_entry_reverse(event, &serio_event_list, node) {
269 if (event->object == object) {
270 if (event->type == event_type)
271 goto out;
272 break;
273 }
274 }
275
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500276 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
277 if (!event) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800278 pr_err("Not enough memory to queue event %d\n", event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500279 retval = -ENOMEM;
280 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500282
283 if (!try_module_get(owner)) {
Joe Perchesfef5f562017-03-17 17:15:38 -0700284 pr_warn("Can't get module reference, dropping event %d\n",
285 event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500286 kfree(event);
287 retval = -EINVAL;
288 goto out;
289 }
290
291 event->type = event_type;
292 event->object = object;
293 event->owner = owner;
294
295 list_add_tail(&event->node, &serio_event_list);
Dmitry Torokhov1d64b652011-02-23 08:51:28 -0800296 queue_work(system_long_wq, &serio_event_work);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298out:
299 spin_unlock_irqrestore(&serio_event_lock, flags);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500300 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400304 * Remove all events that have been submitted for a given
305 * object, be it serio port or driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400307static void serio_remove_pending_events(void *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800309 struct serio_event *event, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned long flags;
311
312 spin_lock_irqsave(&serio_event_lock, flags);
313
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800314 list_for_each_entry_safe(event, next, &serio_event_list, node) {
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400315 if (event->object == object) {
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800316 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 serio_free_event(event);
318 }
319 }
320
321 spin_unlock_irqrestore(&serio_event_lock, flags);
322}
323
324/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700325 * Locate child serio port (if any) that has not been fully registered yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700327 * Children are registered by driver's connect() handler so there can't be a
328 * grandchild pending registration together with a child.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330static struct serio *serio_get_pending_child(struct serio *parent)
331{
332 struct serio_event *event;
333 struct serio *serio, *child = NULL;
334 unsigned long flags;
335
336 spin_lock_irqsave(&serio_event_lock, flags);
337
338 list_for_each_entry(event, &serio_event_list, node) {
339 if (event->type == SERIO_REGISTER_PORT) {
340 serio = event->object;
341 if (serio->parent == parent) {
342 child = serio;
343 break;
344 }
345 }
346 }
347
348 spin_unlock_irqrestore(&serio_event_lock, flags);
349 return child;
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*
353 * Serio port operations
354 */
355
Yani Ioannoue404e272005-05-17 06:42:58 -0400356static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct serio *serio = to_serio_port(dev);
359 return sprintf(buf, "%s\n", serio->name);
360}
361
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700362static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500363{
364 struct serio *serio = to_serio_port(dev);
365
366 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
367 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
368}
369
Greg Kroah-Hartman7eab8de2013-10-07 18:08:23 -0700370static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct serio *serio = to_serio_port(dev);
373 return sprintf(buf, "%02x\n", serio->id.type);
374}
375
Greg Kroah-Hartman7eab8de2013-10-07 18:08:23 -0700376static ssize_t proto_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct serio *serio = to_serio_port(dev);
379 return sprintf(buf, "%02x\n", serio->id.proto);
380}
381
Greg Kroah-Hartman7eab8de2013-10-07 18:08:23 -0700382static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 struct serio *serio = to_serio_port(dev);
385 return sprintf(buf, "%02x\n", serio->id.id);
386}
387
Greg Kroah-Hartman7eab8de2013-10-07 18:08:23 -0700388static ssize_t extra_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct serio *serio = to_serio_port(dev);
391 return sprintf(buf, "%02x\n", serio->id.extra);
392}
393
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700394static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct serio *serio = to_serio_port(dev);
397 struct device_driver *drv;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400398 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400400 error = mutex_lock_interruptible(&serio_mutex);
401 if (error)
402 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (!strncmp(buf, "none", count)) {
405 serio_disconnect_port(serio);
406 } else if (!strncmp(buf, "reconnect", count)) {
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700407 serio_reconnect_subtree(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 } else if (!strncmp(buf, "rescan", count)) {
409 serio_disconnect_port(serio);
410 serio_find_driver(serio);
Duncan Laurie19e95542011-02-02 22:59:54 -0800411 serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
413 serio_disconnect_port(serio);
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400414 error = serio_bind_driver(serio, to_serio_driver(drv));
Duncan Laurie19e95542011-02-02 22:59:54 -0800415 serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 } else {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400417 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500420 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400422 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Yani Ioannoue404e272005-05-17 06:42:58 -0400425static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct serio *serio = to_serio_port(dev);
428 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
429}
430
Yani Ioannoue404e272005-05-17 06:42:58 -0400431static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 struct serio *serio = to_serio_port(dev);
434 int retval;
435
436 retval = count;
437 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700438 serio->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700440 serio->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 } else {
442 retval = -EINVAL;
443 }
444
445 return retval;
446}
447
Hans de Goede0456c662014-04-19 20:39:35 -0700448static ssize_t firmware_id_show(struct device *dev, struct device_attribute *attr, char *buf)
449{
450 struct serio *serio = to_serio_port(dev);
451
452 return sprintf(buf, "%s\n", serio->firmware_id);
453}
454
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700455static DEVICE_ATTR_RO(type);
456static DEVICE_ATTR_RO(proto);
457static DEVICE_ATTR_RO(id);
458static DEVICE_ATTR_RO(extra);
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700459
460static struct attribute *serio_device_id_attrs[] = {
461 &dev_attr_type.attr,
462 &dev_attr_proto.attr,
463 &dev_attr_id.attr,
464 &dev_attr_extra.attr,
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700465 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466};
467
Arvind Yadav547a915d2017-07-10 20:14:59 -0700468static const struct attribute_group serio_id_attr_group = {
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700469 .name = "id",
470 .attrs = serio_device_id_attrs,
471};
472
Dmitry Torokhove696c682013-12-07 07:13:56 -0800473static DEVICE_ATTR_RO(modalias);
474static DEVICE_ATTR_WO(drvctl);
475static DEVICE_ATTR(description, S_IRUGO, serio_show_description, NULL);
476static DEVICE_ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode);
Hans de Goede0456c662014-04-19 20:39:35 -0700477static DEVICE_ATTR_RO(firmware_id);
Dmitry Torokhove696c682013-12-07 07:13:56 -0800478
479static struct attribute *serio_device_attrs[] = {
480 &dev_attr_modalias.attr,
481 &dev_attr_description.attr,
482 &dev_attr_drvctl.attr,
483 &dev_attr_bind_mode.attr,
Hans de Goede0456c662014-04-19 20:39:35 -0700484 &dev_attr_firmware_id.attr,
Dmitry Torokhove696c682013-12-07 07:13:56 -0800485 NULL
486};
487
Arvind Yadav547a915d2017-07-10 20:14:59 -0700488static const struct attribute_group serio_device_attr_group = {
Dmitry Torokhove696c682013-12-07 07:13:56 -0800489 .attrs = serio_device_attrs,
490};
491
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700492static const struct attribute_group *serio_device_attr_groups[] = {
493 &serio_id_attr_group,
Dmitry Torokhove696c682013-12-07 07:13:56 -0800494 &serio_device_attr_group,
Greg Kroah-Hartman3778a2122013-10-07 18:09:08 -0700495 NULL
496};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498static void serio_release_port(struct device *dev)
499{
500 struct serio *serio = to_serio_port(dev);
501
502 kfree(serio);
503 module_put(THIS_MODULE);
504}
505
506/*
507 * Prepare serio port for registration.
508 */
509static void serio_init_port(struct serio *serio)
510{
Aniroop Mathur939ffb12014-12-03 14:27:42 -0800511 static atomic_t serio_no = ATOMIC_INIT(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 __module_get(THIS_MODULE);
514
Randy Dunlap73b59a32006-07-19 01:14:55 -0400515 INIT_LIST_HEAD(&serio->node);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700516 INIT_LIST_HEAD(&serio->child_node);
517 INIT_LIST_HEAD(&serio->children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500519 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 device_initialize(&serio->dev);
Richard Leitner0224ec92014-10-08 15:21:32 -0700521 dev_set_name(&serio->dev, "serio%lu",
Aniroop Mathur939ffb12014-12-03 14:27:42 -0800522 (unsigned long)atomic_inc_return(&serio_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 serio->dev.bus = &serio_bus;
524 serio->dev.release = serio_release_port;
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800525 serio->dev.groups = serio_device_attr_groups;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400526 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400528 serio->depth = serio->parent->depth + 1;
529 } else
530 serio->depth = 0;
531 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
534/*
535 * Complete serio port registration.
536 * Driver core will attempt to find appropriate driver for the port.
537 */
538static void serio_add_port(struct serio *serio)
539{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700540 struct serio *parent = serio->parent;
Randy Dunlap73b59a32006-07-19 01:14:55 -0400541 int error;
542
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700543 if (parent) {
544 serio_pause_rx(parent);
545 list_add_tail(&serio->child_node, &parent->children);
546 serio_continue_rx(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 list_add_tail(&serio->node, &serio_list);
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (serio->start)
552 serio->start(serio);
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800553
Randy Dunlap73b59a32006-07-19 01:14:55 -0400554 error = device_add(&serio->dev);
555 if (error)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800556 dev_err(&serio->dev,
557 "device_add() failed for %s (%s), error: %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400558 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700562 * serio_destroy_port() completes unregistration process and removes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * port from the system
564 */
565static void serio_destroy_port(struct serio *serio)
566{
567 struct serio *child;
568
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700569 while ((child = serio_get_pending_child(serio)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 serio_remove_pending_events(child);
571 put_device(&child->dev);
572 }
573
574 if (serio->stop)
575 serio->stop(serio);
576
577 if (serio->parent) {
578 serio_pause_rx(serio->parent);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700579 list_del_init(&serio->child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 serio_continue_rx(serio->parent);
581 serio->parent = NULL;
582 }
583
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -0800584 if (device_is_registered(&serio->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Randy Dunlap73b59a32006-07-19 01:14:55 -0400587 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 serio_remove_pending_events(serio);
589 put_device(&serio->dev);
590}
591
592/*
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400593 * Reconnect serio port (re-initialize attached device).
594 * If reconnect fails (old device is no longer attached or
595 * there was no device to begin with) we do full rescan in
596 * hope of finding a driver for the port.
597 */
598static int serio_reconnect_port(struct serio *serio)
599{
600 int error = serio_reconnect_driver(serio);
601
602 if (error) {
603 serio_disconnect_port(serio);
604 serio_find_driver(serio);
605 }
606
607 return error;
608}
609
610/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700611 * Reconnect serio port and all its children (re-initialize attached
612 * devices).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700614static void serio_reconnect_subtree(struct serio *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700616 struct serio *s = root;
617 int error;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 do {
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700620 error = serio_reconnect_port(s);
621 if (!error) {
622 /*
623 * Reconnect was successful, move on to do the
624 * first child.
625 */
626 if (!list_empty(&s->children)) {
627 s = list_first_entry(&s->children,
628 struct serio, child_node);
629 continue;
630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700632
633 /*
634 * Either it was a leaf node or reconnect failed and it
635 * became a leaf node. Continue reconnecting starting with
636 * the next sibling of the parent node.
637 */
638 while (s != root) {
639 struct serio *parent = s->parent;
640
641 if (!list_is_last(&s->child_node, &parent->children)) {
642 s = list_entry(s->child_node.next,
643 struct serio, child_node);
644 break;
645 }
646
647 s = parent;
648 }
649 } while (s != root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/*
653 * serio_disconnect_port() unbinds a port from its driver. As a side effect
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700654 * all children ports are unbound and destroyed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
656static void serio_disconnect_port(struct serio *serio)
657{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700658 struct serio *s = serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700660 /*
661 * Children ports should be disconnected and destroyed
662 * first; we travel the tree in depth-first order.
663 */
664 while (!list_empty(&serio->children)) {
665
666 /* Locate a leaf */
667 while (!list_empty(&s->children))
668 s = list_first_entry(&s->children,
669 struct serio, child_node);
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700672 * Prune this leaf node unless it is the one we
673 * started with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700675 if (s != serio) {
676 struct serio *parent = s->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400678 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 serio_destroy_port(s);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700680
681 s = parent;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 /*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700686 * OK, no children left, now disconnect this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400688 device_release_driver(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691void serio_rescan(struct serio *serio)
692{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500693 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700695EXPORT_SYMBOL(serio_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697void serio_reconnect(struct serio *serio)
698{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700699 serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700701EXPORT_SYMBOL(serio_reconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703/*
704 * Submits register request to kseriod for subsequent execution.
705 * Note that port registration is always asynchronous.
706 */
707void __serio_register_port(struct serio *serio, struct module *owner)
708{
709 serio_init_port(serio);
710 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
711}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700712EXPORT_SYMBOL(__serio_register_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714/*
715 * Synchronously unregisters serio port.
716 */
717void serio_unregister_port(struct serio *serio)
718{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500719 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 serio_disconnect_port(serio);
721 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500722 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700724EXPORT_SYMBOL(serio_unregister_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700727 * Safely unregisters children ports if they are present.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700728 */
729void serio_unregister_child_port(struct serio *serio)
730{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700731 struct serio *s, *next;
732
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500733 mutex_lock(&serio_mutex);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700734 list_for_each_entry_safe(s, next, &serio->children, child_node) {
735 serio_disconnect_port(s);
736 serio_destroy_port(s);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700737 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500738 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700739}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700740EXPORT_SYMBOL(serio_unregister_child_port);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743/*
744 * Serio driver operations
745 */
746
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -0700747static ssize_t description_show(struct device_driver *drv, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct serio_driver *driver = to_serio_driver(drv);
750 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
751}
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -0700752static DRIVER_ATTR_RO(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -0700754static ssize_t bind_mode_show(struct device_driver *drv, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct serio_driver *serio_drv = to_serio_driver(drv);
757 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
758}
759
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -0700760static ssize_t bind_mode_store(struct device_driver *drv, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct serio_driver *serio_drv = to_serio_driver(drv);
763 int retval;
764
765 retval = count;
766 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700767 serio_drv->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700769 serio_drv->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 } else {
771 retval = -EINVAL;
772 }
773
774 return retval;
775}
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -0700776static DRIVER_ATTR_RW(bind_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -0700778static struct attribute *serio_driver_attrs[] = {
779 &driver_attr_description.attr,
780 &driver_attr_bind_mode.attr,
781 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782};
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -0700783ATTRIBUTE_GROUPS(serio_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785static int serio_driver_probe(struct device *dev)
786{
787 struct serio *serio = to_serio_port(dev);
788 struct serio_driver *drv = to_serio_driver(dev->driver);
789
Linus Torvalds3c803e82005-06-27 17:49:45 -0700790 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793static int serio_driver_remove(struct device *dev)
794{
795 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds3c803e82005-06-27 17:49:45 -0700797 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return 0;
799}
800
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500801static void serio_cleanup(struct serio *serio)
802{
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400803 mutex_lock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500804 if (serio->drv && serio->drv->cleanup)
805 serio->drv->cleanup(serio);
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400806 mutex_unlock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500807}
808
809static void serio_shutdown(struct device *dev)
810{
811 struct serio *serio = to_serio_port(dev);
812
813 serio_cleanup(serio);
814}
815
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500816static void serio_attach_driver(struct serio_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400817{
818 int error;
819
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500820 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400821 if (error)
Joe Perchesfef5f562017-03-17 17:15:38 -0700822 pr_warn("driver_attach() failed for %s with error %d\n",
823 drv->driver.name, error);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400824}
825
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800826int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700828 bool manual_bind = drv->manual_bind;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500829 int error;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 drv->driver.bus = &serio_bus;
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800832 drv->driver.owner = owner;
833 drv->driver.mod_name = mod_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500835 /*
836 * Temporarily disable automatic binding because probing
837 * takes long time and we are better off doing it in kseriod
838 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700839 drv->manual_bind = true;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500840
841 error = driver_register(&drv->driver);
842 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800843 pr_err("driver_register() failed for %s, error: %d\n",
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500844 drv->driver.name, error);
845 return error;
846 }
847
848 /*
849 * Restore original bind mode and let kseriod bind the
850 * driver to free ports
851 */
852 if (!manual_bind) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700853 drv->manual_bind = false;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500854 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
855 if (error) {
856 driver_unregister(&drv->driver);
857 return error;
858 }
859 }
860
861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700863EXPORT_SYMBOL(__serio_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865void serio_unregister_driver(struct serio_driver *drv)
866{
867 struct serio *serio;
868
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500869 mutex_lock(&serio_mutex);
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400870
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700871 drv->manual_bind = true; /* so serio_find_driver ignores it */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400872 serio_remove_pending_events(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874start_over:
875 list_for_each_entry(serio, &serio_list, node) {
876 if (serio->drv == drv) {
877 serio_disconnect_port(serio);
878 serio_find_driver(serio);
879 /* we could've deleted some ports, restart */
880 goto start_over;
881 }
882 }
883
884 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500885 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700887EXPORT_SYMBOL(serio_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
890{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 serio_pause_rx(serio);
892 serio->drv = drv;
893 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
896static int serio_bus_match(struct device *dev, struct device_driver *drv)
897{
898 struct serio *serio = to_serio_port(dev);
899 struct serio_driver *serio_drv = to_serio_driver(drv);
900
901 if (serio->manual_bind || serio_drv->manual_bind)
902 return 0;
903
904 return serio_match_port(serio_drv->id_table, serio);
905}
906
Kay Sievers312c0042005-11-16 09:00:00 +0100907#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500908 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200909 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500910 if (err) \
911 return err; \
912 } while (0)
913
Kay Sievers7eff2e72007-08-14 15:15:12 +0200914static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (!dev)
919 return -ENODEV;
920
921 serio = to_serio_port(dev);
922
Kay Sievers312c0042005-11-16 09:00:00 +0100923 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
924 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
925 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
926 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
Hans de Goede0456c662014-04-19 20:39:35 -0700927
Kay Sievers312c0042005-11-16 09:00:00 +0100928 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500929 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Hans de Goede0456c662014-04-19 20:39:35 -0700931 if (serio->firmware_id[0])
932 SERIO_ADD_UEVENT_VAR("SERIO_FIRMWARE_ID=%s",
933 serio->firmware_id);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return 0;
936}
Kay Sievers312c0042005-11-16 09:00:00 +0100937#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500939#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700940static int serio_suspend(struct device *dev)
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500941{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700942 struct serio *serio = to_serio_port(dev);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500943
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700944 serio_cleanup(serio);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500945
946 return 0;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949static int serio_resume(struct device *dev)
950{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700951 struct serio *serio = to_serio_port(dev);
Dmitry Torokhov5ea13202017-03-03 11:47:40 -0800952 int error = -ENOENT;
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700953
Dmitry Torokhov5ea13202017-03-03 11:47:40 -0800954 mutex_lock(&serio->drv_mutex);
955 if (serio->drv && serio->drv->fast_reconnect) {
956 error = serio->drv->fast_reconnect(serio);
957 if (error && error != -ENOENT)
958 dev_warn(dev, "fast reconnect failed with error %d\n",
959 error);
960 }
961 mutex_unlock(&serio->drv_mutex);
962
963 if (error) {
964 /*
965 * Driver reconnect can take a while, so better let
966 * kseriod deal with it.
967 */
968 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 return 0;
972}
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700973
974static const struct dev_pm_ops serio_pm_ops = {
975 .suspend = serio_suspend,
976 .resume = serio_resume,
977 .poweroff = serio_suspend,
978 .restore = serio_resume,
979};
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500980#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500982/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983int serio_open(struct serio *serio, struct serio_driver *drv)
984{
985 serio_set_drv(serio, drv);
986
987 if (serio->open && serio->open(serio)) {
988 serio_set_drv(serio, NULL);
989 return -1;
990 }
991 return 0;
992}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700993EXPORT_SYMBOL(serio_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500995/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996void serio_close(struct serio *serio)
997{
998 if (serio->close)
999 serio->close(serio);
1000
1001 serio_set_drv(serio, NULL);
1002}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -07001003EXPORT_SYMBOL(serio_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +01001006 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 unsigned long flags;
1009 irqreturn_t ret = IRQ_NONE;
1010
1011 spin_lock_irqsave(&serio->lock, flags);
1012
1013 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +01001014 ret = serio->drv->interrupt(serio, data, dfl);
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -08001015 } else if (!dfl && device_is_registered(&serio->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 serio_rescan(serio);
1017 ret = IRQ_HANDLED;
1018 }
1019
1020 spin_unlock_irqrestore(&serio->lock, flags);
1021
1022 return ret;
1023}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -07001024EXPORT_SYMBOL(serio_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001026struct bus_type serio_bus = {
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001027 .name = "serio",
Greg Kroah-Hartman7048f5d2013-08-23 14:24:34 -07001028 .drv_groups = serio_driver_groups,
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001029 .match = serio_bus_match,
1030 .uevent = serio_uevent,
1031 .probe = serio_driver_probe,
1032 .remove = serio_driver_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001033 .shutdown = serio_shutdown,
1034#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -07001035 .pm = &serio_pm_ops,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001036#endif
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001037};
Stephen Chandler Paule1443d22015-07-15 10:20:17 -07001038EXPORT_SYMBOL(serio_bus);
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040static int __init serio_init(void)
1041{
Randy Dunlap73b59a32006-07-19 01:14:55 -04001042 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Randy Dunlap73b59a32006-07-19 01:14:55 -04001044 error = bus_register(&serio_bus);
1045 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -08001046 pr_err("Failed to register serio bus, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -04001047 return error;
1048 }
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return 0;
1051}
1052
1053static void __exit serio_exit(void)
1054{
1055 bus_unregister(&serio_bus);
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -08001056
1057 /*
1058 * There should not be any outstanding events but work may
1059 * still be scheduled so simply cancel it.
1060 */
1061 cancel_work_sync(&serio_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Dmitry Torokhov51c38f92006-02-19 00:22:51 -05001064subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065module_exit(serio_exit);