linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] media: si2168: Refactor command setup code
@ 2019-07-01 11:44 Marc Gonzalez
  2019-07-02  8:37 ` Marc Gonzalez
  2019-07-02  9:51 ` Jonathan Neuschäfer
  0 siblings, 2 replies; 6+ messages in thread
From: Marc Gonzalez @ 2019-07-01 11:44 UTC (permalink / raw)
  To: Antti Palosaari, Mauro Carvalho Chehab
  Cc: linux-media, LKML, Jonathan Neuschäfer, Bjorn Andersson, Brad Love

By refactoring the command setup code, we can let the compiler
determine the size of each command.

Signed-off-by: Marc Gonzalez <marc.w.gonzalez@free.fr>
---
 drivers/media/dvb-frontends/si2168.c | 142 ++++++++-------------------
 1 file changed, 41 insertions(+), 101 deletions(-)

diff --git a/drivers/media/dvb-frontends/si2168.c b/drivers/media/dvb-frontends/si2168.c
index 168c503e9154..19398f041c79 100644
--- a/drivers/media/dvb-frontends/si2168.c
+++ b/drivers/media/dvb-frontends/si2168.c
@@ -11,6 +11,12 @@
 
 static const struct dvb_frontend_ops si2168_ops;
 
+#define CMD_SETUP(cmd, __args, __rlen) do {	\
+	int wlen = sizeof(__args) - 1;		\
+	memcpy(cmd.args, __args, wlen);		\
+	cmd.wlen = wlen; cmd.rlen = __rlen;	\
+} while (0)
+
 /* execute firmware command */
 static int si2168_cmd_execute(struct i2c_client *client, struct si2168_cmd *cmd)
 {
@@ -83,15 +89,13 @@ static int si2168_ts_bus_ctrl(struct dvb_frontend *fe, int acquire)
 	dev_dbg(&client->dev, "%s acquire: %d\n", __func__, acquire);
 
 	/* set TS_MODE property */
-	memcpy(cmd.args, "\x14\x00\x01\x10\x10\x00", 6);
+	CMD_SETUP(cmd, "\x14\x00\x01\x10\x10\x00", 4);
 	if (acquire)
 		cmd.args[4] |= dev->ts_mode;
 	else
 		cmd.args[4] |= SI2168_TS_TRISTATE;
 	if (dev->ts_clock_gapped)
 		cmd.args[4] |= 0x40;
-	cmd.wlen = 6;
-	cmd.rlen = 4;
 	ret = si2168_cmd_execute(client, &cmd);
 
 	return ret;
@@ -115,19 +119,13 @@ static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
 
 	switch (c->delivery_system) {
 	case SYS_DVBT:
-		memcpy(cmd.args, "\xa0\x01", 2);
-		cmd.wlen = 2;
-		cmd.rlen = 13;
+		CMD_SETUP(cmd, "\xa0\x01", 13);
 		break;
 	case SYS_DVBC_ANNEX_A:
-		memcpy(cmd.args, "\x90\x01", 2);
-		cmd.wlen = 2;
-		cmd.rlen = 9;
+		CMD_SETUP(cmd, "\x90\x01", 9);
 		break;
 	case SYS_DVBT2:
-		memcpy(cmd.args, "\x50\x01", 2);
-		cmd.wlen = 2;
-		cmd.rlen = 14;
+		CMD_SETUP(cmd, "\x50\x01", 14);
 		break;
 	default:
 		ret = -EINVAL;
@@ -164,9 +162,7 @@ static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
 
 	/* BER */
 	if (*status & FE_HAS_VITERBI) {
-		memcpy(cmd.args, "\x82\x00", 2);
-		cmd.wlen = 2;
-		cmd.rlen = 3;
+		CMD_SETUP(cmd, "\x82\x00", 3);
 		ret = si2168_cmd_execute(client, &cmd);
 		if (ret)
 			goto err;
@@ -197,9 +193,7 @@ static int si2168_read_status(struct dvb_frontend *fe, enum fe_status *status)
 
 	/* UCB */
 	if (*status & FE_HAS_SYNC) {
-		memcpy(cmd.args, "\x84\x01", 2);
-		cmd.wlen = 2;
-		cmd.rlen = 3;
+		CMD_SETUP(cmd, "\x84\x01", 3);
 		ret = si2168_cmd_execute(client, &cmd);
 		if (ret)
 			goto err;
@@ -285,22 +279,18 @@ static int si2168_set_frontend(struct dvb_frontend *fe)
 			goto err;
 	}
 
-	memcpy(cmd.args, "\x88\x02\x02\x02\x02", 5);
-	cmd.wlen = 5;
-	cmd.rlen = 5;
+	CMD_SETUP(cmd, "\x88\x02\x02\x02\x02", 5);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
 	/* that has no big effect */
 	if (c->delivery_system == SYS_DVBT)
-		memcpy(cmd.args, "\x89\x21\x06\x11\xff\x98", 6);
+		CMD_SETUP(cmd, "\x89\x21\x06\x11\xff\x98", 3);
 	else if (c->delivery_system == SYS_DVBC_ANNEX_A)
-		memcpy(cmd.args, "\x89\x21\x06\x11\x89\xf0", 6);
+		CMD_SETUP(cmd, "\x89\x21\x06\x11\x89\xf0", 3);
 	else if (c->delivery_system == SYS_DVBT2)
-		memcpy(cmd.args, "\x89\x21\x06\x11\x89\x20", 6);
-	cmd.wlen = 6;
-	cmd.rlen = 3;
+		CMD_SETUP(cmd, "\x89\x21\x06\x11\x89\x20", 3);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
@@ -317,103 +307,77 @@ static int si2168_set_frontend(struct dvb_frontend *fe)
 			goto err;
 	}
 
-	memcpy(cmd.args, "\x51\x03", 2);
-	cmd.wlen = 2;
-	cmd.rlen = 12;
+	CMD_SETUP(cmd, "\x51\x03", 12);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x12\x08\x04", 3);
-	cmd.wlen = 3;
-	cmd.rlen = 3;
+	CMD_SETUP(cmd, "\x12\x08\x04", 3);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x0c\x10\x12\x00", 6);
-	cmd.wlen = 6;
-	cmd.rlen = 4;
+	CMD_SETUP(cmd, "\x14\x00\x0c\x10\x12\x00", 4);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x06\x10\x24\x00", 6);
-	cmd.wlen = 6;
-	cmd.rlen = 4;
+	CMD_SETUP(cmd, "\x14\x00\x06\x10\x24\x00", 4);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x07\x10\x00\x24", 6);
-	cmd.wlen = 6;
-	cmd.rlen = 4;
+	CMD_SETUP(cmd, "\x14\x00\x07\x10\x00\x24", 4);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x0a\x10\x00\x00", 6);
+	CMD_SETUP(cmd, "\x14\x00\x0a\x10\x00\x00", 4);
 	cmd.args[4] = delivery_system | bandwidth;
 	if (dev->spectral_inversion)
 		cmd.args[5] |= 1;
-	cmd.wlen = 6;
-	cmd.rlen = 4;
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
 	/* set DVB-C symbol rate */
 	if (c->delivery_system == SYS_DVBC_ANNEX_A) {
-		memcpy(cmd.args, "\x14\x00\x02\x11", 4);
+		CMD_SETUP(cmd, "\x14\x00\x02\x11\x00\x00", 4);
 		cmd.args[4] = ((c->symbol_rate / 1000) >> 0) & 0xff;
 		cmd.args[5] = ((c->symbol_rate / 1000) >> 8) & 0xff;
-		cmd.wlen = 6;
-		cmd.rlen = 4;
 		ret = si2168_cmd_execute(client, &cmd);
 		if (ret)
 			goto err;
 	}
 
-	memcpy(cmd.args, "\x14\x00\x0f\x10\x10\x00", 6);
-	cmd.wlen = 6;
-	cmd.rlen = 4;
+	CMD_SETUP(cmd, "\x14\x00\x0f\x10\x10\x00", 4);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x09\x10\xe3\x08", 6);
+	CMD_SETUP(cmd, "\x14\x00\x09\x10\xe3\x08", 4);
 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
-	cmd.wlen = 6;
-	cmd.rlen = 4;
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x08\x10\xd7\x05", 6);
+	CMD_SETUP(cmd, "\x14\x00\x08\x10\xd7\x05", 4);
 	cmd.args[5] |= dev->ts_clock_inv ? 0x00 : 0x10;
-	cmd.wlen = 6;
-	cmd.rlen = 4;
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x01\x12\x00\x00", 6);
-	cmd.wlen = 6;
-	cmd.rlen = 4;
+	CMD_SETUP(cmd, "\x14\x00\x01\x12\x00\x00", 4);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x14\x00\x01\x03\x0c\x00", 6);
-	cmd.wlen = 6;
-	cmd.rlen = 4;
+	CMD_SETUP(cmd, "\x14\x00\x01\x03\x0c\x00", 4);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
-	memcpy(cmd.args, "\x85", 1);
-	cmd.wlen = 1;
-	cmd.rlen = 1;
+	CMD_SETUP(cmd, "\x85", 1);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
@@ -443,26 +407,20 @@ static int si2168_init(struct dvb_frontend *fe)
 	dev_dbg(&client->dev, "\n");
 
 	/* initialize */
-	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
-	cmd.wlen = 13;
-	cmd.rlen = 0;
+	CMD_SETUP(cmd, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 0);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
 	if (dev->warm) {
 		/* resume */
-		memcpy(cmd.args, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 8);
-		cmd.wlen = 8;
-		cmd.rlen = 1;
+		CMD_SETUP(cmd, "\xc0\x06\x08\x0f\x00\x20\x21\x01", 1);
 		ret = si2168_cmd_execute(client, &cmd);
 		if (ret)
 			goto err;
 
 		udelay(100);
-		memcpy(cmd.args, "\x85", 1);
-		cmd.wlen = 1;
-		cmd.rlen = 1;
+		CMD_SETUP(cmd, "\x85", 1);
 		ret = si2168_cmd_execute(client, &cmd);
 		if (ret)
 			goto err;
@@ -471,9 +429,7 @@ static int si2168_init(struct dvb_frontend *fe)
 	}
 
 	/* power up */
-	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
-	cmd.wlen = 8;
-	cmd.rlen = 1;
+	CMD_SETUP(cmd, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 1);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
@@ -541,17 +497,13 @@ static int si2168_init(struct dvb_frontend *fe)
 
 	release_firmware(fw);
 
-	memcpy(cmd.args, "\x01\x01", 2);
-	cmd.wlen = 2;
-	cmd.rlen = 1;
+	CMD_SETUP(cmd, "\x01\x01", 1);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
 
 	/* query firmware version */
-	memcpy(cmd.args, "\x11", 1);
-	cmd.wlen = 1;
-	cmd.rlen = 10;
+	CMD_SETUP(cmd, "\x11", 10);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
@@ -609,9 +561,7 @@ static int si2168_sleep(struct dvb_frontend *fe)
 	if (dev->version > ('B' << 24 | 4 << 16 | 0 << 8 | 11 << 0))
 		dev->warm = false;
 
-	memcpy(cmd.args, "\x13", 1);
-	cmd.wlen = 1;
-	cmd.rlen = 0;
+	CMD_SETUP(cmd, "\x13", 0);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
@@ -637,9 +587,7 @@ static int si2168_select(struct i2c_mux_core *muxc, u32 chan)
 	struct si2168_cmd cmd;
 
 	/* open I2C gate */
-	memcpy(cmd.args, "\xc0\x0d\x01", 3);
-	cmd.wlen = 3;
-	cmd.rlen = 0;
+	CMD_SETUP(cmd, "\xc0\x0d\x01", 0);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
@@ -657,9 +605,7 @@ static int si2168_deselect(struct i2c_mux_core *muxc, u32 chan)
 	struct si2168_cmd cmd;
 
 	/* close I2C gate */
-	memcpy(cmd.args, "\xc0\x0d\x00", 3);
-	cmd.wlen = 3;
-	cmd.rlen = 0;
+	CMD_SETUP(cmd, "\xc0\x0d\x00", 0);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err;
@@ -730,25 +676,19 @@ static int si2168_probe(struct i2c_client *client,
 	mutex_init(&dev->i2c_mutex);
 
 	/* Initialize */
-	memcpy(cmd.args, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 13);
-	cmd.wlen = 13;
-	cmd.rlen = 0;
+	CMD_SETUP(cmd, "\xc0\x12\x00\x0c\x00\x0d\x16\x00\x00\x00\x00\x00\x00", 0);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err_kfree;
 
 	/* Power up */
-	memcpy(cmd.args, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 8);
-	cmd.wlen = 8;
-	cmd.rlen = 1;
+	CMD_SETUP(cmd, "\xc0\x06\x01\x0f\x00\x20\x20\x01", 1);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err_kfree;
 
 	/* Query chip revision */
-	memcpy(cmd.args, "\x02", 1);
-	cmd.wlen = 1;
-	cmd.rlen = 13;
+	CMD_SETUP(cmd, "\x02", 13);
 	ret = si2168_cmd_execute(client, &cmd);
 	if (ret)
 		goto err_kfree;
-- 
2.17.1

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] media: si2168: Refactor command setup code
  2019-07-01 11:44 [PATCH v1] media: si2168: Refactor command setup code Marc Gonzalez
@ 2019-07-02  8:37 ` Marc Gonzalez
  2019-07-02  9:51 ` Jonathan Neuschäfer
  1 sibling, 0 replies; 6+ messages in thread
From: Marc Gonzalez @ 2019-07-02  8:37 UTC (permalink / raw)
  To: Antti Palosaari, Mauro Carvalho Chehab
  Cc: linux-media, LKML, Jonathan Neuschäfer, Bjorn Andersson, Brad Love

On 01/07/2019 13:44, Marc Gonzalez wrote:

> By refactoring the command setup code, we can let the compiler
> determine the size of each command.
> 
> Signed-off-by: Marc Gonzalez <marc.w.gonzalez@free.fr>
> ---
>  drivers/media/dvb-frontends/si2168.c | 142 ++++++++-------------------
>  1 file changed, 41 insertions(+), 101 deletions(-)
> 
> diff --git a/drivers/media/dvb-frontends/si2168.c b/drivers/media/dvb-frontends/si2168.c
> index 168c503e9154..19398f041c79 100644
> --- a/drivers/media/dvb-frontends/si2168.c
> +++ b/drivers/media/dvb-frontends/si2168.c
> @@ -11,6 +11,12 @@
>  
>  static const struct dvb_frontend_ops si2168_ops;
>  
> +#define CMD_SETUP(cmd, __args, __rlen) do {	\
> +	int wlen = sizeof(__args) - 1;		\
> +	memcpy(cmd.args, __args, wlen);		\
> +	cmd.wlen = wlen; cmd.rlen = __rlen;	\
> +} while (0)
> +

I'm planning on sending a v2 where drivers/media/tuners/si2157.c
is refactored the same way. Not sure where to store the macro.
Maybe include/media/dvb_frontend.h ?

Regards.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] media: si2168: Refactor command setup code
  2019-07-01 11:44 [PATCH v1] media: si2168: Refactor command setup code Marc Gonzalez
  2019-07-02  8:37 ` Marc Gonzalez
@ 2019-07-02  9:51 ` Jonathan Neuschäfer
  2019-07-03 12:47   ` Marc Gonzalez
  1 sibling, 1 reply; 6+ messages in thread
From: Jonathan Neuschäfer @ 2019-07-02  9:51 UTC (permalink / raw)
  To: Marc Gonzalez
  Cc: Antti Palosaari, Mauro Carvalho Chehab, linux-media, LKML,
	Jonathan Neuschäfer, Bjorn Andersson, Brad Love

[-- Attachment #1: Type: text/plain, Size: 1830 bytes --]

Hi,

On Mon, Jul 01, 2019 at 01:44:09PM +0200, Marc Gonzalez wrote:
> By refactoring the command setup code, we can let the compiler
> determine the size of each command.

I like the idea, it definitely saves some code.

The conversion also looks correct.

> Signed-off-by: Marc Gonzalez <marc.w.gonzalez@free.fr>
> ---
>  drivers/media/dvb-frontends/si2168.c | 142 ++++++++-------------------
>  1 file changed, 41 insertions(+), 101 deletions(-)
> 
> diff --git a/drivers/media/dvb-frontends/si2168.c b/drivers/media/dvb-frontends/si2168.c
> index 168c503e9154..19398f041c79 100644
> --- a/drivers/media/dvb-frontends/si2168.c
> +++ b/drivers/media/dvb-frontends/si2168.c
> @@ -11,6 +11,12 @@
>  
>  static const struct dvb_frontend_ops si2168_ops;
>  
> +#define CMD_SETUP(cmd, __args, __rlen) do {	\
> +	int wlen = sizeof(__args) - 1;		\
> +	memcpy(cmd.args, __args, wlen);		\
> +	cmd.wlen = wlen; cmd.rlen = __rlen;	\
> +} while (0)

It would be nice for casual readers to have a little comment here, that
explains (briefly) what this macro does, and what the arguments mean,
and their types.

Why cmd rather than __cmd? This seems inconsistent.

The wlen local variable can be avoided by a bit of suffling:

	#define CMD_SETUP(cmd, __args, __rlen) do {	\
		cmd.rlen = __rlen;			\
		cmd.wlen = sizeof(__args) - 1;		\
		memcpy(cmd.args, __args, cmd.wlen);	\
	} while (0)


[from the other mail]
> I'm planning on sending a v2 where drivers/media/tuners/si2157.c
> is refactored the same way.

Makes sense.

> Not sure where to store the macro. Maybe include/media/dvb_frontend.h?

Then include/media/dvb_frontend.h would contain information about the
private structs of a few (two) drivers. This doesn't seem like a good
idea to me.


Greetings,
Jonathan Neuschäfer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] media: si2168: Refactor command setup code
  2019-07-02  9:51 ` Jonathan Neuschäfer
