''Part of the WritingPortableDrivers section''[[BR]] Processors store internal data in one of two ways: little-endian or big-endian. Little-endian processors store data with the right-most bytes (those with a higher address value) being the most significant, while big-endian processors store data with the left-most bytes (those with a lower address value) being the most significant. For example, the table below shows how the decimal value 684686 is stored in a 4-byte integer on the two different processor types (684686 decimal = a72be hex = 00000000 00001010 01110010 10001110 binary). ||Address||Big-Endian||Little-Endian|| ||0||00000000||10001110|| ||1||00001010||01110010|| ||2||01110010||00001010|| ||3||10001110||00000000|| Intel processors, for example the i386 and IA-64 series, are little-endian machines, whereas the SPARC processors are big-endian. The PowerPC processors can be run in either little- or big-endian mode, but for Linux, they are defined as running in big-endian mode. The ARM processor can be either, depending on the specific ARM chip being used, but usually it also runs in big-endian mode. Because of the different endian types of processors, you need to be aware of data you receive from external sources and the order in which it appears. For example, the USB specification dictates that all multibyte data fields are in little-endian form. So if you have a USB driver that reads a multibyte field from the USB connection, you need to convert that data into the processor's native format. Code that assumes the processor is little-endian could ignore the data format coming from the USB connection successfully. But this same code would not work on PowerPC or ARM processors and is the leading cause of drivers that are broken on different platforms. Thankfully, there are a number of helpful macros that have been created to make this an easy task. All of the following macros can be found in the asm/byteorder.h header file. To convert from the processor's native format into little-endian form you can use the following functions: {{{ #!cplusplus __le64 cpu_to_le64(u64); __le32 cpu_to_le32(u32); __le16 cpu_to_le16(u16); }}} To convert from little-endian format into the processor's native format you should use these functions: {{{ #!cplusplus u64 le64_to_cpu(__le64); u32 le32_to_cpu(__le32); u16 le16_to_cpu(__le16); }}} For big-endian forms, the following functions are available: {{{ #!cplusplus __be64 cpu_to_be64(u64); __be32 cpu_to_be32(u32); __be16 cpu_to_be16(u16); u64 be64_to_cpu(__be64); u32 be32_to_cpu(__be32); u16 be16_to_cpu(__be16); }}} If you have a pointer to the value to convert, then you should use the following functions: {{{ #!cplusplus __le64 cpu_to_le64p(u64 *); __le32 cpu_to_le32p(u32 *); __le16 cpu_to_le16p(u16 *); u64 le64_to_cpup(__le64 *); u32 le32_to_cpup(__le32 *); u16 le16_to_cpup(__le16 *); __be64 cpu_to_be64p(u64 *); __be32 cpu_to_be32p(u32 *); __be16 cpu_to_be16p(u16 *); u64 be64_to_cpup(__be64 *); u32 be32_to_cpup(__be32 *); u16 be16_to_cpup(__be16 *); }}} If you want to convert the value within a variable and store the modified value in the same variable (in situ), then you should use the following functions: {{{ #!cplusplus void cpu_to_le64s(u64 *); void cpu_to_le32s(u32 *); void cpu_to_le16s(u16 *); void le64_to_cpus(u64 *); void le32_to_cpus(u32 *); void le16_to_cpus(u16 *); void cpu_to_be64s(u64 *); void cpu_to_be32s(u32 *); void cpu_to_be16s(u16 *); void be64_to_cpus(u64 *); void be32_to_cpus(u32 *); void be16_to_cpus(u16 *); }}} As stated before, the USB protocol is in little-endian format. The code snippet from drivers/usb/serial/visor.c presented in Listing 2 shows how a structure is read from the USB connection and then converted into the proper CPU format. Listing 2. How a structure is read from the USB connection and converted into the proper CPU format.