Linux中断子系统---workqueue工作队列

一、工作队列相关API

初始化函数:
#define INIT_WORK(_work, _func)
_work 表示要初始化的工作, _func 是工作对应的处理函数。

也可以使用 DECLARE_WORK 宏一次性完成工作的创建和初始化,宏定义如下:
#define DECLARE_WORK(n, f)
n 表示定义的工作(work_struct), f 表示工作对应的处理函数。

工作调度函数:
bool schedule_work(struct work_struct *work)

其中work_struct 结构体如下:

struct work_struct {
	atomic_long_t data;
	struct list_head entry;
	work_func_t func; //处理函数
#ifdef CONFIG_LOCKDEP
	struct lockdep_map lockdep_map;
#endif
};

/* by 面朝大海0902 */

二、工作队列的使用方法

/* 定义工作(work) */
struct work_struct testwork;

void testwork_func_t(struct work_struct *work);
{
/* work 具体处理内容 */
}

/* 中断处理函数 */
irqreturn_t test_handler(int irq, void *dev_id)
{
......
/* 调度 work */
schedule_work(&testwork);
......
}

/* 驱动入口函数 */
static int __init xxxx_init(void)
{
......
/* 初始化 work */
INIT_WORK(&testwork, testwork_func_t);
/* 注册中断处理函数 */
request_irq(xxx_irq, test_handler, 0, "xxx", &xxx_dev);
......
}

此处参考《【正点原子】I.MX6U嵌入式Linux驱动开发指南》

三、驱动使用实例

#include <linux/module.h>
#include <linux/poll.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/slab.h>
#include <linux/fcntl.h>
#include <linux/timer.h>

struct gpio_key{
	int gpio;
	struct gpio_desc *gpiod;
	int flag;
	int irq;
};

static struct gpio_key *ptr_gpio_key;

static int major =0;
static struct class *key_class;
static int g_key_value = 0;
struct fasync_struct *key_fasync;

/* 环形缓冲区 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;

#define NEXT_POS(x) ((x+1) % BUF_LEN)

static int is_key_buf_empty(void)
{
	return (r == w);
}

static int is_key_buf_full(void)
{
	return (r == NEXT_POS(w));
}

static void put_key(int key)
{
	if (!is_key_buf_full())
	{
		g_keys[w] = key;
		w = NEXT_POS(w);
	}
}

static int get_key(void)
{
	int key = 0;
	if (!is_key_buf_empty())
	{
		key = g_keys[r];
		r = NEXT_POS(r);
	}
	return key;
}

//static struct tasklet_struct tasklet_key;

void tasklet_function(unsigned long arg)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
}

DECLARE_TASKLET(tasklet_key, tasklet_function, 0);

static DECLARE_WAIT_QUEUE_HEAD(key_wait);
struct timer_list key_timer;

void timer_function(unsigned long arg)
{
	int value;
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	struct gpio_key *ptr_gpio_key_temp = arg;
	value = gpiod_get_value(ptr_gpio_key_temp->gpiod);
	g_key_value = (ptr_gpio_key_temp->gpio << 8) | value;
	printk(KERN_INFO "g_key_value is %d \r\n", g_key_value);
	put_key(g_key_value);
	wake_up_interruptible(&key_wait);
	kill_fasync(&key_fasync, SIGIO, POLLIN);
}

void work_function(struct work_struct *work)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
}

DECLARE_WORK(work_key, work_function);


static int key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	wait_event_interruptible(key_wait, !is_key_buf_empty());
	g_key_value = get_key();
	copy_to_user(buf, &g_key_value, 4);
	return 4;
}


static unsigned int key_drv_poll(struct file *fp, poll_table * wait)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	poll_wait(fp, &key_wait, wait);//非阻塞函数
	
	return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}

static int key_drv_fasync(int fd, struct file *file, int on)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	if(fasync_helper(fd, file, on, &key_fasync) >= 0)
		return 0;
	else
		return -EIO;
}

static struct file_operations key_drv =
{
	.owner = THIS_MODULE,
	.read  = key_drv_read,
	.poll  = key_drv_poll,
	.fasync = key_drv_fasync,
};

static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
	key_timer.data =(unsigned long) dev_id;
	mod_timer(&key_timer, jiffies + HZ/4);
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	tasklet_schedule(&tasklet_key);
	schedule_work(&work_key);
	return IRQ_HANDLED;
}

static int key_probe(struct platform_device *pdev)
{
	int count = 0;
	int i=0;
	enum of_gpio_flags flag;
	struct device_node *node = pdev->dev.of_node;
	
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	count = of_gpio_count(node);
	ptr_gpio_key = kzalloc(sizeof(struct gpio_key)*count, GFP_KERNEL);
	
	for(i=0;i<count;i++)
	{
		ptr_gpio_key[i].gpio = of_get_gpio_flags(node, i, &flag);
		if(ptr_gpio_key[i].gpio < 0)
		{
			printk(KERN_ERR "of_get_gpio_flags is err\r\n");
		}
		ptr_gpio_key[i].gpiod = gpio_to_desc(ptr_gpio_key[i].gpio);
		ptr_gpio_key[i].flag = flag & OF_GPIO_ACTIVE_LOW;
		ptr_gpio_key[i].irq = gpio_to_irq(ptr_gpio_key[i].gpio);
		request_irq(ptr_gpio_key[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "gpio_key", &ptr_gpio_key[i]);
	}
	
	major = register_chrdev(0, "my_keydrv", &key_drv);
	key_class = class_create(THIS_MODULE, "my_key_class");
	if(IS_ERR(key_class))
	{
		printk(KERN_ERR "class_create is err\r\n");
	}
	
	device_create(key_class, NULL, MKDEV(major, 0), NULL, "my_key%d", 0);
	init_timer(&key_timer);
	key_timer.function = timer_function;
	key_timer.expires = ~0;
	add_timer(&key_timer);

	return 0;	
}



static int key_remove(struct platform_device *pdev)
{
	int count = 0;
	int i = 0;
	struct device_node *node = pdev->dev.of_node;

	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	count = of_gpio_count(node);

	device_destroy(key_class, MKDEV(major, 0));
	class_destroy(key_class);
	unregister_chrdev(major, "my_keydrv");
	for(i=0;i<count;i++)
	{
		free_irq(ptr_gpio_key[i].irq, &ptr_gpio_key[i]);
	}

	kfree(ptr_gpio_key);
	del_timer(&key_timer);
	tasklet_kill(&tasklet_key);
	return 0;	
}


static const struct of_device_id my_key[] =
{
	{.compatible = "my,key_driver"},
	{},
};


static struct platform_driver key_driver =
{
	.probe  = key_probe,
	.remove = key_remove,
	.driver = 
	{
		.name = "key_gpio",
		.of_match_table = my_key,
	},
};


static int __init my_key_init(void)
{
	int result;
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	result = platform_driver_register(&key_driver);
	return result;
}


static void __exit my_key_exit(void)
{
	printk(KERN_INFO "%s %s line is %d \r\n", __FILE__, __FUNCTION__, __LINE__);
	platform_driver_unregister(&key_driver);
}

module_init(my_key_init);
module_exit(my_key_exit);

MODULE_LICENSE("GPL");

/* by 面朝大海0902 */
这里的驱动代码与上一节我们讲的Linux中断子系统—tasklet合一起了,他们的用法类似,区别在于tasklet的处理在中断上下文中,不能休眠且因为在执行中断,普通的进程是无法被调度到的,处理的事情一般耗时短(如果耗时长其他进程得不到调度,系统会卡顿),而工作队列是在进程上下文执行,所以它处理的任务可以被看作为一个普通线程一样,由调度器去调度,可以休眠,处理的事情可以是比较耗时的任务。

