Check return from nxsem_wait_initialize()

Resolution of Issue 619 will require multiple steps, this part of the first step in that resolution:  Every call to nxsem_wait_uninterruptible() must handle the return value from nxsem_wait_uninterruptible properly.  This commit is only for those files under drivers/usbhost.
This commit is contained in:
Gregory Nutt
2020-03-30 14:20:53 -06:00
committed by Abdelatif Guettouche
parent 2d13ea7477
commit d9b42cebe7
8 changed files with 1592 additions and 974 deletions
+101 -60
View File
@@ -1,36 +1,20 @@
/**************************************************************************** /****************************************************************************
* drivers/usbhost/usbhost_cdcacm.c * drivers/usbhost/usbhost_cdcacm.c
* *
* Copyright (C) 2015-2017, 2019 Gregory Nutt. All rights reserved. * Licensed to the Apache Software Foundation (ASF) under one or more
* Author: Gregory Nutt <gnutt@nuttx.org> * 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
* *
* Redistribution and use in source and binary forms, with or without * http://www.apache.org/licenses/LICENSE-2.0
* modification, are permitted provided that the following conditions
* are met:
* *
* 1. Redistributions of source code must retain the above copyright * Unless required by applicable law or agreed to in writing, software
* notice, this list of conditions and the following disclaimer. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* 2. Redistributions in binary form must reproduce the above copyright * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* 2. Redistributions in binary form must reproduce the above copyright * License for the specific language governing permissions and limitations
* notice, this list of conditions and the following disclaimer in * under the License.
* 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.
* *
****************************************************************************/ ****************************************************************************/
@@ -303,7 +287,8 @@ struct usbhost_freestate_s
/* Semaphores */ /* Semaphores */
static void usbhost_takesem(FAR sem_t *sem); static int usbhost_takesem(FAR sem_t *sem);
static void usbhost_forcetake(FAR sem_t *sem);
#define usbhost_givesem(s) nxsem_post(s); #define usbhost_givesem(s) nxsem_post(s);
/* Memory allocation services */ /* Memory allocation services */
@@ -374,7 +359,8 @@ static int usbhost_setup(FAR struct uart_dev_s *uartdev);
static void usbhost_shutdown(FAR struct uart_dev_s *uartdev); static void usbhost_shutdown(FAR struct uart_dev_s *uartdev);
static int usbhost_attach(FAR struct uart_dev_s *uartdev); static int usbhost_attach(FAR struct uart_dev_s *uartdev);
static void usbhost_detach(FAR struct uart_dev_s *uartdev); static void usbhost_detach(FAR struct uart_dev_s *uartdev);
static int usbhost_ioctl(FAR struct file *filep, int cmd, unsigned long arg); static int usbhost_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
static void usbhost_rxint(FAR struct uart_dev_s *uartdev, bool enable); static void usbhost_rxint(FAR struct uart_dev_s *uartdev, bool enable);
static bool usbhost_rxavailable(FAR struct uart_dev_s *uartdev); static bool usbhost_rxavailable(FAR struct uart_dev_s *uartdev);
#ifdef CONFIG_SERIAL_IFLOWCONTROL #ifdef CONFIG_SERIAL_IFLOWCONTROL
@@ -486,9 +472,37 @@ static uint32_t g_devinuse;
* *
****************************************************************************/ ****************************************************************************/
static void usbhost_takesem(FAR sem_t *sem) static int usbhost_takesem(FAR sem_t *sem)
{ {
nxsem_wait_uninterruptible(sem); return nxsem_wait_uninterruptible(sem);
}
/****************************************************************************
* Name: usbhost_forcetake
*
* Description:
* This is just another wrapper but this one continues even if the thread
* is canceled. This must be done in certain conditions where were must
* continue in order to clean-up resources.
*
****************************************************************************/
static void usbhost_forcetake(FAR sem_t *sem)
{
int ret;
do
{
ret = nxsem_wait_uninterruptible(sem);
/* The only expected error would -ECANCELED meaning that the
* parent thread has been canceled. We have to continue and
* terminate the poll in this case.
*/
DEBUGASSERT(ret == OK || ret == -ECANCELED);
}
while (ret < 0);
} }
/**************************************************************************** /****************************************************************************
@@ -983,7 +997,8 @@ static void usbhost_txdata_work(FAR void *arg)
* the device is disconnected). * the device is disconnected).
*/ */
uerr("ERROR: DRVR_TRANSFER for packet failed: %d\n", (int)nwritten); uerr("ERROR: DRVR_TRANSFER for packet failed: %d\n",
(int)nwritten);
break; break;
} }
} }
@@ -1315,8 +1330,8 @@ static void usbhost_destroy(FAR void *arg)
* desclen - The length in bytes of the configuration descriptor. * desclen - The length in bytes of the configuration descriptor.
* *
* Returned Value: * Returned Value:
* On success, zero (OK) is returned. On a failure, a negated errno value is * On success, zero (OK) is returned. On a failure, a negated errno value
* returned indicating the nature of the failure * is returned indicating the nature of the failure
* *
* Assumptions: * Assumptions:
* This function will *not* be called from an interrupt handler. * This function will *not* be called from an interrupt handler.
@@ -1386,7 +1401,8 @@ static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
case USB_DESC_TYPE_INTERFACE: case USB_DESC_TYPE_INTERFACE:
{ {
FAR struct usb_ifdesc_s *ifdesc = (FAR struct usb_ifdesc_s *)configdesc; FAR struct usb_ifdesc_s *ifdesc =
(FAR struct usb_ifdesc_s *)configdesc;
uinfo("Interface descriptor: class: %d subclass: %d proto: %d\n", uinfo("Interface descriptor: class: %d subclass: %d proto: %d\n",
ifdesc->classid, ifdesc->subclass, ifdesc->protocol); ifdesc->classid, ifdesc->subclass, ifdesc->protocol);
@@ -1433,7 +1449,8 @@ static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
case USB_DESC_TYPE_ENDPOINT: case USB_DESC_TYPE_ENDPOINT:
{ {
FAR struct usb_epdesc_s *epdesc = (FAR struct usb_epdesc_s *)configdesc; FAR struct usb_epdesc_s *epdesc =
(FAR struct usb_epdesc_s *)configdesc;
uinfo("Endpoint descriptor: currif: %02x attr: %02x\n", uinfo("Endpoint descriptor: currif: %02x attr: %02x\n",
currif, epdesc->attr); currif, epdesc->attr);
@@ -1442,7 +1459,8 @@ static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
/* Check for a bulk endpoint. */ /* Check for a bulk endpoint. */
if (currif == USBHOST_DATAIF_FOUND && if (currif == USBHOST_DATAIF_FOUND &&
(epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) == USB_EP_ATTR_XFER_BULK) (epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
USB_EP_ATTR_XFER_BULK)
{ {
/* Yes.. it is a bulk endpoint. IN or OUT? */ /* Yes.. it is a bulk endpoint. IN or OUT? */
@@ -1466,11 +1484,13 @@ static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
/* Save the bulk OUT endpoint information */ /* Save the bulk OUT endpoint information */
boutdesc.hport = hport; boutdesc.hport = hport;
boutdesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK; boutdesc.addr = epdesc->addr &
USB_EP_ADDR_NUMBER_MASK;
boutdesc.in = false; boutdesc.in = false;
boutdesc.xfrtype = USB_EP_ATTR_XFER_BULK; boutdesc.xfrtype = USB_EP_ATTR_XFER_BULK;
boutdesc.interval = epdesc->interval; boutdesc.interval = epdesc->interval;
boutdesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize); boutdesc.mxpacketsize =
usbhost_getle16(epdesc->mxpacketsize);
uinfo("Bulk OUT EP addr:%d mxpacketsize:%d\n", uinfo("Bulk OUT EP addr:%d mxpacketsize:%d\n",
boutdesc.addr, boutdesc.mxpacketsize); boutdesc.addr, boutdesc.mxpacketsize);
@@ -1495,11 +1515,14 @@ static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
/* Save the bulk IN endpoint information */ /* Save the bulk IN endpoint information */
bindesc.hport = hport; bindesc.hport = hport;
bindesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK; bindesc.addr = epdesc->addr &
USB_EP_ADDR_NUMBER_MASK;
bindesc.in = 1; bindesc.in = 1;
bindesc.xfrtype = USB_EP_ATTR_XFER_BULK; bindesc.xfrtype = USB_EP_ATTR_XFER_BULK;
bindesc.interval = epdesc->interval; bindesc.interval = epdesc->interval;
bindesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize); bindesc.mxpacketsize =
usbhost_getle16(epdesc->mxpacketsize);
uinfo("Bulk IN EP addr:%d mxpacketsize:%d\n", uinfo("Bulk IN EP addr:%d mxpacketsize:%d\n",
bindesc.addr, bindesc.mxpacketsize); bindesc.addr, bindesc.mxpacketsize);
} }
@@ -1539,11 +1562,13 @@ static int usbhost_cfgdesc(FAR struct usbhost_cdcacm_s *priv,
/* Save the bulk OUT endpoint information */ /* Save the bulk OUT endpoint information */
iindesc.hport = hport; iindesc.hport = hport;
iindesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK; iindesc.addr = epdesc->addr &
USB_EP_ADDR_NUMBER_MASK;
iindesc.in = false; iindesc.in = false;
iindesc.xfrtype = USB_EP_ATTR_XFER_INT; iindesc.xfrtype = USB_EP_ATTR_XFER_INT;
iindesc.interval = epdesc->interval; iindesc.interval = epdesc->interval;
iindesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize); iindesc.mxpacketsize =
usbhost_getle16(epdesc->mxpacketsize);
uinfo("Interrupt IN EP addr:%d mxpacketsize:%d\n", uinfo("Interrupt IN EP addr:%d mxpacketsize:%d\n",
boutdesc.addr, boutdesc.mxpacketsize); boutdesc.addr, boutdesc.mxpacketsize);
@@ -1865,12 +1890,13 @@ static void usbhost_free_buffers(FAR struct usbhost_cdcacm_s *priv)
* Name: usbhost_create * Name: usbhost_create
* *
* Description: * Description:
* This function implements the create() method of struct usbhost_registry_s. * This function implements the create() method of struct
* The create() method is a callback into the class implementation. It is * usbhost_registry_s. The create() method is a callback into the class
* used to (1) create a new instance of the USB host class state and to (2) * implementation. It is used to (1) create a new instance of the USB
* bind a USB host driver "session" to the class instance. Use of this * host class state and to (2) bind a USB host driver "session" to the
* create() method will support environments where there may be multiple * class instance. Use of this create() method will support environments
* USB ports and multiple USB devices simultaneously connected. * where there may be multiple USB ports and multiple USB devices
* simultaneously connected.
* *
* Input Parameters: * Input Parameters:
* hport - The hub port that manages the new class instance. * hport - The hub port that manages the new class instance.
@@ -1978,8 +2004,8 @@ usbhost_create(FAR struct usbhost_hubport_s *hport,
* desclen - The length in bytes of the configuration descriptor. * desclen - The length in bytes of the configuration descriptor.
* *
* Returned Value: * Returned Value:
* On success, zero (OK) is returned. On a failure, a negated errno value is * On success, zero (OK) is returned. On a failure, a negated errno value
* returned indicating the nature of the failure * is returned indicating the nature of the failure
* *
* NOTE that the class instance remains valid upon return with a failure. * NOTE that the class instance remains valid upon return with a failure.
* It is the responsibility of the higher level enumeration logic to call * It is the responsibility of the higher level enumeration logic to call
@@ -1995,7 +2021,8 @@ usbhost_create(FAR struct usbhost_hubport_s *hport,
static int usbhost_connect(FAR struct usbhost_class_s *usbclass, static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
FAR const uint8_t *configdesc, int desclen) FAR const uint8_t *configdesc, int desclen)
{ {
FAR struct usbhost_cdcacm_s *priv = (FAR struct usbhost_cdcacm_s *)usbclass; FAR struct usbhost_cdcacm_s *priv =
(FAR struct usbhost_cdcacm_s *)usbclass;
#ifdef HAVE_INTIN_ENDPOINT #ifdef HAVE_INTIN_ENDPOINT
FAR struct usbhost_hubport_s *hport; FAR struct usbhost_hubport_s *hport;
#endif #endif
@@ -2013,7 +2040,11 @@ static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
/* Get exclusive access to the device structure */ /* Get exclusive access to the device structure */
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Increment the reference count. This will prevent usbhost_destroy() from /* Increment the reference count. This will prevent usbhost_destroy() from
* being called asynchronously if the device is removed. * being called asynchronously if the device is removed.
@@ -2128,7 +2159,8 @@ errout:
static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass) static int usbhost_disconnected(FAR struct usbhost_class_s *usbclass)
{ {
FAR struct usbhost_cdcacm_s *priv = (FAR struct usbhost_cdcacm_s *)usbclass; FAR struct usbhost_cdcacm_s *priv =
(FAR struct usbhost_cdcacm_s *)usbclass;
FAR struct usbhost_hubport_s *hport; FAR struct usbhost_hubport_s *hport;
irqstate_t flags; irqstate_t flags;
int ret; int ret;
@@ -2238,7 +2270,11 @@ static int usbhost_setup(FAR struct uart_dev_s *uartdev)
/* Make sure that we have exclusive access to the private data structure */ /* Make sure that we have exclusive access to the private data structure */
DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Check if the CDC/ACM device is still connected. We need to disable /* Check if the CDC/ACM device is still connected. We need to disable
* interrupts momentarily to assure that there are no asynchronous * interrupts momentarily to assure that there are no asynchronous
@@ -2289,7 +2325,7 @@ static void usbhost_shutdown(FAR struct uart_dev_s *uartdev)
/* Decrement the reference count on the block driver */ /* Decrement the reference count on the block driver */
DEBUGASSERT(priv->crefs > 1); DEBUGASSERT(priv->crefs > 1);
usbhost_takesem(&priv->exclsem); usbhost_forcetake(&priv->exclsem);
priv->crefs--; priv->crefs--;
/* Release the semaphore. The following operations when crefs == 1 are /* Release the semaphore. The following operations when crefs == 1 are
@@ -2348,8 +2384,8 @@ static int usbhost_attach(FAR struct uart_dev_s *uartdev)
* *
* Description: * Description:
* Detach USART interrupts. This method is called when the serial port is * Detach USART interrupts. This method is called when the serial port is
* closed normally just before the shutdown method is called. The exception * closed normally just before the shutdown method is called. The
* is the serial console which is never shutdown. * exception is the serial console which is never shutdown.
* *
****************************************************************************/ ****************************************************************************/
@@ -2396,7 +2432,12 @@ static int usbhost_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{ {
/* Process the IOCTL by command */ /* Process the IOCTL by command */
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
switch (cmd) switch (cmd)
{ {
#ifdef CONFIG_SERIAL_TIOCSERGSTRUCT #ifdef CONFIG_SERIAL_TIOCSERGSTRUCT
+39 -35
View File
@@ -2,35 +2,20 @@
* drivers/usbhost/usbhost_devaddr.c * drivers/usbhost/usbhost_devaddr.c
* Manage USB device addresses * Manage USB device addresses
* *
* Copyright (C) 2013, 2015, 2017 Gregory Nutt. All rights reserved. * Licensed to the Apache Software Foundation (ASF) under one or more
* Authors: Gregory Nutt <gnutt@nuttx.org> * 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
* *
* Redistribution and use in source and binary forms, with or without * http://www.apache.org/licenses/LICENSE-2.0
* modification, are permitted provided that the following conditions
* are met:
* *
* 1. Redistributions of source code must retain the above copyright * Unless required by applicable law or agreed to in writing, software
* notice, this list of conditions and the following disclaimer. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* 2. Redistributions in binary form must reproduce the above copyright * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* notice, this list of conditions and the following disclaimer in * License for the specific language governing permissions and limitations
* the documentation and/or other materials provided with the * under the License.
* 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.
* *
****************************************************************************/ ****************************************************************************/
@@ -65,9 +50,9 @@
* *
****************************************************************************/ ****************************************************************************/
static void usbhost_takesem(FAR struct usbhost_devaddr_s *devgen) static int usbhost_takesem(FAR struct usbhost_devaddr_s *devgen)
{ {
nxsem_wait_uninterruptible(&devgen->exclsem); return nxsem_wait_uninterruptible(&devgen->exclsem);
} }
#define usbhost_givesem(devgen) nxsem_post(&devgen->exclsem) #define usbhost_givesem(devgen) nxsem_post(&devgen->exclsem)
@@ -183,8 +168,8 @@ usbhost_roothubport(FAR struct usbhost_hubport_s *hport)
while (hport->parent != NULL) while (hport->parent != NULL)
{ {
/* This is not a root hub port. It is a port on a hub. Try the port of /* This is not a root hub port. It is a port on a hub. Try the port
* the parent hub that supports this port. * of the parent hub that supports this port.
*/ */
hport = hport->parent; hport = hport->parent;
@@ -269,6 +254,7 @@ int usbhost_devaddr_create(FAR struct usbhost_hubport_s *hport)
{ {
FAR struct usbhost_devaddr_s *devgen; FAR struct usbhost_devaddr_s *devgen;
int devaddr; int devaddr;
int ret;
/* Get the address generation data from the root hub port */ /* Get the address generation data from the root hub port */
@@ -278,7 +264,11 @@ int usbhost_devaddr_create(FAR struct usbhost_hubport_s *hport)
/* Get exclusive access to the root hub port device address data */ /* Get exclusive access to the root hub port device address data */
usbhost_takesem(devgen); ret = usbhost_takesem(devgen);
if (ret < 0)
{
return ret;
}
/* Allocate a device address */ /* Allocate a device address */
@@ -297,7 +287,8 @@ int usbhost_devaddr_create(FAR struct usbhost_hubport_s *hport)
* Name: usbhost_devaddr_destroy * Name: usbhost_devaddr_destroy
* *
* Description: * Description:
* Release a device address previously assigned by usbhost_devaddr_create(). * Release a device address previously assigned by
* usbhost_devaddr_create().
* *
* Input Parameters: * Input Parameters:
* hport - A reference to a hub port structure from which a device has been * hport - A reference to a hub port structure from which a device has been
@@ -309,9 +300,11 @@ int usbhost_devaddr_create(FAR struct usbhost_hubport_s *hport)
* *
****************************************************************************/ ****************************************************************************/
void usbhost_devaddr_destroy(FAR struct usbhost_hubport_s *hport, uint8_t devaddr) void usbhost_devaddr_destroy(FAR struct usbhost_hubport_s *hport,
uint8_t devaddr)
{ {
FAR struct usbhost_devaddr_s *devgen; FAR struct usbhost_devaddr_s *devgen;
int ret;
/* Ignore bad device address */ /* Ignore bad device address */
@@ -325,7 +318,18 @@ void usbhost_devaddr_destroy(FAR struct usbhost_hubport_s *hport, uint8_t devadd
/* Get exclusive access to the root hub port device address data */ /* Get exclusive access to the root hub port device address data */
usbhost_takesem(devgen); do
{
ret = usbhost_takesem(devgen);
/* 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);
/* Free the device address */ /* Free the device address */
File diff suppressed because it is too large Load Diff
+163 -98
View File
@@ -1,35 +1,20 @@
/**************************************************************************** /****************************************************************************
* drivers/usbhost/usbhost_hidmouse.c * drivers/usbhost/usbhost_hidmouse.c
* *
* Copyright (C) 2014, 2015-2017 Gregory Nutt. All rights reserved. * Licensed to the Apache Software Foundation (ASF) under one or more
* Author: Gregory Nutt <gnutt@nuttx.org> * 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
* *
* Redistribution and use in source and binary forms, with or without * http://www.apache.org/licenses/LICENSE-2.0
* modification, are permitted provided that the following conditions
* are met:
* *
* 1. Redistributions of source code must retain the above copyright * Unless required by applicable law or agreed to in writing, software
* notice, this list of conditions and the following disclaimer. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* 2. Redistributions in binary form must reproduce the above copyright * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* notice, this list of conditions and the following disclaimer in * License for the specific language governing permissions and limitations
* the documentation and/or other materials provided with the * under the License.
* 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.
* *
****************************************************************************/ ****************************************************************************/
@@ -301,7 +286,8 @@ struct usbhost_state_s
/* Semaphores */ /* Semaphores */
static void usbhost_takesem(sem_t *sem); static int usbhost_takesem(FAR sem_t *sem);
static void usbhost_forcetake(FAR sem_t *sem);
#define usbhost_givesem(s) nxsem_post(s); #define usbhost_givesem(s) nxsem_post(s);
/* Polling support */ /* Polling support */
@@ -443,9 +429,37 @@ static struct usbhost_state_s *g_priv; /* Data passed to thread */
* *
****************************************************************************/ ****************************************************************************/
static void usbhost_takesem(sem_t *sem) static int usbhost_takesem(FAR sem_t *sem)
{ {
nxsem_wait_uninterruptible(sem); return nxsem_wait_uninterruptible(sem);
}
/****************************************************************************
* Name: usbhost_forcetake
*
* Description:
* This is just another wrapper but this one continues even if the thread
* is canceled. This must be done in certain conditions where were must
* continue in order to clean-up resources.
*
****************************************************************************/
static void usbhost_forcetake(FAR sem_t *sem)
{
int ret;
do
{
ret = nxsem_wait_uninterruptible(sem);
/* The only expected error would -ECANCELED meaning that the
* parent thread has been canceled. We have to continue and
* terminate the poll in this case.
*/
DEBUGASSERT(ret == OK || ret == -ECANCELED);
}
while (ret < 0);
} }
/**************************************************************************** /****************************************************************************
@@ -495,7 +509,10 @@ static inline FAR struct usbhost_state_s *usbhost_allocclass(void)
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
DEBUGASSERT(!up_interrupt_context()); DEBUGASSERT(!up_interrupt_context());
priv = (FAR struct usbhost_state_s *)kmm_malloc(sizeof(struct usbhost_state_s));
priv = (FAR struct usbhost_state_s *)
kmm_malloc(sizeof(struct usbhost_state_s));
uinfo("Allocated: %p\n", priv); uinfo("Allocated: %p\n", priv);
return priv; return priv;
} }
@@ -666,10 +683,10 @@ static void usbhost_notify(FAR struct usbhost_state_s *priv)
nxsem_post(&priv->waitsem); nxsem_post(&priv->waitsem);
} }
/* If there are threads waiting on poll() for mouse data to become available, /* If there are threads waiting on poll() for mouse data to become
* then wake them up now. NOTE: we wake up all waiting threads because we * available, then wake them up now. NOTE: we wake up all waiting
* do not know that they are going to do. If they all try to read the data, * threads because we do not know that they are going to do. If they
* then some make end up blocking after all. * all try to read the data, then some make end up blocking after all.
*/ */
for (i = 0; i < CONFIG_HIDMOUSE_NPOLLWAITERS; i++) for (i = 0; i < CONFIG_HIDMOUSE_NPOLLWAITERS; i++)
@@ -1030,10 +1047,10 @@ static int usbhost_mouse_poll(int argc, char *argv[])
* the start-up logic, and wait a bit to make sure that all of the class * the start-up logic, and wait a bit to make sure that all of the class
* creation logic has a chance to run to completion. * creation logic has a chance to run to completion.
* *
* NOTE: that the reference count is *not* incremented here. When the driver * NOTE: that the reference count is *not* incremented here. When the
* structure was created, it was created with a reference count of one. This * driver structure was created, it was created with a reference count of
* thread is responsible for that count. The count will be decrement when * one. This thread is responsible for that count. The count will be
* this thread exits. * decremented when this thread exits.
*/ */
priv = g_priv; priv = g_priv;
@@ -1097,7 +1114,13 @@ static int usbhost_mouse_poll(int argc, char *argv[])
{ {
/* Get exclusive access to the mouse state data */ /* Get exclusive access to the mouse state data */
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
/* Break out and disconnect if the thread is canceled. */
break;
}
/* Get the HID mouse report */ /* Get the HID mouse report */
@@ -1165,13 +1188,13 @@ static int usbhost_mouse_poll(int argc, char *argv[])
usbhost_notify(priv); usbhost_notify(priv);
} }
/* Release our lock on the state structure */
usbhost_givesem(&priv->exclsem);
} }
} }
/* Release our lock on the state structure */
usbhost_givesem(&priv->exclsem);
/* If USB debug is on, then provide some periodic indication that /* If USB debug is on, then provide some periodic indication that
* polling is still happening. * polling is still happening.
*/ */
@@ -1185,15 +1208,15 @@ static int usbhost_mouse_poll(int argc, char *argv[])
#endif #endif
} }
/* We get here when the driver is removed.. or when too many errors have /* We get here when the driver is removed, when too many errors have
* been encountered. * been encountered, or the parent thread has been canceled.
* *
* Make sure that we have exclusive access to the private data structure. * Make sure that we have exclusive access to the private data structure.
* There may now be other tasks with the character driver open and actively * There may now be other tasks with the character driver open and actively
* trying to interact with the class driver. * trying to interact with the class driver.
*/ */
usbhost_takesem(&priv->exclsem); usbhost_forcetake(&priv->exclsem);
/* Indicate that we are no longer running and decrement the reference /* Indicate that we are no longer running and decrement the reference
* count held by this thread. If there are no other users of the class, * count held by this thread. If there are no other users of the class,
@@ -1370,8 +1393,8 @@ static int usbhost_waitsample(FAR struct usbhost_state_s *priv,
iinfo("Sampled\n"); iinfo("Sampled\n");
/* Re-acquire the semaphore that manages mutually exclusive access to /* Re-acquire the semaphore that manages mutually exclusive access to
* the device structure. We may have to wait here. But we have our sample. * the device structure. We may have to wait here. But we have our
* Interrupts and pre-emption will be re-enabled while we wait. * sample. Interrupts and pre-emption will be re-enabled while we wait.
*/ */
ret = nxsem_wait(&priv->exclsem); ret = nxsem_wait(&priv->exclsem);
@@ -1410,8 +1433,8 @@ errout:
* desclen - The length in bytes of the configuration descriptor. * desclen - The length in bytes of the configuration descriptor.
* *
* Returned Value: * Returned Value:
* On success, zero (OK) is returned. On a failure, a negated errno value is * On success, zero (OK) is returned. On a failure, a negated errno value
* returned indicating the nature of the failure * is returned indicating the nature of the failure
* *
* Assumptions: * Assumptions:
* This function will *not* be called from an interrupt handler. * This function will *not* be called from an interrupt handler.
@@ -1506,14 +1529,16 @@ static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
case USB_DESC_TYPE_ENDPOINT: case USB_DESC_TYPE_ENDPOINT:
{ {
FAR struct usb_epdesc_s *epdesc = (FAR struct usb_epdesc_s *)configdesc; FAR struct usb_epdesc_s *epdesc =
(FAR struct usb_epdesc_s *)configdesc;
uinfo("Endpoint descriptor\n"); uinfo("Endpoint descriptor\n");
DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC); DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC);
/* Check for an interrupt endpoint. */ /* Check for an interrupt endpoint. */
if ((epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) == USB_EP_ATTR_XFER_INT) if ((epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
USB_EP_ATTR_XFER_INT)
{ {
/* Yes.. it is a interrupt endpoint. IN or OUT? */ /* Yes.. it is a interrupt endpoint. IN or OUT? */
@@ -1537,11 +1562,13 @@ static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
/* Save the interrupt IN endpoint information */ /* Save the interrupt IN endpoint information */
epindesc.hport = hport; epindesc.hport = hport;
epindesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK; epindesc.addr = epdesc->addr &
USB_EP_ADDR_NUMBER_MASK;
epindesc.in = true; epindesc.in = true;
epindesc.xfrtype = USB_EP_ATTR_XFER_INT; epindesc.xfrtype = USB_EP_ATTR_XFER_INT;
epindesc.interval = epdesc->interval; epindesc.interval = epdesc->interval;
epindesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize); epindesc.mxpacketsize =
usbhost_getle16(epdesc->mxpacketsize);
uinfo("Interrupt IN EP addr:%d mxpacketsize:%d\n", uinfo("Interrupt IN EP addr:%d mxpacketsize:%d\n",
epindesc.addr, epindesc.mxpacketsize); epindesc.addr, epindesc.mxpacketsize);
@@ -1635,25 +1662,32 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv)
priv->crefs++; priv->crefs++;
DEBUGASSERT(priv->crefs == 2); DEBUGASSERT(priv->crefs == 2);
/* Start a worker task to poll the USB device. It would be nice to used the /* Start a worker task to poll the USB device. It would be nice to use
* the NuttX worker thread to do this, but this task needs to wait for events * the NuttX worker thread to do this, but this task needs to wait for
* and activities on the worker thread should not involve significant waiting. * events and activities on the worker thread should not involve
* Having a dedicated thread is more efficient in this sense, but requires more * significant waiting. Having a dedicated thread is more efficient in
* memory resources, primarily for the dedicated stack (CONFIG_HIDMOUSE_STACKSIZE). * this sense, but requires more memory resources, primarily for the
* dedicated stack (CONFIG_HIDMOUSE_STACKSIZE).
*/ */
uinfo("Start poll task\n"); uinfo("Start poll task\n");
/* The inputs to a task started by kthread_create() are very awkward for this /* The inputs to a task started by kthread_create() are very awkward for
* purpose. They are really designed for command line tasks (argc/argv). So * this purpose. They are really designed for command line tasks
* the following is kludge pass binary data when the mouse poll task * (argc/argv). So the following is kludge pass binary data when the
* is started. * mouse poll task is started.
* *
* First, make sure we have exclusive access to g_priv (what is the likelihood * First, make sure we have exclusive access to g_priv (what is the
* of this being used? About zero, but we protect it anyway). * likelihood of this being used? About zero, but we protect it anyway).
*/ */
usbhost_takesem(&g_exclsem); ret = usbhost_takesem(&g_exclsem);
if (ret < 0)
{
usbhost_tdfree(priv);
goto errout;
}
g_priv = priv; g_priv = priv;
priv->pollpid = kthread_create("mouse", CONFIG_HIDMOUSE_DEFPRIO, priv->pollpid = kthread_create("mouse", CONFIG_HIDMOUSE_DEFPRIO,
@@ -1671,9 +1705,14 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv)
/* Now wait for the poll task to get properly initialized */ /* Now wait for the poll task to get properly initialized */
usbhost_takesem(&g_syncsem); ret = usbhost_takesem(&g_syncsem);
usbhost_givesem(&g_exclsem); usbhost_givesem(&g_exclsem);
if (ret < 0)
{
goto errout;
}
/* Register the driver */ /* Register the driver */
uinfo("Register driver\n"); uinfo("Register driver\n");
@@ -1685,9 +1724,10 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv)
*/ */
errout: errout:
usbhost_takesem(&priv->exclsem); usbhost_forcetake(&priv->exclsem);
priv->crefs--; priv->crefs--;
usbhost_givesem(&priv->exclsem); usbhost_givesem(&priv->exclsem);
return ret; return ret;
} }
@@ -1750,7 +1790,8 @@ static inline uint32_t usbhost_getle32(const uint8_t *val)
{ {
/* Little endian means LS halfword first in byte stream */ /* Little endian means LS halfword first in byte stream */
return (uint32_t)usbhost_getle16(&val[2]) << 16 | (uint32_t)usbhost_getle16(val); return (uint32_t)usbhost_getle16(&val[2]) << 16 |
(uint32_t)usbhost_getle16(val);
} }
/**************************************************************************** /****************************************************************************
@@ -1845,12 +1886,13 @@ static inline int usbhost_tdfree(FAR struct usbhost_state_s *priv)
* Name: usbhost_create * Name: usbhost_create
* *
* Description: * Description:
* This function implements the create() method of struct usbhost_registry_s. * This function implements the create() method of struct
* The create() method is a callback into the class implementation. It is * usbhost_registry_s. The create() method is a callback into the class
* used to (1) create a new instance of the USB host class state and to (2) * implementation. It is used to (1) create a new instance of the USB
* bind a USB host driver "session" to the class instance. Use of this * host class state and to (2) bind a USB host driver "session" to the
* create() method will support environments where there may be multiple * class instance. Use of this create() method will support environments
* USB ports and multiple USB devices simultaneously connected. * where there may be multiple USB ports and multiple USB devices
* simultaneously connected.
* *
* Input Parameters: * Input Parameters:
* hport - The hub port that manages the new class instance. * hport - The hub port that manages the new class instance.
@@ -1945,11 +1987,11 @@ static FAR struct usbhost_class_s *
* desclen - The length in bytes of the configuration descriptor. * desclen - The length in bytes of the configuration descriptor.
* *
* Returned Value: * Returned Value:
* On success, zero (OK) is returned. On a failure, a negated errno value is * On success, zero (OK) is returned. On a failure, a negated errno value
* returned indicating the nature of the failure * is returned indicating the nature of the failure
* *
* NOTE that the class instance remains valid upon return with a failure. It is * NOTE that the class instance remains valid upon return with a failure.
* the responsibility of the higher level enumeration logic to call * It is the responsibility of the higher level enumeration logic to call
* CLASS_DISCONNECTED to free up the class driver resources. * CLASS_DISCONNECTED to free up the class driver resources.
* *
* Assumptions: * Assumptions:
@@ -1992,8 +2034,9 @@ static int usbhost_connect(FAR struct usbhost_class_s *usbclass,
* - Failure occurred before the mouse poll task was started successfully. * - Failure occurred before the mouse poll task was started successfully.
* In this case, the disconnection will have to be handled on the worker * In this case, the disconnection will have to be handled on the worker
* task. * task.
* - Failure occurred after the mouse poll task was started successfully. In * - Failure occurred after the mouse poll task was started successfully.
* this case, the disconnection can be performed on the mouse poll thread. * In this case, the disconnection can be performed on the mouse poll
* thread.
*/ */
return ret; return ret;
@@ -2049,8 +2092,9 @@ static int usbhost_disconnected(struct usbhost_class_s *usbclass)
* - Failure occurred before the mouse poll task was started successfully. * - Failure occurred before the mouse poll task was started successfully.
* In this case, the disconnection will have to be handled on the worker * In this case, the disconnection will have to be handled on the worker
* task. * task.
* - Failure occurred after the mouse poll task was started successfully. In * - Failure occurred after the mouse poll task was started successfully.
* this case, the disconnection can be performed on the mouse poll thread. * In this case, the disconnection can be performed on the mouse poll
* thread.
*/ */
if (priv->polling) if (priv->polling)
@@ -2104,11 +2148,15 @@ static int usbhost_open(FAR struct file *filep)
/* Make sure that we have exclusive access to the private data structure */ /* Make sure that we have exclusive access to the private data structure */
DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Check if the mouse device is still connected. We need to disable /* Check if the mouse device is still connected. We need to disable
* interrupts momentarily to assure that there are no asynchronous disconnect * interrupts momentarily to assure that there are no asynchronous
* events. * disconnect events.
*/ */
flags = enter_critical_section(); flags = enter_critical_section();
@@ -2170,6 +2218,7 @@ static int usbhost_close(FAR struct file *filep)
FAR struct inode *inode; FAR struct inode *inode;
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
irqstate_t flags; irqstate_t flags;
int ret;
uinfo("Entry\n"); uinfo("Entry\n");
DEBUGASSERT(filep && filep->f_inode); DEBUGASSERT(filep && filep->f_inode);
@@ -2179,7 +2228,11 @@ static int usbhost_close(FAR struct file *filep)
/* Decrement the reference count on the driver */ /* Decrement the reference count on the driver */
DEBUGASSERT(priv->crefs >= 1); DEBUGASSERT(priv->crefs >= 1);
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* We need to disable interrupts momentarily to assure that there are no /* We need to disable interrupts momentarily to assure that there are no
* asynchronous poll or disconnect events. * asynchronous poll or disconnect events.
@@ -2200,9 +2253,9 @@ static int usbhost_close(FAR struct file *filep)
* *
* 1) It might be zero meaning that the polling thread has already * 1) It might be zero meaning that the polling thread has already
* exited and decremented its count. * exited and decremented its count.
* 2) If might be one meaning either that (a) the polling thread is still * 2) If might be one meaning either that (a) the polling thread is
* running and still holds a count, or (b) the polling thread has exited, * still running and still holds a count, or (b) the polling thread
* but there is still an outstanding open reference. * has exited, but there is still an outstanding open reference.
*/ */
if (priv->crefs == 0 || (priv->crefs == 1 && priv->polling)) if (priv->crefs == 0 || (priv->crefs == 1 && priv->polling))
@@ -2255,7 +2308,8 @@ static int usbhost_close(FAR struct file *filep)
* *
****************************************************************************/ ****************************************************************************/
static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, size_t len) static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer,
size_t len)
{ {
FAR struct inode *inode; FAR struct inode *inode;
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
@@ -2275,7 +2329,11 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, size_t len
/* Make sure that we have exclusive access to the private data structure */ /* Make sure that we have exclusive access to the private data structure */
DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); DEBUGASSERT(priv && priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Check if the mouse is still connected. We need to disable interrupts /* Check if the mouse is still connected. We need to disable interrupts
* momentarily to assure that there are no asynchronous disconnect events. * momentarily to assure that there are no asynchronous disconnect events.
@@ -2344,7 +2402,8 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, size_t len
if (sample.valid) if (sample.valid)
{ {
report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID | TOUCH_POS_VALID; report->point[0].flags = TOUCH_UP | TOUCH_ID_VALID |
TOUCH_POS_VALID;
} }
else else
{ {
@@ -2355,13 +2414,15 @@ static ssize_t usbhost_read(FAR struct file *filep, FAR char *buffer, size_t len
{ {
/* First event */ /* First event */
report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID | TOUCH_POS_VALID; report->point[0].flags = TOUCH_DOWN | TOUCH_ID_VALID |
TOUCH_POS_VALID;
} }
else /* if (sample->event == BUTTON_MOVE) */ else /* if (sample->event == BUTTON_MOVE) */
{ {
/* Movement of the same event */ /* Movement of the same event */
report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID | TOUCH_POS_VALID; report->point[0].flags = TOUCH_MOVE | TOUCH_ID_VALID |
TOUCH_POS_VALID;
} }
iinfo(" id: %d\n", report->point[0].id); iinfo(" id: %d\n", report->point[0].id);
@@ -2419,7 +2480,7 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds,
{ {
FAR struct inode *inode; FAR struct inode *inode;
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
int ret = OK; int ret;
int i; int i;
uinfo("Entry\n"); uinfo("Entry\n");
@@ -2430,7 +2491,11 @@ static int usbhost_poll(FAR struct file *filep, FAR struct pollfd *fds,
/* Make sure that we have exclusive access to the private data structure */ /* Make sure that we have exclusive access to the private data structure */
DEBUGASSERT(priv); DEBUGASSERT(priv);
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Check if the mouse is still connected. We need to disable interrupts /* Check if the mouse is still connected. We need to disable interrupts
* momentarily to assure that there are no asynchronous disconnect events. * momentarily to assure that there are no asynchronous disconnect events.
File diff suppressed because it is too large Load Diff
+104 -61
View File
@@ -1,35 +1,20 @@
/**************************************************************************** /****************************************************************************
* drivers/usbhost/usbhost_skeleton.c * drivers/usbhost/usbhost_skeleton.c
* *
* Copyright (C) 2018 Gregory Nutt. All rights reserved. * Licensed to the Apache Software Foundation (ASF) under one or more
* Author: Gregory Nutt <gnutt@nuttx.org> * 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
* *
* Redistribution and use in source and binary forms, with or without * http://www.apache.org/licenses/LICENSE-2.0
* modification, are permitted provided that the following conditions
* are met:
* *
* 1. Redistributions of source code must retain the above copyright * Unless required by applicable law or agreed to in writing, software
* notice, this list of conditions and the following disclaimer. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* 2. Redistributions in binary form must reproduce the above copyright * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* notice, this list of conditions and the following disclaimer in * License for the specific language governing permissions and limitations
* the documentation and/or other materials provided with the * under the License.
* 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.
* *
****************************************************************************/ ****************************************************************************/
@@ -67,6 +52,7 @@
#endif #endif
/* Driver support ***********************************************************/ /* Driver support ***********************************************************/
/* This format is used to construct the /dev/skel[n] device driver path. It /* This format is used to construct the /dev/skel[n] device driver path. It
* defined here so that it will be used consistently in all places. * defined here so that it will be used consistently in all places.
*/ */
@@ -117,7 +103,8 @@ struct usbhost_state_s
/* Semaphores */ /* Semaphores */
static void usbhost_takesem(sem_t *sem); static int usbhost_takesem(FAR sem_t *sem);
static void usbhost_forcetake(FAR sem_t *sem);
#define usbhost_givesem(s) nxsem_post(s); #define usbhost_givesem(s) nxsem_post(s);
/* Memory allocation services */ /* Memory allocation services */
@@ -157,8 +144,9 @@ static inline int usbhost_tfree(FAR struct usbhost_state_s *priv);
/* struct usbhost_registry_s methods */ /* struct usbhost_registry_s methods */
static struct usbhost_class_s *usbhost_create(FAR struct usbhost_hubport_s *hport, static struct usbhost_class_s *
FAR const struct usbhost_id_s *id); usbhost_create(FAR struct usbhost_hubport_s *hport,
FAR const struct usbhost_id_s *id);
/* struct usbhost_class_s methods */ /* struct usbhost_class_s methods */
@@ -212,9 +200,37 @@ static uint32_t g_devinuse;
* *
****************************************************************************/ ****************************************************************************/
static void usbhost_takesem(sem_t *sem) static int usbhost_takesem(FAR sem_t *sem)
{ {
nxsem_wait_uninterruptible(sem); return nxsem_wait_uninterruptible(sem);
}
/****************************************************************************
* Name: usbhost_forcetake
*
* Description:
* This is just another wrapper but this one continues even if the thread
* is canceled. This must be done in certain conditions where were must
* continue in order to clean-up resources.
*
****************************************************************************/
static void usbhost_forcetake(FAR sem_t *sem)
{
int ret;
do
{
ret = nxsem_wait_uninterruptible(sem);
/* The only expected error would -ECANCELED meaning that the
* parent thread has been canceled. We have to continue and
* terminate the poll in this case.
*/
DEBUGASSERT(ret == OK || ret == -ECANCELED);
}
while (ret < 0);
} }
/**************************************************************************** /****************************************************************************
@@ -241,7 +257,10 @@ static inline FAR struct usbhost_state_s *usbhost_allocclass(void)
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
DEBUGASSERT(!up_interrupt_context()); DEBUGASSERT(!up_interrupt_context());
priv = (FAR struct usbhost_state_s *)kmm_malloc(sizeof(struct usbhost_state_s));
priv = (FAR struct usbhost_state_s *)
kmm_malloc(sizeof(struct usbhost_state_s));
uinfo("Allocated: %p\n", priv); uinfo("Allocated: %p\n", priv);
return priv; return priv;
} }
@@ -396,8 +415,8 @@ static void usbhost_destroy(FAR void *arg)
* desclen - The length in bytes of the configuration descriptor. * desclen - The length in bytes of the configuration descriptor.
* *
* Returned Value: * Returned Value:
* On success, zero (OK) is returned. On a failure, a negated errno value is * On success, zero (OK) is returned. On a failure, a negated errno value
* returned indicating the nature of the failure * is returned indicating the nature of the failure
* *
* Assumptions: * Assumptions:
* This function will *not* be called from an interrupt handler. * This function will *not* be called from an interrupt handler.
@@ -454,7 +473,8 @@ static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
case USB_DESC_TYPE_INTERFACE: case USB_DESC_TYPE_INTERFACE:
{ {
FAR struct usb_ifdesc_s *ifdesc = (FAR struct usb_ifdesc_s *)configdesc; FAR struct usb_ifdesc_s *ifdesc =
(FAR struct usb_ifdesc_s *)configdesc;
uinfo("Interface descriptor\n"); uinfo("Interface descriptor\n");
DEBUGASSERT(remaining >= USB_SIZEOF_IFDESC); DEBUGASSERT(remaining >= USB_SIZEOF_IFDESC);
@@ -472,14 +492,16 @@ static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
case USB_DESC_TYPE_ENDPOINT: case USB_DESC_TYPE_ENDPOINT:
{ {
FAR struct usb_epdesc_s *epdesc = (FAR struct usb_epdesc_s *)configdesc; FAR struct usb_epdesc_s *epdesc =
(FAR struct usb_epdesc_s *)configdesc;
uinfo("Endpoint descriptor\n"); uinfo("Endpoint descriptor\n");
DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC); DEBUGASSERT(remaining >= USB_SIZEOF_EPDESC);
/* Check for a bulk endpoint. */ /* Check for a bulk endpoint. */
if ((epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) == USB_EP_ATTR_XFER_BULK) if ((epdesc->attr & USB_EP_ATTR_XFERTYPE_MASK) ==
USB_EP_ATTR_XFER_BULK)
{ {
/* Yes.. it is a bulk endpoint. IN or OUT? */ /* Yes.. it is a bulk endpoint. IN or OUT? */
@@ -497,16 +519,20 @@ static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
return -EINVAL; return -EINVAL;
} }
found |= USBHOST_BOUTFOUND; found |= USBHOST_BOUTFOUND;
/* Save the bulk OUT endpoint information */ /* Save the bulk OUT endpoint information */
boutdesc.hport = hport; boutdesc.hport = hport;
boutdesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK; boutdesc.addr = epdesc->addr &
USB_EP_ADDR_NUMBER_MASK;
boutdesc.in = false; boutdesc.in = false;
boutdesc.xfrtype = USB_EP_ATTR_XFER_BULK; boutdesc.xfrtype = USB_EP_ATTR_XFER_BULK;
boutdesc.interval = epdesc->interval; boutdesc.interval = epdesc->interval;
boutdesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize); boutdesc.mxpacketsize =
usbhost_getle16(epdesc->mxpacketsize);
uinfo("Bulk OUT EP addr:%d mxpacketsize:%d\n", uinfo("Bulk OUT EP addr:%d mxpacketsize:%d\n",
boutdesc.addr, boutdesc.mxpacketsize); boutdesc.addr, boutdesc.mxpacketsize);
} }
@@ -530,11 +556,14 @@ static inline int usbhost_cfgdesc(FAR struct usbhost_state_s *priv,
/* Save the bulk IN endpoint information */ /* Save the bulk IN endpoint information */
bindesc.hport = hport; bindesc.hport = hport;
bindesc.addr = epdesc->addr & USB_EP_ADDR_NUMBER_MASK; bindesc.addr = epdesc->addr &
USB_EP_ADDR_NUMBER_MASK;
bindesc.in = 1; bindesc.in = 1;
bindesc.xfrtype = USB_EP_ATTR_XFER_BULK; bindesc.xfrtype = USB_EP_ATTR_XFER_BULK;
bindesc.interval = epdesc->interval; bindesc.interval = epdesc->interval;
bindesc.mxpacketsize = usbhost_getle16(epdesc->mxpacketsize); bindesc.mxpacketsize =
usbhost_getle16(epdesc->mxpacketsize);
uinfo("Bulk IN EP addr:%d mxpacketsize:%d\n", uinfo("Bulk IN EP addr:%d mxpacketsize:%d\n",
bindesc.addr, bindesc.mxpacketsize); bindesc.addr, bindesc.mxpacketsize);
} }
@@ -637,7 +666,9 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv)
uinfo("Register block driver\n"); uinfo("Register block driver\n");
usbhost_mkdevname(priv, devname); usbhost_mkdevname(priv, devname);
// ret = register_blockdriver(devname, &g_bops, 0, priv); #if 0 /* Finish me */
ret = register_blockdriver(devname, &g_bops, 0, priv);
#endif
} }
/* Check if we successfully initialized. We now have to be concerned /* Check if we successfully initialized. We now have to be concerned
@@ -647,7 +678,12 @@ static inline int usbhost_devinit(FAR struct usbhost_state_s *priv)
if (ret >= 0) if (ret >= 0)
{ {
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ert;
}
DEBUGASSERT(priv->crefs >= 2); DEBUGASSERT(priv->crefs >= 2);
/* Handle a corner case where (1) open() has been called so the /* Handle a corner case where (1) open() has been called so the
@@ -736,7 +772,8 @@ static inline uint32_t usbhost_getle32(const uint8_t *val)
{ {
/* Little endian means LS halfword first in byte stream */ /* Little endian means LS halfword first in byte stream */
return (uint32_t)usbhost_getle16(&val[2]) << 16 | (uint32_t)usbhost_getle16(val); return (uint32_t)usbhost_getle16(&val[2]) << 16 |
(uint32_t)usbhost_getle16(val);
} }
/**************************************************************************** /****************************************************************************
@@ -759,7 +796,7 @@ static void usbhost_putle32(uint8_t *dest, uint32_t val)
/* Little endian means LS halfword first in byte stream */ /* Little endian means LS halfword first in byte stream */
usbhost_putle16(dest, (uint16_t)(val & 0xffff)); usbhost_putle16(dest, (uint16_t)(val & 0xffff));
usbhost_putle16(dest+2, (uint16_t)(val >> 16)); usbhost_putle16(dest + 2, (uint16_t)(val >> 16));
} }
/**************************************************************************** /****************************************************************************
@@ -829,12 +866,13 @@ static inline int usbhost_tfree(FAR struct usbhost_state_s *priv)
* Name: usbhost_create * Name: usbhost_create
* *
* Description: * Description:
* This function implements the create() method of struct usbhost_registry_s. * This function implements the create() method of struct
* The create() method is a callback into the class implementation. It is * usbhost_registry_s. The create() method is a callback into the class
* used to (1) create a new instance of the USB host class state and to (2) * implementation. It is used to (1) create a new instance of the USB
* bind a USB host driver "session" to the class instance. Use of this * host class state and to (2) bind a USB host driver "session" to the
* create() method will support environments where there may be multiple * class instance. Use of this create() method will support environments
* USB ports and multiple USB devices simultaneously connected. * where there may be multiple USB ports and multiple USB devices
* simultaneously connected.
* *
* Input Parameters: * Input Parameters:
* hport - The hub hat manages the new class instance. * hport - The hub hat manages the new class instance.
@@ -850,8 +888,9 @@ static inline int usbhost_tfree(FAR struct usbhost_state_s *priv)
* *
****************************************************************************/ ****************************************************************************/
static FAR struct usbhost_class_s *usbhost_create(FAR struct usbhost_hubport_s *hport, static FAR struct usbhost_class_s *
FAR const struct usbhost_id_s *id) usbhost_create(FAR struct usbhost_hubport_s *hport,
FAR const struct usbhost_id_s *id)
{ {
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
@@ -868,7 +907,7 @@ static FAR struct usbhost_class_s *usbhost_create(FAR struct usbhost_hubport_s *
if (usbhost_allocdevno(priv) == OK) if (usbhost_allocdevno(priv) == OK)
{ {
/* Initialize class method function pointers */ /* Initialize class method function pointers */
priv->usbclass.hport = hport; priv->usbclass.hport = hport;
priv->usbclass.connect = usbhost_connect; priv->usbclass.connect = usbhost_connect;
@@ -894,12 +933,14 @@ static FAR struct usbhost_class_s *usbhost_create(FAR struct usbhost_hubport_s *
{ {
usbhost_freeclass(priv); usbhost_freeclass(priv);
} }
return NULL; return NULL;
} }
/**************************************************************************** /****************************************************************************
* struct usbhost_class_s methods * struct usbhost_class_s methods
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: usbhost_connect * Name: usbhost_connect
* *
@@ -917,11 +958,11 @@ static FAR struct usbhost_class_s *usbhost_create(FAR struct usbhost_hubport_s *
* desclen - The length in bytes of the configuration descriptor. * desclen - The length in bytes of the configuration descriptor.
* *
* Returned Value: * Returned Value:
* On success, zero (OK) is returned. On a failure, a negated errno value is * On success, zero (OK) is returned. On a failure, a negated errno value
* returned indicating the nature of the failure * is returned indicating the nature of the failure
* *
* NOTE that the class instance remains valid upon return with a failure. It is * NOTE that the class instance remains valid upon return with a failure.
* the responsibility of the higher level enumeration logic to call * It is the responsibility of the higher level enumeration logic to call
* CLASS_DISCONNECTED to free up the class driver resources. * CLASS_DISCONNECTED to free up the class driver resources.
* *
* Assumptions: * Assumptions:
@@ -1016,10 +1057,12 @@ static int usbhost_disconnected(struct usbhost_class_s *usbclass)
{ {
/* Destroy the instance on the worker thread. */ /* Destroy the instance on the worker thread. */
uinfo("Queuing destruction: worker %p->%p\n", priv->work.worker, usbhost_destroy); uinfo("Queuing destruction: worker %p->%p\n",
priv->work.worker, usbhost_destroy);
DEBUGASSERT(priv->work.worker == NULL); DEBUGASSERT(priv->work.worker == NULL);
work_queue(HPWORK, &priv->work, usbhost_destroy, priv, 0); work_queue(HPWORK, &priv->work, usbhost_destroy, priv, 0);
} }
else else
{ {
/* Do the work now */ /* Do the work now */
+75 -28
View File
@@ -138,7 +138,8 @@ struct usbhost_freestate_s
/* Semaphores */ /* Semaphores */
static void usbhost_takesem(sem_t *sem); static int usbhost_takesem(FAR sem_t *sem);
static void usbhost_forcetake(FAR sem_t *sem);
#define usbhost_givesem(s) nxsem_post(s); #define usbhost_givesem(s) nxsem_post(s);
/* Memory allocation services */ /* Memory allocation services */
@@ -311,9 +312,37 @@ static uint32_t g_devinuse;
* *
****************************************************************************/ ****************************************************************************/
static void usbhost_takesem(sem_t *sem) static int usbhost_takesem(FAR sem_t *sem)
{ {
nxsem_wait_uninterruptible(sem); return nxsem_wait_uninterruptible(sem);
}
/****************************************************************************
* Name: usbhost_forcetake
*
* Description:
* This is just another wrapper but this one continues even if the thread
* is canceled. This must be done in certain conditions where were must
* continue in order to clean-up resources.
*
****************************************************************************/
static void usbhost_forcetake(FAR sem_t *sem)
{
int ret;
do
{
ret = nxsem_wait_uninterruptible(sem);
/* The only expected error would -ECANCELED meaning that the
* parent thread has been canceled. We have to continue and
* terminate the poll in this case.
*/
DEBUGASSERT(ret == OK || ret == -ECANCELED);
}
while (ret < 0);
} }
/**************************************************************************** /****************************************************************************
@@ -1347,7 +1376,7 @@ static inline int usbhost_initvolume(FAR struct usbhost_state_s *priv)
* driver has been registered. * driver has been registered.
*/ */
usbhost_takesem(&priv->exclsem); usbhost_forcetake(&priv->exclsem);
DEBUGASSERT(priv->crefs >= 2); DEBUGASSERT(priv->crefs >= 2);
/* Decrement the reference count */ /* Decrement the reference count */
@@ -1877,7 +1906,11 @@ static int usbhost_open(FAR struct inode *inode)
/* Make sure that we have exclusive access to the private data structure */ /* Make sure that we have exclusive access to the private data structure */
DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS); DEBUGASSERT(priv->crefs > 0 && priv->crefs < USBHOST_MAX_CREFS);
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Check if the mass storage device is still connected. We need to /* Check if the mass storage device is still connected. We need to
* disable interrupts momentarily to assure that there are no asynchronous * disable interrupts momentarily to assure that there are no asynchronous
@@ -1927,7 +1960,8 @@ static int usbhost_close(FAR struct inode *inode)
/* Decrement the reference count on the block driver */ /* Decrement the reference count on the block driver */
DEBUGASSERT(priv->crefs > 1); DEBUGASSERT(priv->crefs > 1);
usbhost_takesem(&priv->exclsem);
usbhost_forcetake(&priv->exclsem);
priv->crefs--; priv->crefs--;
/* Release the semaphore. The following operations when crefs == 1 are /* Release the semaphore. The following operations when crefs == 1 are
@@ -1976,6 +2010,7 @@ static ssize_t usbhost_read(FAR struct inode *inode, unsigned char *buffer,
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
FAR struct usbhost_hubport_s *hport; FAR struct usbhost_hubport_s *hport;
ssize_t nbytes = 0; ssize_t nbytes = 0;
int ret;
DEBUGASSERT(inode && inode->i_private); DEBUGASSERT(inode && inode->i_private);
priv = (FAR struct usbhost_state_s *)inode->i_private; priv = (FAR struct usbhost_state_s *)inode->i_private;
@@ -2001,7 +2036,11 @@ static ssize_t usbhost_read(FAR struct inode *inode, unsigned char *buffer,
{ {
FAR struct usbmsc_cbw_s *cbw; FAR struct usbmsc_cbw_s *cbw;
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Assume allocation failure */ /* Assume allocation failure */
@@ -2084,6 +2123,7 @@ static ssize_t usbhost_write(FAR struct inode *inode,
FAR struct usbhost_state_s *priv; FAR struct usbhost_state_s *priv;
FAR struct usbhost_hubport_s *hport; FAR struct usbhost_hubport_s *hport;
ssize_t nbytes; ssize_t nbytes;
int ret;
uinfo("sector: %d nsectors: %d sectorsize: %d\n"); uinfo("sector: %d nsectors: %d sectorsize: %d\n");
@@ -2108,7 +2148,11 @@ static ssize_t usbhost_write(FAR struct inode *inode,
{ {
FAR struct usbmsc_cbw_s *cbw; FAR struct usbmsc_cbw_s *cbw;
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret < 0)
{
return ret;
}
/* Assume allocation failure */ /* Assume allocation failure */
@@ -2198,19 +2242,19 @@ static int usbhost_geometry(FAR struct inode *inode,
{ {
/* Return the geometry of the USB mass storage device */ /* Return the geometry of the USB mass storage device */
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
if (ret >= 0)
{
geometry->geo_available = true;
geometry->geo_mediachanged = false;
geometry->geo_writeenabled = true;
geometry->geo_nsectors = priv->nblocks;
geometry->geo_sectorsize = priv->blocksize;
usbhost_givesem(&priv->exclsem);
geometry->geo_available = true; uinfo("nsectors: %ld sectorsize: %d\n",
geometry->geo_mediachanged = false; (long)geometry->geo_nsectors, geometry->geo_sectorsize);
geometry->geo_writeenabled = true; }
geometry->geo_nsectors = priv->nblocks;
geometry->geo_sectorsize = priv->blocksize;
usbhost_givesem(&priv->exclsem);
uinfo("nsectors: %ld sectorsize: %d\n",
(long)geometry->geo_nsectors, geometry->geo_sectorsize);
ret = OK;
} }
return ret; return ret;
@@ -2247,17 +2291,20 @@ static int usbhost_ioctl(FAR struct inode *inode, int cmd, unsigned long arg)
{ {
/* Process the IOCTL by command */ /* Process the IOCTL by command */
usbhost_takesem(&priv->exclsem); ret = usbhost_takesem(&priv->exclsem);
switch (cmd) if (ret >= 0)
{ {
/* Add support for ioctl commands here */ switch (cmd)
{
/* Add support for ioctl commands here */
default: default:
ret = -ENOTTY; ret = -ENOTTY;
break; break;
}
usbhost_givesem(&priv->exclsem);
} }
usbhost_givesem(&priv->exclsem);
} }
return ret; return ret;
File diff suppressed because it is too large Load Diff