From nobody Sun May 11 22:27:10 2025 Delivered-To: importer2@patchew.org Received-SPF: pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; envelope-from=linux-kernel-owner@vger.kernel.org; helo=vger.kernel.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1623846537; cv=none; d=zohomail.com; s=zohoarc; b=Xywgqs2zuQQ962UYEv8sBPW5uPESuq66o0IJYBMsg9oQkFt21XCy5gZTJuCr3XGFD1K8sMQSn3Hy92/NTT8guKT9GeRWidh/VG5iN6JWWuGf/rwqfqMeuZA6yYYpWcGyYPQtqcRLrLqHRU3+IN0CkLhsCbFrGB1e41A7dVeXtAE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623846537; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Id:MIME-Version:Message-ID:References:Sender:Subject:To; bh=ncWJ/JC58phfpZ/7+7u3lvh4zZxegV139d/iiBmEY8E=; b=KyNt2ZQqTwKlFoJK8gZVbYNsk/o7q/nwzNo4kcPRB/b6qxTeEo5ELOXtNW14oqDHajtWgQZJbZQcb1RR7y5FRU9S+FCFv5q9bBIDpfAtd1bzUDyOLTtKDLvRlp5/Y6p+EIpqEyYWMwpHVgZPPs5Yw4T2FvSMAGb+gximmt1weH0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mx.zohomail.com with SMTP id 1623846537310155.5944585249125; Wed, 16 Jun 2021 05:28:57 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232982AbhFPMbB (ORCPT ); Wed, 16 Jun 2021 08:31:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:49292 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232430AbhFPMaq (ORCPT ); Wed, 16 Jun 2021 08:30:46 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8CE0E613BF; Wed, 16 Jun 2021 12:28:40 +0000 (UTC) Received: by mail.kernel.org with local (Exim 4.94.2) (envelope-from ) id 1ltUes-004oir-ET; Wed, 16 Jun 2021 14:28:38 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1623846520; bh=5xE3IwwXuxM1BbQOm8ouU3VOutGE5pmZml00Zbft4vo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Wynh6cAEEtZcmoAvQ7U1c+jm8dk1emJPtqjEGMM9iNO3/wFwJ+8sn2IHse1Gx3Si1 soo3emmhNvkyEMUYfhQeK5KkrrsSb2+YfI5nO0CIcpX2PfJeYhzTT5F9dWmTLAVG/r to5ET8UsmbeYeZuahZwYIqF/pWEWUThydrFRu6RaMRcfnaCmZkfBJBcQn/AwJGXrSD hFCUEqfsguCdu6tkY+Snc9wYaObDy7rdbjRWSTiwSYtcugMikNb1DcVirVel9LmMo5 Bb6cQ/VG9npDLy9IpoEKLcVDrAML67GR3f2W5cm7AYKR3lQwHmG3N///cfmcP7HZ58 u9Loou2Ip8njw== From: Mauro Carvalho Chehab Cc: linuxarm@huawei.com, mauro.chehab@huawei.com, Mauro Carvalho Chehab , Hans Verkuil , Mauro Carvalho Chehab , linux-kernel@vger.kernel.org, linux-media@vger.kernel.org Subject: [PATCH 08/11] media: adv7842: better document EDID block size Date: Wed, 16 Jun 2021 14:28:34 +0200 Message-Id: <6bed7a69367856080a62e3ee89df6a2a3d0d5f20.1623846327.git.mchehab+huawei@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: Mauro Carvalho Chehab To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" While the logic there is correct, it leads to smatch warnings: /home/hans/work/build/media-git/drivers/media/i2c/adv7842.c:2538 adv7842_s= et_edid() error: memcpy() '&state->vga_edid.edid' too small (128 vs 512) Because the code tricks static analyzers by doing: memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks); for ADV7842_EDID_PORT_VGA, where a logic before that makes e->blocks being either 0 or 1. Yet, it is ugly to see the "128" magic number all spread about the EDID code. So, while here, replace 128 (and 4 x 128) by macros: And ensure that the logic which copy into the VGA block will use EDID_MAX_VGA_BLOCKS. Signed-off-by: Mauro Carvalho Chehab --- drivers/media/i2c/adv7842.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/drivers/media/i2c/adv7842.c b/drivers/media/i2c/adv7842.c index 78e61fe6f2f0..30bddab320b9 100644 --- a/drivers/media/i2c/adv7842.c +++ b/drivers/media/i2c/adv7842.c @@ -85,6 +85,10 @@ struct adv7842_format_info { u8 op_format_sel; }; =20 +#define EDID_BLOCK_SIZE 128 +#define EDID_MAX_HDMI_BLOCKS 4 +#define EDID_MAX_VGA_BLOCKS 1 + struct adv7842_state { struct adv7842_platform_data pdata; struct v4l2_subdev sd; @@ -98,12 +102,12 @@ struct adv7842_state { =20 v4l2_std_id norm; struct { - u8 edid[512]; + u8 edid[EDID_BLOCK_SIZE * EDID_MAX_HDMI_BLOCKS]; u32 blocks; u32 present; } hdmi_edid; struct { - u8 edid[128]; + u8 edid[EDID_MAX_VGA_BLOCKS * EDID_MAX_VGA_BLOCKS]; u32 blocks; u32 present; } vga_edid; @@ -732,12 +736,13 @@ static int edid_write_vga_segment(struct v4l2_subdev = *sd) /* edid segment pointer '1' for VGA port */ rep_write_and_or(sd, 0x77, 0xef, 0x10); =20 - for (i =3D 0; !err && i < blocks * 128; i +=3D I2C_SMBUS_BLOCK_MAX) + for (i =3D 0; && i < blocks * EDID_BLOCK_SIZE; i +=3D I2C_SMBUS_BLOCK_MAX= ) { err =3D i2c_smbus_write_i2c_block_data(state->i2c_edid, i, I2C_SMBUS_BLOCK_MAX, edid + i); - if (err) - return err; + if (err) + return err; + } =20 /* Calculates the checksums and enables I2C access * to internal EDID ram from VGA DDC port. @@ -785,7 +790,7 @@ static int edid_write_hdmi_segment(struct v4l2_subdev *= sd, u8 port) return 0; } =20 - pa =3D v4l2_get_edid_phys_addr(edid, blocks * 128, &spa_loc); + pa =3D v4l2_get_edid_phys_addr(edid, blocks * EDID_BLOCK_SIZE, &spa_loc); err =3D v4l2_phys_addr_validate(pa, &parent_pa, NULL); if (err) return err; @@ -800,7 +805,7 @@ static int edid_write_hdmi_segment(struct v4l2_subdev *= sd, u8 port) } =20 =20 - for (i =3D 0; !err && i < blocks * 128; i +=3D I2C_SMBUS_BLOCK_MAX) { + for (i =3D 0; !err && i < blocks * EDID_BLOCK_SIZE; i +=3D I2C_SMBUS_BLOC= K_MAX) { /* set edid segment pointer for HDMI ports */ if (i % 256 =3D=3D 0) rep_write_and_or(sd, 0x77, 0xef, i >=3D 256 ? 0x10 : 0x00); @@ -2491,7 +2496,9 @@ static int adv7842_get_edid(struct v4l2_subdev *sd, s= truct v4l2_edid *edid) if (edid->start_block + edid->blocks > blocks) edid->blocks =3D blocks - edid->start_block; =20 - memcpy(edid->edid, data + edid->start_block * 128, edid->blocks * 128); + memcpy(edid->edid, + data + edid->start_block * EDID_BLOCK_SIZE, + edid->blocks * EDID_BLOCK_SIZE); =20 return 0; } @@ -2506,9 +2513,12 @@ static int adv7842_get_edid(struct v4l2_subdev *sd, = struct v4l2_edid *edid) static int adv7842_set_edid(struct v4l2_subdev *sd, struct v4l2_edid *e) { struct adv7842_state *state =3D to_state(sd); - unsigned int max_blocks =3D e->pad =3D=3D ADV7842_EDID_PORT_VGA ? 1 : 4; + unsigned int max_blocks; int err =3D 0; =20 + max_blocks =3D e->pad =3D=3D ADV7842_EDID_PORT_VGA ? + EDID_MAX_VGA_BLOCKS : EDID_MAX_HDMI_BLOCKS; + memset(e->reserved, 0, sizeof(e->reserved)); =20 if (e->pad > ADV7842_EDID_PORT_VGA) @@ -2535,7 +2545,7 @@ static int adv7842_set_edid(struct v4l2_subdev *sd, s= truct v4l2_edid *e) state->vga_edid.blocks =3D e->blocks; state->vga_edid.present =3D e->blocks ? 0x1 : 0x0; if (e->blocks) - memcpy(&state->vga_edid.edid, e->edid, 128 * e->blocks); + memcpy(&state->vga_edid.edid, e->edid, EDID_BLOCK_SIZE); err =3D edid_write_vga_segment(sd); break; case ADV7842_EDID_PORT_A: @@ -2544,7 +2554,8 @@ static int adv7842_set_edid(struct v4l2_subdev *sd, s= truct v4l2_edid *e) state->hdmi_edid.blocks =3D e->blocks; if (e->blocks) { state->hdmi_edid.present |=3D 0x04 << e->pad; - memcpy(&state->hdmi_edid.edid, e->edid, 128 * e->blocks); + memcpy(&state->hdmi_edid.edid, e->edid, + EDID_BLOCK_SIZE * e->blocks); } else { state->hdmi_edid.present &=3D ~(0x04 << e->pad); adv7842_s_detect_tx_5v_ctrl(sd); --=20 2.31.1