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

📄 my_devfs.c

📁 linux char device driver programming
💻 C
字号:
#include <linux/kernel.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/fs.h>#include <linux/interrupt.h>#include <linux/init.h>#include <linux/delay.h>#include <asm/uaccess.h>#define DEV_NAME "my_devfs"MODULE_AUTHOR("huisho");MODULE_LICENSE("Dual BSD/GPL");static int my_devfs_release(struct inode *inode, struct file *file) {        printk(DEV_NAME" :file close!!! \n");	return 0;}static ssize_t my_devfs_write(struct file *file, const char *buf,			       size_t count, loff_t *offp) {            return count;}static ssize_t my_devfs_read(struct file *file,const char *buf,                               size_t count,loff_t *offp){			  char temp[20]="hello world!!!";        copy_to_user(buf,temp,20);        printk(DEV_NAME" :user read: %s\n",temp);        return count;}static int my_devfs_open(struct inode *inode, struct file *file) {	printk(DEV_NAME" :device open!!!\n");	return 0;}static struct file_operations my_fops = {	.owner = THIS_MODULE,	.read = my_devfs_read,	.write = my_devfs_write,	.open = my_devfs_open,	.release = my_devfs_release,};static int __init my_devfs_init(void) {	int ret = 0;	ret = register_chrdev(60, DEV_NAME , &my_fops);	if ( ret ) {		printk(" Unable to register device\n");		return ret;	}	printk(DEV_NAME": Initialized successfully.\n");	return 0;}static void __exit my_devfs_exit(void) {	printk(DEV_NAME": Exiting.\n");	unregister_chrdev(60, DEV_NAME);	return;}module_init(my_devfs_init);module_exit(my_devfs_exit);

⌨️ 快捷键说明

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