USB LinCAN support reworked to use lists instead of arrays.
authorppisa <pisa@cmp.felk.cvut.cz>
Mon, 2 Feb 2009 21:06:53 +0000 (22:06 +0100)
committerppisa <pisa@cmp.felk.cvut.cz>
Mon, 2 Feb 2009 21:06:53 +0000 (22:06 +0100)
This solves problems with incorrect order of received
messages. This version is working, but needs more
cleanups still.

Signed-off-by: ppisa <pisa@cmp.felk.cvut.cz>
lincan/include/usbcan.h
lincan/src/usbcan.c

index 36c3753..f29f9b8 100644 (file)
@@ -127,16 +127,16 @@ struct usbcan_message {
        struct urb      *u;
        struct usbcan_usb *dev;
        u8      msg[USBCAN_TRANSFER_SIZE];
-       spinlock_t              acc;            /* access lock */
        struct canque_edge_t *qedge;
        struct canque_slot_t *slot;
+       struct list_head list_node;
        volatile long   flags;
 };
 
-#define USBCAN_MESSAGE_FREE                                    (1)
-#define USBCAN_MESSAGE_URB_PENDING             (2)
+#define USBCAN_MESSAGE_FREE                            (1)
+#define USBCAN_MESSAGE_URB_PENDING                     (2)
 #define USBCAN_MESSAGE_TERMINATE                       (3)
-#define USBCAN_MESSAGE_ERROR                                   (4)
+#define USBCAN_MESSAGE_ERROR                           (4)
 #define USBCAN_MESSAGE_DATA_OK                         (5)
 #define USBCAN_MESSAGE_TYPE_RX                         (6)
 #define USBCAN_MESSAGE_TYPE_TX                         (7)
@@ -150,10 +150,16 @@ struct usbcan_usb {
        u8                      bulk_in_endpointAddr;   /* the address of the bulk in endpoint */
        u8                      bulk_out_endpointAddr;  /* the address of the bulk out endpoint */
        struct mutex            io_mutex;               /* synchronize I/O with disconnect */
-       struct usbcan_message   rx[USBCAN_TOT_RX_URBS];
-       struct usbcan_message   tx[USBCAN_TOT_TX_URBS];
 
-       struct task_struct *comthread;                      /* usb communication kernel thread  */
+       struct list_head        rx_pend_list;           /* URBs waiting for data receive */
+       struct list_head        rx_ready_list;          /* URBs with valid received data */
+       struct list_head        tx_idle_list;           /* URBs prepared to hold Tx messages */
+       struct list_head        tx_pend_list;           /* URBs holding Tx messages in progress */
+       struct list_head        tx_ready_list;          /* URBs with yet confirmed Tx messages */
+
+       spinlock_t              list_lock;                      /* list lock */
+
+       struct task_struct *comthread;                  /* usb communication kernel thread  */
        wait_queue_head_t queue;
 
        struct canchip_t *chip;
index f67f252..01d2f90 100644 (file)
@@ -683,6 +683,18 @@ int usbcan_config_irqs(struct canchip_t *chip, short irqs)
        return -ENOSYS;
 }
 
+
+static void usbcan_usb_message_move_list(struct usbcan_usb *dev,
+                       struct usbcan_message *m, struct list_head *head)
+{
+       can_spin_irqflags_t flags;
+       can_spin_lock_irqsave(&dev->list_lock, flags);
+       list_del(&m->list_node);
+       list_add_tail(&m->list_node, head);
+       can_spin_unlock_irqrestore(&dev->list_lock, flags);
+}
+
+
 /**
  * usbcan_kthread_read_handler: - part of kthread code responsible for receive completed events
  * @dev: pointer to usb device related structure
@@ -694,44 +706,51 @@ int usbcan_config_irqs(struct canchip_t *chip, short irqs)
  * usbcan_kthread().
  * File: src/usbcan.c
  */
