我的第一个uClinux驱动程序

热烈庆祝第一个最简单的uClinux驱动程序编译成功,虽然按照资料上讲的看起来很简单,但是真正把它放到内核里编译通过还是要有一段路要走的,其实只要仔细,应该不难。
以下是源程序,很多书上都有讲。
#define  __NO_VERSION__
#include  <linux/module.h>
#include  <linux/version.h>
#include  <linux/kernel.h>
#include  <asm/uaccess.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <asm/segment.h>
 
char kernel_version [] = UTS_RELEASE;
/*
struct file_operations {
int (*seek) (struct inode * ,struct file *, off_t ,int);
int (*read) (struct inode * ,struct file *, char ,int);
int (*write) (struct inode * ,struct file *, off_t ,int);
int (*readdir) (struct inode * ,struct file *, struct dirent * ,int);
int (*select) (struct inode * ,struct file *, int ,select_table *);
int (*ioctl) (struct inode * ,struct file *, unsined int ,unsigned long);
int (*mmap) (struct inode * ,struct file *, struct vm_area_struct *);
int (*open) (struct inode * ,struct file *);
int (*release) (struct inode * ,struct file *);
int (*fsync) (struct inode * ,struct file *);
int (*fasync) (struct inode * ,struct file *,int);
int (*check_media_change) (struct inode * ,struct file *);
int (*revalidate) (dev_t dev);
}
*/
unsigned int test_major = 0;
static int read_test(struct inode *node,struct file *file,char *buf,int count)
{
      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;
}
static int write_test(struct inode *inode,struct file *file,const char *buf,int count)
{
  return count;
}
static int open_test(struct inode *inode,struct file *file )
{
  MOD_INC_USE_COUNT;
  return 0;
}
static void release_test(struct inode *inode,struct file *file )
{
  MOD_DEC_USE_COUNT;
}
struct file_operations test_fops={
read:read_test,
write:write_test,
open:open_test,
release:release_test,
};
int init_module(void)
{
  int result;
  result = register_chrdev(0, "testdriver", &test_fops);
  if (result < 0)
  {
  printk("test: can't get major number/n");
  return result;
  }
  if (test_major == 0) test_major = result; /* dynamic */
  return 0;
}
void cleanup_module(void)
{
  unregister_chrdev(test_major,"test");

把这个testdriver.c放到内核的drivers/char目录下。
第二步:添加设备;
1)修改lux - 214. x/ driver/ char/Makefile 在适当位置添加一行: obj - $(CONFIG- TEST) + = test .o
2)linux - 214. x/ driver/ char/ Config. in , 添加一行: bool ’test device’CONFIG- TEST
3)修改linux - 214. x/ driver/ char/ mem. c 在适当位置添加:
# ifdef CONFIG- TEST
extern void init_module (void) ;
# endif
同时在chr - dev- init () 函数中添加:
# ifdef CONFIG- TEST
init_module () ;
# endif
4) 修改vendor/ Samsung/ 44bb0/Makefile建立设备节点:
在12 - - - 35 行间,DEVICE 部分添加如下内容test ,c ,254 ,0
5) make menuconfig ,在character device 选中testdevice
重新编译内核并下载到开发板,在控制台输入
/proc> cat devices 就能看到你的驱动程序了
 
接下来就是要写一个AP去用这个驱动了,待续。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值