X-Git-Url: http://rtime.felk.cvut.cz/gitweb/lincan.git/blobdiff_plain/3129fcd8094edcb910b708463fdf1a234f11fb55..HEAD:/lincan/src/read.c diff --git a/lincan/src/read.c b/lincan/src/read.c index 92fcb68..500c9b3 100644 --- a/lincan/src/read.c +++ b/lincan/src/read.c @@ -1,40 +1,65 @@ -/* 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 lincan-0.2 9 Jul 2003 - */ - -#define __NO_VERSION__ -#include - -#include - -#include -#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)) -#include -#else -#include -#endif -#include -#include -#include - +/**************************************************************************/ +/* File: read.c - read system call support for receiving CAN messages */ +/* */ +/* LinCAN - (Not only) Linux CAN bus driver */ +/* Copyright (C) 2002-2009 DCE FEE CTU Prague */ +/* Copyright (C) 2002-2009 Pavel Pisa */ +/* Funded by OCERA and FRESCOR IST projects */ +/* Based on CAN driver code by Arnaud Westenberg */ +/* */ +/* LinCAN is free software; you can redistribute it and/or modify it */ +/* under terms of the GNU General Public License as published by the */ +/* Free Software Foundation; either version 2, or (at your option) any */ +/* later version. LinCAN is distributed in the hope that it will be */ +/* useful, but WITHOUT ANY WARRANTY; without even the implied warranty */ +/* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ +/* General Public License for more details. You should have received a */ +/* copy of the GNU General Public License along with LinCAN; see file */ +/* COPYING. If not, write to the Free Software Foundation, 675 Mass Ave, */ +/* Cambridge, MA 02139, USA. */ +/* */ +/* To allow use of LinCAN in the compact embedded systems firmware */ +/* and RT-executives (RTEMS for example), main authors agree with next */ +/* special exception: */ +/* */ +/* Including LinCAN header files in a file, instantiating LinCAN generics */ +/* or templates, or linking other files with LinCAN objects to produce */ +/* an application image/executable, does not by itself cause the */ +/* resulting application image/executable to be covered by */ +/* the GNU General Public License. */ +/* This exception does not however invalidate any other reasons */ +/* why the executable file might be covered by the GNU Public License. */ +/* Publication of enhanced or derived LinCAN files is required although. */ +/**************************************************************************/ + +#include "../include/can.h" +#include "../include/can_sysdep.h" #include "../include/main.h" #include "../include/read.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 canque_ends_t *qends, - struct msgobj_t *obj, char *buffer, size_t length) +ssize_t can_read(struct file *file, char *buffer, size_t length, loff_t *offset) { - int ret; + struct canuser_t *canuser = (struct canuser_t*)(file->private_data); + struct canque_ends_t *qends; int bytes_to_copy; struct canque_edge_t *qedge; struct canque_slot_t *slot; - + int ret; + + if(!canuser || (canuser->magic != CAN_USER_MAGIC)){ + CANMSG("can_read: 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"); + return 0; + } + + qends = canuser->qends; + ret=canque_test_outslot(qends, &qedge, &slot); if(ret<0){ if (file->f_flags & O_NONBLOCK) { @@ -53,130 +78,24 @@ inline ssize_t can_std_read(struct file *file, struct canque_ends_t *qends, return -EIO; } } - - copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t)); + + ret = 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); - + if(ret) return -EFAULT; + while (bytes_to_copy > 0) { ret=canque_test_outslot(qends, &qedge, &slot); if(ret<0) break; - copy_to_user(buffer, &slot->msg, sizeof(struct canmsg_t)); + ret = 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); + if(ret) return -EFAULT; } return length-bytes_to_copy; } -/* This is the 'RTR' read handler for remote transmission request messages */ -inline ssize_t can_rtr_read(struct chip_t *chip, struct msgobj_t *obj, - char *buffer) -{ - unsigned long flags; - struct rtr_id *rtr_current, *new_rtr_entry; - struct canmsg_t read_msg; - - DEBUGMSG("Remote transmission request\n"); - spin_lock_irqsave(&hardware_p->rtr_lock, flags); - if (hardware_p->rtr_queue == NULL) { //No remote messages pending - new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC); - if (new_rtr_entry == NULL) { - spin_unlock_irqrestore(&hardware_p->rtr_lock, - flags); - return -ENOMEM; - } - hardware_p->rtr_queue=new_rtr_entry; - } - else { - rtr_current=hardware_p->rtr_queue; - while (rtr_current->next != NULL) - rtr_current=rtr_current->next; - new_rtr_entry=(struct rtr_id *)kmalloc(sizeof(struct rtr_id),GFP_ATOMIC); - rtr_current->next=new_rtr_entry; - } - init_waitqueue_head(&new_rtr_entry->rtr_wq); - new_rtr_entry->id = read_msg.id; - new_rtr_entry->rtr_message = &read_msg; - new_rtr_entry->next=NULL; - - spin_unlock_irqrestore(&hardware_p->rtr_lock, flags); - - /* Send remote transmission request */ - chip->chipspecops->remote_request(chip,obj); - obj->ret = 0; - interruptible_sleep_on(&new_rtr_entry->rtr_wq); - - spin_lock_irqsave(&hardware_p->rtr_lock, flags); - copy_to_user(buffer, &read_msg, sizeof(struct canmsg_t)); - if (hardware_p->rtr_queue == new_rtr_entry) { - if (new_rtr_entry->next != NULL) - hardware_p->rtr_queue=new_rtr_entry->next; - else - hardware_p->rtr_queue=NULL; - } - else { - rtr_current=hardware_p->rtr_queue; - while (rtr_current->next != new_rtr_entry) - rtr_current=rtr_current->next; - if (new_rtr_entry->next != NULL) - rtr_current->next=new_rtr_entry->next; - else - rtr_current->next=NULL; - } - spin_unlock_irqrestore(&hardware_p->rtr_lock, flags); - kfree(new_rtr_entry); - - return obj->ret; -} - -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 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"); - return 0; - } - if (length > 8 * sizeof(struct canmsg_t)) { - DEBUGMSG("Reading more than 8 CAN messages, this is not supported.\n"); - DEBUGMSG("Defaulting to 8 messages.\n"); - length = 8 * sizeof(struct canmsg_t); - } - /* Initialize hardware pointers */ - 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; - } - - 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, qends, obj, buffer, length); - - return ret; -} - -