ioctl提供了一个对设备文件进行控制操作的途径
1 操作码的定义:
#define DELAY_CMD_0 _IOW('A',0,unsigned int)
#define DELAY_CMD_1 _IOW('A',1,unsigned int)
#define DELAY_CMD_2 _IOW('A',2,unsigned int)
#define DELAY_CMD_9 _IOW('A',9,unsigned int)
通过宏_IOW来定义,第一个参数是魔数,理想状态下每一个应用唯一,第二个参数是命令码,比如对应于开灯,关灯,第三个参数是参数,比如第二个参数是速度的时候,第三个参数指定速度值是多少。
应用层的代码:
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <poll.h>
#define DELAY_CMD_OPEN _IOW('A',0,unsigned int)
#define DELAY_CMD_CLOSE _IOW('A',1,unsigned int)
#define DELAY_CMD_2 _IOW('A',2,unsigned int)
#define DELAY_CMD_9 _IOW('A',9,unsigned int)
void* ioctl01(void *ptr) {
int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
if (fd < 0) {
perror("open");
printf("error open\n");
return NULL;
}
printf("ioctl 2\n");
ioctl(fd, DELAY_CMD_2, 100);
sleep(4);
printf("ioctl 9\n");
ioctl(fd, DELAY_CMD_9, 100);
close(fd);
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, ioctl01, NULL);
pthread_join(thread, NULL);
printf("main exit\n");
return EXIT_SUCCESS;
}
以上代码发送了 2个指令 DELAY_CMD_2
和 DELAY_CMD_9
下面是驱动部分的代码:
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/bug.h> /* For BUG_ON. */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#define DELAY_CMD_0 _IOW('A',0,unsigned int)
#define DELAY_CMD_1 _IOW('A',1,unsigned int)
#define DELAY_CMD_2 _IOW('A',2,unsigned int)
#define DELAY_CMD_9 _IOW('A',9,unsigned int)
#define MY_MAX_MINORS 1
#define DEVICE_NAME "device name 500"
DEFINE_KFIFO(myfifo, char, 1024);
struct my_device_data {
struct cdev cdev;
};
DECLARE_WAIT_QUEUE_HEAD(wq);
static struct my_device_data devs[MY_MAX_MINORS];
static int my_open(struct inode *inode, struct file *file) {
pr_info("my_open \n");
return 0;
}
static ssize_t my_write(struct file *filp, const char __user *user_buffer,
size_t size, loff_t *offset) {
int ret;
unsigned int len = 0;
pr_info("write");
ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
if (ret != 0) {
pr_err("kfifo_from_user error");
return 0;
}
if (len <= 0)
return 0;
*offset += len;
wake_up(&wq);
return len;
}
static ssize_t my_read(struct file *filp, char __user *user_buffer,
size_t count, loff_t *offset) {
int ret;
unsigned int len = 0;
pr_info("read");
ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
if (len <= 0)
return 0;
*offset += len;
return len;
}
unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
int mask = 0;
pr_info("my_poll \n");
poll_wait(flip, &wq, table);
if (kfifo_is_empty(&myfifo)) {
} else {
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
int my_close(struct inode *inode, struct file *flip) {
pr_info("my_close \n");
return 0;
}
static long my_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
pr_info("cmd %d\n", cmd);
switch (_IOC_NR(cmd)) {
case 2: {
pr_info("start sleep 2 seconds\n");
ssleep(2);
break;
}
case 9: {
pr_info("start sleep 9 seconds\n");
ssleep(9);
break;
}
default: {
pr_info("default: \n");
}
}
return 0;
}
static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
my_open, .read = my_read, .write = my_write, .poll = my_poll, .release =
my_close, .unlocked_ioctl = my_ioctl,
};
static dev_t mydev;
static __init int my_init(void) {
int i;
int err;
pr_info("a3 init_module\n");err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
if (err != 0) {
return err;
}
for (i = 0; i < MY_MAX_MINORS; i++) {
/* initialize devs[i] fields */
cdev_init(&devs[i].cdev, &my_fops);
cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i)
, 1);
}
return 0;
}
static void __exit my_exit(void) {
int i;
pr_info("a3 cleanup_module\n");for (i = 0; i < MY_MAX_MINORS; i++) {
/* release devs[i] fields */
cdev_del(&devs[i].cdev);
}
unregister_chrdev_region(mydev, MY_MAX_MINORS);
}
module_init(my_init);
module_exit(my_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
主要是 my_ioctl函数,通过 _IOC_NR 可以拿到命令码 ,在这个例子中是 2或者 9 ,然后方便进行操作。
欢迎评论。