diff options
author | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2012-03-06 12:10:21 -0500 |
---|---|---|
committer | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2012-03-06 14:03:30 -0500 |
commit | 104a5f3cad8f2f27cadbdf0029400ecd9e17ccc0 (patch) | |
tree | c64b1a3c71e5688631fbdbbd07e59f025066269f | |
parent | adab30d73844076b6ca9cd3e6382f5db3a44ded4 (diff) |
Input: max8925_onkey - avoid accessing input device too early
Input device must be allocated (but not necessarily registered) before
requesting IRQs, otherwise there is a chance that IRQ handler fires and
tries to reference not yet allocated input device.
Also it makes sense to store relative IRQ numbers in max8925_onkey_info
structure as they are needed in suspend/resume which we expect to be
called more often than probe and remove.
Acked-by: Haojian Zhuang <haojian.zhuang@gmail.com>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
-rw-r--r-- | drivers/input/misc/max8925_onkey.c | 91 |
1 files changed, 44 insertions, 47 deletions
diff --git a/drivers/input/misc/max8925_onkey.c b/drivers/input/misc/max8925_onkey.c index 27174770b13a..0a12b74140d3 100644 --- a/drivers/input/misc/max8925_onkey.c +++ b/drivers/input/misc/max8925_onkey.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /** | 1 | /** |
2 | * max8925_onkey.c - MAX8925 ONKEY driver | 2 | * MAX8925 ONKEY driver |
3 | * | 3 | * |
4 | * Copyright (C) 2009 Marvell International Ltd. | 4 | * Copyright (C) 2009 Marvell International Ltd. |
5 | * Haojian Zhuang <haojian.zhuang@marvell.com> | 5 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
@@ -35,7 +35,7 @@ struct max8925_onkey_info { | |||
35 | struct input_dev *idev; | 35 | struct input_dev *idev; |
36 | struct i2c_client *i2c; | 36 | struct i2c_client *i2c; |
37 | struct device *dev; | 37 | struct device *dev; |
38 | int irq[2]; | 38 | unsigned int irq[2]; |
39 | }; | 39 | }; |
40 | 40 | ||
41 | /* | 41 | /* |
@@ -46,17 +46,14 @@ struct max8925_onkey_info { | |||
46 | static irqreturn_t max8925_onkey_handler(int irq, void *data) | 46 | static irqreturn_t max8925_onkey_handler(int irq, void *data) |
47 | { | 47 | { |
48 | struct max8925_onkey_info *info = data; | 48 | struct max8925_onkey_info *info = data; |
49 | int ret, event; | 49 | int state; |
50 | 50 | ||
51 | ret = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS); | 51 | state = max8925_reg_read(info->i2c, MAX8925_ON_OFF_STATUS); |
52 | if (ret & SW_INPUT) | 52 | |
53 | event = 1; | 53 | input_report_key(info->idev, KEY_POWER, state & SW_INPUT); |
54 | else | ||
55 | event = 0; | ||
56 | input_report_key(info->idev, KEY_POWER, event); | ||
57 | input_sync(info->idev); | 54 | input_sync(info->idev); |
58 | 55 | ||
59 | dev_dbg(info->dev, "onkey event:%d\n", event); | 56 | dev_dbg(info->dev, "onkey state:%d\n", state); |
60 | 57 | ||
61 | /* Enable hardreset to halt if system isn't shutdown on time */ | 58 | /* Enable hardreset to halt if system isn't shutdown on time */ |
62 | max8925_set_bits(info->i2c, MAX8925_SYSENSEL, | 59 | max8925_set_bits(info->i2c, MAX8925_SYSENSEL, |
@@ -69,6 +66,7 @@ static int __devinit max8925_onkey_probe(struct platform_device *pdev) | |||
69 | { | 66 | { |
70 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); | 67 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
71 | struct max8925_onkey_info *info; | 68 | struct max8925_onkey_info *info; |
69 | struct input_dev *input; | ||
72 | int irq[2], error; | 70 | int irq[2], error; |
73 | 71 | ||
74 | irq[0] = platform_get_irq(pdev, 0); | 72 | irq[0] = platform_get_irq(pdev, 0); |
@@ -76,6 +74,7 @@ static int __devinit max8925_onkey_probe(struct platform_device *pdev) | |||
76 | dev_err(&pdev->dev, "No IRQ resource!\n"); | 74 | dev_err(&pdev->dev, "No IRQ resource!\n"); |
77 | return -EINVAL; | 75 | return -EINVAL; |
78 | } | 76 | } |
77 | |||
79 | irq[1] = platform_get_irq(pdev, 1); | 78 | irq[1] = platform_get_irq(pdev, 1); |
80 | if (irq[1] < 0) { | 79 | if (irq[1] < 0) { |
81 | dev_err(&pdev->dev, "No IRQ resource!\n"); | 80 | dev_err(&pdev->dev, "No IRQ resource!\n"); |
@@ -83,11 +82,24 @@ static int __devinit max8925_onkey_probe(struct platform_device *pdev) | |||
83 | } | 82 | } |
84 | 83 | ||
85 | info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL); | 84 | info = kzalloc(sizeof(struct max8925_onkey_info), GFP_KERNEL); |
86 | if (!info) | 85 | input = input_allocate_device(); |
87 | return -ENOMEM; | 86 | if (!info || !input) { |
87 | error = -ENOMEM; | ||
88 | goto err_free_mem; | ||
89 | } | ||
88 | 90 | ||
91 | info->idev = input; | ||
89 | info->i2c = chip->i2c; | 92 | info->i2c = chip->i2c; |
90 | info->dev = &pdev->dev; | 93 | info->dev = &pdev->dev; |
94 | info->irq[0] = irq[0]; | ||
95 | info->irq[1] = irq[1]; | ||
96 | |||
97 | input->name = "max8925_on"; | ||
98 | input->phys = "max8925_on/input0"; | ||
99 | input->id.bustype = BUS_I2C; | ||
100 | input->dev.parent = &pdev->dev; | ||
101 | input_set_capability(input, EV_KEY, KEY_POWER); | ||
102 | |||
91 | irq[0] += chip->irq_base; | 103 | irq[0] += chip->irq_base; |
92 | irq[1] += chip->irq_base; | 104 | irq[1] += chip->irq_base; |
93 | 105 | ||
@@ -96,61 +108,46 @@ static int __devinit max8925_onkey_probe(struct platform_device *pdev) | |||
96 | if (error < 0) { | 108 | if (error < 0) { |
97 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", | 109 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", |
98 | irq[0], error); | 110 | irq[0], error); |
99 | goto out; | 111 | goto err_free_mem; |
100 | } | 112 | } |
113 | |||
101 | error = request_threaded_irq(irq[1], NULL, max8925_onkey_handler, | 114 | error = request_threaded_irq(irq[1], NULL, max8925_onkey_handler, |
102 | IRQF_ONESHOT, "onkey-up", info); | 115 | IRQF_ONESHOT, "onkey-up", info); |
103 | if (error < 0) { | 116 | if (error < 0) { |
104 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", | 117 | dev_err(chip->dev, "Failed to request IRQ: #%d: %d\n", |
105 | irq[1], error); | 118 | irq[1], error); |
106 | goto out_irq; | 119 | goto err_free_irq0; |
107 | } | 120 | } |
108 | 121 | ||
109 | info->idev = input_allocate_device(); | ||
110 | if (!info->idev) { | ||
111 | dev_err(chip->dev, "Failed to allocate input dev\n"); | ||
112 | error = -ENOMEM; | ||
113 | goto out_input; | ||
114 | } | ||
115 | |||
116 | info->idev->name = "max8925_on"; | ||
117 | info->idev->phys = "max8925_on/input0"; | ||
118 | info->idev->id.bustype = BUS_I2C; | ||
119 | info->idev->dev.parent = &pdev->dev; | ||
120 | info->irq[0] = irq[0]; | ||
121 | info->irq[1] = irq[1]; | ||
122 | info->idev->evbit[0] = BIT_MASK(EV_KEY); | ||
123 | info->idev->keybit[BIT_WORD(KEY_POWER)] = BIT_MASK(KEY_POWER); | ||
124 | |||
125 | device_init_wakeup(&pdev->dev, 1); | ||
126 | |||
127 | error = input_register_device(info->idev); | 122 | error = input_register_device(info->idev); |
128 | if (error) { | 123 | if (error) { |
129 | dev_err(chip->dev, "Can't register input device: %d\n", error); | 124 | dev_err(chip->dev, "Can't register input device: %d\n", error); |
130 | goto out_reg; | 125 | goto err_free_irq1; |
131 | } | 126 | } |
132 | 127 | ||
133 | platform_set_drvdata(pdev, info); | 128 | platform_set_drvdata(pdev, info); |
129 | device_init_wakeup(&pdev->dev, 1); | ||
134 | 130 | ||
135 | return 0; | 131 | return 0; |
136 | 132 | ||
137 | out_reg: | 133 | err_free_irq1: |
138 | input_free_device(info->idev); | 134 | free_irq(irq[1], info); |
139 | out_input: | 135 | err_free_irq0: |
140 | free_irq(info->irq[1], info); | 136 | free_irq(irq[0], info); |
141 | out_irq: | 137 | err_free_mem: |
142 | free_irq(info->irq[0], info); | 138 | input_free_device(input); |
143 | out: | ||
144 | kfree(info); | 139 | kfree(info); |
140 | |||
145 | return error; | 141 | return error; |
146 | } | 142 | } |
147 | 143 | ||
148 | static int __devexit max8925_onkey_remove(struct platform_device *pdev) | 144 | static int __devexit max8925_onkey_remove(struct platform_device *pdev) |
149 | { | 145 | { |
150 | struct max8925_onkey_info *info = platform_get_drvdata(pdev); | 146 | struct max8925_onkey_info *info = platform_get_drvdata(pdev); |
147 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); | ||
151 | 148 | ||
152 | free_irq(info->irq[0], info); | 149 | free_irq(info->irq[0] + chip->irq_base, info); |
153 | free_irq(info->irq[1], info); | 150 | free_irq(info->irq[1] + chip->irq_base, info); |
154 | input_unregister_device(info->idev); | 151 | input_unregister_device(info->idev); |
155 | kfree(info); | 152 | kfree(info); |
156 | 153 | ||
@@ -167,8 +164,8 @@ static int max8925_onkey_suspend(struct device *dev) | |||
167 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); | 164 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
168 | 165 | ||
169 | if (device_may_wakeup(dev)) { | 166 | if (device_may_wakeup(dev)) { |
170 | chip->wakeup_flag |= 1 << (info->irq[0] - chip->irq_base); | 167 | chip->wakeup_flag |= 1 << info->irq[0]; |
171 | chip->wakeup_flag |= 1 << (info->irq[1] - chip->irq_base); | 168 | chip->wakeup_flag |= 1 << info->irq[1]; |
172 | } | 169 | } |
173 | 170 | ||
174 | return 0; | 171 | return 0; |
@@ -181,8 +178,8 @@ static int max8925_onkey_resume(struct device *dev) | |||
181 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); | 178 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
182 | 179 | ||
183 | if (device_may_wakeup(dev)) { | 180 | if (device_may_wakeup(dev)) { |
184 | chip->wakeup_flag &= ~(1 << (info->irq[0] - chip->irq_base)); | 181 | chip->wakeup_flag &= ~(1 << info->irq[0]); |
185 | chip->wakeup_flag &= ~(1 << (info->irq[1] - chip->irq_base)); | 182 | chip->wakeup_flag &= ~(1 << info->irq[1]); |
186 | } | 183 | } |
187 | 184 | ||
188 | return 0; | 185 | return 0; |