]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/write.c
Added full RT-Linux POSIX interface to LinCAN driver, needs preparation of RT tests.
[lincan.git] / lincan / src / write.c
1 /* write.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
5  * email:pisa@cmp.felk.cvut.cz
6  * This software is released under the GPL-License.
7  * Version lincan-0.2  9 Jul 2003
8  */
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13
14 ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *offset)
15 {
16         struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
17         struct msgobj_t *obj;
18         struct canmsg_t msg_buff;
19         struct canque_ends_t *qends;
20         struct canque_edge_t *qedge;
21         struct canque_slot_t *slot;
22         int ret = 0;
23         unsigned bytes_to_copy;
24
25         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
26                 CANMSG("can_write: bad canuser magic\n");
27                 return -ENODEV;
28         }
29
30         if (length < sizeof(struct canmsg_t)) {
31                 DEBUGMSG("Trying to write less bytes than a CAN message,\n");
32                 DEBUGMSG("this will always return 0 !\n");
33                 return 0;
34         }
35         if (length > INT_MAX) {
36                 CANMSG("Trying to write more than is supported.\n");
37                 return -1;
38         }
39         if (length % sizeof(struct canmsg_t)) {
40                 CANMSG("The number of bytes requested to be written is not a multiple of\n");
41                 CANMSG("'sizeof(struct canmsg_t)', currently this is not allowed.\n");
42                 return -1;
43         } 
44
45         /* Initialize hardware pointers */
46         obj = canuser->msgobj;
47         if (obj == NULL) {
48                 CANMSG("Could not assign buffer structure\n");
49                 return -1;
50         }
51         qends = canuser->qends;
52
53
54         /* Prepare first message */
55         copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
56         /* Automatic selection of extended format if "extended" set and ID>2047 */
57         if (extended) if (msg_buff.id & ~0x7ffl ) msg_buff.flags |= MSG_EXT;
58
59         /* If the output buffer is full, return immediately in case O_NONBLOCK
60          * has been specified or loop until space becomes available.
61          */
62         if ((ret=canque_get_inslot4id(qends, &qedge, &slot, 
63                         0, msg_buff.id, 0))<0){
64                 DEBUGMSG("Buffer is full\n");
65                 if(ret < -1)
66                         return -EIO;
67
68                 if (file->f_flags & O_NONBLOCK)
69                         return -EAGAIN;
70
71                 ret=canque_get_inslot4id_wait_kern(qends, &qedge, &slot, 
72                                                 0, msg_buff.id, 0);
73                 if(ret<0) {
74                         if (signal_pending(current))
75                                 return -EINTR;
76                         /*if (!can_timeout)*/
77                         return -EIO;
78                 }
79         }
80         slot->msg=msg_buff;
81         canque_put_inslot(qends, qedge, slot);
82         buffer += sizeof(struct canmsg_t);
83         bytes_to_copy = length-sizeof(struct canmsg_t);
84
85         /* 
86          * Try to send more messages
87          */
88         while (bytes_to_copy >= sizeof(struct canmsg_t)) {
89                 bytes_to_copy -= sizeof(struct canmsg_t);
90                 /* Prepare first message */
91                 copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
92                 /* Automatic selection of extended format if "extended" set and ID>2047 */
93                 if (extended) if (msg_buff.id & ~0x7ffl ) msg_buff.flags |= MSG_EXT;
94                 /* Get slot */
95                 if(canque_get_inslot4id(qends, &qedge, &slot, 
96                         0, msg_buff.id, 0) < 0) break;
97                 slot->msg=msg_buff;
98                 canque_put_inslot(qends, qedge, slot);
99                 buffer += sizeof(struct canmsg_t);
100         }
101
102         if(file->f_flags & O_SYNC) {
103                 canque_sync_wait_kern(qends, qedge);
104         }
105         return length-bytes_to_copy;
106 }