• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "usb_init.h"
33 #include "usb_api_pri.h"
34 #include "devsvc_manager_clnt.h"
35 #include "hdf_device_desc.h"
36 
37 typedef struct usb_info {
38 	bool b_init;
39 	controller_type ctype;
40 	device_type dtype;
41 } usb_info_t;
42 
43 static char *dev_name = NULL;
44 static bool uvc_enable = false;
45 static usb_info_t usb_info = { false, (controller_type)0xFFFF, (device_type)0xFFFF };
46 static struct mtx usb_mtx = PTHREAD_MUTEX_INITIALIZER;
47 
48 static struct driver_module_data* usb_driver_mode_list[] = {
49 #ifdef LOSCFG_DRIVERS_USB_HOST_EHCI
50 	/* xxx controller modules */
51 	/* usb generial controller modules */
52 	&usbus_ehci_driver_mod,
53 #endif
54 
55 #if defined (LOSCFG_DRIVERS_USB_HOST_XHCI)
56 	&usbus_xhci_driver_mod,
57 #endif
58 
59 #ifdef LOSCFG_DRIVERS_USB_HOST_DRIVER
60 	/* xxx driver modules */
61 	&uhub_uhub_driver_mod,
62 	&uhub_usbus_driver_mod,
63 #endif
64 
65 #ifdef LOSCFG_DRIVERS_USB_4G_MODEM
66 #ifndef LOSCFG_DRIVERS_HDF_USB_DDK_HOST
67 	&cdce_uhub_driver_mod,
68 #endif
69 	//&bsd_u3g_uhub_driver_mod,
70 #endif
71 
72 #if defined (LOSCFG_DRIVERS_USB_SERIAL) || defined (LOSCFG_DRIVERS_USB_4G_MODEM)
73 	&u3g_uhub_driver_mod,
74 #endif
75 
76 #ifdef LOSCFG_DRIVERS_USB_MASS_STORAGE
77 	&umass_uhub_driver_mod,
78 #endif
79 
80 #ifdef LOSCFG_DRIVERS_USB_ETHERNET
81 	&axe_uhub_driver_mod,
82 	&axge_uhub_driver_mod,
83 #endif
84 
85 #ifdef LOSCFG_DRIVERS_USB_RNDIS_HOST
86 	&urndis_uhub_driver_mod,
87 #endif
88 
89 #ifdef LOSCFG_DRIVERS_USB_WIRELESS
90 	&usb_linux_uhub_driver_mod,
91 #endif
92 
93 #if defined (LOSCFG_DRIVERS_USB_HID_CLASS) && defined (LOSCFG_DRIVERS_HDF_INPUT)
94 	&uhid_uhub_driver_mod,
95 #endif
96 
97 #ifdef LOSCFG_DRIVERS_HDF_USB_DDK_DEVICE
98 	&composite_hiudc3_driver_mod,
99 #endif
100 	NULL
101 };
102 
103 extern device_t
104 bus_get_device(device_t dev, const char *name);
105 
106 void
usbinfo_clean(void)107 usbinfo_clean(void)
108 {
109 	dev_name = NULL;
110 	uvc_enable = false;
111 	usb_info.ctype = (controller_type)0xFFFF;
112 	usb_info.dtype = (device_type)0xFFFF;
113 	usb_info.b_init = false;
114 	return;
115 }
116 
117 char *
dev_name_get(void)118 dev_name_get(void)
119 {
120 	return (dev_name);
121 }
122 
123 static uint32_t
usb_loadonce(void)124 usb_loadonce(void)
125 {
126 	struct driver_module_data *data;
127 	uint32_t i;
128 
129 	dprintf("usb %s\n",fetach_usbversion());
130 
131 #ifdef LOSCFG_DRIVERS_USB
132 	/* init quirk */
133 	usb_quirk_init(NULL);
134 
135 	for (i = 0; (data = usb_driver_mode_list[i]) && (data != NULL); i++) {
136 		driver_module_handler(NULL, MOD_LOAD, data);
137 	}
138 #endif
139 
140 #ifdef LOSCFG_USB_DEBUG
141 	usb_debug_module_regsiter();
142 #endif
143 
144 	return (0);
145 }
146 
147 static void
usb_unloadonce(void)148 usb_unloadonce(void)
149 {
150 	uint32_t i;
151 
152 #ifdef LOSCFG_USB_DEBUG
153 	usb_debug_module_unregsiter();
154 #endif
155 
156 #ifdef LOSCFG_DRIVERS_USB
157 	for (i = 0; usb_driver_mode_list[i] != NULL; i++) {
158 		driver_module_handler(NULL, MOD_UNLOAD, usb_driver_mode_list[i]);
159 	}
160 #endif
161 
162 	return;
163 }
164 #ifdef LOSCFG_DRIVERS_HDF_USB_DDK_DEVICE
composite_add(void)165 static int composite_add(void)
166 {
167     device_t udc;
168     device_t composite;
169 
170 	udc = bus_get_device(nexus, "hiudc3");
171 	if (udc == NULL) {
172 		return -1;
173 	}
174 
175     composite = device_add_child(udc, "composite", -1);
176     if (composite == NULL) {
177     	return -1;
178     }
179 
180     if (device_probe_and_attach(composite)) {
181         device_printf(composite, "WARNING: Probe and attach failed!\n");
182         return -1;
183     }
184     struct HdfDeviceObject *devObj = HdfRegisterDevice("usbfn_master", "usbfn_master", NULL);
185     if (devObj == NULL) {
186 		dprintf("%s register usbfn_master may failed\n", __func__);
187     }
188 
189 	devObj = HdfRegisterDevice("usbfn_cdcacm", "usbfn_cdcacm", NULL);
190     if (devObj == NULL) {
191 		dprintf("%s register usbfn_cdcacm may failed\n", __func__);
192     }
193 
194 	devObj = HdfRegisterDevice("usbfn_cdcecm", "usbfn_cdcecm", NULL);
195     if (devObj == NULL) {
196 		dprintf("%s register usbfn_cdcecm may failed\n", __func__);
197     }
198 	dprintf("%s success\n", __func__);
199     return 0;
200 }
201 #endif
202 /*
203  * step1: modify DRIVER_MODULE,register all driver module
204  * step2: make ehci/ohci device (direct skip pci bus)
205  * step3: insert ehci/ohci device into usb controller
206  * step4: creat ehci/ohci root hub device
207  * step5: ehci/ohci transfer setup/start
208  */
209 uint32_t
usb_init(controller_type ctype,device_type dtype)210 usb_init(controller_type ctype, device_type dtype)
211 {
212 	uint32_t ret;
213 	static int usb_loaded = 0;
214 
215 	dprintf("\n******** %s in **********\n", __FUNCTION__);
216 
217 	mtx_lock(&usb_mtx);
218 	if (usb_info.b_init) {
219 		dprintf("\n duplicate usb_init %s, ctype:%d dtype:%d\n", __FUNCTION__, usb_info.ctype, usb_info.dtype);
220 		mtx_unlock(&usb_mtx);
221 		return (LOS_NOK);
222 	}
223 
224 	if (usb_loaded == 0) {
225 		ret = usb_loadonce();
226 		if (ret) {
227 			goto err;
228 		}
229 		usb_loaded = 1;
230 	}
231 
232 	usb_dev_init(NULL);
233 
234 	if (ctype == HOST) {
235 #if defined (LOSCFG_DRIVERS_USB_HOST_XHCI)
236 		ret = hixhci_init();
237 #endif
238 #ifdef LOSCFG_DRIVERS_USB_HOST_EHCI
239 		ret = hiehci_init();
240 #endif
241 	} else {
242 #ifdef LOSCFG_DRIVERS_HDF_USB_DDK_DEVICE
243 		ret = usbd_load_driver();
244 		if (ret != LOS_OK) {
245 			dprintf("usbd_load_driver failed ,ret = %d\n", ret);
246 			goto err;
247 		}
248 		ret = composite_add();
249 		if (ret != LOS_OK) {
250 			dprintf("composite_add failed ,ret = %d\n", ret);
251 			goto err;
252 		}
253 		ret = usbd_enable_interrupt();
254 		if (ret != LOS_OK) {
255 			dprintf("usbd_enable_interrupt failed, ret = %d\n", ret);
256 			goto err;
257 		}
258 #endif
259 	}
260 
261 	usb_info.b_init = true;
262 	usb_info.ctype = ctype;
263 	usb_info.dtype = dtype;
264 
265 	mtx_unlock(&usb_mtx);
266 	dprintf("******** %s ok**********\n\n", __FUNCTION__);
267 	return (LOS_OK);
268 
269 err:
270 	mtx_unlock(&usb_mtx);
271 	if (!usb_loaded) {
272 		usb_unloadonce();
273 	}
274 	dprintf("******** %s fail**********\n\n", __FUNCTION__);
275 
276 	return (LOS_NOK);
277 }
278 
279 uint32_t
usb_deinit(void)280 usb_deinit(void)
281 {
282 	uint32_t ret = LOS_OK;
283 
284 	dprintf("******** %s in **********\n\n", __FUNCTION__);
285 	mtx_lock(&usb_mtx);
286 
287 	if (usb_info.b_init == false) {
288 		dprintf("******** %s out, no init **********\n\n", __FUNCTION__);
289 		goto err;
290 	}
291 
292 	if (usb_info.ctype == HOST) {
293 		dprintf("******** %s fail, host not support **********\n\n", __FUNCTION__);
294 		goto err;
295 	}
296 
297 	if (ret) {
298 		dprintf("******** %s fail, %d **********\n\n", __FUNCTION__, ret);
299 		goto err;
300 	}
301 
302 	usb_dev_uninit(NULL);
303 
304 	dprintf(" ** %s uninit success **\n", dev_name_get());
305 	usbinfo_clean();
306 	mtx_unlock(&usb_mtx);
307 	return (LOS_OK);
308 
309 err:
310 	mtx_unlock(&usb_mtx);
311 	return (LOS_NOK);
312 }
313 
314 bool
usb_is_devicemode(void)315 usb_is_devicemode(void)
316 {
317 	return (HiUsbIsDeviceMode());
318 }
319 
320