(10)x210: linux2.6之后的字符驱动

#include <linux/module.h>        // module_init  module_exit
#include <linux/init.h>            // __init   __exit
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <mach/regs-gpio.h>
#include <mach/gpio-bank.h>        // arch/arm/mach-s5pv210/include/mach/gpio-bank.h
#include <linux/string.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/cdev.h>

#define MYMAJOR 200
#define MYNAME "chardev"
#define MYCNT 1

int mymajor ;
static dev_t mydev;
static struct cdev test_cdev;

char kbuf[100];
static int test_chardev_open(struct inode *inode,struct file *file)
{
printk(KERN_INFO "new test chardev_open\n");
return 0;
}
static int test_chardev_release(struct inode *inode,struct file *file){
printk(KERN_INFO "new test chardev_release\n");
return 0;
}
static ssize_t test_chardev_read(struct file *file,char __user *buf,size_t size, loff_t *ppos)
{
printk(KERN_INFO "new test read\n");
return 0;

}
static ssize_t test_chardev_write(struct file *file,
const char __user *ubuf,size_t count,loff_t * ppos){
printk(KERN_INFO " new test write\n");
return 0;
}

static const struct file_operations test_fops={
.owner=THIS_MODULE,
.open=test_chardev_open,
.release=test_chardev_release,
.write=test_chardev_write,
.read=test_chardev_read,
};
static int __init chardev_init(void)
{ int ret=-1;
printk(KERN_INFO "new module chardev init");
mydev=MKDEV(MYMAJOR,0);
ret=register_chrdev_region(mydev,MYCNT,MYNAME
);
if(ret)
{
printk(KERN_ERR "register_chrdev fail\n");
return -EINVAL;
}

printk(KERN_INFO "new register_chrdev success...myjor=%d.\n",mymajor);
cdev_init(&test_cdev,&test_fops);
ret=cdev_add(&test_cdev,mydev,MYCNT);
if(ret)
{
printk(KERN_ERR "unable to add cdev\n");
return -EINVAL;
}
printk(KERN_INFO "new cdev_add success\n");



return 0;
}
static void __exit chardev_exit(void)
{
    
printk(KERN_INFO "new module chardev exit\n");
cdev_del(&test_cdev);
unregister_chrdev_region(mydev, MYCNT)
;
}

module_init(chardev_init);
module_exit(chardev_exit);
// MODULE_xxx这种宏作用是用来添加模块描述信息

MODULE_LICENSE("GPL");                // 描述模块的许可证

MODULE_AUTHOR("aston");                // 描述模块的作者

MODULE_DESCRIPTION("module test");    // 描述模块的介绍信息

MODULE_ALIAS("alias xxx");            // 描述模块的别名信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值