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