第一个安卓驱动程序

hello~

经过了三个星期的网上资料学习,以及前人指导,终于算是完成了第一个安卓驱动.使用的是展讯的7715芯片,第一次写博客,权当记录下自己的学习经历吧.

驱动的.c文件

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <linux/device.h>


static struct class *firstdev_class;
static struct device *firstdev_class_dev;


static int first_dev_open(struct inode *inode,struct file *file)
{
    printk("first_dev_open\n");
    return nonseekable_open(inode, file);
}

static int first_dev_write(struct file *file, const char __user * buf, size_t count, loff_t *ppos)
{
    printk("hello write.\n");
    return 0;

}
static struct file_operations first_dev_fops = {
    .owner = THIS_MODULE,     //一个宏
    .open  = first_dev_open,
    .write = first_dev_write,
};

int major;
static struct cdev first_cdev;
static struct class *cls;
static int first_dev_init(void)
{

    //major = register_chrdev(0,"first_dev",&first_dev_fops);//注册,告诉内核,主设备号.名称

/*
    firstdev_class = class_create (THIS_MODULE, "firstdev");
//  if (IS_ERR(firstdev_class))
//      return PTR_ERR(firstdev_class);

    cdev_init(&first_dev, &first_cdev);
    cdev_add(&first_dev, MKDEV(major, 0), 1);

    firstdev_class_dev = device_create(firstdev_class, NULL, MKDEV(major, 0), NULL, "first_dev");  //设别节点会在dev/first_dev
//  if (unlikely(IS_ERR(firstdev_class_dev)))
//      return PTR_ERR(firstdev_class_dev);

*/
dev_t devid;
if (major) {
        devid = MKDEV(major, 0);
        register_chrdev_region(devid, 1, "first");  /* (major,0~1) 对应 hello_fops, (major, 2~255)都不对应hello_fops */
    } else {
        alloc_chrdev_region(&devid, 0, 1, "first"); /* (major,0~1) 对应 hello_fops, (major, 2~255)都不对应hello_fops */
        major = MAJOR(devid);                     
    }

    cdev_init(&first_cdev, &first_dev_fops);
    cdev_add(&first_cdev, devid, 1);
    cls = class_create(THIS_MODULE, "first");
    device_create(cls, NULL, MKDEV(major, 0), NULL, "first");

    return 0;
}

static void first_dev_exit(vodi)
{

    unregister_chrdev(major,"first_dev");   // 卸载
    device_unregister(firstdev_class_dev);
    class_destroy(firstdev_class);

}

module_init(first_dev_init);
module_exit(first_dev_exit);
MODULE_LICENSE("GPL");

Makefile文件

obj-y += first_drv.o

Kconfig文件

config FIRST_DRV
bool “test”
default n
help
enable benchmark proc

应用程序.c文件

 #include<sys/types.h>
 #include<sys/stat.h>
 #include<fcntl.h>
 #include <stdio.h>

int main(int argc , char **argv)
{
    int fd;
    int val = 1;
    fd = open("/dev/first", O_RDWR);
    if(fd < 0)
        printf("ni  haishi  dabukai  ya !\n");
    write(fd, &val, 4);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值