在Linux 2.6內(nèi)核中,devfs被認(rèn)為是過時的方法,并最終被拋棄,udev取代了它。Devfs的一個很重要的特點就是可以動態(tài)創(chuàng)建設(shè)備結(jié)點。那我們現(xiàn)在如何通過udev和sys文件系統(tǒng)動態(tài)創(chuàng)建設(shè)備結(jié)點呢?
下面通過一個實例,說明udev、sys動態(tài)創(chuàng)建設(shè)備結(jié)點的方法。注意代碼中紅色的部分是為了實現(xiàn)動態(tài)創(chuàng)建設(shè)備結(jié)點添加的。
#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 opened ");
return 0;
}
static int hello_release (struct inode *inode, struct file *file)
{
printk (KERN_INFO "Hmmm... device closed ");
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 bytes ", 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 bytes ", count);
if (count>127) return -ENOMEM;
if (count<0) return -EINVAL;
if (copy_from_user (data, buf, count)) {
ret = -EFAULT;
}
else {
data[127]=' 隆尧县| 山阴县| 土默特左旗| 易门县| 安仁县| 辽中县| 静安区| 龙岩市| 肇庆市| 潞城市| 乌鲁木齐县| 翁源县| 浦江县| 阜新市| 金门县| 诏安县| 伊吾县| 万州区| 崇义县| 永宁县| 大渡口区| 肃南| 南岸区| 怀安县| 奉新县| 天门市| 瑞丽市| 梅州市| 阜平县| 安丘市| 长顺县| 礼泉县| 历史| 昭觉县| 普格县| 乡宁县| 本溪| 静安区| 鄯善县| 神农架林区| 孟州市|
電子發(fā)燒友App