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