/*
* @file hv_drv_UsbUtilFunction.c (u_f.c)
* @brief USB function utilities for Gadget stack. 
*
* @author HiView SoC Software Team
* @version 1.0.0
* @date 2022-06-15
*/

#include "hv_drv_UsbUtilFunction.h"
#include "hv_drv_UsbCh9.h"


#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)

static inline size_t usb_ep_align(struct usb_ep *ep, size_t len)
{
    int max_packet_size = (size_t)usb_endpoint_maxp(ep->desc) & 0x7ff;

    return round_up(len, max_packet_size);
}


struct usb_request *alloc_ep_req(struct usb_ep *ep, size_t len)
{
    struct usb_request      *req;

    req = usb_ep_alloc_request(ep, GFP_ATOMIC);
    if (req) {
        req->length = usb_endpoint_dir_out(ep->desc) ?
            usb_ep_align(ep, len) : len;
        req->buf = HV_MALLOC(req->length);
        if (!req->buf) {
            usb_ep_free_request(ep, req);
            req = NULL;
        }
        HV_MEMSET(req->buf, 0, req->length);
    }
    return req;
}



