Input: add force feedback driver for PID devices

This replaces the older PID driver which was never completed.

Signed-off-by: Anssi Hannula <anssi.hannula@gmail.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
This commit is contained in:
Anssi Hannula 2006-07-19 01:40:47 -04:00 committed by Dmitry Torokhov
parent f6a01c8596
commit 224ee88fe3
8 changed files with 1364 additions and 414 deletions

View file

@ -60,12 +60,12 @@ config HID_FF
If unsure, say N.
config HID_PID
bool "PID Devices (Microsoft Sidewinder Force Feedback 2)"
bool "PID device support"
depends on HID_FF
help
Say Y here if you have a PID-compliant joystick and wish to enable force
feedback for it. The Microsoft Sidewinder Force Feedback 2 is one such
device.
Say Y here if you have a PID-compliant device and wish to enable force
feedback for it. Microsoft Sidewinder Force Feedback 2 is one of such
devices.
config LOGITECH_FF
bool "Logitech WingMan *3D support"

View file

@ -14,7 +14,7 @@ ifeq ($(CONFIG_USB_HIDINPUT),y)
usbhid-objs += hid-input.o
endif
ifeq ($(CONFIG_HID_PID),y)
usbhid-objs += pid.o
usbhid-objs += hid-pidff.o
endif
ifeq ($(CONFIG_LOGITECH_FF),y)
usbhid-objs += hid-lgff.o

View file

@ -44,45 +44,34 @@ struct hid_ff_initializer {
int (*init)(struct hid_device*);
};
/*
* We try pidff when no other driver is found because PID is the
* standards compliant way of implementing force feedback in HID.
* pidff_init() will quickly abort if the device doesn't appear to
* be a PID device
*/
static struct hid_ff_initializer inits[] = {
#ifdef CONFIG_LOGITECH_FF
{0x46d, 0xc211, hid_lgff_init}, // Logitech Cordless rumble pad
{0x46d, 0xc283, hid_lgff_init}, // Logitech Wingman Force 3d
{0x46d, 0xc295, hid_lgff_init}, // Logitech MOMO force wheel
{0x46d, 0xc219, hid_lgff_init}, // Logitech Cordless rumble pad 2
#endif
#ifdef CONFIG_HID_PID
{0x45e, 0x001b, hid_pid_init},
{ 0x46d, 0xc211, hid_lgff_init }, /* Logitech Cordless rumble pad */
{ 0x46d, 0xc283, hid_lgff_init }, /* Logitech Wingman Force 3d */
{ 0x46d, 0xc295, hid_lgff_init }, /* Logitech MOMO force wheel */
{ 0x46d, 0xc219, hid_lgff_init }, /* Logitech Cordless rumble pad 2 */
#endif
#ifdef CONFIG_THRUSTMASTER_FF
{0x44f, 0xb304, hid_tmff_init},
{ 0x44f, 0xb304, hid_tmff_init },
#endif
{0, 0, NULL} /* Terminating entry */
{ 0, 0, hid_pidff_init} /* Matches anything */
};
static struct hid_ff_initializer *hid_get_ff_init(__u16 idVendor,
__u16 idProduct)
{
struct hid_ff_initializer *init;
for (init = inits;
init->idVendor
&& !(init->idVendor == idVendor
&& init->idProduct == idProduct);
init++);
return init->idVendor? init : NULL;
}
int hid_ff_init(struct hid_device* hid)
{
struct hid_ff_initializer *init;
int vendor = le16_to_cpu(hid->dev->descriptor.idVendor);
int product = le16_to_cpu(hid->dev->descriptor.idProduct);
init = hid_get_ff_init(le16_to_cpu(hid->dev->descriptor.idVendor),
le16_to_cpu(hid->dev->descriptor.idProduct));
for (init = inits; init->idVendor; init++)
if (init->idVendor == vendor && init->idProduct == product)
break;
if (!init) {
dbg("hid_ff_init could not find initializer");
return -ENOSYS;
}
return init->init(hid);
}

View file

