日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

()shi linux字符设备,Linux字符设备驱动基础(三)

發布時間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ()shi linux字符设备,Linux字符设备驱动基础(三) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Linux字符設備驅動基礎(三)

6 創建設備節點

6.1 手動創建設備節點

查看申請的設備名及主設備號:

cat /proc/devices

# cat /proc/devices

Character devices:

1 mem

2 pty

3 ttyp

4 /dev/vc/0

4 tty

5 /dev/tty

5 /dev/console

5 /dev/ptmx

7 vcs

10 misc

11 module_test

Block devices:

8 sd

93 spinor

259 blkext

Mknod用法:

# mknod

BusyBox v1.27.1 (2018-10-22 13:38:05 CST) multi-call binary.

Usage: mknod [-m MODE] NAME TYPE MAJOR MINOR

Create a special file (block, character, or pipe)

-m MODE Creation mode (default a=rw)

TYPE:

b Block device

c or u Character device

p Named pipe (MAJOR and MINOR are ignored)

如:mknod module_test b 11 0

6.2 自動創建設備節點

利用udev(mdev)來實現設備文件的自動創建,首先應保證支持udev(mdev),由busybox配置。

在驅動用加入對udev 的支持主要做的就是:在驅動初始化的代碼里調用class_create(…)為該設備創建一個class,再為每個設備調用device_create(…)創建對應的設備。

內核中定義的struct class結構體,顧名思義,一個struct class結構體類型變量對應一個類,內核同時提供了class_create(…)函數,可以用它來創建一個類,這個類存放于sysfs下面,一旦創建好了這個類,再調用 device_create(…)函數來在/dev目錄下創建相應的設備節點。

這樣,加載模塊的時候,用戶空間中的udev會自動響應 device_create()函數,去/sysfs下尋找對應的類從而創建設備節點。

第一步:創建class類

Struct class chardev_test_class = class_create(THIS_MODULE, CHARDEV_DRV_NAME);

源碼如下:

/* This is a #define to keep the compiler from merging different

* instances of the __key variable */

#define class_create(owner, name)\

({\

static struct lock_class_key __key;\

__class_create(owner, name, &__key);\

})

參數:

-Owner:THIS_MODULE;

-Name:class name。

/**

* class_create - create a struct class structure

* @owner: pointer to the module that is to "own" this struct class

* @name: pointer to a string for the name of this class.

* @key: the lock_class_key for this class; used by mutex lock debugging

*

* This is used to create a struct class pointer that can then be used

* in calls to device_create().

*

* Returns &struct class pointer on success, or ERR_PTR() on error.

*

* Note, the pointer created here is to be destroyed when finished by

* making a call to class_destroy().

*/

struct class *__class_create(struct module *owner, const char *name,

struct lock_class_key *key)

{

struct class *cls;

int retval;

cls = kzalloc(sizeof(*cls), GFP_KERNEL);

if (!cls) {

retval = -ENOMEM;

goto error;

}

cls->name = name;

cls->owner = owner;

cls->class_release = class_create_release;

retval = __class_register(cls, key);

if (retval)

goto error;

return cls;

error:

kfree(cls);

return ERR_PTR(retval);

}

class銷毀函數:

/**

* class_destroy - destroys a struct class structure

* @cls: pointer to the struct class that is to be destroyed

*

* Note, the pointer to be destroyed must have been created with a call

* to class_create().

*/

void class_destroy(struct class *cls)

{

if ((cls == NULL) || (IS_ERR(cls)))

return;

class_unregister(cls);

}

第二步:創建device

Struct device *dev = device_create(chardev_test_class, &pdev->dev,

MKDEV(major, minor), NULL,

CHARDEV_DRV_NAME);

參數:

-class:該設備需綁定的class類;

-parent:該設備的父設備;

-devt:該設備的設備號;

-drvdata:設備私有參數;

-fmt:設備名

/**

* device_create - creates a device and registers it with sysfs

* @class: pointer to the struct class that this device should be registered to

* @parent: pointer to the parent struct device of this new device, if any

* @devt: the dev_t for the char device to be added

* @drvdata: the data to be added to the device for callbacks

* @fmt: string for the device's name

*

* This function can be used by char device classes. A struct device

* will be created in sysfs, registered to the specified class.

*

* A "dev" file will be created, showing the dev_t for the device, if

* the dev_t is not 0,0.

* If a pointer to a parent struct device is passed in, the newly created

* struct device will be a child of that device in sysfs.

* The pointer to the struct device will be returned from the call.

* Any further sysfs files that might be required can be created using this

* pointer.

*

* Returns &struct device pointer on success, or ERR_PTR() on error.

*

* Note: the struct class passed to this function must have previously

* been created with a call to class_create().

*/

struct device *device_create(struct class *class, struct device *parent,

dev_t devt, void *drvdata, const char *fmt, ...)

{

va_list vargs;

struct device *dev;

va_start(vargs, fmt);

dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);

va_end(vargs);

return dev;

}

設備銷毀函數:

/**

* device_destroy - removes a device that was created with device_create()

* @class: pointer to the struct class that this device was registered with

* @devt: the dev_t of the device that was previously registered

*

* This call unregisters and cleans up a device that was created with a

* call to device_create().

*/

void device_destroy(struct class *class, dev_t devt)

{

struct device *dev;

dev = class_find_device(class, NULL, &devt, __match_devt);

if (dev) {

put_device(dev);

device_unregister(dev);

}

}

總結

以上是生活随笔為你收集整理的()shi linux字符设备,Linux字符设备驱动基础(三)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。