]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/write.c
Merge: Changes, which should enable to handle more VME Unican cards under RT-Linux.
[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.3  17 Jun 2004
8  */
9
10 #include "../include/can.h"
11 #include "../include/can_sysdep.h"
12 #include "../include/main.h"
13 #include "../include/write.h"
14
15 ssize_t can_write(struct file *file, const char *buffer, size_t length, loff_t *offset)
16 {
17         struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
18         struct msgobj_t *obj;
19         struct canmsg_t msg_buff;
20         struct canque_ends_t *qends;
21         struct canque_edge_t *qedge;
22         struct canque_slot_t *slot;
23         int ret = 0;
24         unsigned bytes_to_copy;
25
26         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
27                 CANMSG("can_write: bad canuser magic\n");
28                 return -ENODEV;
29         }
30
31         if (length < sizeof(struct canmsg_t)) {
32                 DEBUGMSG("Trying to write less bytes than a CAN message,\n");
33                 DEBUGMSG("this will always return 0 !\n");
34                 return 0;
35         }
36         if (length > INT_MAX) {
37                 CANMSG("Trying to write more than is supported.\n");
38                 return -1;
39         }
40         if (length % sizeof(struct canmsg_t)) {
41                 CANMSG("The number of bytes requested to be written is not a multiple of\n");
42                 CANMSG("'sizeof(struct canmsg_t)', currently this is not allowed.\n");
43                 return -1;
44         } 
45
46         /* Initialize hardware pointers */
47         obj = canuser->msgobj;
48         if (obj == NULL) {
49                 CANMSG("Could not assign buffer structure\n");
50                 return -1;
51         }
52         qends = canuser->qends;
53
54
55         /* Prepare first message */
56         copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
57
58         /* Automatic selection of extended format if ID>2047 */
59         if (msg_buff.id & ~0x7ffl & MSG_ID_MASK ) msg_buff.flags |= MSG_EXT;
60         /* has been dependent on "extended" option */
61
62         /* If the output buffer is full, return immediately in case O_NONBLOCK
63          * has been specified or loop until space becomes available.
64          */
65         if ((ret=canque_get_inslot4id(qends, &qedge, &slot, 
66                         0, msg_buff.id, 0))<0){
67                 DEBUGMSG("Buffer is full\n");
68                 if(ret < -1)
69                         return -EIO;
70
71                 if (file->f_flags & O_NONBLOCK)
72                         return -EAGAIN;
73
74                 ret=canque_get_inslot4id_wait_kern(qends, &qedge, &slot, 
75                                                 0, msg_buff.id, 0);
76                 if(ret<0) {
77                         if (signal_pending(current))
78                                 return -EINTR;
79                         /*if (!can_timeout)*/
80                         return -EIO;
81                 }
82         }
83         slot->msg=msg_buff;
84         canque_put_inslot(qends, qedge, slot);
85         buffer += sizeof(struct canmsg_t);
86         bytes_to_copy = length-sizeof(struct canmsg_t);
87
88         /* 
89          * Try to send more messages
90          */
91         while (bytes_to_copy >= sizeof(struct canmsg_t)) {
92                 bytes_to_copy -= sizeof(struct canmsg_t);
93                 /* Prepare first message */
94                 copy_from_user(&msg_buff, buffer, sizeof(struct canmsg_t));
95
96                 /* Automatic selection of extended format if ID>2047 */
97                 if (msg_buff.id & ~0x7ffl & MSG_ID_MASK ) msg_buff.flags |= MSG_EXT;
98                 /* has been dependent on "extended" option */
99
100                 /* Get slot */
101                 if(canque_get_inslot4id(qends, &qedge, &slot, 
102                         0, msg_buff.id, 0) < 0) break;
103                 slot->msg=msg_buff;
104                 canque_put_inslot(qends, qedge, slot);
105                 buffer += sizeof(struct canmsg_t);
106         }
107
108         if(file->f_flags & O_SYNC) {
109                 canque_sync_wait_kern(qends, qedge);
110         }
111         return length-bytes_to_copy;
112 }