日韩在线免费播放-日韩在线免费av-日韩在线免费-日韩在线毛片-国产高清不卡视频-国产高清不卡

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > 利用udev、sys動態創建linux設備結點

利用udev、sys動態創建linux設備結點 時間:2018-08-16      來源:未知

在Linux2.6內核中,devfs被認為是過時的方法,并終被拋棄,udev取代了它。Devfs的一個很重要的特點就是可以動態創建設備結點。那我們現在如何通過udev和sys文件系統動態創建設備結點呢?

下面通過一個實例,說明udev、sys動態創建設備結點的方法。注意代碼中紅色的部分是為了實現動態創建設備結點添加的。 
         #include <linux/module.h> 
         #include <linux/kernel.h>
         #include <linux/init.h> 
         #include <linux/fs.h> 
         #include <linux/cdev.h> 
         #include <asm/uaccess.h> 
         #include <linux/device.h>
         MODULE_LICENSE ("GPL");
         int hello_major = 252;
         int hello_minor = 0;
         int number_of_devices = 1;
         char data[50]="foobar not equal to barfoo";
         struct cdev cdev;
         dev_t dev = 0;
         static int hello_open (struct inode *inode, struct file *file)
         {
         printk (KERN_INFO "Hey! device opened\n");
         return 0;
         }
         static int hello_release (struct inode *inode, struct file *file)
         {
         printk (KERN_INFO "Hmmm... device closed\n");
         return 0;
         }
         ssize_t hello_read (struct file *filp, char *buff, size_t count, loff_t *offp)
         {
         ssize_t result = 0;
         if (copy_to_user (buff, data, sizeof(data)-1)) 
         result = -EFAULT;
         else 
         printk (KERN_INFO "wrote %d bytes\n", count);
         return result;
         }
         ssize_t hello_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
         {
         ssize_t ret = 0;
         printk (KERN_INFO "Writing %d bytes\n", count);
         if (count>127) return -ENOMEM;
         if (count<0) return -EINVAL;
         if (copy_from_user (data, buf, count)) {
         ret = -EFAULT;
         }
         else {
         data[127]='\0';
         printk (KERN_INFO"Received: %s\n", data);
         ret = count;
         }
         return ret;
         }
         struct file_operations hello_fops = {
.         owner = THIS_MODULE,
.         open = hello_open,
.         release = hello_release,
.         read = hello_read,
.         write = hello_write
         };
         struct class *my_class;
         static void char_reg_setup_cdev (void)
         {
         int error, devno = MKDEV (hello_major, hello_minor);
         cdev_init (&cdev, &hello_fops);
         cdev.owner = THIS_MODULE;
         cdev.ops = &hello_fops;
         error = cdev_add (&cdev, devno , 1);
         if (error)
         printk (KERN_NOTICE "Error %d adding char_reg_setup_cdev", error);
         /* creating your own class */
         my_class =class_create(THIS_MODULE, "farsight_class");//add by lht
         if(IS_ERR(my_class)) {
         printk("Err: failed in creating class.\n");
         return ;
         }
         /* register your own device in sysfs, and this will cause udevd to create corresponding device node */
         class_device_create(my_class,NULL, devno, NULL,"farsight_dev");
         // device_create(my_class,NULL, devno,"farsight_dev");
         }
         static int __init hello_2_init (void)
         {
         int result;
         dev = MKDEV (hello_major, hello_minor);
         result = register_chrdev_region (dev, number_of_devices, "test");
         if (result<0) {
         printk (KERN_WARNING "hello: can't get major number %d\n", hello_major);
         return result;
         }
         char_reg_setup_cdev ();
         printk (KERN_INFO "char device registered\n");
         return 0;
         }
         static void __exit hello_2_exit (void)
         {
         dev_t devno = MKDEV (hello_major, hello_minor);
         cdev_del (&cdev);
         unregister_chrdev_region (devno, number_of_devices);
         class_device_destroy(my_class, devno);
         class_destroy(my_class);
         }
         module_init (hello_2_init);
         module_exit (hello_2_exit);v

在編譯了驅動后,可以查看/dev/farsight_dev設備結點,和 /sys/class/farsight_class/farsight_dev/ 本代碼的測試環境是Ubantu7.04,內核版本是2.6.20-15-generi。在不同版本的內核中,有些系統函數的參數可能不太一樣。 

上一篇:udev機制

下一篇:arm11開發板適合新手學習嗎

熱點文章推薦
華清學員就業榜單
高薪學員經驗分享
熱點新聞推薦
前臺專線:010-82525158 企業培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,京ICP備16055225號-5京公海網安備11010802025203號

回到頂部

主站蜘蛛池模板: 男人亲女人下面的视频| 脚心的视频vk| av电影网| 13名妓| 莫比乌斯电影讲的是什么意思| 画江湖之不良人第七季什么时候出| 那些回不去的年少时光演员表| 科室对分级护理落实情况检查记录| 狂魔电影| 五年级脱式计算题100道| 韩国车震电影| 天地姻缘七仙女演员表| 宋元甫| 温暖的弦电视剧全集免费观看高清| 荒笛子简谱| 3片| 蓝心妍的电影《极乐宝鉴》免费国语 | 色在线看| 好看的国产动漫| 欧美成熟| 国产伦理女村支书| 草逼的视频| 狐步谍影| 台版十七岁免费观看| free loop中文版歌词| 老片.经典.hd.videos| 韩国电影《爱欲》| 摇曳庄的幽奈小姐| 五年级上册写字表拼音| 宝宝满月酒微信邀请函| 彭丹丹最惊艳的电影| 都市频道在线直播| 大侠霍元甲演员表| 皮皮虾影视| 陈慕义| 双重火力电影| 潜行在线观看| 复仇者联盟4在线完整版观看| 电影《19号海滩》在线播放国语版| 五年级下册语文第七单元口语交际 | 大众故事1974意大利|