1 设备树:
my_platform_device003 {
status = "okay";
compatible ="my_platform_device_003";
interrupt-parent = <&gpio1>;
interrupts = <7 2>;
};
使用 GPIO1 A7 引脚。
2 驱动部分:
#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>
/**
*
*
my_platform_device003 {
status = "okay";
compatible ="my_platform_device_003";
interrupt-parent = <&gpio1>;
interrupts = <7 2>;
};
*/
#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("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;
void key_tasklet_handler(unsigned long data) {
pr_info("tasklet_handler start \n");
mdelay(5);
pr_info("tasklet_handler end\n");
}
DECLARE_TASKLET(key_tasklet, key_tasklet_handler, (unsigned long ) &mydev);
static irqreturn_t key_handler(int irq, void *dev_id) {
pr_info("key_handler \n");
kill_fasync(&fasync_struct, SIGIO, POLL_OUT);
tasklet_schedule(&key_tasklet);
return IRQ_HANDLED;
}
unsigned int num;
int myprobe(struct platform_device *pdev) {
int i;
int err;
int ret;
struct resource *res2;
pr_info("myplatformdriver myprobe \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);
}
res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
pr_err("irq : res->start :%llu\n", res2->start);
num = res2->start ;
pr_info("irq_num:%d\n", num);
ret = request_irq(num, key_handler, res2->flags, "my_key_driver",
NULL);
if (ret < 0) {
pr_err("request_irq failed \n");
return ret;
}
return 0;
}
int myremove(struct platform_device *pdev) {
int i;
free_irq(num, NULL);
pr_info("myplatformdriver myremove \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);
return 0;
}
int mysuspend(struct device *pdev, pm_message_t state) {
pr_info("myplatformdriver mysuspend \n");
return 0;
}
int myresume(struct device *pdev) {
pr_info("myplatformdriver myresume \n");
return 0;
}
struct of_device_id my_of_match_table =
{ .compatible = "my_platform_device_003", };
struct platform_driver my_platform_driver = { .driver = { .of_match_table =
&my_of_match_table, .name = "my-platform-driver", .owner = THIS_MODULE,
.suspend = mysuspend, .resume = myresume, }, .probe = myprobe, .remove =
myremove, };
module_platform_driver(my_platform_driver);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");
测试方法和之前的文章一样。