2016-06-30 20:24:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbmonitor/usbmonitor.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-06-30 20:24:33 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-30 20:24:33 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-06-30 20:24:33 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <nuttx/progmem.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sched.h>
|
2021-05-14 04:03:23 +02:00
|
|
|
#include <debug.h>
|
2021-05-14 04:45:57 +02:00
|
|
|
#include <errno.h>
|
2016-06-30 20:24:33 +02:00
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2016-06-30 20:24:33 +02:00
|
|
|
#include <nuttx/kthread.h>
|
2018-03-04 15:07:07 +01:00
|
|
|
#include <nuttx/syslog/syslog.h>
|
2016-06-30 20:24:33 +02:00
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
|
|
|
#include <nuttx/usb/usbhost_trace.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBMONITOR
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBMONITOR_STACKSIZE
|
|
|
|
# define CONFIG_USBMONITOR_STACKSIZE 2048
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBMONITOR_PRIORITY
|
|
|
|
# define CONFIG_USBMONITOR_PRIORITY 50
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBMONITOR_INTERVAL
|
|
|
|
# define CONFIG_USBMONITOR_INTERVAL 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* USB device trace selection */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
|
|
|
# ifdef CONFIG_USBMONITOR_TRACEINIT
|
|
|
|
# define TRACE_INIT_BITS (TRACE_INIT_BIT)
|
|
|
|
# else
|
|
|
|
# define TRACE_INIT_BITS (0)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# define TRACE_ERROR_BITS (TRACE_DEVERROR_BIT|TRACE_CLSERROR_BIT)
|
|
|
|
|
|
|
|
# ifdef CONFIG_USBMONITOR_TRACECLASS
|
|
|
|
# define TRACE_CLASS_BITS (TRACE_CLASS_BIT|TRACE_CLASSAPI_BIT|\
|
|
|
|
TRACE_CLASSSTATE_BIT)
|
|
|
|
# else
|
|
|
|
# define TRACE_CLASS_BITS (0)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef CONFIG_USBMONITOR_TRACETRANSFERS
|
|
|
|
# define TRACE_TRANSFER_BITS (TRACE_OUTREQQUEUED_BIT|TRACE_INREQQUEUED_BIT|\
|
|
|
|
TRACE_READ_BIT|TRACE_WRITE_BIT|\
|
|
|
|
TRACE_COMPLETE_BIT)
|
|
|
|
# else
|
|
|
|
# define TRACE_TRANSFER_BITS (0)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef CONFIG_USBMONITOR_TRACECONTROLLER
|
|
|
|
# define TRACE_CONTROLLER_BITS (TRACE_EP_BIT|TRACE_DEV_BIT)
|
|
|
|
# else
|
|
|
|
# define TRACE_CONTROLLER_BITS (0)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# ifdef CONFIG_USBMONITOR_TRACEINTERRUPTS
|
|
|
|
# define TRACE_INTERRUPT_BITS (TRACE_INTENTRY_BIT|TRACE_INTDECODE_BIT|\
|
|
|
|
TRACE_INTEXIT_BIT)
|
|
|
|
# else
|
|
|
|
# define TRACE_INTERRUPT_BITS (0)
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# define TRACE_BITSET (TRACE_INIT_BITS|TRACE_ERROR_BITS|\
|
|
|
|
TRACE_CLASS_BITS|TRACE_TRANSFER_BITS|\
|
|
|
|
TRACE_CONTROLLER_BITS|TRACE_INTERRUPT_BITS)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct usbmon_state_s
|
|
|
|
{
|
|
|
|
volatile bool started;
|
|
|
|
volatile bool stop;
|
|
|
|
pid_t pid;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static struct usbmon_state_s g_usbmonitor;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
|
|
|
static int usbtrace_syslog(FAR const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2020-05-06 18:07:34 +02:00
|
|
|
/* Let vsyslog do the real work */
|
2016-06-30 20:24:33 +02:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2020-05-06 18:07:34 +02:00
|
|
|
vsyslog(LOG_INFO, fmt, ap);
|
2016-06-30 20:24:33 +02:00
|
|
|
va_end(ap);
|
2020-05-06 18:07:34 +02:00
|
|
|
return OK;
|
2016-06-30 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int usbmonitor_tracecallback(struct usbtrace_s *trace, void *arg)
|
|
|
|
{
|
|
|
|
usbtrace_trprintf(usbtrace_syslog, trace->event, trace->value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int usbmonitor_daemon(int argc, char **argv)
|
|
|
|
{
|
|
|
|
uinfo("Running: %d\n", g_usbmonitor.pid);
|
|
|
|
|
|
|
|
/* Loop until we detect that there is a request to stop. */
|
|
|
|
|
|
|
|
while (!g_usbmonitor.stop)
|
|
|
|
{
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_sleep(CONFIG_USBMONITOR_INTERVAL);
|
2016-06-30 20:24:33 +02:00
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
2020-01-02 17:49:34 +01:00
|
|
|
usbtrace_enumerate(usbmonitor_tracecallback, NULL);
|
2016-06-30 20:24:33 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_USBHOST_TRACE
|
2020-01-02 17:49:34 +01:00
|
|
|
usbhost_trdump();
|
2016-06-30 20:24:33 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Stopped */
|
|
|
|
|
|
|
|
g_usbmonitor.stop = false;
|
|
|
|
g_usbmonitor.started = false;
|
|
|
|
uinfo("Stopped: %d\n", g_usbmonitor.pid);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-06-30 21:09:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbmonitor_start
|
|
|
|
*
|
2021-02-25 14:34:37 +01:00
|
|
|
* Start the USB monitor kernel daemon.
|
2016-06-30 21:09:22 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2016-06-30 21:09:22 +02:00
|
|
|
* Zero (OK) is returned on success; a negated errno value is return on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbmonitor_start(void)
|
2016-06-30 20:24:33 +02:00
|
|
|
{
|
|
|
|
/* Has the monitor already started? */
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
if (!g_usbmonitor.started)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* No.. start it now */
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
|
|
|
/* First, initialize any USB tracing options that were requested */
|
|
|
|
|
|
|
|
usbtrace_enable(TRACE_BITSET);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Then start the USB monitoring daemon */
|
|
|
|
|
|
|
|
g_usbmonitor.started = true;
|
|
|
|
g_usbmonitor.stop = false;
|
|
|
|
|
2017-10-16 19:38:00 +02:00
|
|
|
ret = kthread_create("USB Monitor", CONFIG_USBMONITOR_PRIORITY,
|
|
|
|
CONFIG_USBMONITOR_STACKSIZE,
|
|
|
|
(main_t)usbmonitor_daemon,
|
|
|
|
(FAR char * const *)NULL);
|
2016-06-30 20:24:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
uerr("ERROR: Failed to start the USB monitor: %d\n",
|
2018-01-31 23:11:54 +01:00
|
|
|
ret);
|
2016-06-30 20:24:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-03-21 23:47:09 +01:00
|
|
|
g_usbmonitor.pid = (pid_t)ret;
|
2016-06-30 20:24:33 +02:00
|
|
|
uinfo("Started: %d\n", g_usbmonitor.pid);
|
2018-01-31 23:11:54 +01:00
|
|
|
ret = OK;
|
2016-06-30 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
2018-01-31 23:11:54 +01:00
|
|
|
return ret;
|
2016-06-30 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
uinfo("%s: %d\n",
|
|
|
|
g_usbmonitor.stop ? "Stopping" : "Running", g_usbmonitor.pid);
|
2018-01-31 23:11:54 +01:00
|
|
|
return OK;
|
2016-06-30 20:24:33 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 21:09:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbmonitor_stop
|
|
|
|
*
|
|
|
|
* Stop the USB monitor kernel daemon.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* None
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2016-06-30 21:09:22 +02:00
|
|
|
* Zero (OK) is returned on success; a negated errno value is return on
|
|
|
|
* any failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int usbmonitor_stop(void)
|
2016-06-30 20:24:33 +02:00
|
|
|
{
|
|
|
|
/* Has the monitor already started? */
|
|
|
|
|
|
|
|
if (g_usbmonitor.started)
|
|
|
|
{
|
|
|
|
/* Stop the USB monitor. The next time the monitor wakes up,
|
2017-05-11 21:35:56 +02:00
|
|
|
* it will see the stop indication and will exist.
|
2016-06-30 20:24:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
uinfo("Stopping: %d\n", g_usbmonitor.pid);
|
|
|
|
g_usbmonitor.stop = true;
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
|
|
|
/* We may as well disable tracing since there is no listener */
|
|
|
|
|
|
|
|
usbtrace_enable(0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uinfo("Stopped: %d\n", g_usbmonitor.pid);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_USBMONITOR */
|