如何通過udev和sys文件系統動態創建設備結點
在Linux2.6內核中,devfs被認為是過時的方法,并最終被拋棄,udev取代了它。Devfs的一個很重要的特點就是可以動態創建設備結點。那我們現在如何通過udev和sys文件系統動態創建設備結點呢?
本文引用地址:http://www.104case.com/article/150776.htm 下面通過一個實例,說明udev、sys動態創建設備結點的方法。注意代碼中紅色的部分是為了實現動態創建設備結點添加的。
#include linux/module.h>
#include linux/kernel.h>
#include linux/init.h>
#include linux/fs.h>
#include linux/cdev.h>
#include asm/uaccess.h>
#include linux/device.h>
MODULE_LICENSE (GPL);
int hello_major = 252;
int hello_minor = 0;
int number_of_devices = 1;
char data[50]=foobar not equal to barfoo;
struct cdev cdev;
dev_t dev = 0;
static int hello_open (struct inode *inode, struct file *file)
{
printk (KERN_INFO Hey! device openedn);
return 0;
}
static int hello_release (struct inode *inode, struct file *file)
{
printk (KERN_INFO Hmmm... device closedn);
return 0;
}
ssize_t hello_read (struct file *filp, char *buff, size_t count, loff_t *offp)
{
ssize_t result = 0;
if (copy_to_user (buff, data, sizeof(data)-1))
result = -EFAULT;
else
printk (KERN_INFO wrote %d bytesn, count);
return result;
}
ssize_t hello_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
ssize_t ret = 0;
printk (KERN_INFO Writing %d bytesn, count);
if (count>127) return -ENOMEM;
if (count0) return -EINVAL;
if (copy_from_user (data, buf, count)) {
ret = -EFAULT;
}
else {
data[127]='
主站蜘蛛池模板:
甘谷县|
光山县|
中西区|
汨罗市|
唐海县|
沁源县|
辰溪县|
武功县|
普兰店市|
安国市|
象山县|
绥阳县|
满洲里市|
阿图什市|
普兰店市|
保康县|
神农架林区|
贵州省|
都匀市|
吴旗县|
英吉沙县|
澄城县|
惠来县|
龙口市|
陇川县|
承德市|
黎平县|
建水县|
祥云县|
保德县|
青海省|
沙坪坝区|
舟山市|
富顺县|
贺兰县|
屯留县|
庆元县|
泗洪县|
祁门县|
枝江市|
四会市|