Welcome to mirror list, hosted at ThFree Co, Russian Federation.

usb_manager.c « manager « sunxi_usb « usb « drivers - github.com/ClusterM/sun-nontendocm-kernel.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eb511463ae4b7c425c7a62b90a097267aac82849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/*
 * drivers/usb/sunxi_usb/manager/usb_manager.c
 * (C) Copyright 2010-2015
 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
 * javen, 2011-4-14, create this file
 *
 * usb manager.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ioport.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/kthread.h>

#include <linux/debugfs.h>
#include <linux/seq_file.h>

#include <asm/byteorder.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/system.h>
#include <asm/unaligned.h>
#include <mach/irqs.h>
#include <mach/platform.h>
#include <linux/gpio.h>

#include  "../include/sunxi_usb_config.h"
#include  "usb_manager.h"
#include  "usbc_platform.h"
#include  "usb_hw_scan.h"
#include  "usb_msg_center.h"

static struct usb_cfg g_usb_cfg;

#ifdef CONFIG_USB_SUNXI_USB0_OTG
static __u32 thread_run_flag = 1;
static __u32 thread_stopped_flag = 1;
atomic_t thread_suspend_flag;
#endif

#ifndef  SUNXI_USB_FPGA
#define BUFLEN 32

static __s32 ctrlio_status = -1;

static int get_battery_status(void)
{
	struct file *filep;
	loff_t pos;
	char buf[BUFLEN];

	filep=filp_open("/sys/class/power_supply/battery/present", O_RDONLY, 0);
	if (IS_ERR(filep)) {
		DMSG_PANIC("open present fail\n");
		return 0;
	}

	pos = 0;
	vfs_read(filep, (char __user *)buf, BUFLEN, &pos);
	filp_close(filep, NULL);

	if (!strncmp((const char *)buf, "0", 1))
		return 0;
	else
		return 1;
}

static int get_charge_status(void)
{
	struct file *filep;
	loff_t pos;
	char buf[BUFLEN];

	filep=filp_open("/sys/class/power_supply/battery/status", O_RDONLY, 0);
	if (IS_ERR(filep)) {
		DMSG_PANIC("open status fail\n");
		return 0;
	}

	pos = 0;
	vfs_read(filep, (char __user *)buf, BUFLEN, &pos);
	filp_close(filep, NULL);

	if (!strncmp((const char *)buf, "Charging", 8) ||
			!strncmp((const char *)buf, "Full", 4))
		return 1;
	else
		return 0;
}

static int get_voltage(void)
{
	struct file *filep;
	loff_t pos;
	char buf[BUFLEN];
	int ret, voltage;

	filep=filp_open("/sys/class/power_supply/battery/voltage_now", O_RDONLY, 0);
	if (IS_ERR(filep)) {
		DMSG_PANIC("open voltage fail\n");
		return 0;
	}

	pos = 0;
	vfs_read(filep, (char __user *)buf, BUFLEN, &pos);
	filp_close(filep, NULL);

	ret = sscanf(buf, "%d\n", &voltage);
	if (ret != 1)
		return 0;
	else
		return voltage;
}

static int get_capacity(void)
{
	struct file *filep;
	loff_t pos;
	char buf[BUFLEN];
	int ret, capacity;

	filep=filp_open("/sys/class/power_supply/battery/capacity", O_RDONLY, 0);
	if (IS_ERR(filep)) {
		DMSG_PANIC("open capacity fail\n");
		return 0;
	}

	pos = 0;
	vfs_read(filep, (char __user *)buf, BUFLEN, &pos);
	filp_close(filep, NULL);
	ret = sscanf(buf, "%d\n", &capacity);
	if (ret != 1)
		return 0;
	else
		return capacity;
}

static int set_ctrl_gpio(struct usb_cfg *cfg, int is_on)
{
	int ret = 0;

	if (ctrlio_status == is_on)
		return 0;
	if (cfg->port[0].restrict_gpio_set.valid) {
		DMSG_INFO("set ctrl gpio %s\n", is_on ? "on" : "off");
		__gpio_set_value(cfg->port[0].restrict_gpio_set.gpio_set.gpio.gpio, is_on);
	}

	ctrlio_status = is_on;

	return ret;
}

static int pin_init(struct usb_cfg *cfg)
{
	int ret = 0;

	if (cfg->port[0].restrict_gpio_set.valid) {
		ret = gpio_request(cfg->port[0].restrict_gpio_set.gpio_set.gpio.gpio, "usb_restrict");
		if (ret != 0) {
			DMSG_PANIC("ERR: usb_restrict gpio_request failed\n");
			cfg->port[0].restrict_gpio_set.valid = 1;
		} else {
			/* set config, ouput */
			//sunxi_gpio_setcfg(cfg->port[0].restrict_gpio_set.gpio_set.gpio.gpio, 1);

			/* reserved is pull down */
			//sunxi_gpio_setpull(cfg->port[0].restrict_gpio_set.gpio_set.gpio.gpio, 2);
			gpio_direction_output(cfg->port[0].restrict_gpio_set.gpio_set.gpio.gpio, 0);
		}
	}
	return 0;
}