四、实验结果

运行读取key值的应用(应用程序源码参考Linux异步通知—signal()、fcntl()函数介绍与使用),2个按键分别按下打印如下:

[root:/mnt]# ./key-fasync-test /dev/my_key0
[  278.547533] /home/book/code/test/key-work.c key_drv_fasync line is 132
by mianchaodahai 0902
by mianchaodahai 0902
[  284.291871] /home/book/code/test/key-work.c gpio_key_isr line is 151
[  284.298690] /home/book/code/test/key-work.c tasklet_function line is 83
[  284.305686] /home/book/code/test/key-work.c work_function line is 106
[  284.463200] /home/book/code/test/key-work.c gpio_key_isr line is 151
[  284.469869] /home/book/code/test/key-work.c tasklet_function line is 83
[  284.476780] /home/book/code/test/key-work.c work_function line is 106
[  284.714498] /home/book/code/test/key-work.c timer_function line is 94
[  284.721255] g_key_value is 33025
[  284.725673] /home/book/code/test/key-work.c key_drv_read line is 114
get button : 0x8101
by mianchaodahai 0902
[  285.777190] /home/book/code/test/key-work.c gpio_key_isr line is 151
[  285.783947] /home/book/code/test/key-work.c tasklet_function line is 83
[  285.790963] /home/book/code/test/key-work.c work_function line is 106
[  286.034537] /home/book/code/test/key-work.c timer_function line is 94
[  286.041301] g_key_value is 28160
[  286.045713] /home/book/code/test/key-work.c key_drv_read line is 114
get button : 0x6e00
by mianchaodahai 0902
[  286.116540] /home/book/code/test/key-work.c gpio_key_isr line is 151
[  286.123209] /home/book/code/test/key-work.c tasklet_function line is 83
[  286.130097] /home/book/code/test/key-work.c work_function line is 106
[  286.374540] /home/book/code/test/key-work.c timer_function line is 94
[  286.381310] g_key_value is 28161
[  286.385786] /home/book/code/test/key-work.c key_drv_read line is 114
get button : 0x6e01
by mianchaodahai 0902
^C[  288.106234] /home/book/code/test/key-work.c key_drv_fasync line is 132

通过打印我们可以看到当按键被按下后gpio_key_isr() 被执行,此时tasklet_schedule()schedule_work() 函数会被执行,tasklet工作队列各自对应的function函数tasklet_function()work_function() 函数被执行。
/* by 面朝大海0902 */

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值