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


/* open.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
 */

#define __NO_VERSION__
#include  

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

#if defined (MODVERSIONS)
#include 
#endif

#include 
#include 
#include 

#include "../include/main.h"
#include "../include/open.h"
//#include "../include/i82527.h"
#include "../include/setup.h"

int serial_open(struct inode *inode, struct file *file)
{
	struct msgobj_t *obj;
	struct chip_t *chip;
	struct canfifo_t *fifo;
      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_open minor=%d,can_io_addr=0x%lx,can_data_addr=0x%lx\n",MINOR_NR,can_io_addr,can_data_addr);


	if ( ((obj=objects_p[MINOR_NR]) == NULL) || 
			((chip=objects_p[MINOR_NR]->hostchip) == NULL) ) {
		CANMSG("There is no hardware support for the device file with minor nr.: %d\n",MINOR_NR);
		return -ENODEV;
	}

	if (objects_p[MINOR_NR]->flags & OPENED) {
		CANMSG("Sorry, only single open per device file.\n");
		return -EBUSY;
	}
	else
		objects_p[MINOR_NR]->flags |= OPENED;

	if (chip->flags & CONFIGURED) 
		DEBUGMSG("Device is already configured.\n");
	//printk("device is already configed!!\n\n");
	else {
		if (chip->chipspecops->chip_config(chip))
			CANMSG("Error configuring chip.\n");
		else
			chip->flags |= CONFIGURED; 
	} /* End of chip configuration */

	/* Allocate output buffer memory for the opened device */
	fifo = objects_p[MINOR_NR]->fifo;
	fifo->buf_tx_entry=(struct serialmsg_t *)kmalloc(MAX_BUF_LENGTH * sizeof(struct serialmsg_t), GFP_KERNEL);
	if (fifo->buf_tx_entry == NULL)
		return -ENOMEM;
	else
		if ( add_mem_to_list(fifo->buf_tx_entry) )
			return -ENOMEM;
	/* Allocate input buffer memory for the opened device */
	fifo->buf_rx_entry=(struct serialmsg_t *)kmalloc(MAX_BUF_LENGTH * sizeof(struct serialmsg_t), GFP_KERNEL);
	if (fifo->buf_rx_entry == NULL)
		return -ENOMEM;
	else
		if ( add_mem_to_list(fifo->buf_rx_entry) )
			return -ENOMEM;

	/* In- and output buffer initialization */
	fifo->tx_readp = fifo->buf_tx_entry;
	fifo->tx_writep = fifo->buf_tx_entry;
	fifo->rx_readp = fifo->buf_rx_entry;
	fifo->rx_writep = fifo->buf_rx_entry;
	fifo->rx_size = MAX_BUF_LENGTH * sizeof(struct serialmsg_t);
	fifo->tx_size = fifo->rx_size;

#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,2,19))
	init_waitqueue(&fifo->readq);
	init_waitqueue(&fifo->writeq);
#else
	init_waitqueue_head(&fifo->readq);
	init_waitqueue_head(&fifo->writeq);
#endif

	fifo->rx_in_progress = 0;
	fifo->tx_in_progress = 0;

	fifo->head = fifo->tail = 0; //TEMP!!

	      //if (chip->chipspecops->pre_read_config(chip,obj)<0)
	 //	CANMSG("Error initializing chip for receiving\n");
		
	MOD_INC_USE_COUNT;
	
	return 0;
}