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
8 #include <linux/autoconf.h>
9 #if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
13 #if defined (MODVERSIONS)
14 #include <linux/modversions.h>
17 #include <linux/malloc.h>
18 #include <linux/version.h>
19 #include <asm/uaccess.h>
22 #include "../include/main.h"
23 #include "../include/read.h"
24 #include "../include/ioctl.h"
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)
31 int bytes_avail = 0, bytes_to_copy = 0;
34 if (fifo->rx_readp == fifo->rx_writep) { // Buffer is empty
35 if (file->f_flags & O_NONBLOCK) {
40 can_timeout = interruptible_sleep_on_timeout(&fifo->readq,
43 if (signal_pending(current)) {
44 DEBUGMSG("Rx interrupted\n");
48 DEBUGMSG("Rx timeout\n");
54 /* Calculate available bytes in the buffer */
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 +
62 bytes_to_copy = (length < bytes_avail) ? length : bytes_avail;
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);
71 if (fifo->rx_readp >= fifo->buf_rx_entry + MAX_BUF_LENGTH)
72 fifo->rx_readp = fifo->buf_rx_entry;
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,
83 struct rtr_id *rtr_current, *new_rtr_entry;
84 struct canmsg_t read_msg;
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,
95 hardware_p->rtr_queue=new_rtr_entry;
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;
104 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,2,19))
105 init_waitqueue(&new_rtr_entry->rtr_wq);
107 init_waitqueue_head(&new_rtr_entry->rtr_wq);
109 new_rtr_entry->id = read_msg.id;
110 new_rtr_entry->rtr_message = &read_msg;
111 new_rtr_entry->next=NULL;
113 spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
115 /* Send remote transmission request */
116 chip->chipspecops->remote_request(chip,obj);
118 interruptible_sleep_on(&new_rtr_entry->rtr_wq);
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;
126 hardware_p->rtr_queue=NULL;
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;
135 rtr_current->next=NULL;
137 spin_unlock_irqrestore(&hardware_p->rtr_lock, flags);
138 kfree(new_rtr_entry);
143 ssize_t can_read(struct file *file, char *buffer, size_t length, loff_t *offset)
145 struct msgobj_t *obj;
147 struct canfifo_t *fifo;
148 struct canmsg_t read_msg;
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");
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);
161 /* Initialize hardware pointers */
162 if ( (obj = objects_p[MINOR_NR]) == NULL) {
163 CANMSG("Could not assign buffer structure\n");
166 if ( (chip = obj->hostchip) == NULL) {
167 CANMSG("Device is not correctly configured,\n");
168 CANMSG("please reload the driver.\n");
171 if ( (fifo = obj->fifo) == NULL) {
172 CANMSG("Could not assign buffer memory.\n");
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);
180 ret = can_std_read(file, fifo, obj, buffer, length);