利用udev、sys動態(tài)創(chuàng)建linux設備結點
在Linux2.6內核中,devfs被認為是過時的方法,并最終被拋棄,udev取代了它。Devfs的一個很重要的特點就是可以動態(tài)創(chuàng)建設備結點。那我們現在如何通過udev和sys文件系統(tǒng)動態(tài)創(chuàng)建設備結點呢?
本文引用地址:http://www.104case.com/article/148527.htm下面通過一個實例,說明udev、sys動態(tài)創(chuàng)建設備結點的方法。注意代碼中紅色的部分是為了實現動態(tài)創(chuàng)建設備結點添加的。
#include
#include
#include
#include
#include
#include
#include
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]=' 主站蜘蛛池模板: 阿勒泰市| 尼勒克县| 惠来县| 卓资县| 泗洪县| 哈密市| 星子县| 宾川县| 安阳市| 安陆市| 肃宁县| 凌源市| 穆棱市| 赤壁市| 临沭县| 渝北区| 辉县市| 望城县| 溧水县| 常山县| 唐海县| 西乌珠穆沁旗| 泸溪县| 安庆市| 桐庐县| 台南市| 理塘县| 赣榆县| 西安市| 崇仁县| 长武县| 贵溪市| 大姚县| 微山县| 万山特区| 绥化市| 孟州市| 武冈市| 甘泉县| 中宁县| 吉隆县|