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

📄 hello.c

📁 Linux 2.6内核版本下的PCI字符驱动开发练习参考代码。
💻 C
字号:
/* *	hello moule practicing...... */// ifndef compile#ifndef __KERNEL__#  define __KERNEL__#endif#ifndef MODULE#  define MODULE#endif#include <linux/init.h>#include <linux/module.h>/*************************************************************************/#include	<linux/kernel.h>	/*printk();*/#include	<linux/fs.h>	/*do everything...*/#include	<linux/types.h>	/*size_t*/#include	<asm/segment.h>	/*asm opt*///#include	"sysdep.h"	/*define header*//**************************************************************************/MODULE_AUTHOR("Mr.DwG");MODULE_LICENSE("GPL");//static char *Version = "$Revision: 1.9 $";MODULE_DESCRIPTION("hello_Driver");MODULE_VERSION("$Revision: 1.9 $");/* MODULE_DEVICE_TABLE(table_info);*//* MODULE_ALIAS(alternate_name);	*///=======================================================/* static char *whom = "world"; * static int howmany = 1; * module_param(howmany, int, S_IRUGO); * module_param(whom, charp, S_IRUGO); *///=======================================================/*  * 头文件, 包含在建立的内核版本信息. * LINUX_VERSION_CODE * 整型宏定义, 对 #ifdef 版本依赖有用. * EXPORT_SYMBOL (symbol); * EXPORT_SYMBOL_GPL (symbol); * 宏定义, 用来输出一个符号给内核. 第 2 种形式输出没有版本信息, 第 3 种限制输出给 GPL 许 * 可的模块. * MODULE_AUTHOR(author); * MODULE_DESCRIPTION(description); * MODULE_VERSION(version_string); * MODULE_DEVICE_TABLE(table_info); * MODULE_ALIAS(alternate_name); * 放置文档在目标文件的模块中. *///========================================================/* #include <linux/moduleparam.h> * module_param(variable, type, perm); * 宏定义, 创建模块参数, 可以被用户在模块加载时调整( 或者在启动时间, 对于内嵌代码). 类 * 型可以是 bool, charp, int, invbool, short, ushort, uint, ulong, 或者 intarray. * #include <linux/kernel.h> * int printk(const char * fmt, ...); * 内核代码的 printf 类似物. *  *///=========================================================/*****************************define OPT********************///static int hello_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id);
//static int hello_release(struct inode *inode, struct file *filp);
//static int hello_open(struct inode *inode, struct file *filp);
//static int hello_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg);
//static int hello_write(struct file *filp, char *buff, size_t count, loff_t *ppos);static int hello_read(struct file *filp, char *buff, size_t count, loff_t *ppos);
//static void hello_remove(struct pci_dev *pdev);int hello_major=0;char hello_buf[1024];static int hello_read(struct file *filp, char *buff, size_t count, loff_t *ppos){	//printk(KERN_DEBUG"read:file %x,buf %x,count %long int\n",filp,buff,count);	printk(KERN_DEBUG"Hi ,hello \n");	//memcpy(buff,hello_buf,count);	return count;}int extend_function(int a,int b){	return a+b;}static int hello_write(struct inode *inode,struct file *filp,char *buf,size_t count){	printk(KERN_DEBUG"write: start");	return 0;}static struct file_operations hello_fops = {
  NULL, // .owner=      THIS_MODULE,    
  NULL, // .ioctl=      evoc_ioctl,    
  NULL, // .open=       evoc_open,    
  NULL, //  .release=    evoc_release,
  		.write=		hello_write,
    	.read=		hello_read,
};
/***********************************************************************************************/static int	__init  hello_init(void){   printk(KERN_ALERT"Hello,word\n");   		int result;   	/*   	*** Register your major, and accept a dynamic number   	*/	result = register_chrdev(hello_major,"hello",&hello_fops);	if(result<0) return result;	if(hello_major==0) hello_major=result; /*dynamic*/	int i=extend_function(2,3);	printk("import: you may tel that 2+3=%d\n",i);		/*	**** allocate the divices--we cannot have them static ,as the number	**** can be specified at load time	*/	   return 0;}static void  __exit  hello_exit(void){  	unregister_chrdev(hello_major,"hello");	printk(KERN_ALERT"Goodby,cruelwork\n");}module_init(hello_init);module_exit(hello_exit);

⌨️ 快捷键说明

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