nuttx/drivers/usbdev/composite.c
Frank Benkert eacb4f0e84 Squashed commit of the following:
commit 69fcf3e849
Author: Alan Carvalho de Assis <acassis@gmail.com>
Date:   Sun Jul 16 08:39:33 2017 -0600

    Fix spark/stm32_composite.c: board_composite_connect cannot be static

commit 28eb253401
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sun Jul 16 08:36:01 2017 -0600

    Composite:  Final review for coding style before merge

commit e6af1b9994
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sun Jul 16 07:41:38 2017 -0600

    Composite:  Simplify some intiialization of data structures.

commit 771c367411
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sun Jul 16 07:15:08 2017 -0600

    Cosmetic changes to alignment.

commit 5d67ddda4e
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sun Jul 16 07:00:48 2017 -0600

    USBMSC: Add missing logic to saved device description.

commit 0729151d29
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 17:11:41 2017 -0600

    Trivial, costmetic

commit 74b916ff84
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 14:50:29 2017 -0600

    Composite:  Private functions need to be marked static.  Move static functions out of 'Public Functions' to 'Private Functions' where they belong.  Disable composite configuration 1 in all STM32 F1 configurations.

commit cfaa4ece13
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 13:20:34 2017 -0600

    Add some comments.

commit 8143563be6
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 12:33:10 2017 -0600

    Spark: Need to condition out MSC logic in composite setup if there is no MSC in the composite.

commit 69d3a91ef1
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 11:03:33 2017 -0600

    Composite:  Remove all dependencies on CONFIG_SYSTEM_COMPOSITE_* configuration settings.  Nothing in the OS can depend on external application settings.

commit 55a4388bbd
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 09:59:31 2017 -0600

    All composite configurations now also support a dual CDC/ACM configuration.

commit 428f2147af
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 09:24:32 2017 -0600

    Composite:  Move board_msc* interfaces from apps/system/composite to the board specific OS logic where they belong.

commit f1cc168a5c
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 08:56:33 2017 -0600

    Refresh all composite configurations.

commit 246afcaa10
Merge: 919877191d 02c6672868
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 08:22:26 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 919877191d
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jul 15 08:20:02 2017 -0600

    Composite setup:  Remove useless board_cdc* wrapper.

commit 82129cf8c6
Merge: f2cb8b252a 6537e4ea20
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jul 14 16:23:57 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit f2cb8b252a
Author: Alan Carvalho de Assis <acassis@gmail.com>
Date:   Fri Jul 14 10:19:35 2017 -0600

    Composite:  Fix for another cloned typo.

commit 676cfd526a
Author: Alan Carvalho de Assis <acassis@gmail.com>
Date:   Fri Jul 14 09:11:37 2017 -0600

    Composite: Fix some typos

commit 1ea0368c18
Author: Alan Carvalho de Assis <acassis@gmail.com>
Date:   Fri Jul 14 09:10:18 2017 -0600

    Composite: ./stm3210e-eval/src/stm32_composite.c

commit e485caced9
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jul 14 09:08:17 2017 -0600

    Composite:  I don't think the original code should have forced minor=0.

commit 6443c29621
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jul 14 07:15:38 2017 -0600

    Composite:  Flesh out support for all of other configurations that support composite.

commit 23cbc28b05
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jul 14 06:59:45 2017 -0600

    Detangle use of board_xyzclassobject() and board_xyzuninitialize()

