#include/* add header files to support character devices */ #include #include/* define mayor number */ #defineMY_MAJOR_NUM202//主设备号staticstructcdev my_dev;staticintmy_dev_open(structinode*inode,structfile*file) {pr_info("my_dev_open() is called.\n");return0; }staticintmy_dev_close(structinode*inode,structfile*file) {pr_info("my_dev_close() is called.\n");return0; }staticlongmy_dev_ioctl(structfile*file,unsignedint cmd,unsignedlong arg) {pr_info("my_dev_ioctl() is called. cmd &#61; %d, arg &#61; %ld\n", cmd, arg);return0; }/* declare a file_operations structure */ //创建一个my_dev_fops的文件file_operations数据结构体 //定义: // *打开 // *读取 // *写入 staticconststructfile_operations my_dev_fops &#61;{.owner &#61; THIS_MODULE,.open &#61; my_dev_open,.release &#61; my_dev_close,.unlocked_ioctl &#61; my_dev_ioctl, };staticint __init hello_init(void) {int ret;dev_t dev &#61;MKDEV(MY_MAJOR_NUM,0);pr_info("Hello world init\n");/* Allocate device numbers */ret &#61;register_chrdev_region(dev,1,"my_char_device");if(ret <0){pr_info("Unable to allocate mayor number %d\n", MY_MAJOR_NUM);return ret;}/* Initialize the cdev structure and add it to the kernel space */cdev_init(&my_dev,&my_dev_fops);ret&#61;cdev_add(&my_dev, dev,1);if(ret <0){unregister_chrdev_region(dev,1);pr_info("Unable to add cdev\n");return ret;}return0; }staticvoid __exit hello_exit(void) {pr_info("Hello world exit\n");cdev_del(&my_dev);unregister_chrdev_region(MKDEV(MY_MAJOR_NUM,0),1); }module_init(hello_init); module_exit(hello_exit);MODULE_LICENSE("GPL"); MODULE_AUTHOR(" "); MODULE_DESCRIPTION("This is a module that interacts with the ioctl system call");
/** A simple application to call helloworld character driver&#39;s ioctl.**/ #include #include #include #includeintmain(void) {/* First you need run "mknod /dev/mydev c 202 0" to create /dev/mydev */int my_dev &#61;open("/dev/mydev",0);if(my_dev <0){perror("Fail to open device file: /dev/mydev.");}else{ioctl(my_dev,100,110);// cmd &#61; 100, arg &#61; 110.close(my_dev);}return0; }
8.3 将模块添加到内核
insmod helloworld_char_driver.ko cat /proc/devices //查看申请的主设备号202 "my_char_device" ls -l /dev mknod /dev/mydev c 2020 //在/dev下&#xff0c;创建mydev ./ioctl_test rmmod helloworld_char_driver.ko
#include #include #include #include#defineDEVICE_NAME"mydev" #defineCLASS_NAME"hello_class"staticstructclass* helloClass; staticstructcdev my_dev; dev_t dev;staticintmy_dev_open(structinode*inode,structfile*file) {pr_info("my_dev_open() is called.\n");return0; }staticintmy_dev_close(structinode*inode,structfile*file) {pr_info("my_dev_close() is called.\n");return0; }staticlongmy_dev_ioctl(structfile*file,unsignedint cmd,unsignedlong arg) {pr_info("my_dev_ioctl() is called. cmd &#61; %d, arg &#61; %ld\n", cmd, arg);return0; }/* declare a file_operations structure */ staticconststructfile_operations my_dev_fops &#61;{.owner &#61; THIS_MODULE,.open &#61; my_dev_open,.release &#61; my_dev_close,.unlocked_ioctl &#61; my_dev_ioctl, };staticint __init hello_init(void) {int ret;dev_t dev_no;int Major;structdevice* helloDevice;pr_info("Hello world init\n");/* Allocate dynamically device numbers */ret &#61;alloc_chrdev_region(&dev_no,0,1, DEVICE_NAME);if(ret <0){pr_info("Unable to allocate Mayor number \n");return ret;}/* Get the device identifiers */Major &#61;MAJOR(dev_no);dev &#61;MKDEV(Major,0);pr_info("Allocated correctly with major number %d\n", Major);/* Initialize the cdev structure and add it to the kernel space */cdev_init(&my_dev,&my_dev_fops);ret &#61;cdev_add(&my_dev, dev,1);if(ret <0){unregister_chrdev_region(dev,1);pr_info("Unable to add cdev\n");return ret;}/* Register the device class */helloClass &#61;class_create(THIS_MODULE, CLASS_NAME);if(IS_ERR(helloClass)){unregister_chrdev_region(dev,1);cdev_del(&my_dev);pr_info("Failed to register device class\n");returnPTR_ERR(helloClass);}pr_info("device class registered correctly\n");/* Create a device node named DEVICE_NAME associated a dev */helloDevice &#61;device_create(helloClass,NULL, dev,NULL, DEVICE_NAME);if(IS_ERR(helloDevice)){class_destroy(helloClass);cdev_del(&my_dev);unregister_chrdev_region(dev,1);pr_info("Failed to create the device\n");returnPTR_ERR(helloDevice);}pr_info("The device is created correctly\n");return0; }staticvoid __exit hello_exit(void) {device_destroy(helloClass, dev);/* remove the device */class_destroy(helloClass);/* remove the device class */cdev_del(&my_dev);unregister_chrdev_region(dev,1);/* unregister the device numbers */pr_info("Hello world with parameter exit\n"); }module_init(hello_init); module_exit(hello_exit);MODULE_LICENSE("GPL"); MODULE_AUTHOR(" "); MODULE_DESCRIPTION("This is a module that interacts with the ioctl system call");
测试调试&#xff1a;
insmod helloworld_class_driver.ko //加载模块 ls /sys/class/ ls /sys/class/hello_class/my_dev ls -l /dev ./ioctl_test rmmod helloworld_clsss_driver.ko
ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ...
[详细]