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