virtio_byteorder.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef _LINUX_VIRTIO_BYTEORDER_H
  2. #define _LINUX_VIRTIO_BYTEORDER_H
  3. #include <linux/types.h>
  4. #include <uapi/linux/virtio_types.h>
  5. static inline bool virtio_legacy_is_little_endian(void)
  6. {
  7. #ifdef __LITTLE_ENDIAN
  8. return true;
  9. #else
  10. return false;
  11. #endif
  12. }
  13. static inline u16 __virtio16_to_cpu(bool little_endian, __virtio16 val)
  14. {
  15. if (little_endian)
  16. return le16_to_cpu((__force __le16)val);
  17. else
  18. return be16_to_cpu((__force __be16)val);
  19. }
  20. static inline __virtio16 __cpu_to_virtio16(bool little_endian, u16 val)
  21. {
  22. if (little_endian)
  23. return (__force __virtio16)cpu_to_le16(val);
  24. else
  25. return (__force __virtio16)cpu_to_be16(val);
  26. }
  27. static inline u32 __virtio32_to_cpu(bool little_endian, __virtio32 val)
  28. {
  29. if (little_endian)
  30. return le32_to_cpu((__force __le32)val);
  31. else
  32. return be32_to_cpu((__force __be32)val);
  33. }
  34. static inline __virtio32 __cpu_to_virtio32(bool little_endian, u32 val)
  35. {
  36. if (little_endian)
  37. return (__force __virtio32)cpu_to_le32(val);
  38. else
  39. return (__force __virtio32)cpu_to_be32(val);
  40. }
  41. static inline u64 __virtio64_to_cpu(bool little_endian, __virtio64 val)
  42. {
  43. if (little_endian)
  44. return le64_to_cpu((__force __le64)val);
  45. else
  46. return be64_to_cpu((__force __be64)val);
  47. }
  48. static inline __virtio64 __cpu_to_virtio64(bool little_endian, u64 val)
  49. {
  50. if (little_endian)
  51. return (__force __virtio64)cpu_to_le64(val);
  52. else
  53. return (__force __virtio64)cpu_to_be64(val);
  54. }
  55. #endif /* _LINUX_VIRTIO_BYTEORDER */