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_show_driver(struct device
*dev
, char *buf
)
251 return sprintf(buf
, "%s\n", dev
->driver
? dev
->driver
->name
: "(none)");
254 static ssize_t
serio_rebind_driver(struct device
*dev
, const char *buf
, size_t count
)
256 struct serio
*serio
= to_serio_port(dev
);
257 struct device_driver
*drv
;
260 retval
= down_interruptible(&serio_sem
);
265 if (!strncmp(buf
, "none", count
)) {
266 serio_disconnect_port(serio
);
267 } else if (!strncmp(buf
, "reconnect", count
)) {
268 serio_reconnect_port(serio
);
269 } else if (!strncmp(buf
, "rescan", count
)) {
270 serio_disconnect_port(serio
);
271 serio_connect_port(serio
, NULL
);
272 } else if ((drv
= driver_find(buf
, &serio_bus
)) != NULL
) {
273 serio_disconnect_port(serio
);
274 serio_connect_port(serio
, to_serio_driver(drv
));
285 static ssize_t
serio_show_bind_mode(struct device
*dev
, char *buf
)
287 struct serio
*serio
= to_serio_port(dev
);
288 return sprintf(buf
, "%s\n", serio
->manual_bind
? "manual" : "auto");
291 static ssize_t
serio_set_bind_mode(struct device
*dev
, const char *buf
, size_t count
)
293 struct serio
*serio
= to_serio_port(dev
);
297 if (!strncmp(buf
, "manual", count
)) {
298 serio
->manual_bind
= 1;
299 } else if (!strncmp(buf
, "auto", count
)) {
300 serio
->manual_bind
= 0;
308 static struct device_attribute serio_device_attrs
[] = {
309 __ATTR(description
, S_IRUGO
, serio_show_description
, NULL
),
310 __ATTR(driver
, S_IWUSR
| S_IRUGO
, serio_show_driver
, serio_rebind_driver
),
311 __ATTR(bind_mode
, S_IWUSR
| S_IRUGO
, serio_show_bind_mode
, serio_set_bind_mode
),
316 static void serio_release_port(struct device
*dev
)
318 struct serio
*serio
= to_serio_port(dev
);
321 module_put(THIS_MODULE
);
324 static void serio_create_port(struct serio
*serio
)
326 try_module_get(THIS_MODULE
);
328 spin_lock_init(&serio
->lock
);
329 list_add_tail(&serio
->node
, &serio_list
);
330 snprintf(serio
->dev
.bus_id
, sizeof(serio
->dev
.bus_id
), "serio%d", serio_no
++);
331 serio
->dev
.bus
= &serio_bus
;
332 serio
->dev
.release
= serio_release_port
;
334 serio
->dev
.parent
= &serio
->parent
->dev
;
335 device_register(&serio
->dev
);
339 * serio_destroy_port() completes deregistration process and removes
340 * port from the system
342 static void serio_destroy_port(struct serio
*serio
)
344 struct serio_driver
*drv
= serio
->drv
;
347 serio_remove_pending_events(serio
);
348 list_del_init(&serio
->node
);
351 drv
->disconnect(serio
);
352 down_write(&serio_bus
.subsys
.rwsem
);
353 device_release_driver(&serio
->dev
);
354 up_write(&serio_bus
.subsys
.rwsem
);
355 put_driver(&drv
->driver
);
359 spin_lock_irqsave(&serio
->parent
->lock
, flags
);
360 serio
->parent
->child
= NULL
;
361 spin_unlock_irqrestore(&serio
->parent
->lock
, flags
);
364 device_unregister(&serio
->dev
);
368 * serio_connect_port() tries to bind the port and possible all its
369 * children to appropriate drivers. If driver passed in the function will not
370 * try otehr drivers when binding parent port.
372 static void serio_connect_port(struct serio
*serio
, struct serio_driver
*drv
)
375 WARN_ON(serio
->child
);
378 serio_bind_driver(serio
, drv
);
379 else if (!serio
->manual_bind
)
380 serio_find_driver(serio
);
382 /* Ok, now bind children, if any */
383 while (serio
->child
) {
384 serio
= serio
->child
;
387 WARN_ON(serio
->child
);
389 serio_create_port(serio
);
391 if (!serio
->manual_bind
) {
393 * With children we just _prefer_ passed in driver,
394 * but we will try other options in case preferred
397 if (!drv
|| !serio_bind_driver(serio
, drv
))
398 serio_find_driver(serio
);
406 static void serio_reconnect_port(struct serio
*serio
)
409 if (!serio
->drv
|| !serio
->drv
->reconnect
|| serio
->drv
->reconnect(serio
)) {
410 serio_disconnect_port(serio
);
411 serio_connect_port(serio
, NULL
);
412 /* Ok, old children are now gone, we are done */
415 serio
= serio
->child
;
420 * serio_disconnect_port() unbinds a port from its driver. As a side effect
421 * all child ports are unbound and destroyed.
423 static void serio_disconnect_port(struct serio
*serio
)
425 struct serio_driver
*drv
= serio
->drv
;
430 * Children ports should be disconnected and destroyed
431 * first, staring with the leaf one, since we don't want
440 serio_destroy_port(s
->child
);
445 * Ok, no children left, now disconnect this port
448 drv
->disconnect(serio
);
449 down_write(&serio_bus
.subsys
.rwsem
);
450 device_release_driver(&serio
->dev
);
451 up_write(&serio_bus
.subsys
.rwsem
);
452 put_driver(&drv
->driver
);
456 void serio_rescan(struct serio
*serio
)
458 serio_queue_event(serio
, SERIO_RESCAN
);
461 void serio_reconnect(struct serio
*serio
)
463 serio_queue_event(serio
, SERIO_RECONNECT
);
466 void serio_register_port(struct serio
*serio
)
469 serio_create_port(serio
);
470 serio_connect_port(serio
, NULL
);
475 * Submits register request to kseriod for subsequent execution.
476 * Can be used when it is not obvious whether the serio_sem is
477 * taken or not and when delayed execution is feasible.
479 void serio_register_port_delayed(struct serio
*serio
)
481 serio_queue_event(serio
, SERIO_REGISTER_PORT
);
484 void serio_unregister_port(struct serio
*serio
)
487 serio_disconnect_port(serio
);
488 serio_destroy_port(serio
);
493 * Submits unregister request to kseriod for subsequent execution.
494 * Can be used when it is not obvious whether the serio_sem is
495 * taken or not and when delayed execution is feasible.
497 void serio_unregister_port_delayed(struct serio
*serio
)
499 serio_queue_event(serio
, SERIO_UNREGISTER_PORT
);
504 * Serio driver operations
507 static ssize_t
serio_driver_show_description(struct device_driver
*drv
, char *buf
)
509 struct serio_driver
*driver
= to_serio_driver(drv
);
510 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
513 static ssize_t
serio_driver_show_bind_mode(struct device_driver
*drv
, char *buf
)
515 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
516 return sprintf(buf
, "%s\n", serio_drv
->manual_bind
? "manual" : "auto");
519 static ssize_t
serio_driver_set_bind_mode(struct device_driver
*drv
, const char *buf
, size_t count
)
521 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
525 if (!strncmp(buf
, "manual", count
)) {
526 serio_drv
->manual_bind
= 1;
527 } else if (!strncmp(buf
, "auto", count
)) {
528 serio_drv
->manual_bind
= 0;
537 static struct driver_attribute serio_driver_attrs
[] = {
538 __ATTR(description
, S_IRUGO
, serio_driver_show_description
, NULL
),
539 __ATTR(bind_mode
, S_IWUSR
| S_IRUGO
,
540 serio_driver_show_bind_mode
, serio_driver_set_bind_mode
),
544 void serio_register_driver(struct serio_driver
*drv
)
550 list_add_tail(&drv
->node
, &serio_driver_list
);
552 drv
->driver
.bus
= &serio_bus
;
553 driver_register(&drv
->driver
);
555 if (drv
->manual_bind
)
559 list_for_each_entry(serio
, &serio_list
, node
) {
561 serio_connect_port(serio
, drv
);
563 * if new child appeared then the list is changed,
564 * we need to start over
575 void serio_unregister_driver(struct serio_driver
*drv
)
581 list_del_init(&drv
->node
);
584 list_for_each_entry(serio
, &serio_list
, node
) {
585 if (serio
->drv
== drv
) {
586 serio_disconnect_port(serio
);
587 serio_connect_port(serio
, NULL
);
588 /* we could've deleted some ports, restart */
593 driver_unregister(&drv
->driver
);
598 /* called from serio_driver->connect/disconnect methods under serio_sem */
599 int serio_open(struct serio
*serio
, struct serio_driver
*drv
)
601 serio_pause_rx(serio
);
603 serio_continue_rx(serio
);
605 if (serio
->open
&& serio
->open(serio
)) {
606 serio_pause_rx(serio
);
608 serio_continue_rx(serio
);
614 /* called from serio_driver->connect/disconnect methods under serio_sem */
615 void serio_close(struct serio
*serio
)
620 serio_pause_rx(serio
);
622 serio_continue_rx(serio
);
625 irqreturn_t
serio_interrupt(struct serio
*serio
,
626 unsigned char data
, unsigned int dfl
, struct pt_regs
*regs
)
629 irqreturn_t ret
= IRQ_NONE
;
631 spin_lock_irqsave(&serio
->lock
, flags
);
633 if (likely(serio
->drv
)) {
634 ret
= serio
->drv
->interrupt(serio
, data
, dfl
, regs
);
637 if ((serio
->type
!= SERIO_8042
&&
638 serio
->type
!= SERIO_8042_XL
) || (data
== 0xaa)) {
645 spin_unlock_irqrestore(&serio
->lock
, flags
);
650 static int __init
serio_init(void)
652 if (!(serio_pid
= kernel_thread(serio_thread
, NULL
, CLONE_KERNEL
))) {
653 printk(KERN_WARNING
"serio: Failed to start kseriod\n");
657 serio_bus
.dev_attrs
= serio_device_attrs
;
658 serio_bus
.drv_attrs
= serio_driver_attrs
;
659 bus_register(&serio_bus
);
664 static void __exit
serio_exit(void)
666 bus_unregister(&serio_bus
);
667 kill_proc(serio_pid
, SIGTERM
, 1);
668 wait_for_completion(&serio_exited
);
671 module_init(serio_init
);
672 module_exit(serio_exit
);