/*
* @file hv_drv_UsbGadgetFuncBillboard.c
* @brief Device Billboard function. 
*
* @author HiView SoC Software Team
* @version 1.0.0
* @date 2022-06-15
*/

#include "hv_drv_UsbErrNo.h"

#include "hv_drv_UsbCh9.h"
#include "hv_drv_UsbGadget.h"
#include "hv_drv_UsbGadgetComposite.h"

#include "hv_drv_UsbGadgetBillboard.h"
#include "hv_drv_UsbGadgetFuncBillboard.h"

#include "hv_drv_UsbDefines.h"


struct f_billboard {
    struct usb_function             usb_function;

    struct usb_descriptor_header    **function;
    struct usb_string               *strings;

    /* when configured, we have one config */
    u8                              config;
    u8                              altsetting;
};


static inline struct f_billboard *func_to_billboard(struct usb_function *f)
{
    return container_of(f, struct f_billboard, usb_function);
}


static struct usb_interface_descriptor billboard_intf = {
    .bLength            =    sizeof billboard_intf,
    .bDescriptorType    =    USB_DT_INTERFACE,
    .bNumEndpoints      =    0,
    .bInterfaceClass    =    USB_CLASS_BILLBOARD,
    .bInterfaceSubClass =    0,
    .bInterfaceProtocol =    0,
    /* .iInterface = DYNAMIC */
};

static struct usb_descriptor_header *billboard_descs[] = {
    (struct usb_descriptor_header *) &billboard_intf,
    NULL,
};

static const char device_name[] = "http://help.vesa.org/dp-usb-type-c/";

/*
 * static strings, in UTF-8
 *
 * dfu_generic configuration
 */
static struct usb_string strings_billboard_generic[] = {
    [0].s = device_name,
    {  }            /* end of list */
};

static struct usb_gadget_strings stringtab_billboard_generic = {
    .language    = 0x0409,    /* en-us */
    .strings    = strings_billboard_generic,
};

static struct usb_gadget_strings *billboard_generic_strings[] = {
    &stringtab_billboard_generic,
    NULL,
};

/*
 * usb_function specific
 */
static struct usb_gadget_strings stringtab_billboard = {
    .language    = 0x0409,    /* en-us */
    /*
     * .strings
     *
     * assigned during initialization,
     * depends on number of flash entities
     *
     */
};

static struct usb_gadget_strings *billboard_strings[] = {
    &stringtab_billboard,
    NULL,
};



/*-------------------------------------------------------------------------*/

