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