summaryrefslogtreecommitdiff
path: root/wilink_6_1/platforms/os/linux/src/osapi.c
blob: fd99089c678ef4e62c93a845a07ee8f089915d62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/*
 * osapi.c
 *
 * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.      
 * All rights reserved.                                                  
 *                                                                       
 * Redistribution and use in source and binary forms, with or without    
 * modification, are permitted provided that the following conditions    
 * are met:                                                              
 *                                                                       
 *  * Redistributions of source code must retain the above copyright     
 *    notice, this list of conditions and the following disclaimer.      
 *  * Redistributions in binary form must reproduce the above copyright  
 *    notice, this list of conditions and the following disclaimer in    
 *    the documentation and/or other materials provided with the         
 *    distribution.                                                      
 *  * Neither the name Texas Instruments nor the names of its            
 *    contributors may be used to endorse or promote products derived    
 *    from this software without specific prior written permission.      
 *                                                                       
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/*
 * src/osapi.c
 *
 */
#include "tidef.h"
#include "arch_ti.h"

#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/completion.h>
#include <linux/etherdevice.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <linux/list.h>
#include <stdarg.h>
#include <asm/io.h>
#include "RxBuf_linux.h"

/*#include "debug_module.h"*/
#include "host_platform.h"
#include "WlanDrvIf.h"
#include "bmtrace_api.h"
#include "TI_IPC_Api.h"
#include "802_11Defs.h"
#include "osApi.h"
#include "txMgmtQueue_Api.h"
#include "EvHandler.h"

#ifdef ESTA_TIMER_DEBUG
#define esta_timer_log(fmt,args...)  printk(fmt, ## args)
#else
#define esta_timer_log(fmt,args...)
#endif

#define FRAG_SIZE        200

typedef struct timer_list TOsTimer;

TI_BOOL bRedirectOutputToLogger = TI_FALSE;
TI_BOOL use_debug_module = TI_FALSE;

/****************************************************************************************
 *                        																*
 *						OS Report API													*       
 *																						*
 ****************************************************************************************/
static void SendLoggerData (TI_HANDLE OsContext, TI_UINT8 *pMsg, TI_UINT16 len)
{    
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;

	if (len > 0)
	{
		EvHandlerSendEvent(drv->tCommon.hEvHandler, IPC_EVENT_LOGGER, pMsg, len);
	}   
}

void os_setDebugOutputToLogger(TI_BOOL value)
{
	bRedirectOutputToLogger = value;
}
/****************************************************************************************
 *                        os_setDebugMode()                                 
 ****************************************************************************************
DESCRIPTION:  	Set the Debug Mode 

INPUT:            

RETURN:			None   

NOTES:         	
*****************************************************************************************/
void os_setDebugMode(TI_BOOL enable)
{
	use_debug_module = enable;
}


/****************************************************************************************
 *                        os_printf()                                 
 ****************************************************************************************
DESCRIPTION:  	Print formatted output. 

INPUT:          format -  Specifies the string, to be printed

RETURN:			None   

NOTES:         	
*****************************************************************************************/
void os_printf(const char *format ,...)
{
	static int from_new_line = 1;		/* Used to save the last message EOL */
	va_list ap;
	static char msg[MAX_MESSAGE_SIZE];
	char *p_msg = msg;					/* Pointer to the message */
	TI_UINT16 message_len;					
	TI_UINT32 sec = 0;
	TI_UINT32 uSec = 0;
	os_memoryZero(NULL,msg, MAX_MESSAGE_SIZE);

	/* Format the message and keep the message length */
	va_start(ap,format);
	message_len = vsnprintf(&msg[0], sizeof(msg) -1 , format, ap);
	if( from_new_line )
        {
            if (msg[1] == '$')
            {
                p_msg += 4;
            }
            
            sec = os_timeStampUs(NULL);
            uSec = sec % MICROSECOND_IN_SECONDS;
            sec /= MICROSECOND_IN_SECONDS;
            
            printk(KERN_INFO DRIVER_NAME ": %d.%06d: %s",sec,uSec,p_msg);
        }
        else
        {
		printk(&msg[0]);
        }
        
        from_new_line = ( msg[message_len] == '\n' );
	va_end(ap);
}

