1 在按键驱动中通过异步通知来通知上层应用:
#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 MY_MAX_MINORS 1
#define DEVICE_NAME "device name 500"
#define GPIO_1_BASE 0xFF730000
#define GPIO1_SWPORTA_DR GPIO_1_BASE
#define GPIO1_SWPORTA_DDR 0xFF730004
#define GPIO_INTEN (GPIO_1_BASE + 0x0030)
#define GPIO_PIN 39
#define LOCATION 7
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("a3 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;
}
struct fasync_struct *fasync_struct;
int my_fasync(int fd, struct file *flip, int on) {
return fasync_helper(fd, flip, on, &fasync_struct);
}
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,
.fasync = my_fasync,
};
static dev_t mydev;
irqreturn_t key_handler(int irq, void *dev_id) {
pr_err("key_handler \n");
kill_fasync(&fasync_struct, SIGIO, POLL_OUT);
pr_err("key_handler 2 \n");
return IRQ_HANDLED;
}
unsigned int num;
static __init int my_init(void) {
int i;
int err;
int ret;
unsigned int __iomem *direction = ioremap(GPIO1_SWPORTA_DDR, 4);
unsigned int a = readl(direction);
unsigned int __iomem *irq = ioremap(GPIO_INTEN, 4);
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);
}
// set direction in
writel(readl(direction) & (~(1 << LOCATION)), direction);
//just check
a = readl(direction);
pr_err("direction: %d \n", a);
pr_err("direction 1 or 0 ? : %d \n", (a & (1 << LOCATION)) >> LOCATION);
// enable Interrupt
writel(readl(irq) | (1 << LOCATION), irq);
pr_err("Interrupt 1 or 0 ? : %d \n",
(readl(irq) & (1 << LOCATION)) >> LOCATION);
num = gpio_to_irq(GPIO_PIN);
pr_info("irq_num:%d\n", num);ret = request_irq(num, key_handler, IRQF_TRIGGER_FALLING,
"my_key_driver", NULL);
if (ret < 0) {
pr_err("request_irq failed \n");
return ret;
}
return 0;
}
static void __exit my_exit(void) {
int i;
free_irq(num, NULL);
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");
收到按键中断信息之后通过
kill_fasync(&fasync_struct, SIGIO, POLL_OUT);
给上层应用发信号.
下面是上层程序:
#define _GNU_SOURCE
#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <poll.h>
/**
*
* https://man7.org/linux/man-pages/man2/fcntl.2.html
*
*/
void sigio_handler(int signum, siginfo_t *siginfo, void *act) {
printf("sigio_handler:%d\n", signum);
}
void* run(void *ptr) {
int flag ;
struct sigaction act, oldact;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGIO);
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = sigio_handler;
if (sigaction(SIGIO, &act, &oldact) == -1) {
goto fail;
}
int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
if (fd < 0) {
perror("open");
printf("error open\n");
goto fail;
}
if (fcntl(fd, F_SETOWN, getpid()) == -1) {
goto fail;
}
if (fcntl(fd, F_SETSIG, SIGIO) == -1) {
goto fail;
}
if ((flag = fcntl(fd, F_GETFL)) == -1) {
goto fail;
}
if (fcntl(fd, F_SETFL, flag | FASYNC) == -1) {
goto fail;
}
while(1){
sleep(9999999);
}
return NULL;
fail: printf("fail exit\n");
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, run, NULL);
pthread_join(thread, NULL);
printf("main exit\n");
return EXIT_SUCCESS;
}
注意第一行的
#define _GNU_SOURCE
不加编译不过.
F_SETOWN 的含义:
Set the process ID or process group ID that will receive
SIGIO and SIGURG signals for events on the file descriptor fd.
就是让当前进程可以接受到这个文件描述符fd发出来的信号。
fcntl(fd, F_SETFL, flag | FASYNC)
是启用起步通知