色偷偷偷亚洲综合网另类,亚洲欧美另类在线观看,欧美午夜激情在线,久久久精品一区

當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > 利用udev、sys動(dòng)態(tài)創(chuàng)建linux設(shè)備結(jié)點(diǎn)

利用udev、sys動(dòng)態(tài)創(chuàng)建linux設(shè)備結(jié)點(diǎn) 時(shí)間:2018-08-16      來源:未知

在Linux2.6內(nèi)核中,devfs被認(rèn)為是過時(shí)的方法,并終被拋棄,udev取代了它。Devfs的一個(gè)很重要的特點(diǎn)就是可以動(dòng)態(tài)創(chuàng)建設(shè)備結(jié)點(diǎn)。那我們現(xiàn)在如何通過udev和sys文件系統(tǒng)動(dòng)態(tài)創(chuàng)建設(shè)備結(jié)點(diǎn)呢?

下面通過一個(gè)實(shí)例,說明udev、sys動(dòng)態(tài)創(chuàng)建設(shè)備結(jié)點(diǎn)的方法。注意代碼中紅色的部分是為了實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建設(shè)備結(jié)點(diǎn)添加的。 
         #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

在編譯了驅(qū)動(dòng)后,可以查看/dev/farsight_dev設(shè)備結(jié)點(diǎn),和 /sys/class/farsight_class/farsight_dev/ 本代碼的測(cè)試環(huán)境是Ubantu7.04,內(nèi)核版本是2.6.20-15-generi。在不同版本的內(nèi)核中,有些系統(tǒng)函數(shù)的參數(shù)可能不太一樣。 

上一篇:udev機(jī)制

下一篇:arm11開發(fā)板適合新手學(xué)習(xí)嗎

熱點(diǎn)文章推薦
華清學(xué)員就業(yè)榜單
高薪學(xué)員經(jīng)驗(yàn)分享
熱點(diǎn)新聞推薦
前臺(tái)專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠(yuǎn)見科技集團(tuán)有限公司 版權(quán)所有 ,京ICP備16055225號(hào)-5京公海網(wǎng)安備11010802025203號(hào)

回到頂部

色偷偷偷亚洲综合网另类,亚洲欧美另类在线观看,欧美午夜激情在线,久久久精品一区
主站蜘蛛池模板: 亚洲男人天堂久| 国产精品免费久久久久久| 欧美一级大片在线观看| 成人h视频在线| 日韩视频在线免费观看| 高清在线视频日韩欧美| 91欧美日韩一区| 裸体女人亚洲精品一区| 国产精品久久久久高潮| 国产一区二区三区在线视频 | 人体精品一二三区| 日韩国产中文字幕| 欧美国产日韩在线| 日韩av一区二区在线| 欧美激情视频一区二区三区不卡| 亚洲已满18点击进入在线看片| 久久影视电视剧免费网站清宫辞电视| 国产精品扒开腿做| 久久久精品久久久| 成人美女免费网站视频| 姬川优奈aav一区二区| 亚洲在线视频观看| 国模精品一区二区三区色天香| 日韩精品免费在线视频| 97精品国产97久久久久久免费| 亚洲免费精彩视频| 国产精品av在线| 疯狂做受xxxx欧美肥白少妇| 亚洲精品电影在线| 欧美性受xxxx白人性爽| 爱福利视频一区| 亚洲精品福利资源站| 欧美一级大胆视频| 欧美老女人性视频| 国产视频久久久| 国产精品综合不卡av| 欧美大片欧美激情性色a∨久久| 亚洲欧洲国产精品| 国产日韩中文在线| 欧美一区二区三区精品电影| 欧美成人sm免费视频|