日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 嵌入式 > 嵌入式教程
[導(dǎo)讀]用udev在/dev/下動態(tài)生成設(shè)備文件,這樣用戶就不用手工調(diào)用mknod了

用udev在/dev/下動態(tài)生成設(shè)備文件,這樣用戶就不用手工調(diào)用mknod了。

利用的kernel API:

class_create : 創(chuàng)建class
class_destroy : 銷毀class
class_device_create : 創(chuàng)建device
class_device_destroy : 銷毀device


注意,這些API是2.6.13開始有的,在2.6.13之前,應(yīng)當(dāng)使用

class_simple_create
class_simple_destroy
class_simple_device_add
class_simple_device_remove

這一系列,也就是ldd3第14章描述的。 詳見:

https://lwn.net/Articles/128644/

Output:
===========================================
[root@localhost dynamic_dev_node]# insmod ./dummy_dev.ko
[root@localhost dynamic_dev_node]# file /dev/dummy_dev0
/dev/dummy_dev0: character special (250/0)
[root@localhost dynamic_dev_node]# rmmod dummy_dev.ko
[root@localhost dynamic_dev_node]# file /dev/dummy_dev0
/dev/dummy_dev0: ERROR: cannot open `/dev/dummy_dev0' (No such file or directory)




#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/pci.h>
#include <asm/uaccess.h>
#include <asm/atomic.h>
#include <asm/unistd.h>



#define THIS_DESCRIPTION "
This module is a dummy device driver, it registern
tta char device, and utilize udev to create/destroy n
ttdevice node under /dev/ dynamicallly."


MODULE_LICENSE("GPL");
MODULE_AUTHOR("albcamus <albcamus@gmail.com>");
MODULE_DESCRIPTION(THIS_DESCRIPTION);



#define DUMMY_MAJOR 250
#define DUMMY_MINOR 0
#define DUMMY_NAME "dummy_dev"





/**
* the open routine of 'dummy_dev'
*/
static int dummy_open(struct inode *inode, struct file *file)
{
printk("Open OKn");
return 0;
}

/**
* the write routine of 'dummy_dev'
*/
static ssize_t dummy_write(struct file *filp, const char *bp, size_t count, loff_t *ppos)
{
printk("Don't Write!n");
return 0;
}

/**
* the read routine of 'dummy_dev'
*/
static ssize_t dummy_read(struct file *filp, char *bp, size_t count, loff_t *ppos)
{
return 0;
}

/**
* the ioctl routine of 'dummy_dev'
*/
static int dummy_ioctl(struct inode *inode, struct file *filep,
unsigned int cmd, unsigned long arg)
{

return 0;
}


/**
* file_operations of 'dummy_dev'
*/
static struct file_operations dummy_dev_ops = {
.owner = THIS_MODULE,
.open = dummy_open,
.read = dummy_read,
.write = dummy_write,
.ioctl = dummy_ioctl,
};


/**
* struct cdev of 'dummy_dev'
*/
struct cdev *my_cdev;
struct class *my_class;



static int __init my_init(void)
{
int err, devno = MKDEV(DUMMY_MAJOR, DUMMY_MINOR);


/* register the 'dummy_dev' char device */
my_cdev = cdev_alloc();
cdev_init(my_cdev, &dummy_dev_ops);

my_cdev->owner = THIS_MODULE;

err = cdev_add(my_cdev, devno, 1);
if (err != 0)
printk("dummy pci device register failed!n");

/* creating your own class */
my_class = class_create(THIS_MODULE, "dummy_class");
if(IS_ERR(my_class)) {
printk("Err: failed in creating class.n");
return -1;
}

/* register your own device in sysfs, and this will cause udevd to create corresponding device node */
class_device_create(my_class, NULL, MKDEV(DUMMY_MAJOR, DUMMY_MINOR), NULL, DUMMY_NAME "%d", DUMMY_MINOR );



return 0;
}

static void __exit my_fini(void)
{
printk("byen");

cdev_del(my_cdev);
//kfree(my_cdev); no use. because that cdev_del() will call kfree if neccessary.


class_device_destroy(my_class, MKDEV(DUMMY_MAJOR, DUMMY_MINOR));
class_destroy(my_class);

}




module_init(my_init);
module_exit(my_fini);

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