/****************************************************************************************
 *                        																*
 *							OS TIMER API												*
 *																						*
 ****************************************************************************************/

/****************************************************************************************
 *                        os_timerCreate()                                 
 ****************************************************************************************
DESCRIPTION:    This function creates and initializes an OS timer object associated with a
				caller's pRoutine function.

ARGUMENTS:		OsContext   - The OS handle
                pRoutine    - The user callback function
                hFuncHandle - The user callback handle

RETURN:			A handle of the created OS timer.

NOTES:         	1) The user's callback is called directly from OS timer context when expired.
                2) In some OSs, it may be needed to use an intermediate callback in the 
                   osapi layer (use os_timerHandlr for that).

*****************************************************************************************/
TI_HANDLE os_timerCreate (TI_HANDLE OsContext, fTimerFunction pRoutine, TI_HANDLE hFuncHandle)
{
	TOsTimer *pOsTimer = os_memoryAlloc (OsContext, sizeof(TOsTimer));

	init_timer (pOsTimer);
	pOsTimer->function = (void *)pRoutine;
	pOsTimer->data     = (int)hFuncHandle;

	return (TI_HANDLE)pOsTimer;
}


/****************************************************************************************
 *                        os_timerDestroy()                                 
 ****************************************************************************************
DESCRIPTION:    This function destroys the OS timer object.

ARGUMENTS:		

RETURN:			

NOTES:         	
*****************************************************************************************/
void os_timerDestroy (TI_HANDLE OsContext, TI_HANDLE TimerHandle)
{
	os_timerStop (OsContext, TimerHandle);
	os_memoryFree (OsContext, TimerHandle, sizeof(TOsTimer));
}


/****************************************************************************************
 *                        os_timerStart()                                 
 ****************************************************************************************
DESCRIPTION:    This function start the timer object.

ARGUMENTS:		

RETURN:			

NOTES:         	
*****************************************************************************************/
void os_timerStart (TI_HANDLE OsContext, TI_HANDLE TimerHandle, TI_UINT32 DelayMs)
{
	TI_UINT32 jiffie_cnt = msecs_to_jiffies (DelayMs);

	mod_timer ((TOsTimer *)TimerHandle, jiffies + jiffie_cnt);
}


/****************************************************************************************
 *                        os_stopTimer()                                 
 ****************************************************************************************
DESCRIPTION:    This function stop the timer object.

ARGUMENTS:		

RETURN:			

NOTES:         	
*****************************************************************************************/
void os_timerStop (TI_HANDLE OsContext, TI_HANDLE TimerHandle)
{
	del_timer_sync((TOsTimer *)TimerHandle);
}


/****************************************************************************************
 *                        os_periodicIntrTimerStart()                                 
 ****************************************************************************************
DESCRIPTION:    This function starts the periodic interrupt mechanism. This mode is used 
				when interrupts that usually received from the Fw is now masked, and we are
				checking for any need of Fw handling in time periods.

ARGUMENTS:		

RETURN:			

NOTES:         	Power level of the CHIP should be always awake in this mode (no ELP)
*****************************************************************************************/
#ifdef PRIODIC_INTERRUPT
void os_periodicIntrTimerStart (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;

	mod_timer (drv->hPollTimer, jiffies + TIWLAN_IRQ_POLL_INTERVAL);
}
#endif


/****************************************************************************************
 *                        os_timeStampMs()                                 
 ****************************************************************************************
DESCRIPTION:	This function returns the number of milliseconds that have elapsed since
				the system was booted.

ARGUMENTS:		OsContext - our adapter context.

RETURN:			

NOTES:         	
*****************************************************************************************/
TI_UINT32 os_timeStampMs (TI_HANDLE OsContext)
{
	struct timeval tv;
	do_gettimeofday(&tv);
	return tv.tv_sec*1000 + tv.tv_usec/1000;
}


/****************************************************************************************
 *                        os_timeStampUs()                                 
 ****************************************************************************************
DESCRIPTION:	This function returns the number of microseconds that have elapsed since
				the system was booted.

ARGUMENTS:		OsContext - our adapter context.
				Note that sometimes this function will be called with NULL(!!!) as argument!

RETURN:			

NOTES:         	
*****************************************************************************************/
TI_UINT32 os_timeStampUs (TI_HANDLE OsContext)
{
	struct timeval tv;
	do_gettimeofday(&tv);
	return tv.tv_sec*1000000 + tv.tv_usec;
}


/****************************************************************************************
 *                        os_StalluSec()                                 
 ****************************************************************************************
DESCRIPTION:	This function make delay in microseconds.

ARGUMENTS:		OsContext - our adapter context.
				uSec - delay time in microseconds

RETURN:			

NOTES:         	
*****************************************************************************************/
void os_StalluSec (TI_HANDLE OsContext, TI_UINT32 uSec)
{
	udelay (uSec);
}


/****************************************************************************************
 *                        																*
 *							Protection services	API										*
 *																						*
 ****************************************************************************************
 * OS protection is implemented as spin_lock_irqsave and spin_unlock_irqrestore  								*
 ****************************************************************************************/


/****************************************************************************************
 *                        os_protectCreate()                                 
 ****************************************************************************************
DESCRIPTION:	

ARGUMENTS:		OsContext - our adapter context.

RETURN:			A handle of the created mutex/spinlock.
				TI_HANDLE_INVALID if there is insufficient memory available or problems
				initializing the mutex

NOTES:         	
*****************************************************************************************/
TI_HANDLE os_protectCreate (TI_HANDLE OsContext)
{
	return NULL;
}


/****************************************************************************************
 *                        os_protectDestroy()                                 
 ****************************************************************************************
DESCRIPTION:		

ARGUMENTS:		OsContext - our adapter context.

RETURN:			None - This had better work since there is not a return value to the user

NOTES:         	
*****************************************************************************************/
void os_protectDestroy (TI_HANDLE OsContext, TI_HANDLE ProtectCtx)
{
}


/****************************************************************************************
 *                        os_protectLock()                                 
 ****************************************************************************************
DESCRIPTION:		

ARGUMENTS:		OsContext - our adapter context.

RETURN:			None - This had better work since there is not a return value to the user

NOTES:         	
*****************************************************************************************/
void os_protectLock (TI_HANDLE OsContext, TI_HANDLE ProtectContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;

	spin_lock_irqsave (&drv->lock, drv->flags);
}


/****************************************************************************************
 *                        os_protectUnlock()                                 
 ****************************************************************************************
DESCRIPTION:		

ARGUMENTS:		OsContext - our adapter context.

RETURN:			None - This had better work since there is not a return value to the user

NOTES:         	
*****************************************************************************************/
void os_protectUnlock (TI_HANDLE OsContext, TI_HANDLE ProtectContext)
{
    TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;
    
    spin_unlock_irqrestore (&drv->lock, drv->flags);
}
/****************************************************************************************
 *                        os_receivePacket()                                 
 ****************************************************************************************
DESCRIPTION:		

ARGUMENTS:		

RETURN:			

NOTES:         	
*****************************************************************************************/
TI_BOOL os_receivePacket (TI_HANDLE OsContext, void* pPacket, TI_UINT16 Length)
{
   TWlanDrvIfObj  *drv     = (TWlanDrvIfObj *)OsContext;
   unsigned char  *pdata   = (unsigned char *)((TI_UINT32)pPacket & ~(TI_UINT32)0x3);
   rx_head_t      *rx_head = (rx_head_t *)(pdata -  WSPI_PAD_BYTES - RX_HEAD_LEN_ALIGNED);
   struct sk_buff *skb     = rx_head->skb;
	
#ifdef TI_DBG
   if ((TI_UINT32)pPacket & 0x3)
   {
     if ((TI_UINT32)pPacket - (TI_UINT32)skb->data != 2)
	 {
	   printk("os_receivePacket() address error skb=0x%x skb->data=0x%x pPacket=0x%x !!!\n",(int)skb, (int)skb->data, (int)pPacket);
	 }
   }
   else
   {
	 if ((TI_UINT32)skb->data != (TI_UINT32)pPacket)
	 {
	   printk("os_receivePacket() address error skb=0x%x skb->data=0x%x pPacket=0x%x !!!\n",(int)skb, (int)skb->data, (int)pPacket);
	 }
   }
   if (Length != RX_ETH_PKT_LEN(pPacket))
   {
	 printk("os_receivePacket() Length=%d !=  RX_ETH_PKT_LEN(pPacket)=%d!!!\n",(int)Length, RX_ETH_PKT_LEN(pPacket));
   }

#endif
/*
   printk("-->> os_receivePacket() pPacket=0x%x Length=%d skb=0x%x skb->data=0x%x skb->head=0x%x skb->len=%d\n",
		  (int)pPacket, (int)Length, (int)skb, (int)skb->data, (int)skb->head, (int)skb->len);
*/
   /* Use skb_reserve, it updates both skb->data and skb->tail. */
   skb->data = RX_ETH_PKT_DATA(pPacket);
   skb->tail = skb->data;
   skb_put(skb, RX_ETH_PKT_LEN(pPacket));
/*
   printk("-->> os_receivePacket() skb=0x%x skb->data=0x%x skb->head=0x%x skb->len=%d\n",
		  (int)skb, (int)skb->data, (int)skb->head, (int)skb->len);
*/
   ti_nodprintf(TIWLAN_LOG_INFO, "os_receivePacket - Received EAPOL len-%d\n", WBUF_LEN(pWbuf));

   skb->dev       = drv->netdev;
   skb->protocol  = eth_type_trans(skb, drv->netdev);
   skb->ip_summed = CHECKSUM_NONE;

   drv->stats.rx_packets++;
   drv->stats.rx_bytes += skb->len;

   /* Send the skb to the TCP stack. 
    * it responsibly of the Linux kernel to free the skb
    */
   {
       CL_TRACE_START_L1();

       os_wake_lock_timeout_enable(drv);

       netif_rx_ni(skb);

       /* Note: Don't change this trace (needed to exclude OS processing from Rx CPU utilization) */
       CL_TRACE_END_L1("tiwlan_drv.ko", "OS", "RX", "");
   }

   return TI_TRUE;
}

/*-----------------------------------------------------------------------------
  
Routine Name:  os_timerHandlr

Routine Description:

    Just a place holder for timer expiration handling in other OSs.
    In Linux, user callback is called directly on OS timer expiry.

Arguments:     parm - timer object handle

Return Value:  None.

Notes:

-----------------------------------------------------------------------------*/
void os_timerHandlr(unsigned long parm)
{
    /* Not needed in Linux (user callback is called directly on OS timer expiry). */
}


