MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / input / pid.c
bloba38b0db8dcea13131f9b644af7df535afcca2d33
1 /*
2 * PID Force feedback support for hid devices.
4 * Copyright (c) 2002 Rodrigo Damazio.
5 * Portions by Johann Deneux and Bjorn Augustson
6 */
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.
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>
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"
40 #define DEBUG
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");
66 return 0;
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;
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;
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;
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);
89 if (type != EV_FF)
90 return -1;
94 return 0;
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)
101 if (play) {
102 set_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
104 } else {
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;
116 unsigned long flags;
117 unsigned wanted_report = HID_UP_PID | FF_PID_USAGE_BLOCK_FREE; /* PID Block Free Report */
118 int ret;
120 if (!CHECK_OWNERSHIP(id, pid))
121 return -EACCES;
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;
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;
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;
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);
150 return ret;
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;
158 int i;
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);
170 return 0;
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);
178 int ret;
179 int is_update;
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");
186 return -EINVAL;
190 * If we want to create a new effect, get a free id
192 if (effect->id == -1) {
193 int id=0;
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))
200 break;
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;
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);
215 is_update = FF_PID_FALSE;
217 else {
218 /* We want to update an effect */
219 if (!CHECK_OWNERSHIP(effect->id, pid_private))
220 return -EACCES;
222 /* Parameter type cannot be updated */
223 if (effect->type != pid_private->effects[effect->id].effect.type)
224 return -EINVAL;
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;
231 is_update = FF_PID_TRUE;
235 * Upload the effect
237 switch (effect->type) {
238 case FF_PERIODIC:
239 ret = pid_upload_periodic(pid_private, effect, is_update);
240 break;
242 case FF_CONSTANT:
243 ret = pid_upload_constant(pid_private, effect, is_update);
244 break;
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;
253 case FF_RAMP:
254 ret = pid_upload_ramp(pid_private, effect, is_update);
255 break;
257 default:
258 dev_dbg(&pid_private->hid->dev->dev, "invalid type of effect requested - %x.\n", effect->type);
259 return -EINVAL;
261 /* If a packet was sent, forbid new updates until we are notified
262 * that the packet was updated
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;
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 */
282 private->hid = hid;
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))) {
289 kfree(private);
290 return -1;
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");
304 return 0;