mirror of
				https://kernel.googlesource.com/pub/scm/linux/kernel/git/stable/linux-stable.git
				synced 2025-11-04 07:44:51 +10:00 
			
		
		
		
	This mechanically converts all remaining cases of ancient open-coded timer
setup with the old setup_timer() API, which is the first step in timer
conversions. This has no behavioral changes, since it ultimately just
changes the order of assignment to fields of struct timer_list when
finding variations of:
    init_timer(&t);
    f.function = timer_callback;
    t.data = timer_callback_arg;
to be converted into:
    setup_timer(&t, timer_callback, timer_callback_arg);
The conversion is done with the following Coccinelle script, which
is an improved version of scripts/cocci/api/setup_timer.cocci, in the
following ways:
 - assignments-before-init_timer() cases
 - limit the .data case removal to the specific struct timer_list instance
 - handling calls by dereference (timer->field vs timer.field)
spatch --very-quiet --all-includes --include-headers \
	-I ./arch/x86/include -I ./arch/x86/include/generated \
	-I ./include -I ./arch/x86/include/uapi \
	-I ./arch/x86/include/generated/uapi -I ./include/uapi \
	-I ./include/generated/uapi --include ./include/linux/kconfig.h \
	--dir . \
	--cocci-file ~/src/data/setup_timer.cocci
@fix_address_of@
expression e;
@@
 init_timer(
-&(e)
+&e
 , ...)
// Match the common cases first to avoid Coccinelle parsing loops with
// "... when" clauses.
@match_immediate_function_data_after_init_timer@
expression e, func, da;
@@
-init_timer
+setup_timer
 ( \(&e\|e\)
+, func, da
 );
(
-\(e.function\|e->function\) = func;
-\(e.data\|e->data\) = da;
|
-\(e.data\|e->data\) = da;
-\(e.function\|e->function\) = func;
)
@match_immediate_function_data_before_init_timer@
expression e, func, da;
@@
(
-\(e.function\|e->function\) = func;
-\(e.data\|e->data\) = da;
|
-\(e.data\|e->data\) = da;
-\(e.function\|e->function\) = func;
)
-init_timer
+setup_timer
 ( \(&e\|e\)
+, func, da
 );
@match_function_and_data_after_init_timer@
expression e, e2, e3, e4, e5, func, da;
@@
-init_timer
+setup_timer
 ( \(&e\|e\)
+, func, da
 );
 ... when != func = e2
     when != da = e3
(
-e.function = func;
... when != da = e4
-e.data = da;
|
-e->function = func;
... when != da = e4
-e->data = da;
|
-e.data = da;
... when != func = e5
-e.function = func;
|
-e->data = da;
... when != func = e5
-e->function = func;
)
@match_function_and_data_before_init_timer@
expression e, e2, e3, e4, e5, func, da;
@@
(
-e.function = func;
... when != da = e4
-e.data = da;
|
-e->function = func;
... when != da = e4
-e->data = da;
|
-e.data = da;
... when != func = e5
-e.function = func;
|
-e->data = da;
... when != func = e5
-e->function = func;
)
... when != func = e2
    when != da = e3
-init_timer
+setup_timer
 ( \(&e\|e\)
+, func, da
 );
@r1 exists@
expression t;
identifier f;
position p;
@@
f(...) { ... when any
  init_timer@p(\(&t\|t\))
  ... when any
}
@r2 exists@
expression r1.t;
identifier g != r1.f;
expression e8;
@@
g(...) { ... when any
  \(t.data\|t->data\) = e8
  ... when any
}
// It is dangerous to use setup_timer if data field is initialized
// in another function.
@script:python depends on r2@
p << r1.p;
@@
cocci.include_match(False)
@r3@
expression r1.t, func, e7;
position r1.p;
@@
(
-init_timer@p(&t);
+setup_timer(&t, func, 0UL);
... when != func = e7
-t.function = func;
|
-t.function = func;
... when != func = e7
-init_timer@p(&t);
+setup_timer(&t, func, 0UL);
|
-init_timer@p(t);
+setup_timer(t, func, 0UL);
... when != func = e7
-t->function = func;
|
-t->function = func;
... when != func = e7
-init_timer@p(t);
+setup_timer(t, func, 0UL);
)
Signed-off-by: Kees Cook <keescook@chromium.org>
		
	
			
		
			
				
	
	
		
			140 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			140 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/*
 | 
						|
 * Generic push-switch framework
 | 
						|
 *
 | 
						|
 * Copyright (C) 2006  Paul Mundt
 | 
						|
 *
 | 
						|
 * This file is subject to the terms and conditions of the GNU General Public
 | 
						|
 * License.  See the file "COPYING" in the main directory of this archive
 | 
						|
 * for more details.
 | 
						|
 */
 | 
						|
#include <linux/init.h>
 | 
						|
#include <linux/slab.h>
 | 
						|
#include <linux/module.h>
 | 
						|
#include <linux/interrupt.h>
 | 
						|
#include <linux/platform_device.h>
 | 
						|
#include <asm/push-switch.h>
 | 
						|
 | 
						|
#define DRV_NAME "push-switch"
 | 
						|
#define DRV_VERSION "0.1.1"
 | 
						|
 | 
						|
