2012-01-24 22:51:26 +01:00
|
|
|
/****************************************************************************
|
2012-04-23 18:49:15 +02:00
|
|
|
* drivers/usbdev/composite_desc.c
|
2012-01-24 22:51:26 +01:00
|
|
|
*
|
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>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2012-01-25 20:27:20 +01:00
|
|
|
#include <assert.h>
|
2012-01-24 22:51:26 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2022-01-18 02:36:37 +01:00
|
|
|
#ifdef CONFIG_COMPOSITE_BOARD_SERIALSTR
|
|
|
|
#include <nuttx/board.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-24 22:51:26 +01:00
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
|
|
|
|
|
|
|
#include "composite.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_COMPOSITE
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
2017-07-16 16:43:17 +02:00
|
|
|
|
2012-01-24 22:51:26 +01:00
|
|
|
/* Device Descriptor */
|
|
|
|
|
|
|
|
static const struct usb_devdesc_s g_devdesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_DEVDESC, /* len */
|
|
|
|
USB_DESC_TYPE_DEVICE, /* type */
|
|
|
|
{ /* usb */
|
|
|
|
LSBYTE(0x0200),
|
|
|
|
MSBYTE(0x0200)
|
|
|
|
},
|
2017-07-16 16:43:17 +02:00
|
|
|
#ifndef CONFIG_COMPOSITE_IAD
|
2012-01-31 15:15:36 +01:00
|
|
|
USB_CLASS_MISC, /* classid */
|
2012-01-27 17:25:57 +01:00
|
|
|
2, /* subclass */
|
|
|
|
1, /* protocol */
|
|
|
|
#else
|
2012-01-31 15:15:36 +01:00
|
|
|
USB_CLASS_PER_INTERFACE, /* classid */
|
2012-01-24 22:51:26 +01:00
|
|
|
0, /* subclass */
|
|
|
|
0, /* protocol */
|
2012-01-27 17:25:57 +01:00
|
|
|
#endif
|
2012-01-24 22:51:26 +01:00
|
|
|
CONFIG_COMPOSITE_EP0MAXPACKET, /* maxpacketsize */
|
|
|
|
{
|
|
|
|
LSBYTE(CONFIG_COMPOSITE_VENDORID), /* vendor */
|
|
|
|
MSBYTE(CONFIG_COMPOSITE_VENDORID)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LSBYTE(CONFIG_COMPOSITE_PRODUCTID), /* product */
|
|
|
|
MSBYTE(CONFIG_COMPOSITE_PRODUCTID)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
LSBYTE(CONFIG_COMPOSITE_VERSIONNO), /* device */
|
|
|
|
MSBYTE(CONFIG_COMPOSITE_VERSIONNO)
|
|
|
|
},
|
|
|
|
COMPOSITE_MANUFACTURERSTRID, /* imfgr */
|
|
|
|
COMPOSITE_PRODUCTSTRID, /* iproduct */
|
|
|
|
COMPOSITE_SERIALSTRID, /* serno */
|
|
|
|
COMPOSITE_NCONFIGS /* nconfigs */
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
static const struct usb_qualdesc_s g_qualdesc =
|
|
|
|
{
|
|
|
|
USB_SIZEOF_QUALDESC, /* len */
|
|
|
|
USB_DESC_TYPE_DEVICEQUALIFIER, /* type */
|
|
|
|
{ /* usb */
|
|
|
|
LSBYTE(0x0200),
|
|
|
|
MSBYTE(0x0200)
|
|
|
|
},
|
2012-01-31 15:15:36 +01:00
|
|
|
USB_CLASS_VENDOR_SPEC, /* classid */
|
2012-01-24 22:51:26 +01:00
|
|
|
0, /* subclass */
|
|
|
|
0, /* protocol */
|
|
|
|
CONFIG_COMPOSITE_EP0MAXPACKET, /* mxpacketsize */
|
|
|
|
COMPOSITE_NCONFIGS, /* nconfigs */
|
|
|
|
0, /* reserved */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-01-25 21:17:59 +01:00
|
|
|
* Name: composite_mkstrdesc
|
2012-01-24 22:51:26 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Construct a string descriptor
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int composite_mkstrdesc(uint8_t id, struct usb_strdesc_s *strdesc)
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
int len;
|
|
|
|
int ndata;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
/* Descriptor 0 is the language id */
|
|
|
|
|
|
|
|
strdesc->len = 4;
|
|
|
|
strdesc->type = USB_DESC_TYPE_STRING;
|
2012-01-25 20:27:20 +01:00
|
|
|
strdesc->data[0] = LSBYTE(COMPOSITE_STR_LANGUAGE);
|
|
|
|
strdesc->data[1] = MSBYTE(COMPOSITE_STR_LANGUAGE);
|
2012-01-24 22:51:26 +01:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2012-01-27 22:03:20 +01:00
|
|
|
case COMPOSITE_MANUFACTURERSTRID:
|
2012-01-25 20:27:20 +01:00
|
|
|
str = g_compvendorstr;
|
2012-01-24 22:51:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COMPOSITE_PRODUCTSTRID:
|
2012-01-25 20:27:20 +01:00
|
|
|
str = g_compproductstr;
|
2012-01-24 22:51:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COMPOSITE_SERIALSTRID:
|
2022-01-18 02:36:37 +01:00
|
|
|
#ifdef CONFIG_COMPOSITE_BOARD_SERIALSTR
|
|
|
|
str = board_usbdev_serialstr();
|
|
|
|
#else
|
2012-01-25 20:27:20 +01:00
|
|
|
str = g_compserialstr;
|
2022-01-18 02:36:37 +01:00
|
|
|
#endif
|
2012-01-24 22:51:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case COMPOSITE_CONFIGSTRID:
|
2012-01-25 20:27:20 +01:00
|
|
|
str = CONFIG_COMPOSITE_CONFIGSTR;
|
2012-01-24 22:51:26 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-01-10 16:06:36 +01:00
|
|
|
/* The string is utf16-le. The poor man's utf-8 to utf16-le
|
|
|
|
* conversion below will only handle 7-bit en-us ascii
|
|
|
|
*/
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
for (i = 0, ndata = 0; i < len; i++, ndata += 2)
|
|
|
|
{
|
|
|
|
strdesc->data[ndata] = str[i];
|
|
|
|
strdesc->data[ndata + 1] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
strdesc->len = ndata + 2;
|
|
|
|
strdesc->type = USB_DESC_TYPE_STRING;
|
|
|
|
return strdesc->len;
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_getepdesc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return a pointer to the raw device descriptor
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR const struct usb_devdesc_s *composite_getdevdesc(void)
|
|
|
|
{
|
|
|
|
return &g_devdesc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_mkcfgdesc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Construct the configuration descriptor
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2021-03-04 08:02:21 +01:00
|
|
|
int16_t composite_mkcfgdesc(FAR struct composite_dev_s *priv,
|
|
|
|
FAR uint8_t *buf,
|
2017-07-16 16:43:17 +02:00
|
|
|
uint8_t speed, uint8_t type)
|
2012-01-24 22:51:26 +01:00
|
|
|
#else
|
2021-03-04 08:02:21 +01:00
|
|
|
int16_t composite_mkcfgdesc(FAR struct composite_dev_s *priv,
|
|
|
|
FAR uint8_t *buf)
|
2012-01-24 22:51:26 +01:00
|
|
|
#endif
|
|
|
|
{
|
2017-07-16 16:43:17 +02:00
|
|
|
FAR struct usb_cfgdesc_s *cfgdesc = (FAR struct usb_cfgdesc_s *)buf;
|
2012-01-25 20:27:20 +01:00
|
|
|
int16_t len;
|
2012-01-27 17:25:57 +01:00
|
|
|
int16_t total;
|
2017-07-16 16:43:17 +02:00
|
|
|
int i;
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
/* Configuration descriptor for the composite device */
|
2020-01-10 16:06:36 +01:00
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
/* Fill in the values directly into the buf */
|
|
|
|
|
|
|
|
cfgdesc->len = USB_SIZEOF_CFGDESC; /* Descriptor length */
|
2020-01-10 16:06:36 +01:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
cfgdesc->type = type; /* Descriptor type */
|
|
|
|
#else
|
2017-07-16 16:43:17 +02:00
|
|
|
cfgdesc->type = USB_DESC_TYPE_CONFIG; /* Descriptor type */
|
2020-01-10 16:06:36 +01:00
|
|
|
#endif
|
2017-07-16 16:43:17 +02:00
|
|
|
cfgdesc->totallen[0] = LSBYTE(priv->cfgdescsize); /* Lower Byte of Total length */
|
|
|
|
cfgdesc->totallen[1] = MSBYTE(priv->cfgdescsize); /* High Byte of Total length */
|
|
|
|
cfgdesc->ninterfaces = priv->ninterfaces; /* Number of interfaces */
|
|
|
|
cfgdesc->cfgvalue = COMPOSITE_CONFIGID; /* Configuration value */
|
|
|
|
cfgdesc->icfg = COMPOSITE_CONFIGSTRID; /* Configuration */
|
|
|
|
cfgdesc->attr = USB_CONFIG_ATTR_ONE | /* Attributes */
|
|
|
|
COMPOSITE_SELFPOWERED |
|
|
|
|
COMPOSITE_REMOTEWAKEUP;
|
|
|
|
cfgdesc->mxpower = (CONFIG_USBDEV_MAXPOWER + 1) / 2; /* Max power (mA/2) */
|
|
|
|
|
2021-03-04 08:02:21 +01:00
|
|
|
/* increment the size and buf to point right behind the information
|
|
|
|
* filled in
|
|
|
|
*/
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2012-01-27 17:25:57 +01:00
|
|
|
total = USB_SIZEOF_CFGDESC;
|
2017-07-16 16:43:17 +02:00
|
|
|
buf += USB_SIZEOF_CFGDESC;
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2017-07-16 16:43:17 +02:00
|
|
|
/* Copy all contained interface descriptors into the buffer too */
|
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-24 22:51:26 +01:00
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
2017-07-16 16:43:17 +02:00
|
|
|
len = priv->device[i].compdesc.mkconfdesc(buf,
|
2021-03-04 08:02:21 +01:00
|
|
|
&priv->device[i].compdesc.devinfo,
|
|
|
|
speed, type);
|
2017-07-16 16:43:17 +02:00
|
|
|
total += len;
|
|
|
|
buf += len;
|
2012-01-24 22:51:26 +01:00
|
|
|
#else
|
2017-07-16 16:43:17 +02:00
|
|
|
len = priv->device[i].compdesc.mkconfdesc(buf,
|
2021-03-04 08:02:21 +01:00
|
|
|
&priv->device[i].compdesc.devinfo);
|
2017-07-16 16:43:17 +02:00
|
|
|
total += len;
|
|
|
|
buf += len;
|
2012-01-24 22:51:26 +01:00
|
|
|
#endif
|
2017-07-16 16:43:17 +02:00
|
|
|
}
|
2012-01-24 22:51:26 +01:00
|
|
|
|
2012-01-27 17:25:57 +01:00
|
|
|
return total;
|
2012-01-24 22:51:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: composite_getqualdesc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return a pointer to the raw qual descriptor
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_DUALSPEED
|
|
|
|
FAR const struct usb_qualdesc_s *composite_getqualdesc(void)
|
|
|
|
{
|
|
|
|
return &g_qualdesc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CONFIG_USBDEV_COMPOSITE */
|