static int pin_exit(struct usb_cfg *cfg)
{
	if (cfg->port[0].restrict_gpio_set.valid) {
		gpio_free(cfg->port[0].restrict_gpio_set.gpio_set.gpio.gpio);
	}
	return 0;
}
#else
static int pin_init(struct usb_cfg *cfg)
{
	return 0;
}

static int pin_exit(struct usb_cfg *cfg)
{
	return 0;
}
#endif

#ifdef CONFIG_USB_SUNXI_USB0_OTG

static int usb_hardware_scan_thread(void * pArg)
{
	struct usb_cfg *cfg = pArg;
#ifndef  SUNXI_USB_FPGA
	__u32 voltage;
	__u32 capacity;
#endif
	/* delay for udc & hcd ready */
	msleep(3000);

	pin_init(cfg);

	while(thread_run_flag) {
		DMSG_DBG_MANAGER("\n\n");

		msleep(1000);  /* 1s */

		if (atomic_read(&thread_suspend_flag))
			continue;
		usb_hw_scan(cfg);
		usb_msg_center(cfg);

		DMSG_DBG_MANAGER("\n\n");


#ifndef  SUNXI_USB_FPGA
		if (cfg->port[0].usb_restrict_flag) {
			if (cfg->port[0].restrict_gpio_set.valid) {
				if (!get_battery_status()) { // battery not exist
					set_ctrl_gpio(cfg, 1);
					continue;
				}

				if (get_charge_status()) { // charging or full
					set_ctrl_gpio(cfg, 1);
					continue;
				}

				voltage = get_voltage();
				capacity = get_capacity();
				if ((voltage > cfg->port[0].voltage) && (capacity > cfg->port[0].capacity)) {
					set_ctrl_gpio(cfg, 1);
				} else {
					set_ctrl_gpio(cfg, 0);
				}
			}
		}
#endif
	}

	thread_stopped_flag = 1;
	return 0;
}

#endif

#ifndef  SUNXI_USB_FPGA

