blob: eea8c942653b2ca4033ad93c19012c04cf84166e [file] [log] [blame]
Yong Wangee027e42010-03-21 10:26:34 +08001/*
2 * Eee PC WMI hotkey driver
3 *
4 * Copyright(C) 2010 Intel Corporation.
Corentin Chary4c4edfa2010-11-29 08:14:11 +01005 * Copyright(C) 2010 Corentin Chary <corentin.chary@gmail.com>
Yong Wangee027e42010-03-21 10:26:34 +08006 *
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
Yong Wang81248882010-04-11 09:26:33 +080027#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
Yong Wangee027e42010-03-21 10:26:34 +080029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
Tejun Heoa32f3922010-04-05 11:37:59 +090033#include <linux/slab.h>
Yong Wangee027e42010-03-21 10:26:34 +080034#include <linux/input.h>
35#include <linux/input/sparse-keymap.h>
Yong Wang3d7b1652010-04-11 09:27:54 +080036#include <linux/fb.h>
37#include <linux/backlight.h>
Corentin Chary084fca62010-11-29 08:14:06 +010038#include <linux/leds.h>
Corentin Charyba48fdb2010-11-29 08:14:07 +010039#include <linux/rfkill.h>
Corentin Charyafa7c882011-02-06 13:28:28 +010040#include <linux/pci.h>
41#include <linux/pci_hotplug.h>
Corentin Chary8c1b2d82010-11-29 08:14:09 +010042#include <linux/debugfs.h>
43#include <linux/seq_file.h>
Yong Wang45f2c692010-04-11 09:27:19 +080044#include <linux/platform_device.h>
Corentin Charyafa7c882011-02-06 13:28:28 +010045#include <linux/dmi.h>
Yong Wangee027e42010-03-21 10:26:34 +080046#include <acpi/acpi_bus.h>
47#include <acpi/acpi_drivers.h>
48
Yong Wang45f2c692010-04-11 09:27:19 +080049#define EEEPC_WMI_FILE "eeepc-wmi"
50
Yong Wangee027e42010-03-21 10:26:34 +080051MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
52MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
53MODULE_LICENSE("GPL");
54
Corentin Charyd358cb52010-11-29 08:14:14 +010055#define EEEPC_ACPI_HID "ASUS010" /* old _HID used in eeepc-laptop */
56
Yong Wangee027e42010-03-21 10:26:34 +080057#define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
Yong Wang3d7b1652010-04-11 09:27:54 +080058#define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080059
60MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +080061MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
Yong Wangee027e42010-03-21 10:26:34 +080062
Corentin Chary33e0e6f2011-02-06 13:28:34 +010063#define NOTIFY_BRNUP_MIN 0x11
64#define NOTIFY_BRNUP_MAX 0x1f
65#define NOTIFY_BRNDOWN_MIN 0x20
66#define NOTIFY_BRNDOWN_MAX 0x2e
Yong Wangee027e42010-03-21 10:26:34 +080067
Corentin Chary43815942011-02-06 13:28:43 +010068/* WMI Methods */
Corentin Chary33e0e6f2011-02-06 13:28:34 +010069#define EEEPC_WMI_METHODID_DSTS 0x53544344
70#define EEEPC_WMI_METHODID_DEVS 0x53564544
71#define EEEPC_WMI_METHODID_CFVS 0x53564643
Yong Wang3d7b1652010-04-11 09:27:54 +080072
Corentin Chary43815942011-02-06 13:28:43 +010073/* Wireless */
Corentin Charyba48fdb2010-11-29 08:14:07 +010074#define EEEPC_WMI_DEVID_WLAN 0x00010011
75#define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
Corentin Chary2e9e1592011-02-06 13:28:37 +010076#define EEEPC_WMI_DEVID_WIMAX 0x00010017
Corentin Charyba48fdb2010-11-29 08:14:07 +010077#define EEEPC_WMI_DEVID_WWAN3G 0x00010019
Corentin Chary43815942011-02-06 13:28:43 +010078
79/* Backlight and Brightness */
Corentin Charyb7187262011-02-06 13:28:39 +010080#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050011
81#define EEEPC_WMI_DEVID_BRIGHTNESS 0x00050012
Corentin Chary43815942011-02-06 13:28:43 +010082
83/* Misc */
Corentin Chary9e1565b2011-02-06 13:28:36 +010084#define EEEPC_WMI_DEVID_CAMERA 0x00060013
Corentin Chary43815942011-02-06 13:28:43 +010085
86/* Storage */
Corentin Chary9e1565b2011-02-06 13:28:36 +010087#define EEEPC_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary43815942011-02-06 13:28:43 +010088
89/* Input */
Corentin Chary4615bb62011-02-06 13:28:42 +010090#define EEEPC_WMI_DEVID_TOUCHPAD 0x00100011
Corentin Chary8571d752011-02-06 13:28:41 +010091#define EEEPC_WMI_DEVID_TOUCHPAD_LED 0x00100012
Yong Wang3d7b1652010-04-11 09:27:54 +080092
Corentin Chary43815942011-02-06 13:28:43 +010093/* DSTS masks */
Corentin Charyaafa7192011-02-06 13:28:35 +010094#define EEEPC_WMI_DSTS_STATUS_BIT 0x00000001
95#define EEEPC_WMI_DSTS_PRESENCE_BIT 0x00010000
Corentin Charyb7187262011-02-06 13:28:39 +010096#define EEEPC_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
97#define EEEPC_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
Corentin Charyaafa7192011-02-06 13:28:35 +010098
Corentin Charyafa7c882011-02-06 13:28:28 +010099static bool hotplug_wireless;
100
101module_param(hotplug_wireless, bool, 0444);
102MODULE_PARM_DESC(hotplug_wireless,
103 "Enable hotplug for wireless device. "
104 "If your laptop needs that, please report to "
105 "acpi4asus-user@lists.sourceforge.net.");
106
Yong Wangee027e42010-03-21 10:26:34 +0800107static const struct key_entry eeepc_wmi_keymap[] = {
108 /* Sleep already handled via generic ACPI code */
Yong Wangee027e42010-03-21 10:26:34 +0800109 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
110 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
Corentin Chary5628e5a2011-02-06 13:28:26 +0100111 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
112 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
113 { KE_KEY, 0x32, { KEY_MUTE } },
Corentin Chary77ca5b02011-02-06 13:30:48 +0100114 { KE_KEY, 0x5c, { KEY_F15 } }, /* Power Gear key */
Corentin Chary5628e5a2011-02-06 13:28:26 +0100115 { KE_KEY, 0x5d, { KEY_WLAN } },
Chris Bagwelleda17482010-10-11 18:47:17 -0500116 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
Corentin Charybc40cce2011-02-06 13:28:27 +0100117 { KE_KEY, 0x88, { KEY_WLAN } },
Corentin Chary5628e5a2011-02-06 13:28:26 +0100118 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
Corentin Chary77ca5b02011-02-06 13:30:48 +0100119 { KE_KEY, 0xe0, { KEY_PROG1 } }, /* Task Manager */
120 { KE_KEY, 0xe1, { KEY_F14 } }, /* Change Resolution */
Corentin Chary54c799a2011-02-06 13:28:38 +0100121 { KE_KEY, 0xe9, { KEY_BRIGHTNESS_ZERO } },
Yong Wangee027e42010-03-21 10:26:34 +0800122 { KE_END, 0},
123};
124
Yong Wang3d7b1652010-04-11 09:27:54 +0800125struct bios_args {
126 u32 dev_id;
127 u32 ctrl_param;
128};
129
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100130/*
131 * eeepc-wmi/ - debugfs root directory
132 * dev_id - current dev_id
133 * ctrl_param - current ctrl_param
134 * devs - call DEVS(dev_id, ctrl_param) and print result
135 * dsts - call DSTS(dev_id) and print result
136 */
137struct eeepc_wmi_debug {
138 struct dentry *root;
139 u32 dev_id;
140 u32 ctrl_param;
141};
142
Yong Wang81248882010-04-11 09:26:33 +0800143struct eeepc_wmi {
Corentin Charyafa7c882011-02-06 13:28:28 +0100144 bool hotplug_wireless;
145
Yong Wang81248882010-04-11 09:26:33 +0800146 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800147 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100148 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100149
150 struct led_classdev tpd_led;
151 int tpd_led_wk;
152 struct workqueue_struct *led_workqueue;
153 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100154
155 struct rfkill *wlan_rfkill;
156 struct rfkill *bluetooth_rfkill;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100157 struct rfkill *wimax_rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100158 struct rfkill *wwan3g_rfkill;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100159
Corentin Charyafa7c882011-02-06 13:28:28 +0100160 struct hotplug_slot *hotplug_slot;
161 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100162 struct mutex wmi_lock;
163 struct workqueue_struct *hotplug_workqueue;
164 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100165
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100166 struct eeepc_wmi_debug debug;
Yong Wang81248882010-04-11 09:26:33 +0800167};
168
Yong Wang81248882010-04-11 09:26:33 +0800169static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800170{
171 int err;
172
Yong Wang81248882010-04-11 09:26:33 +0800173 eeepc->inputdev = input_allocate_device();
174 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800175 return -ENOMEM;
176
Yong Wang81248882010-04-11 09:26:33 +0800177 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800178 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800179 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100180 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800181
Yong Wang81248882010-04-11 09:26:33 +0800182 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800183 if (err)
184 goto err_free_dev;
185
Yong Wang81248882010-04-11 09:26:33 +0800186 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800187 if (err)
188 goto err_free_keymap;
189
190 return 0;
191
192err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800193 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800194err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800195 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800196 return err;
197}
198
Yong Wang81248882010-04-11 09:26:33 +0800199static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
200{
201 if (eeepc->inputdev) {
202 sparse_keymap_free(eeepc->inputdev);
203 input_unregister_device(eeepc->inputdev);
204 }
205
206 eeepc->inputdev = NULL;
207}
208
Corentin Chary2a3f0062010-11-29 08:14:10 +0100209static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800210{
211 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
212 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
213 union acpi_object *obj;
214 acpi_status status;
215 u32 tmp;
216
217 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
Corentin Charyafa7c882011-02-06 13:28:28 +0100218 1, EEEPC_WMI_METHODID_DSTS,
219 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800220
221 if (ACPI_FAILURE(status))
222 return status;
223
224 obj = (union acpi_object *)output.pointer;
225 if (obj && obj->type == ACPI_TYPE_INTEGER)
226 tmp = (u32)obj->integer.value;
227 else
228 tmp = 0;
229
Corentin Chary2a3f0062010-11-29 08:14:10 +0100230 if (retval)
231 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800232
233 kfree(obj);
234
235 return status;
236
237}
238
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100239static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
240 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800241{
242 struct bios_args args = {
243 .dev_id = dev_id,
244 .ctrl_param = ctrl_param,
245 };
246 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
247 acpi_status status;
248
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100249 if (!retval) {
250 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
251 EEEPC_WMI_METHODID_DEVS,
252 &input, NULL);
253 } else {
254 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
255 union acpi_object *obj;
256 u32 tmp;
257
258 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
259 EEEPC_WMI_METHODID_DEVS,
260 &input, &output);
261
262 if (ACPI_FAILURE(status))
263 return status;
264
265 obj = (union acpi_object *)output.pointer;
266 if (obj && obj->type == ACPI_TYPE_INTEGER)
267 tmp = (u32)obj->integer.value;
268 else
269 tmp = 0;
270
271 *retval = tmp;
272
273 kfree(obj);
274 }
Yong Wang3d7b1652010-04-11 09:27:54 +0800275
276 return status;
277}
278
Corentin Chary5c956382011-02-06 13:28:31 +0100279/* Helper for special devices with magic return codes */
Corentin Charyb7187262011-02-06 13:28:39 +0100280static int eeepc_wmi_get_devstate_bits(u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100281{
282 u32 retval = 0;
283 acpi_status status;
284
285 status = eeepc_wmi_get_devstate(dev_id, &retval);
286
287 if (ACPI_FAILURE(status))
288 return -EINVAL;
289
Corentin Charyaafa7192011-02-06 13:28:35 +0100290 if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100291 return -ENODEV;
292
Corentin Charyb7187262011-02-06 13:28:39 +0100293 return retval & mask;
294}
295
296static int eeepc_wmi_get_devstate_simple(u32 dev_id)
297{
298 return eeepc_wmi_get_devstate_bits(dev_id, EEEPC_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100299}
300
Corentin Chary084fca62010-11-29 08:14:06 +0100301/*
302 * LEDs
303 */
304/*
305 * These functions actually update the LED's, and are called from a
306 * workqueue. By doing this as separate work rather than when the LED
307 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
308 * potentially bad time, such as a timer interrupt.
309 */
310static void tpd_led_update(struct work_struct *work)
311{
312 int ctrl_param;
313 struct eeepc_wmi *eeepc;
314
315 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
316
317 ctrl_param = eeepc->tpd_led_wk;
Corentin Chary8571d752011-02-06 13:28:41 +0100318 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100319}
320
321static void tpd_led_set(struct led_classdev *led_cdev,
322 enum led_brightness value)
323{
324 struct eeepc_wmi *eeepc;
325
326 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
327
328 eeepc->tpd_led_wk = !!value;
329 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
330}
331
Corentin Chary8571d752011-02-06 13:28:41 +0100332static int read_tpd_led_state(struct eeepc_wmi *eeepc)
Corentin Chary084fca62010-11-29 08:14:06 +0100333{
Corentin Chary8571d752011-02-06 13:28:41 +0100334 return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100335}
336
337static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
338{
339 struct eeepc_wmi *eeepc;
340
341 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
342
Corentin Chary8571d752011-02-06 13:28:41 +0100343 return read_tpd_led_state(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +0100344}
345
346static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
347{
348 int rv;
349
Corentin Chary8571d752011-02-06 13:28:41 +0100350 if (read_tpd_led_state(eeepc) < 0)
Corentin Chary084fca62010-11-29 08:14:06 +0100351 return 0;
352
353 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
354 if (!eeepc->led_workqueue)
355 return -ENOMEM;
356 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
357
358 eeepc->tpd_led.name = "eeepc::touchpad";
359 eeepc->tpd_led.brightness_set = tpd_led_set;
360 eeepc->tpd_led.brightness_get = tpd_led_get;
361 eeepc->tpd_led.max_brightness = 1;
362
363 rv = led_classdev_register(&eeepc->platform_device->dev,
364 &eeepc->tpd_led);
365 if (rv) {
366 destroy_workqueue(eeepc->led_workqueue);
367 return rv;
368 }
369
370 return 0;
371}
372
373static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
374{
375 if (eeepc->tpd_led.dev)
376 led_classdev_unregister(&eeepc->tpd_led);
377 if (eeepc->led_workqueue)
378 destroy_workqueue(eeepc->led_workqueue);
379}
380
381/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100382 * PCI hotplug (for wlan rfkill)
383 */
384static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
385{
Corentin Chary5c956382011-02-06 13:28:31 +0100386 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100387
Corentin Chary5c956382011-02-06 13:28:31 +0100388 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100389 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100390 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100391}
392
393static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
394{
395 struct pci_dev *dev;
396 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100397 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100398 bool absent;
399 u32 l;
400
Corentin Chary279f8f92011-02-06 13:28:29 +0100401 mutex_lock(&eeepc->wmi_lock);
402 blocked = eeepc_wlan_rfkill_blocked(eeepc);
403 mutex_unlock(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100404
405 mutex_lock(&eeepc->hotplug_lock);
406
Corentin Chary279f8f92011-02-06 13:28:29 +0100407 if (eeepc->wlan_rfkill)
408 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
409
Corentin Charyafa7c882011-02-06 13:28:28 +0100410 if (eeepc->hotplug_slot) {
411 bus = pci_find_bus(0, 1);
412 if (!bus) {
413 pr_warning("Unable to find PCI bus 1?\n");
414 goto out_unlock;
415 }
416
417 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
418 pr_err("Unable to read PCI config space?\n");
419 goto out_unlock;
420 }
421 absent = (l == 0xffffffff);
422
423 if (blocked != absent) {
424 pr_warning("BIOS says wireless lan is %s, "
425 "but the pci device is %s\n",
426 blocked ? "blocked" : "unblocked",
427 absent ? "absent" : "present");
428 pr_warning("skipped wireless hotplug as probably "
429 "inappropriate for this model\n");
430 goto out_unlock;
431 }
432
433 if (!blocked) {
434 dev = pci_get_slot(bus, 0);
435 if (dev) {
436 /* Device already present */
437 pci_dev_put(dev);
438 goto out_unlock;
439 }
440 dev = pci_scan_single_device(bus, 0);
441 if (dev) {
442 pci_bus_assign_resources(bus);
443 if (pci_bus_add_device(dev))
444 pr_err("Unable to hotplug wifi\n");
445 }
446 } else {
447 dev = pci_get_slot(bus, 0);
448 if (dev) {
449 pci_remove_bus_device(dev);
450 pci_dev_put(dev);
451 }
452 }
453 }
454
455out_unlock:
456 mutex_unlock(&eeepc->hotplug_lock);
457}
458
459static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
460{
461 struct eeepc_wmi *eeepc = data;
462
463 if (event != ACPI_NOTIFY_BUS_CHECK)
464 return;
465
Corentin Chary279f8f92011-02-06 13:28:29 +0100466 /*
467 * We can't call directly eeepc_rfkill_hotplug because most
468 * of the time WMBC is still being executed and not reetrant.
469 * There is currently no way to tell ACPICA that we want this
470 * method to be serialized, we schedule a eeepc_rfkill_hotplug
471 * call later, in a safer context.
472 */
473 queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100474}
475
476static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
477 char *node)
478{
479 acpi_status status;
480 acpi_handle handle;
481
482 status = acpi_get_handle(NULL, node, &handle);
483
484 if (ACPI_SUCCESS(status)) {
485 status = acpi_install_notify_handler(handle,
486 ACPI_SYSTEM_NOTIFY,
487 eeepc_rfkill_notify,
488 eeepc);
489 if (ACPI_FAILURE(status))
490 pr_warning("Failed to register notify on %s\n", node);
491 } else
492 return -ENODEV;
493
494 return 0;
495}
496
497static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
498 char *node)
499{
500 acpi_status status = AE_OK;
501 acpi_handle handle;
502
503 status = acpi_get_handle(NULL, node, &handle);
504
505 if (ACPI_SUCCESS(status)) {
506 status = acpi_remove_notify_handler(handle,
507 ACPI_SYSTEM_NOTIFY,
508 eeepc_rfkill_notify);
509 if (ACPI_FAILURE(status))
510 pr_err("Error removing rfkill notify handler %s\n",
511 node);
512 }
513}
514
515static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
516 u8 *value)
517{
Corentin Chary5c956382011-02-06 13:28:31 +0100518 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100519
Corentin Chary5c956382011-02-06 13:28:31 +0100520 if (result < 0)
521 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100522
Corentin Chary5c956382011-02-06 13:28:31 +0100523 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100524 return 0;
525}
526
527static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
528{
529 kfree(hotplug_slot->info);
530 kfree(hotplug_slot);
531}
532
533static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
534 .owner = THIS_MODULE,
535 .get_adapter_status = eeepc_get_adapter_status,
536 .get_power_status = eeepc_get_adapter_status,
537};
538
Corentin Chary279f8f92011-02-06 13:28:29 +0100539static void eeepc_hotplug_work(struct work_struct *work)
540{
541 struct eeepc_wmi *eeepc;
542
543 eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
544 eeepc_rfkill_hotplug(eeepc);
545}
546
Corentin Charyafa7c882011-02-06 13:28:28 +0100547static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
548{
549 int ret = -ENOMEM;
550 struct pci_bus *bus = pci_find_bus(0, 1);
551
552 if (!bus) {
553 pr_err("Unable to find wifi PCI bus\n");
554 return -ENODEV;
555 }
556
Corentin Chary279f8f92011-02-06 13:28:29 +0100557 eeepc->hotplug_workqueue =
558 create_singlethread_workqueue("hotplug_workqueue");
559 if (!eeepc->hotplug_workqueue)
560 goto error_workqueue;
561
562 INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
563
Corentin Charyafa7c882011-02-06 13:28:28 +0100564 eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
565 if (!eeepc->hotplug_slot)
566 goto error_slot;
567
568 eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
569 GFP_KERNEL);
570 if (!eeepc->hotplug_slot->info)
571 goto error_info;
572
573 eeepc->hotplug_slot->private = eeepc;
574 eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
575 eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
576 eeepc_get_adapter_status(eeepc->hotplug_slot,
577 &eeepc->hotplug_slot->info->adapter_status);
578
579 ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
580 if (ret) {
581 pr_err("Unable to register hotplug slot - %d\n", ret);
582 goto error_register;
583 }
584
585 return 0;
586
587error_register:
588 kfree(eeepc->hotplug_slot->info);
589error_info:
590 kfree(eeepc->hotplug_slot);
591 eeepc->hotplug_slot = NULL;
592error_slot:
Corentin Chary279f8f92011-02-06 13:28:29 +0100593 destroy_workqueue(eeepc->hotplug_workqueue);
594error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100595 return ret;
596}
597
598/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100599 * Rfkill devices
600 */
601static int eeepc_rfkill_set(void *data, bool blocked)
602{
603 int dev_id = (unsigned long)data;
604 u32 ctrl_param = !blocked;
Corentin Chary7898cf12011-02-06 13:28:30 +0100605 acpi_status status;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100606
Corentin Chary7898cf12011-02-06 13:28:30 +0100607 status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
608
609 if (ACPI_FAILURE(status))
610 return -EIO;
611
612 return 0;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100613}
614
615static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
616{
617 int dev_id = (unsigned long)data;
Corentin Chary5c956382011-02-06 13:28:31 +0100618 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100619
Corentin Chary5c956382011-02-06 13:28:31 +0100620 result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100621
Corentin Chary5c956382011-02-06 13:28:31 +0100622 if (result < 0)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100623 return ;
624
Corentin Chary5c956382011-02-06 13:28:31 +0100625 rfkill_set_sw_state(rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100626}
627
Corentin Chary279f8f92011-02-06 13:28:29 +0100628static int eeepc_rfkill_wlan_set(void *data, bool blocked)
629{
630 struct eeepc_wmi *eeepc = data;
631 int ret;
632
633 /*
634 * This handler is enabled only if hotplug is enabled.
635 * In this case, the eeepc_wmi_set_devstate() will
636 * trigger a wmi notification and we need to wait
637 * this call to finish before being able to call
638 * any wmi method
639 */
640 mutex_lock(&eeepc->wmi_lock);
641 ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
642 mutex_unlock(&eeepc->wmi_lock);
643 return ret;
644}
645
646static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
647{
648 eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
649}
650
651static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
652 .set_block = eeepc_rfkill_wlan_set,
653 .query = eeepc_rfkill_wlan_query,
654};
655
Corentin Charyba48fdb2010-11-29 08:14:07 +0100656static const struct rfkill_ops eeepc_rfkill_ops = {
657 .set_block = eeepc_rfkill_set,
658 .query = eeepc_rfkill_query,
659};
660
661static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
662 struct rfkill **rfkill,
663 const char *name,
664 enum rfkill_type type, int dev_id)
665{
Corentin Chary5c956382011-02-06 13:28:31 +0100666 int result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100667
Corentin Chary5c956382011-02-06 13:28:31 +0100668 if (result < 0)
669 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100670
Corentin Chary279f8f92011-02-06 13:28:29 +0100671 if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
672 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
673 &eeepc_rfkill_wlan_ops, eeepc);
674 else
675 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
676 &eeepc_rfkill_ops, (void *)(long)dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100677
678 if (!*rfkill)
679 return -EINVAL;
680
Corentin Chary5c956382011-02-06 13:28:31 +0100681 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100682 result = rfkill_register(*rfkill);
683 if (result) {
684 rfkill_destroy(*rfkill);
685 *rfkill = NULL;
686 return result;
687 }
688 return 0;
689}
690
691static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
692{
Corentin Charyafa7c882011-02-06 13:28:28 +0100693 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
694 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
695 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
Corentin Charyba48fdb2010-11-29 08:14:07 +0100696 if (eeepc->wlan_rfkill) {
697 rfkill_unregister(eeepc->wlan_rfkill);
698 rfkill_destroy(eeepc->wlan_rfkill);
699 eeepc->wlan_rfkill = NULL;
700 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100701 /*
702 * Refresh pci hotplug in case the rfkill state was changed after
703 * eeepc_unregister_rfkill_notifier()
704 */
705 eeepc_rfkill_hotplug(eeepc);
706 if (eeepc->hotplug_slot)
707 pci_hp_deregister(eeepc->hotplug_slot);
Corentin Chary279f8f92011-02-06 13:28:29 +0100708 if (eeepc->hotplug_workqueue)
709 destroy_workqueue(eeepc->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100710
Corentin Charyba48fdb2010-11-29 08:14:07 +0100711 if (eeepc->bluetooth_rfkill) {
712 rfkill_unregister(eeepc->bluetooth_rfkill);
713 rfkill_destroy(eeepc->bluetooth_rfkill);
714 eeepc->bluetooth_rfkill = NULL;
715 }
Corentin Chary2e9e1592011-02-06 13:28:37 +0100716 if (eeepc->wimax_rfkill) {
717 rfkill_unregister(eeepc->wimax_rfkill);
718 rfkill_destroy(eeepc->wimax_rfkill);
719 eeepc->wimax_rfkill = NULL;
720 }
Corentin Charyba48fdb2010-11-29 08:14:07 +0100721 if (eeepc->wwan3g_rfkill) {
722 rfkill_unregister(eeepc->wwan3g_rfkill);
723 rfkill_destroy(eeepc->wwan3g_rfkill);
724 eeepc->wwan3g_rfkill = NULL;
725 }
726}
727
728static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
729{
730 int result = 0;
731
Corentin Charyafa7c882011-02-06 13:28:28 +0100732 mutex_init(&eeepc->hotplug_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100733 mutex_init(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100734
Corentin Charyba48fdb2010-11-29 08:14:07 +0100735 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
736 "eeepc-wlan", RFKILL_TYPE_WLAN,
737 EEEPC_WMI_DEVID_WLAN);
738
739 if (result && result != -ENODEV)
740 goto exit;
741
742 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
743 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
744 EEEPC_WMI_DEVID_BLUETOOTH);
745
746 if (result && result != -ENODEV)
747 goto exit;
748
Corentin Chary2e9e1592011-02-06 13:28:37 +0100749 result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
750 "eeepc-wimax", RFKILL_TYPE_WIMAX,
751 EEEPC_WMI_DEVID_WIMAX);
752
753 if (result && result != -ENODEV)
754 goto exit;
755
Corentin Charyba48fdb2010-11-29 08:14:07 +0100756 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
757 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
758 EEEPC_WMI_DEVID_WWAN3G);
759
760 if (result && result != -ENODEV)
761 goto exit;
762
Corentin Charyc14d4b82011-02-06 13:28:40 +0100763 if (!eeepc->hotplug_wireless)
764 goto exit;
765
Corentin Charyafa7c882011-02-06 13:28:28 +0100766 result = eeepc_setup_pci_hotplug(eeepc);
767 /*
768 * If we get -EBUSY then something else is handling the PCI hotplug -
769 * don't fail in this case
770 */
771 if (result == -EBUSY)
772 result = 0;
773
774 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
775 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
776 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
777 /*
778 * Refresh pci hotplug in case the rfkill state was changed during
779 * setup.
780 */
781 eeepc_rfkill_hotplug(eeepc);
782
Corentin Charyba48fdb2010-11-29 08:14:07 +0100783exit:
784 if (result && result != -ENODEV)
785 eeepc_wmi_rfkill_exit(eeepc);
786
787 if (result == -ENODEV)
788 result = 0;
789
790 return result;
791}
792
793/*
Corentin Chary084fca62010-11-29 08:14:06 +0100794 * Backlight
795 */
Corentin Charyb7187262011-02-06 13:28:39 +0100796static int read_backlight_power(void)
797{
798 int ret = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BACKLIGHT);
799
800 if (ret < 0)
801 return ret;
802
803 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
804}
805
Yong Wang3d7b1652010-04-11 09:27:54 +0800806static int read_brightness(struct backlight_device *bd)
807{
Corentin Charydfed65d2010-11-29 08:14:12 +0100808 u32 retval;
Yong Wang3d7b1652010-04-11 09:27:54 +0800809 acpi_status status;
810
Corentin Charyb7187262011-02-06 13:28:39 +0100811 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800812
813 if (ACPI_FAILURE(status))
Corentin Charyb7187262011-02-06 13:28:39 +0100814 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800815 else
Corentin Charyb7187262011-02-06 13:28:39 +0100816 return retval & EEEPC_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +0800817}
818
819static int update_bl_status(struct backlight_device *bd)
820{
Corentin Charydfed65d2010-11-29 08:14:12 +0100821 u32 ctrl_param;
Yong Wang3d7b1652010-04-11 09:27:54 +0800822 acpi_status status;
Corentin Charyb7187262011-02-06 13:28:39 +0100823 int power;
Yong Wang3d7b1652010-04-11 09:27:54 +0800824
825 ctrl_param = bd->props.brightness;
826
Corentin Charyb7187262011-02-06 13:28:39 +0100827 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BRIGHTNESS,
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100828 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800829
830 if (ACPI_FAILURE(status))
Corentin Charyb7187262011-02-06 13:28:39 +0100831 return -EIO;
832
833 power = read_backlight_power();
834 if (power != -ENODEV && bd->props.power != power) {
835 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
836 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
837 ctrl_param, NULL);
838
839 if (ACPI_FAILURE(status))
840 return -EIO;
841 }
842 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800843}
844
845static const struct backlight_ops eeepc_wmi_bl_ops = {
846 .get_brightness = read_brightness,
847 .update_status = update_bl_status,
848};
849
850static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
851{
852 struct backlight_device *bd = eeepc->backlight_device;
853 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200854 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800855
856 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
857 new = code - NOTIFY_BRNUP_MIN + 1;
858 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
859 new = code - NOTIFY_BRNDOWN_MIN;
860
861 bd->props.brightness = new;
862 backlight_update_status(bd);
863 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
864
865 return old;
866}
867
868static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
869{
870 struct backlight_device *bd;
871 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +0100872 int max;
873 int power;
874
875 max = eeepc_wmi_get_devstate_bits(EEEPC_WMI_DEVID_BRIGHTNESS,
876 EEEPC_WMI_DSTS_MAX_BRIGTH_MASK);
877 power = read_backlight_power();
878
879 if (max < 0 && power < 0) {
880 /* Try to keep the original error */
881 if (max == -ENODEV && power == -ENODEV)
882 return -ENODEV;
883 if (max != -ENODEV)
884 return max;
885 else
886 return power;
887 }
888 if (max == -ENODEV)
889 max = 0;
890 if (power == -ENODEV)
891 power = FB_BLANK_UNBLANK;
Yong Wang3d7b1652010-04-11 09:27:54 +0800892
893 memset(&props, 0, sizeof(struct backlight_properties));
Corentin Charyb7187262011-02-06 13:28:39 +0100894 props.max_brightness = max;
Yong Wang3d7b1652010-04-11 09:27:54 +0800895 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100896 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800897 &eeepc_wmi_bl_ops, &props);
898 if (IS_ERR(bd)) {
899 pr_err("Could not register backlight device\n");
900 return PTR_ERR(bd);
901 }
902
903 eeepc->backlight_device = bd;
904
905 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +0100906 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +0800907 backlight_update_status(bd);
908
909 return 0;
910}
911
912static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
913{
914 if (eeepc->backlight_device)
915 backlight_device_unregister(eeepc->backlight_device);
916
917 eeepc->backlight_device = NULL;
918}
919
920static void eeepc_wmi_notify(u32 value, void *context)
921{
922 struct eeepc_wmi *eeepc = context;
923 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
924 union acpi_object *obj;
925 acpi_status status;
926 int code;
927 int orig_code;
928
929 status = wmi_get_event_data(value, &response);
930 if (status != AE_OK) {
931 pr_err("bad event status 0x%x\n", status);
932 return;
933 }
934
935 obj = (union acpi_object *)response.pointer;
936
937 if (obj && obj->type == ACPI_TYPE_INTEGER) {
938 code = obj->integer.value;
939 orig_code = code;
940
941 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
942 code = NOTIFY_BRNUP_MIN;
943 else if (code >= NOTIFY_BRNDOWN_MIN &&
944 code <= NOTIFY_BRNDOWN_MAX)
945 code = NOTIFY_BRNDOWN_MIN;
946
947 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
948 if (!acpi_video_backlight_support())
949 eeepc_wmi_backlight_notify(eeepc, orig_code);
950 }
951
952 if (!sparse_keymap_report_event(eeepc->inputdev,
953 code, 1, true))
954 pr_info("Unknown key %x pressed\n", code);
955 }
956
957 kfree(obj);
958}
959
Corentin Chary9e1565b2011-02-06 13:28:36 +0100960/*
961 * Sys helpers
962 */
963static int parse_arg(const char *buf, unsigned long count, int *val)
964{
965 if (!count)
966 return 0;
967 if (sscanf(buf, "%i", val) != 1)
968 return -EINVAL;
969 return count;
970}
971
972static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
973{
974 acpi_status status;
975 u32 retval;
976 int rv, value;
977
978 value = eeepc_wmi_get_devstate_simple(devid);
979 if (value == -ENODEV) /* Check device presence */
980 return value;
981
982 rv = parse_arg(buf, count, &value);
983 status = eeepc_wmi_set_devstate(devid, value, &retval);
984
985 if (ACPI_FAILURE(status))
986 return -EIO;
987 return rv;
988}
989
990static ssize_t show_sys_wmi(int devid, char *buf)
991{
992 int value = eeepc_wmi_get_devstate_simple(devid);
993
994 if (value < 0)
995 return value;
996
997 return sprintf(buf, "%d\n", value);
998}
999
1000#define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
1001 static ssize_t show_##_name(struct device *dev, \
1002 struct device_attribute *attr, \
1003 char *buf) \
1004 { \
1005 return show_sys_wmi(_cm, buf); \
1006 } \
1007 static ssize_t store_##_name(struct device *dev, \
1008 struct device_attribute *attr, \
1009 const char *buf, size_t count) \
1010 { \
1011 return store_sys_wmi(_cm, buf, count); \
1012 } \
1013 static struct device_attribute dev_attr_##_name = { \
1014 .attr = { \
1015 .name = __stringify(_name), \
1016 .mode = _mode }, \
1017 .show = show_##_name, \
1018 .store = store_##_name, \
1019 }
1020
Corentin Chary4615bb62011-02-06 13:28:42 +01001021EEEPC_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, EEEPC_WMI_DEVID_TOUCHPAD);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001022EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
1023EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
1024
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07001025static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1026 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001027{
1028 int value;
1029 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
1030 acpi_status status;
1031
1032 if (!count || sscanf(buf, "%i", &value) != 1)
1033 return -EINVAL;
1034 if (value < 0 || value > 2)
1035 return -EINVAL;
1036
1037 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
1038 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
1039
1040 if (ACPI_FAILURE(status))
1041 return -EIO;
1042 else
1043 return count;
1044}
1045
1046static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1047
Corentin Chary4e37b422010-11-29 08:14:08 +01001048static struct attribute *platform_attributes[] = {
1049 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01001050 &dev_attr_camera.attr,
1051 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01001052 &dev_attr_touchpad.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01001053 NULL
1054};
1055
Corentin Chary9e1565b2011-02-06 13:28:36 +01001056static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
1057 struct attribute *attr,
1058 int idx)
1059{
1060 bool supported = true;
1061 int devid = -1;
1062
1063 if (attr == &dev_attr_camera.attr)
1064 devid = EEEPC_WMI_DEVID_CAMERA;
1065 else if (attr == &dev_attr_cardr.attr)
1066 devid = EEEPC_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01001067 else if (attr == &dev_attr_touchpad.attr)
1068 devid = EEEPC_WMI_DEVID_TOUCHPAD;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001069
1070 if (devid != -1)
1071 supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
1072
1073 return supported ? attr->mode : 0;
1074}
1075
Corentin Chary4e37b422010-11-29 08:14:08 +01001076static struct attribute_group platform_attribute_group = {
Corentin Chary9e1565b2011-02-06 13:28:36 +01001077 .is_visible = eeepc_sysfs_is_visible,
1078 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001079};
1080
Chris Bagwell7f80d732010-10-11 18:47:18 -05001081static void eeepc_wmi_sysfs_exit(struct platform_device *device)
1082{
Corentin Chary4e37b422010-11-29 08:14:08 +01001083 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001084}
1085
1086static int eeepc_wmi_sysfs_init(struct platform_device *device)
1087{
Corentin Chary4e37b422010-11-29 08:14:08 +01001088 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001089}
1090
Corentin Chary27c136c2010-11-29 08:14:05 +01001091/*
1092 * Platform device
1093 */
1094static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
1095{
Corentin Charya04ce292011-02-06 13:28:33 +01001096 return eeepc_wmi_sysfs_init(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001097}
1098
1099static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
1100{
1101 eeepc_wmi_sysfs_exit(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001102}
1103
1104/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001105 * debugfs
1106 */
1107struct eeepc_wmi_debugfs_node {
1108 struct eeepc_wmi *eeepc;
1109 char *name;
1110 int (*show)(struct seq_file *m, void *data);
1111};
1112
1113static int show_dsts(struct seq_file *m, void *data)
1114{
1115 struct eeepc_wmi *eeepc = m->private;
1116 acpi_status status;
1117 u32 retval = -1;
1118
1119 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
1120
1121 if (ACPI_FAILURE(status))
1122 return -EIO;
1123
1124 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1125
1126 return 0;
1127}
1128
1129static int show_devs(struct seq_file *m, void *data)
1130{
1131 struct eeepc_wmi *eeepc = m->private;
1132 acpi_status status;
1133 u32 retval = -1;
1134
1135 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1136 eeepc->debug.ctrl_param, &retval);
1137 if (ACPI_FAILURE(status))
1138 return -EIO;
1139
1140 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1141 eeepc->debug.ctrl_param, retval);
1142
1143 return 0;
1144}
1145
1146static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1147 { NULL, "devs", show_devs },
1148 { NULL, "dsts", show_dsts },
1149};
1150
1151static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1152{
1153 struct eeepc_wmi_debugfs_node *node = inode->i_private;
1154
1155 return single_open(file, node->show, node->eeepc);
1156}
1157
1158static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1159 .owner = THIS_MODULE,
1160 .open = eeepc_wmi_debugfs_open,
1161 .read = seq_read,
1162 .llseek = seq_lseek,
1163 .release = single_release,
1164};
1165
1166static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1167{
1168 debugfs_remove_recursive(eeepc->debug.root);
1169}
1170
1171static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1172{
1173 struct dentry *dent;
1174 int i;
1175
1176 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1177 if (!eeepc->debug.root) {
1178 pr_err("failed to create debugfs directory");
1179 goto error_debugfs;
1180 }
1181
1182 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1183 eeepc->debug.root, &eeepc->debug.dev_id);
1184 if (!dent)
1185 goto error_debugfs;
1186
1187 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1188 eeepc->debug.root, &eeepc->debug.ctrl_param);
1189 if (!dent)
1190 goto error_debugfs;
1191
1192 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1193 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1194
1195 node->eeepc = eeepc;
1196 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1197 eeepc->debug.root, node,
1198 &eeepc_wmi_debugfs_io_ops);
1199 if (!dent) {
1200 pr_err("failed to create debug file: %s\n", node->name);
1201 goto error_debugfs;
1202 }
1203 }
1204
1205 return 0;
1206
1207error_debugfs:
1208 eeepc_wmi_debugfs_exit(eeepc);
1209 return -ENOMEM;
1210}
1211
1212/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001213 * WMI Driver
1214 */
Corentin Charyafa7c882011-02-06 13:28:28 +01001215static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1216{
1217 const char *model;
1218
1219 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1220 if (!model)
1221 return;
1222
1223 /*
1224 * Whitelist for wlan hotplug
1225 *
1226 * Eeepc 1000H needs the current hotplug code to handle
1227 * Fn+F2 correctly. We may add other Eeepc here later, but
1228 * it seems that most of the laptops supported by eeepc-wmi
1229 * don't need to be on this list
1230 */
1231 if (strcmp(model, "1000H") == 0) {
1232 eeepc->hotplug_wireless = true;
1233 pr_info("wlan hotplug enabled\n");
1234 }
1235}
1236
Corentin Charya04ce292011-02-06 13:28:33 +01001237static int __init eeepc_wmi_add(struct platform_device *pdev)
Yong Wangee027e42010-03-21 10:26:34 +08001238{
Yong Wang45f2c692010-04-11 09:27:19 +08001239 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +08001240 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001241 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001242
Corentin Chary27c136c2010-11-29 08:14:05 +01001243 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1244 if (!eeepc)
Corentin Charya04ce292011-02-06 13:28:33 +01001245 return -ENOMEM;
1246
1247 eeepc->platform_device = pdev;
1248 platform_set_drvdata(eeepc->platform_device, eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001249
Corentin Charyafa7c882011-02-06 13:28:28 +01001250 eeepc->hotplug_wireless = hotplug_wireless;
1251 eeepc_dmi_check(eeepc);
1252
Corentin Chary27c136c2010-11-29 08:14:05 +01001253 err = eeepc_wmi_platform_init(eeepc);
1254 if (err)
1255 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001256
1257 err = eeepc_wmi_input_init(eeepc);
1258 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001259 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001260
Corentin Chary084fca62010-11-29 08:14:06 +01001261 err = eeepc_wmi_led_init(eeepc);
1262 if (err)
1263 goto fail_leds;
1264
Corentin Charyba48fdb2010-11-29 08:14:07 +01001265 err = eeepc_wmi_rfkill_init(eeepc);
1266 if (err)
1267 goto fail_rfkill;
1268
Yong Wang3d7b1652010-04-11 09:27:54 +08001269 if (!acpi_video_backlight_support()) {
1270 err = eeepc_wmi_backlight_init(eeepc);
Corentin Charyb7187262011-02-06 13:28:39 +01001271 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01001272 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001273 } else
1274 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001275
1276 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +01001277 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001278 if (ACPI_FAILURE(status)) {
1279 pr_err("Unable to register notify handler - %d\n",
1280 status);
1281 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001282 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001283 }
1284
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001285 err = eeepc_wmi_debugfs_init(eeepc);
1286 if (err)
1287 goto fail_debugfs;
1288
Corentin Charya04ce292011-02-06 13:28:33 +01001289 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001290
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001291fail_debugfs:
1292 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Corentin Chary27c136c2010-11-29 08:14:05 +01001293fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +08001294 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001295fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +01001296 eeepc_wmi_rfkill_exit(eeepc);
1297fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +01001298 eeepc_wmi_led_exit(eeepc);
1299fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +08001300 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001301fail_input:
1302 eeepc_wmi_platform_exit(eeepc);
1303fail_platform:
1304 kfree(eeepc);
Corentin Charya04ce292011-02-06 13:28:33 +01001305 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001306}
1307
Corentin Charya04ce292011-02-06 13:28:33 +01001308static int __exit eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001309{
1310 struct eeepc_wmi *eeepc;
1311
1312 eeepc = platform_get_drvdata(device);
1313 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +08001314 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001315 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +01001316 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001317 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001318 eeepc_wmi_debugfs_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001319 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001320
Corentin Chary27c136c2010-11-29 08:14:05 +01001321 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001322 return 0;
1323}
1324
Corentin Chary0773d7f2011-02-06 13:28:32 +01001325/*
1326 * Platform driver - hibernate/resume callbacks
1327 */
1328static int eeepc_hotk_thaw(struct device *device)
1329{
1330 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1331
1332 if (eeepc->wlan_rfkill) {
1333 bool wlan;
1334
1335 /*
1336 * Work around bios bug - acpi _PTS turns off the wireless led
1337 * during suspend. Normally it restores it on resume, but
1338 * we should kick it ourselves in case hibernation is aborted.
1339 */
1340 wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1341 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1342 }
1343
1344 return 0;
1345}
1346
1347static int eeepc_hotk_restore(struct device *device)
1348{
1349 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1350 int bl;
1351
1352 /* Refresh both wlan rfkill state and pci hotplug */
1353 if (eeepc->wlan_rfkill)
1354 eeepc_rfkill_hotplug(eeepc);
1355
1356 if (eeepc->bluetooth_rfkill) {
1357 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1358 rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001359 }
1360 if (eeepc->wimax_rfkill) {
1361 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WIMAX);
1362 rfkill_set_sw_state(eeepc->wimax_rfkill, bl);
1363 }
Corentin Chary0773d7f2011-02-06 13:28:32 +01001364 if (eeepc->wwan3g_rfkill) {
1365 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1366 rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1367 }
1368
1369 return 0;
1370}
1371
1372static const struct dev_pm_ops eeepc_pm_ops = {
1373 .thaw = eeepc_hotk_thaw,
1374 .restore = eeepc_hotk_restore,
1375};
1376
Yong Wang45f2c692010-04-11 09:27:19 +08001377static struct platform_driver platform_driver = {
Corentin Charya04ce292011-02-06 13:28:33 +01001378 .remove = __exit_p(eeepc_wmi_remove),
Yong Wang45f2c692010-04-11 09:27:19 +08001379 .driver = {
1380 .name = EEEPC_WMI_FILE,
1381 .owner = THIS_MODULE,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001382 .pm = &eeepc_pm_ops,
Yong Wang45f2c692010-04-11 09:27:19 +08001383 },
Yong Wang45f2c692010-04-11 09:27:19 +08001384};
1385
Corentin Charyd358cb52010-11-29 08:14:14 +01001386static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1387 void *context, void **retval)
1388{
1389 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1390 *(bool *)context = true;
1391 return AE_CTRL_TERMINATE;
1392}
1393
1394static int __init eeepc_wmi_check_atkd(void)
1395{
1396 acpi_status status;
1397 bool found = false;
1398
1399 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1400 &found, NULL);
1401
1402 if (ACPI_FAILURE(status) || !found)
1403 return 0;
1404 return -1;
1405}
1406
Corentin Charya04ce292011-02-06 13:28:33 +01001407static int __init eeepc_wmi_probe(struct platform_device *pdev)
Yong Wang45f2c692010-04-11 09:27:19 +08001408{
Yong Wang3d7b1652010-04-11 09:27:54 +08001409 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1410 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +08001411 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001412 return -ENODEV;
1413 }
1414
Corentin Charyd358cb52010-11-29 08:14:14 +01001415 if (eeepc_wmi_check_atkd()) {
1416 pr_warning("WMI device present, but legacy ATKD device is also "
1417 "present and enabled.");
1418 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1419 "acpi_osi=\"!Windows 2009\"");
1420 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1421 "(preferred) or eeepc-laptop");
1422 return -ENODEV;
1423 }
1424
Corentin Charya04ce292011-02-06 13:28:33 +01001425 return eeepc_wmi_add(pdev);
1426}
Yong Wangee027e42010-03-21 10:26:34 +08001427
Corentin Charya04ce292011-02-06 13:28:33 +01001428static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +08001429
Corentin Charya04ce292011-02-06 13:28:33 +01001430static int __init eeepc_wmi_init(void)
1431{
1432 platform_device = platform_create_bundle(&platform_driver,
1433 eeepc_wmi_probe,
1434 NULL, 0, NULL, 0);
1435 if (IS_ERR(platform_device))
1436 return PTR_ERR(platform_device);
Yong Wangee027e42010-03-21 10:26:34 +08001437 return 0;
1438}
1439
1440static void __exit eeepc_wmi_exit(void)
1441{
Corentin Charya04ce292011-02-06 13:28:33 +01001442 platform_device_unregister(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +08001443 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +08001444}
1445
1446module_init(eeepc_wmi_init);
1447module_exit(eeepc_wmi_exit);