]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/can_devrtl.c
c7ff970e7d51babeff45354fa4a7c094ebce3028
[lincan.git] / lincan / src / can_devrtl.c
1 /* can_devrtl.c - CAN message queues functions for the RT-Linux
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 #ifdef CAN_WITH_RTL
10
11 #include "../include/can.h"
12 #include "../include/can_sysdep.h"
13 #include "../include/can_queue.h"
14 #include "../include/main.h"
15 #include "../include/setup.h"
16
17 #include <rtl_malloc.h>
18
19 #ifdef CAN_ENABLE_VME_SUPPORT
20 #include "ca91c042.h"
21 /* Modified version of ca91c042 driver can be found in
22  * components/comm/contrib directory. */
23 #endif
24
25 can_spinlock_t can_irq_manipulation_lock;
26
27 unsigned int can_rtl_isr( unsigned int irq_num, struct pt_regs *r )
28 {
29         struct canchip_t *chip;
30         struct candevice_t *candev;
31         int board_nr;
32         int chip_nr;
33         int irq2linux=0;
34         int ret;
35         pthread_t thread=NULL;
36
37         DEBUGMSG("can_rtl_isr invoked for irq %d\n",irq_num);
38         
39         /* I hate next loop, but RT-Linux does not provide context to ISR */
40         for (board_nr=hardware_p->nr_boards; board_nr--; ) {
41                 if((candev=hardware_p->candevice[board_nr])==NULL)
42                         continue;
43                 for(chip_nr=candev->nr_all_chips; chip_nr--; ) {
44                         if((chip=candev->chip[chip_nr])==NULL)
45                                 continue;
46                         if(chip->chip_irq!=irq_num) continue;
47
48                         if(chip->chipspecops->irq_accept)
49                                 ret=chip->chipspecops->irq_accept(chip->chip_irq,chip);
50
51                         set_bit(MSGOBJ_IRQ_REQUEST_b,&chip->pend_flags);
52                         set_bit(MSGOBJ_WORKER_WAKE_b,&chip->pend_flags);
53                         if(chip->flags & CHIP_IRQ_PCI)
54                                 irq2linux=1;
55                         if(!chip->worker_thread) continue;
56                         thread=chip->worker_thread;
57                         pthread_kill(thread,RTL_SIGNAL_WAKEUP);
58                 }
59         }
60
61         if(irq2linux)
62                 rtl_global_pend_irq(irq_num);
63
64         /*if(thread) rtl_reschedule_thread(thread);*/
65
66         rtl_schedule();
67
68         return 0;
69 }
70
71
72
73 /*
74 RTL_MARK_READY(pthread_self())
75 RTL_MARK_SUSPENDED(pthread_self());
76 return rtl_schedule();
77 can_enable_irq
78 can_disable_irq 
79 rtl_critical( state )
80 rtl_end_critical( state )
81 rtl_request_global_irq( irq, isr ); 
82 rtl_free_global_irq( irq )
83 */
84
85 void * can_chip_worker_thread(void *arg)
86 {
87         struct canchip_t *chip = (struct canchip_t *) arg;
88         struct msgobj_t *obj;
89         int ret, i;
90         int loop_cnt;
91         
92         if(!chip) return 0;
93         
94         
95         if (!(chip->flags & CHIP_CONFIGURED)){
96                 if (chip->chipspecops->chip_config(chip))
97                         CANMSG("Error configuring chip.\n");
98                 else
99                         chip->flags |= CHIP_CONFIGURED; 
100
101                 if((chip->msgobj[0])!=NULL)
102                         if (chip->chipspecops->pre_read_config(chip,chip->msgobj[0])<0)
103                                 CANMSG("Error initializing chip for receiving\n");
104                                 
105         } /* End of chip configuration */
106         set_bit(MSGOBJ_IRQ_REQUEST_b,&chip->pend_flags);
107         
108
109         while (1) {
110                 DEBUGMSG("Worker thread for chip %d active\n",chip->chip_idx);
111                 if(test_and_clear_bit(MSGOBJ_IRQ_REQUEST_b,&chip->pend_flags)){
112                         DEBUGMSG("IRQ_REQUEST processing ...\n");
113                         loop_cnt = 100;
114                         if(chip->chipspecops->irq_handler) do{
115                                 ret=chip->chipspecops->irq_handler(chip->chip_irq,chip);
116                         }while(ret && --loop_cnt);
117                         continue;
118                 }
119                 if(test_and_clear_bit(MSGOBJ_TX_REQUEST_b,&chip->pend_flags)){
120                         DEBUGMSG("TX_REQUEST processing ...\n");
121                         for(i=0;i<chip->max_objects;i++){
122                                 if((obj=chip->msgobj[i])==NULL)
123                                         continue;
124                                 if(can_msgobj_test_fl(obj,TX_REQUEST)) {
125                                         DEBUGMSG("Calling wakeup_tx\n");
126                                         chip->chipspecops->wakeup_tx(chip, obj);
127                                 }
128                                 if(can_msgobj_test_fl(obj,FILTCH_REQUEST)) {
129                                         DEBUGMSG("Calling filtch_rq\n");
130                                         if(chip->chipspecops->filtch_rq)
131                                                 chip->chipspecops->filtch_rq(chip, obj);
132                                 }
133                         }
134                         continue;
135                 }
136
137                 /*re-enable chip IRQ, I am not sure, if this is required,
138                   but it seems to not work without that */
139                 if(chip->chip_irq>=0) {
140                         if ((chip->flags & CHIP_IRQ_VME) == 0) can_enable_irq(chip->chip_irq);
141                     #ifdef CAN_ENABLE_VME_SUPPORT
142                       #if 0
143                         else tundra_rtl_enable_pci_irq();
144                       #endif
145                         /* FIXME: Bad practice. Doesn't work with more
146                          * than one card.
147                          *
148                          * irq_accept added to the LinCAN driver now,
149                          * and above workaround should not be required.
150                          * Enable rtl_hard_enable_irq() at line 
151                          * ca91c042.c:1045
152                          */
153                     #endif /*CAN_ENABLE_VME_SUPPORT*/
154
155                 }
156
157                 RTL_MARK_SUSPENDED(pthread_self());
158                 if(test_and_clear_bit(MSGOBJ_WORKER_WAKE_b,&chip->pend_flags)){
159                         RTL_MARK_READY(pthread_self());
160                         continue;
161                 }
162                 rtl_schedule();
163
164         }
165         return 0;
166 }
167
168
169 int can_chip_setup_irq(struct canchip_t *chip)
170 {
171         int ret;
172         struct sched_param sched_param;
173         pthread_attr_t attrib;
174         pthread_attr_t *attrib_p=NULL;
175         
176         if(chip==NULL)
177                 return -1;
178         
179         if(can_rtl_priority>=0){
180                 pthread_attr_init(&attrib);
181                 sched_param.sched_priority = can_rtl_priority;
182                 pthread_attr_setschedparam(&attrib, &sched_param);
183                 /* pthread_attr_setschedpolicy(&attrib, SCHED_FIFO); */
184                 attrib_p=&attrib;
185         }
186         
187         if(chip->chipspecops->irq_handler && !(chip->flags & CHIP_IRQ_CUSTOM)){
188                 int (*my_request_irq)(unsigned int vector, unsigned int (*rtl_handler)(unsigned int irq, struct pt_regs *regs));
189 #ifdef CAN_ENABLE_VME_SUPPORT
190                 if ((chip->flags & CHIP_IRQ_VME) != 0)
191                         my_request_irq = rtl_request_vmeirq;
192                 else
193 #endif
194                         my_request_irq = rtl_request_irq;
195
196                 if (my_request_irq(chip->chip_irq,can_rtl_isr))
197                         return -1;
198                 else {
199                         DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
200                         chip->flags |= CHIP_IRQ_SETUP;
201                 }
202         }
203         ret=pthread_create(&chip->worker_thread, attrib_p, can_chip_worker_thread, chip);
204         if(ret<0) chip->worker_thread=NULL;
205         
206         return ret;
207 }
208
209
210 void can_chip_free_irq(struct canchip_t *chip)
211 {
212         if(chip->worker_thread)
213                 pthread_delete_np(chip->worker_thread);
214         if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)
215             && !(chip->flags & CHIP_IRQ_CUSTOM)) {
216                 int (*my_free_irq)(unsigned int vector);
217 #ifdef CAN_ENABLE_VME_SUPPORT
218                 if ((chip->flags & CHIP_IRQ_VME) != 0)
219                         my_free_irq = rtl_free_vmeirq;
220                 else
221 #endif
222                         my_free_irq = rtl_free_irq;
223                 my_free_irq(chip->chip_irq);
224                 chip->flags &= ~CHIP_IRQ_SETUP;
225         }
226 }
227
228
229 #endif /*CAN_WITH_RTL*/