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
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/stddef.h>
28 #include <linux/module.h>
29 #include <linux/serio.h>
30 #include <linux/errno.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/workqueue.h>
34 #include <linux/mutex.h>
36 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
37 MODULE_DESCRIPTION("Serio abstraction core");
38 MODULE_LICENSE("GPL");
41 * serio_mutex protects entire serio subsystem and is taken every time
42 * serio port or driver registered or unregistered.
44 static DEFINE_MUTEX(serio_mutex
);
46 static LIST_HEAD(serio_list
);
48 static void serio_add_port(struct serio
*serio
);
49 static int serio_reconnect_port(struct serio
*serio
);
50 static void serio_disconnect_port(struct serio
*serio
);
51 static void serio_reconnect_subtree(struct serio
*serio
);
52 static void serio_attach_driver(struct serio_driver
*drv
);
54 static int serio_connect_driver(struct serio
*serio
, struct serio_driver
*drv
)
58 mutex_lock(&serio
->drv_mutex
);
59 retval
= drv
->connect(serio
, drv
);
60 mutex_unlock(&serio
->drv_mutex
);
65 static int serio_reconnect_driver(struct serio
*serio
)
69 mutex_lock(&serio
->drv_mutex
);
70 if (serio
->drv
&& serio
->drv
->reconnect
)
71 retval
= serio
->drv
->reconnect(serio
);
72 mutex_unlock(&serio
->drv_mutex
);
77 static void serio_disconnect_driver(struct serio
*serio
)
79 mutex_lock(&serio
->drv_mutex
);
81 serio
->drv
->disconnect(serio
);
82 mutex_unlock(&serio
->drv_mutex
);
85 static int serio_match_port(const struct serio_device_id
*ids
, struct serio
*serio
)
87 while (ids
->type
|| ids
->proto
) {
88 if ((ids
->type
== SERIO_ANY
|| ids
->type
== serio
->id
.type
) &&
89 (ids
->proto
== SERIO_ANY
|| ids
->proto
== serio
->id
.proto
) &&
90 (ids
->extra
== SERIO_ANY
|| ids
->extra
== serio
->id
.extra
) &&
91 (ids
->id
== SERIO_ANY
|| ids
->id
== serio
->id
.id
))
99 * Basic serio -> driver core mappings
102 static int serio_bind_driver(struct serio
*serio
, struct serio_driver
*drv
)
106 if (serio_match_port(drv
->id_table
, serio
)) {
108 serio
->dev
.driver
= &drv
->driver
;
109 if (serio_connect_driver(serio
, drv
)) {
110 serio
->dev
.driver
= NULL
;
114 error
= device_bind_driver(&serio
->dev
);
116 dev_warn(&serio
->dev
,
117 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
118 serio
->phys
, serio
->name
,
119 drv
->description
, error
);
120 serio_disconnect_driver(serio
);
121 serio
->dev
.driver
= NULL
;
128 static void serio_find_driver(struct serio
*serio
)
132 error
= device_attach(&serio
->dev
);
133 if (error
< 0 && error
!= -EPROBE_DEFER
)
134 dev_warn(&serio
->dev
,
135 "device_attach() failed for %s (%s), error: %d\n",
136 serio
->phys
, serio
->name
, error
);
141 * Serio event processing.
144 enum serio_event_type
{
146 SERIO_RECONNECT_PORT
,
147 SERIO_RECONNECT_SUBTREE
,
153 enum serio_event_type type
;
155 struct module
*owner
;
156 struct list_head node
;
159 static DEFINE_SPINLOCK(serio_event_lock
); /* protects serio_event_list */
160 static LIST_HEAD(serio_event_list
);
162 static struct serio_event
*serio_get_event(void)
164 struct serio_event
*event
= NULL
;
167 spin_lock_irqsave(&serio_event_lock
, flags
);
169 if (!list_empty(&serio_event_list
)) {
170 event
= list_first_entry(&serio_event_list
,
171 struct serio_event
, node
);
172 list_del_init(&event
->node
);
175 spin_unlock_irqrestore(&serio_event_lock
, flags
);
179 static void serio_free_event(struct serio_event
*event
)
181 module_put(event
->owner
);
185 static void serio_remove_duplicate_events(void *object
,
186 enum serio_event_type type
)
188 struct serio_event
*e
, *next
;
191 spin_lock_irqsave(&serio_event_lock
, flags
);
193 list_for_each_entry_safe(e
, next
, &serio_event_list
, node
) {
194 if (object
== e
->object
) {
196 * If this event is of different type we should not
197 * look further - we only suppress duplicate events
198 * that were sent back-to-back.
203 list_del_init(&e
->node
);
208 spin_unlock_irqrestore(&serio_event_lock
, flags
);
211 static void serio_handle_event(struct work_struct
*work
)
213 struct serio_event
*event
;
215 mutex_lock(&serio_mutex
);
217 while ((event
= serio_get_event())) {
219 switch (event
->type
) {
221 case SERIO_REGISTER_PORT
:
222 serio_add_port(event
->object
);
225 case SERIO_RECONNECT_PORT
:
226 serio_reconnect_port(event
->object
);
229 case SERIO_RESCAN_PORT
:
230 serio_disconnect_port(event
->object
);
231 serio_find_driver(event
->object
);
234 case SERIO_RECONNECT_SUBTREE
:
235 serio_reconnect_subtree(event
->object
);
238 case SERIO_ATTACH_DRIVER
:
239 serio_attach_driver(event
->object
);
243 serio_remove_duplicate_events(event
->object
, event
->type
);
244 serio_free_event(event
);
247 mutex_unlock(&serio_mutex
);
250 static DECLARE_WORK(serio_event_work
, serio_handle_event
);
252 static int serio_queue_event(void *object
, struct module
*owner
,
253 enum serio_event_type event_type
)
256 struct serio_event
*event
;
259 spin_lock_irqsave(&serio_event_lock
, flags
);
262 * Scan event list for the other events for the same serio port,
263 * starting with the most recent one. If event is the same we
264 * do not need add new one. If event is of different type we
265 * need to add this event and should not look further because
266 * we need to preseve sequence of distinct events.
268 list_for_each_entry_reverse(event
, &serio_event_list
, node
) {
269 if (event
->object
== object
) {
270 if (event
->type
== event_type
)
276 event
= kmalloc(sizeof(struct serio_event
), GFP_ATOMIC
);
278 pr_err("Not enough memory to queue event %d\n", event_type
);
283 if (!try_module_get(owner
)) {
284 pr_warn("Can't get module reference, dropping event %d\n",
291 event
->type
= event_type
;
292 event
->object
= object
;
293 event
->owner
= owner
;
295 list_add_tail(&event
->node
, &serio_event_list
);
296 queue_work(system_long_wq
, &serio_event_work
);
299 spin_unlock_irqrestore(&serio_event_lock
, flags
);
304 * Remove all events that have been submitted for a given
305 * object, be it serio port or driver.
307 static void serio_remove_pending_events(void *object
)
309 struct serio_event
*event
, *next
;
312 spin_lock_irqsave(&serio_event_lock
, flags
);
314 list_for_each_entry_safe(event
, next
, &serio_event_list
, node
) {
315 if (event
->object
== object
) {
316 list_del_init(&event
->node
);
317 serio_free_event(event
);
321 spin_unlock_irqrestore(&serio_event_lock
, flags
);
325 * Locate child serio port (if any) that has not been fully registered yet.
327 * Children are registered by driver's connect() handler so there can't be a
328 * grandchild pending registration together with a child.
330 static struct serio
*serio_get_pending_child(struct serio
*parent
)
332 struct serio_event
*event
;
333 struct serio
*serio
, *child
= NULL
;
336 spin_lock_irqsave(&serio_event_lock
, flags
);
338 list_for_each_entry(event
, &serio_event_list
, node
) {
339 if (event
->type
== SERIO_REGISTER_PORT
) {
340 serio
= event
->object
;
341 if (serio
->parent
== parent
) {
348 spin_unlock_irqrestore(&serio_event_lock
, flags
);
353 * Serio port operations
356 static ssize_t
serio_show_description(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
358 struct serio
*serio
= to_serio_port(dev
);
359 return sprintf(buf
, "%s\n", serio
->name
);
362 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
364 struct serio
*serio
= to_serio_port(dev
);
366 return sprintf(buf
, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
367 serio
->id
.type
, serio
->id
.proto
, serio
->id
.id
, serio
->id
.extra
);
370 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
372 struct serio
*serio
= to_serio_port(dev
);
373 return sprintf(buf
, "%02x\n", serio
->id
.type
);
376 static ssize_t
proto_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
378 struct serio
*serio
= to_serio_port(dev
);
379 return sprintf(buf
, "%02x\n", serio
->id
.proto
);
382 static ssize_t
id_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
384 struct serio
*serio
= to_serio_port(dev
);
385 return sprintf(buf
, "%02x\n", serio
->id
.id
);
388 static ssize_t
extra_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
390 struct serio
*serio
= to_serio_port(dev
);
391 return sprintf(buf
, "%02x\n", serio
->id
.extra
);
394 static ssize_t
drvctl_store(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
396 struct serio
*serio
= to_serio_port(dev
);
397 struct device_driver
*drv
;
400 error
= mutex_lock_interruptible(&serio_mutex
);
404 if (!strncmp(buf
, "none", count
)) {
405 serio_disconnect_port(serio
);
406 } else if (!strncmp(buf
, "reconnect", count
)) {
407 serio_reconnect_subtree(serio
);
408 } else if (!strncmp(buf
, "rescan", count
)) {
409 serio_disconnect_port(serio
);
410 serio_find_driver(serio
);
411 serio_remove_duplicate_events(serio
, SERIO_RESCAN_PORT
);
412 } else if ((drv
= driver_find(buf
, &serio_bus
)) != NULL
) {
413 serio_disconnect_port(serio
);
414 error
= serio_bind_driver(serio
, to_serio_driver(drv
));
415 serio_remove_duplicate_events(serio
, SERIO_RESCAN_PORT
);
420 mutex_unlock(&serio_mutex
);
422 return error
? error
: count
;
425 static ssize_t
serio_show_bind_mode(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
427 struct serio
*serio
= to_serio_port(dev
);
428 return sprintf(buf
, "%s\n", serio
->manual_bind
? "manual" : "auto");
431 static ssize_t
serio_set_bind_mode(struct device
*dev
, struct device_attribute
*attr
, const char *buf
, size_t count
)
433 struct serio
*serio
= to_serio_port(dev
);
437 if (!strncmp(buf
, "manual", count
)) {
438 serio
->manual_bind
= true;
439 } else if (!strncmp(buf
, "auto", count
)) {
440 serio
->manual_bind
= false;
448 static ssize_t
firmware_id_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
450 struct serio
*serio
= to_serio_port(dev
);
452 return sprintf(buf
, "%s\n", serio
->firmware_id
);
455 static DEVICE_ATTR_RO(type
);
456 static DEVICE_ATTR_RO(proto
);
457 static DEVICE_ATTR_RO(id
);
458 static DEVICE_ATTR_RO(extra
);
460 static struct attribute
*serio_device_id_attrs
[] = {
462 &dev_attr_proto
.attr
,
464 &dev_attr_extra
.attr
,
468 static const struct attribute_group serio_id_attr_group
= {
470 .attrs
= serio_device_id_attrs
,
473 static DEVICE_ATTR_RO(modalias
);
474 static DEVICE_ATTR_WO(drvctl
);
475 static DEVICE_ATTR(description
, S_IRUGO
, serio_show_description
, NULL
);
476 static DEVICE_ATTR(bind_mode
, S_IWUSR
| S_IRUGO
, serio_show_bind_mode
, serio_set_bind_mode
);
477 static DEVICE_ATTR_RO(firmware_id
);
479 static struct attribute
*serio_device_attrs
[] = {
480 &dev_attr_modalias
.attr
,
481 &dev_attr_description
.attr
,
482 &dev_attr_drvctl
.attr
,
483 &dev_attr_bind_mode
.attr
,
484 &dev_attr_firmware_id
.attr
,
488 static const struct attribute_group serio_device_attr_group
= {
489 .attrs
= serio_device_attrs
,
492 static const struct attribute_group
*serio_device_attr_groups
[] = {
493 &serio_id_attr_group
,
494 &serio_device_attr_group
,
498 static void serio_release_port(struct device
*dev
)
500 struct serio
*serio
= to_serio_port(dev
);
503 module_put(THIS_MODULE
);
507 * Prepare serio port for registration.
509 static void serio_init_port(struct serio
*serio
)
511 static atomic_t serio_no
= ATOMIC_INIT(-1);
513 __module_get(THIS_MODULE
);
515 INIT_LIST_HEAD(&serio
->node
);
516 INIT_LIST_HEAD(&serio
->child_node
);
517 INIT_LIST_HEAD(&serio
->children
);
518 spin_lock_init(&serio
->lock
);
519 mutex_init(&serio
->drv_mutex
);
520 device_initialize(&serio
->dev
);
521 dev_set_name(&serio
->dev
, "serio%lu",
522 (unsigned long)atomic_inc_return(&serio_no
));
523 serio
->dev
.bus
= &serio_bus
;
524 serio
->dev
.release
= serio_release_port
;
525 serio
->dev
.groups
= serio_device_attr_groups
;
527 serio
->dev
.parent
= &serio
->parent
->dev
;
528 serio
->depth
= serio
->parent
->depth
+ 1;
531 lockdep_set_subclass(&serio
->lock
, serio
->depth
);
535 * Complete serio port registration.
536 * Driver core will attempt to find appropriate driver for the port.
538 static void serio_add_port(struct serio
*serio
)
540 struct serio
*parent
= serio
->parent
;
544 serio_pause_rx(parent
);
545 list_add_tail(&serio
->child_node
, &parent
->children
);
546 serio_continue_rx(parent
);
549 list_add_tail(&serio
->node
, &serio_list
);
554 error
= device_add(&serio
->dev
);
557 "device_add() failed for %s (%s), error: %d\n",
558 serio
->phys
, serio
->name
, error
);
562 * serio_destroy_port() completes unregistration process and removes
563 * port from the system
565 static void serio_destroy_port(struct serio
*serio
)
569 while ((child
= serio_get_pending_child(serio
)) != NULL
) {
570 serio_remove_pending_events(child
);
571 put_device(&child
->dev
);
578 serio_pause_rx(serio
->parent
);
579 list_del_init(&serio
->child_node
);
580 serio_continue_rx(serio
->parent
);
581 serio
->parent
= NULL
;
584 if (device_is_registered(&serio
->dev
))
585 device_del(&serio
->dev
);
587 list_del_init(&serio
->node
);
588 serio_remove_pending_events(serio
);
589 put_device(&serio
->dev
);
593 * Reconnect serio port (re-initialize attached device).
594 * If reconnect fails (old device is no longer attached or
595 * there was no device to begin with) we do full rescan in
596 * hope of finding a driver for the port.
598 static int serio_reconnect_port(struct serio
*serio
)
600 int error
= serio_reconnect_driver(serio
);
603 serio_disconnect_port(serio
);
604 serio_find_driver(serio
);
611 * Reconnect serio port and all its children (re-initialize attached
614 static void serio_reconnect_subtree(struct serio
*root
)
616 struct serio
*s
= root
;
620 error
= serio_reconnect_port(s
);
623 * Reconnect was successful, move on to do the
626 if (!list_empty(&s
->children
)) {
627 s
= list_first_entry(&s
->children
,
628 struct serio
, child_node
);
634 * Either it was a leaf node or reconnect failed and it
635 * became a leaf node. Continue reconnecting starting with
636 * the next sibling of the parent node.
639 struct serio
*parent
= s
->parent
;
641 if (!list_is_last(&s
->child_node
, &parent
->children
)) {
642 s
= list_entry(s
->child_node
.next
,
643 struct serio
, child_node
);
653 * serio_disconnect_port() unbinds a port from its driver. As a side effect
654 * all children ports are unbound and destroyed.
656 static void serio_disconnect_port(struct serio
*serio
)
658 struct serio
*s
= serio
;
661 * Children ports should be disconnected and destroyed
662 * first; we travel the tree in depth-first order.
664 while (!list_empty(&serio
->children
)) {
667 while (!list_empty(&s
->children
))
668 s
= list_first_entry(&s
->children
,
669 struct serio
, child_node
);
672 * Prune this leaf node unless it is the one we
676 struct serio
*parent
= s
->parent
;
678 device_release_driver(&s
->dev
);
679 serio_destroy_port(s
);
686 * OK, no children left, now disconnect this port.
688 device_release_driver(&serio
->dev
);
691 void serio_rescan(struct serio
*serio
)
693 serio_queue_event(serio
, NULL
, SERIO_RESCAN_PORT
);
695 EXPORT_SYMBOL(serio_rescan
);
697 void serio_reconnect(struct serio
*serio
)
699 serio_queue_event(serio
, NULL
, SERIO_RECONNECT_SUBTREE
);
701 EXPORT_SYMBOL(serio_reconnect
);
704 * Submits register request to kseriod for subsequent execution.
705 * Note that port registration is always asynchronous.
707 void __serio_register_port(struct serio
*serio
, struct module
*owner
)
709 serio_init_port(serio
);
710 serio_queue_event(serio
, owner
, SERIO_REGISTER_PORT
);
712 EXPORT_SYMBOL(__serio_register_port
);
715 * Synchronously unregisters serio port.
717 void serio_unregister_port(struct serio
*serio
)
719 mutex_lock(&serio_mutex
);
720 serio_disconnect_port(serio
);
721 serio_destroy_port(serio
);
722 mutex_unlock(&serio_mutex
);
724 EXPORT_SYMBOL(serio_unregister_port
);
727 * Safely unregisters children ports if they are present.
729 void serio_unregister_child_port(struct serio
*serio
)
731 struct serio
*s
, *next
;
733 mutex_lock(&serio_mutex
);
734 list_for_each_entry_safe(s
, next
, &serio
->children
, child_node
) {
735 serio_disconnect_port(s
);
736 serio_destroy_port(s
);
738 mutex_unlock(&serio_mutex
);
740 EXPORT_SYMBOL(serio_unregister_child_port
);
744 * Serio driver operations
747 static ssize_t
description_show(struct device_driver
*drv
, char *buf
)
749 struct serio_driver
*driver
= to_serio_driver(drv
);
750 return sprintf(buf
, "%s\n", driver
->description
? driver
->description
: "(none)");
752 static DRIVER_ATTR_RO(description
);
754 static ssize_t
bind_mode_show(struct device_driver
*drv
, char *buf
)
756 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
757 return sprintf(buf
, "%s\n", serio_drv
->manual_bind
? "manual" : "auto");
760 static ssize_t
bind_mode_store(struct device_driver
*drv
, const char *buf
, size_t count
)
762 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
766 if (!strncmp(buf
, "manual", count
)) {
767 serio_drv
->manual_bind
= true;
768 } else if (!strncmp(buf
, "auto", count
)) {
769 serio_drv
->manual_bind
= false;
776 static DRIVER_ATTR_RW(bind_mode
);
778 static struct attribute
*serio_driver_attrs
[] = {
779 &driver_attr_description
.attr
,
780 &driver_attr_bind_mode
.attr
,
783 ATTRIBUTE_GROUPS(serio_driver
);
785 static int serio_driver_probe(struct device
*dev
)
787 struct serio
*serio
= to_serio_port(dev
);
788 struct serio_driver
*drv
= to_serio_driver(dev
->driver
);
790 return serio_connect_driver(serio
, drv
);
793 static int serio_driver_remove(struct device
*dev
)
795 struct serio
*serio
= to_serio_port(dev
);
797 serio_disconnect_driver(serio
);
801 static void serio_cleanup(struct serio
*serio
)
803 mutex_lock(&serio
->drv_mutex
);
804 if (serio
->drv
&& serio
->drv
->cleanup
)
805 serio
->drv
->cleanup(serio
);
806 mutex_unlock(&serio
->drv_mutex
);
809 static void serio_shutdown(struct device
*dev
)
811 struct serio
*serio
= to_serio_port(dev
);
813 serio_cleanup(serio
);
816 static void serio_attach_driver(struct serio_driver
*drv
)
820 error
= driver_attach(&drv
->driver
);
822 pr_warn("driver_attach() failed for %s with error %d\n",
823 drv
->driver
.name
, error
);
826 int __serio_register_driver(struct serio_driver
*drv
, struct module
*owner
, const char *mod_name
)
828 bool manual_bind
= drv
->manual_bind
;
831 drv
->driver
.bus
= &serio_bus
;
832 drv
->driver
.owner
= owner
;
833 drv
->driver
.mod_name
= mod_name
;
836 * Temporarily disable automatic binding because probing
837 * takes long time and we are better off doing it in kseriod
839 drv
->manual_bind
= true;
841 error
= driver_register(&drv
->driver
);
843 pr_err("driver_register() failed for %s, error: %d\n",
844 drv
->driver
.name
, error
);
849 * Restore original bind mode and let kseriod bind the
850 * driver to free ports
853 drv
->manual_bind
= false;
854 error
= serio_queue_event(drv
, NULL
, SERIO_ATTACH_DRIVER
);
856 driver_unregister(&drv
->driver
);
863 EXPORT_SYMBOL(__serio_register_driver
);
865 void serio_unregister_driver(struct serio_driver
*drv
)
869 mutex_lock(&serio_mutex
);
871 drv
->manual_bind
= true; /* so serio_find_driver ignores it */
872 serio_remove_pending_events(drv
);
875 list_for_each_entry(serio
, &serio_list
, node
) {
876 if (serio
->drv
== drv
) {
877 serio_disconnect_port(serio
);
878 serio_find_driver(serio
);
879 /* we could've deleted some ports, restart */
884 driver_unregister(&drv
->driver
);
885 mutex_unlock(&serio_mutex
);
887 EXPORT_SYMBOL(serio_unregister_driver
);
889 static void serio_set_drv(struct serio
*serio
, struct serio_driver
*drv
)
891 serio_pause_rx(serio
);
893 serio_continue_rx(serio
);
896 static int serio_bus_match(struct device
*dev
, struct device_driver
*drv
)
898 struct serio
*serio
= to_serio_port(dev
);
899 struct serio_driver
*serio_drv
= to_serio_driver(drv
);
901 if (serio
->manual_bind
|| serio_drv
->manual_bind
)
904 return serio_match_port(serio_drv
->id_table
, serio
);
907 #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
909 int err = add_uevent_var(env, fmt, val); \
914 static int serio_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
921 serio
= to_serio_port(dev
);
923 SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio
->id
.type
);
924 SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio
->id
.proto
);
925 SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio
->id
.id
);
926 SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio
->id
.extra
);
928 SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
929 serio
->id
.type
, serio
->id
.proto
, serio
->id
.id
, serio
->id
.extra
);
931 if (serio
->firmware_id
[0])
932 SERIO_ADD_UEVENT_VAR("SERIO_FIRMWARE_ID=%s",
937 #undef SERIO_ADD_UEVENT_VAR
940 static int serio_suspend(struct device
*dev
)
942 struct serio
*serio
= to_serio_port(dev
);
944 serio_cleanup(serio
);
949 static int serio_resume(struct device
*dev
)
951 struct serio
*serio
= to_serio_port(dev
);
954 mutex_lock(&serio
->drv_mutex
);
955 if (serio
->drv
&& serio
->drv
->fast_reconnect
) {
956 error
= serio
->drv
->fast_reconnect(serio
);
957 if (error
&& error
!= -ENOENT
)
958 dev_warn(dev
, "fast reconnect failed with error %d\n",
961 mutex_unlock(&serio
->drv_mutex
);
965 * Driver reconnect can take a while, so better let
966 * kseriod deal with it.
968 serio_queue_event(serio
, NULL
, SERIO_RECONNECT_PORT
);
974 static const struct dev_pm_ops serio_pm_ops
= {
975 .suspend
= serio_suspend
,
976 .resume
= serio_resume
,
977 .poweroff
= serio_suspend
,
978 .restore
= serio_resume
,
980 #endif /* CONFIG_PM */
982 /* called from serio_driver->connect/disconnect methods under serio_mutex */
983 int serio_open(struct serio
*serio
, struct serio_driver
*drv
)
985 serio_set_drv(serio
, drv
);
987 if (serio
->open
&& serio
->open(serio
)) {
988 serio_set_drv(serio
, NULL
);
993 EXPORT_SYMBOL(serio_open
);
995 /* called from serio_driver->connect/disconnect methods under serio_mutex */
996 void serio_close(struct serio
*serio
)
1001 serio_set_drv(serio
, NULL
);
1003 EXPORT_SYMBOL(serio_close
);
1005 irqreturn_t
serio_interrupt(struct serio
*serio
,
1006 unsigned char data
, unsigned int dfl
)
1008 unsigned long flags
;
1009 irqreturn_t ret
= IRQ_NONE
;
1011 spin_lock_irqsave(&serio
->lock
, flags
);
1013 if (likely(serio
->drv
)) {
1014 ret
= serio
->drv
->interrupt(serio
, data
, dfl
);
1015 } else if (!dfl
&& device_is_registered(&serio
->dev
)) {
1016 serio_rescan(serio
);
1020 spin_unlock_irqrestore(&serio
->lock
, flags
);
1024 EXPORT_SYMBOL(serio_interrupt
);
1026 struct bus_type serio_bus
= {
1028 .drv_groups
= serio_driver_groups
,
1029 .match
= serio_bus_match
,
1030 .uevent
= serio_uevent
,
1031 .probe
= serio_driver_probe
,
1032 .remove
= serio_driver_remove
,
1033 .shutdown
= serio_shutdown
,
1035 .pm
= &serio_pm_ops
,
1038 EXPORT_SYMBOL(serio_bus
);
1040 static int __init
serio_init(void)
1044 error
= bus_register(&serio_bus
);
1046 pr_err("Failed to register serio bus, error: %d\n", error
);
1053 static void __exit
serio_exit(void)
1055 bus_unregister(&serio_bus
);
1058 * There should not be any outstanding events but work may
1059 * still be scheduled so simply cancel it.
1061 cancel_work_sync(&serio_event_work
);
1064 subsys_initcall(serio_init
);
1065 module_exit(serio_exit
);