底半部之tasklet和工作队列

来源:互联网 发布:订单打印软件 编辑:程序博客网 时间:2024/05/22 06:17
#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/version.h>#include <linux/platform_device.h>#include <asm/io.h>#include <linux/fs.h>#include <linux/of.h>#include <linux/cdev.h>#include <linux/slab.h>#include <linux/interrupt.h>#include <asm/uaccess.h> MODULE_LICENSE("Dual BSD/GPL");MODULE_DESCRIPTION("a simple driver example!");void func(volatile int n){volatile int i ;while(n--)for(i=0;i<10000;i++);printk("long time delay done !\n");}void key_taskfunc(unsigned long x){func(10000);}void key_workfunc(struct work_struct *work){func(10000);}struct tasklet_struct *key_task;struct work_struct *key_work;irqreturn_t key_handler(int n, void *dev){#if 0key_task = kzalloc(GFP_ATOMIC, sizeof(struct tasklet_struct));tasklet_init(key_task, key_taskfunc, 0);tasklet_schedule(key_task); // tasklet: bottom half(soft interrupt)printk("taskstarted !\n");#endifkey_work = kzalloc(GFP_ATOMIC, sizeof(struct work_struct));INIT_WORK(key_work, key_workfunc);schedule_work(key_work); // workqueue: bottom half(kernel thread)printk("workstarted !\n");return IRQ_HANDLED;}struct resource *res;int key_probe(struct platform_device *pdev){int ret = 0;printk("probe !\n");res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);printk("irqnum: %#x\n", res->start);request_irq(res->start, key_handler, (res->flags&IRQF_TRIGGER_MASK) | IRQF_DISABLED, "key2", NULL);return ret;}int key_remove(struct platform_device *pdev){printk("key module release\n");free_irq(res->start, NULL);return 0;}#ifdef CONFIG_OFstruct of_device_id key_table[] = {{ .compatible = "key" },{ }};#endifstruct platform_driver key_driver = {.probe = key_probe,.remove = key_remove,.driver = {.name = "key",.of_match_table = of_match_ptr(key_table)}};static int key_init(void){printk("module install\n");//add into platform busplatform_driver_register(&key_driver);return 0;}static void key_exit(void){printk("module release\n");//del from platform busplatform_driver_unregister(&key_driver);}module_init(key_init);module_exit(key_exit);

0 0