]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/can_quekern.c
Added missing spin-lock initialization and minor Makefiles update.
[lincan.git] / lincan / src / can_quekern.c
1 /* can_quekern.c - CAN message queues functions for the Linux kernel
2  * Linux CAN-bus device driver.
3  * New CAN queues by Pavel Pisa - OCERA team member
4  * email:pisa@cmp.felk.cvut.cz
5  * This software is released under the GPL-License.
6  * Version lincan-0.2  9 Jul 2003
7  */
8
9 #include "../include/can.h"
10 #include "../include/can_sysdep.h"
11 #include "../include/can_queue.h"
12
13 //#define CAN_DEBUG
14
15 extern atomic_t edge_num_cnt;
16
17 #ifdef CAN_DEBUG
18         #define DEBUGQUE(fmt,args...) printk(KERN_ERR "can_queue (debug): " fmt,\
19         ##args)
20
21 #else
22         #define DEBUGQUE(fmt,args...)
23 #endif
24
25 #define ERRMSGQUE(fmt,args...) printk(KERN_ERR "can_queue: " fmt,\
26         ##args)
27
28
29 /* 
30  * Modifies Tx message processing 
31  *  0 .. local message processing disabled
32  *  1 .. local messages disabled by default but can be enabled by canque_set_filt
33  *  2 .. local messages enabled by default, can be disabled by canque_set_filt
34  */
35 extern int processlocal;
36
37 void canque_dead_func(unsigned long data);
38
39 /* Support for dead ends structures left after client close */
40 can_spinlock_t canque_dead_func_lock;
41 LIST_HEAD(canque_dead_ends);
42 /* retrieved by list_entry(canque_dead_ends.next,struct canque_ends_t,dead_peers) */
43 LIST_HEAD(canque_dead_edges);
44 /* retrieved by list_entry(canque_dead_edges.next,struct canque_edge_t,inpeers) */
45 DECLARE_TASKLET(canque_dead_tl, canque_dead_func, 0);
46 /* activated by tasklet_schedule(&canque_dead_tl) */
47
48
49 static inline
50 struct canque_edge_t *canque_dead_edges_cut_first(void)
51 {
52         can_spin_irqflags_t flags;
53         struct canque_edge_t *edge;
54         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
55         if(list_empty(&canque_dead_edges))
56                 edge=NULL;
57         else{
58                 edge=list_entry(canque_dead_edges.next,struct canque_edge_t,inpeers);
59                 list_del(&edge->inpeers);
60         }
61         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
62         return edge;
63 }
64
65 void canque_dead_func(unsigned long data)
66 {
67         can_spin_irqflags_t flags;
68         struct canque_edge_t *qedge;
69         struct canque_ends_t *qends;
70         struct list_head *entry;
71
72         while((qedge=canque_dead_edges_cut_first())){
73                 DEBUGQUE("edge %d disposed\n",qedge->edge_num);
74                 kfree(qedge);
75         }
76         
77         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
78         entry=canque_dead_ends.next;
79         can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
80         while(entry!=&canque_dead_ends){
81                 qends=list_entry(canque_dead_ends.next,struct canque_ends_t,dead_peers);
82                 entry=entry->next;
83                 if(!list_empty(&qends->inlist))
84                         continue;
85                 if(!list_empty(&qends->outlist))
86                         continue;
87                 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
88                 list_del(&qends->dead_peers);
89                 can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
90                 DEBUGQUE("ends structure disposed\n");
91                 kfree(qends);
92         }
93
94 }
95
96
97 void canque_edge_do_dead(struct canque_edge_t *edge, int dead_fl)
98 {
99         can_spin_irqflags_t flags;
100         
101         if(dead_fl) return;
102         
103         if(canqueue_disconnect_edge(edge)<0){
104                 ERRMSGQUE("canque_edge_do_dead: canqueue_disconnect_edge failed !!!\n");
105                 return;
106         }
107
108         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
109         list_add(&edge->inpeers,&canque_dead_edges);
110         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
111         tasklet_schedule(&canque_dead_tl);
112 }
113
114
115
116 /*if(qends->ends_flags & CAN_ENDSF_DEAD){
117         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
118         list_del(&qends->dead_peers);
119         list_add(&qends->dead_peers,&canque_dead_ends);
120         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
121         tasklet_schedule(&canque_dead_tl);
122 }*/
123
124
125 /**
126  * canqueue_notify_kern - notification callback handler for Linux userspace clients
127  * @qends: pointer to the callback side ends structure
128  * @qedge: edge which invoked notification 
129  * @what: notification type
130  */
131 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
132 {
133         DEBUGQUE("canqueue_notify_kern for edge %d, use %d and event %d\n",
134                         qedge->edge_num,(int)atomic_read(&qedge->edge_used),what);
135         switch(what){
136                 case CANQUEUE_NOTIFY_EMPTY:
137                         wake_up(&qends->endinfo.fileinfo.emptyq);
138                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, FREEONEMPTY))
139                                 canque_edge_decref(qedge);
140                         break;
141                 case CANQUEUE_NOTIFY_SPACE:
142                         wake_up(&qends->endinfo.fileinfo.writeq);
143                     #ifdef CAN_ENABLE_KERN_FASYNC
144                         /* Asynchronous I/O processing */
145                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_OUT); 
146                     #endif /*CAN_ENABLE_KERN_FASYNC*/
147                         break;
148                 case CANQUEUE_NOTIFY_PROC:
149                         wake_up(&qends->endinfo.fileinfo.readq);
150                     #ifdef CAN_ENABLE_KERN_FASYNC
151                         /* Asynchronous I/O processing */
152                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_IN); 
153                     #endif /*CAN_ENABLE_KERN_FASYNC*/
154                         break;
155                 case CANQUEUE_NOTIFY_NOUSR:
156                         wake_up(&qends->endinfo.fileinfo.readq);
157                         wake_up(&qends->endinfo.fileinfo.writeq);
158                         wake_up(&qends->endinfo.fileinfo.emptyq);
159                         break;
160                 case CANQUEUE_NOTIFY_DEAD_WANTED:
161                 case CANQUEUE_NOTIFY_DEAD:
162                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, READY))
163                                 canque_edge_decref(qedge);
164                         break;
165                 case CANQUEUE_NOTIFY_ATTACH:
166                         break;
167         }
168 }
169
170 /**
171  * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
172  * @qends: pointer to the callback side ends structure
173  */
174 int canqueue_ends_init_kern(struct canque_ends_t *qends)
175 {
176         canqueue_ends_init_gen(qends);
177         qends->context=NULL;
178         init_waitqueue_head(&qends->endinfo.fileinfo.readq);
179         init_waitqueue_head(&qends->endinfo.fileinfo.writeq);
180         init_waitqueue_head(&qends->endinfo.fileinfo.emptyq);
181     #ifdef CAN_ENABLE_KERN_FASYNC
182         qends->endinfo.fileinfo.fasync=NULL;
183     #endif /*CAN_ENABLE_KERN_FASYNC*/
184         
185         qends->notify=canqueue_notify_kern;
186         DEBUGQUE("canqueue_ends_init_kern\n");
187         return 0;
188 }
189
190
191 /**
192  * canque_get_inslot4id_wait_kern - find or wait for best outgoing edge and slot for given ID
193  * @qends: ends structure belonging to calling communication object
194  * @qedgep: place to store pointer to found edge
195  * @slotp: place to store pointer to  allocated slot
196  * @cmd: command type for slot
197  * @id: communication ID of message to send into edge
198  * @prio: optional priority of message
199  *
200  * Same as canque_get_inslot4id(), except, that it waits for free slot
201  * in case, that queue is full. Function is specific for Linux userspace clients.
202  * Return Value: If there is no usable edge negative value is returned.
203  */
204 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
205         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
206         int cmd, unsigned long id, int prio)
207 {
208         int ret=-1;
209         DEBUGQUE("canque_get_inslot4id_wait_kern for cmd %d, id %ld, prio %d\n",cmd,id,prio);
210         wait_event_interruptible((qends->endinfo.fileinfo.writeq), 
211                 (ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))!=-1);
212         return ret;
213 }
214
215 /**
216  * canque_get_outslot_wait_kern - receive or wait for ready slot for given ends
217  * @qends: ends structure belonging to calling communication object
218  * @qedgep: place to store pointer to found edge
219  * @slotp: place to store pointer to received slot
220  *
221  * The same as canque_test_outslot(), except it waits in the case, that there is
222  * no ready slot for given ends. Function is specific for Linux userspace clients.
223  * Return Value: Negative value informs, that there is no ready output
224  *      slot for given ends. Positive value is equal to the command
225  *      slot has been allocated by the input side.
226  */
227 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
228         struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
229 {
230         int ret=-1;
231         DEBUGQUE("canque_get_outslot_wait_kern\n");
232         wait_event_interruptible((qends->endinfo.fileinfo.readq), 
233                 (ret=canque_test_outslot(qends,qedgep,slotp))!=-1);
234         return ret;
235 }
236
237 /**
238  * canque_sync_wait_kern - wait for all slots processing
239  * @qends: ends structure belonging to calling communication object
240  * @qedge: pointer to edge
241  *
242  * Functions waits for ends transition into empty state.
243  * Return Value: Positive value indicates, that edge empty state has been reached.
244  *      Negative or zero value informs about interrupted wait or other problem.
245  */
246 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
247 {
248         int ret=-1;
249         DEBUGQUE("canque_sync_wait_kern\n");
250         wait_event_interruptible((qends->endinfo.fileinfo.emptyq), 
251                 (ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0));
252         return ret;
253 }
254
255
256 /**
257  * canque_new_edge_kern - allocate new edge structure in the Linux kernel context
258  * @slotsnr: required number of slots in the newly allocated edge structure
259  *
260  * Return Value: Returns pointer to allocated slot structure or %NULL if
261  *      there is not enough memory to process operation.
262  */
263 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
264 {
265         struct canque_edge_t *qedge;
266         qedge = (struct canque_edge_t *)kmalloc(sizeof(struct canque_edge_t), GFP_KERNEL);
267         if(qedge == NULL) return NULL;
268
269         memset(qedge,0,sizeof(struct canque_edge_t));
270         can_spin_lock_init(&qedge->fifo.fifo_lock);
271         if(canque_fifo_init_slots(&qedge->fifo, slotsnr)<0){
272                 kfree(qedge);
273                 DEBUGQUE("canque_new_edge_kern failed\n");
274                 return NULL;
275         }
276         atomic_set(&qedge->edge_used,1);
277         qedge->filtid = 0;
278         qedge->filtmask = canque_filtid2internal(0l, (processlocal<2)? MSG_LOCAL:0);
279         qedge->edge_prio = 0;
280     #ifdef CAN_DEBUG
281         /* not exactly clean, but enough for debugging */
282         atomic_inc(&edge_num_cnt);
283         qedge->edge_num=atomic_read(&edge_num_cnt);
284     #endif /* CAN_DEBUG */
285         DEBUGQUE("canque_new_edge_kern %d\n",qedge->edge_num);
286         return qedge;
287 }
288
289 #ifdef USE_SYNC_DISCONNECT_EDGE_KERN
290
291 /**
292  * canqueue_disconnect_edge_kern - disconnect edge from communicating entities with wait
293  * @qends: ends structure belonging to calling communication object
294  * @qedge: pointer to edge
295  *
296  * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
297  * use counter.
298  * Return Value: Negative value means, that edge is used and cannot
299  *      be disconnected yet. Operation has to be delayed.
300  */
301 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
302 {
303         canque_fifo_set_fl(&qedge->fifo,BLOCK);
304         DEBUGQUE("canqueue_disconnect_edge_kern %d called\n",qedge->edge_num);
305         if(!canque_fifo_test_and_set_fl(&qedge->fifo,DEAD)){
306                 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_DEAD);
307                 
308                 if(atomic_read(&qedge->edge_used)>0)
309                         atomic_dec(&qedge->edge_used);
310
311                 DEBUGQUE("canqueue_disconnect_edge_kern %d waiting\n",qedge->edge_num);
312                 wait_event((qends->endinfo.fileinfo.emptyq), 
313                         (canqueue_disconnect_edge(qedge)>=0));
314
315                 /*set_current_state(TASK_UNINTERRUPTIBLE);*/
316                 /*schedule_timeout(HZ);*/
317                 return 0;
318         } else {
319                 DEBUGQUE("canqueue_disconnect_edge_kern cannot set DEAD\n");
320                 return -1;
321         }
322 }
323
324
325 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
326 {
327         struct canque_edge_t *edge;
328         can_spin_irqflags_t flags;
329         for(;;){
330                 can_spin_lock_irqsave(&qends->ends_lock,flags);
331                 if(list_empty(list)){
332                         can_spin_unlock_irqrestore(&qends->ends_lock,flags);
333                         return 0;
334                 }
335                 if(list == &qends->inlist)
336                         edge=list_entry(list->next,struct canque_edge_t,inpeers);
337                 else
338                         edge=list_entry(list->next,struct canque_edge_t,outpeers);
339                 atomic_inc(&edge->edge_used);
340                 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
341                 if(canqueue_disconnect_edge_kern(qends, edge)>=0) {
342                         /* Free edge memory */
343                         canque_fifo_done(&edge->fifo);
344                         kfree(edge);
345                 }else{
346                         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
347                         canque_edge_decref(edge);
348                         DEBUGQUE("canqueue_disconnect_list_kern in troubles\n");
349                         DEBUGQUE("the edge %d has usage count %d and flags %ld\n",edge->edge_num,atomic_read(&edge->edge_used),edge->fifo.fifo_flags);
350                         return -1;
351                 }
352         }
353 }
354
355 #endif /*USE_SYNC_DISCONNECT_EDGE_KERN*/
356
357
358 int canqueue_ends_sync_all_kern(struct canque_ends_t *qends)
359 {
360         struct canque_edge_t *qedge;
361         
362         canque_for_each_inedge(qends, qedge){
363                 DEBUGQUE("canque_sync_wait_kern called for edge %d\n",qedge->edge_num);
364                 canque_sync_wait_kern(qends, qedge);
365         }
366         return 0;
367 }
368
369 /**
370  * canqueue_ends_dispose_kern - finalizing of the ends structure for Linux kernel clients
371  * @qends: pointer to ends structure
372  * @sync: flag indicating, that user wants to wait for processing of all remaining
373  *      messages
374  *
375  * Return Value: Function should be designed such way to not fail.
376  */
377 int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync)
378 {
379         can_spin_irqflags_t flags;
380         int delayed;
381
382         DEBUGQUE("canqueue_ends_dispose_kern\n");
383         canqueue_block_inlist(qends);
384         canqueue_block_outlist(qends);
385
386         /*Wait for sending of all pending messages in the output FIFOs*/
387         if(sync)
388                 canqueue_ends_sync_all_kern(qends);
389         
390         /* Finish or kill all outgoing edges listed in inends */
391         delayed=canqueue_ends_kill_inlist(qends, 1);
392         /* Kill all incoming edges listed in outends */
393         delayed|=canqueue_ends_kill_outlist(qends);
394
395         wake_up(&qends->endinfo.fileinfo.readq);
396         wake_up(&qends->endinfo.fileinfo.writeq);
397         wake_up(&qends->endinfo.fileinfo.emptyq);
398
399         if(delayed){
400                 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
401                 qends->ends_flags |= CAN_ENDSF_DEAD;
402                 list_add(&qends->dead_peers,&canque_dead_ends);
403                 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
404                 tasklet_schedule(&canque_dead_tl);
405
406                 DEBUGQUE("canqueue_ends_dispose_kern delayed\n");
407                 return 1;
408         }
409
410         kfree(qends);
411         DEBUGQUE("canqueue_ends_dispose_kern finished\n");
412         return 0;
413 }
414
415 void canqueue_kern_initialize()
416 {
417         can_spin_lock_init(&canque_dead_func_lock);
418 }