linux字元裝置模型

來源:互聯網
上載者:User
//字元裝置的結構體struct xxx_dev_t{struct cdev cdev;...........}xxx_dev;//字元裝置模組的載入和卸載函數static int __init xxx_init(void){......./*初始化cdev*/cdev_init(&xxx_dev.cdev,&xxx_fops);xxx_dev.owner=THIS_MODULE;/*擷取字元裝置號*/if(xxx_major){/*為一個字元驅動擷取一個或多個裝置編號*/register_chrdev_region(xxx_dev_no,1,DEV_NAME);}else {alloc_chrdev_region(&xxx_dev_no,0,1,DEV_NAME);}ret=cdev_add(&xxx_dev.cdev,xxx_dev_no,1);........}//裝置驅動模組的卸載函數static void __exit xxx_exit(void){unregister_chrdev_region(xxx_dev_no,1);//注釋佔用裝置號cdev_del(&xxx_dev.cdev);............}//字元裝置驅動的file_operations結構體中的成員函數/*讀裝置*/ssize_t xxx_read(struct file *filp,char __usr *buf,size_t count,loff_t *f_pos){............copy_to_user(buf,...,...);.............}/*寫裝置*/ssize_t xxx_write(struct file *filp,const char __usr *buf,size_f count,loff_t *f_pos){..............copy_from_user(....,buf,....);..............}/*ioctl函數*/int xxx_ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg){.............switch(cmd){case xxx_cmd1:..........;break;case xxx_cmd2:...........;break;..............default:            /*不能支援的命令*/return -ENOTTY;}return0;}//核心空間和使用者空間互動,經常用到的兩個函數unsigned long copy_from_user(void *to,const void __user *from,unsigned long count);unsigned long copy_to_usr(void __user *to,const void *from,unsigned long count);/*上述函數均返回不能被複製的位元組數,如果完全複製成功,則傳回值為0。*/
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.