usbmonitor.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /****************************************************************************
  2. * drivers/usbmonitor/usbmonitor.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <nuttx/progmem.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. #include <stdbool.h>
  28. #include <unistd.h>
  29. #include <sched.h>
  30. #include <debug.h>
  31. #include <errno.h>
  32. #include <nuttx/signal.h>
  33. #include <nuttx/kthread.h>
  34. #include <nuttx/syslog/syslog.h>
  35. #include <nuttx/usb/usbdev_trace.h>
  36. #include <nuttx/usb/usbhost_trace.h>
  37. #ifdef CONFIG_USBMONITOR
  38. /****************************************************************************
  39. * Pre-processor Definitions
  40. ****************************************************************************/
  41. /* Configuration ************************************************************/
  42. #ifndef CONFIG_USBMONITOR_STACKSIZE
  43. # define CONFIG_USBMONITOR_STACKSIZE 2048
  44. #endif
  45. #ifndef CONFIG_USBMONITOR_PRIORITY
  46. # define CONFIG_USBMONITOR_PRIORITY 50
  47. #endif
  48. #ifndef CONFIG_USBMONITOR_INTERVAL
  49. # define CONFIG_USBMONITOR_INTERVAL 2
  50. #endif
  51. /* USB device trace selection */
  52. #ifdef CONFIG_USBDEV_TRACE
  53. # ifdef CONFIG_USBMONITOR_TRACEINIT
  54. # define TRACE_INIT_BITS (TRACE_INIT_BIT)
  55. # else
  56. # define TRACE_INIT_BITS (0)
  57. # endif
  58. # define TRACE_ERROR_BITS (TRACE_DEVERROR_BIT|TRACE_CLSERROR_BIT)
  59. # ifdef CONFIG_USBMONITOR_TRACECLASS
  60. # define TRACE_CLASS_BITS (TRACE_CLASS_BIT|TRACE_CLASSAPI_BIT|\
  61. TRACE_CLASSSTATE_BIT)
  62. # else
  63. # define TRACE_CLASS_BITS (0)
  64. # endif
  65. # ifdef CONFIG_USBMONITOR_TRACETRANSFERS
  66. # define TRACE_TRANSFER_BITS (TRACE_OUTREQQUEUED_BIT|TRACE_INREQQUEUED_BIT|\
  67. TRACE_READ_BIT|TRACE_WRITE_BIT|\
  68. TRACE_COMPLETE_BIT)
  69. # else
  70. # define TRACE_TRANSFER_BITS (0)
  71. # endif
  72. # ifdef CONFIG_USBMONITOR_TRACECONTROLLER
  73. # define TRACE_CONTROLLER_BITS (TRACE_EP_BIT|TRACE_DEV_BIT)
  74. # else
  75. # define TRACE_CONTROLLER_BITS (0)
  76. # endif
  77. # ifdef CONFIG_USBMONITOR_TRACEINTERRUPTS
  78. # define TRACE_INTERRUPT_BITS (TRACE_INTENTRY_BIT|TRACE_INTDECODE_BIT|\
  79. TRACE_INTEXIT_BIT)
  80. # else
  81. # define TRACE_INTERRUPT_BITS (0)
  82. # endif
  83. # define TRACE_BITSET (TRACE_INIT_BITS|TRACE_ERROR_BITS|\
  84. TRACE_CLASS_BITS|TRACE_TRANSFER_BITS|\
  85. TRACE_CONTROLLER_BITS|TRACE_INTERRUPT_BITS)
  86. #endif
  87. /****************************************************************************
  88. * Private Types
  89. ****************************************************************************/
  90. struct usbmon_state_s
  91. {
  92. volatile bool started;
  93. volatile bool stop;
  94. pid_t pid;
  95. };
  96. /****************************************************************************
  97. * Private Data
  98. ****************************************************************************/
  99. static struct usbmon_state_s g_usbmonitor;
  100. /****************************************************************************
  101. * Private Functions
  102. ****************************************************************************/
  103. #ifdef CONFIG_USBDEV_TRACE
  104. static int usbtrace_syslog(FAR const char *fmt, ...)
  105. {
  106. va_list ap;
  107. /* Let vsyslog do the real work */
  108. va_start(ap, fmt);
  109. vsyslog(LOG_INFO, fmt, ap);
  110. va_end(ap);
  111. return OK;
  112. }
  113. static int usbmonitor_tracecallback(struct usbtrace_s *trace, void *arg)
  114. {
  115. usbtrace_trprintf(usbtrace_syslog, trace->event, trace->value);
  116. return 0;
  117. }
  118. #endif
  119. static int usbmonitor_daemon(int argc, char **argv)
  120. {
  121. uinfo("Running: %d\n", g_usbmonitor.pid);
  122. /* Loop until we detect that there is a request to stop. */
  123. while (!g_usbmonitor.stop)
  124. {
  125. nxsig_sleep(CONFIG_USBMONITOR_INTERVAL);
  126. #ifdef CONFIG_USBDEV_TRACE
  127. usbtrace_enumerate(usbmonitor_tracecallback, NULL);
  128. #endif
  129. #ifdef CONFIG_USBHOST_TRACE
  130. usbhost_trdump();
  131. #endif
  132. }
  133. /* Stopped */
  134. g_usbmonitor.stop = false;
  135. g_usbmonitor.started = false;
  136. uinfo("Stopped: %d\n", g_usbmonitor.pid);
  137. return 0;
  138. }
  139. /****************************************************************************
  140. * Public Functions
  141. ****************************************************************************/
  142. /****************************************************************************
  143. * Name: usbmonitor_start
  144. *
  145. * Start the USB monitor kernel daemon.
  146. *
  147. * Input Parameters:
  148. * None
  149. *
  150. * Returned Value:
  151. * Zero (OK) is returned on success; a negated errno value is return on
  152. * any failure.
  153. *
  154. ****************************************************************************/
  155. int usbmonitor_start(void)
  156. {
  157. /* Has the monitor already started? */
  158. sched_lock();
  159. if (!g_usbmonitor.started)
  160. {
  161. int ret;
  162. /* No.. start it now */
  163. #ifdef CONFIG_USBDEV_TRACE
  164. /* First, initialize any USB tracing options that were requested */
  165. usbtrace_enable(TRACE_BITSET);
  166. #endif
  167. /* Then start the USB monitoring daemon */
  168. g_usbmonitor.started = true;
  169. g_usbmonitor.stop = false;
  170. ret = kthread_create("USB Monitor", CONFIG_USBMONITOR_PRIORITY,
  171. CONFIG_USBMONITOR_STACKSIZE,
  172. (main_t)usbmonitor_daemon,
  173. (FAR char * const *)NULL);
  174. if (ret < 0)
  175. {
  176. uerr("ERROR: Failed to start the USB monitor: %d\n",
  177. ret);
  178. }
  179. else
  180. {
  181. g_usbmonitor.pid = ret;
  182. uinfo("Started: %d\n", g_usbmonitor.pid);
  183. ret = OK;
  184. }
  185. sched_unlock();
  186. return ret;
  187. }
  188. sched_unlock();
  189. uinfo("%s: %d\n",
  190. g_usbmonitor.stop ? "Stopping" : "Running", g_usbmonitor.pid);
  191. return OK;
  192. }
  193. /****************************************************************************
  194. * Name: usbmonitor_stop
  195. *
  196. * Stop the USB monitor kernel daemon.
  197. *
  198. * Input Parameters:
  199. * None
  200. *
  201. * Returned Value:
  202. * Zero (OK) is returned on success; a negated errno value is return on
  203. * any failure.
  204. *
  205. ****************************************************************************/
  206. int usbmonitor_stop(void)
  207. {
  208. /* Has the monitor already started? */
  209. if (g_usbmonitor.started)
  210. {
  211. /* Stop the USB monitor. The next time the monitor wakes up,
  212. * it will see the stop indication and will exist.
  213. */
  214. uinfo("Stopping: %d\n", g_usbmonitor.pid);
  215. g_usbmonitor.stop = true;
  216. #ifdef CONFIG_USBDEV_TRACE
  217. /* We may as well disable tracing since there is no listener */
  218. usbtrace_enable(0);
  219. #endif
  220. }
  221. uinfo("Stopped: %d\n", g_usbmonitor.pid);
  222. return 0;
  223. }
  224. #endif /* CONFIG_USBMONITOR */