]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/can_quekern.c
changed usb vendor and product id.
[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.3  17 Jun 2004
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...) can_printk(KERN_ERR "can_quekern (debug): " fmt,\
19         ##args)
20
21 #else
22         #define DEBUGQUE(fmt,args...)
23 #endif
24
25 #define ERRMSGQUE(fmt,args...) can_printk(KERN_ERR "can_quekern: " 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             #ifdef CAN_WITH_RTL
75                 if(canque_fifo_test_fl(&qedge->fifo,RTL_MEM)){
76                         canque_dispose_edge_rtl(qedge);
77                         continue;
78                 }
79             #endif /*CAN_WITH_RTL*/
80                 canque_fifo_done_kern(&qedge->fifo);
81                 kfree(qedge);
82         }
83         
84         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
85         entry=canque_dead_ends.next;
86         can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
87         /* lock can be released there, because only one instance of canque_dead_tl
88            can run at once and all other functions add ends only to head */
89         while(entry!=&canque_dead_ends){
90                 qends=list_entry(entry,struct canque_ends_t,dead_peers);
91                 entry=entry->next;
92                 if(!list_empty(&qends->inlist))
93                         continue;
94                 if(!list_empty(&qends->outlist))
95                         continue;
96                 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
97                 list_del(&qends->dead_peers);
98                 can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
99                 DEBUGQUE("ends structure disposed\n");
100             #ifdef CAN_WITH_RTL
101                 if(qends->ends_flags&CAN_ENDSF_MEM_RTL){
102                         canque_ends_free_rtl(qends);
103                         continue;
104                 }
105             #endif /*CAN_WITH_RTL*/
106                 kfree(qends);
107         }
108
109 }
110
111 static inline void canque_dead_tasklet_schedule(void)
112 {
113     #ifdef CAN_WITH_RTL
114         if(!rtl_rt_system_is_idle()){
115                 set_bit(CAN_RTL2LIN_PEND_DEAD_b,&canqueue_rtl2lin_pend);
116                 rtl_global_pend_irq (canqueue_rtl_irq);
117                 return;
118         }
119     #endif /*CAN_WITH_RTL*/
120
121         tasklet_schedule(&canque_dead_tl);
122 }
123
124
125 void canque_edge_do_dead(struct canque_edge_t *edge)
126 {
127         can_spin_irqflags_t flags;
128         
129         canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
130     #ifdef CAN_WITH_RTL
131         /* The problem of the above call is, that in RT-Linux to Linux notify
132            case is edge scheduled for delayed notify delivery, this needs
133            to be reflected there */
134         if(atomic_read(&edge->edge_used)>0){
135                 can_spin_lock_irqsave(&edge->inends->ends_lock, flags);
136                 can_spin_lock(&edge->outends->ends_lock);
137                 if(atomic_read(&edge->edge_used)>0){
138                         /* left edge to live for a while, banshee comes again in a while */
139                         canque_fifo_clear_fl(&edge->fifo,DEAD);
140                         can_spin_unlock(&edge->outends->ends_lock);
141                         can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
142                         can_printk(KERN_ERR "can_quertl (debug): canque_edge_do_dead postponed\n");
143                         return;
144                 }
145                 can_spin_unlock(&edge->outends->ends_lock);
146                 can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
147         }
148     #endif /*CAN_WITH_RTL*/
149         
150         if(canqueue_disconnect_edge(edge)<0){
151                 ERRMSGQUE("canque_edge_do_dead: canqueue_disconnect_edge failed !!!\n");
152                 return;
153         }
154
155         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
156         list_add(&edge->inpeers,&canque_dead_edges);
157         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
158         canque_dead_tasklet_schedule();
159 }
160
161
162
163 /*if(qends->ends_flags & CAN_ENDSF_DEAD){
164         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
165         list_del(&qends->dead_peers);
166         list_add(&qends->dead_peers,&canque_dead_ends);
167         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
168         tasklet_schedule(&canque_dead_tl);
169 }*/
170
171
172 /**
173  * canqueue_notify_kern - notification callback handler for Linux userspace clients
174  * @qends: pointer to the callback side ends structure
175  * @qedge: edge which invoked notification 
176  * @what: notification type
177  *
178  * The notification event is handled directly by call of this function except case,
179  * when called from RT-Linux context in mixed mode Linux/RT-Linux compilation.
180  * It is not possible to directly call Linux kernel synchronization primitives
181  * in such case. The notification request is postponed and signaled by @pending_inops flags
182  * by call canqueue_rtl2lin_check_and_pend() function. 
183  * The edge reference count is increased until until all pending notifications are processed.
184  */
185 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
186 {
187         DEBUGQUE("canqueue_notify_kern for edge %d, use %d and event %d\n",
188                         qedge->edge_num,(int)atomic_read(&qedge->edge_used),what);
189
190         /* delay event delivery for RT-Linux -> kernel notifications */
191         if(canqueue_rtl2lin_check_and_pend(qends,qedge,what)){
192                 DEBUGQUE("canqueue_notify_kern postponed\n");
193                 return;
194         }
195         
196         switch(what){
197                 case CANQUEUE_NOTIFY_EMPTY:
198                         wake_up(&qends->endinfo.fileinfo.emptyq);
199                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, FREEONEMPTY))
200                                 canque_edge_decref(qedge);
201                         break;
202                 case CANQUEUE_NOTIFY_SPACE:
203                         wake_up(&qends->endinfo.fileinfo.writeq);
204                     #ifdef CAN_ENABLE_KERN_FASYNC
205                         /* Asynchronous I/O processing */
206                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_OUT); 
207                     #endif /*CAN_ENABLE_KERN_FASYNC*/
208                         break;
209                 case CANQUEUE_NOTIFY_PROC:
210                         wake_up(&qends->endinfo.fileinfo.readq);
211                     #ifdef CAN_ENABLE_KERN_FASYNC
212                         /* Asynchronous I/O processing */
213                         kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_IN); 
214                     #endif /*CAN_ENABLE_KERN_FASYNC*/
215                         break;
216                 case CANQUEUE_NOTIFY_NOUSR:
217                         wake_up(&qends->endinfo.fileinfo.readq);
218                         wake_up(&qends->endinfo.fileinfo.writeq);
219                         wake_up(&qends->endinfo.fileinfo.emptyq);
220                         break;
221                 case CANQUEUE_NOTIFY_DEAD_WANTED:
222                 case CANQUEUE_NOTIFY_DEAD:
223                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, READY))
224                                 canque_edge_decref(qedge);
225                         break;
226                 case CANQUEUE_NOTIFY_ATTACH:
227                         break;
228         }
229 }
230
231 /**
232  * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
233  * @qends: pointer to the callback side ends structure
234  */
235 int canqueue_ends_init_kern(struct canque_ends_t *qends)
236 {
237         canqueue_ends_init_gen(qends);
238         qends->context=NULL;
239         init_waitqueue_head(&qends->endinfo.fileinfo.readq);
240         init_waitqueue_head(&qends->endinfo.fileinfo.writeq);
241         init_waitqueue_head(&qends->endinfo.fileinfo.emptyq);
242     #ifdef CAN_ENABLE_KERN_FASYNC
243         qends->endinfo.fileinfo.fasync=NULL;
244     #endif /*CAN_ENABLE_KERN_FASYNC*/
245         
246         qends->notify=canqueue_notify_kern;
247         DEBUGQUE("canqueue_ends_init_kern\n");
248         return 0;
249 }
250
251
252 /**
253  * canque_get_inslot4id_wait_kern - find or wait for best outgoing edge and slot for given ID
254  * @qends: ends structure belonging to calling communication object
255  * @qedgep: place to store pointer to found edge
256  * @slotp: place to store pointer to  allocated slot
257  * @cmd: command type for slot
258  * @id: communication ID of message to send into edge
259  * @prio: optional priority of message
260  *
261  * Same as canque_get_inslot4id(), except, that it waits for free slot
262  * in case, that queue is full. Function is specific for Linux userspace clients.
263  * Return Value: If there is no usable edge negative value is returned.
264  */
265 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
266         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
267         int cmd, unsigned long id, int prio)
268 {
269         int ret=-1;
270         DEBUGQUE("canque_get_inslot4id_wait_kern for cmd %d, id %ld, prio %d\n",cmd,id,prio);
271         wait_event_interruptible((qends->endinfo.fileinfo.writeq), 
272                 (ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))!=-1);
273         return ret;
274 }
275
276 /**
277  * canque_get_outslot_wait_kern - receive or wait for ready slot for given ends
278  * @qends: ends structure belonging to calling communication object
279  * @qedgep: place to store pointer to found edge
280  * @slotp: place to store pointer to received slot
281  *
282  * The same as canque_test_outslot(), except it waits in the case, that there is
283  * no ready slot for given ends. Function is specific for Linux userspace clients.
284  * Return Value: Negative value informs, that there is no ready output
285  *      slot for given ends. Positive value is equal to the command
286  *      slot has been allocated by the input side.
287  */
288 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
289         struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
290 {
291         int ret=-1;
292         DEBUGQUE("canque_get_outslot_wait_kern\n");
293         wait_event_interruptible((qends->endinfo.fileinfo.readq), 
294                 (ret=canque_test_outslot(qends,qedgep,slotp))!=-1);
295         return ret;
296 }
297
298 /**
299  * canque_sync_wait_kern - wait for all slots processing
300  * @qends: ends structure belonging to calling communication object
301  * @qedge: pointer to edge
302  *
303  * Functions waits for ends transition into empty state.
304  * Return Value: Positive value indicates, that edge empty state has been reached.
305  *      Negative or zero value informs about interrupted wait or other problem.
306  */
307 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
308 {
309         int ret=-1;
310         DEBUGQUE("canque_sync_wait_kern\n");
311         wait_event_interruptible((qends->endinfo.fileinfo.emptyq), 
312                 (ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0));
313         return ret;
314 }
315
316
317 /**
318  * canque_fifo_init_kern - initialize one CAN FIFO
319  * @fifo: pointer to the FIFO structure
320  * @slotsnr: number of requested slots
321  *
322  * Return Value: The negative value indicates, that there is no memory
323  *      to allocate space for the requested number of the slots.
324  */
325 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr)
326 {
327         int size;
328         if(!slotsnr) slotsnr=MAX_BUF_LENGTH;
329         size=sizeof(struct canque_slot_t)*slotsnr;
330         fifo->entry=kmalloc(size,GFP_KERNEL);
331         if(!fifo->entry) return -1;
332         fifo->slotsnr=slotsnr;
333         return canque_fifo_init_slots(fifo);
334 }
335
336 /**
337  * canque_fifo_done_kern - frees slots allocated for CAN FIFO
338  * @fifo: pointer to the FIFO structure
339  */
340 int canque_fifo_done_kern(struct canque_fifo_t *fifo)
341 {
342         if(fifo->entry)
343                 kfree(fifo->entry);
344         fifo->entry=NULL;
345         return 1;
346 }
347
348
349 /**
350  * canque_new_edge_kern - allocate new edge structure in the Linux kernel context
351  * @slotsnr: required number of slots in the newly allocated edge structure
352  *
353  * Return Value: Returns pointer to allocated slot structure or %NULL if
354  *      there is not enough memory to process operation.
355  */
356 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
357 {
358         struct canque_edge_t *qedge;
359         qedge = (struct canque_edge_t *)kmalloc(sizeof(struct canque_edge_t), GFP_KERNEL);
360         if(qedge == NULL) return NULL;
361
362         memset(qedge,0,sizeof(struct canque_edge_t));
363         can_spin_lock_init(&qedge->fifo.fifo_lock);
364         if(canque_fifo_init_kern(&qedge->fifo, slotsnr)<0){
365                 kfree(qedge);
366                 DEBUGQUE("canque_new_edge_kern failed\n");
367                 return NULL;
368         }
369         atomic_set(&qedge->edge_used,1);
370         qedge->filtid = 0;
371         qedge->filtmask = canque_filtid2internal(0l, (processlocal<2)? MSG_LOCAL:0);
372         qedge->edge_prio = 0;
373     #ifdef CAN_DEBUG
374         /* not exactly clean, but enough for debugging */
375         atomic_inc(&edge_num_cnt);
376         qedge->edge_num=atomic_read(&edge_num_cnt);
377     #endif /* CAN_DEBUG */
378         DEBUGQUE("canque_new_edge_kern %d\n",qedge->edge_num);
379         return qedge;
380 }
381
382 #ifdef USE_SYNC_DISCONNECT_EDGE_KERN
383
384 /*not included in doc
385  * canqueue_disconnect_edge_kern - disconnect edge from communicating entities with wait
386  * @qends: ends structure belonging to calling communication object
387  * @qedge: pointer to edge
388  *
389  * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
390  * use counter.
391  * Return Value: Negative value means, that edge is used and cannot
392  *      be disconnected yet. Operation has to be delayed.
393  */
394 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
395 {
396         canque_fifo_set_fl(&qedge->fifo,BLOCK);
397         DEBUGQUE("canqueue_disconnect_edge_kern %d called\n",qedge->edge_num);
398         if(!canque_fifo_test_and_set_fl(&qedge->fifo,DEAD)){
399                 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_DEAD);
400                 
401                 if(atomic_read(&qedge->edge_used)>0)
402                         atomic_dec(&qedge->edge_used);
403
404                 DEBUGQUE("canqueue_disconnect_edge_kern %d waiting\n",qedge->edge_num);
405                 wait_event((qends->endinfo.fileinfo.emptyq), 
406                         (canqueue_disconnect_edge(qedge)>=0));
407
408                 /*set_current_state(TASK_UNINTERRUPTIBLE);*/
409                 /*schedule_timeout(HZ);*/
410                 return 0;
411         } else {
412                 DEBUGQUE("canqueue_disconnect_edge_kern cannot set DEAD\n");
413                 return -1;
414         }
415 }
416
417
418 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
419 {
420         struct canque_edge_t *edge;
421         can_spin_irqflags_t flags;
422         for(;;){
423                 can_spin_lock_irqsave(&qends->ends_lock,flags);
424                 if(list_empty(list)){
425                         can_spin_unlock_irqrestore(&qends->ends_lock,flags);
426                         return 0;
427                 }
428                 if(list == &qends->inlist)
429                         edge=list_entry(list->next,struct canque_edge_t,inpeers);
430                 else
431                         edge=list_entry(list->next,struct canque_edge_t,outpeers);
432                 atomic_inc(&edge->edge_used);
433                 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
434                 if(canqueue_disconnect_edge_kern(qends, edge)>=0) {
435                         /* Free edge memory */
436                         canque_fifo_done_kern(&edge->fifo);
437                         kfree(edge);
438                 }else{
439                         canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
440                         canque_edge_decref(edge);
441                         DEBUGQUE("canqueue_disconnect_list_kern in troubles\n");
442                         DEBUGQUE("the edge %d has usage count %d and flags %ld\n",edge->edge_num,atomic_read(&edge->edge_used),edge->fifo.fifo_flags);
443                         return -1;
444                 }
445         }
446 }
447
448 #endif /*USE_SYNC_DISCONNECT_EDGE_KERN*/
449
450
451 int canqueue_ends_sync_all_kern(struct canque_ends_t *qends)
452 {
453         struct canque_edge_t *qedge;
454         
455         canque_for_each_inedge(qends, qedge){
456                 DEBUGQUE("canque_sync_wait_kern called for edge %d\n",qedge->edge_num);
457                 canque_sync_wait_kern(qends, qedge);
458         }
459         return 0;
460 }
461
462
463 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends)
464 {
465         can_spin_irqflags_t flags;
466
467         can_spin_lock_irqsave(&canque_dead_func_lock, flags);
468         qends->ends_flags |= CAN_ENDSF_DEAD;
469         list_add(&qends->dead_peers,&canque_dead_ends);
470         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
471         canque_dead_tasklet_schedule();
472 }
473
474
475 /**
476  * canqueue_ends_dispose_kern - finalizing of the ends structure for Linux kernel clients
477  * @qends: pointer to ends structure
478  * @sync: flag indicating, that user wants to wait for processing of all remaining
479  *      messages
480  *
481  * Return Value: Function should be designed such way to not fail.
482  */
483 int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync)
484 {
485         int delayed;
486
487         DEBUGQUE("canqueue_ends_dispose_kern\n");
488         canqueue_block_inlist(qends);
489         canqueue_block_outlist(qends);
490
491         /*Wait for sending of all pending messages in the output FIFOs*/
492         if(sync)
493                 canqueue_ends_sync_all_kern(qends);
494         
495         /* Finish or kill all outgoing edges listed in inends */
496         delayed=canqueue_ends_kill_inlist(qends, 1);
497         /* Kill all incoming edges listed in outends */
498         delayed|=canqueue_ends_kill_outlist(qends);
499
500         wake_up(&qends->endinfo.fileinfo.readq);
501         wake_up(&qends->endinfo.fileinfo.writeq);
502         wake_up(&qends->endinfo.fileinfo.emptyq);
503
504         if(delayed){
505                 canqueue_ends_dispose_postpone(qends);
506
507                 DEBUGQUE("canqueue_ends_dispose_kern delayed\n");
508                 return 1;
509         }
510
511         kfree(qends);
512         DEBUGQUE("canqueue_ends_dispose_kern finished\n");
513         return 0;
514 }
515
516 void canqueue_kern_initialize()
517 {
518         can_spin_lock_init(&canque_dead_func_lock);
519 }