1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
9 #define _OSDEP_SERVICE_C_
11 #include <drv_types.h>
12 #include <rtw_debug.h>
15 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
16 * @return: one of RTW_STATUS_CODE
18 inline int RTW_STATUS_CODE(int error_code)
25 void *_rtw_malloc(u32 sz)
27 return kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
30 void *_rtw_zmalloc(u32 sz)
32 void *pbuf = _rtw_malloc(sz);
40 inline struct sk_buff *_rtw_skb_alloc(u32 sz)
42 return __dev_alloc_skb(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
45 inline struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb)
47 return skb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
50 inline struct sk_buff *_rtw_skb_clone(struct sk_buff *skb)
52 return skb_clone(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
55 inline int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb)
61 void _rtw_init_queue(struct __queue *pqueue)
63 INIT_LIST_HEAD(&(pqueue->queue));
65 spin_lock_init(&(pqueue->lock));
69 * Open a file with the specific @param path, @param flag, @param mode
70 * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
71 * @param path the path of the file to open
72 * @param flag file operation flags, please refer to linux document
73 * @param mode please refer to linux document
74 * @return Linux specific error code
76 static int openFile(struct file **fpp, char *path, int flag, int mode)
80 fp =filp_open(path, flag, mode);
92 * Close the file with the specific @param fp
93 * @param fp the pointer of struct file to close
96 static int closeFile(struct file *fp)
102 static int readFile(struct file *fp, char *buf, int len)
104 int rlen = 0, sum = 0;
106 if (!fp->f_op || !fp->f_op->read)
110 rlen =fp->f_op->read(fp, (char __force __user *)buf+sum, len-sum, &fp->f_pos);
124 * Test if the specifi @param path is a file and readable
125 * @param path the path of the file to test
126 * @return Linux specific error code
128 static int isFileReadable(char *path)
135 fp =filp_open(path, O_RDONLY, 0);
140 oldfs = get_fs(); set_fs(get_ds());
142 if (1!=readFile(fp, &buf, 1))
146 filp_close(fp, NULL);
152 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
153 * @param path the path of the file to open and read
154 * @param buf the starting address of the buffer to store file content
155 * @param sz how many bytes to read at most
156 * @return the byte we've read, or Linux specific error code
158 static int retriveFromFile(char *path, u8 *buf, u32 sz)
165 if (0 == (ret =openFile(&fp, path, O_RDONLY, 0))) {
166 DBG_871X("%s openFile path:%s fp =%p\n", __func__, path , fp);
168 oldfs = get_fs(); set_fs(get_ds());
169 ret =readFile(fp, buf, sz);
173 DBG_871X("%s readFile, ret:%d\n", __func__, ret);
176 DBG_871X("%s openFile path:%s Fail, ret:%d\n", __func__, path, ret);
179 DBG_871X("%s NULL pointer\n", __func__);
186 * Test if the specifi @param path is a file and readable
187 * @param path the path of the file to test
188 * @return true or false
190 int rtw_is_file_readable(char *path)
192 if (isFileReadable(path) == 0)
199 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
200 * @param path the path of the file to open and read
201 * @param buf the starting address of the buffer to store file content
202 * @param sz how many bytes to read at most
203 * @return the byte we've read
205 int rtw_retrive_from_file(char *path, u8 *buf, u32 sz)
207 int ret =retriveFromFile(path, buf, sz);
208 return ret>= 0?ret:0;
211 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
213 struct net_device *pnetdev;
214 struct rtw_netdev_priv_indicator *pnpi;
216 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
220 pnpi = netdev_priv(pnetdev);
221 pnpi->priv =old_priv;
222 pnpi->sizeof_priv =sizeof_priv;
228 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
230 struct net_device *pnetdev;
231 struct rtw_netdev_priv_indicator *pnpi;
233 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
237 pnpi = netdev_priv(pnetdev);
239 pnpi->priv = vzalloc(sizeof_priv);
241 free_netdev(pnetdev);
246 pnpi->sizeof_priv =sizeof_priv;
251 void rtw_free_netdev(struct net_device * netdev)
253 struct rtw_netdev_priv_indicator *pnpi;
258 pnpi = netdev_priv(netdev);
270 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
272 struct net_device *pnetdev;
273 struct net_device *cur_pnetdev;
274 struct rereg_nd_name_data *rereg_priv;
280 cur_pnetdev = padapter->pnetdev;
281 rereg_priv = &padapter->rereg_nd_name_priv;
283 /* free the old_pnetdev */
284 if (rereg_priv->old_pnetdev) {
285 free_netdev(rereg_priv->old_pnetdev);
286 rereg_priv->old_pnetdev = NULL;
289 if (!rtnl_is_locked())
290 unregister_netdev(cur_pnetdev);
292 unregister_netdevice(cur_pnetdev);
294 rereg_priv->old_pnetdev =cur_pnetdev;
296 pnetdev = rtw_init_netdev(padapter);
302 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
304 rtw_init_netdev_name(pnetdev, ifname);
306 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
308 if (!rtnl_is_locked())
309 ret = register_netdev(pnetdev);
311 ret = register_netdevice(pnetdev);
314 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("register_netdev() failed\n"));
326 u64 rtw_modular64(u64 x, u64 y)
331 void rtw_buf_free(u8 **buf, u32 *buf_len)
335 if (!buf || !buf_len)
347 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
349 u32 ori_len = 0, dup_len = 0;
353 if (!buf || !buf_len)
356 if (!src || !src_len)
360 dup = rtw_malloc(src_len);
363 memcpy(dup, src, dup_len);
370 /* replace buf with dup */
376 if (ori && ori_len > 0)
382 * rtw_cbuf_full - test if cbuf is full
383 * @cbuf: pointer of struct rtw_cbuf
385 * Returns: true if cbuf is full
387 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
389 return (cbuf->write == cbuf->read-1)? true : false;
393 * rtw_cbuf_empty - test if cbuf is empty
394 * @cbuf: pointer of struct rtw_cbuf
396 * Returns: true if cbuf is empty
398 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
400 return (cbuf->write == cbuf->read)? true : false;
404 * rtw_cbuf_push - push a pointer into cbuf
405 * @cbuf: pointer of struct rtw_cbuf
406 * @buf: pointer to push in
408 * Lock free operation, be careful of the use scheme
409 * Returns: true push success
411 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
413 if (rtw_cbuf_full(cbuf))
416 DBG_871X("%s on %u\n", __func__, cbuf->write);
417 cbuf->bufs[cbuf->write] = buf;
418 cbuf->write = (cbuf->write+1)%cbuf->size;
424 * rtw_cbuf_pop - pop a pointer from cbuf
425 * @cbuf: pointer of struct rtw_cbuf
427 * Lock free operation, be careful of the use scheme
428 * Returns: pointer popped out
430 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
433 if (rtw_cbuf_empty(cbuf))
436 DBG_871X("%s on %u\n", __func__, cbuf->read);
437 buf = cbuf->bufs[cbuf->read];
438 cbuf->read = (cbuf->read+1)%cbuf->size;
444 * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
445 * @size: size of pointer
447 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
449 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
451 struct rtw_cbuf *cbuf;
453 cbuf = rtw_malloc(sizeof(*cbuf) + sizeof(void *) * size);
456 cbuf->write = cbuf->read = 0;