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
9 #include <linux/module.h>
11 #include <linux/autoconf.h>
12 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
16 #if defined (MODVERSIONS)
17 #include <linux/modversions.h>
20 #include <linux/version.h>
21 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
22 #include <linux/malloc.h>
24 #include <linux/slab.h>
26 #include <linux/version.h>
27 #include <asm/uaccess.h>
30 #include "../include/main.h"
31 #include "../include/read.h"
32 #include "../include/ioctl.h"
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)
39 int bytes_avail = 0, bytes_to_copy = 0;
42 if (fifo->rx_readp == fifo->rx_writep) { // Buffer is empty
43 if (file->f_flags & O_NONBLOCK) {
48 can_timeout = interruptible_sleep_on_timeout(&fifo->readq,
51 if (signal_pending(current)) {
52 DEBUGMSG("Rx interrupted\n");
56 DEBUGMSG("no data received\n");
62 /* Calculate available bytes in the buffer */
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 +
70 bytes_to_copy = (length < bytes_avail) ? length : bytes_avail;
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); */
79 /* Copy the data to user space */
80 while (bytes_to_copy > 0) {
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);
87 if (fifo->rx_readp >= fifo->buf_rx_entry + MAX_BUF_LENGTH)
88 fifo->rx_readp = fifo->buf_rx_entry;
90 /* printk(KERN_CRIT "can RxFIFO rp%x\n",fifo->rx_readp); */
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,
101 struct rtr_id *rtr_current, *new_rtr_entry;
102 struct canmsg_t read_msg;
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,
113 hardware_p->rtr_queue=new_rtr_entry;
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;
122 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,3,0))
123 init_waitqueue(&new_rtr_entry->rtr_wq);
125 init_waitqueue_head(&new_rtr_entry->rtr_wq);
127 new_rtr_entry->id = read_msg.id;
128 new_rtr_entry->rtr_message = &read_msg;
129 new_rtr_entry->next=NULL;
131 spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
133 /* Send remote transmission request */
134 chip->chipspecops->remote_request(chip,obj);
136 interruptible_sleep_on(&new_rtr_entry->rtr_wq);
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;
144 hardware_p->rtr_queue=NULL;
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;
153 rtr_current->next=NULL;
155 spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
156 kfree(new_rtr_entry);
161 ssize_t can_read(struct file *file, char *buffer, size_t length, loff_t *offset)
163 struct msgobj_t *obj;
165 struct canfifo_t *fifo;
166 struct canmsg_t read_msg;
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");
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);
179 /* Initialize hardware pointers */
180 if ( (obj = objects_p[MINOR_NR]) == NULL) {
181 CANMSG("Could not assign buffer structure\n");
184 if ( (chip = obj->hostchip) == NULL) {
185 CANMSG("Device is not correctly configured,\n");
186 CANMSG("please reload the driver.\n");
189 if ( (fifo = obj->fifo) == NULL) {
190 CANMSG("Could not assign buffer memory.\n");
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);
198 ret = can_std_read(file, fifo, obj, buffer, length);