usbdev_trace.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /****************************************************************************
  2. * drivers/usbdev/usbdev_trace.c
  3. *
  4. * Copyright (C) 2008-2010, 2012 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 <arch/irq.h>
  44. #include <nuttx/usb/usbdev_trace.h>
  45. #undef usbtrace
  46. /****************************************************************************
  47. * Definitions
  48. ****************************************************************************/
  49. /* Configuration ************************************************************/
  50. #ifndef CONFIG_USBDEV_TRACE_NRECORDS
  51. # define CONFIG_USBDEV_TRACE_NRECORDS 128
  52. #endif
  53. #ifndef CONFIG_USBDEV_TRACE_INITIALIDSET
  54. # define CONFIG_USBDEV_TRACE_INITIALIDSET 0
  55. #endif
  56. /****************************************************************************
  57. * Private Types
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Private Function Prototypes
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Private Data
  64. ****************************************************************************/
  65. #ifdef CONFIG_USBDEV_TRACE
  66. static struct usbtrace_s g_trace[CONFIG_USBDEV_TRACE_NRECORDS];
  67. static uint16_t g_head = 0;
  68. static uint16_t g_tail = 0;
  69. #endif
  70. #if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_USB))
  71. static usbtrace_idset_t g_maskedidset = CONFIG_USBDEV_TRACE_INITIALIDSET;
  72. #endif
  73. /****************************************************************************
  74. * Private Functions
  75. ****************************************************************************/
  76. /****************************************************************************
  77. * Public Functions
  78. ****************************************************************************/
  79. /*******************************************************************************
  80. * Name: usbtrace_enable
  81. *
  82. * Description:
  83. * Enable/disable tracing per trace ID. The initial state is all IDs enabled.
  84. *
  85. * Input Parameters:
  86. * idset - The bitset of IDs to be masked. TRACE_ALLIDS enables all IDS; zero
  87. * masks all IDs.
  88. *
  89. * Returned Value:
  90. * The previous idset value.
  91. *
  92. * Assumptions:
  93. * - May be called from an interrupt handler
  94. *
  95. *******************************************************************************/
  96. #if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_USB))
  97. usbtrace_idset_t usbtrace_enable(usbtrace_idset_t idset)
  98. {
  99. irqstate_t flags;
  100. usbtrace_idset_t ret;
  101. /* The following read and write must be atomic */
  102. flags = irqsave();
  103. ret = g_maskedidset;
  104. g_maskedidset = idset;
  105. irqrestore(flags);
  106. return ret;
  107. }
  108. #endif /* CONFIG_USBDEV_TRACE || CONFIG_DEBUG && CONFIG_DEBUG_USB */
  109. /*******************************************************************************
  110. * Name: usbtrace
  111. *
  112. * Description:
  113. * Record a USB event (tracing must be enabled)
  114. *
  115. * Assumptions:
  116. * May be called from an interrupt handler
  117. *
  118. *******************************************************************************/
  119. #if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_USB))
  120. void usbtrace(uint16_t event, uint16_t value)
  121. {
  122. irqstate_t flags;
  123. /* Check if tracing is enabled for this ID */
  124. flags = irqsave();
  125. if ((g_maskedidset & TRACE_ID2BIT(event)) != 0)
  126. {
  127. #ifdef CONFIG_USBDEV_TRACE
  128. /* Yes... save the new trace data at the head */
  129. g_trace[g_head].event = event;
  130. g_trace[g_head].value = value;
  131. /* Increment the head and (probably) the tail index */
  132. if (++g_head >= CONFIG_USBDEV_TRACE_NRECORDS)
  133. {
  134. g_head = 0;
  135. }
  136. if (g_head == g_tail)
  137. {
  138. if (++g_tail >= CONFIG_USBDEV_TRACE_NRECORDS)
  139. {
  140. g_tail = 0;
  141. }
  142. }
  143. #else
  144. /* Just print the data using lowsyslog */
  145. usbtrace_trprintf((trprintf_t)lowsyslog, event, value);
  146. #endif
  147. }
  148. irqrestore(flags);
  149. }
  150. #endif /* CONFIG_USBDEV_TRACE || CONFIG_DEBUG && CONFIG_DEBUG_USB */
  151. /*******************************************************************************
  152. * Name: usbtrace_enumerate
  153. *
  154. * Description:
  155. * Enumerate all buffer trace data (will temporarily disable tracing)
  156. *
  157. * Assumptions:
  158. * NEVER called from an interrupt handler
  159. *
  160. *******************************************************************************/
  161. #ifdef CONFIG_USBDEV_TRACE
  162. int usbtrace_enumerate(trace_callback_t callback, void *arg)
  163. {
  164. uint16_t ndx;
  165. uint32_t idset;
  166. int ret = OK;
  167. /* Temporarily disable tracing */
  168. idset = usbtrace_enable(0);
  169. /* Visit every entry, starting with the tail */
  170. for (ndx = g_tail; ndx != g_head; )
  171. {
  172. /* Call the user provided callback */
  173. ret = callback(&g_trace[ndx], arg);
  174. if (ret != OK)
  175. {
  176. /* Abort the enumeration */
  177. break;
  178. }
  179. /* Increment the index */
  180. if (++ndx >= CONFIG_USBDEV_TRACE_NRECORDS)
  181. {
  182. ndx = 0;
  183. }
  184. }
  185. /* Discard the trace data after it has been reported */
  186. g_tail = g_head;
  187. /* Restore tracing state */
  188. (void)usbtrace_enable(idset);
  189. return ret;
  190. }
  191. #endif /* CONFIG_USBDEV_TRACE */