linux驅動在sys下新增讀寫屬性

2021-07-24 21:50:41 字數 3105 閱讀 4601

class_create

用來建立乙個類,存放於sysfs下面,解除安裝時配套使用class_destroy

device_create

在/dev/目錄下建立相應的裝置節點,在載入模組的時候,使用者空間的udev會自動響應device_create函式,去/sysfs下尋找對應的類似而建立裝置節點,解除安裝時配套使用 device_destroy

device_create_file

實際呼叫sysfs_create_file,可以在/sys/(注意這裡不一定是/sys/class下,由引數一確定)下建立對應的屬性檔案,從而通過對該檔案的讀寫屬性實現特定的資料操作,解除安裝是自動會把節點刪除

下面以misc裝置和普通字元裝置為例說明一下如何使用上面三個函式在驅動中新增讀寫屬性

注:對於misc裝置,不需要我們去執行class_create和device_create。因為在misc_register進行裝置註冊的時候,就已經自動執行了,所以只需要在註冊完裝置後,再新增相應的讀寫屬性即可。當然也不需要我們去呼叫class_destroy和device_destroy來釋放資源,因為misc本身做了處理。

#include 

#include

#include

#include

#include

#include

#include

#include

//#define misc_device

#define hello_minors 256

#define hello_major 125

#define driver_name "hello_driver"//驅動名字

#define device_name "hello_device"//生成的裝置節點名字

static

intvalue=0;

#ifdef misc_device

#else

static

struct class *hello_dev_class;

#endif

//對應應用層的write

static ssize_t att_store(struct device *dev,

struct device_attribute *attr,

const

char *buf, size_t count)

//對應應用層的read

static ssize_t att_show(struct device *dev,

struct device_attribute *attr,

char *buf)

static device_attr(test,0777,att_show,att_store);

static

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

static

int hello_open(struct inode *inode, struct file *file)

static

struct file_operations hello_ops = ;

#ifdef misc_device

static

struct miscdevice hello_dev = ;

#endif

#ifdef misc_device

//如果匯流排匹配成功 就會執行probe函式

static

int hello_probe(struct platform_device *pdv)

#else //普通字元裝置

static

int hello_probe(struct platform_device *pdv)

hello_dev_class=class_create(this_module,"hello_dev");

if (is_err(hello_dev_class))

//生成裝置 /dev/hello_device /dev/char/125:256

//引數二為指向的父裝置null

dev = device_create(hello_dev_class, null, mkdev(hello_major,hello_minors), null, "%s", device_name);

if (is_err(dev))

//在刪除裝置的時候自動刪除該節點

//生成節點位置/sys/class/hello_dev/hello_device/device/test

ret = device_create_file(dev, &dev_attr_test);

if(ret)

printk("create enable file for hello_device err!\n");

return ret;

}#endif

static

int hello_remove(struct platform_device *pdv)

static

void hello_shutdown(struct platform_device *pdv)

static

int hello_suspend(struct platform_device *pdv,pm_message_t pmt)

static

int hello_resume(struct platform_device *pdv)

//建立裝置

static

struct platform_device hello_device=

; //建立驅動

struct platform_driver hello_driver =

};static

int hello_init(void)

static

void hello_exit(void)

module_init(hello_init);

module_exit(hello_exit);

module_license("gpl");

新增硬碟驅動 讀identify

硬碟的chs模式是指chs cylinder head sector 模式,既磁頭數 heads 柱面數 cylinders 扇區數 sectors per track 以及相應的定址方式.剛一開始硬碟的容量還非常小,人們採用與軟盤類似的結構生產硬碟.也就是硬碟碟片的每一條磁軌都具有相同的扇區數。其...

在linux下新增路由

linux下新增路由的方法 一 使用 route 命令新增 使用route 命令新增的路由,機器重啟或者網絡卡重啟後路由就失效了,方法 新增到主機的路由 route add host 192.168.168.110 dev eth0 route add host 192.168.168.119 gw...

Linux下修改sys密碼

首先利用ssh等工具登入oracle安裝的伺服器 其次切換到oracle使用者登入 su oracle ssh secure shell 3.2.9 build 283 this copy of ssh secure shell is a non commercial version.this ve...