2015-10-03 15:25:53 +02:00
|
|
|
/****************************************************************************
|
2014-09-01 01:04:02 +02:00
|
|
|
* drivers/usbhost/usbhost_devaddr.c
|
|
|
|
*
|
2020-03-30 22:20:53 +02: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
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
2020-03-30 22:20:53 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
2020-03-30 22:20:53 +02: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.
|
2014-09-01 01:04:02 +02:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2021-03-04 07:10:42 +01:00
|
|
|
/* Manage USB device addresses */
|
|
|
|
|
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>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2014-09-01 01:04:02 +02:00
|
|
|
#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
|
|
|
|
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:
|
2022-09-06 08:18:45 +02:00
|
|
|
* Caller hold the lock
|
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 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;
|
|
|
|
}
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* This address has already been allocated. The following logic will
|
2014-09-01 01:04:02 +02:00
|
|
|
* 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:
|
2022-09-06 08:18:45 +02:00
|
|
|
* Caller hold the lock
|
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
|
|
|
{
|
2020-03-30 22:20:53 +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-26 17:53:43 +02:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
{
|
2023-05-11 02:05:21 +02:00
|
|
|
return rhport->pdevgen;
|
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:
|
2023-05-11 02:05:21 +02:00
|
|
|
* devgen - A reference to a usbhost_devaddr_s structure.
|
2015-04-21 20:17:49 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2023-05-11 02:05:21 +02:00
|
|
|
* On success, zero (OK) is returned. On a failure, a negated errno value
|
|
|
|
* is returned indicating the nature of the failure.
|
2015-04-21 20:17:49 +02:00
|
|
|
*
|
2015-10-03 15:25:53 +02:00
|
|
|
****************************************************************************/
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2023-05-11 02:05:21 +02:00
|
|
|
int usbhost_devaddr_initialize(FAR struct usbhost_devaddr_s *devgen)
|
2015-04-21 20:17:49 +02:00
|
|
|
{
|
2023-05-11 02:05:21 +02:00
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
DEBUGASSERT(devgen);
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2023-05-11 02:05:21 +02:00
|
|
|
if (devgen)
|
|
|
|
{
|
|
|
|
memset(devgen, 0, sizeof(struct usbhost_devaddr_s));
|
|
|
|
nxmutex_init(&devgen->lock);
|
|
|
|
devgen->next = 1;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2014-09-01 01:04:02 +02:00
|
|
|
|
2023-05-11 02:05:21 +02:00
|
|
|
return ret;
|
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;
|
2020-03-30 22:20:53 +02:00
|
|
|
int ret;
|
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
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devgen->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
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);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devgen->lock);
|
2015-04-21 20:17:49 +02:00
|
|
|
|
|
|
|
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:
|
2020-03-30 22:20: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
|
|
|
|
2020-03-30 22:20: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;
|
2020-03-30 22:20:53 +02:00
|
|
|
int ret;
|
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
|
|
|
|
2020-03-30 22:20:53 +02:00
|
|
|
do
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&devgen->lock);
|
2020-03-30 22:20:53 +02:00
|
|
|
|
|
|
|
/* The only expected error would -ECANCELED meaning that the parent
|
|
|
|
* thread has been canceled. We have to continue and free the
|
|
|
|
* device address in this case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ret == OK || ret == -ECANCELED);
|
|
|
|
}
|
|
|
|
while (ret < 0);
|
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);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&devgen->lock);
|
2015-04-21 20:17:49 +02:00
|
|
|
}
|
2014-09-01 01:04:02 +02:00
|
|
|
}
|