1beacf11bSopenharmony_ci/****************************************************************************
2beacf11bSopenharmony_ci * drivers/usbdev/composite.h
3beacf11bSopenharmony_ci *
4beacf11bSopenharmony_ci *   Copyright (C) 2011-2012, 2017 Gregory Nutt. All rights reserved.
5beacf11bSopenharmony_ci *   Copyright (c) Huawei Technologies Co., Ltd. 2017-2019. All rights reserved.
6beacf11bSopenharmony_ci *   Author: Gregory Nutt <gnutt@nuttx.org>
7beacf11bSopenharmony_ci *
8beacf11bSopenharmony_ci * Redistribution and use in source and binary forms, with or without
9beacf11bSopenharmony_ci * modification, are permitted provided that the following conditions
10beacf11bSopenharmony_ci * are met:
11beacf11bSopenharmony_ci *
12beacf11bSopenharmony_ci * 1. Redistributions of source code must retain the above copyright
13beacf11bSopenharmony_ci *    notice, this list of conditions and the following disclaimer.
14beacf11bSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright
15beacf11bSopenharmony_ci *    notice, this list of conditions and the following disclaimer in
16beacf11bSopenharmony_ci *    the documentation and/or other materials provided with the
17beacf11bSopenharmony_ci *    distribution.
18beacf11bSopenharmony_ci * 3. Neither the name NuttX nor the names of its contributors may be
19beacf11bSopenharmony_ci *    used to endorse or promote products derived from this software
20beacf11bSopenharmony_ci *    without specific prior written permission.
21beacf11bSopenharmony_ci *
22beacf11bSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23beacf11bSopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24beacf11bSopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25beacf11bSopenharmony_ci * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26beacf11bSopenharmony_ci * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27beacf11bSopenharmony_ci * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28beacf11bSopenharmony_ci * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29beacf11bSopenharmony_ci * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30beacf11bSopenharmony_ci * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31beacf11bSopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32beacf11bSopenharmony_ci * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33beacf11bSopenharmony_ci * POSSIBILITY OF SUCH DAMAGE.
34beacf11bSopenharmony_ci *
35beacf11bSopenharmony_ci ****************************************************************************/
36beacf11bSopenharmony_ci/****************************************************************************
37beacf11bSopenharmony_ci * Notice of Export Control Law
38beacf11bSopenharmony_ci * ===============================================
39beacf11bSopenharmony_ci * Huawei LiteOS may be subject to applicable export control laws and regulations,
40beacf11bSopenharmony_ci * which might include those applicable to Huawei LiteOS of U.S. and the country in
41beacf11bSopenharmony_ci * which you are located.
42beacf11bSopenharmony_ci * Import, export and usage of Huawei LiteOS in any manner by you shall be in
43beacf11bSopenharmony_ci * compliance with such applicable export control laws and regulations.
44beacf11bSopenharmony_ci ****************************************************************************/
45beacf11bSopenharmony_ci
46beacf11bSopenharmony_ci#ifndef __COMPOSITE_H__
47beacf11bSopenharmony_ci#define __COMPOSITE_H__
48beacf11bSopenharmony_ci
49beacf11bSopenharmony_ci#include "gadget/usbdev.h"
50beacf11bSopenharmony_ci#include "implementation/usb_init.h"
51beacf11bSopenharmony_ci#include "osal_atomic.h"
52beacf11bSopenharmony_ci#include <linux/kernel.h>
53beacf11bSopenharmony_ci
54beacf11bSopenharmony_ci#define USB_COMP_EP0_BUFSIZ        4096U
55beacf11bSopenharmony_ci#define USB_DWC_U2_MAX_PACKET_SIZE 512
56beacf11bSopenharmony_ci#define USB_DWC_MAX_PACKET_SIZE    1024
57beacf11bSopenharmony_ci
58beacf11bSopenharmony_ci#define USB_COMPOSITE_DEV_NUM      2
59beacf11bSopenharmony_ci
60beacf11bSopenharmony_ci#define HSETW(ptr, val)  ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
61beacf11bSopenharmony_ci#define HSETDW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8), \
62beacf11bSopenharmony_ci                                 (uint8_t)((val) >> 16), (uint8_t)((val) >> 24) }
63beacf11bSopenharmony_ci
64beacf11bSopenharmony_ci/* This is a flag that device strings struct is ended. */
65beacf11bSopenharmony_ci
66beacf11bSopenharmony_ci#define USBD_DEVICE_STRINGS_END { 0, NULL }
67beacf11bSopenharmony_ci
68beacf11bSopenharmony_ci#define NO_DEV                    ENODEV
69beacf11bSopenharmony_ci#define INVAL_ARGU                EINVAL
70beacf11bSopenharmony_ci#define MAX_CONFIG_INTERFACES_NUM 16 /* arbitrary; max 255 */
71beacf11bSopenharmony_ci
72beacf11bSopenharmony_ci/* predefined index for usb_composite_driver */
73beacf11bSopenharmony_ci
74beacf11bSopenharmony_cienum
75beacf11bSopenharmony_ci{
76beacf11bSopenharmony_ci  USBD_GADGET_MANUFACTURER_IDX = 0,
77beacf11bSopenharmony_ci  USBD_GADGET_PRODUCT_IDX,
78beacf11bSopenharmony_ci  USBD_GADGET_SERIAL_IDX,
79beacf11bSopenharmony_ci  USBD_GADGET_FIRST_AVAIL_IDX,
80beacf11bSopenharmony_ci};
81beacf11bSopenharmony_ci
82beacf11bSopenharmony_cistruct usbd_string
83beacf11bSopenharmony_ci{
84beacf11bSopenharmony_ci  u8          id;
85beacf11bSopenharmony_ci  const char  *s;
86beacf11bSopenharmony_ci};
87beacf11bSopenharmony_ci
88beacf11bSopenharmony_ci#define NUM_DEVICES_TO_HANDLE         (4)
89beacf11bSopenharmony_ci
90beacf11bSopenharmony_ci/* Descriptors **************************************************************/
91beacf11bSopenharmony_ci/* These settings are not modifiable via the NuttX configuration */
92beacf11bSopenharmony_ci
93beacf11bSopenharmony_ci#define COMPOSITE_CONFIGIDNONE        (0)  /* Config ID = 0 means to return to address mode */
94beacf11bSopenharmony_ci#define COMPOSITE_CONFIGID            (1)  /* The only supported configuration ID */
95beacf11bSopenharmony_ci
96beacf11bSopenharmony_ci/* String language */
97beacf11bSopenharmony_ci
98beacf11bSopenharmony_ci#define COMPOSITE_STR_LANGUAGE        (0x0409) /* en-us */
99beacf11bSopenharmony_ci
100beacf11bSopenharmony_ci/* Descriptor strings */
101beacf11bSopenharmony_ci
102beacf11bSopenharmony_ci#define COMPOSITE_MANUFACTURERSTRID   (1)
103beacf11bSopenharmony_ci#define COMPOSITE_PRODUCTSTRID        (2)
104beacf11bSopenharmony_ci#define COMPOSITE_SERIALSTRID         (3)
105beacf11bSopenharmony_ci#define COMPOSITE_CONFIGSTRID         (4)
106beacf11bSopenharmony_ci
107beacf11bSopenharmony_ci#ifndef MIN
108beacf11bSopenharmony_ci#  define MIN(a, b) ((a) < (b) ? (a) : (b))
109beacf11bSopenharmony_ci#endif
110beacf11bSopenharmony_ci
111beacf11bSopenharmony_ci#ifndef MAX
112beacf11bSopenharmony_ci#  define MAX(a, b) ((a) > (b) ? (a) : (b))
113beacf11bSopenharmony_ci#endif
114beacf11bSopenharmony_ci
115beacf11bSopenharmony_cistruct composite_devobj_s
116beacf11bSopenharmony_ci{
117beacf11bSopenharmony_ci  /* Device description given by the user code in the dynamic
118beacf11bSopenharmony_ci   * configuration.
119beacf11bSopenharmony_ci   */
120beacf11bSopenharmony_ci
121beacf11bSopenharmony_ci  struct composite_devdesc_s compdesc;
122beacf11bSopenharmony_ci
123beacf11bSopenharmony_ci  /* Pointer to device class */
124beacf11bSopenharmony_ci
125beacf11bSopenharmony_ci  struct usbdevclass_driver_s *dev;
126beacf11bSopenharmony_ci};
127beacf11bSopenharmony_ci
128beacf11bSopenharmony_ci/* This structure describes the internal state of the driver */
129beacf11bSopenharmony_ci
130beacf11bSopenharmony_cistruct composite_dev_s
131beacf11bSopenharmony_ci{
132beacf11bSopenharmony_ci  struct usbdev_s      *usbdev;      /* usbdev driver pointer */
133beacf11bSopenharmony_ci  uint8_t               config;      /* Configuration number */
134beacf11bSopenharmony_ci  struct usbdev_req_s  *ctrlreq;     /* Allocated control request */
135beacf11bSopenharmony_ci  uint8_t               ndevices;    /* Num devices in this composite device */
136beacf11bSopenharmony_ci  int                   cfgdescsize; /* Total size of the configuration descriptor: */
137beacf11bSopenharmony_ci  int                   ninterfaces; /* The total number of interfaces in this composite device */
138beacf11bSopenharmony_ci
139beacf11bSopenharmony_ci  struct composite_devobj_s device[NUM_DEVICES_TO_HANDLE]; /* Device class object */
140beacf11bSopenharmony_ci};
141beacf11bSopenharmony_ci
142beacf11bSopenharmony_ci/* The internal version of the class driver */
143beacf11bSopenharmony_ci
144beacf11bSopenharmony_cistruct composite_driver_s
145beacf11bSopenharmony_ci{
146beacf11bSopenharmony_ci  struct usbdevclass_driver_s drvr;
147beacf11bSopenharmony_ci  struct composite_dev_s     *dev;
148beacf11bSopenharmony_ci};
149beacf11bSopenharmony_ci
150beacf11bSopenharmony_ci/* This structure describes the internal state of the driver */
151beacf11bSopenharmony_ci
152beacf11bSopenharmony_cistruct composite_softc
153beacf11bSopenharmony_ci{
154beacf11bSopenharmony_ci  struct composite_dev_s    dev;
155beacf11bSopenharmony_ci  struct composite_driver_s drvr;
156beacf11bSopenharmony_ci
157beacf11bSopenharmony_ci#define TASK_STATE_EXIT    0
158beacf11bSopenharmony_ci#define TASK_STATE_RUNNING 1
159beacf11bSopenharmony_ci  unsigned int task_state;
160beacf11bSopenharmony_ci
161beacf11bSopenharmony_ci#define TASK_EVENT_DATA   0x01
162beacf11bSopenharmony_ci#define TASK_EVENT_EXIT   0x10
163beacf11bSopenharmony_ci  EVENT_CB_S task_event;
164beacf11bSopenharmony_ci  struct mtx task_mtx;
165beacf11bSopenharmony_ci  void *parnet_conext;
166beacf11bSopenharmony_ci};
167beacf11bSopenharmony_ci
168beacf11bSopenharmony_ciextern void composite_mkdevdesc(struct composite_dev_s *priv, uint8_t *buf);
169beacf11bSopenharmony_ciextern int composite_mkstrdesc(struct composite_dev_s *priv, uint8_t id, uint16_t index, uint8_t *buf);
170beacf11bSopenharmony_ciextern int16_t composite_mkcfgdesc(struct composite_dev_s *priv, uint8_t *buf);
171beacf11bSopenharmony_ciextern int composite_initialize(struct composite_softc *softc, uint8_t ndevices,
172beacf11bSopenharmony_ci                                struct composite_devdesc_s *pdevices);
173beacf11bSopenharmony_ciextern void usbd_configep_byspeed(struct usbdev_s *dev, struct usb_endpoint_descriptor *ep_desc);
174beacf11bSopenharmony_ciextern struct composite_devobj_s *usbclass_devobj_get(struct composite_dev_s *cdev, device_type type);
175beacf11bSopenharmony_ciextern int usbd_gadget_attach_driver(void *context,
176beacf11bSopenharmony_ci                                     struct usbdevclass_driver_s *driver);
177beacf11bSopenharmony_ciextern int usbd_gadget_detach_driver(void *context,
178beacf11bSopenharmony_ci                                     struct usbdevclass_driver_s *driver);
179beacf11bSopenharmony_ciextern int usbd_start_udc(void);
180beacf11bSopenharmony_ciextern int usbd_stop_udc(void);
181beacf11bSopenharmony_ciextern char *dev_name_get(void);
182beacf11bSopenharmony_ciextern bool device_is_uvc(void);
183beacf11bSopenharmony_ci
184beacf11bSopenharmony_ci#endif
185