+ -
当前位置:首页 → 问答吧 → 【求助】请教一个驱动函数入口的问题

【求助】请教一个驱动函数入口的问题

时间:2006-09-18

来源:互联网

各位兄弟帮忙!

以下是我自己写的一个驱动程序的.c文件,open、close、read3个函数运行正常,读出数据正确,只有write不对,好象是根本就没有调用,没有进到函数里面去,不知道为什么,请有经验的兄弟帮帮忙!
/////////////代码开始////////////////////
/*********************************************************************
*              在AT91RM9200上自行扩展的代替pc104的总线的驱动程序
*********************************************************************/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <asm/io.h>

#ifdef CONFIG_DEVFS_FS
#include <linux/devfs_fs_kernel.h>
#endif

static int AT91_PC104;

#define PC104_SIZE 1024
#define PC104_MAJOR 125
#define NR_PC104_DEVICES 1

#ifdef CONFIG_DEVFS_FS
static devfs_handle_t devfs_handle = NULL;
static devfs_handle_t devfs_pc104[NR_PC104_DEVICES];
#endif
/*********************************************************************
*              函数声明
*********************************************************************/
static int at91_pc104_init(void);
static void at91_pc104_cleanup(void);
static void pc104_init(void);
static int pc104_open(struct inode *inode, struct file *filp);
static int pc104_close(struct inode *inode, struct file *filp);
static ssize_t pc104_read(struct file *filp, char *buf, size_t count, loff_t *offset);
static ssize_t pc104_write(struct file *filp, const char *buf, size_t count, loff_t *offset);
/*********************************************************************+
*              全局变量定义
**********************************************************************/
int pc104_param = 9;
static int pc104_initialized = 0;
static int pc104_reading=0;
static int pc104_writing=0;
static volatile int pc104_flag = 0;
static struct file_operations pc104_fops = {
owner: THIS_MODULE,
read: pc104_read,
write: pc104_write,
open: pc104_open,
release: pc104_close,
};
static int at91_pc104_init(void)
{
int i = 0;
       char name[2];
/*确定模块以前未初始化*/
if(pc104_initialized == 1)
return 0;
/*分配并初始化所有数据结构为缺省状态*/
#ifdef CONFIG_DEVFS_FS
     if(devfs_register_chrdev(PC104_MAJOR, "pc104", &pc104_fops)){
#else
     if(register_chrdev(PC104_MAJOR, "pc104", &pc104_fops)){
#endif
  printk(KERN_CRIT"pc104:i = %d\n",i);
  return -EIO;
}
printk(KERN_CRIT"pc104 registerred successfully!\n");
/*请求中断*/
pc104_initialized = 1;
#ifdef CONFIG_DEVFS_FS
       devfs_handle = devfs_mk_dir(NULL, "pc104", NULL);
       for(i = 0; i < NR_PC104_DEVICES; i++)
         {
           sprintf(name, "%d", i);
           devfs_pc104 = devfs_register(devfs_handle, name,
           DEVFS_FL_DEFAULT, PC104_MAJOR, i, S_IFCHR|S_IRUSR|S_IWUSR,&pc104_fops, NULL);
         }
#endif
return 0;
}
void pc104_init()
{
       AT91_PC104 = (unsigned long) ioremap(0x80000000,SZ_1K);
AT91_SYS->EBI_CFGR = (AT91C_EBI_DBPUC & 0x00) | (AT91C_EBI_EBSEN & 0x00);
AT91_SYS->EBI_SMC2_CSR[7] = (AT91C_SMC2_NWS & 0x4)  | AT91C_SMC2_WSEN |
                              (AT91C_SMC2_TDF & 0x200)| AT91C_SMC2_DBW_8;
}
static int pc104_open(struct inode *inode, struct file *filp)
{
if(pc104_flag == 1)/*检查驱动是否忙*/
{
return -1;
}
/*可以初始化一些内部数据结构*/
pc104_init();
MOD_INC_USE_COUNT;
pc104_flag = 1;
return 0;
}
static int pc104_close(struct inode *inode, struct file *filp)
{
if(pc104_flag == 0)
{
return 0;
}
/*可以删除一些内部数据结构*/
MOD_DEC_USE_COUNT;
     pc104_reading = 0;
      pc104_writing = 0;
pc104_flag = 0;
return 0;
}
static ssize_t pc104_read(struct file *filp, char *buf, size_t count, loff_t *offset)
{
size_t i;
ssize_t ret=0;
/*检查是否有线程在读数据,返回 error*/
if(pc104_reading)
return -1;
pc104_reading=1;//DEMO_RD_LOCK;
if(!count)
return ret;
if(*offset>=PC104_SIZE)
return -EINVAL;
if((*offset+count)>C104_SIZE)
count=PC104_SIZE-*offset;
for(i = 0; i < count; i++)
{
            buf = readb(AT91_PC104+*offset+i);
}
       buf = '\0';
*offset +=count;
ret=count;
pc104_reading=0;//DEMO_RD_UNLOCK;
/*通常返回成功读到的数据*/
return ret;
}
static ssize_t pc104_write(struct file *filp, const char *buf, size_t count, loff_t *offset)
{
  ssize_t ret = 0;
  printk("pc104 devices writing!\n");
  return ret;
}
static void at91_pc104_cleanup(void)
{
/*确保要清掉的模块是否已初始化*/
if(pc104_initialized == 1)
{
unregister_chrdev(PC104_MAJOR,"pc104");
pc104_initialized = 0;
printk(KERN_CRIT"pc104 device is cleanup\n");
}
}
module_init(at91_pc104_init);
module_exit(at91_pc104_cleanup);
//////////代码结束///////////////////////////////      

作者: alldying   发布时间: 2006-09-18

热门下载

更多