⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbdev.c

📁 周立功magic2410实验箱源码 第6章Linux高级实验(part1) 6.1 Linux内核编译实验 6.2 Linux根文件系统实验 6.3 CAT1025读/写实验. 6.4 ZL
💻 C
📖 第 1 页 / 共 3 页
字号:
}


/*********************************************************************************************************
** Function name: usbdev_open
** Descriptions : open device
** Input		: inode : information of device
**      		  filp  : pointer of file
** Output 		: 	  0 : OK
**        		  other : not OK
** Created by	: MingYuan Zheng
** Created Date	: 2005-12-26
**-------------	------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        static int usbdev_open(struct inode *inode, struct file *filp)
{
    unsigned long flag;
    unsigned int num;
    INT32U i;
    
    /* get the device minor and save into filp structure */
    num = MINOR(inode->i_rdev);                             
    filp->private_data = (void *)num;           		/* 取得次设备号并保存在filp结构中 */
    
    if (usage == 0)
    {
    	/* disable interrupt */
        local_irq_save(flag);							
           
        /* initialize global variable */   
        for (i = 0; i < NUMS_REC_ENDPOINTS; i++)		
        {
			sema_init(&rec_sem[i], 1);              	/* 接收端点信号量 */
            sema_init(&rec_irq_sem[i], 0);				/* 接收端点中断信号量 */
                
            sema_init(&send_sem[i], 1);					/* 发送端点信号量 */
            sema_init(&send_irq_sem[i], 0);				/* 发送端点中断信号量 */
                
            init_waitqueue_head(&rec_queue[i]); 		/* 接收数据等待队列 */
            init_waitqueue_head(&send_queue[i]); 		/* 发送数据等待队列 */
                
            rec_timeout[i] = 0;							/* 超时时间置为0 */
            send_timeout[i] = 0;
                        
            b_send[i] = 0;								/* 已发送标志置为0 */
            b_rec[i] = 0;								/* 已接收标志置为0 */
        }
    
    	/* enable interrupt */
        local_irq_restore(flag);
    }
         
    usage++;
    MOD_INC_USE_COUNT;									/* increase the number of opened driver */
    return 0;         									/* success */
}


/*********************************************************************************************************
** Function name: usbdev_init
** Descriptions : init driver
** Input        : none
** Output       : 0: OK
**                other: not OK
** Created by   : MingYuan Zheng
** Created Date : 2005-12-26
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        int usbdev_init(void)
{
    int  result;
    
    /* register the device */
    result = register_chrdev(MAJOR_NR, DEVICE_NAME, &usbdev_fops);       /* 注册设备 */
    if (result < 0)
    {
        printk(KERN_ERR DEVICE_NAME ": Unable to get major %d\n", MAJOR_NR );
        return(result); 
    } 
    
    /* Initialize the USB device controller */
    USB_Initialize();                                                    /* 初始化 USB 设备控制器 */        
    
    /* register the interrupt handler*/                     
    request_irq(IRQ_USBD, usb_irq_handle, SA_INTERRUPT, "my" DEVICE_NAME, NULL);
    
    /* clear usb device interrupt and enable usb interrupt */
    SRCPND = INT_USBD;                                                   /* 清除USB中断并使能USB中断 */                                                    
    INTPND = INT_USBD;     
    INTMSK &= ~(INT_USBD);
 
    if (MAJOR_NR == 0)
    {
        MAJOR_NR = result; /* dynamic */
    }

    printk(KERN_INFO DEVICE_NAME ": init OK\n");
    return(0); 
}


/*********************************************************************************************************
** Function name: usbdev_cleanup
** Descriptions : unload driver
** Input        : none
** Output       : none
** Created by   : MingYuan Zheng
** Created Date : 2005-12-26
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
        void usbdev_cleanup(void)
{
    printk("Unloading S3C2410 USB Device Controller Driver\n");

    CLKCON &= ~CLKCON_USBD;                     /* 关闭 USB 设备控制器时钟 close the clock of the USB device controller */      
        
    free_irq(IRQ_USBD, NULL);                   /* 释放 USB 中断号 release the USB interrupt */
    
    USB_DISCONNCET();                           /* 断开 USB 总线连接 disconnect the USB bus */
    
    unregister_chrdev(MAJOR_NR, DEVICE_NAME);
}

module_init(usbdev_init);
module_exit(usbdev_cleanup);





/*******************************************************************
	
	下面内容与 Linux 操作系统无关

********************************************************************/


/* define USB event flag variable */
EPPFLAGS bEPPflags;										/* 定义 USB 事件标志变量 */

/*********************************************************************************************************
** Function name : USB_Initialize
** Descriptions  : Initialize the USB device controller 				初始化USB设备控制器
** Input		 : NULL  
** Output		 : 0: Initialize sucessfully    1: Initialize fail 		0: 初始化成功  1: 初始化失败
** Created by	 : MingYuan Zheng 郑明远
** Created Date  : 2006-01-12
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
INT8U USB_Initialize(void)
{	
	bEPPflags.value = 0;

	USB_InitHareware();									/* 初始化硬件         Initialize hardware */
	USB_InitSFR();										/* 初始化相关寄存器	  Initialize the related register */	
		
	USB_ConfigEndpoint();								/* 配置所有端点  	  Config all the endpoint */
	USB_SetAddressEnable(0, 1);							/* 设置默认地址为0    Config the default address is 0 */

	USB_Reconnect();

 	return 0;									    	/* 初始化USB成功      Initialize USB sucessfully */
}


