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_connect_driver(struct serio *serio, struct serio_driver *drv)
74 down(&serio->drv_sem);
75 retval = drv->connect(serio, drv);
81 static int serio_reconnect_driver(struct serio *serio)
85 down(&serio->drv_sem);
86 if (serio->drv && serio->drv->reconnect)
87 retval = serio->drv->reconnect(serio);
93 static void serio_disconnect_driver(struct serio *serio)
95 down(&serio->drv_sem);
97 serio->drv->disconnect(serio);
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)
120 down_write(&serio_bus.subsys.rwsem);
122 if (serio_match_port(drv->id_table, serio)) {
123 serio->dev.driver = &drv->driver;
124 if (serio_connect_driver(serio, drv)) {
125 serio->dev.driver = NULL;
128 device_bind_driver(&serio->dev);
131 up_write(&serio_bus.subsys.rwsem);
134 static void serio_release_driver(struct serio *serio)
136 down_write(&serio_bus.subsys.rwsem);
137 device_release_driver(&serio->dev);
138 up_write(&serio_bus.subsys.rwsem);
141 static void serio_find_driver(struct serio *serio)
143 down_write(&serio_bus.subsys.rwsem);
144 device_attach(&serio->dev);
145 up_write(&serio_bus.subsys.rwsem);
150 * Serio event processing.
153 enum serio_event_type {
157 SERIO_UNREGISTER_PORT,
158 SERIO_REGISTER_DRIVER,
162 enum serio_event_type type;
164 struct module *owner;
165 struct list_head node;
168 static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
169 static LIST_HEAD(serio_event_list);
170 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
171 static struct task_struct *serio_task;
173 static void serio_queue_event(void *object, struct module *owner,
174 enum serio_event_type event_type)
177 struct serio_event *event;
179 spin_lock_irqsave(&serio_event_lock, flags);
182 * Scan event list for the other events for the same serio port,
183 * starting with the most recent one. If event is the same we
184 * do not need add new one. If event is of different type we
185 * need to add this event and should not look further because
186 * we need to preseve sequence of distinct events.
188 list_for_each_entry_reverse(event, &serio_event_list, node) {
189 if (event->object == object) {
190 if (event->type == event_type)
196 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
197 if (!try_module_get(owner)) {
198 printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
202 event->type = event_type;
203 event->object = object;
204 event->owner = owner;
206 list_add_tail(&event->node, &serio_event_list);
207 wake_up(&serio_wait);
209 printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
212 spin_unlock_irqrestore(&serio_event_lock, flags);
215 static void serio_free_event(struct serio_event *event)
217 module_put(event->owner);
221 static void serio_remove_duplicate_events(struct serio_event *event)
223 struct list_head *node, *next;
224 struct serio_event *e;
227 spin_lock_irqsave(&serio_event_lock, flags);
229 list_for_each_safe(node, next, &serio_event_list) {
230 e = list_entry(node, struct serio_event, node);
231 if (event->object == e->object) {
233 * If this event is of different type we should not
234 * look further - we only suppress duplicate events
235 * that were sent back-to-back.
237 if (event->type != e->type)
245 spin_unlock_irqrestore(&serio_event_lock, flags);
249 static struct serio_event *serio_get_event(void)
251 struct serio_event *event;
252 struct list_head *node;
255 spin_lock_irqsave(&serio_event_lock, flags);
257 if (list_empty(&serio_event_list)) {
258 spin_unlock_irqrestore(&serio_event_lock, flags);
262 node = serio_event_list.next;
263 event = list_entry(node, struct serio_event, node);
266 spin_unlock_irqrestore(&serio_event_lock, flags);
271 static void serio_handle_events(void)
273 struct serio_event *event;
274 struct serio_driver *serio_drv;
278 while ((event = serio_get_event())) {
280 switch (event->type) {
281 case SERIO_REGISTER_PORT:
282 serio_add_port(event->object);
285 case SERIO_UNREGISTER_PORT:
286 serio_disconnect_port(event->object);
287 serio_destroy_port(event->object);
290 case SERIO_RECONNECT:
291 serio_reconnect_port(event->object);
295 serio_disconnect_port(event->object);
296 serio_find_driver(event->object);
299 case SERIO_REGISTER_DRIVER:
300 serio_drv = event->object;
301 driver_register(&serio_drv->driver);
308 serio_remove_duplicate_events(event);
309 serio_free_event(event);
316 * Remove all events that have been submitted for a given serio port.
318 static void serio_remove_pending_events(struct serio *serio)
320 struct list_head *node, *next;
321 struct serio_event *event;
324 spin_lock_irqsave(&serio_event_lock, flags);
326 list_for_each_safe(node, next, &serio_event_list) {
327 event = list_entry(node, struct serio_event, node);
328 if (event->object == serio) {
330 serio_free_event(event);
334 spin_unlock_irqrestore(&serio_event_lock, flags);
338 * Destroy child serio port (if any) that has not been fully registered yet.
340 * Note that we rely on the fact that port can have only one child and therefore
341 * only one child registration request can be pending. Additionally, children
342 * are registered by driver's connect() handler so there can't be a grandchild
343 * pending registration together with a child.
345 static struct serio *serio_get_pending_child(struct serio *parent)
347 struct serio_event *event;
348 struct serio *serio, *child = NULL;
351 spin_lock_irqsave(&serio_event_lock, flags);
353 list_for_each_entry(event, &serio_event_list, node) {
354 if (event->type == SERIO_REGISTER_PORT) {
355 serio = event->object;
356 if (serio->parent == parent) {
363 spin_unlock_irqrestore(&serio_event_lock, flags);
367 static int serio_thread(void *nothing)
370 serio_handle_events();
371 wait_event_interruptible(serio_wait,
372 kthread_should_stop() || !list_empty(&serio_event_list));
373 try_to_freeze(PF_FREEZE);
374 } while (!kthread_should_stop());
376 printk(KERN_DEBUG "serio: kseriod exiting\n");
382 * Serio port operations
385 static ssize_t serio_show_description(struct device *dev, char *buf)
387 struct serio *serio = to_serio_port(dev);
388 return sprintf(buf, "%s\n", serio->name);
391 static ssize_t serio_show_id_type(struct device *dev, char *buf)
393 struct serio *serio = to_serio_port(dev);
394 return sprintf(buf, "%02x\n", serio->id.type);
397 static ssize_t serio_show_id_proto(struct device *dev, char *buf)
399 struct serio *serio = to_serio_port(dev);
400 return sprintf(buf, "%02x\n", serio->id.proto);
403 static ssize_t serio_show_id_id(struct device *dev, char *buf)
405 struct serio *serio = to_serio_port(dev);
406 return sprintf(buf, "%02x\n", serio->id.id);
409 static ssize_t serio_show_id_extra(struct device *dev, char *buf)
411 struct serio *serio = to_serio_port(dev);
412 return sprintf(buf, "%02x\n", serio->id.extra);
415 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
416 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
417 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
418 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
420 static struct attribute *serio_device_id_attrs[] = {
422 &dev_attr_proto.attr,
424 &dev_attr_extra.attr,
428 static struct attribute_group serio_id_attr_group = {
430 .attrs = serio_device_id_attrs,
433 static ssize_t serio_rebind_driver(struct device *dev, const char *buf, size_t count)
435 struct serio *serio = to_serio_port(dev);
436 struct device_driver *drv;
439 retval = down_interruptible(&serio_sem);
444 if (!strncmp(buf, "none", count)) {
445 serio_disconnect_port(serio);
446 } else if (!strncmp(buf, "reconnect", count)) {
447 serio_reconnect_port(serio);
448 } else if (!strncmp(buf, "rescan", count)) {
449 serio_disconnect_port(serio);
450 serio_find_driver(serio);
451 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
452 serio_disconnect_port(serio);
453 serio_bind_driver(serio, to_serio_driver(drv));
464 static ssize_t serio_show_bind_mode(struct device *dev, char *buf)
466 struct serio *serio = to_serio_port(dev);
467 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
470 static ssize_t serio_set_bind_mode(struct device *dev, const char *buf, size_t count)
472 struct serio *serio = to_serio_port(dev);
476 if (!strncmp(buf, "manual", count)) {
477 serio->manual_bind = 1;
478 } else if (!strncmp(buf, "auto", count)) {
479 serio->manual_bind = 0;
487 static struct device_attribute serio_device_attrs[] = {
488 __ATTR(description, S_IRUGO, serio_show_description, NULL),
489 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
490 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
495 static void serio_release_port(struct device *dev)
497 struct serio *serio = to_serio_port(dev);
500 module_put(THIS_MODULE);
504 * Prepare serio port for registration.
506 static void serio_init_port(struct serio *serio)
508 static atomic_t serio_no = ATOMIC_INIT(0);
510 __module_get(THIS_MODULE);
512 spin_lock_init(&serio->lock);
513 init_MUTEX(&serio->drv_sem);
514 device_initialize(&serio->dev);
515 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
516 "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
517 serio->dev.bus = &serio_bus;
518 serio->dev.release = serio_release_port;
520 serio->dev.parent = &serio->parent->dev;
524 * Complete serio port registration.
525 * Driver core will attempt to find appropriate driver for the port.
527 static void serio_add_port(struct serio *serio)
530 serio_pause_rx(serio->parent);
531 serio->parent->child = serio;
532 serio_continue_rx(serio->parent);
535 list_add_tail(&serio->node, &serio_list);
538 device_add(&serio->dev);
539 sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
540 serio->registered = 1;
544 * serio_destroy_port() completes deregistration process and removes
545 * port from the system
547 static void serio_destroy_port(struct serio *serio)
551 child = serio_get_pending_child(serio);
553 serio_remove_pending_events(child);
554 put_device(&child->dev);
561 serio_pause_rx(serio->parent);
562 serio->parent->child = NULL;
563 serio_continue_rx(serio->parent);
564 serio->parent = NULL;
567 if (serio->registered) {
568 sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
569 device_del(&serio->dev);
570 list_del_init(&serio->node);
571 serio->registered = 0;
574 serio_remove_pending_events(serio);
575 put_device(&serio->dev);
579 * Reconnect serio port and all its children (re-initialize attached devices)
581 static void serio_reconnect_port(struct serio *serio)
584 if (serio_reconnect_driver(serio)) {
585 serio_disconnect_port(serio);
586 serio_find_driver(serio);
587 /* Ok, old children are now gone, we are done */
590 serio = serio->child;
595 * serio_disconnect_port() unbinds a port from its driver. As a side effect
596 * all child ports are unbound and destroyed.
598 static void serio_disconnect_port(struct serio *serio)
600 struct serio *s, *parent;
604 * Children ports should be disconnected and destroyed
605 * first, staring with the leaf one, since we don't want
608 for (s = serio; s->child; s = s->child)
614 serio_release_driver(s);
615 serio_destroy_port(s);
616 } while ((s = parent) != serio);
620 * Ok, no children left, now disconnect this port
622 serio_release_driver(serio);
625 void serio_rescan(struct serio *serio)
627 serio_queue_event(serio, NULL, SERIO_RESCAN);
630 void serio_reconnect(struct serio *serio)
632 serio_queue_event(serio, NULL, SERIO_RECONNECT);
636 * Submits register request to kseriod for subsequent execution.
637 * Note that port registration is always asynchronous.
639 void __serio_register_port(struct serio *serio, struct module *owner)
641 serio_init_port(serio);
642 serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
646 * Synchronously unregisters serio port.
648 void serio_unregister_port(struct serio *serio)
651 serio_disconnect_port(serio);
652 serio_destroy_port(serio);
657 * Submits register request to kseriod for subsequent execution.
658 * Can be used when it is not obvious whether the serio_sem is
659 * taken or not and when delayed execution is feasible.
661 void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
663 serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
668 * Serio driver operations
671 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
673 struct serio_driver *driver = to_serio_driver(drv);
674 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
677 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
679 struct serio_driver *serio_drv = to_serio_driver(drv);
680 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
683 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
685 struct serio_driver *serio_drv = to_serio_driver(drv);
689 if (!strncmp(buf, "manual", count)) {
690 serio_drv->manual_bind = 1;
691 } else if (!strncmp(buf, "auto", count)) {
692 serio_drv->manual_bind = 0;
701 static struct driver_attribute serio_driver_attrs[] = {
702 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
703 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
704 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
708 static int serio_driver_probe(struct device *dev)
710 struct serio *serio = to_serio_port(dev);
711 struct serio_driver *drv = to_serio_driver(dev->driver);
713 return serio_connect_driver(serio, drv);
716 static int serio_driver_remove(struct device *dev)
718 struct serio *serio = to_serio_port(dev);
720 serio_disconnect_driver(serio);
724 void __serio_register_driver(struct serio_driver *drv, struct module *owner)
726 drv->driver.bus = &serio_bus;
727 drv->driver.probe = serio_driver_probe;
728 drv->driver.remove = serio_driver_remove;
730 serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
733 void serio_unregister_driver(struct serio_driver *drv)
738 drv->manual_bind = 1; /* so serio_find_driver ignores it */
741 list_for_each_entry(serio, &serio_list, node) {
742 if (serio->drv == drv) {
743 serio_disconnect_port(serio);
744 serio_find_driver(serio);
745 /* we could've deleted some ports, restart */
750 driver_unregister(&drv->driver);
754 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
756 serio_pause_rx(serio);
758 serio_continue_rx(serio);
761 static int serio_bus_match(struct device *dev, struct device_driver *drv)
763 struct serio *serio = to_serio_port(dev);
764 struct serio_driver *serio_drv = to_serio_driver(drv);
766 if (serio->manual_bind || serio_drv->manual_bind)
769 return serio_match_port(serio_drv->id_table, serio);
772 #ifdef CONFIG_HOTPLUG
774 #define PUT_ENVP(fmt, val) \
776 envp[i++] = buffer; \
777 length += snprintf(buffer, buffer_size - length, fmt, val); \
778 if (buffer_size - length <= 0 || i >= num_envp) \
783 static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
792 serio = to_serio_port(dev);
794 PUT_ENVP("SERIO_TYPE=%02x", serio->id.type);
795 PUT_ENVP("SERIO_PROTO=%02x", serio->id.proto);
796 PUT_ENVP("SERIO_ID=%02x", serio->id.id);
797 PUT_ENVP("SERIO_EXTRA=%02x", serio->id.extra);
807 static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
812 #endif /* CONFIG_HOTPLUG */
814 static int serio_resume(struct device *dev)
816 struct serio *serio = to_serio_port(dev);
818 if (serio_reconnect_driver(serio)) {
820 * Driver re-probing can take a while, so better let kseriod
829 /* called from serio_driver->connect/disconnect methods under serio_sem */
830 int serio_open(struct serio *serio, struct serio_driver *drv)
832 serio_set_drv(serio, drv);
834 if (serio->open && serio->open(serio)) {
835 serio_set_drv(serio, NULL);
841 /* called from serio_driver->connect/disconnect methods under serio_sem */
842 void serio_close(struct serio *serio)
847 serio_set_drv(serio, NULL);
850 irqreturn_t serio_interrupt(struct serio *serio,
851 unsigned char data, unsigned int dfl, struct pt_regs *regs)
854 irqreturn_t ret = IRQ_NONE;
856 spin_lock_irqsave(&serio->lock, flags);
858 if (likely(serio->drv)) {
859 ret = serio->drv->interrupt(serio, data, dfl, regs);
860 } else if (!dfl && serio->registered) {
865 spin_unlock_irqrestore(&serio->lock, flags);
870 static int __init serio_init(void)
872 serio_task = kthread_run(serio_thread, NULL, "kseriod");
873 if (IS_ERR(serio_task)) {
874 printk(KERN_ERR "serio: Failed to start kseriod\n");
875 return PTR_ERR(serio_task);
878 serio_bus.dev_attrs = serio_device_attrs;
879 serio_bus.drv_attrs = serio_driver_attrs;
880 serio_bus.match = serio_bus_match;
881 serio_bus.hotplug = serio_hotplug;
882 serio_bus.resume = serio_resume;
883 bus_register(&serio_bus);
888 static void __exit serio_exit(void)
890 bus_unregister(&serio_bus);
891 kthread_stop(serio_task);
894 module_init(serio_init);
895 module_exit(serio_exit);