specialix: fix compiler warning on specialix_pci_tbl
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / input / serio / serio.c
blob405bf214527c33f08c40c04d60c5e863826dbcc7
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>
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION("Serio abstraction core");
43 MODULE_LICENSE("GPL");
46 * serio_mutex protects entire serio subsystem and is taken every time
47 * serio port or driver registrered or unregistered.
49 static DEFINE_MUTEX(serio_mutex);
51 static LIST_HEAD(serio_list);
53 static struct bus_type serio_bus;
55 static void serio_add_port(struct serio *serio);
56 static int serio_reconnect_port(struct serio *serio);
57 static void serio_disconnect_port(struct serio *serio);
58 static void serio_reconnect_subtree(struct serio *serio);
59 static void serio_attach_driver(struct serio_driver *drv);
61 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
63 int retval;
65 mutex_lock(&serio->drv_mutex);
66 retval = drv->connect(serio, drv);
67 mutex_unlock(&serio->drv_mutex);
69 return retval;
72 static int serio_reconnect_driver(struct serio *serio)
74 int retval = -1;
76 mutex_lock(&serio->drv_mutex);
77 if (serio->drv && serio->drv->reconnect)
78 retval = serio->drv->reconnect(serio);
79 mutex_unlock(&serio->drv_mutex);
81 return retval;
84 static void serio_disconnect_driver(struct serio *serio)
86 mutex_lock(&serio->drv_mutex);
87 if (serio->drv)
88 serio->drv->disconnect(serio);
89 mutex_unlock(&serio->drv_mutex);
92 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
94 while (ids->type || ids->proto) {
95 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
96 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
97 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
98 (ids->id == SERIO_ANY || ids->id == serio->id.id))
99 return 1;
100 ids++;
102 return 0;
106 * Basic serio -> driver core mappings
109 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
111 int error;
113 if (serio_match_port(drv->id_table, serio)) {
115 serio->dev.driver = &drv->driver;
116 if (serio_connect_driver(serio, drv)) {
117 serio->dev.driver = NULL;
118 return -ENODEV;
121 error = device_bind_driver(&serio->dev);
122 if (error) {
123 dev_warn(&serio->dev,
124 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
125 serio->phys, serio->name,
126 drv->description, error);
127 serio_disconnect_driver(serio);
128 serio->dev.driver = NULL;
129 return error;
132 return 0;
135 static void serio_find_driver(struct serio *serio)
137 int error;
139 error = device_attach(&serio->dev);
140 if (error < 0)
141 dev_warn(&serio->dev,
142 "device_attach() failed for %s (%s), error: %d\n",
143 serio->phys, serio->name, error);
148 * Serio event processing.
151 enum serio_event_type {
152 SERIO_RESCAN_PORT,
153 SERIO_RECONNECT_PORT,
154 SERIO_RECONNECT_SUBTREE,
155 SERIO_REGISTER_PORT,
156 SERIO_ATTACH_DRIVER,
159 struct serio_event {
160 enum serio_event_type type;
161 void *object;
162 struct module *owner;
163 struct list_head node;
166 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
167 static LIST_HEAD(serio_event_list);
168 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
169 static struct task_struct *serio_task;
171 static int serio_queue_event(void *object, struct module *owner,
172 enum serio_event_type event_type)
174 unsigned long flags;
175 struct serio_event *event;
176 int retval = 0;
178 spin_lock_irqsave(&serio_event_lock, flags);
181 * Scan event list for the other events for the same serio port,
182 * starting with the most recent one. If event is the same we
183 * do not need add new one. If event is of different type we
184 * need to add this event and should not look further because
185 * we need to preseve sequence of distinct events.
187 list_for_each_entry_reverse(event, &serio_event_list, node) {
188 if (event->object == object) {
189 if (event->type == event_type)
190 goto out;
191 break;
195 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
196 if (!event) {
197 pr_err("Not enough memory to queue event %d\n", event_type);
198 retval = -ENOMEM;
199 goto out;
202 if (!try_module_get(owner)) {
203 pr_warning("Can't get module reference, dropping event %d\n",
204 event_type);
205 kfree(event);
206 retval = -EINVAL;
207 goto out;
210 event->type = event_type;
211 event->object = object;
212 event->owner = owner;
214 list_add_tail(&event->node, &serio_event_list);
215 wake_up(&serio_wait);
217 out:
218 spin_unlock_irqrestore(&serio_event_lock, flags);
219 return retval;
222 static void serio_free_event(struct serio_event *event)
224 module_put(event->owner);
225 kfree(event);
228 static void serio_remove_duplicate_events(struct serio_event *event)
230 struct serio_event *e, *next;
231 unsigned long flags;
233 spin_lock_irqsave(&serio_event_lock, flags);
235 list_for_each_entry_safe(e, next, &serio_event_list, node) {
236 if (event->object == e->object) {
238 * If this event is of different type we should not
239 * look further - we only suppress duplicate events
240 * that were sent back-to-back.
242 if (event->type != e->type)
243 break;
245 list_del_init(&e->node);
246 serio_free_event(e);
250 spin_unlock_irqrestore(&serio_event_lock, flags);
254 static struct serio_event *serio_get_event(void)
256 struct serio_event *event = NULL;
257 unsigned long flags;
259 spin_lock_irqsave(&serio_event_lock, flags);
261 if (!list_empty(&serio_event_list)) {
262 event = list_first_entry(&serio_event_list,
263 struct serio_event, node);
264 list_del_init(&event->node);
267 spin_unlock_irqrestore(&serio_event_lock, flags);
268 return event;
271 static void serio_handle_event(void)
273 struct serio_event *event;
275 mutex_lock(&serio_mutex);
277 while ((event = serio_get_event())) {
279 switch (event->type) {
281 case SERIO_REGISTER_PORT:
282 serio_add_port(event->object);
283 break;
285 case SERIO_RECONNECT_PORT:
286 serio_reconnect_port(event->object);
287 break;
289 case SERIO_RESCAN_PORT:
290 serio_disconnect_port(event->object);
291 serio_find_driver(event->object);
292 break;
294 case SERIO_RECONNECT_SUBTREE:
295 serio_reconnect_subtree(event->object);
296 break;
298 case SERIO_ATTACH_DRIVER:
299 serio_attach_driver(event->object);
300 break;
303 serio_remove_duplicate_events(event);
304 serio_free_event(event);
307 mutex_unlock(&serio_mutex);
311 * Remove all events that have been submitted for a given
312 * object, be it serio port or driver.
314 static void serio_remove_pending_events(void *object)
316 struct serio_event *event, *next;
317 unsigned long flags;
319 spin_lock_irqsave(&serio_event_lock, flags);
321 list_for_each_entry_safe(event, next, &serio_event_list, node) {
322 if (event->object == object) {
323 list_del_init(&event->node);
324 serio_free_event(event);
328 spin_unlock_irqrestore(&serio_event_lock, flags);
332 * Locate child serio port (if any) that has not been fully registered yet.
334 * Children are registered by driver's connect() handler so there can't be a
335 * grandchild pending registration together with a child.
337 static struct serio *serio_get_pending_child(struct serio *parent)
339 struct serio_event *event;
340 struct serio *serio, *child = NULL;
341 unsigned long flags;
343 spin_lock_irqsave(&serio_event_lock, flags);
345 list_for_each_entry(event, &serio_event_list, node) {
346 if (event->type == SERIO_REGISTER_PORT) {
347 serio = event->object;
348 if (serio->parent == parent) {
349 child = serio;
350 break;
355 spin_unlock_irqrestore(&serio_event_lock, flags);
356 return child;
359 static int serio_thread(void *nothing)
361 do {
362 serio_handle_event();
363 wait_event_interruptible(serio_wait,
364 kthread_should_stop() || !list_empty(&serio_event_list));
365 } while (!kthread_should_stop());
367 return 0;
372 * Serio port operations
375 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
377 struct serio *serio = to_serio_port(dev);
378 return sprintf(buf, "%s\n", serio->name);
381 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
383 struct serio *serio = to_serio_port(dev);
385 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
386 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
389 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
391 struct serio *serio = to_serio_port(dev);
392 return sprintf(buf, "%02x\n", serio->id.type);
395 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
397 struct serio *serio = to_serio_port(dev);
398 return sprintf(buf, "%02x\n", serio->id.proto);
401 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
403 struct serio *serio = to_serio_port(dev);
404 return sprintf(buf, "%02x\n", serio->id.id);
407 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
409 struct serio *serio = to_serio_port(dev);
410 return sprintf(buf, "%02x\n", serio->id.extra);
413 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
414 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
415 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
416 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
418 static struct attribute *serio_device_id_attrs[] = {
419 &dev_attr_type.attr,
420 &dev_attr_proto.attr,
421 &dev_attr_id.attr,
422 &dev_attr_extra.attr,
423 NULL
426 static struct attribute_group serio_id_attr_group = {
427 .name = "id",
428 .attrs = serio_device_id_attrs,
431 static const struct attribute_group *serio_device_attr_groups[] = {
432 &serio_id_attr_group,
433 NULL
436 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
438 struct serio *serio = to_serio_port(dev);
439 struct device_driver *drv;
440 int error;
442 error = mutex_lock_interruptible(&serio_mutex);
443 if (error)
444 return error;
446 if (!strncmp(buf, "none", count)) {
447 serio_disconnect_port(serio);
448 } else if (!strncmp(buf, "reconnect", count)) {
449 serio_reconnect_subtree(serio);
450 } else if (!strncmp(buf, "rescan", count)) {
451 serio_disconnect_port(serio);
452 serio_find_driver(serio);
453 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
454 serio_disconnect_port(serio);
455 error = serio_bind_driver(serio, to_serio_driver(drv));
456 put_driver(drv);
457 } else {
458 error = -EINVAL;
461 mutex_unlock(&serio_mutex);
463 return error ? error : count;
466 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
468 struct serio *serio = to_serio_port(dev);
469 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
472 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
474 struct serio *serio = to_serio_port(dev);
475 int retval;
477 retval = count;
478 if (!strncmp(buf, "manual", count)) {
479 serio->manual_bind = true;
480 } else if (!strncmp(buf, "auto", count)) {
481 serio->manual_bind = false;
482 } else {
483 retval = -EINVAL;
486 return retval;
489 static struct device_attribute serio_device_attrs[] = {
490 __ATTR(description, S_IRUGO, serio_show_description, NULL),
491 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
492 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
493 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
494 __ATTR_NULL
498 static void serio_release_port(struct device *dev)
500 struct serio *serio = to_serio_port(dev);
502 kfree(serio);
503 module_put(THIS_MODULE);
507 * Prepare serio port for registration.
509 static void serio_init_port(struct serio *serio)
511 static atomic_t serio_no = ATOMIC_INIT(0);
513 __module_get(THIS_MODULE);
515 INIT_LIST_HEAD(&serio->node);
516 INIT_LIST_HEAD(&serio->child_node);
517 INIT_LIST_HEAD(&serio->children);
518 spin_lock_init(&serio->lock);
519 mutex_init(&serio->drv_mutex);
520 device_initialize(&serio->dev);
521 dev_set_name(&serio->dev, "serio%ld",
522 (long)atomic_inc_return(&serio_no) - 1);
523 serio->dev.bus = &serio_bus;
524 serio->dev.release = serio_release_port;
525 serio->dev.groups = serio_device_attr_groups;
526 if (serio->parent) {
527 serio->dev.parent = &serio->parent->dev;
528 serio->depth = serio->parent->depth + 1;
529 } else
530 serio->depth = 0;
531 lockdep_set_subclass(&serio->lock, serio->depth);
535 * Complete serio port registration.
536 * Driver core will attempt to find appropriate driver for the port.
538 static void serio_add_port(struct serio *serio)
540 struct serio *parent = serio->parent;
541 int error;
543 if (parent) {
544 serio_pause_rx(parent);
545 list_add_tail(&serio->child_node, &parent->children);
546 serio_continue_rx(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 unregistration process and removes
563 * port from the system
565 static void serio_destroy_port(struct serio *serio)
567 struct serio *child;
569 while ((child = serio_get_pending_child(serio)) != NULL) {
570 serio_remove_pending_events(child);
571 put_device(&child->dev);
574 if (serio->stop)
575 serio->stop(serio);
577 if (serio->parent) {
578 serio_pause_rx(serio->parent);
579 list_del_init(&serio->child_node);
580 serio_continue_rx(serio->parent);
581 serio->parent = NULL;
584 if (device_is_registered(&serio->dev))
585 device_del(&serio->dev);
587 list_del_init(&serio->node);
588 serio_remove_pending_events(serio);
589 put_device(&serio->dev);
593 * Reconnect serio port (re-initialize attached device).
594 * If reconnect fails (old device is no longer attached or
595 * there was no device to begin with) we do full rescan in
596 * hope of finding a driver for the port.
598 static int serio_reconnect_port(struct serio *serio)
600 int error = serio_reconnect_driver(serio);
602 if (error) {
603 serio_disconnect_port(serio);
604 serio_find_driver(serio);
607 return error;
611 * Reconnect serio port and all its children (re-initialize attached
612 * devices).
614 static void serio_reconnect_subtree(struct serio *root)
616 struct serio *s = root;
617 int error;
619 do {
620 error = serio_reconnect_port(s);
621 if (!error) {
623 * Reconnect was successful, move on to do the
624 * first child.
626 if (!list_empty(&s->children)) {
627 s = list_first_entry(&s->children,
628 struct serio, child_node);
629 continue;
634 * Either it was a leaf node or reconnect failed and it
635 * became a leaf node. Continue reconnecting starting with
636 * the next sibling of the parent node.
638 while (s != root) {
639 struct serio *parent = s->parent;
641 if (!list_is_last(&s->child_node, &parent->children)) {
642 s = list_entry(s->child_node.next,
643 struct serio, child_node);
644 break;
647 s = parent;
649 } while (s != root);
653 * serio_disconnect_port() unbinds a port from its driver. As a side effect
654 * all children ports are unbound and destroyed.
656 static void serio_disconnect_port(struct serio *serio)
658 struct serio *s = serio;
661 * Children ports should be disconnected and destroyed
662 * first; we travel the tree in depth-first order.
664 while (!list_empty(&serio->children)) {
666 /* Locate a leaf */
667 while (!list_empty(&s->children))
668 s = list_first_entry(&s->children,
669 struct serio, child_node);
672 * Prune this leaf node unless it is the one we
673 * started with.
675 if (s != serio) {
676 struct serio *parent = s->parent;
678 device_release_driver(&s->dev);
679 serio_destroy_port(s);
681 s = parent;
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);
695 EXPORT_SYMBOL(serio_rescan);
697 void serio_reconnect(struct serio *serio)
699 serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
701 EXPORT_SYMBOL(serio_reconnect);
704 * Submits register request to kseriod for subsequent execution.
705 * Note that port registration is always asynchronous.
707 void __serio_register_port(struct serio *serio, struct module *owner)
709 serio_init_port(serio);
710 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
712 EXPORT_SYMBOL(__serio_register_port);
715 * Synchronously unregisters serio port.
717 void serio_unregister_port(struct serio *serio)
719 mutex_lock(&serio_mutex);
720 serio_disconnect_port(serio);
721 serio_destroy_port(serio);
722 mutex_unlock(&serio_mutex);
724 EXPORT_SYMBOL(serio_unregister_port);
727 * Safely unregisters children ports if they are present.
729 void serio_unregister_child_port(struct serio *serio)
731 struct serio *s, *next;
733 mutex_lock(&serio_mutex);
734 list_for_each_entry_safe(s, next, &serio->children, child_node) {
735 serio_disconnect_port(s);
736 serio_destroy_port(s);
738 mutex_unlock(&serio_mutex);
740 EXPORT_SYMBOL(serio_unregister_child_port);
744 * Serio driver operations
747 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
749 struct serio_driver *driver = to_serio_driver(drv);
750 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
753 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
755 struct serio_driver *serio_drv = to_serio_driver(drv);
756 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
759 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
761 struct serio_driver *serio_drv = to_serio_driver(drv);
762 int retval;
764 retval = count;
765 if (!strncmp(buf, "manual", count)) {
766 serio_drv->manual_bind = true;
767 } else if (!strncmp(buf, "auto", count)) {
768 serio_drv->manual_bind = false;
769 } else {
770 retval = -EINVAL;
773 return retval;
777 static struct driver_attribute serio_driver_attrs[] = {
778 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
779 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
780 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
781 __ATTR_NULL
784 static int serio_driver_probe(struct device *dev)
786 struct serio *serio = to_serio_port(dev);
787 struct serio_driver *drv = to_serio_driver(dev->driver);
789 return serio_connect_driver(serio, drv);
792 static int serio_driver_remove(struct device *dev)
794 struct serio *serio = to_serio_port(dev);
796 serio_disconnect_driver(serio);
797 return 0;
800 static void serio_cleanup(struct serio *serio)
802 mutex_lock(&serio->drv_mutex);
803 if (serio->drv && serio->drv->cleanup)
804 serio->drv->cleanup(serio);
805 mutex_unlock(&serio->drv_mutex);
808 static void serio_shutdown(struct device *dev)
810 struct serio *serio = to_serio_port(dev);
812 serio_cleanup(serio);
815 static void serio_attach_driver(struct serio_driver *drv)
817 int error;
819 error = driver_attach(&drv->driver);
820 if (error)
821 pr_warning("driver_attach() failed for %s with error %d\n",
822 drv->driver.name, error);
825 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
827 bool manual_bind = drv->manual_bind;
828 int error;
830 drv->driver.bus = &serio_bus;
831 drv->driver.owner = owner;
832 drv->driver.mod_name = mod_name;
835 * Temporarily disable automatic binding because probing
836 * takes long time and we are better off doing it in kseriod
838 drv->manual_bind = true;
840 error = driver_register(&drv->driver);
841 if (error) {
842 pr_err("driver_register() failed for %s, error: %d\n",
843 drv->driver.name, error);
844 return error;
848 * Restore original bind mode and let kseriod bind the
849 * driver to free ports
851 if (!manual_bind) {
852 drv->manual_bind = false;
853 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
854 if (error) {
855 driver_unregister(&drv->driver);
856 return error;
860 return 0;
862 EXPORT_SYMBOL(__serio_register_driver);
864 void serio_unregister_driver(struct serio_driver *drv)
866 struct serio *serio;
868 mutex_lock(&serio_mutex);
870 drv->manual_bind = true; /* so serio_find_driver ignores it */
871 serio_remove_pending_events(drv);
873 start_over:
874 list_for_each_entry(serio, &serio_list, node) {
875 if (serio->drv == drv) {
876 serio_disconnect_port(serio);
877 serio_find_driver(serio);
878 /* we could've deleted some ports, restart */
879 goto start_over;
883 driver_unregister(&drv->driver);
884 mutex_unlock(&serio_mutex);
886 EXPORT_SYMBOL(serio_unregister_driver);
888 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
890 serio_pause_rx(serio);
891 serio->drv = drv;
892 serio_continue_rx(serio);
895 static int serio_bus_match(struct device *dev, struct device_driver *drv)
897 struct serio *serio = to_serio_port(dev);
898 struct serio_driver *serio_drv = to_serio_driver(drv);
900 if (serio->manual_bind || serio_drv->manual_bind)
901 return 0;
903 return serio_match_port(serio_drv->id_table, serio);
906 #ifdef CONFIG_HOTPLUG
908 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
909 do { \
910 int err = add_uevent_var(env, fmt, val); \
911 if (err) \
912 return err; \
913 } while (0)
915 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
917 struct serio *serio;
919 if (!dev)
920 return -ENODEV;
922 serio = to_serio_port(dev);
924 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
925 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
926 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
927 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
928 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
929 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
931 return 0;
933 #undef SERIO_ADD_UEVENT_VAR
935 #else
937 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
939 return -ENODEV;
942 #endif /* CONFIG_HOTPLUG */
944 #ifdef CONFIG_PM
945 static int serio_suspend(struct device *dev)
947 struct serio *serio = to_serio_port(dev);
949 serio_cleanup(serio);
951 return 0;
954 static int serio_resume(struct device *dev)
956 struct serio *serio = to_serio_port(dev);
959 * Driver reconnect can take a while, so better let kseriod
960 * deal with it.
962 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
964 return 0;
967 static const struct dev_pm_ops serio_pm_ops = {
968 .suspend = serio_suspend,
969 .resume = serio_resume,
970 .poweroff = serio_suspend,
971 .restore = serio_resume,
973 #endif /* CONFIG_PM */
975 /* called from serio_driver->connect/disconnect methods under serio_mutex */
976 int serio_open(struct serio *serio, struct serio_driver *drv)
978 serio_set_drv(serio, drv);
980 if (serio->open && serio->open(serio)) {
981 serio_set_drv(serio, NULL);
982 return -1;
984 return 0;
986 EXPORT_SYMBOL(serio_open);
988 /* called from serio_driver->connect/disconnect methods under serio_mutex */
989 void serio_close(struct serio *serio)
991 if (serio->close)
992 serio->close(serio);
994 serio_set_drv(serio, NULL);
996 EXPORT_SYMBOL(serio_close);
998 irqreturn_t serio_interrupt(struct serio *serio,
999 unsigned char data, unsigned int dfl)
1001 unsigned long flags;
1002 irqreturn_t ret = IRQ_NONE;
1004 spin_lock_irqsave(&serio->lock, flags);
1006 if (likely(serio->drv)) {
1007 ret = serio->drv->interrupt(serio, data, dfl);
1008 } else if (!dfl && device_is_registered(&serio->dev)) {
1009 serio_rescan(serio);
1010 ret = IRQ_HANDLED;
1013 spin_unlock_irqrestore(&serio->lock, flags);
1015 return ret;
1017 EXPORT_SYMBOL(serio_interrupt);
1019 static struct bus_type serio_bus = {
1020 .name = "serio",
1021 .dev_attrs = serio_device_attrs,
1022 .drv_attrs = serio_driver_attrs,
1023 .match = serio_bus_match,
1024 .uevent = serio_uevent,
1025 .probe = serio_driver_probe,
1026 .remove = serio_driver_remove,
1027 .shutdown = serio_shutdown,
1028 #ifdef CONFIG_PM
1029 .pm = &serio_pm_ops,
1030 #endif
1033 static int __init serio_init(void)
1035 int error;
1037 error = bus_register(&serio_bus);
1038 if (error) {
1039 pr_err("Failed to register serio bus, error: %d\n", error);
1040 return error;
1043 serio_task = kthread_run(serio_thread, NULL, "kseriod");
1044 if (IS_ERR(serio_task)) {
1045 bus_unregister(&serio_bus);
1046 error = PTR_ERR(serio_task);
1047 pr_err("Failed to start kseriod, error: %d\n", error);
1048 return error;
1051 return 0;
1054 static void __exit serio_exit(void)
1056 bus_unregister(&serio_bus);
1057 kthread_stop(serio_task);
1060 subsys_initcall(serio_init);
1061 module_exit(serio_exit);