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>
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39 MODULE_DESCRIPTION("Serio abstraction core");
40 MODULE_LICENSE("GPL");
42 EXPORT_SYMBOL(serio_interrupt);
43 EXPORT_SYMBOL(__serio_register_port);
44 EXPORT_SYMBOL(serio_unregister_port);
45 EXPORT_SYMBOL(serio_unregister_child_port);
46 EXPORT_SYMBOL(__serio_unregister_port_delayed);
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_sem protects entire serio subsystem and is taken every time
56 * serio port or driver registrered or unregistered.
58 static DECLARE_MUTEX(serio_sem);
60 static LIST_HEAD(serio_list);
62 static struct bus_type serio_bus = {
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 down(&serio->drv_sem);
76 retval = drv->connect(serio, drv);
82 static int serio_reconnect_driver(struct serio *serio)
86 down(&serio->drv_sem);
87 if (serio->drv && serio->drv->reconnect)
88 retval = serio->drv->reconnect(serio);
94 static void serio_disconnect_driver(struct serio *serio)
96 down(&serio->drv_sem);
98 serio->drv->disconnect(serio);
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)
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 device_bind_driver(&serio->dev);
132 up_write(&serio_bus.subsys.rwsem);
135 static void serio_release_driver(struct serio *serio)
137 down_write(&serio_bus.subsys.rwsem);
138 device_release_driver(&serio->dev);
139 up_write(&serio_bus.subsys.rwsem);
142 static void serio_find_driver(struct serio *serio)
144 down_write(&serio_bus.subsys.rwsem);
145 device_attach(&serio->dev);
146 up_write(&serio_bus.subsys.rwsem);
151 * Serio event processing.
154 enum serio_event_type {
158 SERIO_UNREGISTER_PORT,
159 SERIO_REGISTER_DRIVER,
163 enum serio_event_type type;
165 struct module *owner;
166 struct list_head node;
169 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
170 static LIST_HEAD(serio_event_list);
171 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
172 static struct task_struct *serio_task;
174 static void serio_queue_event(void *object, struct module *owner,
175 enum serio_event_type event_type)
178 struct serio_event *event;
180 spin_lock_irqsave(&serio_event_lock, flags);
183 * Scan event list for the other events for the same serio port,
184 * starting with the most recent one. If event is the same we
185 * do not need add new one. If event is of different type we
186 * need to add this event and should not look further because
187 * we need to preseve sequence of distinct events.
189 list_for_each_entry_reverse(event, &serio_event_list, node) {
190 if (event->object == object) {
191 if (event->type == event_type)
197 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
198 if (!try_module_get(owner)) {
199 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
203 event->type = event_type;
204 event->object = object;
205 event->owner = owner;
207 list_add_tail(&event->node, &serio_event_list);
208 wake_up(&serio_wait);
210 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
213 spin_unlock_irqrestore(&serio_event_lock, flags);
216 static void serio_free_event(struct serio_event *event)
218 module_put(event->owner);
222 static void serio_remove_duplicate_events(struct serio_event *event)
224 struct list_head *node, *next;
225 struct serio_event *e;
228 spin_lock_irqsave(&serio_event_lock, flags);
230 list_for_each_safe(node, next, &serio_event_list) {
231 e = list_entry(node, struct serio_event, node);
232 if (event->object == e->object) {
234 * If this event is of different type we should not
235 * look further - we only suppress duplicate events
236 * that were sent back-to-back.
238 if (event->type != e->type)
246 spin_unlock_irqrestore(&serio_event_lock, flags);
250 static struct serio_event *serio_get_event(void)
252 struct serio_event *event;
253 struct list_head *node;
256 spin_lock_irqsave(&serio_event_lock, flags);
258 if (list_empty(&serio_event_list)) {
259 spin_unlock_irqrestore(&serio_event_lock, flags);
263 node = serio_event_list.next;
264 event = list_entry(node, struct serio_event, node);
267 spin_unlock_irqrestore(&serio_event_lock, flags);
272 static void serio_handle_events(void)
274 struct serio_event *event;
275 struct serio_driver *serio_drv;
279 while ((event = serio_get_event())) {
281 switch (event->type) {
282 case SERIO_REGISTER_PORT:
283 serio_add_port(event->object);
286 case SERIO_UNREGISTER_PORT:
287 serio_disconnect_port(event->object);
288 serio_destroy_port(event->object);
291 case SERIO_RECONNECT:
292 serio_reconnect_port(event->object);
296 serio_disconnect_port(event->object);
297 serio_find_driver(event->object);
300 case SERIO_REGISTER_DRIVER:
301 serio_drv = event->object;
302 driver_register(&serio_drv->driver);
309 serio_remove_duplicate_events(event);
310 serio_free_event(event);
317 * Remove all events that have been submitted for a given serio port.
319 static void serio_remove_pending_events(struct serio *serio)
321 struct list_head *node, *next;
322 struct serio_event *event;
325 spin_lock_irqsave(&serio_event_lock, flags);
327 list_for_each_safe(node, next, &serio_event_list) {
328 event = list_entry(node, struct serio_event, node);
329 if (event->object == serio) {
331 serio_free_event(event);
335 spin_unlock_irqrestore(&serio_event_lock, flags);
339 * Destroy child serio port (if any) that has not been fully registered yet.
341 * Note that we rely on the fact that port can have only one child and therefore
342 * only one child registration request can be pending. Additionally, children
343 * are registered by driver's connect() handler so there can't be a grandchild
344 * pending registration together with a child.
346 static struct serio *serio_get_pending_child(struct serio *parent)
348 struct serio_event *event;
349 struct serio *serio, *child = NULL;
352 spin_lock_irqsave(&serio_event_lock, flags);
354 list_for_each_entry(event, &serio_event_list, node) {
355 if (event->type == SERIO_REGISTER_PORT) {
356 serio = event->object;
357 if (serio->parent == parent) {
364 spin_unlock_irqrestore(&serio_event_lock, flags);
368 static int serio_thread(void *nothing)
371 serio_handle_events();
372 wait_event_interruptible(serio_wait,
373 kthread_should_stop() || !list_empty(&serio_event_list));
375 } while (!kthread_should_stop());
377 printk(KERN_DEBUG "serio: kseriod exiting\n");
383 * Serio port operations
386 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
388 struct serio *serio = to_serio_port(dev);
389 return sprintf(buf, "%s\n", serio->name);
392 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
394 struct serio *serio = to_serio_port(dev);
396 return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
397 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
400 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
402 struct serio *serio = to_serio_port(dev);
403 return sprintf(buf, "%02x\n", serio->id.type);
406 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
408 struct serio *serio = to_serio_port(dev);
409 return sprintf(buf, "%02x\n", serio->id.proto);
412 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
414 struct serio *serio = to_serio_port(dev);
415 return sprintf(buf, "%02x\n", serio->id.id);
418 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
420 struct serio *serio = to_serio_port(dev);
421 return sprintf(buf, "%02x\n", serio->id.extra);
424 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
425 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
426 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
427 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
429 static struct attribute *serio_device_id_attrs[] = {
431 &dev_attr_proto.attr,
433 &dev_attr_extra.attr,
437 static struct attribute_group serio_id_attr_group = {
439 .attrs = serio_device_id_attrs,
442 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
444 struct serio *serio = to_serio_port(dev);
445 struct device_driver *drv;
448 retval = down_interruptible(&serio_sem);
453 if (!strncmp(buf, "none", count)) {
454 serio_disconnect_port(serio);
455 } else if (!strncmp(buf, "reconnect", count)) {
456 serio_reconnect_port(serio);
457 } else if (!strncmp(buf, "rescan", count)) {
458 serio_disconnect_port(serio);
459 serio_find_driver(serio);
460 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
461 serio_disconnect_port(serio);
462 serio_bind_driver(serio, to_serio_driver(drv));
473 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
475 struct serio *serio = to_serio_port(dev);
476 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
479 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
481 struct serio *serio = to_serio_port(dev);
485 if (!strncmp(buf, "manual", count)) {
486 serio->manual_bind = 1;
487 } else if (!strncmp(buf, "auto", count)) {
488 serio->manual_bind = 0;
496 static struct device_attribute serio_device_attrs[] = {
497 __ATTR(description, S_IRUGO, serio_show_description, NULL),
498 __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
499 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
500 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
505 static void serio_release_port(struct device *dev)
507 struct serio *serio = to_serio_port(dev);
510 module_put(THIS_MODULE);
514 * Prepare serio port for registration.
516 static void serio_init_port(struct serio *serio)
518 static atomic_t serio_no = ATOMIC_INIT(0);
520 __module_get(THIS_MODULE);
522 spin_lock_init(&serio->lock);
523 init_MUTEX(&serio->drv_sem);
524 device_initialize(&serio->dev);
525 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
526 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
527 serio->dev.bus = &serio_bus;
528 serio->dev.release = serio_release_port;
530 serio->dev.parent = &serio->parent->dev;
534 * Complete serio port registration.
535 * Driver core will attempt to find appropriate driver for the port.
537 static void serio_add_port(struct serio *serio)
540 serio_pause_rx(serio->parent);
541 serio->parent->child = serio;
542 serio_continue_rx(serio->parent);
545 list_add_tail(&serio->node, &serio_list);
548 device_add(&serio->dev);
549 sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
550 serio->registered = 1;
554 * serio_destroy_port() completes deregistration process and removes
555 * port from the system
557 static void serio_destroy_port(struct serio *serio)
561 child = serio_get_pending_child(serio);
563 serio_remove_pending_events(child);
564 put_device(&child->dev);
571 serio_pause_rx(serio->parent);
572 serio->parent->child = NULL;
573 serio_continue_rx(serio->parent);
574 serio->parent = NULL;
577 if (serio->registered) {
578 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
579 device_del(&serio->dev);
580 list_del_init(&serio->node);
581 serio->registered = 0;
584 serio_remove_pending_events(serio);
585 put_device(&serio->dev);
589 * Reconnect serio port and all its children (re-initialize attached devices)
591 static void serio_reconnect_port(struct serio *serio)
594 if (serio_reconnect_driver(serio)) {
595 serio_disconnect_port(serio);
596 serio_find_driver(serio);
597 /* Ok, old children are now gone, we are done */
600 serio = serio->child;
605 * serio_disconnect_port() unbinds a port from its driver. As a side effect
606 * all child ports are unbound and destroyed.
608 static void serio_disconnect_port(struct serio *serio)
610 struct serio *s, *parent;
614 * Children ports should be disconnected and destroyed
615 * first, staring with the leaf one, since we don't want
618 for (s = serio; s->child; s = s->child)
624 serio_release_driver(s);
625 serio_destroy_port(s);
626 } while ((s = parent) != serio);
630 * Ok, no children left, now disconnect this port
632 serio_release_driver(serio);
635 void serio_rescan(struct serio *serio)
637 serio_queue_event(serio, NULL, SERIO_RESCAN);
640 void serio_reconnect(struct serio *serio)
642 serio_queue_event(serio, NULL, SERIO_RECONNECT);
646 * Submits register request to kseriod for subsequent execution.
647 * Note that port registration is always asynchronous.
649 void __serio_register_port(struct serio *serio, struct module *owner)
651 serio_init_port(serio);
652 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
656 * Synchronously unregisters serio port.
658 void serio_unregister_port(struct serio *serio)
661 serio_disconnect_port(serio);
662 serio_destroy_port(serio);
667 * Safely unregisters child port if one is present.
669 void serio_unregister_child_port(struct serio *serio)
673 serio_disconnect_port(serio->child);
674 serio_destroy_port(serio->child);
680 * Submits register request to kseriod for subsequent execution.
681 * Can be used when it is not obvious whether the serio_sem is
682 * taken or not and when delayed execution is feasible.
684 void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
686 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
691 * Serio driver operations
694 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
696 struct serio_driver *driver = to_serio_driver(drv);
697 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
700 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
702 struct serio_driver *serio_drv = to_serio_driver(drv);
703 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
706 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
708 struct serio_driver *serio_drv = to_serio_driver(drv);
712 if (!strncmp(buf, "manual", count)) {
713 serio_drv->manual_bind = 1;
714 } else if (!strncmp(buf, "auto", count)) {
715 serio_drv->manual_bind = 0;
724 static struct driver_attribute serio_driver_attrs[] = {
725 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
726 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
727 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
731 static int serio_driver_probe(struct device *dev)
733 struct serio *serio = to_serio_port(dev);
734 struct serio_driver *drv = to_serio_driver(dev->driver);
736 return serio_connect_driver(serio, drv);
739 static int serio_driver_remove(struct device *dev)
741 struct serio *serio = to_serio_port(dev);
743 serio_disconnect_driver(serio);
747 void __serio_register_driver(struct serio_driver *drv, struct module *owner)
749 drv->driver.bus = &serio_bus;
750 drv->driver.probe = serio_driver_probe;
751 drv->driver.remove = serio_driver_remove;
753 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
756 void serio_unregister_driver(struct serio_driver *drv)
761 drv->manual_bind = 1; /* so serio_find_driver ignores it */
764 list_for_each_entry(serio, &serio_list, node) {
765 if (serio->drv == drv) {
766 serio_disconnect_port(serio);
767 serio_find_driver(serio);
768 /* we could've deleted some ports, restart */
773 driver_unregister(&drv->driver);
777 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
779 serio_pause_rx(serio);
781 serio_continue_rx(serio);
784 static int serio_bus_match(struct device *dev, struct device_driver *drv)
786 struct serio *serio = to_serio_port(dev);
787 struct serio_driver *serio_drv = to_serio_driver(drv);
789 if (serio->manual_bind || serio_drv->manual_bind)
792 return serio_match_port(serio_drv->id_table, serio);
795 #ifdef CONFIG_HOTPLUG
797 #define SERIO_ADD_HOTPLUG_VAR(fmt, val...) \
799 int err = add_hotplug_env_var(envp, num_envp, &i, \
800 buffer, buffer_size, &len, \
806 static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
815 serio = to_serio_port(dev);
817 SERIO_ADD_HOTPLUG_VAR("SERIO_TYPE=%02x", serio->id.type);
818 SERIO_ADD_HOTPLUG_VAR("SERIO_PROTO=%02x", serio->id.proto);
819 SERIO_ADD_HOTPLUG_VAR("SERIO_ID=%02x", serio->id.id);
820 SERIO_ADD_HOTPLUG_VAR("SERIO_EXTRA=%02x", serio->id.extra);
821 SERIO_ADD_HOTPLUG_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
822 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
827 #undef SERIO_ADD_HOTPLUG_VAR
831 static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
836 #endif /* CONFIG_HOTPLUG */
838 static int serio_resume(struct device *dev)
840 struct serio *serio = to_serio_port(dev);
842 if (serio_reconnect_driver(serio)) {
844 * Driver re-probing can take a while, so better let kseriod
853 /* called from serio_driver->connect/disconnect methods under serio_sem */
854 int serio_open(struct serio *serio, struct serio_driver *drv)
856 serio_set_drv(serio, drv);
858 if (serio->open && serio->open(serio)) {
859 serio_set_drv(serio, NULL);
865 /* called from serio_driver->connect/disconnect methods under serio_sem */
866 void serio_close(struct serio *serio)
871 serio_set_drv(serio, NULL);
874 irqreturn_t serio_interrupt(struct serio *serio,
875 unsigned char data, unsigned int dfl, struct pt_regs *regs)
878 irqreturn_t ret = IRQ_NONE;
880 spin_lock_irqsave(&serio->lock, flags);
882 if (likely(serio->drv)) {
883 ret = serio->drv->interrupt(serio, data, dfl, regs);
884 } else if (!dfl && serio->registered) {
889 spin_unlock_irqrestore(&serio->lock, flags);
894 static int __init serio_init(void)
896 serio_task = kthread_run(serio_thread, NULL, "kseriod");
897 if (IS_ERR(serio_task)) {
898 printk(KERN_ERR "serio: Failed to start kseriod\n");
899 return PTR_ERR(serio_task);
902 serio_bus.dev_attrs = serio_device_attrs;
903 serio_bus.drv_attrs = serio_driver_attrs;
904 serio_bus.match = serio_bus_match;
905 serio_bus.hotplug = serio_hotplug;
906 serio_bus.resume = serio_resume;
907 bus_register(&serio_bus);
912 static void __exit serio_exit(void)
914 bus_unregister(&serio_bus);
915 kthread_stop(serio_task);
918 module_init(serio_init);
919 module_exit(serio_exit);