xref: /utopia/UTPA2-700.0.x/modules/msos/utopia_core/linux_kernel/utopia_adp.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 #include "utopia_adp.h"
2 #include "utopia.h"
3 #include <linux/module.h>
4 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/vmalloc.h>
7 #include <linux/wait.h>
8 #include <linux/sched.h>
9 #include <linux/kthread.h>
10 #include <linux/mutex.h>
11 #include <linux/delay.h>
12 #include <linux/spinlock.h>
13 
14 #include <linux/list.h>
15 
16 
17 #define DEBUG  1
18 #define USE_MUTEX 0
19 #define USE_SPINLOCK 1
20 
21 typedef struct KQueue {
22     char*   pointer;
23     struct  list_head list;
24 } KQueueNode ;
25 
26 
27 static struct list_head KQueue_head_list[MODULE_NUM];
28 #if USE_MUTEX
29 static struct mutex KQueue_mutex[MODULE_NUM];
30 #endif
31 static wait_queue_head_t wq[MODULE_NUM];
32 static spinlock_t KQueue_spinlock[MODULE_NUM];
33 
34 char our_thread[8]="thread1";
35 int j =4;
36 int *k;
37 
UADPKQueuePop(MS_U32 module_ID)38 char * UADPKQueuePop(MS_U32 module_ID)
39 {
40 
41     KQueueNode * pKQueueNode = NULL;
42     unsigned long   dwLockFlag = 0;
43 
44     if(UADPKQueueIsempty(module_ID))
45     {
46         wait_event_interruptible(wq[module_ID],!UADPKQueueIsempty(module_ID));
47     }
48     #if USE_MUTEX
49         mutex_lock(&KQueue_mutex[module_ID]);
50     #endif
51 
52     #if USE_SPINLOCK
53         spin_lock_irqsave(&KQueue_spinlock[module_ID], dwLockFlag);
54     #endif
55         pKQueueNode = (KQueueNode *)list_entry((KQueue_head_list[module_ID].next),KQueueNode,list);
56         list_del(KQueue_head_list[module_ID].next);
57 
58     #if USE_MUTEX
59         mutex_unlock(&KQueue_mutex[module_ID]);
60     #endif
61 
62     #if USE_SPINLOCK
63         spin_unlock_irqrestore(&KQueue_spinlock[module_ID], dwLockFlag);
64     #endif
65 
66     #if DEBUG
67     {
68         printk("get value%d\n",* (pKQueueNode -> pointer) );
69     }
70     #endif
71 
72     return pKQueueNode -> pointer;
73 }
74 EXPORT_SYMBOL(UADPKQueuePop);
75 
76 
UADPKQueuePush(MS_U8 module_ID,char * pointer)77 void UADPKQueuePush(MS_U8 module_ID,char *pointer)
78 {
79     //struct list_head * plist;
80     unsigned long   dwLockFlag = 0;
81     KQueueNode * pKQueueNode=NULL;
82     pKQueueNode= kmalloc(sizeof(*pKQueueNode), GFP_KERNEL);
83     pKQueueNode->pointer = pointer;
84 
85     #if USE_SPINLOCK
86     spin_lock_irqsave(&KQueue_spinlock[module_ID], dwLockFlag);
87     #endif
88 
89     #if USE_MUTEX
90         mutex_lock(&KQueue_mutex[module_ID]);
91     #endif
92     list_add_tail(&pKQueueNode->list,&KQueue_head_list[module_ID]);
93 
94     #if DEBUG
95         pKQueueNode = (KQueueNode *)list_entry((KQueue_head_list[module_ID].next),KQueueNode,list);
96         printk("[%s,%d]:%d\n",__func__,__LINE__,*pKQueueNode -> pointer );
97     #endif
98 
99     #if USE_SPINLOCK
100         spin_unlock_irqrestore(&KQueue_spinlock[module_ID], dwLockFlag);
101     #endif
102 
103     #if USE_MUTEX
104         mutex_unlock(&KQueue_mutex[module_ID]);
105     #endif
106 
107     wake_up(&wq[module_ID]);
108 
109 }
110 EXPORT_SYMBOL(UADPKQueuePush);
111 
112 
UADPKQueueWaitThreadNum(MS_U8 module_ID)113 int UADPKQueueWaitThreadNum(MS_U8 module_ID)
114 {
115     unsigned long   dwLockFlag = 0;
116     struct list_head * plist;
117     int count = 0;
118 
119     #if USE_MUTEX
120         mutex_lock(&KQueue_mutex[module_ID]);
121     #endif
122 
123     #if USE_SPINLOCK
124         spin_lock_irqsave(&KQueue_spinlock[module_ID], dwLockFlag);
125     #endif
126 
127     list_for_each(plist,&(wq[module_ID].task_list))
128     {
129         count++;
130     }
131     #if USE_SPINLOCK
132         spin_unlock_irqrestore(&KQueue_spinlock[module_ID], dwLockFlag);
133     #endif
134 
135     #if USE_MUTEX
136         mutex_unlock(&KQueue_mutex[module_ID]);
137     #endif
138 
139     return count;
140 
141 }
142 
UADPKQueueDataNum(u8 module_ID)143 int UADPKQueueDataNum(u8 module_ID)
144 {
145     unsigned long   dwLockFlag = 0;
146     struct list_head * plist;
147     int count = 0;
148 
149     #if USE_MUTEX
150         mutex_lock(&KQueue_mutex[module_ID]);
151     #endif
152 
153     #if USE_SPINLOCK
154         spin_lock_irqsave(&KQueue_spinlock[module_ID], dwLockFlag);
155     #endif
156 
157     list_for_each(plist,&KQueue_head_list[module_ID])
158     {
159         count++;
160     }
161     #if USE_SPINLOCK
162         spin_unlock_irqrestore(&KQueue_spinlock[module_ID], dwLockFlag);
163     #endif
164 
165     #if USE_MUTEX
166         mutex_unlock(&KQueue_mutex[module_ID]);
167     #endif
168 
169     return count;
170 }
171 EXPORT_SYMBOL(UADPKQueueDataNum);
172 
UADPCreateKQueue(MS_U8 module_ID)173 void UADPCreateKQueue(MS_U8 module_ID)
174 {
175    ;
176 }
177 //EXPORT_SYMBOL(UADPCreateKQueue);
178 
UADPDeleteQueue(MS_U8 module_ID)179 void UADPDeleteQueue(MS_U8 module_ID)
180 {
181 }
182 //EXPORT_SYMBOL(UADPDeleteQueue);
183 
UADPDeleteKQueue(void)184 static void UADPDeleteKQueue(void)
185 {
186     unsigned long dwLockFlag = 0;
187     struct list_head * plist, *temp;
188     int i;
189     KQueueNode * pKQueueNode = NULL;
190 
191     for (i=0;i<MODULE_NUM;i++)
192     {
193         spin_lock_irqsave(&KQueue_spinlock[i], dwLockFlag);
194         list_for_each_safe(plist,temp,&KQueue_head_list[i])
195         {
196             pKQueueNode=(KQueueNode *)list_entry(plist,KQueueNode, list);
197             list_del(plist);
198             kfree((const void*) pKQueueNode);
199         }
200         spin_unlock_irqrestore(&KQueue_spinlock[i], dwLockFlag);
201     }
202     printk("driver removed successfully\n");
203 
204 }
205 
test(void * p)206 static int test(void * p)
207 {
208     //int j =4;
209     int  *k=NULL;
210     //i = &j;
211     int count=0;
212     //UADPKQueuePush(0,(char *)i);
213 
214     k = (int *)UADPKQueuePop(0);
215 
216     if ( k != NULL)
217     {
218         printk("%s:%d 0x%p\n",__func__,__LINE__,k);
219         printk("%s:%d val:%d\n",__func__,__LINE__,*k);
220     }
221 
222     printk("data count = %d\n",count);
223     return 0;
224 }
225 
UADPKQueueInit(void)226 static int UADPKQueueInit(void)
227 {
228     int i,count;
229 
230     struct task_struct *thread1;
231 
232     k = &j;
233 
234     for ( i=0;i< MODULE_NUM; i++)
235     {
236         INIT_LIST_HEAD(&KQueue_head_list[i]);
237         init_waitqueue_head(&wq[i]);
238     #if USE_MUTEX
239         mutex_init(&KQueue_mutex[i]);
240     #endif
241 
242     #if USE_SPINLOCK
243         KQueue_spinlock[i] = __SPIN_LOCK_UNLOCKED(KQueue_spinlock[i]);
244     #endif
245 
246     }
247 
248     return 0;
249 }
250 
UADPKQueueIsempty(MS_U8 module_ID)251 int UADPKQueueIsempty(MS_U8 module_ID)
252 {
253     unsigned long   dwLockFlag = 0;
254     int result;
255     //int count;
256 
257     spin_lock_irqsave(&KQueue_spinlock[module_ID], dwLockFlag);
258     result=list_empty(&KQueue_head_list[module_ID]);
259     //count = UADPKQueueDataNum(0);
260     spin_unlock_irqrestore(&KQueue_spinlock[module_ID], dwLockFlag);
261 
262     printk("result=%d\n",result);
263     //printk("thread count=%d\n",count);
264     return result;
265 }
266 EXPORT_SYMBOL(UADPKQueueIsempty);
267 
268