Skip to contentSkip to conversation

HID: hid-lg4ff add support for G27 LEDs

PATCH [email protected] 2 participants 5 messages Mar 13, 2012 – Mar 14, 2012 HID

lore
Share & tools
lore
#0 lore raw
Quoted 1 line

Not really, it's rather simple to use and as Mark said, it's a proper

infrastructure to use here.

Quoted 1 line

We already have HID drivers which are using this -- you can look at

PicoLCD or Wiimote drivers and you'll see that it's indeed fairly simple.

So _you_ say.... I'm getting an Oops when device attempts to init the LED subsytem. :-(
--

Mar 13 12:54:56 ubuntu kernel: [  458.639674] Call Trace:
Mar 13 12:54:56 ubuntu kernel: [  458.641143]  [<c13498ed>] ?
device_create+0x2d/0x30
Mar 13 12:54:56 ubuntu kernel: [  458.641245]  [<c1439119>]
led_classdev_register+0x99/0xf0
Mar 13 12:54:56 ubuntu kernel: [  458.641306]  [<e0977311>]
lg4ff_init+0x2a1/0x570 [hid_logitech]
Mar 13 12:54:56 ubuntu kernel: [  458.641313]  [<e0975427>]

lg_probe+0x147/0x2c0 [hid_logitech]
--

Code snipet below (full file attached):
--
int lg4ff_init(struct hid_device *hid)

{
    struct hid_input *hidinput = list_entry(hid->inputs.next, struct
hid_input, list);
    struct list_head *report_list =
&hid->report_enum[HID_OUTPUT_REPORT].report_list;
    struct input_dev *dev = hidinput->input;
    struct hid_report *report;
    struct hid_field *field;
    struct lg_drv_data *drv_data;
    struct lg4ff_device_entry *entry;
    struct usb_device_descriptor *udesc;
    int error, i, j;
    __u16 bcdDevice, rev_maj, rev_min;
    struct led_classdev *led;
    struct device *dev2 = &dev->dev;
--
    /* register led subsystem - G27 only */
    //if (rev_maj == G27_REV_MAJ && rev_min == G27_REV_MIN) {
    if (rev_maj != G27_REV_MAJ || rev_min != G27_REV_MIN) {
        for (i = 0; i < 5; i++) {
            led = kzalloc(sizeof(struct led_classdev), GFP_KERNEL); if
(!led) {
                dev_err(dev2, "can't allocate memory for LED %d\n", i);
error = -ENOMEM;
                goto err;
            }
            led->name = (void *)(g27_leds[i]);
            led->brightness = 0;
            led->max_brightness = 1;
            led->brightness_get = lg4ff_led_get_brightness;
            led->brightness_set = lg4ff_led_set_brightness;

            entry->led[i] = led;
#if 1
            error = led_classdev_register(dev2, entry->led[i]);
            if (error) {
                dev_err(dev2, "can't register LED %d\n", i);
                goto err;
            }
#endif
        }

        dbg_hid("sysfs interface created for leds\n");

--

Any suggestions as to what I'm doing wrong?
Simon

/*
* Force feedback support for hid-compliant for some of the devices from
* Logitech, namely:
* - WingMan Cordless RumblePad
* - WingMan Force 3D
*
* Copyright (c) 2002-2004 Johann Deneux
* Copyright (c) 2006 Anssi Hannula <[email protected]> */

/*
* 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 <[email protected]>
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <linux/input.h>
#include <linux/usb.h>
#include <linux/hid.h>

#include "usbhid/usbhid.h"
#include "hid-lg.h"

struct dev_type {
	u16 idVendor;
	u16 idProduct;
	const signed short *ff;
};
static const signed short ff_rumble[] = {
	FF_RUMBLE,
	-1
};
static const signed short ff_joystick[] = {
	FF_CONSTANT,
	-1
};
static const signed short ff_joystick_ac[] = {
	FF_CONSTANT,
	FF_AUTOCENTER,
	-1
};
static const struct dev_type devices[] = {
	{ 0x046d, 0xc211, ff_rumble },
	{ 0x046d, 0xc219, ff_rumble },
	{ 0x046d, 0xc283, ff_joystick },
	{ 0x046d, 0xc286, ff_joystick_ac },
	{ 0x046d, 0xc287, ff_joystick_ac },
	{ 0x046d, 0xc293, ff_joystick },
	{ 0x046d, 0xc295, ff_joystick },
};

static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)

{
	struct hid_device *hid = input_get_drvdata(dev);
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
	int x, y;
	unsigned int left, right;

#define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff

	switch (effect->type) {
	case FF_CONSTANT:
		x = effect->u.ramp.start_level + 0x7f;	/* 0x7f is center */
		y = effect->u.ramp.end_level + 0x7f;
		CLAMP(x);
		CLAMP(y);
		report->field[0]->value[0] = 0x51;
		report->field[0]->value[1] = 0x08;
		report->field[0]->value[2] = x;
		report->field[0]->value[3] = y;
		dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
		usbhid_submit_report(hid, report, USB_DIR_OUT);
		break;

	case FF_RUMBLE:
		right = effect->u.rumble.strong_magnitude;
		left = effect->u.rumble.weak_magnitude;
		right = right * 0xff / 0xffff;
		left = left * 0xff / 0xffff;
		CLAMP(left);
		CLAMP(right);
		report->field[0]->value[0] = 0x42;
		report->field[0]->value[1] = 0x00;
		report->field[0]->value[2] = left;
		report->field[0]->value[3] = right;
		dbg_hid("(left, right)=(%04x, %04x)\n", left, right);
		usbhid_submit_report(hid, report, USB_DIR_OUT);
		break;
	}
	return 0;
}

