1/*
2 * $Id: joydump.c,v 1.1.1.1 2007/08/03 18:52:33 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#include <linux/slab.h>
38
39#define DRIVER_DESC	"Gameport data dumper module"
40
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION(DRIVER_DESC);
43MODULE_LICENSE("GPL");
44
45#define BUF_SIZE 256
46
47struct joydump {
48	unsigned int time;
49	unsigned char data;
50};
51
52static int joydump_connect(struct gameport *gameport, struct gameport_driver *drv)
53{
54	struct joydump *buf;	/* all entries */
55	struct joydump *dump, *prev;	/* one entry each */
56	int axes[4], buttons;
57	int i, j, t, timeout;
58	unsigned long flags;
59	unsigned char u;
60
61	printk(KERN_INFO "joydump: ,------------------ START ----------------.\n");
62	printk(KERN_INFO "joydump: | Dumping: %30s |\n", gameport->phys);
63	printk(KERN_INFO "joydump: | Speed: %28d kHz |\n", gameport->speed);
64
65	if (gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
66
67		printk(KERN_INFO "joydump: | Raw mode not available - trying cooked.    |\n");
68
69		if (gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
70
71			printk(KERN_INFO "joydump: | Cooked not available either. Failing.   |\n");
72			printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
73			return -ENODEV;
74		}
75
76		gameport_cooked_read(gameport, axes, &buttons);
77
78		for (i = 0; i < 4; i++)
79			printk(KERN_INFO "joydump: | Axis %d: %4d.                           |\n", i, axes[i]);
80		printk(KERN_INFO "joydump: | Buttons %02x.                             |\n", buttons);
81		printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
82	}
83
84	timeout = gameport_time(gameport, 10000); /* 10 ms */
85
86	buf = kmalloc(BUF_SIZE * sizeof(struct joydump), GFP_KERNEL);
87	if (!buf) {
88		printk(KERN_INFO "joydump: no memory for testing\n");
89		goto jd_end;
90	}
91	dump = buf;
92	t = 0;
93	i = 1;
94
95	local_irq_save(flags);
96
97	u = gameport_read(gameport);
98
99	dump->data = u;
100	dump->time = t;
101	dump++;
102
103	gameport_trigger(gameport);
104
105	while (i < BUF_SIZE && t < timeout) {
106
107		dump->data = gameport_read(gameport);
108
109		if (dump->data ^ u) {
110			u = dump->data;
111			dump->time = t;
112			i++;
113			dump++;
114		}
115		t++;
116	}
117
118	local_irq_restore(flags);
119
120/*
121 * Dump data.
122 */
123
124	t = i;
125	dump = buf;
126	prev = dump;
127
128	printk(KERN_INFO "joydump: >------------------ DATA -----------------<\n");
129	printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ", 0, 0);
130	for (j = 7; j >= 0; j--)
131		printk("%d", (dump->data >> j) & 1);
132	printk(" |\n");
133	dump++;
134
135	for (i = 1; i < t; i++, dump++, prev++) {
136		printk(KERN_INFO "joydump: | index: %3d delta: %3d us data: ",
137			i, dump->time - prev->time);
138		for (j = 7; j >= 0; j--)
139			printk("%d", (dump->data >> j) & 1);
140		printk(" |\n");
141	}
142	kfree(buf);
143
144jd_end:
145	printk(KERN_INFO "joydump: `------------------- END -----------------'\n");
146
147	return 0;
148}
149
150static void joydump_disconnect(struct gameport *gameport)
151{
152	gameport_close(gameport);
153}
154
155static struct gameport_driver joydump_drv = {
156	.driver		= {
157		.name	= "joydump",
158	},
159	.description	= DRIVER_DESC,
160	.connect	= joydump_connect,
161	.disconnect	= joydump_disconnect,
162};
163
164static int __init joydump_init(void)
165{
166	gameport_register_driver(&joydump_drv);
167	return 0;
168}
169
170static void __exit joydump_exit(void)
171{
172	gameport_unregister_driver(&joydump_drv);
173}
174
175module_init(joydump_init);
176module_exit(joydump_exit);
177