1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
4  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
5  *
6  *  USB/RS232 I-Force joysticks and wheels.
7  */
8 
9 /*
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/input.h>
15 #include <linux/module.h>
16 #include <linux/spinlock.h>
17 #include <linux/circ_buf.h>
18 #include <linux/mutex.h>
19 
20 /* This module provides arbitrary resource management routines.
21  * I use it to manage the device's memory.
22  * Despite the name of this module, I am *not* going to access the ioports.
23  */
24 #include <linux/ioport.h>
25 
26 
27 #define IFORCE_MAX_LENGTH	16
28 
29 #define IFORCE_EFFECTS_MAX	32
30 
31 /* Each force feedback effect is made of one core effect, which can be
32  * associated to at most to effect modifiers
33  */
34 #define FF_MOD1_IS_USED		0
35 #define FF_MOD2_IS_USED		1
36 #define FF_CORE_IS_USED		2
37 #define FF_CORE_IS_PLAYED	3	/* Effect is currently being played */
38 #define FF_CORE_SHOULD_PLAY	4	/* User wants the effect to be played */
39 #define FF_CORE_UPDATE		5	/* Effect is being updated */
40 #define FF_MODCORE_CNT		6
41 
42 struct iforce_core_effect {
43 	/* Information about where modifiers are stored in the device's memory */
44 	struct resource mod1_chunk;
45 	struct resource mod2_chunk;
46 	unsigned long flags[BITS_TO_LONGS(FF_MODCORE_CNT)];
47 };
48 
49 #define FF_CMD_EFFECT		0x010e
50 #define FF_CMD_ENVELOPE		0x0208
51 #define FF_CMD_MAGNITUDE	0x0303
52 #define FF_CMD_PERIOD		0x0407
53 #define FF_CMD_CONDITION	0x050a
54 
55 #define FF_CMD_AUTOCENTER	0x4002
56 #define FF_CMD_PLAY		0x4103
57 #define FF_CMD_ENABLE		0x4201
58 #define FF_CMD_GAIN		0x4301
59 
60 #define FF_CMD_QUERY		0xff01
61 
62 /* Buffer for async write */
63 #define XMIT_SIZE		256
64 #define XMIT_INC(var, n)	(var)+=n; (var)&= XMIT_SIZE -1
65 /* iforce::xmit_flags */
66 #define IFORCE_XMIT_RUNNING	0
67 #define IFORCE_XMIT_AGAIN	1
68 
69 struct iforce_device {
70 	u16 idvendor;
71 	u16 idproduct;
72 	char *name;
73 	signed short *btn;
74 	signed short *abs;
75 	signed short *ff;
76 };
77 
78 struct iforce;
79 
80 struct iforce_xport_ops {
81 	void (*xmit)(struct iforce *iforce);
82 	int (*get_id)(struct iforce *iforce, u8 id,
83 		      u8 *response_data, size_t *response_len);
84 	int (*start_io)(struct iforce *iforce);
85 	void (*stop_io)(struct iforce *iforce);
86 };
87 
88 struct iforce {
89 	struct input_dev *dev;		/* Input device interface */
90 	struct iforce_device *type;
91 	const struct iforce_xport_ops *xport_ops;
92 
93 	spinlock_t xmit_lock;
94 	/* Buffer used for asynchronous sending of bytes to the device */
95 	struct circ_buf xmit;
96 	unsigned char xmit_data[XMIT_SIZE];
97 	unsigned long xmit_flags[1];
98 
99 					/* Force Feedback */
100 	wait_queue_head_t wait;
101 	struct resource device_memory;
102 	struct iforce_core_effect core_effects[IFORCE_EFFECTS_MAX];
103 	struct mutex mem_mutex;
104 };
105 
106 /* Get hi and low bytes of a 16-bits int */
107 #define HI(a)	((unsigned char)((a) >> 8))
108 #define LO(a)	((unsigned char)((a) & 0xff))
109 
110 /* For many parameters, it seems that 0x80 is a special value that should
111  * be avoided. Instead, we replace this value by 0x7f
112  */
113 #define HIFIX80(a) ((unsigned char)(((a)<0? (a)+255 : (a))>>8))
114 
115 /* Encode a time value */
116 #define TIME_SCALE(a)	(a)
117 
118 static inline int iforce_get_id_packet(struct iforce *iforce, u8 id,
119 				       u8 *response_data, size_t *response_len)
120 {
121 	return iforce->xport_ops->get_id(iforce, id,
122 					 response_data, response_len);
123 }
124 
125 /* Public functions */
126 /* iforce-main.c */
127 int iforce_init_device(struct device *parent, u16 bustype,
128 		       struct iforce *iforce);
129 
130 /* iforce-packets.c */
131 int iforce_control_playback(struct iforce*, u16 id, unsigned int);
132 void iforce_process_packet(struct iforce *iforce,
133 			   u8 packet_id, u8 *data, size_t len);
134 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data);
135 void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data);
136 
137 /* iforce-ff.c */
138 int iforce_upload_periodic(struct iforce *, struct ff_effect *, struct ff_effect *);
139 int iforce_upload_constant(struct iforce *, struct ff_effect *, struct ff_effect *);
140 int iforce_upload_condition(struct iforce *, struct ff_effect *, struct ff_effect *);
141 
142 /* Public variables */
143 extern struct serio_driver iforce_serio_drv;
144 extern struct usb_driver iforce_usb_driver;
145