1 /**************************************************************************/
2 /* File: can_quesysless.c - CAN queue functions for embedded target */
4 /* LinCAN - (Not only) Linux CAN bus driver */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz> */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz> */
7 /* Funded by OCERA and FRESCOR IST projects */
9 /* LinCAN is free software; you can redistribute it and/or modify it */
10 /* under terms of the GNU General Public License as published by the */
11 /* Free Software Foundation; either version 2, or (at your option) any */
12 /* later version. LinCAN is distributed in the hope that it will be */
13 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */
14 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
15 /* General Public License for more details. You should have received a */
16 /* copy of the GNU General Public License along with LinCAN; see file */
17 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */
18 /* Cambridge, MA 02139, USA. */
20 /* To allow use of LinCAN in the compact embedded systems firmware */
21 /* and RT-executives (RTEMS for example), main authors agree with next */
22 /* special exception: */
24 /* Including LinCAN header files in a file, instantiating LinCAN generics */
25 /* or templates, or linking other files with LinCAN objects to produce */
26 /* an application image/executable, does not by itself cause the */
27 /* resulting application image/executable to be covered by */
28 /* the GNU General Public License. */
29 /* This exception does not however invalidate any other reasons */
30 /* why the executable file might be covered by the GNU Public License. */
31 /* Publication of enhanced or derived LinCAN files is required although. */
32 /**************************************************************************/
34 #include "./can/can.h"
35 #include "./can/can_sysdep.h"
36 #include "./can/can_queue.h"
40 extern atomic_t edge_num_cnt;
43 #define DEBUGQUE(fmt,args...) can_printk(KERN_ERR "can_quesysless (debug): " fmt,\
47 #define DEBUGQUE(fmt,args...)
50 #define ERRMSGQUE(fmt,args...) can_printk(KERN_ERR "can_quesysless: " fmt,\
55 * Modifies Tx message processing
56 * 0 .. local message processing disabled
57 * 1 .. local messages disabled by default but can be enabled by canque_set_filt
58 * 2 .. local messages enabled by default, can be disabled by canque_set_filt
60 extern int processlocal;
62 void canque_dead_func(unsigned long data);
64 /* Support for dead ends structures left after client close */
65 can_spinlock_t canque_dead_func_lock;
66 LIST_HEAD(canque_dead_ends);
67 /* retrieved by list_entry(canque_dead_ends.next,struct canque_ends_t,dead_peers) */
68 LIST_HEAD(canque_dead_edges);
69 /* retrieved by list_entry(canque_dead_edges.next,struct canque_edge_t,inpeers) */
70 // DECLARE_TASKLET(canque_dead_tl, canque_dead_func, 0);
71 /* activated by tasklet_schedule(&canque_dead_tl) */
75 struct canque_edge_t *canque_dead_edges_cut_first(void)
77 can_spin_irqflags_t flags;
78 struct canque_edge_t *edge;
79 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
80 if(list_empty(&canque_dead_edges))
83 edge=list_entry(canque_dead_edges.next,struct canque_edge_t,inpeers);
84 list_del(&edge->inpeers);
86 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
90 void canque_dead_func(unsigned long data)
92 can_spin_irqflags_t flags;
93 struct canque_edge_t *qedge;
94 struct canque_ends_t *qends;
95 struct list_head *entry;
97 while((qedge=canque_dead_edges_cut_first())){
98 DEBUGQUE("edge %d disposed\n",qedge->edge_num);
100 if(canque_fifo_test_fl(&qedge->fifo,RTL_MEM)){
101 canque_dispose_edge_rtl(qedge);
104 #endif /*CAN_WITH_RTL*/
105 canque_fifo_done_kern(&qedge->fifo);
109 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
110 entry=canque_dead_ends.next;
111 while(entry!=&canque_dead_ends){
112 qends=list_entry(entry,struct canque_ends_t,dead_peers);
114 if(!list_empty(&qends->inlist))
116 if(!list_empty(&qends->outlist))
118 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
119 list_del(&qends->dead_peers);
120 can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
121 DEBUGQUE("ends structure disposed\n");
123 if(qends->ends_flags&CAN_ENDSF_MEM_RTL){
124 canque_ends_free_rtl(qends);
127 #endif /*CAN_WITH_RTL*/
130 can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
133 // static inline void canque_dead_tasklet_schedule(void)
135 // #ifdef CAN_WITH_RTL
136 // if(!rtl_rt_system_is_idle()){
137 // set_bit(CAN_RTL2LIN_PEND_DEAD_b,&canqueue_rtl2lin_pend);
138 // rtl_global_pend_irq (canqueue_rtl_irq);
141 // #endif /*CAN_WITH_RTL*/
143 // tasklet_schedule(&canque_dead_tl);
147 void canque_edge_do_dead(struct canque_edge_t *edge)
149 can_spin_irqflags_t flags;
151 canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
153 /* The problem of the above call is, that in RT-Linux to Linux notify
154 case is edge scheduled for delayed notify delivery, this needs
155 to be reflected there */
156 if(atomic_read(&edge->edge_used)>0){
157 can_spin_lock_irqsave(&edge->inends->ends_lock, flags);
158 can_spin_lock(&edge->outends->ends_lock);
159 if(atomic_read(&edge->edge_used)>0){
160 /* left edge to live for a while, banshee comes again in a while */
161 canque_fifo_clear_fl(&edge->fifo,DEAD);
162 can_spin_unlock(&edge->outends->ends_lock);
163 can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
164 can_printk(KERN_ERR "can_quertl (debug): canque_edge_do_dead postponed\n");
167 can_spin_unlock(&edge->outends->ends_lock);
168 can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
170 #endif /*CAN_WITH_RTL*/
172 if(canqueue_disconnect_edge(edge)<0){
173 ERRMSGQUE("canque_edge_do_dead: canqueue_disconnect_edge failed !!!\n");
177 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
178 list_add(&edge->inpeers,&canque_dead_edges);
179 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
180 // canque_dead_tasklet_schedule();
186 /*if(qends->ends_flags & CAN_ENDSF_DEAD){
187 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
188 list_del(&qends->dead_peers);
189 list_add(&qends->dead_peers,&canque_dead_ends);
190 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
191 tasklet_schedule(&canque_dead_tl);
196 * canqueue_notify_kern - notification callback handler for Linux userspace clients
197 * @qends: pointer to the callback side ends structure
198 * @qedge: edge which invoked notification
199 * @what: notification type
201 * The notification event is handled directly by call of this function except case,
202 * when called from RT-Linux context in mixed mode Linux/RT-Linux compilation.
203 * It is not possible to directly call Linux kernel synchronization primitives
204 * in such case. The notification request is postponed and signaled by @pending_inops flags
205 * by call canqueue_rtl2lin_check_and_pend() function.
206 * The edge reference count is increased until until all pending notifications are processed.
208 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
210 DEBUGQUE("canqueue_notify_kern for edge %d, use %d and event %d\n",
211 qedge->edge_num,(int)atomic_read(&qedge->edge_used),what);
213 /* delay event delivery for RT-Linux -> kernel notifications */
214 if(canqueue_rtl2lin_check_and_pend(qends,qedge,what)){
215 DEBUGQUE("canqueue_notify_kern postponed\n");
220 case CANQUEUE_NOTIFY_EMPTY:
221 // wake_up(&qends->endinfo.fileinfo.emptyq);
222 if(canque_fifo_test_and_clear_fl(&qedge->fifo, FREEONEMPTY))
223 canque_edge_decref(qedge);
225 case CANQUEUE_NOTIFY_SPACE:
226 // wake_up(&qends->endinfo.fileinfo.writeq);
227 #ifdef CAN_ENABLE_KERN_FASYNC
228 /* Asynchronous I/O processing */
229 kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_OUT);
230 #endif /*CAN_ENABLE_KERN_FASYNC*/
232 case CANQUEUE_NOTIFY_PROC:
233 // wake_up(&qends->endinfo.fileinfo.readq);
234 #ifdef CAN_ENABLE_KERN_FASYNC
235 /* Asynchronous I/O processing */
236 kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_IN);
237 #endif /*CAN_ENABLE_KERN_FASYNC*/
239 case CANQUEUE_NOTIFY_NOUSR:
240 // wake_up(&qends->endinfo.fileinfo.readq);
241 // wake_up(&qends->endinfo.fileinfo.writeq);
242 // wake_up(&qends->endinfo.fileinfo.emptyq);
244 case CANQUEUE_NOTIFY_DEAD_WANTED:
245 case CANQUEUE_NOTIFY_DEAD:
246 if(canque_fifo_test_and_clear_fl(&qedge->fifo, READY))
247 canque_edge_decref(qedge);
249 case CANQUEUE_NOTIFY_ATTACH:
255 * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
256 * @qends: pointer to the callback side ends structure
258 int canqueue_ends_init_kern(struct canque_ends_t *qends)
260 canqueue_ends_init_gen(qends);
262 init_waitqueue_head(&qends->endinfo.fileinfo.readq);
263 init_waitqueue_head(&qends->endinfo.fileinfo.writeq);
264 init_waitqueue_head(&qends->endinfo.fileinfo.emptyq);
265 #ifdef CAN_ENABLE_KERN_FASYNC
266 qends->endinfo.fileinfo.fasync=NULL;
267 #endif /*CAN_ENABLE_KERN_FASYNC*/
269 qends->notify=canqueue_notify_kern;
270 DEBUGQUE("canqueue_ends_init_kern\n");
276 * canque_get_inslot4id_wait_kern - find or wait for best outgoing edge and slot for given ID
277 * @qends: ends structure belonging to calling communication object
278 * @qedgep: place to store pointer to found edge
279 * @slotp: place to store pointer to allocated slot
280 * @cmd: command type for slot
281 * @id: communication ID of message to send into edge
282 * @prio: optional priority of message
284 * Same as canque_get_inslot4id(), except, that it waits for free slot
285 * in case, that queue is full. Function is specific for Linux userspace clients.
286 * Return Value: If there is no usable edge negative value is returned.
288 // int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
289 // struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
290 // int cmd, unsigned long id, int prio)
293 // DEBUGQUE("canque_get_inslot4id_wait_kern for cmd %d, id %ld, prio %d\n",cmd,id,prio);
294 // wait_event_interruptible((qends->endinfo.fileinfo.writeq),
295 // (ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))!=-1);
300 * canque_get_outslot_wait_kern - receive or wait for ready slot for given ends
301 * @qends: ends structure belonging to calling communication object
302 * @qedgep: place to store pointer to found edge
303 * @slotp: place to store pointer to received slot
305 * The same as canque_test_outslot(), except it waits in the case, that there is
306 * no ready slot for given ends. Function is specific for Linux userspace clients.
307 * Return Value: Negative value informs, that there is no ready output
308 * slot for given ends. Positive value is equal to the command
309 * slot has been allocated by the input side.
311 // int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
312 // struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
315 // DEBUGQUE("canque_get_outslot_wait_kern\n");
316 // wait_event_interruptible((qends->endinfo.fileinfo.readq),
317 // (ret=canque_test_outslot(qends,qedgep,slotp))!=-1);
322 * canque_sync_wait_kern - wait for all slots processing
323 * @qends: ends structure belonging to calling communication object
324 * @qedge: pointer to edge
326 * Functions waits for ends transition into empty state.
327 * Return Value: Positive value indicates, that edge empty state has been reached.
328 * Negative or zero value informs about interrupted wait or other problem.
330 // int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
333 // DEBUGQUE("canque_sync_wait_kern\n");
334 // wait_event_interruptible((qends->endinfo.fileinfo.emptyq),
335 // (ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0));
341 * canque_fifo_init_kern - initialize one CAN FIFO
342 * @fifo: pointer to the FIFO structure
343 * @slotsnr: number of requested slots
345 * Return Value: The negative value indicates, that there is no memory
346 * to allocate space for the requested number of the slots.
348 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr)
351 if(!slotsnr) slotsnr=MAX_BUF_LENGTH;
352 size=sizeof(struct canque_slot_t)*slotsnr;
353 fifo->entry=malloc(size);
354 if(!fifo->entry) return -1;
355 fifo->slotsnr=slotsnr;
356 return canque_fifo_init_slots(fifo);
360 * canque_fifo_done_kern - frees slots allocated for CAN FIFO
361 * @fifo: pointer to the FIFO structure
363 int canque_fifo_done_kern(struct canque_fifo_t *fifo)
373 * canque_new_edge_kern - allocate new edge structure in the Linux kernel context
374 * @slotsnr: required number of slots in the newly allocated edge structure
376 * Return Value: Returns pointer to allocated slot structure or %NULL if
377 * there is not enough memory to process operation.
379 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
381 struct canque_edge_t *qedge;
382 qedge = (struct canque_edge_t *)malloc(sizeof(struct canque_edge_t));
383 if(qedge == NULL) return NULL;
385 memset(qedge,0,sizeof(struct canque_edge_t));
386 can_spin_lock_init(&qedge->fifo.fifo_lock);
387 if(canque_fifo_init_kern(&qedge->fifo, slotsnr)<0){
389 DEBUGQUE("canque_new_edge_kern failed\n");
392 atomic_set(&qedge->edge_used,1);
394 qedge->filtmask = canque_filtid2internal(0l, (processlocal<2)? MSG_LOCAL:0);
395 qedge->edge_prio = 0;
397 /* not exactly clean, but enough for debugging */
398 atomic_inc(&edge_num_cnt);
399 qedge->edge_num=atomic_read(&edge_num_cnt);
400 #endif /* CAN_DEBUG */
401 DEBUGQUE("canque_new_edge_kern %d\n",qedge->edge_num);
405 #ifdef USE_SYNC_DISCONNECT_EDGE_KERN
407 /*not included in doc
408 * canqueue_disconnect_edge_kern - disconnect edge from communicating entities with wait
409 * @qends: ends structure belonging to calling communication object
410 * @qedge: pointer to edge
412 * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
414 * Return Value: Negative value means, that edge is used and cannot
415 * be disconnected yet. Operation has to be delayed.
417 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
419 canque_fifo_set_fl(&qedge->fifo,BLOCK);
420 DEBUGQUE("canqueue_disconnect_edge_kern %d called\n",qedge->edge_num);
421 if(!canque_fifo_test_and_set_fl(&qedge->fifo,DEAD)){
422 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_DEAD);
424 if(atomic_read(&qedge->edge_used)>0)
425 atomic_dec(&qedge->edge_used);
427 DEBUGQUE("canqueue_disconnect_edge_kern %d waiting\n",qedge->edge_num);
428 wait_event((qends->endinfo.fileinfo.emptyq),
429 (canqueue_disconnect_edge(qedge)>=0));
431 /*set_current_state(TASK_UNINTERRUPTIBLE);*/
432 /*schedule_timeout(HZ);*/
435 DEBUGQUE("canqueue_disconnect_edge_kern cannot set DEAD\n");
441 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
443 struct canque_edge_t *edge;
444 can_spin_irqflags_t flags;
446 can_spin_lock_irqsave(&qends->ends_lock,flags);
447 if(list_empty(list)){
448 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
451 if(list == &qends->inlist)
452 edge=list_entry(list->next,struct canque_edge_t,inpeers);
454 edge=list_entry(list->next,struct canque_edge_t,outpeers);
455 atomic_inc(&edge->edge_used);
456 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
457 if(canqueue_disconnect_edge_kern(qends, edge)>=0) {
458 /* Free edge memory */
459 canque_fifo_done_kern(&edge->fifo);
462 canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
463 canque_edge_decref(edge);
464 DEBUGQUE("canqueue_disconnect_list_kern in troubles\n");
465 DEBUGQUE("the edge %d has usage count %d and flags %ld\n",edge->edge_num,atomic_read(&edge->edge_used),edge->fifo.fifo_flags);
471 #endif /*USE_SYNC_DISCONNECT_EDGE_KERN*/
474 // int canqueue_ends_sync_all_kern(struct canque_ends_t *qends)
476 // struct canque_edge_t *qedge;
478 // canque_for_each_inedge(qends, qedge){
479 // DEBUGQUE("canque_sync_wait_kern called for edge %d\n",qedge->edge_num);
480 // canque_sync_wait_kern(qends, qedge);
486 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends)
488 can_spin_irqflags_t flags;
490 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
491 qends->ends_flags |= CAN_ENDSF_DEAD;
492 list_add(&qends->dead_peers,&canque_dead_ends);
493 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
495 // canque_dead_tasklet_schedule();
500 * canqueue_ends_dispose_kern - finalizing of the ends structure for Linux kernel clients
501 * @qends: pointer to ends structure
502 * @sync: flag indicating, that user wants to wait for processing of all remaining
505 * Return Value: Function should be designed such way to not fail.
507 // int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync)
511 // DEBUGQUE("canqueue_ends_dispose_kern\n");
512 // canqueue_block_inlist(qends);
513 // canqueue_block_outlist(qends);
515 // /*Wait for sending of all pending messages in the output FIFOs*/
517 // canqueue_ends_sync_all_kern(qends);
519 // /* Finish or kill all outgoing edges listed in inends */
520 // delayed=canqueue_ends_kill_inlist(qends, 1);
521 // /* Kill all incoming edges listed in outends */
522 // delayed|=canqueue_ends_kill_outlist(qends);
524 // // wake_up(&qends->endinfo.fileinfo.readq);
525 // // wake_up(&qends->endinfo.fileinfo.writeq);
526 // // wake_up(&qends->endinfo.fileinfo.emptyq);
529 // canqueue_ends_dispose_postpone(qends);
531 // DEBUGQUE("canqueue_ends_dispose_kern delayed\n");
536 // DEBUGQUE("canqueue_ends_dispose_kern finished\n");
540 void canqueue_kern_initialize()
542 can_spin_lock_init(&canque_dead_func_lock);