1 /*
2  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
4  *
5  *  USB/RS232 I-Force joysticks and wheels.
6  */
7 
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/input.h>
27 #include <linux/module.h>
28 #include <linux/spinlock.h>
29 #include <linux/usb.h>
30 #include <linux/serio.h>
31 #include <linux/circ_buf.h>
32 #include <linux/mutex.h>
33 
34 /* This module provides arbitrary resource management routines.
35  * I use it to manage the device's memory.
36  * Despite the name of this module, I am *not* going to access the ioports.
37  */
38 #include <linux/ioport.h>
39 
40 
41 #define IFORCE_MAX_LENGTH	16
42 
43 /* iforce::bus */
44 #define IFORCE_232	1
45 #define IFORCE_USB	2
46 
47 #define IFORCE_EFFECTS_MAX	32
48 
49 /* Each force feedback effect is made of one core effect, which can be
50  * associated to at most to effect modifiers
51  */
52 #define FF_MOD1_IS_USED		0
53 #define FF_MOD2_IS_USED		1
54 #define FF_CORE_IS_USED		2
55 #define FF_CORE_IS_PLAYED	3	/* Effect is currently being played */
56 #define FF_CORE_SHOULD_PLAY	4	/* User wants the effect to be played */
57 #define FF_CORE_UPDATE		5	/* Effect is being updated */
58 #define FF_MODCORE_CNT		6
59 
60 struct iforce_core_effect {
61 	/* Information about where modifiers are stored in the device's memory */
62 	struct resource mod1_chunk;
63 	struct resource mod2_chunk;
64 	unsigned long flags[BITS_TO_LONGS(FF_MODCORE_CNT)];
65 };
66 
67 #define FF_CMD_EFFECT		0x010e
68 #define FF_CMD_ENVELOPE		0x0208
69 #define FF_CMD_MAGNITUDE	0x0303
70 #define FF_CMD_PERIOD		0x0407
71 #define FF_CMD_CONDITION	0x050a
72 
73 #define FF_CMD_AUTOCENTER	0x4002
74 #define FF_CMD_PLAY		0x4103
75 #define FF_CMD_ENABLE		0x4201
76 #define FF_CMD_GAIN		0x4301
77 
78 #define FF_CMD_QUERY		0xff01
79 
80 /* Buffer for async write */
81 #define XMIT_SIZE		256
82 #define XMIT_INC(var, n)	(var)+=n; (var)&= XMIT_SIZE -1
83 /* iforce::xmit_flags */
84 #define IFORCE_XMIT_RUNNING	0
85 #define IFORCE_XMIT_AGAIN	1
86 
87 struct iforce_device {
88 	u16 idvendor;
89 	u16 idproduct;
90 	char *name;
91 	signed short *btn;
92 	signed short *abs;
93 	signed short *ff;
94 };
95 
96 struct iforce {
97 	struct input_dev *dev;		/* Input device interface */
98 	struct iforce_device *type;
99 	int bus;
100 
101 	unsigned char data[IFORCE_MAX_LENGTH];
102 	unsigned char edata[IFORCE_MAX_LENGTH];
103 	u16 ecmd;
104 	u16 expect_packet;
105 
106 #ifdef CONFIG_JOYSTICK_IFORCE_232
107 	struct serio *serio;		/* RS232 transfer */
108 	int idx, pkt, len, id;
109 	unsigned char csum;
110 #endif
111 #ifdef CONFIG_JOYSTICK_IFORCE_USB
112 	struct usb_device *usbdev;	/* USB transfer */
113 	struct usb_interface *intf;
114 	struct urb *irq, *out, *ctrl;
115 	struct usb_ctrlrequest cr;
116 #endif
117 	spinlock_t xmit_lock;
118 	/* Buffer used for asynchronous sending of bytes to the device */
119 	struct circ_buf xmit;
120 	unsigned char xmit_data[XMIT_SIZE];
121 	unsigned long xmit_flags[1];
122 
123 					/* Force Feedback */
124 	wait_queue_head_t wait;
125 	struct resource device_memory;
126 	struct iforce_core_effect core_effects[IFORCE_EFFECTS_MAX];
127 	struct mutex mem_mutex;
128 };
129 
130 /* Get hi and low bytes of a 16-bits int */
131 #define HI(a)	((unsigned char)((a) >> 8))
132 #define LO(a)	((unsigned char)((a) & 0xff))
133 
134 /* For many parameters, it seems that 0x80 is a special value that should
135  * be avoided. Instead, we replace this value by 0x7f
136  */
137 #define HIFIX80(a) ((unsigned char)(((a)<0? (a)+255 : (a))>>8))
138 
139 /* Encode a time value */
140 #define TIME_SCALE(a)	(a)
141 
142 
143 /* Public functions */
144 /* iforce-serio.c */
145 void iforce_serial_xmit(struct iforce *iforce);
146 
147 /* iforce-usb.c */
148 void iforce_usb_xmit(struct iforce *iforce);
149 
150 /* iforce-main.c */
151 int iforce_init_device(struct iforce *iforce);
152 
153 /* iforce-packets.c */
154 int iforce_control_playback(struct iforce*, u16 id, unsigned int);
155 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data);
156 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data);
157 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data);
158 int iforce_get_id_packet(struct iforce *iforce, char *packet);
159 
160 /* iforce-ff.c */
161 int iforce_upload_periodic(struct iforce *, struct ff_effect *, struct ff_effect *);
162 int iforce_upload_constant(struct iforce *, struct ff_effect *, struct ff_effect *);
163 int iforce_upload_condition(struct iforce *, struct ff_effect *, struct ff_effect *);
164 
165 /* Public variables */
166 extern struct serio_driver iforce_serio_drv;
167 extern struct usb_driver iforce_usb_driver;
168