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/completion.h>
35 #include <linux/sched.h>
36 #include <linux/smp_lock.h>
37 #include <linux/suspend.h>
38 #include <linux/slab.h>
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("Serio abstraction core");
42 MODULE_LICENSE("GPL");
44 EXPORT_SYMBOL(serio_interrupt);
45 EXPORT_SYMBOL(serio_register_port);
46 EXPORT_SYMBOL(serio_register_port_delayed);
47 EXPORT_SYMBOL(serio_unregister_port);
48 EXPORT_SYMBOL(serio_unregister_port_delayed);
49 EXPORT_SYMBOL(serio_register_driver);
50 EXPORT_SYMBOL(serio_unregister_driver);
51 EXPORT_SYMBOL(serio_open);
52 EXPORT_SYMBOL(serio_close);
53 EXPORT_SYMBOL(serio_rescan);
54 EXPORT_SYMBOL(serio_reconnect);
56 static DECLARE_MUTEX(serio_sem); /* protects serio_list and serio_diriver_list */
57 static LIST_HEAD(serio_list);
58 static LIST_HEAD(serio_driver_list);
59 static unsigned int serio_no;
61 struct bus_type serio_bus = {
65 static void serio_find_driver(struct serio *serio);
66 static void serio_create_port(struct serio *serio);
67 static void serio_destroy_port(struct serio *serio);
68 static void serio_connect_port(struct serio *serio, struct serio_driver *drv);
69 static void serio_reconnect_port(struct serio *serio);
70 static void serio_disconnect_port(struct serio *serio);
72 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
74 get_driver(&drv->driver);
76 drv->connect(serio, drv);
78 down_write(&serio_bus.subsys.rwsem);
79 serio->dev.driver = &drv->driver;
80 device_bind_driver(&serio->dev);
81 up_write(&serio_bus.subsys.rwsem);
85 put_driver(&drv->driver);
89 /* serio_find_driver() must be called with serio_sem down. */
90 static void serio_find_driver(struct serio *serio)
92 struct serio_driver *drv;
94 list_for_each_entry(drv, &serio_driver_list, node)
95 if (!drv->manual_bind)
96 if (serio_bind_driver(serio, drv))
101 * Serio event processing.
107 struct list_head node;
110 enum serio_event_type {
114 SERIO_UNREGISTER_PORT,
117 static spinlock_t serio_event_lock = SPIN_LOCK_UNLOCKED; /* protects serio_event_list */
118 static LIST_HEAD(serio_event_list);
119 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
120 static DECLARE_COMPLETION(serio_exited);
121 static int serio_pid;
123 static void serio_queue_event(struct serio *serio, int event_type)
126 struct serio_event *event;
128 spin_lock_irqsave(&serio_event_lock, flags);
130 if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
131 event->type = event_type;
132 event->serio = serio;
134 list_add_tail(&event->node, &serio_event_list);
135 wake_up(&serio_wait);
138 spin_unlock_irqrestore(&serio_event_lock, flags);
141 static struct serio_event *serio_get_event(void)
143 struct serio_event *event;
144 struct list_head *node;
147 spin_lock_irqsave(&serio_event_lock, flags);
149 if (list_empty(&serio_event_list)) {
150 spin_unlock_irqrestore(&serio_event_lock, flags);
154 node = serio_event_list.next;
155 event = container_of(node, struct serio_event, node);
158 spin_unlock_irqrestore(&serio_event_lock, flags);
163 static void serio_handle_events(void)
165 struct serio_event *event;
167 while ((event = serio_get_event())) {
171 switch (event->type) {
172 case SERIO_REGISTER_PORT :
173 serio_create_port(event->serio);
174 serio_connect_port(event->serio, NULL);
177 case SERIO_UNREGISTER_PORT :
178 serio_disconnect_port(event->serio);
179 serio_destroy_port(event->serio);
182 case SERIO_RECONNECT :
183 serio_reconnect_port(event->serio);
187 serio_disconnect_port(event->serio);
188 serio_connect_port(event->serio, NULL);
199 static void serio_remove_pending_events(struct serio *serio)
201 struct list_head *node, *next;
202 struct serio_event *event;
205 spin_lock_irqsave(&serio_event_lock, flags);
207 list_for_each_safe(node, next, &serio_event_list) {
208 event = container_of(node, struct serio_event, node);
209 if (event->serio == serio) {
215 spin_unlock_irqrestore(&serio_event_lock, flags);
219 static int serio_thread(void *nothing)
222 daemonize("kseriod");
223 allow_signal(SIGTERM);
226 serio_handle_events();
227 wait_event_interruptible(serio_wait, !list_empty(&serio_event_list));
228 if (current->flags & PF_FREEZE)
229 refrigerator(PF_FREEZE);
230 } while (!signal_pending(current));
232 printk(KERN_DEBUG "serio: kseriod exiting\n");
235 complete_and_exit(&serio_exited, 0);
240 * Serio port operations
243 static ssize_t serio_show_description(struct device *dev, char *buf)
245 struct serio *serio = to_serio_port(dev);
246 return sprintf(buf, "%s\n", serio->name);
249 static ssize_t serio_rebind_driver(struct device *dev, const char *buf, size_t count)
251 struct serio *serio = to_serio_port(dev);
252 struct device_driver *drv;
255 retval = down_interruptible(&serio_sem);
260 if (!strncmp(buf, "none", count)) {
261 serio_disconnect_port(serio);
262 } else if (!strncmp(buf, "reconnect", count)) {
263 serio_reconnect_port(serio);
264 } else if (!strncmp(buf, "rescan", count)) {
265 serio_disconnect_port(serio);
266 serio_connect_port(serio, NULL);
267 } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
268 serio_disconnect_port(serio);
269 serio_connect_port(serio, to_serio_driver(drv));
280 static ssize_t serio_show_bind_mode(struct device *dev, char *buf)
282 struct serio *serio = to_serio_port(dev);
283 return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
286 static ssize_t serio_set_bind_mode(struct device *dev, const char *buf, size_t count)
288 struct serio *serio = to_serio_port(dev);
292 if (!strncmp(buf, "manual", count)) {
293 serio->manual_bind = 1;
294 } else if (!strncmp(buf, "auto", count)) {
295 serio->manual_bind = 0;
303 static struct device_attribute serio_device_attrs[] = {
304 __ATTR(description, S_IRUGO, serio_show_description, NULL),
305 __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
306 __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
311 static void serio_release_port(struct device *dev)
313 struct serio *serio = to_serio_port(dev);
316 module_put(THIS_MODULE);
319 static void serio_create_port(struct serio *serio)
321 try_module_get(THIS_MODULE);
323 spin_lock_init(&serio->lock);
324 list_add_tail(&serio->node, &serio_list);
325 snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id), "serio%d", serio_no++);
326 serio->dev.bus = &serio_bus;
327 serio->dev.release = serio_release_port;
329 serio->dev.parent = &serio->parent->dev;
330 device_register(&serio->dev);
334 * serio_destroy_port() completes deregistration process and removes
335 * port from the system
337 static void serio_destroy_port(struct serio *serio)
339 struct serio_driver *drv = serio->drv;
342 serio_remove_pending_events(serio);
343 list_del_init(&serio->node);
346 drv->disconnect(serio);
347 down_write(&serio_bus.subsys.rwsem);
348 device_release_driver(&serio->dev);
349 up_write(&serio_bus.subsys.rwsem);
350 put_driver(&drv->driver);
354 spin_lock_irqsave(&serio->parent->lock, flags);
355 serio->parent->child = NULL;
356 spin_unlock_irqrestore(&serio->parent->lock, flags);
359 device_unregister(&serio->dev);
363 * serio_connect_port() tries to bind the port and possible all its
364 * children to appropriate drivers. If driver passed in the function will not
365 * try otehr drivers when binding parent port.
367 static void serio_connect_port(struct serio *serio, struct serio_driver *drv)
370 WARN_ON(serio->child);
373 serio_bind_driver(serio, drv);
374 else if (!serio->manual_bind)
375 serio_find_driver(serio);
377 /* Ok, now bind children, if any */
378 while (serio->child) {
379 serio = serio->child;
382 WARN_ON(serio->child);
384 serio_create_port(serio);
386 if (!serio->manual_bind) {
388 * With children we just _prefer_ passed in driver,
389 * but we will try other options in case preferred
392 if (!drv || !serio_bind_driver(serio, drv))
393 serio_find_driver(serio);
401 static void serio_reconnect_port(struct serio *serio)
404 if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) {
405 serio_disconnect_port(serio);
406 serio_connect_port(serio, NULL);
407 /* Ok, old children are now gone, we are done */
410 serio = serio->child;
415 * serio_disconnect_port() unbinds a port from its driver. As a side effect
416 * all child ports are unbound and destroyed.
418 static void serio_disconnect_port(struct serio *serio)
420 struct serio_driver *drv = serio->drv;
425 * Children ports should be disconnected and destroyed
426 * first, staring with the leaf one, since we don't want
435 serio_destroy_port(s->child);
440 * Ok, no children left, now disconnect this port
443 drv->disconnect(serio);
444 down_write(&serio_bus.subsys.rwsem);
445 device_release_driver(&serio->dev);
446 up_write(&serio_bus.subsys.rwsem);
447 put_driver(&drv->driver);
451 void serio_rescan(struct serio *serio)
453 serio_queue_event(serio, SERIO_RESCAN);
456 void serio_reconnect(struct serio *serio)
458 serio_queue_event(serio, SERIO_RECONNECT);
461 void serio_register_port(struct serio *serio)
464 serio_create_port(serio);
465 serio_connect_port(serio, NULL);
470 * Submits register request to kseriod for subsequent execution.
471 * Can be used when it is not obvious whether the serio_sem is
472 * taken or not and when delayed execution is feasible.
474 void serio_register_port_delayed(struct serio *serio)
476 serio_queue_event(serio, SERIO_REGISTER_PORT);
479 void serio_unregister_port(struct serio *serio)
482 serio_disconnect_port(serio);
483 serio_destroy_port(serio);
488 * Submits unregister request to kseriod for subsequent execution.
489 * Can be used when it is not obvious whether the serio_sem is
490 * taken or not and when delayed execution is feasible.
492 void serio_unregister_port_delayed(struct serio *serio)
494 serio_queue_event(serio, SERIO_UNREGISTER_PORT);
499 * Serio driver operations
502 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
504 struct serio_driver *driver = to_serio_driver(drv);
505 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
508 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
510 struct serio_driver *serio_drv = to_serio_driver(drv);
511 return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
514 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
516 struct serio_driver *serio_drv = to_serio_driver(drv);
520 if (!strncmp(buf, "manual", count)) {
521 serio_drv->manual_bind = 1;
522 } else if (!strncmp(buf, "auto", count)) {
523 serio_drv->manual_bind = 0;
532 static struct driver_attribute serio_driver_attrs[] = {
533 __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
534 __ATTR(bind_mode, S_IWUSR | S_IRUGO,
535 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
539 void serio_register_driver(struct serio_driver *drv)
545 list_add_tail(&drv->node, &serio_driver_list);
547 drv->driver.bus = &serio_bus;
548 driver_register(&drv->driver);
550 if (drv->manual_bind)
554 list_for_each_entry(serio, &serio_list, node) {
556 serio_connect_port(serio, drv);
558 * if new child appeared then the list is changed,
559 * we need to start over
570 void serio_unregister_driver(struct serio_driver *drv)
576 list_del_init(&drv->node);
579 list_for_each_entry(serio, &serio_list, node) {
580 if (serio->drv == drv) {
581 serio_disconnect_port(serio);
582 serio_connect_port(serio, NULL);
583 /* we could've deleted some ports, restart */
588 driver_unregister(&drv->driver);
593 /* called from serio_driver->connect/disconnect methods under serio_sem */
594 int serio_open(struct serio *serio, struct serio_driver *drv)
596 serio_pause_rx(serio);
598 serio_continue_rx(serio);
600 if (serio->open && serio->open(serio)) {
601 serio_pause_rx(serio);
603 serio_continue_rx(serio);
609 /* called from serio_driver->connect/disconnect methods under serio_sem */
610 void serio_close(struct serio *serio)
615 serio_pause_rx(serio);
617 serio_continue_rx(serio);
620 irqreturn_t serio_interrupt(struct serio *serio,
621 unsigned char data, unsigned int dfl, struct pt_regs *regs)
624 irqreturn_t ret = IRQ_NONE;
626 spin_lock_irqsave(&serio->lock, flags);
628 if (likely(serio->drv)) {
629 ret = serio->drv->interrupt(serio, data, dfl, regs);
632 if ((serio->type != SERIO_8042 &&
633 serio->type != SERIO_8042_XL) || (data == 0xaa)) {
640 spin_unlock_irqrestore(&serio->lock, flags);
645 static int __init serio_init(void)
647 if (!(serio_pid = kernel_thread(serio_thread, NULL, CLONE_KERNEL))) {
648 printk(KERN_WARNING "serio: Failed to start kseriod\n");
652 serio_bus.dev_attrs = serio_device_attrs;
653 serio_bus.drv_attrs = serio_driver_attrs;
654 bus_register(&serio_bus);
659 static void __exit serio_exit(void)
661 bus_unregister(&serio_bus);
662 kill_proc(serio_pid, SIGTERM, 1);
663 wait_for_completion(&serio_exited);
666 module_init(serio_init);
667 module_exit(serio_exit);