2012-01-24 22:51:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/usbdev/composite.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-01-24 22:51:26 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-01-24 22:51:26 +01:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-01-24 22:51:26 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2012-01-25 20:27:20 +01:00
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2012-01-24 22:51:26 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
#include <nuttx/irq.h>
|
2012-01-25 20:27:20 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/usb/usb.h>
|
|
|
|
#include <nuttx/usb/usbdev.h>
|
2012-01-24 22:51:26 +01:00
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
|
|
|
|
|
|
|
#include "composite.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_COMPOSITE
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/* 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;
|
|
|
|
};
|
|
|
|
|
2012-01-24 22:51:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
2018-11-09 14:44:22 +01:00
|
|
|
|
|
|
|
/* USB helpers **************************************************************/
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
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,
|
2012-04-12 18:30:48 +02:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl, FAR uint8_t *dataout,
|
|
|
|
size_t outlen);
|
2012-01-25 20:27:20 +01:00
|
|
|
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);
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* USB class device *********************************************************/
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int composite_bind(FAR struct usbdevclass_driver_s *driver,
|
2012-01-27 00:14:27 +01:00
|
|
|
FAR struct usbdev_s *dev);
|
2012-01-26 18:42:44 +01:00
|
|
|
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,
|
2012-04-12 18:30:48 +02:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl, FAR uint8_t *dataout,
|
|
|
|
size_t outlen);
|
2012-01-26 18:42:44 +01:00
|
|
|
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);
|
2012-01-24 22:51:26 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2017-07-16 16:43:17 +02:00
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/* 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;
|
2022-01-18 02:36:37 +01:00
|
|
|
#ifndef CONFIG_COMPOSITE_BOARD_SERIALSTR
|
2012-01-25 20:27:20 +01:00
|
|
|
const char g_compserialstr[] = CONFIG_COMPOSITE_SERIALSTR;
|
2022-01-18 02:36:37 +01:00
|
|
|
#endif
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2015-10-04 23:04:00 +02:00
|
|
|
/****************************************************************************
|
2012-01-24 22:51:26 +01:00
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_ep0incomplete
|
|
|
|
*
|
|
|
|
* Description:
|
2012-01-26 18:42:44 +01:00
|
|
|
* Handle completion of the composite driver's EP0 control operations
|
2012-01-25 20:27:20 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void composite_ep0incomplete(FAR struct usbdev_ep_s *ep,
|
|
|
|
FAR struct usbdev_req_s *req)
|
|
|
|
{
|
2012-01-26 18:42:44 +01:00
|
|
|
/* Just check the result of the transfer */
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
if (req->result || req->xfrd != req->len)
|
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_REQRESULT),
|
|
|
|
(uint16_t)-req->result);
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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,
|
2012-04-12 18:30:48 +02:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl,
|
|
|
|
FAR uint8_t *dataout, size_t outlen)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
uint16_t index;
|
|
|
|
uint8_t interface;
|
|
|
|
int ret = -EOPNOTSUPP;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
index = GETUINT16(ctrl->index);
|
|
|
|
interface = (uint8_t)(index & 0xff);
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
2017-07-20 17:34:48 +02:00
|
|
|
if (interface >= priv->device[i].compdesc.devinfo.ifnobase &&
|
|
|
|
interface < (priv->device[i].compdesc.devinfo.ifnobase +
|
|
|
|
priv->device[i].compdesc.devinfo.ninterfaces))
|
2017-07-16 16:43:17 +02:00
|
|
|
{
|
|
|
|
ret = CLASS_SETUP(priv->device[i].dev, dev, ctrl, dataout, outlen);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-02 17:12:07 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_msftdescriptor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Assemble the Microsoft OS descriptor from the COMPATIBLE_ID's given
|
|
|
|
* in each device's composite_devdesc_s.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2018-11-09 14:44:22 +01:00
|
|
|
|
2018-11-02 17:12:07 +01:00
|
|
|
#ifdef CONFIG_COMPOSITE_MSFT_OS_DESCRIPTORS
|
2018-11-09 14:44:22 +01:00
|
|
|
static int composite_msftdescriptor(FAR struct composite_dev_s *priv,
|
|
|
|
FAR struct usbdev_s *dev,
|
|
|
|
FAR const struct usb_ctrlreq_s *ctrl,
|
|
|
|
FAR struct usbdev_req_s *ctrl_rsp,
|
|
|
|
FAR bool *dispatched)
|
2018-11-02 17:12:07 +01:00
|
|
|
{
|
|
|
|
if (ctrl->index[0] == MSFTOSDESC_INDEX_FUNCTION)
|
2018-11-09 14:44:22 +01:00
|
|
|
{
|
|
|
|
/* Function descriptor is common to whole device */
|
|
|
|
|
|
|
|
FAR struct usb_msft_os_feature_desc_s *response =
|
|
|
|
(FAR struct usb_msft_os_feature_desc_s *)ctrl_rsp->buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(response, 0, sizeof(*response));
|
|
|
|
|
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
2018-11-02 17:12:07 +01:00
|
|
|
{
|
2018-11-09 14:44:22 +01:00
|
|
|
if (priv->device[i].compdesc.msft_compatible_id[0] != 0)
|
|
|
|
{
|
|
|
|
FAR struct usb_msft_os_function_desc_s *func =
|
|
|
|
&response->function[response->count];
|
|
|
|
|
|
|
|
memset(func, 0, sizeof(*func));
|
|
|
|
func->firstif = priv->device[i].compdesc.devinfo.ifnobase;
|
|
|
|
func->nifs = priv->device[i].compdesc.devinfo.ninterfaces;
|
2021-03-04 08:02:21 +01:00
|
|
|
memcpy(func->compatible_id,
|
|
|
|
priv->device[i].compdesc.msft_compatible_id,
|
2018-11-09 14:44:22 +01:00
|
|
|
sizeof(func->compatible_id));
|
|
|
|
memcpy(func->sub_id, priv->device[i].compdesc.msft_sub_id,
|
|
|
|
sizeof(func->sub_id));
|
|
|
|
|
|
|
|
response->count++;
|
|
|
|
}
|
2018-11-02 17:12:07 +01:00
|
|
|
}
|
2018-11-09 14:44:22 +01:00
|
|
|
|
|
|
|
if (response->count > 0)
|
2018-11-02 17:12:07 +01:00
|
|
|
{
|
2018-11-09 14:44:22 +01:00
|
|
|
size_t total_len = sizeof(struct usb_msft_os_feature_desc_s) +
|
|
|
|
(response->count - 1) *
|
|
|
|
sizeof(struct usb_msft_os_function_desc_s);
|
2020-01-10 16:06:36 +01:00
|
|
|
response->len[0] = (total_len >> 0) & 0xff;
|
|
|
|
response->len[1] = (total_len >> 8) & 0xff;
|
|
|
|
response->len[2] = (total_len >> 16) & 0xff;
|
|
|
|
response->len[3] = (total_len >> 24) & 0xff;
|
2018-11-09 14:44:22 +01:00
|
|
|
response->version[1] = 0x01;
|
|
|
|
response->index[0] = MSFTOSDESC_INDEX_FUNCTION;
|
|
|
|
|
|
|
|
return total_len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
2018-11-02 17:12:07 +01:00
|
|
|
}
|
|
|
|
}
|
2018-11-09 14:44:22 +01:00
|
|
|
else if (ctrl->index[0] == MSFTOSDESC_INDEX_EXTPROP ||
|
|
|
|
ctrl->index[0] == ctrl->value[0])
|
|
|
|
{
|
|
|
|
/* Extended properties are per-interface, pass the request to
|
|
|
|
* subdevice. NOTE: The documentation in OS_Desc_Ext_Prop.docx seems
|
|
|
|
* a bit incorrect here, the interface is in ctrl->value low byte.
|
2021-03-04 08:02:21 +01:00
|
|
|
* Also WinUSB driver has limitation that index[0] will not be correct
|
|
|
|
* if trying to read descriptors using e.g. libusb xusb.exe.
|
2020-01-10 16:06:36 +01:00
|
|
|
*/
|
2018-11-09 14:44:22 +01:00
|
|
|
|
|
|
|
uint8_t interface = ctrl->value[0];
|
|
|
|
int ret = -ENOTSUP;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
|
|
|
{
|
|
|
|
if (interface >= priv->device[i].compdesc.devinfo.ifnobase &&
|
|
|
|
interface < (priv->device[i].compdesc.devinfo.ifnobase +
|
|
|
|
priv->device[i].compdesc.devinfo.ninterfaces))
|
|
|
|
{
|
|
|
|
ret = CLASS_SETUP(priv->device[i].dev, dev, ctrl, NULL, 0);
|
|
|
|
*dispatched = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2018-11-09 14:44:22 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2018-11-02 17:12:07 +01:00
|
|
|
else
|
2018-11-09 14:44:22 +01:00
|
|
|
{
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
2018-11-02 17:12:07 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
2017-07-16 16:43:17 +02:00
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
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);
|
|
|
|
}
|
2017-07-16 16:43:17 +02:00
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
EP_FREEREQ(ep, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* USB Class Driver Methods
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_bind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is bound to a USB device driver
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int composite_bind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
FAR struct composite_dev_s *priv =
|
|
|
|
((FAR struct composite_driver_s *)driver)->dev;
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
int ret;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSBIND, 0);
|
|
|
|
|
|
|
|
/* Bind the structures */
|
|
|
|
|
|
|
|
priv->usbdev = dev;
|
2012-01-26 18:42:44 +01:00
|
|
|
|
|
|
|
/* Save the reference to our private data structure in EP0 so that it
|
|
|
|
* can be recovered in ep0 completion events.
|
|
|
|
*/
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
dev->ep0->priv = priv;
|
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
/* Preallocate one control request */
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
priv->ctrlreq = composite_allocreq(dev->ep0, priv->cfgdescsize);
|
2012-01-25 20:27:20 +01:00
|
|
|
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 */
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
ret = CLASS_BIND(priv->device[i].dev, dev);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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:
|
2012-01-27 00:14:27 +01:00
|
|
|
composite_unbind(driver, dev);
|
2012-01-25 20:27:20 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_unbind
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked when the driver is unbound from a USB device driver
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void composite_unbind(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
FAR struct composite_dev_s *priv;
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSUNBIND, 0);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!driver || !dev || !dev->ep0)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct composite_driver_s *)driver)->dev;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-25 20:27:20 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Make sure that we are not already unbound */
|
|
|
|
|
|
|
|
if (priv != NULL)
|
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/* Unbind the constituent class drivers */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2017-07-16 16:43:17 +02:00
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
|
|
|
{
|
|
|
|
CLASS_UNBIND(priv->device[i].dev, dev);
|
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
/* Free the pre-allocated control request */
|
|
|
|
|
|
|
|
priv->config = COMPOSITE_CONFIGIDNONE;
|
|
|
|
if (priv->ctrlreq != NULL)
|
|
|
|
{
|
|
|
|
composite_freereq(dev->ep0, priv->ctrlreq);
|
|
|
|
priv->ctrlreq = NULL;
|
|
|
|
}
|
2017-07-16 16:43:17 +02:00
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-24 22:51:26 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked for ep0 control requests. This function probably executes
|
|
|
|
* in the context of an interrupt handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static int composite_setup(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev,
|
2012-04-12 18:30:48 +02:00
|
|
|
FAR const struct usb_ctrlreq_s *ctrl,
|
|
|
|
FAR uint8_t *dataout, size_t outlen)
|
2012-01-24 22:51:26 +01:00
|
|
|
{
|
|
|
|
FAR struct composite_dev_s *priv;
|
|
|
|
FAR struct usbdev_req_s *ctrlreq;
|
|
|
|
uint16_t value;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t len;
|
2012-01-25 20:27:20 +01:00
|
|
|
bool dispatched = false;
|
2012-01-24 22:51:26 +01:00
|
|
|
int ret = -EOPNOTSUPP;
|
2019-03-27 04:54:01 +01:00
|
|
|
uint8_t recipient;
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!driver || !dev || !dev->ep0 || !ctrl)
|
2012-01-24 22:51:26 +01:00
|
|
|
{
|
2013-09-07 01:24:02 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_SETUPINVALIDARGS), 0);
|
2012-01-24 22:51:26 +01:00
|
|
|
return -EIO;
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2012-01-24 22:51:26 +01:00
|
|
|
#endif
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/* Extract a reference to private data */
|
2012-01-24 22:51:26 +01:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSSETUP, ctrl->req);
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct composite_driver_s *)driver)->dev;
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-25 20:27:20 +01:00
|
|
|
if (!priv)
|
2012-01-24 22:51:26 +01:00
|
|
|
{
|
2013-09-07 01:24:02 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND2), 0);
|
2012-01-24 22:51:26 +01:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
2017-07-16 16:43:17 +02:00
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
ctrlreq = priv->ctrlreq;
|
2012-01-24 22:51:26 +01:00
|
|
|
|
|
|
|
/* Extract the little-endian 16-bit values to host order */
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
value = GETUINT16(ctrl->value);
|
|
|
|
index = GETUINT16(ctrl->index);
|
|
|
|
len = GETUINT16(ctrl->len);
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
uinfo("type=%02x req=%02x value=%04x index=%04x len=%04x\n",
|
2012-01-24 22:51:26 +01:00
|
|
|
ctrl->type, ctrl->req, value, index, len);
|
2015-07-01 18:02:19 +02:00
|
|
|
UNUSED(index);
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2019-03-27 04:54:01 +01:00
|
|
|
recipient = ctrl->type & USB_REQ_RECIPIENT_MASK;
|
|
|
|
|
|
|
|
if ((ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_STANDARD &&
|
|
|
|
recipient == USB_REQ_RECIPIENT_DEVICE)
|
2012-01-24 22:51:26 +01:00
|
|
|
{
|
|
|
|
/**********************************************************************
|
|
|
|
* Standard Requests
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
switch (ctrl->req)
|
|
|
|
{
|
|
|
|
case USB_REQ_GETDESCRIPTOR:
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
/* The value field specifies the descriptor type in the MS byte
|
|
|
|
* and the descriptor index in the LS byte
|
|
|
|
* (order is little endian)
|
2012-01-24 22:51:26 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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:
|
2012-01-25 20:27:20 +01:00
|
|
|
#endif
|
2012-01-24 22:51:26 +01:00
|
|
|
|
|
|
|
case USB_DESC_TYPE_CONFIG:
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2017-07-16 16:43:17 +02:00
|
|
|
ret = composite_mkcfgdesc(priv, ctrlreq->buf, dev->speed,
|
2012-01-27 22:03:20 +01:00
|
|
|
ctrl->value[1]);
|
2012-01-24 22:51:26 +01:00
|
|
|
#else
|
2017-07-16 16:43:17 +02:00
|
|
|
ret = composite_mkcfgdesc(priv, ctrlreq->buf);
|
2012-01-24 22:51:26 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_DESC_TYPE_STRING:
|
|
|
|
{
|
2012-01-27 22:03:20 +01:00
|
|
|
/* value == string index. Zero is the language ID. */
|
|
|
|
|
|
|
|
uint8_t strid = ctrl->value[0];
|
2020-01-10 16:06:36 +01:00
|
|
|
FAR struct usb_strdesc_s *buf =
|
|
|
|
(FAR struct usb_strdesc_s *)ctrlreq->buf;
|
2012-01-27 22:03:20 +01:00
|
|
|
|
2018-11-09 13:56:58 +01:00
|
|
|
if (strid < COMPOSITE_NSTRIDS)
|
2012-01-27 22:03:20 +01:00
|
|
|
{
|
|
|
|
ret = composite_mkstrdesc(strid, buf);
|
|
|
|
}
|
2018-11-02 17:12:07 +01:00
|
|
|
#ifdef CONFIG_COMPOSITE_MSFT_OS_DESCRIPTORS
|
|
|
|
else if (strid == USB_REQ_GETMSFTOSDESCRIPTOR)
|
|
|
|
{
|
2018-11-09 14:44:22 +01:00
|
|
|
/* Note: Windows has a habit of caching this response,
|
|
|
|
* so if you want to enable/disable it you'll usually
|
|
|
|
* need to change the device serial number afterwards.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const uint8_t msft_response[16] =
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
'M', 0, 'S', 0, 'F', 0, 'T', 0, '1', 0, '0', 0,
|
|
|
|
'0', 0, 0xff, 0
|
2018-11-02 17:12:07 +01:00
|
|
|
};
|
2018-11-09 14:44:22 +01:00
|
|
|
|
2018-11-02 17:12:07 +01:00
|
|
|
buf->len = 18;
|
|
|
|
buf->type = USB_DESC_TYPE_STRING;
|
2022-03-05 18:59:15 +01:00
|
|
|
memcpy(buf + 1, msft_response, 16);
|
2018-11-02 17:12:07 +01:00
|
|
|
ret = buf->len;
|
|
|
|
}
|
|
|
|
#endif
|
2017-07-16 16:43:17 +02:00
|
|
|
else
|
2012-01-27 22:03:20 +01:00
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
if (strid >
|
|
|
|
priv->device[i].compdesc.devinfo.strbase &&
|
|
|
|
strid <=
|
|
|
|
priv->device[i].compdesc.devinfo.strbase +
|
|
|
|
priv->device[i].compdesc.devinfo.nstrings)
|
2017-07-16 16:43:17 +02:00
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
ret = priv->device[i].compdesc.mkstrdesc(
|
|
|
|
strid -
|
|
|
|
priv->device[i].compdesc.devinfo.strbase,
|
|
|
|
buf);
|
2017-07-16 16:43:17 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-01-27 22:03:20 +01:00
|
|
|
}
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(
|
|
|
|
TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_GETUNKNOWNDESC),
|
|
|
|
value);
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SETCONFIGURATION:
|
|
|
|
{
|
|
|
|
if (ctrl->type == 0)
|
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Save the configuration and inform the constituent
|
|
|
|
* classes
|
|
|
|
*/
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
2020-01-10 16:06:36 +01:00
|
|
|
ret = CLASS_SETUP(priv->device[i].dev,
|
|
|
|
dev,
|
|
|
|
ctrl,
|
|
|
|
dataout,
|
|
|
|
outlen);
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
2017-07-16 16:43:17 +02:00
|
|
|
|
|
|
|
dispatched = true;
|
|
|
|
priv->config = value;
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_GETCONFIGURATION:
|
|
|
|
{
|
|
|
|
if (ctrl->type == USB_DIR_IN)
|
|
|
|
{
|
|
|
|
ctrlreq->buf[0] = priv->config;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SETINTERFACE:
|
|
|
|
{
|
2012-01-25 20:27:20 +01:00
|
|
|
if (ctrl->type == USB_REQ_RECIPIENT_INTERFACE &&
|
|
|
|
priv->config == COMPOSITE_CONFIGID)
|
2012-01-24 22:51:26 +01:00
|
|
|
{
|
2012-04-12 18:30:48 +02:00
|
|
|
ret = composite_classsetup(priv, dev, ctrl, dataout, outlen);
|
2012-01-27 22:03:20 +01:00
|
|
|
dispatched = true;
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_GETINTERFACE:
|
|
|
|
{
|
2015-10-10 18:41:00 +02:00
|
|
|
if (ctrl->type == (USB_DIR_IN | USB_REQ_RECIPIENT_INTERFACE) &&
|
2012-01-25 20:27:20 +01:00
|
|
|
priv->config == COMPOSITE_CONFIGIDNONE)
|
2012-01-24 22:51:26 +01:00
|
|
|
{
|
2012-04-12 18:30:48 +02:00
|
|
|
ret = composite_classsetup(priv, dev, ctrl, dataout, outlen);
|
2012-01-27 22:03:20 +01:00
|
|
|
dispatched = true;
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-01-10 16:06:36 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_UNSUPPORTEDSTDREQ),
|
|
|
|
ctrl->req);
|
2012-01-24 22:51:26 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 17:12:07 +01:00
|
|
|
#ifdef CONFIG_COMPOSITE_MSFT_OS_DESCRIPTORS
|
|
|
|
else if (ctrl->req == USB_REQ_GETMSFTOSDESCRIPTOR &&
|
|
|
|
(ctrl->type & USB_REQ_DIR_MASK) == USB_REQ_DIR_IN &&
|
|
|
|
(ctrl->type & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_VENDOR)
|
|
|
|
{
|
|
|
|
ret = composite_msftdescriptor(priv, dev, ctrl, ctrlreq, &dispatched);
|
|
|
|
}
|
|
|
|
#endif
|
2019-03-27 04:54:01 +01:00
|
|
|
else if (recipient == USB_REQ_RECIPIENT_INTERFACE ||
|
|
|
|
recipient == USB_REQ_RECIPIENT_ENDPOINT)
|
2012-01-24 22:51:26 +01:00
|
|
|
{
|
|
|
|
/**********************************************************************
|
|
|
|
* Non-Standard Class Requests
|
|
|
|
**********************************************************************/
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* Class implementations should handle their own interface and
|
|
|
|
* endpoint requests.
|
2012-01-25 20:27:20 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-27 04:54:01 +01:00
|
|
|
ret = composite_classsetup(priv, dev, ctrl, dataout, outlen);
|
|
|
|
dispatched = true;
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
/* 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.
|
2012-01-25 20:27:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (ret >= 0 && !dispatched)
|
|
|
|
{
|
2012-01-26 18:42:44 +01:00
|
|
|
/* Setup the request */
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
ctrlreq->len = MIN(len, ret);
|
|
|
|
ctrlreq->flags = USBDEV_REQFLAGS_NULLPKT;
|
2012-01-26 18:42:44 +01:00
|
|
|
|
|
|
|
/* And submit the request to the USB controller driver */
|
|
|
|
|
|
|
|
ret = EP_SUBMIT(dev->ep0, ctrlreq);
|
2012-01-25 20:27:20 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EPRESPQ),
|
|
|
|
(uint16_t)-ret);
|
2012-01-25 20:27:20 +01:00
|
|
|
ctrlreq->result = OK;
|
|
|
|
composite_ep0incomplete(dev->ep0, ctrlreq);
|
|
|
|
}
|
|
|
|
}
|
2012-01-27 22:03:20 +01:00
|
|
|
|
2012-01-24 22:51:26 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void composite_disconnect(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
FAR struct composite_dev_s *priv;
|
|
|
|
irqstate_t flags;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSDISCONNECT, 0);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!driver || !dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct composite_driver_s *)driver)->dev;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-25 20:27:20 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
|
|
|
|
return;
|
|
|
|
}
|
2012-01-24 22:51:26 +01:00
|
|
|
#endif
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/* Reset the configuration and inform the constituent class drivers of
|
|
|
|
* the disconnection.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2012-01-25 20:27:20 +01:00
|
|
|
priv->config = COMPOSITE_CONFIGIDNONE;
|
2017-07-16 16:43:17 +02:00
|
|
|
|
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
|
|
|
{
|
|
|
|
CLASS_DISCONNECT(priv->device[i].dev, dev);
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
/* Perform the soft connect function so that we will we can be
|
|
|
|
* re-enumerated.
|
|
|
|
*/
|
|
|
|
|
2014-04-13 22:32:20 +02:00
|
|
|
DEV_CONNECT(dev);
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_suspend
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked on a USB suspend event.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void composite_suspend(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
FAR struct composite_dev_s *priv;
|
|
|
|
irqstate_t flags;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
usbtrace(TRACE_CLASSSUSPEND, 0);
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct composite_driver_s *)driver)->dev;
|
2012-01-26 18:42:44 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
/* Forward the suspend event to the constituent devices */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2017-07-16 16:43:17 +02:00
|
|
|
|
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
|
|
|
{
|
|
|
|
CLASS_SUSPEND(priv->device[i].dev, priv->usbdev);
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_resume
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Invoked on a USB resume event.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
static void composite_resume(FAR struct usbdevclass_driver_s *driver,
|
|
|
|
FAR struct usbdev_s *dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
FAR struct composite_dev_s *priv = NULL;
|
|
|
|
irqstate_t flags;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!dev)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_INVALIDARG), 0);
|
|
|
|
return;
|
2020-01-10 16:06:36 +01:00
|
|
|
}
|
2012-01-25 20:27:20 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Extract reference to private data */
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
priv = ((FAR struct composite_driver_s *)driver)->dev;
|
2012-01-26 18:42:44 +01:00
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2012-01-26 18:42:44 +01:00
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_EP0NOTBOUND), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
/* Forward the resume event to the constituent devices */
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
flags = enter_critical_section();
|
2017-07-16 16:43:17 +02:00
|
|
|
|
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
|
|
|
{
|
|
|
|
CLASS_RESUME(priv->device[i].dev, priv->usbdev);
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:32:58 +01:00
|
|
|
leave_critical_section(flags);
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
2012-01-24 22:51:26 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2020-01-10 16:06:36 +01:00
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
2012-01-26 20:37:34 +01:00
|
|
|
* Register USB composite device as configured. This function will call
|
|
|
|
* board-specific implementations in order to obtain the class objects for
|
2017-07-16 16:43:17 +02:00
|
|
|
* each of the members of the composite.
|
2012-01-25 20:27:20 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2012-01-26 20:37:34 +01:00
|
|
|
* None
|
2012-01-25 20:27:20 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2012-01-26 20:37:34 +01:00
|
|
|
* 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.
|
2012-01-25 20:27:20 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
FAR void *composite_initialize(uint8_t ndevices,
|
|
|
|
FAR struct composite_devdesc_s *pdevices)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
|
|
|
FAR struct composite_alloc_s *alloc;
|
|
|
|
FAR struct composite_dev_s *priv;
|
|
|
|
FAR struct composite_driver_s *drvr;
|
|
|
|
int ret;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
DEBUGASSERT(pdevices != NULL && ndevices <= NUM_DEVICES_TO_HANDLE);
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
/* Allocate the structures needed */
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
alloc = (FAR struct composite_alloc_s *)
|
|
|
|
kmm_malloc(sizeof(struct composite_alloc_s));
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
if (!alloc)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_ALLOCDEVSTRUCT), 0);
|
2012-01-26 20:37:34 +01:00
|
|
|
return NULL;
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
priv->cfgdescsize = USB_SIZEOF_CFGDESC;
|
|
|
|
priv->ninterfaces = 0;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
/* Get the constituent class driver objects */
|
2012-01-25 20:27:20 +01:00
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
for (i = 0; i < ndevices; i++)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
memcpy(&priv->device[i].compdesc, &pdevices[i],
|
|
|
|
sizeof(struct composite_devdesc_s));
|
|
|
|
|
|
|
|
ret =
|
|
|
|
priv->device[i].compdesc.classobject(priv->device[i].compdesc.minor,
|
2021-03-04 08:02:21 +01:00
|
|
|
&priv->device[i].compdesc.devinfo,
|
|
|
|
&priv->device[i].dev);
|
2017-07-16 16:43:17 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_CLASSOBJECT),
|
|
|
|
(uint16_t)-ret);
|
|
|
|
goto errout_with_alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->cfgdescsize += priv->device[i].compdesc.cfgdescsize;
|
2017-07-20 17:34:48 +02:00
|
|
|
priv->ninterfaces += priv->device[i].compdesc.devinfo.ninterfaces;
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
priv->ndevices = ndevices;
|
|
|
|
|
2012-01-25 20:27:20 +01:00
|
|
|
/* 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);
|
2017-07-16 16:43:17 +02:00
|
|
|
if (ret < 0)
|
2012-01-25 20:27:20 +01:00
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
usbtrace(TRACE_CLSERROR(USBCOMPOSITE_TRACEERR_DEVREGISTER),
|
|
|
|
(uint16_t)-ret);
|
2012-01-25 20:27:20 +01:00
|
|
|
goto errout_with_alloc;
|
|
|
|
}
|
|
|
|
|
2012-01-27 00:14:27 +01:00
|
|
|
return (FAR void *)alloc;
|
2012-01-25 20:27:20 +01:00
|
|
|
|
|
|
|
errout_with_alloc:
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(alloc);
|
2012-01-26 20:37:34 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-27 00:14:27 +01:00
|
|
|
* Name: composite_uninitialize
|
2012-01-26 20:37:34 +01:00
|
|
|
*
|
|
|
|
* 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
|
2017-07-16 16:43:17 +02:00
|
|
|
* class objects for each of the members of the composite.
|
2012-01-26 20:37:34 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2021-03-04 08:02:21 +01:00
|
|
|
* handle - The handle returned by a previous call to
|
|
|
|
* composite_initialize().
|
2012-01-26 20:37:34 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2012-01-26 20:37:34 +01:00
|
|
|
|
2012-01-27 00:14:27 +01:00
|
|
|
void composite_uninitialize(FAR void *handle)
|
2012-01-26 20:37:34 +01:00
|
|
|
{
|
2021-03-04 08:02:21 +01:00
|
|
|
FAR struct composite_alloc_s *alloc =
|
|
|
|
(FAR struct composite_alloc_s *)handle;
|
2012-01-27 00:14:27 +01:00
|
|
|
FAR struct composite_dev_s *priv;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-26 20:37:34 +01:00
|
|
|
|
2012-01-27 00:14:27 +01:00
|
|
|
DEBUGASSERT(alloc != NULL);
|
2012-01-26 20:37:34 +01:00
|
|
|
|
2013-11-05 16:12:08 +01:00
|
|
|
/* First phase uninitialization each of the member classes */
|
2012-01-26 20:37:34 +01:00
|
|
|
|
2012-01-27 00:14:27 +01:00
|
|
|
priv = &alloc->dev;
|
2012-01-26 20:37:34 +01:00
|
|
|
|
|
|
|
/* Then unregister and destroy the composite class */
|
|
|
|
|
2012-01-27 00:14:27 +01:00
|
|
|
usbdev_unregister(&alloc->drvr.drvr);
|
2012-01-26 20:37:34 +01:00
|
|
|
|
|
|
|
/* Free any resources used by the composite driver */
|
2020-01-10 16:06:36 +01:00
|
|
|
|
2012-01-27 00:14:27 +01:00
|
|
|
/* None */
|
2012-01-26 20:37:34 +01:00
|
|
|
|
2013-11-05 16:12:08 +01:00
|
|
|
/* Second phase uninitialization: Clean up all memory resources */
|
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
for (i = 0; i < priv->ndevices; i++)
|
|
|
|
{
|
|
|
|
priv->device[i].compdesc.uninitialize(priv->device[i].dev);
|
|
|
|
}
|
2013-11-05 16:12:08 +01:00
|
|
|
|
2012-01-26 20:37:34 +01:00
|
|
|
/* Then free the composite driver state structure itself */
|
|
|
|
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(priv);
|
2012-01-25 20:27:20 +01:00
|
|
|
}
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2012-01-26 18:42:44 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
return EP_SUBMIT(dev->ep0, ctrlreq);
|
2012-01-26 18:42:44 +01:00
|
|
|
}
|
|
|
|
|
2012-01-24 22:51:26 +01:00
|
|
|
#endif /* CONFIG_USBDEV_COMPOSITE */
|