Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: staging/imx: remove confusing IS_ERR_OR_NULL usage

While looking at a compiler warning, I noticed the use of
IS_ERR_OR_NULL, which is generally a sign of a bad API design
and should be avoided.

In this driver, this is fairly easy, we can simply stop storing
error pointers in persistent structures, and change the two
functions that might return either a NULL pointer or an error
code to consistently return error pointers when failing.

of_parse_subdev() now separates the error code and the pointer
it looks up, to clarify the interface. There are two cases
where this function originally returns 'NULL', and I have
changed that to '0' for success to keep the current behavior,
though returning an error would also make sense there.

Fixes: e130291212df ("[media] media: Add i.MX media core driver")

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Philipp Zabel <p.zabel@pengutronix.de>
Tested-by: Philipp Zabel <p.zabel@pengutronix.de>
Reviewed-by: Steve Longerbeam <steve_longerbeam@mentor.com>
Tested-by: Steve Longerbeam <steve_longerbeam@mentor.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>

authored by

Arnd Bergmann and committed by
Mauro Carvalho Chehab
0b2e9e79 05ad2b6d

+90 -72
+22 -19
drivers/staging/media/imx/imx-ic-prpencvf.c
··· 134 134 135 135 static void prp_put_ipu_resources(struct prp_priv *priv) 136 136 { 137 - if (!IS_ERR_OR_NULL(priv->ic)) 137 + if (priv->ic) 138 138 ipu_ic_put(priv->ic); 139 139 priv->ic = NULL; 140 140 141 - if (!IS_ERR_OR_NULL(priv->out_ch)) 141 + if (priv->out_ch) 142 142 ipu_idmac_put(priv->out_ch); 143 143 priv->out_ch = NULL; 144 144 145 - if (!IS_ERR_OR_NULL(priv->rot_in_ch)) 145 + if (priv->rot_in_ch) 146 146 ipu_idmac_put(priv->rot_in_ch); 147 147 priv->rot_in_ch = NULL; 148 148 149 - if (!IS_ERR_OR_NULL(priv->rot_out_ch)) 149 + if (priv->rot_out_ch) 150 150 ipu_idmac_put(priv->rot_out_ch); 151 151 priv->rot_out_ch = NULL; 152 152 } ··· 154 154 static int prp_get_ipu_resources(struct prp_priv *priv) 155 155 { 156 156 struct imx_ic_priv *ic_priv = priv->ic_priv; 157 + struct ipu_ic *ic; 158 + struct ipuv3_channel *out_ch, *rot_in_ch, *rot_out_ch; 157 159 int ret, task = ic_priv->task_id; 158 160 159 161 priv->ipu = priv->md->ipu[ic_priv->ipu_id]; 160 162 161 - priv->ic = ipu_ic_get(priv->ipu, task); 162 - if (IS_ERR(priv->ic)) { 163 + ic = ipu_ic_get(priv->ipu, task); 164 + if (IS_ERR(ic)) { 163 165 v4l2_err(&ic_priv->sd, "failed to get IC\n"); 164 - ret = PTR_ERR(priv->ic); 166 + ret = PTR_ERR(ic); 165 167 goto out; 166 168 } 169 + priv->ic = ic; 167 170 168 - priv->out_ch = ipu_idmac_get(priv->ipu, 169 - prp_channel[task].out_ch); 170 - if (IS_ERR(priv->out_ch)) { 171 + out_ch = ipu_idmac_get(priv->ipu, prp_channel[task].out_ch); 172 + if (IS_ERR(out_ch)) { 171 173 v4l2_err(&ic_priv->sd, "could not get IDMAC channel %u\n", 172 174 prp_channel[task].out_ch); 173 - ret = PTR_ERR(priv->out_ch); 175 + ret = PTR_ERR(out_ch); 174 176 goto out; 175 177 } 178 + priv->out_ch = out_ch; 176 179 177 - priv->rot_in_ch = ipu_idmac_get(priv->ipu, 178 - prp_channel[task].rot_in_ch); 179 - if (IS_ERR(priv->rot_in_ch)) { 180 + rot_in_ch = ipu_idmac_get(priv->ipu, prp_channel[task].rot_in_ch); 181 + if (IS_ERR(rot_in_ch)) { 180 182 v4l2_err(&ic_priv->sd, "could not get IDMAC channel %u\n", 181 183 prp_channel[task].rot_in_ch); 182 - ret = PTR_ERR(priv->rot_in_ch); 184 + ret = PTR_ERR(rot_in_ch); 183 185 goto out; 184 186 } 187 + priv->rot_in_ch = rot_in_ch; 185 188 186 - priv->rot_out_ch = ipu_idmac_get(priv->ipu, 187 - prp_channel[task].rot_out_ch); 188 - if (IS_ERR(priv->rot_out_ch)) { 189 + rot_out_ch = ipu_idmac_get(priv->ipu, prp_channel[task].rot_out_ch); 190 + if (IS_ERR(rot_out_ch)) { 189 191 v4l2_err(&ic_priv->sd, "could not get IDMAC channel %u\n", 190 192 prp_channel[task].rot_out_ch); 191 - ret = PTR_ERR(priv->rot_out_ch); 193 + ret = PTR_ERR(rot_out_ch); 192 194 goto out; 193 195 } 196 + priv->rot_out_ch = rot_out_ch; 194 197 195 198 return 0; 196 199 out:
+18 -12
drivers/staging/media/imx/imx-media-csi.c
··· 122 122 123 123 static void csi_idmac_put_ipu_resources(struct csi_priv *priv) 124 124 { 125 - if (!IS_ERR_OR_NULL(priv->idmac_ch)) 125 + if (priv->idmac_ch) 126 126 ipu_idmac_put(priv->idmac_ch); 127 127 priv->idmac_ch = NULL; 128 128 129 - if (!IS_ERR_OR_NULL(priv->smfc)) 129 + if (priv->smfc) 130 130 ipu_smfc_put(priv->smfc); 131 131 priv->smfc = NULL; 132 132 } ··· 134 134 static int csi_idmac_get_ipu_resources(struct csi_priv *priv) 135 135 { 136 136 int ch_num, ret; 137 + struct ipu_smfc *smfc; 138 + struct ipuv3_channel *idmac_ch; 137 139 138 140 ch_num = IPUV3_CHANNEL_CSI0 + priv->smfc_id; 139 141 140 - priv->smfc = ipu_smfc_get(priv->ipu, ch_num); 141 - if (IS_ERR(priv->smfc)) { 142 + smfc = ipu_smfc_get(priv->ipu, ch_num); 143 + if (IS_ERR(smfc)) { 142 144 v4l2_err(&priv->sd, "failed to get SMFC\n"); 143 - ret = PTR_ERR(priv->smfc); 145 + ret = PTR_ERR(smfc); 144 146 goto out; 145 147 } 148 + priv->smfc = smfc; 146 149 147 - priv->idmac_ch = ipu_idmac_get(priv->ipu, ch_num); 148 - if (IS_ERR(priv->idmac_ch)) { 150 + idmac_ch = ipu_idmac_get(priv->ipu, ch_num); 151 + if (IS_ERR(idmac_ch)) { 149 152 v4l2_err(&priv->sd, "could not get IDMAC channel %u\n", 150 153 ch_num); 151 - ret = PTR_ERR(priv->idmac_ch); 154 + ret = PTR_ERR(idmac_ch); 152 155 goto out; 153 156 } 157 + priv->idmac_ch = idmac_ch; 154 158 155 159 return 0; 156 160 out: ··· 1589 1585 static int csi_registered(struct v4l2_subdev *sd) 1590 1586 { 1591 1587 struct csi_priv *priv = v4l2_get_subdevdata(sd); 1588 + struct ipu_csi *csi; 1592 1589 int i, ret; 1593 1590 u32 code; 1594 1591 ··· 1597 1592 priv->md = dev_get_drvdata(sd->v4l2_dev->dev); 1598 1593 1599 1594 /* get handle to IPU CSI */ 1600 - priv->csi = ipu_csi_get(priv->ipu, priv->csi_id); 1601 - if (IS_ERR(priv->csi)) { 1595 + csi = ipu_csi_get(priv->ipu, priv->csi_id); 1596 + if (IS_ERR(csi)) { 1602 1597 v4l2_err(&priv->sd, "failed to get CSI%d\n", priv->csi_id); 1603 - return PTR_ERR(priv->csi); 1598 + return PTR_ERR(csi); 1604 1599 } 1600 + priv->csi = csi; 1605 1601 1606 1602 for (i = 0; i < CSI_NUM_PADS; i++) { 1607 1603 priv->pad[i].flags = (i == CSI_SINK_PAD) ? ··· 1671 1665 if (priv->fim) 1672 1666 imx_media_fim_free(priv->fim); 1673 1667 1674 - if (!IS_ERR_OR_NULL(priv->csi)) 1668 + if (priv->csi) 1675 1669 ipu_csi_put(priv->csi); 1676 1670 } 1677 1671
+2 -2
drivers/staging/media/imx/imx-media-dev.c
··· 87 87 if (pdev) 88 88 devname = dev_name(&pdev->dev); 89 89 90 - /* return NULL if this subdev already added */ 90 + /* return -EEXIST if this subdev already added */ 91 91 if (imx_media_find_async_subdev(imxmd, np, devname)) { 92 92 dev_dbg(imxmd->md.dev, "%s: already added %s\n", 93 93 __func__, np ? np->name : devname); 94 - imxsd = NULL; 94 + imxsd = ERR_PTR(-EEXIST); 95 95 goto out; 96 96 } 97 97
+28 -22
drivers/staging/media/imx/imx-media-of.c
··· 100 100 } 101 101 } 102 102 103 - static struct imx_media_subdev * 103 + static int 104 104 of_parse_subdev(struct imx_media_dev *imxmd, struct device_node *sd_np, 105 - bool is_csi_port) 105 + bool is_csi_port, struct imx_media_subdev **subdev) 106 106 { 107 107 struct imx_media_subdev *imxsd; 108 108 int i, num_pads, ret; ··· 110 110 if (!of_device_is_available(sd_np)) { 111 111 dev_dbg(imxmd->md.dev, "%s: %s not enabled\n", __func__, 112 112 sd_np->name); 113 - return NULL; 113 + *subdev = NULL; 114 + /* unavailable is not an error */ 115 + return 0; 114 116 } 115 117 116 118 /* register this subdev with async notifier */ 117 119 imxsd = imx_media_add_async_subdev(imxmd, sd_np, NULL); 118 - if (IS_ERR_OR_NULL(imxsd)) 119 - return imxsd; 120 + ret = PTR_ERR_OR_ZERO(imxsd); 121 + if (ret) { 122 + if (ret == -EEXIST) { 123 + /* already added, everything is fine */ 124 + *subdev = NULL; 125 + return 0; 126 + } 127 + 128 + /* other error, can't continue */ 129 + return ret; 130 + } 131 + *subdev = imxsd; 120 132 121 133 if (is_csi_port) { 122 134 /* ··· 149 137 } else { 150 138 num_pads = of_get_port_count(sd_np); 151 139 if (num_pads != 1) { 140 + /* confused, but no reason to give up here */ 152 141 dev_warn(imxmd->md.dev, 153 142 "%s: unknown device %s with %d ports\n", 154 143 __func__, sd_np->name, num_pads); 155 - return NULL; 144 + return 0; 156 145 } 157 146 158 147 /* ··· 164 151 } 165 152 166 153 if (imxsd->num_sink_pads >= num_pads) 167 - return ERR_PTR(-EINVAL); 154 + return -EINVAL; 168 155 169 156 imxsd->num_src_pads = num_pads - imxsd->num_sink_pads; 170 157 ··· 204 191 205 192 ret = of_add_pad_link(imxmd, pad, sd_np, remote_np, 206 193 i, remote_pad); 207 - if (ret) { 208 - imxsd = ERR_PTR(ret); 194 + if (ret) 209 195 break; 210 - } 211 196 212 197 if (i < imxsd->num_sink_pads) { 213 198 /* follow sink endpoints upstream */ 214 - remote_imxsd = of_parse_subdev(imxmd, 215 - remote_np, 216 - false); 217 - if (IS_ERR(remote_imxsd)) { 218 - imxsd = remote_imxsd; 199 + ret = of_parse_subdev(imxmd, remote_np, 200 + false, &remote_imxsd); 201 + if (ret) 219 202 break; 220 - } 221 203 } 222 204 223 205 of_node_put(remote_np); ··· 220 212 221 213 if (port != sd_np) 222 214 of_node_put(port); 223 - if (IS_ERR(imxsd)) { 215 + if (ret) { 224 216 of_node_put(remote_np); 225 217 of_node_put(epnode); 226 218 break; 227 219 } 228 220 } 229 221 230 - return imxsd; 222 + return ret; 231 223 } 232 224 233 225 int imx_media_of_parse(struct imx_media_dev *imxmd, ··· 244 236 if (!csi_np) 245 237 break; 246 238 247 - lcsi = of_parse_subdev(imxmd, csi_np, true); 248 - if (IS_ERR(lcsi)) { 249 - ret = PTR_ERR(lcsi); 239 + ret = of_parse_subdev(imxmd, csi_np, true, &lcsi); 240 + if (ret) 250 241 goto err_put; 251 - } 252 242 253 243 ret = of_property_read_u32(csi_np, "reg", &csi_id); 254 244 if (ret) {
+20 -17
drivers/staging/media/imx/imx-media-vdic.c
··· 126 126 127 127 static void vdic_put_ipu_resources(struct vdic_priv *priv) 128 128 { 129 - if (!IS_ERR_OR_NULL(priv->vdi_in_ch_p)) 129 + if (priv->vdi_in_ch_p) 130 130 ipu_idmac_put(priv->vdi_in_ch_p); 131 131 priv->vdi_in_ch_p = NULL; 132 132 133 - if (!IS_ERR_OR_NULL(priv->vdi_in_ch)) 133 + if (priv->vdi_in_ch) 134 134 ipu_idmac_put(priv->vdi_in_ch); 135 135 priv->vdi_in_ch = NULL; 136 136 137 - if (!IS_ERR_OR_NULL(priv->vdi_in_ch_n)) 137 + if (priv->vdi_in_ch_n) 138 138 ipu_idmac_put(priv->vdi_in_ch_n); 139 139 priv->vdi_in_ch_n = NULL; 140 140 ··· 146 146 static int vdic_get_ipu_resources(struct vdic_priv *priv) 147 147 { 148 148 int ret, err_chan; 149 + struct ipuv3_channel *ch; 150 + struct ipu_vdi *vdi; 149 151 150 152 priv->ipu = priv->md->ipu[priv->ipu_id]; 151 153 152 - priv->vdi = ipu_vdi_get(priv->ipu); 153 - if (IS_ERR(priv->vdi)) { 154 + vdi = ipu_vdi_get(priv->ipu); 155 + if (IS_ERR(vdi)) { 154 156 v4l2_err(&priv->sd, "failed to get VDIC\n"); 155 - ret = PTR_ERR(priv->vdi); 157 + ret = PTR_ERR(vdi); 156 158 goto out; 157 159 } 160 + priv->vdi = vdi; 158 161 159 162 if (!priv->csi_direct) { 160 - priv->vdi_in_ch_p = ipu_idmac_get(priv->ipu, 161 - IPUV3_CHANNEL_MEM_VDI_PREV); 162 - if (IS_ERR(priv->vdi_in_ch_p)) { 163 + ch = ipu_idmac_get(priv->ipu, IPUV3_CHANNEL_MEM_VDI_PREV); 164 + if (IS_ERR(ch)) { 163 165 err_chan = IPUV3_CHANNEL_MEM_VDI_PREV; 164 - ret = PTR_ERR(priv->vdi_in_ch_p); 166 + ret = PTR_ERR(ch); 165 167 goto out_err_chan; 166 168 } 169 + priv->vdi_in_ch_p = ch; 167 170 168 - priv->vdi_in_ch = ipu_idmac_get(priv->ipu, 169 - IPUV3_CHANNEL_MEM_VDI_CUR); 170 - if (IS_ERR(priv->vdi_in_ch)) { 171 + ch = ipu_idmac_get(priv->ipu, IPUV3_CHANNEL_MEM_VDI_CUR); 172 + if (IS_ERR(ch)) { 171 173 err_chan = IPUV3_CHANNEL_MEM_VDI_CUR; 172 - ret = PTR_ERR(priv->vdi_in_ch); 174 + ret = PTR_ERR(ch); 173 175 goto out_err_chan; 174 176 } 177 + priv->vdi_in_ch = ch; 175 178 176 - priv->vdi_in_ch_n = ipu_idmac_get(priv->ipu, 177 - IPUV3_CHANNEL_MEM_VDI_NEXT); 179 + ch = ipu_idmac_get(priv->ipu, IPUV3_CHANNEL_MEM_VDI_NEXT); 178 180 if (IS_ERR(priv->vdi_in_ch_n)) { 179 181 err_chan = IPUV3_CHANNEL_MEM_VDI_NEXT; 180 - ret = PTR_ERR(priv->vdi_in_ch_n); 182 + ret = PTR_ERR(ch); 181 183 goto out_err_chan; 182 184 } 185 + priv->vdi_in_ch_n = ch; 183 186 } 184 187 185 188 return 0;