static ssize_t switch_show(struct device *dev,
 | 
						|
			   struct device_attribute *attr,
 | 
						|
			   char *buf)
 | 
						|
{
 | 
						|
	struct push_switch_platform_info *psw_info = dev->platform_data;
 | 
						|
	return sprintf(buf, "%s\n", psw_info->name);
 | 
						|
}
 | 
						|
static DEVICE_ATTR(switch, S_IRUGO, switch_show, NULL);
 | 
						|
 | 
						|
static void switch_timer(unsigned long data)
 | 
						|
{
 | 
						|
	struct push_switch *psw = (struct push_switch *)data;
 | 
						|
 | 
						|
	schedule_work(&psw->work);
 | 
						|
}
 | 
						|
 | 
						|
static void switch_work_handler(struct work_struct *work)
 | 
						|
{
 | 
						|
	struct push_switch *psw = container_of(work, struct push_switch, work);
 | 
						|
	struct platform_device *pdev = psw->pdev;
 | 
						|
 | 
						|
	psw->state = 0;
 | 
						|
 | 
						|
	kobject_uevent(&pdev->dev.kobj, KOBJ_CHANGE);
 | 
						|
}
 | 
						|
 | 
						|
static int switch_drv_probe(struct platform_device *pdev)
 | 
						|
{
 | 
						|
	struct push_switch_platform_info *psw_info;
 | 
						|
	struct push_switch *psw;
 | 
						|
	int ret, irq;
 | 
						|
 | 
						|
	psw = kzalloc(sizeof(struct push_switch), GFP_KERNEL);
 | 
						|
	if (unlikely(!psw))
 | 
						|
		return -ENOMEM;
 | 
						|
 | 
						|
	irq = platform_get_irq(pdev, 0);
 | 
						|
	if (unlikely(irq < 0)) {
 | 
						|
		ret = -ENODEV;
 | 
						|
		goto err;
 | 
						|
	}
 | 
						|
 | 
						|
	psw_info = pdev->dev.platform_data;
 | 
						|
	BUG_ON(!psw_info);
 | 
						|
 | 
						|
	ret = request_irq(irq, psw_info->irq_handler,
 | 
						|
			  psw_info->irq_flags,
 | 
						|
			  psw_info->name ? psw_info->name : DRV_NAME, pdev);
 | 
						|
	if (unlikely(ret < 0))
 | 
						|
		goto err;
 | 
						|
 | 
						|
	if (psw_info->name) {
 | 
						|
		ret = device_create_file(&pdev->dev, &dev_attr_switch);
 | 
						|
		if (unlikely(ret)) {
 | 
						|
			dev_err(&pdev->dev, "Failed creating device attrs\n");
 | 
						|
			ret = -EINVAL;
 | 
						|
			goto err_irq;
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	INIT_WORK(&psw->work, switch_work_handler);
 | 
						|
	setup_timer(&psw->debounce, switch_timer, (unsigned long)psw);
 | 
						|
 | 
						|
	/* Workqueue API brain-damage */
 | 
						|
	psw->pdev = pdev;
 | 
						|
 | 
						|
	platform_set_drvdata(pdev, psw);
 | 
						|
 | 
						|
	return 0;
 | 
						|
 | 
						|
err_irq:
 | 
						|
	free_irq(irq, pdev);
 | 
						|
err:
 | 
						|
	kfree(psw);
 | 
						|
	return ret;
 | 
						|
}
 | 
						|
 | 
						|
static int switch_drv_remove(struct platform_device *pdev)
 | 
						|
{
 | 
						|
	struct push_switch *psw = platform_get_drvdata(pdev);
 | 
						|
	struct push_switch_platform_info *psw_info = pdev->dev.platform_data;
 | 
						|
	int irq = platform_get_irq(pdev, 0);
 | 
						|
 | 
						|
	if (psw_info->name)
 | 
						|
		device_remove_file(&pdev->dev, &dev_attr_switch);
 | 
						|
 | 
						|
	platform_set_drvdata(pdev, NULL);
 | 
						|
	flush_work(&psw->work);
 | 
						|
	del_timer_sync(&psw->debounce);
 | 
						|
	free_irq(irq, pdev);
 | 
						|
 | 
						|
	kfree(psw);
 | 
						|
 | 
						|
	return 0;
 | 
						|
}
 | 
						|
 | 
						|
static struct platform_driver switch_driver = {
 | 
						|
	.probe		= switch_drv_probe,
 | 
						|
	.remove		= switch_drv_remove,
 | 
						|
	.driver		= {
 | 
						|
		.name	= DRV_NAME,
 | 
						|
	},
 | 
						|
};
 | 
						|
 | 
						|
static int __init switch_init(void)
 | 
						|
{
 | 
						|
	printk(KERN_NOTICE DRV_NAME ": version %s loaded\n", DRV_VERSION);
 | 
						|
	return platform_driver_register(&switch_driver);
 | 
						|
}
 | 
						|
 | 
						|
static void __exit switch_exit(void)
 | 
						|
{
 | 
						|
	platform_driver_unregister(&switch_driver);
 | 
						|
}
 | 
						|
module_init(switch_init);
 | 
						|
module_exit(switch_exit);
 | 
						|
 | 
						|
MODULE_VERSION(DRV_VERSION);
 | 
						|
MODULE_AUTHOR("Paul Mundt");
 | 
						|
MODULE_LICENSE("GPL v2");
 |