vserver 1.9.5.x5
[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/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     dev_info(&pid->hid->dev->dev, "requested periodic force upload\n");
66     return 0;
67 }
68
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");
71     return 0;
72 }
73
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");
76     return 0;
77 }
78
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");
81     return 0;
82 }
83
84 static int hid_pid_event(struct hid_device *hid, struct input_dev *input,
85                           unsigned int type, unsigned int code, int value)
86 {
87     dev_dbg(&hid->dev->dev, "PID event received: type=%d,code=%d,value=%d.\n", type, code, value);
88
89     if (type != EV_FF)
90         return -1;
91
92     
93
94     return 0;
95 }
96
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)
100 {
101         if (play) {
102                 set_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
103
104         } else {
105                 clear_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
106         }
107 }
108
109
110 static int hid_pid_erase(struct input_dev *dev, int id)
111 {
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;
116         unsigned long flags;
117         unsigned wanted_report = HID_UP_PID | FF_PID_USAGE_BLOCK_FREE;  /*  PID Block Free Report */
118         int ret;
119
120         if (!CHECK_OWNERSHIP(id, pid))
121                 return -EACCES;
122
123         /* Find report */
124         ret =  hid_find_report_by_usage(hid, wanted_report, &report, HID_OUTPUT_REPORT);
125         if(!ret) {
126                 dev_err(&hid->dev->dev, "couldn't find report\n");
127                 return ret;
128         }
129
130         /* Find field */
131         field = (struct hid_field *) kmalloc(sizeof(struct hid_field), GFP_KERNEL);
132         if(!field) {
133                 dev_err(&hid->dev->dev, "couldn't allocate field\n");
134                 return -ENOMEM;
135         }
136
137         ret = hid_set_field(field, ret, pid->effects[id].device_id);
138         if(!ret) {
139                 dev_err(&hid->dev->dev, "couldn't set field\n");
140                 return ret;
141         }
142
143         hid_submit_report(hid, report, USB_DIR_OUT);
144
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);
149
150         return ret;
151 }
152
153 /* Erase all effects this process owns */
154 static int hid_pid_flush(struct input_dev *dev, struct file *file)
155 {
156         struct hid_device *hid = dev->private;
157         struct hid_ff_pid *pid = hid->ff_private;
158         int i;
159
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);
169
170         return 0;
171 }
172
173
174 static int hid_pid_upload_effect(struct input_dev *dev,
175                                   struct ff_effect *effect)
176 {
177         struct hid_ff_pid* pid_private  = (struct hid_ff_pid*)(dev->private);
178         int ret;
179         int is_update;
180         unsigned long flags = 0;
181
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");
186                 return -EINVAL;
187         }
188
189         /*
190          * If we want to create a new effect, get a free id
191          */
192         if (effect->id == -1) {
193                 int id=0;
194
195                 // Spinlock so we don`t get a race condition when choosing IDs
196                 spin_lock_irqsave(&pid_private->lock, flags);
197
198                 while(id < FF_EFFECTS_MAX)
199                         if (!test_and_set_bit(FF_PID_FLAGS_USED, &pid_private->effects[id++].flags)) 
200                             break;
201
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");
206                         return -ENOMEM;
207                 }
208
209                 effect->id = id;
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);
214
215                 is_update = FF_PID_FALSE;
216         }
217         else {
218                 /* We want to update an effect */
219                 if (!CHECK_OWNERSHIP(effect->id, pid_private))
220                         return -EACCES;
221
222                 /* Parameter type cannot be updated */
223                 if (effect->type != pid_private->effects[effect->id].effect.type)
224                         return -EINVAL;
225
226                 /* Check the effect is not already being updated */
227                 if (test_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags)) {
228                         return -EAGAIN;
229                 }
230
231                 is_update = FF_PID_TRUE;
232         }
233
234         /*
235          * Upload the effect
236          */
237         switch (effect->type) {
238                 case FF_PERIODIC:
239                         ret = pid_upload_periodic(pid_private, effect, is_update);
240                         break;
241
242                 case FF_CONSTANT:
243                         ret = pid_upload_constant(pid_private, effect, is_update);
244                         break;
245
246                 case FF_SPRING:
247                 case FF_FRICTION:
248                 case FF_DAMPER:
249                 case FF_INERTIA:
250                         ret = pid_upload_condition(pid_private, effect, is_update);
251                         break;
252
253                 case FF_RAMP:
254                         ret = pid_upload_ramp(pid_private, effect, is_update);
255                         break;
256
257                 default:
258                         dev_dbg(&pid_private->hid->dev->dev, "invalid type of effect requested - %x.\n", effect->type);
259                         return -EINVAL;
260         }
261         /* If a packet was sent, forbid new updates until we are notified
262          * that the packet was updated
263          */
264         if (ret == 0)
265                 set_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags);
266         pid_private->effects[effect->id].effect = *effect;
267         return ret;
268 }
269
270 int hid_pid_init(struct hid_device *hid)
271 {
272     struct hid_ff_pid *private;
273     struct hid_input *hidinput = list_entry(&hid->inputs, struct hid_input, list);
274
275     private = hid->ff_private = kmalloc(sizeof(struct hid_ff_pid), GFP_KERNEL);
276     if (!private) return -1;
277     
278     memset(private,0,sizeof(struct hid_ff_pid));
279
280     hid->ff_private = private; /* 'cause memset can move the block away */
281
282     private->hid = hid;
283     
284     hid->ff_exit = hid_pid_exit;
285     hid->ff_event = hid_pid_event;
286     
287     /* Open output URB */
288     if (!(private->urbffout = usb_alloc_urb(0, GFP_KERNEL))) {
289         kfree(private);
290         return -1;
291     }
292
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);
299
300     spin_lock_init(&private->lock);
301
302     printk(KERN_INFO "Force feedback driver for PID devices by Rodrigo Damazio <rdamazio@lsi.usp.br>.\n");
303     
304     return 0;    
305 }