]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/read.c
4e02801e31e8eac3c6eb61ce0c8fa68b90235ab9
[lincan.git] / lincan / src / read.c
1 /* read.c
2  * Linux CAN-bus device driver.
3  * Written by Arnaud Westenberg email:arnaud@wanadoo.nl
4  * This software is released under the GPL-License.
5  * Version 0.7  6 Aug 2001
6  */
7
8 #include <linux/autoconf.h>
9 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
10 #define MODVERSIONS
11 #endif
12
13 #if defined (MODVERSIONS)
14 #include <linux/modversions.h>
15 #endif
16
17 #include <linux/malloc.h>
18 #include <linux/version.h>
19 #include <asm/uaccess.h>
20 #include <asm/irq.h>
21
22 #include "../include/main.h"
23 #include "../include/read.h"
24 #include "../include/ioctl.h"
25
26 /* This is the 'Normal' read handler for normal transmission messages */
27 inline ssize_t can_std_read(struct file *file, struct canfifo_t *fifo, 
28                         struct msgobj_t *obj, char *buffer, size_t length)
29 {
30         int can_timeout, ret;
31         int bytes_avail = 0, bytes_to_copy = 0;
32
33         cli();
34         if (fifo->rx_readp == fifo->rx_writep) {        // Buffer is empty
35                 if (file->f_flags & O_NONBLOCK) {
36                         sti();
37                         return -EAGAIN;
38                 }
39                 obj->ret = 0;
40                 can_timeout = interruptible_sleep_on_timeout(&fifo->readq,
41                                                                 CANTIMEOUT);
42                 sti();
43                 if (signal_pending(current)) {
44                         DEBUGMSG("Rx interrupted\n");
45                         return -EINTR;
46                 }
47                 if (!can_timeout) {
48                         DEBUGMSG("Rx timeout\n");
49                         return -EIO;
50                 }
51                 if (obj->ret < 0)
52                         return obj->ret;
53         }
54         /* Calculate available bytes in the buffer */
55         cli();
56         bytes_avail = ((int)fifo->rx_readp < (int)fifo->rx_writep) ?
57                         ((int)fifo->rx_writep - (int)fifo->rx_readp) :
58                         ((int)fifo->rx_writep - (int)fifo->rx_readp + 
59                                                         (int)fifo->rx_size);
60         sti();
61                 
62         bytes_to_copy = (length < bytes_avail) ? length : bytes_avail;
63         ret = bytes_to_copy;
64
65         /* Copy the data to user space */
66         while (bytes_to_copy > 0) {
67                 copy_to_user(buffer, fifo->rx_readp, sizeof(struct canmsg_t));
68                 buffer += sizeof(struct canmsg_t);
69                 bytes_to_copy -= sizeof(struct canmsg_t);
70                 fifo->rx_readp++;
71                 if (fifo->rx_readp >= fifo->buf_rx_entry + MAX_BUF_LENGTH)
72                         fifo->rx_readp = fifo->buf_rx_entry;
73         }
74
75         return ret;
76 }
77
78 /* This is the 'RTR' read handler for remote transmission request messages */
79 inline ssize_t can_rtr_read(struct chip_t *chip, struct msgobj_t *obj, 
80                                                                 char *buffer)
81 {
82         unsigned long flags;
83         struct rtr_id *rtr_current, *new_rtr_entry;
84         struct canmsg_t read_msg;
85         
86         DEBUGMSG("Remote transmission request\n");
87         spin_lock_irqsave(&hardware_p->rtr_lock, flags);
88         if (hardware_p->rtr_queue == NULL) { //No remote messages pending
89                 new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC);
90                 if (new_rtr_entry == NULL) {
91                         spin_unlock_irqrestore(&hardware_p->rtr_lock, 
92                                                                 flags);
93                         return -ENOMEM;
94                 }
95                 hardware_p->rtr_queue=new_rtr_entry;
96         }
97         else {
98                 rtr_current=hardware_p->rtr_queue;
99                 while (rtr_current->next != NULL)
100                         rtr_current=rtr_current->next;
101                 new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC);
102                 rtr_current->next=new_rtr_entry;
103         }
104 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,2,19))
105         init_waitqueue(&new_rtr_entry->rtr_wq);
106 #else
107         init_waitqueue_head(&new_rtr_entry->rtr_wq);
108 #endif
109         new_rtr_entry->id = read_msg.id;
110         new_rtr_entry->rtr_message = &read_msg;
111         new_rtr_entry->next=NULL;
112
113         spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
114
115         /* Send remote transmission request */
116         chip->chipspecops->remote_request(chip,obj);
117         obj->ret = 0;
118         interruptible_sleep_on(&new_rtr_entry->rtr_wq);
119
120         spin_lock_irqsave(&hardware_p->rtr_lock, flags);
121         copy_to_user(buffer, &read_msg, sizeof(struct canmsg_t));
122         if (hardware_p->rtr_queue == new_rtr_entry) {
123                 if (new_rtr_entry->next != NULL) 
124                         hardware_p->rtr_queue=new_rtr_entry->next;
125                 else
126                         hardware_p->rtr_queue=NULL;
127         }
128         else {
129                 rtr_current=hardware_p->rtr_queue;
130                 while (rtr_current->next != new_rtr_entry)
131                         rtr_current=rtr_current->next;
132                 if (new_rtr_entry->next != NULL)
133                         rtr_current->next=new_rtr_entry->next;
134                 else
135                         rtr_current->next=NULL;
136         }
137         spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
138         kfree(new_rtr_entry);
139
140         return obj->ret;
141 }
142
143 ssize_t can_read(struct file *file, char *buffer, size_t length, loff_t *offset)
144 {
145         struct msgobj_t *obj;
146         struct chip_t *chip;
147         struct canfifo_t *fifo;
148         struct canmsg_t read_msg;
149         int ret=0;
150
151         if (length < sizeof(struct canmsg_t)) {
152                 DEBUGMSG("Trying to read less bytes than a CAN message, \n");
153                 DEBUGMSG("this will always return zero.\n");
154                 return 0;
155         }
156         if (length > 8 * sizeof(struct canmsg_t)) {
157                 DEBUGMSG("Reading more than 8 CAN messages, this is not supported.\n");
158                 DEBUGMSG("Defaulting to 8 messages.\n");
159                 length = 8 * sizeof(struct canmsg_t);
160         }
161         /* Initialize hardware pointers */
162         if ( (obj = objects_p[MINOR_NR]) == NULL) {
163                 CANMSG("Could not assign buffer structure\n");
164                 return -1;
165         }
166         if ( (chip = obj->hostchip) == NULL) {
167                 CANMSG("Device is not correctly configured,\n");
168                 CANMSG("please reload the driver.\n");
169                 return -1;
170         }
171         if ( (fifo = obj->fifo) == NULL) {
172                 CANMSG("Could not assign buffer memory.\n");
173                 return -1;
174         }
175
176         copy_from_user(&read_msg, buffer, sizeof(struct canmsg_t));
177         if (read_msg.flags & MSG_RTR)
178                 ret = can_rtr_read(chip, obj, buffer);
179         else
180                 ret = can_std_read(file, fifo, obj, buffer, length);
181
182         return ret;
183 }
184
185