@ -65,11 +65,9 @@ static const struct {
#define map_rel(c) do { usage->code = c; usage->type = EV_REL; bit = input->relbit; max = REL_MAX; } while (0)
#define map_key(c) do { usage->code = c; usage->type = EV_KEY; bit = input->keybit; max = KEY_MAX; } while (0)
#define map_led(c) do { usage->code = c; usage->type = EV_LED; bit = input->ledbit; max = LED_MAX; } while (0)
#define map_ff(c) do { usage->code = c; usage->type = EV_FF; bit = input->ffbit; max = FF_MAX; } while (0)
#define map_abs_clear(c) do { map_abs(c); clear_bit(c, bit); } while (0)
#define map_key_clear(c) do { map_key(c); clear_bit(c, bit); } while (0)
#define map_ff_effect(c) do { set_bit(c, input->ffbit); } while (0)
#ifdef CONFIG_USB_HIDINPUT_POWERBOOK
@ -525,23 +523,7 @@ static void hidinput_configure_usage(struct hid_input *hidinput, struct hid_fiel
case HID_UP_PID:
set_bit(EV_FF, input->evbit);
switch(usage->hid & HID_USAGE) {
case 0x26: map_ff_effect(FF_CONSTANT); goto ignore;
case 0x27: map_ff_effect(FF_RAMP); goto ignore;
case 0x28: map_ff_effect(FF_CUSTOM); goto ignore;
case 0x30: map_ff_effect(FF_SQUARE); map_ff_effect(FF_PERIODIC); goto ignore;
case 0x31: map_ff_effect(FF_SINE); map_ff_effect(FF_PERIODIC); goto ignore;
case 0x32: map_ff_effect(FF_TRIANGLE); map_ff_effect(FF_PERIODIC); goto ignore;
case 0x33: map_ff_effect(FF_SAW_UP); map_ff_effect(FF_PERIODIC); goto ignore;
case 0x34: map_ff_effect(FF_SAW_DOWN); map_ff_effect(FF_PERIODIC); goto ignore;
case 0x40: map_ff_effect(FF_SPRING); goto ignore;
case 0x41: map_ff_effect(FF_DAMPER); goto ignore;
case 0x42: map_ff_effect(FF_INERTIA); goto ignore;
case 0x43: map_ff_effect(FF_FRICTION); goto ignore;
case 0x7e: map_ff(FF_GAIN); break;
case 0x83: input->ff_effects_max = field->value[0]; goto ignore;
case 0x98: map_ff(FF_AUTOCENTER); break;
case 0xa4: map_key_clear(BTN_DEAD); break;
default: goto ignore;
}
@ -698,8 +680,7 @@ void hidinput_hid_event(struct hid_device *hid, struct hid_field *field, struct
}
if (usage->hid == (HID_UP_PID | 0x83UL)) { /* Simultaneous Effects Max */
input->ff_effects_max = value;
dbg("Maximum Effects - %d",input->ff_effects_max);
dbg("Maximum Effects - %d",value);
return;
}

File diff suppressed because it is too large Load diff

View file

@ -527,14 +527,25 @@ int hid_wait_io(struct hid_device* hid);
#ifdef CONFIG_HID_FF
int hid_ff_init(struct hid_device *hid);
int hid_lgff_init(struct hid_device *hid);
int hid_tmff_init(struct hid_device *hid);
#ifdef CONFIG_HID_PID
int hid_pidff_init(struct hid_device *hid);
#else
static inline int hid_pidff_init(struct hid_device *hid) { return -ENODEV; }
#endif
#else
static inline int hid_ff_init(struct hid_device *hid) { return -1; }
#endif
static inline void hid_ff_exit(struct hid_device *hid)
{
if (hid->ff_exit)
hid->ff_exit(hid);
}
static inline int hid_ff_event(struct hid_device *hid, struct input_dev *input,
unsigned int type, unsigned int code, int value)
{
@ -543,7 +554,3 @@ static inline int hid_ff_event(struct hid_device *hid, struct input_dev *input,
return -ENOSYS;
}
int hid_lgff_init(struct hid_device* hid);
int hid_tmff_init(struct hid_device* hid);
int hid_pid_init(struct hid_device* hid);

View file

@ -1,295 +0,0 @@
/*
* PID Force feedback support for hid devices.
*
* Copyright (c) 2002 Rodrigo Damazio.
* Portions by Johann Deneux and Bjorn Augustson
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Should you need to contact me, the author, you can do so by
* e-mail - mail your message to <rdamazio@lsi.usp.br>
*/
#include <linux/config.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/smp_lock.h>
#include <linux/spinlock.h>
#include <linux/input.h>
#include <linux/usb.h>
#include "hid.h"
#include "pid.h"
#define CHECK_OWNERSHIP(i, hid_pid) \
((i) < FF_EFFECTS_MAX && i >= 0 && \
test_bit(FF_PID_FLAGS_USED, &hid_pid->effects[(i)].flags) && \
(current->pid == 0 || \
(hid_pid)->effects[(i)].owner == current->pid))
/* Called when a transfer is completed */
static void hid_pid_ctrl_out(struct urb *u, struct pt_regs *regs)
{
dev_dbg(&u->dev->dev, "hid_pid_ctrl_out - Transfer Completed\n");
}
static void hid_pid_exit(struct hid_device *hid)
{
struct hid_ff_pid *private = hid->ff_private;
if (private->urbffout) {
usb_kill_urb(private->urbffout);
usb_free_urb(private->urbffout);
}
}
static int pid_upload_periodic(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
{
dev_info(&pid->hid->dev->dev, "requested periodic force upload\n");
return 0;
}
static int pid_upload_constant(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
{
dev_info(&pid->hid->dev->dev, "requested constant force upload\n");
return 0;
}
static int pid_upload_condition(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
{
dev_info(&pid->hid->dev->dev, "requested Condition force upload\n");
return 0;
}
static int pid_upload_ramp(struct hid_ff_pid *pid, struct ff_effect *effect, int is_update)
{
dev_info(&pid->hid->dev->dev, "request ramp force upload\n");
return 0;
}
static int hid_pid_event(struct hid_device *hid, struct input_dev *input,
unsigned int type, unsigned int code, int value)
{
dev_dbg(&hid->dev->dev, "PID event received: type=%d,code=%d,value=%d.\n", type, code, value);
if (type != EV_FF)
return -1;
return 0;
}
/* Lock must be held by caller */
static void hid_pid_ctrl_playback(struct hid_device *hid, struct hid_pid_effect *effect, int play)
{
if (play)
set_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
else
clear_bit(FF_PID_FLAGS_PLAYING, &effect->flags);
}
static int hid_pid_erase(struct input_dev *dev, int id)
{
struct hid_device *hid = dev->private;
struct hid_ff_pid *pid = hid->ff_private;
struct hid_field *field;
unsigned long flags;
int ret;
if (!CHECK_OWNERSHIP(id, pid))
return -EACCES;
/* Find report */
field = hid_find_field_by_usage(hid, HID_UP_PID | FF_PID_USAGE_BLOCK_FREE,
HID_OUTPUT_REPORT);
if (!field) {
dev_err(&hid->dev->dev, "couldn't find report\n");
return -EIO;
}
ret = hid_set_field(field, 0, pid->effects[id].device_id);
if (ret) {
dev_err(&hid->dev->dev, "couldn't set field\n");
return ret;
}
hid_submit_report(hid, field->report, USB_DIR_OUT);
spin_lock_irqsave(&pid->lock, flags);
hid_pid_ctrl_playback(hid, pid->effects + id, 0);
pid->effects[id].flags = 0;
spin_unlock_irqrestore(&pid->lock, flags);
return 0;
}
/* Erase all effects this process owns */
static int hid_pid_flush(struct input_dev *dev, struct file *file)
{
struct hid_device *hid = dev->private;
struct hid_ff_pid *pid = hid->ff_private;
int i;
/*NOTE: no need to lock here. The only times EFFECT_USED is
modified is when effects are uploaded or when an effect is
erased. But a process cannot close its dev/input/eventX fd
and perform ioctls on the same fd all at the same time */
/*FIXME: multiple threads, anyone? */
for (i = 0; i < dev->ff_effects_max; ++i)
if (current->pid == pid->effects[i].owner
&& test_bit(FF_PID_FLAGS_USED, &pid->effects[i].flags))
if (hid_pid_erase(dev, i))
dev_warn(&hid->dev->dev, "erase effect %d failed", i);
return 0;
}
static int hid_pid_upload_effect(struct input_dev *dev,
struct ff_effect *effect)
{
struct hid_ff_pid *pid_private = (struct hid_ff_pid *)(dev->private);
int ret;
int is_update;
unsigned long flags;
dev_dbg(&pid_private->hid->dev->dev, "upload effect called: effect_type=%x\n", effect->type);
/* Check this effect type is supported by this device */
if (!test_bit(effect->type, dev->ffbit)) {
dev_dbg(&pid_private->hid->dev->dev,
"invalid kind of effect requested.\n");
return -EINVAL;
}
/*
* If we want to create a new effect, get a free id
*/
if (effect->id == -1) {
int id = 0;
// Spinlock so we don`t get a race condition when choosing IDs
spin_lock_irqsave(&pid_private->lock, flags);
while (id < FF_EFFECTS_MAX)
if (!test_and_set_bit(FF_PID_FLAGS_USED, &pid_private->effects[id++].flags))
break;
if (id == FF_EFFECTS_MAX) {
spin_unlock_irqrestore(&pid_private->lock, flags);
// TEMP - We need to get ff_effects_max correctly first: || id >= dev->ff_effects_max) {
dev_dbg(&pid_private->hid->dev->dev, "Not enough device memory\n");
return -ENOMEM;
}
effect->id = id;
dev_dbg(&pid_private->hid->dev->dev, "effect ID is %d.\n", id);
pid_private->effects[id].owner = current->pid;
pid_private->effects[id].flags = (1 << FF_PID_FLAGS_USED);
spin_unlock_irqrestore(&pid_private->lock, flags);
is_update = FF_PID_FALSE;
} else {
/* We want to update an effect */
if (!CHECK_OWNERSHIP(effect->id, pid_private))
return -EACCES;
/* Parameter type cannot be updated */
if (effect->type != pid_private->effects[effect->id].effect.type)
return -EINVAL;
/* Check the effect is not already being updated */
if (test_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags))
return -EAGAIN;
is_update = FF_PID_TRUE;
}
/*
* Upload the effect
*/
switch (effect->type) {
case FF_PERIODIC:
ret = pid_upload_periodic(pid_private, effect, is_update);
break;
case FF_CONSTANT:
ret = pid_upload_constant(pid_private, effect, is_update);
break;
case FF_SPRING:
case FF_FRICTION:
case FF_DAMPER:
case FF_INERTIA:
ret = pid_upload_condition(pid_private, effect, is_update);
break;
case FF_RAMP:
ret = pid_upload_ramp(pid_private, effect, is_update);
break;
default:
dev_dbg(&pid_private->hid->dev->dev,
"invalid type of effect requested - %x.\n",
effect->type);
return -EINVAL;
}
/* If a packet was sent, forbid new updates until we are notified
* that the packet was updated
*/
if (ret == 0)
set_bit(FF_PID_FLAGS_UPDATING, &pid_private->effects[effect->id].flags);
pid_private->effects[effect->id].effect = *effect;
return ret;
}
int hid_pid_init(struct hid_device *hid)
{
struct hid_ff_pid *private;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
struct input_dev *input_dev = hidinput->input;
private = hid->ff_private = kzalloc(sizeof(struct hid_ff_pid), GFP_KERNEL);
if (!private)
return -ENOMEM;
private->hid = hid;
hid->ff_exit = hid_pid_exit;
hid->ff_event = hid_pid_event;
/* Open output URB */
if (!(private->urbffout = usb_alloc_urb(0, GFP_KERNEL))) {
kfree(private);
return -1;
}
usb_fill_control_urb(private->urbffout, hid->dev, 0,
(void *)&private->ffcr, private->ctrl_buffer, 8,
hid_pid_ctrl_out, hid);
input_dev->upload_effect = hid_pid_upload_effect;
input_dev->flush = hid_pid_flush;
input_dev->ff_effects_max = 8; // A random default
set_bit(EV_FF, input_dev->evbit);
set_bit(EV_FF_STATUS, input_dev->evbit);
spin_lock_init(&private->lock);
printk(KERN_INFO "Force feedback driver for PID devices by Rodrigo Damazio <rdamazio@lsi.usp.br>.\n");
return 0;
}

View file

@ -1,62 +0,0 @@
/*
* PID Force feedback support for hid devices.
*
* Copyright (c) 2002 Rodrigo Damazio.
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Should you need to contact me, the author, you can do so by
* e-mail - mail your message to <rdamazio@lsi.usp.br>
*/
#define FF_EFFECTS_MAX 64
#define FF_PID_FLAGS_USED 1 /* If the effect exists */
#define FF_PID_FLAGS_UPDATING 2 /* If the effect is being updated */
#define FF_PID_FLAGS_PLAYING 3 /* If the effect is currently being played */
#define FF_PID_FALSE 0
#define FF_PID_TRUE 1
struct hid_pid_effect {
unsigned long flags;
pid_t owner;
unsigned int device_id; /* The device-assigned ID */
struct ff_effect effect;
};
struct hid_ff_pid {
struct hid_device *hid;
unsigned long gain;
struct urb *urbffout;
struct usb_ctrlrequest ffcr;
spinlock_t lock;
unsigned char ctrl_buffer[8];
struct hid_pid_effect effects[FF_EFFECTS_MAX];
};
/*
* Constants from the PID usage table (still far from complete)
*/
#define FF_PID_USAGE_BLOCK_LOAD 0x89UL
#define FF_PID_USAGE_BLOCK_FREE 0x90UL
#define FF_PID_USAGE_NEW_EFFECT 0xABUL
#define FF_PID_USAGE_POOL_REPORT 0x7FUL