xref: /openbmc/linux/drivers/hid/hid-lg4ff.c (revision 6a2e176b2d6ae6bb528c0c1a50a6332e176cda12)
132c88cbcSSimon Wood /*
232c88cbcSSimon Wood  *  Force feedback support for Logitech Speed Force Wireless
332c88cbcSSimon Wood  *
432c88cbcSSimon Wood  *  http://wiibrew.org/wiki/Logitech_USB_steering_wheel
532c88cbcSSimon Wood  *
632c88cbcSSimon Wood  *  Copyright (c) 2010 Simon Wood <simon@mungewell.org>
732c88cbcSSimon Wood  */
832c88cbcSSimon Wood 
932c88cbcSSimon Wood /*
1032c88cbcSSimon Wood  * This program is free software; you can redistribute it and/or modify
1132c88cbcSSimon Wood  * it under the terms of the GNU General Public License as published by
1232c88cbcSSimon Wood  * the Free Software Foundation; either version 2 of the License, or
1332c88cbcSSimon Wood  * (at your option) any later version.
1432c88cbcSSimon Wood  *
1532c88cbcSSimon Wood  * This program is distributed in the hope that it will be useful,
1632c88cbcSSimon Wood  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1732c88cbcSSimon Wood  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1832c88cbcSSimon Wood  * GNU General Public License for more details.
1932c88cbcSSimon Wood  *
2032c88cbcSSimon Wood  * You should have received a copy of the GNU General Public License
2132c88cbcSSimon Wood  * along with this program; if not, write to the Free Software
2232c88cbcSSimon Wood  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2332c88cbcSSimon Wood  */
2432c88cbcSSimon Wood 
2532c88cbcSSimon Wood 
2632c88cbcSSimon Wood #include <linux/input.h>
2732c88cbcSSimon Wood #include <linux/usb.h>
2832c88cbcSSimon Wood #include <linux/hid.h>
2932c88cbcSSimon Wood 
3032c88cbcSSimon Wood #include "usbhid/usbhid.h"
3132c88cbcSSimon Wood #include "hid-lg.h"
327362cd22SMichal Malý #include "hid-ids.h"
3332c88cbcSSimon Wood 
3496440c8aSMichal Malý #define DFGT_REV_MAJ 0x13
3596440c8aSMichal Malý #define DFGT_REV_MIN 0x22
3696440c8aSMichal Malý #define DFP_REV_MAJ 0x11
3796440c8aSMichal Malý #define DFP_REV_MIN 0x06
3896440c8aSMichal Malý #define FFEX_REV_MAJ 0x21
3996440c8aSMichal Malý #define FFEX_REV_MIN 0x00
4096440c8aSMichal Malý #define G25_REV_MAJ 0x12
4196440c8aSMichal Malý #define G25_REV_MIN 0x22
4296440c8aSMichal Malý #define G27_REV_MAJ 0x12
4396440c8aSMichal Malý #define G27_REV_MIN 0x38
4496440c8aSMichal Malý 
4530bb75d7SMichal Malý #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev)
4630bb75d7SMichal Malý 
4730bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
4830bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range);
4930bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf);
5030bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count);
5130bb75d7SMichal Malý 
5230bb75d7SMichal Malý static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store);
5330bb75d7SMichal Malý 
5430bb75d7SMichal Malý static bool list_inited;
5530bb75d7SMichal Malý 
5630bb75d7SMichal Malý struct lg4ff_device_entry {
5730bb75d7SMichal Malý 	char  *device_id;	/* Use name in respective kobject structure's address as the ID */
5830bb75d7SMichal Malý 	__u16 range;
5930bb75d7SMichal Malý 	__u16 min_range;
6030bb75d7SMichal Malý 	__u16 max_range;
6130bb75d7SMichal Malý 	__u8  leds;
6230bb75d7SMichal Malý 	struct list_head list;
6330bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
6430bb75d7SMichal Malý };
6530bb75d7SMichal Malý 
6630bb75d7SMichal Malý static struct lg4ff_device_entry device_list;
6730bb75d7SMichal Malý 
687362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
6932c88cbcSSimon Wood 	FF_CONSTANT,
7032c88cbcSSimon Wood 	FF_AUTOCENTER,
7132c88cbcSSimon Wood 	-1
7232c88cbcSSimon Wood };
7332c88cbcSSimon Wood 
747362cd22SMichal Malý struct lg4ff_wheel {
757362cd22SMichal Malý 	const __u32 product_id;
767362cd22SMichal Malý 	const signed short *ff_effects;
777362cd22SMichal Malý 	const __u16 min_range;
787362cd22SMichal Malý 	const __u16 max_range;
7930bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
807362cd22SMichal Malý };
817362cd22SMichal Malý 
827362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
8330bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
8430bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
8530bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
8630bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8730bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8830bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8930bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
9030bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
917362cd22SMichal Malý };
927362cd22SMichal Malý 
9396440c8aSMichal Malý struct lg4ff_native_cmd {
9496440c8aSMichal Malý 	const __u8 cmd_num;	/* Number of commands to send */
9596440c8aSMichal Malý 	const __u8 cmd[];
9696440c8aSMichal Malý };
9796440c8aSMichal Malý 
9896440c8aSMichal Malý struct lg4ff_usb_revision {
9996440c8aSMichal Malý 	const __u16 rev_maj;
10096440c8aSMichal Malý 	const __u16 rev_min;
10196440c8aSMichal Malý 	const struct lg4ff_native_cmd *command;
10296440c8aSMichal Malý };
10396440c8aSMichal Malý 
10496440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfp = {
10596440c8aSMichal Malý 	1,
10696440c8aSMichal Malý 	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
10796440c8aSMichal Malý };
10896440c8aSMichal Malý 
10996440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfgt = {
11096440c8aSMichal Malý 	2,
11196440c8aSMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
11296440c8aSMichal Malý 	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
11396440c8aSMichal Malý };
11496440c8aSMichal Malý 
11596440c8aSMichal Malý static const struct lg4ff_native_cmd native_g25 = {
11696440c8aSMichal Malý 	1,
11796440c8aSMichal Malý 	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
11896440c8aSMichal Malý };
11996440c8aSMichal Malý 
12096440c8aSMichal Malý static const struct lg4ff_native_cmd native_g27 = {
12196440c8aSMichal Malý 	2,
12296440c8aSMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
12396440c8aSMichal Malý 	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
12496440c8aSMichal Malý };
12596440c8aSMichal Malý 
12696440c8aSMichal Malý static const struct lg4ff_usb_revision lg4ff_revs[] = {
12796440c8aSMichal Malý 	{DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt},	/* Driving Force GT */
12896440c8aSMichal Malý 	{DFP_REV_MAJ,  DFP_REV_MIN,  &native_dfp},	/* Driving Force Pro */
12996440c8aSMichal Malý 	{G25_REV_MAJ,  G25_REV_MIN,  &native_g25},	/* G25 */
13096440c8aSMichal Malý 	{G27_REV_MAJ,  G27_REV_MIN,  &native_g27},	/* G27 */
13196440c8aSMichal Malý };
13296440c8aSMichal Malý 
13330bb75d7SMichal Malý static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
13432c88cbcSSimon Wood {
13532c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
13632c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
13732c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
13832c88cbcSSimon Wood 	int x;
13932c88cbcSSimon Wood 
14032c88cbcSSimon Wood #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
14132c88cbcSSimon Wood 
14232c88cbcSSimon Wood 	switch (effect->type) {
14332c88cbcSSimon Wood 	case FF_CONSTANT:
14432c88cbcSSimon Wood 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
14532c88cbcSSimon Wood 		CLAMP(x);
14632c88cbcSSimon Wood 		report->field[0]->value[0] = 0x11;	/* Slot 1 */
1477362cd22SMichal Malý 		report->field[0]->value[1] = 0x08;
14832c88cbcSSimon Wood 		report->field[0]->value[2] = x;
1497362cd22SMichal Malý 		report->field[0]->value[3] = 0x80;
15032c88cbcSSimon Wood 		report->field[0]->value[4] = 0x00;
1517362cd22SMichal Malý 		report->field[0]->value[5] = 0x00;
15232c88cbcSSimon Wood 		report->field[0]->value[6] = 0x00;
15332c88cbcSSimon Wood 
15432c88cbcSSimon Wood 		usbhid_submit_report(hid, report, USB_DIR_OUT);
15532c88cbcSSimon Wood 		break;
15632c88cbcSSimon Wood 	}
15732c88cbcSSimon Wood 	return 0;
15832c88cbcSSimon Wood }
15932c88cbcSSimon Wood 
1606e2de8e0SMichal Malý /* Sends default autocentering command compatible with
1616e2de8e0SMichal Malý  * all wheels except Formula Force EX */
1626e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
16332c88cbcSSimon Wood {
16432c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
16532c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
16632c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
16732c88cbcSSimon Wood 
1687362cd22SMichal Malý 	report->field[0]->value[0] = 0xfe;
1697362cd22SMichal Malý 	report->field[0]->value[1] = 0x0d;
1707362cd22SMichal Malý 	report->field[0]->value[2] = magnitude >> 13;
1717362cd22SMichal Malý 	report->field[0]->value[3] = magnitude >> 13;
1727362cd22SMichal Malý 	report->field[0]->value[4] = magnitude >> 8;
1737362cd22SMichal Malý 	report->field[0]->value[5] = 0x00;
1747362cd22SMichal Malý 	report->field[0]->value[6] = 0x00;
17532c88cbcSSimon Wood 
17632c88cbcSSimon Wood 	usbhid_submit_report(hid, report, USB_DIR_OUT);
17732c88cbcSSimon Wood }
17832c88cbcSSimon Wood 
1796e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */
1806e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
1816e2de8e0SMichal Malý {
1826e2de8e0SMichal Malý 	struct hid_device *hid = input_get_drvdata(dev);
1836e2de8e0SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1846e2de8e0SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1856e2de8e0SMichal Malý 	magnitude = magnitude * 90 / 65535;
1866e2de8e0SMichal Malý 
1876e2de8e0SMichal Malý 
1886e2de8e0SMichal Malý 	report->field[0]->value[0] = 0xfe;
1896e2de8e0SMichal Malý 	report->field[0]->value[1] = 0x03;
1906e2de8e0SMichal Malý 	report->field[0]->value[2] = magnitude >> 14;
1916e2de8e0SMichal Malý 	report->field[0]->value[3] = magnitude >> 14;
1926e2de8e0SMichal Malý 	report->field[0]->value[4] = magnitude;
1936e2de8e0SMichal Malý 	report->field[0]->value[5] = 0x00;
1946e2de8e0SMichal Malý 	report->field[0]->value[6] = 0x00;
1956e2de8e0SMichal Malý 
1966e2de8e0SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
1976e2de8e0SMichal Malý }
1986e2de8e0SMichal Malý 
19930bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */
20030bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
20130bb75d7SMichal Malý {
20230bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
20330bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
20430bb75d7SMichal Malý 	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
20530bb75d7SMichal Malý 
20630bb75d7SMichal Malý 	report->field[0]->value[0] = 0xf8;
20730bb75d7SMichal Malý 	report->field[0]->value[1] = 0x81;
20830bb75d7SMichal Malý 	report->field[0]->value[2] = range & 0x00ff;
20930bb75d7SMichal Malý 	report->field[0]->value[3] = (range & 0xff00) >> 8;
21030bb75d7SMichal Malý 	report->field[0]->value[4] = 0x00;
21130bb75d7SMichal Malý 	report->field[0]->value[5] = 0x00;
21230bb75d7SMichal Malý 	report->field[0]->value[6] = 0x00;
21330bb75d7SMichal Malý 
21430bb75d7SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
21530bb75d7SMichal Malý }
21630bb75d7SMichal Malý 
21730bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */
21830bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
21930bb75d7SMichal Malý {
22030bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
22130bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
22230bb75d7SMichal Malý 	int start_left, start_right, full_range;
22330bb75d7SMichal Malý 	dbg_hid("Driving Force Pro: setting range to %u\n", range);
22430bb75d7SMichal Malý 
22530bb75d7SMichal Malý 	/* Prepare "coarse" limit command */
22630bb75d7SMichal Malý 	report->field[0]->value[0] = 0xf8;
22730bb75d7SMichal Malý 	report->field[0]->value[1] = 0x00; 	/* Set later */
22830bb75d7SMichal Malý 	report->field[0]->value[2] = 0x00;
22930bb75d7SMichal Malý 	report->field[0]->value[3] = 0x00;
23030bb75d7SMichal Malý 	report->field[0]->value[4] = 0x00;
23130bb75d7SMichal Malý 	report->field[0]->value[5] = 0x00;
23230bb75d7SMichal Malý 	report->field[0]->value[6] = 0x00;
23330bb75d7SMichal Malý 
23430bb75d7SMichal Malý 	if (range > 200) {
23530bb75d7SMichal Malý 		report->field[0]->value[1] = 0x03;
23630bb75d7SMichal Malý 		full_range = 900;
23730bb75d7SMichal Malý 	} else {
23830bb75d7SMichal Malý 		report->field[0]->value[1] = 0x02;
23930bb75d7SMichal Malý 		full_range = 200;
24030bb75d7SMichal Malý 	}
24130bb75d7SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
24230bb75d7SMichal Malý 
24330bb75d7SMichal Malý 	/* Prepare "fine" limit command */
24430bb75d7SMichal Malý 	report->field[0]->value[0] = 0x81;
24530bb75d7SMichal Malý 	report->field[0]->value[1] = 0x0b;
24630bb75d7SMichal Malý 	report->field[0]->value[2] = 0x00;
24730bb75d7SMichal Malý 	report->field[0]->value[3] = 0x00;
24830bb75d7SMichal Malý 	report->field[0]->value[4] = 0x00;
24930bb75d7SMichal Malý 	report->field[0]->value[5] = 0x00;
25030bb75d7SMichal Malý 	report->field[0]->value[6] = 0x00;
25130bb75d7SMichal Malý 
25230bb75d7SMichal Malý 	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
25330bb75d7SMichal Malý 		usbhid_submit_report(hid, report, USB_DIR_OUT);
25430bb75d7SMichal Malý 		return;
25530bb75d7SMichal Malý 	}
25630bb75d7SMichal Malý 
25730bb75d7SMichal Malý 	/* Construct fine limit command */
25830bb75d7SMichal Malý 	start_left = (((full_range - range + 1) * 2047) / full_range);
25930bb75d7SMichal Malý 	start_right = 0xfff - start_left;
26030bb75d7SMichal Malý 
26130bb75d7SMichal Malý 	report->field[0]->value[2] = start_left >> 4;
26230bb75d7SMichal Malý 	report->field[0]->value[3] = start_right >> 4;
26330bb75d7SMichal Malý 	report->field[0]->value[4] = 0xff;
26430bb75d7SMichal Malý 	report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
26530bb75d7SMichal Malý 	report->field[0]->value[6] = 0xff;
26630bb75d7SMichal Malý 
26730bb75d7SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
26830bb75d7SMichal Malý }
26930bb75d7SMichal Malý 
27096440c8aSMichal Malý static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
27196440c8aSMichal Malý {
27296440c8aSMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
27396440c8aSMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
27496440c8aSMichal Malý 	__u8 i, j;
27596440c8aSMichal Malý 
27696440c8aSMichal Malý 	j = 0;
27796440c8aSMichal Malý 	while (j < 7*cmd->cmd_num) {
27896440c8aSMichal Malý 		for (i = 0; i < 7; i++)
27996440c8aSMichal Malý 			report->field[0]->value[i] = cmd->cmd[j++];
28096440c8aSMichal Malý 
28196440c8aSMichal Malý 		usbhid_submit_report(hid, report, USB_DIR_OUT);
28296440c8aSMichal Malý 	}
28396440c8aSMichal Malý }
28432c88cbcSSimon Wood 
28530bb75d7SMichal Malý /* Read current range and display it in terminal */
28630bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
28730bb75d7SMichal Malý {
2882bbaf771SDan Carpenter 	struct lg4ff_device_entry *uninitialized_var(entry);
28930bb75d7SMichal Malý 	struct list_head *h;
29030bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
29130bb75d7SMichal Malý 	size_t count;
29230bb75d7SMichal Malý 
29330bb75d7SMichal Malý 	list_for_each(h, &device_list.list) {
29430bb75d7SMichal Malý 		entry = list_entry(h, struct lg4ff_device_entry, list);
29530bb75d7SMichal Malý 		if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
29630bb75d7SMichal Malý 			break;
29730bb75d7SMichal Malý 	}
29830bb75d7SMichal Malý 	if (h == &device_list.list) {
29930bb75d7SMichal Malý 		dbg_hid("Device not found!");
30030bb75d7SMichal Malý 		return 0;
30130bb75d7SMichal Malý 	}
30230bb75d7SMichal Malý 
30330bb75d7SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
30430bb75d7SMichal Malý 	return count;
30530bb75d7SMichal Malý }
30630bb75d7SMichal Malý 
30730bb75d7SMichal Malý /* Set range to user specified value, call appropriate function
30830bb75d7SMichal Malý  * according to the type of the wheel */
30930bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
31030bb75d7SMichal Malý {
3112bbaf771SDan Carpenter 	struct lg4ff_device_entry *uninitialized_var(entry);
31230bb75d7SMichal Malý 	struct list_head *h;
31330bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
31430bb75d7SMichal Malý 	__u16 range = simple_strtoul(buf, NULL, 10);
31530bb75d7SMichal Malý 
31630bb75d7SMichal Malý 	list_for_each(h, &device_list.list) {
31730bb75d7SMichal Malý 		entry = list_entry(h, struct lg4ff_device_entry, list);
31830bb75d7SMichal Malý 		if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0)
31930bb75d7SMichal Malý 			break;
32030bb75d7SMichal Malý 	}
32130bb75d7SMichal Malý 	if (h == &device_list.list) {
32230bb75d7SMichal Malý 		dbg_hid("Device not found!");
32330bb75d7SMichal Malý 		return count;
32430bb75d7SMichal Malý 	}
32530bb75d7SMichal Malý 
32630bb75d7SMichal Malý 	if (range == 0)
32730bb75d7SMichal Malý 		range = entry->max_range;
32830bb75d7SMichal Malý 
32930bb75d7SMichal Malý 	/* Check if the wheel supports range setting
33030bb75d7SMichal Malý 	 * and that the range is within limits for the wheel */
33130bb75d7SMichal Malý 	if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
33230bb75d7SMichal Malý 		entry->set_range(hid, range);
33330bb75d7SMichal Malý 		entry->range = range;
33430bb75d7SMichal Malý 	}
33530bb75d7SMichal Malý 
33630bb75d7SMichal Malý 	return count;
33730bb75d7SMichal Malý }
33830bb75d7SMichal Malý 
33932c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
34032c88cbcSSimon Wood {
34132c88cbcSSimon Wood 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
34232c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
34332c88cbcSSimon Wood 	struct input_dev *dev = hidinput->input;
34432c88cbcSSimon Wood 	struct hid_report *report;
34532c88cbcSSimon Wood 	struct hid_field *field;
34630bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
34730bb75d7SMichal Malý 	struct usb_device_descriptor *udesc;
3487362cd22SMichal Malý 	int error, i, j;
34996440c8aSMichal Malý 	__u16 bcdDevice, rev_maj, rev_min;
35032c88cbcSSimon Wood 
35132c88cbcSSimon Wood 	/* Find the report to use */
35232c88cbcSSimon Wood 	if (list_empty(report_list)) {
3534291ee30SJoe Perches 		hid_err(hid, "No output report found\n");
35432c88cbcSSimon Wood 		return -1;
35532c88cbcSSimon Wood 	}
35632c88cbcSSimon Wood 
35732c88cbcSSimon Wood 	/* Check that the report looks ok */
35832c88cbcSSimon Wood 	report = list_entry(report_list->next, struct hid_report, list);
35932c88cbcSSimon Wood 	if (!report) {
3604291ee30SJoe Perches 		hid_err(hid, "NULL output report\n");
36132c88cbcSSimon Wood 		return -1;
36232c88cbcSSimon Wood 	}
36332c88cbcSSimon Wood 
36432c88cbcSSimon Wood 	field = report->field[0];
36532c88cbcSSimon Wood 	if (!field) {
3664291ee30SJoe Perches 		hid_err(hid, "NULL field\n");
36732c88cbcSSimon Wood 		return -1;
36832c88cbcSSimon Wood 	}
36932c88cbcSSimon Wood 
3707362cd22SMichal Malý 	/* Check what wheel has been connected */
3717362cd22SMichal Malý 	for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
3727362cd22SMichal Malý 		if (hid->product == lg4ff_devices[i].product_id) {
3737362cd22SMichal Malý 			dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
3747362cd22SMichal Malý 			break;
3757362cd22SMichal Malý 		}
3767362cd22SMichal Malý 	}
3777362cd22SMichal Malý 
3787362cd22SMichal Malý 	if (i == ARRAY_SIZE(lg4ff_devices)) {
3797362cd22SMichal Malý 		hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to"
3807362cd22SMichal Malý 			     "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n");
3817362cd22SMichal Malý 		return -1;
3827362cd22SMichal Malý 	}
3837362cd22SMichal Malý 
38496440c8aSMichal Malý 	/* Attempt to switch wheel to native mode when applicable */
38596440c8aSMichal Malý 	udesc = &(hid_to_usb_dev(hid)->descriptor);
38696440c8aSMichal Malý 	if (!udesc) {
38796440c8aSMichal Malý 		hid_err(hid, "NULL USB device descriptor\n");
38896440c8aSMichal Malý 		return -1;
38996440c8aSMichal Malý 	}
39096440c8aSMichal Malý 	bcdDevice = le16_to_cpu(udesc->bcdDevice);
39196440c8aSMichal Malý 	rev_maj = bcdDevice >> 8;
39296440c8aSMichal Malý 	rev_min = bcdDevice & 0xff;
39396440c8aSMichal Malý 
39496440c8aSMichal Malý 	if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) {
39596440c8aSMichal Malý 		dbg_hid("Generic wheel detected, can it do native?\n");
39696440c8aSMichal Malý 		dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min);
39796440c8aSMichal Malý 
39896440c8aSMichal Malý 		for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) {
39996440c8aSMichal Malý 			if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) {
40096440c8aSMichal Malý 				hid_lg4ff_switch_native(hid, lg4ff_revs[j].command);
40196440c8aSMichal Malý 				hid_info(hid, "Switched to native mode\n");
40296440c8aSMichal Malý 			}
40396440c8aSMichal Malý 		}
40496440c8aSMichal Malý 	}
40596440c8aSMichal Malý 
4067362cd22SMichal Malý 	/* Set supported force feedback capabilities */
4077362cd22SMichal Malý 	for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
4087362cd22SMichal Malý 		set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
40932c88cbcSSimon Wood 
41032c88cbcSSimon Wood 	error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
41132c88cbcSSimon Wood 
41232c88cbcSSimon Wood 	if (error)
41332c88cbcSSimon Wood 		return error;
41432c88cbcSSimon Wood 
4156e2de8e0SMichal Malý 	/* Check if autocentering is available and
4166e2de8e0SMichal Malý 	 * set the centering force to zero by default */
4176e2de8e0SMichal Malý 	if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
4186e2de8e0SMichal Malý 		if(rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN)	/* Formula Force EX expects different autocentering command */
4196e2de8e0SMichal Malý 			dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex;
4206e2de8e0SMichal Malý 		else
4216e2de8e0SMichal Malý 			dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default;
4226e2de8e0SMichal Malý 
4236e2de8e0SMichal Malý 		dev->ff->set_autocenter(dev, 0);
4246e2de8e0SMichal Malý 	}
42532c88cbcSSimon Wood 
42630bb75d7SMichal Malý 		/* Initialize device_list if this is the first device to handle by lg4ff */
42730bb75d7SMichal Malý 	if (!list_inited) {
42830bb75d7SMichal Malý 		INIT_LIST_HEAD(&device_list.list);
42930bb75d7SMichal Malý 		list_inited = 1;
43030bb75d7SMichal Malý 	}
43130bb75d7SMichal Malý 
43230bb75d7SMichal Malý 	/* Add the device to device_list */
4338383c6bfSThomas Meyer 	entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
43430bb75d7SMichal Malý 	if (!entry) {
43530bb75d7SMichal Malý 		hid_err(hid, "Cannot add device, insufficient memory.\n");
43630bb75d7SMichal Malý 		return -ENOMEM;
43730bb75d7SMichal Malý 	}
4388f252290SDan Carpenter 	entry->device_id = kstrdup((&hid->dev)->kobj.name, GFP_KERNEL);
43930bb75d7SMichal Malý 	if (!entry->device_id) {
44030bb75d7SMichal Malý 		hid_err(hid, "Cannot set device_id, insufficient memory.\n");
4418f252290SDan Carpenter 		kfree(entry);
44230bb75d7SMichal Malý 		return -ENOMEM;
44330bb75d7SMichal Malý 	}
44430bb75d7SMichal Malý 	entry->min_range = lg4ff_devices[i].min_range;
44530bb75d7SMichal Malý 	entry->max_range = lg4ff_devices[i].max_range;
44630bb75d7SMichal Malý 	entry->set_range = lg4ff_devices[i].set_range;
44730bb75d7SMichal Malý 	list_add(&entry->list, &device_list.list);
44830bb75d7SMichal Malý 
44930bb75d7SMichal Malý 	/* Create sysfs interface */
45030bb75d7SMichal Malý 	error = device_create_file(&hid->dev, &dev_attr_range);
45130bb75d7SMichal Malý 	if (error)
45230bb75d7SMichal Malý 		return error;
45330bb75d7SMichal Malý 	dbg_hid("sysfs interface created\n");
45430bb75d7SMichal Malý 
45530bb75d7SMichal Malý 	/* Set the maximum range to start with */
45630bb75d7SMichal Malý 	entry->range = entry->max_range;
45730bb75d7SMichal Malý 	if (entry->set_range != NULL)
45830bb75d7SMichal Malý 		entry->set_range(hid, entry->range);
45930bb75d7SMichal Malý 
4604291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
46132c88cbcSSimon Wood 	return 0;
46232c88cbcSSimon Wood }
46332c88cbcSSimon Wood 
46430bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid)
46530bb75d7SMichal Malý {
46630bb75d7SMichal Malý 	bool found = 0;
46730bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
46830bb75d7SMichal Malý 	struct list_head *h, *g;
469*6a2e176bSMichal Malý 
470*6a2e176bSMichal Malý 	device_remove_file(&hid->dev, &dev_attr_range);
471*6a2e176bSMichal Malý 
47230bb75d7SMichal Malý 	list_for_each_safe(h, g, &device_list.list) {
47330bb75d7SMichal Malý 		entry = list_entry(h, struct lg4ff_device_entry, list);
47430bb75d7SMichal Malý 		if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) {
47530bb75d7SMichal Malý 			list_del(h);
47630bb75d7SMichal Malý 			kfree(entry->device_id);
47730bb75d7SMichal Malý 			kfree(entry);
47830bb75d7SMichal Malý 			found = 1;
47930bb75d7SMichal Malý 			break;
48030bb75d7SMichal Malý 		}
48130bb75d7SMichal Malý 	}
48230bb75d7SMichal Malý 
48330bb75d7SMichal Malý 	if (!found) {
484*6a2e176bSMichal Malý 		hid_err(hid, "Device entry not found!\n");
48530bb75d7SMichal Malý 		return -1;
48630bb75d7SMichal Malý 	}
48730bb75d7SMichal Malý 
48830bb75d7SMichal Malý 	dbg_hid("Device successfully unregistered\n");
48930bb75d7SMichal Malý 	return 0;
49030bb75d7SMichal Malý }
491