xref: /openbmc/linux/drivers/input/joystick/joydump.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2  * $Id: joydump.c,v 1.1 2002/01/23 06:56:16 jsimmons Exp $
3  *
4  *  Copyright (c) 1996-2001 Vojtech Pavlik
5  */
6 
7 /*
8  * This is just a very simple driver that can dump the data
9  * out of the joystick port into the syslog ...
10  */
11 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * Should you need to contact me, the author, you can do so either by
28  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
30  */
31 
32 #include <linux/module.h>
33 #include <linux/gameport.h>
34 #include <linux/kernel.h>
35 #include <linux/delay.h>
36 #include <linux/init.h>
37 
38 #define DRIVER_DESC	"Gameport data dumper module"
39 
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_LICENSE("GPL");
43 
44 #define BUF_SIZE 256
45 
46 struct joydump {
47 	unsigned int time;
48 	unsigned char data;
49 };
50 
51 static int joydump_connect(struct gameport *gameport, struct gameport_driver *drv)
52 {
53 	struct joydump *buf;	/* all entries */
54 	struct joydump *dump, *prev;	/* one entry each */
55 	int axes[4], buttons;
56 	int i, j, t, timeout;
57 	unsigned long flags;
58 	unsigned char u;
59 
60 	printk(KERN_INFO "joydump: ,------------------ START ----------------.\n");
61 	printk(KERN_INFO "joydump: | Dumping: %30s |\n", gameport->phys);
62 	printk(KERN_INFO "joydump: | Speed: %28d kHz |\n", gameport->speed);
63 
64 	if (gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
65 
66 		printk(KERN_INFO "joydump: | Raw mode not available - trying cooked.    |\n");
67 
68 		if (gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
69 
70 			printk(KERN_INFO "joydump: | Cooked not available either. Failing.   |\n");
71 			printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
72 			return -ENODEV;
73 		}
74 
75 		gameport_cooked_read(gameport, axes, &buttons);
76 
77 		for (i = 0; i < 4; i++)
78 			printk(KERN_INFO "joydump: | Axis %d: %4d.                           |\n", i, axes[i]);
79 		printk(KERN_INFO "joydump: | Buttons %02x.                             |\n", buttons);
80 		printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
81 	}
82 
83 	timeout = gameport_time(gameport, 10000); /* 10 ms */
84 
85 	buf = kmalloc(BUF_SIZE * sizeof(struct joydump), GFP_KERNEL);
86 	if (!buf) {
87 		printk(KERN_INFO "joydump: no memory for testing\n");
88 		goto jd_end;
89 	}
90 	dump = buf;
91 	t = 0;
92 	i = 1;
93 
94 	local_irq_save(flags);
95 
96 	u = gameport_read(gameport);
97 
98 	dump->data = u;
99 	dump->time = t;
100 	dump++;
101 
102 	gameport_trigger(gameport);
103 
104 	while (i < BUF_SIZE && t < timeout) {
105 
106 		dump->data = gameport_read(gameport);
107 
108 		if (dump->data ^ u) {
109 			u = dump->data;
110 			dump->time = t;
111 			i++;
112 			dump++;
113 		}
114 		t++;
115 	}
116 
117 	local_irq_restore(flags);
118 
119 /*
120  * Dump data.
121  */
122 
123 	t = i;
124 	dump = buf;
125 	prev = dump;
126 
127 	printk(KERN_INFO "joydump: >------------------ DATA -----------------<\n");
128 	printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ", 0, 0);
129 	for (j = 7; j >= 0; j--)
130 		printk("%d", (dump->data >> j) & 1);
131 	printk(" |\n");
132 	dump++;
133 
134 	for (i = 1; i < t; i++, dump++, prev++) {
135 		printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ",
136 			i, dump->time - prev->time);
137 		for (j = 7; j >= 0; j--)
138 			printk("%d", (dump->data >> j) & 1);
139 		printk(" |\n");
140 	}
141 	kfree(buf);
142 
143 jd_end:
144 	printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
145 
146 	return 0;
147 }
148 
149 static void joydump_disconnect(struct gameport *gameport)
150 {
151 	gameport_close(gameport);
152 }
153 
154 static struct gameport_driver joydump_drv = {
155 	.driver		= {
156 		.name	= "joydump",
157 	},
158 	.description	= DRIVER_DESC,
159 	.connect	= joydump_connect,
160 	.disconnect	= joydump_disconnect,
161 };
162 
163 static int __init joydump_init(void)
164 {
165 	gameport_register_driver(&joydump_drv);
166 	return 0;
167 }
168 
169 static void __exit joydump_exit(void)
170 {
171 	gameport_unregister_driver(&joydump_drv);
172 }
173 
174 module_init(joydump_init);
175 module_exit(joydump_exit);
176