]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/read_rtl.c
Actual driver code for directly mapped SJA1000 into PCI mem region 0.
[lincan.git] / lincan / src / read_rtl.c
1 /**************************************************************************/
2 /* File: read_rtl.c - RT-Linux variant of read systemcall                 */
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 /* This is the 'Normal' read handler for normal transmission messages */
45 static inline
46 ssize_t can_std_read_rtl(struct canque_ends_t *qends, int nonblock_fl,
47                         char *buffer, size_t length)
48 {
49         int ret;
50         struct canmsg_t *msg_buff = (struct canmsg_t *)buffer;
51         int bytes_to_copy;
52         struct canque_edge_t *qedge;
53         struct canque_slot_t *slot;
54
55         ret=canque_test_outslot(qends, &qedge, &slot);
56         if(ret<0){
57                 if (nonblock_fl) {
58                         return -EAGAIN;
59                 }
60                 ret=canque_get_outslot_wait_rtl(qends, &qedge, &slot);
61                 if(ret<0){
62                         if (ret==-1) {
63                                 DEBUGMSG("Rx interrupted\n");
64                                 return -EINTR;
65                         }
66
67                         return -EIO;
68                 }
69         }
70         *(msg_buff++)=slot->msg;
71         canque_free_outslot(qends, qedge, slot);
72         bytes_to_copy = length-sizeof(struct canmsg_t);
73
74         while (bytes_to_copy > 0) {
75                 ret=canque_test_outslot(qends, &qedge, &slot);
76                 if(ret<0)
77                         break;
78                 *(msg_buff++)=slot->msg;
79                 canque_free_outslot(qends, qedge, slot);
80                 bytes_to_copy -= sizeof(struct canmsg_t);
81         }
82
83         return length-bytes_to_copy;
84 }
85
86
87 ssize_t can_read_rtl_posix(struct rtl_file *fptr, char *buffer,
88                                 size_t length, loff_t *ppos)
89 {
90         struct canuser_t *canuser =
91                 (struct canuser_t *)can_get_rtl_file_private_data(fptr);
92         struct canque_ends_t *qends;
93         int      ret;
94
95         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
96                 CANMSG("can_read_rtl_posix: bad canuser magic\n");
97                 return -ENODEV;
98         }
99
100         if (length < sizeof(struct canmsg_t)) {
101                 DEBUGMSG("Trying to read less bytes than a CAN message, \n");
102                 DEBUGMSG("this will always return zero.\n");
103                 return 0;
104         }
105
106         qends = canuser->qends;
107
108         /*if (((struct canmsg_t *)buffer)->flags & MSG_RTR)
109                 ret = can_rtr_read(chip, obj, buffer);
110         else*/
111                 ret = can_std_read_rtl(qends, fptr->f_flags & O_NONBLOCK,
112                         buffer, length);
113
114         return ret;
115 }
116
117 #endif /*CAN_WITH_RTL*/
118