Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2936754imu; Fri, 18 Jan 2019 01:49:01 -0800 (PST) X-Google-Smtp-Source: ALg8bN4yQHwkwQxtR/lKUG/Z2vkv4hK/APgPegsxFkKQY1VcMFewt2Gc2EHCfbmTK/R/kSUk0NAz X-Received: by 2002:a62:5d0c:: with SMTP id r12mr19410405pfb.0.1547804941306; Fri, 18 Jan 2019 01:49:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547804941; cv=none; d=google.com; s=arc-20160816; b=N84LP1ypTg4UHLOyHjIp4S+aOfRe5qStNOaY0lFl0E0WiHhCRuDti3+jOAsS2F0/ep sFlsDdhVIUaDdV5Obip/2Ed94aTPlMx4f588k/ec/9QBUgU4/RIoeUGI4rwA4bbeMXrj /4lWwwYrKJLkBXevIot7kbruUQ6Pzs9mD3kteHV9tZ5ffJtVvVL8teIgKPJ4c/T8ATy0 g68YV8O29vdckVs0MNW1zKqo9uM8eVRSbuFixlDDw7AuOWFPrrt4hmR9W6ltOIddj2uf sbliAVa9kb1q1aPv546aLou3aT0eLII1vZ0bqb0Zg49F33ffUp8PIUVFqM7ho6GCJjvQ cKmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :references:in-reply-to:date:cc:to:from:subject:message-id; bh=OHM2c286BF+/FitMyxmzxn/buAtnaq4FZhWm8LQODmY=; b=LTxWvK/LtmkX6ql8K6nhWKErx6qcqd0lD/KF6cqflKX0y58fKpI0u4WPszH3AY+lqj B7jm8B/v/oSsknf/trtHg3gdINoh8ro7J7lwzAcx9o/clTOaQJuJ0G2td+G9pO8j1zgS 4TR/g2rFck8GKNtl4X1hAkfAKt1Z/hnALOQQqbWWI4WD0dUZv47E0LBIglt+5BiT9NtB +zUkJMDZnSmhRywwHjRxa76+etpDtCVx8XOiIQGrW0zzjaNlTInUi8zPH9U6EE5LwCqz 1qjW146JGXtzXSuqWuQGw/IQ3FdJbEQCFqONvKXEfBYqapnj195UUUdmhIL3F94YaJZY qM1A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u91si4431551plb.237.2019.01.18.01.48.45; Fri, 18 Jan 2019 01:49:01 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727128AbfARJrl (ORCPT + 99 others); Fri, 18 Jan 2019 04:47:41 -0500 Received: from mailgw02.mediatek.com ([1.203.163.81]:50663 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726095AbfARJrk (ORCPT ); Fri, 18 Jan 2019 04:47:40 -0500 X-UUID: 87cf04890925441fb7e609f0cdf5bfed-20190118 X-UUID: 87cf04890925441fb7e609f0cdf5bfed-20190118 Received: from mtkcas32.mediatek.inc [(172.27.4.250)] by mailgw02.mediatek.com (envelope-from ) (mailgw01.mediatek.com ESMTP with TLS) with ESMTP id 1951519869; Fri, 18 Jan 2019 17:47:35 +0800 Received: from MTKCAS06.mediatek.inc (172.21.101.30) by MTKMBS31N2.mediatek.inc (172.27.4.87) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Fri, 18 Jan 2019 17:47:34 +0800 Received: from [172.21.77.33] (172.21.77.33) by MTKCAS06.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Fri, 18 Jan 2019 17:47:33 +0800 Message-ID: <1547804853.8124.9.camel@mtkswgap22> Subject: Re: [PATCH v1 1/5] pwm: mediatek: add a property "mediatek,num-pwms" From: Ryder Lee To: Uwe =?ISO-8859-1?Q?Kleine-K=F6nig?= CC: Thierry Reding , , , Sean Wang , Weijie Gao , , , Matthias Brugger , Date: Fri, 18 Jan 2019 17:47:33 +0800 In-Reply-To: <20190118080552.ppwgxboh3gt3nasv@pengutronix.de> References: <20190118080552.ppwgxboh3gt3nasv@pengutronix.de> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.2.3-0ubuntu6 Content-Transfer-Encoding: 8bit MIME-Version: 1.0 X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2019-01-18 at 09:05 +0100, Uwe Kleine-König wrote: > Hello, > > just realized another issue while looking up a driver detail ... > > On Fri, Jan 18, 2019 at 11:24:41AM +0800, Ryder Lee wrote: > > This adds a property "mediatek,num-pwms" to avoid having an endless > > list of compatibles with no differences for the same driver. > > > > Thus, the driver should have backwards compatibility to older DTs. > > > > Signed-off-by: Ryder Lee > > --- > > Changes since v1: add some checks for backwards compatibility. > > --- > > drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++--------- > > 1 file changed, 18 insertions(+), 9 deletions(-) > > > > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c > > index eb6674c..81b7e5e 100644 > > --- a/drivers/pwm/pwm-mediatek.c > > +++ b/drivers/pwm/pwm-mediatek.c > > @@ -55,7 +55,7 @@ enum { > > }; > > > > struct mtk_pwm_platform_data { > > - unsigned int num_pwms; > > + unsigned int num_pwms; /* it should not be used in the future SoCs */ > > bool pwm45_fixup; > > bool has_clks; > > }; > > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm) > > > > static int mtk_pwm_probe(struct platform_device *pdev) > > { > > - const struct mtk_pwm_platform_data *data; > > + struct device_node *np = pdev->dev.of_node; > > struct mtk_pwm_chip *pc; > > struct resource *res; > > - unsigned int i; > > + unsigned int i, num_pwms; > > int ret; > > > > pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL); > > if (!pc) > > return -ENOMEM; > > > > - data = of_device_get_match_data(&pdev->dev); > > - if (data == NULL) > > - return -EINVAL; > > - pc->soc = data; > > + pc->soc = of_device_get_match_data(&pdev->dev); > > > > res = platform_get_resource(pdev, IORESOURCE_MEM, 0); > > pc->regs = devm_ioremap_resource(&pdev->dev, res); > > if (IS_ERR(pc->regs)) > > return PTR_ERR(pc->regs); > > > > - for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) { > > + /* Check if we have set 'num-pwms' in DTs. */ > > + ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms); > > + if (ret < 0) { > > + /* If no, fallback to use SoC data for backwards compatibility. */ > > + if (pc->soc->num_pwms) { > > + num_pwms = pc->soc->num_pwms; > > + } else { > > + dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret); > > + return ret; > > + } > > + } > > + > > + for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) { > > pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]); > > If a dt contains > > mediatek,num-pwms = <17>; > > you're accessing pc->clks[18] which is an out-of-bounds access, so > better check the limit or allocate the clks array dynamically. > Thanks for the reminder. I will fix it in v2. Ryder