linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Barnabás Pőcze" <pobrn@protonmail.com>
To: Roderick Colenbrander <roderick@gaikai.com>
Cc: Jiri Kosina <jikos@kernel.org>,
	Benjamin Tissoires <benjamin.tissoires@redhat.com>,
	"linux-input@vger.kernel.org" <linux-input@vger.kernel.org>,
	Chris Ye <lzye@google.com>,
	Roderick Colenbrander <roderick.colenbrander@sony.com>
Subject: Re: [PATCH v2 08/13] HID: playstation: add DualSense classic rumble support.
Date: Thu, 07 Jan 2021 20:41:05 +0000	[thread overview]
Message-ID: <kRzBWmooFAQ_eFPgUsxxgSR1NkdOsH3sH-ucu6OrxCuPoA7_mMP4enx4EhNNq-Rnl1jYzoPzk1P8UJY-r_W2w84B1rSJa1Nwd_aKL9SLjg8=@protonmail.com> (raw)
In-Reply-To: <20210102223109.996781-9-roderick@gaikai.com>

Hi


2021. január 2., szombat 23:31 keltezéssel, Roderick Colenbrander írta:

> From: Roderick Colenbrander <roderick.colenbrander@sony.com>
>
> The DualSense features a haptics system based on voicecoil motors,
> which requires PCM data (or special HID packets using Bluetooth). There
> is no appropriate API yet in the Linux kernel to expose these. The
> controller also provides a classic rumble feature for backwards
> compatibility. Expose this classic rumble feature using the FF framework.
>
> Signed-off-by: Roderick Colenbrander <roderick.colenbrander@sony.com>
>
> diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
> index ef175c1cb15c..e6c67aaa1a1a 100644
> --- a/drivers/hid/Kconfig
> +++ b/drivers/hid/Kconfig
> @@ -863,6 +863,14 @@  config HID_PLAYSTATION
>  	  its special functionalities e.g. touchpad, lights and motion
>  	  sensors.
>
> +config PLAYSTATION_FF

I'm wondering if HID_PLAYSTATION_FF would be a better name?


> +	bool "PlayStation force feedback support"
> +	depends on HID_PLAYSTATION
> +	select INPUT_FF_MEMLESS
> +	help
> +	  Say Y here if you would like to enable force feedback support for
> +	  PlayStation game controllers.
> +
>  config HID_PRIMAX
>  	tristate "Primax non-fully HID-compliant devices"
>  	depends on HID
> diff --git a/drivers/hid/hid-playstation.c b/drivers/hid/hid-playstation.c
> index 552a52a50b78..36a904b2f93f 100644
> --- a/drivers/hid/hid-playstation.c
> +++ b/drivers/hid/hid-playstation.c
> [...]
> @@ -136,6 +151,63 @@  struct dualsense_input_report {
>  	uint8_t reserved4[11];
>  } __packed;
>
> +/* Common data between DualSense BT/USB main output report. */
> +struct dualsense_output_report_common {
> +	uint8_t valid_flag0;
> +	uint8_t valid_flag1;
> +
> +	/* For DualShock 4 compatibility mode. */
> +	uint8_t motor_right;
> +	uint8_t motor_left;
> +
> +	/* Audio controls */
> +	uint8_t reserved[4];
> +	uint8_t mute_button_led;
> +
> +	uint8_t power_save_control;
> +	uint8_t reserved2[28];
> +
> +	/* LEDs and lightbar */
> +	uint8_t valid_flag2;
> +	uint8_t reserved3[2];
> +	uint8_t lightbar_setup;
> +	uint8_t led_brightness;
> +	uint8_t player_leds;
> +	uint8_t lightbar_red;
> +	uint8_t lightbar_green;
> +	uint8_t lightbar_blue;
> +} __packed;
> +
> +struct dualsense_output_report_bt {
> +	uint8_t report_id; /* 0x31 */
> +	uint8_t seq_tag;
> +	uint8_t tag;
> +	struct dualsense_output_report_common common;
> +	uint8_t reserved[24];
> +	__le32 crc32;
> +} __packed;
> +
> +struct dualsense_output_report_usb {
> +	uint8_t report_id; /* 0x02 */
> +	struct dualsense_output_report_common common;
> +} __packed;
> +

I think it'd be good if you could add static_asserts to check the sizes of
the __packed structs at compile time.


