2 * eepc-laptop.c - Asus Eee PC extras
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
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/types.h>
23 #include <linux/platform_device.h>
24 #include <linux/backlight.h>
26 #include <acpi/acpi_drivers.h>
27 #include <acpi/acpi_bus.h>
28 #include <linux/uaccess.h>
30 #define EEEPC_LAPTOP_VERSION "0.1"
32 #define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
33 #define EEEPC_HOTK_FILE "eeepc"
34 #define EEEPC_HOTK_CLASS "hotkey"
35 #define EEEPC_HOTK_DEVICE_NAME "Hotkey"
36 #define EEEPC_HOTK_HID "ASUS010"
38 #define EEEPC_LOG EEEPC_HOTK_FILE ": "
39 #define EEEPC_ERR KERN_ERR EEEPC_LOG
40 #define EEEPC_WARNING KERN_WARNING EEEPC_LOG
41 #define EEEPC_NOTICE KERN_NOTICE EEEPC_LOG
42 #define EEEPC_INFO KERN_INFO EEEPC_LOG
45 * Definitions for Asus EeePC
47 #define NOTIFY_WLAN_ON 0x10
48 #define NOTIFY_BRN_MIN 0x20
49 #define NOTIFY_BRN_MAX 0x2f
52 DISABLE_ASL_WLAN = 0x0001,
53 DISABLE_ASL_BLUETOOTH = 0x0002,
54 DISABLE_ASL_IRDA = 0x0004,
55 DISABLE_ASL_CAMERA = 0x0008,
56 DISABLE_ASL_TV = 0x0010,
57 DISABLE_ASL_GPS = 0x0020,
58 DISABLE_ASL_DISPLAYSWITCH = 0x0040,
59 DISABLE_ASL_MODEM = 0x0080,
60 DISABLE_ASL_CARDREADER = 0x0100
77 CM_ASL_CPUTEMPERATURE,
88 const char *cm_getv[] = {
89 "WLDG", NULL, NULL, NULL,
90 "CAMG", NULL, NULL, NULL,
91 NULL, "PBLG", NULL, NULL,
92 "CFVG", NULL, NULL, NULL,
93 "USBG", NULL, NULL, "MODG",
97 const char *cm_setv[] = {
98 "WLDS", NULL, NULL, NULL,
99 "CAMS", NULL, NULL, NULL,
100 "SDSP", "PBLS", "HDPS", NULL,
101 "CFVS", NULL, NULL, NULL,
102 "USBG", NULL, NULL, "MODS",
107 * This is the main structure, we can use it to store useful information
108 * about the hotk device
111 struct acpi_device *device; /* the device we are in */
112 acpi_handle handle; /* the handle of the hotk device */
113 u32 cm_supported; /* the control methods supported
115 uint init_flag; /* Init flags */
116 u16 event_count[128]; /* count for each event */
119 /* The actual device the driver binds to */
120 static struct eeepc_hotk *ehotk;
122 /* Platform device/driver */
123 static struct platform_driver platform_driver = {
125 .name = EEEPC_HOTK_FILE,
126 .owner = THIS_MODULE,
130 static struct platform_device *platform_device;
133 * The hotkey driver declaration
135 static int eeepc_hotk_add(struct acpi_device *device);
136 static int eeepc_hotk_remove(struct acpi_device *device, int type);
138 static const struct acpi_device_id eeepc_device_ids[] = {
142 MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
144 static struct acpi_driver eeepc_hotk_driver = {
145 .name = EEEPC_HOTK_NAME,
146 .class = EEEPC_HOTK_CLASS,
147 .ids = eeepc_device_ids,
149 .add = eeepc_hotk_add,
150 .remove = eeepc_hotk_remove,
154 /* The backlight device /sys/class/backlight */
155 static struct backlight_device *eeepc_backlight_device;
158 * The backlight class declaration
160 static int read_brightness(struct backlight_device *bd);
161 static int update_bl_status(struct backlight_device *bd);
162 static struct backlight_ops eeepcbl_ops = {
163 .get_brightness = read_brightness,
164 .update_status = update_bl_status,
167 MODULE_AUTHOR("Corentin Chary, Eric Cooper");
168 MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
169 MODULE_LICENSE("GPL");
174 static int write_acpi_int(acpi_handle handle, const char *method, int val,
175 struct acpi_buffer *output)
177 struct acpi_object_list params;
178 union acpi_object in_obj;
182 params.pointer = &in_obj;
183 in_obj.type = ACPI_TYPE_INTEGER;
184 in_obj.integer.value = val;
186 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
187 return (status == AE_OK ? 0 : -1);
190 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
195 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
196 if (ACPI_FAILURE(status)) {
205 static int set_acpi(int cm, int value)
207 if (ehotk->cm_supported & (0x1 << cm)) {
208 const char *method = cm_setv[cm];
211 if (write_acpi_int(ehotk->handle, method, value, NULL))
212 printk(EEEPC_WARNING "Error writing %s\n", method);
217 static int get_acpi(int cm)
220 if ((ehotk->cm_supported & (0x1 << cm))) {
221 const char *method = cm_getv[cm];
224 if (read_acpi_int(ehotk->handle, method, &value))
225 printk(EEEPC_WARNING "Error reading %s\n", method);
233 static int read_brightness(struct backlight_device *bd)
235 return get_acpi(CM_ASL_PANELBRIGHT);
238 static int set_brightness(struct backlight_device *bd, int value)
240 value = max(0, min(15, value));
241 return set_acpi(CM_ASL_PANELBRIGHT, value);
244 static int update_bl_status(struct backlight_device *bd)
246 return set_brightness(bd, bd->props.brightness);
252 static int parse_arg(const char *buf, unsigned long count, int *val)
256 if (sscanf(buf, "%i", val) != 1)
261 static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
265 rv = parse_arg(buf, count, &value);
271 static ssize_t show_sys_acpi(int cm, char *buf)
273 return sprintf(buf, "%d\n", get_acpi(cm));
276 #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
277 static ssize_t show_##_name(struct device *dev, \
278 struct device_attribute *attr, \
281 return show_sys_acpi(_cm, buf); \
283 static ssize_t store_##_name(struct device *dev, \
284 struct device_attribute *attr, \
285 const char *buf, size_t count) \
287 return store_sys_acpi(_cm, buf, count); \
289 static struct device_attribute dev_attr_##_name = { \
291 .name = __stringify(_name), \
293 .show = show_##_name, \
294 .store = store_##_name, \
297 EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
298 EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
299 EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
300 EEEPC_CREATE_DEVICE_ATTR(wlan, CM_ASL_WLAN);
302 static struct attribute *platform_attributes[] = {
303 &dev_attr_camera.attr,
304 &dev_attr_cardr.attr,
310 static struct attribute_group platform_attribute_group = {
311 .attrs = platform_attributes
317 static int eeepc_hotk_check(void)
319 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
322 result = acpi_bus_get_status(ehotk->device);
325 if (ehotk->device->status.present) {
326 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
328 printk(EEEPC_ERR "Hotkey initialization failed\n");
331 printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
334 /* get control methods supported */
335 if (read_acpi_int(ehotk->handle, "CMSG"
336 , &ehotk->cm_supported)) {
338 "Get control methods supported failed\n");
342 "Get control methods supported: 0x%x\n",
343 ehotk->cm_supported);
346 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
352 static void notify_wlan(u32 *event)
354 /* if DISABLE_ASL_WLAN is set, the notify code for fn+f2
355 will always be 0x10 */
356 if (ehotk->cm_supported & (0x1 << CM_ASL_WLAN)) {
357 const char *method = cm_getv[CM_ASL_WLAN];
359 if (read_acpi_int(ehotk->handle, method, &value))
360 printk(EEEPC_WARNING "Error reading %s\n",
367 static void notify_brn(void)
369 struct backlight_device *bd = eeepc_backlight_device;
370 bd->props.brightness = read_brightness(bd);
373 static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
377 if (event == NOTIFY_WLAN_ON && (DISABLE_ASL_WLAN & ehotk->init_flag))
379 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
381 acpi_bus_generate_proc_event(ehotk->device, event,
382 ehotk->event_count[event % 128]++);
385 static int eeepc_hotk_add(struct acpi_device *device)
387 acpi_status status = AE_OK;
392 printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
393 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
396 ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
397 ehotk->handle = device->handle;
398 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
399 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
400 acpi_driver_data(device) = ehotk;
401 ehotk->device = device;
402 result = eeepc_hotk_check();
405 status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
406 eeepc_hotk_notify, ehotk);
407 if (ACPI_FAILURE(status))
408 printk(EEEPC_ERR "Error installing notify handler\n");
417 static int eeepc_hotk_remove(struct acpi_device *device, int type)
419 acpi_status status = 0;
421 if (!device || !acpi_driver_data(device))
423 status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
425 if (ACPI_FAILURE(status))
426 printk(EEEPC_ERR "Error removing notify handler\n");
434 static void eeepc_backlight_exit(void)
436 if (eeepc_backlight_device)
437 backlight_device_unregister(eeepc_backlight_device);
438 eeepc_backlight_device = NULL;
441 static void __exit eeepc_laptop_exit(void)
443 eeepc_backlight_exit();
444 acpi_bus_unregister_driver(&eeepc_hotk_driver);
445 sysfs_remove_group(&platform_device->dev.kobj,
446 &platform_attribute_group);
447 platform_device_unregister(platform_device);
448 platform_driver_unregister(&platform_driver);
451 static int eeepc_backlight_init(struct device *dev)
453 struct backlight_device *bd;
455 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
459 "Could not register eeepc backlight device\n");
460 eeepc_backlight_device = NULL;
463 eeepc_backlight_device = bd;
464 bd->props.max_brightness = 15;
465 bd->props.brightness = read_brightness(NULL);
466 bd->props.power = FB_BLANK_UNBLANK;
467 backlight_update_status(bd);
471 static int __init eeepc_laptop_init(void)
478 result = acpi_bus_register_driver(&eeepc_hotk_driver);
482 acpi_bus_unregister_driver(&eeepc_hotk_driver);
485 dev = acpi_get_physical_device(ehotk->device->handle);
486 result = eeepc_backlight_init(dev);
489 /* Register platform stuff */
490 result = platform_driver_register(&platform_driver);
492 goto fail_platform_driver;
493 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
494 if (!platform_device) {
496 goto fail_platform_device1;
498 result = platform_device_add(platform_device);
500 goto fail_platform_device2;
501 result = sysfs_create_group(&platform_device->dev.kobj,
502 &platform_attribute_group);
507 platform_device_del(platform_device);
508 fail_platform_device2:
509 platform_device_put(platform_device);
510 fail_platform_device1:
511 platform_driver_unregister(&platform_driver);
512 fail_platform_driver:
513 eeepc_backlight_exit();
518 module_init(eeepc_laptop_init);
519 module_exit(eeepc_laptop_exit);