]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/input/serio/serio.c
Input: serio - do not include linux/freezer.h
[mv-sheeva.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_chain(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_CHAIN,
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_CHAIN:
295                         serio_reconnect_chain(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  * Destroy child serio port (if any) that has not been fully registered yet.
333  *
334  * Note that we rely on the fact that port can have only one child and therefore
335  * only one child registration request can be pending. Additionally, children
336  * are registered by driver's connect() handler so there can't be a grandchild
337  * pending registration together with a child.
338  */
339 static struct serio *serio_get_pending_child(struct serio *parent)
340 {
341         struct serio_event *event;
342         struct serio *serio, *child = NULL;
343         unsigned long flags;
344
345         spin_lock_irqsave(&serio_event_lock, flags);
346
347         list_for_each_entry(event, &serio_event_list, node) {
348                 if (event->type == SERIO_REGISTER_PORT) {
349                         serio = event->object;
350                         if (serio->parent == parent) {
351                                 child = serio;
352                                 break;
353                         }
354                 }
355         }
356
357         spin_unlock_irqrestore(&serio_event_lock, flags);
358         return child;
359 }
360
361 static int serio_thread(void *nothing)
362 {
363         do {
364                 serio_handle_event();
365                 wait_event_interruptible(serio_wait,
366                         kthread_should_stop() || !list_empty(&serio_event_list));
367         } while (!kthread_should_stop());
368
369         return 0;
370 }
371
372
373 /*
374  * Serio port operations
375  */
376
377 static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
378 {
379         struct serio *serio = to_serio_port(dev);
380         return sprintf(buf, "%s\n", serio->name);
381 }
382
383 static ssize_t serio_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
384 {
385         struct serio *serio = to_serio_port(dev);
386
387         return sprintf(buf, "serio:ty%02Xpr%02Xid%02Xex%02X\n",
388                         serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
389 }
390
391 static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
392 {
393         struct serio *serio = to_serio_port(dev);
394         return sprintf(buf, "%02x\n", serio->id.type);
395 }
396
397 static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
398 {
399         struct serio *serio = to_serio_port(dev);
400         return sprintf(buf, "%02x\n", serio->id.proto);
401 }
402
403 static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
404 {
405         struct serio *serio = to_serio_port(dev);
406         return sprintf(buf, "%02x\n", serio->id.id);
407 }
408
409 static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
410 {
411         struct serio *serio = to_serio_port(dev);
412         return sprintf(buf, "%02x\n", serio->id.extra);
413 }
414
415 static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
416 static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
417 static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
418 static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
419
420 static struct attribute *serio_device_id_attrs[] = {
421         &dev_attr_type.attr,
422         &dev_attr_proto.attr,
423         &dev_attr_id.attr,
424         &dev_attr_extra.attr,
425         NULL
426 };
427
428 static struct attribute_group serio_id_attr_group = {
429         .name   = "id",
430         .attrs  = serio_device_id_attrs,
431 };
432
433 static const struct attribute_group *serio_device_attr_groups[] = {
434         &serio_id_attr_group,
435         NULL
436 };
437
438 static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
439 {
440         struct serio *serio = to_serio_port(dev);
441         struct device_driver *drv;
442         int error;
443
444         error = mutex_lock_interruptible(&serio_mutex);
445         if (error)
446                 return error;
447
448         if (!strncmp(buf, "none", count)) {
449                 serio_disconnect_port(serio);
450         } else if (!strncmp(buf, "reconnect", count)) {
451                 serio_reconnect_chain(serio);
452         } else if (!strncmp(buf, "rescan", count)) {
453                 serio_disconnect_port(serio);
454                 serio_find_driver(serio);
455         } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
456                 serio_disconnect_port(serio);
457                 error = serio_bind_driver(serio, to_serio_driver(drv));
458                 put_driver(drv);
459         } else {
460                 error = -EINVAL;
461         }
462
463         mutex_unlock(&serio_mutex);
464
465         return error ? error : count;
466 }
467
468 static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
469 {
470         struct serio *serio = to_serio_port(dev);
471         return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
472 }
473
474 static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
475 {
476         struct serio *serio = to_serio_port(dev);
477         int retval;
478
479         retval = count;
480         if (!strncmp(buf, "manual", count)) {
481                 serio->manual_bind = true;
482         } else if (!strncmp(buf, "auto", count)) {
483                 serio->manual_bind = false;
484         } else {
485                 retval = -EINVAL;
486         }
487
488         return retval;
489 }
490
491 static struct device_attribute serio_device_attrs[] = {
492         __ATTR(description, S_IRUGO, serio_show_description, NULL),
493         __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
494         __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
495         __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
496         __ATTR_NULL
497 };
498
499
500 static void serio_release_port(struct device *dev)
501 {
502         struct serio *serio = to_serio_port(dev);
503
504         kfree(serio);
505         module_put(THIS_MODULE);
506 }
507
508 /*
509  * Prepare serio port for registration.
510  */
511 static void serio_init_port(struct serio *serio)
512 {
513         static atomic_t serio_no = ATOMIC_INIT(0);
514
515         __module_get(THIS_MODULE);
516
517         INIT_LIST_HEAD(&serio->node);
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         int error;
541
542         if (serio->parent) {
543                 serio_pause_rx(serio->parent);
544                 serio->parent->child = serio;
545                 serio_continue_rx(serio->parent);
546         }
547
548         list_add_tail(&serio->node, &serio_list);
549
550         if (serio->start)
551                 serio->start(serio);
552
553         error = device_add(&serio->dev);
554         if (error)
555                 dev_err(&serio->dev,
556                         "device_add() failed for %s (%s), error: %d\n",
557                         serio->phys, serio->name, error);
558 }
559
560 /*
561  * serio_destroy_port() completes deregistration process and removes
562  * port from the system
563  */
564 static void serio_destroy_port(struct serio *serio)
565 {
566         struct serio *child;
567
568         child = serio_get_pending_child(serio);
569         if (child) {
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                 serio->parent->child = NULL;
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 devices)
612  */
613 static void serio_reconnect_chain(struct serio *serio)
614 {
615         do {
616                 if (serio_reconnect_port(serio)) {
617                         /* Ok, old children are now gone, we are done */
618                         break;
619                 }
620                 serio = serio->child;
621         } while (serio);
622 }
623
624 /*
625  * serio_disconnect_port() unbinds a port from its driver. As a side effect
626  * all child ports are unbound and destroyed.
627  */
628 static void serio_disconnect_port(struct serio *serio)
629 {
630         struct serio *s, *parent;
631
632         if (serio->child) {
633                 /*
634                  * Children ports should be disconnected and destroyed
635                  * first, staring with the leaf one, since we don't want
636                  * to do recursion
637                  */
638                 for (s = serio; s->child; s = s->child)
639                         /* empty */;
640
641                 do {
642                         parent = s->parent;
643
644                         device_release_driver(&s->dev);
645                         serio_destroy_port(s);
646                 } while ((s = parent) != serio);
647         }
648
649         /*
650          * Ok, no children left, now disconnect this port
651          */
652         device_release_driver(&serio->dev);
653 }
654
655 void serio_rescan(struct serio *serio)
656 {
657         serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
658 }
659 EXPORT_SYMBOL(serio_rescan);
660
661 void serio_reconnect(struct serio *serio)
662 {
663         serio_queue_event(serio, NULL, SERIO_RECONNECT_CHAIN);
664 }
665 EXPORT_SYMBOL(serio_reconnect);
666
667 /*
668  * Submits register request to kseriod for subsequent execution.
669  * Note that port registration is always asynchronous.
670  */
671 void __serio_register_port(struct serio *serio, struct module *owner)
672 {
673         serio_init_port(serio);
674         serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
675 }
676 EXPORT_SYMBOL(__serio_register_port);
677
678 /*
679  * Synchronously unregisters serio port.
680  */
681 void serio_unregister_port(struct serio *serio)
682 {
683         mutex_lock(&serio_mutex);
684         serio_disconnect_port(serio);
685         serio_destroy_port(serio);
686         mutex_unlock(&serio_mutex);
687 }
688 EXPORT_SYMBOL(serio_unregister_port);
689
690 /*
691  * Safely unregisters child port if one is present.
692  */
693 void serio_unregister_child_port(struct serio *serio)
694 {
695         mutex_lock(&serio_mutex);
696         if (serio->child) {
697                 serio_disconnect_port(serio->child);
698                 serio_destroy_port(serio->child);
699         }
700         mutex_unlock(&serio_mutex);
701 }
702 EXPORT_SYMBOL(serio_unregister_child_port);
703
704
705 /*
706  * Serio driver operations
707  */
708
709 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
710 {
711         struct serio_driver *driver = to_serio_driver(drv);
712         return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
713 }
714
715 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
716 {
717         struct serio_driver *serio_drv = to_serio_driver(drv);
718         return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
719 }
720
721 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
722 {
723         struct serio_driver *serio_drv = to_serio_driver(drv);
724         int retval;
725
726         retval = count;
727         if (!strncmp(buf, "manual", count)) {
728                 serio_drv->manual_bind = true;
729         } else if (!strncmp(buf, "auto", count)) {
730                 serio_drv->manual_bind = false;
731         } else {
732                 retval = -EINVAL;
733         }
734
735         return retval;
736 }
737
738
739 static struct driver_attribute serio_driver_attrs[] = {
740         __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
741         __ATTR(bind_mode, S_IWUSR | S_IRUGO,
742                 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
743         __ATTR_NULL
744 };
745
746 static int serio_driver_probe(struct device *dev)
747 {
748         struct serio *serio = to_serio_port(dev);
749         struct serio_driver *drv = to_serio_driver(dev->driver);
750
751         return serio_connect_driver(serio, drv);
752 }
753
754 static int serio_driver_remove(struct device *dev)
755 {
756         struct serio *serio = to_serio_port(dev);
757
758         serio_disconnect_driver(serio);
759         return 0;
760 }
761
762 static void serio_cleanup(struct serio *serio)
763 {
764         mutex_lock(&serio->drv_mutex);
765         if (serio->drv && serio->drv->cleanup)
766                 serio->drv->cleanup(serio);
767         mutex_unlock(&serio->drv_mutex);
768 }
769
770 static void serio_shutdown(struct device *dev)
771 {
772         struct serio *serio = to_serio_port(dev);
773
774         serio_cleanup(serio);
775 }
776
777 static void serio_attach_driver(struct serio_driver *drv)
778 {
779         int error;
780
781         error = driver_attach(&drv->driver);
782         if (error)
783                 pr_warning("driver_attach() failed for %s with error %d\n",
784                            drv->driver.name, error);
785 }
786
787 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
788 {
789         bool manual_bind = drv->manual_bind;
790         int error;
791
792         drv->driver.bus = &serio_bus;
793         drv->driver.owner = owner;
794         drv->driver.mod_name = mod_name;
795
796         /*
797          * Temporarily disable automatic binding because probing
798          * takes long time and we are better off doing it in kseriod
799          */
800         drv->manual_bind = true;
801
802         error = driver_register(&drv->driver);
803         if (error) {
804                 pr_err("driver_register() failed for %s, error: %d\n",
805                         drv->driver.name, error);
806                 return error;
807         }
808
809         /*
810          * Restore original bind mode and let kseriod bind the
811          * driver to free ports
812          */
813         if (!manual_bind) {
814                 drv->manual_bind = false;
815                 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
816                 if (error) {
817                         driver_unregister(&drv->driver);
818                         return error;
819                 }
820         }
821
822         return 0;
823 }
824 EXPORT_SYMBOL(__serio_register_driver);
825
826 void serio_unregister_driver(struct serio_driver *drv)
827 {
828         struct serio *serio;
829
830         mutex_lock(&serio_mutex);
831
832         drv->manual_bind = true;        /* so serio_find_driver ignores it */
833         serio_remove_pending_events(drv);
834
835 start_over:
836         list_for_each_entry(serio, &serio_list, node) {
837                 if (serio->drv == drv) {
838                         serio_disconnect_port(serio);
839                         serio_find_driver(serio);
840                         /* we could've deleted some ports, restart */
841                         goto start_over;
842                 }
843         }
844
845         driver_unregister(&drv->driver);
846         mutex_unlock(&serio_mutex);
847 }
848 EXPORT_SYMBOL(serio_unregister_driver);
849
850 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
851 {
852         serio_pause_rx(serio);
853         serio->drv = drv;
854         serio_continue_rx(serio);
855 }
856
857 static int serio_bus_match(struct device *dev, struct device_driver *drv)
858 {
859         struct serio *serio = to_serio_port(dev);
860         struct serio_driver *serio_drv = to_serio_driver(drv);
861
862         if (serio->manual_bind || serio_drv->manual_bind)
863                 return 0;
864
865         return serio_match_port(serio_drv->id_table, serio);
866 }
867
868 #ifdef CONFIG_HOTPLUG
869
870 #define SERIO_ADD_UEVENT_VAR(fmt, val...)                               \
871         do {                                                            \
872                 int err = add_uevent_var(env, fmt, val);                \
873                 if (err)                                                \
874                         return err;                                     \
875         } while (0)
876
877 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
878 {
879         struct serio *serio;
880
881         if (!dev)
882                 return -ENODEV;
883
884         serio = to_serio_port(dev);
885
886         SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
887         SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
888         SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
889         SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
890         SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
891                                 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
892
893         return 0;
894 }
895 #undef SERIO_ADD_UEVENT_VAR
896
897 #else
898
899 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
900 {
901         return -ENODEV;
902 }
903
904 #endif /* CONFIG_HOTPLUG */
905
906 #ifdef CONFIG_PM
907 static int serio_suspend(struct device *dev)
908 {
909         struct serio *serio = to_serio_port(dev);
910
911         serio_cleanup(serio);
912
913         return 0;
914 }
915
916 static int serio_resume(struct device *dev)
917 {
918         struct serio *serio = to_serio_port(dev);
919
920         /*
921          * Driver reconnect can take a while, so better let kseriod
922          * deal with it.
923          */
924         serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
925
926         return 0;
927 }
928
929 static const struct dev_pm_ops serio_pm_ops = {
930         .suspend        = serio_suspend,
931         .resume         = serio_resume,
932         .poweroff       = serio_suspend,
933         .restore        = serio_resume,
934 };
935 #endif /* CONFIG_PM */
936
937 /* called from serio_driver->connect/disconnect methods under serio_mutex */
938 int serio_open(struct serio *serio, struct serio_driver *drv)
939 {
940         serio_set_drv(serio, drv);
941
942         if (serio->open && serio->open(serio)) {
943                 serio_set_drv(serio, NULL);
944                 return -1;
945         }
946         return 0;
947 }
948 EXPORT_SYMBOL(serio_open);
949
950 /* called from serio_driver->connect/disconnect methods under serio_mutex */
951 void serio_close(struct serio *serio)
952 {
953         if (serio->close)
954                 serio->close(serio);
955
956         serio_set_drv(serio, NULL);
957 }
958 EXPORT_SYMBOL(serio_close);
959
960 irqreturn_t serio_interrupt(struct serio *serio,
961                 unsigned char data, unsigned int dfl)
962 {
963         unsigned long flags;
964         irqreturn_t ret = IRQ_NONE;
965
966         spin_lock_irqsave(&serio->lock, flags);
967
968         if (likely(serio->drv)) {
969                 ret = serio->drv->interrupt(serio, data, dfl);
970         } else if (!dfl && device_is_registered(&serio->dev)) {
971                 serio_rescan(serio);
972                 ret = IRQ_HANDLED;
973         }
974
975         spin_unlock_irqrestore(&serio->lock, flags);
976
977         return ret;
978 }
979 EXPORT_SYMBOL(serio_interrupt);
980
981 static struct bus_type serio_bus = {
982         .name           = "serio",
983         .dev_attrs      = serio_device_attrs,
984         .drv_attrs      = serio_driver_attrs,
985         .match          = serio_bus_match,
986         .uevent         = serio_uevent,
987         .probe          = serio_driver_probe,
988         .remove         = serio_driver_remove,
989         .shutdown       = serio_shutdown,
990 #ifdef CONFIG_PM
991         .pm             = &serio_pm_ops,
992 #endif
993 };
994
995 static int __init serio_init(void)
996 {
997         int error;
998
999         error = bus_register(&serio_bus);
1000         if (error) {
1001                 pr_err("Failed to register serio bus, error: %d\n", error);
1002                 return error;
1003         }
1004
1005         serio_task = kthread_run(serio_thread, NULL, "kseriod");
1006         if (IS_ERR(serio_task)) {
1007                 bus_unregister(&serio_bus);
1008                 error = PTR_ERR(serio_task);
1009                 pr_err("Failed to start kseriod, error: %d\n", error);
1010                 return error;
1011         }
1012
1013         return 0;
1014 }
1015
1016 static void __exit serio_exit(void)
1017 {
1018         bus_unregister(&serio_bus);
1019         kthread_stop(serio_task);
1020 }
1021
1022 subsys_initcall(serio_init);
1023 module_exit(serio_exit);