static __s32 usb_script_parse(struct usb_cfg *cfg)
{
	u32 i = 0;
	char *set_usbc = NULL;
	script_item_value_type_e type = 0;
	script_item_u item_temp;

	for(i = 0; i < cfg->usbc_num; i++) {
		if (i == 0) {
			set_usbc = SET_USB0;
		} else if (i == 1) {
			set_usbc = SET_USB1;
		} else {
			set_usbc = SET_USB2;
		}

		/* usbc enable */
		type = script_get_item(set_usbc, KEY_USB_ENABLE, &item_temp);
		if (type == SCIRPT_ITEM_VALUE_TYPE_INT) {
			cfg->port[i].enable = item_temp.val;
		} else {
			DMSG_INFO("get usbc(%d) enable failed\n", i);
			cfg->port[i].enable = 0;
		}

		/* usbc port type */
		type = script_get_item(set_usbc, KEY_USB_PORT_TYPE, &item_temp);
		if (type == SCIRPT_ITEM_VALUE_TYPE_INT) {
			cfg->port[i].port_type = item_temp.val;
		} else {
			DMSG_INFO("get usbc(%d) port type failed\n", i);
			cfg->port[i].port_type = 0;
		}

		/* usbc usb_restrict_flag  */
		type = script_get_item(set_usbc, KEY_USB_USB_RESTRICT_FLAG, &item_temp);
		if (type == SCIRPT_ITEM_VALUE_TYPE_INT) {
			cfg->port[i].usb_restrict_flag = item_temp.val;
		} else {
			DMSG_INFO("get usbc(%d) usb_restrict_flag failed\n", i);
			cfg->port[i].usb_restrict_flag = 0;
		}

		/* usbc voltage type */
		type = script_get_item(set_usbc, KEY_USB_USB_RESTRICT_VOLTAGE, &item_temp);
		if (type == SCIRPT_ITEM_VALUE_TYPE_INT) {
			cfg->port[i].voltage = item_temp.val;
		} else {
			DMSG_INFO("get usbc(%d) voltage  failed\n", i);
			cfg->port[i].voltage = 0;
		}

		/* usbc capacity type */
		type = script_get_item(set_usbc, KEY_USB_USB_RESTRICT_CAPACITY, &item_temp);
		if (type == SCIRPT_ITEM_VALUE_TYPE_INT) {
			cfg->port[i].capacity = item_temp.val;
		} else {
			DMSG_INFO("get usbc(%d)voltage failed\n", i);
			cfg->port[i].capacity = 0;
		}

		/* usbc detect type */
		type = script_get_item(set_usbc, KEY_USB_DETECT_TYPE, &item_temp);
		if (type == SCIRPT_ITEM_VALUE_TYPE_INT) {
			cfg->port[i].detect_type = item_temp.val;
		} else {
			DMSG_INFO("get usbc(%d) detect_type  failed\n", i);
			cfg->port[i].detect_type = 0;
		}

		/* usbc id */
		type = script_get_item(set_usbc, KEY_USB_ID_GPIO, &(cfg->port[i].id.gpio_set));
		if (type == SCIRPT_ITEM_VALUE_TYPE_PIO) {
			cfg->port[i].id.valid = 1;
		} else {
			cfg->port[i].id.valid = 0;
			DMSG_INFO("get usbc(%d) id failed\n", i);
		}

		/* usbc det_vbus */
		type = script_get_item(set_usbc, KEY_USB_DETVBUS_GPIO, &(cfg->port[i].det_vbus.gpio_set));
		if (type == SCIRPT_ITEM_VALUE_TYPE_PIO) {
			cfg->port[i].det_vbus.valid = 1;
			cfg->port[i].det_vbus_type = USB_DET_VBUS_TYPE_GIPO;
		} else {
			cfg->port[i].det_vbus.valid = 0;
			cfg->port[i].det_vbus_type = USB_DET_VBUS_TYPE_NULL;
			DMSG_INFO("no usbc(%d) det_vbus gpio and try to axp det_pin\n", i);
		}

		if (cfg->port[i].det_vbus.valid == 0) {
			type = script_get_item(set_usbc, KEY_USB_DETVBUS_GPIO, &item_temp);
			if (type == SCIRPT_ITEM_VALUE_TYPE_STR) {
				if (strncmp(item_temp.str, "axp_ctrl", 8) == 0) {
					cfg->port[i].det_vbus_type = USB_DET_VBUS_TYPE_AXP;
				} else {
					cfg->port[i].det_vbus_type = USB_DET_VBUS_TYPE_NULL;
				}
			} else {
				DMSG_INFO("get usbc(%d) det_vbus axp failed\n", i);
				cfg->port[i].det_vbus_type = USB_DET_VBUS_TYPE_NULL;
			}
		}

		/* usbc drv_vbus */
		type = script_get_item(set_usbc, KEY_USB_DRVVBUS_GPIO, &(cfg->port[i].drv_vbus.gpio_set));
		if (type == SCIRPT_ITEM_VALUE_TYPE_PIO) {
			cfg->port[i].drv_vbus.valid = 1;
		} else {
			cfg->port[i].drv_vbus.valid = 0;
			DMSG_INFO("get usbc(%d) det_vbus failed\n", i);
		}

		/* usbc usb_restrict */
		type = script_get_item(set_usbc, KEY_USB_RESTRICT_GPIO, &(cfg->port[i].restrict_gpio_set.gpio_set));
		if (type == SCIRPT_ITEM_VALUE_TYPE_PIO) {
			cfg->port[i].restrict_gpio_set.valid = 1;
		} else {
			DMSG_INFO("get usbc0(usb_restrict pin) failed\n");
			cfg->port[i].restrict_gpio_set.valid = 0;
		}
	}

	return 0;
}

