From patchwork Mon Jan 28 04:04: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: 10783211 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 7593F14E1 for ; Mon, 28 Jan 2019 04:55:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5AAD92A46F for ; Mon, 28 Jan 2019 04:55:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4DE4C2A509; Mon, 28 Jan 2019 04:55:36 +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 B73242A46F for ; Mon, 28 Jan 2019 04:55:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726630AbfA1Ezf (ORCPT ); Sun, 27 Jan 2019 23:55:35 -0500 Received: from mail-yb1-f196.google.com ([209.85.219.196]:41652 "EHLO mail-yb1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726626AbfA1Ezf (ORCPT ); Sun, 27 Jan 2019 23:55:35 -0500 Received: by mail-yb1-f196.google.com with SMTP id n6so794690ybg.8 for ; Sun, 27 Jan 2019 20:55:34 -0800 (PST) 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=Em0gTZZs8XirO6WOSf3SBJaAqxhWwuUB5qNl+5PQVyc=; b=J+YZAbaoLMdkWBbaOnLaPYCckmrud2hmm/UlxhgnAT9dBh5oRKahbTLB6JQGjRpHTa rrSldMD6Rf9SzJifyxzsCkW6NFNYddzUZ97lNJLBPg4JKb0KK+oplKlYYnBBLL9PuqYM seSiQGx8I0kCx7UV9kEskYOOS2lKNecVOdk8dFYpR15DuzyY4oGgKhA/0hYGsD727V6a okm5lgeYMFQJrQzdA/1ees28OSh3/JSY9blIN4OKIdHoykAI+KEfqU17wvy8OEfXktR8 RetvHhECkC0yk14ucmgT2jNVqkPRv78KzXKFt81jFokL5rUVBiRhTpo0aXuHLgAxeW7C BOUA== 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=Em0gTZZs8XirO6WOSf3SBJaAqxhWwuUB5qNl+5PQVyc=; b=InZ8Vjb3Cb46mxHl5ii10SKocyMauu8XDQ7k3JHWup0SA3CMkvFNDxraC8NxR8xFOm gZo++iQan9/k9rhMjnuaX+CaMZJInD/ocN8Q2pENQoY+R5eur99ODuYVJny0TxA683rj XNOM1MMLn8JhF9o9jY9EfLKTh+z9mubtNWbkmk0TAdPnnUrH/MfNeMUbJla2ryzkH3Te v5/10haIWsJ1bwzgZIQjmlbqcidLXnnQs1UB07TYtWyZTByU43QVECWfRziffLErkyUQ Y6bTZ72FpNYnrpZFBfwCek0YTE76F/GARNxkNO5gfw6frKlOfbFX9OKYaBARPvjWj3Xx Gt/Q== X-Gm-Message-State: AJcUukfouB5C3F+lEHW8q98JN7eM2X19CHlWXVjNPRkm2UicxI7RhAKm Hrz65rFPgP/JvlVxLmfXqoLZM98PzRo= X-Google-Smtp-Source: ALg8bN5IhK2RQZ1pZKjFjZclhh8UEfOnFit6TIzMwu7n+PH44G+36PumGaXDhcb9zH9jG7gfhTtm8A== X-Received: by 2002:a25:8385:: with SMTP id t5mr18366916ybk.199.1548648304497; Sun, 27 Jan 2019 20:05:04 -0800 (PST) Received: from localhost.localdomain ([136.61.179.39]) by smtp.gmail.com with ESMTPSA id x9sm12405569ywc.8.2019.01.27.20.05.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 27 Jan 2019 20:05:04 -0800 (PST) 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, "Daniel J. Ogorchock" Subject: [PATCH v3 2/3] HID: switchcon: add player led support Date: Sun, 27 Jan 2019 22:04:20 -0600 Message-Id: <20190128040421.31878-3-djogorchock@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190128040421.31878-1-djogorchock@gmail.com> References: <20190128040421.31878-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/hid-switchcon.c | 111 +++++++++++++++++++++++++++++++++--- 1 file changed, 104 insertions(+), 7 deletions(-) diff --git a/drivers/hid/hid-switchcon.c b/drivers/hid/hid-switchcon.c index f70b7cf95021..7dee7b6c0dc2 100644 --- a/drivers/hid/hid-switchcon.c +++ b/drivers/hid/hid-switchcon.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -198,11 +199,13 @@ struct switchcon_input_report { } __packed; #define SC_MAX_RESP_SIZE (sizeof(struct switchcon_input_report) + 35) +#define SC_NUM_LEDS 4 /* Each physical controller is associated with a switchcon_ctlr struct */ struct switchcon_ctlr { struct hid_device *hdev; struct input_dev *input; + struct led_classdev leds[SC_NUM_LEDS]; enum switchcon_ctlr_type type; enum switchcon_ctlr_state ctlr_state; @@ -447,11 +450,9 @@ static const unsigned int switchcon_button_inputs[] = { 0 /* 0 signals end of array */ }; -static DEFINE_MUTEX(switchcon_input_num_mutex); static int switchcon_input_create(struct switchcon_ctlr *ctlr) { struct hid_device *hdev; - static int input_num = 1; int ret; int i; @@ -492,6 +493,70 @@ static int switchcon_input_create(struct switchcon_ctlr *ctlr) if (ret) goto err_input; + return 0; + +err_input: + input_free_device(ctlr->input); +err: + return ret; +} + +static int switchcon_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 switchcon_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 < SC_NUM_LEDS; num++) { + if (&ctlr->leds[num] == led) + break; + } + if (num >= SC_NUM_LEDS) + return -EINVAL; + + mutex_lock(&ctlr->output_mutex); + for (i = 0; i < SC_NUM_LEDS; i++) { + if (i == num) + val |= brightness << i; + else + val |= ctlr->leds[i].brightness << i; + } + ret = switchcon_set_player_leds(ctlr, 0, val); + mutex_unlock(&ctlr->output_mutex); + + return ret; +} + +static const char * const switchcon_player_led_names[] = { + "player1", + "player2", + "player3", + "player4" +}; + +static DEFINE_MUTEX(switchcon_input_num_mutex); +static int switchcon_player_leds_create(struct switchcon_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(&switchcon_input_num_mutex); mutex_lock(&ctlr->output_mutex); @@ -499,15 +564,40 @@ static int switchcon_input_create(struct switchcon_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 < SC_NUM_LEDS; i++) { + name_len = strlen(switchcon_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), + switchcon_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 = + switchcon_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(&switchcon_input_num_mutex); - return 0; - -err_input: - input_free_device(ctlr->input); -err: return ret; } @@ -773,6 +863,13 @@ static int switchcon_hid_probe(struct hid_device *hdev, goto err_close; } + /* Initialize the leds */ + ret = switchcon_player_leds_create(ctlr); + if (ret) { + hid_err(hdev, "Failed to create leds; ret=%d\n", ret); + goto err_close; + } + ctlr->ctlr_state = SWITCHCON_CTLR_STATE_READ; hid_dbg(hdev, "probe - success\n");