1.用户空间调用ioctl函数传递cmd参数代码如下
printf("In the app program the LED_ON is:%d\n",LED_ON);
printf("In the app program the LED_OFF is:%d\n",LED_OFF);
sleep(1);
for (i = 0;i < 3;i++) {
ioctl(fd,LED_ON);
sleep(1);
ioctl(fd,LED_OFF);
sleep(1);
}
2.内核空间ioctl解析函数如下
static int char_cdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
printk(KERN_INFO "cmd is:%d\n",cmd);
printk(KERN_INFO "LED_ON is:%d\n",LED_ON);
printk(KERN_INFO "LED_OFF is:%d\n",LED_OFF);
if (_IOC_TYPE(cmd)!=LED_IOC_MAGIC) {
return -ENOTTY;
}
if (_IOC_NR(cmd) > LED_IOCTL_MAXNR) {
return -ENOTTY;
}
switch(cmd) {
case LED_ON:
gpio_set_value(GPIO_LED_PIN_NUM,0);
printk(KERN_INFO DEVICE_NAME "LED_ON\n");
break;
case LED_OFF:
gpio_set_value(GPIO_LED_PIN_NUM,1);
printk(KERN_INFO DEVICE_NAME "LED_OFF\n");
break;
default:
gpio_set_value(GPIO_LED_PIN_NUM,1);
break;
}
printk(KERN_INFO DEVICE_NAME " ioctl method!\n");
return 0;
}
3.运行结果如下,cmd传递到内核变成了0,但是应用程序中传递的命令是11776和11777,在内核空间打印命令也是11776和11777,唯有cmd的参数不对
[root@EPC-M6G2C driver]# ./driver_test_app
LED_ERR opened!
In the app program the LED_ON is:11776
In the app program the LED_OFF is:11777
cmd is:0
LED_ON is:11776
LED_OFF is:11777
cmd is:0
LED_ON is:11776
LED_OFF is:11777
cmd is:0
LED_ON is:11776
LED_OFF is:11777
cmd is:0
LED_ON is:11776
LED_OFF is:11777
cmd is:0
LED_ON is:11776
LED_OFF is:11777
cmd is:0
LED_ON is:11776
LED_OFF is:11777
Control led end!LED_ERR closed!