2008-09-30 08:50:22 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbdev/usbdev_trace.c
|
|
|
|
*
|
2021-03-04 14:10:42 +08: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
|
2008-09-30 08:50:22 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-09-30 08:50:22 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08: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.
|
2008-09-30 08:50:22 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2014-10-09 00:18:58 +08:00
|
|
|
#include <stdarg.h>
|
2009-12-15 22:25:14 +08:00
|
|
|
#include <stdint.h>
|
2008-09-30 08:50:22 +08:00
|
|
|
#include <errno.h>
|
2008-10-04 01:31:39 +08:00
|
|
|
#include <debug.h>
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
#include <nuttx/irq.h>
|
2018-03-04 22:07:07 +08:00
|
|
|
#include <nuttx/syslog/syslog.h>
|
2010-12-14 11:33:39 +08:00
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
2018-03-04 22:07:07 +08:00
|
|
|
|
2008-10-08 07:19:27 +08:00
|
|
|
#undef usbtrace
|
2008-09-30 08:50:22 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-03-18 23:31:02 +08:00
|
|
|
* Pre-processor Definitions
|
2008-09-30 08:50:22 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_USBDEV_TRACE_NRECORDS
|
|
|
|
# define CONFIG_USBDEV_TRACE_NRECORDS 128
|
|
|
|
#endif
|
|
|
|
|
2009-11-03 04:11:50 +08:00
|
|
|
#ifndef CONFIG_USBDEV_TRACE_INITIALIDSET
|
|
|
|
# define CONFIG_USBDEV_TRACE_INITIALIDSET 0
|
|
|
|
#endif
|
|
|
|
|
2008-09-30 08:50:22 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-10-27 21:13:09 +08:00
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
2008-09-30 08:50:22 +08:00
|
|
|
static struct usbtrace_s g_trace[CONFIG_USBDEV_TRACE_NRECORDS];
|
2009-12-15 22:25:14 +08:00
|
|
|
static uint16_t g_head = 0;
|
|
|
|
static uint16_t g_tail = 0;
|
2008-10-27 21:13:09 +08:00
|
|
|
#endif
|
|
|
|
|
2016-06-12 04:14:08 +08:00
|
|
|
#if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_USB))
|
2009-11-03 04:11:50 +08:00
|
|
|
static usbtrace_idset_t g_maskedidset = CONFIG_USBDEV_TRACE_INITIALIDSET;
|
2008-10-27 21:13:09 +08:00
|
|
|
#endif
|
2008-09-30 08:50:22 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-09 00:18:58 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbtrace_syslog
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if !defined(CONFIG_USBDEV_TRACE) && \
|
2016-06-12 04:14:08 +08:00
|
|
|
(defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_USB))
|
2024-08-25 07:21:12 +08:00
|
|
|
static int usbtrace_syslog(FAR const char *fmt, ...)
|
2014-10-09 00:18:58 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
2020-05-07 00:07:34 +08:00
|
|
|
/* Let vsyslog do the real work */
|
2014-10-09 00:18:58 +08:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2020-05-07 00:07:34 +08:00
|
|
|
vsyslog(LOG_INFO, fmt, ap);
|
2014-10-09 00:18:58 +08:00
|
|
|
va_end(ap);
|
2020-05-07 00:07:34 +08:00
|
|
|
return OK;
|
2014-10-09 00:18:58 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-30 08:50:22 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2008-09-30 08:50:22 +08:00
|
|
|
* Name: usbtrace_enable
|
|
|
|
*
|
|
|
|
* Description:
|
2020-05-07 00:16:58 +08:00
|
|
|
* Enable/disable tracing per trace ID. The initial state is all IDs
|
|
|
|
* enabled.
|
2008-10-27 21:13:09 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2020-05-07 00:16:58 +08:00
|
|
|
* idset - The bitset of IDs to be masked. TRACE_ALLIDS enables all IDS;
|
|
|
|
* zero masks all IDs.
|
2008-10-27 21:13:09 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The previous idset value.
|
2008-09-30 08:50:22 +08:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - May be called from an interrupt handler
|
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2014-10-09 00:18:58 +08:00
|
|
|
#if defined(CONFIG_USBDEV_TRACE) || \
|
2016-06-12 04:14:08 +08:00
|
|
|
(defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_USB))
|
2008-10-27 21:13:09 +08:00
|
|
|
usbtrace_idset_t usbtrace_enable(usbtrace_idset_t idset)
|
2008-09-30 08:50:22 +08:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
2008-10-27 21:13:09 +08:00
|
|
|
usbtrace_idset_t ret;
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2008-10-27 21:13:09 +08:00
|
|
|
/* The following read and write must be atomic */
|
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
flags = enter_critical_section();
|
2008-10-27 21:13:09 +08:00
|
|
|
ret = g_maskedidset;
|
|
|
|
g_maskedidset = idset;
|
2016-02-14 21:32:58 +08:00
|
|
|
leave_critical_section(flags);
|
2008-10-27 21:13:09 +08:00
|
|
|
return ret;
|
2008-09-30 08:50:22 +08:00
|
|
|
}
|
2016-06-12 04:14:08 +08:00
|
|
|
#endif /* CONFIG_USBDEV_TRACE || CONFIG_DEBUG_FEATURES && CONFIG_DEBUG_USB */
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2008-09-30 08:50:22 +08:00
|
|
|
* Name: usbtrace
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Record a USB event (tracing must be enabled)
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* May be called from an interrupt handler
|
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2016-06-12 04:14:08 +08:00
|
|
|
#if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG_FEATURES) && defined(CONFIG_DEBUG_USB))
|
2009-12-15 22:25:14 +08:00
|
|
|
void usbtrace(uint16_t event, uint16_t value)
|
2008-09-30 08:50:22 +08:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
2008-10-27 21:13:09 +08:00
|
|
|
/* Check if tracing is enabled for this ID */
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
flags = enter_critical_section();
|
2008-10-27 21:13:09 +08:00
|
|
|
if ((g_maskedidset & TRACE_ID2BIT(event)) != 0)
|
2008-09-30 08:50:22 +08:00
|
|
|
{
|
2008-10-27 21:13:09 +08:00
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
2008-10-28 09:18:37 +08:00
|
|
|
/* Yes... save the new trace data at the head */
|
2008-09-30 08:50:22 +08:00
|
|
|
|
|
|
|
g_trace[g_head].event = event;
|
2008-10-29 01:20:37 +08:00
|
|
|
g_trace[g_head].value = value;
|
2008-09-30 08:50:22 +08:00
|
|
|
|
|
|
|
/* Increment the head and (probably) the tail index */
|
|
|
|
|
|
|
|
if (++g_head >= CONFIG_USBDEV_TRACE_NRECORDS)
|
|
|
|
{
|
|
|
|
g_head = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_head == g_tail)
|
|
|
|
{
|
|
|
|
if (++g_tail >= CONFIG_USBDEV_TRACE_NRECORDS)
|
|
|
|
{
|
|
|
|
g_tail = 0;
|
|
|
|
}
|
|
|
|
}
|
2008-10-27 21:13:09 +08:00
|
|
|
#else
|
2016-06-20 22:57:08 +08:00
|
|
|
/* Just print the data using syslog */
|
2008-10-27 21:13:09 +08:00
|
|
|
|
2014-10-09 00:18:58 +08:00
|
|
|
usbtrace_trprintf(usbtrace_syslog, event, value);
|
2009-11-03 04:11:50 +08:00
|
|
|
#endif
|
2008-09-30 08:50:22 +08:00
|
|
|
}
|
2014-03-18 23:31:02 +08:00
|
|
|
|
2016-02-14 21:32:58 +08:00
|
|
|
leave_critical_section(flags);
|
2008-09-30 08:50:22 +08:00
|
|
|
}
|
2016-06-12 04:14:08 +08:00
|
|
|
#endif /* CONFIG_USBDEV_TRACE || CONFIG_DEBUG_FEATURES && CONFIG_DEBUG_USB */
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2015-10-03 21:25:53 +08:00
|
|
|
/****************************************************************************
|
2008-09-30 08:50:22 +08:00
|
|
|
* Name: usbtrace_enumerate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Enumerate all buffer trace data (will temporarily disable tracing)
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* NEVER called from an interrupt handler
|
|
|
|
*
|
2015-10-03 21:25:53 +08:00
|
|
|
****************************************************************************/
|
2008-09-30 08:50:22 +08:00
|
|
|
|
2008-10-27 21:13:09 +08:00
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
2008-09-30 08:50:22 +08:00
|
|
|
int usbtrace_enumerate(trace_callback_t callback, void *arg)
|
|
|
|
{
|
2009-12-15 22:25:14 +08:00
|
|
|
uint16_t ndx;
|
|
|
|
uint32_t idset;
|
2008-09-30 08:50:22 +08:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Temporarily disable tracing */
|
|
|
|
|
2008-10-28 09:18:37 +08:00
|
|
|
idset = usbtrace_enable(0);
|
2008-09-30 08:50:22 +08:00
|
|
|
|
|
|
|
/* Visit every entry, starting with the tail */
|
|
|
|
|
2008-10-28 09:18:37 +08:00
|
|
|
for (ndx = g_tail; ndx != g_head; )
|
2008-09-30 08:50:22 +08:00
|
|
|
{
|
|
|
|
/* Call the user provided callback */
|
|
|
|
|
|
|
|
ret = callback(&g_trace[ndx], arg);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
/* Abort the enumeration */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increment the index */
|
|
|
|
|
|
|
|
if (++ndx >= CONFIG_USBDEV_TRACE_NRECORDS)
|
|
|
|
{
|
|
|
|
ndx = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-28 09:18:37 +08:00
|
|
|
/* Discard the trace data after it has been reported */
|
|
|
|
|
|
|
|
g_tail = g_head;
|
|
|
|
|
|
|
|
/* Restore tracing state */
|
|
|
|
|
2020-01-03 00:49:34 +08:00
|
|
|
usbtrace_enable(idset);
|
2008-09-30 08:50:22 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_USBDEV_TRACE */
|