]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/input/serio/serio.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[net-next-2.6.git] / drivers / input / serio / serio.c
1 /*
2  *  The Serio abstraction module
3  *
4  *  Copyright (c) 1999-2004 Vojtech Pavlik
5  *  Copyright (c) 2004 Dmitry Torokhov
6  *  Copyright (c) 2003 Daniele Bellucci
7  */
8
9 /*
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.
14  *
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.
19  *
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
23  *
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
27  */
28
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
31 #include <linux/stddef.h>
32 #include <linux/module.h>
33 #include <linux/serio.h>
34 #include <linux/errno.h>
35 #include <linux/wait.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/kthread.h>
39 #include <linux/mutex.h>
40
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION("Serio abstraction core");
43 MODULE_LICENSE("GPL");
44
45 /*
46  * serio_mutex protects entire serio subsystem and is taken every time
47  * serio port or driver registrered or unregistered.
48  */
49 static DEFINE_MUTEX(serio_mutex);
50
51 static LIST_HEAD(serio_list);
52
53 static struct bus_type serio_bus;
54
55 static void serio_add_port(struct serio *serio);
56 static int serio_reconnect_port(struct serio *serio);
57 static void serio_disconnect_port(struct serio *serio);
58 static void serio_reconnect_subtree(struct serio *serio);
59 static void serio_attach_driver(struct serio_driver *drv);
60
61 static int serio_connect_driver(struct serio *serio, struct serio_driver *drv)
62 {
63         int retval;
64
65         mutex_lock(&serio->drv_mutex);
66         retval = drv->connect(serio, drv);
67         mutex_unlock(&serio->drv_mutex);
68
69         return retval;
70 }
71
72 static int serio_reconnect_driver(struct serio *serio)
73 {
74         int retval = -1;
75
76         mutex_lock(&serio->drv_mutex);
77         if (serio->drv && serio->drv->reconnect)
78                 retval = serio->drv->reconnect(serio);
79         mutex_unlock(&serio->drv_mutex);
80
81         return retval;
82 }
83
84 static void serio_disconnect_driver(struct serio *serio)
85 {
86         mutex_lock(&serio->drv_mutex);
87         if (serio->drv)
88                 serio->drv->disconnect(serio);
89         mutex_unlock(&serio->drv_mutex);
90 }
91
92 static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
93 {
94         while (ids->type || ids->proto) {
95                 if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
96                     (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
97                     (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
98                     (ids->id == SERIO_ANY || ids->id == serio->id.id))
99                         return 1;
100                 ids++;
101         }
102         return 0;
103 }
104
105 /*
106  * Basic serio -> driver core mappings
107  */
108
109 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
110 {
111         int error;
112
113         if (serio_match_port(drv->id_table, serio)) {
114
115                 serio->dev.driver = &drv->driver;
116                 if (serio_connect_driver(serio, drv)) {
117                         serio->dev.driver = NULL;
118                         return -ENODEV;
119                 }
120
121                 error = device_bind_driver(&serio->dev);
122                 if (error) {
123                         dev_warn(&serio->dev,
124                                  "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
125                                  serio->phys, serio->name,
126                                  drv->description, error);
127                         serio_disconnect_driver(serio);
128                         serio->dev.driver = NULL;
129                         return error;
130                 }
131         }
132         return 0;
133 }
134
135 static void serio_find_driver(struct serio *serio)
136 {
137         int error;
138
139         error = device_attach(&serio->dev);
140         if (error < 0)
141                 dev_warn(&serio->dev,
142                          "device_attach() failed for %s (%s), error: %d\n",
143                          serio->phys, serio->name, error);
144 }
145
146
147 /*
148  * Serio event processing.
149  */
150
151 enum serio_event_type {
152         SERIO_RESCAN_PORT,
153         SERIO_RECONNECT_PORT,
154         SERIO_RECONNECT_SUBTREE,
155         SERIO_REGISTER_PORT,
156         SERIO_ATTACH_DRIVER,
157 };
158
159 struct serio_event {
160         enum serio_event_type type;
161         void *object;
162         struct module *owner;
163         struct list_head node;
164 };
165
166 static DEFINE_SPINLOCK(serio_event_lock);       /* protects serio_event_list */
167 static LIST_HEAD(serio_event_list);
168 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
169 static struct task_struct *serio_task;
170
171 static int serio_queue_event(void *object, struct module *owner,
172                              enum serio_event_type event_type)
173 {
174         unsigned long flags;
175         struct serio_event *event;
176         int retval = 0;
177
178         spin_lock_irqsave(&serio_event_lock, flags);
179
180         /*
181          * Scan event list for the other events for the same serio port,
182          * starting with the most recent one. If event is the same we
183          * do not need add new one. If event is of different type we
184          * need to add this event and should not look further because
185          * we need to preseve sequence of distinct events.
186          */
187         list_for_each_entry_reverse(event, &serio_event_list, node) {
188                 if (event->object == object) {
189                         if (event->type == event_type)
190                                 goto out;
191                         break;
192                 }
193         }
194
195         event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
196         if (!event) {
197                 pr_err("Not enough memory to queue event %d\n", event_type);
198                 retval = -ENOMEM;
199                 goto out;
200         }
201
202         if (!try_module_get(owner)) {
203                 pr_warning("Can't get module reference, dropping event %d\n",
204                            event_type);
205                 kfree(event);
206                 retval = -EINVAL;
207                 goto out;
208         }
209
210         event->type = event_type;
211         event->object = object;
212         event->owner = owner;
213
214         list_add_tail(&event->node, &serio_event_list);
215         wake_up(&serio_wait);
216
217 out:
218         spin_unlock_irqrestore(&serio_event_lock, flags);
219         return retval;
220 }
221
222 static void serio_free_event(struct serio_event *event)
223 {
224         module_put(event->owner);
225         kfree(event);
226 }
227
228 static void serio_remove_duplicate_events(struct serio_event *event)
229 {
230         struct serio_event *e, *next;
231         unsigned long flags;
232
233         spin_lock_irqsave(&serio_event_lock, flags);
234
235         list_for_each_entry_safe(e, next, &serio_event_list, node) {
236                 if (event->object == e->object) {
237                         /*
238                          * If this event is of different type we should not
239                          * look further - we only suppress duplicate events
240                          * that were sent back-to-back.
241                          */
242                         if (event->type != e->type)
243                                 break;
244
245                         list_del_init(&e->node);
246                         serio_free_event(e);
247                 }
248         }
249
250         spin_unlock_irqrestore(&serio_event_lock, flags);
251 }
252
253
254 static struct serio_event *serio_get_event(void)
255 {
256         struct serio_event *event = NULL;
257         unsigned long flags;
258
259         spin_lock_irqsave(&serio_event_lock, flags);
260
261         if (!list_empty(&serio_event_list)) {
262                 event = list_first_entry(&serio_event_list,
263                                          struct serio_event, node);
264                 list_del_init(&event->node);
265         }
266
267         spin_unlock_irqrestore(&serio_event_lock, flags);
268         return event;
269 }
270
271 static void serio_handle_event(void)
272 {
273         struct serio_event *event;
274
275         mutex_lock(&serio_mutex);
276
277         while ((event = serio_get_event())) {
278
279                 switch (event->type) {
280
281                 case SERIO_REGISTER_PORT:
282                         serio_add_port(event->object);
283                         break;
284
285                 case SERIO_RECONNECT_PORT:
286                         serio_reconnect_port(event->object);
287                         break;
288
289                 case SERIO_RESCAN_PORT:
290                         serio_disconnect_port(event->object);
291                         serio_find_driver(event->object);
292                         break;
293
294                 case SERIO_RECONNECT_SUBTREE:
295                         serio_reconnect_subtree(event->object);
296                         break;
297
298                 case SERIO_ATTACH_DRIVER:
299                         serio_attach_driver(event->object);
300                         break;
301                 }
302
303                 serio_remove_duplicate_events(event);
304                 serio_free_event(event);
305         }
306
307         mutex_unlock(&serio_mutex);
308 }
309
310 /*
311  * Remove all events that have been submitted for a given
312  * object, be it serio port or driver.
313  */
314 static void serio_remove_pending_events(void *object)
315 {
316         struct serio_event *event, *next;
317         unsigned long flags;
318
319         spin_lock_irqsave(&serio_event_lock, flags);
320
321         list_for_each_entry_safe(event, next, &serio_event_list, node) {
322                 if (event->object == object) {
323                         list_del_init(&event->node);
324                         serio_free_event(event);
325                 }
326         }
327
328         spin_unlock_irqrestore(&serio_event_lock, flags);
329 }
330
331 /*
332  * Locate child serio port (if any) that has not been fully registered yet.
333  *
334  * Children are registered by driver's connect() handler so there can't be a
335  * grandchild pending registration together with a child.
336  */
337 static struct serio *serio_get_pending_child(struct serio *parent)
338 {
339         struct serio_event *event;
340         struct serio *serio, *child = NULL;
341         unsigned long flags;
342
343         spin_lock_irqsave(&serio_event_lock, flags);
344
345         list_for_each_entry(event, &serio_event_list, node) {
346                 if (event->type == SERIO_REGISTER_PORT) {
347                         serio = event->object;
348                         if (serio->parent == parent) {
349                                 child = serio;
350                                 break;
351                         }
352                 }
353         }
354
355         spin_unlock_irqrestore(&serio_event_lock, flags);
356         return child;
357 }
358
359 static int serio_thread(void *nothing)
360 {
361         do {
362                 serio_handle_event();
363                 wait_event_interruptible(serio_wait,
364                         kthread_should_stop() || !list_empty(&serio_event_list));
365         } while (!kthread_should_stop());
366
367         return 0;
368 }
369
370
371 /*
372  * Serio port operations
373  */
374
375 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
376 {
377         struct serio *serio = to_serio_port(dev);
378         return sprintf(buf, "%s\n", serio->name);
379 }
380
381 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
382 {
383         struct serio *serio = to_serio_port(dev);
384
385         return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
386                         serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
387 }
388
389 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
390 {
391         struct serio *serio = to_serio_port(dev);
392         return sprintf(buf, "%02x\n", serio->id.type);
393 }
394
395 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
396 {
397         struct serio *serio = to_serio_port(dev);
398         return sprintf(buf, "%02x\n", serio->id.proto);
399 }
400
401 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
402 {
403         struct serio *serio = to_serio_port(dev);
404         return sprintf(buf, "%02x\n", serio->id.id);
405 }
406
407 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
408 {
409         struct serio *serio = to_serio_port(dev);
410         return sprintf(buf, "%02x\n", serio->id.extra);
411 }
412
413 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
414 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
415 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
416 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
417
418 static struct attribute *serio_device_id_attrs[] = {
419         &dev_attr_type.attr,
420         &dev_attr_proto.attr,
421         &dev_attr_id.attr,
422         &dev_attr_extra.attr,
423         NULL
424 };
425
426 static struct attribute_group serio_id_attr_group = {
427         .name   = "id",
428         .attrs  = serio_device_id_attrs,
429 };
430
431 static const struct attribute_group *serio_device_attr_groups[] = {
432         &serio_id_attr_group,
433         NULL
434 };
435
436 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
437 {
438         struct serio *serio = to_serio_port(dev);
439         struct device_driver *drv;
440         int error;
441
442         error = mutex_lock_interruptible(&serio_mutex);
443         if (error)
444                 return error;
445
446         if (!strncmp(buf, "none", count)) {
447                 serio_disconnect_port(serio);
448         } else if (!strncmp(buf, "reconnect", count)) {
449                 serio_reconnect_subtree(serio);
450         } else if (!strncmp(buf, "rescan", count)) {
451                 serio_disconnect_port(serio);
452                 serio_find_driver(serio);
453         } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
454                 serio_disconnect_port(serio);
455                 error = serio_bind_driver(serio, to_serio_driver(drv));
456                 put_driver(drv);
457         } else {
458                 error = -EINVAL;
459         }
460
461         mutex_unlock(&serio_mutex);
462
463         return error ? error : count;
464 }
465
466 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
467 {
468         struct serio *serio = to_serio_port(dev);
469         return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
470 }
471
472 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
473 {
474         struct serio *serio = to_serio_port(dev);
475         int retval;
476
477         retval = count;
478         if (!strncmp(buf, "manual", count)) {
479                 serio->manual_bind = true;
480         } else if (!strncmp(buf, "auto", count)) {
481                 serio->manual_bind = false;
482         } else {
483                 retval = -EINVAL;
484         }
485
486         return retval;
487 }
488
489 static struct device_attribute serio_device_attrs[] = {
490         __ATTR(description, S_IRUGO, serio_show_description, NULL),
491         __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
492         __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
493         __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
494         __ATTR_NULL
495 };
496
497
498 static void serio_release_port(struct device *dev)
499 {
500         struct serio *serio = to_serio_port(dev);
501
502         kfree(serio);
503         module_put(THIS_MODULE);
504 }
505
506 /*
507  * Prepare serio port for registration.
508  */
509 static void serio_init_port(struct serio *serio)
510 {
511         static atomic_t serio_no = ATOMIC_INIT(0);
512
513         __module_get(THIS_MODULE);
514
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%ld",
522                         (long)atomic_inc_return(&serio_no) - 1);
523         serio->dev.bus = &serio_bus;
524         serio->dev.release = serio_release_port;
525         serio->dev.groups = serio_device_attr_groups;
526         if (serio->parent) {
527                 serio->dev.parent = &serio->parent->dev;
528                 serio->depth = serio->parent->depth + 1;
529         } else
530                 serio->depth = 0;
531         lockdep_set_subclass(&serio->lock, serio->depth);
532 }
533
534 /*
535  * Complete serio port registration.
536  * Driver core will attempt to find appropriate driver for the port.
537  */
538 static void serio_add_port(struct serio *serio)
539 {
540         struct serio *parent = serio->parent;
541         int error;
542
543         if (parent) {
544                 serio_pause_rx(parent);
545                 list_add_tail(&serio->child_node, &parent->children);
546                 serio_continue_rx(parent);
547         }
548
549         list_add_tail(&serio->node, &serio_list);
550
551         if (serio->start)
552                 serio->start(serio);
553
554         error = device_add(&serio->dev);
555         if (error)
556                 dev_err(&serio->dev,
557                         "device_add() failed for %s (%s), error: %d\n",
558                         serio->phys, serio->name, error);
559 }
560
561 /*
562  * serio_destroy_port() completes unregistration process and removes
563  * port from the system
564  */
565 static void serio_destroy_port(struct serio *serio)
566 {
567         struct serio *child;
568
569         while ((child = serio_get_pending_child(serio)) != NULL) {
570                 serio_remove_pending_events(child);
571                 put_device(&child->dev);
572         }
573
574         if (serio->stop)
575                 serio->stop(serio);
576
577         if (serio->parent) {
578                 serio_pause_rx(serio->parent);
579                 list_del_init(&serio->child_node);
580                 serio_continue_rx(serio->parent);
581                 serio->parent = NULL;
582         }
583
584         if (device_is_registered(&serio->dev))
585                 device_del(&serio->dev);
586
587         list_del_init(&serio->node);
588         serio_remove_pending_events(serio);
589         put_device(&serio->dev);
590 }
591
592 /*
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.
597  */
598 static int serio_reconnect_port(struct serio *serio)
599 {
600         int error = serio_reconnect_driver(serio);
601
602         if (error) {
603                 serio_disconnect_port(serio);
604                 serio_find_driver(serio);
605         }
606
607         return error;
608 }
609
610 /*
611  * Reconnect serio port and all its children (re-initialize attached
612  * devices).
613  */
614 static void serio_reconnect_subtree(struct serio *root)
615 {
616         struct serio *s = root;
617         int error;
618
619         do {
620                 error = serio_reconnect_port(s);
621                 if (!error) {
622                         /*
623                          * Reconnect was successful, move on to do the
624                          * first child.
625                          */
626                         if (!list_empty(&s->children)) {
627                                 s = list_first_entry(&s->children,
628                                                      struct serio, child_node);
629                                 continue;
630                         }
631                 }
632
633                 /*
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.
637                  */
638                 while (s != root) {
639                         struct serio *parent = s->parent;
640
641                         if (!list_is_last(&s->child_node, &parent->children)) {
642                                 s = list_entry(s->child_node.next,
643                                                struct serio, child_node);
644                                 break;
645                         }
646
647                         s = parent;
648                 }
649         } while (s != root);
650 }
651
652 /*
653  * serio_disconnect_port() unbinds a port from its driver. As a side effect
654  * all children ports are unbound and destroyed.
655  */
656 static void serio_disconnect_port(struct serio *serio)
657 {
658         struct serio *s = serio;
659
660         /*
661          * Children ports should be disconnected and destroyed
662          * first; we travel the tree in depth-first order.
663          */
664         while (!list_empty(&serio->children)) {
665
666                 /* Locate a leaf */
667                 while (!list_empty(&s->children))
668                         s = list_first_entry(&s->children,
669                                              struct serio, child_node);
670
671                 /*
672                  * Prune this leaf node unless it is the one we
673                  * started with.
674                  */
675                 if (s != serio) {
676                         struct serio *parent = s->parent;
677
678                         device_release_driver(&s->dev);
679                         serio_destroy_port(s);
680
681                         s = parent;
682                 }
683         }
684
685         /*
686          * OK, no children left, now disconnect this port.
687          */
688         device_release_driver(&serio->dev);
689 }
690
691 void serio_rescan(struct serio *serio)
692 {
693         serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
694 }
695 EXPORT_SYMBOL(serio_rescan);
696
697 void serio_reconnect(struct serio *serio)
698 {
699         serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
700 }
701 EXPORT_SYMBOL(serio_reconnect);
702
703 /*
704  * Submits register request to kseriod for subsequent execution.
705  * Note that port registration is always asynchronous.
706  */
707 void __serio_register_port(struct serio *serio, struct module *owner)
708 {
709         serio_init_port(serio);
710         serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
711 }
712 EXPORT_SYMBOL(__serio_register_port);
713
714 /*
715  * Synchronously unregisters serio port.
716  */
717 void serio_unregister_port(struct serio *serio)
718 {
719         mutex_lock(&serio_mutex);
720         serio_disconnect_port(serio);
721         serio_destroy_port(serio);
722         mutex_unlock(&serio_mutex);
723 }
724 EXPORT_SYMBOL(serio_unregister_port);
725
726 /*
727  * Safely unregisters children ports if they are present.
728  */
729 void serio_unregister_child_port(struct serio *serio)
730 {
731         struct serio *s, *next;
732
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);
737         }
738         mutex_unlock(&serio_mutex);
739 }
740 EXPORT_SYMBOL(serio_unregister_child_port);
741
742
743 /*
744  * Serio driver operations
745  */
746
747 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
748 {
749         struct serio_driver *driver = to_serio_driver(drv);
750         return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
751 }
752
753 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
754 {
755         struct serio_driver *serio_drv = to_serio_driver(drv);
756         return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
757 }
758
759 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
760 {
761         struct serio_driver *serio_drv = to_serio_driver(drv);
762         int retval;
763
764         retval = count;
765         if (!strncmp(buf, "manual", count)) {
766                 serio_drv->manual_bind = true;
767         } else if (!strncmp(buf, "auto", count)) {
768                 serio_drv->manual_bind = false;
769         } else {
770                 retval = -EINVAL;
771         }
772
773         return retval;
774 }
775
776
777 static struct driver_attribute serio_driver_attrs[] = {
778         __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
779         __ATTR(bind_mode, S_IWUSR | S_IRUGO,
780                 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
781         __ATTR_NULL
782 };
783
784 static int serio_driver_probe(struct device *dev)
785 {
786         struct serio *serio = to_serio_port(dev);
787         struct serio_driver *drv = to_serio_driver(dev->driver);
788
789         return serio_connect_driver(serio, drv);
790 }
791
792 static int serio_driver_remove(struct device *dev)
793 {
794         struct serio *serio = to_serio_port(dev);
795
796         serio_disconnect_driver(serio);
797         return 0;
798 }
799
800 static void serio_cleanup(struct serio *serio)
801 {
802         mutex_lock(&serio->drv_mutex);
803         if (serio->drv && serio->drv->cleanup)
804                 serio->drv->cleanup(serio);
805         mutex_unlock(&serio->drv_mutex);
806 }
807
808 static void serio_shutdown(struct device *dev)
809 {
810         struct serio *serio = to_serio_port(dev);
811
812         serio_cleanup(serio);
813 }
814
815 static void serio_attach_driver(struct serio_driver *drv)
816 {
817         int error;
818
819         error = driver_attach(&drv->driver);
820         if (error)
821                 pr_warning("driver_attach() failed for %s with error %d\n",
822                            drv->driver.name, error);
823 }
824
825 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
826 {
827         bool manual_bind = drv->manual_bind;
828         int error;
829
830         drv->driver.bus = &serio_bus;
831         drv->driver.owner = owner;
832         drv->driver.mod_name = mod_name;
833
834         /*
835          * Temporarily disable automatic binding because probing
836          * takes long time and we are better off doing it in kseriod
837          */
838         drv->manual_bind = true;
839
840         error = driver_register(&drv->driver);
841         if (error) {
842                 pr_err("driver_register() failed for %s, error: %d\n",
843                         drv->driver.name, error);
844                 return error;
845         }
846
847         /*
848          * Restore original bind mode and let kseriod bind the
849          * driver to free ports
850          */
851         if (!manual_bind) {
852                 drv->manual_bind = false;
853                 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
854                 if (error) {
855                         driver_unregister(&drv->driver);
856                         return error;
857                 }
858         }
859
860         return 0;
861 }
862 EXPORT_SYMBOL(__serio_register_driver);
863
864 void serio_unregister_driver(struct serio_driver *drv)
865 {
866         struct serio *serio;
867
868         mutex_lock(&serio_mutex);
869
870         drv->manual_bind = true;        /* so serio_find_driver ignores it */
871         serio_remove_pending_events(drv);
872
873 start_over:
874         list_for_each_entry(serio, &serio_list, node) {
875                 if (serio->drv == drv) {
876                         serio_disconnect_port(serio);
877                         serio_find_driver(serio);
878                         /* we could've deleted some ports, restart */
879                         goto start_over;
880                 }
881         }
882
883         driver_unregister(&drv->driver);
884         mutex_unlock(&serio_mutex);
885 }
886 EXPORT_SYMBOL(serio_unregister_driver);
887
888 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
889 {
890         serio_pause_rx(serio);
891         serio->drv = drv;
892         serio_continue_rx(serio);
893 }
894
895 static int serio_bus_match(struct device *dev, struct device_driver *drv)
896 {
897         struct serio *serio = to_serio_port(dev);
898         struct serio_driver *serio_drv = to_serio_driver(drv);
899
900         if (serio->manual_bind || serio_drv->manual_bind)
901                 return 0;
902
903         return serio_match_port(serio_drv->id_table, serio);
904 }
905
906 #ifdef CONFIG_HOTPLUG
907
908 #define SERIO_ADD_UEVENT_VAR(fmt, val...)                               \
909         do {                                                            \
910                 int err = add_uevent_var(env, fmt, val);                \
911                 if (err)                                                \
912                         return err;                                     \
913         } while (0)
914
915 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
916 {
917         struct serio *serio;
918
919         if (!dev)
920                 return -ENODEV;
921
922         serio = to_serio_port(dev);
923
924         SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
925         SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
926         SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
927         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);
930
931         return 0;
932 }
933 #undef SERIO_ADD_UEVENT_VAR
934
935 #else
936
937 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
938 {
939         return -ENODEV;
940 }
941
942 #endif /* CONFIG_HOTPLUG */
943
944 #ifdef CONFIG_PM
945 static int serio_suspend(struct device *dev)
946 {
947         struct serio *serio = to_serio_port(dev);
948
949         serio_cleanup(serio);
950
951         return 0;
952 }
953
954 static int serio_resume(struct device *dev)
955 {
956         struct serio *serio = to_serio_port(dev);
957
958         /*
959          * Driver reconnect can take a while, so better let kseriod
960          * deal with it.
961          */
962         serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
963
964         return 0;
965 }
966
967 static const struct dev_pm_ops serio_pm_ops = {
968         .suspend        = serio_suspend,
969         .resume         = serio_resume,
970         .poweroff       = serio_suspend,
971         .restore        = serio_resume,
972 };
973 #endif /* CONFIG_PM */
974
975 /* called from serio_driver->connect/disconnect methods under serio_mutex */
976 int serio_open(struct serio *serio, struct serio_driver *drv)
977 {
978         serio_set_drv(serio, drv);
979
980         if (serio->open && serio->open(serio)) {
981                 serio_set_drv(serio, NULL);
982                 return -1;
983         }
984         return 0;
985 }
986 EXPORT_SYMBOL(serio_open);
987
988 /* called from serio_driver->connect/disconnect methods under serio_mutex */
989 void serio_close(struct serio *serio)
990 {
991         if (serio->close)
992                 serio->close(serio);
993
994         serio_set_drv(serio, NULL);
995 }
996 EXPORT_SYMBOL(serio_close);
997
998 irqreturn_t serio_interrupt(struct serio *serio,
999                 unsigned char data, unsigned int dfl)
1000 {
1001         unsigned long flags;
1002         irqreturn_t ret = IRQ_NONE;
1003
1004         spin_lock_irqsave(&serio->lock, flags);
1005
1006         if (likely(serio->drv)) {
1007                 ret = serio->drv->interrupt(serio, data, dfl);
1008         } else if (!dfl && device_is_registered(&serio->dev)) {
1009                 serio_rescan(serio);
1010                 ret = IRQ_HANDLED;
1011         }
1012
1013         spin_unlock_irqrestore(&serio->lock, flags);
1014
1015         return ret;
1016 }
1017 EXPORT_SYMBOL(serio_interrupt);
1018
1019 static struct bus_type serio_bus = {
1020         .name           = "serio",
1021         .dev_attrs      = serio_device_attrs,
1022         .drv_attrs      = serio_driver_attrs,
1023         .match          = serio_bus_match,
1024         .uevent         = serio_uevent,
1025         .probe          = serio_driver_probe,
1026         .remove         = serio_driver_remove,
1027         .shutdown       = serio_shutdown,
1028 #ifdef CONFIG_PM
1029         .pm             = &serio_pm_ops,
1030 #endif
1031 };
1032
1033 static int __init serio_init(void)
1034 {
1035         int error;
1036
1037         error = bus_register(&serio_bus);
1038         if (error) {
1039                 pr_err("Failed to register serio bus, error: %d\n", error);
1040                 return error;
1041         }
1042
1043         serio_task = kthread_run(serio_thread, NULL, "kseriod");
1044         if (IS_ERR(serio_task)) {
1045                 bus_unregister(&serio_bus);
1046                 error = PTR_ERR(serio_task);
1047                 pr_err("Failed to start kseriod, error: %d\n", error);
1048                 return error;
1049         }
1050
1051         return 0;
1052 }
1053
1054 static void __exit serio_exit(void)
1055 {
1056         bus_unregister(&serio_bus);
1057         kthread_stop(serio_task);
1058 }
1059
1060 subsys_initcall(serio_init);
1061 module_exit(serio_exit);