static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)

{
	struct hid_device *hid = input_get_drvdata(dev);
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
	__s32 *value = report->field[0]->value;
	magnitude = (magnitude >> 12) & 0xf;
	*value++ = 0xfe;
	*value++ = 0x0d;
	*value++ = magnitude;   /* clockwise strength */
	*value++ = magnitude;   /* counter-clockwise strength */
	*value++ = 0x80;
	*value++ = 0x00;
	*value = 0x00;
	usbhid_submit_report(hid, report, USB_DIR_OUT);
}

int lgff_init(struct hid_device* hid)

{
	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct input_dev *dev = hidinput->input;
	struct hid_report *report;
	struct hid_field *field;
	const signed short *ff_bits = ff_joystick;
	int error;
	int i;

	/* Find the report to use */
	if (list_empty(report_list)) {
		hid_err(hid, "No output report found\n");
		return -1;
	}

	/* Check that the report looks ok */
	report = list_entry(report_list->next, struct hid_report, list);
	field = report->field[0];
	if (!field) {
		hid_err(hid, "NULL field\n");
		return -1;
	}

	for (i = 0; i < ARRAY_SIZE(devices); i++) {
		if (dev->id.vendor == devices[i].idVendor &&
		    dev->id.product == devices[i].idProduct) {
			ff_bits = devices[i].ff;
			break;
		}
	}

	for (i = 0; ff_bits[i] >= 0; i++)
		set_bit(ff_bits[i], dev->ffbit);

	error = input_ff_create_memless(dev, NULL, hid_lgff_play);
	if (error)
		return error;

	if ( test_bit(FF_AUTOCENTER, dev->ffbit) )
		dev->ff->set_autocenter = hid_lgff_set_autocenter;

	pr_info("Force feedback for Logitech force feedback devices by Johann Deneux <[email protected]>\n");

	return 0;
}
#1 lore raw
Quoted 1 line

Code snipet below (full file attached):

Whoops, wrong file attached.
Simon.

/*
* Force feedback support for Logitech Speed Force Wireless *
* http://wiibrew.org/wiki/Logitech_USB_steering_wheel
*
* Copyright (c) 2010 Simon Wood <[email protected]> */

/*
* 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 */

#include <linux/input.h>
#include <linux/usb.h>
#include <linux/hid.h>
#include <linux/leds.h>

#include "usbhid/usbhid.h"
#include "hid-lg.h"
#include "hid-ids.h"

#define DFGT_REV_MAJ 0x13
#define DFGT_REV_MIN 0x22
#define DFP_REV_MAJ 0x11
#define DFP_REV_MIN 0x06
#define FFEX_REV_MAJ 0x21
#define FFEX_REV_MIN 0x00
#define G25_REV_MAJ 0x12
#define G25_REV_MIN 0x22
#define G27_REV_MAJ 0x12
#define G27_REV_MIN 0x38

#define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)

static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf); static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);

static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store);

#if 0

struct g27_leds {
	char * name;
};

