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>
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_unregister_port_delayed);
49 EXPORT_SYMBOL(__serio_register_driver);
50 EXPORT_SYMBOL(serio_unregister_driver);
51 EXPORT_SYMBOL(serio_open);
52 EXPORT_SYMBOL(serio_close);
53 EXPORT_SYMBOL(serio_rescan);
54 EXPORT_SYMBOL(serio_reconnect);
57 * serio_mutex protects entire serio subsystem and is taken every time
58 * serio port or driver registrered or unregistered.
60 static DEFINE_MUTEX(serio_mutex);
62 static LIST_HEAD(serio_list);
64 static struct bus_type serio_bus;
66 static void serio_add_driver(struct serio_driver *drv);
67 static void serio_add_port(struct serio *serio);
68 static void serio_destroy_port(struct serio *serio);
69 static void serio_reconnect_port(struct serio *serio);
70 static void serio_disconnect_port(struct serio *serio);
72 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
76 mutex_lock(&serio->drv_mutex);
77 retval = drv->connect(serio, drv);
78 mutex_unlock(&serio->drv_mutex);
83 static int serio_reconnect_driver(struct serio *serio)
87 mutex_lock(&serio->drv_mutex);
88 if (serio->drv && serio->drv->reconnect)
89 retval = serio->drv->reconnect(serio);
90 mutex_unlock(&serio->drv_mutex);
95 static void serio_disconnect_driver(struct serio *serio)
97 mutex_lock(&serio->drv_mutex);
99 serio->drv->disconnect(serio);
100 mutex_unlock(&serio->drv_mutex);
103 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
105 while (ids->type || ids->proto) {
106 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
107 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
108 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
109 (ids->id == SERIO_ANY || ids->id == serio->id.id))
117 * Basic serio -> driver core mappings
120 static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
124 down_write(&serio_bus.subsys.rwsem);
126 if (serio_match_port(drv->id_table, serio)) {
127 serio->dev.driver = &drv->driver;
128 if (serio_connect_driver(serio, drv)) {
129 serio->dev.driver = NULL;
132 error = device_bind_driver(&serio->dev);
135 "serio: device_bind_driver() failed "
136 "for %s (%s) and %s, error: %d\n",
137 serio->phys, serio->name,
138 drv->description, error);
139 serio_disconnect_driver(serio);
140 serio->dev.driver = NULL;
145 up_write(&serio_bus.subsys.rwsem);
148 static void serio_release_driver(struct serio *serio)
150 down_write(&serio_bus.subsys.rwsem);
151 device_release_driver(&serio->dev);
152 up_write(&serio_bus.subsys.rwsem);
155 static void serio_find_driver(struct serio *serio)
159 down_write(&serio_bus.subsys.rwsem);
160 error = device_attach(&serio->dev);
163 "serio: device_attach() failed for %s (%s), error: %d\n",
164 serio->phys, serio->name, error);
165 up_write(&serio_bus.subsys.rwsem);
170 * Serio event processing.
173 enum serio_event_type {
177 SERIO_UNREGISTER_PORT,
178 SERIO_REGISTER_DRIVER,
182 enum serio_event_type type;
184 struct module *owner;
185 struct list_head node;
188 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
189 static LIST_HEAD(serio_event_list);
190 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
191 static struct task_struct *serio_task;
193 static void serio_queue_event(void *object, struct module *owner,
194 enum serio_event_type event_type)
197 struct serio_event *event;
199 spin_lock_irqsave(&serio_event_lock, flags);
202 * Scan event list for the other events for the same serio port,
203 * starting with the most recent one. If event is the same we
204 * do not need add new one. If event is of different type we
205 * need to add this event and should not look further because
206 * we need to preseve sequence of distinct events.
208 list_for_each_entry_reverse(event, &serio_event_list, node) {
209 if (event->object == object) {
210 if (event->type == event_type)
216 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
217 if (!try_module_get(owner)) {
218 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
223 event->type = event_type;
224 event->object = object;
225 event->owner = owner;
227 list_add_tail(&event->node, &serio_event_list);
228 wake_up(&serio_wait);
230 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
233 spin_unlock_irqrestore(&serio_event_lock, flags);
236 static void serio_free_event(struct serio_event *event)
238 module_put(event->owner);
242 static void serio_remove_duplicate_events(struct serio_event *event)
244 struct list_head *node, *next;
245 struct serio_event *e;
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)
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;
276 spin_lock_irqsave(&serio_event_lock, flags);
278 if (list_empty(&serio_event_list)) {
279 spin_unlock_irqrestore(&serio_event_lock, flags);
283 node = serio_event_list.next;
284 event = list_entry(node, struct serio_event, node);
287 spin_unlock_irqrestore(&serio_event_lock, flags);
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
304 if ((event = serio_get_event())) {
306 switch (event->type) {
307 case SERIO_REGISTER_PORT:
308 serio_add_port(event->object);
311 case SERIO_UNREGISTER_PORT:
312 serio_disconnect_port(event->object);
313 serio_destroy_port(event->object);
316 case SERIO_RECONNECT:
317 serio_reconnect_port(event->object);
321 serio_disconnect_port(event->object);
322 serio_find_driver(event->object);
325 case SERIO_REGISTER_DRIVER:
326 serio_add_driver(event->object);
333 serio_remove_duplicate_events(event);
334 serio_free_event(event);
337 mutex_unlock(&serio_mutex);
341 * Remove all events that have been submitted for a given serio port.
343 static void serio_remove_pending_events(struct serio *serio)
345 struct list_head *node, *next;
346 struct serio_event *event;
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 == serio) {
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;
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) {
388 spin_unlock_irqrestore(&serio_event_lock, flags);
392 static int serio_thread(void *nothing)
395 serio_handle_event();
396 wait_event_interruptible(serio_wait,
397 kthread_should_stop() || !list_empty(&serio_event_list));
399 } while (!kthread_should_stop());
401 printk(KERN_DEBUG "serio: kseriod exiting\n");
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[] = {
455 &dev_attr_proto.attr,
457 &dev_attr_extra.attr,
461 static struct attribute_group serio_id_attr_group = {
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;
472 retval = mutex_lock_interruptible(&serio_mutex);
477 if (!strncmp(buf, "none", count)) {
478 serio_disconnect_port(serio);
479 } else if (!strncmp(buf, "reconnect", count)) {
480 serio_reconnect_port(serio);
481 } else if (!strncmp(buf, "rescan", count)) {
482 serio_disconnect_port(serio);
483 serio_find_driver(serio);
484 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
485 serio_disconnect_port(serio);
486 serio_bind_driver(serio, to_serio_driver(drv));
492 mutex_unlock(&serio_mutex);
497 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
499 struct serio *serio = to_serio_port(dev);
500 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
503 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
505 struct serio *serio = to_serio_port(dev);
509 if (!strncmp(buf, "manual", count)) {
510 serio->manual_bind = 1;
511 } else if (!strncmp(buf, "auto", count)) {
512 serio->manual_bind = 0;
520 static struct device_attribute serio_device_attrs[] = {
521 __ATTR(description, S_IRUGO, serio_show_description, NULL),
522 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
523 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
524 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
529 static void serio_release_port(struct device *dev)
531 struct serio *serio = to_serio_port(dev);
534 module_put(THIS_MODULE);
538 * Prepare serio port for registration.
540 static void serio_init_port(struct serio *serio)
542 static atomic_t serio_no = ATOMIC_INIT(0);
544 __module_get(THIS_MODULE);
546 INIT_LIST_HEAD(&serio->node);
547 spin_lock_init(&serio->lock);
548 mutex_init(&serio->drv_mutex);
549 device_initialize(&serio->dev);
550 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
551 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
552 serio->dev.bus = &serio_bus;
553 serio->dev.release = serio_release_port;
555 serio->dev.parent = &serio->parent->dev;
556 serio->depth = serio->parent->depth + 1;
559 lockdep_set_subclass(&serio->lock, serio->depth);
563 * Complete serio port registration.
564 * Driver core will attempt to find appropriate driver for the port.
566 static void serio_add_port(struct serio *serio)
571 serio_pause_rx(serio->parent);
572 serio->parent->child = serio;
573 serio_continue_rx(serio->parent);
576 list_add_tail(&serio->node, &serio_list);
579 error = device_add(&serio->dev);
582 "serio: device_add() failed for %s (%s), error: %d\n",
583 serio->phys, serio->name, error);
585 serio->registered = 1;
586 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
589 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
590 serio->phys, serio->name, error);
595 * serio_destroy_port() completes deregistration process and removes
596 * port from the system
598 static void serio_destroy_port(struct serio *serio)
602 child = serio_get_pending_child(serio);
604 serio_remove_pending_events(child);
605 put_device(&child->dev);
612 serio_pause_rx(serio->parent);
613 serio->parent->child = NULL;
614 serio_continue_rx(serio->parent);
615 serio->parent = NULL;
618 if (serio->registered) {
619 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
620 device_del(&serio->dev);
621 serio->registered = 0;
624 list_del_init(&serio->node);
625 serio_remove_pending_events(serio);
626 put_device(&serio->dev);
630 * Reconnect serio port and all its children (re-initialize attached devices)
632 static void serio_reconnect_port(struct serio *serio)
635 if (serio_reconnect_driver(serio)) {
636 serio_disconnect_port(serio);
637 serio_find_driver(serio);
638 /* Ok, old children are now gone, we are done */
641 serio = serio->child;
646 * serio_disconnect_port() unbinds a port from its driver. As a side effect
647 * all child ports are unbound and destroyed.
649 static void serio_disconnect_port(struct serio *serio)
651 struct serio *s, *parent;
655 * Children ports should be disconnected and destroyed
656 * first, staring with the leaf one, since we don't want
659 for (s = serio; s->child; s = s->child)
665 serio_release_driver(s);
666 serio_destroy_port(s);
667 } while ((s = parent) != serio);
671 * Ok, no children left, now disconnect this port
673 serio_release_driver(serio);
676 void serio_rescan(struct serio *serio)
678 serio_queue_event(serio, NULL, SERIO_RESCAN);
681 void serio_reconnect(struct serio *serio)
683 serio_queue_event(serio, NULL, SERIO_RECONNECT);
687 * Submits register request to kseriod for subsequent execution.
688 * Note that port registration is always asynchronous.
690 void __serio_register_port(struct serio *serio, struct module *owner)
692 serio_init_port(serio);
693 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
697 * Synchronously unregisters serio port.
699 void serio_unregister_port(struct serio *serio)
701 mutex_lock(&serio_mutex);
702 serio_disconnect_port(serio);
703 serio_destroy_port(serio);
704 mutex_unlock(&serio_mutex);
708 * Safely unregisters child port if one is present.
710 void serio_unregister_child_port(struct serio *serio)
712 mutex_lock(&serio_mutex);
714 serio_disconnect_port(serio->child);
715 serio_destroy_port(serio->child);
717 mutex_unlock(&serio_mutex);
721 * Submits register request to kseriod for subsequent execution.
722 * Can be used when it is not obvious whether the serio_mutex is
723 * taken or not and when delayed execution is feasible.
725 void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
727 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
732 * Serio driver operations
735 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
737 struct serio_driver *driver = to_serio_driver(drv);
738 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
741 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
743 struct serio_driver *serio_drv = to_serio_driver(drv);
744 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
747 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
749 struct serio_driver *serio_drv = to_serio_driver(drv);
753 if (!strncmp(buf, "manual", count)) {
754 serio_drv->manual_bind = 1;
755 } else if (!strncmp(buf, "auto", count)) {
756 serio_drv->manual_bind = 0;
765 static struct driver_attribute serio_driver_attrs[] = {
766 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
767 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
768 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
772 static int serio_driver_probe(struct device *dev)
774 struct serio *serio = to_serio_port(dev);
775 struct serio_driver *drv = to_serio_driver(dev->driver);
777 return serio_connect_driver(serio, drv);
780 static int serio_driver_remove(struct device *dev)
782 struct serio *serio = to_serio_port(dev);
784 serio_disconnect_driver(serio);
788 static struct bus_type serio_bus = {
790 .probe = serio_driver_probe,
791 .remove = serio_driver_remove,
794 static void serio_add_driver(struct serio_driver *drv)
798 error = driver_register(&drv->driver);
801 "serio: driver_register() failed for %s, error: %d\n",
802 drv->driver.name, error);
805 void __serio_register_driver(struct serio_driver *drv, struct module *owner)
807 drv->driver.bus = &serio_bus;
809 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
812 void serio_unregister_driver(struct serio_driver *drv)
816 mutex_lock(&serio_mutex);
817 drv->manual_bind = 1; /* so serio_find_driver ignores it */
820 list_for_each_entry(serio, &serio_list, node) {
821 if (serio->drv == drv) {
822 serio_disconnect_port(serio);
823 serio_find_driver(serio);
824 /* we could've deleted some ports, restart */
829 driver_unregister(&drv->driver);
830 mutex_unlock(&serio_mutex);
833 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
835 serio_pause_rx(serio);
837 serio_continue_rx(serio);
840 static int serio_bus_match(struct device *dev, struct device_driver *drv)
842 struct serio *serio = to_serio_port(dev);
843 struct serio_driver *serio_drv = to_serio_driver(drv);
845 if (serio->manual_bind || serio_drv->manual_bind)
848 return serio_match_port(serio_drv->id_table, serio);
851 #ifdef CONFIG_HOTPLUG
853 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
855 int err = add_uevent_var(envp, num_envp, &i, \
856 buffer, buffer_size, &len, \
862 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
871 serio = to_serio_port(dev);
873 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
874 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
875 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
876 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
877 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
878 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
883 #undef SERIO_ADD_UEVENT_VAR
887 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
892 #endif /* CONFIG_HOTPLUG */
894 static int serio_resume(struct device *dev)
896 struct serio *serio = to_serio_port(dev);
898 if (serio_reconnect_driver(serio)) {
900 * Driver re-probing can take a while, so better let kseriod
909 /* called from serio_driver->connect/disconnect methods under serio_mutex */
910 int serio_open(struct serio *serio, struct serio_driver *drv)
912 serio_set_drv(serio, drv);
914 if (serio->open && serio->open(serio)) {
915 serio_set_drv(serio, NULL);
921 /* called from serio_driver->connect/disconnect methods under serio_mutex */
922 void serio_close(struct serio *serio)
927 serio_set_drv(serio, NULL);
930 irqreturn_t serio_interrupt(struct serio *serio,
931 unsigned char data, unsigned int dfl)
934 irqreturn_t ret = IRQ_NONE;
936 spin_lock_irqsave(&serio->lock, flags);
938 if (likely(serio->drv)) {
939 ret = serio->drv->interrupt(serio, data, dfl);
940 } else if (!dfl && serio->registered) {
945 spin_unlock_irqrestore(&serio->lock, flags);
950 static int __init serio_init(void)
954 serio_bus.dev_attrs = serio_device_attrs;
955 serio_bus.drv_attrs = serio_driver_attrs;
956 serio_bus.match = serio_bus_match;
957 serio_bus.uevent = serio_uevent;
958 serio_bus.resume = serio_resume;
959 error = bus_register(&serio_bus);
961 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
965 serio_task = kthread_run(serio_thread, NULL, "kseriod");
966 if (IS_ERR(serio_task)) {
967 bus_unregister(&serio_bus);
968 error = PTR_ERR(serio_task);
969 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
976 static void __exit serio_exit(void)
978 bus_unregister(&serio_bus);
979 kthread_stop(serio_task);
982 subsys_initcall(serio_init);
983 module_exit(serio_exit);