2af0f86e98eecc2e7148b2af9226c40b38b80fdc
[lincan.git] / embedded / app / usbcan / devcommon.c
1 /* devcommon.c - common device code
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 #include "../include/main.h"
13 #include "../include/devcommon.h"
14
15 #ifdef CAN_WITH_RTL
16 static inline
17 void canqueue_wake_chip_worker(struct canque_ends_t *qends, struct canchip_t *chip, struct msgobj_t *obj)
18 {
19         if(qends->endinfo.chipinfo.worker_thread){
20                 can_msgobj_set_fl(obj,WORKER_WAKE);
21                 pthread_kill(qends->endinfo.chipinfo.worker_thread,RTL_SIGNAL_WAKEUP);
22                 rtl_schedule();
23         } else {
24                 set_bit(MSGOBJ_TX_REQUEST_b,&chip->pend_flags);
25                 if(chip->worker_thread) {
26                         set_bit(MSGOBJ_WORKER_WAKE_b,&chip->pend_flags);
27                         pthread_kill(chip->worker_thread,RTL_SIGNAL_WAKEUP);
28                         rtl_schedule();
29                 }
30         }
31 }
32
33 #endif /*CAN_WITH_RTL*/
34
35
36 /**
37  * canqueue_notify_chip - notification callback handler for CAN chips ends of queues
38  * @qends: pointer to the callback side ends structure
39  * @qedge: edge which invoked notification 
40  * @what: notification type
41  *
42  * This function has to deal with more possible cases. It can be called from
43  * the kernel or interrupt context for Linux only compilation of driver.
44  * The function can be called from kernel context or RT-Linux thread context
45  * for mixed mode Linux/RT-Linux compilation.
46  */
47 void canqueue_notify_chip(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
48 {
49         struct canchip_t *chip=qends->endinfo.chipinfo.chip;
50         struct msgobj_t *obj=qends->endinfo.chipinfo.msgobj;
51
52         DEBUGMSG("canqueue_notify_chip for edge %d and event %d\n",qedge->edge_num,what);
53         switch(what){
54                 /*case CANQUEUE_NOTIFY_EMPTY:*/
55                 /*case CANQUEUE_NOTIFY_SPACE:*/
56                 /*case CANQUEUE_NOTIFY_NOUSR:
57                         wake_up(&qends->endinfo.chipinfo.daemonq);
58                         break;*/
59                 case CANQUEUE_NOTIFY_PROC:
60                     #ifndef CAN_WITH_RTL
61                         /*wake_up(&qends->endinfo.chipinfo.daemonq);*/
62                         chip->chipspecops->wakeup_tx(chip, obj);
63                     #else /*CAN_WITH_RTL*/
64                         can_msgobj_set_fl(obj,TX_REQUEST);
65                         canqueue_wake_chip_worker(qends, chip, obj);
66                     #endif /*CAN_WITH_RTL*/
67                         break;
68                 case CANQUEUE_NOTIFY_DEAD_WANTED:
69                 case CANQUEUE_NOTIFY_DEAD:
70                         if(canque_fifo_test_and_clear_fl(&qedge->fifo, READY))
71                                 canque_edge_decref(qedge);
72                         break;
73                 case CANQUEUE_NOTIFY_ATTACH:
74                         break;
75                 case CANQUEUE_NOTIFY_FILTCH:
76                         if(!chip->chipspecops->filtch_rq)
77                                 break;
78                     #ifndef CAN_WITH_RTL
79                         chip->chipspecops->filtch_rq(chip, obj);
80                     #else /*CAN_WITH_RTL*/
81                         can_msgobj_set_fl(obj,FILTCH_REQUEST);
82                         canqueue_wake_chip_worker(qends, chip, obj);
83                     #endif /*CAN_WITH_RTL*/
84                         
85                         break;
86         }
87 }
88
89
90 /**
91  * canqueue_ends_init_chip - CAN chip specific ends initialization
92  * @qends: pointer to the ends structure
93  * @chip: pointer to the corresponding CAN chip structure
94  * @obj: pointer to the corresponding message object structure
95  */
96 int canqueue_ends_init_chip(struct canque_ends_t *qends, struct canchip_t *chip, struct msgobj_t *obj)
97 {
98         int ret;
99         ret=canqueue_ends_init_gen(qends);
100         if(ret<0) return ret;
101         
102         qends->context=NULL;
103     #ifndef CAN_WITH_RTL
104         init_waitqueue_head(&qends->endinfo.chipinfo.daemonq);
105     #endif /*CAN_WITH_RTL*/
106         qends->endinfo.chipinfo.chip=chip;
107         qends->endinfo.chipinfo.msgobj=obj;
108         qends->notify=canqueue_notify_chip;
109
110         DEBUGMSG("canqueue_ends_init_chip\n");
111         return 0;
112 }
113
114
115 /**
116  * canqueue_ends_done_chip - finalizing of the ends structure for CAN chips
117  * @qends: pointer to ends structure
118  *
119  * Return Value: Function should be designed such way to not fail.
120  */
121 int canqueue_ends_done_chip(struct canque_ends_t *qends)
122 {
123         int delayed;
124         
125         /* Finish or kill all outgoing edges listed in inends */
126         delayed=canqueue_ends_kill_inlist(qends, 1);
127         /* Kill all incoming edges listed in outends */
128         delayed|=canqueue_ends_kill_outlist(qends);
129
130         return delayed;
131 }