当前位置: 技术问答>linux和unix
ioctl()有关求助
来源: 互联网 发布时间:2016-05-27
本文导语: ioctl()有关求助 以下内容为各部分文件,有问题的地方注释出了 先写出各部分文件 其中设备文件为:/dev/ioctldev c 240 0 顺便说一下,本人是自学,这里是离我最近的老师了。谢谢! ===============ioctl_test.h================...
ioctl()有关求助
以下内容为各部分文件,有问题的地方注释出了
先写出各部分文件
其中设备文件为:/dev/ioctldev c 240 0
顺便说一下,本人是自学,这里是离我最近的老师了。谢谢!
===============ioctl_test.h==============================
#ifndef _IOCTLTEST_H_
#define _IOCTLTEST_H_
#define IOCTLTEST_MAGIC 't'
typedef struct
{
unsigned long size;
unsigned char buff[128];
} __attribute__((packed)) ioctl_test_info;
#define IOCTLTEST_LEDOFF _IO( IOCTLTEST_MAGIC, 0)
#define IOCTLTEST_LEDON _IO( IOCTLTEST_MAGIC, 1)
#define IOCTLTEST_GETSTATE _IO( IOCTLTEST_MAGIC, 2)
#define IOCTLTEST_READ _IOR(IOCTLTEST_MAGIC, 3, ioctl_test_info)
#define IOCTLTEST_WRITE _IOW(IOCTLTEST_MAGIC, 4, ioctl_test_info)
#define IOCTLTEST_WRITE_READ _IOWR(IOCTLTEST_MAGIC, 5, ioctl_test_info)
#define IOCTLTEST_MAXNR 6
#endif // IOCTLTEST_H_
=====================ioctl_dev.c==========================
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ioctl_test.h"
#define IOCTLTEST_DEV_NAME "ioctldev"
#define IOCTLTEST_DEV_MAJOR 240
#define IOCTLTEST_WRITE_ADDR 0x0378
#define IOCTLTEST_READ_ADDR 0x0379
int ioctltest_open (struct inode *inode, struct file *filp)
{
return 0;
}
int ioctltest_release (struct inode *inode, struct file *filp)
{
return 0;
}
int ioctltest_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
ioctl_test_info ctrl_info;
int err, size;
int loop;
if(_IOC_TYPE(cmd) != IOCTLTEST_MAGIC) return -EINVAL;
if(_IOC_NR(cmd) >= IOCTLTEST_MAXNR) return -EINVAL;
size = _IOC_SIZE(cmd);
if(size)
{
err = 0;
if(_IOC_DIR(cmd) & _IOC_READ ) err = access_ok(VERIFY_WRITE,(void *)arg,size);
else if(_IOC_DIR(cmd) & _IOC_WRITE) err = access_ok(VERIFY_READ,(void *)arg,size);
if(err) return err;
}
switch(cmd)
{
case IOCTLTEST_LEDOFF : outb(0x00, IOCTLTEST_WRITE_ADDR);
break;
case IOCTLTEST_LEDON : outb(0xFF, IOCTLTEST_WRITE_ADDR);
break;
case IOCTLTEST_GETSTATE : return inb(IOCTLTEST_READ_ADDR);
case IOCTLTEST_READ : //此条代码不响应应用程序(后面的还没能测试)
ctrl_info.buff[0] = inb(IOCTLTEST_READ_ADDR);
ctrl_info.size = 1;
copy_to_user ((void *) arg, (const void *)&ctrl_info, (unsigned long) size);
break;
case IOCTLTEST_WRITE :
copy_from_user ((void *)&ctrl_info, (const void *) arg, size);
for(loop = 0; loop = 2)
{
cnt = 0;
state = ~state;
}
usleep(100000);
}
ioctl(dev, IOCTLTEST_LEDOFF);
close(dev);
}
return 0;
}
以下内容为各部分文件,有问题的地方注释出了
先写出各部分文件
其中设备文件为:/dev/ioctldev c 240 0
顺便说一下,本人是自学,这里是离我最近的老师了。谢谢!
===============ioctl_test.h==============================
#ifndef _IOCTLTEST_H_
#define _IOCTLTEST_H_
#define IOCTLTEST_MAGIC 't'
typedef struct
{
unsigned long size;
unsigned char buff[128];
} __attribute__((packed)) ioctl_test_info;
#define IOCTLTEST_LEDOFF _IO( IOCTLTEST_MAGIC, 0)
#define IOCTLTEST_LEDON _IO( IOCTLTEST_MAGIC, 1)
#define IOCTLTEST_GETSTATE _IO( IOCTLTEST_MAGIC, 2)
#define IOCTLTEST_READ _IOR(IOCTLTEST_MAGIC, 3, ioctl_test_info)
#define IOCTLTEST_WRITE _IOW(IOCTLTEST_MAGIC, 4, ioctl_test_info)
#define IOCTLTEST_WRITE_READ _IOWR(IOCTLTEST_MAGIC, 5, ioctl_test_info)
#define IOCTLTEST_MAXNR 6
#endif // IOCTLTEST_H_
=====================ioctl_dev.c==========================
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ioctl_test.h"
#define IOCTLTEST_DEV_NAME "ioctldev"
#define IOCTLTEST_DEV_MAJOR 240
#define IOCTLTEST_WRITE_ADDR 0x0378
#define IOCTLTEST_READ_ADDR 0x0379
int ioctltest_open (struct inode *inode, struct file *filp)
{
return 0;
}
int ioctltest_release (struct inode *inode, struct file *filp)
{
return 0;
}
int ioctltest_ioctl (struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
ioctl_test_info ctrl_info;
int err, size;
int loop;
if(_IOC_TYPE(cmd) != IOCTLTEST_MAGIC) return -EINVAL;
if(_IOC_NR(cmd) >= IOCTLTEST_MAXNR) return -EINVAL;
size = _IOC_SIZE(cmd);
if(size)
{
err = 0;
if(_IOC_DIR(cmd) & _IOC_READ ) err = access_ok(VERIFY_WRITE,(void *)arg,size);
else if(_IOC_DIR(cmd) & _IOC_WRITE) err = access_ok(VERIFY_READ,(void *)arg,size);
if(err) return err;
}
switch(cmd)
{
case IOCTLTEST_LEDOFF : outb(0x00, IOCTLTEST_WRITE_ADDR);
break;
case IOCTLTEST_LEDON : outb(0xFF, IOCTLTEST_WRITE_ADDR);
break;
case IOCTLTEST_GETSTATE : return inb(IOCTLTEST_READ_ADDR);
case IOCTLTEST_READ : //此条代码不响应应用程序(后面的还没能测试)
ctrl_info.buff[0] = inb(IOCTLTEST_READ_ADDR);
ctrl_info.size = 1;
copy_to_user ((void *) arg, (const void *)&ctrl_info, (unsigned long) size);
break;
case IOCTLTEST_WRITE :
copy_from_user ((void *)&ctrl_info, (const void *) arg, size);
for(loop = 0; loop = 2)
{
cnt = 0;
state = ~state;
}
usleep(100000);
}
ioctl(dev, IOCTLTEST_LEDOFF);
close(dev);
}
return 0;
}
|
case IOCTLTEST_READ : //此条代码不响应应用程序(后面的还没能测试)
ctrl_info.buff[0] = inb(IOCTLTEST_READ_ADDR);
ctrl_info.size = 1;
copy_to_user ((void *) arg, (const void *)&ctrl_info, (unsigned long) size);
break;
不知道楼主指的不响应是什么意思?ioctl出错、应用层死机、核心挂掉、死等在读IOCTLTEST_READ_ADDR?
你也可以自己用软件天一些数据到ctrl_info.buff中,如
memcpy(ctrl_info.buff, "mytest", 6);
ctrl_info.size = 6;
copy_to_user ((void *) arg, (const void *)&ctrl_info, (unsigned long) size);
试试是否读硬件出错。
ctrl_info.buff[0] = inb(IOCTLTEST_READ_ADDR);
ctrl_info.size = 1;
copy_to_user ((void *) arg, (const void *)&ctrl_info, (unsigned long) size);
break;
不知道楼主指的不响应是什么意思?ioctl出错、应用层死机、核心挂掉、死等在读IOCTLTEST_READ_ADDR?
你也可以自己用软件天一些数据到ctrl_info.buff中,如
memcpy(ctrl_info.buff, "mytest", 6);
ctrl_info.size = 6;
copy_to_user ((void *) arg, (const void *)&ctrl_info, (unsigned long) size);
试试是否读硬件出错。