xref: /linux/drivers/input/serio/serio.c (revision f24e9f586b377749dff37554696cf3a105540c94)
1 /*
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 
29 #include <linux/stddef.h>
30 #include <linux/module.h>
31 #include <linux/serio.h>
32 #include <linux/errno.h>
33 #include <linux/wait.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
36 #include <linux/kthread.h>
37 #include <linux/mutex.h>
38 
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION("Serio abstraction core");
41 MODULE_LICENSE("GPL");
42 
43 EXPORT_SYMBOL(serio_interrupt);
44 EXPORT_SYMBOL(__serio_register_port);
45 EXPORT_SYMBOL(serio_unregister_port);
46 EXPORT_SYMBOL(serio_unregister_child_port);
47 EXPORT_SYMBOL(__serio_unregister_port_delayed);
48 EXPORT_SYMBOL(__serio_register_driver);
49 EXPORT_SYMBOL(serio_unregister_driver);
50 EXPORT_SYMBOL(serio_open);
51 EXPORT_SYMBOL(serio_close);
52 EXPORT_SYMBOL(serio_rescan);
53 EXPORT_SYMBOL(serio_reconnect);
54 
55 /*
56  * serio_mutex protects entire serio subsystem and is taken every time
57  * serio port or driver registrered or unregistered.
58  */
59 static DEFINE_MUTEX(serio_mutex);
60 
61 static LIST_HEAD(serio_list);
62 
63 static struct bus_type serio_bus;
64 
65 static void serio_add_driver(struct serio_driver *drv);
66 static void serio_add_port(struct serio *serio);
67 static void serio_destroy_port(struct serio *serio);
68 static void serio_reconnect_port(struct serio *serio);
69 static void serio_disconnect_port(struct serio *serio);
70 
71 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
72 {
73 	int retval;
74 
75 	mutex_lock(&serio->drv_mutex);
76 	retval = drv->connect(serio, drv);
77 	mutex_unlock(&serio->drv_mutex);
78 
79 	return retval;
80 }
81 
82 static int serio_reconnect_driver(struct serio *serio)
83 {
84 	int retval = -1;
85 
86 	mutex_lock(&serio->drv_mutex);
87 	if (serio->drv && serio->drv->reconnect)
88 		retval = serio->drv->reconnect(serio);
89 	mutex_unlock(&serio->drv_mutex);
90 
91 	return retval;
92 }
93 
94 static void serio_disconnect_driver(struct serio *serio)
95 {
96 	mutex_lock(&serio->drv_mutex);
97 	if (serio->drv)
98 		serio->drv->disconnect(serio);
99 	mutex_unlock(&serio->drv_mutex);
100 }
101 
102 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
103 {
104 	while (ids->type || ids->proto) {
105 		if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
106 		    (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
107 		    (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
108 		    (ids->id == SERIO_ANY || ids->id == serio->id.id))
109 			return 1;
110 		ids++;
111 	}
112 	return 0;
113 }
114 
115 /*
116  * Basic serio -> driver core mappings
117  */
118 
119 static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
120 {
121 	down_write(&serio_bus.subsys.rwsem);
122 
123 	if (serio_match_port(drv->id_table, serio)) {
124 		serio->dev.driver = &drv->driver;
125 		if (serio_connect_driver(serio, drv)) {
126 			serio->dev.driver = NULL;
127 			goto out;
128 		}
129 		device_bind_driver(&serio->dev);
130 	}
131 out:
132 	up_write(&serio_bus.subsys.rwsem);
133 }
134 
135 static void serio_release_driver(struct serio *serio)
136 {
137 	down_write(&serio_bus.subsys.rwsem);
138 	device_release_driver(&serio->dev);
139 	up_write(&serio_bus.subsys.rwsem);
140 }
141 
142 static void serio_find_driver(struct serio *serio)
143 {
144 	int error;
145 
146 	down_write(&serio_bus.subsys.rwsem);
147 	error = device_attach(&serio->dev);
148 	if (error < 0)
149 		printk(KERN_WARNING
150 			"serio: device_attach() failed for %s (%s), error: %d\n",
151 			serio->phys, serio->name, error);
152 	up_write(&serio_bus.subsys.rwsem);
153 }
154 
155 
156 /*
157  * Serio event processing.
158  */
159 
160 enum serio_event_type {
161 	SERIO_RESCAN,
162 	SERIO_RECONNECT,
163 	SERIO_REGISTER_PORT,
164 	SERIO_UNREGISTER_PORT,
165 	SERIO_REGISTER_DRIVER,
166 };
167 
168 struct serio_event {
169 	enum serio_event_type type;
170 	void *object;
171 	struct module *owner;
172 	struct list_head node;
173 };
174 
175 static DEFINE_SPINLOCK(serio_event_lock);	/* protects serio_event_list */
176 static LIST_HEAD(serio_event_list);
177 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
178 static struct task_struct *serio_task;
179 
180 static void serio_queue_event(void *object, struct module *owner,
181 			      enum serio_event_type event_type)
182 {
183 	unsigned long flags;
184 	struct serio_event *event;
185 
186 	spin_lock_irqsave(&serio_event_lock, flags);
187 
188 	/*
189 	 * Scan event list for the other events for the same serio port,
190 	 * starting with the most recent one. If event is the same we
191 	 * do not need add new one. If event is of different type we
192 	 * need to add this event and should not look further because
193 	 * we need to preseve sequence of distinct events.
194 	 */
195 	list_for_each_entry_reverse(event, &serio_event_list, node) {
196 		if (event->object == object) {
197 			if (event->type == event_type)
198 				goto out;
199 			break;
200 		}
201 	}
202 
203 	if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
204 		if (!try_module_get(owner)) {
205 			printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
206 			kfree(event);
207 			goto out;
208 		}
209 
210 		event->type = event_type;
211 		event->object = object;
212 		event->owner = owner;
213 
214 		list_add_tail(&event->node, &serio_event_list);
215 		wake_up(&serio_wait);
216 	} else {
217 		printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
218 	}
219 out:
220 	spin_unlock_irqrestore(&serio_event_lock, flags);
221 }
222 
223 static void serio_free_event(struct serio_event *event)
224 {
225 	module_put(event->owner);
226 	kfree(event);
227 }
228 
229 static void serio_remove_duplicate_events(struct serio_event *event)
230 {
231 	struct list_head *node, *next;
232 	struct serio_event *e;
233 	unsigned long flags;
234 
235 	spin_lock_irqsave(&serio_event_lock, flags);
236 
237 	list_for_each_safe(node, next, &serio_event_list) {
238 		e = list_entry(node, struct serio_event, node);
239 		if (event->object == e->object) {
240 			/*
241 			 * If this event is of different type we should not
242 			 * look further - we only suppress duplicate events
243 			 * that were sent back-to-back.
244 			 */
245 			if (event->type != e->type)
246 				break;
247 
248 			list_del_init(node);
249 			serio_free_event(e);
250 		}
251 	}
252 
253 	spin_unlock_irqrestore(&serio_event_lock, flags);
254 }
255 
256 
257 static struct serio_event *serio_get_event(void)
258 {
259 	struct serio_event *event;
260 	struct list_head *node;
261 	unsigned long flags;
262 
263 	spin_lock_irqsave(&serio_event_lock, flags);
264 
265 	if (list_empty(&serio_event_list)) {
266 		spin_unlock_irqrestore(&serio_event_lock, flags);
267 		return NULL;
268 	}
269 
270 	node = serio_event_list.next;
271 	event = list_entry(node, struct serio_event, node);
272 	list_del_init(node);
273 
274 	spin_unlock_irqrestore(&serio_event_lock, flags);
275 
276 	return event;
277 }
278 
279 static void serio_handle_event(void)
280 {
281 	struct serio_event *event;
282 
283 	mutex_lock(&serio_mutex);
284 
285 	/*
286 	 * Note that we handle only one event here to give swsusp
287 	 * a chance to freeze kseriod thread. Serio events should
288 	 * be pretty rare so we are not concerned about taking
289 	 * performance hit.
290 	 */
291 	if ((event = serio_get_event())) {
292 
293 		switch (event->type) {
294 			case SERIO_REGISTER_PORT:
295 				serio_add_port(event->object);
296 				break;
297 
298 			case SERIO_UNREGISTER_PORT:
299 				serio_disconnect_port(event->object);
300 				serio_destroy_port(event->object);
301 				break;
302 
303 			case SERIO_RECONNECT:
304 				serio_reconnect_port(event->object);
305 				break;
306 
307 			case SERIO_RESCAN:
308 				serio_disconnect_port(event->object);
309 				serio_find_driver(event->object);
310 				break;
311 
312 			case SERIO_REGISTER_DRIVER:
313 				serio_add_driver(event->object);
314 				break;
315 
316 			default:
317 				break;
318 		}
319 
320 		serio_remove_duplicate_events(event);
321 		serio_free_event(event);
322 	}
323 
324 	mutex_unlock(&serio_mutex);
325 }
326 
327 /*
328  * Remove all events that have been submitted for a given serio port.
329  */
330 static void serio_remove_pending_events(struct serio *serio)
331 {
332 	struct list_head *node, *next;
333 	struct serio_event *event;
334 	unsigned long flags;
335 
336 	spin_lock_irqsave(&serio_event_lock, flags);
337 
338 	list_for_each_safe(node, next, &serio_event_list) {
339 		event = list_entry(node, struct serio_event, node);
340 		if (event->object == serio) {
341 			list_del_init(node);
342 			serio_free_event(event);
343 		}
344 	}
345 
346 	spin_unlock_irqrestore(&serio_event_lock, flags);
347 }
348 
349 /*
350  * Destroy child serio port (if any) that has not been fully registered yet.
351  *
352  * Note that we rely on the fact that port can have only one child and therefore
353  * only one child registration request can be pending. Additionally, children
354  * are registered by driver's connect() handler so there can't be a grandchild
355  * pending registration together with a child.
356  */
357 static struct serio *serio_get_pending_child(struct serio *parent)
358 {
359 	struct serio_event *event;
360 	struct serio *serio, *child = NULL;
361 	unsigned long flags;
362 
363 	spin_lock_irqsave(&serio_event_lock, flags);
364 
365 	list_for_each_entry(event, &serio_event_list, node) {
366 		if (event->type == SERIO_REGISTER_PORT) {
367 			serio = event->object;
368 			if (serio->parent == parent) {
369 				child = serio;
370 				break;
371 			}
372 		}
373 	}
374 
375 	spin_unlock_irqrestore(&serio_event_lock, flags);
376 	return child;
377 }
378 
379 static int serio_thread(void *nothing)
380 {
381 	do {
382 		serio_handle_event();
383 		wait_event_interruptible(serio_wait,
384 			kthread_should_stop() || !list_empty(&serio_event_list));
385 		try_to_freeze();
386 	} while (!kthread_should_stop());
387 
388 	printk(KERN_DEBUG "serio: kseriod exiting\n");
389 	return 0;
390 }
391 
392 
393 /*
394  * Serio port operations
395  */
396 
397 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
398 {
399 	struct serio *serio = to_serio_port(dev);
400 	return sprintf(buf, "%s\n", serio->name);
401 }
402 
403 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
404 {
405 	struct serio *serio = to_serio_port(dev);
406 
407 	return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
408 			serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
409 }
410 
411 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
412 {
413 	struct serio *serio = to_serio_port(dev);
414 	return sprintf(buf, "%02x\n", serio->id.type);
415 }
416 
417 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
418 {
419 	struct serio *serio = to_serio_port(dev);
420 	return sprintf(buf, "%02x\n", serio->id.proto);
421 }
422 
423 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
424 {
425 	struct serio *serio = to_serio_port(dev);
426 	return sprintf(buf, "%02x\n", serio->id.id);
427 }
428 
429 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
430 {
431 	struct serio *serio = to_serio_port(dev);
432 	return sprintf(buf, "%02x\n", serio->id.extra);
433 }
434 
435 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
436 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
437 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
438 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
439 
440 static struct attribute *serio_device_id_attrs[] = {
441 	&dev_attr_type.attr,
442 	&dev_attr_proto.attr,
443 	&dev_attr_id.attr,
444 	&dev_attr_extra.attr,
445 	NULL
446 };
447 
448 static struct attribute_group serio_id_attr_group = {
449 	.name	= "id",
450 	.attrs	= serio_device_id_attrs,
451 };
452 
453 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
454 {
455 	struct serio *serio = to_serio_port(dev);
456 	struct device_driver *drv;
457 	int retval;
458 
459 	retval = mutex_lock_interruptible(&serio_mutex);
460 	if (retval)
461 		return retval;
462 
463 	retval = count;
464 	if (!strncmp(buf, "none", count)) {
465 		serio_disconnect_port(serio);
466 	} else if (!strncmp(buf, "reconnect", count)) {
467 		serio_reconnect_port(serio);
468 	} else if (!strncmp(buf, "rescan", count)) {
469 		serio_disconnect_port(serio);
470 		serio_find_driver(serio);
471 	} else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
472 		serio_disconnect_port(serio);
473 		serio_bind_driver(serio, to_serio_driver(drv));
474 		put_driver(drv);
475 	} else {
476 		retval = -EINVAL;
477 	}
478 
479 	mutex_unlock(&serio_mutex);
480 
481 	return retval;
482 }
483 
484 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
485 {
486 	struct serio *serio = to_serio_port(dev);
487 	return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
488 }
489 
490 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
491 {
492 	struct serio *serio = to_serio_port(dev);
493 	int retval;
494 
495 	retval = count;
496 	if (!strncmp(buf, "manual", count)) {
497 		serio->manual_bind = 1;
498 	} else if (!strncmp(buf, "auto", count)) {
499 		serio->manual_bind = 0;
500 	} else {
501 		retval = -EINVAL;
502 	}
503 
504 	return retval;
505 }
506 
507 static struct device_attribute serio_device_attrs[] = {
508 	__ATTR(description, S_IRUGO, serio_show_description, NULL),
509 	__ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
510 	__ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
511 	__ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
512 	__ATTR_NULL
513 };
514 
515 
516 static void serio_release_port(struct device *dev)
517 {
518 	struct serio *serio = to_serio_port(dev);
519 
520 	kfree(serio);
521 	module_put(THIS_MODULE);
522 }
523 
524 /*
525  * Prepare serio port for registration.
526  */
527 static void serio_init_port(struct serio *serio)
528 {
529 	static atomic_t serio_no = ATOMIC_INIT(0);
530 
531 	__module_get(THIS_MODULE);
532 
533 	INIT_LIST_HEAD(&serio->node);
534 	spin_lock_init(&serio->lock);
535 	mutex_init(&serio->drv_mutex);
536 	device_initialize(&serio->dev);
537 	snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
538 		 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
539 	serio->dev.bus = &serio_bus;
540 	serio->dev.release = serio_release_port;
541 	if (serio->parent)
542 		serio->dev.parent = &serio->parent->dev;
543 }
544 
545 /*
546  * Complete serio port registration.
547  * Driver core will attempt to find appropriate driver for the port.
548  */
549 static void serio_add_port(struct serio *serio)
550 {
551 	int error;
552 
553 	if (serio->parent) {
554 		serio_pause_rx(serio->parent);
555 		serio->parent->child = serio;
556 		serio_continue_rx(serio->parent);
557 	}
558 
559 	list_add_tail(&serio->node, &serio_list);
560 	if (serio->start)
561 		serio->start(serio);
562 	error = device_add(&serio->dev);
563 	if (error)
564 		printk(KERN_ERR
565 			"serio: device_add() failed for %s (%s), error: %d\n",
566 			serio->phys, serio->name, error);
567 	else {
568 		serio->registered = 1;
569 		error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
570 		if (error)
571 			printk(KERN_ERR
572 				"serio: sysfs_create_group() failed for %s (%s), error: %d\n",
573 				serio->phys, serio->name, error);
574 	}
575 }
576 
577 /*
578  * serio_destroy_port() completes deregistration process and removes
579  * port from the system
580  */
581 static void serio_destroy_port(struct serio *serio)
582 {
583 	struct serio *child;
584 
585 	child = serio_get_pending_child(serio);
586 	if (child) {
587 		serio_remove_pending_events(child);
588 		put_device(&child->dev);
589 	}
590 
591 	if (serio->stop)
592 		serio->stop(serio);
593 
594 	if (serio->parent) {
595 		serio_pause_rx(serio->parent);
596 		serio->parent->child = NULL;
597 		serio_continue_rx(serio->parent);
598 		serio->parent = NULL;
599 	}
600 
601 	if (serio->registered) {
602 		sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
603 		device_del(&serio->dev);
604 		serio->registered = 0;
605 	}
606 
607 	list_del_init(&serio->node);
608 	serio_remove_pending_events(serio);
609 	put_device(&serio->dev);
610 }
611 
612 /*
613  * Reconnect serio port and all its children (re-initialize attached devices)
614  */
615 static void serio_reconnect_port(struct serio *serio)
616 {
617 	do {
618 		if (serio_reconnect_driver(serio)) {
619 			serio_disconnect_port(serio);
620 			serio_find_driver(serio);
621 			/* Ok, old children are now gone, we are done */
622 			break;
623 		}
624 		serio = serio->child;
625 	} while (serio);
626 }
627 
628 /*
629  * serio_disconnect_port() unbinds a port from its driver. As a side effect
630  * all child ports are unbound and destroyed.
631  */
632 static void serio_disconnect_port(struct serio *serio)
633 {
634 	struct serio *s, *parent;
635 
636 	if (serio->child) {
637 		/*
638 		 * Children ports should be disconnected and destroyed
639 		 * first, staring with the leaf one, since we don't want
640 		 * to do recursion
641 		 */
642 		for (s = serio; s->child; s = s->child)
643 			/* empty */;
644 
645 		do {
646 			parent = s->parent;
647 
648 			serio_release_driver(s);
649 			serio_destroy_port(s);
650 		} while ((s = parent) != serio);
651 	}
652 
653 	/*
654 	 * Ok, no children left, now disconnect this port
655 	 */
656 	serio_release_driver(serio);
657 }
658 
659 void serio_rescan(struct serio *serio)
660 {
661 	serio_queue_event(serio, NULL, SERIO_RESCAN);
662 }
663 
664 void serio_reconnect(struct serio *serio)
665 {
666 	serio_queue_event(serio, NULL, SERIO_RECONNECT);
667 }
668 
669 /*
670  * Submits register request to kseriod for subsequent execution.
671  * Note that port registration is always asynchronous.
672  */
673 void __serio_register_port(struct serio *serio, struct module *owner)
674 {
675 	serio_init_port(serio);
676 	serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
677 }
678 
679 /*
680  * Synchronously unregisters serio port.
681  */
682 void serio_unregister_port(struct serio *serio)
683 {
684 	mutex_lock(&serio_mutex);
685 	serio_disconnect_port(serio);
686 	serio_destroy_port(serio);
687 	mutex_unlock(&serio_mutex);
688 }
689 
690 /*
691  * Safely unregisters child port if one is present.
692  */
693 void serio_unregister_child_port(struct serio *serio)
694 {
695 	mutex_lock(&serio_mutex);
696 	if (serio->child) {
697 		serio_disconnect_port(serio->child);
698 		serio_destroy_port(serio->child);
699 	}
700 	mutex_unlock(&serio_mutex);
701 }
702 
703 /*
704  * Submits register request to kseriod for subsequent execution.
705  * Can be used when it is not obvious whether the serio_mutex is
706  * taken or not and when delayed execution is feasible.
707  */
708 void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
709 {
710 	serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
711 }
712 
713 
714 /*
715  * Serio driver operations
716  */
717 
718 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
719 {
720 	struct serio_driver *driver = to_serio_driver(drv);
721 	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
722 }
723 
724 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
725 {
726 	struct serio_driver *serio_drv = to_serio_driver(drv);
727 	return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
728 }
729 
730 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
731 {
732 	struct serio_driver *serio_drv = to_serio_driver(drv);
733 	int retval;
734 
735 	retval = count;
736 	if (!strncmp(buf, "manual", count)) {
737 		serio_drv->manual_bind = 1;
738 	} else if (!strncmp(buf, "auto", count)) {
739 		serio_drv->manual_bind = 0;
740 	} else {
741 		retval = -EINVAL;
742 	}
743 
744 	return retval;
745 }
746 
747 
748 static struct driver_attribute serio_driver_attrs[] = {
749 	__ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
750 	__ATTR(bind_mode, S_IWUSR | S_IRUGO,
751 		serio_driver_show_bind_mode, serio_driver_set_bind_mode),
752 	__ATTR_NULL
753 };
754 
755 static int serio_driver_probe(struct device *dev)
756 {
757 	struct serio *serio = to_serio_port(dev);
758 	struct serio_driver *drv = to_serio_driver(dev->driver);
759 
760 	return serio_connect_driver(serio, drv);
761 }
762 
763 static int serio_driver_remove(struct device *dev)
764 {
765 	struct serio *serio = to_serio_port(dev);
766 
767 	serio_disconnect_driver(serio);
768 	return 0;
769 }
770 
771 static struct bus_type serio_bus = {
772 	.name =	"serio",
773 	.probe = serio_driver_probe,
774 	.remove = serio_driver_remove,
775 };
776 
777 static void serio_add_driver(struct serio_driver *drv)
778 {
779 	int error;
780 
781 	error = driver_register(&drv->driver);
782 	if (error)
783 		printk(KERN_ERR
784 			"serio: driver_register() failed for %s, error: %d\n",
785 			drv->driver.name, error);
786 }
787 
788 void __serio_register_driver(struct serio_driver *drv, struct module *owner)
789 {
790 	drv->driver.bus = &serio_bus;
791 
792 	serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
793 }
794 
795 void serio_unregister_driver(struct serio_driver *drv)
796 {
797 	struct serio *serio;
798 
799 	mutex_lock(&serio_mutex);
800 	drv->manual_bind = 1;	/* so serio_find_driver ignores it */
801 
802 start_over:
803 	list_for_each_entry(serio, &serio_list, node) {
804 		if (serio->drv == drv) {
805 			serio_disconnect_port(serio);
806 			serio_find_driver(serio);
807 			/* we could've deleted some ports, restart */
808 			goto start_over;
809 		}
810 	}
811 
812 	driver_unregister(&drv->driver);
813 	mutex_unlock(&serio_mutex);
814 }
815 
816 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
817 {
818 	serio_pause_rx(serio);
819 	serio->drv = drv;
820 	serio_continue_rx(serio);
821 }
822 
823 static int serio_bus_match(struct device *dev, struct device_driver *drv)
824 {
825 	struct serio *serio = to_serio_port(dev);
826 	struct serio_driver *serio_drv = to_serio_driver(drv);
827 
828 	if (serio->manual_bind || serio_drv->manual_bind)
829 		return 0;
830 
831 	return serio_match_port(serio_drv->id_table, serio);
832 }
833 
834 #ifdef CONFIG_HOTPLUG
835 
836 #define SERIO_ADD_UEVENT_VAR(fmt, val...)				\
837 	do {								\
838 		int err = add_uevent_var(envp, num_envp, &i,	\
839 					buffer, buffer_size, &len,	\
840 					fmt, val);			\
841 		if (err)						\
842 			return err;					\
843 	} while (0)
844 
845 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
846 {
847 	struct serio *serio;
848 	int i = 0;
849 	int len = 0;
850 
851 	if (!dev)
852 		return -ENODEV;
853 
854 	serio = to_serio_port(dev);
855 
856 	SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
857 	SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
858 	SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
859 	SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
860 	SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
861 				serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
862 	envp[i] = NULL;
863 
864 	return 0;
865 }
866 #undef SERIO_ADD_UEVENT_VAR
867 
868 #else
869 
870 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
871 {
872 	return -ENODEV;
873 }
874 
875 #endif /* CONFIG_HOTPLUG */
876 
877 static int serio_resume(struct device *dev)
878 {
879 	struct serio *serio = to_serio_port(dev);
880 
881 	if (serio_reconnect_driver(serio)) {
882 		/*
883 		 * Driver re-probing can take a while, so better let kseriod
884 		 * deal with it.
885 		 */
886 		serio_rescan(serio);
887 	}
888 
889 	return 0;
890 }
891 
892 /* called from serio_driver->connect/disconnect methods under serio_mutex */
893 int serio_open(struct serio *serio, struct serio_driver *drv)
894 {
895 	serio_set_drv(serio, drv);
896 
897 	if (serio->open && serio->open(serio)) {
898 		serio_set_drv(serio, NULL);
899 		return -1;
900 	}
901 	return 0;
902 }
903 
904 /* called from serio_driver->connect/disconnect methods under serio_mutex */
905 void serio_close(struct serio *serio)
906 {
907 	if (serio->close)
908 		serio->close(serio);
909 
910 	serio_set_drv(serio, NULL);
911 }
912 
913 irqreturn_t serio_interrupt(struct serio *serio,
914 		unsigned char data, unsigned int dfl, struct pt_regs *regs)
915 {
916 	unsigned long flags;
917 	irqreturn_t ret = IRQ_NONE;
918 
919 	spin_lock_irqsave(&serio->lock, flags);
920 
921         if (likely(serio->drv)) {
922                 ret = serio->drv->interrupt(serio, data, dfl, regs);
923 	} else if (!dfl && serio->registered) {
924 		serio_rescan(serio);
925 		ret = IRQ_HANDLED;
926 	}
927 
928 	spin_unlock_irqrestore(&serio->lock, flags);
929 
930 	return ret;
931 }
932 
933 static int __init serio_init(void)
934 {
935 	int error;
936 
937 	serio_bus.dev_attrs = serio_device_attrs;
938 	serio_bus.drv_attrs = serio_driver_attrs;
939 	serio_bus.match = serio_bus_match;
940 	serio_bus.uevent = serio_uevent;
941 	serio_bus.resume = serio_resume;
942 	error = bus_register(&serio_bus);
943 	if (error) {
944 		printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
945 		return error;
946 	}
947 
948 	serio_task = kthread_run(serio_thread, NULL, "kseriod");
949 	if (IS_ERR(serio_task)) {
950 		bus_unregister(&serio_bus);
951 		error = PTR_ERR(serio_task);
952 		printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
953 		return error;
954 	}
955 
956 	return 0;
957 }
958 
959 static void __exit serio_exit(void)
960 {
961 	bus_unregister(&serio_bus);
962 	kthread_stop(serio_task);
963 }
964 
965 subsys_initcall(serio_init);
966 module_exit(serio_exit);
967