/**
 * @file hv_drv_UsbCorePlatform.c
 * @brief This is usb platform driver.
 * @details 
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20230417
 * @copyright Copyright(c),2022-9, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                         <th>Date             <th>Details
 * <tr><td>HiView SoC Software Team   <td>20230417       <td>Create and first Version.
 * </table>
 **/
 
#include "hv_drv_UsbMusbDrv.h"
#include "hv_drv_UsbCoreDevice.h"
#include "hv_drv_UsbCh9.h"

struct usb_subsys {
    struct platform_device     platform_dev;
    struct platform_driver    *platform_drv;
};

static struct usb_subsys __subsys;

extern int usb_init(void);
extern int usb_exit(void);
extern int usb_mass_init(void);
extern void usb_mass_exit(void);
extern struct platform_driver musb_driver;


static void init_usb_subsys(struct usb_subsys *sys, struct platform_driver *drv)
{
    usb_init();   
    usb_mass_init(); 

    sys->platform_drv = drv;
}

int Hv_Drv_Usb_PlatformInit(void)
{
    int ret = 0;
    struct usb_subsys *sys = &__subsys;
    if(NULL == sys->platform_drv)
    {
        init_usb_subsys(sys, &musb_driver);
        drv_plat_init(&sys->platform_dev);
        if (sys->platform_drv->probe) {
            ret = sys->platform_drv->probe(&sys->platform_dev); //call musb_probe
        }
    }

    USB_ASSERT(ret == 0);

    return ret;
}

int Hv_Drv_Usb_PlatformExit(void) {
    struct usb_subsys *sys = &__subsys;

    if (sys->platform_drv) 
    {
        if (sys->platform_drv->remove) 
        {
            sys->platform_drv->remove(&sys->platform_dev);
        }
        drv_plat_exit(&sys->platform_dev);
        sys->platform_drv = NULL;
    }
    usb_mass_exit();
    usb_exit();

    return 0;
}

