Actual driver code for directly mapped SJA1000 into PCI mem region 0.
[lincan.git] / lincan / src / read.c
1 /**************************************************************************/
2 /* File: read.c - read system call support for receiving CAN messages     */
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 #include "../include/can.h"
36 #include "../include/can_sysdep.h"
37 #include "../include/main.h"
38 #include "../include/read.h"
39
40 /* This is the 'Normal' read handler for normal transmission messages */
41 ssize_t can_read(struct file *file, char *buffer, size_t length, loff_t *offset)
42 {
43         struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
44         struct canque_ends_t *qends;
45         int bytes_to_copy;
46         struct canque_edge_t *qedge;
47         struct canque_slot_t *slot;
48         int ret;
49
50         if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
51                 CANMSG("can_read: bad canuser magic\n");
52                 return -ENODEV;
53         }
54
55         if (length < sizeof(struct canmsg_t)) {
56                 DEBUGMSG("Trying to read less bytes than a CAN message, \n");
57                 DEBUGMSG("this will always return zero.\n");
58                 return 0;
59         }
60
61         qends = canuser->qends;
62
63         ret=canque_test_outslot(qends, &qedge, &slot);
64         if(ret<0){
65                 if (file->f_flags & O_NONBLOCK) {
66                         return -EAGAIN;
67                 }
68                 ret=canque_get_outslot_wait_kern(qends, &qedge, &slot);
69                 if(ret<0){
70                         if (signal_pending(current)) {
71                                 DEBUGMSG("Rx interrupted\n");
72                                 return -EINTR;
73                         }
74                         /*if (!can_timeout) {
75                                 DEBUGMSG("no data received\n");
76                                 return 0;
77                         }*/
78                         return -EIO;
79                 }
80         }
81
82         ret = copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t));
83         canque_free_outslot(qends, qedge, slot);
84         buffer += sizeof(struct canmsg_t);
85         bytes_to_copy = length-sizeof(struct canmsg_t);
86         if(ret) return -EFAULT;
87
88         while (bytes_to_copy > 0) {
89                 ret=canque_test_outslot(qends, &qedge, &slot);
90                 if(ret<0)
91                         break;
92                 ret = copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t));
93                 canque_free_outslot(qends, qedge, slot);
94                 buffer += sizeof(struct canmsg_t);
95                 bytes_to_copy -= sizeof(struct canmsg_t);
96                 if(ret) return -EFAULT;
97         }
98
99         return length-bytes_to_copy;
100 }
101