usbhost_trace.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /****************************************************************************
  2. * drivers/usbhost/usbhost_trace.c
  3. *
  4. * Copyright (C) 2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include <nuttx/irq.h>
  44. #include <nuttx/usb/usbhost_trace.h>
  45. #undef usbtrace
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /* Configuration ************************************************************/
  50. #ifndef CONFIG_USBHOST_TRACE_NRECORDS
  51. # define CONFIG_USBHOST_TRACE_NRECORDS 128
  52. #endif
  53. /****************************************************************************
  54. * Private Types
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Function Prototypes
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Private Data
  61. ****************************************************************************/
  62. #ifdef CONFIG_USBHOST_TRACE
  63. static uint32_t g_trace[CONFIG_USBHOST_TRACE_NRECORDS];
  64. static volatile uint16_t g_head = 0;
  65. static volatile uint16_t g_tail = 0;
  66. static volatile bool g_disabled = false;
  67. #endif
  68. /****************************************************************************
  69. * Private Functions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Name: usbhost_trsyslog
  73. *
  74. * Description:
  75. * Dump trace data to the syslog()
  76. *
  77. ****************************************************************************/
  78. #ifdef CONFIG_USBHOST_TRACE
  79. static int usbhost_trsyslog(uint32_t event, FAR void *arg)
  80. {
  81. FAR const char *fmt;
  82. uint16_t id;
  83. /* Decode the trace data */
  84. id = TRACE_DECODE_NDX(event);
  85. /* Get the format associated with the trace. Try the one argument format
  86. * first.
  87. */
  88. fmt = usbhost_trformat1(id);
  89. if (fmt)
  90. {
  91. /* Just print the data using syslog() */
  92. syslog(LOG_INFO, fmt, (unsigned int)TRACE_DECODE_U23(event));
  93. }
  94. /* No, then it must the two argument format first. */
  95. else
  96. {
  97. fmt = usbhost_trformat2(id);
  98. DEBUGASSERT(fmt);
  99. if (fmt)
  100. {
  101. /* Just print the data using syslog() */
  102. syslog(LOG_INFO, fmt, (unsigned int)TRACE_DECODE_U7(event),
  103. (unsigned int)TRACE_DECODE_U16(event));
  104. }
  105. }
  106. return OK;
  107. }
  108. #endif /* CONFIG_USBHOST_TRACE */
  109. /****************************************************************************
  110. * Name: usbhost_trace_common
  111. *
  112. * Description:
  113. * Record a USB event (tracing or USB debug must be enabled)
  114. *
  115. * Assumptions:
  116. * May be called from an interrupt handler
  117. *
  118. ****************************************************************************/
  119. #ifdef CONFIG_USBHOST_TRACE
  120. void usbhost_trace_common(uint32_t event)
  121. {
  122. irqstate_t flags;
  123. /* Check if tracing is enabled for this ID */
  124. flags = enter_critical_section();
  125. if (!g_disabled)
  126. {
  127. /* Yes... save the new trace data at the head */
  128. g_trace[g_head] = event;
  129. /* Increment the head and (probably) the tail index */
  130. if (++g_head >= CONFIG_USBHOST_TRACE_NRECORDS)
  131. {
  132. g_head = 0;
  133. }
  134. if (g_head == g_tail)
  135. {
  136. if (++g_tail >= CONFIG_USBHOST_TRACE_NRECORDS)
  137. {
  138. g_tail = 0;
  139. }
  140. }
  141. }
  142. leave_critical_section(flags);
  143. }
  144. #endif /* CONFIG_USBHOST_TRACE */
  145. /****************************************************************************
  146. * Public Functions
  147. ****************************************************************************/
  148. /****************************************************************************
  149. * Name: usbhost_trace1 and usbhost_trace2
  150. *
  151. * Description:
  152. * Record a USB event (tracing or USB debug must be enabled)
  153. *
  154. * Assumptions:
  155. * May be called from an interrupt handler
  156. *
  157. ****************************************************************************/
  158. #if defined(CONFIG_USBHOST_TRACE) || \
  159. (defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_USB))
  160. void usbhost_trace1(uint16_t id, uint32_t u23)
  161. {
  162. #ifdef CONFIG_USBHOST_TRACE
  163. usbhost_trace_common(TRACE_ENCODE1(id, u23));
  164. #else
  165. FAR const char *fmt;
  166. /* Get the format associated with the trace */
  167. fmt = usbhost_trformat1(id);
  168. DEBUGASSERT(fmt);
  169. /* Just print the data using syslog() */
  170. syslog(LOG_INFO, fmt, (unsigned int)u23);
  171. #endif
  172. }
  173. void usbhost_trace2(uint16_t id, uint8_t u7, uint16_t u16)
  174. {
  175. #ifdef CONFIG_USBHOST_TRACE
  176. usbhost_trace_common(TRACE_ENCODE2(id, u7, u16));
  177. #else
  178. FAR const char *fmt;
  179. /* Get the format associated with the trace */
  180. fmt = usbhost_trformat2(id);
  181. DEBUGASSERT(fmt);
  182. /* Just print the data using syslog() */
  183. syslog(LOG_INFO, fmt, u7, u16);
  184. #endif
  185. }
  186. #endif /* CONFIG_USBHOST_TRACE || CONFIG_DEBUG_FEATURES && CONFIG_DEBUG_USB */
  187. /****************************************************************************
  188. * Name: usbtrace_enumerate
  189. *
  190. * Description:
  191. * Enumerate all buffer trace data (will temporarily disable tracing)
  192. *
  193. * Assumptions:
  194. * NEVER called from an interrupt handler
  195. *
  196. ****************************************************************************/
  197. #ifdef CONFIG_USBHOST_TRACE
  198. int usbhost_trenumerate(usbhost_trcallback_t callback, FAR void *arg)
  199. {
  200. uint16_t ndx;
  201. int ret = OK;
  202. /* Temporarily disable tracing to avoid conflicts */
  203. g_disabled = true;
  204. /* Visit every entry, starting with the tail */
  205. for (ndx = g_tail; ndx != g_head; )
  206. {
  207. /* Call the user provided callback */
  208. ret = callback(g_trace[ndx], arg);
  209. if (ret != OK)
  210. {
  211. /* Abort the enumeration */
  212. break;
  213. }
  214. /* Increment the index */
  215. if (++ndx >= CONFIG_USBHOST_TRACE_NRECORDS)
  216. {
  217. ndx = 0;
  218. }
  219. }
  220. /* Discard the trace data after it has been reported */
  221. g_tail = g_head;
  222. /* Restore tracing state */
  223. g_disabled = false;
  224. return ret;
  225. }
  226. #endif /* CONFIG_USBHOST_TRACE */
  227. /****************************************************************************
  228. * Name: usbhost_trdump
  229. *
  230. * Description:
  231. * Used usbhost_trenumerate to dump all buffer trace data to syslog().
  232. *
  233. * Assumptions:
  234. * NEVER called from an interrupt handler
  235. *
  236. ****************************************************************************/
  237. #ifdef CONFIG_USBHOST_TRACE
  238. int usbhost_trdump(void)
  239. {
  240. return usbhost_trenumerate(usbhost_trsyslog, NULL);
  241. }
  242. #endif /* CONFIG_USBHOST_TRACE */