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_register_driver);
49 EXPORT_SYMBOL(serio_unregister_driver);
50 EXPORT_SYMBOL(serio_open);
51 EXPORT_SYMBOL(serio_close);
52 EXPORT_SYMBOL(serio_rescan);
53 EXPORT_SYMBOL(serio_reconnect);
56 * serio_mutex protects entire serio subsystem and is taken every time
57 * serio port or driver registrered or unregistered.
59 static DEFINE_MUTEX(serio_mutex);
61 static LIST_HEAD(serio_list);
63 static struct bus_type serio_bus;
65 static void serio_add_port(struct serio *serio);
66 static void serio_reconnect_port(struct serio *serio);
67 static void serio_disconnect_port(struct serio *serio);
68 static void serio_attach_driver(struct serio_driver *drv);
70 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
74 mutex_lock(&serio->drv_mutex);
75 retval = drv->connect(serio, drv);
76 mutex_unlock(&serio->drv_mutex);
81 static int serio_reconnect_driver(struct serio *serio)
85 mutex_lock(&serio->drv_mutex);
86 if (serio->drv && serio->drv->reconnect)
87 retval = serio->drv->reconnect(serio);
88 mutex_unlock(&serio->drv_mutex);
93 static void serio_disconnect_driver(struct serio *serio)
95 mutex_lock(&serio->drv_mutex);
97 serio->drv->disconnect(serio);
98 mutex_unlock(&serio->drv_mutex);
101 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
103 while (ids->type || ids->proto) {
104 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
105 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
106 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
107 (ids->id == SERIO_ANY || ids->id == serio->id.id))
115 * Basic serio -> driver core mappings
118 static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
122 down_write(&serio_bus.subsys.rwsem);
124 if (serio_match_port(drv->id_table, serio)) {
125 serio->dev.driver = &drv->driver;
126 if (serio_connect_driver(serio, drv)) {
127 serio->dev.driver = NULL;
130 error = device_bind_driver(&serio->dev);
133 "serio: device_bind_driver() failed "
134 "for %s (%s) and %s, error: %d\n",
135 serio->phys, serio->name,
136 drv->description, error);
137 serio_disconnect_driver(serio);
138 serio->dev.driver = NULL;
143 up_write(&serio_bus.subsys.rwsem);
146 static void serio_release_driver(struct serio *serio)
148 down_write(&serio_bus.subsys.rwsem);
149 device_release_driver(&serio->dev);
150 up_write(&serio_bus.subsys.rwsem);
153 static void serio_find_driver(struct serio *serio)
157 down_write(&serio_bus.subsys.rwsem);
158 error = device_attach(&serio->dev);
161 "serio: device_attach() failed for %s (%s), error: %d\n",
162 serio->phys, serio->name, error);
163 up_write(&serio_bus.subsys.rwsem);
168 * Serio event processing.
171 enum serio_event_type {
173 SERIO_RECONNECT_PORT,
179 enum serio_event_type type;
181 struct module *owner;
182 struct list_head node;
185 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
186 static LIST_HEAD(serio_event_list);
187 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
188 static struct task_struct *serio_task;
190 static int serio_queue_event(void *object, struct module *owner,
191 enum serio_event_type event_type)
194 struct serio_event *event;
197 spin_lock_irqsave(&serio_event_lock, flags);
200 * Scan event list for the other events for the same serio port,
201 * starting with the most recent one. If event is the same we
202 * do not need add new one. If event is of different type we
203 * need to add this event and should not look further because
204 * we need to preseve sequence of distinct events.
206 list_for_each_entry_reverse(event, &serio_event_list, node) {
207 if (event->object == object) {
208 if (event->type == event_type)
214 event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
217 "serio: Not enough memory to queue event %d\n",
223 if (!try_module_get(owner)) {
225 "serio: Can't get module reference, dropping event %d\n",
232 event->type = event_type;
233 event->object = object;
234 event->owner = owner;
236 list_add_tail(&event->node, &serio_event_list);
237 wake_up(&serio_wait);
240 spin_unlock_irqrestore(&serio_event_lock, flags);
244 static void serio_free_event(struct serio_event *event)
246 module_put(event->owner);
250 static void serio_remove_duplicate_events(struct serio_event *event)
252 struct list_head *node, *next;
253 struct serio_event *e;
256 spin_lock_irqsave(&serio_event_lock, flags);
258 list_for_each_safe(node, next, &serio_event_list) {
259 e = list_entry(node, struct serio_event, node);
260 if (event->object == e->object) {
262 * If this event is of different type we should not
263 * look further - we only suppress duplicate events
264 * that were sent back-to-back.
266 if (event->type != e->type)
274 spin_unlock_irqrestore(&serio_event_lock, flags);
278 static struct serio_event *serio_get_event(void)
280 struct serio_event *event;
281 struct list_head *node;
284 spin_lock_irqsave(&serio_event_lock, flags);
286 if (list_empty(&serio_event_list)) {
287 spin_unlock_irqrestore(&serio_event_lock, flags);
291 node = serio_event_list.next;
292 event = list_entry(node, struct serio_event, node);
295 spin_unlock_irqrestore(&serio_event_lock, flags);
300 static void serio_handle_event(void)
302 struct serio_event *event;
304 mutex_lock(&serio_mutex);
307 * Note that we handle only one event here to give swsusp
308 * a chance to freeze kseriod thread. Serio events should
309 * be pretty rare so we are not concerned about taking
312 if ((event = serio_get_event())) {
314 switch (event->type) {
315 case SERIO_REGISTER_PORT:
316 serio_add_port(event->object);
319 case SERIO_RECONNECT_PORT:
320 serio_reconnect_port(event->object);
323 case SERIO_RESCAN_PORT:
324 serio_disconnect_port(event->object);
325 serio_find_driver(event->object);
328 case SERIO_ATTACH_DRIVER:
329 serio_attach_driver(event->object);
336 serio_remove_duplicate_events(event);
337 serio_free_event(event);
340 mutex_unlock(&serio_mutex);
344 * Remove all events that have been submitted for a given serio port.
346 static void serio_remove_pending_events(struct serio *serio)
348 struct list_head *node, *next;
349 struct serio_event *event;
352 spin_lock_irqsave(&serio_event_lock, flags);
354 list_for_each_safe(node, next, &serio_event_list) {
355 event = list_entry(node, struct serio_event, node);
356 if (event->object == serio) {
358 serio_free_event(event);
362 spin_unlock_irqrestore(&serio_event_lock, flags);
366 * Destroy child serio port (if any) that has not been fully registered yet.
368 * Note that we rely on the fact that port can have only one child and therefore
369 * only one child registration request can be pending. Additionally, children
370 * are registered by driver's connect() handler so there can't be a grandchild
371 * pending registration together with a child.
373 static struct serio *serio_get_pending_child(struct serio *parent)
375 struct serio_event *event;
376 struct serio *serio, *child = NULL;
379 spin_lock_irqsave(&serio_event_lock, flags);
381 list_for_each_entry(event, &serio_event_list, node) {
382 if (event->type == SERIO_REGISTER_PORT) {
383 serio = event->object;
384 if (serio->parent == parent) {
391 spin_unlock_irqrestore(&serio_event_lock, flags);
395 static int serio_thread(void *nothing)
398 serio_handle_event();
399 wait_event_interruptible(serio_wait,
400 kthread_should_stop() || !list_empty(&serio_event_list));
402 } while (!kthread_should_stop());
404 printk(KERN_DEBUG "serio: kseriod exiting\n");
410 * Serio port operations
413 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
415 struct serio *serio = to_serio_port(dev);
416 return sprintf(buf, "%s\n", serio->name);
419 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
421 struct serio *serio = to_serio_port(dev);
423 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
424 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
427 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
429 struct serio *serio = to_serio_port(dev);
430 return sprintf(buf, "%02x\n", serio->id.type);
433 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
435 struct serio *serio = to_serio_port(dev);
436 return sprintf(buf, "%02x\n", serio->id.proto);
439 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
441 struct serio *serio = to_serio_port(dev);
442 return sprintf(buf, "%02x\n", serio->id.id);
445 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
447 struct serio *serio = to_serio_port(dev);
448 return sprintf(buf, "%02x\n", serio->id.extra);
451 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
452 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
453 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
454 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
456 static struct attribute *serio_device_id_attrs[] = {
458 &dev_attr_proto.attr,
460 &dev_attr_extra.attr,
464 static struct attribute_group serio_id_attr_group = {
466 .attrs = serio_device_id_attrs,
469 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
471 struct serio *serio = to_serio_port(dev);
472 struct device_driver *drv;
475 retval = mutex_lock_interruptible(&serio_mutex);
480 if (!strncmp(buf, "none", count)) {
481 serio_disconnect_port(serio);
482 } else if (!strncmp(buf, "reconnect", count)) {
483 serio_reconnect_port(serio);
484 } else if (!strncmp(buf, "rescan", count)) {
485 serio_disconnect_port(serio);
486 serio_find_driver(serio);
487 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
488 serio_disconnect_port(serio);
489 serio_bind_driver(serio, to_serio_driver(drv));
495 mutex_unlock(&serio_mutex);
500 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
502 struct serio *serio = to_serio_port(dev);
503 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
506 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
508 struct serio *serio = to_serio_port(dev);
512 if (!strncmp(buf, "manual", count)) {
513 serio->manual_bind = 1;
514 } else if (!strncmp(buf, "auto", count)) {
515 serio->manual_bind = 0;
523 static struct device_attribute serio_device_attrs[] = {
524 __ATTR(description, S_IRUGO, serio_show_description, NULL),
525 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
526 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
527 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
532 static void serio_release_port(struct device *dev)
534 struct serio *serio = to_serio_port(dev);
537 module_put(THIS_MODULE);
541 * Prepare serio port for registration.
543 static void serio_init_port(struct serio *serio)
545 static atomic_t serio_no = ATOMIC_INIT(0);
547 __module_get(THIS_MODULE);
549 INIT_LIST_HEAD(&serio->node);
550 spin_lock_init(&serio->lock);
551 mutex_init(&serio->drv_mutex);
552 device_initialize(&serio->dev);
553 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
554 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
555 serio->dev.bus = &serio_bus;
556 serio->dev.release = serio_release_port;
558 serio->dev.parent = &serio->parent->dev;
559 serio->depth = serio->parent->depth + 1;
562 lockdep_set_subclass(&serio->lock, serio->depth);
566 * Complete serio port registration.
567 * Driver core will attempt to find appropriate driver for the port.
569 static void serio_add_port(struct serio *serio)
574 serio_pause_rx(serio->parent);
575 serio->parent->child = serio;
576 serio_continue_rx(serio->parent);
579 list_add_tail(&serio->node, &serio_list);
582 error = device_add(&serio->dev);
585 "serio: device_add() failed for %s (%s), error: %d\n",
586 serio->phys, serio->name, error);
588 serio->registered = 1;
589 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
592 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
593 serio->phys, serio->name, error);
598 * serio_destroy_port() completes deregistration process and removes
599 * port from the system
601 static void serio_destroy_port(struct serio *serio)
605 child = serio_get_pending_child(serio);
607 serio_remove_pending_events(child);
608 put_device(&child->dev);
615 serio_pause_rx(serio->parent);
616 serio->parent->child = NULL;
617 serio_continue_rx(serio->parent);
618 serio->parent = NULL;
621 if (serio->registered) {
622 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
623 device_del(&serio->dev);
624 serio->registered = 0;
627 list_del_init(&serio->node);
628 serio_remove_pending_events(serio);
629 put_device(&serio->dev);
633 * Reconnect serio port and all its children (re-initialize attached devices)
635 static void serio_reconnect_port(struct serio *serio)
638 if (serio_reconnect_driver(serio)) {
639 serio_disconnect_port(serio);
640 serio_find_driver(serio);
641 /* Ok, old children are now gone, we are done */
644 serio = serio->child;
649 * serio_disconnect_port() unbinds a port from its driver. As a side effect
650 * all child ports are unbound and destroyed.
652 static void serio_disconnect_port(struct serio *serio)
654 struct serio *s, *parent;
658 * Children ports should be disconnected and destroyed
659 * first, staring with the leaf one, since we don't want
662 for (s = serio; s->child; s = s->child)
668 serio_release_driver(s);
669 serio_destroy_port(s);
670 } while ((s = parent) != serio);
674 * Ok, no children left, now disconnect this port
676 serio_release_driver(serio);
679 void serio_rescan(struct serio *serio)
681 serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
684 void serio_reconnect(struct serio *serio)
686 serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
690 * Submits register request to kseriod for subsequent execution.
691 * Note that port registration is always asynchronous.
693 void __serio_register_port(struct serio *serio, struct module *owner)
695 serio_init_port(serio);
696 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
700 * Synchronously unregisters serio port.
702 void serio_unregister_port(struct serio *serio)
704 mutex_lock(&serio_mutex);
705 serio_disconnect_port(serio);
706 serio_destroy_port(serio);
707 mutex_unlock(&serio_mutex);
711 * Safely unregisters child port if one is present.
713 void serio_unregister_child_port(struct serio *serio)
715 mutex_lock(&serio_mutex);
717 serio_disconnect_port(serio->child);
718 serio_destroy_port(serio->child);
720 mutex_unlock(&serio_mutex);
725 * Serio driver operations
728 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
730 struct serio_driver *driver = to_serio_driver(drv);
731 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
734 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
736 struct serio_driver *serio_drv = to_serio_driver(drv);
737 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
740 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
742 struct serio_driver *serio_drv = to_serio_driver(drv);
746 if (!strncmp(buf, "manual", count)) {
747 serio_drv->manual_bind = 1;
748 } else if (!strncmp(buf, "auto", count)) {
749 serio_drv->manual_bind = 0;
758 static struct driver_attribute serio_driver_attrs[] = {
759 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
760 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
761 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
765 static int serio_driver_probe(struct device *dev)
767 struct serio *serio = to_serio_port(dev);
768 struct serio_driver *drv = to_serio_driver(dev->driver);
770 return serio_connect_driver(serio, drv);
773 static int serio_driver_remove(struct device *dev)
775 struct serio *serio = to_serio_port(dev);
777 serio_disconnect_driver(serio);
781 static void serio_attach_driver(struct serio_driver *drv)
785 error = driver_attach(&drv->driver);
788 "serio: driver_attach() failed for %s with error %d\n",
789 drv->driver.name, error);
792 int serio_register_driver(struct serio_driver *drv)
794 int manual_bind = drv->manual_bind;
797 drv->driver.bus = &serio_bus;
800 * Temporarily disable automatic binding because probing
801 * takes long time and we are better off doing it in kseriod
803 drv->manual_bind = 1;
805 error = driver_register(&drv->driver);
808 "serio: driver_register() failed for %s, error: %d\n",
809 drv->driver.name, error);
814 * Restore original bind mode and let kseriod bind the
815 * driver to free ports
818 drv->manual_bind = 0;
819 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
821 driver_unregister(&drv->driver);
829 void serio_unregister_driver(struct serio_driver *drv)
833 mutex_lock(&serio_mutex);
834 drv->manual_bind = 1; /* so serio_find_driver ignores it */
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 */
846 driver_unregister(&drv->driver);
847 mutex_unlock(&serio_mutex);
850 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
852 serio_pause_rx(serio);
854 serio_continue_rx(serio);
857 static int serio_bus_match(struct device *dev, struct device_driver *drv)
859 struct serio *serio = to_serio_port(dev);
860 struct serio_driver *serio_drv = to_serio_driver(drv);
862 if (serio->manual_bind || serio_drv->manual_bind)
865 return serio_match_port(serio_drv->id_table, serio);
868 #ifdef CONFIG_HOTPLUG
870 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
872 int err = add_uevent_var(envp, num_envp, &i, \
873 buffer, buffer_size, &len, \
879 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
888 serio = to_serio_port(dev);
890 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
891 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
892 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
893 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
894 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
895 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
900 #undef SERIO_ADD_UEVENT_VAR
904 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
909 #endif /* CONFIG_HOTPLUG */
911 static int serio_resume(struct device *dev)
913 struct serio *serio = to_serio_port(dev);
915 if (serio_reconnect_driver(serio)) {
917 * Driver re-probing can take a while, so better let kseriod
926 /* called from serio_driver->connect/disconnect methods under serio_mutex */
927 int serio_open(struct serio *serio, struct serio_driver *drv)
929 serio_set_drv(serio, drv);
931 if (serio->open && serio->open(serio)) {
932 serio_set_drv(serio, NULL);
938 /* called from serio_driver->connect/disconnect methods under serio_mutex */
939 void serio_close(struct serio *serio)
944 serio_set_drv(serio, NULL);
947 irqreturn_t serio_interrupt(struct serio *serio,
948 unsigned char data, unsigned int dfl)
951 irqreturn_t ret = IRQ_NONE;
953 spin_lock_irqsave(&serio->lock, flags);
955 if (likely(serio->drv)) {
956 ret = serio->drv->interrupt(serio, data, dfl);
957 } else if (!dfl && serio->registered) {
962 spin_unlock_irqrestore(&serio->lock, flags);
967 static struct bus_type serio_bus = {
969 .dev_attrs = serio_device_attrs,
970 .drv_attrs = serio_driver_attrs,
971 .match = serio_bus_match,
972 .uevent = serio_uevent,
973 .probe = serio_driver_probe,
974 .remove = serio_driver_remove,
975 .resume = serio_resume,
978 static int __init serio_init(void)
982 error = bus_register(&serio_bus);
984 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
988 serio_task = kthread_run(serio_thread, NULL, "kseriod");
989 if (IS_ERR(serio_task)) {
990 bus_unregister(&serio_bus);
991 error = PTR_ERR(serio_task);
992 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
999 static void __exit serio_exit(void)
1001 bus_unregister(&serio_bus);
1002 kthread_stop(serio_task);
1005 subsys_initcall(serio_init);
1006 module_exit(serio_exit);