]> rtime.felk.cvut.cz Git - lincan.git/blobdiff - lincan/src/write.c
Structured comments updated.
[lincan.git] / lincan / src / write.c
index 287400304fad118eb7a660af34689de62c081df0..1760bc2fdba740085e9b4f876414dfe4771010c7 100644 (file)
@@ -7,16 +7,8 @@
  * Version lincan-0.2  9 Jul 2003
  */
 
-#include <linux/autoconf.h>
-
-#define __NO_VERSION__
-#include <linux/module.h>
-#include <linux/kernel.h>
-#include <linux/sched.h>
-#include <linux/delay.h>
-#include <asm/irq.h>
-#include <asm/uaccess.h>
-
+#include "../include/can.h"
+#include "../include/can_sysdep.h"
 #include "../include/main.h"
 
 ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *offset)
@@ -28,10 +20,10 @@ ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *
        struct canque_edge_t *qedge;
        struct canque_slot_t *slot;
        int ret = 0;
-       int bytes_to_copy = 0;
+       unsigned bytes_to_copy;
 
        if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
-               CANMSG("can_close: bad canuser magic\n");
+               CANMSG("can_write: bad canuser magic\n");
                return -ENODEV;
        }
 
@@ -40,7 +32,7 @@ ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *
                DEBUGMSG("this will always return 0 !\n");
                return 0;
        }
-       if (length > 8 * sizeof(struct canmsg_t)) {
+       if (length > INT_MAX) {
                CANMSG("Trying to write more than is supported.\n");
                return -1;
        }
@@ -61,6 +53,8 @@ ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *
 
        /* Prepare first message */
        copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
+       /* Automatic selection of extended format if "extended" set and ID>2047 */
+       if (extended) if (msg_buff.id & ~0x7ffl ) msg_buff.flags |= MSG_EXT;
 
        /* If the output buffer is full, return immediately in case O_NONBLOCK
         * has been specified or loop until space becomes available.
@@ -68,11 +62,12 @@ ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *
        if ((ret=canque_get_inslot4id(qends, &qedge, &slot, 
                        0, msg_buff.id, 0))<0){
                DEBUGMSG("Buffer is full\n");
-               if (file->f_flags & O_NONBLOCK)
-                       return -EAGAIN;
                if(ret < -1)
                        return -EIO;
 
+               if (file->f_flags & O_NONBLOCK)
+                       return -EAGAIN;
+
                ret=canque_get_inslot4id_wait_kern(qends, &qedge, &slot, 
                                                0, msg_buff.id, 0);
                if(ret<0) {
@@ -90,16 +85,18 @@ ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *
        /* 
         * Try to send more messages
         */
-       while (bytes_to_copy > 0) {
+       while (bytes_to_copy >= sizeof(struct canmsg_t)) {
+               bytes_to_copy -= sizeof(struct canmsg_t);
                /* Prepare first message */
                copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
+               /* Automatic selection of extended format if "extended" set and ID>2047 */
+               if (extended) if (msg_buff.id & ~0x7ffl ) msg_buff.flags |= MSG_EXT;
                /* Get slot */
                if(canque_get_inslot4id(qends, &qedge, &slot, 
                        0, msg_buff.id, 0) < 0) break;
                slot->msg=msg_buff;
                canque_put_inslot(qends, qedge, slot);
                buffer += sizeof(struct canmsg_t);
-               bytes_to_copy -= sizeof(struct canmsg_t);
        }
 
         if(file->f_flags & O_SYNC) {