> +/* The DualSense has a main output report used to control most features. It is
> + * largely the same between Bluetooth and USB except for different headers and CRC.
> + * This structure hide the differences between the two to simplify sending output reports.
> + */
> +struct dualsense_output_report {
> +	uint8_t *data; /* Start of data */
> +	uint8_t len; /* Size of output report */
> +
> +	/* Points to Bluetooth data payload in case for a Bluetooth report else NULL. */
> +	struct dualsense_output_report_bt *bt;
> +	/* Points to USB data payload in case for a USB report else NULL. */
> +	struct dualsense_output_report_usb *usb;
> +	/* Points to common section of report, so past any headers */
> +	struct dualsense_output_report_common *common;
> +};
> [...]
> +static void dualsense_init_output_report(struct dualsense *ds, struct dualsense_output_report *rp,
> +		void *buf)

If the dualsense struct is already passed in, couldn't this function use
`ds->output_report_dmabuf` directly?


> +{
> +	struct hid_device *hdev = ds->base.hdev;
> +
> +	if (hdev->bus == BUS_BLUETOOTH) {
> +		struct dualsense_output_report_bt *bt = buf;
> +
> +		memset(bt, 0, sizeof(*bt));
> +		bt->report_id = DS_OUTPUT_REPORT_BT;
> +		bt->tag = 0x10; /* Magic number must be set to 0x10 */

I think it would be preferable if that 0x10 were named.


> +
> +		/* Highest 4-bit is a sequence number, which needs to be increased
> +		 * every report. Lowest 4-bit is tag and can be zero for now.
> +		 */
> +		bt->seq_tag = (ds->output_seq << 4) | 0x0;
> +		if (++ds->output_seq == 15)
> +			ds->output_seq = 0;

If I see it correctly, the maximum sequence number is 14; is that intentional?
Or am I missing something?


> +
> +		rp->data = buf;
> +		rp->len = sizeof(*bt);
> +		rp->bt = bt;
> +		rp->usb = NULL;
> +		rp->common = &bt->common;
> +	} else { /* USB */
> +		struct dualsense_output_report_usb *usb = buf;
> +
> +		memset(usb, 0, sizeof(*usb));
> +		usb->report_id = DS_OUTPUT_REPORT_USB;
> +
> +		rp->data = buf;
> +		rp->len = sizeof(*usb);
> +		rp->bt = NULL;
> +		rp->usb = usb;
> +		rp->common = &usb->common;
> +	}
> +}
> +
> +/* Helper function to send DualSense output reports. Applies a CRC at the end of a report
> + * for Bluetooth reports.
> + */
> +static void dualsense_send_output_report(struct dualsense *ds,
> +		struct dualsense_output_report *report)
> +{
> +	struct hid_device *hdev = ds->base.hdev;
> +
> +	/* Bluetooth packets need to be signed with a CRC in the last 4 bytes. */
> +	if (report->bt) {
> +		uint32_t crc;
> +		uint8_t seed = 0xA2;

Maybe this '0xA2' could be named as well? And I think it would be better if
all hexadecimal constants would either be lowercase or uppercase.


> +
> +		crc = crc32_le(0xFFFFFFFF, &seed, 1);
> +		crc = ~crc32_le(crc, report->data, report->len - 4);
> +
> +		report->bt->crc32 = cpu_to_le32(crc);
> +	}
> +
> +	hid_hw_output_report(hdev, report->data, report->len);
> +}
> [...]
>  static struct ps_device *dualsense_create(struct hid_device *hdev)
>  {
>  	struct dualsense *ds;
>  	struct ps_device *ps_dev;
> +	uint8_t max_output_report_size;
>  	int ret;
>
>  	ds = devm_kzalloc(&hdev->dev, sizeof(*ds), GFP_KERNEL);
> @@ -696,8 +882,14 @@  static struct ps_device *dualsense_create(struct hid_device *hdev)
>  	ps_dev->battery_capacity = 100; /* initial value until parse_report. */
>  	ps_dev->battery_status = POWER_SUPPLY_STATUS_UNKNOWN;
>  	ps_dev->parse_report = dualsense_parse_report;
> +	INIT_WORK(&ds->output_worker, dualsense_output_worker);
>  	hid_set_drvdata(hdev, ds);
>
> +	max_output_report_size = sizeof(struct dualsense_output_report_bt);

I think `max(sizeof(..._bt), sizeof(..._usb))` (linux/minmax.h) would be
more expressive?


> +	ds->output_report_dmabuf = devm_kzalloc(&hdev->dev, max_output_report_size, GFP_KERNEL);
> +	if (!ds->output_report_dmabuf)
> +		return ERR_PTR(-ENOMEM);
> +
>  	ret = dualsense_get_mac_address(ds);
>  	if (ret < 0) {
>  		hid_err(hdev, "Failed to get MAC address from DualSense\n");
> @@ -715,7 +907,7 @@  static struct ps_device *dualsense_create(struct hid_device *hdev)
>  		goto err;
>  	}
>
> -	ds->gamepad = ps_gamepad_create(hdev);
> +	ds->gamepad = ps_gamepad_create(hdev, dualsense_play_effect);
>  	if (IS_ERR(ds->gamepad)) {
>  		ret = PTR_ERR(ds->gamepad);
>  		goto err;
>
>


Regards,
Barnabás Pőcze

  reply	other threads:[~2021-01-07 20:42 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-02 22:30 [PATCH v2 00/13] HID: new driver for PS5 'DualSense' controller Roderick Colenbrander
2021-01-02 22:30 ` [PATCH v2 01/13] HID: playstation: initial DualSense USB support Roderick Colenbrander
2021-01-04 12:20   ` Jiri Kosina
2021-01-05  8:20   ` Benjamin Tissoires
2021-01-07 17:14   ` Barnabás Pőcze
2021-01-08  7:12     ` Roderick Colenbrander
2021-01-08 11:53       ` Barnabás Pőcze
2021-01-02 22:30 ` [PATCH v2 02/13] HID: playstation: use DualSense MAC address as unique identifier Roderick Colenbrander
2021-01-07 17:22   ` Barnabás Pőcze
2021-01-02 22:30 ` [PATCH v2 03/13] HID: playstation: add DualSense battery support Roderick Colenbrander
2021-01-07 17:50   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 04/13] HID: playstation: add DualSense touchpad support Roderick Colenbrander
2021-01-07 17:55   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 05/13] HID: playstation: add DualSense accelerometer and gyroscope support Roderick Colenbrander
2021-01-07 13:34   ` Florian Märkl
2021-01-08  5:51     ` Roderick Colenbrander
2021-01-07 18:51   ` Barnabás Pőcze
2021-01-08  6:06     ` Roderick Colenbrander
2021-01-08 12:01       ` Barnabás Pőcze
2021-01-08 17:15         ` Siarhei Vishniakou
2021-01-08 19:54           ` Roderick Colenbrander
2021-01-09  0:11             ` Siarhei Vishniakou
2021-01-11  0:06               ` Roderick Colenbrander
2021-01-02 22:31 ` [PATCH v2 06/13] HID: playstation: track devices in list Roderick Colenbrander
2021-01-07 20:13   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 07/13] HID: playstation: add DualSense Bluetooth support Roderick Colenbrander
2021-01-07 20:18   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 08/13] HID: playstation: add DualSense classic rumble support Roderick Colenbrander
2021-01-07 20:41   ` Barnabás Pőcze [this message]
2021-01-07 20:48     ` Barnabás Pőcze
2021-01-08 17:01     ` Roderick Colenbrander
2021-01-02 22:31 ` [PATCH v2 09/13] HID: playstation: add DualSense lightbar support Roderick Colenbrander
2021-01-07 21:01   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 10/13] HID: playstation: add microphone mute support for DualSense Roderick Colenbrander
2021-01-07 21:57   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 11/13] HID: playstation: add DualSense player LEDs support Roderick Colenbrander
2021-01-07 22:17   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 12/13] HID: playstation: DualSense set LEDs to default player id Roderick Colenbrander
2021-01-07 22:25   ` Barnabás Pőcze
2021-01-02 22:31 ` [PATCH v2 13/13] HID: playstation: report DualSense hardware and firmware version Roderick Colenbrander
2021-01-07 22:26   ` Barnabás Pőcze
2021-01-09  1:35     ` Roderick Colenbrander

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='kRzBWmooFAQ_eFPgUsxxgSR1NkdOsH3sH-ucu6OrxCuPoA7_mMP4enx4EhNNq-Rnl1jYzoPzk1P8UJY-r_W2w84B1rSJa1Nwd_aKL9SLjg8=@protonmail.com' \
    --to=pobrn@protonmail.com \
    --cc=benjamin.tissoires@redhat.com \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=lzye@google.com \
    --cc=roderick.colenbrander@sony.com \
    --cc=roderick@gaikai.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).