From patchwork Fri May 27 09:14:27 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manfred Schlaegl X-Patchwork-Id: 9137951 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 727B96075A for ; Fri, 27 May 2016 09:14:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 640B920202 for ; Fri, 27 May 2016 09:14:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 58BFB27CCD; Fri, 27 May 2016 09:14:54 +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 C303E20202 for ; Fri, 27 May 2016 09:14:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932708AbcE0JOw (ORCPT ); Fri, 27 May 2016 05:14:52 -0400 Received: from mout.gmx.net ([212.227.17.22]:51280 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932463AbcE0JOv (ORCPT ); Fri, 27 May 2016 05:14:51 -0400 Received: from [10.30.2.13] ([90.146.92.193]) by mail.gmx.com (mrgmx102) with ESMTPSA (Nemesis) id 0MVsUW-1aziZO1ZWq-00X3n9; Fri, 27 May 2016 11:14:42 +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> <20160526003615.GE22369@dtor-ws> <57480B5C.7080409@gmx.at> <57480F45.7070106@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: <57480FF3.9060107@gmx.at> Date: Fri, 27 May 2016 11:14:27 +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: <57480F45.7070106@gmx.at> X-Provags-ID: V03:K0:0myWL8QQhSXeldXMZ0NGDgP+GPgDZMyYQ9RevbPY257ssHJIKgJ jCIiGAhXq+vlHQ4JP8TkDZWz9PmGjOte8imnyU7HM7i+FLp6DHXybOXB9ReCP2BKBCX9xnZ oWBkuMBtlHYBUuYm2xqDRQ3INCqs4+p2WBSEqHHiRSCMswPLopVslbLKVG63N+y1Cpa3wNk vh2itWgTBQtPXoEjM+XLw== X-UI-Out-Filterresults: notjunk:1; V01:K0:sw9c1mcbgyQ=:FeuZIsFjCJ0tiflYvMkkoW rAEUcehzt9cpyfHVESoGq5CPgotsK41nNkqwVJWklelpiRoeRzPUgUFr6DjcfV4/Fesh0WuXE S9NhiZBF/YO5BTIGMr6mXW2BtRlVSCM2uyOlbXcfREsocxOluZIGJZq1AAzPH7cSMt9DBm05y vOyuV4k3Bq+XghpF+29anB/+FqfCmfn0Au8zaKw7QW3gY6hDZ9aSayWDas6elACGJcjcUuHQe Jf2Mvbo8Ugm98FMA7E5XSgVlg1TiHnbFK+4zN/3qvZ4U7fFH+A8OU+40PG1+GkrxMj25pZppK Xn8DE1XeZygAs91yvzXbgjolffbOIoZXK5Xdr9+CrC3P7ERT4uGIZ80N5pnXkCjYQDPooN1xN SQLjLdQksrubsm7pKzOK1ku3b8WAl8mn4GJ7uooDvnKNdaw6etDho72cCr5821UeICQS3uOO8 kGxmOE/1YmCDnbsRdOrgvdR3lDTsATRo9cL1cpnVJP39qjJq/dkjwghNNVgSRFwmb8GW7QGCN 5HOYEAnEGdCLJO7/ywtiO2rsUsrcqiaKITyFJ5JtKSlTtjw+RIEBO/0xmXOGfT782RAIMscUa gZyHIsQKsvGTKUQJb3Pq6063WzYK1EAjo6LD6xgWjmif6ipfn+5wCrnOOxMVRPk4W/D+ZMpIu f4fmnKjxlhn3DW02ZYIW+aXS8l4BjpXFzwf99l9fr9RPRaY9VYLBwT6wWGYwLNq8QGQvaJy0G gRVHY8hH8CsKQzB2xfvUALY29G8D20NWsAMGVYduFx7GopeBwpb0Jnmo/NFeiA/ZX92Pj7DTo LpETNWZLdgKBSszRM5jSZ+h4b0sww== 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. Signed-off-by: Manfred Schlaegl --- drivers/input/misc/pwm-beeper.c | 69 ++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 21 deletions(-) diff --git a/drivers/input/misc/pwm-beeper.c b/drivers/input/misc/pwm-beeper.c index 8d71332..5f9655d 100644 --- a/drivers/input/misc/pwm-beeper.c +++ b/drivers/input/misc/pwm-beeper.c @@ -20,21 +20,40 @@ #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; + + if (period) { + pwm_config(beeper->pwm, period / 2, period); + pwm_enable(beeper->pwm); + } else + pwm_disable(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,22 +68,31 @@ 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; } +static void pwm_beeper_stop(struct pwm_beeper *beeper) +{ + cancel_work_sync(&beeper->work); + + if (beeper->period) + pwm_disable(beeper->pwm); +} + +static void pwm_beeper_close(struct input_dev *input) +{ + struct pwm_beeper *beeper = input_get_drvdata(input); + + pwm_beeper_stop(beeper); +} + static int pwm_beeper_probe(struct platform_device *pdev) { unsigned long pwm_id = (unsigned long)dev_get_platdata(&pdev->dev); @@ -93,6 +121,8 @@ static int pwm_beeper_probe(struct platform_device *pdev) */ pwm_apply_args(beeper->pwm); + 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"); @@ -112,6 +142,7 @@ static int pwm_beeper_probe(struct platform_device *pdev) beeper->input->sndbit[0] = BIT(SND_TONE) | BIT(SND_BELL); beeper->input->event = pwm_beeper_event; + beeper->input->close = pwm_beeper_close; input_set_drvdata(beeper->input, beeper); @@ -141,7 +172,6 @@ static int pwm_beeper_remove(struct platform_device *pdev) input_unregister_device(beeper->input); - pwm_disable(beeper->pwm); pwm_free(beeper->pwm); kfree(beeper); @@ -153,8 +183,7 @@ static int __maybe_unused pwm_beeper_suspend(struct device *dev) { struct pwm_beeper *beeper = dev_get_drvdata(dev); - if (beeper->period) - pwm_disable(beeper->pwm); + pwm_beeper_stop(beeper); return 0; } @@ -163,10 +192,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; }