ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / input / hid-lgff.c
1 /*
2  * $$
3  *
4  * Force feedback support for hid-compliant for some of the devices from
5  * Logitech, namely:
6  * - WingMan Cordless RumblePad
7  * - WingMan Force 3D
8  *
9  *  Copyright (c) 2002-2004 Johann Deneux
10  */
11
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26  *
27  * Should you need to contact me, the author, you can do so by
28  * e-mail - mail your message to <johann.deneux@it.uu.se>
29  */
30
31 #include <linux/input.h>
32 #include <linux/sched.h>
33
34 //#define DEBUG
35 #include <linux/usb.h>
36
37 #include <linux/circ_buf.h>
38
39 #include "hid.h"
40 #include "fixp-arith.h"
41
42
43 /* Periodicity of the update */
44 #define PERIOD (HZ/10)
45
46 #define RUN_AT(t) (jiffies + (t))
47
48 /* Effect status */
49 #define EFFECT_STARTED 0     /* Effect is going to play after some time
50                                 (ff_replay.delay) */
51 #define EFFECT_PLAYING 1     /* Effect is being played */
52 #define EFFECT_USED    2
53
54 // For lgff_device::flags
55 #define DEVICE_CLOSING 0     /* The driver is being unitialised */
56
57 /* Check that the current process can access an effect */
58 #define CHECK_OWNERSHIP(effect) (current->pid == 0 \
59         || effect.owner == current->pid)
60
61 #define LGFF_CHECK_OWNERSHIP(i, l) \
62         (i>=0 && i<LGFF_EFFECTS \
63         && test_bit(EFFECT_USED, l->effects[i].flags) \
64         && CHECK_OWNERSHIP(l->effects[i]))
65
66 #define LGFF_EFFECTS 8
67
68 struct device_type {
69         u16 idVendor;
70         u16 idProduct;
71         signed short *ff;
72 };
73
74 struct lgff_effect {
75         pid_t owner;
76
77         struct ff_effect effect;
78
79         unsigned long flags[1];
80         unsigned int count;          /* Number of times left to play */
81         unsigned long started_at;    /* When the effect started to play */
82 };
83
84 struct lgff_device {
85         struct hid_device* hid;
86
87         struct hid_report* constant;
88         struct hid_report* rumble;
89         struct hid_report* condition;
90
91         struct lgff_effect effects[LGFF_EFFECTS];
92         spinlock_t lock;             /* device-level lock. Having locks on
93                                         a per-effect basis could be nice, but
94                                         isn't really necessary */
95
96         unsigned long flags[1];      /* Contains various information about the
97                                         state of the driver for this device */  
98
99         struct timer_list timer;
100 };
101
102 /* Callbacks */
103 static void hid_lgff_exit(struct hid_device* hid);
104 static int hid_lgff_event(struct hid_device *hid, struct input_dev *input,
105                           unsigned int type, unsigned int code, int value);
106 static int hid_lgff_flush(struct input_dev *input, struct file *file);
107 static int hid_lgff_upload_effect(struct input_dev *input,
108                                   struct ff_effect *effect);
109 static int hid_lgff_erase(struct input_dev *input, int id);
110
111 /* Local functions */
112 static void hid_lgff_input_init(struct hid_device* hid);
113 static void hid_lgff_timer(unsigned long timer_data);
114 static struct hid_report* hid_lgff_duplicate_report(struct hid_report*);
115 static void hid_lgff_delete_report(struct hid_report*);
116
117 static signed short ff_rumble[] = {
118         FF_RUMBLE,
119         -1
120 };
121
122 static signed short ff_joystick[] = {
123         FF_CONSTANT,
124         -1
125 };
126
127 static struct device_type devices[] = {
128         {0x046d, 0xc211, ff_rumble},
129         {0x046d, 0xc283, ff_joystick},
130         {0x0000, 0x0000, ff_joystick}
131 };
132
133 int hid_lgff_init(struct hid_device* hid)
134 {
135         struct lgff_device *private;
136         struct hid_report* report;
137         struct hid_field* field;
138
139         /* Find the report to use */
140         if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
141                 err("No output report found");
142                 return -1;
143         }
144         /* Check that the report looks ok */
145         report = (struct hid_report*)hid->report_enum[HID_OUTPUT_REPORT].report_list.next;
146         if (!report) {
147                 err("NULL output report");
148                 return -1;
149         }
150         field = report->field[0];
151         if (!field) {
152                 err("NULL field");
153                 return -1;
154         }
155
156         private = kmalloc(sizeof(struct lgff_device), GFP_KERNEL);
157         if (!private)
158                 return -1;
159         memset(private, 0, sizeof(struct lgff_device));
160         hid->ff_private = private;
161
162         /* Input init */
163         hid_lgff_input_init(hid);
164
165
166         private->constant = hid_lgff_duplicate_report(report);
167         if (!private->constant) {
168                 kfree(private);
169                 return -1;
170         }
171         private->constant->field[0]->value[0] = 0x51;
172         private->constant->field[0]->value[1] = 0x08;
173         private->constant->field[0]->value[2] = 0x7f;
174         private->constant->field[0]->value[3] = 0x7f;
175
176         private->rumble = hid_lgff_duplicate_report(report);
177         if (!private->rumble) {
178                 hid_lgff_delete_report(private->constant);
179                 kfree(private);
180                 return -1;
181         }
182         private->rumble->field[0]->value[0] = 0x42;
183
184
185         private->condition = hid_lgff_duplicate_report(report);
186         if (!private->condition) {
187                 hid_lgff_delete_report(private->rumble);
188                 hid_lgff_delete_report(private->constant);
189                 kfree(private);
190                 return -1;
191         }
192
193         private->hid = hid;
194
195         spin_lock_init(&private->lock);
196         init_timer(&private->timer);
197         private->timer.data = (unsigned long)private;
198         private->timer.function = hid_lgff_timer;
199
200         /* Event and exit callbacks */
201         hid->ff_exit = hid_lgff_exit;
202         hid->ff_event = hid_lgff_event;
203
204         /* Start the update task */
205         private->timer.expires = RUN_AT(PERIOD);
206         add_timer(&private->timer);  /*TODO: only run the timer when at least
207                                        one effect is playing */
208
209         printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
210
211         return 0;
212 }
213
214 static struct hid_report* hid_lgff_duplicate_report(struct hid_report* report)
215 {
216         struct hid_report* ret;
217
218         ret = kmalloc(sizeof(struct lgff_device), GFP_KERNEL);
219         if (!ret)
220                 return NULL;
221         *ret = *report;
222
223         ret->field[0] = kmalloc(sizeof(struct hid_field), GFP_KERNEL);
224         if (!ret->field[0]) {
225                 kfree(ret);
226                 return NULL;
227         }
228         *ret->field[0] = *report->field[0];
229
230         ret->field[0]->value = kmalloc(sizeof(s32[8]), GFP_KERNEL);
231         if (!ret->field[0]->value) {
232                 kfree(ret->field[0]);
233                 kfree(ret);
234                 return NULL;
235         }
236         memset(ret->field[0]->value, 0, sizeof(s32[8]));        
237
238         return ret;
239 }
240
241 static void hid_lgff_delete_report(struct hid_report* report)
242 {
243         if (report) {
244                 kfree(report->field[0]->value);
245                 kfree(report->field[0]);
246                 kfree(report);
247         }
248 }
249
250 static void hid_lgff_input_init(struct hid_device* hid)
251 {
252         struct device_type* dev = devices;
253         signed short* ff;
254         u16 idVendor = hid->dev->descriptor.idVendor;
255         u16 idProduct = hid->dev->descriptor.idProduct;
256         struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
257
258         while (dev->idVendor && (idVendor != dev->idVendor || idProduct != dev->idProduct))
259                 dev++;
260
261         ff = dev->ff;
262
263         while (*ff >= 0) {
264                 set_bit(*ff, hidinput->input.ffbit);
265                 ++ff;
266         }
267
268         hidinput->input.upload_effect = hid_lgff_upload_effect;
269         hidinput->input.flush = hid_lgff_flush;
270
271         set_bit(EV_FF, hidinput->input.evbit);
272         hidinput->input.ff_effects_max = LGFF_EFFECTS;
273 }
274
275 static void hid_lgff_exit(struct hid_device* hid)
276 {
277         struct lgff_device *lgff = hid->ff_private;
278
279         set_bit(DEVICE_CLOSING, lgff->flags);
280         del_timer_sync(&lgff->timer);
281
282         hid_lgff_delete_report(lgff->condition);
283         hid_lgff_delete_report(lgff->rumble);
284         hid_lgff_delete_report(lgff->constant);
285
286         kfree(lgff);
287 }
288
289 static int hid_lgff_event(struct hid_device *hid, struct input_dev* input,
290                           unsigned int type, unsigned int code, int value)
291 {
292         struct lgff_device *lgff = hid->ff_private;
293         struct lgff_effect *effect = lgff->effects + code;
294         unsigned long flags;
295
296         if (type != EV_FF)                     return -EINVAL;
297         if (!LGFF_CHECK_OWNERSHIP(code, lgff)) return -EACCES;
298         if (value < 0)                         return -EINVAL;
299
300         spin_lock_irqsave(&lgff->lock, flags);
301         
302         if (value > 0) {
303                 if (test_bit(EFFECT_STARTED, effect->flags)) {
304                         spin_unlock_irqrestore(&lgff->lock, flags);
305                         return -EBUSY;
306                 }
307                 if (test_bit(EFFECT_PLAYING, effect->flags)) {
308                         spin_unlock_irqrestore(&lgff->lock, flags);
309                         return -EBUSY;
310                 }
311
312                 effect->count = value;
313
314                 if (effect->effect.replay.delay) {
315                         set_bit(EFFECT_STARTED, effect->flags);
316                 } else {
317                         set_bit(EFFECT_PLAYING, effect->flags);
318                 }
319                 effect->started_at = jiffies;
320         }
321         else { /* value == 0 */
322                 clear_bit(EFFECT_STARTED, effect->flags);
323                 clear_bit(EFFECT_PLAYING, effect->flags);
324         }
325
326         spin_unlock_irqrestore(&lgff->lock, flags);
327
328         return 0;
329
330 }
331
332 /* Erase all effects this process owns */
333 static int hid_lgff_flush(struct input_dev *dev, struct file *file)
334 {
335         struct hid_device *hid = dev->private;
336         struct lgff_device *lgff = hid->ff_private;
337         int i;
338
339         for (i=0; i<dev->ff_effects_max; ++i) {
340
341                 /*NOTE: no need to lock here. The only times EFFECT_USED is
342                   modified is when effects are uploaded or when an effect is
343                   erased. But a process cannot close its dev/input/eventX fd
344                   and perform ioctls on the same fd all at the same time */
345                 if ( current->pid == lgff->effects[i].owner
346                      && test_bit(EFFECT_USED, lgff->effects[i].flags)) {
347                         
348                         if (hid_lgff_erase(dev, i))
349                                 warn("erase effect %d failed", i);
350                 }
351
352         }
353
354         return 0;
355 }
356
357 static int hid_lgff_erase(struct input_dev *dev, int id)
358 {
359         struct hid_device *hid = dev->private;
360         struct lgff_device *lgff = hid->ff_private;
361         unsigned long flags;
362
363         if (!LGFF_CHECK_OWNERSHIP(id, lgff)) return -EACCES;
364
365         spin_lock_irqsave(&lgff->lock, flags);
366         lgff->effects[id].flags[0] = 0;
367         spin_unlock_irqrestore(&lgff->lock, flags);
368
369         return 0;
370 }
371
372 static int hid_lgff_upload_effect(struct input_dev* input,
373                                   struct ff_effect* effect)
374 {
375         struct hid_device *hid = input->private;
376         struct lgff_device *lgff = hid->ff_private;
377         struct lgff_effect new;
378         int id;
379         unsigned long flags;
380         
381         dbg("ioctl rumble");
382
383         if (!test_bit(effect->type, input->ffbit)) return -EINVAL;
384
385         spin_lock_irqsave(&lgff->lock, flags);
386
387         if (effect->id == -1) {
388                 int i;
389
390                 for (i=0; i<LGFF_EFFECTS && test_bit(EFFECT_USED, lgff->effects[i].flags); ++i);
391                 if (i >= LGFF_EFFECTS) {
392                         spin_unlock_irqrestore(&lgff->lock, flags);
393                         return -ENOSPC;
394                 }
395
396                 effect->id = i;
397                 lgff->effects[i].owner = current->pid;
398                 lgff->effects[i].flags[0] = 0;
399                 set_bit(EFFECT_USED, lgff->effects[i].flags);
400         }
401         else if (!LGFF_CHECK_OWNERSHIP(effect->id, lgff)) {
402                 spin_unlock_irqrestore(&lgff->lock, flags);
403                 return -EACCES;
404         }
405
406         id = effect->id;
407         new = lgff->effects[id];
408
409         new.effect = *effect;
410
411         if (test_bit(EFFECT_STARTED, lgff->effects[id].flags)
412             || test_bit(EFFECT_STARTED, lgff->effects[id].flags)) {
413
414                 /* Changing replay parameters is not allowed (for the time
415                    being) */
416                 if (new.effect.replay.delay != lgff->effects[id].effect.replay.delay
417                     || new.effect.replay.length != lgff->effects[id].effect.replay.length) {
418                         spin_unlock_irqrestore(&lgff->lock, flags);
419                         return -ENOSYS;
420                 }
421
422                 lgff->effects[id] = new;
423
424         } else {
425                 lgff->effects[id] = new;
426         }
427
428         spin_unlock_irqrestore(&lgff->lock, flags);
429         return 0;
430 }
431
432 static void hid_lgff_timer(unsigned long timer_data)
433 {
434         struct lgff_device *lgff = (struct lgff_device*)timer_data;
435         struct hid_device *hid = lgff->hid;
436         unsigned long flags;
437         int x = 0x7f, y = 0x7f;   // Coordinates of constant effects
438         unsigned int left = 0, right = 0;   // Rumbling
439         int i;
440
441         spin_lock_irqsave(&lgff->lock, flags);
442
443         for (i=0; i<LGFF_EFFECTS; ++i) {
444                 struct lgff_effect* effect = lgff->effects +i;
445
446                 if (test_bit(EFFECT_PLAYING, effect->flags)) {
447
448                         switch (effect->effect.type) {
449                         case FF_CONSTANT: {
450                                 //TODO: handle envelopes
451                                 int degrees = effect->effect.direction * 360 >> 16;
452                                 x += fixp_mult(fixp_sin(degrees),
453                                                fixp_new16(effect->effect.u.constant.level));
454                                 y += fixp_mult(-fixp_cos(degrees),
455                                                fixp_new16(effect->effect.u.constant.level));
456                         }       break;
457                         case FF_RUMBLE:
458                                 right += effect->effect.u.rumble.strong_magnitude;
459                                 left += effect->effect.u.rumble.weak_magnitude;
460                                 break;
461                         };
462
463                         /* One run of the effect is finished playing */
464                         if (time_after(jiffies,
465                                         effect->started_at
466                                         + effect->effect.replay.delay*HZ/1000
467                                         + effect->effect.replay.length*HZ/1000)) {
468                                 dbg("Finished playing once %d", i);
469                                 if (--effect->count <= 0) {
470                                         dbg("Stopped %d", i);
471                                         clear_bit(EFFECT_PLAYING, effect->flags);
472                                 }
473                                 else {
474                                         dbg("Start again %d", i);
475                                         if (effect->effect.replay.length != 0) {
476                                                 clear_bit(EFFECT_PLAYING, effect->flags);
477                                                 set_bit(EFFECT_STARTED, effect->flags);
478                                         }
479                                         effect->started_at = jiffies;
480                                 }
481                         }
482
483                 } else if (test_bit(EFFECT_STARTED, lgff->effects[i].flags)) {
484                         /* Check if we should start playing the effect */
485                         if (time_after(jiffies,
486                                         lgff->effects[i].started_at
487                                         + lgff->effects[i].effect.replay.delay*HZ/1000)) {
488                                 dbg("Now playing %d", i);
489                                 clear_bit(EFFECT_STARTED, lgff->effects[i].flags);
490                                 set_bit(EFFECT_PLAYING, lgff->effects[i].flags);
491                         }
492                 }
493         }
494
495 #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
496
497         // Clamp values
498         CLAMP(x);
499         CLAMP(y);
500         CLAMP(left);
501         CLAMP(right);
502
503 #undef CLAMP
504
505         if (x != lgff->constant->field[0]->value[2]
506             || y != lgff->constant->field[0]->value[3]) {
507                 lgff->constant->field[0]->value[2] = x;
508                 lgff->constant->field[0]->value[3] = y;
509                 dbg("(x,y)=(%04x, %04x)", x, y);
510                 hid_submit_report(hid, lgff->constant, USB_DIR_OUT);
511         }
512
513         if (left != lgff->rumble->field[0]->value[2]
514             || right != lgff->rumble->field[0]->value[3]) {
515                 lgff->rumble->field[0]->value[2] = left;
516                 lgff->rumble->field[0]->value[3] = right;
517                 dbg("(left,right)=(%04x, %04x)", left, right);
518                 hid_submit_report(hid, lgff->rumble, USB_DIR_OUT);
519         }
520
521         if (!test_bit(DEVICE_CLOSING, lgff->flags)) {
522                 lgff->timer.expires = RUN_AT(PERIOD);
523                 add_timer(&lgff->timer);
524         }
525
526         spin_unlock_irqrestore(&lgff->lock, flags);
527 }