Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 patidar | 0a95a7f | 2014-06-11 22:51:41 +0530 | [diff] [blame] | 25 | #include <osdep_intf.h> |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 26 | #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 | */ |
| 35 | inline int RTW_STATUS_CODE(int error_code) |
| 36 | { |
| 37 | if (error_code >= 0) |
| 38 | return _SUCCESS; |
| 39 | return _FAIL; |
| 40 | } |
| 41 | |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 42 | u8 *_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 Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 50 | void *rtw_malloc2d(int h, int w, int size) |
| 51 | { |
| 52 | int j; |
| 53 | |
Tapasweni Pathak | b312fb0 | 2014-10-21 09:48:01 +0530 | [diff] [blame] | 54 | void **a = kzalloc(h*sizeof(void *) + h*w*size, GFP_KERNEL); |
Shraddha Barke | b92ae1f | 2015-09-11 10:40:59 +0530 | [diff] [blame] | 55 | if (!a) { |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 56 | 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 Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 66 | u32 _rtw_down_sema(struct semaphore *sema) |
| 67 | { |
| 68 | if (down_interruptible(sema)) |
| 69 | return _FAIL; |
| 70 | else |
| 71 | return _SUCCESS; |
| 72 | } |
| 73 | |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 74 | void _rtw_init_queue(struct __queue *pqueue) |
| 75 | { |
navin patidar | aa3f5cc | 2014-06-22 13:49:34 +0530 | [diff] [blame] | 76 | INIT_LIST_HEAD(&(pqueue->queue)); |
Larry Finger | f214e52 | 2013-12-19 22:38:38 -0600 | [diff] [blame] | 77 | spin_lock_init(&(pqueue->lock)); |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 80 | struct 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 | |
| 94 | RETURN: |
| 95 | return pnetdev; |
| 96 | } |
| 97 | |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 98 | void 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 Finger | 03bd6ae | 2014-02-06 20:45:43 -0600 | [diff] [blame] | 110 | vfree(pnpi->priv); |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 111 | free_netdev(netdev); |
| 112 | |
| 113 | RETURN: |
| 114 | return; |
| 115 | } |
| 116 | |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 117 | u64 rtw_modular64(u64 x, u64 y) |
| 118 | { |
| 119 | return do_div(x, y); |
| 120 | } |
| 121 | |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 122 | void rtw_buf_free(u8 **buf, u32 *buf_len) |
| 123 | { |
| 124 | *buf_len = 0; |
| 125 | kfree(*buf); |
| 126 | *buf = NULL; |
| 127 | } |
| 128 | |
| 129 | void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len) |
| 130 | { |
Sudip Mukherjee | 9393d34 | 2015-06-12 16:20:42 +0530 | [diff] [blame] | 131 | u32 dup_len = 0; |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 132 | 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 | |
| 148 | keep_ori: |
| 149 | ori = *buf; |
Larry Finger | a2c60d4 | 2013-08-21 22:33:58 -0500 | [diff] [blame] | 150 | |
| 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 | } |