@ 2019-07-03 12:47   ` Marc Gonzalez
  2019-07-03 14:15     ` Marc Gonzalez
  2019-07-03 16:35     ` Jonathan Neuschäfer
  0 siblings, 2 replies; 6+ messages in thread
From: Marc Gonzalez @ 2019-07-03 12:47 UTC (permalink / raw)
  To: Jonathan Neuschäfer
  Cc: Antti Palosaari, Mauro Carvalho Chehab, linux-media, LKML,
	Bjorn Andersson, Brad Love

On 02/07/2019 11:51, Jonathan Neuschäfer wrote:

> On Mon, Jul 01, 2019 at 01:44:09PM +0200, Marc Gonzalez wrote:
>
>> By refactoring the command setup code, we can let the compiler
>> determine the size of each command.
> 
> I like the idea, it definitely saves some code.
> 
> The conversion also looks correct.
> 
>> Signed-off-by: Marc Gonzalez <marc.w.gonzalez@free.fr>
>> ---
>>  drivers/media/dvb-frontends/si2168.c | 142 ++++++++-------------------
>>  1 file changed, 41 insertions(+), 101 deletions(-)
>>
>> diff --git a/drivers/media/dvb-frontends/si2168.c b/drivers/media/dvb-frontends/si2168.c
>> index 168c503e9154..19398f041c79 100644
>> --- a/drivers/media/dvb-frontends/si2168.c
>> +++ b/drivers/media/dvb-frontends/si2168.c
>> @@ -11,6 +11,12 @@
>>  
>>  static const struct dvb_frontend_ops si2168_ops;
>>  
>> +#define CMD_SETUP(cmd, __args, __rlen) do {	\
>> +	int wlen = sizeof(__args) - 1;		\
>> +	memcpy(cmd.args, __args, wlen);		\
>> +	cmd.wlen = wlen; cmd.rlen = __rlen;	\
>> +} while (0)
> 
> It would be nice for casual readers to have a little comment here, that
> explains (briefly) what this macro does, and what the arguments mean,
> and their types.

Just a bit of background.

A macro is required /at some point/ because arrays "decay" into pointers
when used as function arguments.

Come to think of it, I'm really not a fan of "large" macro functions.
I'll outline a different option in v2.


> Why cmd rather than __cmd? This seems inconsistent.

Note: I hate using underscores in macro argument names, but they clashed
with the struct field names. There was no such clash for 'cmd'.


> The wlen local variable can be avoided by a bit of suffling:
> 
> 	#define CMD_SETUP(cmd, __args, __rlen) do {	\
> 		cmd.rlen = __rlen;			\
> 		cmd.wlen = sizeof(__args) - 1;		\
> 		memcpy(cmd.args, __args, cmd.wlen);	\
> 	} while (0)

Do you think it is important to avoid a local variable?


>> Not sure where to store the macro. Maybe include/media/dvb_frontend.h?
> 
> Then include/media/dvb_frontend.h would contain information about the
> private structs of a few (two) drivers. This doesn't seem like a good
> idea to me.

You're right. I found a better place.

Regards.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] media: si2168: Refactor command setup code
  2019-07-03 12:47   ` Marc Gonzalez
