]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/include/can_queue.h
8821dca02c488ea8fdc8d64af161f3879d8bd777
[lincan.git] / lincan / include / can_queue.h
1 #ifndef _CAN_QUEUE_H
2 #define _CAN_QUEUE_H
3
4 #include "./can.h"
5 #include "./constants.h"
6 #include "./can_sysdep.h"
7
8 /**
9  * struct canque_slot_t - one CAN message slot in the CAN FIFO queue 
10  * @next: pointer to the next/younger slot
11  * @slot_flags: space for flags and optional command describing action
12  *      associated with slot data
13  * @msg: space for one CAN message
14  *
15  * This structure is used to store CAN messages in the CAN FIFO queue.
16  */
17  struct canque_slot_t {
18         struct canque_slot_t *next;
19         unsigned long slot_flags;
20         struct canmsg_t msg;
21 };
22
23 #define CAN_SLOTF_CMD   0x00ff  /*  */
24
25 /**
26  * struct canque_fifo_t - CAN FIFO queue representation
27  * @fifo_flags: this field holds global flags describing state of the FIFO.
28  *      %CAN_FIFOF_ERROR is set when some error condition occurs.
29  *      %CAN_FIFOF_ERR2BLOCK defines, that error should lead to the FIFO block state.
30  *      %CAN_FIFOF_BLOCK state blocks insertion of the next messages. 
31  *      %CAN_FIFOF_OVERRUN attempt to acquire new slot, when FIFO is full. 
32  *      %CAN_FIFOF_FULL indicates FIFO full state. 
33  *      %CAN_FIFOF_EMPTY indicates no allocated slot in the FIFO.
34  *      %CAN_FIFOF_DEAD condition indication. Used when FIFO is beeing destroyed.
35  * @error_code: futher description of error condition
36  * @head: pointer to the FIFO head, oldest slot
37  * @tail: pointer to the location, where pointer to newly inserted slot
38  *      should be added
39  * @flist: pointer to list of the free slots associated with queue
40  * @entry: pointer to the memory allocated for the list slots.
41  * @fifo_lock: the lock to ensure atomicity of slot manipulation operations.
42  * @slotnr:  number of allocated slots
43  *
44  * This structure represents CAN FIFO queue. It is implemented as 
45  * a single linked list of slots prepared for processing. The empty slots
46  * are stored in single linked list (@flist).
47  */
48 struct canque_fifo_t {
49         unsigned long fifo_flags;
50         unsigned long error_code;
51         struct canque_slot_t *head;     /* points to the oldest entry */
52         struct canque_slot_t **tail;    /* points to NULL pointer for chaining */
53         struct canque_slot_t *flist;    /* points the first entry in the free list */
54         struct canque_slot_t *entry;    /* points to first allocated entry */
55         can_spinlock_t fifo_lock;       /* can_spin_lock_irqsave / can_spin_unlock_irqrestore */
56         int    slotsnr;
57 };
58
59 #define CAN_FIFOF_DESTROY_b     15
60 #define CAN_FIFOF_ERROR_b       14
61 #define CAN_FIFOF_ERR2BLOCK_b   13
62 #define CAN_FIFOF_BLOCK_b       12
63 #define CAN_FIFOF_OVERRUN_b     11
64 #define CAN_FIFOF_FULL_b        10
65 #define CAN_FIFOF_EMPTY_b       9
66 #define CAN_FIFOF_DEAD_b        8
67 #define CAN_FIFOF_INACTIVE_b    7
68 #define CAN_FIFOF_FREEONEMPTY_b 6
69 #define CAN_FIFOF_READY_b       5
70 #define CAN_FIFOF_NOTIFYPEND_b  4
71 #define CAN_FIFOF_RTL_MEM_b     3
72
73 #define CAN_FIFOF_DESTROY       (1<<CAN_FIFOF_DESTROY_b)
74 #define CAN_FIFOF_ERROR         (1<<CAN_FIFOF_ERROR_b)
75 #define CAN_FIFOF_ERR2BLOCK     (1<<CAN_FIFOF_ERR2BLOCK_b)
76 #define CAN_FIFOF_BLOCK         (1<<CAN_FIFOF_BLOCK_b)
77 #define CAN_FIFOF_OVERRUN       (1<<CAN_FIFOF_OVERRUN_b)
78 #define CAN_FIFOF_FULL          (1<<CAN_FIFOF_FULL_b)
79 #define CAN_FIFOF_EMPTY         (1<<CAN_FIFOF_EMPTY_b)
80 #define CAN_FIFOF_DEAD          (1<<CAN_FIFOF_DEAD_b)
81 #define CAN_FIFOF_INACTIVE      (1<<CAN_FIFOF_INACTIVE_b)
82 #define CAN_FIFOF_FREEONEMPTY   (1<<CAN_FIFOF_FREEONEMPTY_b)
83 #define CAN_FIFOF_READY         (1<<CAN_FIFOF_READY_b)
84 #define CAN_FIFOF_NOTIFYPEND    (1<<CAN_FIFOF_NOTIFYPEND_b)
85 #define CAN_FIFOF_RTL_MEM       (1<<CAN_FIFOF_RTL_MEM_b)
86
87 #define canque_fifo_test_fl(fifo,fifo_fl) \
88   test_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
89 #define canque_fifo_set_fl(fifo,fifo_fl) \
90   set_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
91 #define canque_fifo_clear_fl(fifo,fifo_fl) \
92   clear_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
93 #define canque_fifo_test_and_set_fl(fifo,fifo_fl) \
94   test_and_set_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
95 #define canque_fifo_test_and_clear_fl(fifo,fifo_fl) \
96   test_and_clear_bit(CAN_FIFOF_##fifo_fl##_b,&(fifo)->fifo_flags)
97
98
99 /**
100  * canque_fifo_get_inslot - allocate slot for the input of one CAN message 
101  * @fifo: pointer to the FIFO structure
102  * @slotp: pointer to location to store pointer to the allocated slot.
103  * @cmd: optional command associated with allocated slot.
104  *
105  * Return Value: The function returns negative value if there is no
106  *      free slot in the FIFO queue.
107  */
108 static inline
109 int canque_fifo_get_inslot(struct canque_fifo_t *fifo, struct canque_slot_t **slotp, int cmd)
110 {
111         can_spin_irqflags_t flags;
112         struct canque_slot_t *slot;
113         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
114         /* get the first free slot slot from flist */
115         if(!(slot=fifo->flist)) {
116                 canque_fifo_set_fl(fifo,OVERRUN);
117                 canque_fifo_set_fl(fifo,FULL);
118                 can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
119                 *slotp=NULL;
120                 return -1;
121         }
122         /* adjust free slot list */
123         if(!(fifo->flist=slot->next))
124                 canque_fifo_set_fl(fifo,FULL);
125         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
126         *slotp=slot;
127         slot->slot_flags=cmd&CAN_SLOTF_CMD;
128         return 1;
129 }
130
131 /**
132  * canque_fifo_put_inslot - releases slot to further processing
133  * @fifo: pointer to the FIFO structure
134  * @slot: pointer to the slot previously acquired by canque_fifo_get_inslot().
135  *
136  * Return Value: The nonzero return value indicates, that the queue was empty
137  *      before call to the function. The caller should wake-up output side of the queue.
138  */
139 static inline
140 int canque_fifo_put_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
141 {
142         int ret;
143         can_spin_irqflags_t flags;
144         slot->next=NULL;
145         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
146         if(*fifo->tail) can_printk(KERN_CRIT "canque_fifo_put_inslot: fifo->tail != NULL\n");
147         *fifo->tail=slot;
148         fifo->tail=&slot->next;
149         ret=0;
150         if(canque_fifo_test_and_clear_fl(fifo,EMPTY))
151           ret=CAN_FIFOF_EMPTY;  /* Fifo has been empty before put */
152         if(canque_fifo_test_and_clear_fl(fifo,INACTIVE))
153           ret=CAN_FIFOF_INACTIVE; /* Fifo has been empty before put */
154         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
155         return ret;
156 }
157
158 /**
159  * canque_fifo_abort_inslot - release and abort slot
160  * @fifo: pointer to the FIFO structure
161  * @slot: pointer to the slot previously acquired by canque_fifo_get_inslot().
162  *
163  * Return Value: The nonzero value indicates, that fifo was full
164  */
165 static inline
166 int canque_fifo_abort_inslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
167 {
168         int ret=0;
169         can_spin_irqflags_t flags;
170         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
171         slot->next=fifo->flist;
172         fifo->flist=slot;
173         if(canque_fifo_test_and_clear_fl(fifo,FULL))
174                 ret=CAN_FIFOF_FULL;
175         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
176         return ret;
177 }
178
179 /**
180  * canque_fifo_test_outslot - test and get ready slot from the FIFO
181  * @fifo: pointer to the FIFO structure
182  * @slotp: pointer to location to store pointer to the oldest slot from the FIFO.
183  *
184  * Return Value: The negative value indicates, that queue is empty.
185  *      The positive or zero value represents command stored into slot by
186  *      the call to the function canque_fifo_get_inslot().
187  *      The successfully acquired FIFO output slot has to be released by
188  *      the call canque_fifo_free_outslot() or canque_fifo_again_outslot().
189  */
190 static inline
191 int canque_fifo_test_outslot(struct canque_fifo_t *fifo, struct canque_slot_t **slotp)
192 {
193         can_spin_irqflags_t flags;
194         int cmd;
195         struct canque_slot_t *slot;
196         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
197         if(!(slot=fifo->head)){;
198                 canque_fifo_set_fl(fifo,EMPTY);
199                 can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
200                 *slotp=NULL;
201                 return -1;
202         }
203         if(!(fifo->head=slot->next))
204                 fifo->tail=&fifo->head;
205         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
206
207         *slotp=slot;
208         cmd=slot->slot_flags;
209         return cmd&CAN_SLOTF_CMD;
210 }
211
212
213 /**
214  * canque_fifo_free_outslot - free processed FIFO slot
215  * @fifo: pointer to the FIFO structure
216  * @slot: pointer to the slot previously acquired by canque_fifo_test_outslot().
217  *
218  * Return Value: The returned value informs about FIFO state change.
219  *      The mask %CAN_FIFOF_FULL indicates, that the FIFO was full before
220  *      the function call. The mask %CAN_FIFOF_EMPTY informs, that last ready slot
221  *      has been processed.
222  */
223 static inline
224 int canque_fifo_free_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
225 {
226         int ret=0;
227         can_spin_irqflags_t flags;
228         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
229         slot->next=fifo->flist;
230         fifo->flist=slot;
231         if(canque_fifo_test_and_clear_fl(fifo,FULL))
232                 ret=CAN_FIFOF_FULL;
233         if(!(fifo->head)){
234                 canque_fifo_set_fl(fifo,EMPTY);
235                 ret|=CAN_FIFOF_EMPTY;
236         }
237         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
238         return ret;
239 }
240
241 /**
242  * canque_fifo_again_outslot - interrupt and postpone processing of the slot
243  * @fifo: pointer to the FIFO structure
244  * @slot: pointer to the slot previously acquired by canque_fifo_test_outslot().
245  *
246  * Return Value: The function cannot fail..
247  */
248 static inline
249 int canque_fifo_again_outslot(struct canque_fifo_t *fifo, struct canque_slot_t *slot)
250 {
251         can_spin_irqflags_t flags;
252         can_spin_lock_irqsave(&fifo->fifo_lock, flags);
253         if(!(slot->next=fifo->head))
254                 fifo->tail=&slot->next;
255         fifo->head=slot;
256         can_spin_unlock_irqrestore(&fifo->fifo_lock, flags);
257         return 1;
258 }
259
260 int canque_fifo_flush_slots(struct canque_fifo_t *fifo);
261
262 int canque_fifo_init_slots(struct canque_fifo_t *fifo);
263
264 #define CANQUEUE_PRIO_NR  3
265
266 /* Forward declarations for external types */
267 struct msgobj_t;
268 struct chip_t;
269
270 /**
271  * struct canque_edge_t - CAN message delivery subsystem graph edge
272  * @fifo: place where primitive @struct canque_fifo_t FIFO is located.
273  * @filtid: the possible CAN message identifiers filter.
274  * @filtmask: the filter mask, the comparison considers only
275  *      @filtid bits corresponding to set bits in the @filtmask field.
276  * @inpeers: the lists of all peers FIFOs connected by their
277  *      input side (@inends) to the same terminal (@struct canque_ends_t).
278  * @outpeers: the lists of all peers FIFOs connected by their
279  *      output side (@outends) to the same terminal (@struct canque_ends_t).
280  * @activepeers: the lists of peers FIFOs connected by their
281  *      output side (@outends) to the same terminal (@struct canque_ends_t)
282  *      with same priority and active state.
283  * @inends: the pointer to the FIFO input side terminal (@struct canque_ends_t).
284  * @outends: the pointer to the FIFO output side terminal (@struct canque_ends_t).
285  * @edge_used: the atomic usage counter, mainly used for safe destruction of the edge.
286  * @edge_prio: the assigned queue priority from the range 0 to %CANQUEUE_PRIO_NR-1
287  * @edge_num: edge sequential number intended for debugging purposes only
288  * @pending_peers: edges with pending delayed events (RTL->Linux calls)
289  * @pending_inops: bitmask of pending operations
290  * @pending_outops: bitmask of pending operations
291  *
292  * This structure represents one direction connection from messages source 
293  * (@inends) to message consumer (@outends) fifo ends hub. The edge contains
294  * &struct canque_fifo_t for message fifo implementation.
295  */
296 struct canque_edge_t {
297         struct canque_fifo_t fifo;
298         unsigned long filtid;
299         unsigned long filtmask;
300         struct list_head inpeers;
301         struct list_head outpeers;
302         struct list_head activepeers;
303         struct canque_ends_t *inends;
304         struct canque_ends_t *outends;
305         atomic_t edge_used;
306         int edge_prio;
307         int edge_num;
308     #ifdef CAN_WITH_RTL
309         struct list_head pending_peers;
310         unsigned long pending_inops;
311         unsigned long pending_outops;
312     #endif /*CAN_WITH_RTL*/
313 };
314
315 /**
316  * struct canque_ends_t - CAN message delivery subsystem graph vertex (FIFO ends)
317  * @ends_flags: this field holds flags describing state of the ENDS structure.
318  * @active: the array of the lists of active edges directed to the ends structure
319  *      with ready messages. The array is indexed by the edges priorities. 
320  * @idle: the list of the edges directed to the ends structure with empty FIFOs.
321  * @inlist: the list of outgoing edges input sides.
322  * @ends_lock: the lock synchronizing operations between threads accessing
323  *      same ends structure.
324  * @notify: pointer to notify procedure. The next state changes are notified.
325  *      %CANQUEUE_NOTIFY_EMPTY (out->in call) - all slots are processed by FIFO out side. 
326  *      %CANQUEUE_NOTIFY_SPACE (out->in call) - full state negated => there is space for new message.
327  *      %CANQUEUE_NOTIFY_PROC  (in->out call) - empty state negated => out side is requested to process slots.
328  *      %CANQUEUE_NOTIFY_NOUSR (both) - notify, that the last user has released the edge usage
329  *              called with some lock to prevent edge disappear.
330  *      %CANQUEUE_NOTIFY_DEAD  (both) - edge is in progress of deletion.
331  *      %CANQUEUE_NOTIFY_ATACH (both) - new edge has been attached to end.
332  *      %CANQUEUE_NOTIFY_FILTCH (out->in call) - edge filter rules changed
333  *      %CANQUEUE_NOTIFY_ERROR  (out->in call) - error in messages processing.
334  * @context: space to store ends user specific information
335  * @endinfo: space to store some other ends usage specific informations
336  *      mainly for waking-up by the notify calls.
337  * @dead_peers: used to chain ends wanting for postponed destruction
338  *
339  * Structure represents place to connect edges to for CAN communication entity.
340  * The zero, one or more incoming and outgoing edges can be connected to
341  * this structure.
342  */
343 struct canque_ends_t {
344         unsigned long ends_flags;
345         struct list_head active[CANQUEUE_PRIO_NR];
346         struct list_head idle;
347         struct list_head inlist;
348         struct list_head outlist;
349         can_spinlock_t ends_lock;       /* can_spin_lock_irqsave / can_spin_unlock_irqrestore */
350         void (*notify)(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what);
351         void *context;
352         union {
353                 struct {
354                         wait_queue_head_t readq;
355                         wait_queue_head_t writeq;
356                         wait_queue_head_t emptyq;
357                     #ifdef CAN_ENABLE_KERN_FASYNC
358                         struct fasync_struct *fasync;
359                     #endif /*CAN_ENABLE_KERN_FASYNC*/
360                 } fileinfo;
361             #ifdef CAN_WITH_RTL
362                 struct {
363                         rtl_spinlock_t rtl_lock;
364                         rtl_wait_t rtl_readq;
365                         atomic_t   rtl_readq_age;
366                         rtl_wait_t rtl_writeq;
367                         atomic_t   rtl_writeq_age;
368                         rtl_wait_t rtl_emptyq;
369                         atomic_t   rtl_emptyq_age;
370                         unsigned long pend_flags;
371                 } rtlinfo;
372             #endif /*CAN_WITH_RTL*/
373                 struct {
374                         struct msgobj_t *msgobj;
375                         struct chip_t *chip;
376                     #ifndef CAN_WITH_RTL
377                         wait_queue_head_t daemonq;
378                     #else /*CAN_WITH_RTL*/
379                         pthread_t worker_thread;
380                     #endif /*CAN_WITH_RTL*/
381                 } chipinfo;
382         } endinfo;
383         struct list_head dead_peers;
384 };
385
386 #define CANQUEUE_NOTIFY_EMPTY  1 /* out -> in - all slots are processed by FIFO out side */
387 #define CANQUEUE_NOTIFY_SPACE  2 /* out -> in - full state negated => there is space for new message */
388 #define CANQUEUE_NOTIFY_PROC   3 /* in -> out - empty state negated => out side is requested to process slots */
389 #define CANQUEUE_NOTIFY_NOUSR  4 /* called with some lock to prevent edge disappear */
390 #define CANQUEUE_NOTIFY_DEAD   5 /*  */
391 #define CANQUEUE_NOTIFY_DEAD_WANTED 6 /*  */
392 #define CANQUEUE_NOTIFY_ATTACH 7 /*  */
393 #define CANQUEUE_NOTIFY_FILTCH 8 /* filter changed */
394 #define CANQUEUE_NOTIFY_ERROR      0x10000 /* error notifiers */
395 #define CANQUEUE_NOTIFY_ERRTX_PREP 0x11001 /* tx preparation error */
396 #define CANQUEUE_NOTIFY_ERRTX_SEND 0x11002 /* tx send error */
397 #define CANQUEUE_NOTIFY_ERRTX_BUS  0x11003 /* tx bus error */
398
399 #define CAN_ENDSF_DEAD    (1<<0)
400 #define CAN_ENDSF_MEM_RTL (1<<1)
401
402 /**
403  * canque_notify_inends - request to send notification to the input ends
404  * @qedge: pointer to the edge structure
405  * @what: notification type
406  */
407 static inline
408 void canque_notify_inends(struct canque_edge_t *qedge, int what)
409 {
410         if(qedge->inends)
411                 if(qedge->inends->notify)
412                         qedge->inends->notify(qedge->inends,qedge,what);
413 }
414
415 /**
416  * canque_notify_outends - request to send notification to the output ends
417  * @qedge: pointer to the edge structure
418  * @what: notification type
419  */
420 static inline
421 void canque_notify_outends(struct canque_edge_t *qedge, int what)
422 {
423         if(qedge->outends)
424                 if(qedge->outends->notify)
425                         qedge->outends->notify(qedge->outends,qedge,what);
426 }
427
428 /**
429  * canque_notify_bothends - request to send notification to the both ends
430  * @qedge: pointer to the edge structure
431  * @what: notification type
432  */
433 static inline
434 void canque_notify_bothends(struct canque_edge_t *qedge, int what)
435 {
436         canque_notify_inends(qedge, what);
437         canque_notify_outends(qedge, what);
438 }
439
440 /**
441  * canque_activate_edge - mark output end of the edge as active
442  * @qedge: pointer to the edge structure
443  * @inends: input side of the edge
444  *
445  * Function call moves output side of the edge from idle onto active edges
446  * list.
447  */
448 static inline
449 void canque_activate_edge(struct canque_ends_t *inends, struct canque_edge_t *qedge)
450 {
451         can_spin_irqflags_t flags;
452         struct canque_ends_t *outends;
453         if(qedge->edge_prio>=CANQUEUE_PRIO_NR)
454                 qedge->edge_prio=CANQUEUE_PRIO_NR-1;
455         can_spin_lock_irqsave(&inends->ends_lock, flags);
456         if((outends=qedge->outends)){
457                 can_spin_lock(&outends->ends_lock);
458                 can_spin_lock(&qedge->fifo.fifo_lock);
459                 if(!canque_fifo_test_fl(&qedge->fifo,EMPTY)){
460                         list_del(&qedge->activepeers);
461                         list_add_tail(&qedge->activepeers,&outends->active[qedge->edge_prio]);
462                 }
463                 can_spin_unlock(&qedge->fifo.fifo_lock);
464                 can_spin_unlock(&outends->ends_lock);
465
466         }
467         can_spin_unlock_irqrestore(&inends->ends_lock, flags);
468 }
469
470 /**
471  * canque_filtid2internal - converts message ID and filter flags into internal format
472  * @id: CAN message 11 or 29 bit identifier
473  * @filtflags: CAN message flags
474  *
475  * This function maps message ID and %MSG_RTR, %MSG_EXT and %MSG_LOCAL into one 32 bit number
476  */
477 static inline
478 unsigned int canque_filtid2internal(unsigned long id, int filtflags)
479 {
480         filtflags &= MSG_RTR|MSG_EXT|MSG_LOCAL;
481         filtflags += filtflags&MSG_RTR;
482         return (id&MSG_ID_MASK) | (filtflags<<28);
483 }
484
485 int canque_get_inslot(struct canque_ends_t *qends,
486         struct canque_edge_t **qedgep, struct canque_slot_t **slotp, int cmd);
487         
488 int canque_get_inslot4id(struct canque_ends_t *qends,
489         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
490         int cmd, unsigned long id, int prio);
491         
492 int canque_put_inslot(struct canque_ends_t *qends,
493         struct canque_edge_t *qedge, struct canque_slot_t *slot);
494
495 int canque_abort_inslot(struct canque_ends_t *qends,
496         struct canque_edge_t *qedge, struct canque_slot_t *slot);
497
498 int canque_filter_msg2edges(struct canque_ends_t *qends, struct canmsg_t *msg);
499
500 int canque_test_outslot(struct canque_ends_t *qends,
501         struct canque_edge_t **qedgep, struct canque_slot_t **slotp);
502
503 int canque_free_outslot(struct canque_ends_t *qends,
504         struct canque_edge_t *qedge, struct canque_slot_t *slot);
505
506 int canque_again_outslot(struct canque_ends_t *qends,
507         struct canque_edge_t *qedge, struct canque_slot_t *slot);
508
509 int canque_set_filt(struct canque_edge_t *qedge,
510         unsigned long filtid, unsigned long filtmask, int flags);
511         
512 int canque_flush(struct canque_edge_t *qedge);
513
514 int canqueue_disconnect_edge(struct canque_edge_t *qedge);
515
516 int canqueue_connect_edge(struct canque_edge_t *qedge, struct canque_ends_t *inends, struct canque_ends_t *outends);
517
518 int canqueue_ends_init_gen(struct canque_ends_t *qends);
519
520 void canqueue_block_inlist(struct canque_ends_t *qends);
521
522 void canqueue_block_outlist(struct canque_ends_t *qends);
523
524 int canqueue_ends_kill_inlist(struct canque_ends_t *qends, int send_rest);
525
526 int canqueue_ends_kill_outlist(struct canque_ends_t *qends);
527
528 /* edge reference and traversal functions */
529
530 void canque_edge_do_dead(struct canque_edge_t *edge, int dead_fl);
531
532 static inline
533 void canque_edge_incref(struct canque_edge_t *edge)
534 {
535         atomic_inc(&edge->edge_used);
536 }
537
538 static inline
539 void canque_edge_decref(struct canque_edge_t *edge)
540 {
541         can_spin_irqflags_t flags;
542         struct canque_ends_t *inends=edge->inends;
543         struct canque_ends_t *outends=edge->outends;
544         int dead_fl;
545         
546         can_spin_lock_irqsave(&inends->ends_lock, flags);
547         can_spin_lock(&outends->ends_lock);
548         if(atomic_dec_and_test(&edge->edge_used)) {
549                 dead_fl=canque_fifo_test_and_set_fl(&edge->fifo,DEAD);
550                 /* Because of former evolution of edge references 
551                    management notify of CANQUEUE_NOTIFY_NOUSR could
552                    be moved to canque_edge_do_dead :-) */
553                 can_spin_unlock(&outends->ends_lock);
554                 can_spin_unlock_irqrestore(&inends->ends_lock, flags);
555                 canque_edge_do_dead(edge, dead_fl);
556         } else {
557                 can_spin_unlock(&outends->ends_lock);
558                 can_spin_unlock_irqrestore(&inends->ends_lock, flags);
559         }
560 }
561
562 static inline
563 struct canque_edge_t *canque_first_inedge(struct canque_ends_t *qends)
564 {
565         can_spin_irqflags_t flags;
566         struct list_head *entry;
567         struct canque_edge_t *edge;
568         
569         can_spin_lock_irqsave(&qends->ends_lock, flags);
570         entry=qends->inlist.next;
571     skip_dead:
572         if(entry != &qends->inlist) {
573                 edge=list_entry(entry,struct canque_edge_t,inpeers);
574                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
575                         entry=entry->next;
576                         goto skip_dead;
577                 }
578                 canque_edge_incref(edge);
579         } else {
580                 edge=NULL;
581         }
582         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
583         return edge;
584 }
585
586
587 static inline
588 struct canque_edge_t *canque_next_inedge(struct canque_ends_t *qends, struct canque_edge_t *edge)
589 {
590         can_spin_irqflags_t flags;
591         struct list_head *entry;
592         struct canque_edge_t *next;
593         
594         can_spin_lock_irqsave(&qends->ends_lock, flags);
595         entry=edge->inpeers.next;
596     skip_dead:
597         if(entry != &qends->inlist) {
598                 next=list_entry(entry,struct canque_edge_t,inpeers);
599                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
600                         entry=entry->next;
601                         goto skip_dead;
602                 }
603                 canque_edge_incref(next);
604         } else {
605                 next=NULL;
606         }
607         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
608         canque_edge_decref(edge);
609         return next;
610 }
611
612 #define canque_for_each_inedge(qends, edge) \
613             for(edge=canque_first_inedge(qends);edge;edge=canque_next_inedge(qends, edge))
614
615 static inline
616 struct canque_edge_t *canque_first_outedge(struct canque_ends_t *qends)
617 {
618         can_spin_irqflags_t flags;
619         struct list_head *entry;
620         struct canque_edge_t *edge;
621         
622         can_spin_lock_irqsave(&qends->ends_lock, flags);
623         entry=qends->outlist.next;
624     skip_dead:
625         if(entry != &qends->outlist) {
626                 edge=list_entry(entry,struct canque_edge_t,outpeers);
627                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
628                         entry=entry->next;
629                         goto skip_dead;
630                 }
631                 canque_edge_incref(edge);
632         } else {
633                 edge=NULL;
634         }
635         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
636         return edge;
637 }
638
639
640 static inline
641 struct canque_edge_t *canque_next_outedge(struct canque_ends_t *qends, struct canque_edge_t *edge)
642 {
643         can_spin_irqflags_t flags;
644         struct list_head *entry;
645         struct canque_edge_t *next;
646         
647         can_spin_lock_irqsave(&qends->ends_lock, flags);
648         entry=edge->outpeers.next;
649     skip_dead:
650         if(entry != &qends->outlist) {
651                 next=list_entry(entry,struct canque_edge_t,outpeers);
652                 if(canque_fifo_test_fl(&edge->fifo,DEAD)) {
653                         entry=entry->next;
654                         goto skip_dead;
655                 }
656                 canque_edge_incref(next);
657         } else {
658                 next=NULL;
659         }
660         can_spin_unlock_irqrestore(&qends->ends_lock, flags);
661         canque_edge_decref(edge);
662         return next;
663 }
664
665 #define canque_for_each_outedge(qends, edge) \
666             for(edge=canque_first_outedge(qends);edge;edge=canque_next_outedge(qends, edge))
667
668 /* Linux kernel specific functions */
669
670 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr);
671
672 int canque_fifo_done_kern(struct canque_fifo_t *fifo);
673
674 struct canque_edge_t *canque_new_edge_kern(int slotsnr);
675
676 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
677         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
678         int cmd, unsigned long id, int prio);
679
680 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
681         struct canque_edge_t **qedgep, struct canque_slot_t **slotp);
682
683 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge);
684
685 int canqueue_ends_init_kern(struct canque_ends_t *qends);
686
687 int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync);
688
689 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends);
690
691 void canqueue_kern_initialize(void);
692
693 #ifdef CAN_WITH_RTL
694
695 extern struct tasklet_struct canque_dead_tl;    /*publication required only for RTL*/
696
697 /* RT-Linux specific functions and variables */
698
699 extern int canqueue_rtl_irq;
700
701 extern unsigned long canqueue_rtl2lin_pend;
702
703 #define CAN_RTL2LIN_PEND_DEAD_b 0
704
705 void canqueue_rtl_initialize(void);
706 void canqueue_rtl_done(void);
707
708 int canqueue_rtl2lin_check_and_pend(struct canque_ends_t *qends,
709                          struct canque_edge_t *qedge, int what);
710
711 struct canque_edge_t *canque_new_edge_rtl(int slotsnr);
712
713 void canque_dispose_edge_rtl(struct canque_edge_t *qedge);
714
715 int canque_get_inslot4id_wait_rtl(struct canque_ends_t *qends,
716         struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
717         int cmd, unsigned long id, int prio);
718
719 int canque_get_outslot_wait_rtl(struct canque_ends_t *qends,
720         struct canque_edge_t **qedgep, struct canque_slot_t **slotp);
721
722 int canque_sync_wait_rtl(struct canque_ends_t *qends, struct canque_edge_t *qedge);
723
724 void canque_ends_free_rtl(struct canque_ends_t *qends);
725
726 int canqueue_ends_init_rtl(struct canque_ends_t *qends);
727
728 int canqueue_ends_dispose_rtl(struct canque_ends_t *qends, int sync);
729
730 #else /*CAN_WITH_RTL*/
731
732 static inline int canqueue_rtl2lin_check_and_pend(struct canque_ends_t *qends,
733                         struct canque_edge_t *qedge, int what) { return 0; }
734
735 #endif /*CAN_WITH_RTL*/
736
737
738 #endif /*_CAN_QUEUE_H*/