static struct g27_leds g27_leds[] = {
#else
static char * g27_leds[] = {

#endif
	"g27:green:rpm1",
	"g27:green:rpm2",
	"g27:yellow:rpm3",
	"g27:yellow:rpm4",
	"g27:red:rpm5"
	};
struct lg4ff_device_entry {
	__u16 range;
	__u16 min_range;
	__u16 max_range;
	__u8  led_state;
	struct led_classdev *led[5];
	void (*set_range)(struct hid_device *hid, u16 range);
};
static const signed short lg4ff_wheel_effects[] = {
	FF_CONSTANT,
	FF_AUTOCENTER,
	-1
};
struct lg4ff_wheel {
	const __u32 product_id;
	const signed short *ff_effects;
	const __u16 min_range;
	const __u16 max_range;
	void (*set_range)(struct hid_device *hid, u16 range);
};
static const struct lg4ff_wheel lg4ff_devices[] = {
	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
};
struct lg4ff_native_cmd {
	const u8 cmd_num;	/* Number of commands to send */
	const u8 cmd[];
};
struct lg4ff_usb_revision {
	const u16 rev_maj;
	const u16 rev_min;
	const struct lg4ff_native_cmd *command;
};
static const struct lg4ff_native_cmd native_dfp = {
	1,
	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
};
static const struct lg4ff_native_cmd native_dfgt = {
	2,
	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
};
static const struct lg4ff_native_cmd native_g25 = {
	1,
	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
};
static const struct lg4ff_native_cmd native_g27 = {
	2,
	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
};
static const struct lg4ff_usb_revision lg4ff_revs[] = {
	{DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt},	/* Driving Force GT */
	{DFP_REV_MAJ,  DFP_REV_MIN,  &native_dfp},	/* Driving Force Pro */
	{G25_REV_MAJ,  G25_REV_MIN,  &native_g25},	/* G25 */
	{G27_REV_MAJ,  G27_REV_MIN,  &native_g27},	/* G27 */
};

static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)

{
	struct hid_device *hid = input_get_drvdata(dev);
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
	int x;

#define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff

	switch (effect->type) {
	case FF_CONSTANT:
		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
		CLAMP(x);
		report->field[0]->value[0] = 0x11;	/* Slot 1 */
		report->field[0]->value[1] = 0x08;
		report->field[0]->value[2] = x;
		report->field[0]->value[3] = 0x80;
		report->field[0]->value[4] = 0x00;
		report->field[0]->value[5] = 0x00;
		report->field[0]->value[6] = 0x00;

		usbhid_submit_report(hid, report, USB_DIR_OUT);
		break;
	}
	return 0;
}

/* Sends default autocentering command compatible with
* all wheels except Formula Force EX */
static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)

{
	struct hid_device *hid = input_get_drvdata(dev);
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);

	report->field[0]->value[0] = 0xfe;
	report->field[0]->value[1] = 0x0d;
	report->field[0]->value[2] = magnitude >> 13;
	report->field[0]->value[3] = magnitude >> 13;
	report->field[0]->value[4] = magnitude >> 8;
	report->field[0]->value[5] = 0x00;
	report->field[0]->value[6] = 0x00;

	usbhid_submit_report(hid, report, USB_DIR_OUT);
}

/* Sends autocentering command compatible with Formula Force EX */ static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)

{
	struct hid_device *hid = input_get_drvdata(dev);
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
	magnitude = magnitude * 90 / 65535;
	

	report->field[0]->value[0] = 0xfe;
	report->field[0]->value[1] = 0x03;
	report->field[0]->value[2] = magnitude >> 14;
	report->field[0]->value[3] = magnitude >> 14;
	report->field[0]->value[4] = magnitude;
	report->field[0]->value[5] = 0x00;
	report->field[0]->value[6] = 0x00;

	usbhid_submit_report(hid, report, USB_DIR_OUT);
}

/* Sends command to set range compatible with G25/G27/Driving Force GT */ static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)

{
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);

	report->field[0]->value[0] = 0xf8;
	report->field[0]->value[1] = 0x81;
	report->field[0]->value[2] = range & 0x00ff;
	report->field[0]->value[3] = (range & 0xff00) >> 8;
	report->field[0]->value[4] = 0x00;
	report->field[0]->value[5] = 0x00;
	report->field[0]->value[6] = 0x00;

	usbhid_submit_report(hid, report, USB_DIR_OUT);
}

/* Sends commands to set range compatible with Driving Force Pro wheel */ static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)

{
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
	int start_left, start_right, full_range;
	dbg_hid("Driving Force Pro: setting range to %u\n", range);

	/* Prepare "coarse" limit command */
	report->field[0]->value[0] = 0xf8;
	report->field[0]->value[1] = 0x00; 	/* Set later */
	report->field[0]->value[2] = 0x00;
	report->field[0]->value[3] = 0x00;
	report->field[0]->value[4] = 0x00;
	report->field[0]->value[5] = 0x00;
	report->field[0]->value[6] = 0x00;

	if (range > 200) {
		report->field[0]->value[1] = 0x03;
		full_range = 900;
	} else {
		report->field[0]->value[1] = 0x02;
		full_range = 200;
	}
	usbhid_submit_report(hid, report, USB_DIR_OUT);

	/* Prepare "fine" limit command */
	report->field[0]->value[0] = 0x81;
	report->field[0]->value[1] = 0x0b;
	report->field[0]->value[2] = 0x00;
	report->field[0]->value[3] = 0x00;
	report->field[0]->value[4] = 0x00;
	report->field[0]->value[5] = 0x00;
	report->field[0]->value[6] = 0x00;

	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
		usbhid_submit_report(hid, report, USB_DIR_OUT);
		return;
	}

	/* Construct fine limit command */
	start_left = (((full_range - range + 1) * 2047) / full_range);
	start_right = 0xfff - start_left;

	report->field[0]->value[2] = start_left >> 4;
	report->field[0]->value[3] = start_right >> 4;
	report->field[0]->value[4] = 0xff;
	report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
	report->field[0]->value[6] = 0xff;

	usbhid_submit_report(hid, report, USB_DIR_OUT);
}

