歡迎您光臨本站 註冊首頁

linux字元驅動模板

←手機掃碼閱讀     火星人 @ 2014-03-26 , reply:0

linux 字元驅動模板 2.4以下的內核適用。

#include
#include
#include
#include
#include
#include
#include


#define MAJOR_NUM 125
#define DEVICE_NAME "emptychr"

static ssize_t test_read(struct file *file,char *buf,size_t count,loff_t *f_pos)
{
return count;
}
static ssize_t test_write(struct file *file, const char *buf, size_t count, loff_t *f_pos)
{
return count;
}
static int test_open(struct inode *inode,struct file *file )
{
MOD_INC_USE_COUNT;
return 0;
}
static int test_release(struct inode *inode,struct file *file )
{
MOD_DEC_USE_COUNT;
return 0;
}
static int test_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
return 0;
}
struct file_operations test_fops = {
read:test_read,
write:test_write,
open: test_open,
release:test_release,
ioctl:test_ioctl
};
int test_init(void)
{
int result;
result = register_chrdev(MAJOR_NUM, DEVICE_NAME, &test_fops);
if (result < 0) {
printk(KERN_INFO "test: can't get major number\n");
return result;
}

printk("init module\n");
return 0;

}
void test_exit(void)
{
unregister_chrdev(MAJOR_NUM,DEVICE_NAME);
printk("cleanup_module\n");
}


module_init(test_init);
module_exit(test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("huangxb");

[火星人 ] linux字元驅動模板已經有357次圍觀

http://coctec.com/docs/linux/show-post-188364.html