ioctl作爲linux userspace 與kernel space交互的一種方式,其基於一dev id序列號去使用。一般做法步驟:
Kernel 部分的實現方式:
1, 建立device 設備。
2, 爲該device設備綁定file_operation操作[struct file_operations ]爲比較關鍵的操作。
基本結構體函數:
- 註冊設備驅動。
- #define CDD_MAJOR (256)
- #define CDD_MINOR (0)
- #defein CDD_COUNT (6)
-
<ol start="1" class="dp-objc" style="padding: 0px; border: none; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 26px; margin: 0px 0px 1px 45px !important;"><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold;">struct</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> file_operations cdd_fops = { </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="xcodeconstants" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.owner</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> = THIS_MODULE, </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="xcodeconstants" style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><u>.open</u></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><u> = cdd_open,</u> </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="xcodeconstants" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.read</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> = cdd_read, </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="xcodeconstants" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.write</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> = cdd_write, </span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="xcodeconstants" style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><u>.ioctl</u></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><u> = cdd_ioctl, </u> </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> <span class="xcodeconstants" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.release</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> = cdd_release, </span></span></li><li style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; background-color: rgb(248, 248, 248); line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> <u>.mmap = cdd_mmap</u></span></span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"> }; </span></li></ol>
-
<span style="background-color: inherit; color: black; line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"></span><pre class="SCREEN" name="code" style="background-color: rgb(255, 255, 255); white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; line-height: 26px;"><span class="comment" style="background-color: inherit; line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0);">//生成設備號</span><span style="background-color: inherit; color: black; line-height: 18px; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; margin: 0px; padding: 0px; border: none;"> </span>
-
<span style="background-color: inherit; color: black; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; line-height: 18px;">dev = MKDEV(CDD_MAJOR, CDD_MINOR);</span>
- //註冊設備號;1、要註冊的起始設備號2、連續註冊的設備號個數3、名字
- ret = register_chrdev_region(dev, CDD_COUNT, "cdd_demo");
- /*初始化cdev,綁定fs operation*/
- cdev_init(&cdd_cdev, &cdd_fops);
- /*添加cdev到內核*/
- cdev_add(&cdd_cdev, dev, CDD_COUNT);
-
- #define DEV "/dev/cdd_demo"
- fb = open(DEV,O_RDWR) ; ->其實際運行到kernel調用的就是註冊設備cdd_fops.open函數。
- ioctl(fb,xxxx) ; ->其實際運行到kernel調用的就是註冊設備cdd_fops.ioctl函數。
- mmap(xxx,fb,xx);->其實際運行到kernel 調用的註冊設備cdd_fops.mmap函數。