日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Linux自動創(chuàng)建設(shè)備節(jié)點

 灰色浪漫939 2014-01-14
在驅(qū)動用加入對udev的支持主要做的就是:在驅(qū)動初始化的代碼里調(diào)用class_create(...)為該設(shè)備創(chuàng)建一個class,再為每個設(shè)備調(diào)用device_create(...)( 在2.6較早的內(nèi)核中用class_device_create)創(chuàng)建對應(yīng)的設(shè)備。
    內(nèi)核中定義的struct class結(jié)構(gòu)體,顧名思義,一個struct class結(jié)構(gòu)體類型變量對應(yīng)一個類,內(nèi)核同時提供了class_create(…)函數(shù),可以用它來創(chuàng)建一個類,這個類存放于sysfs下面,一旦創(chuàng)建好了這個類,再調(diào)用 device_create(…)函數(shù)來在/dev目錄下創(chuàng)建相應(yīng)的設(shè)備節(jié)點。這樣,加載模塊的時候,用戶空間中的udev會自動響應(yīng) device_create(…)函數(shù),去/sysfs下尋找對應(yīng)的類從而創(chuàng)建設(shè)備節(jié)點。

    struct class定義在頭文件include/linux/device.h中class_create(…)在/drivers/base/class.c中實現(xiàn)device_create(…)函數(shù)在/drivers/base/core.c中實現(xiàn)class_destroy(...),device_destroy(...)也在/drivers/base/core.c中實現(xiàn)調(diào)用過程類似如下:
static struct class *spidev_class;
/*-------------------------------------------------------------------------*/
static int spidev_probe(struct spi_device *spi)
{
        ....
        dev = device_create(spidev_class, &spi->dev, spidev->devt,
                spidev, "spidev%d.%d",
                spi->master->bus_num, spi->chip_select);
        ...
}
 
static int spidev_remove(struct spi_device *spi)
{
    ......
    device_destroy(spidev_class, spidev->devt);
    .....
 
    return 0;
}
 
static struct spi_driver spidev_spi = {
    .driver = {
        .name =        "spidev",
        .owner =    THIS_MODULE,
    },
    .probe =    spidev_probe,
    .remove =    __devexit_p(spidev_remove),
 
};
/*-------------------------------------------------------------------------*/
static int __init spidev_init(void)
{
    ....
    spidev_class = class_create(THIS_MODULE, "spidev");
    if (IS_ERR(spidev_class)) {
        unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
        return PTR_ERR(spidev_class);
    }
    ....
}
module_init(spidev_init);
 
static void __exit spidev_exit(void)
{
    ......
    class_destroy(spidev_class);
    ......
}
module_exit(spidev_exit);
 
MODULE_DESCRIPTION("User mode SPI device interface");
MODULE_LICENSE("GPL");
 
下面以一個簡單字符設(shè)備驅(qū)動來展示如何使用這幾個函數(shù) 
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/device.h>
 
int HELLO_MAJOR = 0;
int HELLO_MINOR = 0;
int NUMBER_OF_DEVICES = 2;
 
struct class *my_class;
struct cdev cdev;
dev_t devno;
 
struct file_operations hello_fops = {
 .owner = THIS_MODULE
};
 
static int __init hello_init (void)
{
    int result;
    devno = MKDEV(HELLO_MAJOR, HELLO_MINOR);
    if (HELLO_MAJOR)
        result = register_chrdev_region(devno, 2, "memdev");
    else
    {
        result = alloc_chrdev_region(&devno, 0, 2, "memdev");
        HELLO_MAJOR = MAJOR(devno);
    }  
    printk("MAJOR IS %d\n",HELLO_MAJOR);
    my_class = class_create(THIS_MODULE,"hello_char_class");  //類名為hello_char_class
    if(IS_ERR(my_class)) 
    {
        printk("Err: failed in creating class.\n");
        return -1; 
    }
    device_create(my_class,NULL,devno,NULL,"memdev");      //設(shè)備名為memdev
    if (result<0) 
    {
        printk (KERN_WARNING "hello: can't get major number %d\n", HELLO_MAJOR);
        return result;
    }
 
    cdev_init(&cdev, &hello_fops);
    cdev.owner = THIS_MODULE;
    cdev_add(&cdev, devno, NUMBER_OF_DEVICES);
    printk (KERN_INFO "Character driver Registered\n");
    return 0;
}
 
static void __exit hello_exit (void)
{
    cdev_del (&cdev);
    device_destroy(my_class, devno);         //delete device node under /dev//必須先刪除設(shè)備,再刪除class類
    class_destroy(my_class);                 //delete class created by us
    unregister_chrdev_region (devno,NUMBER_OF_DEVICES);
    printk (KERN_INFO "char driver cleaned up\n");
}
 
module_init (hello_init);
module_exit (hello_exit);
 
MODULE_LICENSE ("GPL");
這樣,模塊加載后,就能在/dev目錄下找到memdev這個設(shè)備節(jié)點了。

例2:內(nèi)核中的drivers/i2c/i2c-dev.c
在i2cdev_attach_adapter中調(diào)用device_create(i2c_dev_class, &adap->dev,
         MKDEV(I2C_MAJOR, adap->nr), NULL,
         "i2c-%d", adap->nr);
這樣在dev目錄就產(chǎn)生i2c-0  或i2c-1節(jié)點
 
    接下來就是udev應(yīng)用,udev是應(yīng)用層的東西,udev需要內(nèi)核sysfs和tmpfa的支持,sysfs為udev提供設(shè)備入口和uevent通道,tmpfs為udev設(shè)備文件提供存放空間
udev的源碼可以在去相關(guān)網(wǎng)站下載,然后就是對其在運行環(huán)境下的移植,指定交叉編譯環(huán)境,修改Makefile下的CROSS_COMPILE,如為mipsel-linux-,DESTDIR=xxx,或直接make CROSS_COMPILE=mipsel-linux-,DESTDIR=xxx 并install把主要生成的udevd、udevstart拷貝rootfs下的/sbin/目錄內(nèi),udev的配置文件udev.conf和rules.d下的rules文件拷貝到rootfs下的/etc/目錄內(nèi),并在rootfs/etc/init.d/rcS中添加以下幾行:
echo “Starting udevd...”
/sbin/udevd --daemon
/sbin/udevstart
(原rcS內(nèi)容如下:
# mount filesystems
/bin/mount -t proc /proc /proc
/bin/mount -t sysfs sysfs /sys
/bin/mount -t tmpfs tmpfs /dev
# create necessary devices
/bin/mknod /dev/null c 1 3
/bin/mkdir /dev/pts
/bin/mount -t devpts devpts /dev/pts
/bin/mknod /dev/audio c 14 4
/bin/mknod /dev/ts c 10 16
這樣當系統(tǒng)啟動后,udevd和udevstart就會解析配置文件,并自動在/dev下創(chuàng)建設(shè)備節(jié)點文件

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多