static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)

{
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
	__u8 i, j;

	j = 0;
	while (j < 7*cmd->cmd_num) {
		for (i = 0; i < 7; i++)
			report->field[0]->value[i] = cmd->cmd[j++];

		usbhid_submit_report(hid, report, USB_DIR_OUT);
	}
}

/* Read current range and display it in terminal */
static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)

{
	struct hid_device *hid = to_hid_device(dev);
	struct lg_drv_data *drv_data = (struct lg_drv_data *)hid_get_drvdata(hid);
	struct lg4ff_device_entry *uninitialized_var(entry);
	size_t count;
	
	if (!drv_data) {
		hid_err(hid, "Device data not found.");
		return 0;
	}
	
	entry = (struct lg4ff_device_entry *)drv_data->dev_props;
	if (!entry) {
		hid_err(hid, "Device properties not found.");
		return 0;
	}

	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
	return count;
}

/* Set range to user specified value, call appropriate function
* according to the type of the wheel */
static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)

{
	struct hid_device *hid = to_hid_device(dev);
	struct lg_drv_data* drv_data = (struct lg_drv_data *)hid_get_drvdata(hid);
	struct lg4ff_device_entry *uninitialized_var(entry);
	__u16 range = simple_strtoul(buf, NULL, 10);

	if (!drv_data) {
		hid_err(hid, "Device data not found.");
		return count;
	}

	entry = (struct lg4ff_device_entry *)drv_data->dev_props; 
	if (!entry) {
		hid_err(hid, "Device properties not found.");
		return count;
	}

	if (range == 0)
		range = entry->max_range;

	/* Check if the wheel supports range setting
	 * and that the range is within limits for the wheel */
	if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
		entry->set_range(hid, range);
		entry->range = range;
	}

	return count;
}

static void lg4ff_set_leds(struct hid_device *hid, __u8 leds)

{
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);

	report->field[0]->value[0] = 0xf8;
	report->field[0]->value[1] = 0x12;
	report->field[0]->value[2] = leds;
	report->field[0]->value[3] = 0x00;
	report->field[0]->value[4] = 0x00;
	report->field[0]->value[5] = 0x00;
	report->field[0]->value[6] = 0x00;
	usbhid_submit_report(hid, report, USB_DIR_OUT);
}

static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
enum led_brightness value)

{
	struct device *dev = led_cdev->dev->parent;
	struct hid_device *hid = to_hid_device(dev);
	struct lg_drv_data* drv_data = (struct lg_drv_data *)hid_get_drvdata(hid);
	struct lg4ff_device_entry *uninitialized_var(entry);
	int i, state = 0;

	if (!drv_data) {
		hid_err(hid, "Device data not found.");
		return;
	}

	entry = (struct lg4ff_device_entry *)drv_data->dev_props; 
	if (!entry) {
		hid_err(hid, "Device properties not found.");
		return;
	}

	for (i = 0; i < 5; i++) {
		if (led_cdev != entry->led[i])
			continue;
		state = (entry->led_state >> i) & 1;
		if (value == LED_OFF && state) {
			entry->led_state &= ~(1 << i);
			lg4ff_set_leds(hid, entry->led_state);
		} else if (value != LED_OFF && !state) {
			entry->led_state |= 1 << i;
			lg4ff_set_leds(hid, entry->led_state);
		}
		break;
	}
}

static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)

{
	struct device *dev = led_cdev->dev->parent;
	struct hid_device *hid = to_hid_device(dev);
	struct lg_drv_data* drv_data = (struct lg_drv_data *)hid_get_drvdata(hid);
	struct lg4ff_device_entry *uninitialized_var(entry);
	int i, value=0;

	if (!drv_data) {
		hid_err(hid, "Device data not found.");
		return LED_OFF;
	}

	entry = (struct lg4ff_device_entry *)drv_data->dev_props; 
	if (!entry) {
		hid_err(hid, "Device properties not found.");
		return LED_OFF;
	}

	for (i = 0; i < 5; i++)
		if (led_cdev == entry->led[i]) {
			value = (entry->led_state >> i) & 1;
			break;
		}

	return value ? LED_FULL : LED_OFF;
}

int lg4ff_init(struct hid_device *hid)