-void usbcan_kthread_read_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
-       int i, j, len, retval;
+void usbcan_kthread_read_handler(struct usbcan_usb *dev, struct usbcan_message *m,
+                               struct msgobj_t *obj)
+{
+       int i, len, retval;
+       u8 *ptr;
+
        DEBUGMSG("USBCAN RX handler\n");
-       for (i=0;i<USBCAN_TOT_RX_URBS;i++){
-               if (test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&dev->rx[i].flags)){
-                       DEBUGMSG("USBCAN Thread has received a message\n");
-                       if ((dev->chip)&&(dev->chip->flags & CHIP_CONFIGURED)){
-                               u8 *ptr;
-                               struct usbcan_message *mess=&dev->rx[i];
 
-                               len=*(u8 *)(mess->msg+1);
-                               if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
-                               obj->rx_msg.length = len;
+       if (!test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&m->flags)) {
+               CANMSG("Strange, Rx handler USBCAN_MESSAGE_DATA_OK not set\n");
+               goto skip_msg;
+       }
 
-                               obj->rx_msg.flags=le16_to_cpu(*(u16 *)(mess->msg+2));
-                               obj->rx_msg.id=le32_to_cpu((*(u32 *)(mess->msg+4)));
+       if (!(dev->chip)||!(dev->chip->flags & CHIP_CONFIGURED)) {
+               CANMSG("Destination chip not found\n");
+               goto skip_msg;
+       }
 
-                               for(ptr=mess->msg+8,j=0; j < len; ptr++,j++) {
-                                       obj->rx_msg.data[j]=*ptr;
-                               }
 
-                               // fill CAN message timestamp
-                               can_filltimestamp(&obj->rx_msg.timestamp);
-                               canque_filter_msg2edges(obj->qends, &obj->rx_msg);
-                       }
-                       else
-                               CANMSG("Destination chip not found\n");
-               }
-               if (!test_bit(USBCAN_MESSAGE_URB_PENDING,&dev->rx[i].flags)){
-                       DEBUGMSG("Renewing RX urb\n");
-                       retval = usb_submit_urb (dev->rx[i].u, GFP_KERNEL);
-                       if (retval<0){
-                               CANMSG("%d. URB error %d\n", i, retval);
-                               set_bit(USBCAN_ERROR,&dev->flags);
-                       }
-                       else
-                               set_bit(USBCAN_MESSAGE_URB_PENDING,&dev->rx[i].flags);
-               }
+       DEBUGMSG("USBCAN Thread has received a message\n");
+
+       len=*(u8 *)(m->msg+1);
+       if(len > CAN_MSG_LENGTH) len = CAN_MSG_LENGTH;
+       obj->rx_msg.length = len;
+
+       obj->rx_msg.flags=le16_to_cpu(*(u16 *)(m->msg+2));
+       obj->rx_msg.id=le32_to_cpu((*(u32 *)(m->msg+4)));
+
+       for(ptr=m->msg+8,i=0; i < len; ptr++,i++) {
+               obj->rx_msg.data[i]=*ptr;
+       }
+
+       // fill CAN message timestamp
+       can_filltimestamp(&obj->rx_msg.timestamp);
+       canque_filter_msg2edges(obj->qends, &obj->rx_msg);
+
+skip_msg:
+       set_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+       DEBUGMSG("Renewing RX urb\n");
+
+       usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
+       retval = usb_submit_urb (m->u, GFP_KERNEL);
+       if (retval<0) {
+               CANMSG("URB error %d\n", retval);
+               set_bit(USBCAN_ERROR,&dev->flags);
        }
 }
 
@@ -745,36 +764,40 @@ void usbcan_kthread_read_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
  * usbcan_kthread().
  * File: src/usbcan.c
  */
