]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/app/usbcan/can_quesysless.c
Merge master into can-usb1 branch to include proc update for 3.12+ kernels.
[lincan.git] / embedded / app / usbcan / can_quesysless.c
1 /**************************************************************************/
2 /* File: can_quesysless.c - CAN queue functions for embedded target       */
3 /*                                                                        */
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                               */
8 /*                                                                        */
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.                                              */
19 /*                                                                        */
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:                                                     */
23 /*                                                                        */
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 /**************************************************************************/
33
34 #include "./can/can.h"
35 #include "./can/can_sysdep.h"
36 #include "./can/can_queue.h"
37
38 // #define CAN_DEBUG
39
40 extern atomic_t edge_num_cnt;
41
42 #ifdef CAN_DEBUG
43         #define DEBUGQUE(fmt,args...) can_printk(KERN_ERR "can_quesysless (debug): " fmt,\
44         ##args)
45
46 #else
47         #define DEBUGQUE(fmt,args...)
48 #endif
49
50 #define ERRMSGQUE(fmt,args...) can_printk(KERN_ERR "can_quesysless: " fmt,\
51         ##args)
52
53
54 /*
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
59  */
60 extern int processlocal;
61
62 void canque_dead_func(unsigned long data);
63
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) */
72
73
74 static inline
75 struct canque_edge_t *canque_dead_edges_cut_first(void)
76 {
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))
81                 edge=NULL;
82         else{
83                 edge=list_entry(canque_dead_edges.next,struct canque_edge_t,inpeers);
84                 list_del(&edge->inpeers);
85         }
86         can_spin_unlock_irqrestore(&canque_dead_func_lock, flags);
87         return edge;
88 }
89
90 void canque_dead_func(unsigned long data)
91 {
92         can_spin_irqflags_t flags;
93         struct canque_edge_t *qedge;
94         struct canque_ends_t *qends;
95         struct list_head *entry;
96
97         while((qedge=canque_dead_edges_cut_first())){
98                 DEBUGQUE("edge %d disposed\n",qedge->edge_num);
99             #ifdef CAN_WITH_RTL
100                 if(canque_fifo_test_fl(&qedge->fifo,RTL_MEM)){
101                         canque_dispose_edge_rtl(qedge);
102                         continue;
103                 }
104             #endif /*CAN_WITH_RTL*/
105                 canque_fifo_done_kern(&qedge->fifo);
106                 free(qedge);
107         }
108
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);
113                 entry=entry->next;
114                 if(!list_empty(&qends->inlist))
115                         continue;
116                 if(!list_empty(&qends->outlist))
117                         continue;
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");
122             #ifdef CAN_WITH_RTL
123                 if(qends->ends_flags&CAN_ENDSF_MEM_RTL){
124                         canque_ends_free_rtl(qends);
125                         continue;
126                 }
127             #endif /*CAN_WITH_RTL*/
128                 free(qends);
129         }
130         can_spin_unlock_irqrestore(&canque_dead_func_lock,flags);
131 }
132
133 // static inline void canque_dead_tasklet_schedule(void)
134 // {
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);
139 //              return;
140 //      }
141 //     #endif /*CAN_WITH_RTL*/
142 //
143 //      tasklet_schedule(&canque_dead_tl);
144 // }
145
146
147 void canque_edge_do_dead(struct canque_edge_t *edge)
148 {
149         can_spin_irqflags_t flags;
150
151         canque_notify_bothends(edge,CANQUEUE_NOTIFY_NOUSR);
152     #ifdef CAN_WITH_RTL
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");
165                         return;
166                 }
167                 can_spin_unlock(&edge->outends->ends_lock);
168                 can_spin_unlock_irqrestore(&edge->inends->ends_lock, flags);
169         }
170     #endif /*CAN_WITH_RTL*/
171
172         if(canqueue_disconnect_edge(edge)<0){
173                 ERRMSGQUE("canque_edge_do_dead: canqueue_disconnect_edge failed !!!\n");
174                 return;
175         }
176
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();
181         canque_dead_func(0);
182 }
183
184
185
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);
192 }*/
193
194
195 /**
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
200  *
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.
207  */
208 void canqueue_notify_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
209 {
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);
212
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");
216                 return;
217         }
218
219         switch(what){
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);
224                         break;
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*/
231                         break;
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*/
238                         break;
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);
243                         break;
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);
248                         break;
249                 case CANQUEUE_NOTIFY_ATTACH:
250                         break;
251         }
252 }
253
254 /**
255  * canqueue_ends_init_kern - Linux userspace clients specific ends initialization
256  * @qends: pointer to the callback side ends structure
257  */
258 int canqueue_ends_init_kern(struct canque_ends_t *qends)
259 {
260         canqueue_ends_init_gen(qends);
261         qends->context=NULL;
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*/
268
269         qends->notify=canqueue_notify_kern;
270         DEBUGQUE("canqueue_ends_init_kern\n");
271         return 0;
272 }
273
274
275 /**
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
283  *
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.
287  */
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)
291 // {
292 //      int ret=-1;
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);
296 //      return ret;
297 // }
298
299 /**
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
304  *
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.
310  */
311 // int canque_get_outslot_wait_kern(struct canque_ends_t *qends,
312 //      struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
313 // {
314 //      int ret=-1;
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);
318 //      return ret;
319 // }
320
321 /**
322  * canque_sync_wait_kern - wait for all slots processing
323  * @qends: ends structure belonging to calling communication object
324  * @qedge: pointer to edge
325  *
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.
329  */
330 // int canque_sync_wait_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
331 // {
332 //      int ret=-1;
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));
336 //      return ret;
337 // }
338
339
340 /**
341  * canque_fifo_init_kern - initialize one CAN FIFO
342  * @fifo: pointer to the FIFO structure
343  * @slotsnr: number of requested slots
344  *
345  * Return Value: The negative value indicates, that there is no memory
346  *      to allocate space for the requested number of the slots.
347  */
348 int canque_fifo_init_kern(struct canque_fifo_t *fifo, int slotsnr)
349 {
350         int size;
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);
357 }
358
359 /**
360  * canque_fifo_done_kern - frees slots allocated for CAN FIFO
361  * @fifo: pointer to the FIFO structure
362  */
363 int canque_fifo_done_kern(struct canque_fifo_t *fifo)
364 {
365         if(fifo->entry)
366                 free(fifo->entry);
367         fifo->entry=NULL;
368         return 1;
369 }
370
371
372 /**
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
375  *
376  * Return Value: Returns pointer to allocated slot structure or %NULL if
377  *      there is not enough memory to process operation.
378  */
379 struct canque_edge_t *canque_new_edge_kern(int slotsnr)
380 {
381         struct canque_edge_t *qedge;
382         qedge = (struct canque_edge_t *)malloc(sizeof(struct canque_edge_t));
383         if(qedge == NULL) return NULL;
384
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){
388                 free(qedge);
389                 DEBUGQUE("canque_new_edge_kern failed\n");
390                 return NULL;
391         }
392         atomic_set(&qedge->edge_used,1);
393         qedge->filtid = 0;
394         qedge->filtmask = canque_filtid2internal(0l, (processlocal<2)? MSG_LOCAL:0);
395         qedge->edge_prio = 0;
396     #ifdef CAN_DEBUG
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);
402         return qedge;
403 }
404
405 #ifdef USE_SYNC_DISCONNECT_EDGE_KERN
406
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
411  *
412  * Same as canqueue_disconnect_edge(), but tries to wait for state with zero
413  * use counter.
414  * Return Value: Negative value means, that edge is used and cannot
415  *      be disconnected yet. Operation has to be delayed.
416  */
417 int canqueue_disconnect_edge_kern(struct canque_ends_t *qends, struct canque_edge_t *qedge)
418 {
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);
423
424                 if(atomic_read(&qedge->edge_used)>0)
425                         atomic_dec(&qedge->edge_used);
426
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));
430
431                 /*set_current_state(TASK_UNINTERRUPTIBLE);*/
432                 /*schedule_timeout(HZ);*/
433                 return 0;
434         } else {
435                 DEBUGQUE("canqueue_disconnect_edge_kern cannot set DEAD\n");
436                 return -1;
437         }
438 }
439
440
441 int canqueue_disconnect_list_kern(struct canque_ends_t *qends, struct list_head *list)
442 {
443         struct canque_edge_t *edge;
444         can_spin_irqflags_t flags;
445         for(;;){
446                 can_spin_lock_irqsave(&qends->ends_lock,flags);
447                 if(list_empty(list)){
448                         can_spin_unlock_irqrestore(&qends->ends_lock,flags);
449                         return 0;
450                 }
451                 if(list == &qends->inlist)
452                         edge=list_entry(list->next,struct canque_edge_t,inpeers);
453                 else
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);
460                         free(edge);
461                 }else{
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);
466                         return -1;
467                 }
468         }
469 }
470
471 #endif /*USE_SYNC_DISCONNECT_EDGE_KERN*/
472
473
474 // int canqueue_ends_sync_all_kern(struct canque_ends_t *qends)
475 // {
476 //      struct canque_edge_t *qedge;
477 //
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);
481 //      }
482 //      return 0;
483 // }
484
485
486 void canqueue_ends_dispose_postpone(struct canque_ends_t *qends)
487 {
488         can_spin_irqflags_t flags;
489
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);
494         canque_dead_func(0);
495 //      canque_dead_tasklet_schedule();
496 }
497
498
499 /**
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
503  *      messages
504  *
505  * Return Value: Function should be designed such way to not fail.
506  */
507 // int canqueue_ends_dispose_kern(struct canque_ends_t *qends, int sync)
508 // {
509 //      int delayed;
510 //
511 //      DEBUGQUE("canqueue_ends_dispose_kern\n");
512 //      canqueue_block_inlist(qends);
513 //      canqueue_block_outlist(qends);
514 //
515 //      /*Wait for sending of all pending messages in the output FIFOs*/
516 //      if(sync)
517 //              canqueue_ends_sync_all_kern(qends);
518 //
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);
523 //
524 // //   wake_up(&qends->endinfo.fileinfo.readq);
525 // //   wake_up(&qends->endinfo.fileinfo.writeq);
526 // //   wake_up(&qends->endinfo.fileinfo.emptyq);
527 //
528 //      if(delayed){
529 //              canqueue_ends_dispose_postpone(qends);
530 //
531 //              DEBUGQUE("canqueue_ends_dispose_kern delayed\n");
532 //              return 1;
533 //      }
534 //
535 //      free(qends);
536 //      DEBUGQUE("canqueue_ends_dispose_kern finished\n");
537 //      return 0;
538 // }
539
540 void canqueue_kern_initialize()
541 {
542         can_spin_lock_init(&canque_dead_func_lock);
543 }