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