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

📄 demo.c

📁 demo驱动程序
💻 C
字号:
/****************************************************************************************** **  file name: demo.c**  author   : Zou jian guo**  date     : 2003-12-22**  email    : ah_zou@tom.com**  updated by Wang jun**  data:  2006-6-2**  email:wju_uptech@126.com**  Descriptions: a demo for Linux device deiver  **  This demo character device driver implements the following functions:**  two buffers will be used to simulate a real port.  wbuff is a buffer**  which will be used to store the data that is written to the device,**  rbuff is another buffer which will be used when the data is red from**  the device.**  the string written to wbuff will be copied to rbuff in reverse order.** Copyright (C) 2002-2006  UP-TECH  All rights reserved.**** This file is part of basic demos for UP-NETARM2410 or UP-NETARM2410-S** ** This example program may be used, distributed and modified without limitation.** No warranty is attached, we cannot take responsibility for errors or fitness for use.****************************************************************************************//*************************************************************************************///#define CONFIG_DEVFS_FS      //use devfs#ifndef __KERNEL__#define __KERNEL__#endif#ifndef MODULE#define MODULE#endif/*************************************************************************************/#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/mm.h>#include <linux/init.h>#include <linux/interrupt.h>#include <asm/uaccess.h>#include <asm/hardware.h>#include <linux/irq.h>#include "demo.h"static int DEMO_MAJOR = 0;          //major device number allocated dynamicly /****************************************************************************//* open and close *//************************************************************ function name : demo_open         ** input param   :   ** output param  :  0** description   :  open the demo device************************************************************/static int demo_open(struct inode *inode, struct file *filp){	memset(wbuff, 0, BUFFSIZE);	memset(rbuff, 0, BUFFSIZE);	count = 0;	MOD_INC_USE_COUNT;   //increase  usr counter		return 0;}/*********************************************************************************//************************************************************* function name : demo_close         ** input param   :   ** output param  :  0** description   :  close the demo device ************************************************************/static int demo_close(struct inode *inode, struct file *filp){		MOD_DEC_USE_COUNT;  //操作使用计数的宏		return 0;}/*********************************************************************************//* read and write *//************************************************************* function name : demo_read         ** input param   :   ** output param  : cnt** description   :  send reversed data to user************************************************************/static ssize_t demo_read(struct file *filp, char *buf, size_t cnt, 	loff_t *off){	int i;	if(!count)		return 0;	for(i=0; i<count; i++)		rbuff[i] = wbuff[count-i-1];   //reverse data	if(cnt > count)		cnt = count;	copy_to_user(buf, rbuff, cnt);     // send data to user	return cnt;}/***************************************************************************//************************************************************* function name : demo_write         ** input param   :   ** output param  :  cnt** description   :  receive data from user************************************************************/static ssize_t demo_write(struct file *filp, const char *buf, size_t cnt, 	loff_t *off){	if(cnt > BUFFSIZE)		cnt = BUFFSIZE;	copy_from_user(wbuff, buf, cnt);         // receive data from user	count = cnt;	return cnt;}/*************************************************************************************//* ioctl *//************************************************************* function name : demo_ioctl         ** input param   :   ** output param  :  0** description   :  ioctl frame************************************************************/static int demo_ioctl(struct inode *inode, struct file *filp, 	unsigned int cmd, unsigned long arg){	switch(cmd) {	default:		break;	}	return 0;}/*************************************************************************************//* the device fops */static struct file_operations demo_fops = {	owner:		THIS_MODULE,	read:		demo_read,	write:		demo_write,	ioctl:		demo_ioctl,	open:		demo_open,	release:		demo_close,};/*************************************************************************************//*use devfs*/#ifdef CONFIG_DEVFS_FSstatic devfs_handle_t  devfs_demo_dir, devfs_demoraw;#endif/*************************************************************************************//* init and exit *//************************************************************* function name : demo_init         ** input param   :   ** output param  :  ** description   :  initial demo module************************************************************/static int __init demo_init(void){	int result;	wbuff = kmalloc(BUFFSIZE, GFP_KERNEL);	rbuff = kmalloc(BUFFSIZE, GFP_KERNEL);	result = register_chrdev(DEMO_MAJOR, "demo", &demo_fops);	if(result < 0) {		printk(KERN_ERR "Cannot register demo device.\n");		kfree(wbuff);		kfree(rbuff);		return -EIO;	}    if(DEMO_MAJOR==0)    	DEMO_MAJOR = result;	/*devfs*/#ifdef CONFIG_DEVFS_FS    devfs_demo_dir = devfs_mk_dir(NULL, "demo", NULL);   //make demo device directory    devfs_demoraw = devfs_register(devfs_demo_dir, "0", DEVFS_FL_DEFAULT,                    DEMO_MAJOR, DEMO_MINOR, S_IFCHR | S_IRUSR | S_IWUSR,                    &demo_fops, NULL);   //register demo device "0"#endif   	/* some furthertreatment for this case */		return 0;}/***************************************************************************//************************************************************* function name : demo_exit         ** input param   :   ** output param  :  ** description   :  exit demo module************************************************************/static void __exit demo_exit(void){	int result;#ifdef CONFIG_DEVFS_FS		devfs_unregister(devfs_demoraw);    //delete demo device 	devfs_unregister(devfs_demo_dir);   //delete demo device directory #endif	result = unregister_chrdev(DEMO_MAJOR, "demo");	if(result < 0) {		printk(KERN_ERR "Cannot remove demo device.\n");		return;	}	if(wbuff)		kfree(wbuff);          //free memory	if(rbuff)		kfree(rbuff);	return;}/*************************************************************************************/MODULE_LICENSE("GPL");module_init(demo_init);module_exit(demo_exit);

⌨️ 快捷键说明

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