]> Pileus Git - ~andy/linux/blob - drivers/staging/rtl8188eu/os_dep/osdep_service.c
Merge tag 'firewire-updates' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee139...
[~andy/linux] / 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 /* Open a file with the specific @param path, @param flag, @param mode
360  * @param fpp the pointer of struct file pointer to get struct file pointer while file opening is success
361  * @param path the path of the file to open
362  * @param flag file operation flags, please refer to linux document
363  * @param mode please refer to linux document
364  * @return Linux specific error code
365  */
366 static int openfile(struct file **fpp, char *path, int flag, int mode)
367 {
368         struct file *fp;
369
370         fp = filp_open(path, flag, mode);
371         if (IS_ERR(fp)) {
372                 *fpp = NULL;
373                 return PTR_ERR(fp);
374         } else {
375                 *fpp = fp;
376                 return 0;
377         }
378 }
379
380 /* Close the file with the specific @param fp
381  * @param fp the pointer of struct file to close
382  * @return always 0
383  */
384 static int closefile(struct file *fp)
385 {
386         filp_close(fp, NULL);
387         return 0;
388 }
389
390 static int readfile(struct file *fp, char __user *buf, int len)
391 {
392         int rlen = 0, sum = 0;
393
394         if (!fp->f_op || !fp->f_op->read)
395                 return -EPERM;
396
397         while (sum < len) {
398                 rlen = fp->f_op->read(fp, buf+sum, len-sum, &fp->f_pos);
399                 if (rlen > 0)
400                         sum += rlen;
401                 else if (0 != rlen)
402                         return rlen;
403                 else
404                         break;
405         }
406         return  sum;
407 }
408
409 static int writefile(struct file *fp, char __user *buf, int len)
410 {
411         int wlen = 0, sum = 0;
412
413         if (!fp->f_op || !fp->f_op->write)
414                 return -EPERM;
415
416         while (sum < len) {
417                 wlen = fp->f_op->write(fp, buf+sum, len-sum, &fp->f_pos);
418                 if (wlen > 0)
419                         sum += wlen;
420                 else if (0 != wlen)
421                         return wlen;
422                 else
423                         break;
424         }
425         return sum;
426 }
427
428 /* Test if the specifi @param path is a file and readable
429  * @param path the path of the file to test
430  * @return Linux specific error code
431  */
432 static int isfilereadable(char *path)
433 {
434         struct file *fp;
435         int ret = 0;
436         mm_segment_t oldfs;
437         char __user buf;
438
439         fp = filp_open(path, O_RDONLY, 0);
440         if (IS_ERR(fp)) {
441                 ret = PTR_ERR(fp);
442         } else {
443                 oldfs = get_fs(); set_fs(get_ds());
444
445                 if (1 != readfile(fp, &buf, 1))
446                         ret = PTR_ERR(fp);
447
448                 set_fs(oldfs);
449                 filp_close(fp, NULL);
450         }
451         return ret;
452 }
453
454 /* Open the file with @param path and retrive the file content into
455  * memory starting from @param buf for @param sz at most
456  * @param path the path of the file to open and read
457  * @param buf the starting address of the buffer to store file content
458  * @param sz how many bytes to read at most
459  * @return the byte we've read, or Linux specific error code
460  */
461 static int retrievefromfile(char *path, u8 __user *buf, u32 sz)
462 {
463         int ret = -1;
464         mm_segment_t oldfs;
465         struct file *fp;
466
467         if (path && buf) {
468                 ret = openfile(&fp, path, O_RDONLY, 0);
469                 if (0 == ret) {
470                         DBG_88E("%s openfile path:%s fp =%p\n", __func__,
471                                 path, fp);
472
473                         oldfs = get_fs(); set_fs(get_ds());
474                         ret = readfile(fp, buf, sz);
475                         set_fs(oldfs);
476                         closefile(fp);
477
478                         DBG_88E("%s readfile, ret:%d\n", __func__, ret);
479
480                 } else {
481                         DBG_88E("%s openfile path:%s Fail, ret:%d\n", __func__,
482                                 path, ret);
483                 }
484         } else {
485                 DBG_88E("%s NULL pointer\n", __func__);
486                 ret =  -EINVAL;
487         }
488         return ret;
489 }
490
491 /*
492 * Open the file with @param path and wirte @param sz byte of data starting from @param buf into the file
493 * @param path the path of the file to open and write
494 * @param buf the starting address of the data to write into file
495 * @param sz how many bytes to write at most
496 * @return the byte we've written, or Linux specific error code
497 */
498 static int storetofile(char *path, u8 __user *buf, u32 sz)
499 {
500         int ret = 0;
501         mm_segment_t oldfs;
502         struct file *fp;
503
504         if (path && buf) {
505                 ret = openfile(&fp, path, O_CREAT|O_WRONLY, 0666);
506                 if (0 == ret) {
507                         DBG_88E("%s openfile path:%s fp =%p\n", __func__, path, fp);
508
509                         oldfs = get_fs(); set_fs(get_ds());
510                         ret = writefile(fp, buf, sz);
511                         set_fs(oldfs);
512                         closefile(fp);
513
514                         DBG_88E("%s writefile, ret:%d\n", __func__, ret);
515
516                 } else {
517                         DBG_88E("%s openfile path:%s Fail, ret:%d\n", __func__, path, ret);
518                 }
519         } else {
520                 DBG_88E("%s NULL pointer\n", __func__);
521                 ret =  -EINVAL;
522         }
523         return ret;
524 }
525
526 /*
527 * Test if the specifi @param path is a file and readable
528 * @param path the path of the file to test
529 * @return true or false
530 */
531 int rtw_is_file_readable(char *path)
532 {
533         if (isfilereadable(path) == 0)
534                 return true;
535         else
536                 return false;
537 }
538
539 /*
540 * Open the file with @param path and retrive the file content into memory starting from @param buf for @param sz at most
541 * @param path the path of the file to open and read
542 * @param buf the starting address of the buffer to store file content
543 * @param sz how many bytes to read at most
544 * @return the byte we've read
545 */
546 int rtw_retrive_from_file(char *path, u8 __user *buf, u32 sz)
547 {
548         int ret = retrievefromfile(path, buf, sz);
549
550         return ret >= 0 ? ret : 0;
551 }
552
553 /*
554  * Open the file with @param path and wirte @param sz byte of data
555  * starting from @param buf into the file
556  * @param path the path of the file to open and write
557  * @param buf the starting address of the data to write into file
558  * @param sz how many bytes to write at most
559  * @return the byte we've written
560  */
561 int rtw_store_to_file(char *path, u8 __user *buf, u32 sz)
562 {
563         int ret = storetofile(path, buf, sz);
564         return ret >= 0 ? ret : 0;
565 }
566
567 struct net_device *rtw_alloc_etherdev_with_old_priv(int sizeof_priv,
568                                                     void *old_priv)
569 {
570         struct net_device *pnetdev;
571         struct rtw_netdev_priv_indicator *pnpi;
572
573         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
574         if (!pnetdev)
575                 goto RETURN;
576
577         pnpi = netdev_priv(pnetdev);
578         pnpi->priv = old_priv;
579         pnpi->sizeof_priv = sizeof_priv;
580
581 RETURN:
582         return pnetdev;
583 }
584
585 struct net_device *rtw_alloc_etherdev(int sizeof_priv)
586 {
587         struct net_device *pnetdev;
588         struct rtw_netdev_priv_indicator *pnpi;
589
590         pnetdev = alloc_etherdev_mq(sizeof(struct rtw_netdev_priv_indicator), 4);
591         if (!pnetdev)
592                 goto RETURN;
593
594         pnpi = netdev_priv(pnetdev);
595
596         pnpi->priv = rtw_zvmalloc(sizeof_priv);
597         if (!pnpi->priv) {
598                 free_netdev(pnetdev);
599                 pnetdev = NULL;
600                 goto RETURN;
601         }
602
603         pnpi->sizeof_priv = sizeof_priv;
604 RETURN:
605         return pnetdev;
606 }
607
608 void rtw_free_netdev(struct net_device *netdev)
609 {
610         struct rtw_netdev_priv_indicator *pnpi;
611
612         if (!netdev)
613                 goto RETURN;
614
615         pnpi = netdev_priv(netdev);
616
617         if (!pnpi->priv)
618                 goto RETURN;
619
620         rtw_vmfree(pnpi->priv, pnpi->sizeof_priv);
621         free_netdev(netdev);
622
623 RETURN:
624         return;
625 }
626
627 int rtw_change_ifname(struct adapter *padapter, const char *ifname)
628 {
629         struct net_device *pnetdev;
630         struct net_device *cur_pnetdev = padapter->pnetdev;
631         struct rereg_nd_name_data *rereg_priv;
632         int ret;
633
634         if (!padapter)
635                 goto error;
636
637         rereg_priv = &padapter->rereg_nd_name_priv;
638
639         /* free the old_pnetdev */
640         if (rereg_priv->old_pnetdev) {
641                 free_netdev(rereg_priv->old_pnetdev);
642                 rereg_priv->old_pnetdev = NULL;
643         }
644
645         if (!rtnl_is_locked())
646                 unregister_netdev(cur_pnetdev);
647         else
648                 unregister_netdevice(cur_pnetdev);
649
650         rtw_proc_remove_one(cur_pnetdev);
651
652         rereg_priv->old_pnetdev = cur_pnetdev;
653
654         pnetdev = rtw_init_netdev(padapter);
655         if (!pnetdev)  {
656                 ret = -1;
657                 goto error;
658         }
659
660         SET_NETDEV_DEV(pnetdev, dvobj_to_dev(adapter_to_dvobj(padapter)));
661
662         rtw_init_netdev_name(pnetdev, ifname);
663
664         memcpy(pnetdev->dev_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
665
666         if (!rtnl_is_locked())
667                 ret = register_netdev(pnetdev);
668         else
669                 ret = register_netdevice(pnetdev);
670         if (ret != 0) {
671                 RT_TRACE(_module_hci_intfs_c_, _drv_err_,
672                          ("register_netdev() failed\n"));
673                 goto error;
674         }
675         rtw_proc_init_one(pnetdev);
676         return 0;
677 error:
678         return -1;
679 }
680
681 u64 rtw_modular64(u64 x, u64 y)
682 {
683         return do_div(x, y);
684 }
685
686 u64 rtw_division64(u64 x, u64 y)
687 {
688         do_div(x, y);
689         return x;
690 }
691
692 void rtw_buf_free(u8 **buf, u32 *buf_len)
693 {
694         *buf_len = 0;
695         kfree(*buf);
696         *buf = NULL;
697 }
698
699 void rtw_buf_update(u8 **buf, u32 *buf_len, u8 *src, u32 src_len)
700 {
701         u32 ori_len = 0, dup_len = 0;
702         u8 *ori = NULL;
703         u8 *dup = NULL;
704
705         if (!buf || !buf_len)
706                 return;
707
708         if (!src || !src_len)
709                 goto keep_ori;
710
711         /* duplicate src */
712         dup = rtw_malloc(src_len);
713         if (dup) {
714                 dup_len = src_len;
715                 memcpy(dup, src, dup_len);
716         }
717
718 keep_ori:
719         ori = *buf;
720         ori_len = *buf_len;
721
722         /* replace buf with dup */
723         *buf_len = 0;
724         *buf = dup;
725         *buf_len = dup_len;
726
727         /* free ori */
728         kfree(ori);
729 }
730
731
732 /**
733  * rtw_cbuf_full - test if cbuf is full
734  * @cbuf: pointer of struct rtw_cbuf
735  *
736  * Returns: true if cbuf is full
737  */
738 inline bool rtw_cbuf_full(struct rtw_cbuf *cbuf)
739 {
740         return (cbuf->write == cbuf->read-1) ? true : false;
741 }
742
743 /**
744  * rtw_cbuf_empty - test if cbuf is empty
745  * @cbuf: pointer of struct rtw_cbuf
746  *
747  * Returns: true if cbuf is empty
748  */
749 inline bool rtw_cbuf_empty(struct rtw_cbuf *cbuf)
750 {
751         return (cbuf->write == cbuf->read) ? true : false;
752 }
753
754 /**
755  * rtw_cbuf_push - push a pointer into cbuf
756  * @cbuf: pointer of struct rtw_cbuf
757  * @buf: pointer to push in
758  *
759  * Lock free operation, be careful of the use scheme
760  * Returns: true push success
761  */
762 bool rtw_cbuf_push(struct rtw_cbuf *cbuf, void *buf)
763 {
764         if (rtw_cbuf_full(cbuf))
765                 return _FAIL;
766
767         if (0)
768                 DBG_88E("%s on %u\n", __func__, cbuf->write);
769         cbuf->bufs[cbuf->write] = buf;
770         cbuf->write = (cbuf->write+1)%cbuf->size;
771
772         return _SUCCESS;
773 }
774
775 /**
776  * rtw_cbuf_pop - pop a pointer from cbuf
777  * @cbuf: pointer of struct rtw_cbuf
778  *
779  * Lock free operation, be careful of the use scheme
780  * Returns: pointer popped out
781  */
782 void *rtw_cbuf_pop(struct rtw_cbuf *cbuf)
783 {
784         void *buf;
785         if (rtw_cbuf_empty(cbuf))
786                 return NULL;
787
788         if (0)
789                 DBG_88E("%s on %u\n", __func__, cbuf->read);
790         buf = cbuf->bufs[cbuf->read];
791         cbuf->read = (cbuf->read+1)%cbuf->size;
792
793         return buf;
794 }
795
796 /**
797  * rtw_cbuf_alloc - allocte a rtw_cbuf with given size and do initialization
798  * @size: size of pointer
799  *
800  * Returns: pointer of srtuct rtw_cbuf, NULL for allocation failure
801  */
802 struct rtw_cbuf *rtw_cbuf_alloc(u32 size)
803 {
804         struct rtw_cbuf *cbuf;
805
806         cbuf = (struct rtw_cbuf *)rtw_malloc(sizeof(*cbuf) +
807                sizeof(void *)*size);
808
809         if (cbuf) {
810                 cbuf->write = 0;
811                 cbuf->read = 0;
812                 cbuf->size = size;
813         }
814         return cbuf;
815 }