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_port_delayed);
46 EXPORT_SYMBOL(__serio_register_driver);
47 EXPORT_SYMBOL(serio_unregister_driver);
48 EXPORT_SYMBOL(serio_open);
49 EXPORT_SYMBOL(serio_close);
50 EXPORT_SYMBOL(serio_rescan);
51 EXPORT_SYMBOL(serio_reconnect);
54 * serio_sem protects entire serio subsystem and is taken every time
55 * serio port or driver registrered or unregistered.
57 static DECLARE_MUTEX(serio_sem);
59 static LIST_HEAD(serio_list);
61 static struct bus_type serio_bus = {
65 static void serio_add_port(struct serio *serio);
66 static void serio_destroy_port(struct serio *serio);
67 static void serio_reconnect_port(struct serio *serio);
68 static void serio_disconnect_port(struct serio *serio);
70 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
72 while (ids->type || ids->proto) {
73 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
74 (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
75 (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
76 (ids->id == SERIO_ANY || ids->id == serio->id.id))
84 * Basic serio -> driver core mappings
87 static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
89 down_write(&serio_bus.subsys.rwsem);
91 if (serio_match_port(drv->id_table, serio)) {
92 serio->dev.driver = &drv->driver;
93 if (drv->connect(serio, drv)) {
94 serio->dev.driver = NULL;
97 device_bind_driver(&serio->dev);
100 up_write(&serio_bus.subsys.rwsem);
103 static void serio_release_driver(struct serio *serio)
105 down_write(&serio_bus.subsys.rwsem);
106 device_release_driver(&serio->dev);
107 up_write(&serio_bus.subsys.rwsem);
110 static void serio_find_driver(struct serio *serio)
112 down_write(&serio_bus.subsys.rwsem);
113 device_attach(&serio->dev);
114 up_write(&serio_bus.subsys.rwsem);
119 * Serio event processing.
122 enum serio_event_type {
126 SERIO_UNREGISTER_PORT,
127 SERIO_REGISTER_DRIVER,
131 enum serio_event_type type;
133 struct module *owner;
134 struct list_head node;
137 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
138 static LIST_HEAD(serio_event_list);
139 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
140 static struct task_struct *serio_task;
142 static void serio_queue_event(void *object, struct module *owner,
143 enum serio_event_type event_type)
146 struct serio_event *event;
148 spin_lock_irqsave(&serio_event_lock, flags);
151 * Scan event list for the other events for the same serio port,
152 * starting with the most recent one. If event is the same we
153 * do not need add new one. If event is of different type we
154 * need to add this event and should not look further because
155 * we need to preseve sequence of distinct events.
157 list_for_each_entry_reverse(event, &serio_event_list, node) {
158 if (event->object == object) {
159 if (event->type == event_type)
165 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
166 if (!try_module_get(owner)) {
167 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
171 event->type = event_type;
172 event->object = object;
173 event->owner = owner;
175 list_add_tail(&event->node, &serio_event_list);
176 wake_up(&serio_wait);
178 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
181 spin_unlock_irqrestore(&serio_event_lock, flags);
184 static void serio_free_event(struct serio_event *event)
186 module_put(event->owner);
190 static void serio_remove_duplicate_events(struct serio_event *event)
192 struct list_head *node, *next;
193 struct serio_event *e;
196 spin_lock_irqsave(&serio_event_lock, flags);
198 list_for_each_safe(node, next, &serio_event_list) {
199 e = list_entry(node, struct serio_event, node);
200 if (event->object == e->object) {
202 * If this event is of different type we should not
203 * look further - we only suppress duplicate events
204 * that were sent back-to-back.
206 if (event->type != e->type)
214 spin_unlock_irqrestore(&serio_event_lock, flags);
218 static struct serio_event *serio_get_event(void)
220 struct serio_event *event;
221 struct list_head *node;
224 spin_lock_irqsave(&serio_event_lock, flags);
226 if (list_empty(&serio_event_list)) {
227 spin_unlock_irqrestore(&serio_event_lock, flags);
231 node = serio_event_list.next;
232 event = list_entry(node, struct serio_event, node);
235 spin_unlock_irqrestore(&serio_event_lock, flags);
240 static void serio_handle_events(void)
242 struct serio_event *event;
243 struct serio_driver *serio_drv;
247 while ((event = serio_get_event())) {
249 switch (event->type) {
250 case SERIO_REGISTER_PORT:
251 serio_add_port(event->object);
254 case SERIO_UNREGISTER_PORT:
255 serio_disconnect_port(event->object);
256 serio_destroy_port(event->object);
259 case SERIO_RECONNECT:
260 serio_reconnect_port(event->object);
264 serio_disconnect_port(event->object);
265 serio_find_driver(event->object);
268 case SERIO_REGISTER_DRIVER:
269 serio_drv = event->object;
270 driver_register(&serio_drv->driver);
277 serio_remove_duplicate_events(event);
278 serio_free_event(event);
285 * Remove all events that have been submitted for a given serio port.
287 static void serio_remove_pending_events(struct serio *serio)
289 struct list_head *node, *next;
290 struct serio_event *event;
293 spin_lock_irqsave(&serio_event_lock, flags);
295 list_for_each_safe(node, next, &serio_event_list) {
296 event = list_entry(node, struct serio_event, node);
297 if (event->object == serio) {
299 serio_free_event(event);
303 spin_unlock_irqrestore(&serio_event_lock, flags);
307 * Destroy child serio port (if any) that has not been fully registered yet.
309 * Note that we rely on the fact that port can have only one child and therefore
310 * only one child registration request can be pending. Additionally, children
311 * are registered by driver's connect() handler so there can't be a grandchild
312 * pending registration together with a child.
314 static struct serio *serio_get_pending_child(struct serio *parent)
316 struct serio_event *event;
317 struct serio *serio, *child = NULL;
320 spin_lock_irqsave(&serio_event_lock, flags);
322 list_for_each_entry(event, &serio_event_list, node) {
323 if (event->type == SERIO_REGISTER_PORT) {
324 serio = event->object;
325 if (serio->parent == parent) {
332 spin_unlock_irqrestore(&serio_event_lock, flags);
336 static int serio_thread(void *nothing)
339 serio_handle_events();
340 wait_event_interruptible(serio_wait,
341 kthread_should_stop() || !list_empty(&serio_event_list));
342 try_to_freeze(PF_FREEZE);
343 } while (!kthread_should_stop());
345 printk(KERN_DEBUG "serio: kseriod exiting\n");
351 * Serio port operations
354 static ssize_t serio_show_description(struct device *dev, char *buf)
356 struct serio *serio = to_serio_port(dev);
357 return sprintf(buf, "%s\n", serio->name);
360 static ssize_t serio_show_id_type(struct device *dev, char *buf)
362 struct serio *serio = to_serio_port(dev);
363 return sprintf(buf, "%02x\n", serio->id.type);
366 static ssize_t serio_show_id_proto(struct device *dev, char *buf)
368 struct serio *serio = to_serio_port(dev);
369 return sprintf(buf, "%02x\n", serio->id.proto);
372 static ssize_t serio_show_id_id(struct device *dev, char *buf)
374 struct serio *serio = to_serio_port(dev);
375 return sprintf(buf, "%02x\n", serio->id.id);
378 static ssize_t serio_show_id_extra(struct device *dev, char *buf)
380 struct serio *serio = to_serio_port(dev);
381 return sprintf(buf, "%02x\n", serio->id.extra);
384 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
385 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
386 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
387 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
389 static struct attribute *serio_device_id_attrs[] = {
391 &dev_attr_proto.attr,
393 &dev_attr_extra.attr,
397 static struct attribute_group serio_id_attr_group = {
399 .attrs = serio_device_id_attrs,
402 static ssize_t serio_rebind_driver(struct device *dev, const char *buf, size_t count)
404 struct serio *serio = to_serio_port(dev);
405 struct device_driver *drv;
408 retval = down_interruptible(&serio_sem);
413 if (!strncmp(buf, "none", count)) {
414 serio_disconnect_port(serio);
415 } else if (!strncmp(buf, "reconnect", count)) {
416 serio_reconnect_port(serio);
417 } else if (!strncmp(buf, "rescan", count)) {
418 serio_disconnect_port(serio);
419 serio_find_driver(serio);
420 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
421 serio_disconnect_port(serio);
422 serio_bind_driver(serio, to_serio_driver(drv));
433 static ssize_t serio_show_bind_mode(struct device *dev, char *buf)
435 struct serio *serio = to_serio_port(dev);
436 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
439 static ssize_t serio_set_bind_mode(struct device *dev, const char *buf, size_t count)
441 struct serio *serio = to_serio_port(dev);
445 if (!strncmp(buf, "manual", count)) {
446 serio->manual_bind = 1;
447 } else if (!strncmp(buf, "auto", count)) {
448 serio->manual_bind = 0;
456 static struct device_attribute serio_device_attrs[] = {
457 __ATTR(description, S_IRUGO, serio_show_description, NULL),
458 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
459 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
464 static void serio_release_port(struct device *dev)
466 struct serio *serio = to_serio_port(dev);
469 module_put(THIS_MODULE);
473 * Prepare serio port for registration.
475 static void serio_init_port(struct serio *serio)
477 static atomic_t serio_no = ATOMIC_INIT(0);
479 __module_get(THIS_MODULE);
481 spin_lock_init(&serio->lock);
482 init_MUTEX(&serio->drv_sem);
483 device_initialize(&serio->dev);
484 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
485 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
486 serio->dev.bus = &serio_bus;
487 serio->dev.release = serio_release_port;
489 serio->dev.parent = &serio->parent->dev;
493 * Complete serio port registration.
494 * Driver core will attempt to find appropriate driver for the port.
496 static void serio_add_port(struct serio *serio)
499 serio_pause_rx(serio->parent);
500 serio->parent->child = serio;
501 serio_continue_rx(serio->parent);
504 list_add_tail(&serio->node, &serio_list);
507 device_add(&serio->dev);
508 sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
509 serio->registered = 1;
513 * serio_destroy_port() completes deregistration process and removes
514 * port from the system
516 static void serio_destroy_port(struct serio *serio)
520 child = serio_get_pending_child(serio);
522 serio_remove_pending_events(child);
523 put_device(&child->dev);
530 serio_pause_rx(serio->parent);
531 serio->parent->child = NULL;
532 serio_continue_rx(serio->parent);
533 serio->parent = NULL;
536 if (serio->registered) {
537 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
538 device_del(&serio->dev);
539 list_del_init(&serio->node);
540 serio->registered = 0;
543 serio_remove_pending_events(serio);
544 put_device(&serio->dev);
548 * Reconnect serio port and all its children (re-initialize attached devices)
550 static void serio_reconnect_port(struct serio *serio)
553 if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) {
554 serio_disconnect_port(serio);
555 serio_find_driver(serio);
556 /* Ok, old children are now gone, we are done */
559 serio = serio->child;
564 * serio_disconnect_port() unbinds a port from its driver. As a side effect
565 * all child ports are unbound and destroyed.
567 static void serio_disconnect_port(struct serio *serio)
569 struct serio *s, *parent;
573 * Children ports should be disconnected and destroyed
574 * first, staring with the leaf one, since we don't want
577 for (s = serio; s->child; s = s->child)
583 serio_release_driver(s);
584 serio_destroy_port(s);
585 } while ((s = parent) != serio);
589 * Ok, no children left, now disconnect this port
591 serio_release_driver(serio);
594 void serio_rescan(struct serio *serio)
596 serio_queue_event(serio, NULL, SERIO_RESCAN);
599 void serio_reconnect(struct serio *serio)
601 serio_queue_event(serio, NULL, SERIO_RECONNECT);
605 * Submits register request to kseriod for subsequent execution.
606 * Note that port registration is always asynchronous.
608 void __serio_register_port(struct serio *serio, struct module *owner)
610 serio_init_port(serio);
611 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
615 * Synchronously unregisters serio port.
617 void serio_unregister_port(struct serio *serio)
620 serio_disconnect_port(serio);
621 serio_destroy_port(serio);
626 * Submits register request to kseriod for subsequent execution.
627 * Can be used when it is not obvious whether the serio_sem is
628 * taken or not and when delayed execution is feasible.
630 void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
632 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
637 * Serio driver operations
640 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
642 struct serio_driver *driver = to_serio_driver(drv);
643 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
646 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
648 struct serio_driver *serio_drv = to_serio_driver(drv);
649 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
652 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
654 struct serio_driver *serio_drv = to_serio_driver(drv);
658 if (!strncmp(buf, "manual", count)) {
659 serio_drv->manual_bind = 1;
660 } else if (!strncmp(buf, "auto", count)) {
661 serio_drv->manual_bind = 0;
670 static struct driver_attribute serio_driver_attrs[] = {
671 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
672 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
673 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
677 static int serio_driver_probe(struct device *dev)
679 struct serio *serio = to_serio_port(dev);
680 struct serio_driver *drv = to_serio_driver(dev->driver);
682 return drv->connect(serio, drv);
685 static int serio_driver_remove(struct device *dev)
687 struct serio *serio = to_serio_port(dev);
688 struct serio_driver *drv = to_serio_driver(dev->driver);
690 drv->disconnect(serio);
694 void __serio_register_driver(struct serio_driver *drv, struct module *owner)
696 drv->driver.bus = &serio_bus;
697 drv->driver.probe = serio_driver_probe;
698 drv->driver.remove = serio_driver_remove;
700 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
703 void serio_unregister_driver(struct serio_driver *drv)
708 drv->manual_bind = 1; /* so serio_find_driver ignores it */
711 list_for_each_entry(serio, &serio_list, node) {
712 if (serio->drv == drv) {
713 serio_disconnect_port(serio);
714 serio_find_driver(serio);
715 /* we could've deleted some ports, restart */
720 driver_unregister(&drv->driver);
724 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
726 down(&serio->drv_sem);
727 serio_pause_rx(serio);
729 serio_continue_rx(serio);
733 static int serio_bus_match(struct device *dev, struct device_driver *drv)
735 struct serio *serio = to_serio_port(dev);
736 struct serio_driver *serio_drv = to_serio_driver(drv);
738 if (serio->manual_bind || serio_drv->manual_bind)
741 return serio_match_port(serio_drv->id_table, serio);
744 #ifdef CONFIG_HOTPLUG
746 #define PUT_ENVP(fmt, val) \
748 envp[i++] = buffer; \
749 length += snprintf(buffer, buffer_size - length, fmt, val); \
750 if (buffer_size - length <= 0 || i >= num_envp) \
755 static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
764 serio = to_serio_port(dev);
766 PUT_ENVP("SERIO_TYPE=%02x", serio->id.type);
767 PUT_ENVP("SERIO_PROTO=%02x", serio->id.proto);
768 PUT_ENVP("SERIO_ID=%02x", serio->id.id);
769 PUT_ENVP("SERIO_EXTRA=%02x", serio->id.extra);
779 static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
784 #endif /* CONFIG_HOTPLUG */
786 static int serio_resume(struct device *dev)
788 struct serio *serio = to_serio_port(dev);
790 if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) {
792 * Driver re-probing can take a while, so better let kseriod
801 /* called from serio_driver->connect/disconnect methods under serio_sem */
802 int serio_open(struct serio *serio, struct serio_driver *drv)
804 serio_set_drv(serio, drv);
806 if (serio->open && serio->open(serio)) {
807 serio_set_drv(serio, NULL);
813 /* called from serio_driver->connect/disconnect methods under serio_sem */
814 void serio_close(struct serio *serio)
819 serio_set_drv(serio, NULL);
822 irqreturn_t serio_interrupt(struct serio *serio,
823 unsigned char data, unsigned int dfl, struct pt_regs *regs)
826 irqreturn_t ret = IRQ_NONE;
828 spin_lock_irqsave(&serio->lock, flags);
830 if (likely(serio->drv)) {
831 ret = serio->drv->interrupt(serio, data, dfl, regs);
832 } else if (!dfl && serio->registered) {
837 spin_unlock_irqrestore(&serio->lock, flags);
842 static int __init serio_init(void)
844 serio_task = kthread_run(serio_thread, NULL, "kseriod");
845 if (IS_ERR(serio_task)) {
846 printk(KERN_ERR "serio: Failed to start kseriod\n");
847 return PTR_ERR(serio_task);
850 serio_bus.dev_attrs = serio_device_attrs;
851 serio_bus.drv_attrs = serio_driver_attrs;
852 serio_bus.match = serio_bus_match;
853 serio_bus.hotplug = serio_hotplug;
854 serio_bus.resume = serio_resume;
855 bus_register(&serio_bus);
860 static void __exit serio_exit(void)
862 bus_unregister(&serio_bus);
863 kthread_stop(serio_task);
866 module_init(serio_init);
867 module_exit(serio_exit);