1 /******************************************************************************
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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.
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
14 ******************************************************************************/
17 #define _OSDEP_SERVICE_C_
19 #include <drv_types.h>
20 #include <rtw_debug.h>
23 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
24 * @return: one of RTW_STATUS_CODE
26 inline int RTW_STATUS_CODE(int error_code)
33 u8 *_rtw_malloc(u32 sz)
37 pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
42 u8 *_rtw_zmalloc(u32 sz)
44 u8 *pbuf = _rtw_malloc(sz);
53 inline struct sk_buff *_rtw_skb_alloc(u32 sz)
55 return __dev_alloc_skb(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
58 inline struct sk_buff *_rtw_skb_copy(const struct sk_buff *skb)
60 return skb_copy(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
63 inline struct sk_buff *_rtw_skb_clone(struct sk_buff *skb)
65 return skb_clone(skb, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
68 inline int _rtw_netif_rx(_nic_hdl ndev, struct sk_buff *skb)
74 void rtw_init_timer(_timer *ptimer, void *padapter, void *pfunc)
76 struct adapter *adapter = (struct adapter *)padapter;
78 _init_timer(ptimer, adapter->pnetdev, pfunc, adapter);
81 void _rtw_init_queue(struct __queue *pqueue)
83 INIT_LIST_HEAD(&(pqueue->queue));
85 spin_lock_init(&(pqueue->lock));
89 * Open a file with the specific @param path, @param flag, @param mode
90 * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
91 * @param path the path of the file to open
92 * @param flag file operation flags, please refer to linux document
93 * @param mode please refer to linux document
94 * @return Linux specific error code
96 static int openFile(struct file **fpp, char *path, int flag, int mode)
100 fp =filp_open(path, flag, mode);
112 * Close the file with the specific @param fp
113 * @param fp the pointer of struct file to close
116 static int closeFile(struct file *fp)
118 filp_close(fp, NULL);
122 static int readFile(struct file *fp, char *buf, int len)
124 int rlen = 0, sum = 0;
126 if (!fp->f_op || !fp->f_op->read)
130 rlen =fp->f_op->read(fp, (char __force __user *)buf+sum, len-sum, &fp->f_pos);
144 * Test if the specifi @param path is a file and readable
145 * @param path the path of the file to test
146 * @return Linux specific error code
148 static int isFileReadable(char *path)
155 fp =filp_open(path, O_RDONLY, 0);
160 oldfs = get_fs(); set_fs(get_ds());
162 if (1!=readFile(fp, &buf, 1))
166 filp_close(fp, NULL);
172 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
173 * @param path the path of the file to open and read
174 * @param buf the starting address of the buffer to store file content
175 * @param sz how many bytes to read at most
176 * @return the byte we've read, or Linux specific error code
178 static int retriveFromFile(char *path, u8 *buf, u32 sz)
185 if (0 == (ret =openFile(&fp, path, O_RDONLY, 0))) {
186 DBG_871X("%s openFile path:%s fp =%p\n", __func__, path , fp);
188 oldfs = get_fs(); set_fs(get_ds());
189 ret =readFile(fp, buf, sz);
193 DBG_871X("%s readFile, ret:%d\n", __func__, ret);
196 DBG_871X("%s openFile path:%s Fail, ret:%d\n", __func__, path, ret);
199 DBG_871X("%s NULL pointer\n", __func__);
206 * Test if the specifi @param path is a file and readable
207 * @param path the path of the file to test
208 * @return true or false
210 int rtw_is_file_readable(char *path)
212 if (isFileReadable(path) == 0)
219 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
220 * @param path the path of the file to open and read
221 * @param buf the starting address of the buffer to store file content
222 * @param sz how many bytes to read at most
223 * @return the byte we've read
225 int rtw_retrive_from_file(char *path, u8 *buf, u32 sz)
227 int ret =retriveFromFile(path, buf, sz);
228 return ret>= 0?ret:0;
231 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv, void *old_priv)
233 struct net_device *pnetdev;
234 struct rtw_netdev_priv_indicator *pnpi;
236 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
240 pnpi = netdev_priv(pnetdev);
241 pnpi->priv =old_priv;
242 pnpi->sizeof_priv =sizeof_priv;
248 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
250 struct net_device *pnetdev;
251 struct rtw_netdev_priv_indicator *pnpi;
253 pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
257 pnpi = netdev_priv(pnetdev);
259 pnpi->priv = vzalloc(sizeof_priv);
261 free_netdev(pnetdev);
266 pnpi->sizeof_priv =sizeof_priv;
271 void rtw_free_netdev(struct net_device * netdev)
273 struct rtw_netdev_priv_indicator *pnpi;
278 pnpi = netdev_priv(netdev);
290 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
292 struct net_device *pnetdev;
293 struct net_device *cur_pnetdev;
294 struct rereg_nd_name_data *rereg_priv;
300 cur_pnetdev = padapter->pnetdev;
301 rereg_priv = &padapter->rereg_nd_name_priv;
303 /* free the old_pnetdev */
304 if (rereg_priv->old_pnetdev) {
305 free_netdev(rereg_priv->old_pnetdev);
306 rereg_priv->old_pnetdev = NULL;
309 if (!rtnl_is_locked())
310 unregister_netdev(cur_pnetdev);
312 unregister_netdevice(cur_pnetdev);
314 rereg_priv->old_pnetdev =cur_pnetdev;
316 pnetdev = rtw_init_netdev(padapter);
322 SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
324 rtw_init_netdev_name(pnetdev, ifname);
326 memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
328 if (!rtnl_is_locked())
329 ret = register_netdev(pnetdev);
331 ret = register_netdevice(pnetdev);
334 RT_TRACE(_module_hci_intfs_c_, _drv_err_, ("register_netdev() failed\n"));
346 u64 rtw_modular64(u64 x, u64 y)
351 void rtw_buf_free(u8 **buf, u32 *buf_len)
355 if (!buf || !buf_len)
367 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
369 u32 ori_len = 0, dup_len = 0;
373 if (!buf || !buf_len)
376 if (!src || !src_len)
380 dup = rtw_malloc(src_len);
383 memcpy(dup, src, dup_len);
390 /* replace buf with dup */
396 if (ori && ori_len > 0)
402 * rtw_cbuf_full - test if cbuf is full
403 * @cbuf: pointer of struct rtw_cbuf
405 * Returns: true if cbuf is full
407 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
409 return (cbuf->write == cbuf->read-1)? true : false;
413 * rtw_cbuf_empty - test if cbuf is empty
414 * @cbuf: pointer of struct rtw_cbuf
416 * Returns: true if cbuf is empty
418 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
420 return (cbuf->write == cbuf->read)? true : false;
424 * rtw_cbuf_push - push a pointer into cbuf
425 * @cbuf: pointer of struct rtw_cbuf
426 * @buf: pointer to push in
428 * Lock free operation, be careful of the use scheme
429 * Returns: true push success
431 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
433 if (rtw_cbuf_full(cbuf))
436 DBG_871X("%s on %u\n", __func__, cbuf->write);
437 cbuf->bufs[cbuf->write] = buf;
438 cbuf->write = (cbuf->write+1)%cbuf->size;
444 * rtw_cbuf_pop - pop a pointer from cbuf
445 * @cbuf: pointer of struct rtw_cbuf
447 * Lock free operation, be careful of the use scheme
448 * Returns: pointer popped out
450 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
453 if (rtw_cbuf_empty(cbuf))
456 DBG_871X("%s on %u\n", __func__, cbuf->read);
457 buf = cbuf->bufs[cbuf->read];
458 cbuf->read = (cbuf->read+1)%cbuf->size;
464 * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
465 * @size: size of pointer
467 * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
469 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
471 struct rtw_cbuf *cbuf;
473 cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) + sizeof(void*)*size);
476 cbuf->write = cbuf->read = 0;