vserver 1.9.5.x5
[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/completion.h>
35 #include <linux/sched.h>
36 #include <linux/smp_lock.h>
37 #include <linux/slab.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_register_port_delayed);
46 EXPORT_SYMBOL(serio_unregister_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 static DECLARE_MUTEX(serio_sem);        /* protects serio_list and serio_diriver_list */
56 static LIST_HEAD(serio_list);
57 static LIST_HEAD(serio_driver_list);
58 static unsigned int serio_no;
59
60 struct bus_type serio_bus = {
61         .name = "serio",
62 };
63
64 static void serio_find_driver(struct serio *serio);
65 static void serio_create_port(struct serio *serio);
66 static void serio_destroy_port(struct serio *serio);
67 static void serio_connect_port(struct serio *serio, struct serio_driver *drv);
68 static void serio_reconnect_port(struct serio *serio);
69 static void serio_disconnect_port(struct serio *serio);
70
71 static int serio_bind_driver(struct serio *serio, struct serio_driver *drv)
72 {
73         get_driver(&drv->driver);
74
75         drv->connect(serio, drv);
76         if (serio->drv) {
77                 down_write(&serio_bus.subsys.rwsem);
78                 serio->dev.driver = &drv->driver;
79                 device_bind_driver(&serio->dev);
80                 up_write(&serio_bus.subsys.rwsem);
81                 return 1;
82         }
83
84         put_driver(&drv->driver);
85         return 0;
86 }
87
88 /* serio_find_driver() must be called with serio_sem down.  */
89 static void serio_find_driver(struct serio *serio)
90 {
91         struct serio_driver *drv;
92
93         list_for_each_entry(drv, &serio_driver_list, node)
94                 if (!drv->manual_bind)
95                         if (serio_bind_driver(serio, drv))
96                                 break;
97 }
98
99 /*
100  * Serio event processing.
101  */
102
103 struct serio_event {
104         int type;
105         struct serio *serio;
106         struct list_head node;
107 };
108
109 enum serio_event_type {
110         SERIO_RESCAN,
111         SERIO_RECONNECT,
112         SERIO_REGISTER_PORT,
113         SERIO_UNREGISTER_PORT,
114 };
115
116 static DEFINE_SPINLOCK(serio_event_lock);       /* protects serio_event_list */
117 static LIST_HEAD(serio_event_list);
118 static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
119 static DECLARE_COMPLETION(serio_exited);
120 static int serio_pid;
121
122 static void serio_queue_event(struct serio *serio, int event_type)
123 {
124         unsigned long flags;
125         struct serio_event *event;
126
127         spin_lock_irqsave(&serio_event_lock, flags);
128
129         if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
130                 event->type = event_type;
131                 event->serio = serio;
132
133                 list_add_tail(&event->node, &serio_event_list);
134                 wake_up(&serio_wait);
135         }
136
137         spin_unlock_irqrestore(&serio_event_lock, flags);
138 }
139
140 static struct serio_event *serio_get_event(void)
141 {
142         struct serio_event *event;
143         struct list_head *node;
144         unsigned long flags;
145
146         spin_lock_irqsave(&serio_event_lock, flags);
147
148         if (list_empty(&serio_event_list)) {
149                 spin_unlock_irqrestore(&serio_event_lock, flags);
150                 return NULL;
151         }
152
153         node = serio_event_list.next;
154         event = container_of(node, struct serio_event, node);
155         list_del_init(node);
156
157         spin_unlock_irqrestore(&serio_event_lock, flags);
158
159         return event;
160 }
161
162 static void serio_handle_events(void)
163 {
164         struct serio_event *event;
165
166         while ((event = serio_get_event())) {
167
168                 down(&serio_sem);
169
170                 switch (event->type) {
171                         case SERIO_REGISTER_PORT :
172                                 serio_create_port(event->serio);
173                                 serio_connect_port(event->serio, NULL);
174                                 break;
175
176                         case SERIO_UNREGISTER_PORT :
177                                 serio_disconnect_port(event->serio);
178                                 serio_destroy_port(event->serio);
179                                 break;
180
181                         case SERIO_RECONNECT :
182                                 serio_reconnect_port(event->serio);
183                                 break;
184
185                         case SERIO_RESCAN :
186                                 serio_disconnect_port(event->serio);
187                                 serio_connect_port(event->serio, NULL);
188                                 break;
189                         default:
190                                 break;
191                 }
192
193                 up(&serio_sem);
194                 kfree(event);
195         }
196 }
197
198 static void serio_remove_pending_events(struct serio *serio)
199 {
200         struct list_head *node, *next;
201         struct serio_event *event;
202         unsigned long flags;
203
204         spin_lock_irqsave(&serio_event_lock, flags);
205
206         list_for_each_safe(node, next, &serio_event_list) {
207                 event = container_of(node, struct serio_event, node);
208                 if (event->serio == serio) {
209                         list_del_init(node);
210                         kfree(event);
211                 }
212         }
213
214         spin_unlock_irqrestore(&serio_event_lock, flags);
215 }
216
217
218 static int serio_thread(void *nothing)
219 {
220         lock_kernel();
221         daemonize("kseriod");
222         allow_signal(SIGTERM);
223
224         do {
225                 serio_handle_events();
226                 wait_event_interruptible(serio_wait, !list_empty(&serio_event_list));
227                 try_to_freeze(PF_FREEZE);
228         } while (!signal_pending(current));
229
230         printk(KERN_DEBUG "serio: kseriod exiting\n");
231
232         unlock_kernel();
233         complete_and_exit(&serio_exited, 0);
234 }
235
236
237 /*
238  * Serio port operations
239  */
240
241 static ssize_t serio_show_description(struct device *dev, char *buf)
242 {
243         struct serio *serio = to_serio_port(dev);
244         return sprintf(buf, "%s\n", serio->name);
245 }
246
247 static ssize_t serio_rebind_driver(struct device *dev, const char *buf, size_t count)
248 {
249         struct serio *serio = to_serio_port(dev);
250         struct device_driver *drv;
251         int retval;
252
253         retval = down_interruptible(&serio_sem);
254         if (retval)
255                 return retval;
256
257         retval = count;
258         if (!strncmp(buf, "none", count)) {
259                 serio_disconnect_port(serio);
260         } else if (!strncmp(buf, "reconnect", count)) {
261                 serio_reconnect_port(serio);
262         } else if (!strncmp(buf, "rescan", count)) {
263                 serio_disconnect_port(serio);
264                 serio_connect_port(serio, NULL);
265         } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
266                 serio_disconnect_port(serio);
267                 serio_connect_port(serio, to_serio_driver(drv));
268                 put_driver(drv);
269         } else {
270                 retval = -EINVAL;
271         }
272
273         up(&serio_sem);
274
275         return retval;
276 }
277
278 static ssize_t serio_show_bind_mode(struct device *dev, char *buf)
279 {
280         struct serio *serio = to_serio_port(dev);
281         return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
282 }
283
284 static ssize_t serio_set_bind_mode(struct device *dev, const char *buf, size_t count)
285 {
286         struct serio *serio = to_serio_port(dev);
287         int retval;
288
289         retval = count;
290         if (!strncmp(buf, "manual", count)) {
291                 serio->manual_bind = 1;
292         } else if (!strncmp(buf, "auto", count)) {
293                 serio->manual_bind = 0;
294         } else {
295                 retval = -EINVAL;
296         }
297
298         return retval;
299 }
300
301 static struct device_attribute serio_device_attrs[] = {
302         __ATTR(description, S_IRUGO, serio_show_description, NULL),
303         __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
304         __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
305         __ATTR_NULL
306 };
307
308
309 static void serio_release_port(struct device *dev)
310 {
311         struct serio *serio = to_serio_port(dev);
312
313         kfree(serio);
314         module_put(THIS_MODULE);
315 }
316
317 static void serio_create_port(struct serio *serio)
318 {
319         try_module_get(THIS_MODULE);
320
321         spin_lock_init(&serio->lock);
322         init_MUTEX(&serio->drv_sem);
323         list_add_tail(&serio->node, &serio_list);
324         snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id), "serio%d", serio_no++);
325         serio->dev.bus = &serio_bus;
326         serio->dev.release = serio_release_port;
327         if (serio->parent)
328                 serio->dev.parent = &serio->parent->dev;
329         device_register(&serio->dev);
330 }
331
332 /*
333  * serio_destroy_port() completes deregistration process and removes
334  * port from the system
335  */
336 static void serio_destroy_port(struct serio *serio)
337 {
338         struct serio_driver *drv = serio->drv;
339         unsigned long flags;
340
341         serio_remove_pending_events(serio);
342         list_del_init(&serio->node);
343
344         if (drv) {
345                 drv->disconnect(serio);
346                 down_write(&serio_bus.subsys.rwsem);
347                 device_release_driver(&serio->dev);
348                 up_write(&serio_bus.subsys.rwsem);
349                 put_driver(&drv->driver);
350         }
351
352         if (serio->parent) {
353                 spin_lock_irqsave(&serio->parent->lock, flags);
354                 serio->parent->child = NULL;
355                 spin_unlock_irqrestore(&serio->parent->lock, flags);
356         }
357
358         device_unregister(&serio->dev);
359 }
360
361 /*
362  * serio_connect_port() tries to bind the port and possible all its
363  * children to appropriate drivers. If driver passed in the function will not
364  * try otehr drivers when binding parent port.
365  */
366 static void serio_connect_port(struct serio *serio, struct serio_driver *drv)
367 {
368         WARN_ON(serio->drv);
369         WARN_ON(serio->child);
370
371         if (drv)
372                 serio_bind_driver(serio, drv);
373         else if (!serio->manual_bind)
374                 serio_find_driver(serio);
375
376         /* Ok, now bind children, if any */
377         while (serio->child) {
378                 serio = serio->child;
379
380                 WARN_ON(serio->drv);
381                 WARN_ON(serio->child);
382
383                 serio_create_port(serio);
384
385                 if (!serio->manual_bind) {
386                         /*
387                          * With children we just _prefer_ passed in driver,
388                          * but we will try other options in case preferred
389                          * is not the one
390                          */
391                         if (!drv || !serio_bind_driver(serio, drv))
392                                 serio_find_driver(serio);
393                 }
394         }
395 }
396
397 /*
398  *
399  */
400 static void serio_reconnect_port(struct serio *serio)
401 {
402         do {
403                 if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) {
404                         serio_disconnect_port(serio);
405                         serio_connect_port(serio, NULL);
406                         /* Ok, old children are now gone, we are done */
407                         break;
408                 }
409                 serio = serio->child;
410         } while (serio);
411 }
412
413 /*
414  * serio_disconnect_port() unbinds a port from its driver. As a side effect
415  * all child ports are unbound and destroyed.
416  */
417 static void serio_disconnect_port(struct serio *serio)
418 {
419         struct serio_driver *drv = serio->drv;
420         struct serio *s;
421
422         if (serio->child) {
423                 /*
424                  * Children ports should be disconnected and destroyed
425                  * first, staring with the leaf one, since we don't want
426                  * to do recursion
427                  */
428                 do {
429                         s = serio->child;
430                 } while (s->child);
431
432                 while (s != serio) {
433                         s = s->parent;
434                         serio_destroy_port(s->child);
435                 }
436         }
437
438         /*
439          * Ok, no children left, now disconnect this port
440          */
441         if (drv) {
442                 drv->disconnect(serio);
443                 down_write(&serio_bus.subsys.rwsem);
444                 device_release_driver(&serio->dev);
445                 up_write(&serio_bus.subsys.rwsem);
446                 put_driver(&drv->driver);
447         }
448 }
449
450 void serio_rescan(struct serio *serio)
451 {
452         serio_queue_event(serio, SERIO_RESCAN);
453 }
454
455 void serio_reconnect(struct serio *serio)
456 {
457         serio_queue_event(serio, SERIO_RECONNECT);
458 }
459
460 void serio_register_port(struct serio *serio)
461 {
462         down(&serio_sem);
463         serio_create_port(serio);
464         serio_connect_port(serio, NULL);
465         up(&serio_sem);
466 }
467
468 /*
469  * Submits register request to kseriod for subsequent execution.
470  * Can be used when it is not obvious whether the serio_sem is
471  * taken or not and when delayed execution is feasible.
472  */
473 void serio_register_port_delayed(struct serio *serio)
474 {
475         serio_queue_event(serio, SERIO_REGISTER_PORT);
476 }
477
478 void serio_unregister_port(struct serio *serio)
479 {
480         down(&serio_sem);
481         serio_disconnect_port(serio);
482         serio_destroy_port(serio);
483         up(&serio_sem);
484 }
485
486 /*
487  * Submits unregister request to kseriod for subsequent execution.
488  * Can be used when it is not obvious whether the serio_sem is
489  * taken or not and when delayed execution is feasible.
490  */
491 void serio_unregister_port_delayed(struct serio *serio)
492 {
493         serio_queue_event(serio, SERIO_UNREGISTER_PORT);
494 }
495
496
497 /*
498  * Serio driver operations
499  */
500
501 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
502 {
503         struct serio_driver *driver = to_serio_driver(drv);
504         return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
505 }
506
507 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
508 {
509         struct serio_driver *serio_drv = to_serio_driver(drv);
510         return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
511 }
512
513 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
514 {
515         struct serio_driver *serio_drv = to_serio_driver(drv);
516         int retval;
517
518         retval = count;
519         if (!strncmp(buf, "manual", count)) {
520                 serio_drv->manual_bind = 1;
521         } else if (!strncmp(buf, "auto", count)) {
522                 serio_drv->manual_bind = 0;
523         } else {
524                 retval = -EINVAL;
525         }
526
527         return retval;
528 }
529
530
531 static struct driver_attribute serio_driver_attrs[] = {
532         __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
533         __ATTR(bind_mode, S_IWUSR | S_IRUGO,
534                 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
535         __ATTR_NULL
536 };
537
538 void serio_register_driver(struct serio_driver *drv)
539 {
540         struct serio *serio;
541
542         down(&serio_sem);
543
544         list_add_tail(&drv->node, &serio_driver_list);
545
546         drv->driver.bus = &serio_bus;
547         driver_register(&drv->driver);
548
549         if (drv->manual_bind)
550                 goto out;
551
552 start_over:
553         list_for_each_entry(serio, &serio_list, node) {
554                 if (!serio->drv) {
555                         serio_connect_port(serio, drv);
556                         /*
557                          * if new child appeared then the list is changed,
558                          * we need to start over
559                          */
560                         if (serio->child)
561                                 goto start_over;
562                 }
563         }
564
565 out:
566         up(&serio_sem);
567 }
568
569 void serio_unregister_driver(struct serio_driver *drv)
570 {
571         struct serio *serio;
572
573         down(&serio_sem);
574
575         list_del_init(&drv->node);
576
577 start_over:
578         list_for_each_entry(serio, &serio_list, node) {
579                 if (serio->drv == drv) {
580                         serio_disconnect_port(serio);
581                         serio_connect_port(serio, NULL);
582                         /* we could've deleted some ports, restart */
583                         goto start_over;
584                 }
585         }
586
587         driver_unregister(&drv->driver);
588
589         up(&serio_sem);
590 }
591
592 static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
593 {
594         down(&serio->drv_sem);
595         serio_pause_rx(serio);
596         serio->drv = drv;
597         serio_continue_rx(serio);
598         up(&serio->drv_sem);
599 }
600
601 /* called from serio_driver->connect/disconnect methods under serio_sem */
602 int serio_open(struct serio *serio, struct serio_driver *drv)
603 {
604         serio_set_drv(serio, drv);
605
606         if (serio->open && serio->open(serio)) {
607                 serio_set_drv(serio, NULL);
608                 return -1;
609         }
610         return 0;
611 }
612
613 /* called from serio_driver->connect/disconnect methods under serio_sem */
614 void serio_close(struct serio *serio)
615 {
616         if (serio->close)
617                 serio->close(serio);
618
619         serio_set_drv(serio, NULL);
620 }
621
622 irqreturn_t serio_interrupt(struct serio *serio,
623                 unsigned char data, unsigned int dfl, struct pt_regs *regs)
624 {
625         unsigned long flags;
626         irqreturn_t ret = IRQ_NONE;
627
628         spin_lock_irqsave(&serio->lock, flags);
629
630         if (likely(serio->drv)) {
631                 ret = serio->drv->interrupt(serio, data, dfl, regs);
632         } else {
633                 if (!dfl) {
634                         if ((serio->type != SERIO_8042 &&
635                              serio->type != SERIO_8042_XL) || (data == 0xaa)) {
636                                 serio_rescan(serio);
637                                 ret = IRQ_HANDLED;
638                         }
639                 }
640         }
641
642         spin_unlock_irqrestore(&serio->lock, flags);
643
644         return ret;
645 }
646
647 static int __init serio_init(void)
648 {
649         if (!(serio_pid = kernel_thread(serio_thread, NULL, CLONE_KERNEL))) {
650                 printk(KERN_WARNING "serio: Failed to start kseriod\n");
651                 return -1;
652         }
653
654         serio_bus.dev_attrs = serio_device_attrs;
655         serio_bus.drv_attrs = serio_driver_attrs;
656         bus_register(&serio_bus);
657
658         return 0;
659 }
660
661 static void __exit serio_exit(void)
662 {
663         bus_unregister(&serio_bus);
664         kill_proc(serio_pid, SIGTERM, 1);
665         wait_for_completion(&serio_exited);
666 }
667
668 module_init(serio_init);
669 module_exit(serio_exit);