mv643xx_eth: remove force_phy_addr field
[linux-2.6/btrfs-unstable.git] / drivers / input / gameport / gameport.c
blob078e4eed0894ba45a495184bb91b6518935663b9
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/sched.h> /* HZ */
24 #include <linux/mutex.h>
25 #include <linux/freezer.h>
27 /*#include <asm/io.h>*/
29 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30 MODULE_DESCRIPTION("Generic gameport layer");
31 MODULE_LICENSE("GPL");
33 EXPORT_SYMBOL(__gameport_register_port);
34 EXPORT_SYMBOL(gameport_unregister_port);
35 EXPORT_SYMBOL(__gameport_register_driver);
36 EXPORT_SYMBOL(gameport_unregister_driver);
37 EXPORT_SYMBOL(gameport_open);
38 EXPORT_SYMBOL(gameport_close);
39 EXPORT_SYMBOL(gameport_set_phys);
40 EXPORT_SYMBOL(gameport_start_polling);
41 EXPORT_SYMBOL(gameport_stop_polling);
44 * gameport_mutex protects entire gameport subsystem and is taken
45 * every time gameport port or driver registrered or unregistered.
47 static DEFINE_MUTEX(gameport_mutex);
49 static LIST_HEAD(gameport_list);
51 static struct bus_type gameport_bus;
53 static void gameport_add_driver(struct gameport_driver *drv);
54 static void gameport_add_port(struct gameport *gameport);
55 static void gameport_destroy_port(struct gameport *gameport);
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(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(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(flags);
126 rdtscl(t1);
127 for (t = 0; t < 50; t++) gameport_read(gameport);
128 rdtscl(t2);
129 local_irq_restore(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_REGISTER_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 void gameport_queue_event(void *object, struct module *owner,
249 enum gameport_event_type event_type)
251 unsigned long flags;
252 struct gameport_event *event;
254 spin_lock_irqsave(&gameport_event_lock, flags);
257 * Scan event list for the other events for the same gameport port,
258 * starting with the most recent one. If event is the same we
259 * do not need add new one. If event is of different type we
260 * need to add this event and should not look further because
261 * we need to preseve sequence of distinct events.
263 list_for_each_entry_reverse(event, &gameport_event_list, node) {
264 if (event->object == object) {
265 if (event->type == event_type)
266 goto out;
267 break;
271 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
272 if (!try_module_get(owner)) {
273 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
274 kfree(event);
275 goto out;
278 event->type = event_type;
279 event->object = object;
280 event->owner = owner;
282 list_add_tail(&event->node, &gameport_event_list);
283 wake_up(&gameport_wait);
284 } else {
285 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
287 out:
288 spin_unlock_irqrestore(&gameport_event_lock, flags);
291 static void gameport_free_event(struct gameport_event *event)
293 module_put(event->owner);
294 kfree(event);
297 static void gameport_remove_duplicate_events(struct gameport_event *event)
299 struct list_head *node, *next;
300 struct gameport_event *e;
301 unsigned long flags;
303 spin_lock_irqsave(&gameport_event_lock, flags);
305 list_for_each_safe(node, next, &gameport_event_list) {
306 e = list_entry(node, struct gameport_event, node);
307 if (event->object == e->object) {
309 * If this event is of different type we should not
310 * look further - we only suppress duplicate events
311 * that were sent back-to-back.
313 if (event->type != e->type)
314 break;
316 list_del_init(node);
317 gameport_free_event(e);
321 spin_unlock_irqrestore(&gameport_event_lock, flags);
324 static struct gameport_event *gameport_get_event(void)
326 struct gameport_event *event;
327 struct list_head *node;
328 unsigned long flags;
330 spin_lock_irqsave(&gameport_event_lock, flags);
332 if (list_empty(&gameport_event_list)) {
333 spin_unlock_irqrestore(&gameport_event_lock, flags);
334 return NULL;
337 node = gameport_event_list.next;
338 event = list_entry(node, struct gameport_event, node);
339 list_del_init(node);
341 spin_unlock_irqrestore(&gameport_event_lock, flags);
343 return event;
346 static void gameport_handle_event(void)
348 struct gameport_event *event;
350 mutex_lock(&gameport_mutex);
353 * Note that we handle only one event here to give swsusp
354 * a chance to freeze kgameportd thread. Gameport events
355 * should be pretty rare so we are not concerned about
356 * taking performance hit.
358 if ((event = gameport_get_event())) {
360 switch (event->type) {
361 case GAMEPORT_REGISTER_PORT:
362 gameport_add_port(event->object);
363 break;
365 case GAMEPORT_REGISTER_DRIVER:
366 gameport_add_driver(event->object);
367 break;
369 default:
370 break;
373 gameport_remove_duplicate_events(event);
374 gameport_free_event(event);
377 mutex_unlock(&gameport_mutex);
381 * Remove all events that have been submitted for a given gameport port.
383 static void gameport_remove_pending_events(struct gameport *gameport)
385 struct list_head *node, *next;
386 struct gameport_event *event;
387 unsigned long flags;
389 spin_lock_irqsave(&gameport_event_lock, flags);
391 list_for_each_safe(node, next, &gameport_event_list) {
392 event = list_entry(node, struct gameport_event, node);
393 if (event->object == gameport) {
394 list_del_init(node);
395 gameport_free_event(event);
399 spin_unlock_irqrestore(&gameport_event_lock, flags);
403 * Destroy child gameport port (if any) that has not been fully registered yet.
405 * Note that we rely on the fact that port can have only one child and therefore
406 * only one child registration request can be pending. Additionally, children
407 * are registered by driver's connect() handler so there can't be a grandchild
408 * pending registration together with a child.
410 static struct gameport *gameport_get_pending_child(struct gameport *parent)
412 struct gameport_event *event;
413 struct gameport *gameport, *child = NULL;
414 unsigned long flags;
416 spin_lock_irqsave(&gameport_event_lock, flags);
418 list_for_each_entry(event, &gameport_event_list, node) {
419 if (event->type == GAMEPORT_REGISTER_PORT) {
420 gameport = event->object;
421 if (gameport->parent == parent) {
422 child = gameport;
423 break;
428 spin_unlock_irqrestore(&gameport_event_lock, flags);
429 return child;
432 static int gameport_thread(void *nothing)
434 set_freezable();
435 do {
436 gameport_handle_event();
437 wait_event_freezable(gameport_wait,
438 kthread_should_stop() || !list_empty(&gameport_event_list));
439 } while (!kthread_should_stop());
441 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
442 return 0;
447 * Gameport port operations
450 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
452 struct gameport *gameport = to_gameport_port(dev);
453 return sprintf(buf, "%s\n", gameport->name);
456 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
458 struct gameport *gameport = to_gameport_port(dev);
459 struct device_driver *drv;
460 int error;
462 error = mutex_lock_interruptible(&gameport_mutex);
463 if (error)
464 return error;
466 if (!strncmp(buf, "none", count)) {
467 gameport_disconnect_port(gameport);
468 } else if (!strncmp(buf, "reconnect", count)) {
469 gameport_reconnect_port(gameport);
470 } else if (!strncmp(buf, "rescan", count)) {
471 gameport_disconnect_port(gameport);
472 gameport_find_driver(gameport);
473 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
474 gameport_disconnect_port(gameport);
475 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
476 put_driver(drv);
477 } else {
478 error = -EINVAL;
481 mutex_unlock(&gameport_mutex);
483 return error ? error : count;
486 static struct device_attribute gameport_device_attrs[] = {
487 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
488 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
489 __ATTR_NULL
492 static void gameport_release_port(struct device *dev)
494 struct gameport *gameport = to_gameport_port(dev);
496 kfree(gameport);
497 module_put(THIS_MODULE);
500 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
502 va_list args;
504 va_start(args, fmt);
505 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
506 va_end(args);
510 * Prepare gameport port for registration.
512 static void gameport_init_port(struct gameport *gameport)
514 static atomic_t gameport_no = ATOMIC_INIT(0);
516 __module_get(THIS_MODULE);
518 mutex_init(&gameport->drv_mutex);
519 device_initialize(&gameport->dev);
520 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
521 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
522 gameport->dev.bus = &gameport_bus;
523 gameport->dev.release = gameport_release_port;
524 if (gameport->parent)
525 gameport->dev.parent = &gameport->parent->dev;
527 INIT_LIST_HEAD(&gameport->node);
528 spin_lock_init(&gameport->timer_lock);
529 init_timer(&gameport->poll_timer);
530 gameport->poll_timer.function = gameport_run_poll_handler;
531 gameport->poll_timer.data = (unsigned long)gameport;
535 * Complete gameport port registration.
536 * Driver core will attempt to find appropriate driver for the port.
538 static void gameport_add_port(struct gameport *gameport)
540 int error;
542 if (gameport->parent)
543 gameport->parent->child = gameport;
545 gameport->speed = gameport_measure_speed(gameport);
547 list_add_tail(&gameport->node, &gameport_list);
549 if (gameport->io)
550 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
551 gameport->name, gameport->phys, gameport->io, gameport->speed);
552 else
553 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
554 gameport->name, gameport->phys, gameport->speed);
556 error = device_add(&gameport->dev);
557 if (error)
558 printk(KERN_ERR
559 "gameport: device_add() failed for %s (%s), error: %d\n",
560 gameport->phys, gameport->name, error);
561 else
562 gameport->registered = 1;
566 * gameport_destroy_port() completes deregistration process and removes
567 * port from the system
569 static void gameport_destroy_port(struct gameport *gameport)
571 struct gameport *child;
573 child = gameport_get_pending_child(gameport);
574 if (child) {
575 gameport_remove_pending_events(child);
576 put_device(&child->dev);
579 if (gameport->parent) {
580 gameport->parent->child = NULL;
581 gameport->parent = NULL;
584 if (gameport->registered) {
585 device_del(&gameport->dev);
586 gameport->registered = 0;
589 list_del_init(&gameport->node);
591 gameport_remove_pending_events(gameport);
592 put_device(&gameport->dev);
596 * Reconnect gameport port and all its children (re-initialize attached devices)
598 static void gameport_reconnect_port(struct gameport *gameport)
600 do {
601 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
602 gameport_disconnect_port(gameport);
603 gameport_find_driver(gameport);
604 /* Ok, old children are now gone, we are done */
605 break;
607 gameport = gameport->child;
608 } while (gameport);
612 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
613 * all child ports are unbound and destroyed.
615 static void gameport_disconnect_port(struct gameport *gameport)
617 struct gameport *s, *parent;
619 if (gameport->child) {
621 * Children ports should be disconnected and destroyed
622 * first, staring with the leaf one, since we don't want
623 * to do recursion
625 for (s = gameport; s->child; s = s->child)
626 /* empty */;
628 do {
629 parent = s->parent;
631 device_release_driver(&s->dev);
632 gameport_destroy_port(s);
633 } while ((s = parent) != gameport);
637 * Ok, no children left, now disconnect this port
639 device_release_driver(&gameport->dev);
643 * Submits register request to kgameportd for subsequent execution.
644 * Note that port registration is always asynchronous.
646 void __gameport_register_port(struct gameport *gameport, struct module *owner)
648 gameport_init_port(gameport);
649 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
653 * Synchronously unregisters gameport port.
655 void gameport_unregister_port(struct gameport *gameport)
657 mutex_lock(&gameport_mutex);
658 gameport_disconnect_port(gameport);
659 gameport_destroy_port(gameport);
660 mutex_unlock(&gameport_mutex);
665 * Gameport driver operations
668 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
670 struct gameport_driver *driver = to_gameport_driver(drv);
671 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
674 static struct driver_attribute gameport_driver_attrs[] = {
675 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
676 __ATTR_NULL
679 static int gameport_driver_probe(struct device *dev)
681 struct gameport *gameport = to_gameport_port(dev);
682 struct gameport_driver *drv = to_gameport_driver(dev->driver);
684 drv->connect(gameport, drv);
685 return gameport->drv ? 0 : -ENODEV;
688 static int gameport_driver_remove(struct device *dev)
690 struct gameport *gameport = to_gameport_port(dev);
691 struct gameport_driver *drv = to_gameport_driver(dev->driver);
693 drv->disconnect(gameport);
694 return 0;
697 static void gameport_add_driver(struct gameport_driver *drv)
699 int error;
701 error = driver_register(&drv->driver);
702 if (error)
703 printk(KERN_ERR
704 "gameport: driver_register() failed for %s, error: %d\n",
705 drv->driver.name, error);
708 void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
710 drv->driver.bus = &gameport_bus;
711 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
714 void gameport_unregister_driver(struct gameport_driver *drv)
716 struct gameport *gameport;
718 mutex_lock(&gameport_mutex);
719 drv->ignore = 1; /* so gameport_find_driver ignores it */
721 start_over:
722 list_for_each_entry(gameport, &gameport_list, node) {
723 if (gameport->drv == drv) {
724 gameport_disconnect_port(gameport);
725 gameport_find_driver(gameport);
726 /* we could've deleted some ports, restart */
727 goto start_over;
731 driver_unregister(&drv->driver);
732 mutex_unlock(&gameport_mutex);
735 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
737 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
739 return !gameport_drv->ignore;
742 static struct bus_type gameport_bus = {
743 .name = "gameport",
744 .dev_attrs = gameport_device_attrs,
745 .drv_attrs = gameport_driver_attrs,
746 .match = gameport_bus_match,
747 .probe = gameport_driver_probe,
748 .remove = gameport_driver_remove,
751 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
753 mutex_lock(&gameport->drv_mutex);
754 gameport->drv = drv;
755 mutex_unlock(&gameport->drv_mutex);
758 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
760 if (gameport->open) {
761 if (gameport->open(gameport, mode)) {
762 return -1;
764 } else {
765 if (mode != GAMEPORT_MODE_RAW)
766 return -1;
769 gameport_set_drv(gameport, drv);
770 return 0;
773 void gameport_close(struct gameport *gameport)
775 del_timer_sync(&gameport->poll_timer);
776 gameport->poll_handler = NULL;
777 gameport->poll_interval = 0;
778 gameport_set_drv(gameport, NULL);
779 if (gameport->close)
780 gameport->close(gameport);
783 static int __init gameport_init(void)
785 int error;
787 error = bus_register(&gameport_bus);
788 if (error) {
789 printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
790 return error;
793 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
794 if (IS_ERR(gameport_task)) {
795 bus_unregister(&gameport_bus);
796 error = PTR_ERR(gameport_task);
797 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
798 return error;
801 return 0;
804 static void __exit gameport_exit(void)
806 bus_unregister(&gameport_bus);
807 kthread_stop(gameport_task);
810 subsys_initcall(gameport_init);
811 module_exit(gameport_exit);