linux驅動自動新增裝置節點

2021-06-22 15:08:53 字數 1159 閱讀 2872

大概思路:

先在驅動初始化的**裡呼叫class_create(...)為該裝置建立乙個class

再為每個裝置呼叫device_create(...)( 在2.6較早的核心中用class_device_create)建立對應的裝置

驅動退出函式時先刪除

裝置,再刪除類

下面以乙個簡單字元裝置驅動來展示如何使用這幾個函式 

#include

#include

#include

#include

#include

#include

int hello_major = 210;

int hello_minor = 0;

int number_of_devices = 2;

struct class *my_class;

struct cdev cdev;

dev_t devno;

struct file_operations hello_fops = ;

static int __init hello_init (void)

printk("major is %d\n",hello_major);

my_class = class_create(this_module,"hello_char_class");  //類名為hello_char_class

if(is_err(my_class)) 

device_create(my_class,null,devno,null,"memdev");      //裝置名為memdev

if (result<0) 

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)

module_init (hello_init);

module_exit (hello_exit);

module_license ("gpl");

Linux裝置驅動 新增裝置節點

1 環境描述 host ubuntu14.04 64bit target smdk2410 kernel linux 2.6.39.4 2 前言 我們在剛開始寫linux裝置驅動程式的時候,很多時候都是利用mknod命令手動建立裝置節點,實際上linux核心為我們提供了一組函式,可以用來在模組載入的...

ubuntu乙太網裝置節點 新增裝置節點

mknod 建立塊裝置或者字元裝置檔案。此命令的適用範圍 redhat rhel ubuntu centos suse opensuse fedora。1 語法 mknod 選項 裝置名 裝置型別 主裝置號 次裝置號 建立字元裝置檔案 root localhost mknod dev ttywj c...

新增裝置輪詢操作

private ifacilityservice facilitysrva servicelocator.get private ifacilityservice facilitysrvb servicelocator.get 需要生成兩個 類實體,不然同乙個 類實體的,兩次呼叫會是按序列化方式進行...