blob: 09f6ce6b378dd29c667f57bde686ee8c7ea2a818 [file] [log] [blame]
David Woodhouse58ac7aa2010-08-10 23:44:05 +01001/*
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
David Woodhousece326322010-08-11 17:59:35 +010037struct ideapad_private {
Ike Panhc26c81d52010-10-01 15:39:40 +080038 acpi_handle handle;
David Woodhousece326322010-08-11 17:59:35 +010039 struct rfkill *rfk[5];
David Woodhouse58ac7aa2010-08-10 23:44:05 +010040};
David Woodhousece326322010-08-11 17:59:35 +010041
42static struct {
43 char *name;
Ike Panhcdfa7f6f2010-10-01 15:39:29 +080044 int cfgbit;
David Woodhousece326322010-08-11 17:59:35 +010045 int type;
46} ideapad_rfk_data[] = {
Ike Panhcdfa7f6f2010-10-01 15:39:29 +080047 { "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 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +010052};
53
Ike Panhc6a09f212010-10-01 15:38:46 +080054/*
55 * ACPI Helpers
56 */
57#define IDEAPAD_EC_TIMEOUT (100) /* in ms */
58
59static 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
74static 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", &params, &result);
87
88 if (ACPI_FAILURE(status)) {
89 *ret = -1;
90 return -1;
91 } else {
92 *ret = result;
93 return 0;
94 }
95}
96
97static 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", &params, NULL);
111 if (status != AE_OK)
112 return -1;
113 return 0;
114}
115
116static 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
140static 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
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100163static int ideapad_dev_set_state(int device, int state)
164{
165 acpi_status status;
166 union acpi_object in_params[2];
167 struct acpi_object_list input = { 2, in_params };
168
169 in_params[0].type = ACPI_TYPE_INTEGER;
170 in_params[0].integer.value = device + 1;
171 in_params[1].type = ACPI_TYPE_INTEGER;
172 in_params[1].integer.value = state;
173
174 status = acpi_evaluate_object(NULL, "\\_SB_.SECN", &input, NULL);
175 if (ACPI_FAILURE(status)) {
176 printk(KERN_WARNING "IdeaPAD \\_SB_.SECN method failed %d\n", status);
177 return -ENODEV;
178 }
179 return 0;
180}
181static ssize_t show_ideapad_cam(struct device *dev,
182 struct device_attribute *attr,
183 char *buf)
184{
Ike Panhc26c81d52010-10-01 15:39:40 +0800185 struct ideapad_private *priv = dev_get_drvdata(dev);
186 acpi_handle handle = priv->handle;
187 unsigned long result;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100188
Ike Panhc26c81d52010-10-01 15:39:40 +0800189 if (read_ec_data(handle, 0x1D, &result))
190 return sprintf(buf, "-1\n");
191 return sprintf(buf, "%lu\n", result);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100192}
193
194static ssize_t store_ideapad_cam(struct device *dev,
195 struct device_attribute *attr,
196 const char *buf, size_t count)
197{
Ike Panhc26c81d52010-10-01 15:39:40 +0800198 struct ideapad_private *priv = dev_get_drvdata(dev);
199 acpi_handle handle = priv->handle;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100200 int ret, state;
201
202 if (!count)
203 return 0;
204 if (sscanf(buf, "%i", &state) != 1)
205 return -EINVAL;
Ike Panhc26c81d52010-10-01 15:39:40 +0800206 ret = write_ec_cmd(handle, 0x1E, state);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100207 if (ret < 0)
208 return ret;
209 return count;
210}
211
212static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
213
214static int ideapad_rfk_set(void *data, bool blocked)
215{
216 int device = (unsigned long)data;
217
218 if (device == IDEAPAD_DEV_KILLSW)
219 return -EINVAL;
220 return ideapad_dev_set_state(device, !blocked);
221}
222
223static struct rfkill_ops ideapad_rfk_ops = {
224 .set_block = ideapad_rfk_set,
225};
226
David Woodhousece326322010-08-11 17:59:35 +0100227static void ideapad_sync_rfk_state(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100228{
David Woodhousece326322010-08-11 17:59:35 +0100229 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
Ike Panhc2b7266b2010-10-01 15:39:49 +0800230 acpi_handle handle = priv->handle;
231 unsigned long hw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100232 int i;
233
Ike Panhc2b7266b2010-10-01 15:39:49 +0800234 if (read_ec_data(handle, 0x23, &hw_blocked))
235 return;
236 hw_blocked = !hw_blocked;
237
238 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
David Woodhousece326322010-08-11 17:59:35 +0100239 if (priv->rfk[i])
240 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100241}
242
David Woodhousece326322010-08-11 17:59:35 +0100243static int ideapad_register_rfkill(struct acpi_device *adevice, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100244{
David Woodhousece326322010-08-11 17:59:35 +0100245 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100246 int ret;
Ike Panhc2b7266b2010-10-01 15:39:49 +0800247 unsigned long sw_blocked;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100248
Ike Panhc2b7266b2010-10-01 15:39:49 +0800249 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev].name, &adevice->dev,
250 ideapad_rfk_data[dev].type, &ideapad_rfk_ops,
David Woodhousece326322010-08-11 17:59:35 +0100251 (void *)(long)dev);
252 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100253 return -ENOMEM;
254
Ike Panhc2b7266b2010-10-01 15:39:49 +0800255 if (read_ec_data(ideapad_priv->handle, ideapad_rfk_data[dev].opcode-1,
256 &sw_blocked)) {
257 rfkill_init_sw_state(priv->rfk[dev], 0);
258 } else {
259 sw_blocked = !sw_blocked;
260 rfkill_init_sw_state(priv->rfk[dev], sw_blocked);
261 }
262
David Woodhousece326322010-08-11 17:59:35 +0100263 ret = rfkill_register(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100264 if (ret) {
David Woodhousece326322010-08-11 17:59:35 +0100265 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100266 return ret;
267 }
268 return 0;
269}
270
David Woodhousece326322010-08-11 17:59:35 +0100271static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100272{
David Woodhousece326322010-08-11 17:59:35 +0100273 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
274
275 if (!priv->rfk[dev])
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100276 return;
277
David Woodhousece326322010-08-11 17:59:35 +0100278 rfkill_unregister(priv->rfk[dev]);
279 rfkill_destroy(priv->rfk[dev]);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100280}
281
282static const struct acpi_device_id ideapad_device_ids[] = {
283 { "VPC2004", 0},
284 { "", 0},
285};
286MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
287
David Woodhousece326322010-08-11 17:59:35 +0100288static int ideapad_acpi_add(struct acpi_device *adevice)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100289{
Ike Panhc6f8371c2010-10-01 15:39:14 +0800290 int i, cfg;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100291 int devs_present[5];
David Woodhousece326322010-08-11 17:59:35 +0100292 struct ideapad_private *priv;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100293
Ike Panhc6f8371c2010-10-01 15:39:14 +0800294 if (read_method_int(adevice->handle, "_CFG", &cfg))
295 return -ENODEV;
296
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100297 for (i = IDEAPAD_DEV_CAMERA; i < IDEAPAD_DEV_KILLSW; i++) {
Ike Panhcdfa7f6f2010-10-01 15:39:29 +0800298 if (test_bit(ideapad_rfk_data[i].cfgbit, (unsigned long *)&cfg))
299 devs_present[i] = 1;
300 else
301 devs_present[i] = 0;
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100302 }
303
304 /* The hardware switch is always present */
305 devs_present[IDEAPAD_DEV_KILLSW] = 1;
306
David Woodhousece326322010-08-11 17:59:35 +0100307 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
308 if (!priv)
309 return -ENOMEM;
310
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100311 if (devs_present[IDEAPAD_DEV_CAMERA]) {
David Woodhousece326322010-08-11 17:59:35 +0100312 int ret = device_create_file(&adevice->dev, &dev_attr_camera_power);
313 if (ret) {
314 kfree(priv);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100315 return ret;
David Woodhousece326322010-08-11 17:59:35 +0100316 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100317 }
318
Ike Panhc26c81d52010-10-01 15:39:40 +0800319 priv->handle = adevice->handle;
David Woodhousece326322010-08-11 17:59:35 +0100320 dev_set_drvdata(&adevice->dev, priv);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100321 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) {
322 if (!devs_present[i])
323 continue;
324
David Woodhousece326322010-08-11 17:59:35 +0100325 ideapad_register_rfkill(adevice, i);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100326 }
David Woodhousece326322010-08-11 17:59:35 +0100327 ideapad_sync_rfk_state(adevice);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100328 return 0;
329}
330
David Woodhousece326322010-08-11 17:59:35 +0100331static int ideapad_acpi_remove(struct acpi_device *adevice, int type)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100332{
David Woodhousece326322010-08-11 17:59:35 +0100333 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100334 int i;
David Woodhousece326322010-08-11 17:59:35 +0100335
336 device_remove_file(&adevice->dev, &dev_attr_camera_power);
337
338 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
339 ideapad_unregister_rfkill(adevice, i);
340
341 dev_set_drvdata(&adevice->dev, NULL);
342 kfree(priv);
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100343 return 0;
344}
345
David Woodhousece326322010-08-11 17:59:35 +0100346static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100347{
Ike Panhc8e7d3542010-10-01 15:39:05 +0800348 acpi_handle handle = adevice->handle;
349 unsigned long vpc1, vpc2, vpc_bit;
350
351 if (read_ec_data(handle, 0x10, &vpc1))
352 return;
353 if (read_ec_data(handle, 0x1A, &vpc2))
354 return;
355
356 vpc1 = (vpc2 << 8) | vpc1;
357 for (vpc_bit = 0; vpc_bit < 16; vpc_bit++) {
358 if (test_bit(vpc_bit, &vpc1)) {
359 if (vpc_bit == 9)
360 ideapad_sync_rfk_state(adevice);
361 }
362 }
David Woodhouse58ac7aa2010-08-10 23:44:05 +0100363}
364
365static struct acpi_driver ideapad_acpi_driver = {
366 .name = "ideapad_acpi",
367 .class = "IdeaPad",
368 .ids = ideapad_device_ids,
369 .ops.add = ideapad_acpi_add,
370 .ops.remove = ideapad_acpi_remove,
371 .ops.notify = ideapad_acpi_notify,
372 .owner = THIS_MODULE,
373};
374
375
376static int __init ideapad_acpi_module_init(void)
377{
378 acpi_bus_register_driver(&ideapad_acpi_driver);
379
380 return 0;
381}
382
383
384static void __exit ideapad_acpi_module_exit(void)
385{
386 acpi_bus_unregister_driver(&ideapad_acpi_driver);
387
388}
389
390MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
391MODULE_DESCRIPTION("IdeaPad ACPI Extras");
392MODULE_LICENSE("GPL");
393
394module_init(ideapad_acpi_module_init);
395module_exit(ideapad_acpi_module_exit);