From patchwork Tue Apr 2 14:30:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Armin Wolf X-Patchwork-Id: 13614191 Received: from mout.gmx.net (mout.gmx.net [212.227.15.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9031D17F3; Tue, 2 Apr 2024 14:31:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.18 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068276; cv=none; b=KSuy6UQN3MSfclaLnlqqLAnOQc6vfryMMKQuLgAZzDvQH5rIxdQwBJK/oxhQ65Ui8+J4ey4RNxk1bgtp54faIhP5jZ6Ss4AMABYUajeo0yim3MPwktStIqjNQ0FdRhFaU6CBjo8bPrtnZXnCf+oWmjddWjuPgIho+vsU8XrN4xQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068276; c=relaxed/simple; bh=NYmtOecKlLyzO3nfLMqfP9YJByKvOkIZO/4pDmRw5vg=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=fzrW8M62xp5YytkbjkVarLVPVPQeCRURg9KUAlBYZ7T5fr8FbSOR5iBfqAA/FYHuAKj7PFOU1BvtSEaETpU+vdhphj8vX3AfqVKd5gIK6oIj27xt1QM7DAmNhsfWHRoYMQB7me5MR6g0PT1QcgmuiQd3e6x9MueyeEHLVcNSEUQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de; spf=pass smtp.mailfrom=gmx.de; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b=YP+9/42D; arc=none smtp.client-ip=212.227.15.18 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b="YP+9/42D" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1712068263; x=1712673063; i=w_armin@gmx.de; bh=RwQaqFwNpLwiUaZNu6AXD2WMwZk2ta/0PgKPGIo7pDU=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date; b=YP+9/42DlR6D3HEkpeMMp7fISgGBGI0zbTfhtNIlrjsD8Sp4gslKjbipYxPIRVkX 0LfWSwoHR3SUrHF1kLRFgeaT5vn/vtbS5gqZSxUjon6tv8S/c4C6BVUvgwwRaBIOw XWCUG0XLRY+OoONjs3c494f3obdqW5/QHb2IFejMGNWQMWHpK/yUnhBJasGG2TgHX zPp43C6swgXrO+GHmdflY7RLoiydKToaMWO3sIPBLwLjelxuAA0pq9yoi2Al9mcxW l81aJwHTnPmsaz9/Ai2mWPfgYPXs3Aml0yuArDuP4m9vBKH23M9GeQI7K+H+0VYAY 9Yds5U+WJppcTGz4LQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from mx-amd-b650.users.agdsn.de ([141.30.226.129]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N0G1d-1smlf60Chz-00xHS1; Tue, 02 Apr 2024 16:31:03 +0200 From: Armin Wolf To: hdegoede@redhat.com, ilpo.jarvinen@linux.intel.com Cc: corbet@lwn.net, sathyanarayanan.kuppuswamy@linux.intel.com, linux-doc@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/4] platform/x86: wmi: Mark simple WMI drivers as legacy-free Date: Tue, 2 Apr 2024 16:30:56 +0200 Message-Id: <20240402143059.8456-1-W_Armin@gmx.de> X-Mailer: git-send-email 2.39.2 Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Provags-ID: V03:K1:dtSjc7LZamFk+Gy/7gLU9y0Uon9xGkle8LMwgaUZ6ChQ1P1wyFV 7tSOcK7dc5LNU100bDBSPR0apxI6S7SR64t0AtwnNIdkZU7h3/38hOG9qSN1tK98//2dZPm lwhrw2FQ0Z4lYnUvqFPIiSlvMrVWqpdF9FDaXXXilWqB2XmtuY3C0bPNP4xL3YixcSsdzif TewC2uk8TFlXrW2C5KaSw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:RMvd0Pwq79Y=;YURxJ9+LhdA4F4u7rOfTe6Eajek 0jh1KOSPVrrQVr8aOOOapjQoMc005rzLyFf3Wq5eawD0zjmyWB7KLpj3Xmcpw/zvvtKgC7vYv hmACSoqoH8nn97CI/rQFn58UQqFR6bJH/W5WUnL7g999kxG/4LldQTF/YfKbccqH0lxuBSk1O SIHj/0EnzV/H7RkPW0Pm9Hm5Ssf1heCAtbCx3LWhvqFhq0Duxfn5vAyzBL/w69DwdgFYuELrd ELhFfVIoMhjG4145uFcWbrI/klADQTaULudrlBzZbCYcAfBK5hLozAgvbVd6tGKy/DSlwEFjd XrqUBxlTMTHzW0lCL3yUdCewsydDKBsDJ4PcINDvE0NuK6ofbE51QCwQSk/Z9dNDV6qdFA8jy G4enrSIODs7yil3O8gu5npv7wVBIo5F753yM9889VRmt4CKsdwT99/gTWokxB4GEXIB42njoF PD7ubnkFbFHIBF3w/kjFVkNQPvnqmXfmcAachLP0lAtMO0JIDYaqGZj+WKB9GROyyKvfBC6Hr RpdDsKNYmqwGyUU8lO2Uh8e71P9sF4ZsQ+L8KYPtsqXtVYkFx3qTxRFVA9+QUpF6Y6dgpEgaC BLQRK5JL+HrBFJTF/5dMiy6Y2JFAR7SQspul+nySzDlvINTP8z8Fs4QmN80MfKjDIHrtS5pHi vSsFSODE2t0aBUWoaPkZdb52bTNfYz1+RYGfkIHWBz2EtyD6NOL8Ty7WyHL3xaJl5r0hWXbOT 6Hy9atdqmv4y9TuQG2r84b3lcZ/y9xfAlFA1JDaOEz+jtzGXUwJXh6WmhQsODxvm3fBNSP93x N/Ek1AAOLHUpBhjxMTMJsliXk41o15opng8Dc1247tFC0= The inspur_platform_profile driver and the xiaomi-wmi driver both meet the requirements for modern WMI drivers, as they both do not use the legacy GUID-based interface and can be safely instantiated multiple times. Mark them both as legacy-free using the no_singleton flag. Compile-tested only. Reviewed-by: Kuppuswamy Sathyanarayanan Signed-off-by: Armin Wolf --- Changes since v1: - add Reviewed-by tag --- drivers/platform/x86/inspur_platform_profile.c | 1 + drivers/platform/x86/xiaomi-wmi.c | 1 + 2 files changed, 2 insertions(+) -- 2.39.2 diff --git a/drivers/platform/x86/inspur_platform_profile.c b/drivers/platform/x86/inspur_platform_profile.c index 743705bddda3..8440defa6788 100644 --- a/drivers/platform/x86/inspur_platform_profile.c +++ b/drivers/platform/x86/inspur_platform_profile.c @@ -207,6 +207,7 @@ static struct wmi_driver inspur_wmi_driver = { .id_table = inspur_wmi_id_table, .probe = inspur_wmi_probe, .remove = inspur_wmi_remove, + .no_singleton = true, }; module_wmi_driver(inspur_wmi_driver); diff --git a/drivers/platform/x86/xiaomi-wmi.c b/drivers/platform/x86/xiaomi-wmi.c index 54a2546bb93b..1f5f108d87c0 100644 --- a/drivers/platform/x86/xiaomi-wmi.c +++ b/drivers/platform/x86/xiaomi-wmi.c @@ -83,6 +83,7 @@ static struct wmi_driver xiaomi_wmi_driver = { .id_table = xiaomi_wmi_id_table, .probe = xiaomi_wmi_probe, .notify = xiaomi_wmi_notify, + .no_singleton = true, }; module_wmi_driver(xiaomi_wmi_driver); From patchwork Tue Apr 2 14:30:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Armin Wolf X-Patchwork-Id: 13614192 Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78CC317BCB; Tue, 2 Apr 2024 14:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.21 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068279; cv=none; b=qgFK8cGcD1bvzhifDT8bH5OzCNw191dDPJcoo7mkiW2ldNN58Ao298HSpHZtu3bKrRYs9Ld5gxOuW0TZbaVfCrzyV9ZxXeUpKgsLoZPLuF1V/ciyg16cZ9spyWpiDdydI25xwLtwHnTJP8n/ASEJbrQ1EGsvLwfqY0pNGbFhRAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068279; c=relaxed/simple; bh=GSGeeyds/g8pICmZBmqRu+evqxKRetteZ+3dXQU2ru0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iqf9roll+jSRegPsF5o1mADCX4VAlH2RG5/BeqVTd6MVYZV4swAQ4GZ/S5k850DgS08dIyIIxpstAewH94JV9kPfMtv/WaJIpQ72R/yes34z446fV2KpooQLswMUdJnnhzQug+yRGPCyU31RxXRXzsMnPVhrpXQJS37DInQSmhM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de; spf=pass smtp.mailfrom=gmx.de; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b=MnEd0QNR; arc=none smtp.client-ip=212.227.17.21 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b="MnEd0QNR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1712068266; x=1712673066; i=w_armin@gmx.de; bh=aIeeKP7p962Be1K+3JAM/oYmuMrr0yig8x+lgmqinJs=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To: References; b=MnEd0QNRZ/jC43RNAdpp81FXUKA9tvAPlyOTkFTcyRVtmSB/HHGowpKtCANGG49T +S0JAndgpP1vaUcUPse56hDi3fUqn+Fk8OGEWV3kcGDjTcP3De/ROeoXuNjrXSITk q0hvdyO+u13vJRHzx/Y1d18t+q/GBx/qJG4LBKHEa/wIm70x3CZufHlODiu4gHiYu TTug8r70SmHhdKv2SIPVCz51jw7bxKjvBzECBFOAxRDQTi01CCFppfZq+Mz/p3GXw PU7SpmqxNW+iCfMc+sI0L2OYDrSBMeP2xDe+rWGh1mVw/eskNO4pkRJHXQbkm18AK r48sqK43ofEL/hIpWA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from mx-amd-b650.users.agdsn.de ([141.30.226.129]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N1wpt-1spaGh0bt3-012Gxo; Tue, 02 Apr 2024 16:31:06 +0200 From: Armin Wolf To: hdegoede@redhat.com, ilpo.jarvinen@linux.intel.com Cc: corbet@lwn.net, sathyanarayanan.kuppuswamy@linux.intel.com, linux-doc@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] platform/x86: xiaomi-wmi: Fix race condition when reporting key events Date: Tue, 2 Apr 2024 16:30:57 +0200 Message-Id: <20240402143059.8456-2-W_Armin@gmx.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240402143059.8456-1-W_Armin@gmx.de> References: <20240402143059.8456-1-W_Armin@gmx.de> Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Provags-ID: V03:K1:1MSHdp3U2yQqRtyM8+jE62IQ5HIy4KO3F3v21vPfr3suJPsyNdL oiyilsLxzYlWgFVbK/QrZFghS7d/BOrgbNuDqdaqgWhgQT8EOjVmo/8HgY1tGv0KLvGsclU RJOT5B1n7/VNueNCk4AEvxkB2hILtWJc8Z2IbCfveRjGE9A9ElE4DScDMCk6/gzp6d4R59i doChLL9cM+lOx6+BCuTVQ== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:hpZs6+gCIQk=;8nlTLCcB2sSRRih+kLDgq4cZ4Oq uy8cBbYWawg1CvQ57K+AvqtklCmXMOF67pk56vycDvH66m3Uzyit2xG9KXFcMksd+7BWtgSGX ujfjl5fbCQBNasj4PDQssiu4Qi/U6LdNEeFlAJ9POJLIiOmw2nQ9x7zY2uBgxWcX/MIRWKAug +bbO5degTI6eG1EmGQa83AxTZZ433e3PPfQ1BgzbdsEW14VZ5Q8XyF7k5T27q+WjoBXXKiOF+ 1aEGb9RTLpMf3lgDoklPwX0qjFDzVkeuE1Y1bgi4QZvc5s8AuCxX0IYQI1EiZeGIYyojWNRVv Qz3tBmu6VcWONaclHQfGj8U4Sp3grYzANE3Iw7nukl95mk+HwhOyysTGawtPD4t3mpsmjoj4P JKOfhiGPCbZOATdaG0tdVbUi//jEcDer87bXfEYptsYutQc0UgJ5HDaVseVn734hYHmsh/sIv GbAq4xHrfzKQ7+OHXRdFQ2DheqRkY0WBqgFP+JBxCvSVk9K3Dbe/VX4GDYtXuPBC4JgWvM5Hw sGEM/Dmy7pr+KbnihSIQuXpbiv+Pp4ALZgydNiwyeF61oS7nLloeKIW2zmnaMCmPtswE12f7g mL0PPyw8tj5SYJ8k90zbk945QZIjVMeKrU9li0r4WsOSbvLwdwtjD8o4qke5/sKPQTifVC15V PKZnMl1CQf+ghYXLSiB/gj2vceWjDaXnwY7Vz4cXpvO3312R3DD6OHRZnfiC78U3sHsDU57kD kB+e/0VfcYHmni9atkhO/vKPAkfGiEMt7KvncBWqzQu6Wu0TnIugYlvcIn1e3Mm5BdroEQhVU A7Piz1Y2EaePquVBBN3JRF+em0OLeFkbOvZKGJ6zBMw3M= Multiple WMI events can be received concurrently, so multiple instances of xiaomi_wmi_notify() can be active at the same time. Since the input device is shared between those handlers, the key input sequence can be disturbed. Fix this by protecting the key input sequence with a mutex. Compile-tested only. Fixes: edb73f4f0247 ("platform/x86: wmi: add Xiaomi WMI key driver") Signed-off-by: Armin Wolf Reviewed-by: Kuppuswamy Sathyanarayanan --- drivers/platform/x86/xiaomi-wmi.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) -- 2.39.2 diff --git a/drivers/platform/x86/xiaomi-wmi.c b/drivers/platform/x86/xiaomi-wmi.c index 1f5f108d87c0..7efbdc111803 100644 --- a/drivers/platform/x86/xiaomi-wmi.c +++ b/drivers/platform/x86/xiaomi-wmi.c @@ -2,8 +2,10 @@ /* WMI driver for Xiaomi Laptops */ #include +#include #include #include +#include #include #include @@ -20,12 +22,21 @@ struct xiaomi_wmi { struct input_dev *input_dev; + struct mutex key_lock; /* Protects the key event sequence */ unsigned int key_code; }; +static void xiaomi_mutex_destroy(void *data) +{ + struct mutex *lock = data; + + mutex_destroy(lock); +} + static int xiaomi_wmi_probe(struct wmi_device *wdev, const void *context) { struct xiaomi_wmi *data; + int ret; if (wdev == NULL || context == NULL) return -EINVAL; @@ -35,6 +46,11 @@ static int xiaomi_wmi_probe(struct wmi_device *wdev, const void *context) return -ENOMEM; dev_set_drvdata(&wdev->dev, data); + mutex_init(&data->key_lock); + ret = devm_add_action_or_reset(&wdev->dev, xiaomi_mutex_destroy, &data->key_lock); + if (ret < 0) + return ret; + data->input_dev = devm_input_allocate_device(&wdev->dev); if (data->input_dev == NULL) return -ENOMEM; @@ -59,10 +75,12 @@ static void xiaomi_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy) if (data == NULL) return; + mutex_lock(&data->key_lock); input_report_key(data->input_dev, data->key_code, 1); input_sync(data->input_dev); input_report_key(data->input_dev, data->key_code, 0); input_sync(data->input_dev); + mutex_unlock(&data->key_lock); } static const struct wmi_device_id xiaomi_wmi_id_table[] = { From patchwork Tue Apr 2 14:30:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Armin Wolf X-Patchwork-Id: 13614193 Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5982D12BF31; Tue, 2 Apr 2024 14:31:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.17.22 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068283; cv=none; b=qmywwouOaW6SpsSMdxKyOv05TWXSCtkakz7DsHT42uMxuCNkp0xH0bSOwM60ZkpNUVQTJzsHbQf05ywY5ha9zf5mGXwG8O7MiEEbNwPTnERwIV+d/SNQS9eI69qNIsj0nY/u+9wZWR8IygchlXWo4qHQK85oaR/sc3Ob90tXsO0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068283; c=relaxed/simple; bh=jXpXmnj6OnVqKyhubDX5ir2p4FFes+gOgiUiATMk5OA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cvIJeoXRlAZ1WuLVtQ3jHb7/uLCvk9T2xN47bGZVkAcmniHMYbmWm3tzKsG/A5nqmVgcZONqxSdU1mR9G/ez4pCyPnIWclMxbHizt7gLdJolEJnPUdN53yMQYFGCtts/j4iB6GdP521Gifcne8Fba4pDcisFVod0dFBP+slWR8g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de; spf=pass smtp.mailfrom=gmx.de; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b=LDPSogJW; arc=none smtp.client-ip=212.227.17.22 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b="LDPSogJW" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1712068268; x=1712673068; i=w_armin@gmx.de; bh=NTZrA4QvJZSqwHRW+FevDxzgn/3DYV09kgKCOuqHtsM=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To: References; b=LDPSogJWdBc0kHIq3a8b2Bcb2m5+b2hJv/CLA32t1hjt5FfGWiDheha12QfmosBO 2qi/ppHqQJFekDQH89ibFeLLhS0M65pKzvcKC+tjMwT1rX/NvlzqMb1hYBWUqZTQl l/GivPzNNA/cQBBEC7mZcy6tOk0Zku76WCJU0Y3W3pOQEDuZL9lXZRrvmN2JFpFz0 lDaBqQyVDQbdB6AoC8lF60KZ/nsA/Xx676w7Hl5U9OIQd5J9OsGVAHX++Bt610cVS 7Erpg0E4ZEokuiQbz2GHOhBkIflfTX/fiWmWTulITRtEX7mDiX9bvLZ013D4Xt8nZ VoHXZSSHz1TMfTzWFw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from mx-amd-b650.users.agdsn.de ([141.30.226.129]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N5mGB-1suVxH2ZER-017E61; Tue, 02 Apr 2024 16:31:08 +0200 From: Armin Wolf To: hdegoede@redhat.com, ilpo.jarvinen@linux.intel.com Cc: corbet@lwn.net, sathyanarayanan.kuppuswamy@linux.intel.com, linux-doc@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/4] platform/x86: xiaomi-wmi: Drop unnecessary NULL checks Date: Tue, 2 Apr 2024 16:30:58 +0200 Message-Id: <20240402143059.8456-3-W_Armin@gmx.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240402143059.8456-1-W_Armin@gmx.de> References: <20240402143059.8456-1-W_Armin@gmx.de> Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Provags-ID: V03:K1:XV7sEEbNyVZim6yBA9Y2t4HlZsMGd0xWJLofcFO5Ak01iM/6SlM yEYb/SpACJ0AkzZV6j40aBv70sA7mrJz3ViF52a3XcukBjWODn8j0AwhbTesPW48Gs9DCSc MLNxOa1qNl8s5/IDt7/NgJZUqlGEs7ZPSyF6HVpqywmIM2XiYzvcqJbWsYCiq6QAq/8E0f4 EKPoDbxbqTVjzqhr1Ihrw== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:cOspIHrHTEk=;M5tS7UAT8pdEkEZ/fNKPrM8LYx/ LvFCU5LgIxZDnfY61dlgNTz8Ujj4Z95ZTkmljx6H0qMXQnRGu9M1sCSVT6AvZ+wPkW4wOdj8r wwqxrV/fxllsvfTF4IvWaFUCQEl3OqSC179eNOgZhA26Ia5+PabrnSq2fUVeLSDXbTo6v+RWY n9fRVqwtr226D4u2ql+a7NGDf3dhZaQ/d3q0N0Z4PDvNkeEyNQvoTimNyKRqWbJc0EsgjPjxT aiIz9YJ67h4VWD9RV1314B/SrVqabQhxl5wfN3UW5n4T+Lr/3kXRW2C1S0m/WM0fh9042mpuQ SdpQCKFkERPds4n3oUwfyPkYpA0J7JPKM8JbqxQ6wtlDQPGcYawh/exQrWQczAYfLVBtJ3b/t vPJYGKOcWWKq0ULh3F5Nt00adExqxEIeKtTme1d2BT/X5E5ZUFdvavaXjsX9Ur5ZpZ+mxn/n1 vzZL+e7V960g62VCoqKzh5wSrKXpzQPL+OCT7BZAYWTx3LMknQ2zri23FBIEvuDtq8ya8r0uJ rNRjDGXylfnGJEeQDPGPqOcFLFHh2M9R3ybY+cyR9q6K6kOfdgZq8jgoG4UlJSwt3Xd5sec3h 7d44e67are2896P7HuUw4khQjfM80ECi60dCosMhpY2MFubELmMa4MxB1OOU8ZHFRlRSE6Ugx Rwy57yGLZIBwy3bGtjnDX4b/fkY7fGVkAnfhwMT2Gj4Qmqikmnz2APWpHjAidIvkMzY8wOyKm swkw0Yk/RfYVhBGQcJxkc20cZxJ6j2JG32YnFB9vjoe7hW4V2vJ33XtxAVr5lamiym8on9B4e ud41QYvxAdL8zPm9YKIkCJdwpFma2AmWe2Mnz8N+kPTak= The WMI driver core already makes sure that: - a valid WMI device is passed to each callback - the notify() callback runs after the probe() callback succeeds Remove the unnecessary NULL checks. Compile-tested only. Signed-off-by: Armin Wolf Reviewed-by: Kuppuswamy Sathyanarayanan --- drivers/platform/x86/xiaomi-wmi.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) -- 2.39.2 diff --git a/drivers/platform/x86/xiaomi-wmi.c b/drivers/platform/x86/xiaomi-wmi.c index 7efbdc111803..cbed29ca502a 100644 --- a/drivers/platform/x86/xiaomi-wmi.c +++ b/drivers/platform/x86/xiaomi-wmi.c @@ -38,7 +38,7 @@ static int xiaomi_wmi_probe(struct wmi_device *wdev, const void *context) struct xiaomi_wmi *data; int ret; - if (wdev == NULL || context == NULL) + if (!context) return -EINVAL; data = devm_kzalloc(&wdev->dev, sizeof(struct xiaomi_wmi), GFP_KERNEL); @@ -66,14 +66,7 @@ static int xiaomi_wmi_probe(struct wmi_device *wdev, const void *context) static void xiaomi_wmi_notify(struct wmi_device *wdev, union acpi_object *dummy) { - struct xiaomi_wmi *data; - - if (wdev == NULL) - return; - - data = dev_get_drvdata(&wdev->dev); - if (data == NULL) - return; + struct xiaomi_wmi *data = dev_get_drvdata(&wdev->dev); mutex_lock(&data->key_lock); input_report_key(data->input_dev, data->key_code, 1); From patchwork Tue Apr 2 14:30:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Armin Wolf X-Patchwork-Id: 13614194 Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3052312BF2E; Tue, 2 Apr 2024 14:31:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=212.227.15.15 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068283; cv=none; b=Cy2wnDsDtZcSefXtNR1iYIwN7v+QfQtnHLQllV65M16F3N4HXWw3FKsBfyNLOqWgndwoqVkMM9XM/5lirJqubpYU/RoO2cGbYaAQF/Oo0XJVHgTeIpEewef5/+FJmXXQG8Odb4iIxwQGQ9lbU0vUBSn+LqeEcFZIH5PHZMN1PVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712068283; c=relaxed/simple; bh=2Bkhy71J5qlS5RrsFa5MKmbcnBZRPtNuBcoWl25nh10=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KqPdp748CcbijNn+mhDQJaSwqL1jvIJWIZCveAZ+yQ9OsFldaRCPi9Q9Io/NCX++KuXw93TglI1mNCw1ITdyzlj7IsOoXUfc8WUHgDl3o7MbwXrdbcy+ND4ogpRc0nnxitv9HlbZp4YTQN/1myYILYm5jtrkBaZmqIxNwtVwb5Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de; spf=pass smtp.mailfrom=gmx.de; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b=HTG6LFkc; arc=none smtp.client-ip=212.227.15.15 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmx.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmx.de header.i=w_armin@gmx.de header.b="HTG6LFkc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1712068270; x=1712673070; i=w_armin@gmx.de; bh=VPDvKGPAbnbX62QiYPBfcbU9tv5ioaZTqMr7RjGug6w=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To: References; b=HTG6LFkcRV6+VDZGDgRy12KxnAlgL8jWIAPpczvfJsMp4lqdER+uq++lu83GQJae S8gLopj0KwrQ2dhiTRBYOzYYeFYCQdkUyVEtun/amX47DWdeCFJG1hRMpVsAPkMLM BAXJXF9KD3WAgALVUzBJCPhpRe3SkdINF/i5BMGWaSBKU9kM+wZgMVnNUpFAhF57p PKt0gsSRWX1BuGQVfxxIYc3TpntSnYzXEIsCV//cs01sCdozTfAOqvPQWTxDscpKU MxKV0ch3BhSTwCVVcjaFn/u/vjxfczi33R8BtxNqnmIUfICpmo8MuHH6eLj8mOA68 p+/eRAwbq/rAeWAOFQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from mx-amd-b650.users.agdsn.de ([141.30.226.129]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1M26vL-1rpkQB1Hmj-002XAE; Tue, 02 Apr 2024 16:31:10 +0200 From: Armin Wolf To: hdegoede@redhat.com, ilpo.jarvinen@linux.intel.com Cc: corbet@lwn.net, sathyanarayanan.kuppuswamy@linux.intel.com, linux-doc@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/4] platform/x86: wmi: Add driver development guide Date: Tue, 2 Apr 2024 16:30:59 +0200 Message-Id: <20240402143059.8456-4-W_Armin@gmx.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240402143059.8456-1-W_Armin@gmx.de> References: <20240402143059.8456-1-W_Armin@gmx.de> Precedence: bulk X-Mailing-List: platform-driver-x86@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Provags-ID: V03:K1:fgY4JgX5GAfRIFCjvlKFSyEYOWycN7xPHzcl6k3zcuPynmJh6/z vN9WF6nbxYefL4B44VRP57NQLiizn1cbMfaqA4spouBRwy0UcCtTDDks4xW1ajAbaNmXp7g sSClKxh6K2EcboS1YE5VQbwAW1PD81z9TZj3cl3JlUZFG+ZjCy8Bc8NCbajnLnQYzLAENB7 weXV3GyCzQBsT7tgI4bUg== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:QFp6LdFrmnQ=;lf6V/umIJHQngdIL3wEGH+37K3e dCzMX5vNs7Nzo7idEvAD3dJM/Wpx9JzQPBF5AisdXjJfGXPufs2wp7DZcA0psTOdFk9Ramgyv hzseaI06zD1jq6FqvwaJBrIWS3WuZsl3AkTQWOKNIB+pyE6QSfmt+Uxnur8tY//4ir7p5v7Dg hYk0TA/5yedfSd2lWr46EIttlB/aMixBQmbvRRithRQfzgu25qadqWjnVj8mAAFeFjt01/F9L FRiW4W45uFgYHKtCerroy5EGtAZjsDWOdF9Zxju+3V4cDk6+aGnThUH871EXsMcDZEElSNdvB xEJ5Mo5q+amTrUUShRWwn6xWxS/YBOIqXM1m7rxMmMEC8bKkbGuOg9gCAMjBxvHo0gCZnMdBX gCYAlI/P8F0SwhIvIdqzRFslwaONxwuPVLeh5OPEybyU63QpqVTdjQqFPDugK7qKb61oQEIo7 i/g1SXzgtXt09d+VYAvOEuKEHnei3IsA8/163JDw0UG1iMtvlZZs0ZTpJZkHKHVlvLbTPDip7 mrbjn6z57yJu357YfmGXCTgw0VbISWB5Lh02A4jJPjNIJBUDPJbCT6xKNtwlUvc+5cniiQPAt N4S8YfyZus9uFxMuJcnOBAEBpO8ric2CzUPeR/RyXGGv6dDC9P+tC352Elp8q5FMykFslxVRp IAqJ7whB+DKBx52W7MXMzq1NAct3WqM3yKagg/UvkOrPYEqSMKxEKGBvaJ4P2EBNHpAfqE9vc OmFKDIK7BoR0j++xQ2x+/S2NyjdP+K3TnJobD47o61PExk+nbGEl7AWU7Io4SScqs1G/ntm9K QEv7+hZJMizBeCdJYN6+EoHf+ts9BMGGIcNvY9UtAy5gs= Since 2010, an LWN article covering WMI drivers exists: https://lwn.net/Articles/391230/ Since the introduction of the modern bus-based interface and other userspace tooling (bmfdec, lswmi, ...), this article is outdated and causes people to still submit new WMI drivers using the deprecated GUID-based interface. Fix this by adding a short guide on how to develop WMI drivers using the modern bus-based interface. Signed-off-by: Armin Wolf Reviewed-by: Kuppuswamy Sathyanarayanan --- Changes since v1: - use footnote for lwn article link - wording fixes --- .../wmi/driver-development-guide.rst | 178 ++++++++++++++++++ Documentation/wmi/index.rst | 1 + 2 files changed, 179 insertions(+) create mode 100644 Documentation/wmi/driver-development-guide.rst -- 2.39.2 diff --git a/Documentation/wmi/driver-development-guide.rst b/Documentation/wmi/driver-development-guide.rst new file mode 100644 index 000000000000..429137b2f632 --- /dev/null +++ b/Documentation/wmi/driver-development-guide.rst @@ -0,0 +1,178 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +============================ +WMI driver development guide +============================ + +The WMI subsystem provides a rich driver API for implementing WMI drivers, +documented at Documentation/driver-api/wmi.rst. This document will serve +as an introductory guide for WMI driver writers using this API. It is supposed +to be a successor to the original LWN article [1]_ which deals with WMI drivers +using the deprecated GUID-based WMI interface. + +Obtaining WMI device information +-------------------------------- + +Before developing an WMI driver, information about the WMI device in question +must be obtained. The `lswmi `_ utility can be +used to extract detailed WMI device information using the following command: + +:: + + lswmi -V + +The resulting output will contain information about all WMI devices available on +a given machine, plus some extra information. + +In order to find out more about the interface used to communicate with a WMI device, +the `bmfdec `_ utilities can be used to decode +the Binary MOF (Managed Object Format) information used to describe WMI devices. +The ``wmi-bmof`` driver exposes this information to userspace, see +Documentation/wmi/devices/wmi-bmof.rst. + +In order to retrieve the decoded Binary MOF information, use the following command (requires root): + +:: + + ./bmf2mof /sys/bus/wmi/devices/05901221-D566-11D1-B2F0-00A0C9062910[-X]/bmof + +Sometimes, looking at the disassembled ACPI tables used to describe the WMI device +helps in understanding how the WMI device is supposed to work. The path of the ACPI +method associated with a given WMI device can be retrieved using the ``lswmi`` utility +as mentioned above. + +Basic WMI driver structure +-------------------------- + +The basic WMI driver is build around the struct wmi_driver, which is then bound +to matching WMI devices using a struct wmi_device_id table: + +:: + + static const struct wmi_device_id foo_id_table[] = { + { "936DA01F-9ABD-4D9D-80C7-02AF85C822A8", NULL }, + { } + }; + MODULE_DEVICE_TABLE(wmi, foo_id_table); + + static struct wmi_driver foo_driver = { + .driver = { + .name = "foo", + .probe_type = PROBE_PREFER_ASYNCHRONOUS, /* recommended */ + .pm = pm_sleep_ptr(&foo_dev_pm_ops), /* optional */ + }, + .id_table = foo_id_table, + .probe = foo_probe, + .remove = foo_remove, /* optional, devres is preferred */ + .notify = foo_notify, /* optional, for event handling */ + .no_notify_data = true, /* optional, enables events containing no additional data */ + .no_singleton = true, /* required for new WMI drivers */ + }; + module_wmi_driver(foo_driver); + +The probe() callback is called when the WMI driver is bound to a matching WMI device. Allocating +driver-specific data structures and initialising interfaces to other kernel subsystems should +normally be done in this function. + +The remove() callback is then called when the WMI driver is unbound from a WMI device. In order +to unregister interfaces to other kernel subsystems and release resources, devres should be used. +This simplifies error handling during probe and often allows to omit this callback entirely, see +Documentation/driver-api/driver-model/devres.rst for details. + +Please note that new WMI drivers are required to be able to be instantiated multiple times, +and are forbidden from using any deprecated GUID-based WMI functions. This means that the +WMI driver should be prepared for the scenario that multiple matching WMI devices are present +on a given machine. + +Because of this, WMI drivers should use the state container design pattern as described in +Documentation/driver-api/driver-model/design-patterns.rst. + +WMI method drivers +------------------ + +WMI drivers can call WMI device methods using wmidev_evaluate_method(), the +structure of the ACPI buffer passed to this function is device-specific and usually +needs some tinkering to get right. Looking at the ACPI tables containing the WMI +device usually helps here. The method id and instance number passed to this function +are also device-specific, looking at the decoded Binary MOF is usually enough to +find the right values. + +The maximum instance number can be retrieved during runtime using wmidev_instance_count(). + +Take a look at drivers/platform/x86/inspur_platform_profile.c for an example WMI method driver. + +WMI data block drivers +---------------------- + +WMI drivers can query WMI device data blocks using wmidev_block_query(), the +structure of the returned ACPI object is again device-specific. Some WMI devices +also allow for setting data blocks using wmidev_block_set(). + +The maximum instance number can also be retrieved using wmidev_instance_count(). + +Take a look at drivers/platform/x86/intel/wmi/sbl-fw-update.c for an example +WMI data block driver. + +WMI event drivers +----------------- + +WMI drivers can receive WMI events via the notify() callback inside the struct wmi_driver. +The WMI subsystem will then take care of setting up the WMI event accordingly. Please note that +the structure of the ACPI object passed to this callback is device-specific, and freeing the +ACPI object is being done by the WMI subsystem, not the driver. + +The WMI driver core will take care that the notify() callback will only be called after +the probe() callback has been called, and that no events are being received by the driver +right before and after calling its remove() callback. + +However WMI driver developers should be aware that multiple WMI events can be received concurrently, +so any locking (if necessary) needs to be provided by the WMI driver itself. + +In order to be able to receive WMI events containing no additional event data, +the ``no_notify_data`` flag inside struct wmi_driver should be set to ``true``. + +Take a look at drivers/platform/x86/xiaomi-wmi.c for an example WMI event driver. + +Handling multiple WMI devices at once +------------------------------------- + +There are many cases of firmware vendors using multiple WMI devices to control different aspects +of a single physical device. This can make developing WMI drivers complicated, as those drivers +might need to communicate with each other to present a unified interface to userspace. + +On such case involves a WMI event device which needs to talk to a WMI data block device or WMI +method device upon receiving an WMI event. In such a case, two WMI drivers should be developed, +one for the WMI event device and one for the other WMI device. + +The WMI event device driver has only one purpose: to receive WMI events, validate any additional +event data and invoke a notifier chain. The other WMI driver adds itself to this notifier chain +during probing and thus gets notified every time a WMI event is received. This WMI driver might +then process the event further for example by using an input device. + +For other WMI device constellations, similar mechanisms can be used. + +Things to avoid +--------------- + +When developing WMI drivers, there are a couple of things which should be avoided: + +- usage of the deprecated GUID-based WMI interface which uses GUIDs instead of WMI device structs +- bypassing of the WMI subsystem when talking to WMI devices +- WMI drivers which cannot be instantiated multiple times. + +Many older WMI drivers violate one or more points from this list. The reason for +this is that the WMI subsystem evolved significantly over the last two decades, +so there is a lot of legacy cruft inside older WMI drivers. + +New WMI drivers are also required to conform to the linux kernel coding style as specified in +Documentation/process/coding-style.rst. The checkpatch utility can catch many common coding style +violations, you can invoke it with the following command: + +:: + + ./scripts/checkpatch.pl --strict + +References +========== + +.. [1] https://lwn.net/Articles/391230/ diff --git a/Documentation/wmi/index.rst b/Documentation/wmi/index.rst index 537cff188e14..fec4b6ae97b3 100644 --- a/Documentation/wmi/index.rst +++ b/Documentation/wmi/index.rst @@ -8,6 +8,7 @@ WMI Subsystem :maxdepth: 1 acpi-interface + driver-development-guide devices/index .. only:: subproject and html