2 * PID Force feedback support for hid devices.
4 * Copyright (c) 2002 Rodrigo Damazio.
5 * Portions by Johann Deneux and Bjorn Augustson
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.
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.
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
23 * Should you need to contact me, the author, you can do so by
24 * e-mail - mail your message to <rdamazio@lsi.usp.br>
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #include <linux/kernel.h>
31 #include <linux/init.h>
33 #include <linux/smp_lock.h>
34 #include <linux/spinlock.h>
35 #include <linux/input.h>
36 #include <linux/usb.h>
42 #define CHECK_OWNERSHIP(i, hid_pid) \
43 ((i) < FF_EFFECTS_MAX && i >= 0 && \
44 test_bit(FF_PID_FLAGS_USED, &hid_pid->effects[(i)].flags) && \
45 (current->pid == 0 || \
46 (hid_pid)->effects[(i)].owner == current->pid))
48 /* Called when a transfer is completed */
49 static void hid_pid_ctrl_out(struct urb
*u
, struct pt_regs
*regs
)
51 dev_dbg(&u
->dev
->dev
, "hid_pid_ctrl_out - Transfer Completed\n");
54 static void hid_pid_exit(struct hid_device
* hid
)
56 struct hid_ff_pid
*private = hid
->ff_private
;
58 if (private->urbffout
) {
59 usb_unlink_urb(private->urbffout
);
60 usb_free_urb(private->urbffout
);
64 static int pid_upload_periodic(struct hid_ff_pid
*pid
, struct ff_effect
*effect
, int is_update
) {
65 dev_info(&pid
->hid
->dev
->dev
, "requested periodic force upload\n");
69 static int pid_upload_constant(struct hid_ff_pid
*pid
, struct ff_effect
*effect
, int is_update
) {
70 dev_info(&pid
->hid
->dev
->dev
, "requested constant force upload\n");
74 static int pid_upload_condition(struct hid_ff_pid
*pid
, struct ff_effect
*effect
, int is_update
) {
75 dev_info(&pid
->hid
->dev
->dev
, "requested Condition force upload\n");
79 static int pid_upload_ramp(struct hid_ff_pid
*pid
, struct ff_effect
*effect
, int is_update
) {
80 dev_info(&pid
->hid
->dev
->dev
, "request ramp force upload\n");
84 static int hid_pid_event(struct hid_device
*hid
, struct input_dev
*input
,
85 unsigned int type
, unsigned int code
, int value
)
87 dev_dbg(&hid
->dev
->dev
, "PID event received: type=%d,code=%d,value=%d.\n", type
, code
, value
);
97 /* Lock must be held by caller */
98 static void hid_pid_ctrl_playback(struct hid_device
*hid
,
99 struct hid_pid_effect
*effect
, int play
)
102 set_bit(FF_PID_FLAGS_PLAYING
, &effect
->flags
);
105 clear_bit(FF_PID_FLAGS_PLAYING
, &effect
->flags
);
110 static int hid_pid_erase(struct input_dev
*dev
, int id
)
112 struct hid_device
*hid
= dev
->private;
113 struct hid_field
* field
;
114 struct hid_report
* report
;
115 struct hid_ff_pid
*pid
= hid
->ff_private
;
117 unsigned wanted_report
= HID_UP_PID
| FF_PID_USAGE_BLOCK_FREE
; /* PID Block Free Report */
120 if (!CHECK_OWNERSHIP(id
, pid
))
124 ret
= hid_find_report_by_usage(hid
, wanted_report
, &report
, HID_OUTPUT_REPORT
);
126 dev_err(&hid
->dev
->dev
, "couldn't find report\n");
131 field
= (struct hid_field
*) kmalloc(sizeof(struct hid_field
), GFP_KERNEL
);
133 dev_err(&hid
->dev
->dev
, "couldn't allocate field\n");
137 ret
= hid_set_field(field
, ret
, pid
->effects
[id
].device_id
);
139 dev_err(&hid
->dev
->dev
, "couldn't set field\n");
143 hid_submit_report(hid
, report
, USB_DIR_OUT
);
145 spin_lock_irqsave(&pid
->lock
, flags
);
146 hid_pid_ctrl_playback(hid
, pid
->effects
+ id
, 0);
147 pid
->effects
[id
].flags
= 0;
148 spin_unlock_irqrestore(&pid
->lock
, flags
);
153 /* Erase all effects this process owns */
154 static int hid_pid_flush(struct input_dev
*dev
, struct file
*file
)
156 struct hid_device
*hid
= dev
->private;
157 struct hid_ff_pid
*pid
= hid
->ff_private
;
160 /*NOTE: no need to lock here. The only times EFFECT_USED is
161 modified is when effects are uploaded or when an effect is
162 erased. But a process cannot close its dev/input/eventX fd
163 and perform ioctls on the same fd all at the same time */
164 for (i
=0; i
<dev
->ff_effects_max
; ++i
)
165 if ( current
->pid
== pid
->effects
[i
].owner
166 && test_bit(FF_PID_FLAGS_USED
, &pid
->effects
[i
].flags
))
167 if (hid_pid_erase(dev
, i
))
168 dev_warn(&hid
->dev
->dev
, "erase effect %d failed", i
);
174 static int hid_pid_upload_effect(struct input_dev
*dev
,
175 struct ff_effect
*effect
)
177 struct hid_ff_pid
* pid_private
= (struct hid_ff_pid
*)(dev
->private);
180 unsigned long flags
= 0;
182 dev_dbg(&pid_private
->hid
->dev
->dev
, "upload effect called: effect_type=%x\n",effect
->type
);
183 /* Check this effect type is supported by this device */
184 if (!test_bit(effect
->type
, dev
->ffbit
)) {
185 dev_dbg(&pid_private
->hid
->dev
->dev
, "invalid kind of effect requested.\n");
190 * If we want to create a new effect, get a free id
192 if (effect
->id
== -1) {
195 // Spinlock so we don`t get a race condition when choosing IDs
196 spin_lock_irqsave(&pid_private
->lock
, flags
);
198 while(id
< FF_EFFECTS_MAX
)
199 if (!test_and_set_bit(FF_PID_FLAGS_USED
, &pid_private
->effects
[id
++].flags
))
202 if ( id
== FF_EFFECTS_MAX
) {
203 spin_unlock_irqrestore(&pid_private
->lock
,flags
);
204 // TEMP - We need to get ff_effects_max correctly first: || id >= dev->ff_effects_max) {
205 dev_dbg(&pid_private
->hid
->dev
->dev
, "Not enough device memory\n");
210 dev_dbg(&pid_private
->hid
->dev
->dev
, "effect ID is %d\n.",id
);
211 pid_private
->effects
[id
].owner
= current
->pid
;
212 pid_private
->effects
[id
].flags
= (1<<FF_PID_FLAGS_USED
);
213 spin_unlock_irqrestore(&pid_private
->lock
,flags
);
215 is_update
= FF_PID_FALSE
;
218 /* We want to update an effect */
219 if (!CHECK_OWNERSHIP(effect
->id
, pid_private
))
222 /* Parameter type cannot be updated */
223 if (effect
->type
!= pid_private
->effects
[effect
->id
].effect
.type
)
226 /* Check the effect is not already being updated */
227 if (test_bit(FF_PID_FLAGS_UPDATING
, &pid_private
->effects
[effect
->id
].flags
)) {
231 is_update
= FF_PID_TRUE
;
237 switch (effect
->type
) {
239 ret
= pid_upload_periodic(pid_private
, effect
, is_update
);
243 ret
= pid_upload_constant(pid_private
, effect
, is_update
);
250 ret
= pid_upload_condition(pid_private
, effect
, is_update
);
254 ret
= pid_upload_ramp(pid_private
, effect
, is_update
);
258 dev_dbg(&pid_private
->hid
->dev
->dev
, "invalid type of effect requested - %x.\n", effect
->type
);
261 /* If a packet was sent, forbid new updates until we are notified
262 * that the packet was updated
265 set_bit(FF_PID_FLAGS_UPDATING
, &pid_private
->effects
[effect
->id
].flags
);
266 pid_private
->effects
[effect
->id
].effect
= *effect
;
270 int hid_pid_init(struct hid_device
*hid
)
272 struct hid_ff_pid
*private;
273 struct hid_input
*hidinput
= list_entry(&hid
->inputs
, struct hid_input
, list
);
275 private = hid
->ff_private
= kmalloc(sizeof(struct hid_ff_pid
), GFP_KERNEL
);
276 if (!private) return -1;
278 memset(private,0,sizeof(struct hid_ff_pid
));
280 hid
->ff_private
= private; /* 'cause memset can move the block away */
284 hid
->ff_exit
= hid_pid_exit
;
285 hid
->ff_event
= hid_pid_event
;
287 /* Open output URB */
288 if (!(private->urbffout
= usb_alloc_urb(0, GFP_KERNEL
))) {
293 usb_fill_control_urb(private->urbffout
, hid
->dev
,0,(void *) &private->ffcr
,private->ctrl_buffer
,8,hid_pid_ctrl_out
,hid
);
294 hidinput
->input
.upload_effect
= hid_pid_upload_effect
;
295 hidinput
->input
.flush
= hid_pid_flush
;
296 hidinput
->input
.ff_effects_max
= 8; // A random default
297 set_bit(EV_FF
, hidinput
->input
.evbit
);
298 set_bit(EV_FF_STATUS
, hidinput
->input
.evbit
);
300 spin_lock_init(&private->lock
);
302 printk(KERN_INFO
"Force feedback driver for PID devices by Rodrigo Damazio <rdamazio@lsi.usp.br>.\n");