{
	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
	struct input_dev *dev = hidinput->input;
	struct hid_report *report;
	struct hid_field *field;
	struct lg_drv_data *drv_data;
	struct lg4ff_device_entry *entry;
	struct usb_device_descriptor *udesc;
	int error, i, j;
	__u16 bcdDevice, rev_maj, rev_min;
	struct led_classdev *led;
	struct device *dev2 = &dev->dev;

	/* Find the report to use */
	if (list_empty(report_list)) {
		hid_err(hid, "No output report found\n");
		return -1;
	}

	/* Check that the report looks ok */
	report = list_entry(report_list->next, struct hid_report, list);
	if (!report) {
		hid_err(hid, "NULL output report\n");
		return -1;
	}

	field = report->field[0];
	if (!field) {
		hid_err(hid, "NULL field\n");
		return -1;
	}

	/* Check what wheel has been connected */
	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
		if (hid->product == lg4ff_devices[i].product_id) {
			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
			break;
		}
	}

	if (i == ARRAY_SIZE(lg4ff_devices)) {
		hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
			     "LKML, Simon Wood <[email protected]> or Michal Maly <[email protected]>\n");
		return -1;
	}

	/* Attempt to switch wheel to native mode when applicable */
	udesc = &(hid_to_usb_dev(hid)->descriptor);
	if (!udesc) {
		hid_err(hid, "NULL USB device descriptor\n");
		return -1;
	}
	bcdDevice = le16_to_cpu(udesc->bcdDevice);
	rev_maj = bcdDevice >> 8;
	rev_min = bcdDevice & 0xff;

	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
		dbg_hid("Generic wheel detected, can it do native?\n");
		dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);

		for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
			if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
				hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
				hid_info(hid, "Switched to native mode\n");
			}
		}
	}

	/* Set supported force feedback capabilities */
	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);

	error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);

	if (error)
		return error;

	/* Check if autocentering is available and
	 * set the centering force to zero by default */
	if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
		if(rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN)	/* Formula Force EX expects different autocentering command */
			dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
		else
			dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;

		dev->ff->set_autocenter(dev, 0);
	}

	/* Add the device to device_list */
	entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
	if (!entry) {
		hid_err(hid, "Cannot add device, insufficient memory.\n");
		return -ENOMEM;
	}
	entry->min_range = lg4ff_devices[i].min_range;
	entry->max_range = lg4ff_devices[i].max_range;
	entry->set_range = lg4ff_devices[i].set_range;
	drv_data = (struct lg_drv_data *)hid_get_drvdata(hid);
	drv_data->dev_props = entry;

	/* Create sysfs interface for range */
	error = device_create_file(&hid->dev, &dev_attr_range);
	if (error)
		return error;
	dbg_hid("sysfs interface created for range\n");

	/* Set the maximum range to start with */
	entry->range = entry->max_range;
	if (entry->set_range != NULL)
		entry->set_range(hid, entry->range);

	/* register led subsystem - G27 only */
	//if (rev_maj == G27_REV_MAJ && rev_min == G27_REV_MIN) {
	if (rev_maj != G27_REV_MAJ || rev_min != G27_REV_MIN) {
		for (i = 0; i < 5; i++) {
			led = kzalloc(sizeof(struct led_classdev), GFP_KERNEL);
			if (!led) {
				dev_err(dev2, "can't allocate memory for LED %d\n", i);
				error = -ENOMEM;
				goto err;
			}
			led->name = (void *)(g27_leds[i]);
			led->brightness = 0;
			led->max_brightness = 1;
			led->brightness_get = lg4ff_led_get_brightness;
			led->brightness_set = lg4ff_led_set_brightness;

			entry->led[i] = led;
#if 1
			error = led_classdev_register(dev2, led);
			if (error) {
				dev_err(dev2, "can't register LED %d\n", i);
				goto err;
			}
#endif
		}

		dbg_hid("sysfs interface created for leds\n");

		/* Turn off all leds */
		entry->led_state = 0;
		lg4ff_set_leds(hid, 0);
	}

	hid_info(hid, "Force feedback for Logitech with led subsys\n");
	return 0;

err:

	/* Deregister LEDs (if any) */
	for (i = 0; i < 5; i++) {
		led = entry->led[i];
		entry->led[i] = NULL;
		if (!led)
			continue;
		led_classdev_unregister(led);
		kfree(led);
	}

	return error;
}

int lg4ff_deinit(struct hid_device *hid)

