構建ioctl的方法的討論

   ioctl作爲linux userspace 與kernel space交互的一種方式,其基於一dev id序列號去使用。一般做法步驟:

Kernel 部分的實現方式:

  1, 建立device 設備。

  2, 爲該device設備綁定file_operation操作[struct file_operations ]爲比較關鍵的操作。

   基本結構體函數:

  1. 註冊設備驅動。
  2. #define CDD_MAJOR  (256)
  3. #define CDD_MINOR  (0)
  4. #defein CDD_COUNT  (6)
  5. <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>
  6. <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>
    
    
    
    
    
    
  7. <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>
  8.  //註冊設備號;1、要註冊的起始設備號2、連續註冊的設備號個數3、名字  
  9. ret = register_chrdev_region(dev, CDD_COUNT, "cdd_demo");  
  10. /*初始化cdev,綁定fs operation*/  
  11.  cdev_init(&cdd_cdev, &cdd_fops);  
  12.  /*添加cdev到內核*/  
  13.   cdev_add(&cdd_cdev, dev, CDD_COUNT);  

user space的使用方式:
   通過上面就創建了一個設備驅動,且對該設備驅動綁定了相應文件操作方式。 那user space怎麼去使用該設備驅動。

  1. #define  DEV  "/dev/cdd_demo"
  2. fb = open(DEV,O_RDWR) ; ->其實際運行到kernel調用的就是註冊設備cdd_fops.open函數。
  3. ioctl(fb,xxxx) ; ->其實際運行到kernel調用的就是註冊設備cdd_fops.ioctl函數。
  4. mmap(xxx,fb,xx);->其實際運行到kernel 調用的註冊設備cdd_fops.mmap函數。

經過上面的描述,關於通過dev去控制相應kernel操作,注意如下幾點:
1,確定user space需要做哪些必要操作,且需要對這些必要的操作做一些定製化需求。那麼就需要實現file_operations對應的接口函數,否則
其會使用默認的定義形式。
2,driver 部分創建device,並且去初始化該部分。
3,確定了1,2部分就可以正常使用ioctl這些函數操作:
1)通過device file去控制ioctl部分。 
2)mmap的使用中,建立map關係,這樣子對user來說,直接寫到user buffer,其就將數據自動sync到kernel buffer。同樣對kernel
buffer的數據操作也會自動sync到user space的buffer,這樣子減少了user space與kernel space的不斷copy動作,對頻繁數據訪問的
driver來講是高效的。且一般使用中爲了讓driver去更好的操作指定內存,一般回去客製化,mmap,指定一塊特定的buffer空間讓其使用。
 
Reference:
具體使用方法鏈接:
http://www.linuxidc.com/Linux/2013-04/83307.htm(對GPIO的控制方法)
參數定義鏈接:http://blog.csdn.net/styyzxjq2009/article/details/8023501

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章