1 /**************************************************************************/
2 /* File: can_quekern.c - CAN message queues functions for the Linux kernel*/
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 "../include/can.h"
35 #include "../include/can_sysdep.h"
36 #include "../include/can_queue.h"
40 extern atomic_t edge_num_cnt;
43 #define DEBUGQUE(fmt,args...) can_printk(KERN_ERR "can_quekern (debug): " fmt,\
47 #define DEBUGQUE(fmt,args...)
50 #define ERRMSGQUE(fmt,args...) can_printk(KERN_ERR "can_quekern: " 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 can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
112 /* lock can be released there, because only one instance of canque_dead_tl
113 can run at once and all other functions add ends only to head */
114 while(entry!=&canque_dead_ends){
115 qends=list_entry(entry,struct canque_ends_t,dead_peers);
117 if(!list_empty(&qends->inlist))
119 if(!list_empty(&qends->outlist))
121 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
122 list_del(&qends->dead_peers);
123 can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
124 DEBUGQUE("ends structure disposed\n");
126 if(qends->ends_flags&CAN_ENDSF_MEM_RTL){
127 canque_ends_free_rtl(qends);
130 #endif /*CAN_WITH_RTL*/
136 static inline void canque_dead_tasklet_schedule(void)
139 if(!rtl_rt_system_is_idle()){
140 set_bit(CAN_RTL2LIN_PEND_DEAD_b,&canqueue_rtl2lin_pend);
141 rtl_global_pend_irq (canqueue_rtl_irq);
144 #endif /*CAN_WITH_RTL*/
146 tasklet_schedule(&canque_dead_tl);
150 void canque_edge_do_dead(struct canque_edge_t *edge)
152 can_spin_irqflags_t flags;
154 canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
156 /* The problem of the above call is, that in RT-Linux to Linux notify
157 case is edge scheduled for delayed notify delivery, this needs
158 to be reflected there */
159 if(atomic_read(&edge->edge_used)>0){
160 can_spin_lock_irqsave(&edge->inends->ends_lock, flags);
161 can_spin_lock(&edge->outends->ends_lock);
162 if(atomic_read(&edge->edge_used)>0){
163 /* left edge to live for a while, banshee comes again in a while */
164 canque_fifo_clear_fl(&edge->fifo,DEAD);
165 can_spin_unlock(&edge->outends->ends_lock);
166 can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
167 can_printk(KERN_ERR "can_quertl (debug): canque_edge_do_dead postponed\n");
170 can_spin_unlock(&edge->outends->ends_lock);
171 can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
173 #endif /*CAN_WITH_RTL*/
175 if(canqueue_disconnect_edge(edge)<0){
176 ERRMSGQUE("canque_edge_do_dead: canqueue_disconnect_edge failed !!!\n");
180 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
181 list_add(&edge->inpeers,&canque_dead_edges);
182 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
183 canque_dead_tasklet_schedule();
188 /*if(qends->ends_flags & CAN_ENDSF_DEAD){
189 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
190 list_del(&qends->dead_peers);
191 list_add(&qends->dead_peers,&canque_dead_ends);
192 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
193 tasklet_schedule(&canque_dead_tl);
198 * canqueue_notify_kern - notification callback handler for Linux userspace clients
199 * @qends: pointer to the callback side ends structure
200 * @qedge: edge which invoked notification
201 * @what: notification type
203 * The notification event is handled directly by call of this function except case,
204 * when called from RT-Linux context in mixed mode Linux/RT-Linux compilation.
205 * It is not possible to directly call Linux kernel synchronization primitives
206 * in such case. The notification request is postponed and signaled by @pending_inops flags
207 * by call canqueue_rtl2lin_check_and_pend() function.
208 * The edge reference count is increased until until all pending notifications are processed.
210 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
212 DEBUGQUE("canqueue_notify_kern for edge %d, use %d and event %d\n",
213 qedge->edge_num,(int)atomic_read(&qedge->edge_used),what);
215 /* delay event delivery for RT-Linux -> kernel notifications */
216 if(canqueue_rtl2lin_check_and_pend(qends,qedge,what)){
217 DEBUGQUE("canqueue_notify_kern postponed\n");
222 case CANQUEUE_NOTIFY_EMPTY:
223 wake_up(&qends->endinfo.fileinfo.emptyq);
224 if(canque_fifo_test_and_clear_fl(&qedge->fifo, FREEONEMPTY))
225 canque_edge_decref(qedge);
227 case CANQUEUE_NOTIFY_SPACE:
228 wake_up(&qends->endinfo.fileinfo.writeq);
229 #ifdef CAN_ENABLE_KERN_FASYNC
230 /* Asynchronous I/O processing */
231 kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_OUT);
232 #endif /*CAN_ENABLE_KERN_FASYNC*/
234 case CANQUEUE_NOTIFY_PROC:
235 wake_up(&qends->endinfo.fileinfo.readq);
236 #ifdef CAN_ENABLE_KERN_FASYNC
237 /* Asynchronous I/O processing */
238 kill_fasync(&qends->endinfo.fileinfo.fasync, SIGIO, POLL_IN);
239 #endif /*CAN_ENABLE_KERN_FASYNC*/
241 case CANQUEUE_NOTIFY_NOUSR:
242 wake_up(&qends->endinfo.fileinfo.readq);
243 wake_up(&qends->endinfo.fileinfo.writeq);
244 wake_up(&qends->endinfo.fileinfo.emptyq);
246 case CANQUEUE_NOTIFY_DEAD_WANTED:
247 case CANQUEUE_NOTIFY_DEAD:
248 if(canque_fifo_test_and_clear_fl(&qedge->fifo, READY))
249 canque_edge_decref(qedge);
251 case CANQUEUE_NOTIFY_ATTACH:
257 * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
258 * @qends: pointer to the callback side ends structure
260 int canqueue_ends_init_kern(struct canque_ends_t *qends)
262 canqueue_ends_init_gen(qends);
264 init_waitqueue_head(&qends->endinfo.fileinfo.readq);
265 init_waitqueue_head(&qends->endinfo.fileinfo.writeq);
266 init_waitqueue_head(&qends->endinfo.fileinfo.emptyq);
267 #ifdef CAN_ENABLE_KERN_FASYNC
268 qends->endinfo.fileinfo.fasync=NULL;
269 #endif /*CAN_ENABLE_KERN_FASYNC*/
271 qends->notify=canqueue_notify_kern;
272 DEBUGQUE("canqueue_ends_init_kern\n");
278 * canque_get_inslot4id_wait_kern - find or wait for best outgoing edge and slot for given ID
279 * @qends: ends structure belonging to calling communication object
280 * @qedgep: place to store pointer to found edge
281 * @slotp: place to store pointer to allocated slot
282 * @cmd: command type for slot
283 * @id: communication ID of message to send into edge
284 * @prio: optional priority of message
286 * Same as canque_get_inslot4id(), except, that it waits for free slot
287 * in case, that queue is full. Function is specific for Linux userspace clients.
288 * Return Value: If there is no usable edge negative value is returned.
290 int canque_get_inslot4id_wait_kern(struct canque_ends_t *qends,
291 struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
292 int cmd, unsigned long id, int prio)
295 DEBUGQUE("canque_get_inslot4id_wait_kern for cmd %d, id %ld, prio %d\n",cmd,id,prio);
296 wait_event_interruptible((qends->endinfo.fileinfo.writeq),
297 (ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))!=-1);
302 * canque_get_outslot_wait_kern - receive or wait for ready slot for given ends
303 * @qends: ends structure belonging to calling communication object
304 * @qedgep: place to store pointer to found edge
305 * @slotp: place to store pointer to received slot
307 * The same as canque_test_outslot(), except it waits in the case, that there is
308 * no ready slot for given ends. Function is specific for Linux userspace clients.
309 * Return Value: Negative value informs, that there is no ready output
310 * slot for given ends. Positive value is equal to the command
311 * slot has been allocated by the input side.
313 int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
314 struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
317 DEBUGQUE("canque_get_outslot_wait_kern\n");
318 wait_event_interruptible((qends->endinfo.fileinfo.readq),
319 (ret=canque_test_outslot(qends,qedgep,slotp))!=-1);
324 * canque_sync_wait_kern - wait for all slots processing
325 * @qends: ends structure belonging to calling communication object
326 * @qedge: pointer to edge
328 * Functions waits for ends transition into empty state.
329 * Return Value: Positive value indicates, that edge empty state has been reached.
330 * Negative or zero value informs about interrupted wait or other problem.
332 int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
335 DEBUGQUE("canque_sync_wait_kern\n");
336 wait_event_interruptible((qends->endinfo.fileinfo.emptyq),
337 (ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0));
343 * canque_fifo_init_kern - initialize one CAN FIFO
344 * @fifo: pointer to the FIFO structure
345 * @slotsnr: number of requested slots
347 * Return Value: The negative value indicates, that there is no memory
348 * to allocate space for the requested number of the slots.
350 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr)
353 if(!slotsnr) slotsnr=MAX_BUF_LENGTH;
354 size=sizeof(struct canque_slot_t)*slotsnr;
355 fifo->entry=kmalloc(size,GFP_KERNEL);
356 if(!fifo->entry) return -1;
357 fifo->slotsnr=slotsnr;
358 return canque_fifo_init_slots(fifo);
362 * canque_fifo_done_kern - frees slots allocated for CAN FIFO
363 * @fifo: pointer to the FIFO structure
365 int canque_fifo_done_kern(struct canque_fifo_t *fifo)
375 * canque_new_edge_kern - allocate new edge structure in the Linux kernel context
376 * @slotsnr: required number of slots in the newly allocated edge structure
378 * Return Value: Returns pointer to allocated slot structure or %NULL if
379 * there is not enough memory to process operation.
381 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
383 struct canque_edge_t *qedge;
384 qedge = (struct canque_edge_t *)kmalloc(sizeof(struct canque_edge_t), GFP_KERNEL);
385 if(qedge == NULL) return NULL;
387 memset(qedge,0,sizeof(struct canque_edge_t));
388 can_spin_lock_init(&qedge->fifo.fifo_lock);
389 if(canque_fifo_init_kern(&qedge->fifo, slotsnr)<0){
391 DEBUGQUE("canque_new_edge_kern failed\n");
394 atomic_set(&qedge->edge_used,1);
396 qedge->filtmask = canque_filtid2internal(0l, (processlocal<2)? MSG_LOCAL:0);
397 qedge->edge_prio = 0;
399 /* not exactly clean, but enough for debugging */
400 atomic_inc(&edge_num_cnt);
401 qedge->edge_num=atomic_read(&edge_num_cnt);
402 #endif /* CAN_DEBUG */
403 DEBUGQUE("canque_new_edge_kern %d\n",qedge->edge_num);
407 #ifdef USE_SYNC_DISCONNECT_EDGE_KERN
409 /*not included in doc
410 * canqueue_disconnect_edge_kern - disconnect edge from communicating entities with wait
411 * @qends: ends structure belonging to calling communication object
412 * @qedge: pointer to edge
414 * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
416 * Return Value: Negative value means, that edge is used and cannot
417 * be disconnected yet. Operation has to be delayed.
419 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
421 canque_fifo_set_fl(&qedge->fifo,BLOCK);
422 DEBUGQUE("canqueue_disconnect_edge_kern %d called\n",qedge->edge_num);
423 if(!canque_fifo_test_and_set_fl(&qedge->fifo,DEAD)){
424 canque_notify_bothends(qedge, CANQUEUE_NOTIFY_DEAD);
426 if(atomic_read(&qedge->edge_used)>0)
427 atomic_dec(&qedge->edge_used);
429 DEBUGQUE("canqueue_disconnect_edge_kern %d waiting\n",qedge->edge_num);
430 wait_event((qends->endinfo.fileinfo.emptyq),
431 (canqueue_disconnect_edge(qedge)>=0));
433 /*set_current_state(TASK_UNINTERRUPTIBLE);*/
434 /*schedule_timeout(HZ);*/
437 DEBUGQUE("canqueue_disconnect_edge_kern cannot set DEAD\n");
443 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
445 struct canque_edge_t *edge;
446 can_spin_irqflags_t flags;
448 can_spin_lock_irqsave(&qends->ends_lock,flags);
449 if(list_empty(list)){
450 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
453 if(list == &qends->inlist)
454 edge=list_entry(list->next,struct canque_edge_t,inpeers);
456 edge=list_entry(list->next,struct canque_edge_t,outpeers);
457 atomic_inc(&edge->edge_used);
458 can_spin_unlock_irqrestore(&qends->ends_lock,flags);
459 if(canqueue_disconnect_edge_kern(qends, edge)>=0) {
460 /* Free edge memory */
461 canque_fifo_done_kern(&edge->fifo);
464 canque_notify_bothends(edge, CANQUEUE_NOTIFY_DEAD_WANTED);
465 canque_edge_decref(edge);
466 DEBUGQUE("canqueue_disconnect_list_kern in troubles\n");
467 DEBUGQUE("the edge %d has usage count %d and flags %ld\n",edge->edge_num,atomic_read(&edge->edge_used),edge->fifo.fifo_flags);
473 #endif /*USE_SYNC_DISCONNECT_EDGE_KERN*/
476 int canqueue_ends_sync_all_kern(struct canque_ends_t *qends)
478 struct canque_edge_t *qedge;
480 canque_for_each_inedge(qends, qedge){
481 DEBUGQUE("canque_sync_wait_kern called for edge %d\n",qedge->edge_num);
482 canque_sync_wait_kern(qends, qedge);
488 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends)
490 can_spin_irqflags_t flags;
492 can_spin_lock_irqsave(&canque_dead_func_lock, flags);
493 qends->ends_flags |= CAN_ENDSF_DEAD;
494 list_add(&qends->dead_peers,&canque_dead_ends);
495 can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
496 canque_dead_tasklet_schedule();
501 * canqueue_ends_dispose_kern - finalizing of the ends structure for Linux kernel clients
502 * @qends: pointer to ends structure
503 * @sync: flag indicating, that user wants to wait for processing of all remaining
506 * Return Value: Function should be designed such way to not fail.
508 int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync)
512 DEBUGQUE("canqueue_ends_dispose_kern\n");
513 canqueue_block_inlist(qends);
514 canqueue_block_outlist(qends);
516 /*Wait for sending of all pending messages in the output FIFOs*/
518 canqueue_ends_sync_all_kern(qends);
520 /* Finish or kill all outgoing edges listed in inends */
521 delayed=canqueue_ends_kill_inlist(qends, 1);
522 /* Kill all incoming edges listed in outends */
523 delayed|=canqueue_ends_kill_outlist(qends);
525 wake_up(&qends->endinfo.fileinfo.readq);
526 wake_up(&qends->endinfo.fileinfo.writeq);
527 wake_up(&qends->endinfo.fileinfo.emptyq);
530 canqueue_ends_dispose_postpone(qends);
532 DEBUGQUE("canqueue_ends_dispose_kern delayed\n");
537 DEBUGQUE("canqueue_ends_dispose_kern finished\n");
541 void canqueue_kern_initialize()
543 can_spin_lock_init(&canque_dead_func_lock);