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

📄 tongdev.c

📁 一个linux字符设备驱动。根据操作系统课本上的实验内容设计编写
💻 C
字号:
struct device_struct{const char *name;struct file_operations *chops;};static struct device_struct chrdevs[MAX_CHRDEV];typedef struct tongdev_Dev{void **data;int quantum;                //the capacity of recentint qset;                   //the num of the arrayunsigned long size;unsigned int access_key;    //used by tongdevuid and tongdevprivunsigned int usage;         //lock controlstruct tongdev_Dev *next;}tongdevstatic int tongdev_open(struct inode *inode,struct file *filp);//open char equstatic int tongdev_release(struct inode *inode,struct file *filp);//release char equstatic ssize_t tongdev_write(struct inode *inode,struct file *filp,const char*buffer,int count);//write data to char equstatic ssize_t tongdev_read(struct inode*inode,struct file*filp,char*buffer,int count);//read data from char equstatic int tongdev_ioctl(struct inode*inode,struct file*filp,unsigned long int cmd,unsigned long arg);//char equ controlstruct file_operations chr_fops={null,                   //seektongdev_read,           //readtongdev_write,          //writenull,                   //readdirnull,                   //polltongdev_ioctl,          //ioctlnull,                   //mmaptongdev_open,           //opennull,                   //flushtongdev_release,        //releasenull,                   //fsyncnull,                   //fasyncnull,                   //check media changenull,                   //revalidatenull,                   //lock};#include <linux/fs.h>#include <linux/errno.h>int register_chrdev(unsigned int major,const char *name,struct file_operation *fops);int tongdev_open(struct inode *inode,struct file *filp) //open{mod_inc_use_count ; //increase these mod's userprintk("This chrdev is in open\n");return 0;}int tongdev_write(struct inode *inode,struct file *filp, const char *buffer, int count) //write{if (count<0)return -einval;if(tongdev.usage||tongdev.new_msg)return -ebusy;tongdev.usage=1;kfree(tongdev.data);data=kmalloc(sizeof(char)*(count+1),gef_kernel);if (!tongdev.data){return -enomem;}copy_from_user(tongdev.data,buffer,count+1);tongdev.usage=0;tongdev.new_msg=1;return count;}int tongdev_read(struct inode *inode,struct file *filp,char *buffer, int count) //read{int length;if(count<0)return -einval;if(tongdev.usage)return -ebusy;tongdev.usage=1;if(tongdev.data==0)return 0;length=strlen(tongdev.data);if(length<count)count=length;copy_to_user(buf,tongdev.data,count+1);tongdev.new_msg=0;tongdev.usage=0;return count;}#include <linux/ioctl.h>#define tongdev_major 0#define tongdev_magic tongdev_major#define tongdev_reset _IO(tongdev_magic,0)//reset the data#define tongdev_query_new_msg _IO(tongdev_magic,1)//check the new message#define tongdev_query_msg_length _IO(tongdev_magic,2)//get the message length#define ioc_new_msg 1 static int usage,new_msg; //control tagstatic char *data;int tongdev_ioctl(struct inode *inode,struct file *filp,unsigned long int cmd, unsigned long arg){int ret=0;switch(cmd){case tongdev_reset:                   kfree(data);                   data=null;                   usage=0;                   new_msg=0;break;case tongdev_query_new_msg:                   if(new_msg)                   return ioc_new_msg;break;case tongdec_query_msg_length:                   if(data==null){                   return 0;                   }                   else {                   return strlen(data);}break;default:                   return -enotty;}return ret;}void tongdev_release(struct inode *inode,struct file *filp){mod_dec_use_count; //decrease these mod's userprintk("This chrdev is in release\n");return 0;#ifdef debugprintk("tongdev_release(%p,%p)\n",inode,filp);#endif}

⌨️ 快捷键说明

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