简单的LINUX字符设备驱动及编译进Linux内核
2019-07-12 20:51发布
生成海报
简单的LINUX字符设备驱动及编译进Linux内核…
2010-07-06 11:23:00| 分类: 嵌入式linux|举报|字号 订阅
下载LOFTER我的照片书 |
驱动代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifndef MODULE
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif
#define MAJOR_NUM 253
MODULE_LICENSE("GPL");
int dev_test_open(struct inode *, struct file *);
ssize_t dev_test_read(struct file *, char *, size_t, loff_t *);
ssize_t dev_test_write(struct file *, const char *, size_t, loff_t *);
int dev_test_release(struct inode *, struct file *);
struct file_operations hello_fops =
{
open:dev_test_open,
read:dev_test_read,
write:dev_test_write,
release:dev_test_release,
};
char *pBuff;
static int __init dev_test_init(void)
{
printk("<1>hello dev test
");
if (register_chrdev(MAJOR_NUM, "dev_test", &hello_fops))
printk("<1>register failed
");
return 0;
}
static void __exit dev_test_exit(void)
{
printk("<1>bye dev_test
");
if (unregister_chrdev(MAJOR_NUM, "dev_test"))
printk("<1>unregiter failed
");
}
int dev_test_open(struct inode *pInode, struct file *pFile)
{
pBuff = (char*)kmalloc(128, GFP_KERNEL);
if (pBuff == NULL)
printk("<1>kmalloc memory failed
");
return 0;
}
int dev_test_release(struct inode *pInode, struct file *pFile)
{
kfree(pBuff);
return 0;
}
ssize_t dev_test_read(struct file *pFile, char *buff, size_t len, loff_t *pOff)
{
if (copy_to_user(buff, pBuff, len))
{
printk("<1>copy_from_user failed
");
return -EFAULT;
}
return len;
}
ssize_t dev_test_write(struct file *pFile, const char *buff, size_t len, loff_t *pOff)
{
int i = 0;
if (copy_from_user(pBuff, buff, len))
{
printk("<1>copy_to_user failed
");
return -EFAULT;
}
while (pBuff[i] != '
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