-void usbcan_kthread_write_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
-       int i;
-       DEBUGMSG("USBCAN TX handler\n");
-       for (i=0;i<USBCAN_TOT_TX_URBS;i++){
-               if (test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&dev->tx[i].flags)){
-                       struct usbcan_message *mess=&dev->tx[i];
-                       DEBUGMSG("USBCAN Message successfully sent\n");
-
-                       if(mess->slot){
-                               // Do local transmitted message distribution if enabled
-                               if (processlocal){
-                                       // fill CAN message timestamp
-                                       can_filltimestamp(&mess->slot->msg.timestamp);
-
-                                       mess->slot->msg.flags |= MSG_LOCAL;
-                                       canque_filter_msg2edges(obj->qends, &mess->slot->msg);
-                               }
-                               // Free transmitted slot
-                               canque_free_outslot(obj->qends, mess->qedge, mess->slot);
-                               mess->slot=NULL;
-                       }
-                       can_msgobj_clear_fl(obj,TX_PENDING);
+void usbcan_kthread_write_handler(struct usbcan_usb *dev, struct usbcan_message *m,
+                               struct msgobj_t *obj)
+{
+       if (!test_and_clear_bit(USBCAN_MESSAGE_DATA_OK,&m->flags)) {
+               CANMSG("Strange, Tx handler USBCAN_MESSAGE_DATA_OK not set\n");
+               goto skip_msg;
+       }
 
-                       set_bit(USBCAN_FREE_TX_URB,&dev->flags);
-                       set_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
+       DEBUGMSG("USBCAN Message successfully sent\n");
 
-                       // Test if some new messages arrived
-                       set_bit(USBCAN_TX_PENDING,&dev->flags);
+       if(m->slot){
+               // Do local transmitted message distribution if enabled
+               if (processlocal){
+                       // fill CAN message timestamp
+                       can_filltimestamp(&m->slot->msg.timestamp);
+
+                       m->slot->msg.flags |= MSG_LOCAL;
+                       canque_filter_msg2edges(obj->qends, &m->slot->msg);
                }
+               // Free transmitted slot
+               canque_free_outslot(obj->qends, m->qedge, m->slot);
+               m->slot=NULL;
        }
+       
+       /*FIXME - why there*/
+       can_msgobj_clear_fl(obj,TX_PENDING);
+
+skip_msg:
+       set_bit(USBCAN_FREE_TX_URB,&dev->flags);
+       set_bit(USBCAN_MESSAGE_FREE,&m->flags);
+
+       set_bit(USBCAN_TX_PENDING,&dev->flags);
+
+       usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
 }
 
 /**
@@ -789,52 +812,60 @@ void usbcan_kthread_write_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
  * File: src/usbcan.c
  */
 void usbcan_kthread_write_request_handler(struct usbcan_usb *dev, struct msgobj_t *obj){
-       int i, j, cmd, len, retval;
-       for (i=0;i<USBCAN_TOT_TX_URBS;i++){
-               if (test_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags)){
-                       struct usbcan_message *mess=&dev->tx[i];
-                       u8 *ptr;
-                       cmd=canque_test_outslot(obj->qends, &mess->qedge, &mess->slot);
-                       if(cmd>=0){
-                               DEBUGMSG("USBCAN Sending a message\n");
-
-                               can_msgobj_set_fl(obj,TX_PENDING);
-                               clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
-                               clear_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
-
-                               *(u8 *)(mess->msg)=0;
-                               len = mess->slot->msg.length;
-                               if(len > CAN_MSG_LENGTH)
-                                       len = CAN_MSG_LENGTH;
-                               *(u8 *)(mess->msg+1)=len & 0xFF;
-                               *(u16 *)(mess->msg+2)=cpu_to_le16(mess->slot->msg.flags);
-                               *(u32 *)(mess->msg+4)=cpu_to_le32(mess->slot->msg.id);
-
-                               for(ptr=mess->msg+8,j=0; j < len; ptr++,j++) {
-                                       *ptr=mess->slot->msg.data[j] & 0xFF;
-                               }
-                               for(; j < 8; ptr++,j++) {
-                                       *ptr=0;
-                               }
+       int i, cmd, len, retval;
+       u8 *ptr;
+       struct usbcan_message *m;
 
-                               set_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
-                               retval = usb_submit_urb (dev->tx[i].u, GFP_KERNEL);
-                               if (retval){
-                                       CANMSG("%d. URB error %d\n",i,retval);
-                                       clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
-                                       set_bit(USBCAN_FREE_TX_URB,&dev->flags);
-                                       set_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
-                                       obj->ret = -1;
-                                       canque_notify_inends(mess->qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
-                                       canque_free_outslot(obj->qends, mess->qedge, mess->slot);
-                                       mess->slot=NULL;
-                               }
-                       }
-                       else{
-                               set_bit(USBCAN_FREE_TX_URB,&dev->flags);
-                               break;
-                       }
+       if(list_empty(&dev->tx_idle_list)) {
+               clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
+               return;
+       }
+       
+       m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
+
+       cmd=canque_test_outslot(obj->qends, &m->qedge, &m->slot);
+       if(cmd>=0){
+               DEBUGMSG("USBCAN Sending a message\n");
+
+               can_msgobj_set_fl(obj,TX_PENDING);
+               clear_bit(USBCAN_FREE_TX_URB,&dev->flags);
+               clear_bit(USBCAN_MESSAGE_FREE,&m->flags);
+
+               *(u8 *)(m->msg)=0;
+               len = m->slot->msg.length;
+               if(len > CAN_MSG_LENGTH)
+               len = CAN_MSG_LENGTH;
+               *(u8 *)(m->msg+1)=len & 0xFF;
+               *(u16 *)(m->msg+2)=cpu_to_le16(m->slot->msg.flags);
+               *(u32 *)(m->msg+4)=cpu_to_le32(m->slot->msg.id);
+
+               for(ptr=m->msg+8, i=0; i < len; ptr++,i++) {
+                       *ptr=m->slot->msg.data[i] & 0xFF;
+               }
+               for(; i < 8; ptr++,i++) {
+                       *ptr=0;
+               }
+
+               set_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+
+               usbcan_usb_message_move_list(dev, m, &dev->tx_pend_list);
+
+               retval = usb_submit_urb (m->u, GFP_KERNEL);
+               if (retval){
+                       CANMSG("%d. URB error %d\n",i,retval);
+                       clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+                       set_bit(USBCAN_FREE_TX_URB,&dev->flags);
+                       set_bit(USBCAN_MESSAGE_FREE,&m->flags);
+                       obj->ret = -1;
+                       canque_notify_inends(m->qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
+                       canque_free_outslot(obj->qends, m->qedge, m->slot);
+                       m->slot=NULL;
+                       usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
+               } else {
+                       set_bit(USBCAN_TX_PENDING,&dev->flags);
                }
+       } else {
+               set_bit(USBCAN_FREE_TX_URB,&dev->flags);
        }
 }
 
@@ -1016,44 +1047,90 @@ static int usbcan_sleep_thread(struct usbcan_usb *dev)
        return rc;
 }
 
-static void usbcan_callback(struct urb *urb)
+static void usbcan_tx_callback(struct urb *urb)
 {
-       struct usbcan_message *mess = urb->context;
+       struct usbcan_message *m = urb->context;
        int retval;
 
-       if (!test_bit(USBCAN_THREAD_RUNNING,&mess->dev->flags))
+       if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
                return;
-       if (test_bit(USBCAN_MESSAGE_TERMINATE,&mess->flags))
+       if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
                return;
 
        switch (urb->status) {
        case 0:
                /* success */
                DEBUGMSG("%s > Message OK\n", __FUNCTION__);
-               set_bit(USBCAN_DATA_OK,&mess->dev->flags);
-               set_bit(USBCAN_MESSAGE_DATA_OK,&mess->flags);
-               if (test_bit(USBCAN_MESSAGE_TYPE_RX,&mess->flags)){
-                       DEBUGMSG("%s > RX flag set\n", __FUNCTION__);
-                       set_bit(USBCAN_DATA_RX,&mess->dev->flags);
-               }
-               if (test_bit(USBCAN_MESSAGE_TYPE_TX,&mess->flags))
-                       DEBUGMSG("%s > TX flag set\n", __FUNCTION__);
-                       set_bit(USBCAN_DATA_TX,&mess->dev->flags);
-               clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
-               if (test_bit(USBCAN_THREAD_RUNNING,&mess->dev->flags))
-                       wake_up_process(mess->dev->comthread);
+               set_bit(USBCAN_DATA_OK,&m->dev->flags);
+               set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
+               DEBUGMSG("%s > TX flag set\n", __FUNCTION__);
+               set_bit(USBCAN_DATA_TX,&m->dev->flags);
+               clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+               usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
+               if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
+                       wake_up_process(m->dev->comthread);
+               else
+                       CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
+               return;
+       case -ECONNRESET:
+       case -ENOENT:
+       case -ESHUTDOWN:
+               /* this urb is terminated, clean up */
+               CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
+               set_bit(USBCAN_TERMINATE,&m->dev->flags);
+               set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
+               clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+               return;
+       default:
+               //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
+               break;
+       }
+
+       // Try to send urb again on non significant errors
+       retval = usb_submit_urb (urb, GFP_ATOMIC);
+       if (retval<0){
+               CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
+               set_bit(USBCAN_ERROR,&m->dev->flags);
+               clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+               usbcan_usb_message_move_list(m->dev, m, &m->dev->tx_ready_list);
+               if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
+                       wake_up_process(m->dev->comthread);
+       }
+}
+
+static void usbcan_rx_callback(struct urb *urb)
+{
+       struct usbcan_message *m = urb->context;
+       int retval;
+
+       if (!test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
+               return;
+       if (test_bit(USBCAN_MESSAGE_TERMINATE,&m->flags))
+               return;
+
+       switch (urb->status) {
+       case 0:
+               /* success */
+               DEBUGMSG("%s > Message OK\n", __FUNCTION__);
+               set_bit(USBCAN_DATA_OK,&m->dev->flags);
+               set_bit(USBCAN_MESSAGE_DATA_OK,&m->flags);
+               DEBUGMSG("%s > RX flag set\n", __FUNCTION__);
+               set_bit(USBCAN_DATA_RX,&m->dev->flags);
+               clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+               usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
+               if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
+                       wake_up_process(m->dev->comthread);
                else
                        CANMSG("%s > USBCAN thread not running\n", __FUNCTION__);
-//                     wake_up(&mess->dev->queue);
                return;
        case -ECONNRESET:
        case -ENOENT:
        case -ESHUTDOWN:
                /* this urb is terminated, clean up */
                CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
-               set_bit(USBCAN_TERMINATE,&mess->dev->flags);
-               set_bit(USBCAN_MESSAGE_TERMINATE,&mess->flags);
-               clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
+               set_bit(USBCAN_TERMINATE,&m->dev->flags);
+               set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
+               clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
                return;
        default:
                //CANMSG("%s > Nonzero status received: %d\n", __FUNCTION__, urb->status);
@@ -1064,12 +1141,57 @@ static void usbcan_callback(struct urb *urb)
        retval = usb_submit_urb (urb, GFP_ATOMIC);
        if (retval<0){
                CANMSG("%s > Retrying urb failed with result %d\n", __FUNCTION__, retval);
-               set_bit(USBCAN_ERROR,&mess->dev->flags);
-               clear_bit(USBCAN_MESSAGE_URB_PENDING,&mess->flags);
-               if (test_bit(USBCAN_THREAD_RUNNING,&mess->dev->flags))
-                       wake_up_process(mess->dev->comthread);
-//                     wake_up(&mess->dev->queue);
+               set_bit(USBCAN_ERROR,&m->dev->flags);
+               clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+               usbcan_usb_message_move_list(m->dev, m, &m->dev->rx_ready_list);
+               if (test_bit(USBCAN_THREAD_RUNNING,&m->dev->flags))
+                       wake_up_process(m->dev->comthread);
+       }
+}
+
+
+static void usbcan_kthread_free_urbs(struct usbcan_usb *dev)
+{
+       while(!list_empty(&dev->rx_pend_list)) {
+               struct usbcan_message *m;
+               m = list_first_entry(&dev->rx_pend_list, typeof(*m), list_node);
+               set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
+               usb_kill_urb(m->u);
+               usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
+       }
+
+       while(!list_empty(&dev->tx_pend_list)) {
+               struct usbcan_message *m;
+               m = list_first_entry(&dev->tx_pend_list, typeof(*m), list_node);
+               set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
+               usb_kill_urb(m->u);
+               usbcan_usb_message_move_list(dev, m, &dev->tx_idle_list);
+       }
+
+       while(!list_empty(&dev->rx_ready_list)) {
+               struct usbcan_message *m;
+               m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
+               list_del(&m->list_node);
+               usb_free_urb(m->u);
+               kfree(m);
+       }
+
+       while(!list_empty(&dev->tx_ready_list)) {
+               struct usbcan_message *m;
+               m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
+               list_del(&m->list_node);
+               usb_free_urb(m->u);
+               kfree(m);
+       }
+
+       while(!list_empty(&dev->tx_idle_list)) {
+               struct usbcan_message *m;
+               m = list_first_entry(&dev->tx_idle_list, typeof(*m), list_node);
+               list_del(&m->list_node);
+               usb_free_urb(m->u);
+               kfree(m);
        }
+
 }
 
 int usbcan_kthread(void *data)
@@ -1078,61 +1200,90 @@ int usbcan_kthread(void *data)
        struct usbcan_usb *dev=(struct usbcan_usb *)data;
        struct msgobj_t *obj;
 
-  CANMSG("Usbcan thread started...\n");
+       CANMSG("Usbcan thread started...\n");
 
        if (!dev->chip)
                goto error;
        obj=dev->chip->msgobj[0];
 
+
+       INIT_LIST_HEAD(&dev->rx_pend_list);
+       INIT_LIST_HEAD(&dev->rx_ready_list);
+       INIT_LIST_HEAD(&dev->tx_idle_list);
+       INIT_LIST_HEAD(&dev->tx_pend_list);
+       INIT_LIST_HEAD(&dev->tx_ready_list);
+
        /* Prepare receive urbs  */
        for (i=0;i<USBCAN_TOT_RX_URBS;i++){
-               dev->rx[i].u = usb_alloc_urb(0, GFP_KERNEL);
-               if (!dev->rx[i].u){
+               struct usbcan_message *m;
+               struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
+               if (!u){
                        CANMSG("Error allocating %d. usb receive urb\n",i);
                        goto error;
                }
-               dev->rx[i].u->dev = dev->udev;
-               dev->rx[i].dev = dev;
-               usb_fill_bulk_urb(dev->rx[i].u, dev->udev,
-                               usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
-                               dev->rx[i].msg, USBCAN_TRANSFER_SIZE,
-                               usbcan_callback, &dev->rx[i]);
-               set_bit(USBCAN_MESSAGE_TYPE_RX,&dev->rx[i].flags);
+               m = kmalloc(sizeof(struct usbcan_message), GFP_KERNEL);
+               if(!m) {
+                       usb_free_urb(u);
+                       CANMSG("Error allocating %d. receive usbcan_message\n",i);
+                       goto error;
+               }
+               m->u = u;
+               u->dev = dev->udev;
+               m->dev = dev;
+               usb_fill_bulk_urb(u, dev->udev,
+                       usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
+                       m->msg, USBCAN_TRANSFER_SIZE, usbcan_rx_callback, m);
+               set_bit(USBCAN_MESSAGE_TYPE_RX, &m->flags);
+               list_add_tail(&m->list_node, &dev->rx_ready_list);
        }
 
        /* Prepare transmit urbs  */
        for (i=0;i<USBCAN_TOT_TX_URBS;i++){
-               dev->tx[i].u = usb_alloc_urb(0, GFP_KERNEL);
-               if (!dev->tx[i].u){
+               struct usbcan_message *m;
+               struct urb *u = usb_alloc_urb(0, GFP_KERNEL);
+               if (!u){
                        CANMSG("Error allocating %d. usb transmit urb\n",i);
                        goto error;
                }
-               dev->tx[i].u->dev = dev->udev;
-               dev->tx[i].dev = dev;
-               usb_fill_bulk_urb(dev->tx[i].u, dev->udev,
-                               usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
-                               dev->tx[i].msg, USBCAN_TRANSFER_SIZE,
-                               usbcan_callback, &dev->tx[i]);
-               set_bit(USBCAN_MESSAGE_FREE,&dev->tx[i].flags);
-               set_bit(USBCAN_MESSAGE_TYPE_TX,&dev->tx[i].flags);
+               m = kmalloc(sizeof(struct usbcan_message), GFP_KERNEL);
+               if(!m) {
+                       usb_free_urb(u);
+                       CANMSG("Error allocating %d. transmit usbcan_message\n",i);
+                       goto error;
+               }
+               m->u = u;
+               u->dev = dev->udev;
+               m->dev = dev;
+               usb_fill_bulk_urb(u, dev->udev,
+                       usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
+                       m->msg, USBCAN_TRANSFER_SIZE, usbcan_tx_callback, m);
+                       
+               set_bit(USBCAN_MESSAGE_FREE,&m->flags);
+               set_bit(USBCAN_MESSAGE_TYPE_TX,&m->flags);
+               list_add_tail(&m->list_node, &dev->tx_idle_list);
+
        }
 
        set_bit(USBCAN_THREAD_RUNNING,&dev->flags);
        set_bit(USBCAN_FREE_TX_URB,&dev->flags);
 
        for (i=0;i<USBCAN_TOT_RX_URBS;i++){
-               retval=usb_submit_urb(dev->rx[i].u, GFP_KERNEL);
+               struct usbcan_message *m;
+               m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
+               set_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
+               usbcan_usb_message_move_list(dev, m, &dev->rx_pend_list);
+
+               retval=usb_submit_urb(m->u, GFP_KERNEL);
                if (retval){
                        CANMSG("%d. URB error %d\n",i,retval);
                        set_bit(USBCAN_ERROR,&dev->flags);
+                       usbcan_usb_message_move_list(dev, m, &dev->rx_ready_list);
                        goto exit;
                }
-               else
-                       set_bit(USBCAN_MESSAGE_URB_PENDING,&dev->rx[i].flags);
        }
-  /* an endless loop in which we are doing our work */
-  for(;;)
-  {
+       /* an endless loop in which we are doing our work */
+       for(;;)
+       {
                /* We need to do a memory barrier here to be sure that
                the flags are visible on all CPUs. */
                mb();
@@ -1164,47 +1315,40 @@ int usbcan_kthread(void *data)
                        break;
                }
 
-               { /* Normal work to do */
-                       if (test_and_clear_bit(USBCAN_DATA_OK,&dev->flags)){
-                               DEBUGMSG("USBCAN Succesfull data transfer\n");
+               while(!list_empty(&dev->rx_ready_list)) {
+                       struct usbcan_message *m;
+                       m = list_first_entry(&dev->rx_ready_list, typeof(*m), list_node);
+                       usbcan_kthread_read_handler(dev, m, obj);
+               }
 
-                               if (test_and_clear_bit(USBCAN_DATA_RX,&dev->flags)){
-                                       usbcan_kthread_read_handler(dev, obj);
-                               }
-                               if (test_and_clear_bit(USBCAN_DATA_TX,&dev->flags)){
-                                       usbcan_kthread_write_handler(dev, obj);
-                               }
-                       }
-                       if (test_and_clear_bit(USBCAN_TX_PENDING,&dev->flags)){
-                               usbcan_kthread_write_request_handler(dev, obj);
-                       }
-    }
-  }
-       set_bit(USBCAN_TERMINATE,&dev->flags);
-exit:
-  /* here we go only in case of termination of the thread */
-       for (i=0;i<USBCAN_TOT_RX_URBS;i++){
-               if (dev->rx[i].u){
-                       set_bit(USBCAN_MESSAGE_TERMINATE,&dev->rx[i].flags);
-                       usb_kill_urb(dev->rx[i].u);
-                       usb_free_urb(dev->rx[i].u);
+               while(!list_empty(&dev->tx_ready_list)) {
+                       struct usbcan_message *m;
+                       m = list_first_entry(&dev->tx_ready_list, typeof(*m), list_node);
+                       usbcan_kthread_write_handler(dev, m, obj);
                }
-       }
-       for (i=0;i<USBCAN_TOT_TX_URBS;i++){
-               if (dev->tx[i].u){
-                       set_bit(USBCAN_MESSAGE_TERMINATE,&dev->tx[i].flags);
-                       usb_kill_urb(dev->tx[i].u);
-                       usb_free_urb(dev->tx[i].u);
+
+               if (test_and_clear_bit(USBCAN_TX_PENDING,&dev->flags)) {
+                       usbcan_kthread_write_request_handler(dev, obj);
                }
        }
+
+       set_bit(USBCAN_TERMINATE,&dev->flags);
+exit:
+  /* here we go only in case of termination of the thread */
+
+       usbcan_kthread_free_urbs(dev);
+
+
        clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
 
-  CANMSG ("usbcan thread finished!\n");
-  return 0;
+       CANMSG ("usbcan thread finished!\n");
+       return 0;
 error:
-  /* cleanup the thread, leave */
-  CANMSG ("kernel thread terminated!\n");
-  return -ENOMEM;
+       /* cleanup the thread, leave */
+       usbcan_kthread_free_urbs(dev);
+
+       CANMSG ("kernel thread terminated!\n");
+       return -ENOMEM;
 }
 
 static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id)
@@ -1252,6 +1396,7 @@ static int usbcan_probe(struct usb_interface *interface, const struct usb_device
                }
                memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
                dev=usbdevs->devs[j];
+               spin_lock_init(&usbdevs->devs[j]->list_lock);
 
                mutex_init(&dev->io_mutex);
                init_waitqueue_head(&dev->queue);