]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/write_rtl.c
LinCAN sources go through big white-space cleanup.
[lincan.git] / lincan / src / write_rtl.c
1 /**************************************************************************/
2 /* File: write_rtl.c - RT-Linux write systemcall variant                  */
3 /*                                                                        */
4 /* LinCAN - (Not only) Linux CAN bus driver                               */
5 /* Copyright (C) 2002-2009 DCE FEE CTU Prague <http://dce.felk.cvut.cz>   */
6 /* Copyright (C) 2002-2009 Pavel Pisa <pisa@cmp.felk.cvut.cz>             */
7 /* Funded by OCERA and FRESCOR IST projects                               */
8 /* Based on CAN driver code by Arnaud Westenberg <arnaud@wanadoo.nl>      */
9 /*                                                                        */
10 /* LinCAN is free software; you can redistribute it and/or modify it      */
11 /* under terms of the GNU General Public License as published by the      */
12 /* Free Software Foundation; either version 2, or (at your option) any    */
13 /* later version.  LinCAN is distributed in the hope that it will be      */
14 /* useful, but WITHOUT ANY WARRANTY; without even the implied warranty    */
15 /* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU    */
16 /* General Public License for more details. You should have received a    */
17 /* copy of the GNU General Public License along with LinCAN; see file     */
18 /* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,  */
19 /* Cambridge, MA 02139, USA.                                              */
20 /*                                                                        */
21 /* To allow use of LinCAN in the compact embedded systems firmware        */
22 /* and RT-executives (RTEMS for example), main authors agree with next    */
23 /* special exception:                                                     */
24 /*                                                                        */
25 /* Including LinCAN header files in a file, instantiating LinCAN generics */
26 /* or templates, or linking other files with LinCAN objects to produce    */
27 /* an application image/executable, does not by itself cause the          */
28 /* resulting application image/executable to be covered by                */
29 /* the GNU General Public License.                                        */
30 /* This exception does not however invalidate any other reasons           */
31 /* why the executable file might be covered by the GNU Public License.    */
32 /* Publication of enhanced or derived LinCAN files is required although.  */
33 /**************************************************************************/
34
35 #ifdef CAN_WITH_RTL
36
37 #include "../include/can.h"
38 #include "../include/can_sysdep.h"
39 #include "../include/main.h"
40
41 #include <rtl_posixio.h>
42 #include "../include/can_iortl.h"
43
44 ssize_t can_write_rtl_posix(struct rtl_file *fptr, const char *buffer,
45                                  size_t length, loff_t *ppos)
46 {
47         struct canuser_t *canuser =
48                 (struct canuser_t *)can_get_rtl_file_private_data(fptr);
49         const struct canmsg_t *msg_buff = (struct canmsg_t *)buffer;
50         struct canque_ends_t *qends;
51         struct canque_edge_t *qedge;
52         struct canque_slot_t *slot;
53         int      msg_flags;
54         int      ret;
55         size_t   bytes_to_copy;
56
57         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
58                 CANMSG("can_write_rtl_posix: bad canuser magic\n");
59                 return -ENODEV;
60         }
61
62         if (length < sizeof(struct canmsg_t)) {
63                 DEBUGMSG("Trying to write less bytes than a CAN message,\n");
64                 DEBUGMSG("this will always return 0 !\n");
65                 return 0;
66         }
67         if (length > INT_MAX) {
68                 CANMSG("Trying to write more than is supported.\n");
69                 return -1;
70         }
71
72         qends = canuser->qends;
73
74         msg_flags=msg_buff->flags;
75
76         /* Automatic selection of extended format if ID>2047 */
77         if (msg_buff->id & ~0x7ffl & MSG_ID_MASK ) msg_flags |= MSG_EXT;
78         /* has been dependent on "extended" option */
79
80         /* If the output buffer is full, return immediately in case O_NONBLOCK
81          * has been specified or loop until space becomes available.
82          */
83         if ((ret=canque_get_inslot4id(qends, &qedge, &slot,
84                         0, msg_buff->id, 0))<0){
85                 DEBUGMSG("Buffer is full\n");
86                 if(ret < -1)
87                         return -EIO;
88
89                 if (fptr->f_flags & O_NONBLOCK)
90                         return -EAGAIN;
91
92                 ret=canque_get_inslot4id_wait_rtl(qends, &qedge, &slot,
93                                                 0, msg_buff->id, 0);
94                 if (ret == -1)
95                         return -EINTR;
96
97                 if(ret<0) {
98                         return -EIO;
99                 }
100         }
101         slot->msg=*(msg_buff++);
102         slot->msg.flags=msg_flags;
103         canque_put_inslot(qends, qedge, slot);
104         bytes_to_copy = length-sizeof(struct canmsg_t);
105
106         /*
107          * Try to send more messages
108          */
109         while (bytes_to_copy >= sizeof(struct canmsg_t)) {
110                 bytes_to_copy -= sizeof(struct canmsg_t);
111                 msg_flags=msg_buff->flags;
112
113                 /* Automatic selection of extended format if ID>2047 */
114                 if (msg_buff->id & ~0x7ffl & MSG_ID_MASK ) msg_flags |= MSG_EXT;
115                 /* has been dependent on "extended" option */
116
117                 /* Get slot */
118                 if(canque_get_inslot4id(qends, &qedge, &slot,
119                         0, msg_buff->id, 0) < 0) break;
120                 slot->msg=*(msg_buff++);
121                 slot->msg.flags=msg_flags;
122                 canque_put_inslot(qends, qedge, slot);
123         }
124
125         if(fptr->f_flags & O_SYNC) {
126                 canque_sync_wait_rtl(qends, qedge);
127         }
128         return length-bytes_to_copy;
129 }
130
131 #endif /*CAN_WITH_RTL*/