MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / input / gameport / gameport.c
blobb2a68ab2afedeb6797e52564503b1df34eed1124
1 /*
2 * Generic gameport layer
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
7 /*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
13 #include <asm/io.h>
14 #include <linux/module.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/gameport.h>
18 #include <linux/slab.h>
19 #include <linux/stddef.h>
20 #include <linux/delay.h>
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION("Generic gameport layer");
24 MODULE_LICENSE("GPL");
26 EXPORT_SYMBOL(gameport_register_port);
27 EXPORT_SYMBOL(gameport_unregister_port);
28 EXPORT_SYMBOL(gameport_register_device);
29 EXPORT_SYMBOL(gameport_unregister_device);
30 EXPORT_SYMBOL(gameport_open);
31 EXPORT_SYMBOL(gameport_close);
32 EXPORT_SYMBOL(gameport_rescan);
33 EXPORT_SYMBOL(gameport_cooked_read);
35 static LIST_HEAD(gameport_list);
36 static LIST_HEAD(gameport_dev_list);
38 #ifdef __i386__
40 #define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
41 #define GET_TIME(x) do { x = get_time_pit(); } while (0)
43 static unsigned int get_time_pit(void)
45 extern spinlock_t i8253_lock;
46 unsigned long flags;
47 unsigned int count;
49 spin_lock_irqsave(&i8253_lock, flags);
50 outb_p(0x00, 0x43);
51 count = inb_p(0x40);
52 count |= inb_p(0x40) << 8;
53 spin_unlock_irqrestore(&i8253_lock, flags);
55 return count;
58 #endif
61 * gameport_measure_speed() measures the gameport i/o speed.
64 static int gameport_measure_speed(struct gameport *gameport)
66 #ifdef __i386__
68 unsigned int i, t, t1, t2, t3, tx;
69 unsigned long flags;
71 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
72 return 0;
74 tx = 1 << 30;
76 for(i = 0; i < 50; i++) {
77 local_irq_save(flags);
78 GET_TIME(t1);
79 for(t = 0; t < 50; t++) gameport_read(gameport);
80 GET_TIME(t2);
81 GET_TIME(t3);
82 local_irq_restore(flags);
83 udelay(i * 10);
84 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
87 gameport_close(gameport);
88 return 59659 / (tx < 1 ? 1 : tx);
90 #else
92 unsigned int j, t = 0;
94 j = jiffies; while (j == jiffies);
95 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
97 gameport_close(gameport);
98 return t * HZ / 1000;
100 #endif
103 static void gameport_find_dev(struct gameport *gameport)
105 struct gameport_dev *dev;
107 list_for_each_entry(dev, &gameport_dev_list, node) {
108 if (gameport->dev)
109 break;
110 if (dev->connect)
111 dev->connect(gameport, dev);
115 void gameport_rescan(struct gameport *gameport)
117 gameport_close(gameport);
118 gameport_find_dev(gameport);
121 void gameport_register_port(struct gameport *gameport)
123 list_add_tail(&gameport->node, &gameport_list);
124 gameport->speed = gameport_measure_speed(gameport);
125 gameport_find_dev(gameport);
128 void gameport_unregister_port(struct gameport *gameport)
130 list_del_init(&gameport->node);
131 if (gameport->dev && gameport->dev->disconnect)
132 gameport->dev->disconnect(gameport);
135 void gameport_register_device(struct gameport_dev *dev)
137 struct gameport *gameport;
139 list_add_tail(&dev->node, &gameport_dev_list);
140 list_for_each_entry(gameport, &gameport_list, node)
141 if (!gameport->dev && dev->connect)
142 dev->connect(gameport, dev);
145 void gameport_unregister_device(struct gameport_dev *dev)
147 struct gameport *gameport;
149 list_del_init(&dev->node);
150 list_for_each_entry(gameport, &gameport_list, node) {
151 if (gameport->dev == dev && dev->disconnect)
152 dev->disconnect(gameport);
153 gameport_find_dev(gameport);
157 int gameport_open(struct gameport *gameport, struct gameport_dev *dev, int mode)
159 if (gameport->open) {
160 if (gameport->open(gameport, mode))
161 return -1;
162 } else {
163 if (mode != GAMEPORT_MODE_RAW)
164 return -1;
167 if (gameport->dev)
168 return -1;
170 gameport->dev = dev;
172 return 0;
175 void gameport_close(struct gameport *gameport)
177 gameport->dev = NULL;
178 if (gameport->close)
179 gameport->close(gameport);