Merge commit 'v2.6.27-rc3' into x86/prototypes
[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_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_port(struct serio *serio);
66 static int serio_reconnect_port(struct serio *serio);
67 static void serio_disconnect_port(struct serio *serio);
68 static void serio_reconnect_chain(struct serio *serio);
69 static void serio_attach_driver(struct serio_driver *drv);
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 int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
120 {
121         int error;
122
123         if (serio_match_port(drv->id_table, serio)) {
124
125                 serio->dev.driver = &drv->driver;
126                 if (serio_connect_driver(serio, drv)) {
127                         serio->dev.driver = NULL;
128                         return -ENODEV;
129                 }
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                         return error;
141                 }
142         }
143         return 0;
144 }
145
146 static void serio_find_driver(struct serio *serio)
147 {
148         int error;
149
150         error = device_attach(&serio->dev);
151         if (error < 0)
152                 printk(KERN_WARNING
153                         "serio: device_attach() failed for %s (%s), error: %d\n",
154                         serio->phys, serio->name, error);
155 }
156
157
158 /*
159  * Serio event processing.
160  */
161
162 enum serio_event_type {
163         SERIO_RESCAN_PORT,
164         SERIO_RECONNECT_PORT,
165         SERIO_RECONNECT_CHAIN,
166         SERIO_REGISTER_PORT,
167         SERIO_ATTACH_DRIVER,
168 };
169
170 struct serio_event {
171         enum serio_event_type type;
172         void *object;
173         struct module *owner;
174         struct list_head node;
175 };
176
177 static DEFINE_SPINLOCK(serio_event_lock);       /* protects serio_event_list */
178 static LIST_HEAD(serio_event_list);
179 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
180 static struct task_struct *serio_task;
181
182 static int serio_queue_event(void *object, struct module *owner,
183                              enum serio_event_type event_type)
184 {
185         unsigned long flags;
186         struct serio_event *event;
187         int retval = 0;
188
189         spin_lock_irqsave(&serio_event_lock, flags);
190
191         /*
192          * Scan event list for the other events for the same serio port,
193          * starting with the most recent one. If event is the same we
194          * do not need add new one. If event is of different type we
195          * need to add this event and should not look further because
196          * we need to preseve sequence of distinct events.
197          */
198         list_for_each_entry_reverse(event, &serio_event_list, node) {
199                 if (event->object == object) {
200                         if (event->type == event_type)
201                                 goto out;
202                         break;
203                 }
204         }
205
206         event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC);
207         if (!event) {
208                 printk(KERN_ERR
209                         "serio: Not enough memory to queue event %d\n",
210                         event_type);
211                 retval = -ENOMEM;
212                 goto out;
213         }
214
215         if (!try_module_get(owner)) {
216                 printk(KERN_WARNING
217                         "serio: Can't get module reference, dropping event %d\n",
218                         event_type);
219                 kfree(event);
220                 retval = -EINVAL;
221                 goto out;
222         }
223
224         event->type = event_type;
225         event->object = object;
226         event->owner = owner;
227
228         list_add_tail(&event->node, &serio_event_list);
229         wake_up(&serio_wait);
230
231 out:
232         spin_unlock_irqrestore(&serio_event_lock, flags);
233         return retval;
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_RECONNECT_PORT:
312                                 serio_reconnect_port(event->object);
313                                 break;
314
315                         case SERIO_RESCAN_PORT:
316                                 serio_disconnect_port(event->object);
317                                 serio_find_driver(event->object);
318                                 break;
319
320                         case SERIO_RECONNECT_CHAIN:
321                                 serio_reconnect_chain(event->object);
322                                 break;
323
324                         case SERIO_ATTACH_DRIVER:
325                                 serio_attach_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
341  * object, be it serio port or driver.
342  */
343 static void serio_remove_pending_events(void *object)
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 == object) {
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         set_freezable();
395         do {
396                 serio_handle_event();
397                 wait_event_freezable(serio_wait,
398                         kthread_should_stop() || !list_empty(&serio_event_list));
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 error;
471
472         error = mutex_lock_interruptible(&serio_mutex);
473         if (error)
474                 return error;
475
476         if (!strncmp(buf, "none", count)) {
477                 serio_disconnect_port(serio);
478         } else if (!strncmp(buf, "reconnect", count)) {
479                 serio_reconnect_chain(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                 error = serio_bind_driver(serio, to_serio_driver(drv));
486                 put_driver(drv);
487         } else {
488                 error = -EINVAL;
489         }
490
491         mutex_unlock(&serio_mutex);
492
493         return error ? error : count;
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 (re-initialize attached device).
630  * If reconnect fails (old device is no longer attached or
631  * there was no device to begin with) we do full rescan in
632  * hope of finding a driver for the port.
633  */
634 static int serio_reconnect_port(struct serio *serio)
635 {
636         int error = serio_reconnect_driver(serio);
637
638         if (error) {
639                 serio_disconnect_port(serio);
640                 serio_find_driver(serio);
641         }
642
643         return error;
644 }
645
646 /*
647  * Reconnect serio port and all its children (re-initialize attached devices)
648  */
649 static void serio_reconnect_chain(struct serio *serio)
650 {
651         do {
652                 if (serio_reconnect_port(serio)) {
653                         /* Ok, old children are now gone, we are done */
654                         break;
655                 }
656                 serio = serio->child;
657         } while (serio);
658 }
659
660 /*
661  * serio_disconnect_port() unbinds a port from its driver. As a side effect
662  * all child ports are unbound and destroyed.
663  */
664 static void serio_disconnect_port(struct serio *serio)
665 {
666         struct serio *s, *parent;
667
668         if (serio->child) {
669                 /*
670                  * Children ports should be disconnected and destroyed
671                  * first, staring with the leaf one, since we don't want
672                  * to do recursion
673                  */
674                 for (s = serio; s->child; s = s->child)
675                         /* empty */;
676
677                 do {
678                         parent = s->parent;
679
680                         device_release_driver(&s->dev);
681                         serio_destroy_port(s);
682                 } while ((s = parent) != serio);
683         }
684
685         /*
686          * Ok, no children left, now disconnect this port
687          */
688         device_release_driver(&serio->dev);
689 }
690
691 void serio_rescan(struct serio *serio)
692 {
693         serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
694 }
695
696 void serio_reconnect(struct serio *serio)
697 {
698         serio_queue_event(serio, NULL, SERIO_RECONNECT_CHAIN);
699 }
700
701 /*
702  * Submits register request to kseriod for subsequent execution.
703  * Note that port registration is always asynchronous.
704  */
705 void __serio_register_port(struct serio *serio, struct module *owner)
706 {
707         serio_init_port(serio);
708         serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
709 }
710
711 /*
712  * Synchronously unregisters serio port.
713  */
714 void serio_unregister_port(struct serio *serio)
715 {
716         mutex_lock(&serio_mutex);
717         serio_disconnect_port(serio);
718         serio_destroy_port(serio);
719         mutex_unlock(&serio_mutex);
720 }
721
722 /*
723  * Safely unregisters child port if one is present.
724  */
725 void serio_unregister_child_port(struct serio *serio)
726 {
727         mutex_lock(&serio_mutex);
728         if (serio->child) {
729                 serio_disconnect_port(serio->child);
730                 serio_destroy_port(serio->child);
731         }
732         mutex_unlock(&serio_mutex);
733 }
734
735
736 /*
737  * Serio driver operations
738  */
739
740 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
741 {
742         struct serio_driver *driver = to_serio_driver(drv);
743         return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
744 }
745
746 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
747 {
748         struct serio_driver *serio_drv = to_serio_driver(drv);
749         return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
750 }
751
752 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
753 {
754         struct serio_driver *serio_drv = to_serio_driver(drv);
755         int retval;
756
757         retval = count;
758         if (!strncmp(buf, "manual", count)) {
759                 serio_drv->manual_bind = 1;
760         } else if (!strncmp(buf, "auto", count)) {
761                 serio_drv->manual_bind = 0;
762         } else {
763                 retval = -EINVAL;
764         }
765
766         return retval;
767 }
768
769
770 static struct driver_attribute serio_driver_attrs[] = {
771         __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
772         __ATTR(bind_mode, S_IWUSR | S_IRUGO,
773                 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
774         __ATTR_NULL
775 };
776
777 static int serio_driver_probe(struct device *dev)
778 {
779         struct serio *serio = to_serio_port(dev);
780         struct serio_driver *drv = to_serio_driver(dev->driver);
781
782         return serio_connect_driver(serio, drv);
783 }
784
785 static int serio_driver_remove(struct device *dev)
786 {
787         struct serio *serio = to_serio_port(dev);
788
789         serio_disconnect_driver(serio);
790         return 0;
791 }
792
793 static void serio_cleanup(struct serio *serio)
794 {
795         mutex_lock(&serio->drv_mutex);
796         if (serio->drv && serio->drv->cleanup)
797                 serio->drv->cleanup(serio);
798         mutex_unlock(&serio->drv_mutex);
799 }
800
801 static void serio_shutdown(struct device *dev)
802 {
803         struct serio *serio = to_serio_port(dev);
804
805         serio_cleanup(serio);
806 }
807
808 static void serio_attach_driver(struct serio_driver *drv)
809 {
810         int error;
811
812         error = driver_attach(&drv->driver);
813         if (error)
814                 printk(KERN_WARNING
815                         "serio: driver_attach() failed for %s with error %d\n",
816                         drv->driver.name, error);
817 }
818
819 int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
820 {
821         int manual_bind = drv->manual_bind;
822         int error;
823
824         drv->driver.bus = &serio_bus;
825         drv->driver.owner = owner;
826         drv->driver.mod_name = mod_name;
827
828         /*
829          * Temporarily disable automatic binding because probing
830          * takes long time and we are better off doing it in kseriod
831          */
832         drv->manual_bind = 1;
833
834         error = driver_register(&drv->driver);
835         if (error) {
836                 printk(KERN_ERR
837                         "serio: driver_register() failed for %s, error: %d\n",
838                         drv->driver.name, error);
839                 return error;
840         }
841
842         /*
843          * Restore original bind mode and let kseriod bind the
844          * driver to free ports
845          */
846         if (!manual_bind) {
847                 drv->manual_bind = 0;
848                 error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
849                 if (error) {
850                         driver_unregister(&drv->driver);
851                         return error;
852                 }
853         }
854
855         return 0;
856 }
857
858 void serio_unregister_driver(struct serio_driver *drv)
859 {
860         struct serio *serio;
861
862         mutex_lock(&serio_mutex);
863
864         drv->manual_bind = 1;   /* so serio_find_driver ignores it */
865         serio_remove_pending_events(drv);
866
867 start_over:
868         list_for_each_entry(serio, &serio_list, node) {
869                 if (serio->drv == drv) {
870                         serio_disconnect_port(serio);
871                         serio_find_driver(serio);
872                         /* we could've deleted some ports, restart */
873                         goto start_over;
874                 }
875         }
876
877         driver_unregister(&drv->driver);
878         mutex_unlock(&serio_mutex);
879 }
880
881 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
882 {
883         serio_pause_rx(serio);
884         serio->drv = drv;
885         serio_continue_rx(serio);
886 }
887
888 static int serio_bus_match(struct device *dev, struct device_driver *drv)
889 {
890         struct serio *serio = to_serio_port(dev);
891         struct serio_driver *serio_drv = to_serio_driver(drv);
892
893         if (serio->manual_bind || serio_drv->manual_bind)
894                 return 0;
895
896         return serio_match_port(serio_drv->id_table, serio);
897 }
898
899 #ifdef CONFIG_HOTPLUG
900
901 #define SERIO_ADD_UEVENT_VAR(fmt, val...)                               \
902         do {                                                            \
903                 int err = add_uevent_var(env, fmt, val);                \
904                 if (err)                                                \
905                         return err;                                     \
906         } while (0)
907
908 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
909 {
910         struct serio *serio;
911
912         if (!dev)
913                 return -ENODEV;
914
915         serio = to_serio_port(dev);
916
917         SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
918         SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
919         SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
920         SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
921         SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
922                                 serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
923
924         return 0;
925 }
926 #undef SERIO_ADD_UEVENT_VAR
927
928 #else
929
930 static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
931 {
932         return -ENODEV;
933 }
934
935 #endif /* CONFIG_HOTPLUG */
936
937 #ifdef CONFIG_PM
938 static int serio_suspend(struct device *dev, pm_message_t state)
939 {
940         if (dev->power.power_state.event != state.event) {
941                 if (state.event == PM_EVENT_SUSPEND)
942                         serio_cleanup(to_serio_port(dev));
943
944                 dev->power.power_state = state;
945         }
946
947         return 0;
948 }
949
950 static int serio_resume(struct device *dev)
951 {
952         /*
953          * Driver reconnect can take a while, so better let kseriod
954          * deal with it.
955          */
956         if (dev->power.power_state.event != PM_EVENT_ON) {
957                 dev->power.power_state = PMSG_ON;
958                 serio_queue_event(to_serio_port(dev), NULL,
959                                   SERIO_RECONNECT_PORT);
960         }
961
962         return 0;
963 }
964 #endif /* CONFIG_PM */
965
966 /* called from serio_driver->connect/disconnect methods under serio_mutex */
967 int serio_open(struct serio *serio, struct serio_driver *drv)
968 {
969         serio_set_drv(serio, drv);
970
971         if (serio->open && serio->open(serio)) {
972                 serio_set_drv(serio, NULL);
973                 return -1;
974         }
975         return 0;
976 }
977
978 /* called from serio_driver->connect/disconnect methods under serio_mutex */
979 void serio_close(struct serio *serio)
980 {
981         if (serio->close)
982                 serio->close(serio);
983
984         serio_set_drv(serio, NULL);
985 }
986
987 irqreturn_t serio_interrupt(struct serio *serio,
988                 unsigned char data, unsigned int dfl)
989 {
990         unsigned long flags;
991         irqreturn_t ret = IRQ_NONE;
992
993         spin_lock_irqsave(&serio->lock, flags);
994
995         if (likely(serio->drv)) {
996                 ret = serio->drv->interrupt(serio, data, dfl);
997         } else if (!dfl && serio->registered) {
998                 serio_rescan(serio);
999                 ret = IRQ_HANDLED;
1000         }
1001
1002         spin_unlock_irqrestore(&serio->lock, flags);
1003
1004         return ret;
1005 }
1006
1007 static struct bus_type serio_bus = {
1008         .name           = "serio",
1009         .dev_attrs      = serio_device_attrs,
1010         .drv_attrs      = serio_driver_attrs,
1011         .match          = serio_bus_match,
1012         .uevent         = serio_uevent,
1013         .probe          = serio_driver_probe,
1014         .remove         = serio_driver_remove,
1015         .shutdown       = serio_shutdown,
1016 #ifdef CONFIG_PM
1017         .suspend        = serio_suspend,
1018         .resume         = serio_resume,
1019 #endif
1020 };
1021
1022 static int __init serio_init(void)
1023 {
1024         int error;
1025
1026         error = bus_register(&serio_bus);
1027         if (error) {
1028                 printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
1029                 return error;
1030         }
1031
1032         serio_task = kthread_run(serio_thread, NULL, "kseriod");
1033         if (IS_ERR(serio_task)) {
1034                 bus_unregister(&serio_bus);
1035                 error = PTR_ERR(serio_task);
1036                 printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
1037                 return error;
1038         }
1039
1040         return 0;
1041 }
1042
1043 static void __exit serio_exit(void)
1044 {
1045         bus_unregister(&serio_bus);
1046         kthread_stop(serio_task);
1047 }
1048
1049 subsys_initcall(serio_init);
1050 module_exit(serio_exit);