From patchwork Wed May 18 15:16:49 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Manfred Schlaegl X-Patchwork-Id: 9120571 Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 00F329F30C for ; Wed, 18 May 2016 15:17:08 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0DEA220351 for ; Wed, 18 May 2016 15:17:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 378D620125 for ; Wed, 18 May 2016 15:17:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753108AbcERPRD (ORCPT ); Wed, 18 May 2016 11:17:03 -0400 Received: from mout.gmx.net ([212.227.17.20]:55313 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751736AbcERPRC (ORCPT ); Wed, 18 May 2016 11:17:02 -0400 Received: from [10.10.1.106] ([81.10.210.238]) by mail.gmx.com (mrgmx103) with ESMTPSA (Nemesis) id 0M2cDB-1bswsg3lfp-00sPO7; Wed, 18 May 2016 17:16:52 +0200 Subject: [PATCH] Input: pwm-beeper - fix: scheduling while atomic To: Thierry Reding , Dmitry Torokhov References: <56C4735E.6020300@gmx.at> <20160222194639.GD26177@dtor-ws> <20160512121852.GB26824@ulmo.ba.sec> <5735F4E3.20008@gmx.at> Cc: 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: <573C8761.9070601@gmx.at> Date: Wed, 18 May 2016 17:16:49 +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: <5735F4E3.20008@gmx.at> X-Provags-ID: V03:K0:bXJVrBarNZz8BIO2NMGVgX5PxfK+4la3EZir2OSXeSDSP8AgiJq 2VUd0YQ2clLU5BvgaTcxm/eJfZeqYFByMM+hG37g26G/dH/vYx5AUAcwFOPKP0g0gkRxDNn ebV1K0W5PLBXdXq4FZYEGiB1wJkx8zJuPASljfPJRpMklynpvyosf4yLEthHTaKFztSrBj3 VJIzV1ewTKJseWbL5FeCw== X-UI-Out-Filterresults: notjunk:1; V01:K0:Nf4p9gFDQZU=:GgZoTHTRpD+EfhE7GYa5UI Pdsz1o1iz0DyCJSqb0a6vH1RAOp73wz9GZv0UXDIbqeR7v+IySknZBs9xOVkF1M5YwWMjbB+I QHpg/CviUa6PmRT+m218O8ipXFhW9N12KnEk6gqbHn0axOEPgkYPurrVXuAugZIqOWy9DyLVc 3+/9qEcfwzOKS6grhd+w2umXgx/S6BHiGc32xPbOsUj+wMQ9SOIxbNU39EdFeZxwoo3cyIpkq xTJ6a6ggrXTFh43gJQiA2U52gYt3pKLqG2LghHdaXsYoobrC9pxzeNG3cieZKbWPOK4PHKUhP g396XG1DauI+PdMFW8PpRv+RGfHg6l6kXJkVPZJoSjjdd6V2ziqdZHNU009RBlbfRw7uR/yPz 6rgctzZOdBSMUa1OqH9ykmYCmv7FMnz818NW2mN3T9n7kuzsZzUGTOdmPdH1cRxq7ZniQkoEc taJdgIwtm5U62kZrcJsiSL9cBLjMJodo0xgkO4W5MTOUmJDd+7XGkbvStqyRthkaK7vJGAUnw 7QfRzilqiDb3A8B3SbEJvDC71rDsJtTiRgffU5YfaWtwptf13J+frbB9vBttkEeSs4exdMPrM Rq2jMq8TVMouPPwSo9gcfmSW3kBJRof33a3IX5HgZphhbSEzgbYhPFChEkAVGAIZEoeJU0r6j Myo/sB//Vrf5yZEiGSGYVKCz/Lwh8Y6crgsr/FxEHFWZb4bk7ITOxRF/huRqoMIi45Nl+biIc USZ4cUkLDlU2/bVVr8RFo+o4fr9xLIDyFsiUigb0Qi/5J/ZNdfLhhqlojtxti0mH7cxft+ZGk XR1ZMbR Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Pwm config may sleep so defer it using a worker. Trigger: 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. Unmodified applicable to * 4.6 (stable) * 4.5.4 (stable) * 4.4.10 (longterm) * 4.1.24 (longterm) Modified applicable to * 3.18.33 (longterm) 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..7d783c8 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"); @@ -133,6 +149,8 @@ static int pwm_beeper_remove(struct platform_device *pdev) { struct pwm_beeper *beeper = platform_get_drvdata(pdev); + cancel_work_sync(&beeper->work); + input_unregister_device(beeper->input); pwm_disable(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; }