/* read.c
* Linux CAN-bus device driver.
* Written by Arnaud Westenberg email:arnaud@wanadoo.nl
+ * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
+ * email:pisa@cmp.felk.cvut.cz
* This software is released under the GPL-License.
- * Version 0.7 6 Aug 2001
+ * Version lincan-0.2 9 Jul 2003
*/
#define __NO_VERSION__
#include <linux/module.h>
#include <linux/autoconf.h>
-#if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
-#define MODVERSIONS
-#endif
-
-#if defined (MODVERSIONS)
-#include <linux/modversions.h>
-#endif
#include <linux/version.h>
-#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
+#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0))
#include <linux/malloc.h>
#else
#include <linux/slab.h>
#include "../include/ioctl.h"
/* This is the 'Normal' read handler for normal transmission messages */
-inline ssize_t can_std_read(struct file *file, struct canfifo_t *fifo,
+inline ssize_t can_std_read(struct file *file, struct canque_ends_t *qends,
struct msgobj_t *obj, char *buffer, size_t length)
{
- int can_timeout, ret;
- int bytes_avail = 0, bytes_to_copy = 0;
-
- cli();
- if (fifo->rx_readp == fifo->rx_writep) { // Buffer is empty
+ int ret;
+ int bytes_to_copy;
+ struct canque_edge_t *qedge;
+ struct canque_slot_t *slot;
+
+ ret=canque_test_outslot(qends, &qedge, &slot);
+ if(ret<0){
if (file->f_flags & O_NONBLOCK) {
- sti();
return -EAGAIN;
}
- obj->ret = 0;
- can_timeout = interruptible_sleep_on_timeout(&fifo->readq,
- CANTIMEOUT);
- sti();
- if (signal_pending(current)) {
- DEBUGMSG("Rx interrupted\n");
- return -EINTR;
- }
- if (!can_timeout) {
- DEBUGMSG("no data received\n");
- return 0;
+ ret=canque_get_outslot_wait_kern(qends, &qedge, &slot);
+ if(ret<0){
+ if (signal_pending(current)) {
+ DEBUGMSG("Rx interrupted\n");
+ return -EINTR;
+ }
+ /*if (!can_timeout) {
+ DEBUGMSG("no data received\n");
+ return 0;
+ }*/
+ return -EIO;
}
- if (obj->ret < 0)
- return obj->ret;
}
- /* Calculate available bytes in the buffer */
- cli();
- bytes_avail = ((int)fifo->rx_readp < (int)fifo->rx_writep) ?
- ((int)fifo->rx_writep - (int)fifo->rx_readp) :
- ((int)fifo->rx_writep - (int)fifo->rx_readp +
- (int)fifo->rx_size);
- sti();
-
- bytes_to_copy = (length < bytes_avail) ? length : bytes_avail;
- ret = bytes_to_copy;
-
- /* printk(KERN_CRIT "can RxFIFO b:%x e:%x bs:%x msg:%x rp:%x wp:%x btc:%x\n",
- fifo->buf_rx_entry, fifo->buf_rx_entry+MAX_BUF_LENGTH,
- fifo->rx_size, sizeof(struct canmsg_t),
- fifo->rx_readp, fifo->rx_writep, bytes_to_copy); */
-
-
- /* Copy the data to user space */
+
+ copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t));
+ canque_free_outslot(qends, qedge, slot);
+ buffer += sizeof(struct canmsg_t);
+ bytes_to_copy = length-sizeof(struct canmsg_t);
+
while (bytes_to_copy > 0) {
-
-
- copy_to_user(buffer, fifo->rx_readp, sizeof(struct canmsg_t));
+ ret=canque_test_outslot(qends, &qedge, &slot);
+ if(ret<0)
+ break;
+ copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t));
+ canque_free_outslot(qends, qedge, slot);
buffer += sizeof(struct canmsg_t);
bytes_to_copy -= sizeof(struct canmsg_t);
- fifo->rx_readp++;
- if (fifo->rx_readp >= fifo->buf_rx_entry + MAX_BUF_LENGTH)
- fifo->rx_readp = fifo->buf_rx_entry;
-
- /* printk(KERN_CRIT "can RxFIFO rp%x\n",fifo->rx_readp); */
}
- return ret;
+ return length-bytes_to_copy;
}
/* This is the 'RTR' read handler for remote transmission request messages */
ssize_t can_read(struct file *file, char *buffer, size_t length, loff_t *offset)
{
+ struct canuser_t *canuser = (struct canuser_t*)(file->private_data);
struct msgobj_t *obj;
struct chip_t *chip;
- struct canfifo_t *fifo;
struct canmsg_t read_msg;
+ struct canque_ends_t *qends;
int ret=0;
+ if(!canuser || (canuser->magic != CAN_USER_MAGIC)){
+ CANMSG("can_close: bad canuser magic\n");
+ return -ENODEV;
+ }
+
if (length < sizeof(struct canmsg_t)) {
DEBUGMSG("Trying to read less bytes than a CAN message, \n");
DEBUGMSG("this will always return zero.\n");
length = 8 * sizeof(struct canmsg_t);
}
/* Initialize hardware pointers */
- if ( (obj = objects_p[MINOR_NR]) == NULL) {
+ obj = canuser->msgobj;
+ if (obj == NULL) {
CANMSG("Could not assign buffer structure\n");
return -1;
}
+ qends = canuser->qends;
if ( (chip = obj->hostchip) == NULL) {
CANMSG("Device is not correctly configured,\n");
CANMSG("please reload the driver.\n");
return -1;
}
- if ( (fifo = obj->fifo) == NULL) {
- CANMSG("Could not assign buffer memory.\n");
- return -1;
- }
copy_from_user(&read_msg, buffer, sizeof(struct canmsg_t));
if (read_msg.flags & MSG_RTR)
ret = can_rtr_read(chip, obj, buffer);
else
- ret = can_std_read(file, fifo, obj, buffer, length);
+ ret = can_std_read(file, qends, obj, buffer, length);
return ret;
}