blob: d87b54711c0d52d3eebfd563b798c2733d99c572 [file] [log] [blame]
Larry Fingera2c60d42013-08-21 22:33:58 -05001/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 *
19 ******************************************************************************/
20
21
22#define _OSDEP_SERVICE_C_
23
24#include <osdep_service.h>
navin patidar0a95a7f2014-06-11 22:51:41 +053025#include <osdep_intf.h>
Larry Fingera2c60d42013-08-21 22:33:58 -050026#include <drv_types.h>
27#include <recv_osdep.h>
28#include <linux/vmalloc.h>
29#include <rtw_ioctl_set.h>
30
31/*
32* Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
33* @return: one of RTW_STATUS_CODE
34*/
35inline int RTW_STATUS_CODE(int error_code)
36{
37 if (error_code >= 0)
38 return _SUCCESS;
39 return _FAIL;
40}
41
Larry Fingera2c60d42013-08-21 22:33:58 -050042u8 *_rtw_malloc(u32 sz)
43{
44 u8 *pbuf = NULL;
45
46 pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
47 return pbuf;
48}
49
Larry Fingera2c60d42013-08-21 22:33:58 -050050void *rtw_malloc2d(int h, int w, int size)
51{
52 int j;
53
Tapasweni Pathakb312fb02014-10-21 09:48:01 +053054 void **a = kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL);
Shraddha Barkeb92ae1f2015-09-11 10:40:59 +053055 if (!a) {
Larry Fingera2c60d42013-08-21 22:33:58 -050056 pr_info("%s: alloc memory fail!\n", __func__);
57 return NULL;
58 }
59
60 for (j = 0; j < h; j++)
61 a[j] = ((char *)(a+h)) + j*w*size;
62
63 return a;
64}
65
Larry Fingera2c60d42013-08-21 22:33:58 -050066u32 _rtw_down_sema(struct semaphore *sema)
67{
68 if (down_interruptible(sema))
69 return _FAIL;
70 else
71 return _SUCCESS;
72}
73
Larry Fingera2c60d42013-08-21 22:33:58 -050074void _rtw_init_queue(struct __queue *pqueue)
75{
navin patidaraa3f5cc2014-06-22 13:49:34 +053076 INIT_LIST_HEAD(&(pqueue->queue));
Larry Fingerf214e522013-12-19 22:38:38 -060077 spin_lock_init(&(pqueue->lock));
Larry Fingera2c60d42013-08-21 22:33:58 -050078}
79
Larry Fingera2c60d42013-08-21 22:33:58 -050080struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
81 void *old_priv)
82{
83 struct net_device *pnetdev;
84 struct rtw_netdev_priv_indicator *pnpi;
85
86 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
87 if (!pnetdev)
88 goto RETURN;
89
90 pnpi = netdev_priv(pnetdev);
91 pnpi->priv = old_priv;
92 pnpi->sizeof_priv = sizeof_priv;
93
94RETURN:
95 return pnetdev;
96}
97
Larry Fingera2c60d42013-08-21 22:33:58 -050098void rtw_free_netdev(struct net_device *netdev)
99{
100 struct rtw_netdev_priv_indicator *pnpi;
101
102 if (!netdev)
103 goto RETURN;
104
105 pnpi = netdev_priv(netdev);
106
107 if (!pnpi->priv)
108 goto RETURN;
109
Larry Finger03bd6ae2014-02-06 20:45:43 -0600110 vfree(pnpi->priv);
Larry Fingera2c60d42013-08-21 22:33:58 -0500111 free_netdev(netdev);
112
113RETURN:
114 return;
115}
116
Larry Fingera2c60d42013-08-21 22:33:58 -0500117u64 rtw_modular64(u64 x, u64 y)
118{
119 return do_div(x, y);
120}
121
Larry Fingera2c60d42013-08-21 22:33:58 -0500122void rtw_buf_free(u8 **buf, u32 *buf_len)
123{
124 *buf_len = 0;
125 kfree(*buf);
126 *buf = NULL;
127}
128
129void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
130{
Sudip Mukherjee9393d342015-06-12 16:20:42 +0530131 u32 dup_len = 0;
Larry Fingera2c60d42013-08-21 22:33:58 -0500132 u8 *ori = NULL;
133 u8 *dup = NULL;
134
135 if (!buf || !buf_len)
136 return;
137
138 if (!src || !src_len)
139 goto keep_ori;
140
141 /* duplicate src */
142 dup = rtw_malloc(src_len);
143 if (dup) {
144 dup_len = src_len;
145 memcpy(dup, src, dup_len);
146 }
147
148keep_ori:
149 ori = *buf;
Larry Fingera2c60d42013-08-21 22:33:58 -0500150
151 /* replace buf with dup */
152 *buf_len = 0;
153 *buf = dup;
154 *buf_len = dup_len;
155
156 /* free ori */
157 kfree(ori);
158}