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