{
	struct lg_drv_data *drv_data = (struct lg_drv_data *)drv_data;
	struct lg4ff_device_entry *uninitialized_var(entry);
	struct led_classdev *led;
	int i;

	if (drv_data) {
		if (drv_data->dev_props) {
			entry = (struct lg4ff_device_entry *)drv_data->dev_props;
			if (entry) {
				/* Deregister LEDs (if any) */
				for (i = 0; i < 5; i++) {
					led = entry->led[i];
					entry->led[i] = NULL;
					if (!led)
						continue;
					led_classdev_unregister(led);
					kfree(led);
				}
			}

			kfree(drv_data->dev_props);
		}
	}

	device_remove_file(&hid->dev, &dev_attr_range);

	dbg_hid("Device successfully unregistered\n");
	return 0;
}
#2 lore raw
In reply to [email protected]
Quoted 17 lines · On Tue, 13 Mar 2012, [email protected] wrote:
Quoted 1 line

Not really, it's rather simple to use and as Mark said, it's a proper

infrastructure to use here.

Quoted 1 line

We already have HID drivers which are using this -- you can look at

PicoLCD or Wiimote drivers and you'll see that it's indeed fairly simple.

So _you_ say.... I'm getting an Oops when device attempts to init the LED subsytem. :-(
--

Mar 13 12:54:56 ubuntu kernel: [  458.639674] Call Trace:
Mar 13 12:54:56 ubuntu kernel: [  458.641143]  [<c13498ed>] ?
device_create+0x2d/0x30
Mar 13 12:54:56 ubuntu kernel: [  458.641245]  [<c1439119>]
led_classdev_register+0x99/0xf0
Mar 13 12:54:56 ubuntu kernel: [  458.641306]  [<e0977311>]
lg4ff_init+0x2a1/0x570 [hid_logitech]
Mar 13 12:54:56 ubuntu kernel: [  458.641313]  [<e0975427>]
Quoted 2 lines

lg_probe+0x147/0x2c0 [hid_logitech]
--

Could you please post the full oops?

Signature · 2 lines
Jiri Kosina
SUSE Labs
#3 lore raw
Quoted 2 lines

Could you please post the full oops?

--

