Re: PATCH: usb: descriptor structures have to be packed

Sergei Organov <[email protected]>
Newsgroups gmane.linux.usb.devel
Message-ID <[email protected]>
Inaky Perez-Gonzalez <[email protected]> writes:

[...]

> Here is my point: it's way easier and more maintenable to do
>
> 	struct some_descriptor {
> 		__le16 foo;
> 		__u8 bar;
> 	} __attribute__((packed));
> 	...
> 	struct some_descriptor descr;
> 	...
> 	read_from_wire(&descr, sizeof(descr));
> 	do_something_with_values(&descr->foo, &descr->bar)

Out of curiosity, where endianness issues are handled in this code?

>
> than
>
> 	u8 buffer[SOME_DESCRIPTOR_DT_SIZE], foo;
> 	le16 bar;
> 	read_from_wire(buffer, SOME_DESCRIPTOR_DT_SIZE);
> 	bar = le16_to_cpu((__le16)buffer[1] << 8 | buffer[0]);
> 	foo = buffer[2];
> 	do_something_with_values(foo, bar)

This IMHO, could look, e.g., like this:

  struct some_descriptor {
    __u8 foo[2];
    __u8 bar;
  };
  ...  
  struct some_descriptor descr;
  ...
  read_from_wire(&descr, sizeof(descr));
  do_something_with_values(usb_get_uint16(descr->foo), bar);
  ...
  usb_set_uint16(descr->foo, 2385);
  
where:
  
  static inline unsigned usb_get_uint16(__u8 const *p)
  {
    return p[0] | (p[1] << 8);
  }
  
  static inline void usb_set_uint16(__u8 const *p, unsigned value)
  {
    p[0] = value;
    p[1] = value >> 8;
  }
  
Not only this avoids packed attribute, but makes it obvious that one
can't just use bare descr->foo in and endianness-safe way.

-- Sergei.


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
[email protected]
To unsubscribe, use the last form field at:
https://lists.sourceforge.net/lists/listinfo/linux-usb-devel
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.