www.pudn.com > 422.rar > read.c


/* read.c
 * Linux serial-bus device driver.
 * Written by wang song yue  email:wsy5228@sina.com
 * This software is released under the GPL-License.
 * Version 0.1  28 october 2005
 */

#include 
#if defined (CONFIG_MODVERSIONS) && !defined (MODVERSIONS)
#define MODVERSIONS
#endif

#if defined (MODVERSIONS)
#include 
#endif

#include 
#include 
#include 
#include 

#include "../include/main.h"
#include "../include/read.h"
#include "../include/ioctl.h"

ssize_t serial_read(struct file *file, char *buffer, size_t length, loff_t *offset)
{
	struct msgobj_t *obj;
	struct chip_t *chip;
	struct canfifo_t *fifo;
	struct serialmsg_t read_msg[3];
	int ret=0,i=0;
        int  bytes_avail=0, bytes_to_copy=0;
 if(MINOR_NR==0)
        {
                tts_io_addr=0xffd00000;
              
        }
        else if(MINOR_NR==1)
        {
                tts_io_addr=0xffd00008;
        }
       else if(MINOR_NR==2)
        {
                tts_io_addr=0xffd00010;
                                                                                                               
        }
                                                                                                               
        else if(MINOR_NR==3)
        {
                tts_io_addr=0xffd00018;
                                                                                                               
        }
                                                                                                               

// printk("i am in can read minor=%d,can_io_addr=0x%lx,can_data_addr=0x%lx\n",MINOR_NR,can_io_addr,can_data_addr);
	if (length < sizeof(struct serialmsg_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 serialmsg_t)) {
		DEBUGMSG("Reading more than 8 CAN messages, this is not supported.\n");
		DEBUGMSG("Defaulting to 8 messages.\n");
		length = 8 * sizeof(struct serialmsg_t);
	}
	/* Initialize hardware pointers */
	if ( (obj = objects_p[MINOR_NR]) == NULL) {
		CANMSG("Could not assign buffer structure\n");
		return -1;
	}


	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 serialmsg_t));           // no use
 /********************************can_std_read******************************************************************************/ 

    cli();
//printk("\ninterrupt 222222222222222222222222222\n");
        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("bytes_to_copy==0x%x,length=0x%x\n",bytes_to_copy,length);
/*
   if(!fifo->rx_in_progress){
  if ( (ret =chip->chipspecops->pre_read_config(chip, obj)) < 0)
 {
                        CANMSG("Error initializing hardware for receving\n");
                       return -EIO;
              }
      obj->ret=0;
   }
 */                                                                                                                       
// copy_to_user(buffer, fifo->rx_readp, sizeof(struct serialmsg_t));
                                                                                                                                               
//printk("bytes_to_copy=%d,bytes_avail=%d,length=%d\n",ret,bytes_avail,length);
        /* Copy the data to user space */
        while (bytes_to_copy > 0) {
 if(!fifo->rx_in_progress){
  if ( (ret =chip->chipspecops->pre_read_config(chip, obj)) < 0)
 {
                        CANMSG("Error initializing hardware for receving\n");
                       return -EIO;
              }
      obj->ret=0;
   }
    if(ret==0){printk("there is no message received!!!!!!\n");
fifo->rx_readp->length=0;
for(i=0;i<17;i++){fifo->rx_readp->data[i]=0;}
//return 0;
 }                                                                                                                                          
//printk("copy_to_user---qian  fifo->rx_readp.length======0x%x,fifo->rx_readp->data[0]=0x%x\n",fifo->rx_readp->length,fifo->rx_readp->data[0]);
 copy_to_user(buffer, fifo->rx_readp, sizeof(struct serialmsg_t));
//printk("copy_to_user---hou  fifo->rx_readp.length======0x%x,fifo->rx_readp->data[0]=0x%x\n",fifo->rx_readp->length,fifo->rx_readp->data[0]);
                buffer += sizeof(struct serialmsg_t);
                bytes_to_copy -= sizeof(struct serialmsg_t);
fifo->rx_readp = fifo->buf_rx_entry;
//                fifo->rx_readp++;
                if (fifo->rx_readp >= fifo->buf_rx_entry + MAX_BUF_LENGTH-1)
                {printk("dao tou le !!!!!!!!!!!!!!!\n\n\n");
                 fifo->rx_readp = fifo->buf_rx_entry;}
        }
   /*                                                                                                                                            
if(!fifo->rx_in_progress){
  if ( (ret =chip->chipspecops->pre_read_config(chip, obj)) < 0)
 {
                        CANMSG("Error initializing hardware for receving\n");
                       return -EIO;
              }
      obj->ret=0;
   }
*/

     
/*
	printk("2222222222buffer read_msg.length=%d,read_msg.data[1]=%d\n",read_msg.length,read_msg.data[1]);
	if (read_msg.flags & MSG_RTR)
		ret = can_rtr_read(chip, obj, buffer);
	else
		ret = can_std_read(file, fifo, obj, buffer, length);
 printk("33333333333333buffer read_msg.length=%d,read_msg.data[1]=%d\n",read_msg.length,read_msg.data[1]);
*/
	return ret;
}