±¾ÎÄÊÇÎÒÔÚandoidʵÑéµÄioctlµÄ¹¦ÄÜ£¬ÈçË«Ïò´«µÝ²ÎÊý¡£Ìù³öÀ´Ï£Íû¶ÔѧϰioctlµÄÈ˺ÜÓаïÖú¡£
Õâ¸öʵÑé¶Ô³õѧÕߺÜÓаïÖúµÄ¡£
boardÖÐÌí¼Óplatform_devce
static struct ioctl_test_platform_data ioctl_pdata = {
static struct ioctl_test_platform_data ioctl_pdata = {
.gpio=17,
};
static struct platform_device msm_device_ioctl = {
.name = "gpio_test",
.id = -1,
.dev = {
.platform_data = &ioctl_pdata,
},
};
Í·Îļþ£º
ioctl_test.h
#ifndef IOCTL_TEST_H
#define IOCTL_TEST_H
/* platform data from board file */
struct ioctl_test_platform_data{
u16 gpio;
};
struct gpio_priv_t {
int gpio;
int state0;
int state1;
int state2;
int state3;
};
#define GPIO_TEST_IOC_MAGIC 0x92
#define GPIO_TEST_SET_HIGH _IO(GPIO_TEST_IOC_MAGIC, 1)
#define GPIO_TEST_SET_LOW _IO(GPIO_TEST_IOC_MAGIC, 2)
#define GPIO_TEST_WRITE_STRUCT _IOW(GPIO_TEST_IOC_MAGIC, 3,struct gpio_priv_t *)
#define GPIO_TEST_WRITE_INT _IOW(GPIO_TEST_IOC_MAGIC, 4,unsigned int *)
#define GPIO_TEST_READ_STRUCT _IOR(GPIO_TEST_IOC_MAGIC, 5,struct gpio_priv_t *)
#define GPIO_TEST_READ_INT _IOR(GPIO_TEST_IOC_MAGIC, 6,unsigned int *)
#endif
driver:
Éú³ÉÉ豸½Úµã£º/dev/gpio_test
ioctl_test.c
#define pr_fmt(fmt) "%s: " fmt, __func__
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static struct gpio_priv_t *gpio_priv;
static int gpio_test_open(struct inode *inode, struct file *filp)
{
if (gpio_priv == NULL)
return -ENODEV;
filp->private_data = gpio_priv;
return 0;
}
static int gpio_test_release(struct inode *inode, struct file *filp)
{
filp->private_data = NULL;
return 0;
}
static long
gpio_test_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int err = 0;
void __user *argp = (void __user *)arg;
int value0;
int value1 ;
/* Verify user arguments. */
if (_IOC_TYPE(cmd) != GPIO_TEST_IOC_MAGIC)
return -ENOTTY;
switch (cmd) {
case GPIO_TEST_SET_HIGH:
printk("GPIO_TEST_SET_HIGH\n");
if (arg == 0) {
pr_err("user space arg not supplied\n");
err = -EFAULT;
break;
}
gpio_set_value(17,1);
break;
case GPIO_TEST_SET_LOW:
printk("GPIO_TEST_SET_LOW\n");
if (arg == 0) {
pr_err("user space arg not supplied\n");
err = -EFAULT;
break;
}
gpio_set_value(17,0);
break;
case GPIO_TEST_WRITE_STRUCT:
printk("GPIO_TEST_WRITE\n");
if (copy_from_user(gpio_priv, argp, sizeof(struct gpio_priv_t)))
return -EFAULT;
printk("gpio_priv->state0=%d\n",gpio_priv->state0);
printk("gpio_priv->state1=%d\n",gpio_priv->state1);
printk("gpio_priv->state2=%d\n",gpio_priv->state2);
break;
case GPIO_TEST_READ_STRUCT:
printk("GPIO_TEST_READ\n");
gpio_priv->state0=1;
gpio_priv->state1=2;
gpio_priv->state2=3;
if (copy_to_user(argp, gpio_priv, sizeof(struct gpio_priv_t)))
return -EFAULT;
break;
case GPIO_TEST_WRITE_INT:
printk("GPIO_TEST_WRITE_INT\n");
if(copy_from_user(&value0,argp,sizeof(int)))
return -EFAULT;
printk("value0 = %d\n",value0);
break;
case GPIO_TEST_READ_INT:
printk("GPIO_TEST_READ_INT\n");
value1=101;
if (copy_to_user(argp, &value1, sizeof(int)))
return -EFAULT;
break;