@ 2019-07-03 14:15     ` Marc Gonzalez
  2019-07-03 16:35     ` Jonathan Neuschäfer
  1 sibling, 0 replies; 6+ messages in thread
From: Marc Gonzalez @ 2019-07-03 14:15 UTC (permalink / raw)
  To: Jonathan Neuschäfer, Antti Palosaari, Mauro Carvalho Chehab
  Cc: linux-media, LKML, Bjorn Andersson, Brad Love

On 03/07/2019 14:47, Marc Gonzalez wrote:

> Come to think of it, I'm really not a fan of "large" macro functions.
> I'll outline a different option in v2.

My idea is to use a trivial helper function to assign the 3 struct fields,
and then a trivial macro to invoke the helper:

static void cmd_setup(struct si2168_cmd *cmd, char *args, int wlen, int rlen)
{
	memcpy(cmd->args, args, wlen);
	cmd->wlen = wlen;
	cmd->rlen = rlen;
}

#define CMD_SETUP(cmd, args, rlen) \
	cmd_setup(cmd, args, sizeof(args) - 1, rlen)


I think this way addresses all of your comments:

- since we're using a real function, the compiler can perform type-checking,
and the function's prototype is self-documenting.
- no more funky __args and __rlen arguments
- no local variables


Note however, that drivers/media/tuners/si2157.c uses a different struct
(struct si2157_cmd). Therefore, if we wanted to apply the same changes
to si2157.c, we would have to define a common struct, and replace all
the uses of struct si2157_cmd and struct si2168_cmd. That seemed too
invasive to do without the maintainer's approval and guidance.

