]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - lincan/src/can_quertl.c
Actual driver code for directly mapped SJA1000 into PCI mem region 0.
[lincan.git] / lincan / src / can_quertl.c
index 45129b41b480418d395e0dfb754a8893ddae69bc..50d60819feea15d5d37096297637bd7a3c9eea88 100644 (file)
@@ -1,10 +1,35 @@
-/* can_quertl.c - CAN message queues functions for the RT-Linux
- * Linux CAN-bus device driver.
- * New CAN queues by Pavel Pisa - OCERA team member
- * email:pisa@cmp.felk.cvut.cz
- * This software is released under the GPL-License.
- * Version lincan-0.2  9 Jul 2003
- */
+/**************************************************************************/
+/* File: can_quertl.c - CAN message queues functions for the RT-Linux     */
+/*                                                                        */
+/* LinCAN - (Not only) Linux CAN bus driver                               */
+/* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
+/* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
+/* Funded by OCERA and FRESCOR IST projects                               */
+/*                                                                        */
+/* LinCAN is free software; you can redistribute it and/or modify it      */
+/* under terms of the GNU General Public License as published by the      */
+/* Free Software Foundation; either version 2, or (at your option) any    */
+/* later version.  LinCAN is distributed in the hope that it will be      */
+/* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
+/* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
+/* General Public License for more details. You should have received a    */
+/* copy of the GNU General Public License along with LinCAN; see file     */
+/* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
+/* Cambridge, MA 02139, USA.                                              */
+/*                                                                        */
+/* To allow use of LinCAN in the compact embedded systems firmware        */
+/* and RT-executives (RTEMS for example), main authors agree with next    */
+/* special exception:                                                     */
+/*                                                                        */
+/* Including LinCAN header files in a file, instantiating LinCAN generics */
+/* or templates, or linking other files with LinCAN objects to produce    */
+/* an application image/executable, does not by itself cause the          */
+/* resulting application image/executable to be covered by                */
+/* the GNU General Public License.                                        */
+/* This exception does not however invalidate any other reasons           */
+/* why the executable file might be covered by the GNU Public License.    */
+/* Publication of enhanced or derived LinCAN files is required although.  */
+/**************************************************************************/
 
 #ifdef CAN_WITH_RTL
 
@@ -14,8 +39,8 @@
 
 #include <rtl_malloc.h>
 