static __s32 check_usb_board_info(struct usb_cfg *cfg)
{
	/* USB0 */
	if (cfg->port[0].enable) {
		/* check if port type valid */
		if (cfg->port[0].port_type != USB_PORT_TYPE_DEVICE
				&& cfg->port[0].port_type != USB_PORT_TYPE_HOST
				&& cfg->port[0].port_type != USB_PORT_TYPE_OTG) {
			DMSG_PANIC("ERR: usbc0 port_type(%d) is unkown\n", cfg->port[0].port_type);
			goto err;
		}

		/* check if USB detect way valid */
		if (cfg->port[0].detect_type != USB_DETECT_TYPE_DP_DM
				&& cfg->port[0].detect_type != USB_DETECT_TYPE_VBUS_ID) {
			DMSG_PANIC("ERR: usbc0 detect_type(%d) is unkown\n", cfg->port[0].detect_type);
			goto err;
		}

		/* if use VBUS/ID for detect, must check id/vbus pin validity */
		if (cfg->port[0].detect_type == USB_DETECT_TYPE_VBUS_ID) {
			if (cfg->port[0].id.valid == 0) {
				DMSG_PANIC("ERR: id pin is invaild\n");
				goto err;
			}

			if (cfg->port[0].det_vbus_type == USB_DET_VBUS_TYPE_GIPO) {
				if (cfg->port[0].det_vbus.valid == 0) {
					DMSG_PANIC("ERR: det_vbus pin is invaild\n");
					goto err;
				}
			}
		}
	}

	//-------------------------------------
	// USB1
	//-------------------------------------

	//-------------------------------------
	// USB2
	//-------------------------------------

	return 0;
err:
	return -1;
}

static void print_gpio_set(struct gpio_config *gpio)
{
	DMSG_MANAGER_DEBUG("gpio_name            = %s\n", gpio->gpio.gpio);
	DMSG_MANAGER_DEBUG("mul_sel              = %x\n", gpio->gpio.mul_sel);
	DMSG_MANAGER_DEBUG("pull                 = %x\n", gpio->gpio.pull);
	DMSG_MANAGER_DEBUG("drv_level            = %x\n", gpio->gpio.drv_level);
	DMSG_MANAGER_DEBUG("data                 = %x\n", gpio->gpio.data);
}

