]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/can_devrtl.c
812ea84a731be5de4952af924b4416ebb5f25f7e
[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.2  9 Jul 2003
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 unsigned int can_rtl_isr( unsigned int irq_num, struct pt_regs *r )
20 {
21         struct chip_t *chip;
22         struct candevice_t *candev;
23         int board_nr;
24         int chip_nr;
25         int irq2linux=0;
26         pthread_t thread=NULL;
27
28         DEBUGMSG("can_rtl_isr invoked for irq %d\n",irq_num);
29         
30         /* I hate next loop, but RT-Linux does not provide context to ISR */
31         for (board_nr=hardware_p->nr_boards; board_nr--; ) {
32                 if((candev=hardware_p->candevice[board_nr])==NULL)
33                         continue;
34                 for(chip_nr=candev->nr_all_chips; chip_nr--; ) {
35                         if((chip=candev->chip[chip_nr])==NULL)
36                                 continue;
37                         if(chip->chip_irq!=irq_num) continue;
38
39                         set_bit(MSGOBJ_IRQ_REQUEST_b,&chip->pend_flags);
40                         set_bit(MSGOBJ_WORKER_WAKE_b,&chip->pend_flags);
41                         if(chip->flags & CHIP_IRQ_PCI)
42                                 irq2linux=1;
43                         if(!chip->worker_thread) continue;
44                         thread=chip->worker_thread;
45                         pthread_kill(thread,RTL_SIGNAL_WAKEUP);
46                 }
47         }
48
49         if(irq2linux)
50                 rtl_global_pend_irq(irq_num);
51
52         /*if(thread) rtl_reschedule_thread(thread);*/
53
54         rtl_schedule();
55
56         return 0;
57 }
58
59
60
61 /*
62 RTL_MARK_READY(pthread_self())
63 RTL_MARK_SUSPENDED(pthread_self());
64 return rtl_schedule();
65 can_enable_irq
66 can_disable_irq 
67 rtl_critical( state )
68 rtl_end_critical( state )
69 rtl_request_global_irq( irq, isr ); 
70 rtl_free_global_irq( irq )
71 */
72
73 void * can_chip_worker_thread(void *arg)
74 {
75         struct chip_t *chip = (struct chip_t *) arg;
76         struct msgobj_t *obj;
77         int ret, i;
78         int loop_cnt;
79         
80         if(!chip) return 0;
81         
82         
83         if (!(chip->flags & CHIP_CONFIGURED)){
84                 if (chip->chipspecops->chip_config(chip))
85                         CANMSG("Error configuring chip.\n");
86                 else
87                         chip->flags |= CHIP_CONFIGURED; 
88
89                 if((chip->msgobj[0])!=NULL)
90                         if (chip->chipspecops->pre_read_config(chip,chip->msgobj[0])<0)
91                                 CANMSG("Error initializing chip for receiving\n");
92                                 
93         } /* End of chip configuration */
94         set_bit(MSGOBJ_IRQ_REQUEST_b,&chip->pend_flags);
95         
96
97         while (1) {
98                 DEBUGMSG("Worker thread for chip %d active\n",chip->chip_idx);
99                 if(test_and_clear_bit(MSGOBJ_IRQ_REQUEST_b,&chip->pend_flags)){
100                         DEBUGMSG("IRQ_REQUEST processing ...\n");
101                         loop_cnt = 100;
102                         if(chip->chipspecops->irq_handler) do{
103                                 ret=chip->chipspecops->irq_handler(chip->chip_irq,chip,NULL);
104                         }while(ret && --loop_cnt);
105                         continue;
106                 }
107                 if(test_and_clear_bit(MSGOBJ_TX_REQUEST_b,&chip->pend_flags)){
108                         DEBUGMSG("TX_REQUEST processing ...\n");
109                         for(i=0;i<chip->max_objects;i++){
110                                 if((obj=chip->msgobj[i])==NULL)
111                                         continue;
112                                 if(!can_msgobj_test_and_clear_fl(obj,TX_REQUEST))
113                                         continue;
114                                 DEBUGMSG("Calling wakeup_tx\n");
115                                 chip->chipspecops->wakeup_tx(chip, obj);
116                         }
117                         continue;
118                 }
119
120                 /*re-enable chip IRQ, I am not sure, if this is required,
121                   but it seems to not work without that */
122                 if(chip->chip_irq>=0)
123                         can_enable_irq(chip->chip_irq);
124
125                 RTL_MARK_SUSPENDED(pthread_self());
126                 if(test_and_clear_bit(MSGOBJ_WORKER_WAKE_b,&chip->pend_flags)){
127                         RTL_MARK_READY(pthread_self());
128                         continue;
129                 }
130                 rtl_schedule();
131
132         }
133         return 0;
134 }
135
136
137 int can_chip_setup_irq(struct chip_t *chip)
138 {
139         int ret;
140         struct sched_param sched_param;
141         pthread_attr_t attrib;
142         pthread_attr_t *attrib_p=NULL;
143         
144         if(chip==NULL)
145                 return -1;
146         
147         if(can_rtl_priority>=0){
148                 pthread_attr_init(&attrib);
149                 sched_param.sched_priority = can_rtl_priority;
150                 pthread_attr_setschedparam(&attrib, &sched_param);
151                 /* pthread_attr_setschedpolicy(&attrib, SCHED_FIFO); */
152                 attrib_p=&attrib;
153         }
154         
155         if(chip->chipspecops->irq_handler){
156                 if (rtl_request_irq(chip->chip_irq,can_rtl_isr))
157                         return -1;
158                 else {
159                         DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
160                         chip->flags |= CHIP_IRQ_SETUP;
161                 }
162         }
163         ret=pthread_create(&chip->worker_thread, attrib_p, can_chip_worker_thread, chip);
164         if(ret<0) chip->worker_thread=NULL;
165         
166         return ret;
167 }
168
169
170 void can_chip_free_irq(struct chip_t *chip)
171 {
172         if(chip->worker_thread)
173                 pthread_delete_np(chip->worker_thread);
174         if((chip->flags & CHIP_IRQ_SETUP) && (chip->chip_irq>=0)) {
175                 rtl_free_irq(chip->chip_irq);
176                 chip->flags &= ~CHIP_IRQ_SETUP;
177         }
178 }
179
180
181 #endif /*CAN_WITH_RTL*/