2 * The Serio abstraction module
4 * Copyright (c) 1999-2004 Vojtech Pavlik
5 * Copyright (c) 2004 Dmitry Torokhov
6 * Copyright (c) 2003 Daniele Bellucci
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>
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION("Serio abstraction core");
41 MODULE_LICENSE("GPL");
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_register_driver);
48 EXPORT_SYMBOL(serio_unregister_driver);
49 EXPORT_SYMBOL(serio_open);
50 EXPORT_SYMBOL(serio_close);
51 EXPORT_SYMBOL(serio_rescan);
52 EXPORT_SYMBOL(serio_reconnect);
55 * serio_mutex protects entire serio subsystem and is taken every time
56 * serio port or driver registrered or unregistered.
58 static DEFINE_MUTEX(serio_mutex);
60 static LIST_HEAD(serio_list);
62 static struct bus_type serio_bus;
64 static void serio_add_port(struct serio *serio);
65 static void serio_reconnect_port(struct serio *serio);
66 static void serio_disconnect_port(struct serio *serio);
67 static void serio_attach_driver(struct serio_driver *drv);
69 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
73 mutex_lock(&serio->drv_mutex);
74 retval = drv->connect(serio, drv);
75 mutex_unlock(&serio->drv_mutex);
80 static int serio_reconnect_driver(struct serio *serio)
84 mutex_lock(&serio->drv_mutex);
85 if (serio->drv && serio->drv->reconnect)
86 retval = serio->drv->reconnect(serio);
87 mutex_unlock(&serio->drv_mutex);
92 static void serio_disconnect_driver(struct serio *serio)
94 mutex_lock(&serio->drv_mutex);
96 serio->drv->disconnect(serio);
97 mutex_unlock(&serio->drv_mutex);
100 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
102 while (ids->type || ids->proto) {
103 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
104 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
105 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
106 (ids->id == SERIO_ANY || ids->id == serio->id.id))
114 * Basic serio -> driver core mappings
117 static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
121 down_write(&serio_bus.subsys.rwsem);
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;
129 error = device_bind_driver(&serio->dev);
132 "serio: device_bind_driver() failed "
133 "for %s (%s) and %s, error: %d\n",
134 serio->phys, serio->name,
135 drv->description, error);
136 serio_disconnect_driver(serio);
137 serio->dev.driver = NULL;
142 up_write(&serio_bus.subsys.rwsem);
145 static void serio_release_driver(struct serio *serio)
147 down_write(&serio_bus.subsys.rwsem);
148 device_release_driver(&serio->dev);
149 up_write(&serio_bus.subsys.rwsem);
152 static void serio_find_driver(struct serio *serio)
156 down_write(&serio_bus.subsys.rwsem);
157 error = device_attach(&serio->dev);
160 "serio: device_attach() failed for %s (%s), error: %d\n",
161 serio->phys, serio->name, error);
162 up_write(&serio_bus.subsys.rwsem);
167 * Serio event processing.
170 enum serio_event_type {
172 SERIO_RECONNECT_PORT,
178 enum serio_event_type type;
180 struct module *owner;
181 struct list_head node;
184 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
185 static LIST_HEAD(serio_event_list);
186 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
187 static struct task_struct *serio_task;
189 static int serio_queue_event(void *object, struct module *owner,
190 enum serio_event_type event_type)
193 struct serio_event *event;
196 spin_lock_irqsave(&serio_event_lock, flags);
199 * Scan event list for the other events for the same serio port,
200 * starting with the most recent one. If event is the same we
201 * do not need add new one. If event is of different type we
202 * need to add this event and should not look further because
203 * we need to preseve sequence of distinct events.
205 list_for_each_entry_reverse(event, &serio_event_list, node) {
206 if (event->object == object) {
207 if (event->type == event_type)
213 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
216 "serio: Not enough memory to queue event %d\n",
222 if (!try_module_get(owner)) {
224 "serio: Can't get module reference, dropping event %d\n",
231 event->type = event_type;
232 event->object = object;
233 event->owner = owner;
235 list_add_tail(&event->node, &serio_event_list);
236 wake_up(&serio_wait);
239 spin_unlock_irqrestore(&serio_event_lock, flags);
243 static void serio_free_event(struct serio_event *event)
245 module_put(event->owner);
249 static void serio_remove_duplicate_events(struct serio_event *event)
251 struct list_head *node, *next;
252 struct serio_event *e;
255 spin_lock_irqsave(&serio_event_lock, flags);
257 list_for_each_safe(node, next, &serio_event_list) {
258 e = list_entry(node, struct serio_event, node);
259 if (event->object == e->object) {
261 * If this event is of different type we should not
262 * look further - we only suppress duplicate events
263 * that were sent back-to-back.
265 if (event->type != e->type)
273 spin_unlock_irqrestore(&serio_event_lock, flags);
277 static struct serio_event *serio_get_event(void)
279 struct serio_event *event;
280 struct list_head *node;
283 spin_lock_irqsave(&serio_event_lock, flags);
285 if (list_empty(&serio_event_list)) {
286 spin_unlock_irqrestore(&serio_event_lock, flags);
290 node = serio_event_list.next;
291 event = list_entry(node, struct serio_event, node);
294 spin_unlock_irqrestore(&serio_event_lock, flags);
299 static void serio_handle_event(void)
301 struct serio_event *event;
303 mutex_lock(&serio_mutex);
306 * Note that we handle only one event here to give swsusp
307 * a chance to freeze kseriod thread. Serio events should
308 * be pretty rare so we are not concerned about taking
311 if ((event = serio_get_event())) {
313 switch (event->type) {
314 case SERIO_REGISTER_PORT:
315 serio_add_port(event->object);
318 case SERIO_RECONNECT_PORT:
319 serio_reconnect_port(event->object);
322 case SERIO_RESCAN_PORT:
323 serio_disconnect_port(event->object);
324 serio_find_driver(event->object);
327 case SERIO_ATTACH_DRIVER:
328 serio_attach_driver(event->object);
335 serio_remove_duplicate_events(event);
336 serio_free_event(event);
339 mutex_unlock(&serio_mutex);
343 * Remove all events that have been submitted for a given serio port.
345 static void serio_remove_pending_events(struct serio *serio)
347 struct list_head *node, *next;
348 struct serio_event *event;
351 spin_lock_irqsave(&serio_event_lock, flags);
353 list_for_each_safe(node, next, &serio_event_list) {
354 event = list_entry(node, struct serio_event, node);
355 if (event->object == serio) {
357 serio_free_event(event);
361 spin_unlock_irqrestore(&serio_event_lock, flags);
365 * Destroy child serio port (if any) that has not been fully registered yet.
367 * Note that we rely on the fact that port can have only one child and therefore
368 * only one child registration request can be pending. Additionally, children
369 * are registered by driver's connect() handler so there can't be a grandchild
370 * pending registration together with a child.
372 static struct serio *serio_get_pending_child(struct serio *parent)
374 struct serio_event *event;
375 struct serio *serio, *child = NULL;
378 spin_lock_irqsave(&serio_event_lock, flags);
380 list_for_each_entry(event, &serio_event_list, node) {
381 if (event->type == SERIO_REGISTER_PORT) {
382 serio = event->object;
383 if (serio->parent == parent) {
390 spin_unlock_irqrestore(&serio_event_lock, flags);
394 static int serio_thread(void *nothing)
397 serio_handle_event();
398 wait_event_interruptible(serio_wait,
399 kthread_should_stop() || !list_empty(&serio_event_list));
401 } while (!kthread_should_stop());
403 printk(KERN_DEBUG "serio: kseriod exiting\n");
409 * Serio port operations
412 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
414 struct serio *serio = to_serio_port(dev);
415 return sprintf(buf, "%s\n", serio->name);
418 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
420 struct serio *serio = to_serio_port(dev);
422 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
423 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
426 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
428 struct serio *serio = to_serio_port(dev);
429 return sprintf(buf, "%02x\n", serio->id.type);
432 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
434 struct serio *serio = to_serio_port(dev);
435 return sprintf(buf, "%02x\n", serio->id.proto);
438 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
440 struct serio *serio = to_serio_port(dev);
441 return sprintf(buf, "%02x\n", serio->id.id);
444 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
446 struct serio *serio = to_serio_port(dev);
447 return sprintf(buf, "%02x\n", serio->id.extra);
450 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
451 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
452 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
453 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
455 static struct attribute *serio_device_id_attrs[] = {
457 &dev_attr_proto.attr,
459 &dev_attr_extra.attr,
463 static struct attribute_group serio_id_attr_group = {
465 .attrs = serio_device_id_attrs,
468 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
470 struct serio *serio = to_serio_port(dev);
471 struct device_driver *drv;
474 retval = mutex_lock_interruptible(&serio_mutex);
479 if (!strncmp(buf, "none", count)) {
480 serio_disconnect_port(serio);
481 } else if (!strncmp(buf, "reconnect", count)) {
482 serio_reconnect_port(serio);
483 } else if (!strncmp(buf, "rescan", count)) {
484 serio_disconnect_port(serio);
485 serio_find_driver(serio);
486 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
487 serio_disconnect_port(serio);
488 serio_bind_driver(serio, to_serio_driver(drv));
494 mutex_unlock(&serio_mutex);
499 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
501 struct serio *serio = to_serio_port(dev);
502 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
505 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
507 struct serio *serio = to_serio_port(dev);
511 if (!strncmp(buf, "manual", count)) {
512 serio->manual_bind = 1;
513 } else if (!strncmp(buf, "auto", count)) {
514 serio->manual_bind = 0;
522 static struct device_attribute serio_device_attrs[] = {
523 __ATTR(description, S_IRUGO, serio_show_description, NULL),
524 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
525 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
526 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
531 static void serio_release_port(struct device *dev)
533 struct serio *serio = to_serio_port(dev);
536 module_put(THIS_MODULE);
540 * Prepare serio port for registration.
542 static void serio_init_port(struct serio *serio)
544 static atomic_t serio_no = ATOMIC_INIT(0);
546 __module_get(THIS_MODULE);
548 INIT_LIST_HEAD(&serio->node);
549 spin_lock_init(&serio->lock);
550 mutex_init(&serio->drv_mutex);
551 device_initialize(&serio->dev);
552 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
553 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
554 serio->dev.bus = &serio_bus;
555 serio->dev.release = serio_release_port;
557 serio->dev.parent = &serio->parent->dev;
558 serio->depth = serio->parent->depth + 1;
561 lockdep_set_subclass(&serio->lock, serio->depth);
565 * Complete serio port registration.
566 * Driver core will attempt to find appropriate driver for the port.
568 static void serio_add_port(struct serio *serio)
573 serio_pause_rx(serio->parent);
574 serio->parent->child = serio;
575 serio_continue_rx(serio->parent);
578 list_add_tail(&serio->node, &serio_list);
581 error = device_add(&serio->dev);
584 "serio: device_add() failed for %s (%s), error: %d\n",
585 serio->phys, serio->name, error);
587 serio->registered = 1;
588 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
591 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
592 serio->phys, serio->name, error);
597 * serio_destroy_port() completes deregistration process and removes
598 * port from the system
600 static void serio_destroy_port(struct serio *serio)
604 child = serio_get_pending_child(serio);
606 serio_remove_pending_events(child);
607 put_device(&child->dev);
614 serio_pause_rx(serio->parent);
615 serio->parent->child = NULL;
616 serio_continue_rx(serio->parent);
617 serio->parent = NULL;
620 if (serio->registered) {
621 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
622 device_del(&serio->dev);
623 serio->registered = 0;
626 list_del_init(&serio->node);
627 serio_remove_pending_events(serio);
628 put_device(&serio->dev);
632 * Reconnect serio port and all its children (re-initialize attached devices)
634 static void serio_reconnect_port(struct serio *serio)
637 if (serio_reconnect_driver(serio)) {
638 serio_disconnect_port(serio);
639 serio_find_driver(serio);
640 /* Ok, old children are now gone, we are done */
643 serio = serio->child;
648 * serio_disconnect_port() unbinds a port from its driver. As a side effect
649 * all child ports are unbound and destroyed.
651 static void serio_disconnect_port(struct serio *serio)
653 struct serio *s, *parent;
657 * Children ports should be disconnected and destroyed
658 * first, staring with the leaf one, since we don't want
661 for (s = serio; s->child; s = s->child)
667 serio_release_driver(s);
668 serio_destroy_port(s);
669 } while ((s = parent) != serio);
673 * Ok, no children left, now disconnect this port
675 serio_release_driver(serio);
678 void serio_rescan(struct serio *serio)
680 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
683 void serio_reconnect(struct serio *serio)
685 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
689 * Submits register request to kseriod for subsequent execution.
690 * Note that port registration is always asynchronous.
692 void __serio_register_port(struct serio *serio, struct module *owner)
694 serio_init_port(serio);
695 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
699 * Synchronously unregisters serio port.
701 void serio_unregister_port(struct serio *serio)
703 mutex_lock(&serio_mutex);
704 serio_disconnect_port(serio);
705 serio_destroy_port(serio);
706 mutex_unlock(&serio_mutex);
710 * Safely unregisters child port if one is present.
712 void serio_unregister_child_port(struct serio *serio)
714 mutex_lock(&serio_mutex);
716 serio_disconnect_port(serio->child);
717 serio_destroy_port(serio->child);
719 mutex_unlock(&serio_mutex);
724 * Serio driver operations
727 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
729 struct serio_driver *driver = to_serio_driver(drv);
730 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
733 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
735 struct serio_driver *serio_drv = to_serio_driver(drv);
736 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
739 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
741 struct serio_driver *serio_drv = to_serio_driver(drv);
745 if (!strncmp(buf, "manual", count)) {
746 serio_drv->manual_bind = 1;
747 } else if (!strncmp(buf, "auto", count)) {
748 serio_drv->manual_bind = 0;
757 static struct driver_attribute serio_driver_attrs[] = {
758 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
759 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
760 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
764 static int serio_driver_probe(struct device *dev)
766 struct serio *serio = to_serio_port(dev);
767 struct serio_driver *drv = to_serio_driver(dev->driver);
769 return serio_connect_driver(serio, drv);
772 static int serio_driver_remove(struct device *dev)
774 struct serio *serio = to_serio_port(dev);
776 serio_disconnect_driver(serio);
780 static void serio_attach_driver(struct serio_driver *drv)
784 error = driver_attach(&drv->driver);
787 "serio: driver_attach() failed for %s with error %d\n",
788 drv->driver.name, error);
791 int serio_register_driver(struct serio_driver *drv)
793 int manual_bind = drv->manual_bind;
796 drv->driver.bus = &serio_bus;
799 * Temporarily disable automatic binding because probing
800 * takes long time and we are better off doing it in kseriod
802 drv->manual_bind = 1;
804 error = driver_register(&drv->driver);
807 "serio: driver_register() failed for %s, error: %d\n",
808 drv->driver.name, error);
813 * Restore original bind mode and let kseriod bind the
814 * driver to free ports
817 drv->manual_bind = 0;
818 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
820 driver_unregister(&drv->driver);
828 void serio_unregister_driver(struct serio_driver *drv)
832 mutex_lock(&serio_mutex);
833 drv->manual_bind = 1; /* so serio_find_driver ignores it */
836 list_for_each_entry(serio, &serio_list, node) {
837 if (serio->drv == drv) {
838 serio_disconnect_port(serio);
839 serio_find_driver(serio);
840 /* we could've deleted some ports, restart */
845 driver_unregister(&drv->driver);
846 mutex_unlock(&serio_mutex);
849 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
851 serio_pause_rx(serio);
853 serio_continue_rx(serio);
856 static int serio_bus_match(struct device *dev, struct device_driver *drv)
858 struct serio *serio = to_serio_port(dev);
859 struct serio_driver *serio_drv = to_serio_driver(drv);
861 if (serio->manual_bind || serio_drv->manual_bind)
864 return serio_match_port(serio_drv->id_table, serio);
867 #ifdef CONFIG_HOTPLUG
869 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
871 int err = add_uevent_var(envp, num_envp, &i, \
872 buffer, buffer_size, &len, \
878 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
887 serio = to_serio_port(dev);
889 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
890 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
891 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
892 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
893 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
894 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
899 #undef SERIO_ADD_UEVENT_VAR
903 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
908 #endif /* CONFIG_HOTPLUG */
910 static int serio_resume(struct device *dev)
912 struct serio *serio = to_serio_port(dev);
914 if (serio_reconnect_driver(serio)) {
916 * Driver re-probing can take a while, so better let kseriod
925 /* called from serio_driver->connect/disconnect methods under serio_mutex */
926 int serio_open(struct serio *serio, struct serio_driver *drv)
928 serio_set_drv(serio, drv);
930 if (serio->open && serio->open(serio)) {
931 serio_set_drv(serio, NULL);
937 /* called from serio_driver->connect/disconnect methods under serio_mutex */
938 void serio_close(struct serio *serio)
943 serio_set_drv(serio, NULL);
946 irqreturn_t serio_interrupt(struct serio *serio,
947 unsigned char data, unsigned int dfl)
950 irqreturn_t ret = IRQ_NONE;
952 spin_lock_irqsave(&serio->lock, flags);
954 if (likely(serio->drv)) {
955 ret = serio->drv->interrupt(serio, data, dfl);
956 } else if (!dfl && serio->registered) {
961 spin_unlock_irqrestore(&serio->lock, flags);
966 static struct bus_type serio_bus = {
968 .dev_attrs = serio_device_attrs,
969 .drv_attrs = serio_driver_attrs,
970 .match = serio_bus_match,
971 .uevent = serio_uevent,
972 .probe = serio_driver_probe,
973 .remove = serio_driver_remove,
974 .resume = serio_resume,
977 static int __init serio_init(void)
981 error = bus_register(&serio_bus);
983 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
987 serio_task = kthread_run(serio_thread, NULL, "kseriod");
988 if (IS_ERR(serio_task)) {
989 bus_unregister(&serio_bus);
990 error = PTR_ERR(serio_task);
991 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
998 static void __exit serio_exit(void)
1000 bus_unregister(&serio_bus);
1001 kthread_stop(serio_task);
1004 subsys_initcall(serio_init);
1005 module_exit(serio_exit);