device_compat.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
  4. * Copyright (c) 2013 Google, Inc
  5. *
  6. * (C) Copyright 2012
  7. * Pavel Herrmann <morpheus.ibis@gmail.com>
  8. * Marek Vasut <marex@denx.de>
  9. */
  10. #ifndef _DM_DEVICE_COMPAT_H
  11. #define _DM_DEVICE_COMPAT_H
  12. #include <log.h>
  13. #include <linux/build_bug.h>
  14. #include <linux/compat.h>
  15. /*
  16. * REVISIT:
  17. * remove the following after resolving conflicts with <linux/compat.h>
  18. */
  19. #ifdef dev_dbg
  20. #undef dev_dbg
  21. #endif
  22. #ifdef dev_vdbg
  23. #undef dev_vdbg
  24. #endif
  25. #ifdef dev_info
  26. #undef dev_info
  27. #endif
  28. #ifdef dev_err
  29. #undef dev_err
  30. #endif
  31. #ifdef dev_warn
  32. #undef dev_warn
  33. #endif
  34. /*
  35. * Define a new identifier which can be tested on by C code. A similar
  36. * definition is made for DEBUG in <log.h>.
  37. */
  38. #ifdef VERBOSE_DEBUG
  39. #define _VERBOSE_DEBUG 1
  40. #else
  41. #define _VERBOSE_DEBUG 0
  42. #endif
  43. /**
  44. * dev_printk_emit() - Emit a formatted log message
  45. * @cat: Category of the message
  46. * @level: Log level of the message
  47. * @fmt: Format string
  48. * @...: Arguments for @fmt
  49. *
  50. * This macro logs a message through the appropriate channel. It is a macro so
  51. * the if statements can be optimized out (as @level should be a constant known
  52. * at compile-time).
  53. *
  54. * If DEBUG or VERBOSE_DEBUG is defined, then some messages are always printed
  55. * (through printf()). This is to match the historical behavior of the dev_xxx
  56. * functions.
  57. *
  58. * If LOG is enabled, use log() to emit the message, otherwise print it based on
  59. * the console loglevel.
  60. */
  61. #define dev_printk_emit(cat, level, fmt, ...) \
  62. ({ \
  63. if ((_DEBUG && level == LOGL_DEBUG) || \
  64. (_VERBOSE_DEBUG && level == LOGL_DEBUG_CONTENT)) \
  65. printf(fmt, ##__VA_ARGS__); \
  66. else if (CONFIG_IS_ENABLED(LOG)) \
  67. log(cat, level, fmt, ##__VA_ARGS__); \
  68. else if (level < CONFIG_VAL(LOGLEVEL)) \
  69. printf(fmt, ##__VA_ARGS__); \
  70. })
  71. /**
  72. * __dev_printk() - Log a message for a device
  73. * @level: Log level of the message
  74. * @dev: A &struct udevice or &struct device
  75. * @fmt: Format string
  76. * @...: Arguments for @fmt
  77. *
  78. * This macro formats and prints dev_xxx log messages. It is done as a macro
  79. * because working with variadic argument is much easier this way, we can
  80. * interrogate the type of device we are passed (and whether it *is* a &struct
  81. * udevice or &struct device), and dev_printk_emit() can optimize out unused if
  82. * branches.
  83. *
  84. * Because this is a macro, we must enforce type checks ourselves. Ideally, we
  85. * would only accept udevices, but there is a significant amount of code (mostly
  86. * USB) which calls dev_xxx with &struct device. When assigning ``__dev``, we
  87. * must first cast ``dev`` to ``void *`` so we don't get warned when ``dev`` is
  88. * a &struct device. Even though the latter branch is not taken, it will still
  89. * get compiled and type-checked.
  90. *
  91. * The format strings in case of a ``NULL`` ``dev`` MUST be kept the same.
  92. * Otherwise, @fmt will be duplicated in the data section (with slightly
  93. * different prefixes). This is why ``(NULL udevice *)`` is printed as two
  94. * string arguments, and not by string pasting.
  95. */
  96. #define __dev_printk(level, dev, fmt, ...) \
  97. ({ \
  98. if (__same_type(dev, struct device *)) { \
  99. dev_printk_emit(LOG_CATEGORY, level, fmt, ##__VA_ARGS__); \
  100. } else { \
  101. BUILD_BUG_ON(!__same_type(dev, struct udevice *)); \
  102. struct udevice *__dev = (void *)dev; \
  103. if (__dev) \
  104. dev_printk_emit(__dev->driver->id, level, \
  105. "%s %s: " fmt, \
  106. __dev->driver->name, __dev->name, \
  107. ##__VA_ARGS__); \
  108. else \
  109. dev_printk_emit(LOG_CATEGORY, level, \
  110. "%s %s: " fmt, \
  111. "(NULL", "udevice *)", \
  112. ##__VA_ARGS__); \
  113. } \
  114. })
  115. #define dev_emerg(dev, fmt, ...) \
  116. __dev_printk(LOGL_EMERG, dev, fmt, ##__VA_ARGS__)
  117. #define dev_alert(dev, fmt, ...) \
  118. __dev_printk(LOGL_ALERT, dev, fmt, ##__VA_ARGS__)
  119. #define dev_crit(dev, fmt, ...) \
  120. __dev_printk(LOGL_CRIT, dev, fmt, ##__VA_ARGS__)
  121. #define dev_err(dev, fmt, ...) \
  122. __dev_printk(LOGL_ERR, dev, fmt, ##__VA_ARGS__)
  123. #define dev_warn(dev, fmt, ...) \
  124. __dev_printk(LOGL_WARNING, dev, fmt, ##__VA_ARGS__)
  125. #define dev_notice(dev, fmt, ...) \
  126. __dev_printk(LOGL_NOTICE, dev, fmt, ##__VA_ARGS__)
  127. #define dev_info(dev, fmt, ...) \
  128. __dev_printk(LOGL_INFO, dev, fmt, ##__VA_ARGS__)
  129. #define dev_dbg(dev, fmt, ...) \
  130. __dev_printk(LOGL_DEBUG, dev, fmt, ##__VA_ARGS__)
  131. #define dev_vdbg(dev, fmt, ...) \
  132. __dev_printk(LOGL_DEBUG_CONTENT, dev, fmt, ##__VA_ARGS__)
  133. #endif