#include <drv_types.h>
#include <rtw_debug.h>
inline int RTW_STATUS_CODE(int error_code)
{
if (error_code >= 0)
return _SUCCESS;
return _FAIL;
}
void *_rtw_malloc(u32 sz)
{
return kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
}
void *_rtw_zmalloc(u32 sz)
{
void *pbuf = _rtw_malloc(sz);
if (pbuf)
memset(pbuf, 0, sz);
return pbuf;
}
inline struct sk_buff *_rtw_skb_alloc(u32 sz)
{
return __dev_alloc_skb(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
}
inline struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb)
{
return skb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
}
inline int _rtw_netif_rx(struct net_device *ndev, struct sk_buff *skb)
{
skb->dev = ndev;
return netif_rx(skb);
}
struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
{
struct net_device *pnetdev;
struct rtw_netdev_priv_indicator *pnpi;
pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
if (!pnetdev)
goto RETURN;
pnpi = netdev_priv(pnetdev);
pnpi->priv = old_priv;
pnpi->sizeof_priv = sizeof_priv;
RETURN:
return pnetdev;
}
struct net_device *rtw_alloc_etherdev(int sizeof_priv)
{
struct net_device *pnetdev;
struct rtw_netdev_priv_indicator *pnpi;
pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
if (!pnetdev)
goto RETURN;
pnpi = netdev_priv(pnetdev);
pnpi->priv = vzalloc(sizeof_priv);
if (!pnpi->priv) {
free_netdev(pnetdev);
pnetdev = NULL;
goto RETURN;
}
pnpi->sizeof_priv = sizeof_priv;
RETURN:
return pnetdev;
}
void rtw_free_netdev(struct net_device *netdev)
{
struct rtw_netdev_priv_indicator *pnpi;
if (!netdev)
goto RETURN;
pnpi = netdev_priv(netdev);
if (!pnpi->priv)
goto RETURN;
vfree(pnpi->priv);
free_netdev(netdev);
RETURN:
return;
}
void rtw_buf_free(u8 **buf, u32 *buf_len)
{
if (!buf || !buf_len)
return;
if (*buf) {
*buf_len = 0;
kfree(*buf);
*buf = NULL;
}
}
void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
{
u32 ori_len = 0, dup_len = 0;
u8 *ori = NULL;
u8 *dup = NULL;
if (!buf || !buf_len)
return;
if (!src || !src_len)
goto keep_ori;
dup = rtw_malloc(src_len);
if (dup) {
dup_len = src_len;
memcpy(dup, src, dup_len);
}
keep_ori:
ori = *buf;
ori_len = *buf_len;
*buf_len = 0;
*buf = dup;
*buf_len = dup_len;
if (ori && ori_len > 0)
kfree(ori);
}
inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
{
return (cbuf->write == cbuf->read - 1) ? true : false;
}
inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
{
return (cbuf->write == cbuf->read) ? true : false;
}
bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
{
if (rtw_cbuf_full(cbuf))
return _FAIL;
cbuf->bufs[cbuf->write] = buf;
cbuf->write = (cbuf->write + 1) % cbuf->size;
return _SUCCESS;
}
void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
{
void *buf;
if (rtw_cbuf_empty(cbuf))
return NULL;
buf = cbuf->bufs[cbuf->read];
cbuf->read = (cbuf->read + 1) % cbuf->size;
return buf;
}
struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
{
struct rtw_cbuf *cbuf;
cbuf = rtw_malloc(struct_size(cbuf, bufs, size));
if (cbuf) {
cbuf->write = cbuf->read = 0;
cbuf->size = size;
}
return cbuf;
}