Commit | Line | Data |
---|---|---|
58ac7aa0 DW |
1 | /* |
2 | * ideapad_acpi.c - Lenovo IdeaPad ACPI Extras | |
3 | * | |
4 | * Copyright © 2010 Intel Corporation | |
5 | * Copyright © 2010 David Woodhouse <dwmw2@infradead.org> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
20 | * 02110-1301, USA. | |
21 | */ | |
22 | ||
23 | #include <linux/kernel.h> | |
24 | #include <linux/module.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/types.h> | |
27 | #include <acpi/acpi_bus.h> | |
28 | #include <acpi/acpi_drivers.h> | |
29 | #include <linux/rfkill.h> | |
30 | ||
31 | #define IDEAPAD_DEV_CAMERA 0 | |
32 | #define IDEAPAD_DEV_WLAN 1 | |
33 | #define IDEAPAD_DEV_BLUETOOTH 2 | |
34 | #define IDEAPAD_DEV_3G 3 | |
35 | #define IDEAPAD_DEV_KILLSW 4 | |
36 | ||
ce326329 | 37 | struct ideapad_private { |
26c81d5c | 38 | acpi_handle handle; |
ce326329 | 39 | struct rfkill *rfk[5]; |
58ac7aa0 | 40 | }; |
ce326329 DW |
41 | |
42 | static struct { | |
43 | char *name; | |
dfa7f6fe | 44 | int cfgbit; |
ce326329 DW |
45 | int type; |
46 | } ideapad_rfk_data[] = { | |
dfa7f6fe IP |
47 | { "ideapad_camera", 19, NUM_RFKILL_TYPES }, |
48 | { "ideapad_wlan", 18, RFKILL_TYPE_WLAN }, | |
49 | { "ideapad_bluetooth", 16, RFKILL_TYPE_BLUETOOTH }, | |
50 | { "ideapad_3g", 17, RFKILL_TYPE_WWAN }, | |
51 | { "ideapad_killsw", 0, RFKILL_TYPE_WLAN } | |
58ac7aa0 DW |
52 | }; |
53 | ||
6a09f21d IP |
54 | /* |
55 | * ACPI Helpers | |
56 | */ | |
57 | #define IDEAPAD_EC_TIMEOUT (100) /* in ms */ | |
58 | ||
59 | static int read_method_int(acpi_handle handle, const char *method, int *val) | |
60 | { | |
61 | acpi_status status; | |
62 | unsigned long long result; | |
63 | ||
64 | status = acpi_evaluate_integer(handle, (char *)method, NULL, &result); | |
65 | if (ACPI_FAILURE(status)) { | |
66 | *val = -1; | |
67 | return -1; | |
68 | } else { | |
69 | *val = result; | |
70 | return 0; | |
71 | } | |
72 | } | |
73 | ||
74 | static int method_vpcr(acpi_handle handle, int cmd, int *ret) | |
75 | { | |
76 | acpi_status status; | |
77 | unsigned long long result; | |
78 | struct acpi_object_list params; | |
79 | union acpi_object in_obj; | |
80 | ||
81 | params.count = 1; | |
82 | params.pointer = &in_obj; | |
83 | in_obj.type = ACPI_TYPE_INTEGER; | |
84 | in_obj.integer.value = cmd; | |
85 | ||
86 | status = acpi_evaluate_integer(handle, "VPCR", ¶ms, &result); | |
87 | ||
88 | if (ACPI_FAILURE(status)) { | |
89 | *ret = -1; | |
90 | return -1; | |
91 | } else { | |
92 | *ret = result; | |
93 | return 0; | |
94 | } | |
95 | } | |
96 | ||
97 | static int method_vpcw(acpi_handle handle, int cmd, int data) | |
98 | { | |
99 | struct acpi_object_list params; | |
100 | union acpi_object in_obj[2]; | |
101 | acpi_status status; | |
102 | ||
103 | params.count = 2; | |
104 | params.pointer = in_obj; | |
105 | in_obj[0].type = ACPI_TYPE_INTEGER; | |
106 | in_obj[0].integer.value = cmd; | |
107 | in_obj[1].type = ACPI_TYPE_INTEGER; | |
108 | in_obj[1].integer.value = data; | |
109 | ||
110 | status = acpi_evaluate_object(handle, "VPCW", ¶ms, NULL); | |
111 | if (status != AE_OK) | |
112 | return -1; | |
113 | return 0; | |
114 | } | |
115 | ||
116 | static int read_ec_data(acpi_handle handle, int cmd, unsigned long *data) | |
117 | { | |
118 | int val; | |
119 | unsigned long int end_jiffies; | |
120 | ||
121 | if (method_vpcw(handle, 1, cmd)) | |
122 | return -1; | |
123 | ||
124 | for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; | |
125 | time_before(jiffies, end_jiffies);) { | |
126 | schedule(); | |
127 | if (method_vpcr(handle, 1, &val)) | |
128 | return -1; | |
129 | if (val == 0) { | |
130 | if (method_vpcr(handle, 0, &val)) | |
131 | return -1; | |
132 | *data = val; | |
133 | return 0; | |
134 | } | |
135 | } | |
136 | pr_err("timeout in read_ec_cmd\n"); | |
137 | return -1; | |
138 | } | |
139 | ||
140 | static int write_ec_cmd(acpi_handle handle, int cmd, unsigned long data) | |
141 | { | |
142 | int val; | |
143 | unsigned long int end_jiffies; | |
144 | ||
145 | if (method_vpcw(handle, 0, data)) | |
146 | return -1; | |
147 | if (method_vpcw(handle, 1, cmd)) | |
148 | return -1; | |
149 | ||
150 | for (end_jiffies = jiffies+(HZ)*IDEAPAD_EC_TIMEOUT/1000+1; | |
151 | time_before(jiffies, end_jiffies);) { | |
152 | schedule(); | |
153 | if (method_vpcr(handle, 1, &val)) | |
154 | return -1; | |
155 | if (val == 0) | |
156 | return 0; | |
157 | } | |
158 | pr_err("timeout in write_ec_cmd\n"); | |
159 | return -1; | |
160 | } | |
161 | /* the above is ACPI helpers */ | |
162 | ||
58ac7aa0 DW |
163 | static int ideapad_dev_get_state(int device) |
164 | { | |
165 | acpi_status status; | |
166 | union acpi_object in_param; | |
167 | struct acpi_object_list input = { 1, &in_param }; | |
168 | struct acpi_buffer output; | |
169 | union acpi_object out_obj; | |
170 | ||
171 | output.length = sizeof(out_obj); | |
172 | output.pointer = &out_obj; | |
173 | ||
174 | in_param.type = ACPI_TYPE_INTEGER; | |
175 | in_param.integer.value = device + 1; | |
176 | ||
177 | status = acpi_evaluate_object(NULL, "\\_SB_.GECN", &input, &output); | |
178 | if (ACPI_FAILURE(status)) { | |
179 | printk(KERN_WARNING "IdeaPAD \\_SB_.GECN method failed %d\n", status); | |
180 | return -ENODEV; | |
181 | } | |
182 | if (out_obj.type != ACPI_TYPE_INTEGER) { | |
183 | printk(KERN_WARNING "IdeaPAD \\_SB_.GECN method returned unexpected type\n"); | |
184 | return -ENODEV; | |
185 | } | |
186 | return out_obj.integer.value; | |
187 | } | |
188 | ||
189 | static int ideapad_dev_set_state(int device, int state) | |
190 | { | |
191 | acpi_status status; | |
192 | union acpi_object in_params[2]; | |
193 | struct acpi_object_list input = { 2, in_params }; | |
194 | ||
195 | in_params[0].type = ACPI_TYPE_INTEGER; | |
196 | in_params[0].integer.value = device + 1; | |
197 | in_params[1].type = ACPI_TYPE_INTEGER; | |
198 | in_params[1].integer.value = state; | |
199 | ||
200 | status = acpi_evaluate_object(NULL, "\\_SB_.SECN", &input, NULL); | |
201 | if (ACPI_FAILURE(status)) { | |
202 | printk(KERN_WARNING "IdeaPAD \\_SB_.SECN method failed %d\n", status); | |
203 | return -ENODEV; | |
204 | } | |
205 | return 0; | |
206 | } | |
207 | static ssize_t show_ideapad_cam(struct device *dev, | |
208 | struct device_attribute *attr, | |
209 | char *buf) | |
210 | { | |
26c81d5c IP |
211 | struct ideapad_private *priv = dev_get_drvdata(dev); |
212 | acpi_handle handle = priv->handle; | |
213 | unsigned long result; | |
58ac7aa0 | 214 | |
26c81d5c IP |
215 | if (read_ec_data(handle, 0x1D, &result)) |
216 | return sprintf(buf, "-1\n"); | |
217 | return sprintf(buf, "%lu\n", result); | |
58ac7aa0 DW |
218 | } |
219 | ||
220 | static ssize_t store_ideapad_cam(struct device *dev, | |
221 | struct device_attribute *attr, | |
222 | const char *buf, size_t count) | |
223 | { | |
26c81d5c IP |
224 | struct ideapad_private *priv = dev_get_drvdata(dev); |
225 | acpi_handle handle = priv->handle; | |
58ac7aa0 DW |
226 | int ret, state; |
227 | ||
228 | if (!count) | |
229 | return 0; | |
230 | if (sscanf(buf, "%i", &state) != 1) | |
231 | return -EINVAL; | |
26c81d5c | 232 | ret = write_ec_cmd(handle, 0x1E, state); |
58ac7aa0 DW |
233 | if (ret < 0) |
234 | return ret; | |
235 | return count; | |
236 | } | |
237 | ||
238 | static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam); | |
239 | ||
240 | static int ideapad_rfk_set(void *data, bool blocked) | |
241 | { | |
242 | int device = (unsigned long)data; | |
243 | ||
244 | if (device == IDEAPAD_DEV_KILLSW) | |
245 | return -EINVAL; | |
246 | return ideapad_dev_set_state(device, !blocked); | |
247 | } | |
248 | ||
249 | static struct rfkill_ops ideapad_rfk_ops = { | |
250 | .set_block = ideapad_rfk_set, | |
251 | }; | |
252 | ||
ce326329 | 253 | static void ideapad_sync_rfk_state(struct acpi_device *adevice) |
58ac7aa0 | 254 | { |
ce326329 | 255 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
58ac7aa0 DW |
256 | int hw_blocked = !ideapad_dev_get_state(IDEAPAD_DEV_KILLSW); |
257 | int i; | |
258 | ||
ce326329 | 259 | rfkill_set_hw_state(priv->rfk[IDEAPAD_DEV_KILLSW], hw_blocked); |
58ac7aa0 | 260 | for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++) |
ce326329 DW |
261 | if (priv->rfk[i]) |
262 | rfkill_set_hw_state(priv->rfk[i], hw_blocked); | |
58ac7aa0 DW |
263 | if (hw_blocked) |
264 | return; | |
265 | ||
266 | for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++) | |
ce326329 DW |
267 | if (priv->rfk[i]) |
268 | rfkill_set_sw_state(priv->rfk[i], !ideapad_dev_get_state(i)); | |
58ac7aa0 DW |
269 | } |
270 | ||
ce326329 | 271 | static int ideapad_register_rfkill(struct acpi_device *adevice, int dev) |
58ac7aa0 | 272 | { |
ce326329 | 273 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
58ac7aa0 DW |
274 | int ret; |
275 | ||
ce326329 DW |
276 | priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev-1].name, &adevice->dev, |
277 | ideapad_rfk_data[dev-1].type, &ideapad_rfk_ops, | |
278 | (void *)(long)dev); | |
279 | if (!priv->rfk[dev]) | |
58ac7aa0 DW |
280 | return -ENOMEM; |
281 | ||
ce326329 | 282 | ret = rfkill_register(priv->rfk[dev]); |
58ac7aa0 | 283 | if (ret) { |
ce326329 | 284 | rfkill_destroy(priv->rfk[dev]); |
58ac7aa0 DW |
285 | return ret; |
286 | } | |
287 | return 0; | |
288 | } | |
289 | ||
ce326329 | 290 | static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev) |
58ac7aa0 | 291 | { |
ce326329 DW |
292 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
293 | ||
294 | if (!priv->rfk[dev]) | |
58ac7aa0 DW |
295 | return; |
296 | ||
ce326329 DW |
297 | rfkill_unregister(priv->rfk[dev]); |
298 | rfkill_destroy(priv->rfk[dev]); | |
58ac7aa0 DW |
299 | } |
300 | ||
301 | static const struct acpi_device_id ideapad_device_ids[] = { | |
302 | { "VPC2004", 0}, | |
303 | { "", 0}, | |
304 | }; | |
305 | MODULE_DEVICE_TABLE(acpi, ideapad_device_ids); | |
306 | ||
ce326329 | 307 | static int ideapad_acpi_add(struct acpi_device *adevice) |
58ac7aa0 | 308 | { |
6f8371c0 | 309 | int i, cfg; |
58ac7aa0 | 310 | int devs_present[5]; |
ce326329 | 311 | struct ideapad_private *priv; |
58ac7aa0 | 312 | |
6f8371c0 IP |
313 | if (read_method_int(adevice->handle, "_CFG", &cfg)) |
314 | return -ENODEV; | |
315 | ||
58ac7aa0 | 316 | for (i = IDEAPAD_DEV_CAMERA; i < IDEAPAD_DEV_KILLSW; i++) { |
dfa7f6fe IP |
317 | if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg)) |
318 | devs_present[i] = 1; | |
319 | else | |
320 | devs_present[i] = 0; | |
58ac7aa0 DW |
321 | } |
322 | ||
323 | /* The hardware switch is always present */ | |
324 | devs_present[IDEAPAD_DEV_KILLSW] = 1; | |
325 | ||
ce326329 DW |
326 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
327 | if (!priv) | |
328 | return -ENOMEM; | |
329 | ||
58ac7aa0 | 330 | if (devs_present[IDEAPAD_DEV_CAMERA]) { |
ce326329 DW |
331 | int ret = device_create_file(&adevice->dev, &dev_attr_camera_power); |
332 | if (ret) { | |
333 | kfree(priv); | |
58ac7aa0 | 334 | return ret; |
ce326329 | 335 | } |
58ac7aa0 DW |
336 | } |
337 | ||
26c81d5c | 338 | priv->handle = adevice->handle; |
ce326329 | 339 | dev_set_drvdata(&adevice->dev, priv); |
58ac7aa0 DW |
340 | for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) { |
341 | if (!devs_present[i]) | |
342 | continue; | |
343 | ||
ce326329 | 344 | ideapad_register_rfkill(adevice, i); |
58ac7aa0 | 345 | } |
ce326329 | 346 | ideapad_sync_rfk_state(adevice); |
58ac7aa0 DW |
347 | return 0; |
348 | } | |
349 | ||
ce326329 | 350 | static int ideapad_acpi_remove(struct acpi_device *adevice, int type) |
58ac7aa0 | 351 | { |
ce326329 | 352 | struct ideapad_private *priv = dev_get_drvdata(&adevice->dev); |
58ac7aa0 | 353 | int i; |
ce326329 DW |
354 | |
355 | device_remove_file(&adevice->dev, &dev_attr_camera_power); | |
356 | ||
357 | for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) | |
358 | ideapad_unregister_rfkill(adevice, i); | |
359 | ||
360 | dev_set_drvdata(&adevice->dev, NULL); | |
361 | kfree(priv); | |
58ac7aa0 DW |
362 | return 0; |
363 | } | |
364 | ||
ce326329 | 365 | static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event) |
58ac7aa0 | 366 | { |
8e7d3543 IP |
367 | acpi_handle handle = adevice->handle; |
368 | unsigned long vpc1, vpc2, vpc_bit; | |
369 | ||
370 | if (read_ec_data(handle, 0x10, &vpc1)) | |
371 | return; | |
372 | if (read_ec_data(handle, 0x1A, &vpc2)) | |
373 | return; | |
374 | ||
375 | vpc1 = (vpc2 << 8) | vpc1; | |
376 | for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) { | |
377 | if (test_bit(vpc_bit, &vpc1)) { | |
378 | if (vpc_bit == 9) | |
379 | ideapad_sync_rfk_state(adevice); | |
380 | } | |
381 | } | |
58ac7aa0 DW |
382 | } |
383 | ||
384 | static struct acpi_driver ideapad_acpi_driver = { | |
385 | .name = "ideapad_acpi", | |
386 | .class = "IdeaPad", | |
387 | .ids = ideapad_device_ids, | |
388 | .ops.add = ideapad_acpi_add, | |
389 | .ops.remove = ideapad_acpi_remove, | |
390 | .ops.notify = ideapad_acpi_notify, | |
391 | .owner = THIS_MODULE, | |
392 | }; | |
393 | ||
394 | ||
395 | static int __init ideapad_acpi_module_init(void) | |
396 | { | |
397 | acpi_bus_register_driver(&ideapad_acpi_driver); | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
402 | ||
403 | static void __exit ideapad_acpi_module_exit(void) | |
404 | { | |
405 | acpi_bus_unregister_driver(&ideapad_acpi_driver); | |
406 | ||
407 | } | |
408 | ||
409 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
410 | MODULE_DESCRIPTION("IdeaPad ACPI Extras"); | |
411 | MODULE_LICENSE("GPL"); | |
412 | ||
413 | module_init(ideapad_acpi_module_init); | |
414 | module_exit(ideapad_acpi_module_exit); |