第一个安卓驱动程序

来源:互联网 发布:备案域名交易平台top 编辑:程序博客网 时间:2024/05/21 09:52

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;}