Import 2.4.0-test2pre6
[davej-history.git] / drivers / char / joystick / joy-warrior.c
blob232699859930808324b718f8c55e361dd301ce94
1 /*
2 * joy-warrior.c Version 0.1
4 * Copyright (c) 1998 David Thompson
5 * Copyright (c) 1999 Vojtech Pavlik
7 * Sponsored by SuSE
8 */
11 * This is a module for the Linux joystick driver, supporting
12 * the Logitech WingMan Warrior joystick.
16 * This program is free warftware; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Should you need to contact me, the author, you can do so either by
31 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
32 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
35 #include <asm/io.h>
36 #include <asm/system.h>
37 #include <linux/errno.h>
38 #include <linux/joystick.h>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/delay.h>
42 #include <linux/tty.h>
43 #include <linux/init.h>
46 * Constants.
49 #define N_JOYSTICK_WAR 13
50 #define JS_WAR_MAX_LENGTH 16
53 * List of Warriors.
56 static struct js_port* js_war_port = NULL;
58 static char js_war_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 };
61 * Per-Warrior data.
64 struct js_war_info {
65 struct tty_struct* tty;
66 struct js_port* port;
67 int idx;
68 int len;
69 unsigned char data[JS_WAR_MAX_LENGTH];
70 char used;
74 * js_war_process_packet() decodes packets the driver receives from the
75 * Warrior. It updates the data accordingly.
78 static void js_war_process_packet(struct js_war_info* info)
80 int **axes = info->port->axes;
81 int **buttons = info->port->buttons;
82 unsigned char *data = info->data;
83 int i;
85 if (!info->idx) return;
87 switch ((data[0] >> 4) & 7) {
89 case 1: /* Button data */
90 if (!info->port->devs[0]) return;
91 buttons[0][0] = ((data[3] & 0xa) >> 1) | ((data[3] & 0x5) << 1);
92 return;
93 case 3: /* XY-axis info->data */
94 if (!info->port->devs[0]) return;
95 axes[0][0] = ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5));
96 axes[0][1] = (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7);
97 return;
98 break;
99 case 5: /* Throttle, spinner, hat info->data */
100 if (!info->port->devs[0]) return;
101 axes[0][2] = (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7);
102 axes[0][3] = (data[3] & 2 ? 1 : 0) - (info->data[3] & 1 ? 1 : 0);
103 axes[0][4] = (data[3] & 8 ? 1 : 0) - (info->data[3] & 4 ? 1 : 0);
104 axes[0][5] = (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5);
105 return;
106 case 2: /* Static status (Send !S to get one) */
107 case 4: /* Dynamic status */
108 return;
109 default:
110 printk("joy-warrior: Unknown packet %d length %d:", (data[0] >> 4) & 7, info->idx);
111 for (i = 0; i < info->idx; i++)
112 printk(" %02x", data[i]);
113 printk("\n");
114 return;
119 * js_war_open() is a callback from the joystick device open routine.
122 static int js_war_open(struct js_dev *jd)
124 struct js_war_info *info = jd->port->info;
125 info->used++;
126 return 0;
130 * js_war_close() is a callback from the joystick device release routine.
133 static int js_war_close(struct js_dev *jd)
135 struct js_war_info *info = jd->port->info;
136 if (!--info->used) {
137 js_unregister_device(jd->port->devs[0]);
138 js_war_port = js_unregister_port(jd->port);
140 return 0;
144 * js_war_init_corr() initializes the correction values for the Warrior.
147 static void __init js_war_init_corr(struct js_corr **corr)
149 int i;
151 for (i = 0; i < 6; i++) {
152 corr[0][i].type = JS_CORR_BROKEN;
153 corr[0][i].prec = 0;
154 corr[0][i].coef[0] = -8;
155 corr[0][i].coef[1] = 8;
156 corr[0][i].coef[2] = (1 << 29) / (128 - 64);
157 corr[0][i].coef[3] = (1 << 29) / (128 - 64);
160 corr[0][2].coef[2] = (1 << 29) / (128 - 16);
161 corr[0][2].coef[3] = (1 << 29) / (128 - 16);
163 for (i = 3; i < 5; i++) {
164 corr[0][i].coef[0] = 0;
165 corr[0][i].coef[1] = 0;
166 corr[0][i].coef[2] = (1 << 29);
167 corr[0][i].coef[3] = (1 << 29);
170 corr[0][5].prec = -1;
171 corr[0][5].coef[0] = 0;
172 corr[0][5].coef[1] = 0;
173 corr[0][5].coef[2] = (1 << 29) / 128;
174 corr[0][5].coef[3] = (1 << 29) / 128;
178 * js_war_ldisc_open() is the routine that is called upon setting our line
179 * discipline on a tty.
182 static int js_war_ldisc_open(struct tty_struct *tty)
184 struct js_war_info iniinfo;
185 struct js_war_info *info = &iniinfo;
187 MOD_INC_USE_COUNT;
189 info->tty = tty;
190 info->idx = 0;
191 info->len = 0;
192 info->used = 1;
194 js_war_port = js_register_port(js_war_port, info, 1, sizeof(struct js_war_info), NULL);
196 info = js_war_port->info;
197 info->port = js_war_port;
198 tty->disc_data = info;
200 printk(KERN_INFO "js%d: WingMan Warrior on %s%d\n",
201 js_register_device(js_war_port, 0, 6, 4, "WingMan Warrior", THIS_MODULE, js_war_open, js_war_close),
202 tty->driver.name, MINOR(tty->device) - tty->driver.minor_start);
204 js_war_init_corr(js_war_port->corr);
206 return 0;
210 * js_war_ldisc_close() is the opposite of js_war_ldisc_open()
213 static void js_war_ldisc_close(struct tty_struct *tty)
215 struct js_war_info* info = (struct js_war_info*) tty->disc_data;
216 if (!--info->used) {
217 js_unregister_device(info->port->devs[0]);
218 js_war_port = js_unregister_port(info->port);
220 MOD_DEC_USE_COUNT;
224 * js_war_ldisc_receive() is called by the low level driver when characters
225 * are ready for us. We then buffer them for further processing, or call the
226 * packet processing routine.
229 static void js_war_ldisc_receive(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
231 struct js_war_info* info = (struct js_war_info*) tty->disc_data;
232 int i;
234 for (i = 0; i < count; i++) {
235 if (cp[i] & 0x80) {
236 if (info->idx)
237 js_war_process_packet(info);
238 info->idx = 0;
239 info->len = js_war_lengths[(cp[i] >> 4) & 7];
242 if (info->idx < JS_WAR_MAX_LENGTH)
243 info->data[info->idx++] = cp[i];
245 if (info->idx == info->len) {
246 if (info->idx)
247 js_war_process_packet(info);
248 info->idx = 0;
249 info->len = 0;
255 * js_war_ldisc_room() reports how much room we do have for receiving data.
256 * Although we in fact have infinite room, we need to specify some value
257 * here, so why not the size of our packet buffer. It's big anyway.
260 static int js_war_ldisc_room(struct tty_struct *tty)
262 return JS_WAR_MAX_LENGTH;
266 * The line discipline structure.
269 static struct tty_ldisc js_war_ldisc = {
270 magic: TTY_LDISC_MAGIC,
271 name: "warrior",
272 open: js_war_ldisc_open,
273 close: js_war_ldisc_close,
274 receive_buf: js_war_ldisc_receive,
275 receive_room: js_war_ldisc_room,
279 * The functions for inserting/removing us as a module.
282 #ifdef MODULE
283 int init_module(void)
284 #else
285 int __init js_war_init(void)
286 #endif
288 if (tty_register_ldisc(N_JOYSTICK_WAR, &js_war_ldisc)) {
289 printk(KERN_ERR "joy-warrior: Error registering line discipline.\n");
290 return -ENODEV;
293 return 0;
296 #ifdef MODULE
297 void cleanup_module(void)
299 tty_register_ldisc(N_JOYSTICK_WAR, NULL);
301 #endif