blob: db5b0bca1a1a111041f616edec24817e7ae06266 [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
23 *
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
27 */
28
Dmitry Torokhovcac91692010-01-05 17:56:04 -080029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/stddef.h>
32#include <linux/module.h>
33#include <linux/serio.h>
34#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/slab.h>
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -080037#include <linux/workqueue.h>
Arjan van de Venc4e32e92006-02-19 00:21:55 -050038#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41MODULE_DESCRIPTION("Serio abstraction core");
42MODULE_LICENSE("GPL");
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
Arjan van de Venc4e32e92006-02-19 00:21:55 -050045 * serio_mutex protects entire serio subsystem and is taken every time
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -080046 * serio port or driver registered or unregistered.
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 */
Arjan van de Venc4e32e92006-02-19 00:21:55 -050048static DEFINE_MUTEX(serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static LIST_HEAD(serio_list);
51
Russell King30226f82006-01-05 14:38:53 +000052static struct bus_type serio_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54static void serio_add_port(struct serio *serio);
Shaohua Li6aabcdf2008-07-03 10:45:38 -040055static int serio_reconnect_port(struct serio *serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static void serio_disconnect_port(struct serio *serio);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -070057static void serio_reconnect_subtree(struct serio *serio);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -050058static void serio_attach_driver(struct serio_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds3c803e82005-06-27 17:49:45 -070060static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
61{
62 int retval;
63
Arjan van de Venc4e32e92006-02-19 00:21:55 -050064 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070065 retval = drv->connect(serio, drv);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050066 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070067
68 return retval;
69}
70
71static int serio_reconnect_driver(struct serio *serio)
72{
73 int retval = -1;
74
Arjan van de Venc4e32e92006-02-19 00:21:55 -050075 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070076 if (serio->drv && serio->drv->reconnect)
77 retval = serio->drv->reconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050078 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070079
80 return retval;
81}
82
83static void serio_disconnect_driver(struct serio *serio)
84{
Arjan van de Venc4e32e92006-02-19 00:21:55 -050085 mutex_lock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070086 if (serio->drv)
87 serio->drv->disconnect(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -050088 mutex_unlock(&serio->drv_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -070089}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
92{
93 while (ids->type || ids->proto) {
94 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
95 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
96 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
97 (ids->id == SERIO_ANY || ids->id == serio->id.id))
98 return 1;
99 ids++;
100 }
101 return 0;
102}
103
104/*
105 * Basic serio -> driver core mappings
106 */
107
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400108static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400110 int error;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (serio_match_port(drv->id_table, serio)) {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 serio->dev.driver = &drv->driver;
Linus Torvalds3c803e82005-06-27 17:49:45 -0700115 if (serio_connect_driver(serio, drv)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400117 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400119
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400120 error = device_bind_driver(&serio->dev);
121 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800122 dev_warn(&serio->dev,
123 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
124 serio->phys, serio->name,
125 drv->description, error);
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400126 serio_disconnect_driver(serio);
127 serio->dev.driver = NULL;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400128 return error;
Dmitry Torokhov0a660452006-10-12 01:06:23 -0400129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static void serio_find_driver(struct serio *serio)
135{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400136 int error;
137
Randy Dunlap73b59a32006-07-19 01:14:55 -0400138 error = device_attach(&serio->dev);
139 if (error < 0)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800140 dev_warn(&serio->dev,
141 "device_attach() failed for %s (%s), error: %d\n",
142 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145
146/*
147 * Serio event processing.
148 */
149
150enum serio_event_type {
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500151 SERIO_RESCAN_PORT,
152 SERIO_RECONNECT_PORT,
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700153 SERIO_RECONNECT_SUBTREE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 SERIO_REGISTER_PORT,
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500155 SERIO_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
158struct serio_event {
159 enum serio_event_type type;
160 void *object;
161 struct module *owner;
162 struct list_head node;
163};
164
165static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
166static LIST_HEAD(serio_event_list);
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -0800167
168static struct serio_event *serio_get_event(void)
169{
170 struct serio_event *event = NULL;
171 unsigned long flags;
172
173 spin_lock_irqsave(&serio_event_lock, flags);
174
175 if (!list_empty(&serio_event_list)) {
176 event = list_first_entry(&serio_event_list,
177 struct serio_event, node);
178 list_del_init(&event->node);
179 }
180
181 spin_unlock_irqrestore(&serio_event_lock, flags);
182 return event;
183}
184
185static void serio_free_event(struct serio_event *event)
186{
187 module_put(event->owner);
188 kfree(event);
189}
190
191static void serio_remove_duplicate_events(struct serio_event *event)
192{
193 struct serio_event *e, *next;
194 unsigned long flags;
195
196 spin_lock_irqsave(&serio_event_lock, flags);
197
198 list_for_each_entry_safe(e, next, &serio_event_list, node) {
199 if (event->object == e->object) {
200 /*
201 * If this event is of different type we should not
202 * look further - we only suppress duplicate events
203 * that were sent back-to-back.
204 */
205 if (event->type != e->type)
206 break;
207
208 list_del_init(&e->node);
209 serio_free_event(e);
210 }
211 }
212
213 spin_unlock_irqrestore(&serio_event_lock, flags);
214}
215
216static void serio_handle_event(struct work_struct *work)
217{
218 struct serio_event *event;
219
220 mutex_lock(&serio_mutex);
221
222 while ((event = serio_get_event())) {
223
224 switch (event->type) {
225
226 case SERIO_REGISTER_PORT:
227 serio_add_port(event->object);
228 break;
229
230 case SERIO_RECONNECT_PORT:
231 serio_reconnect_port(event->object);
232 break;
233
234 case SERIO_RESCAN_PORT:
235 serio_disconnect_port(event->object);
236 serio_find_driver(event->object);
237 break;
238
239 case SERIO_RECONNECT_SUBTREE:
240 serio_reconnect_subtree(event->object);
241 break;
242
243 case SERIO_ATTACH_DRIVER:
244 serio_attach_driver(event->object);
245 break;
246 }
247
248 serio_remove_duplicate_events(event);
249 serio_free_event(event);
250 }
251
252 mutex_unlock(&serio_mutex);
253}
254
255static DECLARE_WORK(serio_event_work, serio_handle_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500257static int serio_queue_event(void *object, struct module *owner,
258 enum serio_event_type event_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 unsigned long flags;
261 struct serio_event *event;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500262 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 spin_lock_irqsave(&serio_event_lock, flags);
265
266 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700267 * Scan event list for the other events for the same serio port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * starting with the most recent one. If event is the same we
269 * do not need add new one. If event is of different type we
270 * need to add this event and should not look further because
271 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 list_for_each_entry_reverse(event, &serio_event_list, node) {
274 if (event->object == object) {
275 if (event->type == event_type)
276 goto out;
277 break;
278 }
279 }
280
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500281 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
282 if (!event) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800283 pr_err("Not enough memory to queue event %d\n", event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500284 retval = -ENOMEM;
285 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500287
288 if (!try_module_get(owner)) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800289 pr_warning("Can't get module reference, dropping event %d\n",
290 event_type);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500291 kfree(event);
292 retval = -EINVAL;
293 goto out;
294 }
295
296 event->type = event_type;
297 event->object = object;
298 event->owner = owner;
299
300 list_add_tail(&event->node, &serio_event_list);
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -0800301 schedule_work(&serio_event_work);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303out:
304 spin_unlock_irqrestore(&serio_event_lock, flags);
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500305 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/*
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400309 * Remove all events that have been submitted for a given
310 * object, be it serio port or driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400312static void serio_remove_pending_events(void *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800314 struct serio_event *event, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned long flags;
316
317 spin_lock_irqsave(&serio_event_lock, flags);
318
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800319 list_for_each_entry_safe(event, next, &serio_event_list, node) {
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400320 if (event->object == object) {
Dmitry Torokhov4516c812010-01-05 17:56:04 -0800321 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 serio_free_event(event);
323 }
324 }
325
326 spin_unlock_irqrestore(&serio_event_lock, flags);
327}
328
329/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700330 * Locate child serio port (if any) that has not been fully registered yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700332 * Children are registered by driver's connect() handler so there can't be a
333 * grandchild pending registration together with a child.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335static struct serio *serio_get_pending_child(struct serio *parent)
336{
337 struct serio_event *event;
338 struct serio *serio, *child = NULL;
339 unsigned long flags;
340
341 spin_lock_irqsave(&serio_event_lock, flags);
342
343 list_for_each_entry(event, &serio_event_list, node) {
344 if (event->type == SERIO_REGISTER_PORT) {
345 serio = event->object;
346 if (serio->parent == parent) {
347 child = serio;
348 break;
349 }
350 }
351 }
352
353 spin_unlock_irqrestore(&serio_event_lock, flags);
354 return child;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/*
358 * Serio port operations
359 */
360
Yani Ioannoue404e272005-05-17 06:42:58 -0400361static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct serio *serio = to_serio_port(dev);
364 return sprintf(buf, "%s\n", serio->name);
365}
366
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500367static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
368{
369 struct serio *serio = to_serio_port(dev);
370
371 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
372 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
373}
374
Yani Ioannoue404e272005-05-17 06:42:58 -0400375static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 struct serio *serio = to_serio_port(dev);
378 return sprintf(buf, "%02x\n", serio->id.type);
379}
380
Yani Ioannoue404e272005-05-17 06:42:58 -0400381static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct serio *serio = to_serio_port(dev);
384 return sprintf(buf, "%02x\n", serio->id.proto);
385}
386
Yani Ioannoue404e272005-05-17 06:42:58 -0400387static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct serio *serio = to_serio_port(dev);
390 return sprintf(buf, "%02x\n", serio->id.id);
391}
392
Yani Ioannoue404e272005-05-17 06:42:58 -0400393static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 struct serio *serio = to_serio_port(dev);
396 return sprintf(buf, "%02x\n", serio->id.extra);
397}
398
Dmitry Torokhovbaae9562005-05-16 21:53:09 -0700399static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
400static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
401static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
402static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
403
404static struct attribute *serio_device_id_attrs[] = {
405 &dev_attr_type.attr,
406 &dev_attr_proto.attr,
407 &dev_attr_id.attr,
408 &dev_attr_extra.attr,
409 NULL
410};
411
412static struct attribute_group serio_id_attr_group = {
413 .name = "id",
414 .attrs = serio_device_id_attrs,
415};
416
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800417static const struct attribute_group *serio_device_attr_groups[] = {
418 &serio_id_attr_group,
419 NULL
420};
421
Yani Ioannoue404e272005-05-17 06:42:58 -0400422static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct serio *serio = to_serio_port(dev);
425 struct device_driver *drv;
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400426 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400428 error = mutex_lock_interruptible(&serio_mutex);
429 if (error)
430 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!strncmp(buf, "none", count)) {
433 serio_disconnect_port(serio);
434 } else if (!strncmp(buf, "reconnect", count)) {
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700435 serio_reconnect_subtree(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 } else if (!strncmp(buf, "rescan", count)) {
437 serio_disconnect_port(serio);
438 serio_find_driver(serio);
439 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
440 serio_disconnect_port(serio);
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400441 error = serio_bind_driver(serio, to_serio_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 put_driver(drv);
443 } else {
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400444 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500447 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400449 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Yani Ioannoue404e272005-05-17 06:42:58 -0400452static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct serio *serio = to_serio_port(dev);
455 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
456}
457
Yani Ioannoue404e272005-05-17 06:42:58 -0400458static 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 -0700459{
460 struct serio *serio = to_serio_port(dev);
461 int retval;
462
463 retval = count;
464 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700465 serio->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700467 serio->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } else {
469 retval = -EINVAL;
470 }
471
472 return retval;
473}
474
475static struct device_attribute serio_device_attrs[] = {
476 __ATTR(description, S_IRUGO, serio_show_description, NULL),
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500477 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
479 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
480 __ATTR_NULL
481};
482
483
484static void serio_release_port(struct device *dev)
485{
486 struct serio *serio = to_serio_port(dev);
487
488 kfree(serio);
489 module_put(THIS_MODULE);
490}
491
492/*
493 * Prepare serio port for registration.
494 */
495static void serio_init_port(struct serio *serio)
496{
497 static atomic_t serio_no = ATOMIC_INIT(0);
498
499 __module_get(THIS_MODULE);
500
Randy Dunlap73b59a32006-07-19 01:14:55 -0400501 INIT_LIST_HEAD(&serio->node);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700502 INIT_LIST_HEAD(&serio->child_node);
503 INIT_LIST_HEAD(&serio->children);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 spin_lock_init(&serio->lock);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500505 mutex_init(&serio->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 device_initialize(&serio->dev);
Kay Sieversa6c24902008-10-30 00:07:50 -0400507 dev_set_name(&serio->dev, "serio%ld",
508 (long)atomic_inc_return(&serio_no) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 serio->dev.bus = &serio_bus;
510 serio->dev.release = serio_release_port;
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800511 serio->dev.groups = serio_device_attr_groups;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400512 if (serio->parent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 serio->dev.parent = &serio->parent->dev;
Jiri Kosina88aa0102006-10-11 01:45:31 -0400514 serio->depth = serio->parent->depth + 1;
515 } else
516 serio->depth = 0;
517 lockdep_set_subclass(&serio->lock, serio->depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520/*
521 * Complete serio port registration.
522 * Driver core will attempt to find appropriate driver for the port.
523 */
524static void serio_add_port(struct serio *serio)
525{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700526 struct serio *parent = serio->parent;
Randy Dunlap73b59a32006-07-19 01:14:55 -0400527 int error;
528
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700529 if (parent) {
530 serio_pause_rx(parent);
531 list_add_tail(&serio->child_node, &parent->children);
532 serio_continue_rx(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 list_add_tail(&serio->node, &serio_list);
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (serio->start)
538 serio->start(serio);
Dmitry Torokhov386d8772010-01-05 17:56:03 -0800539
Randy Dunlap73b59a32006-07-19 01:14:55 -0400540 error = device_add(&serio->dev);
541 if (error)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800542 dev_err(&serio->dev,
543 "device_add() failed for %s (%s), error: %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400544 serio->phys, serio->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
547/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700548 * serio_destroy_port() completes unregistration process and removes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * port from the system
550 */
551static void serio_destroy_port(struct serio *serio)
552{
553 struct serio *child;
554
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700555 while ((child = serio_get_pending_child(serio)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 serio_remove_pending_events(child);
557 put_device(&child->dev);
558 }
559
560 if (serio->stop)
561 serio->stop(serio);
562
563 if (serio->parent) {
564 serio_pause_rx(serio->parent);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700565 list_del_init(&serio->child_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 serio_continue_rx(serio->parent);
567 serio->parent = NULL;
568 }
569
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -0800570 if (device_is_registered(&serio->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 device_del(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Randy Dunlap73b59a32006-07-19 01:14:55 -0400573 list_del_init(&serio->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 serio_remove_pending_events(serio);
575 put_device(&serio->dev);
576}
577
578/*
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400579 * Reconnect serio port (re-initialize attached device).
580 * If reconnect fails (old device is no longer attached or
581 * there was no device to begin with) we do full rescan in
582 * hope of finding a driver for the port.
583 */
584static int serio_reconnect_port(struct serio *serio)
585{
586 int error = serio_reconnect_driver(serio);
587
588 if (error) {
589 serio_disconnect_port(serio);
590 serio_find_driver(serio);
591 }
592
593 return error;
594}
595
596/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700597 * Reconnect serio port and all its children (re-initialize attached
598 * devices).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 */
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700600static void serio_reconnect_subtree(struct serio *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700602 struct serio *s = root;
603 int error;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 do {
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700606 error = serio_reconnect_port(s);
607 if (!error) {
608 /*
609 * Reconnect was successful, move on to do the
610 * first child.
611 */
612 if (!list_empty(&s->children)) {
613 s = list_first_entry(&s->children,
614 struct serio, child_node);
615 continue;
616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700618
619 /*
620 * Either it was a leaf node or reconnect failed and it
621 * became a leaf node. Continue reconnecting starting with
622 * the next sibling of the parent node.
623 */
624 while (s != root) {
625 struct serio *parent = s->parent;
626
627 if (!list_is_last(&s->child_node, &parent->children)) {
628 s = list_entry(s->child_node.next,
629 struct serio, child_node);
630 break;
631 }
632
633 s = parent;
634 }
635 } while (s != root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
638/*
639 * serio_disconnect_port() unbinds a port from its driver. As a side effect
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700640 * all children ports are unbound and destroyed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
642static void serio_disconnect_port(struct serio *serio)
643{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700644 struct serio *s = serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700646 /*
647 * Children ports should be disconnected and destroyed
648 * first; we travel the tree in depth-first order.
649 */
650 while (!list_empty(&serio->children)) {
651
652 /* Locate a leaf */
653 while (!list_empty(&s->children))
654 s = list_first_entry(&s->children,
655 struct serio, child_node);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700658 * Prune this leaf node unless it is the one we
659 * started with.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700661 if (s != serio) {
662 struct serio *parent = s->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400664 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 serio_destroy_port(s);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700666
667 s = parent;
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670
671 /*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700672 * OK, no children left, now disconnect this port.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 */
Dmitry Torokhov70f256f2007-04-10 00:40:27 -0400674 device_release_driver(&serio->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677void serio_rescan(struct serio *serio)
678{
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500679 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700681EXPORT_SYMBOL(serio_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683void serio_reconnect(struct serio *serio)
684{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700685 serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700687EXPORT_SYMBOL(serio_reconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689/*
690 * Submits register request to kseriod for subsequent execution.
691 * Note that port registration is always asynchronous.
692 */
693void __serio_register_port(struct serio *serio, struct module *owner)
694{
695 serio_init_port(serio);
696 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
697}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700698EXPORT_SYMBOL(__serio_register_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700/*
701 * Synchronously unregisters serio port.
702 */
703void serio_unregister_port(struct serio *serio)
704{
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500705 mutex_lock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 serio_disconnect_port(serio);
707 serio_destroy_port(serio);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500708 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700710EXPORT_SYMBOL(serio_unregister_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712/*
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700713 * Safely unregisters children ports if they are present.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700714 */
715void serio_unregister_child_port(struct serio *serio)
716{
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700717 struct serio *s, *next;
718
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500719 mutex_lock(&serio_mutex);
Dmitry Eremin-Solenikov09822582010-10-04 21:46:10 -0700720 list_for_each_entry_safe(s, next, &serio->children, child_node) {
721 serio_disconnect_port(s);
722 serio_destroy_port(s);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700723 }
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500724 mutex_unlock(&serio_mutex);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700725}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700726EXPORT_SYMBOL(serio_unregister_child_port);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729/*
730 * Serio driver operations
731 */
732
733static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
734{
735 struct serio_driver *driver = to_serio_driver(drv);
736 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
737}
738
739static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
740{
741 struct serio_driver *serio_drv = to_serio_driver(drv);
742 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
743}
744
745static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
746{
747 struct serio_driver *serio_drv = to_serio_driver(drv);
748 int retval;
749
750 retval = count;
751 if (!strncmp(buf, "manual", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700752 serio_drv->manual_bind = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 } else if (!strncmp(buf, "auto", count)) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700754 serio_drv->manual_bind = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 } else {
756 retval = -EINVAL;
757 }
758
759 return retval;
760}
761
762
763static struct driver_attribute serio_driver_attrs[] = {
764 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
765 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
766 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
767 __ATTR_NULL
768};
769
770static int serio_driver_probe(struct device *dev)
771{
772 struct serio *serio = to_serio_port(dev);
773 struct serio_driver *drv = to_serio_driver(dev->driver);
774
Linus Torvalds3c803e82005-06-27 17:49:45 -0700775 return serio_connect_driver(serio, drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778static int serio_driver_remove(struct device *dev)
779{
780 struct serio *serio = to_serio_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Linus Torvalds3c803e82005-06-27 17:49:45 -0700782 serio_disconnect_driver(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784}
785
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500786static void serio_cleanup(struct serio *serio)
787{
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400788 mutex_lock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500789 if (serio->drv && serio->drv->cleanup)
790 serio->drv->cleanup(serio);
Dmitry Torokhov33143ea2007-06-29 01:06:35 -0400791 mutex_unlock(&serio->drv_mutex);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500792}
793
794static void serio_shutdown(struct device *dev)
795{
796 struct serio *serio = to_serio_port(dev);
797
798 serio_cleanup(serio);
799}
800
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500801static void serio_attach_driver(struct serio_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400802{
803 int error;
804
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500805 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400806 if (error)
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800807 pr_warning("driver_attach() failed for %s with error %d\n",
808 drv->driver.name, error);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400809}
810
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800811int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700813 bool manual_bind = drv->manual_bind;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500814 int error;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 drv->driver.bus = &serio_bus;
Greg Kroah-Hartman4b315622007-01-15 11:50:02 -0800817 drv->driver.owner = owner;
818 drv->driver.mod_name = mod_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500820 /*
821 * Temporarily disable automatic binding because probing
822 * takes long time and we are better off doing it in kseriod
823 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700824 drv->manual_bind = true;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500825
826 error = driver_register(&drv->driver);
827 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -0800828 pr_err("driver_register() failed for %s, error: %d\n",
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500829 drv->driver.name, error);
830 return error;
831 }
832
833 /*
834 * Restore original bind mode and let kseriod bind the
835 * driver to free ports
836 */
837 if (!manual_bind) {
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700838 drv->manual_bind = false;
Dmitry Torokhoved7b1f62006-11-23 23:34:49 -0500839 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
840 if (error) {
841 driver_unregister(&drv->driver);
842 return error;
843 }
844 }
845
846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700848EXPORT_SYMBOL(__serio_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850void serio_unregister_driver(struct serio_driver *drv)
851{
852 struct serio *serio;
853
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500854 mutex_lock(&serio_mutex);
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400855
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700856 drv->manual_bind = true; /* so serio_find_driver ignores it */
Dmitry Torokhove8ef4342008-06-02 00:41:57 -0400857 serio_remove_pending_events(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859start_over:
860 list_for_each_entry(serio, &serio_list, node) {
861 if (serio->drv == drv) {
862 serio_disconnect_port(serio);
863 serio_find_driver(serio);
864 /* we could've deleted some ports, restart */
865 goto start_over;
866 }
867 }
868
869 driver_unregister(&drv->driver);
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500870 mutex_unlock(&serio_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700872EXPORT_SYMBOL(serio_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 serio_pause_rx(serio);
877 serio->drv = drv;
878 serio_continue_rx(serio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static int serio_bus_match(struct device *dev, struct device_driver *drv)
882{
883 struct serio *serio = to_serio_port(dev);
884 struct serio_driver *serio_drv = to_serio_driver(drv);
885
886 if (serio->manual_bind || serio_drv->manual_bind)
887 return 0;
888
889 return serio_match_port(serio_drv->id_table, serio);
890}
891
892#ifdef CONFIG_HOTPLUG
893
Kay Sievers312c0042005-11-16 09:00:00 +0100894#define SERIO_ADD_UEVENT_VAR(fmt, val...) \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500895 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200896 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500897 if (err) \
898 return err; \
899 } while (0)
900
Kay Sievers7eff2e72007-08-14 15:15:12 +0200901static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
903 struct serio *serio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (!dev)
906 return -ENODEV;
907
908 serio = to_serio_port(dev);
909
Kay Sievers312c0042005-11-16 09:00:00 +0100910 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
911 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
912 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
913 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
914 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
Dmitry Torokhovae87dff2005-06-30 00:48:34 -0500915 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 return 0;
918}
Kay Sievers312c0042005-11-16 09:00:00 +0100919#undef SERIO_ADD_UEVENT_VAR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921#else
922
Kay Sievers7eff2e72007-08-14 15:15:12 +0200923static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
925 return -ENODEV;
926}
927
928#endif /* CONFIG_HOTPLUG */
929
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500930#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700931static int serio_suspend(struct device *dev)
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500932{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700933 struct serio *serio = to_serio_port(dev);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500934
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700935 serio_cleanup(serio);
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500936
937 return 0;
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940static int serio_resume(struct device *dev)
941{
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700942 struct serio *serio = to_serio_port(dev);
943
Shaohua Li6aabcdf2008-07-03 10:45:38 -0400944 /*
945 * Driver reconnect can take a while, so better let kseriod
946 * deal with it.
947 */
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700948 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 return 0;
951}
Dmitry Torokhov633aae22009-07-22 21:51:36 -0700952
953static const struct dev_pm_ops serio_pm_ops = {
954 .suspend = serio_suspend,
955 .resume = serio_resume,
956 .poweroff = serio_suspend,
957 .restore = serio_resume,
958};
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -0500959#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500961/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962int serio_open(struct serio *serio, struct serio_driver *drv)
963{
964 serio_set_drv(serio, drv);
965
966 if (serio->open && serio->open(serio)) {
967 serio_set_drv(serio, NULL);
968 return -1;
969 }
970 return 0;
971}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700972EXPORT_SYMBOL(serio_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Arjan van de Venc4e32e92006-02-19 00:21:55 -0500974/* called from serio_driver->connect/disconnect methods under serio_mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975void serio_close(struct serio *serio)
976{
977 if (serio->close)
978 serio->close(serio);
979
980 serio_set_drv(serio, NULL);
981}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -0700982EXPORT_SYMBOL(serio_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984irqreturn_t serio_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100985 unsigned char data, unsigned int dfl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
987 unsigned long flags;
988 irqreturn_t ret = IRQ_NONE;
989
990 spin_lock_irqsave(&serio->lock, flags);
991
992 if (likely(serio->drv)) {
David Howells7d12e782006-10-05 14:55:46 +0100993 ret = serio->drv->interrupt(serio, data, dfl);
Dmitry Torokhovddf1ffb2010-01-05 17:56:04 -0800994 } else if (!dfl && device_is_registered(&serio->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 serio_rescan(serio);
996 ret = IRQ_HANDLED;
997 }
998
999 spin_unlock_irqrestore(&serio->lock, flags);
1000
1001 return ret;
1002}
Dmitry Torokhov89b09b92009-04-17 20:12:34 -07001003EXPORT_SYMBOL(serio_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001005static struct bus_type serio_bus = {
1006 .name = "serio",
1007 .dev_attrs = serio_device_attrs,
1008 .drv_attrs = serio_driver_attrs,
1009 .match = serio_bus_match,
1010 .uevent = serio_uevent,
1011 .probe = serio_driver_probe,
1012 .remove = serio_driver_remove,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001013 .shutdown = serio_shutdown,
1014#ifdef CONFIG_PM
Dmitry Torokhov633aae22009-07-22 21:51:36 -07001015 .pm = &serio_pm_ops,
Dmitry Torokhov82dd9ef2007-02-18 01:40:30 -05001016#endif
Marton Nemeth1ea2a692006-11-02 23:27:21 -05001017};
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019static int __init serio_init(void)
1020{
Randy Dunlap73b59a32006-07-19 01:14:55 -04001021 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Randy Dunlap73b59a32006-07-19 01:14:55 -04001023 error = bus_register(&serio_bus);
1024 if (error) {
Dmitry Torokhovcac91692010-01-05 17:56:04 -08001025 pr_err("Failed to register serio bus, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -04001026 return error;
1027 }
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return 0;
1030}
1031
1032static void __exit serio_exit(void)
1033{
1034 bus_unregister(&serio_bus);
Dmitry Torokhov8ee294c2010-11-15 01:39:57 -08001035
1036 /*
1037 * There should not be any outstanding events but work may
1038 * still be scheduled so simply cancel it.
1039 */
1040 cancel_work_sync(&serio_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Dmitry Torokhov51c38f92006-02-19 00:22:51 -05001043subsys_initcall(serio_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044module_exit(serio_exit);