/*-----------------------------------------------------------------------------
Routine Name:  os_connectionStatus

Routine Description:

The eSTA-DK will call this API so the OS stack is aware that the
WLAN layer is ready to function.

Arguments:
cStatus = 1; WLAN in ready for network packets
cStatus = 0; WLAN in not ready for network packets

Return Value:  None
-----------------------------------------------------------------------------*/
TI_INT32 os_IndicateEvent (TI_HANDLE OsContext, IPC_EV_DATA* pData)
{
	IPC_EVENT_PARAMS *pInParam =  &pData->EvParams;
	TWlanDrvIfObj    *drv = (TWlanDrvIfObj *)OsContext;
	/*TI_UINT8 AuthBuf[sizeof(TI_UINT32) + sizeof(OS_802_11_AUTHENTICATION_REQUEST)];*/

	ti_nodprintf(TIWLAN_LOG_INFO, "\n  os_ConnectionStatus Event 0x%08x \n", CsStatus->Event);
   
	switch(pInParam->uEventType)
	{
		case IPC_EVENT_ASSOCIATED:
			if (drv->netdev != NULL)
            	netif_carrier_on(drv->netdev);
         	break;

       case IPC_EVENT_DISASSOCIATED:
         	if (drv->netdev != NULL)
        		netif_carrier_off(drv->netdev);
      		break;

      case IPC_EVENT_LINK_SPEED:
         	drv->tCommon.uLinkSpeed = (*(TI_UINT32*)pData->uBuffer * 10000) / 2;
         	ti_nodprintf(TIWLAN_LOG_INFO, "\n  Link Speed = 0x%08x \n",drv->tCommon.uLinkSpeed);
      		break;
   }

   return TI_OK;
}



/******************************************************************************/

void os_disableIrq (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;
	disable_irq (drv->irq);
}

void os_enableIrq (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;
	enable_irq (drv->irq);
}

/*-----------------------------------------------------------------------------
Routine Name:  os_InterruptServiced

Routine Description: Called when IRQ line is not asserted any more 
					(i.e. we can enable IRQ in Level sensitive platform)

Arguments:	OsContext - handle to OS context

Return Value:  none
-----------------------------------------------------------------------------*/
void os_InterruptServiced (TI_HANDLE OsContext)
{
	/* To be implemented with Level IRQ */
}

/*-----------------------------------------------------------------------------
Routine Name:  os_wake_lock_timeout

Routine Description: Called to prevent system from suspend for some time

Arguments:     OsContext - handle to OS context

Return Value:  packet counter
-----------------------------------------------------------------------------*/
int os_wake_lock_timeout (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;
	int ret = 0;
	unsigned long flags;

	spin_lock_irqsave(&drv->lock, flags);
	if (drv) {
		ret = drv->wl_packet;
		if (drv->wl_packet) {
			drv->wl_packet = 0;
#ifdef CONFIG_HAS_WAKELOCK
			wake_lock_timeout(&drv->wl_rxwake, (HZ >> 1));
#endif
		}
	}
	spin_unlock_irqrestore(&drv->lock, flags);
	/* printk("%s: %d\n", __func__, ret); */
	return ret;
}

/*-----------------------------------------------------------------------------
Routine Name:  os_wake_lock_timeout_enable

Routine Description: Called to set flag for suspend prevention for some time

Arguments:     OsContext - handle to OS context

Return Value:  packet counter
-----------------------------------------------------------------------------*/
int os_wake_lock_timeout_enable (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;
	unsigned long flags;
	int ret;

	spin_lock_irqsave(&drv->lock, flags);
	ret = drv->wl_packet = 1;
	spin_unlock_irqrestore(&drv->lock, flags);
	return ret;
}

/*-----------------------------------------------------------------------------
Routine Name:  os_wake_lock

Routine Description: Called to prevent system from suspend

Arguments:     OsContext - handle to OS context

Return Value:  wake_lock counter
-----------------------------------------------------------------------------*/
int os_wake_lock (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;
	int ret = 0;
	unsigned long flags;

	spin_lock_irqsave(&drv->lock, flags);
	if (drv) {
#ifdef CONFIG_HAS_WAKELOCK
		if (!drv->wl_count)
			wake_lock(&drv->wl_wifi);
#endif
		drv->wl_count++;
		ret = drv->wl_count;
	}
	spin_unlock_irqrestore(&drv->lock, flags);
	/* printk("%s: %d\n", __func__, ret); */
	return ret;
}

