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