From patchwork Fri Feb 10 04:48:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Orlando Chamberlain X-Patchwork-Id: 55223 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp756343wrn; Thu, 9 Feb 2023 20:54:25 -0800 (PST) X-Google-Smtp-Source: AK7set+VWqiaDea0aiem0NgPesEnQabD2fczjGEmRyKkeybzdU9lXnUVA7R5hhNTECLqmphq9g/S X-Received: by 2002:a17:902:e383:b0:19a:703d:c1c6 with SMTP id g3-20020a170902e38300b0019a703dc1c6mr1144660ple.26.1676004865645; Thu, 09 Feb 2023 20:54:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676004865; cv=none; d=google.com; s=arc-20160816; b=EOBJfp98z9It11J9ouWnDPTGGEkOq9HYFgpZSXYLPis65gSphwMEYI/Fhn6T2erVlm IAVzpUuuWlZZxnqmapT7bZymOcUeS+fI1idl7ZkzvrO7TvutLPBbEw4iDN4K04c+E+Og bL0tlTWfNUg4njNYCWhNMWO3w5oH5IFfTUWf6t+UsVZYD5vbSMBX9h80vgzBzXsaAybj lOie46rBC9HWYIzVx1/oo7Haz2Umn8lptMxKj5UqJU+0+tYhhlu3iFiKVctAQd8qhU+b nZtP6A+TEii5AemouDyGz5tVuZpOel4JB7RUtzJJ9oCzVSpSMsjVeowx9+VLdDvcCcz7 PSVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=tEls10WodkspsIvZIvxa4LCxo5R1yCy5xPl31tNRCfY=; b=NpWgGf25QCoPyHJgx9j7a+5oTexDctLBQqrUpiYe5y+DqpjqJjcdC2r5r7reKjyOd2 PceGjlhlsBAYmQc06KFkQKawBoUaZOPOoC7reLqSywNfahWQJv1Nrn1QxQdzok2jny3e YebQclnvFcKfhzlqfe7hM16K/9DF7j2c0jWA5MwWl3GpHCzJodgEJqg85sckGKWuNjOX EdOlR3jrnpDxv+D+2S6oPB2pm3DzMh0UZVnJx7Dbve4RWlzrEU98PFhz/xdFqz1oEt0O 9J8IQ7rBq0kLSeG/TTpYOOXe1X3rw5os9lwjy0uNYsFKwfG8lcr+ix9ecN3d6hBGKpM/ Bx4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AI5OkR5n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y11-20020a170902cacb00b0019a5f479dbfsi2759912pld.567.2023.02.09.20.54.13; Thu, 09 Feb 2023 20:54:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=AI5OkR5n; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231225AbjBJEv4 (ORCPT + 99 others); Thu, 9 Feb 2023 23:51:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231322AbjBJEve (ORCPT ); Thu, 9 Feb 2023 23:51:34 -0500 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CF0477164; Thu, 9 Feb 2023 20:51:08 -0800 (PST) Received: by mail-pf1-x433.google.com with SMTP id s20so2770596pfe.2; Thu, 09 Feb 2023 20:51:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tEls10WodkspsIvZIvxa4LCxo5R1yCy5xPl31tNRCfY=; b=AI5OkR5ngOdIYwA82Bd2NHyMJ+O2BLu1AlHUiAUk5u2mES8050ILPnIi34V/HwnVmn UCnnl/NNHpnSFBN3X0R580Y09W5YGDjQXorCqi2JWHAwRM3mAVXKAyPB7q7B66d3DoWt zBvfn3AmAyRezdi5kNYRSaoRxCZe3u4hC11prURWNE0PK6ZNCw5117fr7UHA3ZhhHI6z ua9y3dmza6fdLajFD3iCgQy+ca3vadxm1QgxmG6b53N46lfItM+tP6BzFu/BuB6LbEC1 2TmnClABN6Fxc12v6c0kbWm8LL7Al2NDRYIr/vhwsP9jpjc8vOPXdePVWan8RRrIxgG1 ma/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tEls10WodkspsIvZIvxa4LCxo5R1yCy5xPl31tNRCfY=; b=lmJb34hiHE2MKpge1IHUKV2tS1XFwb8bxosbiT2J896tS7T4k1U1wPr3Bf+6Lr/v13 ajnq0VRLQP4Bkjnt4+ZgptsUptt1Uv748xgPMoPuWTonR+N6ntFNtVGXnkhV2NYzG6tf UPidFvVoer+ubY3u08cInx1573NX4KheBnSlyM6jgJiBm1EGg5je6X7RR3eAxo1A+lAp fSK5ReSVZZQNT6vQCcbY+CUp0bBShk6XhIcTWn2buGbR81l/UQm3oHRRiB4p84og0z4w RowDodSQ9l/6YqRtn6g13hXB89guG2iuJn4sJKwbtbLydC9ZisXXEy3om50TohOAUxfM abzw== X-Gm-Message-State: AO0yUKXbl0X9D01G7bu97r1DTU0rHQ4dyvDn3JUm2ZdS5mnQg7UrsrzJ e9EM2exKXpZPS8baTKcQsp4LYv9XdxQigg== X-Received: by 2002:a62:15d3:0:b0:5a8:4db3:266e with SMTP id 202-20020a6215d3000000b005a84db3266emr3828885pfv.10.1676004667360; Thu, 09 Feb 2023 20:51:07 -0800 (PST) Received: from localhost.localdomain ([202.53.32.211]) by smtp.gmail.com with ESMTPSA id t13-20020a62ea0d000000b0057fec210d33sm2269218pfh.152.2023.02.09.20.50.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 20:51:07 -0800 (PST) From: Orlando Chamberlain To: platform-driver-x86@vger.kernel.org, amd-gfx@lists.freedesktop.org, linux-kernel@vger.kernel.org, alsa-devel@alsa-project.org Cc: Alex Deucher , =?utf-8?q?Christian_K=C3=B6nig?= , "Pan, Xinhui" , David Airlie , Daniel Vetter , Hans de Goede , Mark Gross , Jaroslav Kysela , Takashi Iwai , Hawking Zhang , Andrey Grodzovsky , Lijo Lazar , YiPeng Chai , Somalapuram Amaranath , Mario Limonciello , Bokun Zhang , Jack Xiao , Kai Vehmanen , Pierre-Louis Bossart , Rander Wang , Ranjani Sridharan , =?utf-8?q?Amadeusz_S?= =?utf-8?q?=C5=82awi=C5=84ski?= , Yong Zhi , Evan Quan , Kerem Karabay , Aditya Garg , Aun-Ali Zaidi , Orlando Chamberlain Subject: [RFC PATCH 7/9] apple-gmux: add sysfs interface Date: Fri, 10 Feb 2023 15:48:24 +1100 Message-Id: <20230210044826.9834-8-orlandoch.dev@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230210044826.9834-1-orlandoch.dev@gmail.com> References: <20230210044826.9834-1-orlandoch.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757418477943310994?= X-GMAIL-MSGID: =?utf-8?q?1757418477943310994?= Allow reading gmux ports from userspace. When the unsafe module parameter allow_user_writes is true, writing 1 byte values is also allowed. For example: cd /sys/bus/acpi/devices/APP000B:00/physical_node/ echo 4 > gmux_selected_port cat gmux_selected_port_data | xxd -p Will show the gmux version information (00000005 in this case) Signed-off-by: Orlando Chamberlain --- drivers/platform/x86/apple-gmux.c | 129 ++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/drivers/platform/x86/apple-gmux.c b/drivers/platform/x86/apple-gmux.c index c38d6ef0c15a..756059d48393 100644 --- a/drivers/platform/x86/apple-gmux.c +++ b/drivers/platform/x86/apple-gmux.c @@ -66,6 +66,11 @@ struct apple_gmux_data { enum vga_switcheroo_client_id switch_state_external; enum vga_switcheroo_state power_state; struct completion powerchange_done; + +#ifdef CONFIG_SYSFS + /* sysfs data */ + int selected_port; +#endif /* CONFIG_SYSFS */ }; static struct apple_gmux_data *apple_gmux_data; @@ -651,6 +656,121 @@ static void gmux_notify_handler(acpi_handle device, u32 value, void *context) complete(&gmux_data->powerchange_done); } +/** + * DOC: Sysfs Interface + * + * gmux ports can be read from userspace as a sysfs interface. For example: + * + * # echo 4 > /sys/bus/acpi/devices/APP000B:00/physical_node/gmux_selected_port + * # cat /sys/bus/acpi/devices/APP000B:00/physical_node/gmux_selected_port_data | xxd -p + * 00000005 + * + * Reads 4 bytes from port 4 (GMUX_PORT_VERSION_MAJOR). + * + * Single byte writes are also supported, however this must be enabled with the + * unsafe allow_user_writes module parameter. + * + */ + +#ifdef CONFIG_SYSFS + +static bool allow_user_writes; +module_param_unsafe(allow_user_writes, bool, 0); +MODULE_PARM_DESC(allow_user_writes, "Allow userspace to write to gmux ports (default: false) (bool)"); + +static ssize_t gmux_selected_port_store(struct device *dev, + struct device_attribute *attr, const char *sysfsbuf, size_t count) +{ + struct apple_gmux_data *gmux_data = dev_get_drvdata(dev); + u8 port; + + if (kstrtou8(sysfsbuf, 10, &port) < 0) + return -EINVAL; + + /* On pio gmux's, make sure the user doesn't access too high of a port. */ + if ((gmux_data->config == &apple_gmux_pio) && + port > (gmux_data->iolen - 4)) + return -EINVAL; + + gmux_data->selected_port = port; + return count; +} + +static ssize_t gmux_selected_port_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) +{ + struct apple_gmux_data *gmux_data = dev_get_drvdata(dev); + + return sysfs_emit(sysfsbuf, "%d\n", gmux_data->selected_port); +} + +DEVICE_ATTR_RW(gmux_selected_port); + +static ssize_t gmux_selected_port_data_store(struct device *dev, + struct device_attribute *attr, const char *sysfsbuf, size_t count) +{ + struct apple_gmux_data *gmux_data = dev_get_drvdata(dev); + + if (count == 1) + gmux_write8(gmux_data, gmux_data->selected_port, *sysfsbuf); + else + return -EINVAL; + + return count; +} + +static ssize_t gmux_selected_port_data_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) +{ + struct apple_gmux_data *gmux_data = dev_get_drvdata(dev); + u32 data; + + data = gmux_read32(gmux_data, gmux_data->selected_port); + memcpy(sysfsbuf, &data, sizeof(data)); + + return sizeof(data); +} + +struct device_attribute dev_attr_gmux_selected_port_data_rw = __ATTR_RW(gmux_selected_port_data); +struct device_attribute dev_attr_gmux_selected_port_data_ro = __ATTR_RO(gmux_selected_port_data); + +static int gmux_init_sysfs(struct pnp_dev *pnp) +{ + int ret; + + ret = device_create_file(&pnp->dev, &dev_attr_gmux_selected_port); + if (ret) + return ret; + if (allow_user_writes) + ret = device_create_file(&pnp->dev, &dev_attr_gmux_selected_port_data_rw); + else + ret = device_create_file(&pnp->dev, &dev_attr_gmux_selected_port_data_ro); + if (ret) + device_remove_file(&pnp->dev, &dev_attr_gmux_selected_port); + return ret; +} + +static void gmux_fini_sysfs(struct pnp_dev *pnp) +{ + device_remove_file(&pnp->dev, &dev_attr_gmux_selected_port); + if (allow_user_writes) + device_remove_file(&pnp->dev, &dev_attr_gmux_selected_port_data_rw); + else + device_remove_file(&pnp->dev, &dev_attr_gmux_selected_port_data_ro); +} + +#else + +static int gmux_init_sysfs(struct pnp_dev *pnp) +{ + return 0; +} +static void gmux_fini_sysfs(struct pnp_dev *pnp) +{ +} + +#endif /* CONFIG_SYSFS */ + static int gmux_suspend(struct device *dev) { struct pnp_dev *pnp = to_pnp_dev(dev); @@ -846,8 +966,16 @@ static int gmux_probe(struct pnp_dev *pnp, const struct pnp_device_id *id) goto err_register_handler; } + ret = gmux_init_sysfs(pnp); + if (ret) { + pr_err("Failed to register gmux sysfs entries\n"); + goto err_sysfs; + } + return 0; +err_sysfs: + vga_switcheroo_unregister_handler(); err_register_handler: gmux_disable_interrupts(gmux_data); apple_gmux_data = NULL; @@ -877,6 +1005,7 @@ static void gmux_remove(struct pnp_dev *pnp) { struct apple_gmux_data *gmux_data = pnp_get_drvdata(pnp); + gmux_fini_sysfs(pnp); vga_switcheroo_unregister_handler(); gmux_disable_interrupts(gmux_data); if (gmux_data->gpe >= 0) {