This commit was generated by cvs2svn to compensate for changes in r517,
[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_rebind_driver(struct device *dev, const char *buf, size_t count)
250 {
251         struct serio *serio = to_serio_port(dev);
252         struct device_driver *drv;
253         int retval;
254
255         retval = down_interruptible(&serio_sem);
256         if (retval)
257                 return retval;
258
259         retval = count;
260         if (!strncmp(buf, "none", count)) {
261                 serio_disconnect_port(serio);
262         } else if (!strncmp(buf, "reconnect", count)) {
263                 serio_reconnect_port(serio);
264         } else if (!strncmp(buf, "rescan", count)) {
265                 serio_disconnect_port(serio);
266                 serio_connect_port(serio, NULL);
267         } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
268                 serio_disconnect_port(serio);
269                 serio_connect_port(serio, to_serio_driver(drv));
270                 put_driver(drv);
271         } else {
272                 retval = -EINVAL;
273         }
274
275         up(&serio_sem);
276
277         return retval;
278 }
279
280 static ssize_t serio_show_bind_mode(struct device *dev, char *buf)
281 {
282         struct serio *serio = to_serio_port(dev);
283         return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
284 }
285
286 static ssize_t serio_set_bind_mode(struct device *dev, const char *buf, size_t count)
287 {
288         struct serio *serio = to_serio_port(dev);
289         int retval;
290
291         retval = count;
292         if (!strncmp(buf, "manual", count)) {
293                 serio->manual_bind = 1;
294         } else if (!strncmp(buf, "auto", count)) {
295                 serio->manual_bind = 0;
296         } else {
297                 retval = -EINVAL;
298         }
299
300         return retval;
301 }
302
303 static struct device_attribute serio_device_attrs[] = {
304         __ATTR(description, S_IRUGO, serio_show_description, NULL),
305         __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
306         __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
307         __ATTR_NULL
308 };
309
310
311 static void serio_release_port(struct device *dev)
312 {
313         struct serio *serio = to_serio_port(dev);
314
315         kfree(serio);
316         module_put(THIS_MODULE);
317 }
318
319 static void serio_create_port(struct serio *serio)
320 {
321         try_module_get(THIS_MODULE);
322
323         spin_lock_init(&serio->lock);
324         list_add_tail(&serio->node, &serio_list);
325         snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id), "serio%d", serio_no++);
326         serio->dev.bus = &serio_bus;
327         serio->dev.release = serio_release_port;
328         if (serio->parent)
329                 serio->dev.parent = &serio->parent->dev;
330         device_register(&serio->dev);
331 }
332
333 /*
334  * serio_destroy_port() completes deregistration process and removes
335  * port from the system
336  */
337 static void serio_destroy_port(struct serio *serio)
338 {
339         struct serio_driver *drv = serio->drv;
340         unsigned long flags;
341
342         serio_remove_pending_events(serio);
343         list_del_init(&serio->node);
344
345         if (drv) {
346                 drv->disconnect(serio);
347                 down_write(&serio_bus.subsys.rwsem);
348                 device_release_driver(&serio->dev);
349                 up_write(&serio_bus.subsys.rwsem);
350                 put_driver(&drv->driver);
351         }
352
353         if (serio->parent) {
354                 spin_lock_irqsave(&serio->parent->lock, flags);
355                 serio->parent->child = NULL;
356                 spin_unlock_irqrestore(&serio->parent->lock, flags);
357         }
358
359         device_unregister(&serio->dev);
360 }
361
362 /*
363  * serio_connect_port() tries to bind the port and possible all its
364  * children to appropriate drivers. If driver passed in the function will not
365  * try otehr drivers when binding parent port.
366  */
367 static void serio_connect_port(struct serio *serio, struct serio_driver *drv)
368 {
369         WARN_ON(serio->drv);
370         WARN_ON(serio->child);
371
372         if (drv)
373                 serio_bind_driver(serio, drv);
374         else if (!serio->manual_bind)
375                 serio_find_driver(serio);
376
377         /* Ok, now bind children, if any */
378         while (serio->child) {
379                 serio = serio->child;
380
381                 WARN_ON(serio->drv);
382                 WARN_ON(serio->child);
383
384                 serio_create_port(serio);
385
386                 if (!serio->manual_bind) {
387                         /*
388                          * With children we just _prefer_ passed in driver,
389                          * but we will try other options in case preferred
390                          * is not the one
391                          */
392                         if (!drv || !serio_bind_driver(serio, drv))
393                                 serio_find_driver(serio);
394                 }
395         }
396 }
397
398 /*
399  *
400  */
401 static void serio_reconnect_port(struct serio *serio)
402 {
403         do {
404                 if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) {
405                         serio_disconnect_port(serio);
406                         serio_connect_port(serio, NULL);
407                         /* Ok, old children are now gone, we are done */
408                         break;
409                 }
410                 serio = serio->child;
411         } while (serio);
412 }
413
414 /*
415  * serio_disconnect_port() unbinds a port from its driver. As a side effect
416  * all child ports are unbound and destroyed.
417  */
418 static void serio_disconnect_port(struct serio *serio)
419 {
420         struct serio_driver *drv = serio->drv;
421         struct serio *s;
422
423         if (serio->child) {
424                 /*
425                  * Children ports should be disconnected and destroyed
426                  * first, staring with the leaf one, since we don't want
427                  * to do recursion
428                  */
429                 do {
430                         s = serio->child;
431                 } while (s->child);
432
433                 while (s != serio) {
434                         s = s->parent;
435                         serio_destroy_port(s->child);
436                 }
437         }
438
439         /*
440          * Ok, no children left, now disconnect this port
441          */
442         if (drv) {
443                 drv->disconnect(serio);
444                 down_write(&serio_bus.subsys.rwsem);
445                 device_release_driver(&serio->dev);
446                 up_write(&serio_bus.subsys.rwsem);
447                 put_driver(&drv->driver);
448         }
449 }
450
451 void serio_rescan(struct serio *serio)
452 {
453         serio_queue_event(serio, SERIO_RESCAN);
454 }
455
456 void serio_reconnect(struct serio *serio)
457 {
458         serio_queue_event(serio, SERIO_RECONNECT);
459 }
460
461 void serio_register_port(struct serio *serio)
462 {
463         down(&serio_sem);
464         serio_create_port(serio);
465         serio_connect_port(serio, NULL);
466         up(&serio_sem);
467 }
468
469 /*
470  * Submits register request to kseriod for subsequent execution.
471  * Can be used when it is not obvious whether the serio_sem is
472  * taken or not and when delayed execution is feasible.
473  */
474 void serio_register_port_delayed(struct serio *serio)
475 {
476         serio_queue_event(serio, SERIO_REGISTER_PORT);
477 }
478
479 void serio_unregister_port(struct serio *serio)
480 {
481         down(&serio_sem);
482         serio_disconnect_port(serio);
483         serio_destroy_port(serio);
484         up(&serio_sem);
485 }
486
487 /*
488  * Submits unregister request to kseriod for subsequent execution.
489  * Can be used when it is not obvious whether the serio_sem is
490  * taken or not and when delayed execution is feasible.
491  */
492 void serio_unregister_port_delayed(struct serio *serio)
493 {
494         serio_queue_event(serio, SERIO_UNREGISTER_PORT);
495 }
496
497
498 /*
499  * Serio driver operations
500  */
501
502 static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
503 {
504         struct serio_driver *driver = to_serio_driver(drv);
505         return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
506 }
507
508 static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
509 {
510         struct serio_driver *serio_drv = to_serio_driver(drv);
511         return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
512 }
513
514 static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
515 {
516         struct serio_driver *serio_drv = to_serio_driver(drv);
517         int retval;
518
519         retval = count;
520         if (!strncmp(buf, "manual", count)) {
521                 serio_drv->manual_bind = 1;
522         } else if (!strncmp(buf, "auto", count)) {
523                 serio_drv->manual_bind = 0;
524         } else {
525                 retval = -EINVAL;
526         }
527
528         return retval;
529 }
530
531
532 static struct driver_attribute serio_driver_attrs[] = {
533         __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
534         __ATTR(bind_mode, S_IWUSR | S_IRUGO,
535                 serio_driver_show_bind_mode, serio_driver_set_bind_mode),
536         __ATTR_NULL
537 };
538
539 void serio_register_driver(struct serio_driver *drv)
540 {
541         struct serio *serio;
542
543         down(&serio_sem);
544
545         list_add_tail(&drv->node, &serio_driver_list);
546
547         drv->driver.bus = &serio_bus;
548         driver_register(&drv->driver);
549
550         if (drv->manual_bind)
551                 goto out;
552
553 start_over:
554         list_for_each_entry(serio, &serio_list, node) {
555                 if (!serio->drv) {
556                         serio_connect_port(serio, drv);
557                         /*
558                          * if new child appeared then the list is changed,
559                          * we need to start over
560                          */
561                         if (serio->child)
562                                 goto start_over;
563                 }
564         }
565
566 out:
567         up(&serio_sem);
568 }
569
570 void serio_unregister_driver(struct serio_driver *drv)
571 {
572         struct serio *serio;
573
574         down(&serio_sem);
575
576         list_del_init(&drv->node);
577
578 start_over:
579         list_for_each_entry(serio, &serio_list, node) {
580                 if (serio->drv == drv) {
581                         serio_disconnect_port(serio);
582                         serio_connect_port(serio, NULL);
583                         /* we could've deleted some ports, restart */
584                         goto start_over;
585                 }
586         }
587
588         driver_unregister(&drv->driver);
589
590         up(&serio_sem);
591 }
592
593 /* called from serio_driver->connect/disconnect methods under serio_sem */
594 int serio_open(struct serio *serio, struct serio_driver *drv)
595 {
596         serio_pause_rx(serio);
597         serio->drv = drv;
598         serio_continue_rx(serio);
599
600         if (serio->open && serio->open(serio)) {
601                 serio_pause_rx(serio);
602                 serio->drv = NULL;
603                 serio_continue_rx(serio);
604                 return -1;
605         }
606         return 0;
607 }
608
609 /* called from serio_driver->connect/disconnect methods under serio_sem */
610 void serio_close(struct serio *serio)
611 {
612         if (serio->close)
613                 serio->close(serio);
614
615         serio_pause_rx(serio);
616         serio->drv = NULL;
617         serio_continue_rx(serio);
618 }
619
620 irqreturn_t serio_interrupt(struct serio *serio,
621                 unsigned char data, unsigned int dfl, struct pt_regs *regs)
622 {
623         unsigned long flags;
624         irqreturn_t ret = IRQ_NONE;
625
626         spin_lock_irqsave(&serio->lock, flags);
627
628         if (likely(serio->drv)) {
629                 ret = serio->drv->interrupt(serio, data, dfl, regs);
630         } else {
631                 if (!dfl) {
632                         if ((serio->type != SERIO_8042 &&
633                              serio->type != SERIO_8042_XL) || (data == 0xaa)) {
634                                 serio_rescan(serio);
635                                 ret = IRQ_HANDLED;
636                         }
637                 }
638         }
639
640         spin_unlock_irqrestore(&serio->lock, flags);
641
642         return ret;
643 }
644
645 static int __init serio_init(void)
646 {
647         if (!(serio_pid = kernel_thread(serio_thread, NULL, CLONE_KERNEL))) {
648                 printk(KERN_WARNING "serio: Failed to start kseriod\n");
649                 return -1;
650         }
651
652         serio_bus.dev_attrs = serio_device_attrs;
653         serio_bus.drv_attrs = serio_driver_attrs;
654         bus_register(&serio_bus);
655
656         return 0;
657 }
658
659 static void __exit serio_exit(void)
660 {
661         bus_unregister(&serio_bus);
662         kill_proc(serio_pid, SIGTERM, 1);
663         wait_for_completion(&serio_exited);
664 }
665
666 module_init(serio_init);
667 module_exit(serio_exit);