static int
billboard_handle(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
{
    struct usb_gadget *gadget = f->config->cdev->gadget;
    struct usb_request *req = f->config->cdev->req;
    struct f_billboard *f_billboard = f->config->cdev->req->context;
    u16 len = (ctrl->wLength);
    u16 w_value = (ctrl->wValue);
    int value = 0;
    u8 req_type = ctrl->bRequestType & USB_TYPE_MASK;

    if (req_type == USB_TYPE_STANDARD) {
        //billboard no function descriptor, so del followings.
        /*if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR &&
            (w_value >> 8) == DFU_DT_FUNC) {
            value = min(len, (u16) sizeof(dfu_func));
            USB_MEMCPY(req->buf, &dfu_func, value);
        }*/
    } 

    if (value >= 0) {
        req->length = value;
        req->zero = value < len;
        value = usb_ep_queue(gadget->ep0, req, 0);
        if (value < 0) {
            req->status = 0;
        }
    }

    return value;
}

/*-------------------------------------------------------------------------*/

static int billboard_prepare_strings(struct f_billboard *f_billboard, int n)
{
    int i = 0;

    f_billboard->strings = USB_MALLOC(sizeof(struct usb_string) * (n + 1));
    HV_MEMSET(f_billboard->strings, 0, (sizeof(struct usb_string) * (n + 1)));
    
    if (!f_billboard->strings)
        return -ENOMEM;

    for (i = 0; i < n; ++i) {
        f_billboard->strings[i].s = "";
    }

    f_billboard->strings[i].id = 0;
    f_billboard->strings[i].s = NULL;

    return 0;
}


static int billboard_bind(struct usb_configuration *c, struct usb_function *f)
{
    struct usb_composite_dev *cdev = c->cdev;
    struct f_billboard *f_billboard = func_to_billboard(f);
    int alt_num = 1;
    int rv, id, i;

    id = usb_interface_id(c, f);
    if (id < 0)
        return id;
    billboard_intf.bInterfaceNumber = id;


    //rv = billboard_prepare_function(f_billboard, alt_num);
    //if (rv)
    //    goto error;

    rv = billboard_prepare_strings(f_billboard, alt_num);
    if (rv)
        goto error;
    for (i = 0; i < alt_num; i++) {
        id = usb_string_id(cdev);
        if (id < 0)
            return id;
        f_billboard->strings[i].id = id;
        //((struct usb_interface_descriptor *)f_billboard->function[i])
        //    ->iInterface = id;
    }

    stringtab_billboard.strings = f_billboard->strings;

    cdev->req->context = f_billboard;

error:
    return rv;
}

static void billboard_unbind(struct usb_configuration *c, struct usb_function *f)
{
    struct f_billboard *f_billboard = func_to_billboard(f);
    int alt_num = 0;
    int i;

    if (f_billboard->strings) {
        i = alt_num;
        while (i)
            f_billboard->strings[--i].s = NULL;

        USB_FREE(f_billboard->strings);
    }

    if (f_billboard->function) {
        i = alt_num;
        while (i) {
            USB_FREE(f_billboard->function[--i]);
            f_billboard->function[i] = NULL;
        }
        USB_FREE(f_billboard->function);
    }

    USB_FREE(f_billboard);
}

static int billboard_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
{
    struct f_billboard *f_billboard = func_to_billboard(f);

    debug("%s: intf:%d alt:%d\n", __func__, intf, alt);

    f_billboard->altsetting = alt;

    return 0;
}

static int __billboard_get_alt(struct usb_function *f, unsigned intf)
{
    struct f_billboard *f_billboard = func_to_billboard(f);

    return f_billboard->altsetting;
}

/* TODO: is this really what we need here? */
static void billboard_disable(struct usb_function *f)
{
    struct f_billboard *f_billboard = func_to_billboard(f);
    if (f_billboard->config == 0)
        return;

    f_billboard->config = 0;
}

static int billboard_bind_config(struct usb_configuration *c)
{
    struct f_billboard *f_billboard;
    int status;

    f_billboard = USB_MALLOC(sizeof(*f_billboard) * 1);
    HV_MEMSET(f_billboard, 0, (sizeof(*f_billboard) * 1));
    
    if (!f_billboard)
        return -ENOMEM;
    f_billboard->usb_function.name            = "BillboardFunc";
    f_billboard->usb_function.hs_descriptors  = billboard_descs;
    f_billboard->usb_function.descriptors     = billboard_descs;
    f_billboard->usb_function.bind            = billboard_bind;
    f_billboard->usb_function.unbind          = billboard_unbind;
    f_billboard->usb_function.set_alt         = billboard_set_alt;
    f_billboard->usb_function.get_alt         = __billboard_get_alt;
    f_billboard->usb_function.disable         = billboard_disable;
    f_billboard->usb_function.strings         = billboard_generic_strings;
    f_billboard->usb_function.setup           = billboard_handle;

    status = usb_add_function(c, &f_billboard->usb_function);
    if (status)
        USB_FREE(f_billboard);

    return status;
}

int billboard_add(struct usb_configuration *c)
{
    int id;

    id = usb_string_id(c->cdev);
    if (id < 0)
        return id;
    strings_billboard_generic[0].id = id;
    billboard_intf.iInterface = id;

    return billboard_bind_config(c);
}