/*********************************************************************************************************
** Function name : USB_InitSFR
** Descriptions  : Initialize the SFR of USB Device Contoller	  
				   初始化USB设备控制器相关寄存器	 
** Input		 : NULL  
** Output		 : NULL
** Created by	 : MingYuan Zheng 郑明远
** Created Date  : 2006-01-12
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void USB_InitSFR(void)
{	
	INT32U tmp;
		
	UD_PWR = DISABLE_SUSPEND;				/* 禁止挂起模式 		 disable suspend mode */

	tmp = UD_INT;
	UD_INT = tmp;	
											/* 清除USB控制器所有中断 clear all interrupt of USB Device */
	tmp = UD_USBINT;
	UD_USBINT = tmp;
	
	/* 使能 EP0 ~ EP4 和 复位中断 Enable interrupt of EP0 ~ EP4 and reset interrupt */
	USB_InterruptEnable(EP0_INT | EP1_INT | EP2_INT | EP3_INT | EP4_INT, RESET_INT);
}


/*********************************************************************************************************
** Function name : USB_ConfigEndpoint
** Descriptions  : config all endpoint	  
				   配置所有端点	 
** Input		 : NULL  
** Output		 : NULL
** Created by	 : MingYuan Zheng 郑明远
** Created Date  : 2006-01-12
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void USB_ConfigEndpoint(void)
{
	USB_ConfigMaxPaketSize(0, FIFO_SIZE_16);			/* 端点 0 最大包长度为16 Endpoint 0 Max packet is 16 */
	USB_ConfigMaxPaketSize(1, FIFO_SIZE_64);			/* 端点 1 最大包长度为64 Endpoint 1 Max packet is 64 */
	USB_ConfigMaxPaketSize(2, FIFO_SIZE_64);			/* 端点 2 最大包长度为64 Endpoint 2 Max packet is 64 */
	USB_ConfigMaxPaketSize(3, FIFO_SIZE_64);			/* 端点 3 最大包长度为64 Endpoint 3 Max packet is 64 */
	USB_ConfigMaxPaketSize(4, FIFO_SIZE_64);			/* 端点 4 最大包长度为64 Endpoint 4 Max packet is 64 */	
	
	/* Configure endpoint 0 */
	USB_SetCurEndpoint(0);
	UD_ICSR1 = EP0_SERVICED_OUT_PKT_RDY | EP0_SERVICED_SETUP_END;   /* 配置端点0: 清除相关标志 */

	/* Endpoint 1: IN,  Bulk transfer, disble DMA */
	USB_ConfigEpControlStatus(1, EPI_MODE_IN, EPI_BULK, EPO_BULK);  /* 配置端点1: IN 端点, 批量传输, 禁止DMA中断 */	
	
	/* Endpoint 2: OUT, Bulk transfer, disble DMA */
	USB_ConfigEpControlStatus(2, EPI_MODE_OUT, EPI_BULK, EPO_BULK);	/* 配置端点2: OUT 端点, 批量传输, 禁止DMA中断 */	
	
	/* Endpoint 3: IN,  Bulk transfer, disble DMA */ 	
	USB_ConfigEpControlStatus(3, EPI_MODE_IN, EPI_BULK, EPO_BULK);  /* 配置端点3: IN 端点, 批量传输, 禁止DMA中断 */	 
	
	/* Endpoint 4: OUT, Bulk transfer, disble DMA */
	USB_ConfigEpControlStatus(4, EPI_MODE_OUT, EPI_BULK, EPO_BULK); /* 配置端点4: OUT 端点, 批量传输, 禁止DMA中断 */
}


/*********************************************************************************************************
** Function name : USB_BusReset
** Descriptions  : process USB bus reset	  
				   USB 总线复位处理	 
** Input		 : NULL  
** Output		 : NULL
** Created by	 : MingYuan Zheng 郑明远
** Created Date  : 2006-01-12
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void USB_BusReset(void)
{
	USB_InitHareware();	
	
	USB_InitSFR();									/* 初始化相关寄存器	  Initialize the related register */
         
	USB_ConfigEndpoint();							/* 配置所有端点  	  Config all the endpoint */

	USB_SetAddressEnable(0, 1);						/* 设置默认地址为0    Config the default address is 0 */
	
	UD_USBINT = RESET_INT;							/* 清除USB中断 */
}



/*********************************************************************************************************
** Function name : USB_Suspend
** Descriptions  : process USB bus suspend  
				   USB 总线挂起	 
** Input		 : NULL  
** Output		 : NULL
** Created by	 : MingYuan Zheng 郑明远
** Created Date  : 2006-01-12
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void USB_Suspend(void)
{
	UD_USBINT = SUSPEND_INT;
}


/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -