GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / input / serio / serio.c
blobc3b626e9eae7c075cd7597779ddbef4becb731f6
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/stddef.h>
32 #include <linux/module.h>
33 #include <linux/serio.h>
34 #include <linux/errno.h>
35 #include <linux/wait.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40 #include <linux/freezer.h>
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION("Serio abstraction core");
44 MODULE_LICENSE("GPL");
47 * serio_mutex protects entire serio subsystem and is taken every time
48 * serio port or driver registrered or unregistered.
50 static DEFINE_MUTEX(serio_mutex);
52 static LIST_HEAD(serio_list);
54 static struct bus_type serio_bus;
56 static void serio_add_port(struct serio *serio);
57 static int serio_reconnect_port(struct serio *serio);
58 static void serio_disconnect_port(struct serio *serio);
59 static void serio_reconnect_chain(struct serio *serio);
60 static void serio_attach_driver(struct serio_driver *drv);
62 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
64 int retval;
66 mutex_lock(&serio->drv_mutex);
67 retval = drv->connect(serio, drv);
68 mutex_unlock(&serio->drv_mutex);
70 return retval;
73 static int serio_reconnect_driver(struct serio *serio)
75 int retval = -1;
77 mutex_lock(&serio->drv_mutex);
78 if (serio->drv && serio->drv->reconnect)
79 retval = serio->drv->reconnect(serio);
80 mutex_unlock(&serio->drv_mutex);
82 return retval;
85 static void serio_disconnect_driver(struct serio *serio)
87 mutex_lock(&serio->drv_mutex);
88 if (serio->drv)
89 serio->drv->disconnect(serio);
90 mutex_unlock(&serio->drv_mutex);
93 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
95 while (ids->type || ids->proto) {
96 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
97 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
98 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
99 (ids->id == SERIO_ANY || ids->id == serio->id.id))
100 return 1;
101 ids++;
103 return 0;
107 * Basic serio -> driver core mappings
110 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
112 int error;
114 if (serio_match_port(drv->id_table, serio)) {
116 serio->dev.driver = &drv->driver;
117 if (serio_connect_driver(serio, drv)) {
118 serio->dev.driver = NULL;
119 return -ENODEV;
122 error = device_bind_driver(&serio->dev);
123 if (error) {
124 dev_warn(&serio->dev,
125 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
126 serio->phys, serio->name,
127 drv->description, error);
128 serio_disconnect_driver(serio);
129 serio->dev.driver = NULL;
130 return error;
133 return 0;
136 static void serio_find_driver(struct serio *serio)
138 int error;
140 error = device_attach(&serio->dev);
141 if (error < 0)
142 dev_warn(&serio->dev,
143 "device_attach() failed for %s (%s), error: %d\n",
144 serio->phys, serio->name, error);
149 * Serio event processing.
152 enum serio_event_type {
153 SERIO_RESCAN_PORT,
154 SERIO_RECONNECT_PORT,
155 SERIO_RECONNECT_CHAIN,
156 SERIO_REGISTER_PORT,
157 SERIO_ATTACH_DRIVER,
160 struct serio_event {
161 enum serio_event_type type;
162 void *object;
163 struct module *owner;
164 struct list_head node;
167 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
168 static LIST_HEAD(serio_event_list);
169 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
170 static struct task_struct *serio_task;
172 static int serio_queue_event(void *object, struct module *owner,
173 enum serio_event_type event_type)
175 unsigned long flags;
176 struct serio_event *event;
177 int retval = 0;
179 spin_lock_irqsave(&serio_event_lock, flags);
182 * Scan event list for the other events for the same serio port,
183 * starting with the most recent one. If event is the same we
184 * do not need add new one. If event is of different type we
185 * need to add this event and should not look further because
186 * we need to preseve sequence of distinct events.
188 list_for_each_entry_reverse(event, &serio_event_list, node) {
189 if (event->object == object) {
190 if (event->type == event_type)
191 goto out;
192 break;
196 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
197 if (!event) {
198 pr_err("Not enough memory to queue event %d\n", event_type);
199 retval = -ENOMEM;
200 goto out;
203 if (!try_module_get(owner)) {
204 pr_warning("Can't get module reference, dropping event %d\n",
205 event_type);
206 kfree(event);
207 retval = -EINVAL;
208 goto out;
211 event->type = event_type;
212 event->object = object;
213 event->owner = owner;
215 list_add_tail(&event->node, &serio_event_list);
216 wake_up(&serio_wait);
218 out:
219 spin_unlock_irqrestore(&serio_event_lock, flags);
220 return retval;
223 static void serio_free_event(struct serio_event *event)
225 module_put(event->owner);
226 kfree(event);
229 static void serio_remove_duplicate_events(struct serio_event *event)
231 struct serio_event *e, *next;
232 unsigned long flags;
234 spin_lock_irqsave(&serio_event_lock, flags);
236 list_for_each_entry_safe(e, next, &serio_event_list, node) {
237 if (event->object == e->object) {
239 * If this event is of different type we should not
240 * look further - we only suppress duplicate events
241 * that were sent back-to-back.
243 if (event->type != e->type)
244 break;
246 list_del_init(&e->node);
247 serio_free_event(e);
251 spin_unlock_irqrestore(&serio_event_lock, flags);
255 static struct serio_event *serio_get_event(void)
257 struct serio_event *event = NULL;
258 unsigned long flags;
260 spin_lock_irqsave(&serio_event_lock, flags);
262 if (!list_empty(&serio_event_list)) {
263 event = list_first_entry(&serio_event_list,
264 struct serio_event, node);
265 list_del_init(&event->node);
268 spin_unlock_irqrestore(&serio_event_lock, flags);
269 return event;
272 static void serio_handle_event(void)
274 struct serio_event *event;
276 mutex_lock(&serio_mutex);
278 while ((event = serio_get_event())) {
280 switch (event->type) {
282 case SERIO_REGISTER_PORT:
283 serio_add_port(event->object);
284 break;
286 case SERIO_RECONNECT_PORT:
287 serio_reconnect_port(event->object);
288 break;
290 case SERIO_RESCAN_PORT:
291 serio_disconnect_port(event->object);
292 serio_find_driver(event->object);
293 break;
295 case SERIO_RECONNECT_CHAIN:
296 serio_reconnect_chain(event->object);
297 break;
299 case SERIO_ATTACH_DRIVER:
300 serio_attach_driver(event->object);
301 break;
304 serio_remove_duplicate_events(event);
305 serio_free_event(event);
308 mutex_unlock(&serio_mutex);
312 * Remove all events that have been submitted for a given
313 * object, be it serio port or driver.
315 static void serio_remove_pending_events(void *object)
317 struct serio_event *event, *next;
318 unsigned long flags;
320 spin_lock_irqsave(&serio_event_lock, flags);
322 list_for_each_entry_safe(event, next, &serio_event_list, node) {
323 if (event->object == object) {
324 list_del_init(&event->node);
325 serio_free_event(event);
329 spin_unlock_irqrestore(&serio_event_lock, flags);
333 * Destroy child serio port (if any) that has not been fully registered yet.
335 * Note that we rely on the fact that port can have only one child and therefore
336 * only one child registration request can be pending. Additionally, children
337 * are registered by driver's connect() handler so there can't be a grandchild
338 * pending registration together with a child.
340 static struct serio *serio_get_pending_child(struct serio *parent)
342 struct serio_event *event;
343 struct serio *serio, *child = NULL;
344 unsigned long flags;
346 spin_lock_irqsave(&serio_event_lock, flags);
348 list_for_each_entry(event, &serio_event_list, node) {
349 if (event->type == SERIO_REGISTER_PORT) {
350 serio = event->object;
351 if (serio->parent == parent) {
352 child = serio;
353 break;
358 spin_unlock_irqrestore(&serio_event_lock, flags);
359 return child;
362 static int serio_thread(void *nothing)
364 do {
365 serio_handle_event();
366 wait_event_interruptible(serio_wait,
367 kthread_should_stop() || !list_empty(&serio_event_list));
368 } while (!kthread_should_stop());
370 return 0;
375 * Serio port operations
378 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
380 struct serio *serio = to_serio_port(dev);
381 return sprintf(buf, "%s\n", serio->name);
384 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
386 struct serio *serio = to_serio_port(dev);
388 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
389 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
392 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
394 struct serio *serio = to_serio_port(dev);
395 return sprintf(buf, "%02x\n", serio->id.type);
398 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
400 struct serio *serio = to_serio_port(dev);
401 return sprintf(buf, "%02x\n", serio->id.proto);
404 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
406 struct serio *serio = to_serio_port(dev);
407 return sprintf(buf, "%02x\n", serio->id.id);
410 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
412 struct serio *serio = to_serio_port(dev);
413 return sprintf(buf, "%02x\n", serio->id.extra);
416 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
417 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
418 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
419 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
421 static struct attribute *serio_device_id_attrs[] = {
422 &dev_attr_type.attr,
423 &dev_attr_proto.attr,
424 &dev_attr_id.attr,
425 &dev_attr_extra.attr,
426 NULL
429 static struct attribute_group serio_id_attr_group = {
430 .name = "id",
431 .attrs = serio_device_id_attrs,
434 static const struct attribute_group *serio_device_attr_groups[] = {
435 &serio_id_attr_group,
436 NULL
439 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
441 struct serio *serio = to_serio_port(dev);
442 struct device_driver *drv;
443 int error;
445 error = mutex_lock_interruptible(&serio_mutex);
446 if (error)
447 return error;
449 if (!strncmp(buf, "none", count)) {
450 serio_disconnect_port(serio);
451 } else if (!strncmp(buf, "reconnect", count)) {
452 serio_reconnect_chain(serio);
453 } else if (!strncmp(buf, "rescan", count)) {
454 serio_disconnect_port(serio);
455 serio_find_driver(serio);
456 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
457 serio_disconnect_port(serio);
458 error = serio_bind_driver(serio, to_serio_driver(drv));
459 put_driver(drv);
460 } else {
461 error = -EINVAL;
464 mutex_unlock(&serio_mutex);
466 return error ? error : count;
469 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
471 struct serio *serio = to_serio_port(dev);
472 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
475 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
477 struct serio *serio = to_serio_port(dev);
478 int retval;
480 retval = count;
481 if (!strncmp(buf, "manual", count)) {
482 serio->manual_bind = true;
483 } else if (!strncmp(buf, "auto", count)) {
484 serio->manual_bind = false;
485 } else {
486 retval = -EINVAL;
489 return retval;
492 static struct device_attribute serio_device_attrs[] = {
493 __ATTR(description, S_IRUGO, serio_show_description, NULL),
494 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
495 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
496 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
497 __ATTR_NULL
501 static void serio_release_port(struct device *dev)
503 struct serio *serio = to_serio_port(dev);
505 kfree(serio);
506 module_put(THIS_MODULE);
510 * Prepare serio port for registration.
512 static void serio_init_port(struct serio *serio)
514 static atomic_t serio_no = ATOMIC_INIT(0);
516 __module_get(THIS_MODULE);
518 INIT_LIST_HEAD(&serio->node);
519 spin_lock_init(&serio->lock);
520 mutex_init(&serio->drv_mutex);
521 device_initialize(&serio->dev);
522 dev_set_name(&serio->dev, "serio%ld",
523 (long)atomic_inc_return(&serio_no) - 1);
524 serio->dev.bus = &serio_bus;
525 serio->dev.release = serio_release_port;
526 serio->dev.groups = serio_device_attr_groups;
527 if (serio->parent) {
528 serio->dev.parent = &serio->parent->dev;
529 serio->depth = serio->parent->depth + 1;
530 } else
531 serio->depth = 0;
532 lockdep_set_subclass(&serio->lock, serio->depth);
536 * Complete serio port registration.
537 * Driver core will attempt to find appropriate driver for the port.
539 static void serio_add_port(struct serio *serio)
541 int error;
543 if (serio->parent) {
544 serio_pause_rx(serio->parent);
545 serio->parent->child = serio;
546 serio_continue_rx(serio->parent);
549 list_add_tail(&serio->node, &serio_list);
551 if (serio->start)
552 serio->start(serio);
554 error = device_add(&serio->dev);
555 if (error)
556 dev_err(&serio->dev,
557 "device_add() failed for %s (%s), error: %d\n",
558 serio->phys, serio->name, error);
562 * serio_destroy_port() completes deregistration process and removes
563 * port from the system
565 static void serio_destroy_port(struct serio *serio)
567 struct serio *child;
569 child = serio_get_pending_child(serio);
570 if (child) {
571 serio_remove_pending_events(child);
572 put_device(&child->dev);
575 if (serio->stop)
576 serio->stop(serio);
578 if (serio->parent) {
579 serio_pause_rx(serio->parent);
580 serio->parent->child = NULL;
581 serio_continue_rx(serio->parent);
582 serio->parent = NULL;
585 if (device_is_registered(&serio->dev))
586 device_del(&serio->dev);
588 list_del_init(&serio->node);
589 serio_remove_pending_events(serio);
590 put_device(&serio->dev);
594 * Reconnect serio port (re-initialize attached device).
595 * If reconnect fails (old device is no longer attached or
596 * there was no device to begin with) we do full rescan in
597 * hope of finding a driver for the port.
599 static int serio_reconnect_port(struct serio *serio)
601 int error = serio_reconnect_driver(serio);
603 if (error) {
604 serio_disconnect_port(serio);
605 serio_find_driver(serio);
608 return error;
612 * Reconnect serio port and all its children (re-initialize attached devices)
614 static void serio_reconnect_chain(struct serio *serio)
616 do {
617 if (serio_reconnect_port(serio)) {
618 /* Ok, old children are now gone, we are done */
619 break;
621 serio = serio->child;
622 } while (serio);
626 * serio_disconnect_port() unbinds a port from its driver. As a side effect
627 * all child ports are unbound and destroyed.
629 static void serio_disconnect_port(struct serio *serio)
631 struct serio *s, *parent;
633 if (serio->child) {
635 * Children ports should be disconnected and destroyed
636 * first, staring with the leaf one, since we don't want
637 * to do recursion
639 for (s = serio; s->child; s = s->child)
640 /* empty */;
642 do {
643 parent = s->parent;
645 device_release_driver(&s->dev);
646 serio_destroy_port(s);
647 } while ((s = parent) != serio);
651 * Ok, no children left, now disconnect this port
653 device_release_driver(&serio->dev);
656 void serio_rescan(struct serio *serio)
658 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
660 EXPORT_SYMBOL(serio_rescan);
662 void serio_reconnect(struct serio *serio)
664 serio_queue_event(serio, NULL, SERIO_RECONNECT_CHAIN);
666 EXPORT_SYMBOL(serio_reconnect);
669 * Submits register request to kseriod for subsequent execution.
670 * Note that port registration is always asynchronous.
672 void __serio_register_port(struct serio *serio, struct module *owner)
674 serio_init_port(serio);
675 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
677 EXPORT_SYMBOL(__serio_register_port);
680 * Synchronously unregisters serio port.
682 void serio_unregister_port(struct serio *serio)
684 mutex_lock(&serio_mutex);
685 serio_disconnect_port(serio);
686 serio_destroy_port(serio);
687 mutex_unlock(&serio_mutex);
689 EXPORT_SYMBOL(serio_unregister_port);
692 * Safely unregisters child port if one is present.
694 void serio_unregister_child_port(struct serio *serio)
696 mutex_lock(&serio_mutex);
697 if (serio->child) {
698 serio_disconnect_port(serio->child);
699 serio_destroy_port(serio->child);
701 mutex_unlock(&serio_mutex);
703 EXPORT_SYMBOL(serio_unregister_child_port);
707 * Serio driver operations
710 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
712 struct serio_driver *driver = to_serio_driver(drv);
713 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
716 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
718 struct serio_driver *serio_drv = to_serio_driver(drv);
719 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
722 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
724 struct serio_driver *serio_drv = to_serio_driver(drv);
725 int retval;
727 retval = count;
728 if (!strncmp(buf, "manual", count)) {
729 serio_drv->manual_bind = true;
730 } else if (!strncmp(buf, "auto", count)) {
731 serio_drv->manual_bind = false;
732 } else {
733 retval = -EINVAL;
736 return retval;
740 static struct driver_attribute serio_driver_attrs[] = {
741 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
742 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
743 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
744 __ATTR_NULL
747 static int serio_driver_probe(struct device *dev)
749 struct serio *serio = to_serio_port(dev);
750 struct serio_driver *drv = to_serio_driver(dev->driver);
752 return serio_connect_driver(serio, drv);
755 static int serio_driver_remove(struct device *dev)
757 struct serio *serio = to_serio_port(dev);
759 serio_disconnect_driver(serio);
760 return 0;
763 static void serio_cleanup(struct serio *serio)
765 mutex_lock(&serio->drv_mutex);
766 if (serio->drv && serio->drv->cleanup)
767 serio->drv->cleanup(serio);
768 mutex_unlock(&serio->drv_mutex);
771 static void serio_shutdown(struct device *dev)
773 struct serio *serio = to_serio_port(dev);
775 serio_cleanup(serio);
778 static void serio_attach_driver(struct serio_driver *drv)
780 int error;
782 error = driver_attach(&drv->driver);
783 if (error)
784 pr_warning("driver_attach() failed for %s with error %d\n",
785 drv->driver.name, error);
788 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
790 bool manual_bind = drv->manual_bind;
791 int error;
793 drv->driver.bus = &serio_bus;
794 drv->driver.owner = owner;
795 drv->driver.mod_name = mod_name;
798 * Temporarily disable automatic binding because probing
799 * takes long time and we are better off doing it in kseriod
801 drv->manual_bind = true;
803 error = driver_register(&drv->driver);
804 if (error) {
805 pr_err("driver_register() failed for %s, error: %d\n",
806 drv->driver.name, error);
807 return error;
811 * Restore original bind mode and let kseriod bind the
812 * driver to free ports
814 if (!manual_bind) {
815 drv->manual_bind = false;
816 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
817 if (error) {
818 driver_unregister(&drv->driver);
819 return error;
823 return 0;
825 EXPORT_SYMBOL(__serio_register_driver);
827 void serio_unregister_driver(struct serio_driver *drv)
829 struct serio *serio;
831 mutex_lock(&serio_mutex);
833 drv->manual_bind = true; /* so serio_find_driver ignores it */
834 serio_remove_pending_events(drv);
836 start_over:
837 list_for_each_entry(serio, &serio_list, node) {
838 if (serio->drv == drv) {
839 serio_disconnect_port(serio);
840 serio_find_driver(serio);
841 /* we could've deleted some ports, restart */
842 goto start_over;
846 driver_unregister(&drv->driver);
847 mutex_unlock(&serio_mutex);
849 EXPORT_SYMBOL(serio_unregister_driver);
851 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
853 serio_pause_rx(serio);
854 serio->drv = drv;
855 serio_continue_rx(serio);
858 static int serio_bus_match(struct device *dev, struct device_driver *drv)
860 struct serio *serio = to_serio_port(dev);
861 struct serio_driver *serio_drv = to_serio_driver(drv);
863 if (serio->manual_bind || serio_drv->manual_bind)
864 return 0;
866 return serio_match_port(serio_drv->id_table, serio);
869 #ifdef CONFIG_HOTPLUG
871 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
872 do { \
873 int err = add_uevent_var(env, fmt, val); \
874 if (err) \
875 return err; \
876 } while (0)
878 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
880 struct serio *serio;
882 if (!dev)
883 return -ENODEV;
885 serio = to_serio_port(dev);
887 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
888 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
889 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
890 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
891 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
892 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
894 return 0;
896 #undef SERIO_ADD_UEVENT_VAR
898 #else
900 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
902 return -ENODEV;
905 #endif /* CONFIG_HOTPLUG */
907 #ifdef CONFIG_PM
908 static int serio_suspend(struct device *dev)
910 struct serio *serio = to_serio_port(dev);
912 serio_cleanup(serio);
914 return 0;
917 static int serio_resume(struct device *dev)
919 struct serio *serio = to_serio_port(dev);
922 * Driver reconnect can take a while, so better let kseriod
923 * deal with it.
925 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
927 return 0;
930 static const struct dev_pm_ops serio_pm_ops = {
931 .suspend = serio_suspend,
932 .resume = serio_resume,
933 .poweroff = serio_suspend,
934 .restore = serio_resume,
936 #endif /* CONFIG_PM */
938 /* called from serio_driver->connect/disconnect methods under serio_mutex */
939 int serio_open(struct serio *serio, struct serio_driver *drv)
941 serio_set_drv(serio, drv);
943 if (serio->open && serio->open(serio)) {
944 serio_set_drv(serio, NULL);
945 return -1;
947 return 0;
949 EXPORT_SYMBOL(serio_open);
951 /* called from serio_driver->connect/disconnect methods under serio_mutex */
952 void serio_close(struct serio *serio)
954 if (serio->close)
955 serio->close(serio);
957 serio_set_drv(serio, NULL);
959 EXPORT_SYMBOL(serio_close);
961 irqreturn_t serio_interrupt(struct serio *serio,
962 unsigned char data, unsigned int dfl)
964 unsigned long flags;
965 irqreturn_t ret = IRQ_NONE;
967 spin_lock_irqsave(&serio->lock, flags);
969 if (likely(serio->drv)) {
970 ret = serio->drv->interrupt(serio, data, dfl);
971 } else if (!dfl && device_is_registered(&serio->dev)) {
972 serio_rescan(serio);
973 ret = IRQ_HANDLED;
976 spin_unlock_irqrestore(&serio->lock, flags);
978 return ret;
980 EXPORT_SYMBOL(serio_interrupt);
982 static struct bus_type serio_bus = {
983 .name = "serio",
984 .dev_attrs = serio_device_attrs,
985 .drv_attrs = serio_driver_attrs,
986 .match = serio_bus_match,
987 .uevent = serio_uevent,
988 .probe = serio_driver_probe,
989 .remove = serio_driver_remove,
990 .shutdown = serio_shutdown,
991 #ifdef CONFIG_PM
992 .pm = &serio_pm_ops,
993 #endif
996 static int __init serio_init(void)
998 int error;
1000 error = bus_register(&serio_bus);
1001 if (error) {
1002 pr_err("Failed to register serio bus, error: %d\n", error);
1003 return error;
1006 serio_task = kthread_run(serio_thread, NULL, "kseriod");
1007 if (IS_ERR(serio_task)) {
1008 bus_unregister(&serio_bus);
1009 error = PTR_ERR(serio_task);
1010 pr_err("Failed to start kseriod, error: %d\n", error);
1011 return error;
1014 return 0;
1017 static void __exit serio_exit(void)
1019 bus_unregister(&serio_bus);
1020 kthread_stop(serio_task);
1023 subsys_initcall(serio_init);
1024 module_exit(serio_exit);