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