Antti, do you Ack this new approach?

It's not clear what HW is supported by drivers/media/dvb-frontends/si21xx.c
(that's a very generic name).

Regards.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] media: si2168: Refactor command setup code
  2019-07-03 12:47   ` Marc Gonzalez
  2019-07-03 14:15     ` Marc Gonzalez
@ 2019-07-03 16:35     ` Jonathan Neuschäfer
  1 sibling, 0 replies; 6+ messages in thread
From: Jonathan Neuschäfer @ 2019-07-03 16:35 UTC (permalink / raw)
  To: Marc Gonzalez
  Cc: Jonathan Neuschäfer, Antti Palosaari, Mauro Carvalho Chehab,
	linux-media, LKML, Bjorn Andersson, Brad Love

[-- Attachment #1: Type: text/plain, Size: 1884 bytes --]

Hi again,

On Wed, Jul 03, 2019 at 02:47:59PM +0200, Marc Gonzalez wrote:
> On 02/07/2019 11:51, Jonathan Neuschäfer wrote:
> > On Mon, Jul 01, 2019 at 01:44:09PM +0200, Marc Gonzalez wrote:
[...]
> >>  static const struct dvb_frontend_ops si2168_ops;
> >>  
> >> +#define CMD_SETUP(cmd, __args, __rlen) do {	\
> >> +	int wlen = sizeof(__args) - 1;		\
> >> +	memcpy(cmd.args, __args, wlen);		\
> >> +	cmd.wlen = wlen; cmd.rlen = __rlen;	\
> >> +} while (0)
> > 
> > It would be nice for casual readers to have a little comment here, that
> > explains (briefly) what this macro does, and what the arguments mean,
> > and their types.
> 
> Just a bit of background.
> 
> A macro is required /at some point/ because arrays "decay" into pointers
> when used as function arguments.

*nod*

[ I should have been more specific: By "here" I meant at that spot in
  the code, and by casual readers I mostly meant casual readers of the
  code once it's merged. ]

> Come to think of it, I'm really not a fan of "large" macro functions.
> I'll outline a different option in v2.

The v2 approach looks nicer to me, thanks.

> > Why cmd rather than __cmd? This seems inconsistent.
> 
> Note: I hate using underscores in macro argument names, but they clashed
> with the struct field names. There was no such clash for 'cmd'.

Hmm, ok.

> > The wlen local variable can be avoided by a bit of suffling:
> > 
> > 	#define CMD_SETUP(cmd, __args, __rlen) do {	\
> > 		cmd.rlen = __rlen;			\
> > 		cmd.wlen = sizeof(__args) - 1;		\
> > 		memcpy(cmd.args, __args, cmd.wlen);	\
> > 	} while (0)
> 
> Do you think it is important to avoid a local variable?

Not exactly important, but wlen would be another name that can collide
with the name spaces of the functions where the macro is used and
trigger -Wshadow.


Greetings,
Jonathan Neuschäfer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-07-03 16:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-01 11:44 [PATCH v1] media: si2168: Refactor command setup code Marc Gonzalez
2019-07-02  8:37 ` Marc Gonzalez
2019-07-02  9:51 ` Jonathan Neuschäfer
2019-07-03 12:47   ` Marc Gonzalez
2019-07-03 14:15     ` Marc Gonzalez
2019-07-03 16:35     ` Jonathan Neuschäfer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).