Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / usb / input / pid.c
1 /*
2  *  PID Force feedback support for hid devices.
3  *
4  *  Copyright (c) 2002 Rodrigo Damazio.
5  *  Portions by Johann Deneux and Bjorn Augustson
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  * Should you need to contact me, the author, you can do so by
24  * e-mail - mail your message to <rdamazio@lsi.usp.br>
25  */
26
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/mm.h>
32 #include <linux/smp_lock.h>
33 #include <linux/spinlock.h>
34 #include <linux/input.h>
35 #include <linux/usb.h>
36 #include "hid.h"
37 #include "pid.h"
38
39 #define CHECK_OWNERSHIP(i, hid_pid)     \
40         ((i) < FF_EFFECTS_MAX && i >= 0 && \
41         test_bit(FF_PID_FLAGS_USED, &hid_pid->effects[(i)].flags) && \
42         (current->pid == 0 || \
43         (hid_pid)->effects[(i)].owner == current->pid))
44
45 /* Called when a transfer is completed */
46 static void hid_pid_ctrl_out(struct urb *u, struct pt_regs *regs)
47 {
48         dev_dbg(&u->dev->dev, "hid_pid_ctrl_out - Transfer Completed\n");
49 }
50
51 static void hid_pid_exit(struct hid_device *hid)
52 {
53         struct hid_ff_pid *private = hid->ff_private;
54
55         if (private->urbffout) {
56                 usb_kill_urb(private->urbffout);
57                 usb_free_urb(private->urbffout);
58         }
59 }
60
61 static int pid_upload_periodic(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
62 {
63         dev_info(&pid->hid->dev->dev, "requested periodic force upload\n");
64         return 0;
65 }
66
67 static int pid_upload_constant(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
68 {
69         dev_info(&pid->hid->dev->dev, "requested constant force upload\n");
70         return 0;
71 }
72
73 static int pid_upload_condition(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
74 {
75         dev_info(&pid->hid->dev->dev, "requested Condition force upload\n");
76         return 0;
77 }
78
79 static int pid_upload_ramp(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
80 {
81         dev_info(&pid->hid->dev->dev, "request ramp force upload\n");
82         return 0;
83 }
84
85 static int hid_pid_event(struct hid_device *hid, struct input_dev *input,
86                          unsigned int type, unsigned int code, int value)
87 {
88         dev_dbg(&hid->dev->dev, "PID event received: type=%d,code=%d,value=%d.\n", type, code, value);
89
90         if (type != EV_FF)
91                 return -1;
92
93         return 0;
94 }
95
96 /* Lock must be held by caller */
97 static void hid_pid_ctrl_playback(struct hid_device *hid, struct hid_pid_effect *effect, int play)
98 {
99         if (play)
100                 set_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
101         else
102                 clear_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
103 }
104
105 static int hid_pid_erase(struct input_dev *dev, int id)
106 {
107         struct hid_device *hid = dev->private;
108         struct hid_ff_pid *pid = hid->ff_private;
109         struct hid_field *field;
110         unsigned long flags;
111         int ret;
112
113         if (!CHECK_OWNERSHIP(id, pid))
114                 return -EACCES;
115
116         /* Find report */
117         field = hid_find_field_by_usage(hid, HID_UP_PID | FF_PID_USAGE_BLOCK_FREE,
118                                         HID_OUTPUT_REPORT);
119         if (!field) {
120                 dev_err(&hid->dev->dev, "couldn't find report\n");
121                 return -EIO;
122         }
123
124         ret = hid_set_field(field, 0, pid->effects[id].device_id);
125         if (ret) {
126                 dev_err(&hid->dev->dev, "couldn't set field\n");
127                 return ret;
128         }
129
130         hid_submit_report(hid, field->report, USB_DIR_OUT);
131
132         spin_lock_irqsave(&pid->lock, flags);
133         hid_pid_ctrl_playback(hid, pid->effects + id, 0);
134         pid->effects[id].flags = 0;
135         spin_unlock_irqrestore(&pid->lock, flags);
136
137         return 0;
138 }
139
140 /* Erase all effects this process owns */
141 static int hid_pid_flush(struct input_dev *dev, struct file *file)
142 {
143         struct hid_device *hid = dev->private;
144         struct hid_ff_pid *pid = hid->ff_private;
145         int i;
146
147         /*NOTE: no need to lock here. The only times EFFECT_USED is
148            modified is when effects are uploaded or when an effect is
149            erased. But a process cannot close its dev/input/eventX fd
150            and perform ioctls on the same fd all at the same time */
151         /*FIXME: multiple threads, anyone? */
152         for (i = 0; i < dev->ff_effects_max; ++i)
153                 if (current->pid == pid->effects[i].owner
154                     && test_bit(FF_PID_FLAGS_USED, &pid->effects[i].flags))
155                         if (hid_pid_erase(dev, i))
156                                 dev_warn(&hid->dev->dev, "erase effect %d failed", i);
157
158         return 0;
159 }
160
161 static int hid_pid_upload_effect(struct input_dev *dev,
162                                  struct ff_effect *effect)
163 {
164         struct hid_ff_pid *pid_private = (struct hid_ff_pid *)(dev->private);
165         int ret;
166         int is_update;
167         unsigned long flags;
168
169         dev_dbg(&pid_private->hid->dev->dev, "upload effect called: effect_type=%x\n", effect->type);
170         /* Check this effect type is supported by this device */
171         if (!test_bit(effect->type, dev->ffbit)) {
172                 dev_dbg(&pid_private->hid->dev->dev,
173                         "invalid kind of effect requested.\n");
174                 return -EINVAL;
175         }
176
177         /*
178          * If we want to create a new effect, get a free id
179          */
180         if (effect->id == -1) {
181                 int id = 0;
182
183                 // Spinlock so we don`t get a race condition when choosing IDs
184                 spin_lock_irqsave(&pid_private->lock, flags);
185
186                 while (id < FF_EFFECTS_MAX)
187                         if (!test_and_set_bit(FF_PID_FLAGS_USED, &pid_private->effects[id++].flags))
188                                 break;
189
190                 if (id == FF_EFFECTS_MAX) {
191                         spin_unlock_irqrestore(&pid_private->lock, flags);
192 // TEMP - We need to get ff_effects_max correctly first:  || id >= dev->ff_effects_max) {
193                         dev_dbg(&pid_private->hid->dev->dev, "Not enough device memory\n");
194                         return -ENOMEM;
195                 }
196
197                 effect->id = id;
198                 dev_dbg(&pid_private->hid->dev->dev, "effect ID is %d.\n", id);
199                 pid_private->effects[id].owner = current->pid;
200                 pid_private->effects[id].flags = (1 << FF_PID_FLAGS_USED);
201                 spin_unlock_irqrestore(&pid_private->lock, flags);
202
203                 is_update = FF_PID_FALSE;
204         } else {
205                 /* We want to update an effect */
206                 if (!CHECK_OWNERSHIP(effect->id, pid_private))
207                         return -EACCES;
208
209                 /* Parameter type cannot be updated */
210                 if (effect->type != pid_private->effects[effect->id].effect.type)
211                         return -EINVAL;
212
213                 /* Check the effect is not already being updated */
214                 if (test_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags))
215                         return -EAGAIN;
216
217                 is_update = FF_PID_TRUE;
218         }
219
220         /*
221          * Upload the effect
222          */
223         switch (effect->type) {
224         case FF_PERIODIC:
225                 ret = pid_upload_periodic(pid_private, effect, is_update);
226                 break;
227
228         case FF_CONSTANT:
229                 ret = pid_upload_constant(pid_private, effect, is_update);
230                 break;
231
232         case FF_SPRING:
233         case FF_FRICTION:
234         case FF_DAMPER:
235         case FF_INERTIA:
236                 ret = pid_upload_condition(pid_private, effect, is_update);
237                 break;
238
239         case FF_RAMP:
240                 ret = pid_upload_ramp(pid_private, effect, is_update);
241                 break;
242
243         default:
244                 dev_dbg(&pid_private->hid->dev->dev,
245                         "invalid type of effect requested - %x.\n",
246                         effect->type);
247                 return -EINVAL;
248         }
249         /* If a packet was sent, forbid new updates until we are notified
250          * that the packet was updated
251          */
252         if (ret == 0)
253                 set_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags);
254         pid_private->effects[effect->id].effect = *effect;
255         return ret;
256 }
257
258 int hid_pid_init(struct hid_device *hid)
259 {
260         struct hid_ff_pid *private;
261         struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
262         struct input_dev *input_dev = hidinput->input;
263
264         private = hid->ff_private = kzalloc(sizeof(struct hid_ff_pid), GFP_KERNEL);
265         if (!private)
266                 return -ENOMEM;
267
268         private->hid = hid;
269
270         hid->ff_exit = hid_pid_exit;
271         hid->ff_event = hid_pid_event;
272
273         /* Open output URB */
274         if (!(private->urbffout = usb_alloc_urb(0, GFP_KERNEL))) {
275                 kfree(private);
276                 return -1;
277         }
278
279         usb_fill_control_urb(private->urbffout, hid->dev, 0,
280                              (void *)&private->ffcr, private->ctrl_buffer, 8,
281                              hid_pid_ctrl_out, hid);
282
283         input_dev->upload_effect = hid_pid_upload_effect;
284         input_dev->flush = hid_pid_flush;
285         input_dev->ff_effects_max = 8;  // A random default
286         set_bit(EV_FF, input_dev->evbit);
287         set_bit(EV_FF_STATUS, input_dev->evbit);
288
289         spin_lock_init(&private->lock);
290
291         printk(KERN_INFO "Force feedback driver for PID devices by Rodrigo Damazio <rdamazio@lsi.usp.br>.\n");
292
293         return 0;
294 }