1818#include <asm/mach-ps2/sif.h>
1919
2020static const unsigned int ev_keys [] = { GAMEPAD_INPUT_EV_KEYS };
21+ static const unsigned int ev_key_pressures [] = { GAMEPAD_INPUT_EV_ABS };
2122
2223struct gamepad_controllers {
2324 struct gamepad_controller {
@@ -49,18 +50,39 @@ static bool gamepad_device_mode_is_digital(
4950 return state -> device .mode == 0x41 ;
5051}
5152
53+ static bool gamepad_device_mode_is_analog_dualshock (
54+ const struct gamepad_controller_state * state )
55+ {
56+ return state -> device .mode == 0x73 ;
57+ }
58+
59+ static bool gamepad_device_mode_is_analog_dualshock2 (
60+ const struct gamepad_controller_state * state )
61+ {
62+ return state -> device .mode == 0x79 ;
63+ }
64+
5265static bool gamepad_device_mode_is_analog (
5366 const struct gamepad_controller_state * state )
5467{
55- return state -> device .mode == 0x73 ||
56- state -> device .mode == 0x79 ;
68+ return gamepad_device_mode_is_analog_dualshock (state ) ||
69+ gamepad_device_mode_is_analog_dualshock2 (state );
70+ }
71+
72+ static bool gamepad_device_mode_is_analog_sticks (
73+ const struct gamepad_controller_state * state )
74+ {
75+ return gamepad_device_mode_is_analog (state ) &&
76+ (state -> model .type == 1 ||
77+ state -> model .type == 3 ) &&
78+ state -> model .modes == 2 ;
5779}
5880
5981static bool gamepad_device_is_dualshock (
6082 const struct gamepad_controller_state * state )
6183{
6284 return (gamepad_device_mode_is_digital (state ) ||
63- gamepad_device_mode_is_analog (state )) &&
85+ gamepad_device_mode_is_analog_dualshock (state )) &&
6486 state -> model .type == 1 &&
6587 state -> model .modes == 2 &&
6688 state -> model .actuators == 2 ;
@@ -100,13 +122,10 @@ static inline int port_id(const struct gamepad_controller *ctrl)
100122 return 1 + ctrl -> port .index ;
101123}
102124
103- struct gamepad_rumble_data { struct gamepad_controller * ctrl ; };
104-
105125static int gamepad_rumble (struct input_dev * dev ,
106126 void * data , struct ff_effect * effect )
107127{
108- struct gamepad_rumble_data * rumble_data = data ;
109- struct gamepad_controller * ctrl = rumble_data -> ctrl ;
128+ struct gamepad_controller * ctrl = input_get_drvdata (dev );
110129
111130 switch (effect -> type ) {
112131 case FF_RUMBLE : {
@@ -131,41 +150,81 @@ static int gamepad_rumble(struct input_dev *dev,
131150 return 0 ;
132151}
133152
134- static void register_dualshock (struct gamepad_controller * ctrl )
153+ static void register_keys (struct gamepad_controller * ctrl )
154+ {
155+ int k ;
156+
157+ for (k = 0 ; k < ARRAY_SIZE (ev_keys ); k ++ )
158+ input_set_capability (ctrl -> dev , EV_KEY , ev_keys [k ]);
159+ }
160+
161+ static void register_sticks (struct gamepad_controller * ctrl )
135162{
136163 input_set_abs_params (ctrl -> dev , ABS_X , -128 , 127 , 0 , 0 );
137164 input_set_abs_params (ctrl -> dev , ABS_Y , -128 , 127 , 0 , 0 );
138165 input_set_abs_params (ctrl -> dev , ABS_RX , -128 , 127 , 0 , 0 );
139166 input_set_abs_params (ctrl -> dev , ABS_RY , -128 , 127 , 0 , 0 );
140167}
141168
169+ static void register_key_pressures (struct gamepad_controller * ctrl )
170+ {
171+ int k ;
172+
173+ for (k = 0 ; k < ARRAY_SIZE (ev_key_pressures ); k ++ )
174+ input_set_abs_params (ctrl -> dev ,
175+ ev_key_pressures [k ], 0 , 255 , 0 , 0 );
176+ }
177+
142178static void register_rumble (struct gamepad_controller * ctrl )
143179{
144180 int err ;
145181
146- /*
147- * ml_ff_destroy() calls kfree() on rumble_data,
148- * so it must be allocated with kmalloc.
149- */
150- struct gamepad_rumble_data * rumble_data =
151- kmalloc (sizeof (struct gamepad_rumble_data ), GFP_KERNEL );
152- if (!rumble_data ) {
153- pr_err ("%s: kmalloc failed\n" , __func__ );
154- return ;
155- }
156- * rumble_data = (struct gamepad_rumble_data ) { .ctrl = ctrl };
157-
158182 input_set_capability (ctrl -> dev , EV_FF , FF_RUMBLE );
159183
160- err = input_ff_create_memless (ctrl -> dev , rumble_data , gamepad_rumble );
184+ err = input_ff_create_memless (ctrl -> dev , NULL , gamepad_rumble );
161185 if (err < 0 )
162186 pr_err ("gamepad: input_ff_create_memless failed with %d\n" , err );
163187}
164188
189+ static void gamepad_ctrl_clear (struct gamepad_controller * ctrl )
190+ {
191+ ctrl -> dev = NULL ;
192+ ctrl -> name [0 ] = '\0' ;
193+ ctrl -> digital = (struct gamepad_controller_digital ) { };
194+ ctrl -> state = (struct gamepad_controller_state ) { };
195+ }
196+
197+ static int gamepad_controller_open (struct input_dev * dev )
198+ {
199+ struct gamepad_controller * ctrl = input_get_drvdata (dev );
200+ const struct gamepad_sif_opt opt = {
201+ .op = gamepad_rop_open ,
202+ .data = ctrl -> port .index ,
203+ };
204+
205+ int err = sif_cmd_opt (SIF_CMD_GAMEPAD , opt .raw , NULL , 0 );
206+ if (err < 0 )
207+ pr_err ("%s: sif_cmd_opt failed with %d\n" , __func__ , err );
208+
209+ return err ;
210+ }
211+
212+ static void gamepad_controller_close (struct input_dev * dev )
213+ {
214+ struct gamepad_controller * ctrl = input_get_drvdata (dev );
215+ const struct gamepad_sif_opt opt = {
216+ .op = gamepad_rop_close ,
217+ .data = ctrl -> port .index ,
218+ };
219+
220+ int err = sif_cmd_opt (SIF_CMD_GAMEPAD , opt .raw , NULL , 0 );
221+ if (err < 0 )
222+ pr_err ("%s: sif_cmd_opt failed with %d\n" , __func__ , err );
223+ }
224+
165225static bool gamepad_register (struct gamepad_controller * ctrl )
166226{
167227 const int device_id = gamepad_device_id (& ctrl -> state );
168- size_t k ;
169228 int err ;
170229
171230 if (ctrl -> dev )
@@ -186,12 +245,26 @@ static bool gamepad_register(struct gamepad_controller *ctrl)
186245 "PlayStation 2 gamepad port %d" , port_id (ctrl ));
187246 ctrl -> dev -> name = ctrl -> name ;
188247
189- for (k = 0 ; k < ARRAY_SIZE (ev_keys ); k ++ )
190- input_set_capability (ctrl -> dev , EV_KEY , ev_keys [k ]);
248+ input_set_drvdata (ctrl -> dev , ctrl );
191249
192- if (gamepad_device_is_dualshock (& ctrl -> state ) ||
193- gamepad_device_is_dualshock2 (& ctrl -> state ))
194- register_dualshock (ctrl );
250+ ctrl -> dev -> open = gamepad_controller_open ;
251+ ctrl -> dev -> close = gamepad_controller_close ;
252+
253+ register_keys (ctrl );
254+
255+ switch (gamepad_device_id (& ctrl -> state )) {
256+ case GAMEPAD_DEVICE_DUALSHOCK :
257+ register_sticks (ctrl );
258+ break ;
259+
260+ case GAMEPAD_DEVICE_DUALSHOCK2 :
261+ register_sticks (ctrl );
262+ register_key_pressures (ctrl );
263+ break ;
264+
265+ case GAMEPAD_DEVICE_UNDEFINED :
266+ break ;
267+ }
195268
196269 if (ctrl -> state .model .actuators > 0 )
197270 register_rumble (ctrl );
@@ -207,7 +280,7 @@ static bool gamepad_register(struct gamepad_controller *ctrl)
207280err :
208281 input_free_device (ctrl -> dev );
209282
210- ctrl -> dev = NULL ; // FIXME: Clear rest
283+ gamepad_ctrl_clear ( ctrl );
211284
212285 return false;
213286}
@@ -219,10 +292,7 @@ static void gamepad_unregister(struct gamepad_controller *ctrl)
219292
220293 input_unregister_device (ctrl -> dev );
221294
222- ctrl -> dev = NULL ;
223- ctrl -> name [0 ] = '\0' ;
224- ctrl -> digital = (struct gamepad_controller_digital ) { };
225- ctrl -> state = (struct gamepad_controller_state ) { };
295+ gamepad_ctrl_clear (ctrl );
226296}
227297
228298static void gamepad_work (struct work_struct * work )
@@ -245,27 +315,48 @@ static inline bool gamepad_device_connected(
245315 return state -> device .mode != 0 ;
246316}
247317
248- static void report_dualshock2 (struct gamepad_controller * ctrl ,
318+ static void report_keys (struct gamepad_controller * ctrl ,
249319 const struct gamepad_controller_state * state )
250320{
251- if (!gamepad_device_mode_is_analog (state ))
252- return ;
321+ const u32 dm = state -> digital .byte [0 ] |
322+ (state -> digital .byte [1 ] << 8 );
323+ int k ;
253324
325+ for (k = 0 ; k < ARRAY_SIZE (ev_keys ); k ++ )
326+ if ((dm ^ ctrl -> digital .mask ) & BIT (k ))
327+ input_report_key (ctrl -> dev , ev_keys [k ], (~dm ) & BIT (k ));
328+
329+ ctrl -> digital .mask = dm ;
330+ }
331+
332+ static void report_analog_sticks (struct gamepad_controller * ctrl ,
333+ const struct gamepad_controller_state * state )
334+ {
254335 input_report_abs (ctrl -> dev , ABS_X , (int )state -> analog .pad .lx - 128 );
255336 input_report_abs (ctrl -> dev , ABS_Y , (int )state -> analog .pad .ly - 128 );
256337 input_report_abs (ctrl -> dev , ABS_RX , (int )state -> analog .pad .rx - 128 );
257338 input_report_abs (ctrl -> dev , ABS_RY , (int )state -> analog .pad .ry - 128 );
258339}
259340
341+ static void report_key_pressures (struct gamepad_controller * ctrl ,
342+ const struct gamepad_controller_state * state )
343+ {
344+ enum { STICK_BYTES = 4 };
345+ int k ;
346+
347+ BUILD_BUG_ON (sizeof (struct gamepad_analogue_pad ) !=
348+ STICK_BYTES + ARRAY_SIZE (ev_key_pressures ));
349+
350+ for (k = 0 ; k < ARRAY_SIZE (ev_key_pressures ); k ++ )
351+ input_report_abs (ctrl -> dev , ev_key_pressures [k ],
352+ (int )state -> analog .byte [STICK_BYTES + k ]);
353+ }
354+
260355static void gamepad_event (const struct sif_cmd_header * header , void * arg )
261356{
262357 struct gamepad_controllers * ctrls = arg ;
263358
264359 const struct gamepad_controller_state * state = sif_cmd_payload (header );
265- const u32 dm = state -> digital .byte [0 ] |
266- (state -> digital .byte [1 ] << 8 );
267- size_t k ;
268-
269360 struct gamepad_controller * ctrl ;
270361
271362 if (state -> port .index >= GAMEPAD_PORTS ) {
@@ -295,15 +386,14 @@ static void gamepad_event(const struct sif_cmd_header *header, void *arg)
295386 return ;
296387 }
297388
298- for (k = 0 ; k < ARRAY_SIZE (ev_keys ); k ++ )
299- if ((dm ^ ctrl -> digital .mask ) & BIT (k ))
300- input_report_key (ctrl -> dev ,
301- ev_keys [k ], (~dm ) & BIT (k ));
389+ report_keys (ctrl , state );
302390
303- if (gamepad_device_is_dualshock2 (state ))
304- report_dualshock2 (ctrl , state );
391+ if (gamepad_device_mode_is_analog_sticks (state ))
392+ report_analog_sticks (ctrl , state );
393+
394+ if (gamepad_device_mode_is_analog_dualshock2 (state ))
395+ report_key_pressures (ctrl , state );
305396
306- ctrl -> digital .mask = dm ;
307397 ctrl -> state = * state ;
308398
309399 input_sync (ctrl -> dev );
0 commit comments