blob: f457587e64d3bdb7c6121e6c9c83fb4986ef3bbd [file] [log] [blame]
Eric Coopere59f8792008-03-13 12:55:46 +01001/*
2 * eepc-laptop.c - Asus Eee PC extras
3 *
4 * Based on asus_acpi.c as patched for the Eee PC by Asus:
5 * ftp://ftp.asus.com/pub/ASUS/EeePC/701/ASUS_ACPI_071126.rar
6 * Based on eee.c from eeepc-linux
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
Joe Perches19b53282009-06-25 13:25:37 +020019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Eric Coopere59f8792008-03-13 12:55:46 +010021#include <linux/kernel.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/types.h>
25#include <linux/platform_device.h>
Corentin Charya5fa4292008-03-13 12:56:37 +010026#include <linux/backlight.h>
27#include <linux/fb.h>
Corentin Charye1faa9d2008-03-13 12:57:18 +010028#include <linux/hwmon.h>
29#include <linux/hwmon-sysfs.h>
Eric Coopere59f8792008-03-13 12:55:46 +010030#include <acpi/acpi_drivers.h>
31#include <acpi/acpi_bus.h>
32#include <linux/uaccess.h>
Matthew Garretta195dcd2008-08-19 12:13:20 +010033#include <linux/input.h>
34#include <linux/rfkill.h>
Matthew Garrett57402942009-01-20 16:17:48 +010035#include <linux/pci.h>
Corentin Chary2b121bc2009-06-25 13:25:36 +020036#include <linux/pci_hotplug.h>
Corentin Chary3c0eb512009-12-03 07:44:52 +000037#include <linux/leds.h>
Eric Coopere59f8792008-03-13 12:55:46 +010038
39#define EEEPC_LAPTOP_VERSION "0.1"
40
41#define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
42#define EEEPC_HOTK_FILE "eeepc"
43#define EEEPC_HOTK_CLASS "hotkey"
44#define EEEPC_HOTK_DEVICE_NAME "Hotkey"
45#define EEEPC_HOTK_HID "ASUS010"
46
Alan Jenkins52bbe3c2009-12-03 07:45:07 +000047MODULE_AUTHOR("Corentin Chary, Eric Cooper");
48MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
49MODULE_LICENSE("GPL");
Eric Coopere59f8792008-03-13 12:55:46 +010050
51/*
52 * Definitions for Asus EeePC
53 */
54#define NOTIFY_WLAN_ON 0x10
Corentin Charya5fa4292008-03-13 12:56:37 +010055#define NOTIFY_BRN_MIN 0x20
56#define NOTIFY_BRN_MAX 0x2f
Eric Coopere59f8792008-03-13 12:55:46 +010057
58enum {
59 DISABLE_ASL_WLAN = 0x0001,
60 DISABLE_ASL_BLUETOOTH = 0x0002,
61 DISABLE_ASL_IRDA = 0x0004,
62 DISABLE_ASL_CAMERA = 0x0008,
63 DISABLE_ASL_TV = 0x0010,
64 DISABLE_ASL_GPS = 0x0020,
65 DISABLE_ASL_DISPLAYSWITCH = 0x0040,
66 DISABLE_ASL_MODEM = 0x0080,
Corentin Charyb7b700d2009-06-16 19:28:52 +000067 DISABLE_ASL_CARDREADER = 0x0100,
68 DISABLE_ASL_3G = 0x0200,
69 DISABLE_ASL_WIMAX = 0x0400,
70 DISABLE_ASL_HWCF = 0x0800
Eric Coopere59f8792008-03-13 12:55:46 +010071};
72
73enum {
74 CM_ASL_WLAN = 0,
75 CM_ASL_BLUETOOTH,
76 CM_ASL_IRDA,
77 CM_ASL_1394,
78 CM_ASL_CAMERA,
79 CM_ASL_TV,
80 CM_ASL_GPS,
81 CM_ASL_DVDROM,
82 CM_ASL_DISPLAYSWITCH,
83 CM_ASL_PANELBRIGHT,
84 CM_ASL_BIOSFLASH,
85 CM_ASL_ACPIFLASH,
86 CM_ASL_CPUFV,
87 CM_ASL_CPUTEMPERATURE,
88 CM_ASL_FANCPU,
89 CM_ASL_FANCHASSIS,
90 CM_ASL_USBPORT1,
91 CM_ASL_USBPORT2,
92 CM_ASL_USBPORT3,
93 CM_ASL_MODEM,
94 CM_ASL_CARDREADER,
Corentin Charyb7b700d2009-06-16 19:28:52 +000095 CM_ASL_3G,
96 CM_ASL_WIMAX,
97 CM_ASL_HWCF,
98 CM_ASL_LID,
99 CM_ASL_TYPE,
100 CM_ASL_PANELPOWER, /*P901*/
101 CM_ASL_TPD
Eric Coopere59f8792008-03-13 12:55:46 +0100102};
103
Adrian Bunk14109462008-06-25 19:25:47 +0300104static const char *cm_getv[] = {
Jonathan McDowell3af9bfc2008-12-03 20:31:11 +0000105 "WLDG", "BTHG", NULL, NULL,
Eric Coopere59f8792008-03-13 12:55:46 +0100106 "CAMG", NULL, NULL, NULL,
107 NULL, "PBLG", NULL, NULL,
108 "CFVG", NULL, NULL, NULL,
109 "USBG", NULL, NULL, "MODG",
Corentin Charyb7b700d2009-06-16 19:28:52 +0000110 "CRDG", "M3GG", "WIMG", "HWCF",
111 "LIDG", "TYPE", "PBPG", "TPDG"
Eric Coopere59f8792008-03-13 12:55:46 +0100112};
113
Adrian Bunk14109462008-06-25 19:25:47 +0300114static const char *cm_setv[] = {
Jonathan McDowell3af9bfc2008-12-03 20:31:11 +0000115 "WLDS", "BTHS", NULL, NULL,
Eric Coopere59f8792008-03-13 12:55:46 +0100116 "CAMS", NULL, NULL, NULL,
117 "SDSP", "PBLS", "HDPS", NULL,
118 "CFVS", NULL, NULL, NULL,
119 "USBG", NULL, NULL, "MODS",
Corentin Charyb7b700d2009-06-16 19:28:52 +0000120 "CRDS", "M3GS", "WIMS", NULL,
121 NULL, NULL, "PBPS", "TPDS"
Eric Coopere59f8792008-03-13 12:55:46 +0100122};
123
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000124struct key_entry {
125 char type;
126 u8 code;
127 u16 keycode;
128};
Corentin Charye1faa9d2008-03-13 12:57:18 +0100129
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000130enum { KE_KEY, KE_END };
131
132static struct key_entry eeepc_keymap[] = {
133 /* Sleep already handled via generic ACPI code */
134 {KE_KEY, 0x10, KEY_WLAN },
135 {KE_KEY, 0x11, KEY_WLAN },
136 {KE_KEY, 0x12, KEY_PROG1 },
137 {KE_KEY, 0x13, KEY_MUTE },
138 {KE_KEY, 0x14, KEY_VOLUMEDOWN },
139 {KE_KEY, 0x15, KEY_VOLUMEUP },
140 {KE_KEY, 0x1a, KEY_COFFEE },
141 {KE_KEY, 0x1b, KEY_ZOOM },
142 {KE_KEY, 0x1c, KEY_PROG2 },
143 {KE_KEY, 0x1d, KEY_PROG3 },
144 {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
145 {KE_KEY, NOTIFY_BRN_MAX, KEY_BRIGHTNESSUP },
146 {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
147 {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
148 {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
149 {KE_END, 0},
150};
Alan Jenkins463b4e42009-12-03 07:45:03 +0000151
Corentin Charye1faa9d2008-03-13 12:57:18 +0100152
Eric Coopere59f8792008-03-13 12:55:46 +0100153/*
154 * This is the main structure, we can use it to store useful information
155 * about the hotk device
156 */
157struct eeepc_hotk {
158 struct acpi_device *device; /* the device we are in */
159 acpi_handle handle; /* the handle of the hotk device */
160 u32 cm_supported; /* the control methods supported
161 by this BIOS */
Eric Coopere59f8792008-03-13 12:55:46 +0100162 u16 event_count[128]; /* count for each event */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100163 struct input_dev *inputdev;
164 u16 *keycode_map;
Corentin Chary7de39382009-06-25 13:25:38 +0200165 struct rfkill *wlan_rfkill;
166 struct rfkill *bluetooth_rfkill;
Corentin Chary3cd530b2009-06-25 13:25:42 +0200167 struct rfkill *wwan3g_rfkill;
Corentin Charyd1ec9c32009-08-28 12:56:41 +0000168 struct rfkill *wimax_rfkill;
Corentin Chary2b121bc2009-06-25 13:25:36 +0200169 struct hotplug_slot *hotplug_slot;
Alan Jenkinsdcf443b2009-08-28 12:56:33 +0000170 struct mutex hotplug_lock;
Eric Coopere59f8792008-03-13 12:55:46 +0100171};
172
173/* The actual device the driver binds to */
174static struct eeepc_hotk *ehotk;
175
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000176/* The platform device */
Eric Coopere59f8792008-03-13 12:55:46 +0100177static struct platform_device *platform_device;
178
Corentin Charya5fa4292008-03-13 12:56:37 +0100179/* The backlight device /sys/class/backlight */
180static struct backlight_device *eeepc_backlight_device;
181
Corentin Charye1faa9d2008-03-13 12:57:18 +0100182/* The hwmon device */
183static struct device *eeepc_hwmon_device;
184
Eric Coopere59f8792008-03-13 12:55:46 +0100185
186/*
187 * ACPI Helpers
188 */
Alan Jenkins6b188a72009-12-03 07:45:02 +0000189static int write_acpi_int(acpi_handle handle, const char *method, int val)
Eric Coopere59f8792008-03-13 12:55:46 +0100190{
191 struct acpi_object_list params;
192 union acpi_object in_obj;
193 acpi_status status;
194
195 params.count = 1;
196 params.pointer = &in_obj;
197 in_obj.type = ACPI_TYPE_INTEGER;
198 in_obj.integer.value = val;
199
Alan Jenkins6b188a72009-12-03 07:45:02 +0000200 status = acpi_evaluate_object(handle, (char *)method, &params, NULL);
Eric Coopere59f8792008-03-13 12:55:46 +0100201 return (status == AE_OK ? 0 : -1);
202}
203
204static int read_acpi_int(acpi_handle handle, const char *method, int *val)
205{
206 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400207 unsigned long long result;
Eric Coopere59f8792008-03-13 12:55:46 +0100208
209 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
210 if (ACPI_FAILURE(status)) {
211 *val = -1;
212 return -1;
213 } else {
214 *val = result;
215 return 0;
216 }
217}
218
219static int set_acpi(int cm, int value)
220{
Alan Jenkins13f70022009-12-03 07:44:59 +0000221 const char *method = cm_setv[cm];
222
223 if (method == NULL)
224 return -ENODEV;
225 if ((ehotk->cm_supported & (0x1 << cm)) == 0)
226 return -ENODEV;
227
Alan Jenkins6b188a72009-12-03 07:45:02 +0000228 if (write_acpi_int(ehotk->handle, method, value))
Alan Jenkins13f70022009-12-03 07:44:59 +0000229 pr_warning("Error writing %s\n", method);
Eric Coopere59f8792008-03-13 12:55:46 +0100230 return 0;
231}
232
233static int get_acpi(int cm)
234{
Alan Jenkins13f70022009-12-03 07:44:59 +0000235 const char *method = cm_getv[cm];
236 int value;
237
238 if (method == NULL)
239 return -ENODEV;
240 if ((ehotk->cm_supported & (0x1 << cm)) == 0)
241 return -ENODEV;
242
243 if (read_acpi_int(ehotk->handle, method, &value))
244 pr_warning("Error reading %s\n", method);
Eric Coopere59f8792008-03-13 12:55:46 +0100245 return value;
246}
247
248/*
249 * Sys helpers
250 */
251static int parse_arg(const char *buf, unsigned long count, int *val)
252{
253 if (!count)
254 return 0;
255 if (sscanf(buf, "%i", val) != 1)
256 return -EINVAL;
257 return count;
258}
259
260static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
261{
262 int rv, value;
263
264 rv = parse_arg(buf, count, &value);
265 if (rv > 0)
Corentin Charyf36509e2009-06-25 13:25:40 +0200266 value = set_acpi(cm, value);
267 if (value < 0)
Alan Jenkins6dff29b2009-12-03 07:44:45 +0000268 return -EIO;
Eric Coopere59f8792008-03-13 12:55:46 +0100269 return rv;
270}
271
272static ssize_t show_sys_acpi(int cm, char *buf)
273{
Corentin Charyf36509e2009-06-25 13:25:40 +0200274 int value = get_acpi(cm);
275
276 if (value < 0)
Alan Jenkins6dff29b2009-12-03 07:44:45 +0000277 return -EIO;
Corentin Charyf36509e2009-06-25 13:25:40 +0200278 return sprintf(buf, "%d\n", value);
Eric Coopere59f8792008-03-13 12:55:46 +0100279}
280
Alan Jenkins6dff29b2009-12-03 07:44:45 +0000281#define EEEPC_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Eric Coopere59f8792008-03-13 12:55:46 +0100282 static ssize_t show_##_name(struct device *dev, \
283 struct device_attribute *attr, \
284 char *buf) \
285 { \
286 return show_sys_acpi(_cm, buf); \
287 } \
288 static ssize_t store_##_name(struct device *dev, \
289 struct device_attribute *attr, \
290 const char *buf, size_t count) \
291 { \
292 return store_sys_acpi(_cm, buf, count); \
293 } \
294 static struct device_attribute dev_attr_##_name = { \
295 .attr = { \
296 .name = __stringify(_name), \
Alan Jenkins6dff29b2009-12-03 07:44:45 +0000297 .mode = _mode }, \
Eric Coopere59f8792008-03-13 12:55:46 +0100298 .show = show_##_name, \
299 .store = store_##_name, \
300 }
301
Alan Jenkins6dff29b2009-12-03 07:44:45 +0000302EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA);
303EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);
304EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
Corentin Charyb31d0fd2009-06-16 19:28:56 +0000305
306struct eeepc_cpufv {
307 int num;
308 int cur;
309};
310
311static int get_cpufv(struct eeepc_cpufv *c)
312{
313 c->cur = get_acpi(CM_ASL_CPUFV);
314 c->num = (c->cur >> 8) & 0xff;
315 c->cur &= 0xff;
316 if (c->cur < 0 || c->num <= 0 || c->num > 12)
317 return -ENODEV;
318 return 0;
319}
320
321static ssize_t show_available_cpufv(struct device *dev,
322 struct device_attribute *attr,
323 char *buf)
324{
325 struct eeepc_cpufv c;
326 int i;
327 ssize_t len = 0;
328
329 if (get_cpufv(&c))
330 return -ENODEV;
331 for (i = 0; i < c.num; i++)
332 len += sprintf(buf + len, "%d ", i);
333 len += sprintf(buf + len, "\n");
334 return len;
335}
336
337static ssize_t show_cpufv(struct device *dev,
338 struct device_attribute *attr,
339 char *buf)
340{
341 struct eeepc_cpufv c;
342
343 if (get_cpufv(&c))
344 return -ENODEV;
345 return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
346}
347
348static ssize_t store_cpufv(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf, size_t count)
351{
352 struct eeepc_cpufv c;
353 int rv, value;
354
355 if (get_cpufv(&c))
356 return -ENODEV;
357 rv = parse_arg(buf, count, &value);
358 if (rv < 0)
359 return rv;
360 if (!rv || value < 0 || value >= c.num)
361 return -EINVAL;
362 set_acpi(CM_ASL_CPUFV, value);
363 return rv;
364}
365
366static struct device_attribute dev_attr_cpufv = {
367 .attr = {
368 .name = "cpufv",
369 .mode = 0644 },
370 .show = show_cpufv,
371 .store = store_cpufv
372};
373
374static struct device_attribute dev_attr_available_cpufv = {
375 .attr = {
376 .name = "available_cpufv",
377 .mode = 0444 },
378 .show = show_available_cpufv
379};
Eric Coopere59f8792008-03-13 12:55:46 +0100380
381static struct attribute *platform_attributes[] = {
382 &dev_attr_camera.attr,
383 &dev_attr_cardr.attr,
384 &dev_attr_disp.attr,
Grigori Goronzy158ca1d2009-04-27 09:23:40 +0200385 &dev_attr_cpufv.attr,
Corentin Charyb31d0fd2009-06-16 19:28:56 +0000386 &dev_attr_available_cpufv.attr,
Eric Coopere59f8792008-03-13 12:55:46 +0100387 NULL
388};
389
390static struct attribute_group platform_attribute_group = {
391 .attrs = platform_attributes
392};
393
Alan Jenkins9db106b2009-12-03 07:45:06 +0000394static int eeepc_platform_init(void)
395{
396 int result;
397
398 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
399 if (!platform_device)
400 return -ENOMEM;
401
402 result = platform_device_add(platform_device);
403 if (result)
404 goto fail_platform_device;
405
406 result = sysfs_create_group(&platform_device->dev.kobj,
407 &platform_attribute_group);
408 if (result)
409 goto fail_sysfs;
410 return 0;
411
412fail_sysfs:
413 platform_device_del(platform_device);
414fail_platform_device:
415 platform_device_put(platform_device);
416 return result;
417}
418
419static void eeepc_platform_exit(void)
420{
421 sysfs_remove_group(&platform_device->dev.kobj,
422 &platform_attribute_group);
423 platform_device_unregister(platform_device);
424}
425
Eric Coopere59f8792008-03-13 12:55:46 +0100426/*
Corentin Chary3c0eb512009-12-03 07:44:52 +0000427 * LEDs
428 */
429/*
430 * These functions actually update the LED's, and are called from a
431 * workqueue. By doing this as separate work rather than when the LED
432 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
433 * potentially bad time, such as a timer interrupt.
434 */
435static int tpd_led_wk;
436
437static void tpd_led_update(struct work_struct *ignored)
438{
439 int value = tpd_led_wk;
440 set_acpi(CM_ASL_TPD, value);
441}
442
443static struct workqueue_struct *led_workqueue;
444static DECLARE_WORK(tpd_led_work, tpd_led_update);
445
446static void tpd_led_set(struct led_classdev *led_cdev,
447 enum led_brightness value)
448{
449 tpd_led_wk = (value > 0) ? 1 : 0;
450 queue_work(led_workqueue, &tpd_led_work);
451}
452
453static struct led_classdev tpd_led = {
454 .name = "eeepc::touchpad",
455 .brightness_set = tpd_led_set,
456 .max_brightness = 1
457};
458
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000459static int eeepc_led_init(struct device *dev)
460{
461 int rv;
462
463 if (get_acpi(CM_ASL_TPD) == -ENODEV)
464 return 0;
465
466 led_workqueue = create_singlethread_workqueue("led_workqueue");
467 if (!led_workqueue)
468 return -ENOMEM;
469
470 rv = led_classdev_register(dev, &tpd_led);
471 if (rv) {
472 destroy_workqueue(led_workqueue);
473 return rv;
474 }
475
476 return 0;
477}
478
479static void eeepc_led_exit(void)
480{
481 if (tpd_led.dev)
482 led_classdev_unregister(&tpd_led);
483 if (led_workqueue)
484 destroy_workqueue(led_workqueue);
485}
486
487
Corentin Chary3c0eb512009-12-03 07:44:52 +0000488/*
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000489 * PCI hotplug (for wlan rfkill)
Eric Coopere59f8792008-03-13 12:55:46 +0100490 */
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000491static bool eeepc_wlan_rfkill_blocked(void)
Matthew Garretta195dcd2008-08-19 12:13:20 +0100492{
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000493 if (get_acpi(CM_ASL_WLAN) == 1)
494 return false;
495 return true;
Corentin Chary2b121bc2009-06-25 13:25:36 +0200496}
497
Corentin Chary58ce48a2009-10-16 22:22:46 +0200498static void eeepc_rfkill_hotplug(void)
Matthew Garrett57402942009-01-20 16:17:48 +0100499{
500 struct pci_dev *dev;
Alan Jenkins6d418392009-08-28 12:56:32 +0000501 struct pci_bus *bus;
Corentin Chary58ce48a2009-10-16 22:22:46 +0200502 bool blocked = eeepc_wlan_rfkill_blocked();
Matthew Garrett57402942009-01-20 16:17:48 +0100503
Corentin Chary58ce48a2009-10-16 22:22:46 +0200504 if (ehotk->wlan_rfkill)
Alan Jenkins07e84aa2009-08-28 12:56:34 +0000505 rfkill_set_sw_state(ehotk->wlan_rfkill, blocked);
Alan Jenkins6d418392009-08-28 12:56:32 +0000506
Alan Jenkinsdcf443b2009-08-28 12:56:33 +0000507 mutex_lock(&ehotk->hotplug_lock);
508
Alan Jenkins07e84aa2009-08-28 12:56:34 +0000509 if (ehotk->hotplug_slot) {
510 bus = pci_find_bus(0, 1);
511 if (!bus) {
512 pr_warning("Unable to find PCI bus 1?\n");
Alan Jenkinsdcf443b2009-08-28 12:56:33 +0000513 goto out_unlock;
Matthew Garrett57402942009-01-20 16:17:48 +0100514 }
Alan Jenkins07e84aa2009-08-28 12:56:34 +0000515
516 if (!blocked) {
517 dev = pci_get_slot(bus, 0);
518 if (dev) {
519 /* Device already present */
520 pci_dev_put(dev);
521 goto out_unlock;
522 }
523 dev = pci_scan_single_device(bus, 0);
524 if (dev) {
525 pci_bus_assign_resources(bus);
526 if (pci_bus_add_device(dev))
527 pr_err("Unable to hotplug wifi\n");
528 }
529 } else {
530 dev = pci_get_slot(bus, 0);
531 if (dev) {
532 pci_remove_bus_device(dev);
533 pci_dev_put(dev);
534 }
Matthew Garrett57402942009-01-20 16:17:48 +0100535 }
536 }
Alan Jenkinsdcf443b2009-08-28 12:56:33 +0000537
538out_unlock:
539 mutex_unlock(&ehotk->hotplug_lock);
Matthew Garrett57402942009-01-20 16:17:48 +0100540}
541
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100542static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
543{
544 if (event != ACPI_NOTIFY_BUS_CHECK)
545 return;
546
Corentin Chary58ce48a2009-10-16 22:22:46 +0200547 eeepc_rfkill_hotplug();
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100548}
549
Matthew Garrett57402942009-01-20 16:17:48 +0100550static int eeepc_register_rfkill_notifier(char *node)
551{
552 acpi_status status = AE_OK;
553 acpi_handle handle;
554
555 status = acpi_get_handle(NULL, node, &handle);
556
557 if (ACPI_SUCCESS(status)) {
558 status = acpi_install_notify_handler(handle,
559 ACPI_SYSTEM_NOTIFY,
560 eeepc_rfkill_notify,
561 NULL);
562 if (ACPI_FAILURE(status))
Joe Perches19b53282009-06-25 13:25:37 +0200563 pr_warning("Failed to register notify on %s\n", node);
Matthew Garrett57402942009-01-20 16:17:48 +0100564 } else
565 return -ENODEV;
566
567 return 0;
568}
569
570static void eeepc_unregister_rfkill_notifier(char *node)
571{
572 acpi_status status = AE_OK;
573 acpi_handle handle;
574
575 status = acpi_get_handle(NULL, node, &handle);
576
577 if (ACPI_SUCCESS(status)) {
578 status = acpi_remove_notify_handler(handle,
579 ACPI_SYSTEM_NOTIFY,
580 eeepc_rfkill_notify);
581 if (ACPI_FAILURE(status))
Joe Perches19b53282009-06-25 13:25:37 +0200582 pr_err("Error removing rfkill notify handler %s\n",
Matthew Garrett57402942009-01-20 16:17:48 +0100583 node);
584 }
585}
586
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000587static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
588 u8 *value)
589{
590 int val = get_acpi(CM_ASL_WLAN);
591
592 if (val == 1 || val == 0)
593 *value = val;
594 else
595 return -EINVAL;
596
597 return 0;
598}
599
Corentin Chary2b121bc2009-06-25 13:25:36 +0200600static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
601{
602 kfree(hotplug_slot->info);
603 kfree(hotplug_slot);
604}
605
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000606static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
607 .owner = THIS_MODULE,
608 .get_adapter_status = eeepc_get_adapter_status,
609 .get_power_status = eeepc_get_adapter_status,
610};
611
Corentin Chary2b121bc2009-06-25 13:25:36 +0200612static int eeepc_setup_pci_hotplug(void)
613{
614 int ret = -ENOMEM;
615 struct pci_bus *bus = pci_find_bus(0, 1);
616
617 if (!bus) {
Joe Perches19b53282009-06-25 13:25:37 +0200618 pr_err("Unable to find wifi PCI bus\n");
Corentin Chary2b121bc2009-06-25 13:25:36 +0200619 return -ENODEV;
620 }
621
622 ehotk->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
623 if (!ehotk->hotplug_slot)
624 goto error_slot;
625
626 ehotk->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
627 GFP_KERNEL);
628 if (!ehotk->hotplug_slot->info)
629 goto error_info;
630
631 ehotk->hotplug_slot->private = ehotk;
632 ehotk->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
633 ehotk->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
634 eeepc_get_adapter_status(ehotk->hotplug_slot,
635 &ehotk->hotplug_slot->info->adapter_status);
636
637 ret = pci_hp_register(ehotk->hotplug_slot, bus, 0, "eeepc-wifi");
638 if (ret) {
Joe Perches19b53282009-06-25 13:25:37 +0200639 pr_err("Unable to register hotplug slot - %d\n", ret);
Corentin Chary2b121bc2009-06-25 13:25:36 +0200640 goto error_register;
641 }
642
643 return 0;
644
645error_register:
646 kfree(ehotk->hotplug_slot->info);
647error_info:
648 kfree(ehotk->hotplug_slot);
649 ehotk->hotplug_slot = NULL;
650error_slot:
651 return ret;
652}
653
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000654/*
655 * Rfkill devices
656 */
657static int eeepc_rfkill_set(void *data, bool blocked)
658{
659 unsigned long asl = (unsigned long)data;
660 return set_acpi(asl, !blocked);
661}
662
663static const struct rfkill_ops eeepc_rfkill_ops = {
664 .set_block = eeepc_rfkill_set,
665};
666
667static int eeepc_new_rfkill(struct rfkill **rfkill,
668 const char *name, struct device *dev,
669 enum rfkill_type type, int cm)
670{
671 int result;
672
673 result = get_acpi(cm);
674 if (result < 0)
675 return result;
676
677 *rfkill = rfkill_alloc(name, dev, type,
678 &eeepc_rfkill_ops, (void *)(unsigned long)cm);
679
680 if (!*rfkill)
681 return -EINVAL;
682
683 rfkill_init_sw_state(*rfkill, get_acpi(cm) != 1);
684 result = rfkill_register(*rfkill);
685 if (result) {
686 rfkill_destroy(*rfkill);
687 *rfkill = NULL;
688 return result;
689 }
690 return 0;
691}
692
693static void eeepc_rfkill_exit(void)
694{
695 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P5");
696 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
697 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
698 if (ehotk->wlan_rfkill) {
699 rfkill_unregister(ehotk->wlan_rfkill);
700 rfkill_destroy(ehotk->wlan_rfkill);
701 ehotk->wlan_rfkill = NULL;
702 }
703 /*
704 * Refresh pci hotplug in case the rfkill state was changed after
705 * eeepc_unregister_rfkill_notifier()
706 */
707 eeepc_rfkill_hotplug();
708 if (ehotk->hotplug_slot)
709 pci_hp_deregister(ehotk->hotplug_slot);
710
711 if (ehotk->bluetooth_rfkill) {
712 rfkill_unregister(ehotk->bluetooth_rfkill);
713 rfkill_destroy(ehotk->bluetooth_rfkill);
714 ehotk->bluetooth_rfkill = NULL;
715 }
716 if (ehotk->wwan3g_rfkill) {
717 rfkill_unregister(ehotk->wwan3g_rfkill);
718 rfkill_destroy(ehotk->wwan3g_rfkill);
719 ehotk->wwan3g_rfkill = NULL;
720 }
721 if (ehotk->wimax_rfkill) {
722 rfkill_unregister(ehotk->wimax_rfkill);
723 rfkill_destroy(ehotk->wimax_rfkill);
724 ehotk->wimax_rfkill = NULL;
725 }
726}
727
728static int eeepc_rfkill_init(struct device *dev)
729{
730 int result = 0;
731
732 mutex_init(&ehotk->hotplug_lock);
733
734 result = eeepc_new_rfkill(&ehotk->wlan_rfkill,
735 "eeepc-wlan", dev,
736 RFKILL_TYPE_WLAN, CM_ASL_WLAN);
737
738 if (result && result != -ENODEV)
739 goto exit;
740
741 result = eeepc_new_rfkill(&ehotk->bluetooth_rfkill,
742 "eeepc-bluetooth", dev,
743 RFKILL_TYPE_BLUETOOTH, CM_ASL_BLUETOOTH);
744
745 if (result && result != -ENODEV)
746 goto exit;
747
748 result = eeepc_new_rfkill(&ehotk->wwan3g_rfkill,
749 "eeepc-wwan3g", dev,
750 RFKILL_TYPE_WWAN, CM_ASL_3G);
751
752 if (result && result != -ENODEV)
753 goto exit;
754
755 result = eeepc_new_rfkill(&ehotk->wimax_rfkill,
756 "eeepc-wimax", dev,
757 RFKILL_TYPE_WIMAX, CM_ASL_WIMAX);
758
759 if (result && result != -ENODEV)
760 goto exit;
761
762 result = eeepc_setup_pci_hotplug();
763 /*
764 * If we get -EBUSY then something else is handling the PCI hotplug -
765 * don't fail in this case
766 */
767 if (result == -EBUSY)
768 result = 0;
769
770 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P5");
771 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
772 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
773 /*
774 * Refresh pci hotplug in case the rfkill state was changed during
775 * setup.
776 */
777 eeepc_rfkill_hotplug();
778
779exit:
780 if (result && result != -ENODEV)
781 eeepc_rfkill_exit();
782 return result;
783}
784
785/*
786 * Platform driver - hibernate/resume callbacks
787 */
Alan Jenkinsc200da52009-08-28 12:56:40 +0000788static int eeepc_hotk_thaw(struct device *device)
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100789{
Corentin Chary7de39382009-06-25 13:25:38 +0200790 if (ehotk->wlan_rfkill) {
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100791 bool wlan;
792
Alan Jenkinsc1edd992009-08-28 12:56:39 +0000793 /*
794 * Work around bios bug - acpi _PTS turns off the wireless led
795 * during suspend. Normally it restores it on resume, but
Alan Jenkinsc200da52009-08-28 12:56:40 +0000796 * we should kick it ourselves in case hibernation is aborted.
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100797 */
798 wlan = get_acpi(CM_ASL_WLAN);
799 set_acpi(CM_ASL_WLAN, wlan);
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100800 }
801
Alan Jenkinsc200da52009-08-28 12:56:40 +0000802 return 0;
803}
804
805static int eeepc_hotk_restore(struct device *device)
806{
807 /* Refresh both wlan rfkill state and pci hotplug */
808 if (ehotk->wlan_rfkill)
Corentin Chary58ce48a2009-10-16 22:22:46 +0200809 eeepc_rfkill_hotplug();
Alan Jenkinsc200da52009-08-28 12:56:40 +0000810
Corentin Chary7de39382009-06-25 13:25:38 +0200811 if (ehotk->bluetooth_rfkill)
812 rfkill_set_sw_state(ehotk->bluetooth_rfkill,
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100813 get_acpi(CM_ASL_BLUETOOTH) != 1);
Alan Jenkinsa4746102009-08-28 12:56:38 +0000814 if (ehotk->wwan3g_rfkill)
815 rfkill_set_sw_state(ehotk->wwan3g_rfkill,
816 get_acpi(CM_ASL_3G) != 1);
Corentin Charyd1ec9c32009-08-28 12:56:41 +0000817 if (ehotk->wimax_rfkill)
818 rfkill_set_sw_state(ehotk->wimax_rfkill,
819 get_acpi(CM_ASL_WIMAX) != 1);
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100820
821 return 0;
822}
823
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000824static struct dev_pm_ops eeepc_pm_ops = {
825 .thaw = eeepc_hotk_thaw,
826 .restore = eeepc_hotk_restore,
827};
828
829static struct platform_driver platform_driver = {
830 .driver = {
831 .name = EEEPC_HOTK_FILE,
832 .owner = THIS_MODULE,
833 .pm = &eeepc_pm_ops,
834 }
835};
836
Eric Coopere59f8792008-03-13 12:55:46 +0100837/*
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000838 * Hwmon device
Corentin Charye1faa9d2008-03-13 12:57:18 +0100839 */
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000840
841#define EEEPC_EC_SC00 0x61
842#define EEEPC_EC_FAN_PWM (EEEPC_EC_SC00 + 2) /* Fan PWM duty cycle (%) */
843#define EEEPC_EC_FAN_HRPM (EEEPC_EC_SC00 + 5) /* High byte, fan speed (RPM) */
844#define EEEPC_EC_FAN_LRPM (EEEPC_EC_SC00 + 6) /* Low byte, fan speed (RPM) */
845
846#define EEEPC_EC_SFB0 0xD0
847#define EEEPC_EC_FAN_CTRL (EEEPC_EC_SFB0 + 3) /* Byte containing SF25 */
848
Corentin Charye1faa9d2008-03-13 12:57:18 +0100849static int eeepc_get_fan_pwm(void)
850{
Alan Jenkins463b4e42009-12-03 07:45:03 +0000851 u8 value = 0;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100852
Alan Jenkins463b4e42009-12-03 07:45:03 +0000853 ec_read(EEEPC_EC_FAN_PWM, &value);
854 return value * 255 / 100;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100855}
856
857static void eeepc_set_fan_pwm(int value)
858{
Corentin Chary04dcd842008-10-09 15:33:57 +0200859 value = SENSORS_LIMIT(value, 0, 255);
860 value = value * 100 / 255;
Alan Jenkins463b4e42009-12-03 07:45:03 +0000861 ec_write(EEEPC_EC_FAN_PWM, value);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100862}
863
864static int eeepc_get_fan_rpm(void)
865{
Alan Jenkins463b4e42009-12-03 07:45:03 +0000866 u8 high = 0;
867 u8 low = 0;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100868
Alan Jenkins463b4e42009-12-03 07:45:03 +0000869 ec_read(EEEPC_EC_FAN_HRPM, &high);
870 ec_read(EEEPC_EC_FAN_LRPM, &low);
871 return high << 8 | low;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100872}
873
874static int eeepc_get_fan_ctrl(void)
875{
Alan Jenkins463b4e42009-12-03 07:45:03 +0000876 u8 value = 0;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100877
Alan Jenkins463b4e42009-12-03 07:45:03 +0000878 ec_read(EEEPC_EC_FAN_CTRL, &value);
Alan Jenkins48718682009-12-03 07:44:56 +0000879 if (value & 0x02)
880 return 1; /* manual */
881 else
882 return 2; /* automatic */
Corentin Charye1faa9d2008-03-13 12:57:18 +0100883}
884
885static void eeepc_set_fan_ctrl(int manual)
886{
Alan Jenkins463b4e42009-12-03 07:45:03 +0000887 u8 value = 0;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100888
Alan Jenkins463b4e42009-12-03 07:45:03 +0000889 ec_read(EEEPC_EC_FAN_CTRL, &value);
Alan Jenkins48718682009-12-03 07:44:56 +0000890 if (manual == 1)
Corentin Charye1faa9d2008-03-13 12:57:18 +0100891 value |= 0x02;
892 else
893 value &= ~0x02;
Alan Jenkins463b4e42009-12-03 07:45:03 +0000894 ec_write(EEEPC_EC_FAN_CTRL, value);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100895}
896
897static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
898{
899 int rv, value;
900
901 rv = parse_arg(buf, count, &value);
902 if (rv > 0)
903 set(value);
904 return rv;
905}
906
907static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
908{
909 return sprintf(buf, "%d\n", get());
910}
911
912#define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
913 static ssize_t show_##_name(struct device *dev, \
914 struct device_attribute *attr, \
915 char *buf) \
916 { \
917 return show_sys_hwmon(_set, buf); \
918 } \
919 static ssize_t store_##_name(struct device *dev, \
920 struct device_attribute *attr, \
921 const char *buf, size_t count) \
922 { \
923 return store_sys_hwmon(_get, buf, count); \
924 } \
925 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
926
927EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
Corentin Chary04dcd842008-10-09 15:33:57 +0200928EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100929 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
930EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
931 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
932
Corentin Chary04dcd842008-10-09 15:33:57 +0200933static ssize_t
934show_name(struct device *dev, struct device_attribute *attr, char *buf)
935{
936 return sprintf(buf, "eeepc\n");
937}
938static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
939
Corentin Charye1faa9d2008-03-13 12:57:18 +0100940static struct attribute *hwmon_attributes[] = {
Corentin Chary04dcd842008-10-09 15:33:57 +0200941 &sensor_dev_attr_pwm1.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100942 &sensor_dev_attr_fan1_input.dev_attr.attr,
943 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Corentin Chary04dcd842008-10-09 15:33:57 +0200944 &sensor_dev_attr_name.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100945 NULL
946};
947
948static struct attribute_group hwmon_attribute_group = {
949 .attrs = hwmon_attributes
950};
951
Corentin Charye1faa9d2008-03-13 12:57:18 +0100952static void eeepc_hwmon_exit(void)
953{
954 struct device *hwmon;
955
956 hwmon = eeepc_hwmon_device;
957 if (!hwmon)
958 return ;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100959 sysfs_remove_group(&hwmon->kobj,
960 &hwmon_attribute_group);
Matthew Garrettf1441312008-08-20 14:08:57 -0700961 hwmon_device_unregister(hwmon);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100962 eeepc_hwmon_device = NULL;
963}
964
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000965static int eeepc_hwmon_init(struct device *dev)
Corentin Chary3c0eb512009-12-03 07:44:52 +0000966{
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000967 struct device *hwmon;
Corentin Chary7de39382009-06-25 13:25:38 +0200968 int result;
969
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000970 hwmon = hwmon_device_register(dev);
971 if (IS_ERR(hwmon)) {
972 pr_err("Could not register eeepc hwmon device\n");
973 eeepc_hwmon_device = NULL;
974 return PTR_ERR(hwmon);
Corentin Chary7de39382009-06-25 13:25:38 +0200975 }
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000976 eeepc_hwmon_device = hwmon;
977 result = sysfs_create_group(&hwmon->kobj,
978 &hwmon_attribute_group);
979 if (result)
980 eeepc_hwmon_exit();
981 return result;
Corentin Chary7de39382009-06-25 13:25:38 +0200982}
983
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000984/*
985 * Backlight device
986 */
987static int read_brightness(struct backlight_device *bd)
Corentin Chary7de39382009-06-25 13:25:38 +0200988{
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000989 return get_acpi(CM_ASL_PANELBRIGHT);
990}
Corentin Chary7de39382009-06-25 13:25:38 +0200991
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000992static int set_brightness(struct backlight_device *bd, int value)
993{
994 return set_acpi(CM_ASL_PANELBRIGHT, value);
995}
Alan Jenkins73345462009-06-29 09:40:07 +0100996
Alan Jenkins52bbe3c2009-12-03 07:45:07 +0000997static int update_bl_status(struct backlight_device *bd)
998{
999 return set_brightness(bd, bd->props.brightness);
1000}
Corentin Chary7de39382009-06-25 13:25:38 +02001001
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001002static struct backlight_ops eeepcbl_ops = {
1003 .get_brightness = read_brightness,
1004 .update_status = update_bl_status,
1005};
Corentin Chary7de39382009-06-25 13:25:38 +02001006
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001007static int eeepc_backlight_notify(void)
1008{
1009 struct backlight_device *bd = eeepc_backlight_device;
1010 int old = bd->props.brightness;
Corentin Chary7de39382009-06-25 13:25:38 +02001011
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001012 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
Corentin Chary7de39382009-06-25 13:25:38 +02001013
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001014 return old;
Corentin Chary7de39382009-06-25 13:25:38 +02001015}
1016
Corentin Charya5fa4292008-03-13 12:56:37 +01001017static int eeepc_backlight_init(struct device *dev)
1018{
1019 struct backlight_device *bd;
1020
1021 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
1022 NULL, &eeepcbl_ops);
1023 if (IS_ERR(bd)) {
Joe Perches19b53282009-06-25 13:25:37 +02001024 pr_err("Could not register eeepc backlight device\n");
Corentin Charya5fa4292008-03-13 12:56:37 +01001025 eeepc_backlight_device = NULL;
1026 return PTR_ERR(bd);
1027 }
1028 eeepc_backlight_device = bd;
1029 bd->props.max_brightness = 15;
1030 bd->props.brightness = read_brightness(NULL);
1031 bd->props.power = FB_BLANK_UNBLANK;
1032 backlight_update_status(bd);
1033 return 0;
1034}
1035
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001036static void eeepc_backlight_exit(void)
Corentin Charye1faa9d2008-03-13 12:57:18 +01001037{
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001038 if (eeepc_backlight_device)
1039 backlight_device_unregister(eeepc_backlight_device);
1040 eeepc_backlight_device = NULL;
1041}
Corentin Charye1faa9d2008-03-13 12:57:18 +01001042
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001043
1044/*
1045 * Input device (i.e. hotkeys)
1046 */
1047static struct key_entry *eeepc_get_entry_by_scancode(int code)
1048{
1049 struct key_entry *key;
1050
1051 for (key = eeepc_keymap; key->type != KE_END; key++)
1052 if (code == key->code)
1053 return key;
1054
1055 return NULL;
1056}
1057
1058static void eeepc_input_notify(int event)
1059{
1060 static struct key_entry *key;
1061
1062 key = eeepc_get_entry_by_scancode(event);
1063 if (key) {
1064 switch (key->type) {
1065 case KE_KEY:
1066 input_report_key(ehotk->inputdev, key->keycode,
1067 1);
1068 input_sync(ehotk->inputdev);
1069 input_report_key(ehotk->inputdev, key->keycode,
1070 0);
1071 input_sync(ehotk->inputdev);
1072 break;
1073 }
Corentin Charye1faa9d2008-03-13 12:57:18 +01001074 }
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001075}
1076
1077static struct key_entry *eepc_get_entry_by_keycode(int code)
1078{
1079 struct key_entry *key;
1080
1081 for (key = eeepc_keymap; key->type != KE_END; key++)
1082 if (code == key->keycode && key->type == KE_KEY)
1083 return key;
1084
1085 return NULL;
1086}
1087
1088static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
1089{
1090 struct key_entry *key = eeepc_get_entry_by_scancode(scancode);
1091
1092 if (key && key->type == KE_KEY) {
1093 *keycode = key->keycode;
1094 return 0;
1095 }
1096
1097 return -EINVAL;
1098}
1099
1100static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
1101{
1102 struct key_entry *key;
1103 int old_keycode;
1104
1105 if (keycode < 0 || keycode > KEY_MAX)
1106 return -EINVAL;
1107
1108 key = eeepc_get_entry_by_scancode(scancode);
1109 if (key && key->type == KE_KEY) {
1110 old_keycode = key->keycode;
1111 key->keycode = keycode;
1112 set_bit(keycode, dev->keybit);
1113 if (!eepc_get_entry_by_keycode(old_keycode))
1114 clear_bit(old_keycode, dev->keybit);
1115 return 0;
1116 }
1117
1118 return -EINVAL;
Corentin Charye1faa9d2008-03-13 12:57:18 +01001119}
1120
Alan Jenkinsf2a9d5e2009-08-28 12:56:36 +00001121static int eeepc_input_init(struct device *dev)
1122{
1123 const struct key_entry *key;
1124 int result;
1125
1126 ehotk->inputdev = input_allocate_device();
1127 if (!ehotk->inputdev) {
1128 pr_info("Unable to allocate input device\n");
1129 return -ENOMEM;
1130 }
1131 ehotk->inputdev->name = "Asus EeePC extra buttons";
1132 ehotk->inputdev->dev.parent = dev;
1133 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
1134 ehotk->inputdev->id.bustype = BUS_HOST;
1135 ehotk->inputdev->getkeycode = eeepc_getkeycode;
1136 ehotk->inputdev->setkeycode = eeepc_setkeycode;
1137
1138 for (key = eeepc_keymap; key->type != KE_END; key++) {
1139 switch (key->type) {
1140 case KE_KEY:
1141 set_bit(EV_KEY, ehotk->inputdev->evbit);
1142 set_bit(key->keycode, ehotk->inputdev->keybit);
1143 break;
1144 }
1145 }
1146 result = input_register_device(ehotk->inputdev);
1147 if (result) {
1148 pr_info("Unable to register input device\n");
1149 input_free_device(ehotk->inputdev);
1150 return result;
1151 }
1152 return 0;
1153}
1154
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001155static void eeepc_input_exit(void)
Corentin Chary3c0eb512009-12-03 07:44:52 +00001156{
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001157 if (ehotk->inputdev)
1158 input_unregister_device(ehotk->inputdev);
1159}
Corentin Chary3c0eb512009-12-03 07:44:52 +00001160
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001161/*
1162 * ACPI driver
1163 */
1164static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
1165{
1166 u16 count;
Corentin Chary3c0eb512009-12-03 07:44:52 +00001167
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001168 if (event > ACPI_MAX_SYS_NOTIFY)
1169 return;
1170 count = ehotk->event_count[event % 128]++;
1171 acpi_bus_generate_proc_event(ehotk->device, event, count);
1172 acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
1173 dev_name(&ehotk->device->dev), event,
1174 count);
Corentin Chary3c0eb512009-12-03 07:44:52 +00001175
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001176 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX) {
1177 int old_brightness, new_brightness;
1178
1179 /* Update backlight device. */
1180 old_brightness = eeepc_backlight_notify();
1181
1182 /* Convert brightness event to keypress (obsolescent hack). */
1183 new_brightness = event - NOTIFY_BRN_MIN;
1184
1185 if (new_brightness < old_brightness) {
1186 event = NOTIFY_BRN_MIN; /* brightness down */
1187 } else if (new_brightness > old_brightness) {
1188 event = NOTIFY_BRN_MAX; /* brightness up */
1189 } else {
1190 /*
1191 * no change in brightness - already at min/max,
1192 * event will be desired value (or else ignored).
1193 */
1194 }
1195 }
1196 eeepc_input_notify(event);
1197}
1198
1199static void cmsg_quirk(int cm, const char *name)
1200{
1201 int dummy;
1202
1203 /* Some BIOSes do not report cm although it is avaliable.
1204 Check if cm_getv[cm] works and, if yes, assume cm should be set. */
1205 if (!(ehotk->cm_supported & (1 << cm))
1206 && !read_acpi_int(ehotk->handle, cm_getv[cm], &dummy)) {
1207 pr_info("%s (%x) not reported by BIOS,"
1208 " enabling anyway\n", name, 1 << cm);
1209 ehotk->cm_supported |= 1 << cm;
1210 }
1211}
1212
1213static void cmsg_quirks(void)
1214{
1215 cmsg_quirk(CM_ASL_LID, "LID");
1216 cmsg_quirk(CM_ASL_TYPE, "TYPE");
1217 cmsg_quirk(CM_ASL_PANELPOWER, "PANELPOWER");
1218 cmsg_quirk(CM_ASL_TPD, "TPD");
1219}
1220
1221static int eeepc_hotk_init(void)
1222{
1223 unsigned int init_flags;
1224 int result;
1225
1226 result = acpi_bus_get_status(ehotk->device);
1227 if (result)
1228 return result;
1229 if (!ehotk->device->status.present) {
1230 pr_err("Hotkey device not present, aborting\n");
1231 return -ENODEV;
Alan Jenkinsdc56ad92009-12-03 07:44:58 +00001232 }
Alan Jenkins2b56f1c2009-12-03 07:44:57 +00001233
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001234 init_flags = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
1235 pr_notice("Hotkey init flags 0x%x\n", init_flags);
1236
1237 if (write_acpi_int(ehotk->handle, "INIT", init_flags)) {
1238 pr_err("Hotkey initialization failed\n");
1239 return -ENODEV;
1240 }
1241
1242 /* get control methods supported */
1243 if (read_acpi_int(ehotk->handle, "CMSG", &ehotk->cm_supported)) {
1244 pr_err("Get control methods supported failed\n");
1245 return -ENODEV;
1246 }
1247 cmsg_quirks();
1248 pr_info("Get control methods supported: 0x%x\n", ehotk->cm_supported);
1249
Corentin Chary3c0eb512009-12-03 07:44:52 +00001250 return 0;
1251}
1252
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001253static void __devinit eeepc_enable_camera(void)
1254{
1255 /*
1256 * If the following call to set_acpi() fails, it's because there's no
1257 * camera so we can ignore the error.
1258 */
1259 if (get_acpi(CM_ASL_CAMERA) == 0)
1260 set_acpi(CM_ASL_CAMERA, 1);
1261}
1262
Rakib Mullickdcb73ee2009-10-13 00:13:32 +02001263static int __devinit eeepc_hotk_add(struct acpi_device *device)
Eric Coopere59f8792008-03-13 12:55:46 +01001264{
1265 struct device *dev;
1266 int result;
1267
Alan Jenkins1e779852009-08-28 12:56:35 +00001268 pr_notice(EEEPC_HOTK_NAME "\n");
1269 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
1270 if (!ehotk)
1271 return -ENOMEM;
Alan Jenkins1e779852009-08-28 12:56:35 +00001272 ehotk->handle = device->handle;
1273 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
1274 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
1275 device->driver_data = ehotk;
1276 ehotk->device = device;
Pekka Enbergcede2cb2009-06-16 19:28:45 +00001277
Alan Jenkins6b188a72009-12-03 07:45:02 +00001278 result = eeepc_hotk_init();
Alan Jenkins1e779852009-08-28 12:56:35 +00001279 if (result)
Alan Jenkins9db106b2009-12-03 07:45:06 +00001280 goto fail_platform;
Pekka Enbergcede2cb2009-06-16 19:28:45 +00001281 eeepc_enable_camera();
1282
Alan Jenkins9db106b2009-12-03 07:45:06 +00001283 result = eeepc_platform_init();
Eric Coopere59f8792008-03-13 12:55:46 +01001284 if (result)
Alan Jenkins9db106b2009-12-03 07:45:06 +00001285 goto fail_platform;
Corentin Chary7de39382009-06-25 13:25:38 +02001286
Corentin Chary1ddec2f2009-06-25 13:25:39 +02001287 dev = &platform_device->dev;
1288
1289 if (!acpi_video_backlight_support()) {
1290 result = eeepc_backlight_init(dev);
1291 if (result)
1292 goto fail_backlight;
1293 } else
Alan Jenkins9db106b2009-12-03 07:45:06 +00001294 pr_info("Backlight controlled by ACPI video driver\n");
Corentin Chary1ddec2f2009-06-25 13:25:39 +02001295
Alan Jenkinsf2a9d5e2009-08-28 12:56:36 +00001296 result = eeepc_input_init(dev);
1297 if (result)
1298 goto fail_input;
1299
Corentin Chary1ddec2f2009-06-25 13:25:39 +02001300 result = eeepc_hwmon_init(dev);
1301 if (result)
1302 goto fail_hwmon;
1303
Corentin Chary3c0eb512009-12-03 07:44:52 +00001304 result = eeepc_led_init(dev);
1305 if (result)
1306 goto fail_led;
1307
Corentin Chary7de39382009-06-25 13:25:38 +02001308 result = eeepc_rfkill_init(dev);
1309 if (result)
1310 goto fail_rfkill;
1311
Eric Coopere59f8792008-03-13 12:55:46 +01001312 return 0;
Alan Jenkins1e779852009-08-28 12:56:35 +00001313
Corentin Chary7de39382009-06-25 13:25:38 +02001314fail_rfkill:
Corentin Chary3c0eb512009-12-03 07:44:52 +00001315 eeepc_led_exit();
1316fail_led:
Corentin Chary1ddec2f2009-06-25 13:25:39 +02001317 eeepc_hwmon_exit();
1318fail_hwmon:
Alan Jenkinsf2a9d5e2009-08-28 12:56:36 +00001319 eeepc_input_exit();
1320fail_input:
Corentin Chary1ddec2f2009-06-25 13:25:39 +02001321 eeepc_backlight_exit();
1322fail_backlight:
Alan Jenkins9db106b2009-12-03 07:45:06 +00001323 eeepc_platform_exit();
1324fail_platform:
Alan Jenkins1e779852009-08-28 12:56:35 +00001325 kfree(ehotk);
1326
Eric Coopere59f8792008-03-13 12:55:46 +01001327 return result;
1328}
1329
Alan Jenkins1e779852009-08-28 12:56:35 +00001330static int eeepc_hotk_remove(struct acpi_device *device, int type)
1331{
Alan Jenkins1e779852009-08-28 12:56:35 +00001332 eeepc_backlight_exit();
1333 eeepc_rfkill_exit();
1334 eeepc_input_exit();
1335 eeepc_hwmon_exit();
Corentin Chary3c0eb512009-12-03 07:44:52 +00001336 eeepc_led_exit();
Alan Jenkins9db106b2009-12-03 07:45:06 +00001337 eeepc_platform_exit();
Alan Jenkins1e779852009-08-28 12:56:35 +00001338
1339 kfree(ehotk);
1340 return 0;
1341}
1342
Alan Jenkins52bbe3c2009-12-03 07:45:07 +00001343
1344static const struct acpi_device_id eeepc_device_ids[] = {
1345 {EEEPC_HOTK_HID, 0},
1346 {"", 0},
1347};
1348MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
1349
1350static struct acpi_driver eeepc_hotk_driver = {
1351 .name = EEEPC_HOTK_NAME,
1352 .class = EEEPC_HOTK_CLASS,
1353 .owner = THIS_MODULE,
1354 .ids = eeepc_device_ids,
1355 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1356 .ops = {
1357 .add = eeepc_hotk_add,
1358 .remove = eeepc_hotk_remove,
1359 .notify = eeepc_hotk_notify,
1360 },
1361};
1362
1363
Alan Jenkins1e779852009-08-28 12:56:35 +00001364static int __init eeepc_laptop_init(void)
1365{
1366 int result;
1367
Alan Jenkins22072e92009-12-03 07:45:05 +00001368 result = platform_driver_register(&platform_driver);
Alan Jenkins1e779852009-08-28 12:56:35 +00001369 if (result < 0)
1370 return result;
Alan Jenkins22072e92009-12-03 07:45:05 +00001371
1372 result = acpi_bus_register_driver(&eeepc_hotk_driver);
1373 if (result < 0)
1374 goto fail_acpi_driver;
Alan Jenkins1e779852009-08-28 12:56:35 +00001375 if (!ehotk) {
Alan Jenkins22072e92009-12-03 07:45:05 +00001376 result = -ENODEV;
1377 goto fail_no_device;
Alan Jenkins1e779852009-08-28 12:56:35 +00001378 }
1379 return 0;
Alan Jenkins22072e92009-12-03 07:45:05 +00001380
1381fail_no_device:
1382 acpi_bus_unregister_driver(&eeepc_hotk_driver);
1383fail_acpi_driver:
1384 platform_driver_unregister(&platform_driver);
1385 return result;
Alan Jenkins1e779852009-08-28 12:56:35 +00001386}
1387
1388static void __exit eeepc_laptop_exit(void)
1389{
1390 acpi_bus_unregister_driver(&eeepc_hotk_driver);
Alan Jenkins22072e92009-12-03 07:45:05 +00001391 platform_driver_unregister(&platform_driver);
Alan Jenkins1e779852009-08-28 12:56:35 +00001392}
1393
Eric Coopere59f8792008-03-13 12:55:46 +01001394module_init(eeepc_laptop_init);
1395module_exit(eeepc_laptop_exit);