From patchwork Mon Mar 11 02:03:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ogorchock X-Patchwork-Id: 10846563 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B28FA6C2 for ; Mon, 11 Mar 2019 02:03:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C4AE28C92 for ; Mon, 11 Mar 2019 02:03:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8F74928E96; Mon, 11 Mar 2019 02:03:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EC6D828C92 for ; Mon, 11 Mar 2019 02:03:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727046AbfCKCD4 (ORCPT ); Sun, 10 Mar 2019 22:03:56 -0400 Received: from mail-yw1-f66.google.com ([209.85.161.66]:33742 "EHLO mail-yw1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726758AbfCKCD4 (ORCPT ); Sun, 10 Mar 2019 22:03:56 -0400 Received: by mail-yw1-f66.google.com with SMTP id a199so1712407ywa.0 for ; Sun, 10 Mar 2019 19:03:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ip6nHX4I/6DEIGcQyE8li3JV4n5osOP5REXEuA4rpJo=; b=m/BQZp1JbXk9dcrY/WYaQ6Y5jJXQ1wPpr+BLwaNaMoEFv5oGdD4FSenAnDj1YMYNqI BSlzQJEQTiGNrycTPAch1GDBJG1gXkFxsDZ290paadpdnYKnS3uk8anPkh8ARd5KBX4b s1Ho1uLDfUj6maO2PGtT2KpfqD48d7oCMHF4xmdVwMxDSRfqCEJF8RbhPCJIwDxQd0gI A3OVoI7CVLj14EyMTUrDMAryTQCKPRI8Bx1+wpGTrGnkIL0Yfy9J4O8Li7K3WEBIupld dunxUGfrLKeVPzL6prFguRfwTGDHNzKn+AG53MoYEWzjP30s8wt+linmJVyjhsjGlncx V0YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Ip6nHX4I/6DEIGcQyE8li3JV4n5osOP5REXEuA4rpJo=; b=FkUXo5pvtZ33TgCJojvjwrEFqA6gfk3y6CtmdmjNLKi0D3Xoh5mu0YcsQx+vkdubq2 +Wv853e8+AO4RAJ45Pxx2VvYGqGlY2VDgByKEkOWZUooa5b7dhS0BrD2+EbFYkvIDqcb t0CzVYeYdfecNDQAneY54NhSLBkH9tWiRaJR3dU8z4iCcYDxFIk7jMsEQJnWkGD/jEvA 6PfZ4cdbH6N9u8z1LHJzhzRH6X4Vhflx4EVn6dWxhBZiU+lxRMqj95bdXdsvuSZeMILh EwcE/KGLvMSRj23CErXzvH/MpbzmEPkXm+5OdlQBSYe/Kzh7gI3D4xGe0EWTkMWwwATG xknA== X-Gm-Message-State: APjAAAW9ADR4e5OgY+kv5dTRI46CpYrzVYGkJT02aniaN+77CKrg/WKA bfKOVBYn4BKper3RWJUHTXdRHBe8cFs= X-Google-Smtp-Source: APXvYqyf1RGkqV/LTKORu1Hko7B7rkdis2WHca3epL/obYS/0KkOA8zrBIvUgesuN18oXSsotnfYqg== X-Received: by 2002:a25:3743:: with SMTP id e64mr25845583yba.182.1552269834200; Sun, 10 Mar 2019 19:03:54 -0700 (PDT) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id r130sm1907088ywr.4.2019.03.10.19.03.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Mar 2019 19:03:53 -0700 (PDT) From: "Daniel J. Ogorchock" To: linux-input@vger.kernel.org Cc: thunderbird2k@gmail.com, blaws05@gmail.com, benjamin.tissoires@redhat.com, jikos@kernel.org, Roderick.Colenbrander@sony.com, peter.hutterer@who-t.net, jbrandst@2ds.eu, hadess@hadess.net, "Daniel J. Ogorchock" Subject: [PATCH v4 1/6] HID: joycon: add nintendo switch controller driver Date: Sun, 10 Mar 2019 21:03:20 -0500 Message-Id: <20190311020324.23619-2-djogorchock@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190311020324.23619-1-djogorchock@gmail.com> References: <20190311020324.23619-1-djogorchock@gmail.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The joycon driver supports the Nintendo Switch Pro Controllers and the Joy-Cons. The Pro Controllers can be used over USB or Bluetooth. The Joy-Cons each create their own, independent input devices, so it is up to userspace to combine them if desired. Signed-off-by: Daniel J. Ogorchock --- MAINTAINERS | 6 + drivers/hid/Kconfig | 11 + drivers/hid/Makefile | 1 + drivers/hid/hid-ids.h | 3 + drivers/hid/hid-joycon.c | 792 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 813 insertions(+) create mode 100644 drivers/hid/hid-joycon.c diff --git a/MAINTAINERS b/MAINTAINERS index dce5c099f43c..82a851135875 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8162,6 +8162,12 @@ S: Maintained F: fs/jbd2/ F: include/linux/jbd2.h +JOYCON HID DRIVER +M: Daniel J. Ogorchock +L: linux-input@vger.kernel.org +S: Maintained +F: drivers/hid/hid-joycon* + JPU V4L2 MEM2MEM DRIVER FOR RENESAS M: Mikhail Ulyanov L: linux-media@vger.kernel.org diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 41e9935fc584..085633df2972 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -445,6 +445,17 @@ config HID_JABRA mouse button events. Say M here if you may ever plug in a Jabra USB device. +config HID_JOYCON + tristate "Nintendo Joy-Con and Pro Controller support" + depends on HID + help + Adds support for the Nintendo Switch Joy-Cons and Pro Controller. + All controllers support bluetooth, and the Pro Controller also supports + its USB mode. + + To compile this driver as a module, choose M here: the + module will be called hid-joycon. + config HID_TWINHAN tristate "Twinhan IR remote control" depends on HID diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 896a51ce7ce0..570ca33c4c98 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -57,6 +57,7 @@ obj-$(CONFIG_HID_HYPERV_MOUSE) += hid-hyperv.o obj-$(CONFIG_HID_ICADE) += hid-icade.o obj-$(CONFIG_HID_ITE) += hid-ite.o obj-$(CONFIG_HID_JABRA) += hid-jabra.o +obj-$(CONFIG_HID_JOYCON) += hid-joycon.o obj-$(CONFIG_HID_KENSINGTON) += hid-kensington.o obj-$(CONFIG_HID_KEYTOUCH) += hid-keytouch.o obj-$(CONFIG_HID_KYE) += hid-kye.o diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index 24f846d67478..50d8ed9ea10e 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -851,6 +851,9 @@ #define USB_VENDOR_ID_NINTENDO 0x057e #define USB_DEVICE_ID_NINTENDO_WIIMOTE 0x0306 #define USB_DEVICE_ID_NINTENDO_WIIMOTE2 0x0330 +#define USB_DEVICE_ID_NINTENDO_JOYCONL 0x2006 +#define USB_DEVICE_ID_NINTENDO_JOYCONR 0x2007 +#define USB_DEVICE_ID_NINTENDO_PROCON 0x2009 #define USB_VENDOR_ID_NOVATEK 0x0603 #define USB_DEVICE_ID_NOVATEK_PCT 0x0600 diff --git a/drivers/hid/hid-joycon.c b/drivers/hid/hid-joycon.c new file mode 100644 index 000000000000..e70b9f8239ca --- /dev/null +++ b/drivers/hid/hid-joycon.c @@ -0,0 +1,792 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * HID driver for Nintendo Switch Joy-Cons and Pro Controllers + * + * Copyright (c) 2019 Daniel J. Ogorchock + * + * The following resources/projects were referenced for this driver: + * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering + * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin) + * https://github.com/FrotBot/SwitchProConLinuxUSB + * https://github.com/MTCKC/ProconXInput + * hid-wiimote kernel hid driver + * hid-logitech-hidpp driver + * + * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The + * Pro Controllers can either be used over USB or Bluetooth. + * + * The driver will retrieve the factory calibration info from the controllers, + * so little to no user calibration should be required. + * + */ + +#include "hid-ids.h" +#include +#include +#include +#include +#include +#include + +/* + * Reference the url below for the following HID report defines: + * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering + */ + +/* Output Reports */ +#define JC_OUTPUT_RUMBLE_AND_SUBCMD 0x01 +#define JC_OUTPUT_FW_UPDATE_PKT 0x03 +#define JC_OUTPUT_RUMBLE_ONLY 0x10 +#define JC_OUTPUT_MCU_DATA 0x11 +#define JC_OUTPUT_USB_CMD 0x80 + +/* Subcommand IDs */ +#define JC_SUBCMD_STATE 0x00 +#define JC_SUBCMD_MANUAL_BT_PAIRING 0x01 +#define JC_SUBCMD_REQ_DEV_INFO 0x02 +#define JC_SUBCMD_SET_REPORT_MODE 0x03 +#define JC_SUBCMD_TRIGGERS_ELAPSED 0x04 +#define JC_SUBCMD_GET_PAGE_LIST_STATE 0x05 +#define JC_SUBCMD_SET_HCI_STATE 0x06 +#define JC_SUBCMD_RESET_PAIRING_INFO 0x07 +#define JC_SUBCMD_LOW_POWER_MODE 0x08 +#define JC_SUBCMD_SPI_FLASH_READ 0x10 +#define JC_SUBCMD_SPI_FLASH_WRITE 0x11 +#define JC_SUBCMD_RESET_MCU 0x20 +#define JC_SUBCMD_SET_MCU_CONFIG 0x21 +#define JC_SUBCMD_SET_MCU_STATE 0x22 +#define JC_SUBCMD_SET_PLAYER_LIGHTS 0x30 +#define JC_SUBCMD_GET_PLAYER_LIGHTS 0x31 +#define JC_SUBCMD_SET_HOME_LIGHT 0x38 +#define JC_SUBCMD_ENABLE_IMU 0x40 +#define JC_SUBCMD_SET_IMU_SENSITIVITY 0x41 +#define JC_SUBCMD_WRITE_IMU_REG 0x42 +#define JC_SUBCMD_READ_IMU_REG 0x43 +#define JC_SUBCMD_ENABLE_VIBRATION 0x48 +#define JC_SUBCMD_GET_REGULATED_VOLTAGE 0x50 + +/* Input Reports */ +#define JC_INPUT_BUTTON_EVENT 0x3F +#define JC_INPUT_SUBCMD_REPLY 0x21 +#define JC_INPUT_IMU_DATA 0x30 +#define JC_INPUT_MCU_DATA 0x31 +#define JC_INPUT_USB_RESPONSE 0x81 + +/* Feature Reports */ +#define JC_FEATURE_LAST_SUBCMD 0x02 +#define JC_FEATURE_OTA_FW_UPGRADE 0x70 +#define JC_FEATURE_SETUP_MEM_READ 0x71 +#define JC_FEATURE_MEM_READ 0x72 +#define JC_FEATURE_ERASE_MEM_SECTOR 0x73 +#define JC_FEATURE_MEM_WRITE 0x74 +#define JC_FEATURE_LAUNCH 0x75 + +/* USB Commands */ +#define JC_USB_CMD_CONN_STATUS 0x01 +#define JC_USB_CMD_HANDSHAKE 0x02 +#define JC_USB_CMD_BAUDRATE_3M 0x03 +#define JC_USB_CMD_NO_TIMEOUT 0x04 +#define JC_USB_CMD_EN_TIMEOUT 0x05 +#define JC_USB_RESET 0x06 +#define JC_USB_PRE_HANDSHAKE 0x91 +#define JC_USB_SEND_UART 0x92 + +/* SPI storage addresses of factory calibration data */ +#define JC_CAL_DATA_START 0x603d +#define JC_CAL_DATA_END 0x604e +#define JC_CAL_DATA_SIZE (JC_CAL_DATA_END - JC_CAL_DATA_START + 1) + + +/* The raw analog joystick values will be mapped in terms of this magnitude */ +#define JC_MAX_STICK_MAG 32767 +#define JC_STICK_FUZZ 250 +#define JC_STICK_FLAT 500 + +/* States for controller state machine */ +enum joycon_ctlr_state { + JOYCON_CTLR_STATE_INIT, + JOYCON_CTLR_STATE_READ, +}; + +struct joycon_stick_cal { + s32 max; + s32 min; + s32 center; +}; + +/* + * All the controller's button values are stored in a u32. + * They can be accessed with bitwise ANDs. + */ +#define JC_BTN_Y BIT(0) +#define JC_BTN_X BIT(1) +#define JC_BTN_B BIT(2) +#define JC_BTN_A BIT(3) +#define JC_BTN_SR_R BIT(4) +#define JC_BTN_SL_R BIT(5) +#define JC_BTN_R BIT(6) +#define JC_BTN_ZR BIT(7) +#define JC_BTN_MINUS BIT(8) +#define JC_BTN_PLUS BIT(9) +#define JC_BTN_RSTICK BIT(10) +#define JC_BTN_LSTICK BIT(11) +#define JC_BTN_HOME BIT(12) +#define JC_BTN_CAP BIT(13) /* capture button */ +#define JC_BTN_DOWN BIT(16) +#define JC_BTN_UP BIT(17) +#define JC_BTN_RIGHT BIT(18) +#define JC_BTN_LEFT BIT(19) +#define JC_BTN_SR_L BIT(20) +#define JC_BTN_SL_L BIT(21) +#define JC_BTN_L BIT(22) +#define JC_BTN_ZL BIT(23) + +enum joycon_msg_type { + JOYCON_MSG_TYPE_NONE, + JOYCON_MSG_TYPE_USB, + JOYCON_MSG_TYPE_SUBCMD, +}; + +struct joycon_subcmd_request { + u8 output_id; /* must be 0x01 for subcommand, 0x10 for rumble only */ + u8 packet_num; /* incremented every send */ + u8 rumble_data[8]; + u8 subcmd_id; + /* data is here */ +} __packed; + +/* should pass in pointer to a struct joycon_subcmd_request */ +#define JC_SUBCMD_REQ_GET_DATA(req) \ + ((u8 *)(req) + sizeof(struct joycon_subcmd_request)) + +struct joycon_subcmd_reply { + u8 ack; /* MSB 1 for ACK, 0 for NACK */ + u8 id; /* id of requested subcmd */ + /* data is here, can be up to 35 bytes */ +} __packed; + +/* should pass in pointer to a struct joycon_subcmd_reply */ +#define JC_SUBCMD_REPLY_GET_DATA(reply) \ + ((u8 *)(reply) + sizeof(struct joycon_subcmd_reply)) + +struct joycon_input_report { + u8 id; + u8 timer; + u8 bat_con; /* battery and connection info */ + u8 button_status[3]; + u8 left_stick[3]; + u8 right_stick[3]; + u8 vibrator_report; + + /* + * If support for firmware updates, gyroscope data, and/or NFC/IR + * are added in the future, this can be swapped for a union. + */ + struct joycon_subcmd_reply reply; +} __packed; + +#define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35) + +/* Each physical controller is associated with a joycon_ctlr struct */ +struct joycon_ctlr { + struct hid_device *hdev; + struct input_dev *input; + enum joycon_ctlr_state ctlr_state; + + /* The following members are used for synchronous sends/receives */ + enum joycon_msg_type msg_type; + u8 subcmd_num; + struct mutex output_mutex; + u8 input_buf[JC_MAX_RESP_SIZE]; + wait_queue_head_t wait; + bool received_resp; + u8 usb_ack_match; + u8 subcmd_ack_match; + + /* factory calibration data */ + struct joycon_stick_cal left_stick_cal_x; + struct joycon_stick_cal left_stick_cal_y; + struct joycon_stick_cal right_stick_cal_x; + struct joycon_stick_cal right_stick_cal_y; + +}; + +static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len) +{ + u8 *buf; + int ret; + + buf = kmemdup(data, len, GFP_KERNEL); + if (!buf) + return -ENOMEM; + ret = hid_hw_output_report(hdev, buf, len); + kfree(buf); + if (ret < 0) + hid_dbg(hdev, "Failed to send output report ret=%d\n", ret); + return ret; +} + +static int joycon_hid_send_sync(struct joycon_ctlr *ctlr, u8 *data, size_t len) +{ + int ret; + + ret = __joycon_hid_send(ctlr->hdev, data, len); + if (ret < 0) { + memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); + return ret; + } + + if (!wait_event_timeout(ctlr->wait, ctlr->received_resp, HZ)) { + hid_dbg(ctlr->hdev, "syncronous send/receive timed out\n"); + memset(ctlr->input_buf, 0, JC_MAX_RESP_SIZE); + return -ETIMEDOUT; + } + + ctlr->received_resp = false; + return 0; +} + +static int joycon_send_usb(struct joycon_ctlr *ctlr, u8 cmd) +{ + int ret; + u8 buf[2] = {JC_OUTPUT_USB_CMD}; + + buf[1] = cmd; + ctlr->usb_ack_match = cmd; + ctlr->msg_type = JOYCON_MSG_TYPE_USB; + ret = joycon_hid_send_sync(ctlr, buf, sizeof(buf)); + if (ret) + hid_dbg(ctlr->hdev, "send usb command failed; ret=%d\n", ret); + return ret; +} + +static int joycon_send_subcmd(struct joycon_ctlr *ctlr, + struct joycon_subcmd_request *subcmd, + size_t data_len) +{ + int ret; + + subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; + subcmd->packet_num = ctlr->subcmd_num; + if (++ctlr->subcmd_num > 0xF) + ctlr->subcmd_num = 0; + ctlr->subcmd_ack_match = subcmd->subcmd_id; + ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; + + ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd, + sizeof(*subcmd) + data_len); + if (ret) + hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); + return ret; +} + +/* Supply nibbles for flash and on. Ones correspond to active */ +static int joycon_set_player_leds(struct joycon_ctlr *ctlr, u8 flash, u8 on) +{ + struct joycon_subcmd_request *req; + u8 buffer[sizeof(*req) + 1] = { 0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id = JC_SUBCMD_SET_PLAYER_LIGHTS; + JC_SUBCMD_REQ_GET_DATA(req)[0] = (flash << 4) | on; + + hid_dbg(ctlr->hdev, "setting player leds\n"); + return joycon_send_subcmd(ctlr, req, 1); +} + +static int joycon_request_calibration(struct joycon_ctlr *ctlr) +{ + struct joycon_subcmd_request *req; + u8 buffer[sizeof(*req) + 5] = { 0 }; + struct joycon_input_report *report; + struct joycon_stick_cal *cal_x; + struct joycon_stick_cal *cal_y; + s32 x_max_above; + s32 x_min_below; + s32 y_max_above; + s32 y_min_below; + u8 *data; + u8 *raw_cal; + int ret; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id = JC_SUBCMD_SPI_FLASH_READ; + data = JC_SUBCMD_REQ_GET_DATA(req); + data[0] = 0xFF & JC_CAL_DATA_START; + data[1] = 0xFF & (JC_CAL_DATA_START >> 8); + data[2] = 0xFF & (JC_CAL_DATA_START >> 16); + data[3] = 0xFF & (JC_CAL_DATA_START >> 24); + data[4] = JC_CAL_DATA_SIZE; + + hid_dbg(ctlr->hdev, "requesting cal data\n"); + ret = joycon_send_subcmd(ctlr, req, 5); + if (ret) + return ret; + + report = (struct joycon_input_report *)ctlr->input_buf; + raw_cal = JC_SUBCMD_REPLY_GET_DATA(&report->reply) + 5; + + /* left stick calibration parsing */ + cal_x = &ctlr->left_stick_cal_x; + cal_y = &ctlr->left_stick_cal_y; + + x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, 12); + y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, 12); + cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, 12); + cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, 12); + x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, 12); + y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, 12); + cal_x->max = cal_x->center + x_max_above; + cal_x->min = cal_x->center - x_min_below; + cal_y->max = cal_y->center + y_max_above; + cal_y->min = cal_y->center - y_min_below; + + /* right stick calibration parsing */ + raw_cal += 9; + cal_x = &ctlr->right_stick_cal_x; + cal_y = &ctlr->right_stick_cal_y; + + cal_x->center = hid_field_extract(ctlr->hdev, (raw_cal + 0), 0, 12); + cal_y->center = hid_field_extract(ctlr->hdev, (raw_cal + 1), 4, 12); + x_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 3), 0, 12); + y_min_below = hid_field_extract(ctlr->hdev, (raw_cal + 4), 4, 12); + x_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 6), 0, 12); + y_max_above = hid_field_extract(ctlr->hdev, (raw_cal + 7), 4, 12); + cal_x->max = cal_x->center + x_max_above; + cal_x->min = cal_x->center - x_min_below; + cal_y->max = cal_y->center + y_max_above; + cal_y->min = cal_y->center - y_min_below; + + hid_dbg(ctlr->hdev, "calibration:\n" + "l_x_c=%d l_x_max=%d l_x_min=%d\n" + "l_y_c=%d l_y_max=%d l_y_min=%d\n" + "r_x_c=%d r_x_max=%d r_x_min=%d\n" + "r_y_c=%d r_y_max=%d r_y_min=%d\n", + ctlr->left_stick_cal_x.center, + ctlr->left_stick_cal_x.max, + ctlr->left_stick_cal_x.min, + ctlr->left_stick_cal_y.center, + ctlr->left_stick_cal_y.max, + ctlr->left_stick_cal_y.min, + ctlr->right_stick_cal_x.center, + ctlr->right_stick_cal_x.max, + ctlr->right_stick_cal_x.min, + ctlr->right_stick_cal_y.center, + ctlr->right_stick_cal_y.max, + ctlr->right_stick_cal_y.min); + + return 0; +} + +static int joycon_set_report_mode(struct joycon_ctlr *ctlr) +{ + struct joycon_subcmd_request *req; + u8 buffer[sizeof(*req) + 1] = { 0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id = JC_SUBCMD_SET_REPORT_MODE; + JC_SUBCMD_REQ_GET_DATA(req)[0] = 0x30; /* standard, full report mode */ + + hid_dbg(ctlr->hdev, "setting controller report mode\n"); + return joycon_send_subcmd(ctlr, req, 1); +} + +static int joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val) +{ + s32 center = cal->center; + s32 min = cal->min; + s32 max = cal->max; + int new_val; + + if (val > center) { + new_val = (val - center) * JC_MAX_STICK_MAG; + new_val /= (max - center); + } else { + new_val = (center - val) * -JC_MAX_STICK_MAG; + new_val /= (center - min); + } + new_val = clamp(new_val, -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG); + return new_val; +} + +static void joycon_parse_report(struct joycon_ctlr *ctlr, u8 *data) +{ + struct input_dev *dev = ctlr->input; + u32 btns; + u32 id = ctlr->hdev->product; + + btns = hid_field_extract(ctlr->hdev, data + 3, 0, 24); + + if (id != USB_DEVICE_ID_NINTENDO_JOYCONR) { + u16 raw_x; + u16 raw_y; + int x; + int y; + + /* get raw stick values */ + raw_x = hid_field_extract(ctlr->hdev, (data + 6), 0, 12); + raw_y = hid_field_extract(ctlr->hdev, (data + 7), 4, 12); + /* map the stick values */ + x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); + y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); + /* report sticks */ + input_report_abs(dev, ABS_X, x); + input_report_abs(dev, ABS_Y, y); + + /* report buttons */ + input_report_key(dev, BTN_TL, btns & JC_BTN_L || + btns & JC_BTN_SL_L); + input_report_key(dev, BTN_TR, btns & JC_BTN_R || + btns & JC_BTN_SR_L); + input_report_key(dev, BTN_TL2, btns & JC_BTN_ZL); + input_report_key(dev, BTN_SELECT, btns & JC_BTN_MINUS); + input_report_key(dev, BTN_THUMBL, btns & JC_BTN_LSTICK); + input_report_key(dev, BTN_Z, btns & JC_BTN_CAP); + input_report_key(dev, BTN_DPAD_DOWN, btns & JC_BTN_DOWN); + input_report_key(dev, BTN_DPAD_UP, btns & JC_BTN_UP); + input_report_key(dev, BTN_DPAD_RIGHT, btns & JC_BTN_RIGHT); + input_report_key(dev, BTN_DPAD_LEFT, btns & JC_BTN_LEFT); + } + if (id != USB_DEVICE_ID_NINTENDO_JOYCONL) { + u16 raw_x; + u16 raw_y; + int x; + int y; + + /* get raw stick values */ + raw_x = hid_field_extract(ctlr->hdev, (data + 9), 0, 12); + raw_y = hid_field_extract(ctlr->hdev, (data + 10), 4, 12); + /* map stick values */ + x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); + y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); + /* report sticks */ + input_report_abs(dev, ABS_RX, x); + input_report_abs(dev, ABS_RY, y); + + /* report buttons */ + input_report_key(dev, BTN_TL, btns & JC_BTN_L || + btns & JC_BTN_SL_R); + input_report_key(dev, BTN_TR, btns & JC_BTN_R || + btns & JC_BTN_SR_R); + input_report_key(dev, BTN_TR2, btns & JC_BTN_ZR); + input_report_key(dev, BTN_START, btns & JC_BTN_PLUS); + input_report_key(dev, BTN_THUMBR, btns & JC_BTN_RSTICK); + input_report_key(dev, BTN_MODE, btns & JC_BTN_HOME); + input_report_key(dev, BTN_WEST, btns & JC_BTN_Y); + input_report_key(dev, BTN_NORTH, btns & JC_BTN_X); + input_report_key(dev, BTN_EAST, btns & JC_BTN_A); + input_report_key(dev, BTN_SOUTH, btns & JC_BTN_B); + } + + input_sync(dev); +} + + +static const unsigned int joycon_button_inputs[] = { + BTN_SELECT, BTN_Z, BTN_THUMBL, + BTN_START, BTN_MODE, BTN_THUMBR, + BTN_SOUTH, BTN_EAST, BTN_NORTH, BTN_WEST, + BTN_DPAD_UP, BTN_DPAD_DOWN, BTN_DPAD_LEFT, BTN_DPAD_RIGHT, + BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, + 0 /* 0 signals end of array */ +}; + +static DEFINE_MUTEX(joycon_input_num_mutex); +static int joycon_input_create(struct joycon_ctlr *ctlr) +{ + struct hid_device *hdev; + static int input_num = 1; + const char *name; + int ret; + int i; + + hdev = ctlr->hdev; + + switch (hdev->product) { + case USB_DEVICE_ID_NINTENDO_PROCON: + name = "Nintendo Switch Pro Controller"; + break; + case USB_DEVICE_ID_NINTENDO_JOYCONL: + name = "Nintendo Switch Left Joy-Con"; + break; + case USB_DEVICE_ID_NINTENDO_JOYCONR: + name = "Nintendo Switch Right Joy-Con"; + break; + default: /* Should be impossible */ + hid_err(hdev, "Invalid hid product\n"); + ret = -EINVAL; + goto err; + } + + ctlr->input = input_allocate_device(); + if (!ctlr->input) { + ret = -ENOMEM; + goto err; + } + ctlr->input->dev.parent = &hdev->dev; + ctlr->input->id.bustype = hdev->bus; + ctlr->input->id.vendor = hdev->vendor; + ctlr->input->id.product = hdev->product; + ctlr->input->id.version = hdev->version; + ctlr->input->name = name; + input_set_drvdata(ctlr->input, ctlr); + + + /* set up sticks */ + input_set_abs_params(ctlr->input, ABS_X, + -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, + JC_STICK_FUZZ, JC_STICK_FLAT); + input_set_abs_params(ctlr->input, ABS_Y, + -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, + JC_STICK_FUZZ, JC_STICK_FLAT); + input_set_abs_params(ctlr->input, ABS_RX, + -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, + JC_STICK_FUZZ, JC_STICK_FLAT); + input_set_abs_params(ctlr->input, ABS_RY, + -JC_MAX_STICK_MAG, JC_MAX_STICK_MAG, + JC_STICK_FUZZ, JC_STICK_FLAT); + /* set up buttons */ + for (i = 0; joycon_button_inputs[i] > 0; i++) + input_set_capability(ctlr->input, EV_KEY, + joycon_button_inputs[i]); + + ret = input_register_device(ctlr->input); + if (ret) + goto err_input; + + /* Set the default controller player leds based on controller number */ + mutex_lock(&joycon_input_num_mutex); + mutex_lock(&ctlr->output_mutex); + ret = joycon_set_player_leds(ctlr, 0, 0xF >> (4 - input_num)); + if (ret) + hid_warn(ctlr->hdev, "Failed to set leds; ret=%d\n", ret); + mutex_unlock(&ctlr->output_mutex); + if (++input_num > 4) + input_num = 1; + mutex_unlock(&joycon_input_num_mutex); + + return 0; + +err_input: + input_free_device(ctlr->input); +err: + return ret; +} + +/* Common handler for parsing inputs */ +static int joycon_ctlr_read_handler(struct joycon_ctlr *ctlr, u8 *data, + int size) +{ + int ret = 0; + + switch (data[0]) { + case JC_INPUT_SUBCMD_REPLY: + case JC_INPUT_IMU_DATA: + case JC_INPUT_MCU_DATA: + if (size >= 12) /* make sure it contains the input report */ + joycon_parse_report(ctlr, data); + break; + default: + break; + } + + return ret; +} + +static int joycon_ctlr_handle_event(struct joycon_ctlr *ctlr, u8 *data, + int size) +{ + int ret = 0; + bool match = false; + struct joycon_input_report *report; + + if (unlikely(mutex_is_locked(&ctlr->output_mutex)) && + ctlr->msg_type != JOYCON_MSG_TYPE_NONE) { + switch (ctlr->msg_type) { + case JOYCON_MSG_TYPE_USB: + if (size < 2) + break; + if (data[0] == JC_INPUT_USB_RESPONSE && + data[1] == ctlr->usb_ack_match) + match = true; + break; + case JOYCON_MSG_TYPE_SUBCMD: + if (size < sizeof(struct joycon_input_report) || + data[0] != JC_INPUT_SUBCMD_REPLY) + break; + report = (struct joycon_input_report *)data; + if (report->reply.id == ctlr->subcmd_ack_match) + match = true; + break; + default: + break; + } + + if (match) { + memcpy(ctlr->input_buf, data, + min(size, (int)JC_MAX_RESP_SIZE)); + ctlr->msg_type = JOYCON_MSG_TYPE_NONE; + ctlr->received_resp = true; + wake_up(&ctlr->wait); + + /* This message has been handled */ + return 1; + } + } + + if (ctlr->ctlr_state == JOYCON_CTLR_STATE_READ) + ret = joycon_ctlr_read_handler(ctlr, data, size); + + return ret; +} + +static int joycon_hid_event(struct hid_device *hdev, + struct hid_report *report, u8 *raw_data, int size) +{ + struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); + + if (size < 1) + return -EINVAL; + + return joycon_ctlr_handle_event(ctlr, raw_data, size); +} + +static void joycon_ctlr_destroy(struct joycon_ctlr *ctlr) +{ + if (ctlr->input) + input_unregister_device(ctlr->input); + mutex_destroy(&ctlr->output_mutex); +} + +static int joycon_hid_probe(struct hid_device *hdev, + const struct hid_device_id *id) +{ + int ret; + struct joycon_ctlr *ctlr; + + hid_dbg(hdev, "probe - start\n"); + + ctlr = devm_kzalloc(&hdev->dev, sizeof(*ctlr), GFP_KERNEL); + if (!ctlr) { + ret = -ENOMEM; + goto err; + } + + ctlr->hdev = hdev; + ctlr->ctlr_state = JOYCON_CTLR_STATE_INIT; + hid_set_drvdata(hdev, ctlr); + mutex_init(&ctlr->output_mutex); + init_waitqueue_head(&ctlr->wait); + + ret = hid_parse(hdev); + if (ret) { + hid_err(hdev, "HID parse failed\n"); + goto err; + } + + ret = hid_hw_start(hdev, HID_CONNECT_HIDRAW); + if (ret) { + hid_err(hdev, "HW start failed\n"); + goto err; + } + + ret = hid_hw_open(hdev); + if (ret) { + hid_err(hdev, "cannot start hardware I/O\n"); + goto err_stop; + } + + hid_device_io_start(hdev); + + /* Initialize the controller */ + mutex_lock(&ctlr->output_mutex); + /* if baudrate command fails, assume ble pro controller */ + if (hdev->product == USB_DEVICE_ID_NINTENDO_PROCON && + !joycon_send_usb(ctlr, JC_USB_CMD_BAUDRATE_3M)) { + /* handshake */ + ret = joycon_send_usb(ctlr, JC_USB_CMD_HANDSHAKE); + if (ret) { + hid_err(hdev, "Failed handshake; ret=%d\n", ret); + goto err_mutex; + } + /* + * Set no timeout (to keep controller in USB mode). + * This doesn't send a response, so ignore the timeout. + */ + joycon_send_usb(ctlr, JC_USB_CMD_NO_TIMEOUT); + } + + /* get controller calibration data, and parse it */ + ret = joycon_request_calibration(ctlr); + if (ret) { + hid_err(hdev, "Failed to retrieve calibration; ret=%d\n", ret); + goto err_mutex; + } + + /* Set the reporting mode to 0x30, which is the full report mode */ + ret = joycon_set_report_mode(ctlr); + if (ret) { + hid_err(hdev, "Failed to set report mode; ret=%d\n", ret); + goto err_mutex; + } + + mutex_unlock(&ctlr->output_mutex); + + ret = joycon_input_create(ctlr); + if (ret) { + hid_err(hdev, "Failed to create input device; ret=%d\n", ret); + goto err_close; + } + + ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; + + hid_dbg(hdev, "probe - success\n"); + return 0; + +err_mutex: + mutex_unlock(&ctlr->output_mutex); +err_close: + joycon_ctlr_destroy(ctlr); + hid_hw_close(hdev); +err_stop: + hid_hw_stop(hdev); +err: + hid_err(hdev, "probe - fail = %d\n", ret); + return ret; +} + +static void joycon_hid_remove(struct hid_device *hdev) +{ + struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); + + hid_dbg(hdev, "remove\n"); + hid_hw_close(hdev); + hid_hw_stop(hdev); + joycon_ctlr_destroy(ctlr); +} + +static const struct hid_device_id joycon_hid_devices[] = { + { HID_USB_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_PROCON) }, + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_PROCON) }, + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_JOYCONL) }, + { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NINTENDO, + USB_DEVICE_ID_NINTENDO_JOYCONR) }, + { } +}; +MODULE_DEVICE_TABLE(hid, joycon_hid_devices); + +static struct hid_driver joycon_hid_driver = { + .name = "joycon", + .id_table = joycon_hid_devices, + .probe = joycon_hid_probe, + .remove = joycon_hid_remove, + .raw_event = joycon_hid_event, +}; +module_hid_driver(joycon_hid_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Daniel J. Ogorchock "); +MODULE_DESCRIPTION("Driver for Nintendo Switch Controllers"); From patchwork Mon Mar 11 02:03:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ogorchock X-Patchwork-Id: 10846565 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8EE1B1823 for ; Mon, 11 Mar 2019 02:04:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7AD0328D43 for ; Mon, 11 Mar 2019 02:04:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6F1E328D4E; Mon, 11 Mar 2019 02:04:08 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E611128D43 for ; Mon, 11 Mar 2019 02:04:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727148AbfCKCEH (ORCPT ); Sun, 10 Mar 2019 22:04:07 -0400 Received: from mail-yw1-f65.google.com ([209.85.161.65]:37664 "EHLO mail-yw1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726758AbfCKCEH (ORCPT ); Sun, 10 Mar 2019 22:04:07 -0400 Received: by mail-yw1-f65.google.com with SMTP id k14so2632592ywe.4 for ; Sun, 10 Mar 2019 19:04:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=T6k/eQgqOVvnhKag5UpGkNvhNzB3Z963soX3T6WKykk=; b=O/iXedK2u+muvrOi87Kx0gBNrVhhUQwHoTAciT4H5Ybg9W/a28yjoRh5fkT9WzVHuE hOrne/9ZVvnrcflHOILEz7ibRv1ezXxjJRViGE4O2k5yRydob0w478/v10CkN6A6Paxr BchKOFkS2qua8CcL683b3KEKkzs6cCGISu6r1hDVz9de6aQsjXO/F6aCnDYbWUf96Am9 RK5yNO3JlYlH08TGKq47tVLg1NCSQ2WVtWZYXCsSE4E1fZgmdrE+9aKu7HBUtEzO9XJY RfYdOEa4rQXDMnFK/ExUEJK2auxS/eQEtA9tiLhAcbqNLSNXeBud8IRNC+u+GiKvDcCE gWhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=T6k/eQgqOVvnhKag5UpGkNvhNzB3Z963soX3T6WKykk=; b=BLC8jjNVJtjJuAdyHosOojod01eI+fxM0/6kGZQsmrNPdRlv3jmgXMG4JcBva2Cnl0 +QHP1OMSL3gkBXoBEhKi/rut0uT/FH/dA6ScMOCBkg83Gy0cAfFN0w7wTvf9z7c+miFz SRDPNFWHPbDAVJhgc8E9ZwX562CHLmNoydGAywlVQ9tlk44sXrGkOvIXr3GVbC0vkW4H n38yGUefFvtQRwZytoc+Xlqw1KGv8zdlp/zHn/YKddF4njiXRfhfiS74fyZ99z1VKWso eGm/f4s0SEqfx9XTHe6bfwk6iaP1pNpXtuWM1oK4qcEJkupme2cGnkHBqXJVeVQTUqW5 /pfg== X-Gm-Message-State: APjAAAWIQJuplKKCDrskmLpHlP7JGzlkTBwp9dcHMkx/Olgl8l4ounCv 5xTvMy9Mv0OVi6Drf1iU1O1GLFHk9fU= X-Google-Smtp-Source: APXvYqxjXIaFX9T65zIDGWZXWqg6ZnubXdflyuYdIWJczaACr3J4uRe0ODhqTaOlG+uimKPZlgpIFQ== X-Received: by 2002:a81:5f86:: with SMTP id t128mr24344552ywb.467.1552269845885; Sun, 10 Mar 2019 19:04:05 -0700 (PDT) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id r130sm1907088ywr.4.2019.03.10.19.04.04 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Mar 2019 19:04:05 -0700 (PDT) From: "Daniel J. Ogorchock" To: linux-input@vger.kernel.org Cc: thunderbird2k@gmail.com, blaws05@gmail.com, benjamin.tissoires@redhat.com, jikos@kernel.org, Roderick.Colenbrander@sony.com, peter.hutterer@who-t.net, jbrandst@2ds.eu, hadess@hadess.net, "Daniel J. Ogorchock" Subject: [PATCH v4 2/6] HID: joycon: add player led support Date: Sun, 10 Mar 2019 21:03:21 -0500 Message-Id: <20190311020324.23619-3-djogorchock@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190311020324.23619-1-djogorchock@gmail.com> References: <20190311020324.23619-1-djogorchock@gmail.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds led_classdev functionality to the switch controller driver. It adds support for the 4 player LEDs. The Home Button LED still needs to be supported on the pro controllers and right joy-con. Signed-off-by: Daniel J. Ogorchock --- drivers/hid/Kconfig | 2 + drivers/hid/hid-joycon.c | 111 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 106 insertions(+), 7 deletions(-) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 085633df2972..f324ef3face3 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -448,6 +448,8 @@ config HID_JABRA config HID_JOYCON tristate "Nintendo Joy-Con and Pro Controller support" depends on HID + depends on NEW_LEDS + depends on LEDS_CLASS help Adds support for the Nintendo Switch Joy-Cons and Pro Controller. All controllers support bluetooth, and the Pro Controller also supports diff --git a/drivers/hid/hid-joycon.c b/drivers/hid/hid-joycon.c index e70b9f8239ca..fde7ae3bb9bf 100644 --- a/drivers/hid/hid-joycon.c +++ b/drivers/hid/hid-joycon.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -186,11 +187,13 @@ struct joycon_input_report { } __packed; #define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35) +#define JC_NUM_LEDS 4 /* Each physical controller is associated with a joycon_ctlr struct */ struct joycon_ctlr { struct hid_device *hdev; struct input_dev *input; + struct led_classdev leds[JC_NUM_LEDS]; enum joycon_ctlr_state ctlr_state; /* The following members are used for synchronous sends/receives */ @@ -491,11 +494,9 @@ static const unsigned int joycon_button_inputs[] = { 0 /* 0 signals end of array */ }; -static DEFINE_MUTEX(joycon_input_num_mutex); static int joycon_input_create(struct joycon_ctlr *ctlr) { struct hid_device *hdev; - static int input_num = 1; const char *name; int ret; int i; @@ -554,6 +555,70 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) if (ret) goto err_input; + return 0; + +err_input: + input_free_device(ctlr->input); +err: + return ret; +} + +static int joycon_player_led_brightness_set(struct led_classdev *led, + enum led_brightness brightness) +{ + struct device *dev = led->dev->parent; + struct hid_device *hdev = to_hid_device(dev); + struct joycon_ctlr *ctlr; + int val = 0; + int i; + int ret; + int num; + + ctlr = hid_get_drvdata(hdev); + if (!ctlr) { + hid_err(hdev, "No controller data\n"); + return -ENODEV; + } + + /* determine which player led this is */ + for (num = 0; num < JC_NUM_LEDS; num++) { + if (&ctlr->leds[num] == led) + break; + } + if (num >= JC_NUM_LEDS) + return -EINVAL; + + mutex_lock(&ctlr->output_mutex); + for (i = 0; i < JC_NUM_LEDS; i++) { + if (i == num) + val |= brightness << i; + else + val |= ctlr->leds[i].brightness << i; + } + ret = joycon_set_player_leds(ctlr, 0, val); + mutex_unlock(&ctlr->output_mutex); + + return ret; +} + +static const char * const joycon_player_led_names[] = { + "player1", + "player2", + "player3", + "player4" +}; + +static DEFINE_MUTEX(joycon_input_num_mutex); +static int joycon_player_leds_create(struct joycon_ctlr *ctlr) +{ + struct hid_device *hdev = ctlr->hdev; + struct led_classdev *led; + size_t name_len; + char *name; + int ret = 0; + int i; + static int input_num = 1; + /* Set the default controller player leds based on controller number */ mutex_lock(&joycon_input_num_mutex); mutex_lock(&ctlr->output_mutex); @@ -561,15 +626,40 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) if (ret) hid_warn(ctlr->hdev, "Failed to set leds; ret=%d\n", ret); mutex_unlock(&ctlr->output_mutex); + + /* configure the player LEDs */ + for (i = 0; i < JC_NUM_LEDS; i++) { + name_len = strlen(joycon_player_led_names[i]) + + strlen(dev_name(&hdev->dev)) + 2; + name = devm_kzalloc(&hdev->dev, name_len, GFP_KERNEL); + if (!name) { + ret = -ENOMEM; + break; + } + ret = snprintf(name, name_len, "%s:%s", dev_name(&hdev->dev), + joycon_player_led_names[i]); + if (ret < 0) + break; + + led = &ctlr->leds[i]; + led->name = name; + led->brightness = ((i + 1) <= input_num) ? LED_ON : LED_OFF; + led->max_brightness = LED_ON; + led->brightness_set_blocking = + joycon_player_led_brightness_set; + led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; + + ret = devm_led_classdev_register(&hdev->dev, led); + if (ret) { + hid_err(hdev, "Failed registering %s LED\n", led->name); + break; + } + } + if (++input_num > 4) input_num = 1; mutex_unlock(&joycon_input_num_mutex); - return 0; - -err_input: - input_free_device(ctlr->input); -err: return ret; } @@ -738,6 +828,13 @@ static int joycon_hid_probe(struct hid_device *hdev, goto err_close; } + /* Initialize the leds */ + ret = joycon_player_leds_create(ctlr); + if (ret) { + hid_err(hdev, "Failed to create leds; ret=%d\n", ret); + goto err_close; + } + ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; hid_dbg(hdev, "probe - success\n"); From patchwork Mon Mar 11 02:03:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ogorchock X-Patchwork-Id: 10846567 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 438751823 for ; Mon, 11 Mar 2019 02:04:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F23F28D49 for ; Mon, 11 Mar 2019 02:04:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2309E28D52; Mon, 11 Mar 2019 02:04:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8014228D49 for ; Mon, 11 Mar 2019 02:04:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727153AbfCKCEP (ORCPT ); Sun, 10 Mar 2019 22:04:15 -0400 Received: from mail-yw1-f67.google.com ([209.85.161.67]:43800 "EHLO mail-yw1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726758AbfCKCEO (ORCPT ); Sun, 10 Mar 2019 22:04:14 -0400 Received: by mail-yw1-f67.google.com with SMTP id d129so2617917ywe.10 for ; Sun, 10 Mar 2019 19:04:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cJOLtQZtZEOafRlY/HZ7/9/crk5OhMrCluYfjvlfZjc=; b=eAiaaOjr+/IM64BsGQkvU3jdbI6woK1d5SoTlbWdXH9hQ6v/WukfwmvWG2B7Z4ISJq 9VbM3pFcGUHf/9uuhiSKF/ZW5wGLUFLiEvlIiWsSCFOfFf7ZsrWzXxn67BTPyeP9VOLr leoRDFslOvKE4clmf/VGxNV/KHzoNjzOXoX9+0mYT9gCB9r4ocnOL5zK2rv2xZaW5Wb9 5aC0Myjs1+PWjWxCTWXQ+NcNutLBFQEGa8VV4i7BhN20FxeaOFkrnzzS43lKf53dOsog NDiCzYKU1RAb19luzf+uO3YxqOf7I3XojDIL8SEuNSrK/c0YgOUVs0r3SZTYMAephElz T97g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cJOLtQZtZEOafRlY/HZ7/9/crk5OhMrCluYfjvlfZjc=; b=M/RLHyzINJD2cpitCibYSxhV4S4juhLM6JvhsTDfqed3prXIjYjFCeF2RrOwIpgjRD CaSQWVnuct61fGaL+NzNJZS9ADVs8aczf4ams+xtP0+/pk/dEIF1hV4Urgst4ZKmJmng 44Q8IBMexc7eDtV2sOYHirHQ25SeU8fWIfv7My+ivBKQ/6Jeu+RjDFmB62U+KTSD9KEA q4Ccy8L07fC8r48qO4HJvPYDz3SiVbsmiB9svPXmFa4Gy2elN1DEOCjqQoBQQPwEc4lK EfuTWaOLnfA/V66eLNx47mYwhLyz9RqL5wYTwyZmjrCCCmdQM180/k3/yqgTyCGU79pQ TOEg== X-Gm-Message-State: APjAAAWOytWrPdSwPKhmzssvOrxiRIr1AHgTrX7flyW762dDbXA+ZsNb Iw6tv2EOLIdR6RQNs6oNTUsYJzwriZE= X-Google-Smtp-Source: APXvYqx4MuyuRwCj2bhb4YJuhWdPNP17AQvzUyafENKcrzsjNlzrb1WpIyKerQOfiqke5wRWXHzrqA== X-Received: by 2002:a25:5f01:: with SMTP id t1mr26256713ybb.286.1552269853337; Sun, 10 Mar 2019 19:04:13 -0700 (PDT) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id r130sm1907088ywr.4.2019.03.10.19.04.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Mar 2019 19:04:12 -0700 (PDT) From: "Daniel J. Ogorchock" To: linux-input@vger.kernel.org Cc: thunderbird2k@gmail.com, blaws05@gmail.com, benjamin.tissoires@redhat.com, jikos@kernel.org, Roderick.Colenbrander@sony.com, peter.hutterer@who-t.net, jbrandst@2ds.eu, hadess@hadess.net, "Daniel J. Ogorchock" Subject: [PATCH v4 3/6] HID: joycon: add power supply support Date: Sun, 10 Mar 2019 21:03:22 -0500 Message-Id: <20190311020324.23619-4-djogorchock@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190311020324.23619-1-djogorchock@gmail.com> References: <20190311020324.23619-1-djogorchock@gmail.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds power_supply functionality to the switch controller driver for its battery. Signed-off-by: Daniel J. Ogorchock --- drivers/hid/Kconfig | 1 + drivers/hid/hid-joycon.c | 132 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index f324ef3face3..15bed99a7fa1 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -450,6 +450,7 @@ config HID_JOYCON depends on HID depends on NEW_LEDS depends on LEDS_CLASS + select POWER_SUPPLY help Adds support for the Nintendo Switch Joy-Cons and Pro Controller. All controllers support bluetooth, and the Pro Controller also supports diff --git a/drivers/hid/hid-joycon.c b/drivers/hid/hid-joycon.c index fde7ae3bb9bf..cfc23e881e4c 100644 --- a/drivers/hid/hid-joycon.c +++ b/drivers/hid/hid-joycon.c @@ -11,6 +11,7 @@ * https://github.com/MTCKC/ProconXInput * hid-wiimote kernel hid driver * hid-logitech-hidpp driver + * hid-sony driver * * This driver supports the Nintendo Switch Joy-Cons and Pro Controllers. The * Pro Controllers can either be used over USB or Bluetooth. @@ -27,6 +28,7 @@ #include #include #include +#include #include /* @@ -195,6 +197,7 @@ struct joycon_ctlr { struct input_dev *input; struct led_classdev leds[JC_NUM_LEDS]; enum joycon_ctlr_state ctlr_state; + spinlock_t lock; /* The following members are used for synchronous sends/receives */ enum joycon_msg_type msg_type; @@ -212,6 +215,12 @@ struct joycon_ctlr { struct joycon_stick_cal right_stick_cal_x; struct joycon_stick_cal right_stick_cal_y; + /* power supply data */ + struct power_supply *battery; + struct power_supply_desc battery_desc; + u8 battery_capacity; + bool battery_charging; + bool host_powered; }; static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len) @@ -415,9 +424,41 @@ static int joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val) static void joycon_parse_report(struct joycon_ctlr *ctlr, u8 *data) { struct input_dev *dev = ctlr->input; + unsigned long flags; + u8 tmp; u32 btns; u32 id = ctlr->hdev->product; + /* Parse the battery status */ + tmp = data[2]; + spin_lock_irqsave(&ctlr->lock, flags); + ctlr->host_powered = tmp & BIT(0); + ctlr->battery_charging = tmp & BIT(4); + tmp = tmp >> 5; + switch (tmp) { + case 0: /* empty */ + ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL; + break; + case 1: /* low */ + ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_LOW; + break; + case 2: /* medium */ + ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; + break; + case 3: /* high */ + ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_HIGH; + break; + case 4: /* full */ + ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_FULL; + break; + default: + ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; + hid_warn(ctlr->hdev, "Invalid battery status\n"); + break; + } + spin_unlock_irqrestore(&ctlr->lock, flags); + + /* Parse the buttons and sticks */ btns = hid_field_extract(ctlr->hdev, data + 3, 0, 24); if (id != USB_DEVICE_ID_NINTENDO_JOYCONR) { @@ -663,6 +704,89 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr) return ret; } +static int joycon_battery_get_property(struct power_supply *supply, + enum power_supply_property prop, + union power_supply_propval *val) +{ + struct joycon_ctlr *ctlr = power_supply_get_drvdata(supply); + unsigned long flags; + int ret = 0; + u8 capacity; + bool charging; + bool powered; + + spin_lock_irqsave(&ctlr->lock, flags); + capacity = ctlr->battery_capacity; + charging = ctlr->battery_charging; + powered = ctlr->host_powered; + spin_unlock_irqrestore(&ctlr->lock, flags); + + switch (prop) { + case POWER_SUPPLY_PROP_PRESENT: + val->intval = 1; + break; + case POWER_SUPPLY_PROP_SCOPE: + val->intval = POWER_SUPPLY_SCOPE_DEVICE; + break; + case POWER_SUPPLY_PROP_CAPACITY_LEVEL: + val->intval = capacity; + break; + case POWER_SUPPLY_PROP_STATUS: + if (charging) + val->intval = POWER_SUPPLY_STATUS_CHARGING; + else if (capacity == 100 && powered) + val->intval = POWER_SUPPLY_STATUS_FULL; + else + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; + break; + default: + ret = -EINVAL; + break; + } + return ret; +} + +static enum power_supply_property joycon_battery_props[] = { + POWER_SUPPLY_PROP_PRESENT, + POWER_SUPPLY_PROP_CAPACITY_LEVEL, + POWER_SUPPLY_PROP_SCOPE, + POWER_SUPPLY_PROP_STATUS, +}; + +static int joycon_power_supply_create(struct joycon_ctlr *ctlr) +{ + struct hid_device *hdev = ctlr->hdev; + struct power_supply_config supply_config = { .drv_data = ctlr, }; + const char * const name_fmt = "nintendo_switch_controller_battery_%s"; + int ret = 0; + + /* Set initially to unknown before receiving first input report */ + ctlr->battery_capacity = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; + + /* Configure the battery's description */ + ctlr->battery_desc.properties = joycon_battery_props; + ctlr->battery_desc.num_properties = + ARRAY_SIZE(joycon_battery_props); + ctlr->battery_desc.get_property = joycon_battery_get_property; + ctlr->battery_desc.use_for_apm = 0; + ctlr->battery_desc.name = devm_kasprintf(&hdev->dev, GFP_KERNEL, + name_fmt, + dev_name(&hdev->dev)); + if (!ctlr->battery_desc.name) + return -ENOMEM; + + ctlr->battery = devm_power_supply_register(&hdev->dev, + &ctlr->battery_desc, + &supply_config); + if (IS_ERR(ctlr->battery)) { + ret = PTR_ERR(ctlr->battery); + hid_err(hdev, "Failed to register battery; ret=%d\n", ret); + return ret; + } + power_supply_powers(ctlr->battery, &hdev->dev); + return 0; +} + /* Common handler for parsing inputs */ static int joycon_ctlr_read_handler(struct joycon_ctlr *ctlr, u8 *data, int size) @@ -767,6 +891,7 @@ static int joycon_hid_probe(struct hid_device *hdev, hid_set_drvdata(hdev, ctlr); mutex_init(&ctlr->output_mutex); init_waitqueue_head(&ctlr->wait); + spin_lock_init(&ctlr->lock); ret = hid_parse(hdev); if (ret) { @@ -835,6 +960,13 @@ static int joycon_hid_probe(struct hid_device *hdev, goto err_close; } + /* Initialize the battery power supply */ + ret = joycon_power_supply_create(ctlr); + if (ret) { + hid_err(hdev, "Failed to create power_supply; ret=%d\n", ret); + goto err_close; + } + ctlr->ctlr_state = JOYCON_CTLR_STATE_READ; hid_dbg(hdev, "probe - success\n"); From patchwork Mon Mar 11 02:03:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ogorchock X-Patchwork-Id: 10846569 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D36806C2 for ; Mon, 11 Mar 2019 02:04:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BFE9728D49 for ; Mon, 11 Mar 2019 02:04:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B0D3A28D4E; Mon, 11 Mar 2019 02:04:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41F2028D55 for ; Mon, 11 Mar 2019 02:04:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726758AbfCKCEP (ORCPT ); Sun, 10 Mar 2019 22:04:15 -0400 Received: from mail-yw1-f67.google.com ([209.85.161.67]:39215 "EHLO mail-yw1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727151AbfCKCEP (ORCPT ); Sun, 10 Mar 2019 22:04:15 -0400 Received: by mail-yw1-f67.google.com with SMTP id z191so2626844ywa.6 for ; Sun, 10 Mar 2019 19:04:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IcDZjyxkNgetjjgWf4zVzG8MZPPWgHMPeik4Pg/g2W0=; b=LKQJb295Iq8KlgStgHbvub0Cj/cEOZNeAtV0GrWr+d6nFRRuNDaCN0nsI+qlU0pD3Q YIS7H+9hvKfq/k5kQoeVanpatxKaiu954lujal8MxdomLdEeagdH1WNs3e504lPmAjNi DFrAegNiX2SMVtxq5uE5mUKUfwu1oIxl6HHVxCh+0Ea4dHSx79kG83Ihnl68CtGlbIpN eUO8bkcodOOnBHMNtyQL95SIeOxfJZ0xsWFll3ymcoWiykRUpWjZOtQBqsHMbQMN47+x t3g0Sie6/3lOFpQAqJy3t0N+Z8nr3MBXXswdDLEwiUfiEqjiAnQQbFNlm6RmWUaTB1Mx e1Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IcDZjyxkNgetjjgWf4zVzG8MZPPWgHMPeik4Pg/g2W0=; b=adBV3lpTSEiEm+TjbxLG1Oljk4yDZDomDJFrJs37MjfsOuBFZJVu7XqLbhl4wUi99X 1b5pFJV3u1gJxzVr5bmec797jS6nONYVHX4KjrUQBt9Q2mhT2d0Ab3DEy06HMQn7i5A8 ECLIcH6i+bcfzNdv/Hp6aREIKZIX4A/+oOX2oLjo4Wo7hunAJixvpjFguplAUxoy0bKF trAEcC6v4PlZlpJQRFAliBpHzxOyUxY9xP3O9VeIudMQMl5riFwFG2qdoDWfVAqU81ez 8dD3hUSNJqrjKU3Alchs5xoM6vSIvV/p3aTgNyU0pkj2iNWVI0V6g92qvjx2tO5PLD3x 1RMQ== X-Gm-Message-State: APjAAAUcJ1QCwGc2VoYt0sLPRY8Qe4e5Npls+zRk6KXxH2ELfnAGkg2m svR6nB8gGJqaYjg+TQvQ2qqsC3AWsD8= X-Google-Smtp-Source: APXvYqyJzmlMTdl95ONw6DgR6h4YIekq8o11Kvxhwi4uS6VlahSqLR2V2metJUuepbhaQ3d+NOTcGg== X-Received: by 2002:a25:7c87:: with SMTP id x129mr25398086ybc.44.1552269854354; Sun, 10 Mar 2019 19:04:14 -0700 (PDT) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id r130sm1907088ywr.4.2019.03.10.19.04.13 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Mar 2019 19:04:13 -0700 (PDT) From: "Daniel J. Ogorchock" To: linux-input@vger.kernel.org Cc: thunderbird2k@gmail.com, blaws05@gmail.com, benjamin.tissoires@redhat.com, jikos@kernel.org, Roderick.Colenbrander@sony.com, peter.hutterer@who-t.net, jbrandst@2ds.eu, hadess@hadess.net, "Daniel J. Ogorchock" Subject: [PATCH v4 4/6] HID: joycon: add home led support Date: Sun, 10 Mar 2019 21:03:23 -0500 Message-Id: <20190311020324.23619-5-djogorchock@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190311020324.23619-1-djogorchock@gmail.com> References: <20190311020324.23619-1-djogorchock@gmail.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds the ability to set the intensity level of the home button's LED. Signed-off-by: Daniel J. Ogorchock --- drivers/hid/hid-joycon.c | 79 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 6 deletions(-) diff --git a/drivers/hid/hid-joycon.c b/drivers/hid/hid-joycon.c index cfc23e881e4c..fcf66a961365 100644 --- a/drivers/hid/hid-joycon.c +++ b/drivers/hid/hid-joycon.c @@ -195,7 +195,8 @@ struct joycon_input_report { struct joycon_ctlr { struct hid_device *hdev; struct input_dev *input; - struct led_classdev leds[JC_NUM_LEDS]; + struct led_classdev leds[JC_NUM_LEDS]; /* player leds */ + struct led_classdev home_led; enum joycon_ctlr_state ctlr_state; spinlock_t lock; @@ -642,6 +643,40 @@ static int joycon_player_led_brightness_set(struct led_classdev *led, return ret; } +static int joycon_home_led_brightness_set(struct led_classdev *led, + enum led_brightness brightness) +{ + struct device *dev = led->dev->parent; + struct hid_device *hdev = to_hid_device(dev); + struct joycon_ctlr *ctlr; + struct joycon_subcmd_request *req; + u8 buffer[sizeof(*req) + 5] = { 0 }; + u8 *data; + int ret; + + ctlr = hid_get_drvdata(hdev); + if (!ctlr) { + hid_err(hdev, "No controller data\n"); + return -ENODEV; + } + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id = JC_SUBCMD_SET_HOME_LIGHT; + data = JC_SUBCMD_REQ_GET_DATA(req); + data[0] = 0x01; + data[1] = brightness << 4; + data[2] = brightness | (brightness << 4); + data[3] = 0x11; + data[4] = 0x11; + + hid_dbg(hdev, "setting home led brightness\n"); + mutex_lock(&ctlr->output_mutex); + ret = joycon_send_subcmd(ctlr, req, 5); + mutex_unlock(&ctlr->output_mutex); + + return ret; +} + static const char * const joycon_player_led_names[] = { "player1", "player2", @@ -650,7 +685,7 @@ static const char * const joycon_player_led_names[] = { }; static DEFINE_MUTEX(joycon_input_num_mutex); -static int joycon_player_leds_create(struct joycon_ctlr *ctlr) +static int joycon_leds_create(struct joycon_ctlr *ctlr) { struct hid_device *hdev = ctlr->hdev; struct led_classdev *led; @@ -675,12 +710,12 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr) name = devm_kzalloc(&hdev->dev, name_len, GFP_KERNEL); if (!name) { ret = -ENOMEM; - break; + goto err; } ret = snprintf(name, name_len, "%s:%s", dev_name(&hdev->dev), joycon_player_led_names[i]); if (ret < 0) - break; + goto err; led = &ctlr->leds[i]; led->name = name; @@ -693,7 +728,7 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr) ret = devm_led_classdev_register(&hdev->dev, led); if (ret) { hid_err(hdev, "Failed registering %s LED\n", led->name); - break; + goto err; } } @@ -701,6 +736,38 @@ static int joycon_player_leds_create(struct joycon_ctlr *ctlr) input_num = 1; mutex_unlock(&joycon_input_num_mutex); + /* configure the home LED */ + if (ctlr->hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONL) { + name_len = strlen("home") + strlen(dev_name(&hdev->dev)) + 2; + name = devm_kzalloc(&hdev->dev, name_len, GFP_KERNEL); + if (!name) { + ret = -ENOMEM; + goto err; + } + ret = snprintf(name, name_len, "%s:%s", dev_name(&hdev->dev), + "home"); + if (ret < 0) + goto err; + + led = &ctlr->home_led; + led->name = name; + led->brightness = 0; + led->max_brightness = 0xF; + led->brightness_set_blocking = joycon_home_led_brightness_set; + led->flags = LED_CORE_SUSPENDRESUME | LED_HW_PLUGGABLE; + ret = devm_led_classdev_register(&hdev->dev, led); + if (ret) { + hid_err(hdev, "Failed registering home led\n"); + goto err; + } + /* Set the home LED to 0 as default state */ + ret = joycon_home_led_brightness_set(led, 0); + if (ret) + hid_err(hdev, "Failed to set home LED dflt; ret=%d\n", + ret); + } + +err: return ret; } @@ -954,7 +1021,7 @@ static int joycon_hid_probe(struct hid_device *hdev, } /* Initialize the leds */ - ret = joycon_player_leds_create(ctlr); + ret = joycon_leds_create(ctlr); if (ret) { hid_err(hdev, "Failed to create leds; ret=%d\n", ret); goto err_close; From patchwork Mon Mar 11 02:03:24 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ogorchock X-Patchwork-Id: 10846573 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id AEAD01823 for ; Mon, 11 Mar 2019 02:04:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9954728D43 for ; Mon, 11 Mar 2019 02:04:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8DB4628D4E; Mon, 11 Mar 2019 02:04:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 307A328D43 for ; Mon, 11 Mar 2019 02:04:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727159AbfCKCER (ORCPT ); Sun, 10 Mar 2019 22:04:17 -0400 Received: from mail-yw1-f66.google.com ([209.85.161.66]:36148 "EHLO mail-yw1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727151AbfCKCER (ORCPT ); Sun, 10 Mar 2019 22:04:17 -0400 Received: by mail-yw1-f66.google.com with SMTP id 189so2633111ywi.3 for ; Sun, 10 Mar 2019 19:04:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tDkC6f3jP/s3rGju1UT+B8EZ7geXUnJ5YQSy+O/fvZQ=; b=Kx6tdhJwfrVgaTWEQMG19p7jcQRTQ7uJnuN2IB/XfBkzWswY5poSOh1DazGC89vhHf zcSFr509EMz2WY+hNIVxM5xuHwmWWJHB9K4N+UmEvoEVswQgT2ooFzRBjx0lxNoTK14P ta1yYBOvJ97yH87df5Kx0ysO0VwzfpQFUqHuctElhxQKp4jcN4Ket5GOP/9LmV5INm1w 7Es5eHCPjWvP0w1Mn8VER6OzOof+spMWixSrEB8+PrwXqn5vTpRQkJ8Jn8t/JqMWQ+DN TZGicTWRPWVPpN5VgeYjHxVgjneYOeX78+cJj4buf+R0su7Bo+r2+qCvPwldyfJl1HQ1 P9dA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tDkC6f3jP/s3rGju1UT+B8EZ7geXUnJ5YQSy+O/fvZQ=; b=KnZfW9sbNkUm7OJ7Ka4ACxM2XWUMgG/EtLmSaf/g7r9GJ8H+cpdDXvd5UbB69KdFz1 8MHvEzysQHwF4kreGpKOdwlmgI2Uma2CRMJ8DpPGIbbzj8gTerBYFaLP7dIe5RVu9ldN Usz0QrJUvShZjuQXnimiPzuGrxzm3M457tX1Y2XWoMS/RfcsRpE9/saPkrUla9wgrz8f eZz22+RqN9pOwgzz/oBclejofpHjmXOg5AVZXyMncezxj8pCmgN8bW4xIRbQZBOHgR9n gBHK1e+MYJbYjXSKUEiqjc7YaS49pFSo0tagh8FWlTE03lCGSVwu54O6YvlvxSqF655a 3Ivg== X-Gm-Message-State: APjAAAWSZWTyH9yqQb8z8JrA8uMimw1w0evHKnV/TpCVjHUQLa7YBvSX F+U7hP6xljv/ne0O7M+N1BUQkxAGR+0= X-Google-Smtp-Source: APXvYqwMOesyR+mPYkBDV0IeMbr0js11cXFwXlMoIGQz6jG3veBBXacFapODwAZ2FPePGTVn3jjVKg== X-Received: by 2002:a81:130b:: with SMTP id 11mr23209400ywt.422.1552269855287; Sun, 10 Mar 2019 19:04:15 -0700 (PDT) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id r130sm1907088ywr.4.2019.03.10.19.04.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Mar 2019 19:04:14 -0700 (PDT) From: "Daniel J. Ogorchock" To: linux-input@vger.kernel.org Cc: thunderbird2k@gmail.com, blaws05@gmail.com, benjamin.tissoires@redhat.com, jikos@kernel.org, Roderick.Colenbrander@sony.com, peter.hutterer@who-t.net, jbrandst@2ds.eu, hadess@hadess.net, "Daniel J. Ogorchock" Subject: [PATCH v4 5/6] HID: joycon: add rumble support Date: Sun, 10 Mar 2019 21:03:24 -0500 Message-Id: <20190311020324.23619-6-djogorchock@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190311020324.23619-1-djogorchock@gmail.com> References: <20190311020324.23619-1-djogorchock@gmail.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds support for controller rumble. It also switches the report parser to using the joycon_input_report struct, which makes accessing the fields clearer. The ff_effect weak magnitude is associated with the pro controller's right motor (or with a right joy-con). The strong magnitude is associated with the pro's left motor (or a left joy-con). The rumble data is sent periodically (currently configured for every 250 milliseconds). If the controller receives no rumble data for too long a time period, it will stop vibrating. The data is also sent every time joycon_set_rumble is called to avoid latency of up to 250ms. Signed-off-by: Daniel J. Ogorchock --- drivers/hid/Kconfig | 10 ++ drivers/hid/hid-joycon.c | 326 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 328 insertions(+), 8 deletions(-) diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 15bed99a7fa1..40d1095fb73f 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -459,6 +459,16 @@ config HID_JOYCON To compile this driver as a module, choose M here: the module will be called hid-joycon. +config JOYCON_FF + bool "Nintendo Switch controller force feedback support" + depends on HID_JOYCON + select INPUT_FF_MEMLESS + help + Say Y here if you have a Nintendo Switch controller and want to enable + force feedback support for it. This works for both joy-cons and the pro + controller. For the pro controller, both rumble motors can be controlled + individually. + config HID_TWINHAN tristate "Twinhan IR remote control" depends on HID diff --git a/drivers/hid/hid-joycon.c b/drivers/hid/hid-joycon.c index fcf66a961365..10460bac3307 100644 --- a/drivers/hid/hid-joycon.c +++ b/drivers/hid/hid-joycon.c @@ -9,6 +9,7 @@ * https://gitlab.com/pjranki/joycon-linux-kernel (Peter Rankin) * https://github.com/FrotBot/SwitchProConLinuxUSB * https://github.com/MTCKC/ProconXInput + * https://github.com/Davidobot/BetterJoyForCemu * hid-wiimote kernel hid driver * hid-logitech-hidpp driver * hid-sony driver @@ -26,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -105,6 +107,123 @@ #define JC_STICK_FUZZ 250 #define JC_STICK_FLAT 500 +/* frequency/amplitude tables for rumble */ +struct joycon_rumble_freq_data { + u16 high; + u8 low; + u16 freq; /* Hz*/ +}; + +struct joycon_rumble_amp_data { + u8 high; + u16 low; + u16 amp; +}; + +/* + * These tables are from + * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md + */ +static const struct joycon_rumble_freq_data joycon_rumble_frequencies[] = { + /* high, low, freq */ + { 0x0000, 0x01, 41 }, { 0x0000, 0x02, 42 }, { 0x0000, 0x03, 43 }, + { 0x0000, 0x04, 44 }, { 0x0000, 0x05, 45 }, { 0x0000, 0x06, 46 }, + { 0x0000, 0x07, 47 }, { 0x0000, 0x08, 48 }, { 0x0000, 0x09, 49 }, + { 0x0000, 0x0A, 50 }, { 0x0000, 0x0B, 51 }, { 0x0000, 0x0C, 52 }, + { 0x0000, 0x0D, 53 }, { 0x0000, 0x0E, 54 }, { 0x0000, 0x0F, 55 }, + { 0x0000, 0x10, 57 }, { 0x0000, 0x11, 58 }, { 0x0000, 0x12, 59 }, + { 0x0000, 0x13, 60 }, { 0x0000, 0x14, 62 }, { 0x0000, 0x15, 63 }, + { 0x0000, 0x16, 64 }, { 0x0000, 0x17, 66 }, { 0x0000, 0x18, 67 }, + { 0x0000, 0x19, 69 }, { 0x0000, 0x1A, 70 }, { 0x0000, 0x1B, 72 }, + { 0x0000, 0x1C, 73 }, { 0x0000, 0x1D, 75 }, { 0x0000, 0x1e, 77 }, + { 0x0000, 0x1f, 78 }, { 0x0000, 0x20, 80 }, { 0x0400, 0x21, 82 }, + { 0x0800, 0x22, 84 }, { 0x0c00, 0x23, 85 }, { 0x1000, 0x24, 87 }, + { 0x1400, 0x25, 89 }, { 0x1800, 0x26, 91 }, { 0x1c00, 0x27, 93 }, + { 0x2000, 0x28, 95 }, { 0x2400, 0x29, 97 }, { 0x2800, 0x2a, 99 }, + { 0x2c00, 0x2b, 102 }, { 0x3000, 0x2c, 104 }, { 0x3400, 0x2d, 106 }, + { 0x3800, 0x2e, 108 }, { 0x3c00, 0x2f, 111 }, { 0x4000, 0x30, 113 }, + { 0x4400, 0x31, 116 }, { 0x4800, 0x32, 118 }, { 0x4c00, 0x33, 121 }, + { 0x5000, 0x34, 123 }, { 0x5400, 0x35, 126 }, { 0x5800, 0x36, 129 }, + { 0x5c00, 0x37, 132 }, { 0x6000, 0x38, 135 }, { 0x6400, 0x39, 137 }, + { 0x6800, 0x3a, 141 }, { 0x6c00, 0x3b, 144 }, { 0x7000, 0x3c, 147 }, + { 0x7400, 0x3d, 150 }, { 0x7800, 0x3e, 153 }, { 0x7c00, 0x3f, 157 }, + { 0x8000, 0x40, 160 }, { 0x8400, 0x41, 164 }, { 0x8800, 0x42, 167 }, + { 0x8c00, 0x43, 171 }, { 0x9000, 0x44, 174 }, { 0x9400, 0x45, 178 }, + { 0x9800, 0x46, 182 }, { 0x9c00, 0x47, 186 }, { 0xa000, 0x48, 190 }, + { 0xa400, 0x49, 194 }, { 0xa800, 0x4a, 199 }, { 0xac00, 0x4b, 203 }, + { 0xb000, 0x4c, 207 }, { 0xb400, 0x4d, 212 }, { 0xb800, 0x4e, 217 }, + { 0xbc00, 0x4f, 221 }, { 0xc000, 0x50, 226 }, { 0xc400, 0x51, 231 }, + { 0xc800, 0x52, 236 }, { 0xcc00, 0x53, 241 }, { 0xd000, 0x54, 247 }, + { 0xd400, 0x55, 252 }, { 0xd800, 0x56, 258 }, { 0xdc00, 0x57, 263 }, + { 0xe000, 0x58, 269 }, { 0xe400, 0x59, 275 }, { 0xe800, 0x5a, 281 }, + { 0xec00, 0x5b, 287 }, { 0xf000, 0x5c, 293 }, { 0xf400, 0x5d, 300 }, + { 0xf800, 0x5e, 306 }, { 0xfc00, 0x5f, 313 }, { 0x0001, 0x60, 320 }, + { 0x0401, 0x61, 327 }, { 0x0801, 0x62, 334 }, { 0x0c01, 0x63, 341 }, + { 0x1001, 0x64, 349 }, { 0x1401, 0x65, 357 }, { 0x1801, 0x66, 364 }, + { 0x1c01, 0x67, 372 }, { 0x2001, 0x68, 381 }, { 0x2401, 0x69, 389 }, + { 0x2801, 0x6a, 397 }, { 0x2c01, 0x6b, 406 }, { 0x3001, 0x6c, 415 }, + { 0x3401, 0x6d, 424 }, { 0x3801, 0x6e, 433 }, { 0x3c01, 0x6f, 443 }, + { 0x4001, 0x70, 453 }, { 0x4401, 0x71, 462 }, { 0x4801, 0x72, 473 }, + { 0x4c01, 0x73, 483 }, { 0x5001, 0x74, 494 }, { 0x5401, 0x75, 504 }, + { 0x5801, 0x76, 515 }, { 0x5c01, 0x77, 527 }, { 0x6001, 0x78, 538 }, + { 0x6401, 0x79, 550 }, { 0x6801, 0x7a, 562 }, { 0x6c01, 0x7b, 574 }, + { 0x7001, 0x7c, 587 }, { 0x7401, 0x7d, 600 }, { 0x7801, 0x7e, 613 }, + { 0x7c01, 0x7f, 626 }, { 0x8001, 0x00, 640 }, { 0x8401, 0x00, 654 }, + { 0x8801, 0x00, 668 }, { 0x8c01, 0x00, 683 }, { 0x9001, 0x00, 698 }, + { 0x9401, 0x00, 713 }, { 0x9801, 0x00, 729 }, { 0x9c01, 0x00, 745 }, + { 0xa001, 0x00, 761 }, { 0xa401, 0x00, 778 }, { 0xa801, 0x00, 795 }, + { 0xac01, 0x00, 812 }, { 0xb001, 0x00, 830 }, { 0xb401, 0x00, 848 }, + { 0xb801, 0x00, 867 }, { 0xbc01, 0x00, 886 }, { 0xc001, 0x00, 905 }, + { 0xc401, 0x00, 925 }, { 0xc801, 0x00, 945 }, { 0xcc01, 0x00, 966 }, + { 0xd001, 0x00, 987 }, { 0xd401, 0x00, 1009 }, { 0xd801, 0x00, 1031 }, + { 0xdc01, 0x00, 1053 }, { 0xe001, 0x00, 1076 }, { 0xe401, 0x00, 1100 }, + { 0xe801, 0x00, 1124 }, { 0xec01, 0x00, 1149 }, { 0xf001, 0x00, 1174 }, + { 0xf401, 0x00, 1199 }, { 0xf801, 0x00, 1226 }, { 0xfc01, 0x00, 1253 } +}; +static const u16 joycon_max_rumble_freq = +joycon_rumble_frequencies[ARRAY_SIZE(joycon_rumble_frequencies) - 1].freq; + +static const struct joycon_rumble_amp_data joycon_rumble_amplitudes[] = { + /* high, low, amp */ + { 0x00, 0x0040, 0 }, + { 0x02, 0x8040, 10 }, { 0x04, 0x0041, 12 }, { 0x06, 0x8041, 14 }, + { 0x08, 0x0042, 17 }, { 0x0a, 0x8042, 20 }, { 0x0c, 0x0043, 24 }, + { 0x0e, 0x8043, 28 }, { 0x10, 0x0044, 33 }, { 0x12, 0x8044, 40 }, + { 0x14, 0x0045, 47 }, { 0x16, 0x8045, 56 }, { 0x18, 0x0046, 67 }, + { 0x1a, 0x8046, 80 }, { 0x1c, 0x0047, 95 }, { 0x1e, 0x8047, 112 }, + { 0x20, 0x0048, 117 }, { 0x22, 0x8048, 123 }, { 0x24, 0x0049, 128 }, + { 0x26, 0x8049, 134 }, { 0x28, 0x004a, 140 }, { 0x2a, 0x804a, 146 }, + { 0x2c, 0x004b, 152 }, { 0x2e, 0x804b, 159 }, { 0x30, 0x004c, 166 }, + { 0x32, 0x804c, 173 }, { 0x34, 0x004d, 181 }, { 0x36, 0x804d, 189 }, + { 0x38, 0x004e, 198 }, { 0x3a, 0x804e, 206 }, { 0x3c, 0x004f, 215 }, + { 0x3e, 0x804f, 225 }, { 0x40, 0x0050, 230 }, { 0x42, 0x8050, 235 }, + { 0x44, 0x0051, 240 }, { 0x46, 0x8051, 245 }, { 0x48, 0x0052, 251 }, + { 0x4a, 0x8052, 256 }, { 0x4c, 0x0053, 262 }, { 0x4e, 0x8053, 268 }, + { 0x50, 0x0054, 273 }, { 0x52, 0x8054, 279 }, { 0x54, 0x0055, 286 }, + { 0x56, 0x8055, 292 }, { 0x58, 0x0056, 298 }, { 0x5a, 0x8056, 305 }, + { 0x5c, 0x0057, 311 }, { 0x5e, 0x8057, 318 }, { 0x60, 0x0058, 325 }, + { 0x62, 0x8058, 332 }, { 0x64, 0x0059, 340 }, { 0x66, 0x8059, 347 }, + { 0x68, 0x005a, 355 }, { 0x6a, 0x805a, 362 }, { 0x6c, 0x005b, 370 }, + { 0x6e, 0x805b, 378 }, { 0x70, 0x005c, 387 }, { 0x72, 0x805c, 395 }, + { 0x74, 0x005d, 404 }, { 0x76, 0x805d, 413 }, { 0x78, 0x005e, 422 }, + { 0x7a, 0x805e, 431 }, { 0x7c, 0x005f, 440 }, { 0x7e, 0x805f, 450 }, + { 0x80, 0x0060, 460 }, { 0x82, 0x8060, 470 }, { 0x84, 0x0061, 480 }, + { 0x86, 0x8061, 491 }, { 0x88, 0x0062, 501 }, { 0x8a, 0x8062, 512 }, + { 0x8c, 0x0063, 524 }, { 0x8e, 0x8063, 535 }, { 0x90, 0x0064, 547 }, + { 0x92, 0x8064, 559 }, { 0x94, 0x0065, 571 }, { 0x96, 0x8065, 584 }, + { 0x98, 0x0066, 596 }, { 0x9a, 0x8066, 609 }, { 0x9c, 0x0067, 623 }, + { 0x9e, 0x8067, 636 }, { 0xa0, 0x0068, 650 }, { 0xa2, 0x8068, 665 }, + { 0xa4, 0x0069, 679 }, { 0xa6, 0x8069, 694 }, { 0xa8, 0x006a, 709 }, + { 0xaa, 0x806a, 725 }, { 0xac, 0x006b, 741 }, { 0xae, 0x806b, 757 }, + { 0xb0, 0x006c, 773 }, { 0xb2, 0x806c, 790 }, { 0xb4, 0x006d, 808 }, + { 0xb6, 0x806d, 825 }, { 0xb8, 0x006e, 843 }, { 0xba, 0x806e, 862 }, + { 0xbc, 0x006f, 881 }, { 0xbe, 0x806f, 900 }, { 0xc0, 0x0070, 920 }, + { 0xc2, 0x8070, 940 }, { 0xc4, 0x0071, 960 }, { 0xc6, 0x8071, 981 }, + { 0xc8, 0x0072, 1003 } +}; +static const u16 joycon_max_rumble_amp = +joycon_rumble_amplitudes[ARRAY_SIZE(joycon_rumble_amplitudes) - 1].amp; + /* States for controller state machine */ enum joycon_ctlr_state { JOYCON_CTLR_STATE_INIT, @@ -190,6 +309,9 @@ struct joycon_input_report { #define JC_MAX_RESP_SIZE (sizeof(struct joycon_input_report) + 35) #define JC_NUM_LEDS 4 +#define JC_RUMBLE_DFLT_LOW_FREQ 160 +#define JC_RUMBLE_DFLT_HIGH_FREQ 320 +#define JC_RUMBLE_PERIOD_MS 250 /* Each physical controller is associated with a joycon_ctlr struct */ struct joycon_ctlr { @@ -222,6 +344,15 @@ struct joycon_ctlr { u8 battery_capacity; bool battery_charging; bool host_powered; + + /* rumble */ + u8 rumble_data[8]; + struct work_struct rumble_worker; + unsigned int rumble_msecs; + u16 rumble_ll_freq; + u16 rumble_lh_freq; + u16 rumble_rl_freq; + u16 rumble_rh_freq; }; static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len) @@ -278,6 +409,12 @@ static int joycon_send_subcmd(struct joycon_ctlr *ctlr, size_t data_len) { int ret; + unsigned long flags; + + spin_lock_irqsave(&ctlr->lock, flags); + memcpy(subcmd->rumble_data, ctlr->rumble_data, + sizeof(subcmd->rumble_data)); + spin_unlock_irqrestore(&ctlr->lock, flags); subcmd->output_id = JC_OUTPUT_RUMBLE_AND_SUBCMD; subcmd->packet_num = ctlr->subcmd_num; @@ -287,9 +424,11 @@ static int joycon_send_subcmd(struct joycon_ctlr *ctlr, ctlr->msg_type = JOYCON_MSG_TYPE_SUBCMD; ret = joycon_hid_send_sync(ctlr, (u8 *)subcmd, - sizeof(*subcmd) + data_len); - if (ret) + sizeof(*subcmd) + data_len); + if (ret < 0) hid_dbg(ctlr->hdev, "send subcommand failed; ret=%d\n", ret); + else + ret = 0; return ret; } @@ -404,6 +543,19 @@ static int joycon_set_report_mode(struct joycon_ctlr *ctlr) return joycon_send_subcmd(ctlr, req, 1); } +static int joycon_enable_rumble(struct joycon_ctlr *ctlr, bool enable) +{ + struct joycon_subcmd_request *req; + u8 buffer[sizeof(*req) + 1] = { 0 }; + + req = (struct joycon_subcmd_request *)buffer; + req->subcmd_id = JC_SUBCMD_ENABLE_VIBRATION; + JC_SUBCMD_REQ_GET_DATA(req)[0] = enable ? 0x01 : 0x00; + + hid_dbg(ctlr->hdev, "%s rumble\n", enable ? "enabling" : "disabling"); + return joycon_send_subcmd(ctlr, req, 1); +} + static int joycon_map_stick_val(struct joycon_stick_cal *cal, s32 val) { s32 center = cal->center; @@ -429,9 +581,17 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, u8 *data) u8 tmp; u32 btns; u32 id = ctlr->hdev->product; + struct joycon_input_report *rep = (struct joycon_input_report *) data; + unsigned long msecs = jiffies_to_msecs(jiffies); + + if (rep->vibrator_report && + (msecs - ctlr->rumble_msecs) >= JC_RUMBLE_PERIOD_MS) { + ctlr->rumble_msecs = msecs; + schedule_work(&ctlr->rumble_worker); + } /* Parse the battery status */ - tmp = data[2]; + tmp = rep->bat_con; spin_lock_irqsave(&ctlr->lock, flags); ctlr->host_powered = tmp & BIT(0); ctlr->battery_charging = tmp & BIT(4); @@ -460,7 +620,7 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, u8 *data) spin_unlock_irqrestore(&ctlr->lock, flags); /* Parse the buttons and sticks */ - btns = hid_field_extract(ctlr->hdev, data + 3, 0, 24); + btns = hid_field_extract(ctlr->hdev, rep->button_status, 0, 24); if (id != USB_DEVICE_ID_NINTENDO_JOYCONR) { u16 raw_x; @@ -469,8 +629,10 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, u8 *data) int y; /* get raw stick values */ - raw_x = hid_field_extract(ctlr->hdev, (data + 6), 0, 12); - raw_y = hid_field_extract(ctlr->hdev, (data + 7), 4, 12); + raw_x = hid_field_extract(ctlr->hdev, + rep->left_stick, 0, 12); + raw_y = hid_field_extract(ctlr->hdev, + rep->left_stick + 1, 4, 12); /* map the stick values */ x = joycon_map_stick_val(&ctlr->left_stick_cal_x, raw_x); y = -joycon_map_stick_val(&ctlr->left_stick_cal_y, raw_y); @@ -499,8 +661,10 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, u8 *data) int y; /* get raw stick values */ - raw_x = hid_field_extract(ctlr->hdev, (data + 9), 0, 12); - raw_y = hid_field_extract(ctlr->hdev, (data + 10), 4, 12); + raw_x = hid_field_extract(ctlr->hdev, + rep->right_stick, 0, 12); + raw_y = hid_field_extract(ctlr->hdev, + rep->right_stick + 1, 4, 12); /* map stick values */ x = joycon_map_stick_val(&ctlr->right_stick_cal_x, raw_x); y = -joycon_map_stick_val(&ctlr->right_stick_cal_y, raw_y); @@ -526,6 +690,133 @@ static void joycon_parse_report(struct joycon_ctlr *ctlr, u8 *data) input_sync(dev); } +static void joycon_rumble_worker(struct work_struct *work) +{ + struct joycon_ctlr *ctlr = container_of(work, struct joycon_ctlr, + rumble_worker); + struct joycon_subcmd_request req = { 0 }; + unsigned long flags; + int ret; + + spin_lock_irqsave(&ctlr->lock, flags); + memcpy(req.rumble_data, ctlr->rumble_data, sizeof(req.rumble_data)); + spin_unlock_irqrestore(&ctlr->lock, flags); + + mutex_lock(&ctlr->output_mutex); + req.output_id = JC_OUTPUT_RUMBLE_ONLY; + req.packet_num = ctlr->subcmd_num; + if (++ctlr->subcmd_num > 0xF) + ctlr->subcmd_num = 0x0; + + ret = __joycon_hid_send(ctlr->hdev, (u8 *)&req, sizeof(req) - 1); + mutex_unlock(&ctlr->output_mutex); + if (ret < 0) + hid_warn(ctlr->hdev, "Failed to set rumble data; ret=%d", ret); +} + +static struct joycon_rumble_freq_data joycon_find_rumble_freq(u16 freq) +{ + const size_t length = ARRAY_SIZE(joycon_rumble_frequencies); + const struct joycon_rumble_freq_data *data = joycon_rumble_frequencies; + int i = 0; + + if (freq > data[0].freq) { + for (i = 1; i < length - 1; i++) { + if (freq > data[i - 1].freq && freq <= data[i].freq) + break; + } + } + + return data[i]; +} + +static struct joycon_rumble_amp_data joycon_find_rumble_amp(u16 amp) +{ + const size_t length = ARRAY_SIZE(joycon_rumble_amplitudes); + const struct joycon_rumble_amp_data *data = joycon_rumble_amplitudes; + int i = 0; + + if (amp > data[0].amp) { + for (i = 1; i < length - 1; i++) { + if (amp > data[i - 1].amp && amp <= data[i].amp) + break; + } + } + + return data[i]; +} + +static void joycon_encode_rumble(u8 *data, u16 freq_low, u16 freq_high, u16 amp) +{ + struct joycon_rumble_freq_data freq_data_low; + struct joycon_rumble_freq_data freq_data_high; + struct joycon_rumble_amp_data amp_data; + + freq_data_low = joycon_find_rumble_freq(freq_low); + freq_data_high = joycon_find_rumble_freq(freq_high); + amp_data = joycon_find_rumble_amp(amp); + + data[0] = (freq_data_high.high >> 8) & 0xFF; + data[1] = (freq_data_high.high & 0xFF) + amp_data.high; + data[2] = freq_data_low.low + ((amp_data.low >> 8) & 0xFF); + data[3] = amp_data.low & 0xFF; +} + +static int joycon_set_rumble(struct joycon_ctlr *ctlr, u16 amp_r, u16 amp_l) +{ + u8 data[8]; + u16 freq_low; + u16 freq_high; + u16 amp; + u16 freq_r_low; + u16 freq_r_high; + u16 freq_l_low; + u16 freq_l_high; + unsigned long flags; + + spin_lock_irqsave(&ctlr->lock, flags); + freq_r_low = ctlr->rumble_rl_freq; + freq_r_high = ctlr->rumble_rh_freq; + freq_l_low = ctlr->rumble_ll_freq; + freq_l_high = ctlr->rumble_lh_freq; + spin_unlock_irqrestore(&ctlr->lock, flags); + + /* right joy-con */ + freq_low = min(freq_r_low, joycon_max_rumble_freq); + freq_high = min(freq_r_high, joycon_max_rumble_freq); + amp = amp_r * (u32)joycon_max_rumble_amp / 65535; + joycon_encode_rumble(data + 4, freq_low, freq_high, amp); + + /* left joy-con */ + freq_low = min(freq_l_low, joycon_max_rumble_freq); + freq_high = min(freq_l_high, joycon_max_rumble_freq); + amp = amp_l * (u32)joycon_max_rumble_amp / 65535; + joycon_encode_rumble(data, freq_low, freq_high, amp); + + spin_lock_irqsave(&ctlr->lock, flags); + memcpy(ctlr->rumble_data, data, sizeof(ctlr->rumble_data)); + spin_unlock_irqrestore(&ctlr->lock, flags); + + /* don't wait for the periodic send (reduces latency) */ + schedule_work(&ctlr->rumble_worker); + + return 0; +} + +#ifdef CONFIG_JOYCON_FF +static int joycon_play_effect(struct input_dev *dev, void *data, + struct ff_effect *effect) +{ + struct joycon_ctlr *ctlr = input_get_drvdata(dev); + + if (effect->type != FF_RUMBLE) + return 0; + + return joycon_set_rumble(ctlr, + effect->u.rumble.weak_magnitude, + effect->u.rumble.strong_magnitude); +} +#endif static const unsigned int joycon_button_inputs[] = { BTN_SELECT, BTN_Z, BTN_THUMBL, @@ -593,6 +884,17 @@ static int joycon_input_create(struct joycon_ctlr *ctlr) input_set_capability(ctlr->input, EV_KEY, joycon_button_inputs[i]); +#ifdef CONFIG_JOYCON_FF + /* set up rumble */ + input_set_capability(ctlr->input, EV_FF, FF_RUMBLE); + input_ff_create_memless(ctlr->input, NULL, joycon_play_effect); + ctlr->rumble_ll_freq = JC_RUMBLE_DFLT_LOW_FREQ; + ctlr->rumble_lh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; + ctlr->rumble_rl_freq = JC_RUMBLE_DFLT_LOW_FREQ; + ctlr->rumble_rh_freq = JC_RUMBLE_DFLT_HIGH_FREQ; + joycon_set_rumble(ctlr, 0, 0); +#endif + ret = input_register_device(ctlr->input); if (ret) goto err_input; @@ -959,6 +1261,7 @@ static int joycon_hid_probe(struct hid_device *hdev, mutex_init(&ctlr->output_mutex); init_waitqueue_head(&ctlr->wait); spin_lock_init(&ctlr->lock); + INIT_WORK(&ctlr->rumble_worker, joycon_rumble_worker); ret = hid_parse(hdev); if (ret) { @@ -1012,6 +1315,13 @@ static int joycon_hid_probe(struct hid_device *hdev, goto err_mutex; } + /* Enable rumble */ + ret = joycon_enable_rumble(ctlr, true); + if (ret) { + hid_err(hdev, "Failed to enable rumble; ret=%d\n", ret); + goto err_mutex; + } + mutex_unlock(&ctlr->output_mutex); ret = joycon_input_create(ctlr); From patchwork Mon Mar 11 02:03:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Ogorchock X-Patchwork-Id: 10846571 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2B6521515 for ; Mon, 11 Mar 2019 02:04:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 17A8C28D49 for ; Mon, 11 Mar 2019 02:04:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0BC9828D52; Mon, 11 Mar 2019 02:04:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6672328D49 for ; Mon, 11 Mar 2019 02:04:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727151AbfCKCER (ORCPT ); Sun, 10 Mar 2019 22:04:17 -0400 Received: from mail-yw1-f68.google.com ([209.85.161.68]:41537 "EHLO mail-yw1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727156AbfCKCER (ORCPT ); Sun, 10 Mar 2019 22:04:17 -0400 Received: by mail-yw1-f68.google.com with SMTP id q128so2620248ywg.8 for ; Sun, 10 Mar 2019 19:04:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=sf/fnyTSc33lvp768LHLlF9PFjGVDRaQt2XB67nnkS8=; b=Cxf7IlgEQbM7EBYIrXrid1c8qbQjbbXLGnjvVI1qKi0kcLrL1qDh3wtRmMYq82Y2KE mYfDLfOAaJADrua4imkXV7sYd5GqPPFzDHUmOoeR5rHgrzK0P/50+pPTWRhMiIsR2lTU 4sL7JfmzWzFGZqkCAcLPPlRZDCI3te4rs7Vb0BvLtzpjkaeKG2qza0j1Ptl7ykgFWs6q XgiVbjCDlXUKbYOS9bakW+omAbwa8dn6hbYn6llGlZwrXwSoVGRRH9UlCg952+LiOpMt oSnCvGnJBhXbQwiXNmxVeBIpqLXOIk82rLdxEBEOAhcPuvuZmvuk1JozlLfuwEsN+Riu /9hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=sf/fnyTSc33lvp768LHLlF9PFjGVDRaQt2XB67nnkS8=; b=qTSv9zr5CLoDbSp8LxO5paCz3vwJT+8dfuQYB4LT/3DqUV8CGZiRj0bVzsJnfd96q4 EwydWn2lCV3JuNTcbCX0b90Fby8h3eYjpSJo54sD+MD3P86zGW4M1Q2xvZENeIZbEiIV O/iUr0Ydhzq4mhZKcR652IO2ndssnZ50wUBdWyd+plYb22vKqtVwAP/1orAJ8H3hT2cf HUoF86y9SsM0PgCZegt59GocKO0ex+I9kQEknmcSHWjqb3y2IUcZV5zyYmze31UlHpRU 2yrloY3UqR+Ik+dGjZd1NBer+MHzCOBmWVkPbagF+CeN6n98ntsPnAUAdNS+M7lxUHSo CbZA== X-Gm-Message-State: APjAAAVl6pkJRD9YdGs8ArgIqhgRQvFaEP4nheob5Ql+/XQjCBZlwNM7 ui9hJ4nBy0mtdhPRC876kDeZ6Gr3WSA= X-Google-Smtp-Source: APXvYqxTPjVr045jHTTM6YAYR95IkBWhiR9aPtx7XJiHYZ8Tg/vQVZE1vaCh3CXi/WlUJf/0hajppQ== X-Received: by 2002:a25:23d8:: with SMTP id j207mr25101127ybj.0.1552269856201; Sun, 10 Mar 2019 19:04:16 -0700 (PDT) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id r130sm1907088ywr.4.2019.03.10.19.04.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Mar 2019 19:04:15 -0700 (PDT) From: "Daniel J. Ogorchock" To: linux-input@vger.kernel.org Cc: thunderbird2k@gmail.com, blaws05@gmail.com, benjamin.tissoires@redhat.com, jikos@kernel.org, Roderick.Colenbrander@sony.com, peter.hutterer@who-t.net, jbrandst@2ds.eu, hadess@hadess.net, "Daniel J. Ogorchock" Subject: [PATCH v4 6/6] HID: joycon: add rumble frequency sysfs attributes Date: Sun, 10 Mar 2019 21:03:25 -0500 Message-Id: <20190311020324.23619-7-djogorchock@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190311020324.23619-1-djogorchock@gmail.com> References: <20190311020324.23619-1-djogorchock@gmail.com> MIME-Version: 1.0 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds four sysfs attributes (one for each configurable rumble frequency). This allows userspace software to play different tones on the joy-cons by varying the frequency. Signed-off-by: Daniel J. Ogorchock --- drivers/hid/hid-joycon.c | 161 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 151 insertions(+), 10 deletions(-) diff --git a/drivers/hid/hid-joycon.c b/drivers/hid/hid-joycon.c index 10460bac3307..a8ea8b09265d 100644 --- a/drivers/hid/hid-joycon.c +++ b/drivers/hid/hid-joycon.c @@ -180,8 +180,6 @@ static const struct joycon_rumble_freq_data joycon_rumble_frequencies[] = { { 0xe801, 0x00, 1124 }, { 0xec01, 0x00, 1149 }, { 0xf001, 0x00, 1174 }, { 0xf401, 0x00, 1199 }, { 0xf801, 0x00, 1226 }, { 0xfc01, 0x00, 1253 } }; -static const u16 joycon_max_rumble_freq = -joycon_rumble_frequencies[ARRAY_SIZE(joycon_rumble_frequencies) - 1].freq; static const struct joycon_rumble_amp_data joycon_rumble_amplitudes[] = { /* high, low, amp */ @@ -353,6 +351,8 @@ struct joycon_ctlr { u16 rumble_lh_freq; u16 rumble_rl_freq; u16 rumble_rh_freq; + u16 rumble_l_amp; + u16 rumble_r_amp; }; static int __joycon_hid_send(struct hid_device *hdev, u8 *data, size_t len) @@ -762,11 +762,34 @@ static void joycon_encode_rumble(u8 *data, u16 freq_low, u16 freq_high, u16 amp) data[3] = amp_data.low & 0xFF; } +#define JOYCON_MAX_RUMBLE_HIGH_FREQ ((u16) 1253) +#define JOYCON_MIN_RUMBLE_HIGH_FREQ ((u16) 82) +#define JOYCON_MAX_RUMBLE_LOW_FREQ ((u16) 626) +#define JOYCON_MIN_RUMBLE_LOW_FREQ ((u16) 41) + +static void joycon_clamp_rumble_freqs(struct joycon_ctlr *ctlr) +{ + unsigned long flags; + + spin_lock_irqsave(&ctlr->lock, flags); + ctlr->rumble_ll_freq = clamp(ctlr->rumble_ll_freq, + JOYCON_MIN_RUMBLE_LOW_FREQ, + JOYCON_MAX_RUMBLE_LOW_FREQ); + ctlr->rumble_lh_freq = clamp(ctlr->rumble_lh_freq, + JOYCON_MIN_RUMBLE_HIGH_FREQ, + JOYCON_MAX_RUMBLE_HIGH_FREQ); + ctlr->rumble_rl_freq = clamp(ctlr->rumble_rl_freq, + JOYCON_MIN_RUMBLE_LOW_FREQ, + JOYCON_MAX_RUMBLE_LOW_FREQ); + ctlr->rumble_rh_freq = clamp(ctlr->rumble_rh_freq, + JOYCON_MIN_RUMBLE_HIGH_FREQ, + JOYCON_MAX_RUMBLE_HIGH_FREQ); + spin_unlock_irqrestore(&ctlr->lock, flags); +} + static int joycon_set_rumble(struct joycon_ctlr *ctlr, u16 amp_r, u16 amp_l) { u8 data[8]; - u16 freq_low; - u16 freq_high; u16 amp; u16 freq_r_low; u16 freq_r_high; @@ -782,16 +805,14 @@ static int joycon_set_rumble(struct joycon_ctlr *ctlr, u16 amp_r, u16 amp_l) spin_unlock_irqrestore(&ctlr->lock, flags); /* right joy-con */ - freq_low = min(freq_r_low, joycon_max_rumble_freq); - freq_high = min(freq_r_high, joycon_max_rumble_freq); amp = amp_r * (u32)joycon_max_rumble_amp / 65535; - joycon_encode_rumble(data + 4, freq_low, freq_high, amp); + ctlr->rumble_r_amp = amp; + joycon_encode_rumble(data + 4, freq_r_low, freq_r_high, amp); /* left joy-con */ - freq_low = min(freq_l_low, joycon_max_rumble_freq); - freq_high = min(freq_l_high, joycon_max_rumble_freq); amp = amp_l * (u32)joycon_max_rumble_amp / 65535; - joycon_encode_rumble(data, freq_low, freq_high, amp); + ctlr->rumble_l_amp = amp; + joycon_encode_rumble(data, freq_l_low, freq_l_high, amp); spin_lock_irqsave(&ctlr->lock, flags); memcpy(ctlr->rumble_data, data, sizeof(ctlr->rumble_data)); @@ -1241,6 +1262,111 @@ static void joycon_ctlr_destroy(struct joycon_ctlr *ctlr) mutex_destroy(&ctlr->output_mutex); } +static ssize_t joycon_show_freq(struct device *dev, + struct device_attribute *attr, char *buf); + +static ssize_t joycon_store_freq(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count); + +static DEVICE_ATTR(left_high_freq, 0644, + joycon_show_freq, joycon_store_freq); +static DEVICE_ATTR(left_low_freq, 0644, + joycon_show_freq, joycon_store_freq); +static DEVICE_ATTR(right_high_freq, 0644, + joycon_show_freq, joycon_store_freq); +static DEVICE_ATTR(right_low_freq, 0644, + joycon_show_freq, joycon_store_freq); + +static ssize_t joycon_show_freq(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct hid_device *hdev = to_hid_device(dev); + struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); + const char *name = attr->attr.name; + u16 val; + + if (!strcmp(name, dev_attr_left_high_freq.attr.name)) + val = ctlr->rumble_lh_freq; + else if (!strcmp(name, dev_attr_left_low_freq.attr.name)) + val = ctlr->rumble_ll_freq; + else if (!strcmp(name, dev_attr_right_high_freq.attr.name)) + val = ctlr->rumble_rh_freq; + else + val = ctlr->rumble_rl_freq; + + return scnprintf(buf, PAGE_SIZE, "%hu\n", val); +} + +static ssize_t joycon_store_freq(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + struct hid_device *hdev = to_hid_device(dev); + struct joycon_ctlr *ctlr = hid_get_drvdata(hdev); + const char *name = attr->attr.name; + int ret; + u16 *val; + + if (!strcmp(name, dev_attr_left_high_freq.attr.name)) + val = &ctlr->rumble_lh_freq; + else if (!strcmp(name, dev_attr_left_low_freq.attr.name)) + val = &ctlr->rumble_ll_freq; + else if (!strcmp(name, dev_attr_right_high_freq.attr.name)) + val = &ctlr->rumble_rh_freq; + else + val = &ctlr->rumble_rl_freq; + + ret = kstrtou16(buf, 10, val); + if (ret >= 0) + ret = count; + + joycon_clamp_rumble_freqs(ctlr); + joycon_set_rumble(ctlr, ctlr->rumble_r_amp, ctlr->rumble_l_amp); + + return ret; +} + +static int joycon_create_sysfs_attribs(struct joycon_ctlr *ctlr) +{ + struct device *dev = &ctlr->hdev->dev; + int ret = 0; + + if (ctlr->hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONL) { + ret = device_create_file(dev, &dev_attr_right_high_freq); + if (!ret) { + ret = device_create_file(dev, &dev_attr_right_low_freq); + if (ret) + device_remove_file(dev, + &dev_attr_right_high_freq); + } + } + if (ctlr->hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONR && !ret) { + ret = device_create_file(dev, &dev_attr_left_high_freq); + if (!ret) { + ret = device_create_file(dev, &dev_attr_left_low_freq); + if (ret) + device_remove_file(dev, + &dev_attr_left_high_freq); + } + } + return ret; +} + +static void joycon_remove_sysfs_attribs(struct joycon_ctlr *ctlr) +{ + struct device *dev = &ctlr->hdev->dev; + + if (ctlr->hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONL) { + device_remove_file(dev, &dev_attr_right_high_freq); + device_remove_file(dev, &dev_attr_right_low_freq); + } + if (ctlr->hdev->product != USB_DEVICE_ID_NINTENDO_JOYCONR) { + device_remove_file(dev, &dev_attr_left_high_freq); + device_remove_file(dev, &dev_attr_left_low_freq); + } +} + static int joycon_hid_probe(struct hid_device *hdev, const struct hid_device_id *id) { @@ -1330,6 +1456,16 @@ static int joycon_hid_probe(struct hid_device *hdev, goto err_close; } + /* Create the sysfs entries for configuring rumble frequencies */ + if (IS_ENABLED(CONFIG_JOYCON_FF)) { + ret = joycon_create_sysfs_attribs(ctlr); + if (ret) { + hid_err(hdev, "Failed to create sysfs attrs; ret=%d\n", + ret); + goto err_close; + } + } + /* Initialize the leds */ ret = joycon_leds_create(ctlr); if (ret) { @@ -1368,6 +1504,11 @@ static void joycon_hid_remove(struct hid_device *hdev) hid_dbg(hdev, "remove\n"); hid_hw_close(hdev); hid_hw_stop(hdev); + + /* Remove the sysfs entries if rumble is enabled */ + if (IS_ENABLED(CONFIG_JOYCON_FF)) + joycon_remove_sysfs_attribs(ctlr); + joycon_ctlr_destroy(ctlr); }