Conversion Functions

Version 2.1.10 introduced the availability of a few new conversion functions, declared in <asm/byteorder.h>. The functions can be used to access multi-byte values when the value is known to be stored as either little-endian or big-endian. Since the functions sometimes make a good shortcut in writing driver code, the header sysdep-2.1.h takes care of defining them for earlier kernel versions.

The native implementation offered by 2.1 kernel sources is faster than the portable one offered by sysdep-2.1.h, because it can exploit architecture-dependent functionalities.

The new functions respond to the following prototypes, where le stands for little-endian, and be stands for big-endian. Note that strict data typing is not enforced by the compiler, as most of the functions are preprocessor macros anyway; the types shown below are there for your reference.

__u16 cpu_to_le16(__u16 cpu_val);
__u32 cpu_to_le32(__u32 cpu_val);
__u16 cpu_to_be16(__u16 cpu_val);
__u32 cpu_to_be16(__u32 cpu_val);
__u16 le16_to_cpu(__u16 le_val);
__u32 le32_to_cpu(__u32 le_val);
__u16 be16_to_cpu(__u16 be_val);
__u32 be32_to_cpu(__u32 be_val);

Given the usefulness of such functions when dealing with binary data streams (such as filesystem data or information stored on an interface board), version 2.1.43 added two new sets of conversion functions. These sets allow you to retrieve a value by pointer, or to convert in place the value pointed to by the argument. The functions for 16-bit little-endian respond to the following prototypes; similar functions exist for the other types of integers, leading to a total of 16 new functions.

__u16 cpu_to_le16p(__u16 *addr)
__u16 le16_to_cpup(__u16 *addr)

void cpu_to_le16s(__u16 *addr)
void le16_to_cpus(__16 *addr)

The ``p'' functions work like pointer dereferencing, but convert the value if needed; the ``s'' functions (from the ``in situ'' clause) can be used to convert the endianness of a value in place (for example, cpu_to_le16s(addr) does the same as *addr = cpu_to_le16(*addr)).

These functions are defined in sysdep-2.1.h as well. The header uses inline functions instead of preprocessor macros as necessary to avoid the side effects of double interpretations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.150.80