of: Add new helper of_parse_phandles_with_args()
[linux-2.6/libata-dev.git] / drivers / input / serio / serio.c
blob2f12d60eee3b4e61f4b93363f5458189d471b0c1
1 /*
2 * The Serio abstraction module
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
6 * Copyright (c) 2003 Daniele Bellucci
7 */
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.
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.
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
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
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 #include <linux/freezer.h>
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("Serio abstraction core");
42 MODULE_LICENSE("GPL");
44 EXPORT_SYMBOL(serio_interrupt);
45 EXPORT_SYMBOL(__serio_register_port);
46 EXPORT_SYMBOL(serio_unregister_port);
47 EXPORT_SYMBOL(serio_unregister_child_port);
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);
56 * serio_mutex protects entire serio subsystem and is taken every time
57 * serio port or driver registrered or unregistered.
59 static DEFINE_MUTEX(serio_mutex);
61 static LIST_HEAD(serio_list);
63 static struct bus_type serio_bus;
65 static void serio_add_port(struct serio *serio);
66 static int serio_reconnect_port(struct serio *serio);
67 static void serio_disconnect_port(struct serio *serio);
68 static void serio_reconnect_chain(struct serio *serio);
69 static void serio_attach_driver(struct serio_driver *drv);
71 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
73 int retval;
75 mutex_lock(&serio->drv_mutex);
76 retval = drv->connect(serio, drv);
77 mutex_unlock(&serio->drv_mutex);
79 return retval;
82 static int serio_reconnect_driver(struct serio *serio)
84 int retval = -1;
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);
91 return retval;
94 static void serio_disconnect_driver(struct serio *serio)
96 mutex_lock(&serio->drv_mutex);
97 if (serio->drv)
98 serio->drv->disconnect(serio);
99 mutex_unlock(&serio->drv_mutex);
102 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
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++;
112 return 0;
116 * Basic serio -> driver core mappings
119 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
121 int error;
123 if (serio_match_port(drv->id_table, serio)) {
125 serio->dev.driver = &drv->driver;
126 if (serio_connect_driver(serio, drv)) {
127 serio->dev.driver = NULL;
128 return -ENODEV;
131 error = device_bind_driver(&serio->dev);
132 if (error) {
133 printk(KERN_WARNING
134 "serio: device_bind_driver() failed "
135 "for %s (%s) and %s, error: %d\n",
136 serio->phys, serio->name,
137 drv->description, error);
138 serio_disconnect_driver(serio);
139 serio->dev.driver = NULL;
140 return error;
143 return 0;
146 static void serio_find_driver(struct serio *serio)
148 int error;
150 error = device_attach(&serio->dev);
151 if (error < 0)
152 printk(KERN_WARNING
153 "serio: device_attach() failed for %s (%s), error: %d\n",
154 serio->phys, serio->name, error);
159 * Serio event processing.
162 enum serio_event_type {
163 SERIO_RESCAN_PORT,
164 SERIO_RECONNECT_PORT,
165 SERIO_RECONNECT_CHAIN,
166 SERIO_REGISTER_PORT,
167 SERIO_ATTACH_DRIVER,
170 struct serio_event {
171 enum serio_event_type type;
172 void *object;
173 struct module *owner;
174 struct list_head node;
177 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
178 static LIST_HEAD(serio_event_list);
179 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
180 static struct task_struct *serio_task;
182 static int serio_queue_event(void *object, struct module *owner,
183 enum serio_event_type event_type)
185 unsigned long flags;
186 struct serio_event *event;
187 int retval = 0;
189 spin_lock_irqsave(&serio_event_lock, flags);
192 * Scan event list for the other events for the same serio port,
193 * starting with the most recent one. If event is the same we
194 * do not need add new one. If event is of different type we
195 * need to add this event and should not look further because
196 * we need to preseve sequence of distinct events.
198 list_for_each_entry_reverse(event, &serio_event_list, node) {
199 if (event->object == object) {
200 if (event->type == event_type)
201 goto out;
202 break;
206 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
207 if (!event) {
208 printk(KERN_ERR
209 "serio: Not enough memory to queue event %d\n",
210 event_type);
211 retval = -ENOMEM;
212 goto out;
215 if (!try_module_get(owner)) {
216 printk(KERN_WARNING
217 "serio: Can't get module reference, dropping event %d\n",
218 event_type);
219 kfree(event);
220 retval = -EINVAL;
221 goto out;
224 event->type = event_type;
225 event->object = object;
226 event->owner = owner;
228 list_add_tail(&event->node, &serio_event_list);
229 wake_up(&serio_wait);
231 out:
232 spin_unlock_irqrestore(&serio_event_lock, flags);
233 return retval;
236 static void serio_free_event(struct serio_event *event)
238 module_put(event->owner);
239 kfree(event);
242 static void serio_remove_duplicate_events(struct serio_event *event)
244 struct list_head *node, *next;
245 struct serio_event *e;
246 unsigned long flags;
248 spin_lock_irqsave(&serio_event_lock, flags);
250 list_for_each_safe(node, next, &serio_event_list) {
251 e = list_entry(node, struct serio_event, node);
252 if (event->object == e->object) {
254 * If this event is of different type we should not
255 * look further - we only suppress duplicate events
256 * that were sent back-to-back.
258 if (event->type != e->type)
259 break;
261 list_del_init(node);
262 serio_free_event(e);
266 spin_unlock_irqrestore(&serio_event_lock, flags);
270 static struct serio_event *serio_get_event(void)
272 struct serio_event *event;
273 struct list_head *node;
274 unsigned long flags;
276 spin_lock_irqsave(&serio_event_lock, flags);
278 if (list_empty(&serio_event_list)) {
279 spin_unlock_irqrestore(&serio_event_lock, flags);
280 return NULL;
283 node = serio_event_list.next;
284 event = list_entry(node, struct serio_event, node);
285 list_del_init(node);
287 spin_unlock_irqrestore(&serio_event_lock, flags);
289 return event;
292 static void serio_handle_event(void)
294 struct serio_event *event;
296 mutex_lock(&serio_mutex);
299 * Note that we handle only one event here to give swsusp
300 * a chance to freeze kseriod thread. Serio events should
301 * be pretty rare so we are not concerned about taking
302 * performance hit.
304 if ((event = serio_get_event())) {
306 switch (event->type) {
307 case SERIO_REGISTER_PORT:
308 serio_add_port(event->object);
309 break;
311 case SERIO_RECONNECT_PORT:
312 serio_reconnect_port(event->object);
313 break;
315 case SERIO_RESCAN_PORT:
316 serio_disconnect_port(event->object);
317 serio_find_driver(event->object);
318 break;
320 case SERIO_RECONNECT_CHAIN:
321 serio_reconnect_chain(event->object);
322 break;
324 case SERIO_ATTACH_DRIVER:
325 serio_attach_driver(event->object);
326 break;
328 default:
329 break;
332 serio_remove_duplicate_events(event);
333 serio_free_event(event);
336 mutex_unlock(&serio_mutex);
340 * Remove all events that have been submitted for a given
341 * object, be it serio port or driver.
343 static void serio_remove_pending_events(void *object)
345 struct list_head *node, *next;
346 struct serio_event *event;
347 unsigned long flags;
349 spin_lock_irqsave(&serio_event_lock, flags);
351 list_for_each_safe(node, next, &serio_event_list) {
352 event = list_entry(node, struct serio_event, node);
353 if (event->object == object) {
354 list_del_init(node);
355 serio_free_event(event);
359 spin_unlock_irqrestore(&serio_event_lock, flags);
363 * Destroy child serio port (if any) that has not been fully registered yet.
365 * Note that we rely on the fact that port can have only one child and therefore
366 * only one child registration request can be pending. Additionally, children
367 * are registered by driver's connect() handler so there can't be a grandchild
368 * pending registration together with a child.
370 static struct serio *serio_get_pending_child(struct serio *parent)
372 struct serio_event *event;
373 struct serio *serio, *child = NULL;
374 unsigned long flags;
376 spin_lock_irqsave(&serio_event_lock, flags);
378 list_for_each_entry(event, &serio_event_list, node) {
379 if (event->type == SERIO_REGISTER_PORT) {
380 serio = event->object;
381 if (serio->parent == parent) {
382 child = serio;
383 break;
388 spin_unlock_irqrestore(&serio_event_lock, flags);
389 return child;
392 static int serio_thread(void *nothing)
394 set_freezable();
395 do {
396 serio_handle_event();
397 wait_event_freezable(serio_wait,
398 kthread_should_stop() || !list_empty(&serio_event_list));
399 } while (!kthread_should_stop());
401 printk(KERN_DEBUG "serio: kseriod exiting\n");
402 return 0;
407 * Serio port operations
410 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
412 struct serio *serio = to_serio_port(dev);
413 return sprintf(buf, "%s\n", serio->name);
416 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
418 struct serio *serio = to_serio_port(dev);
420 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
421 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
424 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
426 struct serio *serio = to_serio_port(dev);
427 return sprintf(buf, "%02x\n", serio->id.type);
430 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
432 struct serio *serio = to_serio_port(dev);
433 return sprintf(buf, "%02x\n", serio->id.proto);
436 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
438 struct serio *serio = to_serio_port(dev);
439 return sprintf(buf, "%02x\n", serio->id.id);
442 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
444 struct serio *serio = to_serio_port(dev);
445 return sprintf(buf, "%02x\n", serio->id.extra);
448 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
449 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
450 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
451 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
453 static struct attribute *serio_device_id_attrs[] = {
454 &dev_attr_type.attr,
455 &dev_attr_proto.attr,
456 &dev_attr_id.attr,
457 &dev_attr_extra.attr,
458 NULL
461 static struct attribute_group serio_id_attr_group = {
462 .name = "id",
463 .attrs = serio_device_id_attrs,
466 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
468 struct serio *serio = to_serio_port(dev);
469 struct device_driver *drv;
470 int error;
472 error = mutex_lock_interruptible(&serio_mutex);
473 if (error)
474 return error;
476 if (!strncmp(buf, "none", count)) {
477 serio_disconnect_port(serio);
478 } else if (!strncmp(buf, "reconnect", count)) {
479 serio_reconnect_chain(serio);
480 } else if (!strncmp(buf, "rescan", count)) {
481 serio_disconnect_port(serio);
482 serio_find_driver(serio);
483 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
484 serio_disconnect_port(serio);
485 error = serio_bind_driver(serio, to_serio_driver(drv));
486 put_driver(drv);
487 } else {
488 error = -EINVAL;
491 mutex_unlock(&serio_mutex);
493 return error ? error : count;
496 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
498 struct serio *serio = to_serio_port(dev);
499 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
502 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
504 struct serio *serio = to_serio_port(dev);
505 int retval;
507 retval = count;
508 if (!strncmp(buf, "manual", count)) {
509 serio->manual_bind = 1;
510 } else if (!strncmp(buf, "auto", count)) {
511 serio->manual_bind = 0;
512 } else {
513 retval = -EINVAL;
516 return retval;
519 static struct device_attribute serio_device_attrs[] = {
520 __ATTR(description, S_IRUGO, serio_show_description, NULL),
521 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
522 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
523 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
524 __ATTR_NULL
528 static void serio_release_port(struct device *dev)
530 struct serio *serio = to_serio_port(dev);
532 kfree(serio);
533 module_put(THIS_MODULE);
537 * Prepare serio port for registration.
539 static void serio_init_port(struct serio *serio)
541 static atomic_t serio_no = ATOMIC_INIT(0);
543 __module_get(THIS_MODULE);
545 INIT_LIST_HEAD(&serio->node);
546 spin_lock_init(&serio->lock);
547 mutex_init(&serio->drv_mutex);
548 device_initialize(&serio->dev);
549 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
550 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
551 serio->dev.bus = &serio_bus;
552 serio->dev.release = serio_release_port;
553 if (serio->parent) {
554 serio->dev.parent = &serio->parent->dev;
555 serio->depth = serio->parent->depth + 1;
556 } else
557 serio->depth = 0;
558 lockdep_set_subclass(&serio->lock, serio->depth);
562 * Complete serio port registration.
563 * Driver core will attempt to find appropriate driver for the port.
565 static void serio_add_port(struct serio *serio)
567 int error;
569 if (serio->parent) {
570 serio_pause_rx(serio->parent);
571 serio->parent->child = serio;
572 serio_continue_rx(serio->parent);
575 list_add_tail(&serio->node, &serio_list);
576 if (serio->start)
577 serio->start(serio);
578 error = device_add(&serio->dev);
579 if (error)
580 printk(KERN_ERR
581 "serio: device_add() failed for %s (%s), error: %d\n",
582 serio->phys, serio->name, error);
583 else {
584 serio->registered = 1;
585 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
586 if (error)
587 printk(KERN_ERR
588 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
589 serio->phys, serio->name, error);
594 * serio_destroy_port() completes deregistration process and removes
595 * port from the system
597 static void serio_destroy_port(struct serio *serio)
599 struct serio *child;
601 child = serio_get_pending_child(serio);
602 if (child) {
603 serio_remove_pending_events(child);
604 put_device(&child->dev);
607 if (serio->stop)
608 serio->stop(serio);
610 if (serio->parent) {
611 serio_pause_rx(serio->parent);
612 serio->parent->child = NULL;
613 serio_continue_rx(serio->parent);
614 serio->parent = NULL;
617 if (serio->registered) {
618 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
619 device_del(&serio->dev);
620 serio->registered = 0;
623 list_del_init(&serio->node);
624 serio_remove_pending_events(serio);
625 put_device(&serio->dev);
629 * Reconnect serio port (re-initialize attached device).
630 * If reconnect fails (old device is no longer attached or
631 * there was no device to begin with) we do full rescan in
632 * hope of finding a driver for the port.
634 static int serio_reconnect_port(struct serio *serio)
636 int error = serio_reconnect_driver(serio);
638 if (error) {
639 serio_disconnect_port(serio);
640 serio_find_driver(serio);
643 return error;
647 * Reconnect serio port and all its children (re-initialize attached devices)
649 static void serio_reconnect_chain(struct serio *serio)
651 do {
652 if (serio_reconnect_port(serio)) {
653 /* Ok, old children are now gone, we are done */
654 break;
656 serio = serio->child;
657 } while (serio);
661 * serio_disconnect_port() unbinds a port from its driver. As a side effect
662 * all child ports are unbound and destroyed.
664 static void serio_disconnect_port(struct serio *serio)
666 struct serio *s, *parent;
668 if (serio->child) {
670 * Children ports should be disconnected and destroyed
671 * first, staring with the leaf one, since we don't want
672 * to do recursion
674 for (s = serio; s->child; s = s->child)
675 /* empty */;
677 do {
678 parent = s->parent;
680 device_release_driver(&s->dev);
681 serio_destroy_port(s);
682 } while ((s = parent) != serio);
686 * Ok, no children left, now disconnect this port
688 device_release_driver(&serio->dev);
691 void serio_rescan(struct serio *serio)
693 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
696 void serio_reconnect(struct serio *serio)
698 serio_queue_event(serio, NULL, SERIO_RECONNECT_CHAIN);
702 * Submits register request to kseriod for subsequent execution.
703 * Note that port registration is always asynchronous.
705 void __serio_register_port(struct serio *serio, struct module *owner)
707 serio_init_port(serio);
708 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
712 * Synchronously unregisters serio port.
714 void serio_unregister_port(struct serio *serio)
716 mutex_lock(&serio_mutex);
717 serio_disconnect_port(serio);
718 serio_destroy_port(serio);
719 mutex_unlock(&serio_mutex);
723 * Safely unregisters child port if one is present.
725 void serio_unregister_child_port(struct serio *serio)
727 mutex_lock(&serio_mutex);
728 if (serio->child) {
729 serio_disconnect_port(serio->child);
730 serio_destroy_port(serio->child);
732 mutex_unlock(&serio_mutex);
737 * Serio driver operations
740 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
742 struct serio_driver *driver = to_serio_driver(drv);
743 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
746 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
748 struct serio_driver *serio_drv = to_serio_driver(drv);
749 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
752 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
754 struct serio_driver *serio_drv = to_serio_driver(drv);
755 int retval;
757 retval = count;
758 if (!strncmp(buf, "manual", count)) {
759 serio_drv->manual_bind = 1;
760 } else if (!strncmp(buf, "auto", count)) {
761 serio_drv->manual_bind = 0;
762 } else {
763 retval = -EINVAL;
766 return retval;
770 static struct driver_attribute serio_driver_attrs[] = {
771 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
772 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
773 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
774 __ATTR_NULL
777 static int serio_driver_probe(struct device *dev)
779 struct serio *serio = to_serio_port(dev);
780 struct serio_driver *drv = to_serio_driver(dev->driver);
782 return serio_connect_driver(serio, drv);
785 static int serio_driver_remove(struct device *dev)
787 struct serio *serio = to_serio_port(dev);
789 serio_disconnect_driver(serio);
790 return 0;
793 static void serio_cleanup(struct serio *serio)
795 mutex_lock(&serio->drv_mutex);
796 if (serio->drv && serio->drv->cleanup)
797 serio->drv->cleanup(serio);
798 mutex_unlock(&serio->drv_mutex);
801 static void serio_shutdown(struct device *dev)
803 struct serio *serio = to_serio_port(dev);
805 serio_cleanup(serio);
808 static void serio_attach_driver(struct serio_driver *drv)
810 int error;
812 error = driver_attach(&drv->driver);
813 if (error)
814 printk(KERN_WARNING
815 "serio: driver_attach() failed for %s with error %d\n",
816 drv->driver.name, error);
819 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
821 int manual_bind = drv->manual_bind;
822 int error;
824 drv->driver.bus = &serio_bus;
825 drv->driver.owner = owner;
826 drv->driver.mod_name = mod_name;
829 * Temporarily disable automatic binding because probing
830 * takes long time and we are better off doing it in kseriod
832 drv->manual_bind = 1;
834 error = driver_register(&drv->driver);
835 if (error) {
836 printk(KERN_ERR
837 "serio: driver_register() failed for %s, error: %d\n",
838 drv->driver.name, error);
839 return error;
843 * Restore original bind mode and let kseriod bind the
844 * driver to free ports
846 if (!manual_bind) {
847 drv->manual_bind = 0;
848 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
849 if (error) {
850 driver_unregister(&drv->driver);
851 return error;
855 return 0;
858 void serio_unregister_driver(struct serio_driver *drv)
860 struct serio *serio;
862 mutex_lock(&serio_mutex);
864 drv->manual_bind = 1; /* so serio_find_driver ignores it */
865 serio_remove_pending_events(drv);
867 start_over:
868 list_for_each_entry(serio, &serio_list, node) {
869 if (serio->drv == drv) {
870 serio_disconnect_port(serio);
871 serio_find_driver(serio);
872 /* we could've deleted some ports, restart */
873 goto start_over;
877 driver_unregister(&drv->driver);
878 mutex_unlock(&serio_mutex);
881 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
883 serio_pause_rx(serio);
884 serio->drv = drv;
885 serio_continue_rx(serio);
888 static int serio_bus_match(struct device *dev, struct device_driver *drv)
890 struct serio *serio = to_serio_port(dev);
891 struct serio_driver *serio_drv = to_serio_driver(drv);
893 if (serio->manual_bind || serio_drv->manual_bind)
894 return 0;
896 return serio_match_port(serio_drv->id_table, serio);
899 #ifdef CONFIG_HOTPLUG
901 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
902 do { \
903 int err = add_uevent_var(env, fmt, val); \
904 if (err) \
905 return err; \
906 } while (0)
908 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
910 struct serio *serio;
912 if (!dev)
913 return -ENODEV;
915 serio = to_serio_port(dev);
917 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
918 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
919 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
920 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
921 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
922 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
924 return 0;
926 #undef SERIO_ADD_UEVENT_VAR
928 #else
930 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
932 return -ENODEV;
935 #endif /* CONFIG_HOTPLUG */
937 #ifdef CONFIG_PM
938 static int serio_suspend(struct device *dev, pm_message_t state)
940 if (dev->power.power_state.event != state.event) {
941 if (state.event == PM_EVENT_SUSPEND)
942 serio_cleanup(to_serio_port(dev));
944 dev->power.power_state = state;
947 return 0;
950 static int serio_resume(struct device *dev)
953 * Driver reconnect can take a while, so better let kseriod
954 * deal with it.
956 if (dev->power.power_state.event != PM_EVENT_ON) {
957 dev->power.power_state = PMSG_ON;
958 serio_queue_event(to_serio_port(dev), NULL,
959 SERIO_RECONNECT_PORT);
962 return 0;
964 #endif /* CONFIG_PM */
966 /* called from serio_driver->connect/disconnect methods under serio_mutex */
967 int serio_open(struct serio *serio, struct serio_driver *drv)
969 serio_set_drv(serio, drv);
971 if (serio->open && serio->open(serio)) {
972 serio_set_drv(serio, NULL);
973 return -1;
975 return 0;
978 /* called from serio_driver->connect/disconnect methods under serio_mutex */
979 void serio_close(struct serio *serio)
981 if (serio->close)
982 serio->close(serio);
984 serio_set_drv(serio, NULL);
987 irqreturn_t serio_interrupt(struct serio *serio,
988 unsigned char data, unsigned int dfl)
990 unsigned long flags;
991 irqreturn_t ret = IRQ_NONE;
993 spin_lock_irqsave(&serio->lock, flags);
995 if (likely(serio->drv)) {
996 ret = serio->drv->interrupt(serio, data, dfl);
997 } else if (!dfl && serio->registered) {
998 serio_rescan(serio);
999 ret = IRQ_HANDLED;
1002 spin_unlock_irqrestore(&serio->lock, flags);
1004 return ret;
1007 static struct bus_type serio_bus = {
1008 .name = "serio",
1009 .dev_attrs = serio_device_attrs,
1010 .drv_attrs = serio_driver_attrs,
1011 .match = serio_bus_match,
1012 .uevent = serio_uevent,
1013 .probe = serio_driver_probe,
1014 .remove = serio_driver_remove,
1015 .shutdown = serio_shutdown,
1016 #ifdef CONFIG_PM
1017 .suspend = serio_suspend,
1018 .resume = serio_resume,
1019 #endif
1022 static int __init serio_init(void)
1024 int error;
1026 error = bus_register(&serio_bus);
1027 if (error) {
1028 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
1029 return error;
1032 serio_task = kthread_run(serio_thread, NULL, "kseriod");
1033 if (IS_ERR(serio_task)) {
1034 bus_unregister(&serio_bus);
1035 error = PTR_ERR(serio_task);
1036 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
1037 return error;
1040 return 0;
1043 static void __exit serio_exit(void)
1045 bus_unregister(&serio_bus);
1046 kthread_stop(serio_task);
1049 subsys_initcall(serio_init);
1050 module_exit(serio_exit);