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];
47 } ideapad_rfk_data
[] = {
48 { "ideapad_camera", 19, 0x1E, NUM_RFKILL_TYPES
},
49 { "ideapad_wlan", 18, 0x15, RFKILL_TYPE_WLAN
},
50 { "ideapad_bluetooth", 16, 0x17, RFKILL_TYPE_BLUETOOTH
},
51 { "ideapad_3g", 17, 0x20, RFKILL_TYPE_WWAN
},
52 { "ideapad_killsw", 0, 0, RFKILL_TYPE_WLAN
}
55 static bool no_bt_rfkill
;
56 module_param(no_bt_rfkill
, bool, 0444);
57 MODULE_PARM_DESC(no_bt_rfkill
, "No rfkill for bluetooth.");
62 #define IDEAPAD_EC_TIMEOUT (100) /* in ms */
64 static int read_method_int(acpi_handle handle
, const char *method
, int *val
)
67 unsigned long long result
;
69 status
= acpi_evaluate_integer(handle
, (char *)method
, NULL
, &result
);
70 if (ACPI_FAILURE(status
)) {
79 static int method_vpcr(acpi_handle handle
, int cmd
, int *ret
)
82 unsigned long long result
;
83 struct acpi_object_list params
;
84 union acpi_object in_obj
;
87 params
.pointer
= &in_obj
;
88 in_obj
.type
= ACPI_TYPE_INTEGER
;
89 in_obj
.integer
.value
= cmd
;
91 status
= acpi_evaluate_integer(handle
, "VPCR", ¶ms
, &result
);
93 if (ACPI_FAILURE(status
)) {
102 static int method_vpcw(acpi_handle handle
, int cmd
, int data
)
104 struct acpi_object_list params
;
105 union acpi_object in_obj
[2];
109 params
.pointer
= in_obj
;
110 in_obj
[0].type
= ACPI_TYPE_INTEGER
;
111 in_obj
[0].integer
.value
= cmd
;
112 in_obj
[1].type
= ACPI_TYPE_INTEGER
;
113 in_obj
[1].integer
.value
= data
;
115 status
= acpi_evaluate_object(handle
, "VPCW", ¶ms
, NULL
);
121 static int read_ec_data(acpi_handle handle
, int cmd
, unsigned long *data
)
124 unsigned long int end_jiffies
;
126 if (method_vpcw(handle
, 1, cmd
))
129 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
130 time_before(jiffies
, end_jiffies
);) {
132 if (method_vpcr(handle
, 1, &val
))
135 if (method_vpcr(handle
, 0, &val
))
141 pr_err("timeout in read_ec_cmd\n");
145 static int write_ec_cmd(acpi_handle handle
, int cmd
, unsigned long data
)
148 unsigned long int end_jiffies
;
150 if (method_vpcw(handle
, 0, data
))
152 if (method_vpcw(handle
, 1, cmd
))
155 for (end_jiffies
= jiffies
+(HZ
)*IDEAPAD_EC_TIMEOUT
/1000+1;
156 time_before(jiffies
, end_jiffies
);) {
158 if (method_vpcr(handle
, 1, &val
))
163 pr_err("timeout in write_ec_cmd\n");
166 /* the above is ACPI helpers */
168 static ssize_t
show_ideapad_cam(struct device
*dev
,
169 struct device_attribute
*attr
,
172 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
173 acpi_handle handle
= priv
->handle
;
174 unsigned long result
;
176 if (read_ec_data(handle
, 0x1D, &result
))
177 return sprintf(buf
, "-1\n");
178 return sprintf(buf
, "%lu\n", result
);
181 static ssize_t
store_ideapad_cam(struct device
*dev
,
182 struct device_attribute
*attr
,
183 const char *buf
, size_t count
)
185 struct ideapad_private
*priv
= dev_get_drvdata(dev
);
186 acpi_handle handle
= priv
->handle
;
191 if (sscanf(buf
, "%i", &state
) != 1)
193 ret
= write_ec_cmd(handle
, 0x1E, state
);
199 static DEVICE_ATTR(camera_power
, 0644, show_ideapad_cam
, store_ideapad_cam
);
201 static int ideapad_rfk_set(void *data
, bool blocked
)
203 int device
= (unsigned long)data
;
205 if (device
== IDEAPAD_DEV_KILLSW
)
208 return write_ec_cmd(ideapad_priv
->handle
,
209 ideapad_rfk_data
[device
].opcode
,
213 static struct rfkill_ops ideapad_rfk_ops
= {
214 .set_block
= ideapad_rfk_set
,
217 static void ideapad_sync_rfk_state(struct acpi_device
*adevice
)
219 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
220 acpi_handle handle
= priv
->handle
;
221 unsigned long hw_blocked
;
224 if (read_ec_data(handle
, 0x23, &hw_blocked
))
226 hw_blocked
= !hw_blocked
;
228 for (i
= IDEAPAD_DEV_WLAN
; i
<= IDEAPAD_DEV_KILLSW
; i
++)
230 rfkill_set_hw_state(priv
->rfk
[i
], hw_blocked
);
233 static int ideapad_register_rfkill(struct acpi_device
*adevice
, int dev
)
235 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
237 unsigned long sw_blocked
;
240 (ideapad_rfk_data
[dev
].type
== RFKILL_TYPE_BLUETOOTH
)) {
241 /* Force to enable bluetooth when no_bt_rfkill=1 */
242 write_ec_cmd(ideapad_priv
->handle
,
243 ideapad_rfk_data
[dev
].opcode
, 1);
247 priv
->rfk
[dev
] = rfkill_alloc(ideapad_rfk_data
[dev
].name
, &adevice
->dev
,
248 ideapad_rfk_data
[dev
].type
, &ideapad_rfk_ops
,
253 if (read_ec_data(ideapad_priv
->handle
, ideapad_rfk_data
[dev
].opcode
-1,
255 rfkill_init_sw_state(priv
->rfk
[dev
], 0);
257 sw_blocked
= !sw_blocked
;
258 rfkill_init_sw_state(priv
->rfk
[dev
], sw_blocked
);
261 ret
= rfkill_register(priv
->rfk
[dev
]);
263 rfkill_destroy(priv
->rfk
[dev
]);
269 static void ideapad_unregister_rfkill(struct acpi_device
*adevice
, int dev
)
271 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
276 rfkill_unregister(priv
->rfk
[dev
]);
277 rfkill_destroy(priv
->rfk
[dev
]);
280 static const struct acpi_device_id ideapad_device_ids
[] = {
284 MODULE_DEVICE_TABLE(acpi
, ideapad_device_ids
);
286 static int ideapad_acpi_add(struct acpi_device
*adevice
)
290 struct ideapad_private
*priv
;
292 if (read_method_int(adevice
->handle
, "_CFG", &cfg
))
295 for (i
= IDEAPAD_DEV_CAMERA
; i
< IDEAPAD_DEV_KILLSW
; i
++) {
296 if (test_bit(ideapad_rfk_data
[i
].cfgbit
, (unsigned long *)&cfg
))
302 /* The hardware switch is always present */
303 devs_present
[IDEAPAD_DEV_KILLSW
] = 1;
305 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
309 if (devs_present
[IDEAPAD_DEV_CAMERA
]) {
310 int ret
= device_create_file(&adevice
->dev
, &dev_attr_camera_power
);
317 priv
->handle
= adevice
->handle
;
318 dev_set_drvdata(&adevice
->dev
, priv
);
320 for (i
= IDEAPAD_DEV_WLAN
; i
<= IDEAPAD_DEV_KILLSW
; i
++) {
321 if (!devs_present
[i
])
324 ideapad_register_rfkill(adevice
, i
);
326 ideapad_sync_rfk_state(adevice
);
330 static int ideapad_acpi_remove(struct acpi_device
*adevice
, int type
)
332 struct ideapad_private
*priv
= dev_get_drvdata(&adevice
->dev
);
335 device_remove_file(&adevice
->dev
, &dev_attr_camera_power
);
337 for (i
= IDEAPAD_DEV_WLAN
; i
<= IDEAPAD_DEV_KILLSW
; i
++)
338 ideapad_unregister_rfkill(adevice
, i
);
340 dev_set_drvdata(&adevice
->dev
, NULL
);
345 static void ideapad_acpi_notify(struct acpi_device
*adevice
, u32 event
)
347 acpi_handle handle
= adevice
->handle
;
348 unsigned long vpc1
, vpc2
, vpc_bit
;
350 if (read_ec_data(handle
, 0x10, &vpc1
))
352 if (read_ec_data(handle
, 0x1A, &vpc2
))
355 vpc1
= (vpc2
<< 8) | vpc1
;
356 for (vpc_bit
= 0; vpc_bit
< 16; vpc_bit
++) {
357 if (test_bit(vpc_bit
, &vpc1
)) {
359 ideapad_sync_rfk_state(adevice
);
364 static struct acpi_driver ideapad_acpi_driver
= {
365 .name
= "ideapad_acpi",
367 .ids
= ideapad_device_ids
,
368 .ops
.add
= ideapad_acpi_add
,
369 .ops
.remove
= ideapad_acpi_remove
,
370 .ops
.notify
= ideapad_acpi_notify
,
371 .owner
= THIS_MODULE
,
375 static int __init
ideapad_acpi_module_init(void)
377 acpi_bus_register_driver(&ideapad_acpi_driver
);
383 static void __exit
ideapad_acpi_module_exit(void)
385 acpi_bus_unregister_driver(&ideapad_acpi_driver
);
389 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
390 MODULE_DESCRIPTION("IdeaPad ACPI Extras");
391 MODULE_LICENSE("GPL");
393 module_init(ideapad_acpi_module_init
);
394 module_exit(ideapad_acpi_module_exit
);