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