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
{
39 struct rfkill
*rfk
[5];
46 } ideapad_rfk_data
[] = {
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
}
57 #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
59 static int read_method_int(acpi_handle handle
, const char *method
, int *val
)
62 unsigned long long result
;
64 status
= acpi_evaluate_integer(handle
, (char *)method
, NULL
, &result
);
65 if (ACPI_FAILURE(status
)) {
74 static int method_vpcr(acpi_handle handle
, int cmd
, int *ret
)
77 unsigned long long result
;
78 struct acpi_object_list params
;
79 union acpi_object in_obj
;
82 params
.pointer
= &in_obj
;
83 in_obj
.type
= ACPI_TYPE_INTEGER
;
84 in_obj
.integer
.value
= cmd
;
86 status
= acpi_evaluate_integer(handle
, "VPCR", ¶ms
, &result
);
88 if (ACPI_FAILURE(status
)) {
97 static int method_vpcw(acpi_handle handle
, int cmd
, int data
)
99 struct acpi_object_list params
;
100 union acpi_object in_obj
[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
;
110 status
= acpi_evaluate_object(handle
, "VPCW", ¶ms
, NULL
);
116 static int read_ec_data(acpi_handle handle
, int cmd
, unsigned long *data
)
119 unsigned long int end_jiffies
;
121 if (method_vpcw(handle
, 1, cmd
))
124 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
125 time_before(jiffies
, end_jiffies
);) {
127 if (method_vpcr(handle
, 1, &val
))
130 if (method_vpcr(handle
, 0, &val
))
136 pr_err("timeout in read_ec_cmd\n");
140 static int write_ec_cmd(acpi_handle handle
, int cmd
, unsigned long data
)
143 unsigned long int end_jiffies
;
145 if (method_vpcw(handle
, 0, data
))
147 if (method_vpcw(handle
, 1, cmd
))
150 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
151 time_before(jiffies
, end_jiffies
);) {
153 if (method_vpcr(handle
, 1, &val
))
158 pr_err("timeout in write_ec_cmd\n");
161 /* the above is ACPI helpers */
163 static int ideapad_dev_get_state(int device
)
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
;
171 output
.length
= sizeof(out_obj
);
172 output
.pointer
= &out_obj
;
174 in_param
.type
= ACPI_TYPE_INTEGER
;
175 in_param
.integer
.value
= device
+ 1;
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
);
182 if (out_obj
.type
!= ACPI_TYPE_INTEGER
) {
183 printk(KERN_WARNING
"IdeaPAD \\_SB_.GECN method returned unexpected type\n");
186 return out_obj
.integer
.value
;
189 static int ideapad_dev_set_state(int device
, int state
)
192 union acpi_object in_params
[2];
193 struct acpi_object_list input
= { 2, in_params
};
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
;
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
);
207 static ssize_t
show_ideapad_cam(struct device
*dev
,
208 struct device_attribute
*attr
,
211 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
212 acpi_handle handle
= priv
->handle
;
213 unsigned long result
;
215 if (read_ec_data(handle
, 0x1D, &result
))
216 return sprintf(buf
, "-1\n");
217 return sprintf(buf
, "%lu\n", result
);
220 static ssize_t
store_ideapad_cam(struct device
*dev
,
221 struct device_attribute
*attr
,
222 const char *buf
, size_t count
)
224 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
225 acpi_handle handle
= priv
->handle
;
230 if (sscanf(buf
, "%i", &state
) != 1)
232 ret
= write_ec_cmd(handle
, 0x1E, state
);
238 static DEVICE_ATTR(camera_power
, 0644, show_ideapad_cam
, store_ideapad_cam
);
240 static int ideapad_rfk_set(void *data
, bool blocked
)
242 int device
= (unsigned long)data
;
244 if (device
== IDEAPAD_DEV_KILLSW
)
246 return ideapad_dev_set_state(device
, !blocked
);
249 static struct rfkill_ops ideapad_rfk_ops
= {
250 .set_block
= ideapad_rfk_set
,
253 static void ideapad_sync_rfk_state(struct acpi_device
*adevice
)
255 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
256 int hw_blocked
= !ideapad_dev_get_state(IDEAPAD_DEV_KILLSW
);
259 rfkill_set_hw_state(priv
->rfk
[IDEAPAD_DEV_KILLSW
], hw_blocked
);
260 for (i
= IDEAPAD_DEV_WLAN
; i
< IDEAPAD_DEV_KILLSW
; i
++)
262 rfkill_set_hw_state(priv
->rfk
[i
], hw_blocked
);
266 for (i
= IDEAPAD_DEV_WLAN
; i
< IDEAPAD_DEV_KILLSW
; i
++)
268 rfkill_set_sw_state(priv
->rfk
[i
], !ideapad_dev_get_state(i
));
271 static int ideapad_register_rfkill(struct acpi_device
*adevice
, int dev
)
273 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
276 priv
->rfk
[dev
] = rfkill_alloc(ideapad_rfk_data
[dev
-1].name
, &adevice
->dev
,
277 ideapad_rfk_data
[dev
-1].type
, &ideapad_rfk_ops
,
282 ret
= rfkill_register(priv
->rfk
[dev
]);
284 rfkill_destroy(priv
->rfk
[dev
]);
290 static void ideapad_unregister_rfkill(struct acpi_device
*adevice
, int dev
)
292 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
297 rfkill_unregister(priv
->rfk
[dev
]);
298 rfkill_destroy(priv
->rfk
[dev
]);
301 static const struct acpi_device_id ideapad_device_ids
[] = {
305 MODULE_DEVICE_TABLE(acpi
, ideapad_device_ids
);
307 static int ideapad_acpi_add(struct acpi_device
*adevice
)
311 struct ideapad_private
*priv
;
313 if (read_method_int(adevice
->handle
, "_CFG", &cfg
))
316 for (i
= IDEAPAD_DEV_CAMERA
; i
< IDEAPAD_DEV_KILLSW
; i
++) {
317 if (test_bit(ideapad_rfk_data
[i
].cfgbit
, (unsigned long *)&cfg
))
323 /* The hardware switch is always present */
324 devs_present
[IDEAPAD_DEV_KILLSW
] = 1;
326 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
330 if (devs_present
[IDEAPAD_DEV_CAMERA
]) {
331 int ret
= device_create_file(&adevice
->dev
, &dev_attr_camera_power
);
338 priv
->handle
= adevice
->handle
;
339 dev_set_drvdata(&adevice
->dev
, priv
);
340 for (i
= IDEAPAD_DEV_WLAN
; i
<= IDEAPAD_DEV_KILLSW
; i
++) {
341 if (!devs_present
[i
])
344 ideapad_register_rfkill(adevice
, i
);
346 ideapad_sync_rfk_state(adevice
);
350 static int ideapad_acpi_remove(struct acpi_device
*adevice
, int type
)
352 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
355 device_remove_file(&adevice
->dev
, &dev_attr_camera_power
);
357 for (i
= IDEAPAD_DEV_WLAN
; i
<= IDEAPAD_DEV_KILLSW
; i
++)
358 ideapad_unregister_rfkill(adevice
, i
);
360 dev_set_drvdata(&adevice
->dev
, NULL
);
365 static void ideapad_acpi_notify(struct acpi_device
*adevice
, u32 event
)
367 acpi_handle handle
= adevice
->handle
;
368 unsigned long vpc1
, vpc2
, vpc_bit
;
370 if (read_ec_data(handle
, 0x10, &vpc1
))
372 if (read_ec_data(handle
, 0x1A, &vpc2
))
375 vpc1
= (vpc2
<< 8) | vpc1
;
376 for (vpc_bit
= 0; vpc_bit
< 16; vpc_bit
++) {
377 if (test_bit(vpc_bit
, &vpc1
)) {
379 ideapad_sync_rfk_state(adevice
);
384 static struct acpi_driver ideapad_acpi_driver
= {
385 .name
= "ideapad_acpi",
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
,
395 static int __init
ideapad_acpi_module_init(void)
397 acpi_bus_register_driver(&ideapad_acpi_driver
);
403 static void __exit
ideapad_acpi_module_exit(void)
405 acpi_bus_unregister_driver(&ideapad_acpi_driver
);
409 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
410 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
411 MODULE_LICENSE("GPL");
413 module_init(ideapad_acpi_module_init
);
414 module_exit(ideapad_acpi_module_exit
);