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