📄 test.c
字号:
#ifndef __KERNEL__ #define __KERNEL__#endif#ifndef MODULE #define MODULE#endif
#include <linux/module.h>#include <linux/sched.h>#include <linux/kernel.h> /* printk() */#include <linux/init.h>
static int test_open(struct inode *inode, struct file *filp);static int test_release(struct inode *inode, struct file *filp); static int test_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long param);static int check_test_change(kdev_t dev);static int test_revalidate(kdev_t dev);int test_init(void);void test_cleanup(void);#define MAJOR_NR 125#define DEVICE_NAME "test" /* name for messaging */#define DEVICE_NR(device) MINOR(device)#define DEVICE_NO_RANDOM#define DEVICE_OFF(d)module_init(test_init);module_exit(test_cleanup);#include <linux/blk.h>/********************************************************************************************************/
static struct block_device_operations test_fops = /* driver info */{ owner: THIS_MODULE, open: test_open, release: test_release, ioctl: test_ioctl, check_media_change: check_test_change, revalidate: test_revalidate,}; static int test_open(struct inode *inode, struct file *filp){ MOD_INC_USE_COUNT; return 0; /* success */} static int test_release(struct inode *inode, struct file *filp) { MOD_DEC_USE_COUNT; return(0); } static int test_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg){ return 0;} static int check_test_change(kdev_t dev){ return 0;} static int test_revalidate(kdev_t dev){ return 0;} int test_init(void){ int result; result = register_blkdev(MAJOR_NR, DEVICE_NAME, &test_fops); if (result < 0) { printk(KERN_ERR DEVICE_NAME ": Unable to get major %d\n", MAJOR_NR ); return(result); } printk(KERN_ERR DEVICE_NAME ": init OK\n"); return(0); } void test_cleanup(void){ unregister_blkdev(MAJOR_NR, DEVICE_NAME);}/*********************************************************************************************************** End Of File********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -