Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[pandora-kernel.git] / drivers / staging / rtl8188eu / os_dep / osdep_service.c
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>
25 #include <drv_types.h>
26 #include <recv_osdep.h>
27 #include <linux/vmalloc.h>
28 #include <rtw_ioctl_set.h>
29
30 /*
31 * Translate the OS dependent @param error_code to OS independent RTW_STATUS_CODE
32 * @return: one of RTW_STATUS_CODE
33 */
34 inline int RTW_STATUS_CODE(int error_code)
35 {
36         if (error_code >= 0)
37                 return _SUCCESS;
38         return _FAIL;
39 }
40
41 u32 rtw_atoi(u8 *s)
42 {
43         int num = 0, flag = 0;
44         int i;
45         for (i = 0; i <= strlen(s); i++) {
46                 if (s[i] >= '0' && s[i] <= '9')
47                         num = num * 10 + s[i] - '0';
48                 else if (s[0] == '-' && i == 0)
49                         flag = 1;
50                 else
51                         break;
52         }
53         if (flag == 1)
54                 num = num * -1;
55          return num;
56 }
57
58 inline u8 *_rtw_vmalloc(u32 sz)
59 {
60         u8      *pbuf;
61         pbuf = vmalloc(sz);
62         return pbuf;
63 }
64
65 inline u8 *_rtw_zvmalloc(u32 sz)
66 {
67         u8      *pbuf;
68         pbuf = _rtw_vmalloc(sz);
69         if (pbuf != NULL)
70                 memset(pbuf, 0, sz);
71         return pbuf;
72 }
73
74 inline void _rtw_vmfree(u8 *pbuf, u32 sz)
75 {
76         vfree(pbuf);
77 }
78
79 u8 *_rtw_malloc(u32 sz)
80 {
81         u8      *pbuf = NULL;
82
83         pbuf = kmalloc(sz, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
84         return pbuf;
85 }
86
87 u8 *_rtw_zmalloc(u32 sz)
88 {
89         u8      *pbuf = _rtw_malloc(sz);
90
91         if (pbuf != NULL)
92                 memset(pbuf, 0, sz);
93         return pbuf;
94 }
95
96 void *rtw_malloc2d(int h, int w, int size)
97 {
98         int j;
99
100         void **a = (void **)rtw_zmalloc(h*sizeof(void *) + h*w*size);
101         if (a == NULL) {
102                 pr_info("%s: alloc memory fail!\n", __func__);
103                 return NULL;
104         }
105
106         for (j = 0; j < h; j++)
107                 a[j] = ((char *)(a+h)) + j*w*size;
108
109         return a;
110 }
111
112 void rtw_mfree2d(void *pbuf, int h, int w, int size)
113 {
114         kfree(pbuf);
115 }
116
117 int _rtw_memcmp(void *dst, void *src, u32 sz)
118 {
119 /* under Linux/GNU/GLibc, the return value of memcmp for two same
120  * mem. chunk is 0 */
121         if (!(memcmp(dst, src, sz)))
122                 return true;
123         else
124                 return false;
125 }
126
127 void _rtw_memset(void *pbuf, int c, u32 sz)
128 {
129         memset(pbuf, c, sz);
130 }
131
132 void _rtw_init_listhead(struct list_head *list)
133 {
134         INIT_LIST_HEAD(list);
135 }
136
137 /*
138 For the following list_xxx operations,
139 caller must guarantee the atomic context.
140 Otherwise, there will be racing condition.
141 */
142 u32     rtw_is_list_empty(struct list_head *phead)
143 {
144         if (list_empty(phead))
145                 return true;
146         else
147                 return false;
148 }
149
150 void rtw_list_insert_head(struct list_head *plist, struct list_head *phead)
151 {
152         list_add(plist, phead);
153 }
154
155 void rtw_list_insert_tail(struct list_head *plist, struct list_head *phead)
156 {
157         list_add_tail(plist, phead);
158 }
159
160 /*
161 Caller must check if the list is empty before calling rtw_list_delete
162 */
163
164 void _rtw_init_sema(struct semaphore *sema, int init_val)
165 {
166         sema_init(sema, init_val);
167 }
168
169 void _rtw_free_sema(struct semaphore *sema)
170 {
171 }
172
173 void _rtw_up_sema(struct semaphore *sema)
174 {
175         up(sema);
176 }
177
178 u32 _rtw_down_sema(struct semaphore *sema)
179 {
180         if (down_interruptible(sema))
181                 return _FAIL;
182         else
183                 return _SUCCESS;
184 }
185
186 void    _rtw_mutex_init(struct mutex *pmutex)
187 {
188         mutex_init(pmutex);
189 }
190
191 void    _rtw_mutex_free(struct mutex *pmutex)
192 {
193         mutex_destroy(pmutex);
194 }
195
196 void    _rtw_spinlock_init(spinlock_t *plock)
197 {
198         spin_lock_init(plock);
199 }
200
201 void    _rtw_spinlock_free(spinlock_t *plock)
202 {
203 }
204
205 void    _rtw_init_queue(struct __queue *pqueue)
206 {
207         _rtw_init_listhead(&(pqueue->queue));
208         _rtw_spinlock_init(&(pqueue->lock));
209 }
210
211 u32       _rtw_queue_empty(struct __queue *pqueue)
212 {
213         return rtw_is_list_empty(&(pqueue->queue));
214 }
215
216 u32 rtw_end_of_queue_search(struct list_head *head, struct list_head *plist)
217 {
218         if (head == plist)
219                 return true;
220         else
221                 return false;
222 }
223
224 u32     rtw_get_current_time(void)
225 {
226         return jiffies;
227 }
228
229 inline u32 rtw_systime_to_ms(u32 systime)
230 {
231         return systime * 1000 / HZ;
232 }
233
234 inline u32 rtw_ms_to_systime(u32 ms)
235 {
236         return ms * HZ / 1000;
237 }
238
239 /*  the input parameter start use the same unit as returned by
240  *  rtw_get_current_time */
241 inline s32 rtw_get_passing_time_ms(u32 start)
242 {
243         return rtw_systime_to_ms(jiffies-start);
244 }
245
246 inline s32 rtw_get_time_interval_ms(u32 start, u32 end)
247 {
248         return rtw_systime_to_ms(end-start);
249 }
250
251 void rtw_sleep_schedulable(int ms)
252 {
253         u32 delta;
254
255         delta = (ms * HZ)/1000;/* ms) */
256         if (delta == 0)
257                 delta = 1;/*  1 ms */
258         set_current_state(TASK_INTERRUPTIBLE);
259         if (schedule_timeout(delta) != 0)
260                 return;
261 }
262
263 void rtw_msleep_os(int ms)
264 {
265         msleep((unsigned int)ms);
266 }
267
268 void rtw_usleep_os(int us)
269 {
270         if (1 < (us/1000))
271                 msleep(1);
272         else
273                 msleep((us/1000) + 1);
274 }
275
276 void rtw_mdelay_os(int ms)
277 {
278         mdelay((unsigned long)ms);
279 }
280
281 void rtw_udelay_os(int us)
282 {
283         udelay((unsigned long)us);
284 }
285
286 void rtw_yield_os(void)
287 {
288         yield();
289 }
290
291 #define RTW_SUSPEND_LOCK_NAME "rtw_wifi"
292
293 inline void rtw_suspend_lock_init(void)
294 {
295 }
296
297 inline void rtw_suspend_lock_uninit(void)
298 {
299 }
300
301 inline void rtw_lock_suspend(void)
302 {
303 }
304
305 inline void rtw_unlock_suspend(void)
306 {
307 }
308
309 inline void ATOMIC_SET(ATOMIC_T *v, int i)
310 {
311         atomic_set(v, i);
312 }
313
314 inline int ATOMIC_READ(ATOMIC_T *v)
315 {
316         return atomic_read(v);
317 }
318
319 inline void ATOMIC_ADD(ATOMIC_T *v, int i)
320 {
321         atomic_add(i, v);
322 }
323
324 inline void ATOMIC_SUB(ATOMIC_T *v, int i)
325 {
326         atomic_sub(i, v);
327 }
328
329 inline void ATOMIC_INC(ATOMIC_T *v)
330 {
331         atomic_inc(v);
332 }
333
334 inline void ATOMIC_DEC(ATOMIC_T *v)
335 {
336         atomic_dec(v);
337 }
338
339 inline int ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
340 {
341         return atomic_add_return(i, v);
342 }
343
344 inline int ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
345 {
346         return atomic_sub_return(i, v);
347 }
348
349 inline int ATOMIC_INC_RETURN(ATOMIC_T *v)
350 {
351         return atomic_inc_return(v);
352 }
353
354 inline int ATOMIC_DEC_RETURN(ATOMIC_T *v)
355 {
356         return atomic_dec_return(v);
357 }
358
359 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
360                                                     void *old_priv)
361 {
362         struct net_device *pnetdev;
363         struct rtw_netdev_priv_indicator *pnpi;
364
365         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
366         if (!pnetdev)
367                 goto RETURN;
368
369         pnpi = netdev_priv(pnetdev);
370         pnpi->priv = old_priv;
371         pnpi->sizeof_priv = sizeof_priv;
372
373 RETURN:
374         return pnetdev;
375 }
376
377 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
378 {
379         struct net_device *pnetdev;
380         struct rtw_netdev_priv_indicator *pnpi;
381
382         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
383         if (!pnetdev)
384                 goto RETURN;
385
386         pnpi = netdev_priv(pnetdev);
387
388         pnpi->priv = rtw_zvmalloc(sizeof_priv);
389         if (!pnpi->priv) {
390                 free_netdev(pnetdev);
391                 pnetdev = NULL;
392                 goto RETURN;
393         }
394
395         pnpi->sizeof_priv = sizeof_priv;
396 RETURN:
397         return pnetdev;
398 }
399
400 void rtw_free_netdev(struct net_device *netdev)
401 {
402         struct rtw_netdev_priv_indicator *pnpi;
403
404         if (!netdev)
405                 goto RETURN;
406
407         pnpi = netdev_priv(netdev);
408
409         if (!pnpi->priv)
410                 goto RETURN;
411
412         rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
413         free_netdev(netdev);
414
415 RETURN:
416         return;
417 }
418
419 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
420 {
421         struct net_device *pnetdev;
422         struct net_device *cur_pnetdev;
423         struct rereg_nd_name_data *rereg_priv;
424         int ret;
425
426         if (!padapter)
427                 goto error;
428
429         cur_pnetdev = padapter->pnetdev;
430         rereg_priv = &padapter->rereg_nd_name_priv;
431
432         /* free the old_pnetdev */
433         if (rereg_priv->old_pnetdev) {
434                 free_netdev(rereg_priv->old_pnetdev);
435                 rereg_priv->old_pnetdev = NULL;
436         }
437
438         if (!rtnl_is_locked())
439                 unregister_netdev(cur_pnetdev);
440         else
441                 unregister_netdevice(cur_pnetdev);
442
443         rtw_proc_remove_one(cur_pnetdev);
444
445         rereg_priv->old_pnetdev = cur_pnetdev;
446
447         pnetdev = rtw_init_netdev(padapter);
448         if (!pnetdev)  {
449                 ret = -1;
450                 goto error;
451         }
452
453         SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
454
455         rtw_init_netdev_name(pnetdev, ifname);
456
457         memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
458
459         if (!rtnl_is_locked())
460                 ret = register_netdev(pnetdev);
461         else
462                 ret = register_netdevice(pnetdev);
463         if (ret != 0) {
464                 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
465                          ("register_netdev() failed\n"));
466                 goto error;
467         }
468         rtw_proc_init_one(pnetdev);
469         return 0;
470 error:
471         return -1;
472 }
473
474 u64 rtw_modular64(u64 x, u64 y)
475 {
476         return do_div(x, y);
477 }
478
479 u64 rtw_division64(u64 x, u64 y)
480 {
481         do_div(x, y);
482         return x;
483 }
484
485 void rtw_buf_free(u8 **buf, u32 *buf_len)
486 {
487         *buf_len = 0;
488         kfree(*buf);
489         *buf = NULL;
490 }
491
492 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
493 {
494         u32 ori_len = 0, dup_len = 0;
495         u8 *ori = NULL;
496         u8 *dup = NULL;
497
498         if (!buf || !buf_len)
499                 return;
500
501         if (!src || !src_len)
502                 goto keep_ori;
503
504         /* duplicate src */
505         dup = rtw_malloc(src_len);
506         if (dup) {
507                 dup_len = src_len;
508                 memcpy(dup, src, dup_len);
509         }
510
511 keep_ori:
512         ori = *buf;
513         ori_len = *buf_len;
514
515         /* replace buf with dup */
516         *buf_len = 0;
517         *buf = dup;
518         *buf_len = dup_len;
519
520         /* free ori */
521         kfree(ori);
522 }
523
524
525 /**
526  * rtw_cbuf_full - test if cbuf is full
527  * @cbuf: pointer of struct rtw_cbuf
528  *
529  * Returns: true if cbuf is full
530  */
531 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
532 {
533         return (cbuf->write == cbuf->read-1) ? true : false;
534 }
535
536 /**
537  * rtw_cbuf_empty - test if cbuf is empty
538  * @cbuf: pointer of struct rtw_cbuf
539  *
540  * Returns: true if cbuf is empty
541  */
542 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
543 {
544         return (cbuf->write == cbuf->read) ? true : false;
545 }
546
547 /**
548  * rtw_cbuf_push - push a pointer into cbuf
549  * @cbuf: pointer of struct rtw_cbuf
550  * @buf: pointer to push in
551  *
552  * Lock free operation, be careful of the use scheme
553  * Returns: true push success
554  */
555 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
556 {
557         if (rtw_cbuf_full(cbuf))
558                 return _FAIL;
559
560         if (0)
561                 DBG_88E("%s on %u\n", __func__, cbuf->write);
562         cbuf->bufs[cbuf->write] = buf;
563         cbuf->write = (cbuf->write+1)%cbuf->size;
564
565         return _SUCCESS;
566 }
567
568 /**
569  * rtw_cbuf_pop - pop a pointer from cbuf
570  * @cbuf: pointer of struct rtw_cbuf
571  *
572  * Lock free operation, be careful of the use scheme
573  * Returns: pointer popped out
574  */
575 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
576 {
577         void *buf;
578         if (rtw_cbuf_empty(cbuf))
579                 return NULL;
580
581         if (0)
582                 DBG_88E("%s on %u\n", __func__, cbuf->read);
583         buf = cbuf->bufs[cbuf->read];
584         cbuf->read = (cbuf->read+1)%cbuf->size;
585
586         return buf;
587 }
588
589 /**
590  * rtw_cbuf_alloc - allocate a rtw_cbuf with given size and do initialization
591  * @size: size of pointer
592  *
593  * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
594  */
595 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
596 {
597         struct rtw_cbuf *cbuf;
598
599         cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
600                sizeof(void *)*size);
601
602         if (cbuf) {
603                 cbuf->write = 0;
604                 cbuf->read = 0;
605                 cbuf->size = size;
606         }
607         return cbuf;
608 }