-/* 
- * Modifies Tx message processing 
+/*
+ * Modifies Tx message processing
  *  0 .. local message processing disabled
  *  1 .. local messages disabled by default but can be enabled by canque_set_filt
  *  2 .. local messages enabled by default, can be disabled by canque_set_filt
@@ -28,8 +53,9 @@ extern int processlocal;
 
 struct list_head canque_pending_edges_list;
 can_spinlock_t canque_pending_edges_lock;
+unsigned long canqueue_rtl2lin_pend;
 
-static int canqueue_rtl_irq = 0;
+int canqueue_rtl_irq = 0;
 
 void
 canqueue_rtl2lin_handler(int irq, void *ignore, struct pt_regs *ignoreregs)
@@ -39,7 +65,7 @@ canqueue_rtl2lin_handler(int irq, void *ignore, struct pt_regs *ignoreregs)
        unsigned pending_inops;
        unsigned pending_outops;
        int i;
-       
+
        can_spin_lock_irqsave (&canque_pending_edges_lock, flags);
 
        while(!list_empty(&canque_pending_edges_list)){
@@ -68,19 +94,22 @@ canqueue_rtl2lin_handler(int irq, void *ignore, struct pt_regs *ignoreregs)
                        if(pending_inops&1)
                                canque_notify_inends(qedge,i);
                }
-               
+
                canque_edge_decref(qedge);
                can_spin_lock_irqsave (&canque_pending_edges_lock, flags);
        }
 
        can_spin_unlock_irqrestore (&canque_pending_edges_lock, flags);
 
+       if(test_and_clear_bit(CAN_RTL2LIN_PEND_DEAD_b,&canqueue_rtl2lin_pend))
+               tasklet_schedule(&canque_dead_tl);
+
        return;
 }
 
 
 /**
- * canqueue_rtl2lin_check_and_pend - postpones edge notification if called from RT-Linux 
+ * canqueue_rtl2lin_check_and_pend - postpones edge notification if called from RT-Linux
  * @qends: notification target ends
  * @qedge: edge delivering notification
  * @what:  notification type
@@ -97,7 +126,7 @@ int canqueue_rtl2lin_check_and_pend(struct canque_ends_t *qends,
        if(rtl_rt_system_is_idle()) return 0;
 
        can_spin_lock_irqsave (&canque_pending_edges_lock, flags);
-       
+
        if(what>CANQUE_PENDOPS_LIMIT) what=CANQUE_PENDOPS_LIMIT;
 
        if(qends == qedge->inends) {
@@ -113,12 +142,117 @@ int canqueue_rtl2lin_check_and_pend(struct canque_ends_t *qends,
        }
 
        can_spin_unlock_irqrestore (&canque_pending_edges_lock, flags);
-       
+
        return 1;
 
 }
 
 
+/**
+ * canque_get_inslot4id_wait_rtl - find or wait for best outgoing edge and slot for given ID
+ * @qends: ends structure belonging to calling communication object
+ * @qedgep: place to store pointer to found edge
+ * @slotp: place to store pointer to  allocated slot
+ * @cmd: command type for slot
+ * @id: communication ID of message to send into edge
+ * @prio: optional priority of message
+ *
+ * Same as canque_get_inslot4id(), except, that it waits for free slot
+ * in case, that queue is full. Function is specific for Linux userspace clients.
+ * Return Value: If there is no usable edge negative value is returned.
+ */
+int canque_get_inslot4id_wait_rtl(struct canque_ends_t *qends,
+       struct canque_edge_t **qedgep, struct canque_slot_t **slotp,
+       int cmd, unsigned long id, int prio)
+{
+       rtl_irqstate_t flags;
+       int ret;
+       unsigned old_age;
+       rtl_sigset_t sigset;
+
+       old_age=atomic_read(&qends->endinfo.rtlinfo.rtl_writeq_age);
+       while((ret=canque_get_inslot4id(qends,qedgep,slotp,cmd,id,prio))==-1){
+               rtl_sigemptyset(&sigset);
+               rtl_spin_lock_irqsave(&qends->endinfo.rtlinfo.rtl_lock, flags);
+               if(old_age == atomic_read(&qends->endinfo.rtlinfo.rtl_writeq_age))
+                       sigset=rtl_wait_sleep(&qends->endinfo.rtlinfo.rtl_writeq, &qends->endinfo.rtlinfo.rtl_lock);
+               rtl_spin_unlock_irqrestore(&qends->endinfo.rtlinfo.rtl_lock, flags);
+               if(RTL_SIGINTR(&sigset))
+                       return -1;
+               old_age=atomic_read(&qends->endinfo.rtlinfo.rtl_writeq_age);
+       }
+
+       return ret;
+}
+
+
+/**
+ * canque_get_outslot_wait_rtl - receive or wait for ready slot for given ends
+ * @qends: ends structure belonging to calling communication object
+ * @qedgep: place to store pointer to found edge
+ * @slotp: place to store pointer to received slot
+ *
+ * The same as canque_test_outslot(), except it waits in the case, that there is
+ * no ready slot for given ends. Function is specific for Linux userspace clients.
+ * Return Value: Negative value informs, that there is no ready output
+ *     slot for given ends. Positive value is equal to the command
+ *     slot has been allocated by the input side.
+ */
+int canque_get_outslot_wait_rtl(struct canque_ends_t *qends,
+       struct canque_edge_t **qedgep, struct canque_slot_t **slotp)
+{
+       rtl_irqstate_t flags;
+       int ret;
+       unsigned old_age;
+       rtl_sigset_t sigset;
+
+       old_age=atomic_read(&qends->endinfo.rtlinfo.rtl_readq_age);
+       while((ret=canque_test_outslot(qends,qedgep,slotp))==-1){
+               rtl_sigemptyset(&sigset);
+               rtl_spin_lock_irqsave(&qends->endinfo.rtlinfo.rtl_lock, flags);
+               if(old_age == atomic_read(&qends->endinfo.rtlinfo.rtl_readq_age))
+                       sigset=rtl_wait_sleep(&qends->endinfo.rtlinfo.rtl_readq, &qends->endinfo.rtlinfo.rtl_lock);
+               rtl_spin_unlock_irqrestore(&qends->endinfo.rtlinfo.rtl_lock, flags);
+               if(RTL_SIGINTR(&sigset))
+                       return -1;
+               old_age=atomic_read(&qends->endinfo.rtlinfo.rtl_readq_age);
+       }
+       return ret;
+}
+
+
+/**
+ * canque_sync_wait_rtl - wait for all slots processing
+ * @qends: ends structure belonging to calling communication object
+ * @qedge: pointer to edge
+ *
+ * Functions waits for ends transition into empty state.
+ * Return Value: Positive value indicates, that edge empty state has been reached.
+ *     Negative or zero value informs about interrupted wait or other problem.
+ */
+int canque_sync_wait_rtl(struct canque_ends_t *qends, struct canque_edge_t *qedge)
+{
+       rtl_irqstate_t flags;
+       int ret;
+       unsigned old_age;
+       rtl_sigset_t sigset;
+
+       old_age=atomic_read(&qends->endinfo.rtlinfo.rtl_emptyq_age);
+       while(!(ret=canque_fifo_test_fl(&qedge->fifo,EMPTY)?1:0)){
+               rtl_sigemptyset(&sigset);
+               rtl_spin_lock_irqsave(&qends->endinfo.rtlinfo.rtl_lock, flags);
+               if(old_age == atomic_read(&qends->endinfo.rtlinfo.rtl_emptyq_age))
+                       sigset=rtl_wait_sleep(&qends->endinfo.rtlinfo.rtl_emptyq, &qends->endinfo.rtlinfo.rtl_lock);
+               rtl_spin_unlock_irqrestore(&qends->endinfo.rtlinfo.rtl_lock, flags);
+               if(RTL_SIGINTR(&sigset))
+                       return -1;
+               old_age=atomic_read(&qends->endinfo.rtlinfo.rtl_emptyq_age);
+       }
+
+       return ret;
+}
+
+
 /**
  * canque_fifo_init_rtl - initialize one CAN FIFO
  * @fifo: pointer to the FIFO structure
@@ -138,6 +272,7 @@ int canque_fifo_init_rtl(struct canque_fifo_t *fifo, int slotsnr)
        return canque_fifo_init_slots(fifo);
 }
 
+
 /**
  * canque_fifo_done_rtl - frees slots allocated for CAN FIFO
  * @fifo: pointer to the FIFO structure
@@ -197,16 +332,17 @@ void canque_ends_free_rtl(struct canque_ends_t *qends)
 /**
  * canqueue_notify_rtl - notification callback handler for Linux userspace clients
  * @qends: pointer to the callback side ends structure
- * @qedge: edge which invoked notification 
+ * @qedge: edge which invoked notification
  * @what: notification type
  */
 void canqueue_notify_rtl(struct canque_ends_t *qends, struct canque_edge_t *qedge, int what)
 {
        rtl_irqstate_t flags;
-       
+
        switch(what){
                case CANQUEUE_NOTIFY_EMPTY:
                        rtl_spin_lock_irqsave(&qends->endinfo.rtlinfo.rtl_lock, flags);
+                       atomic_inc(&qends->endinfo.rtlinfo.rtl_emptyq_age);
                        rtl_wait_wakeup(&qends->endinfo.rtlinfo.rtl_emptyq);
                        rtl_spin_unlock_irqrestore(&qends->endinfo.rtlinfo.rtl_lock, flags);
                        if(canque_fifo_test_and_clear_fl(&qedge->fifo, FREEONEMPTY))
@@ -214,19 +350,28 @@ void canqueue_notify_rtl(struct canque_ends_t *qends, struct canque_edge_t *qedg
                        break;
                case CANQUEUE_NOTIFY_SPACE:
                        rtl_spin_lock_irqsave(&qends->endinfo.rtlinfo.rtl_lock, flags);
+                       atomic_inc(&qends->endinfo.rtlinfo.rtl_writeq_age);
                        rtl_wait_wakeup(&qends->endinfo.rtlinfo.rtl_writeq);
                        rtl_spin_unlock_irqrestore(&qends->endinfo.rtlinfo.rtl_lock, flags);
                        break;
                case CANQUEUE_NOTIFY_PROC:
                        rtl_spin_lock_irqsave(&qends->endinfo.rtlinfo.rtl_lock, flags);
+                       atomic_inc(&qends->endinfo.rtlinfo.rtl_readq_age);
                        rtl_wait_wakeup(&qends->endinfo.rtlinfo.rtl_readq);
                        rtl_spin_unlock_irqrestore(&qends->endinfo.rtlinfo.rtl_lock, flags);
                        break;
                case CANQUEUE_NOTIFY_NOUSR:
                        rtl_spin_lock_irqsave(&qends->endinfo.rtlinfo.rtl_lock, flags);
+
+                       atomic_inc(&qends->endinfo.rtlinfo.rtl_readq_age);
                        rtl_wait_wakeup(&qends->endinfo.rtlinfo.rtl_readq);
+
+                       atomic_inc(&qends->endinfo.rtlinfo.rtl_writeq_age);
                        rtl_wait_wakeup(&qends->endinfo.rtlinfo.rtl_writeq);
+
+                       atomic_inc(&qends->endinfo.rtlinfo.rtl_emptyq_age);
                        rtl_wait_wakeup(&qends->endinfo.rtlinfo.rtl_emptyq);
+
                        rtl_spin_unlock_irqrestore(&qends->endinfo.rtlinfo.rtl_lock, flags);
                        break;
                case CANQUEUE_NOTIFY_DEAD_WANTED:
@@ -252,8 +397,9 @@ int canqueue_ends_init_rtl(struct canque_ends_t *qends)
        rtl_wait_init(&(qends->endinfo.rtlinfo.rtl_readq));
        rtl_wait_init(&(qends->endinfo.rtlinfo.rtl_writeq));
        rtl_wait_init(&(qends->endinfo.rtlinfo.rtl_emptyq));
-       
+
        qends->notify=canqueue_notify_rtl;
+       qends->endinfo.rtlinfo.pend_flags=0;
        return 0;
 }
 
@@ -276,7 +422,7 @@ int canqueue_ends_dispose_rtl(struct canque_ends_t *qends, int sync)
        /*Wait for sending of all pending messages in the output FIFOs*/
        /*if(sync)
                canqueue_ends_sync_all_rtl(qends);*/
-       
+
        /* Finish or kill all outgoing edges listed in inends */
        delayed=canqueue_ends_kill_inlist(qends, 1);
        /* Kill all incoming edges listed in outends */
@@ -300,6 +446,9 @@ int canqueue_ends_dispose_rtl(struct canque_ends_t *qends, int sync)
 
 
 
+/**
+ * canqueue_rtl_initialize - initialization of global RT-Linux specific features
+ */
 void canqueue_rtl_initialize(void)
 {
        INIT_LIST_HEAD(&canque_pending_edges_list);
@@ -309,6 +458,9 @@ void canqueue_rtl_initialize(void)
 }
 
 
+/**
+ * canqueue_rtl_done - finalization of glopal RT-Linux specific features
+ */
 void canqueue_rtl_done(void)
 {
        rtl_free_soft_irq (canqueue_rtl_irq);