1beacf11bSopenharmony_ci/* ----------------------------------------------------------------------------
2beacf11bSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
3beacf11bSopenharmony_ci * Description: LiteOS USB Driver Camera
4beacf11bSopenharmony_ci * Author: huangjieliang
5beacf11bSopenharmony_ci * Create: 2018-05-17
6beacf11bSopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
7beacf11bSopenharmony_ci * are permitted provided that the following conditions are met:
8beacf11bSopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
9beacf11bSopenharmony_ci * conditions and the following disclaimer.
10beacf11bSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11beacf11bSopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials
12beacf11bSopenharmony_ci * provided with the distribution.
13beacf11bSopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14beacf11bSopenharmony_ci * to endorse or promote products derived from this software without specific prior written
15beacf11bSopenharmony_ci * permission.
16beacf11bSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17beacf11bSopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18beacf11bSopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19beacf11bSopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20beacf11bSopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21beacf11bSopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22beacf11bSopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23beacf11bSopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24beacf11bSopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25beacf11bSopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26beacf11bSopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27beacf11bSopenharmony_ci * --------------------------------------------------------------------------- */
28beacf11bSopenharmony_ci/* ----------------------------------------------------------------------------
29beacf11bSopenharmony_ci * Notice of Export Control Law
30beacf11bSopenharmony_ci * ===============================================
31beacf11bSopenharmony_ci * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
32beacf11bSopenharmony_ci * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
33beacf11bSopenharmony_ci * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
34beacf11bSopenharmony_ci * applicable export control laws and regulations.
35beacf11bSopenharmony_ci * --------------------------------------------------------------------------- */
36beacf11bSopenharmony_ci
37beacf11bSopenharmony_ci#include "gadget/f_uvc.h"
38beacf11bSopenharmony_ci#include "gadget/hicamera_control.h"
39beacf11bSopenharmony_ci#include "gadget/f_uac.h"
40beacf11bSopenharmony_ci#include "implementation/global_implementation.h"
41beacf11bSopenharmony_ci#ifdef LOSCFG_DRIVERS_USB2_DEVICE_CONTROLLER
42beacf11bSopenharmony_ci#include "controller/usb_device/dwc_otg_pcd.h"
43beacf11bSopenharmony_ci#endif
44beacf11bSopenharmony_ci
45beacf11bSopenharmony_ci#ifdef __cplusplus
46beacf11bSopenharmony_ci#if __cplusplus
47beacf11bSopenharmony_ciextern "C" {
48beacf11bSopenharmony_ci#endif /* __cplusplus */
49beacf11bSopenharmony_ci#endif /* __cplusplus */
50beacf11bSopenharmony_ci
51beacf11bSopenharmony_ciint usbdev_camera_initialize(struct module *mod, int n, void *arg);
52beacf11bSopenharmony_ci
53beacf11bSopenharmony_cistatic const driver_t g_fcamera_driver =
54beacf11bSopenharmony_ci{
55beacf11bSopenharmony_ci  .name     = "fcamera",
56beacf11bSopenharmony_ci  .methods  = NULL,
57beacf11bSopenharmony_ci  .size     = 0
58beacf11bSopenharmony_ci};
59beacf11bSopenharmony_ci
60beacf11bSopenharmony_ci/* private device class information */
61beacf11bSopenharmony_ci
62beacf11bSopenharmony_cistatic devclass_t g_fcamera_devclass;
63beacf11bSopenharmony_ciDRIVER_MODULE(fcamera, simple, g_fcamera_driver, g_fcamera_devclass, usbdev_camera_initialize, 0);
64beacf11bSopenharmony_ci
65beacf11bSopenharmony_ci#define CAMERA_STR_LANG   0
66beacf11bSopenharmony_cistatic const char g_fcamera_str_lang[] =
67beacf11bSopenharmony_ci{
68beacf11bSopenharmony_ci  4, UDESC_STRING,
69beacf11bSopenharmony_ci  0x09, 0x04
70beacf11bSopenharmony_ci};
71beacf11bSopenharmony_ci
72beacf11bSopenharmony_ci#define CAMERA_STR_IDX_MAN   1
73beacf11bSopenharmony_cistatic const char g_fcamera_str_manufacturer[] =
74beacf11bSopenharmony_ci{
75beacf11bSopenharmony_ci  14, UDESC_STRING,
76beacf11bSopenharmony_ci  'H', 0, 'U', 0, 'A', 0, 'W', 0, 'E', 0, 'I', 0
77beacf11bSopenharmony_ci};
78beacf11bSopenharmony_ci
79beacf11bSopenharmony_ci#define CAMERA_STR_IDX_PRODUCT   2
80beacf11bSopenharmony_cistatic const char g_fcamera_str_product[] =
81beacf11bSopenharmony_ci{
82beacf11bSopenharmony_ci  14, UDESC_STRING,
83beacf11bSopenharmony_ci  'L', 0, 'i', 0, 't', 0, 'e', 0, 'O', 0, 'S', 0
84beacf11bSopenharmony_ci};
85beacf11bSopenharmony_ci
86beacf11bSopenharmony_ci#define CAMERA_STR_IDX_CONFIG   5
87beacf11bSopenharmony_cistatic const char g_fcamera_str_config[] =
88beacf11bSopenharmony_ci{
89beacf11bSopenharmony_ci  22, UDESC_STRING,
90beacf11bSopenharmony_ci  'U', 0, 'V', 0, 'C', 0, ' ', 0, 'C', 0, 'A', 0, 'M', 0,
91beacf11bSopenharmony_ci  'E', 0, 'R', 0, 'A', 0
92beacf11bSopenharmony_ci};
93beacf11bSopenharmony_ci
94beacf11bSopenharmony_ci#define CAMERA_STR_IDX_VIDEO   4
95beacf11bSopenharmony_cistatic const char g_fcamera_str_video[] =
96beacf11bSopenharmony_ci{
97beacf11bSopenharmony_ci  12, UDESC_STRING,
98beacf11bSopenharmony_ci  'V', 0, 'i', 0, 'd', 0, 'e', 0, 'o', 0
99beacf11bSopenharmony_ci};
100beacf11bSopenharmony_ci
101beacf11bSopenharmony_ci#define CAMERA_STR_IDX_INTERFACE   6
102beacf11bSopenharmony_cistatic const char g_fcamera_str_interface[] =
103beacf11bSopenharmony_ci{
104beacf11bSopenharmony_ci  32, UDESC_STRING,
105beacf11bSopenharmony_ci  'V', 0, 'i', 0, 'd', 0, 'e', 0, 'o', 0, ' ', 0, 'S', 0, 't', 0, 'r', 0,
106beacf11bSopenharmony_ci  'e', 0, 'a', 0, 'm', 0, 'i', 0, 'n', 0, 'g', 0
107beacf11bSopenharmony_ci};
108beacf11bSopenharmony_ci
109beacf11bSopenharmony_ci#define CAMERA_IN_TERMINAL   8
110beacf11bSopenharmony_cistatic const char g_fcamera_input_terminal[] =
111beacf11bSopenharmony_ci{
112beacf11bSopenharmony_ci  46, UDESC_STRING,
113beacf11bSopenharmony_ci  'C', 0, 'a', 0, 'p', 0, 't', 0, 'u', 0, 'r', 0, 'e', 0, ' ', 0,
114beacf11bSopenharmony_ci  'I', 0, 'n', 0, 'p', 0, 'u', 0, 't', 0, ' ', 0, 't', 0, 'e', 0,
115beacf11bSopenharmony_ci  'r', 0, 'm', 0, 'i', 0, 'n', 0, 'a', 0, 'l', 0
116beacf11bSopenharmony_ci};
117beacf11bSopenharmony_ci
118beacf11bSopenharmony_ci#define CAMERA_OUT_TERMINAL   11
119beacf11bSopenharmony_cistatic const char g_fcamera_output_terminal[] =
120beacf11bSopenharmony_ci{
121beacf11bSopenharmony_ci  48, UDESC_STRING,
122beacf11bSopenharmony_ci  'C', 0, 'a', 0, 'p', 0, 't', 0, 'u', 0, 'r', 0, 'e', 0, ' ', 0, 'O', 0,
123beacf11bSopenharmony_ci  'u', 0, 't', 0, 'p', 0, 'u', 0, 't', 0, ' ', 0, 't', 0, 'e', 0, 'r', 0,
124beacf11bSopenharmony_ci  'm', 0, 'i', 0, 'n', 0, 'a', 0, 'l', 0
125beacf11bSopenharmony_ci};
126beacf11bSopenharmony_ci
127beacf11bSopenharmony_cistatic const struct usb_device_descriptor g_fcamera_device_desc =
128beacf11bSopenharmony_ci{
129beacf11bSopenharmony_ci  .bLength            = sizeof(struct usb_device_descriptor),
130beacf11bSopenharmony_ci  .bDescriptorType    = UDESC_DEVICE,           /* Constant for device descriptor */
131beacf11bSopenharmony_ci  HSETW(.bcdUSB, UD_BCD_USB),                   /* USB version required: 2.0 */
132beacf11bSopenharmony_ci  .bDeviceClass       = UICLASS_IAD,            /* Miscellaneous Device Class */
133beacf11bSopenharmony_ci  .bDeviceSubClass    = 0x02,                   /* Common Class */
134beacf11bSopenharmony_ci  .bDeviceProtocol    = 0x01,                   /* Interface Association Descriptor */
135beacf11bSopenharmony_ci  .bMaxPacketSize     = UD_USB_MPS,             /* Control Endpoint packet size */
136beacf11bSopenharmony_ci  HSETW(.idVendor,    0x1d6b),                  /* Vendor ID of Huawei Technologies */
137beacf11bSopenharmony_ci  HSETW(.idProduct,   0x0102),                  /* Product ID, webcamera? */
138beacf11bSopenharmony_ci  HSETW(.bcdDevice,   0x0318),                  /* Device release code */
139beacf11bSopenharmony_ci  .iManufacturer      = CAMERA_STR_IDX_MAN,     /* Manufacturer name, string index */
140beacf11bSopenharmony_ci  .iProduct           = CAMERA_STR_IDX_PRODUCT, /* Product name, string index */
141beacf11bSopenharmony_ci  .iSerialNumber      = 0,                      /* Not Used */
142beacf11bSopenharmony_ci  .bNumConfigurations = 1                       /* One Configuration */
143beacf11bSopenharmony_ci};
144beacf11bSopenharmony_ci
145beacf11bSopenharmony_cistruct usbd_string g_fcamera_device_strings[9] =
146beacf11bSopenharmony_ci{
147beacf11bSopenharmony_ci  { CAMERA_STR_LANG,          g_fcamera_str_lang },
148beacf11bSopenharmony_ci  { CAMERA_STR_IDX_MAN,       g_fcamera_str_manufacturer },
149beacf11bSopenharmony_ci  { CAMERA_STR_IDX_PRODUCT,   g_fcamera_str_product },
150beacf11bSopenharmony_ci  { CAMERA_STR_IDX_VIDEO,     g_fcamera_str_video },
151beacf11bSopenharmony_ci  { CAMERA_STR_IDX_CONFIG,    g_fcamera_str_config },
152beacf11bSopenharmony_ci  { CAMERA_STR_IDX_INTERFACE, g_fcamera_str_interface },
153beacf11bSopenharmony_ci  { CAMERA_IN_TERMINAL,       g_fcamera_input_terminal },
154beacf11bSopenharmony_ci  { CAMERA_OUT_TERMINAL,      g_fcamera_output_terminal },
155beacf11bSopenharmony_ci  USBD_DEVICE_STRINGS_END
156beacf11bSopenharmony_ci};
157beacf11bSopenharmony_ci
158beacf11bSopenharmony_civoid camera_mkdevdesc(uint8_t *buf)
159beacf11bSopenharmony_ci{
160beacf11bSopenharmony_ci  errno_t ret = memcpy_s(buf, USB_COMP_EP0_BUFSIZ, &g_fcamera_device_desc, sizeof(g_fcamera_device_desc));
161beacf11bSopenharmony_ci  if (ret != EOK)
162beacf11bSopenharmony_ci    {
163beacf11bSopenharmony_ci      usb_err("memcpy_s fail!, ret:%d\n", ret);
164beacf11bSopenharmony_ci      return;
165beacf11bSopenharmony_ci    }
166beacf11bSopenharmony_ci}
167beacf11bSopenharmony_ci
168beacf11bSopenharmony_ciint camera_mkstrdesc(uint8_t id, uint8_t *buf)
169beacf11bSopenharmony_ci{
170beacf11bSopenharmony_ci  errno_t ret;
171beacf11bSopenharmony_ci  const char *str;
172beacf11bSopenharmony_ci  int i;
173beacf11bSopenharmony_ci
174beacf11bSopenharmony_ci  for (i = 0; g_fcamera_device_strings[i].s != NULL; i++)
175beacf11bSopenharmony_ci    {
176beacf11bSopenharmony_ci      str = g_fcamera_device_strings[i].s;
177beacf11bSopenharmony_ci      if (g_fcamera_device_strings[i].id == id)
178beacf11bSopenharmony_ci        {
179beacf11bSopenharmony_ci          ret = memcpy_s(buf, USB_COMP_EP0_BUFSIZ, str, str[0]);
180beacf11bSopenharmony_ci          if (ret != EOK)
181beacf11bSopenharmony_ci            {
182beacf11bSopenharmony_ci              usb_err("memcpy_s failed, ret = %d\n", ret);
183beacf11bSopenharmony_ci              return -1;
184beacf11bSopenharmony_ci            }
185beacf11bSopenharmony_ci          return str[0];
186beacf11bSopenharmony_ci        }
187beacf11bSopenharmony_ci    }
188beacf11bSopenharmony_ci
189beacf11bSopenharmony_ci  usb_err("Can not find the id = %u of string\n", id);
190beacf11bSopenharmony_ci  return -1;
191beacf11bSopenharmony_ci}
192beacf11bSopenharmony_ci
193beacf11bSopenharmony_civoid camera_get_composite_devdesc(struct composite_devdesc_s *dev)
194beacf11bSopenharmony_ci{
195beacf11bSopenharmony_ci  dev->mkdevdesc = camera_mkdevdesc;
196beacf11bSopenharmony_ci  dev->mkstrdesc = camera_mkstrdesc;
197beacf11bSopenharmony_ci}
198beacf11bSopenharmony_ci
199beacf11bSopenharmony_ci#define USB_UVC_FIRST_INTERFACE_NUM 0
200beacf11bSopenharmony_ci#define USB_UAC_FIRST_INTERFACE_NUM 2
201beacf11bSopenharmony_ciint usbdev_camera_initialize(struct module *mod, int n, void *arg)
202beacf11bSopenharmony_ci{
203beacf11bSopenharmony_ci  struct composite_softc *com_s = (struct composite_softc *)arg;
204beacf11bSopenharmony_ci  struct composite_devdesc_s dev[USB_COMPOSITE_DEV_NUM];
205beacf11bSopenharmony_ci  int ret;
206beacf11bSopenharmony_ci
207beacf11bSopenharmony_ci  (void)mod;
208beacf11bSopenharmony_ci  (void)n;
209beacf11bSopenharmony_ci  if (com_s == NULL)
210beacf11bSopenharmony_ci    {
211beacf11bSopenharmony_ci      return -1;
212beacf11bSopenharmony_ci    }
213beacf11bSopenharmony_ci
214beacf11bSopenharmony_ci  usbdev_uvc_initialize_sub(&dev[0], USB_UVC_FIRST_INTERFACE_NUM, DEV_UVC);
215beacf11bSopenharmony_ci  camera_get_composite_devdesc(&dev[0]);
216beacf11bSopenharmony_ci  usbdev_uac_initialize_sub(&dev[1], USB_UAC_FIRST_INTERFACE_NUM, DEV_UAC);
217beacf11bSopenharmony_ci  camera_get_composite_devdesc(&dev[1]);
218beacf11bSopenharmony_ci
219beacf11bSopenharmony_ci  ret = composite_initialize(com_s, USB_COMPOSITE_DEV_NUM, dev);
220beacf11bSopenharmony_ci  if (ret < 0)
221beacf11bSopenharmony_ci    {
222beacf11bSopenharmony_ci      return -1;
223beacf11bSopenharmony_ci    }
224beacf11bSopenharmony_ci
225beacf11bSopenharmony_ci  PRINTK("  ** camera device initialized successfully! **\n");
226beacf11bSopenharmony_ci  return 0;
227beacf11bSopenharmony_ci}
228beacf11bSopenharmony_ci
229beacf11bSopenharmony_ci#ifdef __cplusplus
230beacf11bSopenharmony_ci#if __cplusplus
231beacf11bSopenharmony_ci}
232beacf11bSopenharmony_ci#endif /* __cplusplus */
233beacf11bSopenharmony_ci#endif /* __cplusplus */