Mar 13 14:05:55 ubuntu kernel: [ 3286.170380] logitech
0003:046D:C294.0002: input,hidraw0: USB HID v1.11 Joystick [PS3/USB
Cordless Wheel] on usb-0000:02:00.0-1/input0
Mar 13 14:05:55 ubuntu kernel: [ 3286.171462] BUG: unable to handle kernel
NULL pointer dereference at 00000091
Mar 13 14:05:55 ubuntu kernel: [ 3286.171543] IP: [<e09f3998>]
lg4ff_led_get_brightness+0x28/0x90 [hid_logitech]
Mar 13 14:05:55 ubuntu kernel: [ 3286.171931] *pde = 00000000
Mar 13 14:05:55 ubuntu kernel: [ 3286.171991] Oops: 0000 [#1] SMP
Mar 13 14:05:55 ubuntu kernel: [ 3286.172253] Modules linked in:

hid_logitech joydev ff_memless usbhid hid binfmt_misc snd_ens1371 gameport snd_ac97_codec ac97_bus snd_pcm snd_seq_midi snd_rawmidi snd_seq_midi_event snd_seq snd_timer snd_seq_device snd soundcore i2c_piix4 ppdev snd_page_alloc shpchp psmouse serio_raw lp vmw_balloon parport_pc parport floppy pcnet32 mptspi mptscsih mptbase scsi_transport_spi [last unloaded: hid_logitech]

Mar 13 14:05:55 ubuntu kernel: [ 3286.172434]
Mar 13 14:05:55 ubuntu kernel: [ 3286.172517] Pid: 16, comm: khubd Not
tainted 3.3.0-rc6-stock+ #10 VMware, Inc. VMware Virtual Platform/440BX
Desktop Reference Platform
Mar 13 14:05:55 ubuntu kernel: [ 3286.173257] EIP: 0060:[<e09f3998>]
EFLAGS: 00010202 CPU: 0
Mar 13 14:05:55 ubuntu kernel: [ 3286.173325] EIP is at
lg4ff_led_get_brightness+0x28/0x90 [hid_logitech]
Mar 13 14:05:55 ubuntu kernel: [ 3286.173393] EAX: 00000089 EBX: dd3d2540
ECX: 00000000 EDX: ddc9a000
Mar 13 14:05:55 ubuntu kernel: [ 3286.173406] ESI: dd3a51b8 EDI: dd3a51b8
EBP: de59fa94 ESP: de59fa84
Mar 13 14:05:55 ubuntu kernel: [ 3286.173419]  DS: 007b ES: 007b FS: 00d8
GS: 00e0 SS: 0068
Mar 13 14:05:55 ubuntu kernel: [ 3286.173452] Process khubd (pid: 16,
ti=de59e000 task=de4cbf70 task.ti=de59e000)
Mar 13 14:05:55 ubuntu kernel: [ 3286.173471] Stack:
Mar 13 14:05:55 ubuntu kernel: [ 3286.173517]  c13498ed de59fa94 dd3d2540
dda080a0 de59fab8 c1439119 de57c5c0 dd3a51b8
Mar 13 14:05:55 ubuntu kernel: [ 3286.173681]  00000000 dd3d2540 c16f79e0
e09f5e1d 00000000 de59fafc e09f4311 00002200
Mar 13 14:05:55 ubuntu kernel: [ 3286.173689]  00000000 dd3d23c0 00000002
ddc9ac64 00000000 dd3a51b8 00010800 dde10022
Mar 13 14:05:55 ubuntu kernel: [ 3286.173738] Call Trace:
Mar 13 14:05:55 ubuntu kernel: [ 3286.174090]  [<c13498ed>] ?
device_create+0x2d/0x30
Mar 13 14:05:55 ubuntu kernel: [ 3286.174212]  [<c1439119>]
led_classdev_register+0x99/0xf0
Mar 13 14:05:55 ubuntu kernel: [ 3286.174269]  [<e09f4311>]
lg4ff_init+0x2a1/0x570 [hid_logitech]
Mar 13 14:05:55 ubuntu kernel: [ 3286.174276]  [<e09f2427>]
lg_probe+0x147/0x2c0 [hid_logitech]
Mar 13 14:05:55 ubuntu kernel: [ 3286.174360]  [<c1027938>] ?
default_spin_lock_flags+0x8/0x10
Mar 13 14:05:55 ubuntu kernel: [ 3286.174638]  [<c153b6af>] ?
_raw_spin_lock_irqsave+0x2f/0x50
Mar 13 14:05:55 ubuntu kernel: [ 3286.174672]  [<e0a025a3>] ?
hid_match_device+0x83/0x90 [hid]
Mar 13 14:05:55 ubuntu kernel: [ 3286.174680]  [<e0a02bf0>]
hid_device_probe+0x80/0x100 [hid]
Mar 13 14:05:55 ubuntu kernel: [ 3286.174685]  [<c134b452>] ?
driver_sysfs_add+0x72/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174692]  [<c134b6cf>]
driver_probe_device+0x7f/0x190
Mar 13 14:05:55 ubuntu kernel: [ 3286.174698]  [<c134b8b9>]
__device_attach+0x49/0x60
Mar 13 14:05:55 ubuntu kernel: [ 3286.174703]  [<c134b870>] ?
__driver_attach+0x90/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174707]  [<c134a0ff>]
bus_for_each_drv+0x4f/0x70
Mar 13 14:05:55 ubuntu kernel: [ 3286.174712]  [<c134b5aa>]
device_attach+0x8a/0xa0
Mar 13 14:05:55 ubuntu kernel: [ 3286.174716]  [<c134b870>] ?
__driver_attach+0x90/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174721]  [<c134ae3f>]
bus_probe_device+0x6f/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174725]  [<c1349710>]
device_add+0x560/0x610
Mar 13 14:05:55 ubuntu kernel: [ 3286.174732]  [<e0a026b2>]
hid_add_device+0x92/0x1f0 [hid]
Mar 13 14:05:55 ubuntu kernel: [ 3286.174742]  [<e09a5c26>]
usbhid_probe+0x2e6/0x430 [usbhid]
Mar 13 14:05:55 ubuntu kernel: [ 3286.174753]  [<c13c7c06>]
usb_probe_interface+0xd6/0x1b0
Mar 13 14:05:55 ubuntu kernel: [ 3286.174766]  [<c11898c7>] ?
sysfs_create_link+0x17/0x20
Mar 13 14:05:55 ubuntu kernel: [ 3286.174772]  [<c134b6cf>]
driver_probe_device+0x7f/0x190
Mar 13 14:05:55 ubuntu kernel: [ 3286.174776]  [<c13c75fb>] ?
usb_device_match+0x6b/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174781]  [<c134b8b9>]
__device_attach+0x49/0x60
Mar 13 14:05:55 ubuntu kernel: [ 3286.174813]  [<c134b870>] ?
__driver_attach+0x90/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174821]  [<c134a0ff>]
bus_for_each_drv+0x4f/0x70
Mar 13 14:05:55 ubuntu kernel: [ 3286.174825]  [<c134b5aa>]
device_attach+0x8a/0xa0
Mar 13 14:05:55 ubuntu kernel: [ 3286.174830]  [<c134b870>] ?
__driver_attach+0x90/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174834]  [<c134ae3f>]
bus_probe_device+0x6f/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174839]  [<c1349710>]
device_add+0x560/0x610
Mar 13 14:05:55 ubuntu kernel: [ 3286.174845]  [<c13c6385>] ?
usb_set_configuration+0x4c5/0x650
Mar 13 14:05:55 ubuntu kernel: [ 3286.174850]  [<c13c63ee>]
usb_set_configuration+0x52e/0x650
Mar 13 14:05:55 ubuntu kernel: [ 3286.174859]  [<c13ce5eb>]
generic_probe+0x3b/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174864]  [<c11898c7>] ?
sysfs_create_link+0x17/0x20
Mar 13 14:05:55 ubuntu kernel: [ 3286.174868]  [<c13c7d06>]
usb_probe_device+0x26/0x50
Mar 13 14:05:55 ubuntu kernel: [ 3286.174873]  [<c134b6cf>]
driver_probe_device+0x7f/0x190
Mar 13 14:05:55 ubuntu kernel: [ 3286.174908]  [<c128c165>] ?
kobject_uevent_env+0x105/0x420
Mar 13 14:05:55 ubuntu kernel: [ 3286.174917]  [<c153b50d>] ?
_raw_spin_lock+0xd/0x10
Mar 13 14:05:55 ubuntu kernel: [ 3286.174922]  [<c134b8b9>]
__device_attach+0x49/0x60
Mar 13 14:05:55 ubuntu kernel: [ 3286.174925]  [<c134b870>] ?
__driver_attach+0x90/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174929]  [<c134a0ff>]
bus_for_each_drv+0x4f/0x70
Mar 13 14:05:55 ubuntu kernel: [ 3286.174934]  [<c134b5aa>]
device_attach+0x8a/0xa0
Mar 13 14:05:55 ubuntu kernel: [ 3286.174938]  [<c134b870>] ?
__driver_attach+0x90/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174942]  [<c134ae3f>]
bus_probe_device+0x6f/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.174947]  [<c1349710>]
device_add+0x560/0x610
Mar 13 14:05:55 ubuntu kernel: [ 3286.174956]  [<c13bcf00>] ?
usb_enumerate_device+0x20/0x100
Mar 13 14:05:55 ubuntu kernel: [ 3286.174965]  [<c13547c9>] ?
pm_runtime_forbid+0x49/0x60
Mar 13 14:05:55 ubuntu kernel: [ 3286.174969]  [<c13bf720>]
usb_new_device+0xe0/0x120
Mar 13 14:05:55 ubuntu kernel: [ 3286.174974]  [<c13c0b64>] ?
hub_thread+0xc44/0xfd0
Mar 13 14:05:55 ubuntu kernel: [ 3286.174978]  [<c13c094f>]
hub_thread+0xa2f/0xfd0
Mar 13 14:05:55 ubuntu kernel: [ 3286.174988]  [<c1052220>] ?
wake_up_bit+0x30/0x30
Mar 13 14:05:55 ubuntu kernel: [ 3286.174993]  [<c13bff20>] ?
usb_remote_wakeup+0x50/0x50
Mar 13 14:05:55 ubuntu kernel: [ 3286.174997]  [<c1051c3c>] kthread+0x7c/0x90
Mar 13 14:05:55 ubuntu kernel: [ 3286.175001]  [<c1051bc0>] ?
flush_kthread_worker+0xb0/0xb0
Mar 13 14:05:55 ubuntu kernel: [ 3286.175028]  [<c15428be>]
kernel_thread_helper+0x6/0x10
Mar 13 14:05:55 ubuntu kernel: [ 3286.175075] Code: 00 00 00 55 89 e5 56
53 83 ec 08 3e 8d 74 26 00 89 c3 8b 40 1c 8b 30 89 f0 e8 a5 78 95 e0 85 c0
74 39 8b 40 04 31 c9 85 c0 74 48 <39> 5c 88 08 74 12 83 c1 01 83 f9 05 75
f2 31 c0 83 c4 08 5b 5e
Mar 13 14:05:55 ubuntu kernel: [ 3286.175218] EIP: [<e09f3998>]
lg4ff_led_get_brightness+0x28/0x90 [hid_logitech] SS:ESP 0068:de59fa84
Mar 13 14:05:55 ubuntu kernel: [ 3286.175263] CR2: 0000000000000091
Mar 13 14:05:55 ubuntu kernel: [ 3286.175702] ---[ end trace
bae956db68dd6567 ]---
Mar 13 14:06:00 ubuntu dhclient: DHCPREQUEST of 192.168.227.143 on eth0 to
192.168.227.254 port 67
Mar 13 14:06:00 ubuntu dhclient: DHCPACK of 192.168.227.143 from
192.168.227.254
Mar 13 14:06:00 ubuntu dhclient: bound to 192.168.227.143 -- renewal in

736 seconds.
--

#4 lore raw
In reply to Jiri Kosina
Quoted 3 lines

Could you please post the full oops?

Just so you don't burn time on this. I think I've got it figured, no longer Oops (just locks up the machine the second time wheel is connected).

Going home to try with a real G27...
Simon