static void print_usb_cfg(struct usb_cfg *cfg)
{
	u32 i = 0;

	DMSG_MANAGER_DEBUG("\n-----------usb config information--------------\n");

	DMSG_MANAGER_DEBUG("controller number    = %x\n", (u32)USBC_MAX_CTL_NUM);

	DMSG_MANAGER_DEBUG("usb_global_enable    = %x\n", cfg->usb_global_enable);
	DMSG_MANAGER_DEBUG("usbc_num             = %x\n", cfg->usbc_num);

	for(i = 0; i < USBC_MAX_CTL_NUM; i++) {
		DMSG_MANAGER_DEBUG("\n");
		DMSG_MANAGER_DEBUG("port[%d]:\n", i);
		DMSG_MANAGER_DEBUG("enable               = %x\n", cfg->port[i].enable);
		DMSG_MANAGER_DEBUG("port_no              = %x\n", cfg->port[i].port_no);
		DMSG_MANAGER_DEBUG("port_type            = %x\n", cfg->port[i].port_type);
		DMSG_MANAGER_DEBUG("detect_type          = %x\n", cfg->port[i].detect_type);

		DMSG_MANAGER_DEBUG("id.valid             = %x\n", cfg->port[i].id.valid);
		print_gpio_set(&cfg->port[i].id.gpio_set.gpio);

		if (cfg->port[0].det_vbus_type == USB_DET_VBUS_TYPE_GIPO) {
			DMSG_MANAGER_DEBUG("vbus.valid           = %x\n", cfg->port[i].det_vbus.valid);
			print_gpio_set(&cfg->port[i].det_vbus.gpio_set.gpio);
		}

		DMSG_MANAGER_DEBUG("drv_vbus.valid       = %x\n", cfg->port[i].drv_vbus.valid);
		print_gpio_set(&cfg->port[i].drv_vbus.gpio_set.gpio);

		DMSG_MANAGER_DEBUG("\n");
	}

	DMSG_MANAGER_DEBUG("-------------------------------------------------\n");
}
#else
static __s32 usb_script_parse(struct usb_cfg *cfg)
{
	/* usbc enable */
	cfg->port[0].enable = 1;

	/* usbc port type */
	cfg->port[0].port_type = USB_PORT_TYPE_OTG;

	/* usbc detect type */
	cfg->port[0].detect_type = USB_DETECT_TYPE_VBUS_ID;
	return 0;
}

static __s32 check_usb_board_info(struct usb_cfg *cfg)
{
	if (cfg->port[0].enable) {
		/* check if port type valid */
		if (cfg->port[0].port_type != USB_PORT_TYPE_DEVICE
			&& cfg->port[0].port_type != USB_PORT_TYPE_HOST
			&& cfg->port[0].port_type != USB_PORT_TYPE_OTG) {
			DMSG_PANIC("ERR: usbc0 port_type(%d) is unkown\n", cfg->port[0].port_type);
			goto err;
		}
	}

	return 0;
err:
	return -1;
}

static void print_usb_cfg(struct usb_cfg *cfg)
{
	return;
}
#endif

static __s32 get_usb_cfg(struct usb_cfg *cfg)
{
	__s32 ret = 0;

	/* parse script */
	ret = usb_script_parse(cfg);
	if (ret != 0) {
		DMSG_PANIC("ERR: usb_script_parse failed\n");
		return -1;
	}

	print_usb_cfg(cfg);

	ret = check_usb_board_info(cfg);
	if (ret != 0) {
		DMSG_PANIC("ERR: check_usb_board_info failed\n");
		return -1;
	}

	return 0;
}

