Structured comments updated.
[lincan.git] / lincan / src / read_rtl.c
1 /* read.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 #ifdef CAN_WITH_RTL
11
12 #include "../include/can.h"
13 #include "../include/can_sysdep.h"
14 #include "../include/main.h"
15
16 #include <rtl_posixio.h>
17 #include "../include/can_iortl.h"
18
19 /* This is the 'Normal' read handler for normal transmission messages */
20 static inline 
21 ssize_t can_std_read_rtl(struct canque_ends_t *qends, int nonblock_fl, 
22                         char *buffer, size_t length)
23 {
24         int ret;
25         struct canmsg_t *msg_buff = (struct canmsg_t *)buffer;
26         int bytes_to_copy;
27         struct canque_edge_t *qedge;
28         struct canque_slot_t *slot;
29         
30         ret=canque_test_outslot(qends, &qedge, &slot);
31         if(ret<0){
32                 if (nonblock_fl) {
33                         return -EAGAIN;
34                 }
35                 ret=canque_get_outslot_wait_rtl(qends, &qedge, &slot);
36                 if(ret<0){
37                         if (ret==-1) {
38                                 DEBUGMSG("Rx interrupted\n");
39                                 return -EINTR;
40                         }
41
42                         return -EIO;
43                 }
44         }
45         *(msg_buff++)=slot->msg;
46         canque_free_outslot(qends, qedge, slot);
47         bytes_to_copy = length-sizeof(struct canmsg_t);
48         
49         while (bytes_to_copy > 0) {
50                 ret=canque_test_outslot(qends, &qedge, &slot);
51                 if(ret<0)
52                         break;
53                 *(msg_buff++)=slot->msg;
54                 canque_free_outslot(qends, qedge, slot);
55                 bytes_to_copy -= sizeof(struct canmsg_t);
56         }
57
58         return length-bytes_to_copy;
59 }
60
61
62 ssize_t can_read_rtl_posix(struct rtl_file *fptr, char *buffer,
63                                 size_t length, loff_t *ppos)
64 {
65         struct canuser_t *canuser = 
66                 (struct canuser_t *)can_get_rtl_file_private_data(fptr);
67         struct canque_ends_t *qends;
68         int      ret;
69
70         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
71                 CANMSG("can_read_rtl_posix: bad canuser magic\n");
72                 return -ENODEV;
73         }
74
75         if (length < sizeof(struct canmsg_t)) {
76                 DEBUGMSG("Trying to read less bytes than a CAN message, \n");
77                 DEBUGMSG("this will always return zero.\n");
78                 return 0;
79         }
80
81         qends = canuser->qends;
82
83         /*if (((struct canmsg_t *)buffer)->flags & MSG_RTR)
84                 ret = can_rtr_read(chip, obj, buffer);
85         else*/
86                 ret = can_std_read_rtl(qends, fptr->f_flags & O_NONBLOCK,
87                         buffer, length);
88
89         return ret;
90 }
91
92 #endif /*CAN_WITH_RTL*/
93