commit 1674cb8c8e
Merge: 6bc881a192 5033a6def7
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jul 13 13:57:40 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 6bc881a192
Merge: fe3af4941d 85b8d16d8c
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Tue Jul 11 12:24:07 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit fe3af4941d
Merge: 0f9ad16e18 1bc0eea143
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Mon Jul 10 11:07:36 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 0f9ad16e18
Merge: a4cd90d4ef aa2e9c15a5
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jul 7 20:26:53 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit a4cd90d4ef
Merge: 8a4be7175e 31f832d8c5
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Wed Jul 5 11:12:52 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 8a4be7175e
Merge: 18a32ed2ca ae1771454a
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jun 30 16:14:04 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 18a32ed2ca
Merge: aaa81ce497 6d8df90b79
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 29 10:18:16 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit aaa81ce497
Merge: 4eb548226b 8cb4636bb1
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Mon Jun 26 11:56:11 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 4eb548226b
Merge: 2327f5a1b4 dc8eec0b61
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Mon Jun 19 17:27:00 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 2327f5a1b4
Merge: 49cd279fc6 99bf0b522b
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jun 16 17:30:03 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 49cd279fc6
Merge: bb6a13f30a 46f86982ee
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Wed Jun 14 09:17:49 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit bb6a13f30a
Merge: 918480047a ac93d4bda9
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Mon Jun 5 17:40:06 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 918480047a
Merge: e4d262436c 4526cd665e
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Sat Jun 3 08:52:31 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit e4d262436c
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jun 2 07:19:27 2017 -0600

    SAMV71-Xult Composite:  Now can switch between two different composite configurations dynamically.

commit 815257743d
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Fri Jun 2 07:11:57 2017 -0600

    usbdev composite and SAMV7-Xult: Move board-specific USB composite configuration out of boardctl.c and into board-specific logic where it belongs.  Add a configuration option to the boardctl() calls to support multiple composite device configurations dynamically.

commit ac13619dc5
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 17:03:58 2017 -0600

    Cosmetic

commit 9dd41bdd2f
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 17:02:43 2017 -0600

    Composite: More compile-related fixes

commit fc1438c95d
Merge: 049ccbfcbe ff2b54a5e0
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 16:35:41 2017 -0600

    Merge remote-tracking branch 'origin/master' into composite

commit 049ccbfcbe
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 16:35:16 2017 -0600

    Composite: Add some structure definitions missed in first application of the patch.

commit ef33329e3a
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 16:14:46 2017 -0600

    Add a warning

commit 89f77cd91a
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 16:11:27 2017 -0600

    Fix some incomplete name changes

commit 0bb7af549a
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 15:09:50 2017 -0600

    It is unnecessary to pack a structure that consists only of uint8_t data fields.

commit bd9b548914
Author: Gregory Nutt <gnutt@nuttx.org>
Date:   Thu Jun 1 15:05:41 2017 -0600

    Remove COMPILE_TIME_ASSERTION

commit 7e6f481581
Author: Frank Benkert <Frank.Benkert@avat.de>
Date:   Thu Jun 1 14:58:04 2017 -0600

    Part II of the same big commit

commit dcc9b07715
Author: Frank Benkert <Frank.Benkert@avat.de>
Date:   Thu Jun 1 14:08:22 2017 -0600

    [[This is part 1 or several commits]]

    We developed a huge Changeset over a year ago to make USB Composite configuration dynamical and be able to instanciate the CDCACM multiple times inside this device.  We use this feature to switch between one in normal and up to three CDCACMs in maintenance boot.

    The control path starts in “boardctl.c” where the configuration for the device is constructed.  There are still a few issues which I’ll ask you to have a look at before this beast can be merged.

    1. To be able to construct the data dynamically I have changed some USB-Structs to be packed. Maybe there are additional structs to change (just for completeness – not for current functionality).

    2. I’ve added the Macro “COMPILE_TIME_ASSERTION” two times (in usbmsc_desc.c and in cdcacm_desc.c) to stay private. Maybe you’ll find a better place. It’s used to check the size of the structs against the assumptions.

    3. I’ve changed the interface for some USB-Functions to receive also the dynamic configuration. Maybe this can be done more elegant.

    4. The original NuttX (without the patch) seems to have problems with a Composite device holding a CDCACM and an MSC. The “USB SET CONFIGURATION” request does not to work at all. This makes the test fail under Windows and under Linux. Applying this patch doesn’t change anything – because it only changes the configuration – not the behavior. Maybe you’ll have a look at this problem before applying the patch.
2017-07-16 08:43:17 -06:00

964 lines
28 KiB
C

