我试图通过ioctl将struct pwmData类型变量传递给我的驱动程序。但在我的应用程序中,我通过perror获得了无效的参数错误。 有人可以告诉我为什么我得到这个? 我的.h文件:
typedef struct
{
unsigned int pwmChannel;
unsigned int pwmDutyCycle;
unsigned int pwmPeriod;
}pwmData;
#define PWM_MAGIC '0xF2' //Is not used in kernel, hence used here
#define ENABLE_PWM _IOW(PWM_MAGIC, 1,pwmData*)
#define DISABLE_PWM _IOW(PWM_MAGIC, 2,pwmData*)
我的应用(其中fd是我的文件描述符):
pwmData param;
param.pwmPeriod = 1000;
param.pwmDutyCycle = 500;
param.pwmChannel = 1;
ret = ioctl(fd, ENABLE_PWM, ¶m); // Check casting
if(ret<0){
printf("ENABLE IOCTL failed %d.\n", ret);
perror("Error:");
exit(-1);
}
我的司机的ioctl部分:
static long pwm_device_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
printk(KERN_DEBUG "Entered IOCTL\n");
int retval;
static pwmData* data;
data = (void *)kmalloc(sizeof(pwmData)+1,GFP_KERNEL);
switch (cmd)
{
case ENABLE_PWM:
if(copy_from_user((void*)data, (void*)arg, sizeof(pwmData)))
{
return -EFAULT;
}
printk(KERN_DEBUG "%d %d %d\n",data->pwmChannel,data->pwmDutyCycle,data->pwmPeriod);
retval = pwm_config(&cpwm1->chip.pwms[(data->pwmChannel) - 1], data->pwmDutyCycle, data->pwmPeriod);
if(retval == 0)
{
retval = pwm_enable(&cpwm1->chip.pwms[(data->pwmChannel) - 1]);
return retval;
}
else
{
return retval;
}
break;
类似于PWM_DISABLE。 我刚刚启动了linux驱动程序开发。所以任何建议都会被提升。