static int __init usb_manager_init(void)
{
	__s32 ret = 0;
	bsp_usbc_t usbc;

#ifdef CONFIG_USB_SUNXI_USB0_OTG
	struct task_struct *th = NULL;
#endif

	DMSG_MANAGER_DEBUG("[sw usb]: usb_manager_init\n");
#if defined(CONFIG_USB_SUNXI_USB0_DEVICE_ONLY)
	DMSG_INFO_MANAGER("CONFIG_USB_SUNXI_USB0_DEVICE_ONLY\n");
#elif defined(CONFIG_USB_SUNXI_USB0_HOST_ONLY)
	DMSG_INFO_MANAGER("CONFIG_USB_SUNXI_USB0_HOST_ONLY\n");
#elif defined(CONFIG_USB_SUNXI_USB0_OTG)
	DMSG_INFO_MANAGER("CONFIG_USB_SUNXI_USB0_OTG\n");
#else
	DMSG_INFO_MANAGER("CONFIG_USB_SUNXI_USB0_NULL\n");
	return 0;
#endif

	memset(&g_usb_cfg, 0, sizeof(struct usb_cfg));
	g_usb_cfg.usb_global_enable = 1;
	g_usb_cfg.usbc_num = 1;

	ret = get_usb_cfg(&g_usb_cfg);
	if (ret != 0) {
		DMSG_PANIC("ERR: get_usb_cfg failed\n");
		return -1;
	}
	if (g_usb_cfg.port[0].enable == 0) {
		DMSG_PANIC("wrn: usb0 is disable\n");
		return 0;
	}

	memset(&usbc, 0, sizeof(bsp_usbc_t));
#ifndef CONFIG_ARCH_SUN9IW1
	usbc.usbc_info[0].base = (__u32 __force)SUNXI_USB_OTG_VBASE;
	usbc.sram_base = (__u32 __force)SUNXI_SRAMCTRL_VBASE;
	USBC_init(&usbc);
#endif
	usbc0_platform_device_init(&g_usb_cfg.port[0]);

#ifdef CONFIG_USB_SUNXI_USB0_OTG
	if (g_usb_cfg.port[0].port_type == USB_PORT_TYPE_OTG
		&& g_usb_cfg.port[0].detect_type == USB_DETECT_TYPE_VBUS_ID) {
		usb_hw_scan_init(&g_usb_cfg);

		atomic_set(&thread_suspend_flag, 0);
		thread_run_flag = 1;
		thread_stopped_flag = 0;
		th = kthread_create(usb_hardware_scan_thread, &g_usb_cfg, "usb-hardware-scan");
		if (IS_ERR(th)) {
			DMSG_PANIC("ERR: kthread_create failed\n");
			return -1;
		}

		wake_up_process(th);
	}
#endif

	DMSG_MANAGER_DEBUG("[sw usb]: usb_manager_init end\n");
	return 0;
}

static void __exit usb_manager_exit(void)
{
	bsp_usbc_t usbc;

	DMSG_MANAGER_DEBUG("[sw usb]: usb_manager_exit\n");

#if defined(CONFIG_USB_SUNXI_USB0_DEVICE_ONLY)
	DMSG_INFO("CONFIG_USB_SUNXI_USB0_DEVICE_ONLY\n");
#elif defined(CONFIG_USB_SUNXI_USB0_HOST_ONLY)
	DMSG_INFO("CONFIG_USB_SUNXI_USB0_HOST_ONLY\n");
#elif defined(CONFIG_USB_SUNXI_USB0_OTG)
	DMSG_INFO("CONFIG_USB_SUNXI_USB0_OTG\n");
#else
	DMSG_INFO("CONFIG_USB_SUNXI_USB0_NULL\n");
	return;
#endif

	if (g_usb_cfg.port[0].enable == 0) {
		DMSG_PANIC("wrn: usb0 is disable\n");
		return ;
	}

	memset(&usbc, 0, sizeof(bsp_usbc_t));
	USBC_exit(&usbc);
#ifdef CONFIG_USB_SUNXI_USB0_OTG
	if (g_usb_cfg.port[0].port_type == USB_PORT_TYPE_OTG
		&& g_usb_cfg.port[0].detect_type == USB_DETECT_TYPE_VBUS_ID) {
		thread_run_flag = 0;
		while(!thread_stopped_flag) {
			DMSG_INFO("waitting for usb_hardware_scan_thread stop\n");
			msleep(10);
		}
		pin_exit(&g_usb_cfg);
		usb_hw_scan_exit(&g_usb_cfg);
	}
#endif

	usbc0_platform_device_exit(&g_usb_cfg.port[0]);
	return;
}

//module_init(usb_manager_init);
fs_initcall(usb_manager_init);
module_exit(usb_manager_exit);