1/*
2 * $Id: iforce.c,v 1.1.1.1 2008/10/15 03:26:32 james26_jang Exp $
3 *
4 *  Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@suse.cz>
5 *  Copyright (c) 2001 Johann Deneux <deneux@ifrance.com>
6 *
7 *  USB/RS232 I-Force joysticks and wheels.
8 *
9 *  Sponsored by SuSE
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@suse.cz>, or by paper mail:
29 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
30 */
31
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/input.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/spinlock.h>
38#include <linux/usb.h>
39#include <linux/serio.h>
40#include <linux/config.h>
41
42/* FF: This module provides arbitrary resource management routines.
43 * I use it to manage the device's memory.
44 * Despite the name of this module, I am *not* going to access the ioports.
45 */
46#include <linux/ioport.h>
47
48MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>, Johann Deneux <deneux@ifrance.com>");
49MODULE_DESCRIPTION("USB/RS232 I-Force joysticks and wheels driver");
50MODULE_LICENSE("GPL");
51
52#define IFORCE_MAX_LENGTH	16
53
54#if defined(CONFIG_INPUT_IFORCE_232) || defined(CONFIG_INPUT_IFORCE_232_MODULE)
55#define IFORCE_232	1
56#endif
57#if defined(CONFIG_INPUT_IFORCE_USB) || defined(CONFIG_INPUT_IFORCE_USB_MODULE)
58#define IFORCE_USB	2
59#endif
60
61#define FF_EFFECTS_MAX	32
62
63/* Each force feedback effect is made of one core effect, which can be
64 * associated to at most to effect modifiers
65 */
66#define FF_MOD1_IS_USED		0
67#define FF_MOD2_IS_USED		1
68#define FF_CORE_IS_USED		2
69#define FF_CORE_IS_PLAYED	3
70#define FF_MODCORE_MAX		3
71
72struct iforce_core_effect {
73	/* Information about where modifiers are stored in the device's memory */
74	struct resource mod1_chunk;
75	struct resource mod2_chunk;
76	unsigned long flags[NBITS(FF_MODCORE_MAX)];
77};
78
79#define FF_CMD_EFFECT		0x010e
80#define FF_CMD_SHAPE		0x0208
81#define FF_CMD_MAGNITUDE	0x0303
82#define FF_CMD_PERIOD		0x0407
83#define FF_CMD_INTERACT		0x050a
84
85#define FF_CMD_AUTOCENTER	0x4002
86#define FF_CMD_PLAY		0x4103
87#define FF_CMD_ENABLE		0x4201
88#define FF_CMD_GAIN		0x4301
89
90#define FF_CMD_QUERY		0xff01
91
92static signed short btn_joystick[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE,
93	BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_A, BTN_B, BTN_C, BTN_DEAD, -1 };
94static signed short btn_wheel[] =    { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE,
95	BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_A, BTN_B, BTN_C, -1 };
96static signed short abs_joystick[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
97static signed short abs_wheel[] =    { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, -1 };
98static signed short ff_iforce[] =    { FF_PERIODIC, FF_CONSTANT, FF_SPRING, FF_FRICTION,
99	FF_SQUARE, FF_TRIANGLE, FF_SINE, FF_SAW_UP, FF_SAW_DOWN, FF_GAIN, FF_AUTOCENTER, -1 };
100
101static struct iforce_device {
102	u16 idvendor;
103	u16 idproduct;
104	char *name;
105	signed short *btn;
106	signed short *abs;
107	signed short *ff;
108} iforce_device[] = {
109	{ 0x046d, 0xc281, "Logitech WingMan Force",			btn_joystick, abs_joystick, ff_iforce },
110	{ 0x046d, 0xc291, "Logitech WingMan Formula Force",		btn_wheel, abs_wheel, ff_iforce },
111	{ 0x05ef, 0x020a, "AVB Top Shot Pegasus",			btn_joystick, abs_joystick, ff_iforce },
112	{ 0x05ef, 0x8884, "AVB Mag Turbo Force",			btn_wheel, abs_wheel, ff_iforce },
113	{ 0x06f8, 0x0001, "Guillemot Race Leader Force Feedback",	btn_wheel, abs_wheel, ff_iforce },
114	{ 0x0000, 0x0000, "Unknown I-Force Device [%04x:%04x]",		btn_joystick, abs_joystick, ff_iforce }
115};
116
117struct iforce {
118	struct input_dev dev;		/* Input device interface */
119	struct iforce_device *type;
120	char name[64];
121	int open;
122	int bus;
123
124	unsigned char data[IFORCE_MAX_LENGTH];
125	unsigned char edata[IFORCE_MAX_LENGTH];
126	u16 ecmd;
127	u16 expect_packet;
128
129#ifdef IFORCE_232
130	struct serio *serio;		/* RS232 transfer */
131	int idx, pkt, len, id;
132	unsigned char csum;
133#endif
134#ifdef IFORCE_USB
135	struct usb_device *usbdev;	/* USB transfer */
136	struct urb irq, out, ctrl;
137	struct usb_ctrlrequest dr;
138#endif
139					/* Force Feedback */
140	wait_queue_head_t wait;
141	struct resource device_memory;
142	struct iforce_core_effect core_effects[FF_EFFECTS_MAX];
143};
144
145static struct {
146	__s32 x;
147	__s32 y;
148} iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
149
150/* Get hi and low bytes of a 16-bits int */
151#define HI(a)	((unsigned char)((a) >> 8))
152#define LO(a)	((unsigned char)((a) & 0xff))
153
154/* Encode a time value */
155#define TIME_SCALE(a)	((a) == 0xffff ? 0xffff : (a) * 1000 / 256)
156
157static void dump_packet(char *msg, u16 cmd, unsigned char *data)
158{
159	int i;
160
161	printk(KERN_DEBUG "iforce.c: %s ( cmd = %04x, data = ", msg, cmd);
162	for (i = 0; i < LO(cmd); i++)
163		printk("%02x ", data[i]);
164	printk(")\n");
165}
166
167/*
168 * Send a packet of bytes to the device
169 */
170static void send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
171{
172	switch (iforce->bus) {
173
174#ifdef IFORCE_232
175		case IFORCE_232: {
176
177			int i;
178			unsigned char csum = 0x2b ^ HI(cmd) ^ LO(cmd);
179
180			serio_write(iforce->serio, 0x2b);
181			serio_write(iforce->serio, HI(cmd));
182			serio_write(iforce->serio, LO(cmd));
183
184			for (i = 0; i < LO(cmd); i++) {
185				serio_write(iforce->serio, data[i]);
186				csum = csum ^ data[i];
187			}
188
189			serio_write(iforce->serio, csum);
190			return;
191		}
192#endif
193#ifdef IFORCE_USB
194		case IFORCE_USB: {
195
196			DECLARE_WAITQUEUE(wait, current);
197			int timeout = HZ; /* 1 second */
198
199			memcpy(iforce->out.transfer_buffer + 1, data, LO(cmd));
200			((char*)iforce->out.transfer_buffer)[0] = HI(cmd);
201			iforce->out.transfer_buffer_length = LO(cmd) + 2;
202			iforce->out.dev = iforce->usbdev;
203
204			set_current_state(TASK_INTERRUPTIBLE);
205			add_wait_queue(&iforce->wait, &wait);
206
207			if (usb_submit_urb(&iforce->out)) {
208				set_current_state(TASK_RUNNING);
209				remove_wait_queue(&iforce->wait, &wait);
210				return;
211			}
212
213			while (timeout && iforce->out.status == -EINPROGRESS)
214				timeout = schedule_timeout(timeout);
215
216			set_current_state(TASK_RUNNING);
217			remove_wait_queue(&iforce->wait, &wait);
218
219			if (!timeout)
220				usb_unlink_urb(&iforce->out);
221
222			return;
223		}
224#endif
225	}
226}
227
228static void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
229{
230	struct input_dev *dev = &iforce->dev;
231	int i;
232
233#ifdef IFORCE_232
234	if (HI(iforce->expect_packet) == HI(cmd)) {
235		iforce->expect_packet = 0;
236		iforce->ecmd = cmd;
237		memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
238		if (waitqueue_active(&iforce->wait))
239			wake_up(&iforce->wait);
240	}
241#endif
242
243	if (!iforce->type)
244		return;
245
246	switch (HI(cmd)) {
247
248		case 0x01:	/* joystick position data */
249		case 0x03:	/* wheel position data */
250
251			if (HI(cmd) == 1) {
252				input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
253				input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
254				input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
255			} else {
256				input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
257				input_report_abs(dev, ABS_GAS,   255 - data[2]);
258				input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
259			}
260
261			input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
262			input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
263
264			for (i = 0; iforce->type->btn[i] >= 0; i++)
265				input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
266
267			break;
268
269		case 0x02:	/* status report */
270
271			input_report_key(dev, BTN_DEAD, data[0] & 0x02);
272			break;
273	}
274}
275
276static int get_id_packet(struct iforce *iforce, char *packet)
277{
278	DECLARE_WAITQUEUE(wait, current);
279	int timeout = HZ; /* 1 second */
280
281	switch (iforce->bus) {
282
283#ifdef IFORCE_USB
284		case IFORCE_USB:
285
286			iforce->dr.bRequest = packet[0];
287			iforce->ctrl.dev = iforce->usbdev;
288
289			set_current_state(TASK_INTERRUPTIBLE);
290			add_wait_queue(&iforce->wait, &wait);
291
292			if (usb_submit_urb(&iforce->ctrl)) {
293				set_current_state(TASK_RUNNING);
294				remove_wait_queue(&iforce->wait, &wait);
295				return -1;
296			}
297
298			while (timeout && iforce->ctrl.status == -EINPROGRESS)
299				timeout = schedule_timeout(timeout);
300
301			set_current_state(TASK_RUNNING);
302			remove_wait_queue(&iforce->wait, &wait);
303
304			if (!timeout) {
305				usb_unlink_urb(&iforce->ctrl);
306				return -1;
307			}
308
309			break;
310#endif
311#ifdef IFORCE_232
312		case IFORCE_232:
313
314			iforce->expect_packet = FF_CMD_QUERY;
315			send_packet(iforce, FF_CMD_QUERY, packet);
316
317			set_current_state(TASK_INTERRUPTIBLE);
318			add_wait_queue(&iforce->wait, &wait);
319
320			while (timeout && iforce->expect_packet)
321				timeout = schedule_timeout(timeout);
322
323			set_current_state(TASK_RUNNING);
324			remove_wait_queue(&iforce->wait, &wait);
325
326			if (!timeout) {
327				iforce->expect_packet = 0;
328				return -1;
329			}
330
331			break;
332#endif
333	}
334
335	return -(iforce->edata[0] != packet[0]);
336}
337
338static int iforce_open(struct input_dev *dev)
339{
340	struct iforce *iforce = dev->private;
341
342	switch (iforce->bus) {
343#ifdef IFORCE_USB
344		case IFORCE_USB:
345			if (iforce->open++)
346				break;
347			iforce->irq.dev = iforce->usbdev;
348			if (usb_submit_urb(&iforce->irq))
349					return -EIO;
350			break;
351#endif
352	}
353	return 0;
354}
355
356static void iforce_close(struct input_dev *dev)
357{
358	struct iforce *iforce = dev->private;
359
360	switch (iforce->bus) {
361#ifdef IFORCE_USB
362		case IFORCE_USB:
363			if (!--iforce->open)
364				usb_unlink_urb(&iforce->irq);
365			break;
366#endif
367	}
368}
369
370/*
371 * Start or stop playing an effect
372 */
373
374static int iforce_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
375{
376	struct iforce* iforce = (struct iforce*)(dev->private);
377	unsigned char data[3];
378
379	printk(KERN_DEBUG "iforce.c: input_event(type = %d, code = %d, value = %d)\n", type, code, value);
380
381	if (type != EV_FF)
382		return -1;
383
384	switch (code) {
385
386		case FF_GAIN:
387
388			data[0] = value >> 9;
389			send_packet(iforce, FF_CMD_GAIN, data);
390
391			return 0;
392
393		case FF_AUTOCENTER:
394
395			data[0] = 0x03;
396			data[1] = value >> 9;
397			send_packet(iforce, FF_CMD_AUTOCENTER, data);
398
399			data[0] = 0x04;
400			data[1] = 0x01;
401			send_packet(iforce, FF_CMD_AUTOCENTER, data);
402
403			return 0;
404
405		default: /* Play an effect */
406
407			if (code >= iforce->dev.ff_effects_max)
408				return -1;
409
410			data[0] = LO(code);
411			data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
412			data[2] = LO(value);
413			send_packet(iforce, FF_CMD_PLAY, data);
414
415			return 0;
416	}
417
418	return -1;
419}
420
421/*
422 * Set the magnitude of a constant force effect
423 * Return error code
424 *
425 * Note: caller must ensure exclusive access to device
426 */
427
428static int make_magnitude_modifier(struct iforce* iforce,
429	struct resource* mod_chunk, __s16 level)
430{
431	unsigned char data[3];
432
433	if (allocate_resource(&(iforce->device_memory), mod_chunk, 2,
434		iforce->device_memory.start, iforce->device_memory.end, 2L,
435		NULL, NULL)) {
436		return -ENOMEM;
437	}
438
439	data[0] = LO(mod_chunk->start);
440	data[1] = HI(mod_chunk->start);
441	data[2] = HI(level);
442
443	send_packet(iforce, FF_CMD_MAGNITUDE, data);
444
445	return 0;
446}
447
448/*
449 * Upload the component of an effect dealing with the period, phase and magnitude
450 */
451
452static int make_period_modifier(struct iforce* iforce, struct resource* mod_chunk,
453	__s16 magnitude, __s16 offset, u16 period, u16 phase)
454{
455	unsigned char data[7];
456
457	period = TIME_SCALE(period);
458
459	if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0c,
460		iforce->device_memory.start, iforce->device_memory.end, 2L,
461		NULL, NULL)) {
462		return -ENOMEM;
463	}
464
465	data[0] = LO(mod_chunk->start);
466	data[1] = HI(mod_chunk->start);
467
468	data[2] = HI(magnitude);
469	data[3] = HI(offset);
470	data[4] = HI(phase);
471
472	data[5] = LO(period);
473	data[6] = HI(period);
474
475	send_packet(iforce, FF_CMD_PERIOD, data);
476
477	return 0;
478}
479
480/*
481 * Uploads the part of an effect setting the shape of the force
482 */
483
484static int make_shape_modifier(struct iforce* iforce, struct resource* mod_chunk,
485	u16 attack_duration, __s16 initial_level,
486	u16 fade_duration, __s16 final_level)
487{
488	unsigned char data[8];
489
490	attack_duration = TIME_SCALE(attack_duration);
491	fade_duration = TIME_SCALE(fade_duration);
492
493	if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0e,
494		iforce->device_memory.start, iforce->device_memory.end, 2L,
495		NULL, NULL)) {
496		return -ENOMEM;
497	}
498
499	data[0] = LO(mod_chunk->start);
500	data[1] = HI(mod_chunk->start);
501
502	data[2] = LO(attack_duration);
503	data[3] = HI(attack_duration);
504	data[4] = HI(initial_level);
505
506	data[5] = LO(fade_duration);
507	data[6] = HI(fade_duration);
508	data[7] = HI(final_level);
509
510	send_packet(iforce, FF_CMD_SHAPE, data);
511
512	return 0;
513}
514
515/*
516 * Component of spring, friction, inertia... effects
517 */
518
519static int make_interactive_modifier(struct iforce* iforce,
520	struct resource* mod_chunk,
521	__s16 rsat, __s16 lsat, __s16 rk, __s16 lk, u16 db, __s16 center)
522{
523	unsigned char data[10];
524
525	if (allocate_resource(&(iforce->device_memory), mod_chunk, 8,
526		iforce->device_memory.start, iforce->device_memory.end, 2L,
527		NULL, NULL)) {
528		return -ENOMEM;
529	}
530
531	data[0] = LO(mod_chunk->start);
532	data[1] = HI(mod_chunk->start);
533
534	data[2] = HI(rk);
535	data[3] = HI(lk);
536
537	data[4] = LO(center);
538	data[5] = HI(center);
539
540	data[6] = LO(db);
541	data[7] = HI(db);
542
543	data[8] = HI(rsat);
544	data[9] = HI(lsat);
545
546	send_packet(iforce, FF_CMD_INTERACT, data);
547
548	return 0;
549}
550
551static unsigned char find_button(struct iforce *iforce, signed short button)
552{
553	int i;
554	for (i = 1; iforce->type->btn[i] >= 0; i++)
555		if (iforce->type->btn[i] == button)
556			return i + 1;
557	return 0;
558}
559
560/*
561 * Send the part common to all effects to the device
562 */
563
564static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
565	u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
566	u16 interval, u16 direction)
567{
568	unsigned char data[14];
569
570	duration = TIME_SCALE(duration);
571	delay    = TIME_SCALE(delay);
572	interval = TIME_SCALE(interval);
573
574	data[0]  = LO(id);
575	data[1]  = effect_type;
576	data[2]  = LO(axes) | find_button(iforce, button);
577
578	data[3]  = LO(duration);
579	data[4]  = HI(duration);
580
581	data[5]  = HI(direction);
582
583	data[6]  = LO(interval);
584	data[7]  = HI(interval);
585
586	data[8]  = LO(mod_id1);
587	data[9]  = HI(mod_id1);
588	data[10] = LO(mod_id2);
589	data[11] = HI(mod_id2);
590
591	data[12] = LO(delay);
592	data[13] = HI(delay);
593
594	send_packet(iforce, FF_CMD_EFFECT, data);
595
596	return 0;
597}
598
599/*
600 * Upload a periodic effect to the device
601 */
602
603static int iforce_upload_periodic(struct iforce* iforce, struct ff_effect* effect)
604{
605	u8 wave_code;
606	int core_id = effect->id;
607	struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
608	struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
609	struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
610	int err = 0;
611
612	err = make_period_modifier(iforce, mod1_chunk,
613		effect->u.periodic.magnitude, effect->u.periodic.offset,
614		effect->u.periodic.period, effect->u.periodic.phase);
615	if (err) return err;
616	set_bit(FF_MOD1_IS_USED, core_effect->flags);
617
618	err = make_shape_modifier(iforce, mod2_chunk,
619		effect->u.periodic.shape.attack_length,
620		effect->u.periodic.shape.attack_level,
621		effect->u.periodic.shape.fade_length,
622		effect->u.periodic.shape.fade_level);
623	if (err) return err;
624	set_bit(FF_MOD2_IS_USED, core_effect->flags);
625
626	switch (effect->u.periodic.waveform) {
627		case FF_SQUARE:		wave_code = 0x20; break;
628		case FF_TRIANGLE:	wave_code = 0x21; break;
629		case FF_SINE:		wave_code = 0x22; break;
630		case FF_SAW_UP:		wave_code = 0x23; break;
631		case FF_SAW_DOWN:	wave_code = 0x24; break;
632		default:		wave_code = 0x20; break;
633	}
634
635	err = make_core(iforce, effect->id,
636		mod1_chunk->start,
637		mod2_chunk->start,
638		wave_code,
639		0x20,
640		effect->replay.length,
641		effect->replay.delay,
642		effect->trigger.button,
643		effect->trigger.interval,
644		effect->u.periodic.direction);
645
646	return err;
647}
648
649/*
650 * Upload a constant force effect
651 */
652static int iforce_upload_constant(struct iforce* iforce, struct ff_effect* effect)
653{
654	int core_id = effect->id;
655	struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
656	struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
657	struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
658	int err = 0;
659
660	printk(KERN_DEBUG "iforce.c: make constant effect\n");
661
662	err = make_magnitude_modifier(iforce, mod1_chunk, effect->u.constant.level);
663	if (err) return err;
664	set_bit(FF_MOD1_IS_USED, core_effect->flags);
665
666	err = make_shape_modifier(iforce, mod2_chunk,
667		effect->u.constant.shape.attack_length,
668		effect->u.constant.shape.attack_level,
669		effect->u.constant.shape.fade_length,
670		effect->u.constant.shape.fade_level);
671	if (err) return err;
672	set_bit(FF_MOD2_IS_USED, core_effect->flags);
673
674	err = make_core(iforce, effect->id,
675		mod1_chunk->start,
676		mod2_chunk->start,
677		0x00,
678		0x20,
679		effect->replay.length,
680		effect->replay.delay,
681		effect->trigger.button,
682		effect->trigger.interval,
683		effect->u.constant.direction);
684
685	return err;
686}
687
688/*
689 * Upload an interactive effect. Those are for example friction, inertia, springs...
690 */
691static int iforce_upload_interactive(struct iforce* iforce, struct ff_effect* effect)
692{
693	int core_id = effect->id;
694	struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
695	struct resource* mod_chunk = &(core_effect->mod1_chunk);
696	u8 type, axes;
697	u16 mod1, mod2, direction;
698	int err = 0;
699
700	printk(KERN_DEBUG "iforce.c: make interactive effect\n");
701
702	switch (effect->type) {
703		case FF_SPRING:		type = 0x40; break;
704		case FF_FRICTION:	type = 0x41; break;
705		default: return -1;
706	}
707
708	err = make_interactive_modifier(iforce, mod_chunk,
709		effect->u.interactive.right_saturation,
710		effect->u.interactive.left_saturation,
711		effect->u.interactive.right_coeff,
712		effect->u.interactive.left_coeff,
713		effect->u.interactive.deadband,
714		effect->u.interactive.center);
715	if (err) return err;
716	set_bit(FF_MOD1_IS_USED, core_effect->flags);
717
718	switch ((test_bit(ABS_X, &effect->u.interactive.axis) ||
719		test_bit(ABS_WHEEL, &effect->u.interactive.axis)) |
720		(!!test_bit(ABS_Y, &effect->u.interactive.axis) << 1)) {
721
722		case 0: /* Only one axis, choose orientation */
723			mod1 = mod_chunk->start;
724			mod2 = 0xffff;
725			direction = effect->u.interactive.direction;
726			axes = 0x20;
727			break;
728
729		case 1:	/* Only X axis */
730			mod1 = mod_chunk->start;
731			mod2 = 0xffff;
732			direction = 0x5a00;
733			axes = 0x40;
734			break;
735
736		case 2: /* Only Y axis */
737			mod1 = 0xffff;
738			mod2 = mod_chunk->start;
739			direction = 0xb400;
740			axes = 0x80;
741			break;
742
743		case 3:	/* Both X and Y axes */
744			/* TODO: same setting for both axes is not mandatory */
745			mod1 = mod_chunk->start;
746			mod2 = mod_chunk->start;
747			direction = 0x6000;
748			axes = 0xc0;
749			break;
750
751		default:
752			return -1;
753	}
754
755	err = make_core(iforce, effect->id,
756		mod1, mod2,
757		type, axes,
758		effect->replay.length, effect->replay.delay,
759		effect->trigger.button, effect->trigger.interval,
760		direction);
761
762	return err;
763}
764
765/*
766 * Function called when an ioctl is performed on the event dev entry.
767 * It uploads an effect to the device
768 */
769static int iforce_upload_effect(struct input_dev *dev, struct ff_effect *effect)
770{
771	struct iforce* iforce = (struct iforce*)(dev->private);
772	int id;
773
774	printk(KERN_DEBUG "iforce.c: upload effect\n");
775
776/*
777 * Get a free id
778 */
779
780	for (id=0; id < FF_EFFECTS_MAX; ++id)
781		if (!test_bit(FF_CORE_IS_USED, iforce->core_effects[id].flags)) break;
782
783	if ( id == FF_EFFECTS_MAX || id >= iforce->dev.ff_effects_max)
784		return -ENOMEM;
785
786	effect->id = id;
787	set_bit(FF_CORE_IS_USED, iforce->core_effects[id].flags);
788
789/*
790 * Upload the effect
791 */
792
793	switch (effect->type) {
794
795		case FF_PERIODIC:
796			return iforce_upload_periodic(iforce, effect);
797
798		case FF_CONSTANT:
799			return iforce_upload_constant(iforce, effect);
800
801		case FF_SPRING:
802		case FF_FRICTION:
803			return iforce_upload_interactive(iforce, effect);
804
805		default:
806			return -1;
807	}
808}
809
810/*
811 * Erases an effect: it frees the effect id and mark as unused the memory
812 * allocated for the parameters
813 */
814static int iforce_erase_effect(struct input_dev *dev, int effect_id)
815{
816	struct iforce* iforce = (struct iforce*)(dev->private);
817	int err = 0;
818	struct iforce_core_effect* core_effect;
819
820	printk(KERN_DEBUG "iforce.c: erase effect %d\n", effect_id);
821
822	if (effect_id < 0 || effect_id >= FF_EFFECTS_MAX)
823		return -EINVAL;
824
825	core_effect = iforce->core_effects + effect_id;
826
827	if (test_bit(FF_MOD1_IS_USED, core_effect->flags))
828		err = release_resource(&(iforce->core_effects[effect_id].mod1_chunk));
829
830	if (!err && test_bit(FF_MOD2_IS_USED, core_effect->flags))
831		err = release_resource(&(iforce->core_effects[effect_id].mod2_chunk));
832
833	/*TODO: remember to change that if more FF_MOD* bits are added */
834	core_effect->flags[0] = 0;
835
836	return err;
837}
838static int iforce_init_device(struct iforce *iforce)
839{
840	unsigned char c[] = "CEOV";
841	int i;
842
843	init_waitqueue_head(&iforce->wait);
844	iforce->dev.ff_effects_max = 10;
845
846/*
847 * Input device fields.
848 */
849
850	iforce->dev.idbus = BUS_USB;
851	iforce->dev.private = iforce;
852	iforce->dev.name = iforce->name;
853	iforce->dev.open = iforce_open;
854	iforce->dev.close = iforce_close;
855	iforce->dev.event = iforce_input_event;
856	iforce->dev.upload_effect = iforce_upload_effect;
857	iforce->dev.erase_effect = iforce_erase_effect;
858
859/*
860 * On-device memory allocation.
861 */
862
863	iforce->device_memory.name = "I-Force device effect memory";
864	iforce->device_memory.start = 0;
865	iforce->device_memory.end = 200;
866	iforce->device_memory.flags = IORESOURCE_MEM;
867	iforce->device_memory.parent = NULL;
868	iforce->device_memory.child = NULL;
869	iforce->device_memory.sibling = NULL;
870
871/*
872 * Wait until device ready - until it sends its first response.
873 */
874
875	for (i = 0; i < 20; i++)
876		if (!get_id_packet(iforce, "O"))
877			break;
878
879	if (i == 20) { /* 5 seconds */
880		printk(KERN_ERR "iforce.c: Timeout waiting for response from device.\n");
881		iforce_close(&iforce->dev);
882		return -1;
883	}
884
885/*
886 * Get device info.
887 */
888
889	if (!get_id_packet(iforce, "M"))
890		iforce->dev.idvendor = (iforce->edata[2] << 8) | iforce->edata[1];
891	if (!get_id_packet(iforce, "P"))
892		iforce->dev.idproduct = (iforce->edata[2] << 8) | iforce->edata[1];
893	if (!get_id_packet(iforce, "B"))
894		iforce->device_memory.end = (iforce->edata[2] << 8) | iforce->edata[1];
895	if (!get_id_packet(iforce, "N"))
896		iforce->dev.ff_effects_max = iforce->edata[1];
897
898/*
899 * Display additional info.
900 */
901
902	for (i = 0; c[i]; i++)
903		if (!get_id_packet(iforce, c + i))
904			dump_packet("info", iforce->ecmd, iforce->edata);
905
906
907	send_packet(iforce, FF_CMD_AUTOCENTER, "\004\000");
908	send_packet(iforce, FF_CMD_ENABLE, "\004");
909
910/*
911 * Find appropriate device entry
912 */
913
914	for (i = 0; iforce_device[i].idvendor; i++)
915		if (iforce_device[i].idvendor == iforce->dev.idvendor &&
916		    iforce_device[i].idproduct == iforce->dev.idproduct)
917			break;
918
919	iforce->type = iforce_device + i;
920
921	sprintf(iforce->name, iforce->type->name,
922		iforce->dev.idproduct, iforce->dev.idvendor);
923
924/*
925 * Set input device bitfields and ranges.
926 */
927
928	iforce->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_FF);
929
930	for (i = 0; iforce->type->btn[i] >= 0; i++) {
931		signed short t = iforce->type->btn[i];
932		set_bit(t, iforce->dev.keybit);
933		if (t != BTN_DEAD)
934			set_bit(FF_BTN(t), iforce->dev.ffbit);
935	}
936
937	for (i = 0; iforce->type->abs[i] >= 0; i++) {
938
939		signed short t = iforce->type->abs[i];
940		set_bit(t, iforce->dev.absbit);
941
942		switch (t) {
943
944			case ABS_X:
945			case ABS_Y:
946			case ABS_WHEEL:
947
948				iforce->dev.absmax[t] =  1920;
949				iforce->dev.absmin[t] = -1920;
950				iforce->dev.absflat[t] = 128;
951				iforce->dev.absfuzz[t] = 16;
952
953				set_bit(FF_ABS(t), iforce->dev.ffbit);
954				break;
955
956			case ABS_THROTTLE:
957			case ABS_GAS:
958			case ABS_BRAKE:
959
960				iforce->dev.absmax[t] = 255;
961				iforce->dev.absmin[t] = 0;
962				break;
963
964			case ABS_HAT0X:
965			case ABS_HAT0Y:
966				iforce->dev.absmax[t] =  1;
967				iforce->dev.absmin[t] = -1;
968				break;
969		}
970	}
971
972	for (i = 0; iforce->type->ff[i] >= 0; i++)
973		set_bit(iforce->type->ff[i], iforce->dev.ffbit);
974
975/*
976 * Register input device.
977 */
978
979	input_register_device(&iforce->dev);
980
981	return 0;
982}
983
984#ifdef IFORCE_USB
985
986static void iforce_usb_irq(struct urb *urb)
987{
988	struct iforce *iforce = urb->context;
989	if (urb->status) return;
990	iforce_process_packet(iforce,
991		(iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
992}
993
994static void iforce_usb_out(struct urb *urb)
995{
996	struct iforce *iforce = urb->context;
997	if (urb->status) return;
998	if (waitqueue_active(&iforce->wait))
999		wake_up(&iforce->wait);
1000}
1001
1002static void iforce_usb_ctrl(struct urb *urb)
1003{
1004	struct iforce *iforce = urb->context;
1005	if (urb->status) return;
1006	iforce->ecmd = 0xff00 | urb->actual_length;
1007	if (waitqueue_active(&iforce->wait))
1008		wake_up(&iforce->wait);
1009}
1010
1011static void *iforce_usb_probe(struct usb_device *dev, unsigned int ifnum,
1012				const struct usb_device_id *id)
1013{
1014	struct usb_endpoint_descriptor *epirq, *epout;
1015	struct iforce *iforce;
1016
1017	epirq = dev->config[0].interface[ifnum].altsetting[0].endpoint + 0;
1018	epout = dev->config[0].interface[ifnum].altsetting[0].endpoint + 1;
1019
1020	if (!(iforce = kmalloc(sizeof(struct iforce) + 32, GFP_KERNEL))) return NULL;
1021	memset(iforce, 0, sizeof(struct iforce));
1022
1023	iforce->bus = IFORCE_USB;
1024	iforce->usbdev = dev;
1025
1026	iforce->dr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
1027	iforce->dr.wIndex = 0;
1028	iforce->dr.wLength = 16;
1029
1030	FILL_INT_URB(&iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
1031			iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
1032
1033	FILL_BULK_URB(&iforce->out, dev, usb_sndbulkpipe(dev, epout->bEndpointAddress),
1034			iforce + 1, 32, iforce_usb_out, iforce);
1035
1036	FILL_CONTROL_URB(&iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
1037			(void*) &iforce->dr, iforce->edata, 16, iforce_usb_ctrl, iforce);
1038
1039	if (iforce_init_device(iforce)) {
1040		kfree(iforce);
1041		return NULL;
1042	}
1043
1044	printk(KERN_INFO "input%d: %s [%d effects, %ld bytes memory] on usb%d:%d.%d\n",
1045		iforce->dev.number, iforce->dev.name, iforce->dev.ff_effects_max,
1046		iforce->device_memory.end, dev->bus->busnum, dev->devnum, ifnum);
1047
1048	return iforce;
1049}
1050
1051static void iforce_usb_disconnect(struct usb_device *dev, void *ptr)
1052{
1053	struct iforce *iforce = ptr;
1054	usb_unlink_urb(&iforce->irq);
1055	input_unregister_device(&iforce->dev);
1056	kfree(iforce);
1057}
1058
1059static struct usb_device_id iforce_usb_ids [] = {
1060	{ USB_DEVICE(0x046d, 0xc281) },		/* Logitech WingMan Force */
1061	{ USB_DEVICE(0x046d, 0xc291) },		/* Logitech WingMan Formula Force */
1062	{ USB_DEVICE(0x05ef, 0x020a) },		/* AVB Top Shot Pegasus */
1063	{ USB_DEVICE(0x05ef, 0x8884) },		/* AVB Mag Turbo Force */
1064	{ USB_DEVICE(0x06f8, 0x0001) },		/* Guillemot Race Leader Force Feedback */
1065	{ }					/* Terminating entry */
1066};
1067
1068MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
1069
1070static struct usb_driver iforce_usb_driver = {
1071	name:		"iforce",
1072	probe:		iforce_usb_probe,
1073	disconnect:	iforce_usb_disconnect,
1074	id_table:	iforce_usb_ids,
1075};
1076
1077#endif
1078
1079#ifdef IFORCE_232
1080
1081static void iforce_serio_irq(struct serio *serio, unsigned char data, unsigned int flags)
1082{
1083	struct iforce* iforce = serio->private;
1084
1085	if (!iforce->pkt) {
1086		if (data != 0x2b) {
1087			return;
1088		}
1089		iforce->pkt = 1;
1090		return;
1091	}
1092
1093	if (!iforce->id) {
1094		if (data > 3 && data != 0xff) {
1095			iforce->pkt = 0;
1096			return;
1097		}
1098		iforce->id = data;
1099		return;
1100	}
1101
1102	if (!iforce->len) {
1103		if (data > IFORCE_MAX_LENGTH) {
1104			iforce->pkt = 0;
1105			iforce->id = 0;
1106			return;
1107		}
1108		iforce->len = data;
1109		return;
1110	}
1111
1112	if (iforce->idx < iforce->len) {
1113		iforce->csum += iforce->data[iforce->idx++] = data;
1114		return;
1115	}
1116
1117	if (iforce->idx == iforce->len) {
1118		iforce_process_packet(iforce, (iforce->id << 8) | iforce->idx, iforce->data);
1119		iforce->pkt = 0;
1120		iforce->id  = 0;
1121		iforce->len = 0;
1122		iforce->idx = 0;
1123		iforce->csum = 0;
1124	}
1125}
1126
1127static void iforce_serio_connect(struct serio *serio, struct serio_dev *dev)
1128{
1129	struct iforce *iforce;
1130	if (serio->type != (SERIO_RS232 | SERIO_IFORCE))
1131		return;
1132
1133	if (!(iforce = kmalloc(sizeof(struct iforce), GFP_KERNEL))) return;
1134	memset(iforce, 0, sizeof(struct iforce));
1135
1136	iforce->bus = IFORCE_232;
1137	iforce->serio = serio;
1138	serio->private = iforce;
1139
1140	if (serio_open(serio, dev)) {
1141		kfree(iforce);
1142		return;
1143	}
1144
1145	if (iforce_init_device(iforce)) {
1146		serio_close(serio);
1147		kfree(iforce);
1148		return;
1149	}
1150
1151	printk(KERN_INFO "input%d: %s [%d effects, %ld bytes memory] on serio%d\n",
1152		iforce->dev.number, iforce->dev.name, iforce->dev.ff_effects_max,
1153		iforce->device_memory.end, serio->number);
1154}
1155
1156static void iforce_serio_disconnect(struct serio *serio)
1157{
1158	struct iforce* iforce = serio->private;
1159
1160	input_unregister_device(&iforce->dev);
1161	serio_close(serio);
1162	kfree(iforce);
1163}
1164
1165static struct serio_dev iforce_serio_dev = {
1166	interrupt:	iforce_serio_irq,
1167	connect:	iforce_serio_connect,
1168	disconnect:	iforce_serio_disconnect,
1169};
1170
1171#endif
1172
1173static int __init iforce_init(void)
1174{
1175#ifdef IFORCE_USB
1176	usb_register(&iforce_usb_driver);
1177#endif
1178#ifdef IFORCE_232
1179	serio_register_device(&iforce_serio_dev);
1180#endif
1181	return 0;
1182}
1183
1184static void __exit iforce_exit(void)
1185{
1186#ifdef IFORCE_USB
1187	usb_deregister(&iforce_usb_driver);
1188#endif
1189#ifdef IFORCE_232
1190	serio_unregister_device(&iforce_serio_dev);
1191#endif
1192}
1193
1194module_init(iforce_init);
1195module_exit(iforce_exit);
1196