2008-09-30 08:50:22 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbdev/usbdev_trace.c
|
|
|
|
*
|
2009-11-03 04:11:50 +08:00
|
|
|
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
|
2008-09-30 08:50:22 +08:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.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
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
#include <nuttx/usbdev_trace.h>
|
2008-10-08 07:19:27 +08:00
|
|
|
#undef usbtrace
|
2008-09-30 08:50:22 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
|
#if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG) && 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
* Name: usbtrace_enable
|
|
|
|
*
|
|
|
|
* Description:
|
2008-10-27 21:13:09 +08:00
|
|
|
* Enable/disable tracing per trace ID. The initial state is all IDs enabled.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* idset - The bitset of IDs to be masked. TRACE_ALLIDS enables all IDS; zero
|
|
|
|
* masks all IDs.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The previous idset value.
|
2008-09-30 08:50:22 +08:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* - May be called from an interrupt handler
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2008-10-27 21:13:09 +08:00
|
|
|
#if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_USB))
|
|
|
|
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 */
|
|
|
|
|
|
|
|
flags = irqsave();
|
|
|
|
ret = g_maskedidset;
|
|
|
|
g_maskedidset = idset;
|
2008-09-30 08:50:22 +08:00
|
|
|
irqrestore(flags);
|
2008-10-27 21:13:09 +08:00
|
|
|
return ret;
|
2008-09-30 08:50:22 +08:00
|
|
|
}
|
2008-10-27 21:13:09 +08:00
|
|
|
#endif /* CONFIG_USBDEV_TRACE || CONFIG_DEBUG && CONFIG_DEBUG_USB */
|
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
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
2008-10-27 21:13:09 +08:00
|
|
|
#if defined(CONFIG_USBDEV_TRACE) || (defined(CONFIG_DEBUG) && 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
|
|
|
|
|
|
|
flags = irqsave();
|
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
|
2009-11-03 04:11:50 +08:00
|
|
|
/* Just print the data using lib_lowprintf */
|
2008-10-27 21:13:09 +08:00
|
|
|
|
2009-11-03 04:11:50 +08:00
|
|
|
usbtrace_trprintf((trprintf_t)lib_lowprintf, event, value);
|
|
|
|
#endif
|
2008-09-30 08:50:22 +08:00
|
|
|
}
|
|
|
|
irqrestore(flags);
|
|
|
|
}
|
2008-10-27 21:13:09 +08:00
|
|
|
#endif /* CONFIG_USBDEV_TRACE || CONFIG_DEBUG && CONFIG_DEBUG_USB */
|
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
|
|
|
|
*
|
|
|
|
*******************************************************************************/
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
(void)usbtrace_enable(idset);
|
2008-09-30 08:50:22 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_USBDEV_TRACE */
|