新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > 利用udev、sys動態創建linux設備結點

        利用udev、sys動態創建linux設備結點

        作者: 時間:2012-08-27 來源:網絡 收藏

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

        本文引用地址:http://www.104case.com/article/148527.htm

        下面通過一個實例,說明的方法。注意代碼中紅色的部分是為了實現動態創建設備結點添加的。

        #include

        #include

        #include

        #include

        #include

        #include

        #include

        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 openedn);

        return 0;

        }

        static int hello_release (struct inode *inode, struct file *file)

        {

        printk (KERN_INFO Hmmm... device closedn);

        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 bytesn, 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 bytesn, count);

        if (count>127) return -ENOMEM;

        if (count0) return -EINVAL;

        if (copy_from_user (data, buf, count)) {

        ret = -EFAULT;

        }

        else {

        data[127]='

        主站蜘蛛池模板: 田阳县| 临湘市| 河源市| 镇雄县| 大渡口区| 商城县| 汉沽区| 习水县| 鄂尔多斯市| 苏州市| 金坛市| 建始县| 双峰县| 宁陕县| 甘肃省| 大英县| 酒泉市| 永安市| 晋中市| 沙田区| 醴陵市| 襄城县| 磐石市| 五台县| 甘洛县| 岳西县| 天祝| 新宁县| 磐石市| 从江县| 顺昌县| 温州市| 贵阳市| 宝坻区| 安福县| 南丹县| 禄丰县| 岳普湖县| 泰宁县| 安徽省| 德化县|