[PATCH 3/4 v2] lib: add crc16_le helper

Joakim Tjernlund joakim.tjernlund at transmode.se
Wed Aug 18 12:34:50 EDT 2010


Matthieu CASTET <matthieu.castet at parrot.com> wrote on 2010/08/18 18:10:11:
>
> Hi,
>
> thanks for the review.
>
> Joakim Tjernlund a écrit :
> >> This patch adds a crc16_le helper function to lib/crc16.c
> >>
> >> Signed-off-by: Matthieu CASTET <matthieu.castet at parrot.com>
> >> Signed-off-by: Florian Fainelli <ffainelli at freebox.fr>
> >> --
> >> diff --git a/include/linux/crc16.h b/include/linux/crc16.h
> >> index 9443c08..6c1c61f 100644
> >> --- a/include/linux/crc16.h
> >> +++ b/include/linux/crc16.h
> >> @@ -26,5 +26,7 @@ static inline u16 crc16_byte(u16 crc, const u8 data)
> >>     return (crc >> 8) ^ crc16_table[(crc ^ data) & 0xff];
> >>  }
> >>
> >> +extern u16 crc16_le(u16 crc, const u8 *buffer, size_t len);
> >> +
> >>  #endif /* __CRC16_H */
> >>
> >> diff --git a/lib/crc16.c b/lib/crc16.c
> >> index 8737b08..ec4a95c 100644
> >> --- a/lib/crc16.c
> >> +++ b/lib/crc16.c
> >> @@ -62,6 +62,18 @@ u16 crc16(u16 crc, u8 const *buffer, size_t len)
> >>  }
> >>  EXPORT_SYMBOL(crc16);
> >>
> >> +u16 crc16_le(u16 crc, u8 const *p, size_t len)
> >> +{
> >> +   int i;
> >> +   while (len--) {
> >> +      crc ^= *p++ << 8;
> >> +      for (i = 0; i < 8; i++)
> >> +         crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
> >> +   }
> >> +   return crc;
> >> +}
> >> +EXPORT_SYMBOL(crc16_le);
> >> +
> >>  MODULE_DESCRIPTION("CRC16 calculations");
> >>  MODULE_LICENSE("GPL");
> >
> > Don't recall much of my earlier crc32 work but the above looks wrong.
> > crc & 0x8000 suggests this is a big endian crc.
> yes it is a be crc while crc16 is a le one. So we should rename it to
> crc16_be.
>
>
> >if so, 0x8005 looks
> > wrong too. see CRCPOLY_LE resp. CRCPOLY_BE for an idea.
> Why ?

Because changing endian of crc also require the POLY
to be bit reversed as well. See:
 #define CRCPOLY_LE 0xedb88320
 #define CRCPOLY_BE 0x04c11db7
So I assume you need to do that as well for crc16_be, otherwise it
won't be a BE version of the standard crc16 LE

>
> > What is this crc sum used for?
> Onfi flash parsing in mtd.

And this is a one time operation of fairly small amount of
data? I ask because you impl. is really slow.
Why can't you use the crc16 LE version?




More information about the linux-mtd mailing list