日期:2014-05-16 浏览次数:20820 次
本文是我在andoid实验的ioctl的功能,如双向传递参数。贴出来希望对学习ioctl的人很有帮助。
linux的ioctl功能是很强大的,android显示模块还有camera模块都离不开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 <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <linux/regulator/consumer.h>
#include <linux/platform_device.h>
#include <linux/mutex.h>
#include <mach/gpio.h>
#include<linux/ioctl_test.h>
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