/*-----------------------------------------------------------------------------
Routine Name:  os_wake_unlock

Routine Description: Called to allow system to suspend

Arguments:     OsContext - handle to OS context

Return Value:  wake_lock counter
-----------------------------------------------------------------------------*/
int os_wake_unlock (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;
	int ret = 0;
	unsigned long flags;

	spin_lock_irqsave(&drv->lock, flags);
	if (drv && drv->wl_count) {
		drv->wl_count--;
#ifdef CONFIG_HAS_WAKELOCK
		if (!drv->wl_count)
			wake_unlock(&drv->wl_wifi);
#endif
		ret = drv->wl_count;
	}
	spin_unlock_irqrestore(&drv->lock, flags);
	/* printk("%s: %d\n", __func__, ret); */
	return ret;
}

/*-----------------------------------------------------------------------------
Routine Name:  os_RequestSchedule

Routine Description:

Arguments:

Return Value:  TI_OK
-----------------------------------------------------------------------------*/
int os_RequestSchedule (TI_HANDLE OsContext)
{
	TWlanDrvIfObj *drv = (TWlanDrvIfObj *)OsContext;

	/* Note: The performance trace below doesn't inclose the schedule
	 *   itself because the rescheduling can occur immediately and call
	 *   os_RequestSchedule again which will confuse the trace tools */
	CL_TRACE_START_L3();
	CL_TRACE_END_L3("tiwlan_drv.ko", "OS", "TASK", "");

	if( !queue_work(drv->tiwlan_wq, &drv->tWork) ) {
		/* printk("%s: Fail\n",__func__); */
		return TI_NOK;
	}

	return TI_OK;
}


/*-----------------------------------------------------------------------------
Routine Name: os_SignalObjectCreate

Routine Description:

Arguments:

Return Value: TI_OK
-----------------------------------------------------------------------------*/
void *os_SignalObjectCreate (TI_HANDLE OsContext)
{
	struct completion *myPtr;
	myPtr = os_memoryAlloc(OsContext, sizeof(struct completion));
	if (myPtr)
		init_completion (myPtr);
	return (myPtr);
}


/*-----------------------------------------------------------------------------
Routine Name: os_SignalObjectWait

Routine Description:

Arguments:

Return Value: TI_OK
-----------------------------------------------------------------------------*/
int os_SignalObjectWait (TI_HANDLE OsContext, void *signalObject)
{
	if (!signalObject)
		return TI_NOK;
	wait_for_completion ((struct completion *)signalObject);
	return TI_OK;
}


/*-----------------------------------------------------------------------------
Routine Name: os_SignalObjectSet

Routine Description:

Arguments:

Return Value: TI_OK
-----------------------------------------------------------------------------*/
int os_SignalObjectSet (TI_HANDLE OsContext, void *signalObject)
{
	if (!signalObject)
		return TI_NOK;
	complete ((struct completion *)signalObject);
	return TI_OK;
}


/*-----------------------------------------------------------------------------
Routine Name: os_SignalObjectFree

Routine Description:

Arguments:

Return Value: TI_OK
-----------------------------------------------------------------------------*/
int os_SignalObjectFree (TI_HANDLE OsContext, void *signalObject)
{
	if (!signalObject)
		return TI_NOK;
	os_memoryFree(OsContext, signalObject, sizeof(struct completion));
	return TI_OK;
}


/** 
 * \fn     os_Trace
 * \brief  Prepare and send trace message to the logger.
 * 
 * \param  OsContext    - The OS handle
 * \param  uLevel   	- Severity level of the trace message
 * \param  uFileId  	- Source file ID of the trace message
 * \param  uLineNum 	- Line number of the trace message
 * \param  uParamsNum   - Number of parameters in the trace message
 * \param  ...          - The trace message parameters
 * 
 * \return void
 */