/****************************************************************************
* drivers/usbdev/composite.c
*
* Copyright (C) 2012, 2016-2017 Gregory Nutt. All rights reserved.
* Author: 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include <nuttx/usb/usb.h>
#include <nuttx/usb/usbdev.h>
#include <nuttx/usb/usbdev_trace.h>
#include "composite.h"
#ifdef CONFIG_USBDEV_COMPOSITE
/****************************************************************************
* Private Types
****************************************************************************/
/* The internal version of the class driver */
struct composite_driver_s
{
struct usbdevclass_driver_s drvr;
FAR struct composite_dev_s *dev;
};
/* This is what is allocated */
struct composite_alloc_s
{
struct composite_dev_s dev;
struct composite_driver_s drvr;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* USB helps ****************************************************************/
static void composite_ep0incomplete(FAR struct usbdev_ep_s *ep,
FAR struct usbdev_req_s *req);
static int composite_classsetup(FAR struct composite_dev_s *priv,
FAR struct usbdev_s *dev,
FAR const struct usb_ctrlreq_s *ctrl, FAR uint8_t *dataout,
size_t outlen);
static struct usbdev_req_s *composite_allocreq(FAR struct usbdev_ep_s *ep,
uint16_t len);
static void composite_freereq(FAR struct usbdev_ep_s *ep,
FAR struct usbdev_req_s *req);
/* USB class device ********************************************************/
static int composite_bind(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev);
static void composite_unbind(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev);
static int composite_setup(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev,
FAR const struct usb_ctrlreq_s *ctrl, FAR uint8_t *dataout,
size_t outlen);
static void composite_disconnect(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev);
static void composite_suspend(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev);
static void composite_resume(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev);
/****************************************************************************
* Private Data
****************************************************************************/
/* USB class device *********************************************************/
static const struct usbdevclass_driverops_s g_driverops =
{
composite_bind, /* bind */
composite_unbind, /* unbind */
composite_setup, /* setup */
composite_disconnect, /* disconnect */
composite_suspend, /* suspend */
composite_resume, /* resume */
};
/****************************************************************************
* Public Data
****************************************************************************/
const char g_compvendorstr[] = CONFIG_COMPOSITE_VENDORSTR;
const char g_compproductstr[] = CONFIG_COMPOSITE_PRODUCTSTR;
const char g_compserialstr[] = CONFIG_COMPOSITE_SERIALSTR;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: composite_ep0incomplete
*
* Description:
* Handle completion of the composite driver's EP0 control operations
*
****************************************************************************/
static void composite_ep0incomplete(FAR struct usbdev_ep_s *ep,
FAR struct usbdev_req_s *req)
{
/* Just check the result of the transfer */
if (req->result || req->xfrd != req->len)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_REQRESULT),
(uint16_t)-req->result);
}
}
/****************************************************************************
* Name: composite_classsetup
*
* Description:
* Forward a setup command to the appropriate component device
*
****************************************************************************/
static int composite_classsetup(FAR struct composite_dev_s *priv,
FAR struct usbdev_s *dev,
FAR const struct usb_ctrlreq_s *ctrl,
FAR uint8_t *dataout, size_t outlen)
{
uint16_t index;
uint8_t interface;
int ret = -EOPNOTSUPP;
int i;
index = GETUINT16(ctrl->index);
interface = (uint8_t)(index & 0xff);
for (i = 0; i < priv->ndevices; i++)
{
if (interface >= priv->device[i].compdesc.devdesc.ifnobase &&
interface < (priv->device[i].compdesc.devdesc.ifnobase +
priv->device[i].compdesc.devdesc.ninterfaces))
{
ret = CLASS_SETUP(priv->device[i].dev, dev, ctrl, dataout, outlen);
break;
}
}
return ret;
}
/****************************************************************************
* Name: composite_allocreq
*
* Description:
* Allocate a request instance along with its buffer
*
****************************************************************************/
static struct usbdev_req_s *composite_allocreq(FAR struct usbdev_ep_s *ep,
uint16_t len)
{
FAR struct usbdev_req_s *req;
req = EP_ALLOCREQ(ep);
if (req != NULL)
{
req->len = len;
req->buf = EP_ALLOCBUFFER(ep, len);
if (!req->buf)
{
EP_FREEREQ(ep, req);
req = NULL;
}
}
return req;
}
/****************************************************************************
* Name: composite_freereq
*
* Description:
* Free a request instance along with its buffer
*
****************************************************************************/
static void composite_freereq(FAR struct usbdev_ep_s *ep,
FAR struct usbdev_req_s *req)
{
if (ep != NULL && req != NULL)
{
if (req->buf != NULL)
{
EP_FREEBUFFER(ep, req->buf);
}
EP_FREEREQ(ep, req);
}
}
/****************************************************************************
* USB Class Driver Methods
****************************************************************************/
/****************************************************************************
* Name: composite_bind
*
* Description:
* Invoked when the driver is bound to a USB device driver
*
****************************************************************************/
static int composite_bind(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev)
{
FAR struct composite_dev_s *priv =
((FAR struct composite_driver_s *)driver)->dev;
int ret;
int i;
usbtrace(TRACE_CLASSBIND, 0);
/* Bind the structures */
priv->usbdev = dev;
/* Save the reference to our private data structure in EP0 so that it
* can be recovered in ep0 completion events.
*/
dev->ep0->priv = priv;
/* Preallocate one control request */
priv->ctrlreq = composite_allocreq(dev->ep0, priv->cfgdescsize);
if (priv->ctrlreq == NULL)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_ALLOCCTRLREQ), 0);
ret = -ENOMEM;
goto errout;
}
/* Initialize the pre-allocated control request */
priv->ctrlreq->callback = composite_ep0incomplete;
/* Then bind each of the constituent class drivers */
for (i = 0; i < priv->ndevices; i++)
{
ret = CLASS_BIND(priv->device[i].dev, dev);
if (ret < 0)
{
goto errout;
}
}
/* Report if we are selfpowered */
#ifdef CONFIG_USBDEV_SELFPOWERED
DEV_SETSELFPOWERED(dev);
#endif
/* And pull-up the data line for the soft connect function */
DEV_CONNECT(dev);
return OK;
errout:
composite_unbind(driver, dev);
return ret;
}
/****************************************************************************
* Name: composite_unbind
*
* Description:
* Invoked when the driver is unbound from a USB device driver
*
****************************************************************************/
static void composite_unbind(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev)
{
FAR struct composite_dev_s *priv;
irqstate_t flags;
usbtrace(TRACE_CLASSUNBIND, 0);
#ifdef CONFIG_DEBUG_FEATURES
if (!driver || !dev || !dev->ep0)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
return;
}
#endif
/* Extract reference to private data */
priv = ((FAR struct composite_driver_s *)driver)->dev;
#ifdef CONFIG_DEBUG_FEATURES
if (!priv)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
return;
}
#endif
/* Make sure that we are not already unbound */
if (priv != NULL)
{
int i;
/* Unbind the constituent class drivers */
flags = enter_critical_section();
for (i = 0; i < priv->ndevices; i++)
{
CLASS_UNBIND(priv->device[i].dev, dev);
}
/* Free the pre-allocated control request */
priv->config = COMPOSITE_CONFIGIDNONE;
if (priv->ctrlreq != NULL)
{
composite_freereq(dev->ep0, priv->ctrlreq);
priv->ctrlreq = NULL;
}
leave_critical_section(flags);
}
}
/****************************************************************************
* Name: composite_setup
*
* Description:
* Invoked for ep0 control requests. This function probably executes
* in the context of an interrupt handler.
*
****************************************************************************/
static int composite_setup(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev,
FAR const struct usb_ctrlreq_s *ctrl,
FAR uint8_t *dataout, size_t outlen)
{
FAR struct composite_dev_s *priv;
FAR struct usbdev_req_s *ctrlreq;
uint16_t value;
uint16_t index;
uint16_t len;
bool dispatched = false;
int ret = -EOPNOTSUPP;
#ifdef CONFIG_DEBUG_FEATURES
if (!driver || !dev || !dev->ep0 || !ctrl)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_SETUPINVALIDARGS), 0);
return -EIO;
}
#endif
/* Extract a reference to private data */
usbtrace(TRACE_CLASSSETUP, ctrl->req);
priv = ((FAR struct composite_driver_s *)driver)->dev;
#ifdef CONFIG_DEBUG_FEATURES
if (!priv)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND2), 0);
return -ENODEV;
}
#endif
ctrlreq = priv->ctrlreq;
/* Extract the little-endian 16-bit values to host order */
value = GETUINT16(ctrl->value);
index = GETUINT16(ctrl->index);
len = GETUINT16(ctrl->len);
uinfo("type=%02x req=%02x value=%04x index=%04x len=%04x\n",
ctrl->type, ctrl->req, value, index, len);
UNUSED(index);
if ((ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD)
{
/**********************************************************************
* Standard Requests
**********************************************************************/
switch (ctrl->req)
{
case USB_REQ_GETDESCRIPTOR:
{
/* The value field specifies the descriptor type in the MS byte and the
* descriptor index in the LS byte (order is little endian)
*/
switch (ctrl->value[1])
{
case USB_DESC_TYPE_DEVICE:
{
ret = USB_SIZEOF_DEVDESC;
memcpy(ctrlreq->buf, composite_getdevdesc(), ret);
}
break;
#ifdef CONFIG_USBDEV_DUALSPEED
case USB_DESC_TYPE_DEVICEQUALIFIER:
{
ret = USB_SIZEOF_QUALDESC;
memcpy(ctrlreq->buf, composite_getqualdesc(), ret);
}
break;
case USB_DESC_TYPE_OTHERSPEEDCONFIG:
#endif
case USB_DESC_TYPE_CONFIG:
{
#ifdef CONFIG_USBDEV_DUALSPEED
ret = composite_mkcfgdesc(priv, ctrlreq->buf, dev->speed,
ctrl->value[1]);
#else
ret = composite_mkcfgdesc(priv, ctrlreq->buf);
#endif
}
break;
case USB_DESC_TYPE_STRING:
{
/* value == string index. Zero is the language ID. */
uint8_t strid = ctrl->value[0];
FAR struct usb_strdesc_s *buf = (FAR struct usb_strdesc_s *)ctrlreq->buf;
if (strid <= COMPOSITE_NSTRIDS)
{
ret = composite_mkstrdesc(strid, buf);
}
else
{
int i;
for (i = 0; i < priv->ndevices; i++)
{
if (strid >= priv->device[i].compdesc.devdesc.strbase &&
strid < priv->device[i].compdesc.devdesc.strbase +
priv->device[i].compdesc.devdesc.nstrings)
{
ret = priv->device[i].compdesc.mkstrdesc(strid - priv->device[i].compdesc.devdesc.strbase, buf);
break;
}
}
}
}
break;
default:
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_GETUNKNOWNDESC), value);
}
break;
}
}
break;
case USB_REQ_SETCONFIGURATION:
{
if (ctrl->type == 0)
{
int i;
/* Save the configuration and inform the constituent classes */
for (i = 0; i < priv->ndevices; i++)
{
ret = CLASS_SETUP(priv->device[i].dev, dev, ctrl, dataout, outlen);
}
dispatched = true;
priv->config = value;
}
}
break;
case USB_REQ_GETCONFIGURATION:
{
if (ctrl->type == USB_DIR_IN)
{
ctrlreq->buf[0] = priv->config;
ret = 1;
}
}
break;
case USB_REQ_SETINTERFACE:
{
if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE &&
priv->config == COMPOSITE_CONFIGID)
{
ret = composite_classsetup(priv, dev, ctrl, dataout, outlen);
dispatched = true;
}
}
break;
case USB_REQ_GETINTERFACE:
{
if (ctrl->type == (USB_DIR_IN | USB_REQ_RECIPIENT_INTERFACE) &&
priv->config == COMPOSITE_CONFIGIDNONE)
{
ret = composite_classsetup(priv, dev, ctrl, dataout, outlen);
dispatched = true;
}
}
break;
default:
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_UNSUPPORTEDSTDREQ), ctrl->req);
break;
}
}
else
{
uint8_t recipient;
/**********************************************************************
* Non-Standard Class Requests
**********************************************************************/
/* Class implementations should handle there own interface and endpoint
* requests.
*/
recipient = ctrl->type & USB_REQ_RECIPIENT_MASK;
if (recipient == USB_REQ_RECIPIENT_INTERFACE || recipient == USB_REQ_RECIPIENT_ENDPOINT)
{
ret = composite_classsetup(priv, dev, ctrl, dataout, outlen);
dispatched = true;
}
}
/* Respond to the setup command if (1) data was returned, and (2) the
* request was NOT successfully dispatched to the component class driver.
* On an error return value (ret < 0), the USB driver will stall EP0.
*/
if (ret >= 0 && !dispatched)
{
/* Setup the request */
ctrlreq->len = MIN(len, ret);
ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
/* And submit the request to the USB controller driver */
ret = EP_SUBMIT(dev->ep0, ctrlreq);
if (ret < 0)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EPRESPQ), (uint16_t)-ret);
ctrlreq->result = OK;
composite_ep0incomplete(dev->ep0, ctrlreq);
}
}
return ret;
}
/****************************************************************************
* Name: composite_disconnect
*
* Description:
* Invoked after all transfers have been stopped, when the host is
* disconnected. This function is probably called from the context of an
* interrupt handler.
*
****************************************************************************/
static void composite_disconnect(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev)
{
FAR struct composite_dev_s *priv;
irqstate_t flags;
int i;
usbtrace(TRACE_CLASSDISCONNECT, 0);
#ifdef CONFIG_DEBUG_FEATURES
if (!driver || !dev)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
return;
}
#endif
/* Extract reference to private data */
priv = ((FAR struct composite_driver_s *)driver)->dev;
#ifdef CONFIG_DEBUG_FEATURES
if (!priv)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
return;
}
#endif
/* Reset the configuration and inform the constituent class drivers of
* the disconnection.
*/
flags = enter_critical_section();
priv->config = COMPOSITE_CONFIGIDNONE;
for (i = 0; i < priv->ndevices; i++)
{
CLASS_DISCONNECT(priv->device[i].dev, dev);
}
leave_critical_section(flags);
/* Perform the soft connect function so that we will we can be
* re-enumerated.
*/
DEV_CONNECT(dev);
}
/****************************************************************************
* Name: composite_suspend
*
* Description:
* Invoked on a USB suspend event.
*
****************************************************************************/
static void composite_suspend(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev)
{
FAR struct composite_dev_s *priv;
irqstate_t flags;
int i;
usbtrace(TRACE_CLASSSUSPEND, 0);
#ifdef CONFIG_DEBUG_FEATURES
if (!dev)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
return;
}
#endif
/* Extract reference to private data */
priv = ((FAR struct composite_driver_s *)driver)->dev;
#ifdef CONFIG_DEBUG_FEATURES
if (!priv)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
return;
}
#endif
/* Forward the suspend event to the constituent devices */
flags = enter_critical_section();
for (i = 0; i < priv->ndevices; i++)
{
CLASS_SUSPEND(priv->device[i].dev, priv->usbdev);
}
leave_critical_section(flags);
}
/****************************************************************************
* Name: composite_resume
*
* Description:
* Invoked on a USB resume event.
*
****************************************************************************/
static void composite_resume(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev)
{
FAR struct composite_dev_s *priv = NULL;
irqstate_t flags;
int i;
#ifdef CONFIG_DEBUG_FEATURES
if (!dev)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
return;
}
#endif
/* Extract reference to private data */
priv = ((FAR struct composite_driver_s *)driver)->dev;
#ifdef CONFIG_DEBUG_FEATURES
if (!priv)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
return;
}
#endif
/* Forward the resume event to the constituent devices */
flags = enter_critical_section();
for (i = 0; i < priv->ndevices; i++)
{
CLASS_RESUME(priv->device[i].dev, priv->usbdev);
}
leave_critical_section(flags);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: composite_initialize
*
* Description:
* Register USB composite device as configured. This function will call
* board-specific implementations in order to obtain the class objects for
* each of the members of the composite.
*
* Input Parameter:
* None
*
* Returned Value:
* A non-NULL "handle" is returned on success. This handle may be used
* later with composite_uninitialize() in order to removed the composite
* device. This handle is the (untyped) internal representation of the
* the class driver instance.
*
* NULL is returned on any failure.
*
****************************************************************************/
FAR void *composite_initialize(uint8_t ndevices,
FAR struct composite_devdesc_s *pdevices)
{
FAR struct composite_alloc_s *alloc;
FAR struct composite_dev_s *priv;
FAR struct composite_driver_s *drvr;
int ret;
int i;
DEBUGASSERT(pdevices != NULL && ndevices <= NUM_DEVICES_TO_HANDLE);
/* Allocate the structures needed */
alloc = (FAR struct composite_alloc_s *)
kmm_malloc(sizeof(struct composite_alloc_s));
if (!alloc)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_ALLOCDEVSTRUCT), 0);
return NULL;
}
/* Convenience pointers into the allocated blob */
priv = &alloc->dev;
drvr = &alloc->drvr;
/* Initialize the USB composite driver structure */
memset(priv, 0, sizeof(struct composite_dev_s));
priv->cfgdescsize = USB_SIZEOF_CFGDESC;
priv->ninterfaces = 0;
/* Get the constituent class driver objects */
for (i = 0; i < ndevices; i++)
{
memcpy(&priv->device[i].compdesc, &pdevices[i],
sizeof(struct composite_devdesc_s));
ret =
priv->device[i].compdesc.classobject(priv->device[i].compdesc.minor,
&priv->device[i].compdesc.devdesc,
&priv->device[i].dev);
if (ret < 0)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_CLASSOBJECT),
(uint16_t)-ret);
goto errout_with_alloc;
}
priv->cfgdescsize += priv->device[i].compdesc.cfgdescsize;
priv->ninterfaces += priv->device[i].compdesc.devdesc.ninterfaces;
}
priv->ndevices = ndevices;
/* Initialize the USB class driver structure */
#ifdef CONFIG_USBDEV_DUALSPEED
drvr->drvr.speed = USB_SPEED_HIGH;
#else
drvr->drvr.speed = USB_SPEED_FULL;
#endif
drvr->drvr.ops = &g_driverops;
drvr->dev = priv;
/* Register the USB composite class driver */
ret = usbdev_register(&drvr->drvr);
if (ret < 0)
{
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_DEVREGISTER),
(uint16_t)-ret);
goto errout_with_alloc;
}
return (FAR void *)alloc;
errout_with_alloc:
kmm_free(alloc);
return NULL;
}
/****************************************************************************
* Name: composite_uninitialize
*
* Description:
* Un-initialize the USB composite driver. The handle is the USB composite
* class' device object as was returned by composite_initialize(). This
* function will call board-specific implementations in order to free the
* class objects for each of the members of the composite.
*
* Input Parameters:
* handle - The handle returned by a previous call to composite_initialize().
*
* Returned Value:
* None
*
****************************************************************************/
void composite_uninitialize(FAR void *handle)
{
FAR struct composite_alloc_s *alloc = (FAR struct composite_alloc_s *)handle;
FAR struct composite_dev_s *priv;
int i;
DEBUGASSERT(alloc != NULL);
/* First phase uninitialization each of the member classes */
priv = &alloc->dev;
for (i = 0; i < priv->ndevices; i++)
{
priv->device[i].compdesc.uninitialize(priv->device[i].dev);
}
/* Then unregister and destroy the composite class */
usbdev_unregister(&alloc->drvr.drvr);
/* Free any resources used by the composite driver */
/* None */
/* Second phase uninitialization: Clean up all memory resources */
for (i = 0; i < priv->ndevices; i++)
{
priv->device[i].compdesc.uninitialize(priv->device[i].dev);
}
/* Then free the composite driver state structure itself */
kmm_free(priv);
}
/****************************************************************************
* Name: composite_ep0submit
*
* Description:
* Members of the composite cannot send on EP0 directly because EP0 is
* is "owned" by the composite device. Instead, when configured as members
* of a composite device, those classes should call this method so that
* the composite device can send on EP0 onbehalf of the class.
*
****************************************************************************/
int composite_ep0submit(FAR struct usbdevclass_driver_s *driver,
FAR struct usbdev_s *dev,
FAR struct usbdev_req_s *ctrlreq)
{
/* This function is not really necessary in the current design. However,
* keeping this will provide us a little flexibility in the future if
* it becomes necessary to manage the completion callbacks.
*/
return EP_SUBMIT(dev->ep0, ctrlreq);
}
#endif /* CONFIG_USBDEV_COMPOSITE */