mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Krzysztof Opasiak <k.opasiak@samsung.com>
To: "'Michal Nazarewicz'" <mina86@mina86.com>,
	Krzysztof Opasiak <k.opasiak@samsung.com>,
	"'Felipe Balbi'" <balbi@ti.com>,
	Krzysztof Opasiak <k.opasiak@samsung.com>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: RE: [PATCHv2 5/5] tools: ffs-test: add compatibility code for old kernels
Date: Mon, 09 Jun 2014 16:02:17 +0200	[thread overview]
Message-ID: <007601cf83eb$6cf9b0d0$46ed1270$%opasiak@samsung.com> (raw)
In-Reply-To: <xa1tmwdmdxgh.fsf@mina86.com>

Hi,

> -----Original Message-----
> From: Michal Nazarewicz [mailto:mpn@google.com] On Behalf Of Michal
> Nazarewicz
> Sent: Monday, June 09, 2014 3:28 PM
> To: Krzysztof Opasiak; 'Felipe Balbi'; Krzysztof Opasiak
> Cc: linux-usb@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: Re: [PATCHv2 5/5] tools: ffs-test: add compatibility code
> for old kernels
> 
> On Mon, Jun 09 2014, Krzysztof Opasiak wrote:
> > As this is an example which will be copy-paste by many users
> maybe you
> > should you struct usb_functionfs_descs_head and struct
> > usb_functionfs_descs_head_v2 instead of direct operations using
> > hard-coded offsets to make this function more readable?
> 
> v3 uses usb_functionfs_descs_head{_v2,} instead of hard-coded
> offsets.
> I also started wondering if it would make sense to go one step
> further
> and define a temporary structure holding the headers.  Something
> like:
> 
> ----------- >8 ----------------------------------------------------
> -----
> 	/* Read v2 header */
> 	{
> 		const struct {
> 			const struct usb_functionfs_descs_head_v2 header;
> 			const __le32 counts[];
> 		} __attribute__((packed)) *const in = descriptors;
> 		const __le32 *counts = in->counts;
> 		__u32 flags;
> 
> 		if (le32_to_cpu(in->header.magic) !=
> 		    FUNCTIONFS_DESCRIPTORS_MAGIC_V2)
> 			return 0;
> 		length = le32_to_cpu(in->header.length);
> 		if (length <= sizeof in->header)
> 			return 0;
> 		length -= sizeof in->header;
> 		flags = le32_to_cpu(in->header.flags);
> 		if (flags & ~(FUNCTIONFS_HAS_FS_DESC |
> FUNCTIONFS_HAS_HS_DESC |
> 			      FUNCTIONFS_HAS_SS_DESC))
> 			return 0;
> 
> #define GET_NEXT_COUNT_IF_FLAG(ret, flg) do {		\
> 			if (!(flags & (flg)))		\
> 				break;			\
> 			if (length < 4)			\
> 				return 0;		\
> 			ret = le32_to_cpu(*counts);	\
> 			length -= 4;			\
> 			++counts;			\
> 		} while (0)
> 
> 		GET_NEXT_COUNT_IF_FLAG(fs_count,
> FUNCTIONFS_HAS_FS_DESC);
> 		GET_NEXT_COUNT_IF_FLAG(hs_count,
> FUNCTIONFS_HAS_HS_DESC);
> 		GET_NEXT_COUNT_IF_FLAG(count, FUNCTIONFS_HAS_SS_DESC);
> 
> 		count = fs_count + hs_count;
> 		if (!count)
> 			return 0;
> 		descs_start = descs_end = (const void *)counts;
> 
> #undef GET_NEXT_COUNT_IF_FLAG
> 	}
> 
> ----------- >8 ----------------------------------------------------
> -----
> 
> 	/* Allocate legacy descriptors and copy the data. */
> 	{
> 		struct {
> 			struct usb_functionfs_descs_head header;
> 			__u8 descriptors[];
> 		} __attribute__((packed)) *out;
> 
> 		length = sizeof out->header + (descs_end -
> descs_start);
> 		out = malloc(length);
> 		out->header.magic =
> cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
> 		out->header.length = cpu_to_le32(length);
> 		out->header.fs_count = cpu_to_le32(fs_count);
> 		out->header.hs_count = cpu_to_le32(hs_count);
> 		memcpy(out->descriptors, descs_start, descs_end -
> descs_start);
> 		*legacy = out;
> 	}
> ----------- >8 ----------------------------------------------------
> -----
> 
> Thoughts?

Looks very good to me! In my opinion this one should be used, it emphasizes your intentions very well so it's perfect code for sample app.

--
BR's
Krzysztof Opasiak
Samsung R&D Institute Poland
Samsung Electronics
k.opasiak@samsung.com






      reply	other threads:[~2014-06-09 14:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-06  9:12 [PATCHv2 1/5] usb: gadget: f_fs: resurect usb_functionfs_descs_head structure Michal Nazarewicz
2014-06-06  9:12 ` [PATCHv2 2/5] tools: ffs-test: fix header values endianess Michal Nazarewicz
2014-06-06  9:12 ` [PATCHv2 3/5] usb: gadget: f_fs: add usb_functionfs_descs_head_v2 structure Michal Nazarewicz
2014-06-06  9:12 ` [PATCHv2 4/5] tools: ffs-test: convert to new descriptor format Michal Nazarewicz
2014-06-06  9:12 ` [PATCHv2 5/5] tools: ffs-test: add compatibility code for old kernels Michal Nazarewicz
2014-06-09  8:25   ` Krzysztof Opasiak
2014-06-09 10:21     ` [PATCHv3] " Michal Nazarewicz
2014-06-09 13:27     ` [PATCHv2 5/5] " Michal Nazarewicz
2014-06-09 14:02       ` Krzysztof Opasiak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='007601cf83eb$6cf9b0d0$46ed1270$%opasiak@samsung.com' \
    --to=k.opasiak@samsung.com \
    --cc=balbi@ti.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mina86@mina86.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®