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