Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1822972yba; Sat, 27 Apr 2019 07:53:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqwCmD7zZrQpyJ7qXgmxqUB5uhH1222p8bPMYWvyv8rWPAFmioBsZEjg2XvKOrYfizbSedY/ X-Received: by 2002:a63:6ac1:: with SMTP id f184mr50642844pgc.25.1556376817888; Sat, 27 Apr 2019 07:53:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556376817; cv=none; d=google.com; s=arc-20160816; b=mW8bwGtwEW4cR9Ev2npk9UjNTKF3KWdLP+umIjMjakcFLZt0WrD6AWNi439lBZl84t 1vSRWs+MDTe5mBcT+uPG4Xm5dEpcRBsDHzVCNOROPaN9WF1Hhy1458r6rp+vwSaCOtZC 6/aDBEDAd0xbzuoIhBGnCY4vNcv4TBmFYgPVmxMSPz8U3XuVU6jY4qM3rinH5XlJwuEG S9R9/lT9c/Km9CQ3OEDx6fBvTGG63ml6AgYgdbSdfapt6Vc6todNFseTUP+qO7Nfi+Ti voUGu8pp8q4Gy4tkagh9kWECO2fJpo9+S0ZK9DLcglpgtpwO4PtFL3gSkofLP9044eBQ 7j5Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=+HyS8qKGVRvHcw7cerxrg/A6q6KuQ3LhpKax3Ve9MmQ=; b=hXIxK++kwvxfx1/2RjF3c3Ouon70msX5M9qHPmeza5jux88/WB5NoFYuJ8XgmW9nSw DbcdnZ6rtbc6Ayi4L8XixveEkHRp7ylsbvTbyTuh4ypYQrH3IgXOt9j0mtTHu1nyWM5A 6r5uwJ84c0dlxcCSSQucYUn/zQHATQgN6RhhemCbBNyVR5xrhR18sBVJ/yov0bQJEn2S OYkzVsKg2MYBQRE9BFBXNg9j5NFuA3KEHzt34xszczVU25nO5uw+4VgU3joTjWiX21+e A0VbPmDgZb8JjMEN3sA9W5Yl013GmSZDvVUb7g7MSEoZnoGnLzTRpdrFrr8KWY0E1h5H r4tA== 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 f3si29798916plb.396.2019.04.27.07.53.22; Sat, 27 Apr 2019 07:53:37 -0700 (PDT) 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 S1726927AbfD0OvH (ORCPT + 99 others); Sat, 27 Apr 2019 10:51:07 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:49536 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725942AbfD0OvG (ORCPT ); Sat, 27 Apr 2019 10:51:06 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1hKOfN-00080E-Mx; Sat, 27 Apr 2019 16:51:01 +0200 From: "Maciej S. Szmigiero" To: Michael Krufky , Mauro Carvalho Chehab Cc: Andy Walls , linux-kernel , linux-media@vger.kernel.org, Hans Verkuil Subject: [PATCH v11 4/7] cx25840: set_fmt operation should clamp out-of-range picture sizes Date: Sat, 27 Apr 2019 16:50:31 +0200 Message-Id: X-Mailer: git-send-email 2.21.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org According to V4L2 API set_fmt subdev operation should not return an error on out-of-range picture sizes, the values should be clamped instead to the supported range. The cx25840 datasheet says that the chip is capable of scaling down the picture width and height, respectively, 16 and 8 times. These values agree with what the old implementation enforced. Signed-off-by: Maciej S. Szmigiero --- drivers/media/i2c/cx25840/cx25840-core.c | 59 ++++++++++++++++-------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/drivers/media/i2c/cx25840/cx25840-core.c b/drivers/media/i2c/cx25840/cx25840-core.c index 01889613173c..baa79efae1d1 100644 --- a/drivers/media/i2c/cx25840/cx25840-core.c +++ b/drivers/media/i2c/cx25840/cx25840-core.c @@ -1702,7 +1702,8 @@ static int cx25840_set_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *fmt = &format->format; struct cx25840_state *state = to_state(sd); struct i2c_client *client = v4l2_get_subdevdata(sd); - int HSC, VSC, Vsrc, Hsrc, filter, Vlines; + u32 HSC, VSC, Vsrc, Hsrc, Vadd; + int filter; int is_50Hz = !(state->std & V4L2_STD_525_60); if (format->pad || fmt->code != MEDIA_BUS_FMT_FIXED) @@ -1727,28 +1728,46 @@ static int cx25840_set_fmt(struct v4l2_subdev *sd, Hsrc |= (cx25840_read(client, 0x471) & 0xf0) >> 4; } - Vlines = fmt->height; - if (!state->generic_mode) - Vlines += is_50Hz ? 4 : 7; + if (!state->generic_mode) { + Vadd = is_50Hz ? 4 : 7; - /* - * We keep 1 margin for the Vsrc < Vlines check since the - * cx23888 reports a Vsrc of 486 instead of 487 for the NTSC - * height. Without that margin the cx23885 fails in this - * check. - */ - if ((fmt->width == 0) || (Vlines == 0) || - (fmt->width * 16 < Hsrc) || (Hsrc < fmt->width) || - (Vlines * 8 < Vsrc) || (Vsrc + 1 < Vlines)) { - v4l_err(client, "%dx%d is not a valid size!\n", - fmt->width, fmt->height); - return -ERANGE; + /* + * cx23888 in 525-line mode is programmed for 486 active lines + * while other chips use 487 active lines. + * + * See reg 0x428 bits [21:12] in cx23888_std_setup() vs + * vactive in cx25840_std_setup(). + */ + if (is_cx23888(state) && !is_50Hz) + Vadd--; + } else + Vadd = 0; + + if (Hsrc == 0 || + Vsrc <= Vadd) { + v4l_err(client, + "chip reported picture size (%u x %u) is far too small\n", + (unsigned int)Hsrc, (unsigned int)Vsrc); + /* + * that's the best we can do since the output picture + * size is completely unknown in this case + */ + return -EINVAL; } + + fmt->width = clamp(fmt->width, (Hsrc + 15) / 16, Hsrc); + + if (Vadd * 8 >= Vsrc) + fmt->height = clamp(fmt->height, (u32)1, Vsrc - Vadd); + else + fmt->height = clamp(fmt->height, (Vsrc - Vadd * 8 + 7) / 8, + Vsrc - Vadd); + if (format->which == V4L2_SUBDEV_FORMAT_TRY) return 0; HSC = (Hsrc * (1 << 20)) / fmt->width - (1 << 20); - VSC = (1 << 16) - (Vsrc * (1 << 9) / Vlines - (1 << 9)); + VSC = (1 << 16) - (Vsrc * (1 << 9) / (fmt->height + Vadd) - (1 << 9)); VSC &= 0x1fff; if (fmt->width >= 385) @@ -1760,8 +1779,10 @@ static int cx25840_set_fmt(struct v4l2_subdev *sd, else filter = 3; - v4l_dbg(1, cx25840_debug, client, "decoder set size %dx%d -> scale %ux%u\n", - fmt->width, fmt->height, HSC, VSC); + v4l_dbg(1, cx25840_debug, client, + "decoder set size %u x %u with scale %x x %x\n", + (unsigned int)fmt->width, (unsigned int)fmt->height, + (unsigned int)HSC, (unsigned int)VSC); /* HSCALE=HSC */ if (is_cx23888(state)) {