Android 添加内核驱动

1. 在kernel的drivers目录下新建一个文件夹,如CharDriver,并进入此目录.

2. 把CharDriver.c文件放到当前目录,文件内容如下:

/*CharDriver.c*/
#define _NO_VERSION
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/slab.h>
#define KERNEL
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
#define SUCCESS 0
#ifndef MAX_PATH
#define MAX_PATH 260
#endif
MODULE_LICENSE("Dual BSD/GPL");
static char global_buf[MAX_PATH] = {0};
static int device_read(struct file *file, char *buf, size_t count, loff_t *f_pos);
static int device_write(struct file *file, const char *buf, size_t count, loff_t *f_pos);
static int device_open(struct inode *inode, struct file *file);
static int device_release(struct inode *inode, struct file *file);
struct file_operations tdd_fops = 
{
 .owner = THIS_MODULE,
 .read = device_read,
 .write = device_write,
 .open = device_open,
 .release = device_release,
};
#define DEVICE_NAME "char_dev"
static struct class *char_dev_class;
static int Device_Open = 0;
unsigned int test_major = 0;
static int device_open(struct inode *inode, struct file *file)
{
 #ifdef DEBUG
  printk("device_open(%p)\n",file);
 #endif
 if(Device_Open)
  return -EBUSY;
 Device_Open++;
 return SUCCESS;
}
static int device_release(struct inode *inode, struct file *file)
{
 #ifdef DEBUG
 printk("device_release(%p,%p)\n",inode,file);
 #endif
 Device_Open--;
 return Device_Open;
}
static inline int verify_area(int type, const void *addr, unsigned long size)
{
 return access_ok(type, addr, size)? 0:-EFAULT;
}
static int device_read(struct file *file, char *buf, size_t count, loff_t *f_pos)
{/*
 int left;
 if(verify_area(VERIFY_WRITE, buf, count) == -EFAULT)
  return -EFAULT;
 for(left = count; left>0;left--)
 {
  put_user(1,buf);
  buf++;
 }
 return count;*/
 if(copy_to_user(buf,global_buf,(count>MAX_PATH?MAX_PATH:count)*sizeof(char)))
  return -EFAULT;
 return count>MAX_PATH?MAX_PATH:count;
}
static int device_write(struct file *file, const char *buf, size_t count, loff_t *f_pos)
{
 if(copy_from_user(global_buf,buf,(count>MAX_PATH?MAX_PATH:count)*sizeof(char)))
  return -EFAULT;
 return count>MAX_PATH?MAX_PATH:count;
}
static int __init device_init(void)
{
 int result;
 result = register_chrdev(0,DEVICE_NAME, &tdd_fops);
 if(result < 0)
 {
  printk("char_dev:can't get major number\n");
  return result;
 }
 if(test_major == 0)
  test_major = result;
 printk("Hello, I'm in kernel mode\n");
 
 char_dev_class = class_create(THIS_MODULE, DEVICE_NAME);
 if (IS_ERR(char_dev_class))
  return -1;
 device_create(char_dev_class, NULL, MKDEV(test_major, 0), NULL, DEVICE_NAME);
 return 0;
}
static void __exit device_exit(void)
{
 printk("Hello, I'm goint to out\n");
 if(char_dev_class)
 {
  device_destroy(char_dev_class, MKDEV(test_major, 0));
  class_destroy(char_dev_class);
 }
 unregister_chrdev(test_major,DEVICE_NAME);
}
module_init(device_init);
module_exit(device_exit); 
3. 在当前目录中新建Makefile文件,内容为: obj-$(CONFIG_CHARDRIVER) += CharDriver.o

4. 在当前目录中新建Kconfig文件,内容如下:

   config CHARDRIVER
   tristate "Char Driver added by llx" 
   default n
   help
      test for adding driver to menuconfig.

5. 在kernel的drivers目录下找到Makefile文件,并添加: obj-$(CONFIG_CHARDRIVER) += CharDriver/

6. 在kernel的drivers目录下找到Kconfig文件, 在menu "Device Drivers"与endmenu之间添加: source "drivers/CharDriver/Kconfig"

7. 在kernel目录下运行make menuconfig可打开配置菜单配置是否打开此驱动.

8. 打开驱动编译后,把kernel烧录到设备中,可以使用adb命令:"cat /proc/devices"查看到驱动对应的"char_dev"已产生.

9. 如果在device_init中未调用class_create和device_create函数(对应在device_exit时不调用device_destroy和class_destroy),则不会在/dev目录下产生设备,需要使用: mknod /dev/char_dev c 251 0创建设备文件,其中"/dev/char_dev"是要生成的设备名及目录,选项c是指字符设备,251是装载驱动后生成的主设备号(可由cat /proc/devices查到),从设备号为0,创建设备文件后可以在/dev目录下ls char_dev

10. 如果要通过JNI等操作此设备,需要开放相应的权限,在Z:\android2.3.4_GB_T34_RV63\system\core\rootdir\ueventd.rc文件中添加: /dev/char_dev       0777  root   root 后,重编译OS,然后重新烧录OS即可.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值