driver: IOCTL command behavior
Hi,
I wrote a driver and used IOCTL command. When I test my driver with a test application with root access the IOCTL cmd work fine.
I notice when I declared my IOCTL command that I used the _IOR for all of them. Even the IOCTL command is a read (on register) or write. So I try to correct my mistake and wants to use _IOR for read command and _IOW for write command.
For the write command, I got a error -25 (not a typewriter) so I don't understand why.
I also try to change the _IOW to _IOWR but same mistake.
Someone can explain what is the problem, please ?
Marcel.
Quoted 12 lines · Marcel Soulot
Hi,
I wrote a driver and used IOCTL command. When I test my driver with a test application with root access the IOCTL cmd work fine.
I notice when I declared my IOCTL command that I used the_IORfor all of them. Even the IOCTL command is a read (on register) or write. So I try to correct my mistake and wants to use_IORfor read command and_IOWfor write command.
For the write command, I got a error -25 (not a typewriter) so I don't understand why.
I also try to change the_IOWto_IOWRbut same mistake.
Quoted 1 line
Someone can explain what is the problem, please ?
A piece of code is worth a thousand words...
Signature · 1 line
Gilles.
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 32 lines · Marcel Soulot
Hi,
sorry, i don't realise that my description is a bit fuzzy.Here I define two IOCTL command:
#define RT_GPIO_RTIOC_GET_DIRECTION\ _IOR(RTIOC_TYPE_GPIO, 0x01, int)#define RT_GPIO_RTIOC_SET_DIRECTION\ _IOR(RTIOC_TYPE_GPIO, 0x02, int)the commands (both) works fine. For the command RT_GPIO_RTIOC_SET_DIRECTION,I try to switch
_IOR()to_IOW()according the driver "xeno_16550A" (in the
file/usr/xenomai/include/rtdm/rtserial.h)#define RTSER_RTIOC_GET_CONFIG \ _IOR(RTIOC_TYPE_SERIAL, 0x00, struct rtser_config)#define RTSER_RTIOC_SET_CONFIG \ _IOW(RTIOC_TYPE_SERIAL, 0x01, struct rtser_config)But when I define :
#define RT_GPIO_RTIOC_SET_DIRECTION\ _IOW(RTIOC_TYPE_GPIO, 0x02, int)or
#define RT_GPIO_RTIOC_SET_DIRECTION\ _IOWR(RTIOC_TYPE_GPIO, 0x02, int)
Quoted 6 lines
the call of "
rt_dev_ioctl(my_fd,RT_GPIO_RTIOC_SET_DIRECTION,0xFF)" returns me the error "-ENOTTY".
Which means that the commandRT_GPIO_RTIOC_SET_DIRECTIONis not recognized and I drop in default error.Hope my explanation is better,
No, please send us enough code for us to reproduce the issue.
Please do not drop the mailing list from the discussion.
Signature · 1 line
Gilles.
Hi,
here is the my ioctl function and the header file below.
/*****************************************************************************************/ int rt_gpio_ioctl(struct rtdm_dev_context *context,
rtdm_user_info_t *user_info,
unsigned int request, void *arg)
{
struct rt_gpio_context *ctx_gpio;
int err = 0;
unsigned long base;
int mode;
ctx_gpio = (struct rt_gpio_context *)context->dev_private;
base = ctx_gpio->base_addr;
mode = rt_gpio_io_mode_from_ctx_gpio(ctx_gpio);
printk(KERN_INFO "IOCTL : request %x\n", request);
switch (request)
{
case RT_GPIO_RTIOC_GET_DIRECTION:
{
/*
** 1 = input and 0 = output, by default all are inputs.
*/
int gpio_dir;
gpio_dir = rt_gpio_reg_in(mode, base, OFF_MPIOSEL);
printk(KERN_INFO "GET_DIRECTION %x\n", gpio_dir);
if (user_info)
{
err = rtdm_safe_copy_to_user(user_info, arg,
&gpio_dir, sizeof(int));
}
else
{
*(int *)arg = gpio_dir;
}
break;
}
case RT_GPIO_RTIOC_SET_DIRECTION:
{
/*
** 1 = input and 0 = output, by default all are inputs.
*/
unsigned int gpio_dir = (unsigned int)arg;
rt_gpio_reg_out(mode, base, OFF_MPIOSEL, gpio_dir);
printk(KERN_INFO "SET_DIRECTION %x\n", gpio_dir);
break;
}
default:
err = -ENOTTY;
}
return err;
}/**************************************************************************************************/ header file /**************************************************************************************************/
#ifndef _RTGPIO_H
#define _RTGPIO_H
#include <rtdm/rtdm.h>
#define RT_GPIO_PROFILE_VER 2
#define RTIOC_TYPE_GPIO RTDM_CLASS_GPIO
#define RTDM_SUBCLASS_GPIO 0
#define RT_GPIO_RTIOC_GET_DIRECTION\
_IOR(RTIOC_TYPE_GPIO, 0x00, int)#define RT_GPIO_RTIOC_SET_DIRECTION\
_IOR(RTIOC_TYPE_GPIO, 0x01, int)#endif /* _RTGPIO_H */
/**************************************************************************************************/
So for the "RT_GPIO_RTIOC_SET_DIRECTION" when I switch "_IOR(RTIOC_TYPE_GPIO, 0x01, int)" to "_IOW(RTIOC_TYPE_GPIO, 0x01, int)" or "_IOWR(RTIOC_TYPE_GPIO, 0x01, int)", the switch pass in "default".
Someone as an explanation ?
Thanks,
Marcel.
Quoted 87 lines · Marcel Soulot
Hi,
here is the my ioctl function and the header file below./*****************************************************************************************/ int rt_gpio_ioctl(struct
rtdm_dev_context*context,
rtdm_user_info_t*user_info,
unsigned int request, void *arg){ struct rt_gpio_context *ctx_gpio; int err = 0; unsigned long base; int mode; ctx_gpio = (struct rt_gpio_context *)context->dev_private; base = ctx_gpio->base_addr; mode = rt_gpio_io_mode_from_ctx_gpio(ctx_gpio); printk(KERN_INFO "IOCTL : request %x\n", request); switch (request) { case RT_GPIO_RTIOC_GET_DIRECTION: { /* ** 1 = input and 0 = output, by default all are inputs. */ int gpio_dir; gpio_dir = rt_gpio_reg_in(mode, base, OFF_MPIOSEL); printk(KERN_INFO "GET_DIRECTION %x\n", gpio_dir); if (user_info) { err = rtdm_safe_copy_to_user(user_info, arg, &gpio_dir, sizeof(int)); } else { *(int *)arg = gpio_dir; } break; } case RT_GPIO_RTIOC_SET_DIRECTION: { /* ** 1 = input and 0 = output, by default all are inputs. */ unsigned int gpio_dir = (unsigned int)arg; rt_gpio_reg_out(mode, base, OFF_MPIOSEL, gpio_dir); printk(KERN_INFO "SET_DIRECTION %x\n", gpio_dir); break; } default: err = -ENOTTY; } return err; }/**************************************************************************************************/ header file /**************************************************************************************************/
#ifndef
_RTGPIO_H
#define_RTGPIO_H#include <
rtdm/rtdm.h>#define
RT_GPIO_PROFILE_VER2#define
RTIOC_TYPE_GPIORTDM_CLASS_GPIO
#defineRTDM_SUBCLASS_GPIO0#define RT_GPIO_RTIOC_GET_DIRECTION\ _IOR(RTIOC_TYPE_GPIO, 0x00, int)#define RT_GPIO_RTIOC_SET_DIRECTION\ _IOR(RTIOC_TYPE_GPIO, 0x01, int)#endif /*
_RTGPIO_H*/
Quoted 7 lines
/**************************************************************************************************/
So for the "
RT_GPIO_RTIOC_SET_DIRECTION" when I switch "_IOR(RTIOC_TYPE_GPIO, 0x01, int)" to "_IOW(RTIOC_TYPE_GPIO, 0x01, int)" or "_IOWR(RTIOC_TYPE_GPIO, 0x01, int)", the switch pass in "default".Someone as an explanation ?
Did you cleanly rebuild both kernel and user space after fiddling with _IO*? The numerical value of RT_GPIO_RTIOC_SET_DIRECTION changes each time.
BTW, _IO* are conventions between driver and user, nothing is checked or enforced elsewhere.
Jan
Signature · 2 lines
Siemens AG, Corporate Technology, CT SE 2 Corporate Competence Center Embedded Linux
Thanks for the tips. I forgot to recompile the user application.
Marcel.