From c0decac19da3906d9b66291e57b7759489e1170f Mon Sep 17 00:00:00 2001 From: Mauro Carvalho Chehab Date: Mon, 10 Sep 2018 08:19:14 -0400 Subject: media: use strscpy() instead of strlcpy() The implementation of strscpy() is more robust and safer. That's now the recommended way to copy NUL terminated strings. Signed-off-by: Mauro Carvalho Chehab Reviewed-by: Kees Cook Acked-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab --- drivers/media/v4l2-core/v4l2-common.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'drivers/media/v4l2-core/v4l2-common.c') diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c index b518b92d6d96..a443cfa050b6 100644 --- a/drivers/media/v4l2-core/v4l2-common.c +++ b/drivers/media/v4l2-core/v4l2-common.c @@ -100,7 +100,7 @@ int v4l2_ctrl_query_fill(struct v4l2_queryctrl *qctrl, s32 _min, s32 _max, s32 _ qctrl->step = step; qctrl->default_value = def; qctrl->reserved[0] = qctrl->reserved[1] = 0; - strlcpy(qctrl->name, name, sizeof(qctrl->name)); + strscpy(qctrl->name, name, sizeof(qctrl->name)); return 0; } EXPORT_SYMBOL(v4l2_ctrl_query_fill); @@ -186,7 +186,7 @@ struct v4l2_subdev *v4l2_i2c_new_subdev(struct v4l2_device *v4l2_dev, /* Setup the i2c board info with the device type and the device address. */ memset(&info, 0, sizeof(info)); - strlcpy(info.type, client_type, sizeof(info.type)); + strscpy(info.type, client_type, sizeof(info.type)); info.addr = addr; return v4l2_i2c_new_subdev_board(v4l2_dev, adapter, &info, probe_addrs); @@ -255,7 +255,7 @@ void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi, v4l2_set_subdevdata(sd, spi); spi_set_drvdata(spi, sd); /* initialize name */ - strlcpy(sd->name, spi->dev.driver->name, sizeof(sd->name)); + strscpy(sd->name, spi->dev.driver->name, sizeof(sd->name)); } EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init); -- cgit v1.2.3 From 447d66855391f702493cbbc8b8bdcc5a75d71f52 Mon Sep 17 00:00:00 2001 From: Philippe De Muyter Date: Wed, 1 Aug 2018 17:20:56 -0400 Subject: media: v4l2-common: v4l2_spi_subdev_init : generate unique name While v4l2_i2c_subdev_init does give a unique name to the subdev, matching the one appearing in dmesg for messages generated by dev_info and friends (e.g. imx185 30-0010), v4l2_spi_subdev_init does a poor job, copying only the driver name, but not the dev_name(), yielding e.g. "imx185", but missing the "spi1.1" part, and not generating a unique name. Fix that. Signed-off-by: Philippe De Muyter Acked-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab --- drivers/media/v4l2-core/v4l2-common.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'drivers/media/v4l2-core/v4l2-common.c') diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c index a443cfa050b6..7c755952398f 100644 --- a/drivers/media/v4l2-core/v4l2-common.c +++ b/drivers/media/v4l2-core/v4l2-common.c @@ -255,7 +255,8 @@ void v4l2_spi_subdev_init(struct v4l2_subdev *sd, struct spi_device *spi, v4l2_set_subdevdata(sd, spi); spi_set_drvdata(spi, sd); /* initialize name */ - strscpy(sd->name, spi->dev.driver->name, sizeof(sd->name)); + snprintf(sd->name, sizeof(sd->name), "%s %s", + spi->dev.driver->name, dev_name(&spi->dev)); } EXPORT_SYMBOL_GPL(v4l2_spi_subdev_init); -- cgit v1.2.3 From 0658293012af1e69d8bb8a25e71781470f9b2ac6 Mon Sep 17 00:00:00 2001 From: Sakari Ailus Date: Wed, 29 Aug 2018 05:52:46 -0400 Subject: media: v4l: subdev: Add a function to set an I²C sub-device's name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit v4l2_i2c_subdev_set_name() can be used to assign a name to a sub-device. This way uniform names can be formed easily without having to resort to things such as snprintf in drivers. Signed-off-by: Sakari Ailus Acked-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab --- drivers/media/v4l2-core/v4l2-common.c | 18 ++++++++++++++---- include/media/v4l2-common.h | 12 ++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) (limited to 'drivers/media/v4l2-core/v4l2-common.c') diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c index 7c755952398f..50763fb42a1b 100644 --- a/drivers/media/v4l2-core/v4l2-common.c +++ b/drivers/media/v4l2-core/v4l2-common.c @@ -109,6 +109,19 @@ EXPORT_SYMBOL(v4l2_ctrl_query_fill); #if IS_ENABLED(CONFIG_I2C) +void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client, + const char *devname, const char *postfix) +{ + if (!devname) + devname = client->dev.driver->name; + if (!postfix) + postfix = ""; + + snprintf(sd->name, sizeof(sd->name), "%s%s %d-%04x", devname, postfix, + i2c_adapter_id(client->adapter), client->addr); +} +EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_set_name); + void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, const struct v4l2_subdev_ops *ops) { @@ -120,10 +133,7 @@ void v4l2_i2c_subdev_init(struct v4l2_subdev *sd, struct i2c_client *client, /* i2c_client and v4l2_subdev point to one another */ v4l2_set_subdevdata(sd, client); i2c_set_clientdata(client, sd); - /* initialize name */ - snprintf(sd->name, sizeof(sd->name), "%s %d-%04x", - client->dev.driver->name, i2c_adapter_id(client->adapter), - client->addr); + v4l2_i2c_subdev_set_name(sd, client, NULL, NULL); } EXPORT_SYMBOL_GPL(v4l2_i2c_subdev_init); diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h index cdc87ec61e54..bd880a909ecf 100644 --- a/include/media/v4l2-common.h +++ b/include/media/v4l2-common.h @@ -154,6 +154,18 @@ struct v4l2_subdev *v4l2_i2c_new_subdev_board(struct v4l2_device *v4l2_dev, struct i2c_adapter *adapter, struct i2c_board_info *info, const unsigned short *probe_addrs); +/** + * v4l2_i2c_subdev_set_name - Set name for an I²C sub-device + * + * @sd: pointer to &struct v4l2_subdev + * @client: pointer to struct i2c_client + * @devname: the name of the device; if NULL, the I²C device's name will be used + * @postfix: sub-device specific string to put right after the I²C device name; + * may be NULL + */ +void v4l2_i2c_subdev_set_name(struct v4l2_subdev *sd, struct i2c_client *client, + const char *devname, const char *postfix); + /** * v4l2_i2c_subdev_init - Initializes a &struct v4l2_subdev with data from * an i2c_client struct. -- cgit v1.2.3