aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorAnssi Hannula <anssi.hannula@gmail.com>2006-07-19 01:40:55 -0400
committerDmitry Torokhov <dtor@insightbb.com>2006-07-19 01:40:55 -0400
commitdc76c912145febae8b62746d6f93e5edae342c9d (patch)
tree21c53d43c22d90e0cad8f87882b49f618e56a36a /drivers/usb
parent224ee88fe39564358ec99b46bf3ee6e6999ae17d (diff)
Input: use new FF interface in the HID force feedback drivers
Signed-off-by: Anssi Hannula <anssi.hannula@gmail.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/input/Kconfig2
-rw-r--r--drivers/usb/input/hid-core.c2
-rw-r--r--drivers/usb/input/hid-input.c2
-rw-r--r--drivers/usb/input/hid-lgff.c523
-rw-r--r--drivers/usb/input/hid-tmff.c399
-rw-r--r--drivers/usb/input/hid.h19
6 files changed, 116 insertions, 831 deletions
diff --git a/drivers/usb/input/Kconfig b/drivers/usb/input/Kconfig
index 7ec22c8decc8..3ca64c3cada3 100644
--- a/drivers/usb/input/Kconfig
+++ b/drivers/usb/input/Kconfig
@@ -70,6 +70,7 @@ config HID_PID
70config LOGITECH_FF 70config LOGITECH_FF
71 bool "Logitech WingMan *3D support" 71 bool "Logitech WingMan *3D support"
72 depends on HID_FF 72 depends on HID_FF
73 select INPUT_FF_MEMLESS if USB_HID
73 help 74 help
74 Say Y here if you have one of these devices: 75 Say Y here if you have one of these devices:
75 - Logitech WingMan Cordless RumblePad 76 - Logitech WingMan Cordless RumblePad
@@ -81,6 +82,7 @@ config LOGITECH_FF
81config THRUSTMASTER_FF 82config THRUSTMASTER_FF
82 bool "ThrustMaster FireStorm Dual Power 2 support (EXPERIMENTAL)" 83 bool "ThrustMaster FireStorm Dual Power 2 support (EXPERIMENTAL)"
83 depends on HID_FF && EXPERIMENTAL 84 depends on HID_FF && EXPERIMENTAL
85 select INPUT_FF_MEMLESS if USB_HID
84 help 86 help
85 Say Y here if you have a THRUSTMASTER FireStore Dual Power 2, 87 Say Y here if you have a THRUSTMASTER FireStore Dual Power 2,
86 and want to enable force feedback support for it. 88 and want to enable force feedback support for it.
diff --git a/drivers/usb/input/hid-core.c b/drivers/usb/input/hid-core.c
index b9fb9687f926..2f477a8badab 100644
--- a/drivers/usb/input/hid-core.c
+++ b/drivers/usb/input/hid-core.c
@@ -543,8 +543,6 @@ static void hid_free_device(struct hid_device *device)
543{ 543{
544 unsigned i,j; 544 unsigned i,j;
545 545
546 hid_ff_exit(device);
547
548 for (i = 0; i < HID_REPORT_TYPES; i++) { 546 for (i = 0; i < HID_REPORT_TYPES; i++) {
549 struct hid_report_enum *report_enum = device->report_enum + i; 547 struct hid_report_enum *report_enum = device->report_enum + i;
550 548
diff --git a/drivers/usb/input/hid-input.c b/drivers/usb/input/hid-input.c
index 37b9ea8ec3b3..4c62afbeb430 100644
--- a/drivers/usb/input/hid-input.c
+++ b/drivers/usb/input/hid-input.c
@@ -729,7 +729,7 @@ static int hidinput_input_event(struct input_dev *dev, unsigned int type, unsign
729 int offset; 729 int offset;
730 730
731 if (type == EV_FF) 731 if (type == EV_FF)
732 return hid_ff_event(hid, dev, type, code, value); 732 return input_ff_event(dev, type, code, value);
733 733
734 if (type != EV_LED) 734 if (type != EV_LED)
735 return -1; 735 return -1;
diff --git a/drivers/usb/input/hid-lgff.c b/drivers/usb/input/hid-lgff.c
index 7e264a8c81b1..93da222b6da8 100644
--- a/drivers/usb/input/hid-lgff.c
+++ b/drivers/usb/input/hid-lgff.c
@@ -1,12 +1,11 @@
1/* 1/*
2 * $$
3 *
4 * Force feedback support for hid-compliant for some of the devices from 2 * Force feedback support for hid-compliant for some of the devices from
5 * Logitech, namely: 3 * Logitech, namely:
6 * - WingMan Cordless RumblePad 4 * - WingMan Cordless RumblePad
7 * - WingMan Force 3D 5 * - WingMan Force 3D
8 * 6 *
9 * Copyright (c) 2002-2004 Johann Deneux 7 * Copyright (c) 2002-2004 Johann Deneux
8 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
10 */ 9 */
11 10
12/* 11/*
@@ -29,495 +28,117 @@
29 */ 28 */
30 29
31#include <linux/input.h> 30#include <linux/input.h>
32#include <linux/sched.h>
33
34//#define DEBUG
35#include <linux/usb.h> 31#include <linux/usb.h>
36
37#include <linux/circ_buf.h>
38
39#include "hid.h" 32#include "hid.h"
40#include "../../input/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 33
68struct device_type { 34struct device_type {
69 u16 idVendor; 35 u16 idVendor;
70 u16 idProduct; 36 u16 idProduct;
71 signed short *ff; 37 const signed short *ff;
72};
73
74struct 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
84struct 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}; 38};
101 39
102/* Callbacks */ 40static const signed short ff_rumble[] = {
103static void hid_lgff_exit(struct hid_device* hid);
104static int hid_lgff_event(struct hid_device *hid, struct input_dev *input,
105 unsigned int type, unsigned int code, int value);
106static int hid_lgff_flush(struct input_dev *input, struct file *file);
107static int hid_lgff_upload_effect(struct input_dev *input,
108 struct ff_effect *effect);
109static int hid_lgff_erase(struct input_dev *input, int id);
110
111/* Local functions */
112static void hid_lgff_input_init(struct hid_device* hid);
113static void hid_lgff_timer(unsigned long timer_data);
114static struct hid_report* hid_lgff_duplicate_report(struct hid_report*);
115static void hid_lgff_delete_report(struct hid_report*);
116
117static signed short ff_rumble[] = {
118 FF_RUMBLE, 41 FF_RUMBLE,
119 -1 42 -1
120}; 43};
121 44
122static signed short ff_joystick[] = { 45static const signed short ff_joystick[] = {
123 FF_CONSTANT, 46 FF_CONSTANT,
124 -1 47 -1
125}; 48};
126 49
127static struct device_type devices[] = { 50static const struct device_type devices[] = {
128 {0x046d, 0xc211, ff_rumble}, 51 { 0x046d, 0xc211, ff_rumble },
129 {0x046d, 0xc219, ff_rumble}, 52 { 0x046d, 0xc219, ff_rumble },
130 {0x046d, 0xc283, ff_joystick}, 53 { 0x046d, 0xc283, ff_joystick },
131 {0x0000, 0x0000, ff_joystick} 54 { 0x0000, 0x0000, ff_joystick }
132}; 55};
133 56
57static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
58{
59 struct hid_device *hid = dev->private;
60 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
61 struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
62 int x, y;
63 unsigned int left, right;
64
65#define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
66
67 switch (effect->type) {
68 case FF_CONSTANT:
69 x = effect->u.ramp.start_level + 0x7f; /* 0x7f is center */
70 y = effect->u.ramp.end_level + 0x7f;
71 CLAMP(x);
72 CLAMP(y);
73 report->field[0]->value[0] = 0x51;
74 report->field[0]->value[1] = 0x08;
75 report->field[0]->value[2] = x;
76 report->field[0]->value[3] = y;
77 dbg("(x, y)=(%04x, %04x)", x, y);
78 hid_submit_report(hid, report, USB_DIR_OUT);
79 break;
80
81 case FF_RUMBLE:
82 right = effect->u.rumble.strong_magnitude;
83 left = effect->u.rumble.weak_magnitude;
84 right = right * 0xff / 0xffff;
85 left = left * 0xff / 0xffff;
86 CLAMP(left);
87 CLAMP(right);
88 report->field[0]->value[0] = 0x42;
89 report->field[0]->value[1] = 0x00;
90 report->field[0]->value[2] = left;
91 report->field[0]->value[3] = right;
92 dbg("(left, right)=(%04x, %04x)", left, right);
93 hid_submit_report(hid, report, USB_DIR_OUT);
94 break;
95 }
96 return 0;
97}
98
134int hid_lgff_init(struct hid_device* hid) 99int hid_lgff_init(struct hid_device* hid)
135{ 100{
136 struct lgff_device *private; 101 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
137 struct hid_report* report; 102 struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
138 struct hid_field* field; 103 struct input_dev *dev = hidinput->input;
104 struct hid_report *report;
105 struct hid_field *field;
106 int error;
107 int i, j;
139 108
140 /* Find the report to use */ 109 /* Find the report to use */
141 if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) { 110 if (list_empty(report_list)) {
142 err("No output report found"); 111 err("No output report found");
143 return -1; 112 return -1;
144 } 113 }
114
145 /* Check that the report looks ok */ 115 /* Check that the report looks ok */
146 report = (struct hid_report*)hid->report_enum[HID_OUTPUT_REPORT].report_list.next; 116 report = list_entry(report_list->next, struct hid_report, list);
147 if (!report) { 117 if (!report) {
148 err("NULL output report"); 118 err("NULL output report");
149 return -1; 119 return -1;
150 } 120 }
121
151 field = report->field[0]; 122 field = report->field[0];
152 if (!field) { 123 if (!field) {
153 err("NULL field"); 124 err("NULL field");
154 return -1; 125 return -1;
155 } 126 }
156 127
157 private = kzalloc(sizeof(struct lgff_device), GFP_KERNEL); 128 for (i = 0; i < ARRAY_SIZE(devices); i++) {
158 if (!private) 129 if (dev->id.vendor == devices[i].idVendor &&
159 return -1; 130 dev->id.product == devices[i].idProduct) {
160 hid->ff_private = private; 131 for (j = 0; devices[i].ff[j] >= 0; j++)
161 132 set_bit(devices[i].ff[j], dev->ffbit);
162 /* Input init */ 133 break;
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
214static 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 = kzalloc(sizeof(s32[8]), GFP_KERNEL);
231 if (!ret->field[0]->value) {
232 kfree(ret->field[0]);
233 kfree(ret);
234 return NULL;
235 }
236
237 return ret;
238}
239
240static void hid_lgff_delete_report(struct hid_report* report)
241{
242 if (report) {
243 kfree(report->field[0]->value);
244 kfree(report->field[0]);
245 kfree(report);
246 }
247}
248
249static void hid_lgff_input_init(struct hid_device* hid)
250{
251 struct device_type* dev = devices;
252 signed short* ff;
253 u16 idVendor = le16_to_cpu(hid->dev->descriptor.idVendor);
254 u16 idProduct = le16_to_cpu(hid->dev->descriptor.idProduct);
255 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
256 struct input_dev *input_dev = hidinput->input;
257
258 while (dev->idVendor && (idVendor != dev->idVendor || idProduct != dev->idProduct))
259 dev++;
260
261 for (ff = dev->ff; *ff >= 0; ff++)
262 set_bit(*ff, input_dev->ffbit);
263
264 input_dev->upload_effect = hid_lgff_upload_effect;
265 input_dev->flush = hid_lgff_flush;
266
267 set_bit(EV_FF, input_dev->evbit);
268 input_dev->ff_effects_max = LGFF_EFFECTS;
269}
270
271static void hid_lgff_exit(struct hid_device* hid)
272{
273 struct lgff_device *lgff = hid->ff_private;
274
275 set_bit(DEVICE_CLOSING, lgff->flags);
276 del_timer_sync(&lgff->timer);
277
278 hid_lgff_delete_report(lgff->condition);
279 hid_lgff_delete_report(lgff->rumble);
280 hid_lgff_delete_report(lgff->constant);
281
282 kfree(lgff);
283}
284
285static int hid_lgff_event(struct hid_device *hid, struct input_dev* input,
286 unsigned int type, unsigned int code, int value)
287{
288 struct lgff_device *lgff = hid->ff_private;
289 struct lgff_effect *effect = lgff->effects + code;
290 unsigned long flags;
291
292 if (type != EV_FF) return -EINVAL;
293 if (!LGFF_CHECK_OWNERSHIP(code, lgff)) return -EACCES;
294 if (value < 0) return -EINVAL;
295
296 spin_lock_irqsave(&lgff->lock, flags);
297
298 if (value > 0) {
299 if (test_bit(EFFECT_STARTED, effect->flags)) {
300 spin_unlock_irqrestore(&lgff->lock, flags);
301 return -EBUSY;
302 }
303 if (test_bit(EFFECT_PLAYING, effect->flags)) {
304 spin_unlock_irqrestore(&lgff->lock, flags);
305 return -EBUSY;
306 }
307
308 effect->count = value;
309
310 if (effect->effect.replay.delay) {
311 set_bit(EFFECT_STARTED, effect->flags);
312 } else {
313 set_bit(EFFECT_PLAYING, effect->flags);
314 }
315 effect->started_at = jiffies;
316 }
317 else { /* value == 0 */
318 clear_bit(EFFECT_STARTED, effect->flags);
319 clear_bit(EFFECT_PLAYING, effect->flags);
320 }
321
322 spin_unlock_irqrestore(&lgff->lock, flags);
323
324 return 0;
325
326}
327
328/* Erase all effects this process owns */
329static int hid_lgff_flush(struct input_dev *dev, struct file *file)
330{
331 struct hid_device *hid = dev->private;
332 struct lgff_device *lgff = hid->ff_private;
333 int i;
334
335 for (i=0; i<dev->ff_effects_max; ++i) {
336
337 /*NOTE: no need to lock here. The only times EFFECT_USED is
338 modified is when effects are uploaded or when an effect is
339 erased. But a process cannot close its dev/input/eventX fd
340 and perform ioctls on the same fd all at the same time */
341 if ( current->pid == lgff->effects[i].owner
342 && test_bit(EFFECT_USED, lgff->effects[i].flags)) {
343
344 if (hid_lgff_erase(dev, i))
345 warn("erase effect %d failed", i);
346 }
347
348 }
349
350 return 0;
351}
352
353static int hid_lgff_erase(struct input_dev *dev, int id)
354{
355 struct hid_device *hid = dev->private;
356 struct lgff_device *lgff = hid->ff_private;
357 unsigned long flags;
358
359 if (!LGFF_CHECK_OWNERSHIP(id, lgff)) return -EACCES;
360
361 spin_lock_irqsave(&lgff->lock, flags);
362 lgff->effects[id].flags[0] = 0;
363 spin_unlock_irqrestore(&lgff->lock, flags);
364
365 return 0;
366}
367
368static int hid_lgff_upload_effect(struct input_dev* input,
369 struct ff_effect* effect)
370{
371 struct hid_device *hid = input->private;
372 struct lgff_device *lgff = hid->ff_private;
373 struct lgff_effect new;
374 int id;
375 unsigned long flags;
376
377 dbg("ioctl rumble");
378
379 if (!test_bit(effect->type, input->ffbit)) return -EINVAL;
380
381 spin_lock_irqsave(&lgff->lock, flags);
382
383 if (effect->id == -1) {
384 int i;
385
386 for (i=0; i<LGFF_EFFECTS && test_bit(EFFECT_USED, lgff->effects[i].flags); ++i);
387 if (i >= LGFF_EFFECTS) {
388 spin_unlock_irqrestore(&lgff->lock, flags);
389 return -ENOSPC;
390 } 134 }
391
392 effect->id = i;
393 lgff->effects[i].owner = current->pid;
394 lgff->effects[i].flags[0] = 0;
395 set_bit(EFFECT_USED, lgff->effects[i].flags);
396 } 135 }
397 else if (!LGFF_CHECK_OWNERSHIP(effect->id, lgff)) {
398 spin_unlock_irqrestore(&lgff->lock, flags);
399 return -EACCES;
400 }
401
402 id = effect->id;
403 new = lgff->effects[id];
404
405 new.effect = *effect;
406
407 if (test_bit(EFFECT_STARTED, lgff->effects[id].flags)
408 || test_bit(EFFECT_STARTED, lgff->effects[id].flags)) {
409
410 /* Changing replay parameters is not allowed (for the time
411 being) */
412 if (new.effect.replay.delay != lgff->effects[id].effect.replay.delay
413 || new.effect.replay.length != lgff->effects[id].effect.replay.length) {
414 spin_unlock_irqrestore(&lgff->lock, flags);
415 return -ENOSYS;
416 }
417 136
418 lgff->effects[id] = new; 137 error = input_ff_create_memless(dev, NULL, hid_lgff_play);
138 if (error)
139 return error;
419 140
420 } else { 141 printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
421 lgff->effects[id] = new;
422 }
423 142
424 spin_unlock_irqrestore(&lgff->lock, flags);
425 return 0; 143 return 0;
426} 144}
427
428static void hid_lgff_timer(unsigned long timer_data)
429{
430 struct lgff_device *lgff = (struct lgff_device*)timer_data;
431 struct hid_device *hid = lgff->hid;
432 unsigned long flags;
433 int x = 0x7f, y = 0x7f; // Coordinates of constant effects
434 unsigned int left = 0, right = 0; // Rumbling
435 int i;
436
437 spin_lock_irqsave(&lgff->lock, flags);
438
439 for (i=0; i<LGFF_EFFECTS; ++i) {
440 struct lgff_effect* effect = lgff->effects +i;
441
442 if (test_bit(EFFECT_PLAYING, effect->flags)) {
443
444 switch (effect->effect.type) {
445 case FF_CONSTANT: {
446 //TODO: handle envelopes
447 int degrees = effect->effect.direction * 360 >> 16;
448 x += fixp_mult(fixp_sin(degrees),
449 fixp_new16(effect->effect.u.constant.level));
450 y += fixp_mult(-fixp_cos(degrees),
451 fixp_new16(effect->effect.u.constant.level));
452 } break;
453 case FF_RUMBLE:
454 right += effect->effect.u.rumble.strong_magnitude;
455 left += effect->effect.u.rumble.weak_magnitude;
456 break;
457 };
458
459 /* One run of the effect is finished playing */
460 if (time_after(jiffies,
461 effect->started_at
462 + effect->effect.replay.delay*HZ/1000
463 + effect->effect.replay.length*HZ/1000)) {
464 dbg("Finished playing once %d", i);
465 if (--effect->count <= 0) {
466 dbg("Stopped %d", i);
467 clear_bit(EFFECT_PLAYING, effect->flags);
468 }
469 else {
470 dbg("Start again %d", i);
471 if (effect->effect.replay.length != 0) {
472 clear_bit(EFFECT_PLAYING, effect->flags);
473 set_bit(EFFECT_STARTED, effect->flags);
474 }
475 effect->started_at = jiffies;
476 }
477 }
478
479 } else if (test_bit(EFFECT_STARTED, lgff->effects[i].flags)) {
480 /* Check if we should start playing the effect */
481 if (time_after(jiffies,
482 lgff->effects[i].started_at
483 + lgff->effects[i].effect.replay.delay*HZ/1000)) {
484 dbg("Now playing %d", i);
485 clear_bit(EFFECT_STARTED, lgff->effects[i].flags);
486 set_bit(EFFECT_PLAYING, lgff->effects[i].flags);
487 }
488 }
489 }
490
491#define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
492
493 // Clamp values
494 CLAMP(x);
495 CLAMP(y);
496 CLAMP(left);
497 CLAMP(right);
498
499#undef CLAMP
500
501 if (x != lgff->constant->field[0]->value[2]
502 || y != lgff->constant->field[0]->value[3]) {
503 lgff->constant->field[0]->value[2] = x;
504 lgff->constant->field[0]->value[3] = y;
505 dbg("(x,y)=(%04x, %04x)", x, y);
506 hid_submit_report(hid, lgff->constant, USB_DIR_OUT);
507 }
508
509 if (left != lgff->rumble->field[0]->value[2]
510 || right != lgff->rumble->field[0]->value[3]) {
511 lgff->rumble->field[0]->value[2] = left;
512 lgff->rumble->field[0]->value[3] = right;
513 dbg("(left,right)=(%04x, %04x)", left, right);
514 hid_submit_report(hid, lgff->rumble, USB_DIR_OUT);
515 }
516
517 if (!test_bit(DEVICE_CLOSING, lgff->flags)) {
518 lgff->timer.expires = RUN_AT(PERIOD);
519 add_timer(&lgff->timer);
520 }
521
522 spin_unlock_irqrestore(&lgff->lock, flags);
523}
diff --git a/drivers/usb/input/hid-tmff.c b/drivers/usb/input/hid-tmff.c
index bc6c54c4c7d3..2d5be4c318ac 100644
--- a/drivers/usb/input/hid-tmff.c
+++ b/drivers/usb/input/hid-tmff.c
@@ -28,97 +28,65 @@
28 */ 28 */
29 29
30#include <linux/input.h> 30#include <linux/input.h>
31#include <linux/sched.h>
32 31
33#undef DEBUG 32#undef DEBUG
34#include <linux/usb.h> 33#include <linux/usb.h>
35 34
36#include <linux/circ_buf.h>
37
38#include "hid.h" 35#include "hid.h"
39#include "../../input/fixp-arith.h"
40 36
41/* Usages for thrustmaster devices I know about */ 37/* Usages for thrustmaster devices I know about */
42#define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb) 38#define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb)
43#define DELAY_CALC(t,delay) ((t) + (delay)*HZ/1000)
44
45/* Effect status */
46#define EFFECT_STARTED 0 /* Effect is going to play after some time */
47#define EFFECT_PLAYING 1 /* Effect is playing */
48#define EFFECT_USED 2
49
50/* For tmff_device::flags */
51#define DEVICE_CLOSING 0 /* The driver is being unitialised */
52
53/* Check that the current process can access an effect */
54#define CHECK_OWNERSHIP(effect) (current->pid == 0 \
55 || effect.owner == current->pid)
56
57#define TMFF_CHECK_ID(id) ((id) >= 0 && (id) < TMFF_EFFECTS)
58 39
59#define TMFF_CHECK_OWNERSHIP(i, l) \
60 (test_bit(EFFECT_USED, l->effects[i].flags) \
61 && CHECK_OWNERSHIP(l->effects[i]))
62
63#define TMFF_EFFECTS 8
64
65struct tmff_effect {
66 pid_t owner;
67
68 struct ff_effect effect;
69
70 unsigned long flags[1];
71 unsigned int count; /* Number of times left to play */
72
73 unsigned long play_at; /* When the effect starts to play */
74 unsigned long stop_at; /* When the effect ends */
75};
76 40
77struct tmff_device { 41struct tmff_device {
78 struct hid_device *hid;
79
80 struct hid_report *report; 42 struct hid_report *report;
81
82 struct hid_field *rumble; 43 struct hid_field *rumble;
44};
83 45
84 unsigned int effects_playing; 46/* Changes values from 0 to 0xffff into values from minimum to maximum */
85 struct tmff_effect effects[TMFF_EFFECTS]; 47static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum)
86 spinlock_t lock; /* device-level lock. Having locks on 48{
87 a per-effect basis could be nice, but 49 int ret;
88 isn't really necessary */
89 50
90 unsigned long flags[1]; /* Contains various information about the 51 ret = (in * (maximum - minimum) / 0xffff) + minimum;
91 state of the driver for this device */ 52 if (ret < minimum)
53 return minimum;
54 if (ret > maximum)
55 return maximum;
56 return ret;
57}
92 58
93 struct timer_list timer; 59static int hid_tmff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
94}; 60{
61 struct hid_device *hid = dev->private;
62 struct tmff_device *tmff = data;
63 int left, right; /* Rumbling */
95 64
96/* Callbacks */ 65 left = hid_tmff_scale(effect->u.rumble.weak_magnitude,
97static void hid_tmff_exit(struct hid_device *hid); 66 tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
98static int hid_tmff_event(struct hid_device *hid, struct input_dev *input, 67 right = hid_tmff_scale(effect->u.rumble.strong_magnitude,
99 unsigned int type, unsigned int code, int value); 68 tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
100static int hid_tmff_flush(struct input_dev *input, struct file *file);
101static int hid_tmff_upload_effect(struct input_dev *input,
102 struct ff_effect *effect);
103static int hid_tmff_erase(struct input_dev *input, int id);
104 69
105/* Local functions */ 70 tmff->rumble->value[0] = left;
106static void hid_tmff_recalculate_timer(struct tmff_device *tmff); 71 tmff->rumble->value[1] = right;
107static void hid_tmff_timer(unsigned long timer_data); 72 dbg("(left,right)=(%08x, %08x)", left, right);
73 hid_submit_report(hid, tmff->report, USB_DIR_OUT);
74
75 return 0;
76}
108 77
109int hid_tmff_init(struct hid_device *hid) 78int hid_tmff_init(struct hid_device *hid)
110{ 79{
111 struct tmff_device *private; 80 struct tmff_device *tmff;
112 struct list_head *pos; 81 struct list_head *pos;
113 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 82 struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
114 struct input_dev *input_dev = hidinput->input; 83 struct input_dev *input_dev = hidinput->input;
84 int error;
115 85
116 private = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); 86 tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
117 if (!private) 87 if (!tmff)
118 return -ENOMEM; 88 return -ENOMEM;
119 89
120 hid->ff_private = private;
121
122 /* Find the report to use */ 90 /* Find the report to use */
123 __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) { 91 __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
124 struct hid_report *report = (struct hid_report *)pos; 92 struct hid_report *report = (struct hid_report *)pos;
@@ -142,18 +110,18 @@ int hid_tmff_init(struct hid_device *hid)
142 continue; 110 continue;
143 } 111 }
144 112
145 if (private->report && private->report != report) { 113 if (tmff->report && tmff->report != report) {
146 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report"); 114 warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report");
147 continue; 115 continue;
148 } 116 }
149 117
150 if (private->rumble && private->rumble != field) { 118 if (tmff->rumble && tmff->rumble != field) {
151 warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR"); 119 warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR");
152 continue; 120 continue;
153 } 121 }
154 122
155 private->report = report; 123 tmff->report = report;
156 private->rumble = field; 124 tmff->rumble = field;
157 125
158 set_bit(FF_RUMBLE, input_dev->ffbit); 126 set_bit(FF_RUMBLE, input_dev->ffbit);
159 break; 127 break;
@@ -162,302 +130,17 @@ int hid_tmff_init(struct hid_device *hid)
162 warn("ignoring unknown output usage %08x", field->usage[0].hid); 130 warn("ignoring unknown output usage %08x", field->usage[0].hid);
163 continue; 131 continue;
164 } 132 }
165
166 /* Fallthrough to here only when a valid usage is found */
167 input_dev->upload_effect = hid_tmff_upload_effect;
168 input_dev->flush = hid_tmff_flush;
169
170 set_bit(EV_FF, input_dev->evbit);
171 input_dev->ff_effects_max = TMFF_EFFECTS;
172 } 133 }
173 } 134 }
174 135
175 private->hid = hid; 136 error = input_ff_create_memless(input_dev, tmff, hid_tmff_play);
176 137 if (error) {
177 spin_lock_init(&private->lock); 138 kfree(tmff);
178 init_timer(&private->timer); 139 return error;
179 private->timer.data = (unsigned long)private;
180 private->timer.function = hid_tmff_timer;
181
182 /* Event and exit callbacks */
183 hid->ff_exit = hid_tmff_exit;
184 hid->ff_event = hid_tmff_event;
185
186 info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
187
188 return 0;
189}
190
191static void hid_tmff_exit(struct hid_device *hid)
192{
193 struct tmff_device *tmff = hid->ff_private;
194 unsigned long flags;
195
196 spin_lock_irqsave(&tmff->lock, flags);
197
198 set_bit(DEVICE_CLOSING, tmff->flags);
199 del_timer_sync(&tmff->timer);
200
201 spin_unlock_irqrestore(&tmff->lock, flags);
202
203 kfree(tmff);
204}
205
206static int hid_tmff_event(struct hid_device *hid, struct input_dev *input,
207 unsigned int type, unsigned int code, int value)
208{
209 struct tmff_device *tmff = hid->ff_private;
210 struct tmff_effect *effect = &tmff->effects[code];
211 unsigned long flags;
212
213 if (type != EV_FF)
214 return -EINVAL;
215 if (!TMFF_CHECK_ID(code))
216 return -EINVAL;
217 if (!TMFF_CHECK_OWNERSHIP(code, tmff))
218 return -EACCES;
219 if (value < 0)
220 return -EINVAL;
221
222 spin_lock_irqsave(&tmff->lock, flags);
223
224 if (value > 0) {
225 set_bit(EFFECT_STARTED, effect->flags);
226 clear_bit(EFFECT_PLAYING, effect->flags);
227 effect->count = value;
228 effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay);
229 } else {
230 clear_bit(EFFECT_STARTED, effect->flags);
231 clear_bit(EFFECT_PLAYING, effect->flags);
232 }
233
234 hid_tmff_recalculate_timer(tmff);
235
236 spin_unlock_irqrestore(&tmff->lock, flags);
237
238 return 0;
239
240}
241
242/* Erase all effects this process owns */
243
244static int hid_tmff_flush(struct input_dev *dev, struct file *file)
245{
246 struct hid_device *hid = dev->private;
247 struct tmff_device *tmff = hid->ff_private;
248 int i;
249
250 for (i=0; i<dev->ff_effects_max; ++i)
251
252 /* NOTE: no need to lock here. The only times EFFECT_USED is
253 modified is when effects are uploaded or when an effect is
254 erased. But a process cannot close its dev/input/eventX fd
255 and perform ioctls on the same fd all at the same time */
256
257 if (current->pid == tmff->effects[i].owner
258 && test_bit(EFFECT_USED, tmff->effects[i].flags))
259 if (hid_tmff_erase(dev, i))
260 warn("erase effect %d failed", i);
261
262
263 return 0;
264}
265
266static int hid_tmff_erase(struct input_dev *dev, int id)
267{
268 struct hid_device *hid = dev->private;
269 struct tmff_device *tmff = hid->ff_private;
270 unsigned long flags;
271
272 if (!TMFF_CHECK_ID(id))
273 return -EINVAL;
274 if (!TMFF_CHECK_OWNERSHIP(id, tmff))
275 return -EACCES;
276
277 spin_lock_irqsave(&tmff->lock, flags);
278
279 tmff->effects[id].flags[0] = 0;
280 hid_tmff_recalculate_timer(tmff);
281
282 spin_unlock_irqrestore(&tmff->lock, flags);
283
284 return 0;
285}
286
287static int hid_tmff_upload_effect(struct input_dev *input,
288 struct ff_effect *effect)
289{
290 struct hid_device *hid = input->private;
291 struct tmff_device *tmff = hid->ff_private;
292 int id;
293 unsigned long flags;
294
295 if (!test_bit(effect->type, input->ffbit))
296 return -EINVAL;
297 if (effect->id != -1 && !TMFF_CHECK_ID(effect->id))
298 return -EINVAL;
299
300 spin_lock_irqsave(&tmff->lock, flags);
301
302 if (effect->id == -1) {
303 /* Find a free effect */
304 for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id);
305
306 if (id >= TMFF_EFFECTS) {
307 spin_unlock_irqrestore(&tmff->lock, flags);
308 return -ENOSPC;
309 }
310
311 effect->id = id;
312 tmff->effects[id].owner = current->pid;
313 tmff->effects[id].flags[0] = 0;
314 set_bit(EFFECT_USED, tmff->effects[id].flags);
315
316 } else {
317 /* Re-uploading an owned effect, to change parameters */
318 id = effect->id;
319 clear_bit(EFFECT_PLAYING, tmff->effects[id].flags);
320 } 140 }
321 141
322 tmff->effects[id].effect = *effect; 142 info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
323
324 hid_tmff_recalculate_timer(tmff);
325 143
326 spin_unlock_irqrestore(&tmff->lock, flags);
327 return 0; 144 return 0;
328} 145}
329 146
330/* Start the timer for the next start/stop/delay */
331/* Always call this while tmff->lock is locked */
332
333static void hid_tmff_recalculate_timer(struct tmff_device *tmff)
334{
335 int i;
336 int events = 0;
337 unsigned long next_time;
338
339 next_time = 0; /* Shut up compiler's incorrect warning */
340
341 /* Find the next change in an effect's status */
342 for (i = 0; i < TMFF_EFFECTS; ++i) {
343 struct tmff_effect *effect = &tmff->effects[i];
344 unsigned long play_time;
345
346 if (!test_bit(EFFECT_STARTED, effect->flags))
347 continue;
348
349 effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length);
350
351 if (!test_bit(EFFECT_PLAYING, effect->flags))
352 play_time = effect->play_at;
353 else
354 play_time = effect->stop_at;
355
356 events++;
357
358 if (time_after(jiffies, play_time))
359 play_time = jiffies;
360
361 if (events == 1)
362 next_time = play_time;
363 else {
364 if (time_after(next_time, play_time))
365 next_time = play_time;
366 }
367 }
368
369 if (!events && tmff->effects_playing) {
370 /* Treat all effects turning off as an event */
371 events = 1;
372 next_time = jiffies;
373 }
374
375 if (!events) {
376 /* No events, no time, no need for a timer. */
377 del_timer_sync(&tmff->timer);
378 return;
379 }
380
381 mod_timer(&tmff->timer, next_time);
382}
383
384/* Changes values from 0 to 0xffff into values from minimum to maximum */
385static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum)
386{
387 int ret;
388
389 ret = (in * (maximum - minimum) / 0xffff) + minimum;
390 if (ret < minimum)
391 return minimum;
392 if (ret > maximum)
393 return maximum;
394 return ret;
395}
396
397static void hid_tmff_timer(unsigned long timer_data)
398{
399 struct tmff_device *tmff = (struct tmff_device *) timer_data;
400 struct hid_device *hid = tmff->hid;
401 unsigned long flags;
402 int left = 0, right = 0; /* Rumbling */
403 int i;
404
405 spin_lock_irqsave(&tmff->lock, flags);
406
407 tmff->effects_playing = 0;
408
409 for (i = 0; i < TMFF_EFFECTS; ++i) {
410 struct tmff_effect *effect = &tmff->effects[i];
411
412 if (!test_bit(EFFECT_STARTED, effect->flags))
413 continue;
414
415 if (!time_after(jiffies, effect->play_at))
416 continue;
417
418 if (time_after(jiffies, effect->stop_at)) {
419
420 dbg("Finished playing once %d", i);
421 clear_bit(EFFECT_PLAYING, effect->flags);
422
423 if (--effect->count <= 0) {
424 dbg("Stopped %d", i);
425 clear_bit(EFFECT_STARTED, effect->flags);
426 continue;
427 } else {
428 dbg("Start again %d", i);
429 effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay);
430 continue;
431 }
432 }
433
434 ++tmff->effects_playing;
435
436 set_bit(EFFECT_PLAYING, effect->flags);
437
438 switch (effect->effect.type) {
439 case FF_RUMBLE:
440 right += effect->effect.u.rumble.strong_magnitude;
441 left += effect->effect.u.rumble.weak_magnitude;
442 break;
443 default:
444 BUG();
445 break;
446 }
447 }
448
449 left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
450 right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
451
452 if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) {
453 tmff->rumble->value[0] = left;
454 tmff->rumble->value[1] = right;
455 dbg("(left,right)=(%08x, %08x)", left, right);
456 hid_submit_report(hid, tmff->report, USB_DIR_OUT);
457 }
458
459 if (!test_bit(DEVICE_CLOSING, tmff->flags))
460 hid_tmff_recalculate_timer(tmff);
461
462 spin_unlock_irqrestore(&tmff->lock, flags);
463}
diff --git a/drivers/usb/input/hid.h b/drivers/usb/input/hid.h
index 29f1613bd08c..6d88c0c0b903 100644
--- a/drivers/usb/input/hid.h
+++ b/drivers/usb/input/hid.h
@@ -449,11 +449,6 @@ struct hid_device { /* device report descriptor */
449 char phys[64]; /* Device physical location */ 449 char phys[64]; /* Device physical location */
450 char uniq[64]; /* Device unique identifier (serial #) */ 450 char uniq[64]; /* Device unique identifier (serial #) */
451 451
452 void *ff_private; /* Private data for the force-feedback driver */
453 void (*ff_exit)(struct hid_device*); /* Called by hid_exit_ff(hid) */
454 int (*ff_event)(struct hid_device *hid, struct input_dev *input,
455 unsigned int type, unsigned int code, int value);
456
457#ifdef CONFIG_USB_HIDINPUT_POWERBOOK 452#ifdef CONFIG_USB_HIDINPUT_POWERBOOK
458 unsigned long pb_pressed_fn[NBITS(KEY_MAX)]; 453 unsigned long pb_pressed_fn[NBITS(KEY_MAX)];
459 unsigned long pb_pressed_numlock[NBITS(KEY_MAX)]; 454 unsigned long pb_pressed_numlock[NBITS(KEY_MAX)];
@@ -540,17 +535,3 @@ static inline int hid_pidff_init(struct hid_device *hid) { return -ENODEV; }
540static inline int hid_ff_init(struct hid_device *hid) { return -1; } 535static inline int hid_ff_init(struct hid_device *hid) { return -1; }
541#endif 536#endif
542 537
543static inline void hid_ff_exit(struct hid_device *hid)
544{
545 if (hid->ff_exit)
546 hid->ff_exit(hid);
547}
548
549static inline int hid_ff_event(struct hid_device *hid, struct input_dev *input,
550 unsigned int type, unsigned int code, int value)
551{
552 if (hid->ff_event)
553 return hid->ff_event(hid, input, type, code, value);
554 return -ENOSYS;
555}
556