18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Force feedback support for Mayflash game controller adapters.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * These devices are manufactured by Mayflash but identify themselves
68c2ecf20Sopenharmony_ci * using the vendor ID of DragonRise Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Tested with:
98c2ecf20Sopenharmony_ci * 0079:1801 "DragonRise Inc. Mayflash PS3 Game Controller Adapter"
108c2ecf20Sopenharmony_ci * 0079:1803 "DragonRise Inc. Mayflash Wireless Sensor DolphinBar"
118c2ecf20Sopenharmony_ci * 0079:1843 "DragonRise Inc. Mayflash GameCube Game Controller Adapter"
128c2ecf20Sopenharmony_ci * 0079:1844 "DragonRise Inc. Mayflash GameCube Game Controller Adapter (v04)"
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The following adapters probably work too, but need to be tested:
158c2ecf20Sopenharmony_ci * 0079:1800 "DragonRise Inc. Mayflash WIIU Game Controller Adapter"
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * Copyright (c) 2016-2017 Marcel Hasler <mahasler@gmail.com>
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/input.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/hid.h>
268c2ecf20Sopenharmony_ci#include <linux/module.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "hid-ids.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct mf_device {
318c2ecf20Sopenharmony_ci	struct hid_report *report;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic int mf_play(struct input_dev *dev, void *data, struct ff_effect *effect)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct hid_device *hid = input_get_drvdata(dev);
378c2ecf20Sopenharmony_ci	struct mf_device *mf = data;
388c2ecf20Sopenharmony_ci	int strong, weak;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	strong = effect->u.rumble.strong_magnitude;
418c2ecf20Sopenharmony_ci	weak = effect->u.rumble.weak_magnitude;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	dbg_hid("Called with 0x%04x 0x%04x.\n", strong, weak);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	strong = strong * 0xff / 0xffff;
468c2ecf20Sopenharmony_ci	weak = weak * 0xff / 0xffff;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	dbg_hid("Running with 0x%02x 0x%02x.\n", strong, weak);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	mf->report->field[0]->value[0] = weak;
518c2ecf20Sopenharmony_ci	mf->report->field[0]->value[1] = strong;
528c2ecf20Sopenharmony_ci	hid_hw_request(hid, mf->report, HID_REQ_SET_REPORT);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	return 0;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic int mf_init(struct hid_device *hid)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	struct mf_device *mf;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	struct list_head *report_list =
628c2ecf20Sopenharmony_ci			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	struct list_head *report_ptr;
658c2ecf20Sopenharmony_ci	struct hid_report *report;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	struct list_head *input_ptr = &hid->inputs;
688c2ecf20Sopenharmony_ci	struct hid_input *input;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	struct input_dev *dev;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	int error;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	/* Setup each of the four inputs */
758c2ecf20Sopenharmony_ci	list_for_each(report_ptr, report_list) {
768c2ecf20Sopenharmony_ci		report = list_entry(report_ptr, struct hid_report, list);
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci		if (report->maxfield < 1 || report->field[0]->report_count < 2) {
798c2ecf20Sopenharmony_ci			hid_err(hid, "Invalid report, this should never happen!\n");
808c2ecf20Sopenharmony_ci			return -ENODEV;
818c2ecf20Sopenharmony_ci		}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci		if (list_is_last(input_ptr, &hid->inputs)) {
848c2ecf20Sopenharmony_ci			hid_err(hid, "Missing input, this should never happen!\n");
858c2ecf20Sopenharmony_ci			return -ENODEV;
868c2ecf20Sopenharmony_ci		}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		input_ptr = input_ptr->next;
898c2ecf20Sopenharmony_ci		input = list_entry(input_ptr, struct hid_input, list);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci		mf = kzalloc(sizeof(struct mf_device), GFP_KERNEL);
928c2ecf20Sopenharmony_ci		if (!mf)
938c2ecf20Sopenharmony_ci			return -ENOMEM;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		dev = input->input;
968c2ecf20Sopenharmony_ci		set_bit(FF_RUMBLE, dev->ffbit);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		error = input_ff_create_memless(dev, mf, mf_play);
998c2ecf20Sopenharmony_ci		if (error) {
1008c2ecf20Sopenharmony_ci			kfree(mf);
1018c2ecf20Sopenharmony_ci			return error;
1028c2ecf20Sopenharmony_ci		}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		mf->report = report;
1058c2ecf20Sopenharmony_ci		mf->report->field[0]->value[0] = 0x00;
1068c2ecf20Sopenharmony_ci		mf->report->field[0]->value[1] = 0x00;
1078c2ecf20Sopenharmony_ci		hid_hw_request(hid, mf->report, HID_REQ_SET_REPORT);
1088c2ecf20Sopenharmony_ci	}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	hid_info(hid, "Force feedback for HJZ Mayflash game controller "
1118c2ecf20Sopenharmony_ci		      "adapters by Marcel Hasler <mahasler@gmail.com>\n");
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int mf_probe(struct hid_device *hid, const struct hid_device_id *id)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	int error;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	dev_dbg(&hid->dev, "Mayflash HID hardware probe...\n");
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	/* Apply quirks as needed */
1238c2ecf20Sopenharmony_ci	hid->quirks |= id->driver_data;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	error = hid_parse(hid);
1268c2ecf20Sopenharmony_ci	if (error) {
1278c2ecf20Sopenharmony_ci		hid_err(hid, "HID parse failed.\n");
1288c2ecf20Sopenharmony_ci		return error;
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	error = hid_hw_start(hid, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
1328c2ecf20Sopenharmony_ci	if (error) {
1338c2ecf20Sopenharmony_ci		hid_err(hid, "HID hw start failed\n");
1348c2ecf20Sopenharmony_ci		return error;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	error = mf_init(hid);
1388c2ecf20Sopenharmony_ci	if (error) {
1398c2ecf20Sopenharmony_ci		hid_err(hid, "Force feedback init failed.\n");
1408c2ecf20Sopenharmony_ci		hid_hw_stop(hid);
1418c2ecf20Sopenharmony_ci		return error;
1428c2ecf20Sopenharmony_ci	}
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	return 0;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic const struct hid_device_id mf_devices[] = {
1488c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_PS3),
1498c2ecf20Sopenharmony_ci		.driver_data = HID_QUIRK_MULTI_INPUT },
1508c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_DOLPHINBAR),
1518c2ecf20Sopenharmony_ci		.driver_data = HID_QUIRK_MULTI_INPUT },
1528c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_GAMECUBE1),
1538c2ecf20Sopenharmony_ci		.driver_data = HID_QUIRK_MULTI_INPUT },
1548c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_GAMECUBE2),
1558c2ecf20Sopenharmony_ci		.driver_data = 0 }, /* No quirk required */
1568c2ecf20Sopenharmony_ci	{ HID_USB_DEVICE(USB_VENDOR_ID_DRAGONRISE, USB_DEVICE_ID_DRAGONRISE_GAMECUBE3),
1578c2ecf20Sopenharmony_ci		.driver_data = HID_QUIRK_MULTI_INPUT },
1588c2ecf20Sopenharmony_ci	{ }
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(hid, mf_devices);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic struct hid_driver mf_driver = {
1638c2ecf20Sopenharmony_ci	.name = "hid_mf",
1648c2ecf20Sopenharmony_ci	.id_table = mf_devices,
1658c2ecf20Sopenharmony_ci	.probe = mf_probe,
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_cimodule_hid_driver(mf_driver);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
170