2 mmoneyhome mmoneyhome 于 2013.11.13 21:17 提问

linux 用户态调用驱动中的函数返回-1

/*****************驱动代码***************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include "pciedrv.h"

#define GLOBAL_MAJOR 133

PCIE_DRV_CONTEXT pcieContext;
S32 x4orx1;

#define WRITE_DMA_AND_SEND 0x100
#define READ_FROM_BUFFER 0x200

#define BUSY 0x10
#define IDLE 0x11
#define EXIT 0x12
#define GLOBALMEM_MAJOR 133
#define TRANSMIT _IOC (_IOC_READ,IOCTL_MAGIC, 3, 4 )
#define RECEIVE _IOC (_IOC_WRITE,IOCTL_MAGIC,4, 4 )
ssize_t DriverRead (struct file* stFileName, const char __user* input, size_t size, loff_t* offset);
ssize_t DriverWrite (struct file* stFileName, const char __user* input, size_t size, loff_t* offset);

struct file_operations globalmem_fops =
{
.owner = THIS_MODULE,
.read = DriverRead,
.write = DriverWrite,
};
static globalmem_major = GLOBALMEM_MAJOR;
struct globalmem_dev
{
struct cdev cdev;
unsigned char mem[0x10000];
};
struct globalmem_dev dev;

ssize_t DriverRead (struct file* stFileName, const char user* input, size_t size, loff_t* offset)
{
printk("%s\n",
FUNCTION__);
return 0;
}
ssize_t DriverWrite(struct file* stFileName, const char user* input, size_t size, loff_t* offset)
{
printk("%s\n",
FUNCTION__);
return 0;
}

static int __init_func(void)
{
int retValue = 0;
dev_t devno = MKDEV(globalmem_major, 0);
if(globalmem_major)
{
retValue = register_chrdev_region(devno, 1, "pciedrv");
printk(KERN_ALERT "register charactor device region\n");
}
else
{
retValue = alloc_chrdev_region(devno, 0, 1, "pciedrv");
globalmem_major = MAJOR(devno);
printk(KERN_ALERT "alloc charactor device region\n");
}
if(retValue <0)
{
printk("Failed to register or alloc major\n");
}
devno = MKDEV(globalmem_major, 0);
cdev_init(&dev.cdev, &globalmem_fops);
dev.cdev.owner = THIS_MODULE;
dev.cdev.ops = &globalmem_fops;
retValue = cdev_add(&dev.cdev,devno,1);
if(retValue)
printk("cdev_add return %d\n",retValue);
retValue = PCIeDrvInit(PCIE0_BASEADDR, x4orx1);
if(retValue == 0)
printk(KERN_ALERT "PCIeDrvInit Success!\n ");
else
printk(KERN_ALERT "PCIeDrvInit Failed!\n");
control_buffer.status = 0;
control_buffer.command = 0;

printk(":D\n");
return retValue;
}
static void exit_func(void)
{
printk(KERN_ALERT "Here is entering the exit-point of the PCIE driver!!! \n");
cdev_del(&dev.cdev);
unregister_chrdev_region(MKDEV(globalmem_major, 0), 1);
PCIeDrvDeinit();
}
module_init(
init_func);
module_exit(__exit_func);
MODULE_AUTHOR("ZCTT");
MODULE_DESCRIPTION("Driver of PCIEx4");
MODULE_ALIAS("ICC Driver");
MODULE_LICENSE("GPL");
EXPORT_SYMBOL(__init_func);
EXPORT_SYMBOL(__exit_func);
/**************************调用函数代码***********************************/
#include
#include
#include
#include
#include
#include
#include
#define DEVICE_FILENAME "/bin/pciedrv.ko"
#define DEVICE_TYPE (0xA4<<24)
#define DEVICE_SERIAL (0x33<<16)
#define DEVICE_DERICTION IOC_NON
#define READ_PHY_ADDRESS 0x100
#define WRITE_PHY_ADDRESS 0x101
#define GLOBAL_MAJOR 133
#define IOCTL_MAGIC 't'

typedef struct {
unsigned long size;
unsigned int offset;
unsigned char buff [128];
}__attribute__((packed)) ioctl_read_info_t;
typedef struct {
unsigned long size;
unsigned int offset;
unsigned char buff [128];
}__attribute__((packed)) ioctl_write_info_t;
ioctl_read_info_t ioctl_read_info ;
ioctl_write_info_t ioctl_write_info ;
int main()
{
char* str = "Hello Guys This is the Message!\n";
int retValue ;
int dev = 0;
memset (ioctl_write_info.buff, 0, 128);
ioctl_write_info.size = strlen(str)+1;
ioctl_write_info.offset = 0;
memcpy (ioctl_write_info.buff, str, ioctl_write_info.size);
/**********************************************************/
dev = open(DEVICE_FILENAME, O_RDWR);
printf("dev = %d\n",dev);
printf(":--\n");
while(1)
{
int retValue = read(dev, str, strlen(str));
printf ("retValue = %d\n",retValue);
sleep(2);
write(dev, str, strlen(str));
printf ("retValue = %d\n",retValue);
}
printf("Finished system calling! \n");

/运行结果/
驱动部分的dmesg,事实上只打印出了init部分的内容
register charactor device region

:D
root@OpenWrt:~# a.out
dev = 3
:--
retValue = -1
retValue = -1
retValue = -1
retValue = -1
retValue = -1
retValue = -1
retValue = -1
^C
运行完a.out之后 demesg发现没有任何新增内容。

1个回答

fafactx
fafactx   2016.01.18 12:36

你把read write换个函数名试试吧。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!