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_driver(struct serio_driver *drv);
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);
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 {
174 SERIO_REGISTER_DRIVER,
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 void serio_queue_event(void *object, struct module *owner,
190 enum serio_event_type event_type)
193 struct serio_event *event;
195 spin_lock_irqsave(&serio_event_lock, flags);
198 * Scan event list for the other events for the same serio port,
199 * starting with the most recent one. If event is the same we
200 * do not need add new one. If event is of different type we
201 * need to add this event and should not look further because
202 * we need to preseve sequence of distinct events.
204 list_for_each_entry_reverse(event, &serio_event_list, node) {
205 if (event->object == object) {
206 if (event->type == event_type)
212 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
213 if (!try_module_get(owner)) {
214 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
219 event->type = event_type;
220 event->object = object;
221 event->owner = owner;
223 list_add_tail(&event->node, &serio_event_list);
224 wake_up(&serio_wait);
226 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
229 spin_unlock_irqrestore(&serio_event_lock, flags);
232 static void serio_free_event(struct serio_event *event)
234 module_put(event->owner);
238 static void serio_remove_duplicate_events(struct serio_event *event)
240 struct list_head *node, *next;
241 struct serio_event *e;
244 spin_lock_irqsave(&serio_event_lock, flags);
246 list_for_each_safe(node, next, &serio_event_list) {
247 e = list_entry(node, struct serio_event, node);
248 if (event->object == e->object) {
250 * If this event is of different type we should not
251 * look further - we only suppress duplicate events
252 * that were sent back-to-back.
254 if (event->type != e->type)
262 spin_unlock_irqrestore(&serio_event_lock, flags);
266 static struct serio_event *serio_get_event(void)
268 struct serio_event *event;
269 struct list_head *node;
272 spin_lock_irqsave(&serio_event_lock, flags);
274 if (list_empty(&serio_event_list)) {
275 spin_unlock_irqrestore(&serio_event_lock, flags);
279 node = serio_event_list.next;
280 event = list_entry(node, struct serio_event, node);
283 spin_unlock_irqrestore(&serio_event_lock, flags);
288 static void serio_handle_event(void)
290 struct serio_event *event;
292 mutex_lock(&serio_mutex);
295 * Note that we handle only one event here to give swsusp
296 * a chance to freeze kseriod thread. Serio events should
297 * be pretty rare so we are not concerned about taking
300 if ((event = serio_get_event())) {
302 switch (event->type) {
303 case SERIO_REGISTER_PORT:
304 serio_add_port(event->object);
307 case SERIO_RECONNECT:
308 serio_reconnect_port(event->object);
312 serio_disconnect_port(event->object);
313 serio_find_driver(event->object);
316 case SERIO_REGISTER_DRIVER:
317 serio_add_driver(event->object);
324 serio_remove_duplicate_events(event);
325 serio_free_event(event);
328 mutex_unlock(&serio_mutex);
332 * Remove all events that have been submitted for a given serio port.
334 static void serio_remove_pending_events(struct serio *serio)
336 struct list_head *node, *next;
337 struct serio_event *event;
340 spin_lock_irqsave(&serio_event_lock, flags);
342 list_for_each_safe(node, next, &serio_event_list) {
343 event = list_entry(node, struct serio_event, node);
344 if (event->object == serio) {
346 serio_free_event(event);
350 spin_unlock_irqrestore(&serio_event_lock, flags);
354 * Destroy child serio port (if any) that has not been fully registered yet.
356 * Note that we rely on the fact that port can have only one child and therefore
357 * only one child registration request can be pending. Additionally, children
358 * are registered by driver's connect() handler so there can't be a grandchild
359 * pending registration together with a child.
361 static struct serio *serio_get_pending_child(struct serio *parent)
363 struct serio_event *event;
364 struct serio *serio, *child = NULL;
367 spin_lock_irqsave(&serio_event_lock, flags);
369 list_for_each_entry(event, &serio_event_list, node) {
370 if (event->type == SERIO_REGISTER_PORT) {
371 serio = event->object;
372 if (serio->parent == parent) {
379 spin_unlock_irqrestore(&serio_event_lock, flags);
383 static int serio_thread(void *nothing)
386 serio_handle_event();
387 wait_event_interruptible(serio_wait,
388 kthread_should_stop() || !list_empty(&serio_event_list));
390 } while (!kthread_should_stop());
392 printk(KERN_DEBUG "serio: kseriod exiting\n");
398 * Serio port operations
401 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
403 struct serio *serio = to_serio_port(dev);
404 return sprintf(buf, "%s\n", serio->name);
407 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
409 struct serio *serio = to_serio_port(dev);
411 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
412 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
415 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
417 struct serio *serio = to_serio_port(dev);
418 return sprintf(buf, "%02x\n", serio->id.type);
421 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
423 struct serio *serio = to_serio_port(dev);
424 return sprintf(buf, "%02x\n", serio->id.proto);
427 static ssize_t serio_show_id_id(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.id);
433 static ssize_t serio_show_id_extra(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.extra);
439 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
440 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
441 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
442 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
444 static struct attribute *serio_device_id_attrs[] = {
446 &dev_attr_proto.attr,
448 &dev_attr_extra.attr,
452 static struct attribute_group serio_id_attr_group = {
454 .attrs = serio_device_id_attrs,
457 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
459 struct serio *serio = to_serio_port(dev);
460 struct device_driver *drv;
463 retval = mutex_lock_interruptible(&serio_mutex);
468 if (!strncmp(buf, "none", count)) {
469 serio_disconnect_port(serio);
470 } else if (!strncmp(buf, "reconnect", count)) {
471 serio_reconnect_port(serio);
472 } else if (!strncmp(buf, "rescan", count)) {
473 serio_disconnect_port(serio);
474 serio_find_driver(serio);
475 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
476 serio_disconnect_port(serio);
477 serio_bind_driver(serio, to_serio_driver(drv));
483 mutex_unlock(&serio_mutex);
488 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
490 struct serio *serio = to_serio_port(dev);
491 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
494 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
496 struct serio *serio = to_serio_port(dev);
500 if (!strncmp(buf, "manual", count)) {
501 serio->manual_bind = 1;
502 } else if (!strncmp(buf, "auto", count)) {
503 serio->manual_bind = 0;
511 static struct device_attribute serio_device_attrs[] = {
512 __ATTR(description, S_IRUGO, serio_show_description, NULL),
513 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
514 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
515 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
520 static void serio_release_port(struct device *dev)
522 struct serio *serio = to_serio_port(dev);
525 module_put(THIS_MODULE);
529 * Prepare serio port for registration.
531 static void serio_init_port(struct serio *serio)
533 static atomic_t serio_no = ATOMIC_INIT(0);
535 __module_get(THIS_MODULE);
537 INIT_LIST_HEAD(&serio->node);
538 spin_lock_init(&serio->lock);
539 mutex_init(&serio->drv_mutex);
540 device_initialize(&serio->dev);
541 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
542 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
543 serio->dev.bus = &serio_bus;
544 serio->dev.release = serio_release_port;
546 serio->dev.parent = &serio->parent->dev;
547 serio->depth = serio->parent->depth + 1;
550 lockdep_set_subclass(&serio->lock, serio->depth);
554 * Complete serio port registration.
555 * Driver core will attempt to find appropriate driver for the port.
557 static void serio_add_port(struct serio *serio)
562 serio_pause_rx(serio->parent);
563 serio->parent->child = serio;
564 serio_continue_rx(serio->parent);
567 list_add_tail(&serio->node, &serio_list);
570 error = device_add(&serio->dev);
573 "serio: device_add() failed for %s (%s), error: %d\n",
574 serio->phys, serio->name, error);
576 serio->registered = 1;
577 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
580 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
581 serio->phys, serio->name, error);
586 * serio_destroy_port() completes deregistration process and removes
587 * port from the system
589 static void serio_destroy_port(struct serio *serio)
593 child = serio_get_pending_child(serio);
595 serio_remove_pending_events(child);
596 put_device(&child->dev);
603 serio_pause_rx(serio->parent);
604 serio->parent->child = NULL;
605 serio_continue_rx(serio->parent);
606 serio->parent = NULL;
609 if (serio->registered) {
610 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
611 device_del(&serio->dev);
612 serio->registered = 0;
615 list_del_init(&serio->node);
616 serio_remove_pending_events(serio);
617 put_device(&serio->dev);
621 * Reconnect serio port and all its children (re-initialize attached devices)
623 static void serio_reconnect_port(struct serio *serio)
626 if (serio_reconnect_driver(serio)) {
627 serio_disconnect_port(serio);
628 serio_find_driver(serio);
629 /* Ok, old children are now gone, we are done */
632 serio = serio->child;
637 * serio_disconnect_port() unbinds a port from its driver. As a side effect
638 * all child ports are unbound and destroyed.
640 static void serio_disconnect_port(struct serio *serio)
642 struct serio *s, *parent;
646 * Children ports should be disconnected and destroyed
647 * first, staring with the leaf one, since we don't want
650 for (s = serio; s->child; s = s->child)
656 serio_release_driver(s);
657 serio_destroy_port(s);
658 } while ((s = parent) != serio);
662 * Ok, no children left, now disconnect this port
664 serio_release_driver(serio);
667 void serio_rescan(struct serio *serio)
669 serio_queue_event(serio, NULL, SERIO_RESCAN);
672 void serio_reconnect(struct serio *serio)
674 serio_queue_event(serio, NULL, SERIO_RECONNECT);
678 * Submits register request to kseriod for subsequent execution.
679 * Note that port registration is always asynchronous.
681 void __serio_register_port(struct serio *serio, struct module *owner)
683 serio_init_port(serio);
684 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
688 * Synchronously unregisters serio port.
690 void serio_unregister_port(struct serio *serio)
692 mutex_lock(&serio_mutex);
693 serio_disconnect_port(serio);
694 serio_destroy_port(serio);
695 mutex_unlock(&serio_mutex);
699 * Safely unregisters child port if one is present.
701 void serio_unregister_child_port(struct serio *serio)
703 mutex_lock(&serio_mutex);
705 serio_disconnect_port(serio->child);
706 serio_destroy_port(serio->child);
708 mutex_unlock(&serio_mutex);
713 * Serio driver operations
716 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
718 struct serio_driver *driver = to_serio_driver(drv);
719 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
722 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
724 struct serio_driver *serio_drv = to_serio_driver(drv);
725 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
728 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
730 struct serio_driver *serio_drv = to_serio_driver(drv);
734 if (!strncmp(buf, "manual", count)) {
735 serio_drv->manual_bind = 1;
736 } else if (!strncmp(buf, "auto", count)) {
737 serio_drv->manual_bind = 0;
746 static struct driver_attribute serio_driver_attrs[] = {
747 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
748 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
749 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
753 static int serio_driver_probe(struct device *dev)
755 struct serio *serio = to_serio_port(dev);
756 struct serio_driver *drv = to_serio_driver(dev->driver);
758 return serio_connect_driver(serio, drv);
761 static int serio_driver_remove(struct device *dev)
763 struct serio *serio = to_serio_port(dev);
765 serio_disconnect_driver(serio);
769 static void serio_add_driver(struct serio_driver *drv)
773 error = driver_register(&drv->driver);
776 "serio: driver_register() failed for %s, error: %d\n",
777 drv->driver.name, error);
780 void __serio_register_driver(struct serio_driver *drv, struct module *owner)
782 drv->driver.bus = &serio_bus;
784 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
787 void serio_unregister_driver(struct serio_driver *drv)
791 mutex_lock(&serio_mutex);
792 drv->manual_bind = 1; /* so serio_find_driver ignores it */
795 list_for_each_entry(serio, &serio_list, node) {
796 if (serio->drv == drv) {
797 serio_disconnect_port(serio);
798 serio_find_driver(serio);
799 /* we could've deleted some ports, restart */
804 driver_unregister(&drv->driver);
805 mutex_unlock(&serio_mutex);
808 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
810 serio_pause_rx(serio);
812 serio_continue_rx(serio);
815 static int serio_bus_match(struct device *dev, struct device_driver *drv)
817 struct serio *serio = to_serio_port(dev);
818 struct serio_driver *serio_drv = to_serio_driver(drv);
820 if (serio->manual_bind || serio_drv->manual_bind)
823 return serio_match_port(serio_drv->id_table, serio);
826 #ifdef CONFIG_HOTPLUG
828 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
830 int err = add_uevent_var(envp, num_envp, &i, \
831 buffer, buffer_size, &len, \
837 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
846 serio = to_serio_port(dev);
848 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
849 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
850 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
851 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
852 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
853 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
858 #undef SERIO_ADD_UEVENT_VAR
862 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
867 #endif /* CONFIG_HOTPLUG */
869 static int serio_resume(struct device *dev)
871 struct serio *serio = to_serio_port(dev);
873 if (serio_reconnect_driver(serio)) {
875 * Driver re-probing can take a while, so better let kseriod
884 /* called from serio_driver->connect/disconnect methods under serio_mutex */
885 int serio_open(struct serio *serio, struct serio_driver *drv)
887 serio_set_drv(serio, drv);
889 if (serio->open && serio->open(serio)) {
890 serio_set_drv(serio, NULL);
896 /* called from serio_driver->connect/disconnect methods under serio_mutex */
897 void serio_close(struct serio *serio)
902 serio_set_drv(serio, NULL);
905 irqreturn_t serio_interrupt(struct serio *serio,
906 unsigned char data, unsigned int dfl)
909 irqreturn_t ret = IRQ_NONE;
911 spin_lock_irqsave(&serio->lock, flags);
913 if (likely(serio->drv)) {
914 ret = serio->drv->interrupt(serio, data, dfl);
915 } else if (!dfl && serio->registered) {
920 spin_unlock_irqrestore(&serio->lock, flags);
925 static struct bus_type serio_bus = {
927 .dev_attrs = serio_device_attrs,
928 .drv_attrs = serio_driver_attrs,
929 .match = serio_bus_match,
930 .uevent = serio_uevent,
931 .probe = serio_driver_probe,
932 .remove = serio_driver_remove,
933 .resume = serio_resume,
936 static int __init serio_init(void)
940 error = bus_register(&serio_bus);
942 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
946 serio_task = kthread_run(serio_thread, NULL, "kseriod");
947 if (IS_ERR(serio_task)) {
948 bus_unregister(&serio_bus);
949 error = PTR_ERR(serio_task);
950 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
957 static void __exit serio_exit(void)
959 bus_unregister(&serio_bus);
960 kthread_stop(serio_task);
963 subsys_initcall(serio_init);
964 module_exit(serio_exit);