From patchwork Tue May 24 08:37:16 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manfred Schlaegl X-Patchwork-Id: 9132993 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B65D46075E for ; Tue, 24 May 2016 08:37:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AEB662823B for ; Tue, 24 May 2016 08:37:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A31532828D; Tue, 24 May 2016 08:37:37 +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=-6.9 required=2.0 tests=BAYES_00,FREEMAIL_FROM, 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 823CC2823B for ; Tue, 24 May 2016 08:37:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753147AbcEXIhf (ORCPT ); Tue, 24 May 2016 04:37:35 -0400 Received: from mout.gmx.net ([212.227.17.22]:56248 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752794AbcEXIhd (ORCPT ); Tue, 24 May 2016 04:37:33 -0400 Received: from [10.10.1.106] ([81.10.210.238]) by mail.gmx.com (mrgmx103) with ESMTPSA (Nemesis) id 0LhwAY-1bsDyw0yJe-00n87p; Tue, 24 May 2016 10:37:24 +0200 Subject: Re: [PATCH] Input: pwm-beeper - fix: scheduling while atomic To: Dmitry Torokhov References: <56C4735E.6020300@gmx.at> <20160222194639.GD26177@dtor-ws> <20160512121852.GB26824@ulmo.ba.sec> <5735F4E3.20008@gmx.at> <573C8761.9070601@gmx.at> <20160520165918.GE14951@dtor-ws> <574411B5.2010407@gmx.at> Cc: Thierry Reding , Manfred Schlaegl , Luis de Bethencourt , Olivier Sobrie , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Greg Kroah-Hartman From: Manfred Schlaegl Message-ID: <574412BC.1070406@gmx.at> Date: Tue, 24 May 2016 10:37:16 +0200 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Icedove/38.8.0 MIME-Version: 1.0 In-Reply-To: <574411B5.2010407@gmx.at> X-Provags-ID: V03:K0:RDSibhPpIZv9hfWUq8l8/lVafG9WD8tq2zYCazG711WiO+DyLT7 jvs+AL4hkc9DFvxl5weUYQnuOUmGv/9FUXKTJOKpEG6k0XxPYaw12YJsyooEq+q+nIRotM8 HhC/GLGt6uj+M/Oa+EfprC++TnpOl10kWZugeOIFo8WQ3aXvH1Yoqzh1FlxlFGqDX9CFXX6 /FE6aM/zN/+NkCedGJhEQ== X-UI-Out-Filterresults: notjunk:1; V01:K0:rHrOXNqoTN4=:ZzZXD4wUkwAA/NZT0tz+we Bh9reM8iULNSrVZEVd06sRU7tIMW5tFN4McQwQGoo1em8SBdu/jFBr9es30iidzQgQqETyiqT 7ZTZS6IuSiPJMVi6XK/FJMXI/Oxs24ia+twK1EnV0mbjd8J7vZZxGB/GLYyAyPU70It6BVazo /r2eDTt+JvnoOxo9BjEWAKq81kV13Pvo1foS24f+fiC1+3NKW46qRPAgCvD0EPBm6FiDrwZQb pHETJvw5XEds+6ABH27C2AFVmFUeF7zSVQf9hBcTtBysMhRGNCUFXbqEIz1xly6pNCjp44yoI +XUUinbu1Sbyx30nfeRMYoFdwdCUCb5eDM8miiKFT9pw0zmRfCkg7USHxHBkMam9TlRUrwrK+ +D54mlQL7RIxg4wFOuoJBG9GX8l/yWt6Jx1JtuTHh1uOu6hYdPWg57AH6FHDnH7y6XF1wJjs4 wPkpdSPq9zgVHg6/7AauzMQsKH+jgIKx5pLfWstftA3trnXWwc7LMVFsRJcIsrEWzhP30SnUk /PPfZZKbz1tVrkHs1OJsyyyVDEDkYfTYnAsLytb4vxJ823MUPY4n3U0AfFaTml21z4q57ub0f 3SqwSh+IkfACf6+ewZHR2VB6vpMuoK0dPAt+GBu74OHS+ITXl06utsgKvzmxc9CK9J/fkBPrl 8IRbspPcgC5Bonh6tsPrttEK0CvtPirrbD5iOMn3eXZZHISVyGUVXkcH20QcKMP1iWTISMZ5F 6SktObcJUpz3T+btHPZ4fOJGehBaOJSandx+vgUr30I9Sbr4ungF+oqLw/FBFUbA5qDcgod3y ox3ie5g 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 Pwm config may sleep so defer it using a worker. On a Freescale i.MX53 based board we ran into "BUG: scheduling while atomic" because input_inject_event locks interrupts, but imx_pwm_config_v2 sleeps. Tested on Freescale i.MX53 SoC with 4.6.0 and 4.1.24. Signed-off-by: Manfred Schlaegl --- drivers/input/misc/pwm-beeper.c | 54 +++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/drivers/input/misc/pwm-beeper.c b/drivers/input/misc/pwm-beeper.c index f2261ab..014495d3 100644 --- a/drivers/input/misc/pwm-beeper.c +++ b/drivers/input/misc/pwm-beeper.c @@ -20,21 +20,41 @@ #include #include #include +#include struct pwm_beeper { struct input_dev *input; struct pwm_device *pwm; + struct work_struct work; unsigned long period; }; #define HZ_TO_NANOSECONDS(x) (1000000000UL/(x)) +static void __pwm_beeper_set(struct pwm_beeper *beeper) +{ + unsigned long period = beeper->period; + + pwm_config(beeper->pwm, period / 2, period); + + if (period == 0) + pwm_disable(beeper->pwm); + else + pwm_enable(beeper->pwm); +} + +static void pwm_beeper_work(struct work_struct *work) +{ + struct pwm_beeper *beeper = + container_of(work, struct pwm_beeper, work); + + __pwm_beeper_set(beeper); +} + static int pwm_beeper_event(struct input_dev *input, unsigned int type, unsigned int code, int value) { - int ret = 0; struct pwm_beeper *beeper = input_get_drvdata(input); - unsigned long period; if (type != EV_SND || value < 0) return -EINVAL; @@ -49,18 +69,12 @@ static int pwm_beeper_event(struct input_dev *input, return -EINVAL; } - if (value == 0) { - pwm_disable(beeper->pwm); - } else { - period = HZ_TO_NANOSECONDS(value); - ret = pwm_config(beeper->pwm, period / 2, period); - if (ret) - return ret; - ret = pwm_enable(beeper->pwm); - if (ret) - return ret; - beeper->period = period; - } + if (value == 0) + beeper->period = 0; + else + beeper->period = HZ_TO_NANOSECONDS(value); + + schedule_work(&beeper->work); return 0; } @@ -87,6 +101,8 @@ static int pwm_beeper_probe(struct platform_device *pdev) goto err_free; } + INIT_WORK(&beeper->work, pwm_beeper_work); + beeper->input = input_allocate_device(); if (!beeper->input) { dev_err(&pdev->dev, "Failed to allocate input device\n"); @@ -135,6 +151,8 @@ static int pwm_beeper_remove(struct platform_device *pdev) input_unregister_device(beeper->input); + cancel_work_sync(&beeper->work); + pwm_disable(beeper->pwm); pwm_free(beeper->pwm); @@ -147,6 +165,8 @@ static int __maybe_unused pwm_beeper_suspend(struct device *dev) { struct pwm_beeper *beeper = dev_get_drvdata(dev); + cancel_work_sync(&beeper->work); + if (beeper->period) pwm_disable(beeper->pwm); @@ -157,10 +177,8 @@ static int __maybe_unused pwm_beeper_resume(struct device *dev) { struct pwm_beeper *beeper = dev_get_drvdata(dev); - if (beeper->period) { - pwm_config(beeper->pwm, beeper->period / 2, beeper->period); - pwm_enable(beeper->pwm); - } + if (beeper->period) + __pwm_beeper_set(beeper); return 0; }