xref: /openbmc/linux/drivers/hid/hid-lg4ff.c (revision 3b6b17b7330c0fab7d8442b6d225bee905df3745)
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ý struct lg4ff_device_entry {
5530bb75d7SMichal Malý 	__u16 range;
5630bb75d7SMichal Malý 	__u16 min_range;
5730bb75d7SMichal Malý 	__u16 max_range;
5830bb75d7SMichal Malý 	__u8  leds;
5930bb75d7SMichal Malý 	struct list_head list;
6030bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
6130bb75d7SMichal Malý };
6230bb75d7SMichal Malý 
637362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = {
6432c88cbcSSimon Wood 	FF_CONSTANT,
6532c88cbcSSimon Wood 	FF_AUTOCENTER,
6632c88cbcSSimon Wood 	-1
6732c88cbcSSimon Wood };
6832c88cbcSSimon Wood 
697362cd22SMichal Malý struct lg4ff_wheel {
707362cd22SMichal Malý 	const __u32 product_id;
717362cd22SMichal Malý 	const signed short *ff_effects;
727362cd22SMichal Malý 	const __u16 min_range;
737362cd22SMichal Malý 	const __u16 max_range;
7430bb75d7SMichal Malý 	void (*set_range)(struct hid_device *hid, u16 range);
757362cd22SMichal Malý };
767362cd22SMichal Malý 
777362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = {
7830bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
7930bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
8030bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp},
8130bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8230bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8330bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25},
8430bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
8530bb75d7SMichal Malý 	{USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
867362cd22SMichal Malý };
877362cd22SMichal Malý 
8896440c8aSMichal Malý struct lg4ff_native_cmd {
8996440c8aSMichal Malý 	const __u8 cmd_num;	/* Number of commands to send */
9096440c8aSMichal Malý 	const __u8 cmd[];
9196440c8aSMichal Malý };
9296440c8aSMichal Malý 
9396440c8aSMichal Malý struct lg4ff_usb_revision {
9496440c8aSMichal Malý 	const __u16 rev_maj;
9596440c8aSMichal Malý 	const __u16 rev_min;
9696440c8aSMichal Malý 	const struct lg4ff_native_cmd *command;
9796440c8aSMichal Malý };
9896440c8aSMichal Malý 
9996440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfp = {
10096440c8aSMichal Malý 	1,
10196440c8aSMichal Malý 	{0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
10296440c8aSMichal Malý };
10396440c8aSMichal Malý 
10496440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfgt = {
10596440c8aSMichal Malý 	2,
10696440c8aSMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
10796440c8aSMichal Malý 	 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
10896440c8aSMichal Malý };
10996440c8aSMichal Malý 
11096440c8aSMichal Malý static const struct lg4ff_native_cmd native_g25 = {
11196440c8aSMichal Malý 	1,
11296440c8aSMichal Malý 	{0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
11396440c8aSMichal Malý };
11496440c8aSMichal Malý 
11596440c8aSMichal Malý static const struct lg4ff_native_cmd native_g27 = {
11696440c8aSMichal Malý 	2,
11796440c8aSMichal Malý 	{0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,	/* 1st command */
11896440c8aSMichal Malý 	 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}	/* 2nd command */
11996440c8aSMichal Malý };
12096440c8aSMichal Malý 
12196440c8aSMichal Malý static const struct lg4ff_usb_revision lg4ff_revs[] = {
12296440c8aSMichal Malý 	{DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt},	/* Driving Force GT */
12396440c8aSMichal Malý 	{DFP_REV_MAJ,  DFP_REV_MIN,  &native_dfp},	/* Driving Force Pro */
12496440c8aSMichal Malý 	{G25_REV_MAJ,  G25_REV_MIN,  &native_g25},	/* G25 */
12596440c8aSMichal Malý 	{G27_REV_MAJ,  G27_REV_MIN,  &native_g27},	/* G27 */
12696440c8aSMichal Malý };
12796440c8aSMichal Malý 
12830bb75d7SMichal Malý static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
12932c88cbcSSimon Wood {
13032c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
13132c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
13232c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
13332c88cbcSSimon Wood 	int x;
13432c88cbcSSimon Wood 
13532c88cbcSSimon Wood #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
13632c88cbcSSimon Wood 
13732c88cbcSSimon Wood 	switch (effect->type) {
13832c88cbcSSimon Wood 	case FF_CONSTANT:
13932c88cbcSSimon Wood 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
14032c88cbcSSimon Wood 		CLAMP(x);
14132c88cbcSSimon Wood 		report->field[0]->value[0] = 0x11;	/* Slot 1 */
1427362cd22SMichal Malý 		report->field[0]->value[1] = 0x08;
14332c88cbcSSimon Wood 		report->field[0]->value[2] = x;
1447362cd22SMichal Malý 		report->field[0]->value[3] = 0x80;
14532c88cbcSSimon Wood 		report->field[0]->value[4] = 0x00;
1467362cd22SMichal Malý 		report->field[0]->value[5] = 0x00;
14732c88cbcSSimon Wood 		report->field[0]->value[6] = 0x00;
14832c88cbcSSimon Wood 
14932c88cbcSSimon Wood 		usbhid_submit_report(hid, report, USB_DIR_OUT);
15032c88cbcSSimon Wood 		break;
15132c88cbcSSimon Wood 	}
15232c88cbcSSimon Wood 	return 0;
15332c88cbcSSimon Wood }
15432c88cbcSSimon Wood 
1556e2de8e0SMichal Malý /* Sends default autocentering command compatible with
1566e2de8e0SMichal Malý  * all wheels except Formula Force EX */
1576e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
15832c88cbcSSimon Wood {
15932c88cbcSSimon Wood 	struct hid_device *hid = input_get_drvdata(dev);
16032c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
16132c88cbcSSimon Wood 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
16232c88cbcSSimon Wood 
1637362cd22SMichal Malý 	report->field[0]->value[0] = 0xfe;
1647362cd22SMichal Malý 	report->field[0]->value[1] = 0x0d;
1657362cd22SMichal Malý 	report->field[0]->value[2] = magnitude >> 13;
1667362cd22SMichal Malý 	report->field[0]->value[3] = magnitude >> 13;
1677362cd22SMichal Malý 	report->field[0]->value[4] = magnitude >> 8;
1687362cd22SMichal Malý 	report->field[0]->value[5] = 0x00;
1697362cd22SMichal Malý 	report->field[0]->value[6] = 0x00;
17032c88cbcSSimon Wood 
17132c88cbcSSimon Wood 	usbhid_submit_report(hid, report, USB_DIR_OUT);
17232c88cbcSSimon Wood }
17332c88cbcSSimon Wood 
1746e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */
1756e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
1766e2de8e0SMichal Malý {
1776e2de8e0SMichal Malý 	struct hid_device *hid = input_get_drvdata(dev);
1786e2de8e0SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1796e2de8e0SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1806e2de8e0SMichal Malý 	magnitude = magnitude * 90 / 65535;
1816e2de8e0SMichal Malý 
1826e2de8e0SMichal Malý 
1836e2de8e0SMichal Malý 	report->field[0]->value[0] = 0xfe;
1846e2de8e0SMichal Malý 	report->field[0]->value[1] = 0x03;
1856e2de8e0SMichal Malý 	report->field[0]->value[2] = magnitude >> 14;
1866e2de8e0SMichal Malý 	report->field[0]->value[3] = magnitude >> 14;
1876e2de8e0SMichal Malý 	report->field[0]->value[4] = magnitude;
1886e2de8e0SMichal Malý 	report->field[0]->value[5] = 0x00;
1896e2de8e0SMichal Malý 	report->field[0]->value[6] = 0x00;
1906e2de8e0SMichal Malý 
1916e2de8e0SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
1926e2de8e0SMichal Malý }
1936e2de8e0SMichal Malý 
19430bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */
19530bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range)
19630bb75d7SMichal Malý {
19730bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
19830bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
19930bb75d7SMichal Malý 	dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
20030bb75d7SMichal Malý 
20130bb75d7SMichal Malý 	report->field[0]->value[0] = 0xf8;
20230bb75d7SMichal Malý 	report->field[0]->value[1] = 0x81;
20330bb75d7SMichal Malý 	report->field[0]->value[2] = range & 0x00ff;
20430bb75d7SMichal Malý 	report->field[0]->value[3] = (range & 0xff00) >> 8;
20530bb75d7SMichal Malý 	report->field[0]->value[4] = 0x00;
20630bb75d7SMichal Malý 	report->field[0]->value[5] = 0x00;
20730bb75d7SMichal Malý 	report->field[0]->value[6] = 0x00;
20830bb75d7SMichal Malý 
20930bb75d7SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
21030bb75d7SMichal Malý }
21130bb75d7SMichal Malý 
21230bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */
21330bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range)
21430bb75d7SMichal Malý {
21530bb75d7SMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
21630bb75d7SMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
21730bb75d7SMichal Malý 	int start_left, start_right, full_range;
21830bb75d7SMichal Malý 	dbg_hid("Driving Force Pro: setting range to %u\n", range);
21930bb75d7SMichal Malý 
22030bb75d7SMichal Malý 	/* Prepare "coarse" limit command */
22130bb75d7SMichal Malý 	report->field[0]->value[0] = 0xf8;
22230bb75d7SMichal Malý 	report->field[0]->value[1] = 0x00; 	/* Set later */
22330bb75d7SMichal Malý 	report->field[0]->value[2] = 0x00;
22430bb75d7SMichal Malý 	report->field[0]->value[3] = 0x00;
22530bb75d7SMichal Malý 	report->field[0]->value[4] = 0x00;
22630bb75d7SMichal Malý 	report->field[0]->value[5] = 0x00;
22730bb75d7SMichal Malý 	report->field[0]->value[6] = 0x00;
22830bb75d7SMichal Malý 
22930bb75d7SMichal Malý 	if (range > 200) {
23030bb75d7SMichal Malý 		report->field[0]->value[1] = 0x03;
23130bb75d7SMichal Malý 		full_range = 900;
23230bb75d7SMichal Malý 	} else {
23330bb75d7SMichal Malý 		report->field[0]->value[1] = 0x02;
23430bb75d7SMichal Malý 		full_range = 200;
23530bb75d7SMichal Malý 	}
23630bb75d7SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
23730bb75d7SMichal Malý 
23830bb75d7SMichal Malý 	/* Prepare "fine" limit command */
23930bb75d7SMichal Malý 	report->field[0]->value[0] = 0x81;
24030bb75d7SMichal Malý 	report->field[0]->value[1] = 0x0b;
24130bb75d7SMichal Malý 	report->field[0]->value[2] = 0x00;
24230bb75d7SMichal Malý 	report->field[0]->value[3] = 0x00;
24330bb75d7SMichal Malý 	report->field[0]->value[4] = 0x00;
24430bb75d7SMichal Malý 	report->field[0]->value[5] = 0x00;
24530bb75d7SMichal Malý 	report->field[0]->value[6] = 0x00;
24630bb75d7SMichal Malý 
24730bb75d7SMichal Malý 	if (range == 200 || range == 900) {	/* Do not apply any fine limit */
24830bb75d7SMichal Malý 		usbhid_submit_report(hid, report, USB_DIR_OUT);
24930bb75d7SMichal Malý 		return;
25030bb75d7SMichal Malý 	}
25130bb75d7SMichal Malý 
25230bb75d7SMichal Malý 	/* Construct fine limit command */
25330bb75d7SMichal Malý 	start_left = (((full_range - range + 1) * 2047) / full_range);
25430bb75d7SMichal Malý 	start_right = 0xfff - start_left;
25530bb75d7SMichal Malý 
25630bb75d7SMichal Malý 	report->field[0]->value[2] = start_left >> 4;
25730bb75d7SMichal Malý 	report->field[0]->value[3] = start_right >> 4;
25830bb75d7SMichal Malý 	report->field[0]->value[4] = 0xff;
25930bb75d7SMichal Malý 	report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
26030bb75d7SMichal Malý 	report->field[0]->value[6] = 0xff;
26130bb75d7SMichal Malý 
26230bb75d7SMichal Malý 	usbhid_submit_report(hid, report, USB_DIR_OUT);
26330bb75d7SMichal Malý }
26430bb75d7SMichal Malý 
26596440c8aSMichal Malý static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd)
26696440c8aSMichal Malý {
26796440c8aSMichal Malý 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
26896440c8aSMichal Malý 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
26996440c8aSMichal Malý 	__u8 i, j;
27096440c8aSMichal Malý 
27196440c8aSMichal Malý 	j = 0;
27296440c8aSMichal Malý 	while (j < 7*cmd->cmd_num) {
27396440c8aSMichal Malý 		for (i = 0; i < 7; i++)
27496440c8aSMichal Malý 			report->field[0]->value[i] = cmd->cmd[j++];
27596440c8aSMichal Malý 
27696440c8aSMichal Malý 		usbhid_submit_report(hid, report, USB_DIR_OUT);
27796440c8aSMichal Malý 	}
27896440c8aSMichal Malý }
27932c88cbcSSimon Wood 
28030bb75d7SMichal Malý /* Read current range and display it in terminal */
28130bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf)
28230bb75d7SMichal Malý {
28330bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
284*3b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
285*3b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
28630bb75d7SMichal Malý 	size_t count;
28730bb75d7SMichal Malý 
288*3b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
289*3b6b17b7SMichal Malý 	if (!drv_data) {
290*3b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
291*3b6b17b7SMichal Malý 		return 0;
29230bb75d7SMichal Malý 	}
293*3b6b17b7SMichal Malý 
294*3b6b17b7SMichal Malý 	entry = drv_data->device_props;
295*3b6b17b7SMichal Malý 	if (!entry) {
296*3b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
29730bb75d7SMichal Malý 		return 0;
29830bb75d7SMichal Malý 	}
29930bb75d7SMichal Malý 
30030bb75d7SMichal Malý 	count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range);
30130bb75d7SMichal Malý 	return count;
30230bb75d7SMichal Malý }
30330bb75d7SMichal Malý 
30430bb75d7SMichal Malý /* Set range to user specified value, call appropriate function
30530bb75d7SMichal Malý  * according to the type of the wheel */
30630bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
30730bb75d7SMichal Malý {
30830bb75d7SMichal Malý 	struct hid_device *hid = to_hid_device(dev);
309*3b6b17b7SMichal Malý 	struct lg4ff_device_entry *entry;
310*3b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
31130bb75d7SMichal Malý 	__u16 range = simple_strtoul(buf, NULL, 10);
31230bb75d7SMichal Malý 
313*3b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
314*3b6b17b7SMichal Malý 	if (!drv_data) {
315*3b6b17b7SMichal Malý 		hid_err(hid, "Private driver data not found!\n");
316*3b6b17b7SMichal Malý 		return 0;
31730bb75d7SMichal Malý 	}
318*3b6b17b7SMichal Malý 
319*3b6b17b7SMichal Malý 	entry = drv_data->device_props;
320*3b6b17b7SMichal Malý 	if (!entry) {
321*3b6b17b7SMichal Malý 		hid_err(hid, "Device properties not found!\n");
322*3b6b17b7SMichal Malý 		return 0;
32330bb75d7SMichal Malý 	}
32430bb75d7SMichal Malý 
32530bb75d7SMichal Malý 	if (range == 0)
32630bb75d7SMichal Malý 		range = entry->max_range;
32730bb75d7SMichal Malý 
32830bb75d7SMichal Malý 	/* Check if the wheel supports range setting
32930bb75d7SMichal Malý 	 * and that the range is within limits for the wheel */
33030bb75d7SMichal Malý 	if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) {
33130bb75d7SMichal Malý 		entry->set_range(hid, range);
33230bb75d7SMichal Malý 		entry->range = range;
33330bb75d7SMichal Malý 	}
33430bb75d7SMichal Malý 
33530bb75d7SMichal Malý 	return count;
33630bb75d7SMichal Malý }
33730bb75d7SMichal Malý 
33832c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid)
33932c88cbcSSimon Wood {
34032c88cbcSSimon Wood 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
34132c88cbcSSimon Wood 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
34232c88cbcSSimon Wood 	struct input_dev *dev = hidinput->input;
34332c88cbcSSimon Wood 	struct hid_report *report;
34432c88cbcSSimon Wood 	struct hid_field *field;
34530bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
346*3b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
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 
426*3b6b17b7SMichal Malý 	/* Get private driver data */
427*3b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
428*3b6b17b7SMichal Malý 	if (!drv_data) {
429*3b6b17b7SMichal Malý 		hid_err(hid, "Cannot add device, private driver data not allocated\n");
430*3b6b17b7SMichal Malý 		return -1;
43130bb75d7SMichal Malý 	}
43230bb75d7SMichal Malý 
433*3b6b17b7SMichal Malý 	/* Initialize device properties */
4348383c6bfSThomas Meyer 	entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL);
43530bb75d7SMichal Malý 	if (!entry) {
436*3b6b17b7SMichal Malý 		hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n");
43730bb75d7SMichal Malý 		return -ENOMEM;
43830bb75d7SMichal Malý 	}
439*3b6b17b7SMichal Malý 	drv_data->device_props = entry;
440*3b6b17b7SMichal Malý 
44130bb75d7SMichal Malý 	entry->min_range = lg4ff_devices[i].min_range;
44230bb75d7SMichal Malý 	entry->max_range = lg4ff_devices[i].max_range;
44330bb75d7SMichal Malý 	entry->set_range = lg4ff_devices[i].set_range;
44430bb75d7SMichal Malý 
44530bb75d7SMichal Malý 	/* Create sysfs interface */
44630bb75d7SMichal Malý 	error = device_create_file(&hid->dev, &dev_attr_range);
44730bb75d7SMichal Malý 	if (error)
44830bb75d7SMichal Malý 		return error;
44930bb75d7SMichal Malý 	dbg_hid("sysfs interface created\n");
45030bb75d7SMichal Malý 
45130bb75d7SMichal Malý 	/* Set the maximum range to start with */
45230bb75d7SMichal Malý 	entry->range = entry->max_range;
45330bb75d7SMichal Malý 	if (entry->set_range != NULL)
45430bb75d7SMichal Malý 		entry->set_range(hid, entry->range);
45530bb75d7SMichal Malý 
4564291ee30SJoe Perches 	hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
45732c88cbcSSimon Wood 	return 0;
45832c88cbcSSimon Wood }
45932c88cbcSSimon Wood 
46030bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid)
46130bb75d7SMichal Malý {
46230bb75d7SMichal Malý 	struct lg4ff_device_entry *entry;
463*3b6b17b7SMichal Malý 	struct lg_drv_data *drv_data;
4646a2e176bSMichal Malý 
4656a2e176bSMichal Malý 	device_remove_file(&hid->dev, &dev_attr_range);
4666a2e176bSMichal Malý 
467*3b6b17b7SMichal Malý 	drv_data = hid_get_drvdata(hid);
468*3b6b17b7SMichal Malý 	if (!drv_data) {
469*3b6b17b7SMichal Malý 		hid_err(hid, "Error while deinitializing device, no private driver data.\n");
47030bb75d7SMichal Malý 		return -1;
47130bb75d7SMichal Malý 	}
472*3b6b17b7SMichal Malý 	entry = drv_data->device_props;
473*3b6b17b7SMichal Malý 	if (!entry) {
474*3b6b17b7SMichal Malý 		hid_err(hid, "Error while deinitializing device, no device properties data.\n");
475*3b6b17b7SMichal Malý 		return -1;
476*3b6b17b7SMichal Malý 	}
477*3b6b17b7SMichal Malý 	/* Deallocate memory */
478*3b6b17b7SMichal Malý 	kfree(entry);
47930bb75d7SMichal Malý 
48030bb75d7SMichal Malý 	dbg_hid("Device successfully unregistered\n");
48130bb75d7SMichal Malý 	return 0;
48230bb75d7SMichal Malý }
483