repo init
[linux-rt-nao.git] / drivers / input / gameport / gameport.c
blob9647cb0525586b8450df87ff18d679a03cb5f739
1 /*
2 * Generic gameport layer
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 * Copyright (c) 2005 Dmitry Torokhov
6 */
8 /*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
14 #include <linux/stddef.h>
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/gameport.h>
19 #include <linux/wait.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/kthread.h>
23 #include <linux/interrupt.h>
24 #include <linux/sched.h> /* HZ */
25 #include <linux/mutex.h>
26 #include <linux/freezer.h>
28 /*#include <asm/io.h>*/
30 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
31 MODULE_DESCRIPTION("Generic gameport layer");
32 MODULE_LICENSE("GPL");
34 EXPORT_SYMBOL(__gameport_register_port);
35 EXPORT_SYMBOL(gameport_unregister_port);
36 EXPORT_SYMBOL(__gameport_register_driver);
37 EXPORT_SYMBOL(gameport_unregister_driver);
38 EXPORT_SYMBOL(gameport_open);
39 EXPORT_SYMBOL(gameport_close);
40 EXPORT_SYMBOL(gameport_set_phys);
41 EXPORT_SYMBOL(gameport_start_polling);
42 EXPORT_SYMBOL(gameport_stop_polling);
45 * gameport_mutex protects entire gameport subsystem and is taken
46 * every time gameport port or driver registrered or unregistered.
48 static DEFINE_MUTEX(gameport_mutex);
50 static LIST_HEAD(gameport_list);
52 static struct bus_type gameport_bus;
54 static void gameport_add_port(struct gameport *gameport);
55 static void gameport_attach_driver(struct gameport_driver *drv);
56 static void gameport_reconnect_port(struct gameport *gameport);
57 static void gameport_disconnect_port(struct gameport *gameport);
59 #if defined(__i386__)
61 #include <asm/i8253.h>
63 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
64 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
66 static unsigned int get_time_pit(void)
68 unsigned long flags;
69 unsigned int count;
71 spin_lock_irqsave(&i8253_lock, flags);
72 outb_p(0x00, 0x43);
73 count = inb_p(0x40);
74 count |= inb_p(0x40) << 8;
75 spin_unlock_irqrestore(&i8253_lock, flags);
77 return count;
80 #endif
85 * gameport_measure_speed() measures the gameport i/o speed.
88 static int gameport_measure_speed(struct gameport *gameport)
90 #if defined(__i386__)
92 unsigned int i, t, t1, t2, t3, tx;
93 unsigned long flags;
95 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
96 return 0;
98 tx = 1 << 30;
100 for(i = 0; i < 50; i++) {
101 local_irq_save_nort(flags);
102 GET_TIME(t1);
103 for (t = 0; t < 50; t++) gameport_read(gameport);
104 GET_TIME(t2);
105 GET_TIME(t3);
106 local_irq_restore_nort(flags);
107 udelay(i * 10);
108 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
111 gameport_close(gameport);
112 return 59659 / (tx < 1 ? 1 : tx);
114 #elif defined (__x86_64__)
116 unsigned int i, t;
117 unsigned long tx, t1, t2, flags;
119 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
120 return 0;
122 tx = 1 << 30;
124 for(i = 0; i < 50; i++) {
125 local_irq_save_nort(flags);
126 rdtscl(t1);
127 for (t = 0; t < 50; t++) gameport_read(gameport);
128 rdtscl(t2);
129 local_irq_restore_nort(flags);
130 udelay(i * 10);
131 if (t2 - t1 < tx) tx = t2 - t1;
134 gameport_close(gameport);
135 return (cpu_data(raw_smp_processor_id()).loops_per_jiffy *
136 (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
138 #else
140 unsigned int j, t = 0;
142 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
143 return 0;
145 j = jiffies; while (j == jiffies);
146 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
148 gameport_close(gameport);
149 return t * HZ / 1000;
151 #endif
154 void gameport_start_polling(struct gameport *gameport)
156 spin_lock(&gameport->timer_lock);
158 if (!gameport->poll_cnt++) {
159 BUG_ON(!gameport->poll_handler);
160 BUG_ON(!gameport->poll_interval);
161 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
164 spin_unlock(&gameport->timer_lock);
167 void gameport_stop_polling(struct gameport *gameport)
169 spin_lock(&gameport->timer_lock);
171 if (!--gameport->poll_cnt)
172 del_timer(&gameport->poll_timer);
174 spin_unlock(&gameport->timer_lock);
177 static void gameport_run_poll_handler(unsigned long d)
179 struct gameport *gameport = (struct gameport *)d;
181 gameport->poll_handler(gameport);
182 if (gameport->poll_cnt)
183 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
187 * Basic gameport -> driver core mappings
190 static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
192 int error;
194 gameport->dev.driver = &drv->driver;
195 if (drv->connect(gameport, drv)) {
196 gameport->dev.driver = NULL;
197 return -ENODEV;
200 error = device_bind_driver(&gameport->dev);
201 if (error) {
202 printk(KERN_WARNING
203 "gameport: device_bind_driver() failed "
204 "for %s (%s) and %s, error: %d\n",
205 gameport->phys, gameport->name,
206 drv->description, error);
207 drv->disconnect(gameport);
208 gameport->dev.driver = NULL;
209 return error;
212 return 0;
215 static void gameport_find_driver(struct gameport *gameport)
217 int error;
219 error = device_attach(&gameport->dev);
220 if (error < 0)
221 printk(KERN_WARNING
222 "gameport: device_attach() failed for %s (%s), error: %d\n",
223 gameport->phys, gameport->name, error);
228 * Gameport event processing.
231 enum gameport_event_type {
232 GAMEPORT_REGISTER_PORT,
233 GAMEPORT_ATTACH_DRIVER,
236 struct gameport_event {
237 enum gameport_event_type type;
238 void *object;
239 struct module *owner;
240 struct list_head node;
243 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
244 static LIST_HEAD(gameport_event_list);
245 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
246 static struct task_struct *gameport_task;
248 static int gameport_queue_event(void *object, struct module *owner,
249 enum gameport_event_type event_type)
251 unsigned long flags;
252 struct gameport_event *event;
253 int retval = 0;
255 spin_lock_irqsave(&gameport_event_lock, flags);
258 * Scan event list for the other events for the same gameport port,
259 * starting with the most recent one. If event is the same we
260 * do not need add new one. If event is of different type we
261 * need to add this event and should not look further because
262 * we need to preseve sequence of distinct events.
264 list_for_each_entry_reverse(event, &gameport_event_list, node) {
265 if (event->object == object) {
266 if (event->type == event_type)
267 goto out;
268 break;
272 event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
273 if (!event) {
274 printk(KERN_ERR
275 "gameport: Not enough memory to queue event %d\n",
276 event_type);
277 retval = -ENOMEM;
278 goto out;
281 if (!try_module_get(owner)) {
282 printk(KERN_WARNING
283 "gameport: Can't get module reference, dropping event %d\n",
284 event_type);
285 kfree(event);
286 retval = -EINVAL;
287 goto out;
290 event->type = event_type;
291 event->object = object;
292 event->owner = owner;
294 list_add_tail(&event->node, &gameport_event_list);
295 wake_up(&gameport_wait);
297 out:
298 spin_unlock_irqrestore(&gameport_event_lock, flags);
299 return retval;
302 static void gameport_free_event(struct gameport_event *event)
304 module_put(event->owner);
305 kfree(event);
308 static void gameport_remove_duplicate_events(struct gameport_event *event)
310 struct list_head *node, *next;
311 struct gameport_event *e;
312 unsigned long flags;
314 spin_lock_irqsave(&gameport_event_lock, flags);
316 list_for_each_safe(node, next, &gameport_event_list) {
317 e = list_entry(node, struct gameport_event, node);
318 if (event->object == e->object) {
320 * If this event is of different type we should not
321 * look further - we only suppress duplicate events
322 * that were sent back-to-back.
324 if (event->type != e->type)
325 break;
327 list_del_init(node);
328 gameport_free_event(e);
332 spin_unlock_irqrestore(&gameport_event_lock, flags);
335 static struct gameport_event *gameport_get_event(void)
337 struct gameport_event *event;
338 struct list_head *node;
339 unsigned long flags;
341 spin_lock_irqsave(&gameport_event_lock, flags);
343 if (list_empty(&gameport_event_list)) {
344 spin_unlock_irqrestore(&gameport_event_lock, flags);
345 return NULL;
348 node = gameport_event_list.next;
349 event = list_entry(node, struct gameport_event, node);
350 list_del_init(node);
352 spin_unlock_irqrestore(&gameport_event_lock, flags);
354 return event;
357 static void gameport_handle_event(void)
359 struct gameport_event *event;
361 mutex_lock(&gameport_mutex);
364 * Note that we handle only one event here to give swsusp
365 * a chance to freeze kgameportd thread. Gameport events
366 * should be pretty rare so we are not concerned about
367 * taking performance hit.
369 if ((event = gameport_get_event())) {
371 switch (event->type) {
372 case GAMEPORT_REGISTER_PORT:
373 gameport_add_port(event->object);
374 break;
376 case GAMEPORT_ATTACH_DRIVER:
377 gameport_attach_driver(event->object);
378 break;
380 default:
381 break;
384 gameport_remove_duplicate_events(event);
385 gameport_free_event(event);
388 mutex_unlock(&gameport_mutex);
392 * Remove all events that have been submitted for a given object,
393 * be it a gameport port or a driver.
395 static void gameport_remove_pending_events(void *object)
397 struct list_head *node, *next;
398 struct gameport_event *event;
399 unsigned long flags;
401 spin_lock_irqsave(&gameport_event_lock, flags);
403 list_for_each_safe(node, next, &gameport_event_list) {
404 event = list_entry(node, struct gameport_event, node);
405 if (event->object == object) {
406 list_del_init(node);
407 gameport_free_event(event);
411 spin_unlock_irqrestore(&gameport_event_lock, flags);
415 * Destroy child gameport port (if any) that has not been fully registered yet.
417 * Note that we rely on the fact that port can have only one child and therefore
418 * only one child registration request can be pending. Additionally, children
419 * are registered by driver's connect() handler so there can't be a grandchild
420 * pending registration together with a child.
422 static struct gameport *gameport_get_pending_child(struct gameport *parent)
424 struct gameport_event *event;
425 struct gameport *gameport, *child = NULL;
426 unsigned long flags;
428 spin_lock_irqsave(&gameport_event_lock, flags);
430 list_for_each_entry(event, &gameport_event_list, node) {
431 if (event->type == GAMEPORT_REGISTER_PORT) {
432 gameport = event->object;
433 if (gameport->parent == parent) {
434 child = gameport;
435 break;
440 spin_unlock_irqrestore(&gameport_event_lock, flags);
441 return child;
444 static int gameport_thread(void *nothing)
446 set_freezable();
447 do {
448 gameport_handle_event();
449 wait_event_freezable(gameport_wait,
450 kthread_should_stop() || !list_empty(&gameport_event_list));
451 } while (!kthread_should_stop());
453 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
454 return 0;
459 * Gameport port operations
462 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
464 struct gameport *gameport = to_gameport_port(dev);
465 return sprintf(buf, "%s\n", gameport->name);
468 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
470 struct gameport *gameport = to_gameport_port(dev);
471 struct device_driver *drv;
472 int error;
474 error = mutex_lock_interruptible(&gameport_mutex);
475 if (error)
476 return error;
478 if (!strncmp(buf, "none", count)) {
479 gameport_disconnect_port(gameport);
480 } else if (!strncmp(buf, "reconnect", count)) {
481 gameport_reconnect_port(gameport);
482 } else if (!strncmp(buf, "rescan", count)) {
483 gameport_disconnect_port(gameport);
484 gameport_find_driver(gameport);
485 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
486 gameport_disconnect_port(gameport);
487 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
488 put_driver(drv);
489 } else {
490 error = -EINVAL;
493 mutex_unlock(&gameport_mutex);
495 return error ? error : count;
498 static struct device_attribute gameport_device_attrs[] = {
499 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
500 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
501 __ATTR_NULL
504 static void gameport_release_port(struct device *dev)
506 struct gameport *gameport = to_gameport_port(dev);
508 kfree(gameport);
509 module_put(THIS_MODULE);
512 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
514 va_list args;
516 va_start(args, fmt);
517 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
518 va_end(args);
522 * Prepare gameport port for registration.
524 static void gameport_init_port(struct gameport *gameport)
526 static atomic_t gameport_no = ATOMIC_INIT(0);
528 __module_get(THIS_MODULE);
530 mutex_init(&gameport->drv_mutex);
531 device_initialize(&gameport->dev);
532 dev_set_name(&gameport->dev, "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
533 gameport->dev.bus = &gameport_bus;
534 gameport->dev.release = gameport_release_port;
535 if (gameport->parent)
536 gameport->dev.parent = &gameport->parent->dev;
538 INIT_LIST_HEAD(&gameport->node);
539 spin_lock_init(&gameport->timer_lock);
540 init_timer(&gameport->poll_timer);
541 gameport->poll_timer.function = gameport_run_poll_handler;
542 gameport->poll_timer.data = (unsigned long)gameport;
546 * Complete gameport port registration.
547 * Driver core will attempt to find appropriate driver for the port.
549 static void gameport_add_port(struct gameport *gameport)
551 int error;
553 if (gameport->parent)
554 gameport->parent->child = gameport;
556 gameport->speed = gameport_measure_speed(gameport);
558 list_add_tail(&gameport->node, &gameport_list);
560 if (gameport->io)
561 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
562 gameport->name, gameport->phys, gameport->io, gameport->speed);
563 else
564 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
565 gameport->name, gameport->phys, gameport->speed);
567 error = device_add(&gameport->dev);
568 if (error)
569 printk(KERN_ERR
570 "gameport: device_add() failed for %s (%s), error: %d\n",
571 gameport->phys, gameport->name, error);
572 else
573 gameport->registered = 1;
577 * gameport_destroy_port() completes deregistration process and removes
578 * port from the system
580 static void gameport_destroy_port(struct gameport *gameport)
582 struct gameport *child;
584 child = gameport_get_pending_child(gameport);
585 if (child) {
586 gameport_remove_pending_events(child);
587 put_device(&child->dev);
590 if (gameport->parent) {
591 gameport->parent->child = NULL;
592 gameport->parent = NULL;
595 if (gameport->registered) {
596 device_del(&gameport->dev);
597 gameport->registered = 0;
600 list_del_init(&gameport->node);
602 gameport_remove_pending_events(gameport);
603 put_device(&gameport->dev);
607 * Reconnect gameport port and all its children (re-initialize attached devices)
609 static void gameport_reconnect_port(struct gameport *gameport)
611 do {
612 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
613 gameport_disconnect_port(gameport);
614 gameport_find_driver(gameport);
615 /* Ok, old children are now gone, we are done */
616 break;
618 gameport = gameport->child;
619 } while (gameport);
623 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
624 * all child ports are unbound and destroyed.
626 static void gameport_disconnect_port(struct gameport *gameport)
628 struct gameport *s, *parent;
630 if (gameport->child) {
632 * Children ports should be disconnected and destroyed
633 * first, staring with the leaf one, since we don't want
634 * to do recursion
636 for (s = gameport; s->child; s = s->child)
637 /* empty */;
639 do {
640 parent = s->parent;
642 device_release_driver(&s->dev);
643 gameport_destroy_port(s);
644 } while ((s = parent) != gameport);
648 * Ok, no children left, now disconnect this port
650 device_release_driver(&gameport->dev);
654 * Submits register request to kgameportd for subsequent execution.
655 * Note that port registration is always asynchronous.
657 void __gameport_register_port(struct gameport *gameport, struct module *owner)
659 gameport_init_port(gameport);
660 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
664 * Synchronously unregisters gameport port.
666 void gameport_unregister_port(struct gameport *gameport)
668 mutex_lock(&gameport_mutex);
669 gameport_disconnect_port(gameport);
670 gameport_destroy_port(gameport);
671 mutex_unlock(&gameport_mutex);
676 * Gameport driver operations
679 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
681 struct gameport_driver *driver = to_gameport_driver(drv);
682 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
685 static struct driver_attribute gameport_driver_attrs[] = {
686 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
687 __ATTR_NULL
690 static int gameport_driver_probe(struct device *dev)
692 struct gameport *gameport = to_gameport_port(dev);
693 struct gameport_driver *drv = to_gameport_driver(dev->driver);
695 drv->connect(gameport, drv);
696 return gameport->drv ? 0 : -ENODEV;
699 static int gameport_driver_remove(struct device *dev)
701 struct gameport *gameport = to_gameport_port(dev);
702 struct gameport_driver *drv = to_gameport_driver(dev->driver);
704 drv->disconnect(gameport);
705 return 0;
708 static void gameport_attach_driver(struct gameport_driver *drv)
710 int error;
712 error = driver_attach(&drv->driver);
713 if (error)
714 printk(KERN_ERR
715 "gameport: driver_attach() failed for %s, error: %d\n",
716 drv->driver.name, error);
719 int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
720 const char *mod_name)
722 int error;
724 drv->driver.bus = &gameport_bus;
725 drv->driver.owner = owner;
726 drv->driver.mod_name = mod_name;
729 * Temporarily disable automatic binding because probing
730 * takes long time and we are better off doing it in kgameportd
732 drv->ignore = 1;
734 error = driver_register(&drv->driver);
735 if (error) {
736 printk(KERN_ERR
737 "gameport: driver_register() failed for %s, error: %d\n",
738 drv->driver.name, error);
739 return error;
743 * Reset ignore flag and let kgameportd bind the driver to free ports
745 drv->ignore = 0;
746 error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
747 if (error) {
748 driver_unregister(&drv->driver);
749 return error;
752 return 0;
755 void gameport_unregister_driver(struct gameport_driver *drv)
757 struct gameport *gameport;
759 mutex_lock(&gameport_mutex);
761 drv->ignore = 1; /* so gameport_find_driver ignores it */
762 gameport_remove_pending_events(drv);
764 start_over:
765 list_for_each_entry(gameport, &gameport_list, node) {
766 if (gameport->drv == drv) {
767 gameport_disconnect_port(gameport);
768 gameport_find_driver(gameport);
769 /* we could've deleted some ports, restart */
770 goto start_over;
774 driver_unregister(&drv->driver);
776 mutex_unlock(&gameport_mutex);
779 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
781 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
783 return !gameport_drv->ignore;
786 static struct bus_type gameport_bus = {
787 .name = "gameport",
788 .dev_attrs = gameport_device_attrs,
789 .drv_attrs = gameport_driver_attrs,
790 .match = gameport_bus_match,
791 .probe = gameport_driver_probe,
792 .remove = gameport_driver_remove,
795 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
797 mutex_lock(&gameport->drv_mutex);
798 gameport->drv = drv;
799 mutex_unlock(&gameport->drv_mutex);
802 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
804 if (gameport->open) {
805 if (gameport->open(gameport, mode)) {
806 return -1;
808 } else {
809 if (mode != GAMEPORT_MODE_RAW)
810 return -1;
813 gameport_set_drv(gameport, drv);
814 return 0;
817 void gameport_close(struct gameport *gameport)
819 del_timer_sync(&gameport->poll_timer);
820 gameport->poll_handler = NULL;
821 gameport->poll_interval = 0;
822 gameport_set_drv(gameport, NULL);
823 if (gameport->close)
824 gameport->close(gameport);
827 static int __init gameport_init(void)
829 int error;
831 error = bus_register(&gameport_bus);
832 if (error) {
833 printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
834 return error;
837 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
838 if (IS_ERR(gameport_task)) {
839 bus_unregister(&gameport_bus);
840 error = PTR_ERR(gameport_task);
841 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
842 return error;
845 return 0;
848 static void __exit gameport_exit(void)
850 bus_unregister(&gameport_bus);
851 kthread_stop(gameport_task);
854 subsys_initcall(gameport_init);
855 module_exit(gameport_exit);