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