2 * ideapad_acpi.c - Lenovo IdeaPad ACPI Extras
4 * Copyright © 2010 Intel Corporation
5 * Copyright © 2010 David Woodhouse <dwmw2@infradead.org>
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.
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.
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
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>
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
37 struct ideapad_private {
38 struct rfkill *rfk[5];
44 } ideapad_rfk_data[] = {
45 /* camera has no rfkill */
46 { "ideapad_wlan", RFKILL_TYPE_WLAN },
47 { "ideapad_bluetooth", RFKILL_TYPE_BLUETOOTH },
48 { "ideapad_3g", RFKILL_TYPE_WWAN },
49 { "ideapad_killsw", RFKILL_TYPE_WLAN }
52 static int ideapad_dev_exists(int device)
55 union acpi_object in_param;
56 struct acpi_object_list input = { 1, &in_param };
57 struct acpi_buffer output;
58 union acpi_object out_obj;
60 output.length = sizeof(out_obj);
61 output.pointer = &out_obj;
63 in_param.type = ACPI_TYPE_INTEGER;
64 in_param.integer.value = device + 1;
66 status = acpi_evaluate_object(NULL, "\\_SB_.DECN", &input, &output);
67 if (ACPI_FAILURE(status)) {
68 printk(KERN_WARNING "IdeaPAD \\_SB_.DECN method failed %d. Is this an IdeaPAD?\n", status);
71 if (out_obj.type != ACPI_TYPE_INTEGER) {
72 printk(KERN_WARNING "IdeaPAD \\_SB_.DECN method returned unexpected type\n");
75 return out_obj.integer.value;
78 static int ideapad_dev_get_state(int device)
81 union acpi_object in_param;
82 struct acpi_object_list input = { 1, &in_param };
83 struct acpi_buffer output;
84 union acpi_object out_obj;
86 output.length = sizeof(out_obj);
87 output.pointer = &out_obj;
89 in_param.type = ACPI_TYPE_INTEGER;
90 in_param.integer.value = device + 1;
92 status = acpi_evaluate_object(NULL, "\\_SB_.GECN", &input, &output);
93 if (ACPI_FAILURE(status)) {
94 printk(KERN_WARNING "IdeaPAD \\_SB_.GECN method failed %d\n", status);
97 if (out_obj.type != ACPI_TYPE_INTEGER) {
98 printk(KERN_WARNING "IdeaPAD \\_SB_.GECN method returned unexpected type\n");
101 return out_obj.integer.value;
104 static int ideapad_dev_set_state(int device, int state)
107 union acpi_object in_params[2];
108 struct acpi_object_list input = { 2, in_params };
110 in_params[0].type = ACPI_TYPE_INTEGER;
111 in_params[0].integer.value = device + 1;
112 in_params[1].type = ACPI_TYPE_INTEGER;
113 in_params[1].integer.value = state;
115 status = acpi_evaluate_object(NULL, "\\_SB_.SECN", &input, NULL);
116 if (ACPI_FAILURE(status)) {
117 printk(KERN_WARNING "IdeaPAD \\_SB_.SECN method failed %d\n", status);
122 static ssize_t show_ideapad_cam(struct device *dev,
123 struct device_attribute *attr,
126 int state = ideapad_dev_get_state(IDEAPAD_DEV_CAMERA);
130 return sprintf(buf, "%d\n", state);
133 static ssize_t store_ideapad_cam(struct device *dev,
134 struct device_attribute *attr,
135 const char *buf, size_t count)
141 if (sscanf(buf, "%i", &state) != 1)
143 ret = ideapad_dev_set_state(IDEAPAD_DEV_CAMERA, !!state);
149 static DEVICE_ATTR(camera_power, 0644, show_ideapad_cam, store_ideapad_cam);
151 static int ideapad_rfk_set(void *data, bool blocked)
153 int device = (unsigned long)data;
155 if (device == IDEAPAD_DEV_KILLSW)
157 return ideapad_dev_set_state(device, !blocked);
160 static struct rfkill_ops ideapad_rfk_ops = {
161 .set_block = ideapad_rfk_set,
164 static void ideapad_sync_rfk_state(struct acpi_device *adevice)
166 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
167 int hw_blocked = !ideapad_dev_get_state(IDEAPAD_DEV_KILLSW);
170 rfkill_set_hw_state(priv->rfk[IDEAPAD_DEV_KILLSW], hw_blocked);
171 for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++)
173 rfkill_set_hw_state(priv->rfk[i], hw_blocked);
177 for (i = IDEAPAD_DEV_WLAN; i < IDEAPAD_DEV_KILLSW; i++)
179 rfkill_set_sw_state(priv->rfk[i], !ideapad_dev_get_state(i));
182 static int ideapad_register_rfkill(struct acpi_device *adevice, int dev)
184 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
187 priv->rfk[dev] = rfkill_alloc(ideapad_rfk_data[dev-1].name, &adevice->dev,
188 ideapad_rfk_data[dev-1].type, &ideapad_rfk_ops,
193 ret = rfkill_register(priv->rfk[dev]);
195 rfkill_destroy(priv->rfk[dev]);
201 static void ideapad_unregister_rfkill(struct acpi_device *adevice, int dev)
203 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
208 rfkill_unregister(priv->rfk[dev]);
209 rfkill_destroy(priv->rfk[dev]);
212 static const struct acpi_device_id ideapad_device_ids[] = {
216 MODULE_DEVICE_TABLE(acpi, ideapad_device_ids);
218 static int ideapad_acpi_add(struct acpi_device *adevice)
222 struct ideapad_private *priv;
224 for (i = IDEAPAD_DEV_CAMERA; i < IDEAPAD_DEV_KILLSW; i++) {
225 devs_present[i] = ideapad_dev_exists(i);
226 if (devs_present[i] < 0)
227 return devs_present[i];
230 /* The hardware switch is always present */
231 devs_present[IDEAPAD_DEV_KILLSW] = 1;
233 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
237 if (devs_present[IDEAPAD_DEV_CAMERA]) {
238 int ret = device_create_file(&adevice->dev, &dev_attr_camera_power);
245 dev_set_drvdata(&adevice->dev, priv);
246 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++) {
247 if (!devs_present[i])
250 ideapad_register_rfkill(adevice, i);
252 ideapad_sync_rfk_state(adevice);
256 static int ideapad_acpi_remove(struct acpi_device *adevice, int type)
258 struct ideapad_private *priv = dev_get_drvdata(&adevice->dev);
261 device_remove_file(&adevice->dev, &dev_attr_camera_power);
263 for (i = IDEAPAD_DEV_WLAN; i <= IDEAPAD_DEV_KILLSW; i++)
264 ideapad_unregister_rfkill(adevice, i);
266 dev_set_drvdata(&adevice->dev, NULL);
271 static void ideapad_acpi_notify(struct acpi_device *adevice, u32 event)
273 ideapad_sync_rfk_state(adevice);
276 static struct acpi_driver ideapad_acpi_driver = {
277 .name = "ideapad_acpi",
279 .ids = ideapad_device_ids,
280 .ops.add = ideapad_acpi_add,
281 .ops.remove = ideapad_acpi_remove,
282 .ops.notify = ideapad_acpi_notify,
283 .owner = THIS_MODULE,
287 static int __init ideapad_acpi_module_init(void)
289 acpi_bus_register_driver(&ideapad_acpi_driver);
295 static void __exit ideapad_acpi_module_exit(void)
297 acpi_bus_unregister_driver(&ideapad_acpi_driver);
301 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
302 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
303 MODULE_LICENSE("GPL");
305 module_init(ideapad_acpi_module_init);
306 module_exit(ideapad_acpi_module_exit);