V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / input / gameport / gameport.c
blob1dc2ac9f3d1cce12d1516c07b567c4656b28ee54
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/sched.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/kthread.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_rescan);
41 EXPORT_SYMBOL(gameport_set_phys);
42 EXPORT_SYMBOL(gameport_start_polling);
43 EXPORT_SYMBOL(gameport_stop_polling);
46 * gameport_mutex protects entire gameport subsystem and is taken
47 * every time gameport port or driver registrered or unregistered.
49 static DEFINE_MUTEX(gameport_mutex);
51 static LIST_HEAD(gameport_list);
53 static struct bus_type gameport_bus;
55 static void gameport_add_driver(struct gameport_driver *drv);
56 static void gameport_add_port(struct gameport *gameport);
57 static void gameport_destroy_port(struct gameport *gameport);
58 static void gameport_reconnect_port(struct gameport *gameport);
59 static void gameport_disconnect_port(struct gameport *gameport);
61 #if defined(__i386__)
63 #include <asm/i8253.h>
65 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
66 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
68 static unsigned int get_time_pit(void)
70 unsigned long flags;
71 unsigned int count;
73 spin_lock_irqsave(&i8253_lock, flags);
74 outb_p(0x00, 0x43);
75 count = inb_p(0x40);
76 count |= inb_p(0x40) << 8;
77 spin_unlock_irqrestore(&i8253_lock, flags);
79 return count;
82 #endif
87 * gameport_measure_speed() measures the gameport i/o speed.
90 static int gameport_measure_speed(struct gameport *gameport)
92 #if defined(__i386__)
94 unsigned int i, t, t1, t2, t3, tx;
95 unsigned long flags;
97 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
98 return 0;
100 tx = 1 << 30;
102 for(i = 0; i < 50; i++) {
103 local_irq_save(flags);
104 GET_TIME(t1);
105 for (t = 0; t < 50; t++) gameport_read(gameport);
106 GET_TIME(t2);
107 GET_TIME(t3);
108 local_irq_restore(flags);
109 udelay(i * 10);
110 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
113 gameport_close(gameport);
114 return 59659 / (tx < 1 ? 1 : tx);
116 #elif defined (__x86_64__)
118 unsigned int i, t;
119 unsigned long tx, t1, t2, flags;
121 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
122 return 0;
124 tx = 1 << 30;
126 for(i = 0; i < 50; i++) {
127 local_irq_save(flags);
128 rdtscl(t1);
129 for (t = 0; t < 50; t++) gameport_read(gameport);
130 rdtscl(t2);
131 local_irq_restore(flags);
132 udelay(i * 10);
133 if (t2 - t1 < tx) tx = t2 - t1;
136 gameport_close(gameport);
137 return (cpu_data(raw_smp_processor_id()).loops_per_jiffy *
138 (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
140 #else
142 unsigned int j, t = 0;
144 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
145 return 0;
147 j = jiffies; while (j == jiffies);
148 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
150 gameport_close(gameport);
151 return t * HZ / 1000;
153 #endif
156 void gameport_start_polling(struct gameport *gameport)
158 spin_lock(&gameport->timer_lock);
160 if (!gameport->poll_cnt++) {
161 BUG_ON(!gameport->poll_handler);
162 BUG_ON(!gameport->poll_interval);
163 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
166 spin_unlock(&gameport->timer_lock);
169 void gameport_stop_polling(struct gameport *gameport)
171 spin_lock(&gameport->timer_lock);
173 if (!--gameport->poll_cnt)
174 del_timer(&gameport->poll_timer);
176 spin_unlock(&gameport->timer_lock);
179 static void gameport_run_poll_handler(unsigned long d)
181 struct gameport *gameport = (struct gameport *)d;
183 gameport->poll_handler(gameport);
184 if (gameport->poll_cnt)
185 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
189 * Basic gameport -> driver core mappings
192 static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
194 int error;
196 gameport->dev.driver = &drv->driver;
197 if (drv->connect(gameport, drv)) {
198 gameport->dev.driver = NULL;
199 return -ENODEV;
202 error = device_bind_driver(&gameport->dev);
203 if (error) {
204 printk(KERN_WARNING
205 "gameport: device_bind_driver() failed "
206 "for %s (%s) and %s, error: %d\n",
207 gameport->phys, gameport->name,
208 drv->description, error);
209 drv->disconnect(gameport);
210 gameport->dev.driver = NULL;
211 return error;
214 return 0;
217 static void gameport_find_driver(struct gameport *gameport)
219 int error;
221 error = device_attach(&gameport->dev);
222 if (error < 0)
223 printk(KERN_WARNING
224 "gameport: device_attach() failed for %s (%s), error: %d\n",
225 gameport->phys, gameport->name, error);
230 * Gameport event processing.
233 enum gameport_event_type {
234 GAMEPORT_RESCAN,
235 GAMEPORT_RECONNECT,
236 GAMEPORT_REGISTER_PORT,
237 GAMEPORT_REGISTER_DRIVER,
240 struct gameport_event {
241 enum gameport_event_type type;
242 void *object;
243 struct module *owner;
244 struct list_head node;
247 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
248 static LIST_HEAD(gameport_event_list);
249 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
250 static struct task_struct *gameport_task;
252 static void gameport_queue_event(void *object, struct module *owner,
253 enum gameport_event_type event_type)
255 unsigned long flags;
256 struct gameport_event *event;
258 spin_lock_irqsave(&gameport_event_lock, flags);
261 * Scan event list for the other events for the same gameport port,
262 * starting with the most recent one. If event is the same we
263 * do not need add new one. If event is of different type we
264 * need to add this event and should not look further because
265 * we need to preseve sequence of distinct events.
267 list_for_each_entry_reverse(event, &gameport_event_list, node) {
268 if (event->object == object) {
269 if (event->type == event_type)
270 goto out;
271 break;
275 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
276 if (!try_module_get(owner)) {
277 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
278 kfree(event);
279 goto out;
282 event->type = event_type;
283 event->object = object;
284 event->owner = owner;
286 list_add_tail(&event->node, &gameport_event_list);
287 wake_up(&gameport_wait);
288 } else {
289 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
291 out:
292 spin_unlock_irqrestore(&gameport_event_lock, flags);
295 static void gameport_free_event(struct gameport_event *event)
297 module_put(event->owner);
298 kfree(event);
301 static void gameport_remove_duplicate_events(struct gameport_event *event)
303 struct list_head *node, *next;
304 struct gameport_event *e;
305 unsigned long flags;
307 spin_lock_irqsave(&gameport_event_lock, flags);
309 list_for_each_safe(node, next, &gameport_event_list) {
310 e = list_entry(node, struct gameport_event, node);
311 if (event->object == e->object) {
313 * If this event is of different type we should not
314 * look further - we only suppress duplicate events
315 * that were sent back-to-back.
317 if (event->type != e->type)
318 break;
320 list_del_init(node);
321 gameport_free_event(e);
325 spin_unlock_irqrestore(&gameport_event_lock, flags);
328 static struct gameport_event *gameport_get_event(void)
330 struct gameport_event *event;
331 struct list_head *node;
332 unsigned long flags;
334 spin_lock_irqsave(&gameport_event_lock, flags);
336 if (list_empty(&gameport_event_list)) {
337 spin_unlock_irqrestore(&gameport_event_lock, flags);
338 return NULL;
341 node = gameport_event_list.next;
342 event = list_entry(node, struct gameport_event, node);
343 list_del_init(node);
345 spin_unlock_irqrestore(&gameport_event_lock, flags);
347 return event;
350 static void gameport_handle_event(void)
352 struct gameport_event *event;
354 mutex_lock(&gameport_mutex);
357 * Note that we handle only one event here to give swsusp
358 * a chance to freeze kgameportd thread. Gameport events
359 * should be pretty rare so we are not concerned about
360 * taking performance hit.
362 if ((event = gameport_get_event())) {
364 switch (event->type) {
365 case GAMEPORT_REGISTER_PORT:
366 gameport_add_port(event->object);
367 break;
369 case GAMEPORT_RECONNECT:
370 gameport_reconnect_port(event->object);
371 break;
373 case GAMEPORT_RESCAN:
374 gameport_disconnect_port(event->object);
375 gameport_find_driver(event->object);
376 break;
378 case GAMEPORT_REGISTER_DRIVER:
379 gameport_add_driver(event->object);
380 break;
382 default:
383 break;
386 gameport_remove_duplicate_events(event);
387 gameport_free_event(event);
390 mutex_unlock(&gameport_mutex);
394 * Remove all events that have been submitted for a given gameport port.
396 static void gameport_remove_pending_events(struct gameport *gameport)
398 struct list_head *node, *next;
399 struct gameport_event *event;
400 unsigned long flags;
402 spin_lock_irqsave(&gameport_event_lock, flags);
404 list_for_each_safe(node, next, &gameport_event_list) {
405 event = list_entry(node, struct gameport_event, node);
406 if (event->object == gameport) {
407 list_del_init(node);
408 gameport_free_event(event);
412 spin_unlock_irqrestore(&gameport_event_lock, flags);
416 * Destroy child gameport port (if any) that has not been fully registered yet.
418 * Note that we rely on the fact that port can have only one child and therefore
419 * only one child registration request can be pending. Additionally, children
420 * are registered by driver's connect() handler so there can't be a grandchild
421 * pending registration together with a child.
423 static struct gameport *gameport_get_pending_child(struct gameport *parent)
425 struct gameport_event *event;
426 struct gameport *gameport, *child = NULL;
427 unsigned long flags;
429 spin_lock_irqsave(&gameport_event_lock, flags);
431 list_for_each_entry(event, &gameport_event_list, node) {
432 if (event->type == GAMEPORT_REGISTER_PORT) {
433 gameport = event->object;
434 if (gameport->parent == parent) {
435 child = gameport;
436 break;
441 spin_unlock_irqrestore(&gameport_event_lock, flags);
442 return child;
445 static int gameport_thread(void *nothing)
447 set_freezable();
448 do {
449 gameport_handle_event();
450 wait_event_freezable(gameport_wait,
451 kthread_should_stop() || !list_empty(&gameport_event_list));
452 } while (!kthread_should_stop());
454 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
455 return 0;
460 * Gameport port operations
463 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
465 struct gameport *gameport = to_gameport_port(dev);
466 return sprintf(buf, "%s\n", gameport->name);
469 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
471 struct gameport *gameport = to_gameport_port(dev);
472 struct device_driver *drv;
473 int error;
475 error = mutex_lock_interruptible(&gameport_mutex);
476 if (error)
477 return error;
479 if (!strncmp(buf, "none", count)) {
480 gameport_disconnect_port(gameport);
481 } else if (!strncmp(buf, "reconnect", count)) {
482 gameport_reconnect_port(gameport);
483 } else if (!strncmp(buf, "rescan", count)) {
484 gameport_disconnect_port(gameport);
485 gameport_find_driver(gameport);
486 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
487 gameport_disconnect_port(gameport);
488 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
489 put_driver(drv);
490 } else {
491 error = -EINVAL;
494 mutex_unlock(&gameport_mutex);
496 return error ? error : count;
499 static struct device_attribute gameport_device_attrs[] = {
500 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
501 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
502 __ATTR_NULL
505 static void gameport_release_port(struct device *dev)
507 struct gameport *gameport = to_gameport_port(dev);
509 kfree(gameport);
510 module_put(THIS_MODULE);
513 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
515 va_list args;
517 va_start(args, fmt);
518 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
519 va_end(args);
523 * Prepare gameport port for registration.
525 static void gameport_init_port(struct gameport *gameport)
527 static atomic_t gameport_no = ATOMIC_INIT(0);
529 __module_get(THIS_MODULE);
531 mutex_init(&gameport->drv_mutex);
532 device_initialize(&gameport->dev);
533 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
534 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
535 gameport->dev.bus = &gameport_bus;
536 gameport->dev.release = gameport_release_port;
537 if (gameport->parent)
538 gameport->dev.parent = &gameport->parent->dev;
540 INIT_LIST_HEAD(&gameport->node);
541 spin_lock_init(&gameport->timer_lock);
542 init_timer(&gameport->poll_timer);
543 gameport->poll_timer.function = gameport_run_poll_handler;
544 gameport->poll_timer.data = (unsigned long)gameport;
548 * Complete gameport port registration.
549 * Driver core will attempt to find appropriate driver for the port.
551 static void gameport_add_port(struct gameport *gameport)
553 int error;
555 if (gameport->parent)
556 gameport->parent->child = gameport;
558 gameport->speed = gameport_measure_speed(gameport);
560 list_add_tail(&gameport->node, &gameport_list);
562 if (gameport->io)
563 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
564 gameport->name, gameport->phys, gameport->io, gameport->speed);
565 else
566 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
567 gameport->name, gameport->phys, gameport->speed);
569 error = device_add(&gameport->dev);
570 if (error)
571 printk(KERN_ERR
572 "gameport: device_add() failed for %s (%s), error: %d\n",
573 gameport->phys, gameport->name, error);
574 else
575 gameport->registered = 1;
579 * gameport_destroy_port() completes deregistration process and removes
580 * port from the system
582 static void gameport_destroy_port(struct gameport *gameport)
584 struct gameport *child;
586 child = gameport_get_pending_child(gameport);
587 if (child) {
588 gameport_remove_pending_events(child);
589 put_device(&child->dev);
592 if (gameport->parent) {
593 gameport->parent->child = NULL;
594 gameport->parent = NULL;
597 if (gameport->registered) {
598 device_del(&gameport->dev);
599 gameport->registered = 0;
602 list_del_init(&gameport->node);
604 gameport_remove_pending_events(gameport);
605 put_device(&gameport->dev);
609 * Reconnect gameport port and all its children (re-initialize attached devices)
611 static void gameport_reconnect_port(struct gameport *gameport)
613 do {
614 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
615 gameport_disconnect_port(gameport);
616 gameport_find_driver(gameport);
617 /* Ok, old children are now gone, we are done */
618 break;
620 gameport = gameport->child;
621 } while (gameport);
625 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
626 * all child ports are unbound and destroyed.
628 static void gameport_disconnect_port(struct gameport *gameport)
630 struct gameport *s, *parent;
632 if (gameport->child) {
634 * Children ports should be disconnected and destroyed
635 * first, staring with the leaf one, since we don't want
636 * to do recursion
638 for (s = gameport; s->child; s = s->child)
639 /* empty */;
641 do {
642 parent = s->parent;
644 device_release_driver(&s->dev);
645 gameport_destroy_port(s);
646 } while ((s = parent) != gameport);
650 * Ok, no children left, now disconnect this port
652 device_release_driver(&gameport->dev);
655 void gameport_rescan(struct gameport *gameport)
657 gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
660 void gameport_reconnect(struct gameport *gameport)
662 gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
666 * Submits register request to kgameportd for subsequent execution.
667 * Note that port registration is always asynchronous.
669 void __gameport_register_port(struct gameport *gameport, struct module *owner)
671 gameport_init_port(gameport);
672 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
676 * Synchronously unregisters gameport port.
678 void gameport_unregister_port(struct gameport *gameport)
680 mutex_lock(&gameport_mutex);
681 gameport_disconnect_port(gameport);
682 gameport_destroy_port(gameport);
683 mutex_unlock(&gameport_mutex);
688 * Gameport driver operations
691 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
693 struct gameport_driver *driver = to_gameport_driver(drv);
694 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
697 static struct driver_attribute gameport_driver_attrs[] = {
698 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
699 __ATTR_NULL
702 static int gameport_driver_probe(struct device *dev)
704 struct gameport *gameport = to_gameport_port(dev);
705 struct gameport_driver *drv = to_gameport_driver(dev->driver);
707 drv->connect(gameport, drv);
708 return gameport->drv ? 0 : -ENODEV;
711 static int gameport_driver_remove(struct device *dev)
713 struct gameport *gameport = to_gameport_port(dev);
714 struct gameport_driver *drv = to_gameport_driver(dev->driver);
716 drv->disconnect(gameport);
717 return 0;
720 static void gameport_add_driver(struct gameport_driver *drv)
722 int error;
724 error = driver_register(&drv->driver);
725 if (error)
726 printk(KERN_ERR
727 "gameport: driver_register() failed for %s, error: %d\n",
728 drv->driver.name, error);
731 void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
733 drv->driver.bus = &gameport_bus;
734 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
737 void gameport_unregister_driver(struct gameport_driver *drv)
739 struct gameport *gameport;
741 mutex_lock(&gameport_mutex);
742 drv->ignore = 1; /* so gameport_find_driver ignores it */
744 start_over:
745 list_for_each_entry(gameport, &gameport_list, node) {
746 if (gameport->drv == drv) {
747 gameport_disconnect_port(gameport);
748 gameport_find_driver(gameport);
749 /* we could've deleted some ports, restart */
750 goto start_over;
754 driver_unregister(&drv->driver);
755 mutex_unlock(&gameport_mutex);
758 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
760 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
762 return !gameport_drv->ignore;
765 static struct bus_type gameport_bus = {
766 .name = "gameport",
767 .dev_attrs = gameport_device_attrs,
768 .drv_attrs = gameport_driver_attrs,
769 .match = gameport_bus_match,
770 .probe = gameport_driver_probe,
771 .remove = gameport_driver_remove,
774 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
776 mutex_lock(&gameport->drv_mutex);
777 gameport->drv = drv;
778 mutex_unlock(&gameport->drv_mutex);
781 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
783 if (gameport->open) {
784 if (gameport->open(gameport, mode)) {
785 return -1;
787 } else {
788 if (mode != GAMEPORT_MODE_RAW)
789 return -1;
792 gameport_set_drv(gameport, drv);
793 return 0;
796 void gameport_close(struct gameport *gameport)
798 del_timer_sync(&gameport->poll_timer);
799 gameport->poll_handler = NULL;
800 gameport->poll_interval = 0;
801 gameport_set_drv(gameport, NULL);
802 if (gameport->close)
803 gameport->close(gameport);
806 static int __init gameport_init(void)
808 int error;
810 error = bus_register(&gameport_bus);
811 if (error) {
812 printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
813 return error;
816 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
817 if (IS_ERR(gameport_task)) {
818 bus_unregister(&gameport_bus);
819 error = PTR_ERR(gameport_task);
820 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
821 return error;
824 return 0;
827 static void __exit gameport_exit(void)
829 bus_unregister(&gameport_bus);
830 kthread_stop(gameport_task);
833 subsys_initcall(gameport_init);
834 module_exit(gameport_exit);