1 /**************************************************************************/
2 /* File: can_devrtl.c - CAN message queues functions for the RT-Linux */
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 */
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. */
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: */
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 /**************************************************************************/
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"
42 #include <rtl_malloc.h>
44 #ifdef CAN_ENABLE_VME_SUPPORT
46 /* Modified version of ca91c042 driver can be found in
47 * components/comm/contrib directory. */
50 can_spinlock_t can_irq_manipulation_lock;
52 unsigned int can_rtl_isr( unsigned int irq_num, struct pt_regs *r )
54 struct canchip_t *chip;
55 struct candevice_t *candev;
60 pthread_t thread=NULL;
62 DEBUGMSG("can_rtl_isr invoked for irq %d\n",irq_num);
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)
68 for(chip_nr=candev->nr_all_chips; chip_nr--; ) {
69 if((chip=candev->chip[chip_nr])==NULL)
71 if(chip->chip_irq!=irq_num) continue;
73 if(chip->chipspecops->irq_accept)
74 ret=chip->chipspecops->irq_accept(chip->chip_irq,chip);
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)
80 #ifdef CAN_ENABLE_VME_SUPPORT
81 if (chip->flags & CHIP_IRQ_VME)
82 tundra_rtl_ack_irq_vector(irq_num);
84 if(!chip->worker_thread) continue;
85 thread=chip->worker_thread;
86 pthread_kill(thread,RTL_SIGNAL_WAKEUP);
90 /* The following lines are commented out because of it is not
91 * possible to share level activated (PCI) IRQs between Linux
94 /* rtl_global_pend_irq(irq_num); */
96 /*if(thread) rtl_reschedule_thread(thread);*/
106 RTL_MARK_READY(pthread_self())
107 RTL_MARK_SUSPENDED(pthread_self());
108 return rtl_schedule();
111 rtl_critical( state )
112 rtl_end_critical( state )
113 rtl_request_global_irq( irq, isr );
114 rtl_free_global_irq( irq )
117 void * can_chip_worker_thread(void *arg)
119 struct canchip_t *chip = (struct canchip_t *) arg;
120 struct msgobj_t *obj;
123 rtl_irqstate_t flags;
128 if (!(chip->flags & CHIP_CONFIGURED)){
129 if (chip->chipspecops->chip_config(chip))
130 CANMSG("Error configuring chip.\n");
132 chip->flags |= CHIP_CONFIGURED;
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");
138 } /* End of chip configuration */
139 set_bit(MSGOBJ_IRQ_REQUEST_b,&chip->pend_flags);
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");
147 if(chip->chipspecops->irq_handler) do{
148 ret=chip->chipspecops->irq_handler(chip->chip_irq,chip);
149 }while(ret && --loop_cnt);
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)
157 if(can_msgobj_test_fl(obj,TX_REQUEST)) {
158 DEBUGMSG("Calling wakeup_tx\n");
159 chip->chipspecops->wakeup_tx(chip, obj);
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);
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
176 else tundra_rtl_enable_pci_irq();
178 /* FIXME: Bad practice. Doesn't work with more
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
186 #endif /*CAN_ENABLE_VME_SUPPORT*/
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);
197 rtl_restore_interrupts (flags);
205 int can_chip_setup_irq(struct canchip_t *chip)
208 struct sched_param sched_param;
209 pthread_attr_t attrib;
210 pthread_attr_t *attrib_p=NULL;
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); */
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;
230 my_request_irq = rtl_request_irq;
232 if (my_request_irq(chip->chip_irq,can_rtl_isr))
235 DEBUGMSG("Registered interrupt %d\n",chip->chip_irq);
236 chip->flags |= CHIP_IRQ_SETUP;
239 ret=pthread_create(&chip->worker_thread, attrib_p, can_chip_worker_thread, chip);
240 if(ret<0) chip->worker_thread=NULL;
246 void can_chip_free_irq(struct canchip_t *chip)
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;
258 my_free_irq = rtl_free_irq;
259 my_free_irq(chip->chip_irq);
260 chip->flags &= ~CHIP_IRQ_SETUP;
265 #endif /*CAN_WITH_RTL*/