2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-09-01 01:04:02 +02:00
|
|
|
* drivers/usbhost/usbhost_devaddr.c
|
|
|
|
* Manage USB device addresses
|
|
|
|
*
|
2017-10-04 23:22:27 +02:00
|
|
|
* Copyright (C) 2013, 2015, 2017 Gregory Nutt. All rights reserved.
|
2014-09-01 01:04:02 +02:00
|
|
|
* Authors: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-09-01 01:04:02 +02:00
|
|
|
* Included Files
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2015-04-21 20:17:49 +02:00
|
|
|
#include <nuttx/usb/usbhost.h>
|
2014-09-01 01:04:02 +02:00
|
|
|
#include <nuttx/usb/usbhost_devaddr.h>
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-09-01 01:04:02 +02:00
|
|
|
* Pre-processor Definitions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-09-01 01:04:02 +02:00
|
|
|
* Private Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: usbhost_takesem and usbhost_givesem
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is just a wrapper to handle the annoying behavior of semaphore
|
|
|
|
* waits that return due to the receipt of a signal.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
static void usbhost_takesem(FAR struct usbhost_devaddr_s *devgen)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_wait_uninterruptible(&devgen->exclsem);
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
2017-10-03 23:35:24 +02:00
|
|
|
#define usbhost_givesem(devgen) nxsem_post(&devgen->exclsem)
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-09-01 01:04:02 +02:00
|
|
|
* Name: usbhost_devaddr_allocate
|
|
|
|
*
|
|
|
|
* Description:
|
2015-04-21 20:17:49 +02:00
|
|
|
* Allocate a new unique device address.
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Caller hold the exclsem
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
static int usbhost_devaddr_allocate(FAR struct usbhost_devaddr_s *devgen)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
uint8_t startaddr = devgen->next;
|
2014-09-01 01:04:02 +02:00
|
|
|
uint8_t devaddr;
|
|
|
|
int index;
|
|
|
|
int bitno;
|
|
|
|
|
|
|
|
/* Loop until we find a valid device address */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
for (; ; )
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
|
|
|
/* Try the next device address */
|
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
devaddr = devgen->next;
|
|
|
|
if (devgen->next >= 0x7f)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
devgen->next = 1;
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
devgen->next++;
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this address already allocated? */
|
|
|
|
|
|
|
|
index = devaddr >> 5;
|
|
|
|
bitno = devaddr & 0x1f;
|
2015-04-21 20:17:49 +02:00
|
|
|
if ((devgen->alloctab[index] & (1 << bitno)) == 0)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
|
|
|
/* No... allocate it now */
|
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
devgen->alloctab[index] |= (1 << bitno);
|
2014-09-01 01:04:02 +02:00
|
|
|
return (int)devaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This address has already been allocated. The followign logic will
|
|
|
|
* prevent (unexpected) infinite loops.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (startaddr == devaddr)
|
|
|
|
{
|
|
|
|
/* We are back where we started... the are no free device address */
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2015-04-21 20:17:49 +02:00
|
|
|
* Name: usbhost_devaddr_free
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-04-21 20:17:49 +02:00
|
|
|
* De-allocate a device address.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* Caller hold the exclsem
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
static void usbhost_devaddr_free(FAR struct usbhost_devaddr_s *devgen,
|
|
|
|
uint8_t devaddr)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
int index;
|
|
|
|
int bitno;
|
|
|
|
|
|
|
|
/* Free the address by clearing the associated bit in the alloctab[]; */
|
|
|
|
|
|
|
|
index = devaddr >> 5;
|
|
|
|
bitno = devaddr & 0x1f;
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
DEBUGASSERT((devgen->alloctab[index] |= (1 << bitno)) != 0);
|
|
|
|
devgen->alloctab[index] &= ~(1 << bitno);
|
2015-05-05 22:59:03 +02:00
|
|
|
|
|
|
|
/* Reset the next pointer if the one just released has a lower value */
|
|
|
|
|
|
|
|
if (devaddr < devgen->next)
|
|
|
|
{
|
|
|
|
devgen->next = devaddr;
|
|
|
|
}
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2015-04-21 20:17:49 +02:00
|
|
|
* Name: usbhost_roothubport
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-04-21 20:17:49 +02:00
|
|
|
* Find and return a reference the root hub port.
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
static inline FAR struct usbhost_roothubport_s *
|
|
|
|
usbhost_roothubport(FAR struct usbhost_hubport_s *hport)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-26 17:53:43 +02:00
|
|
|
#ifdef CONFIG_USBHOST_HUB
|
|
|
|
/* If this is a root hub port then the parent port pointer will be NULL.
|
|
|
|
* Otherwise, we need to traverse the parent pointer list until we find the
|
|
|
|
* root hub port.
|
|
|
|
*/
|
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
while (hport->parent != NULL)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-26 17:53:43 +02:00
|
|
|
/* This is not a root hub port. It is a port on a hub. Try the port of
|
|
|
|
* the parent hub that supports this port.
|
|
|
|
*/
|
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
hport = hport->parent;
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
2015-04-26 17:53:43 +02:00
|
|
|
#endif
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
return (FAR struct usbhost_roothubport_s *)hport;
|
|
|
|
}
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2015-04-21 20:17:49 +02:00
|
|
|
* Name: usbhost_devaddr_gen
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Find root hub port and return a reference to the device function address
|
|
|
|
* data set.
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
static FAR struct usbhost_devaddr_s *
|
|
|
|
usbhost_devaddr_gen(FAR struct usbhost_hubport_s *hport)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_roothubport_s *rhport;
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
rhport = usbhost_roothubport(hport);
|
|
|
|
if (rhport != NULL)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
return &rhport->devgen;
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2015-04-21 20:17:49 +02:00
|
|
|
* Public Functions
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2015-04-21 20:17:49 +02:00
|
|
|
* Name: usbhost_devaddr_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the caller provided struct usbhost_devaddr_s instance in
|
|
|
|
* preparation for the management of device addresses on behalf of an root
|
|
|
|
* hub port.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* rhport - A reference to a roothubport structure.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
void usbhost_devaddr_initialize(FAR struct usbhost_roothubport_s *rhport)
|
|
|
|
{
|
|
|
|
FAR struct usbhost_devaddr_s *devgen;
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
DEBUGASSERT(rhport);
|
|
|
|
devgen = &rhport->devgen;
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
memset(devgen, 0, sizeof(struct usbhost_devaddr_s));
|
2017-10-03 20:51:15 +02:00
|
|
|
nxsem_init(&devgen->exclsem, 0, 1);
|
2015-04-21 20:17:49 +02:00
|
|
|
devgen->next = 1;
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2015-04-21 20:17:49 +02:00
|
|
|
* Name: usbhost_devaddr_create
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2015-04-21 20:17:49 +02:00
|
|
|
* Create a new unique device address for this hub port.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* hport - A reference to a hub port structure to which a device has been
|
|
|
|
* newly connected and so is in need of a function address.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2017-05-11 21:35:56 +02:00
|
|
|
* On success, a new device function address in the range 0x01 to 0x7f
|
2015-04-24 00:42:53 +02:00
|
|
|
* is returned. On failure, a negated errno value is returned.
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
int usbhost_devaddr_create(FAR struct usbhost_hubport_s *hport)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
FAR struct usbhost_devaddr_s *devgen;
|
|
|
|
int devaddr;
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Get the address generation data from the root hub port */
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
DEBUGASSERT(hport);
|
|
|
|
devgen = usbhost_devaddr_gen(hport);
|
|
|
|
DEBUGASSERT(devgen);
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Get exclusive access to the root hub port device address data */
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
usbhost_takesem(devgen);
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Allocate a device address */
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
devaddr = usbhost_devaddr_allocate(devgen);
|
|
|
|
usbhost_givesem(devgen);
|
|
|
|
|
|
|
|
if (devaddr < 0)
|
|
|
|
{
|
2016-06-11 23:50:49 +02:00
|
|
|
uerr("ERROR: Failed to allocate a device address\n");
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 00:42:53 +02:00
|
|
|
return devaddr;
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-09-01 01:04:02 +02:00
|
|
|
* Name: usbhost_devaddr_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
2015-04-24 00:42:53 +02:00
|
|
|
* Release a device address previously assigned by usbhost_devaddr_create().
|
2015-04-21 20:17:49 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* hport - A reference to a hub port structure from which a device has been
|
|
|
|
* disconnected and so no longer needs the function address.
|
2015-04-24 00:42:53 +02:00
|
|
|
* devaddr - The address to be released.
|
2015-04-21 20:17:49 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-24 00:42:53 +02:00
|
|
|
void usbhost_devaddr_destroy(FAR struct usbhost_hubport_s *hport, uint8_t devaddr)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
FAR struct usbhost_devaddr_s *devgen;
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Ignore bad device address */
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-24 00:42:53 +02:00
|
|
|
if (devaddr > 0 && devaddr < 0x7f)
|
2014-09-01 01:04:02 +02:00
|
|
|
{
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Get the address generation data from the root hub port */
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
DEBUGASSERT(hport);
|
|
|
|
devgen = usbhost_devaddr_gen(hport);
|
|
|
|
DEBUGASSERT(devgen);
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Get exclusive access to the root hub port device address data */
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
usbhost_takesem(devgen);
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-21 20:17:49 +02:00
|
|
|
/* Free the device address */
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2015-04-24 00:42:53 +02:00
|
|
|
usbhost_devaddr_free(devgen, devaddr);
|
2015-04-21 20:17:49 +02:00
|
|
|
usbhost_givesem(devgen);
|
|
|
|
}
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|