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_unregister_port_delayed);
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_driver(struct serio_driver *drv);
66 static void serio_add_port(struct serio *serio);
67 static void serio_destroy_port(struct serio *serio);
68 static void serio_reconnect_port(struct serio *serio);
69 static void serio_disconnect_port(struct serio *serio);
71 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
75 mutex_lock(&serio->drv_mutex);
76 retval = drv->connect(serio, drv);
77 mutex_unlock(&serio->drv_mutex);
82 static int serio_reconnect_driver(struct serio *serio)
86 mutex_lock(&serio->drv_mutex);
87 if (serio->drv && serio->drv->reconnect)
88 retval = serio->drv->reconnect(serio);
89 mutex_unlock(&serio->drv_mutex);
94 static void serio_disconnect_driver(struct serio *serio)
96 mutex_lock(&serio->drv_mutex);
98 serio->drv->disconnect(serio);
99 mutex_unlock(&serio->drv_mutex);
102 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
104 while (ids->type || ids->proto) {
105 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
106 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
107 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
108 (ids->id == SERIO_ANY || ids->id == serio->id.id))
116 * Basic serio -> driver core mappings
119 static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
123 down_write(&serio_bus.subsys.rwsem);
125 if (serio_match_port(drv->id_table, serio)) {
126 serio->dev.driver = &drv->driver;
127 if (serio_connect_driver(serio, drv)) {
128 serio->dev.driver = NULL;
131 error = device_bind_driver(&serio->dev);
134 "serio: device_bind_driver() failed "
135 "for %s (%s) and %s, error: %d\n",
136 serio->phys, serio->name,
137 drv->description, error);
138 serio_disconnect_driver(serio);
139 serio->dev.driver = NULL;
144 up_write(&serio_bus.subsys.rwsem);
147 static void serio_release_driver(struct serio *serio)
149 down_write(&serio_bus.subsys.rwsem);
150 device_release_driver(&serio->dev);
151 up_write(&serio_bus.subsys.rwsem);
154 static void serio_find_driver(struct serio *serio)
158 down_write(&serio_bus.subsys.rwsem);
159 error = device_attach(&serio->dev);
162 "serio: device_attach() failed for %s (%s), error: %d\n",
163 serio->phys, serio->name, error);
164 up_write(&serio_bus.subsys.rwsem);
169 * Serio event processing.
172 enum serio_event_type {
176 SERIO_UNREGISTER_PORT,
177 SERIO_REGISTER_DRIVER,
181 enum serio_event_type type;
183 struct module *owner;
184 struct list_head node;
187 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
188 static LIST_HEAD(serio_event_list);
189 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
190 static struct task_struct *serio_task;
192 static void serio_queue_event(void *object, struct module *owner,
193 enum serio_event_type event_type)
196 struct serio_event *event;
198 spin_lock_irqsave(&serio_event_lock, flags);
201 * Scan event list for the other events for the same serio port,
202 * starting with the most recent one. If event is the same we
203 * do not need add new one. If event is of different type we
204 * need to add this event and should not look further because
205 * we need to preseve sequence of distinct events.
207 list_for_each_entry_reverse(event, &serio_event_list, node) {
208 if (event->object == object) {
209 if (event->type == event_type)
215 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
216 if (!try_module_get(owner)) {
217 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
222 event->type = event_type;
223 event->object = object;
224 event->owner = owner;
226 list_add_tail(&event->node, &serio_event_list);
227 wake_up(&serio_wait);
229 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
232 spin_unlock_irqrestore(&serio_event_lock, flags);
235 static void serio_free_event(struct serio_event *event)
237 module_put(event->owner);
241 static void serio_remove_duplicate_events(struct serio_event *event)
243 struct list_head *node, *next;
244 struct serio_event *e;
247 spin_lock_irqsave(&serio_event_lock, flags);
249 list_for_each_safe(node, next, &serio_event_list) {
250 e = list_entry(node, struct serio_event, node);
251 if (event->object == e->object) {
253 * If this event is of different type we should not
254 * look further - we only suppress duplicate events
255 * that were sent back-to-back.
257 if (event->type != e->type)
265 spin_unlock_irqrestore(&serio_event_lock, flags);
269 static struct serio_event *serio_get_event(void)
271 struct serio_event *event;
272 struct list_head *node;
275 spin_lock_irqsave(&serio_event_lock, flags);
277 if (list_empty(&serio_event_list)) {
278 spin_unlock_irqrestore(&serio_event_lock, flags);
282 node = serio_event_list.next;
283 event = list_entry(node, struct serio_event, node);
286 spin_unlock_irqrestore(&serio_event_lock, flags);
291 static void serio_handle_event(void)
293 struct serio_event *event;
295 mutex_lock(&serio_mutex);
298 * Note that we handle only one event here to give swsusp
299 * a chance to freeze kseriod thread. Serio events should
300 * be pretty rare so we are not concerned about taking
303 if ((event = serio_get_event())) {
305 switch (event->type) {
306 case SERIO_REGISTER_PORT:
307 serio_add_port(event->object);
310 case SERIO_UNREGISTER_PORT:
311 serio_disconnect_port(event->object);
312 serio_destroy_port(event->object);
315 case SERIO_RECONNECT:
316 serio_reconnect_port(event->object);
320 serio_disconnect_port(event->object);
321 serio_find_driver(event->object);
324 case SERIO_REGISTER_DRIVER:
325 serio_add_driver(event->object);
332 serio_remove_duplicate_events(event);
333 serio_free_event(event);
336 mutex_unlock(&serio_mutex);
340 * Remove all events that have been submitted for a given serio port.
342 static void serio_remove_pending_events(struct serio *serio)
344 struct list_head *node, *next;
345 struct serio_event *event;
348 spin_lock_irqsave(&serio_event_lock, flags);
350 list_for_each_safe(node, next, &serio_event_list) {
351 event = list_entry(node, struct serio_event, node);
352 if (event->object == serio) {
354 serio_free_event(event);
358 spin_unlock_irqrestore(&serio_event_lock, flags);
362 * Destroy child serio port (if any) that has not been fully registered yet.
364 * Note that we rely on the fact that port can have only one child and therefore
365 * only one child registration request can be pending. Additionally, children
366 * are registered by driver's connect() handler so there can't be a grandchild
367 * pending registration together with a child.
369 static struct serio *serio_get_pending_child(struct serio *parent)
371 struct serio_event *event;
372 struct serio *serio, *child = NULL;
375 spin_lock_irqsave(&serio_event_lock, flags);
377 list_for_each_entry(event, &serio_event_list, node) {
378 if (event->type == SERIO_REGISTER_PORT) {
379 serio = event->object;
380 if (serio->parent == parent) {
387 spin_unlock_irqrestore(&serio_event_lock, flags);
391 static int serio_thread(void *nothing)
394 serio_handle_event();
395 wait_event_interruptible(serio_wait,
396 kthread_should_stop() || !list_empty(&serio_event_list));
398 } while (!kthread_should_stop());
400 printk(KERN_DEBUG "serio: kseriod exiting\n");
406 * Serio port operations
409 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
411 struct serio *serio = to_serio_port(dev);
412 return sprintf(buf, "%s\n", serio->name);
415 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
417 struct serio *serio = to_serio_port(dev);
419 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
420 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
423 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
425 struct serio *serio = to_serio_port(dev);
426 return sprintf(buf, "%02x\n", serio->id.type);
429 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
431 struct serio *serio = to_serio_port(dev);
432 return sprintf(buf, "%02x\n", serio->id.proto);
435 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
437 struct serio *serio = to_serio_port(dev);
438 return sprintf(buf, "%02x\n", serio->id.id);
441 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
443 struct serio *serio = to_serio_port(dev);
444 return sprintf(buf, "%02x\n", serio->id.extra);
447 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
448 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
449 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
450 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
452 static struct attribute *serio_device_id_attrs[] = {
454 &dev_attr_proto.attr,
456 &dev_attr_extra.attr,
460 static struct attribute_group serio_id_attr_group = {
462 .attrs = serio_device_id_attrs,
465 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
467 struct serio *serio = to_serio_port(dev);
468 struct device_driver *drv;
471 retval = mutex_lock_interruptible(&serio_mutex);
476 if (!strncmp(buf, "none", count)) {
477 serio_disconnect_port(serio);
478 } else if (!strncmp(buf, "reconnect", count)) {
479 serio_reconnect_port(serio);
480 } else if (!strncmp(buf, "rescan", count)) {
481 serio_disconnect_port(serio);
482 serio_find_driver(serio);
483 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
484 serio_disconnect_port(serio);
485 serio_bind_driver(serio, to_serio_driver(drv));
491 mutex_unlock(&serio_mutex);
496 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
498 struct serio *serio = to_serio_port(dev);
499 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
502 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
504 struct serio *serio = to_serio_port(dev);
508 if (!strncmp(buf, "manual", count)) {
509 serio->manual_bind = 1;
510 } else if (!strncmp(buf, "auto", count)) {
511 serio->manual_bind = 0;
519 static struct device_attribute serio_device_attrs[] = {
520 __ATTR(description, S_IRUGO, serio_show_description, NULL),
521 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
522 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
523 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
528 static void serio_release_port(struct device *dev)
530 struct serio *serio = to_serio_port(dev);
533 module_put(THIS_MODULE);
537 * Prepare serio port for registration.
539 static void serio_init_port(struct serio *serio)
541 static atomic_t serio_no = ATOMIC_INIT(0);
543 __module_get(THIS_MODULE);
545 INIT_LIST_HEAD(&serio->node);
546 spin_lock_init(&serio->lock);
547 mutex_init(&serio->drv_mutex);
548 device_initialize(&serio->dev);
549 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
550 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
551 serio->dev.bus = &serio_bus;
552 serio->dev.release = serio_release_port;
554 serio->dev.parent = &serio->parent->dev;
555 serio->depth = serio->parent->depth + 1;
558 lockdep_set_subclass(&serio->lock, serio->depth);
562 * Complete serio port registration.
563 * Driver core will attempt to find appropriate driver for the port.
565 static void serio_add_port(struct serio *serio)
570 serio_pause_rx(serio->parent);
571 serio->parent->child = serio;
572 serio_continue_rx(serio->parent);
575 list_add_tail(&serio->node, &serio_list);
578 error = device_add(&serio->dev);
581 "serio: device_add() failed for %s (%s), error: %d\n",
582 serio->phys, serio->name, error);
584 serio->registered = 1;
585 error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
588 "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
589 serio->phys, serio->name, error);
594 * serio_destroy_port() completes deregistration process and removes
595 * port from the system
597 static void serio_destroy_port(struct serio *serio)
601 child = serio_get_pending_child(serio);
603 serio_remove_pending_events(child);
604 put_device(&child->dev);
611 serio_pause_rx(serio->parent);
612 serio->parent->child = NULL;
613 serio_continue_rx(serio->parent);
614 serio->parent = NULL;
617 if (serio->registered) {
618 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
619 device_del(&serio->dev);
620 serio->registered = 0;
623 list_del_init(&serio->node);
624 serio_remove_pending_events(serio);
625 put_device(&serio->dev);
629 * Reconnect serio port and all its children (re-initialize attached devices)
631 static void serio_reconnect_port(struct serio *serio)
634 if (serio_reconnect_driver(serio)) {
635 serio_disconnect_port(serio);
636 serio_find_driver(serio);
637 /* Ok, old children are now gone, we are done */
640 serio = serio->child;
645 * serio_disconnect_port() unbinds a port from its driver. As a side effect
646 * all child ports are unbound and destroyed.
648 static void serio_disconnect_port(struct serio *serio)
650 struct serio *s, *parent;
654 * Children ports should be disconnected and destroyed
655 * first, staring with the leaf one, since we don't want
658 for (s = serio; s->child; s = s->child)
664 serio_release_driver(s);
665 serio_destroy_port(s);
666 } while ((s = parent) != serio);
670 * Ok, no children left, now disconnect this port
672 serio_release_driver(serio);
675 void serio_rescan(struct serio *serio)
677 serio_queue_event(serio, NULL, SERIO_RESCAN);
680 void serio_reconnect(struct serio *serio)
682 serio_queue_event(serio, NULL, SERIO_RECONNECT);
686 * Submits register request to kseriod for subsequent execution.
687 * Note that port registration is always asynchronous.
689 void __serio_register_port(struct serio *serio, struct module *owner)
691 serio_init_port(serio);
692 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
696 * Synchronously unregisters serio port.
698 void serio_unregister_port(struct serio *serio)
700 mutex_lock(&serio_mutex);
701 serio_disconnect_port(serio);
702 serio_destroy_port(serio);
703 mutex_unlock(&serio_mutex);
707 * Safely unregisters child port if one is present.
709 void serio_unregister_child_port(struct serio *serio)
711 mutex_lock(&serio_mutex);
713 serio_disconnect_port(serio->child);
714 serio_destroy_port(serio->child);
716 mutex_unlock(&serio_mutex);
720 * Submits register request to kseriod for subsequent execution.
721 * Can be used when it is not obvious whether the serio_mutex is
722 * taken or not and when delayed execution is feasible.
724 void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
726 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
731 * Serio driver operations
734 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
736 struct serio_driver *driver = to_serio_driver(drv);
737 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
740 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
742 struct serio_driver *serio_drv = to_serio_driver(drv);
743 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
746 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
748 struct serio_driver *serio_drv = to_serio_driver(drv);
752 if (!strncmp(buf, "manual", count)) {
753 serio_drv->manual_bind = 1;
754 } else if (!strncmp(buf, "auto", count)) {
755 serio_drv->manual_bind = 0;
764 static struct driver_attribute serio_driver_attrs[] = {
765 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
766 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
767 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
771 static int serio_driver_probe(struct device *dev)
773 struct serio *serio = to_serio_port(dev);
774 struct serio_driver *drv = to_serio_driver(dev->driver);
776 return serio_connect_driver(serio, drv);
779 static int serio_driver_remove(struct device *dev)
781 struct serio *serio = to_serio_port(dev);
783 serio_disconnect_driver(serio);
787 static struct bus_type serio_bus = {
789 .probe = serio_driver_probe,
790 .remove = serio_driver_remove,
793 static void serio_add_driver(struct serio_driver *drv)
797 error = driver_register(&drv->driver);
800 "serio: driver_register() failed for %s, error: %d\n",
801 drv->driver.name, error);
804 void __serio_register_driver(struct serio_driver *drv, struct module *owner)
806 drv->driver.bus = &serio_bus;
808 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
811 void serio_unregister_driver(struct serio_driver *drv)
815 mutex_lock(&serio_mutex);
816 drv->manual_bind = 1; /* so serio_find_driver ignores it */
819 list_for_each_entry(serio, &serio_list, node) {
820 if (serio->drv == drv) {
821 serio_disconnect_port(serio);
822 serio_find_driver(serio);
823 /* we could've deleted some ports, restart */
828 driver_unregister(&drv->driver);
829 mutex_unlock(&serio_mutex);
832 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
834 serio_pause_rx(serio);
836 serio_continue_rx(serio);
839 static int serio_bus_match(struct device *dev, struct device_driver *drv)
841 struct serio *serio = to_serio_port(dev);
842 struct serio_driver *serio_drv = to_serio_driver(drv);
844 if (serio->manual_bind || serio_drv->manual_bind)
847 return serio_match_port(serio_drv->id_table, serio);
850 #ifdef CONFIG_HOTPLUG
852 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
854 int err = add_uevent_var(envp, num_envp, &i, \
855 buffer, buffer_size, &len, \
861 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
870 serio = to_serio_port(dev);
872 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
873 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
874 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
875 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
876 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
877 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
882 #undef SERIO_ADD_UEVENT_VAR
886 static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
891 #endif /* CONFIG_HOTPLUG */
893 static int serio_resume(struct device *dev)
895 struct serio *serio = to_serio_port(dev);
897 if (serio_reconnect_driver(serio)) {
899 * Driver re-probing can take a while, so better let kseriod
908 /* called from serio_driver->connect/disconnect methods under serio_mutex */
909 int serio_open(struct serio *serio, struct serio_driver *drv)
911 serio_set_drv(serio, drv);
913 if (serio->open && serio->open(serio)) {
914 serio_set_drv(serio, NULL);
920 /* called from serio_driver->connect/disconnect methods under serio_mutex */
921 void serio_close(struct serio *serio)
926 serio_set_drv(serio, NULL);
929 irqreturn_t serio_interrupt(struct serio *serio,
930 unsigned char data, unsigned int dfl)
933 irqreturn_t ret = IRQ_NONE;
935 spin_lock_irqsave(&serio->lock, flags);
937 if (likely(serio->drv)) {
938 ret = serio->drv->interrupt(serio, data, dfl);
939 } else if (!dfl && serio->registered) {
944 spin_unlock_irqrestore(&serio->lock, flags);
949 static int __init serio_init(void)
953 serio_bus.dev_attrs = serio_device_attrs;
954 serio_bus.drv_attrs = serio_driver_attrs;
955 serio_bus.match = serio_bus_match;
956 serio_bus.uevent = serio_uevent;
957 serio_bus.resume = serio_resume;
958 error = bus_register(&serio_bus);
960 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
964 serio_task = kthread_run(serio_thread, NULL, "kseriod");
965 if (IS_ERR(serio_task)) {
966 bus_unregister(&serio_bus);
967 error = PTR_ERR(serio_task);
968 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
975 static void __exit serio_exit(void)
977 bus_unregister(&serio_bus);
978 kthread_stop(serio_task);
981 subsys_initcall(serio_init);
982 module_exit(serio_exit);