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/slab.h>
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION("Serio abstraction core");
41 MODULE_LICENSE("GPL");
43 EXPORT_SYMBOL(serio_interrupt
);
44 EXPORT_SYMBOL(__serio_register_port
);
45 EXPORT_SYMBOL(serio_unregister_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_match_port(const struct serio_device_id
*ids
, struct serio
*serio
)
73 while (ids
->type
|| ids
->proto
) {
74 if ((ids
->type
== SERIO_ANY
|| ids
->type
== serio
->id
.type
) &&
75 (ids
->proto
== SERIO_ANY
|| ids
->proto
== serio
->id
.proto
) &&
76 (ids
->extra
== SERIO_ANY
|| ids
->extra
== serio
->id
.extra
) &&
77 (ids
->id
== SERIO_ANY
|| ids
->id
== serio
->id
.id
))
85 * Basic serio -> driver core mappings
88 static void serio_bind_driver(struct serio
*serio
, struct serio_driver
*drv
)
90 down_write(&serio_bus
.subsys
.rwsem
);
92 if (serio_match_port(drv
->id_table
, serio
)) {
93 serio
->dev
.driver
= &drv
->driver
;
94 if (drv
->connect(serio
, drv
)) {
95 serio
->dev
.driver
= NULL
;
98 device_bind_driver(&serio
->dev
);
101 up_write(&serio_bus
.subsys
.rwsem
);
104 static void serio_release_driver(struct serio
*serio
)
106 down_write(&serio_bus
.subsys
.rwsem
);
107 device_release_driver(&serio
->dev
);
108 up_write(&serio_bus
.subsys
.rwsem
);
111 static void serio_find_driver(struct serio
*serio
)
113 down_write(&serio_bus
.subsys
.rwsem
);
114 device_attach(&serio
->dev
);
115 up_write(&serio_bus
.subsys
.rwsem
);
120 * Serio event processing.
123 enum serio_event_type
{
127 SERIO_UNREGISTER_PORT
,
128 SERIO_REGISTER_DRIVER
,
132 enum serio_event_type type
;
134 struct module
*owner
;
135 struct list_head node
;
138 static DEFINE_SPINLOCK(serio_event_lock
); /* protects serio_event_list */
139 static LIST_HEAD(serio_event_list
);
140 static DECLARE_WAIT_QUEUE_HEAD(serio_wait
);
141 static DECLARE_COMPLETION(serio_exited
);
142 static int serio_pid
;
144 static void serio_queue_event(void *object
, struct module
*owner
,
145 enum serio_event_type event_type
)
148 struct serio_event
*event
;
150 spin_lock_irqsave(&serio_event_lock
, flags
);
153 * Scan event list for the other events for the same serio port,
154 * starting with the most recent one. If event is the same we
155 * do not need add new one. If event is of different type we
156 * need to add this event and should not look further because
157 * we need to preseve sequence of distinct events.
159 list_for_each_entry_reverse(event
, &serio_event_list
, node
) {
160 if (event
->object
== object
) {
161 if (event
->type
== event_type
)
167 if ((event
= kmalloc(sizeof(struct serio_event
), GFP_ATOMIC
))) {
168 if (!try_module_get(owner
)) {
169 printk(KERN_WARNING
"serio: Can't get module reference, dropping event %d\n", event_type
);
173 event
->type
= event_type
;
174 event
->object
= object
;
175 event
->owner
= owner
;
177 list_add_tail(&event
->node
, &serio_event_list
);
178 wake_up(&serio_wait
);
180 printk(KERN_ERR
"serio: Not enough memory to queue event %d\n", event_type
);
183 spin_unlock_irqrestore(&serio_event_lock
, flags
);
186 static void serio_free_event(struct serio_event
*event
)
188 module_put(event
->owner
);
192 static void serio_remove_duplicate_events(struct serio_event
*event
)
194 struct list_head
*node
, *next
;
195 struct serio_event
*e
;
198 spin_lock_irqsave(&serio_event_lock
, flags
);
200 list_for_each_safe(node
, next
, &serio_event_list
) {
201 e
= list_entry(node
, struct serio_event
, node
);
202 if (event
->object
== e
->object
) {
204 * If this event is of different type we should not
205 * look further - we only suppress duplicate events
206 * that were sent back-to-back.
208 if (event
->type
!= e
->type
)
216 spin_unlock_irqrestore(&serio_event_lock
, flags
);
220 static struct serio_event
*serio_get_event(void)
222 struct serio_event
*event
;
223 struct list_head
*node
;
226 spin_lock_irqsave(&serio_event_lock
, flags
);
228 if (list_empty(&serio_event_list
)) {
229 spin_unlock_irqrestore(&serio_event_lock
, flags
);
233 node
= serio_event_list
.next
;
234 event
= list_entry(node
, struct serio_event
, node
);
237 spin_unlock_irqrestore(&serio_event_lock
, flags
);
242 static void serio_handle_events(void)
244 struct serio_event
*event
;
245 struct serio_driver
*serio_drv
;
249 while ((event
= serio_get_event())) {
251 switch (event
->type
) {
252 case SERIO_REGISTER_PORT
:
253 serio_add_port(event
->object
);
256 case SERIO_UNREGISTER_PORT
:
257 serio_disconnect_port(event
->object
);
258 serio_destroy_port(event
->object
);
261 case SERIO_RECONNECT
:
262 serio_reconnect_port(event
->object
);
266 serio_disconnect_port(event
->object
);
267 serio_find_driver(event
->object
);
270 case SERIO_REGISTER_DRIVER
:
271 serio_drv
= event
->object
;
272 driver_register(&serio_drv
->driver
);
279 serio_remove_duplicate_events(event
);
280 serio_free_event(event
);
287 * Remove all events that have been submitted for a given serio port.
289 static void serio_remove_pending_events(struct serio
*serio
)
291 struct list_head
*node
, *next
;
292 struct serio_event
*event
;
295 spin_lock_irqsave(&serio_event_lock
, flags
);
297 list_for_each_safe(node
, next
, &serio_event_list
) {
298 event
= list_entry(node
, struct serio_event
, node
);
299 if (event
->object
== serio
) {
301 serio_free_event(event
);
305 spin_unlock_irqrestore(&serio_event_lock
, flags
);
309 * Destroy child serio port (if any) that has not been fully registered yet.
311 * Note that we rely on the fact that port can have only one child and therefore
312 * only one child registration request can be pending. Additionally, children
313 * are registered by driver's connect() handler so there can't be a grandchild
314 * pending registration together with a child.
316 static struct serio
*serio_get_pending_child(struct serio
*parent
)
318 struct serio_event
*event
;
319 struct serio
*serio
, *child
= NULL
;
322 spin_lock_irqsave(&serio_event_lock
, flags
);
324 list_for_each_entry(event
, &serio_event_list
, node
) {
325 if (event
->type
== SERIO_REGISTER_PORT
) {
326 serio
= event
->object
;
327 if (serio
->parent
== parent
) {
334 spin_unlock_irqrestore(&serio_event_lock
, flags
);
338 static int serio_thread(void *nothing
)
341 daemonize("kseriod");
342 allow_signal(SIGTERM
);
345 serio_handle_events();
346 wait_event_interruptible(serio_wait
, !list_empty(&serio_event_list
));
347 try_to_freeze(PF_FREEZE
);
348 } while (!signal_pending(current
));
350 printk(KERN_DEBUG
"serio: kseriod exiting\n");
353 complete_and_exit(&serio_exited
, 0);
358 * Serio port operations
361 static ssize_t
serio_show_description(struct device
*dev
, char *buf
)
363 struct serio
*serio
= to_serio_port(dev
);
364 return sprintf(buf
, "%s\n", serio
->name
);
367 static ssize_t
serio_show_id_type(struct device
*dev
, char *buf
)
369 struct serio
*serio
= to_serio_port(dev
);
370 return sprintf(buf
, "%02x\n", serio
->id
.type
);
373 static ssize_t
serio_show_id_proto(struct device
*dev
, char *buf
)
375 struct serio
*serio
= to_serio_port(dev
);
376 return sprintf(buf
, "%02x\n", serio
->id
.proto
);
379 static ssize_t
serio_show_id_id(struct device
*dev
, char *buf
)
381 struct serio
*serio
= to_serio_port(dev
);
382 return sprintf(buf
, "%02x\n", serio
->id
.id
);
385 static ssize_t
serio_show_id_extra(struct device
*dev
, char *buf
)
387 struct serio
*serio
= to_serio_port(dev
);
388 return sprintf(buf
, "%02x\n", serio
->id
.extra
);
391 static ssize_t
serio_rebind_driver(struct device
*dev
, const char *buf
, size_t count
)
393 struct serio
*serio
= to_serio_port(dev
);
394 struct device_driver
*drv
;
397 retval
= down_interruptible(&serio_sem
);
402 if (!strncmp(buf
, "none", count
)) {
403 serio_disconnect_port(serio
);
404 } else if (!strncmp(buf
, "reconnect", count
)) {
405 serio_reconnect_port(serio
);
406 } else if (!strncmp(buf
, "rescan", count
)) {
407 serio_disconnect_port(serio
);
408 serio_find_driver(serio
);
409 } else if ((drv
= driver_find(buf
, &serio_bus
)) != NULL
) {
410 serio_disconnect_port(serio
);
411 serio_bind_driver(serio
, to_serio_driver(drv
));
422 static ssize_t
serio_show_bind_mode(struct device
*dev
, char *buf
)
424 struct serio
*serio
= to_serio_port(dev
);
425 return sprintf(buf
, "%s\n", serio
->manual_bind
? "manual" : "auto");
428 static ssize_t
serio_set_bind_mode(struct device
*dev
, const char *buf
, size_t count
)
430 struct serio
*serio
= to_serio_port(dev
);
434 if (!strncmp(buf
, "manual", count
)) {
435 serio
->manual_bind
= 1;
436 } else if (!strncmp(buf
, "auto", count
)) {
437 serio
->manual_bind
= 0;
445 static struct device_attribute serio_device_attrs
[] = {
446 __ATTR(description
, S_IRUGO
, serio_show_description
, NULL
),
447 __ATTR(id_type
, S_IRUGO
, serio_show_id_type
, NULL
),
448 __ATTR(id_proto
, S_IRUGO
, serio_show_id_proto
, NULL
),
449 __ATTR(id_id
, S_IRUGO
, serio_show_id_id
, NULL
),
450 __ATTR(id_extra
, S_IRUGO
, serio_show_id_extra
, NULL
),
451 __ATTR(drvctl
, S_IWUSR
, NULL
, serio_rebind_driver
),
452 __ATTR(bind_mode
, S_IWUSR
| S_IRUGO
, serio_show_bind_mode
, serio_set_bind_mode
),
457 static void serio_release_port(struct device
*dev
)
459 struct serio
*serio
= to_serio_port(dev
);
462 module_put(THIS_MODULE
);
466 * Prepare serio port for registration.
468 static void serio_init_port(struct serio
*serio
)
470 static atomic_t serio_no
= ATOMIC_INIT(0);
472 __module_get(THIS_MODULE
);
474 spin_lock_init(&serio
->lock
);
475 init_MUTEX(&serio
->drv_sem
);
476 device_initialize(&serio
->dev
);
477 snprintf(serio
->dev
.bus_id
, sizeof(serio
->dev
.bus_id
),
478 "serio%ld", (long)atomic_inc_return(&serio_no
) - 1);
479 serio
->dev
.bus
= &serio_bus
;
480 serio
->dev
.release
= serio_release_port
;
482 serio
->dev
.parent
= &serio
->parent
->dev
;
486 * Complete serio port registration.
487 * Driver core will attempt to find appropriate driver for the port.
489 static void serio_add_port(struct serio
*serio
)
492 serio_pause_rx(serio
->parent
);
493 serio
->parent
->child
= serio
;
494 serio_continue_rx(serio
->parent
);
497 list_add_tail(&serio
->node
, &serio_list
);
500 device_add(&serio
->dev
);
501 serio
->registered
= 1;
505 * serio_destroy_port() completes deregistration process and removes
506 * port from the system
508 static void serio_destroy_port(struct serio
*serio
)
512 child
= serio_get_pending_child(serio
);
514 serio_remove_pending_events(child
);
515 put_device(&child
->dev
);
522 serio_pause_rx(serio
->parent
);
523 serio
->parent
->child
= NULL
;
524 serio_continue_rx(serio
->parent
);
525 serio
->parent
= NULL
;
528 if (serio
->registered
) {
529 device_del(&serio
->dev
);
530 list_del_init(&serio
->node
);
531 serio
->registered
= 0;
534 serio_remove_pending_events(serio
);
535 put_device(&serio
->dev
);
539 * Reconnect serio port and all its children (re-initialize attached devices)
541 static void serio_reconnect_port(struct serio
*serio
)
544 if (!serio
->drv
|| !serio
->drv
->reconnect
|| serio
->drv
->reconnect(serio
)) {
545 serio_disconnect_port(serio
);
546 serio_find_driver(serio
);
547 /* Ok, old children are now gone, we are done */
550 serio
= serio
->child
;
555 * serio_disconnect_port() unbinds a port from its driver. As a side effect
556 * all child ports are unbound and destroyed.
558 static void serio_disconnect_port(struct serio
*serio
)
560 struct serio
*s
, *parent
;
564 * Children ports should be disconnected and destroyed
565 * first, staring with the leaf one, since we don't want
568 for (s
= serio
; s
->child
; s
= s
->child
)
574 serio_release_driver(s
);
575 serio_destroy_port(s
);
576 } while ((s
= parent
) != serio
);
580 * Ok, no children left, now disconnect this port
582 serio_release_driver(serio
);
585 void serio_rescan(struct serio
*serio
)
587 serio_queue_event(serio
, NULL
, SERIO_RESCAN
);
590 void serio_reconnect(struct serio
*serio
)
592 serio_queue_event(serio
, NULL
, SERIO_RECONNECT
);
596 * Submits register request to kseriod for subsequent execution.
597 * Note that port registration is always asynchronous.
599 void __serio_register_port(struct serio
*serio
, struct module
*owner
)
601 serio_init_port(serio
);
602 serio_queue_event(serio
, owner
, SERIO_REGISTER_PORT
);
606 * Synchronously unregisters serio port.
608 void serio_unregister_port(struct serio
*serio
)
611 serio_disconnect_port(serio
);
612 serio_destroy_port(serio
);
617 * Submits register request to kseriod for subsequent execution.
618 * Can be used when it is not obvious whether the serio_sem is
619 * taken or not and when delayed execution is feasible.
621 void __serio_unregister_port_delayed(struct serio
*serio
, struct module
*owner
)
623 serio_queue_event(serio
, owner
, SERIO_UNREGISTER_PORT
);
628 * Serio driver operations
631 static ssize_t
serio_driver_show_description(struct device_driver
*drv
, char *buf
)
633 struct serio_driver
*driver
= to_serio_driver(drv
);
634 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
637 static ssize_t
serio_driver_show_bind_mode(struct device_driver
*drv
, char *buf
)
639 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
640 return sprintf(buf
, "%s\n", serio_drv
->manual_bind
? "manual" : "auto");
643 static ssize_t
serio_driver_set_bind_mode(struct device_driver
*drv
, const char *buf
, size_t count
)
645 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
649 if (!strncmp(buf
, "manual", count
)) {
650 serio_drv
->manual_bind
= 1;
651 } else if (!strncmp(buf
, "auto", count
)) {
652 serio_drv
->manual_bind
= 0;
661 static struct driver_attribute serio_driver_attrs
[] = {
662 __ATTR(description
, S_IRUGO
, serio_driver_show_description
, NULL
),
663 __ATTR(bind_mode
, S_IWUSR
| S_IRUGO
,
664 serio_driver_show_bind_mode
, serio_driver_set_bind_mode
),
668 static int serio_driver_probe(struct device
*dev
)
670 struct serio
*serio
= to_serio_port(dev
);
671 struct serio_driver
*drv
= to_serio_driver(dev
->driver
);
673 return drv
->connect(serio
, drv
);
676 static int serio_driver_remove(struct device
*dev
)
678 struct serio
*serio
= to_serio_port(dev
);
679 struct serio_driver
*drv
= to_serio_driver(dev
->driver
);
681 drv
->disconnect(serio
);
685 void __serio_register_driver(struct serio_driver
*drv
, struct module
*owner
)
687 drv
->driver
.bus
= &serio_bus
;
688 drv
->driver
.probe
= serio_driver_probe
;
689 drv
->driver
.remove
= serio_driver_remove
;
691 serio_queue_event(drv
, owner
, SERIO_REGISTER_DRIVER
);
694 void serio_unregister_driver(struct serio_driver
*drv
)
699 drv
->manual_bind
= 1; /* so serio_find_driver ignores it */
702 list_for_each_entry(serio
, &serio_list
, node
) {
703 if (serio
->drv
== drv
) {
704 serio_disconnect_port(serio
);
705 serio_find_driver(serio
);
706 /* we could've deleted some ports, restart */
711 driver_unregister(&drv
->driver
);
715 static void serio_set_drv(struct serio
*serio
, struct serio_driver
*drv
)
717 down(&serio
->drv_sem
);
718 serio_pause_rx(serio
);
720 serio_continue_rx(serio
);
724 static int serio_bus_match(struct device
*dev
, struct device_driver
*drv
)
726 struct serio
*serio
= to_serio_port(dev
);
727 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
729 if (serio
->manual_bind
|| serio_drv
->manual_bind
)
732 return serio_match_port(serio_drv
->id_table
, serio
);
735 #ifdef CONFIG_HOTPLUG
737 #define PUT_ENVP(fmt, val) \
739 envp[i++] = buffer; \
740 length += snprintf(buffer, buffer_size - length, fmt, val); \
741 if (buffer_size - length <= 0 || i >= num_envp) \
746 static int serio_hotplug(struct device
*dev
, char **envp
, int num_envp
, char *buffer
, int buffer_size
)
755 serio
= to_serio_port(dev
);
757 PUT_ENVP("SERIO_TYPE=%02x", serio
->id
.type
);
758 PUT_ENVP("SERIO_PROTO=%02x", serio
->id
.proto
);
759 PUT_ENVP("SERIO_ID=%02x", serio
->id
.id
);
760 PUT_ENVP("SERIO_EXTRA=%02x", serio
->id
.extra
);
770 static int serio_hotplug(struct device
*dev
, char **envp
, int num_envp
, char *buffer
, int buffer_size
)
775 #endif /* CONFIG_HOTPLUG */
777 static int serio_resume(struct device
*dev
)
779 struct serio
*serio
= to_serio_port(dev
);
781 if (!serio
->drv
|| !serio
->drv
->reconnect
|| serio
->drv
->reconnect(serio
)) {
782 serio_disconnect_port(serio
);
784 * Driver re-probing can take a while, so better let kseriod
793 /* called from serio_driver->connect/disconnect methods under serio_sem */
794 int serio_open(struct serio
*serio
, struct serio_driver
*drv
)
796 serio_set_drv(serio
, drv
);
798 if (serio
->open
&& serio
->open(serio
)) {
799 serio_set_drv(serio
, NULL
);
805 /* called from serio_driver->connect/disconnect methods under serio_sem */
806 void serio_close(struct serio
*serio
)
811 serio_set_drv(serio
, NULL
);
814 irqreturn_t
serio_interrupt(struct serio
*serio
,
815 unsigned char data
, unsigned int dfl
, struct pt_regs
*regs
)
818 irqreturn_t ret
= IRQ_NONE
;
820 spin_lock_irqsave(&serio
->lock
, flags
);
822 if (likely(serio
->drv
)) {
823 ret
= serio
->drv
->interrupt(serio
, data
, dfl
, regs
);
824 } else if (!dfl
&& serio
->registered
) {
829 spin_unlock_irqrestore(&serio
->lock
, flags
);
834 static int __init
serio_init(void)
836 if (!(serio_pid
= kernel_thread(serio_thread
, NULL
, CLONE_KERNEL
))) {
837 printk(KERN_ERR
"serio: Failed to start kseriod\n");
841 serio_bus
.dev_attrs
= serio_device_attrs
;
842 serio_bus
.drv_attrs
= serio_driver_attrs
;
843 serio_bus
.match
= serio_bus_match
;
844 serio_bus
.hotplug
= serio_hotplug
;
845 serio_bus
.resume
= serio_resume
;
846 bus_register(&serio_bus
);
851 static void __exit
serio_exit(void)
853 bus_unregister(&serio_bus
);
854 kill_proc(serio_pid
, SIGTERM
, 1);
855 wait_for_completion(&serio_exited
);
858 module_init(serio_init
);
859 module_exit(serio_exit
);