linux驅動之裝置號與建立裝置節點,linux建立裝置節點

來源:互聯網
上載者:User

linux驅動之裝置號與建立裝置節點,linux建立裝置節點

裝置號:

1、自動分配

major = register_chrdev(0,"first_drv",&first_sdv_fops);//註冊

註冊裝置時給裝置號寫0,則核心會自動分配一個主裝置號返回。

2、手動指定

到 proc/devices 檔案下尋找都有哪些主裝置號,如果沒有被使用的我們即可拿過來使用它。

應用程式裝置節點:

我們會使用open這個函數帶開一個裝置,那麼open("dev/xxx")中dev/xxx 這個裝置節點怎麼來?

1、用 mknod /dev/xxx c 主裝置號  次裝置號

2、自動建立: 

提示:

mdev:  根據系統資訊 建立裝置節點

方法:

定義兩個變數:

static struct class *firstdrv_class;
static struct class_devices *firstdrv_class_dev;

在入口函數中添加:

firstdrv_class = class_create(THIS_MODULE,"first_drv");if(IS_ERR(firstdrv_class))return PTR_ERR(firstdrv_class);firstdrv_class_dev = device_create(firstdrv_class,NULL,MKDEV(major,0),NULL,"wq_device");if(unlikely(IS_ERR(firstdrv_class_dev)))return PTR_ERR(firstdrv_class_dev);

在出口函數中添加:

device_unregister(firstdrv_class_dev);class_destroy(firstdrv_class);

這樣在 /dev下就會產生一個  wq_device  裝置。並且會在 /sys/class/first_drv  目錄下找到一個 wq_device裝置,

在/sys/class/first_drv/wq_device 會有一個 dev的檔案  檔案的內容是: “252:0”   

mdev就會根據這些資訊建立裝置節點。

為什麼sys目錄一更改,mdev就會根據這些資訊建立裝置節點呢?

原因是 在我們的/etc/init.d/  目錄中的rcS檔案中 有如下內容:

echo /sbin/mdev > /proc/sys/kernel/hotplug  

核心一旦有載入或者卸載時  就會調用/proc/sys/kernel/hotplug 所指示的應用程式。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.