void os_Trace (TI_HANDLE OsContext, TI_UINT32 uLevel, TI_UINT32 uFileId, TI_UINT32 uLineNum, TI_UINT32 uParamsNum, ...)
{
	TI_UINT32	index;
	TI_UINT32	uParam;
	TI_UINT32	uMaxParamValue = 0;
	TI_UINT32	uMsgLen	= TRACE_MSG_MIN_LENGTH;
	TI_UINT8    aMsg[TRACE_MSG_MAX_LENGTH] = {0};
	TTraceMsg   *pMsgHdr  = (TTraceMsg *)&aMsg[0];
	TI_UINT8    *pMsgData = &aMsg[0] + sizeof(TTraceMsg);
	va_list	    list;

	if (!bRedirectOutputToLogger)
	{
		return;
	}

	if (uParamsNum > TRACE_MSG_MAX_PARAMS)
	{
		uParamsNum = TRACE_MSG_MAX_PARAMS;
	}

	/* sync on the parameters */
	va_start(list, uParamsNum);

	/* find the longest parameter */
	for (index = 0; index < uParamsNum; index++)
	{
		/* get parameter from the stack */
		uParam = va_arg (list, TI_UINT32);

		/* save the longest parameter at variable 'uMaxParamValue' */
		if (uParam > uMaxParamValue)
        {
			uMaxParamValue = uParam;
        }

		/* 32 bit parameter is the longest possible - get out of the loop */
		if (uMaxParamValue > UINT16_MAX_VAL)
        {
			break;
        }
	}

	/* Set msg length and format according to the biggest parameter value (8/16/32 bits) */
	if (uMaxParamValue > UINT16_MAX_VAL)		
	{
		pMsgHdr->uFormat = TRACE_FORMAT_32_BITS_PARAMS;
		uMsgLen += uParamsNum * sizeof(TI_UINT32);
	}
	else if (uMaxParamValue > UINT8_MAX_VAL)	
	{
		pMsgHdr->uFormat = TRACE_FORMAT_16_BITS_PARAMS;
		uMsgLen += uParamsNum * sizeof(TI_UINT16);
	}
	else							
	{
		pMsgHdr->uFormat = TRACE_FORMAT_8_BITS_PARAMS;
		uMsgLen += uParamsNum;
	}

	/* Fill all other header information */
	pMsgHdr->uLevel     = (TI_UINT8)uLevel;
	pMsgHdr->uParamsNum = (TI_UINT8)uParamsNum;
	pMsgHdr->uFileId    = (TI_UINT16)uFileId;
	pMsgHdr->uLineNum   = (TI_UINT16)uLineNum;

	/* re-sync on the parameters */
	va_start(list, uParamsNum);

	/* add the parameters */
	for (index = 0; index < uParamsNum; index++)
	{
		/* get parameter from the stack */
		uParam = va_arg(list, TI_UINT32);

		/* insert the parameter and increment msg pointer */
		switch(pMsgHdr->uFormat)
		{
		case TRACE_FORMAT_8_BITS_PARAMS:
			INSERT_BYTE(pMsgData, uParam);
			break;

		case TRACE_FORMAT_16_BITS_PARAMS:
			INSERT_2_BYTES(pMsgData, uParam);
			break;

		case TRACE_FORMAT_32_BITS_PARAMS:
			INSERT_4_BYTES(pMsgData, uParam);
			break;

        default:
            va_end(list);
            return;
		}     
	}

	va_end(list);

	/* Send the trace message to the logger */
	SendLoggerData(OsContext, aMsg, (TI_UINT16)uMsgLen);
}

/*--------------------------------------------------------------------------------------*/

/** 
 * \fn     os_SetDrvThreadPriority
 * \brief  Called upon init to set WLAN driver thread priority.
 *         Currently not supported in Linux.
 * 
 * \param  OsContext              - The OS handle
 * \param  uWlanDrvThreadPriority - The WLAN driver thread priority
 * \return 
 */
void os_SetDrvThreadPriority (TI_HANDLE OsContext, TI_UINT32 uWlanDrvThreadPriority)
{
}