blob: 1e28413060b26416c9332aefeb840bf5a64a63d7 [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
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>
Corentin Charya5fa4292008-03-13 12:56:37 +010024#include <linux/backlight.h>
25#include <linux/fb.h>
Corentin Charye1faa9d2008-03-13 12:57:18 +010026#include <linux/hwmon.h>
27#include <linux/hwmon-sysfs.h>
Eric Coopere59f8792008-03-13 12:55:46 +010028#include <acpi/acpi_drivers.h>
29#include <acpi/acpi_bus.h>
30#include <linux/uaccess.h>
Matthew Garretta195dcd2008-08-19 12:13:20 +010031#include <linux/input.h>
32#include <linux/rfkill.h>
Matthew Garrett57402942009-01-20 16:17:48 +010033#include <linux/pci.h>
Eric Coopere59f8792008-03-13 12:55:46 +010034
35#define EEEPC_LAPTOP_VERSION "0.1"
36
37#define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
38#define EEEPC_HOTK_FILE "eeepc"
39#define EEEPC_HOTK_CLASS "hotkey"
40#define EEEPC_HOTK_DEVICE_NAME "Hotkey"
41#define EEEPC_HOTK_HID "ASUS010"
42
43#define EEEPC_LOG EEEPC_HOTK_FILE ": "
44#define EEEPC_ERR KERN_ERR EEEPC_LOG
45#define EEEPC_WARNING KERN_WARNING EEEPC_LOG
46#define EEEPC_NOTICE KERN_NOTICE EEEPC_LOG
47#define EEEPC_INFO KERN_INFO EEEPC_LOG
48
49/*
50 * Definitions for Asus EeePC
51 */
52#define NOTIFY_WLAN_ON 0x10
Corentin Charya5fa4292008-03-13 12:56:37 +010053#define NOTIFY_BRN_MIN 0x20
54#define NOTIFY_BRN_MAX 0x2f
Eric Coopere59f8792008-03-13 12:55:46 +010055
56enum {
57 DISABLE_ASL_WLAN = 0x0001,
58 DISABLE_ASL_BLUETOOTH = 0x0002,
59 DISABLE_ASL_IRDA = 0x0004,
60 DISABLE_ASL_CAMERA = 0x0008,
61 DISABLE_ASL_TV = 0x0010,
62 DISABLE_ASL_GPS = 0x0020,
63 DISABLE_ASL_DISPLAYSWITCH = 0x0040,
64 DISABLE_ASL_MODEM = 0x0080,
65 DISABLE_ASL_CARDREADER = 0x0100
66};
67
68enum {
69 CM_ASL_WLAN = 0,
70 CM_ASL_BLUETOOTH,
71 CM_ASL_IRDA,
72 CM_ASL_1394,
73 CM_ASL_CAMERA,
74 CM_ASL_TV,
75 CM_ASL_GPS,
76 CM_ASL_DVDROM,
77 CM_ASL_DISPLAYSWITCH,
78 CM_ASL_PANELBRIGHT,
79 CM_ASL_BIOSFLASH,
80 CM_ASL_ACPIFLASH,
81 CM_ASL_CPUFV,
82 CM_ASL_CPUTEMPERATURE,
83 CM_ASL_FANCPU,
84 CM_ASL_FANCHASSIS,
85 CM_ASL_USBPORT1,
86 CM_ASL_USBPORT2,
87 CM_ASL_USBPORT3,
88 CM_ASL_MODEM,
89 CM_ASL_CARDREADER,
90 CM_ASL_LID
91};
92
Adrian Bunk14109462008-06-25 19:25:47 +030093static const char *cm_getv[] = {
Jonathan McDowell3af9bfc2008-12-03 20:31:11 +000094 "WLDG", "BTHG", NULL, NULL,
Eric Coopere59f8792008-03-13 12:55:46 +010095 "CAMG", NULL, NULL, NULL,
96 NULL, "PBLG", NULL, NULL,
97 "CFVG", NULL, NULL, NULL,
98 "USBG", NULL, NULL, "MODG",
99 "CRDG", "LIDG"
100};
101
Adrian Bunk14109462008-06-25 19:25:47 +0300102static const char *cm_setv[] = {
Jonathan McDowell3af9bfc2008-12-03 20:31:11 +0000103 "WLDS", "BTHS", NULL, NULL,
Eric Coopere59f8792008-03-13 12:55:46 +0100104 "CAMS", NULL, NULL, NULL,
105 "SDSP", "PBLS", "HDPS", NULL,
106 "CFVS", NULL, NULL, NULL,
107 "USBG", NULL, NULL, "MODS",
108 "CRDS", NULL
109};
110
Corentin Charye1faa9d2008-03-13 12:57:18 +0100111#define EEEPC_EC "\\_SB.PCI0.SBRG.EC0."
112
113#define EEEPC_EC_FAN_PWM EEEPC_EC "SC02" /* Fan PWM duty cycle (%) */
114#define EEEPC_EC_SC02 0x63
115#define EEEPC_EC_FAN_HRPM EEEPC_EC "SC05" /* High byte, fan speed (RPM) */
116#define EEEPC_EC_FAN_LRPM EEEPC_EC "SC06" /* Low byte, fan speed (RPM) */
117#define EEEPC_EC_FAN_CTRL EEEPC_EC "SFB3" /* Byte containing SF25 */
118#define EEEPC_EC_SFB3 0xD3
119
Eric Coopere59f8792008-03-13 12:55:46 +0100120/*
121 * This is the main structure, we can use it to store useful information
122 * about the hotk device
123 */
124struct eeepc_hotk {
125 struct acpi_device *device; /* the device we are in */
126 acpi_handle handle; /* the handle of the hotk device */
127 u32 cm_supported; /* the control methods supported
128 by this BIOS */
129 uint init_flag; /* Init flags */
130 u16 event_count[128]; /* count for each event */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100131 struct input_dev *inputdev;
132 u16 *keycode_map;
133 struct rfkill *eeepc_wlan_rfkill;
134 struct rfkill *eeepc_bluetooth_rfkill;
Eric Coopere59f8792008-03-13 12:55:46 +0100135};
136
137/* The actual device the driver binds to */
138static struct eeepc_hotk *ehotk;
139
140/* Platform device/driver */
141static struct platform_driver platform_driver = {
142 .driver = {
143 .name = EEEPC_HOTK_FILE,
144 .owner = THIS_MODULE,
145 }
146};
147
148static struct platform_device *platform_device;
149
Matthew Garretta195dcd2008-08-19 12:13:20 +0100150struct key_entry {
151 char type;
152 u8 code;
153 u16 keycode;
154};
155
156enum { KE_KEY, KE_END };
157
158static struct key_entry eeepc_keymap[] = {
159 /* Sleep already handled via generic ACPI code */
160 {KE_KEY, 0x10, KEY_WLAN },
Alan Jenkins978605c2009-04-27 09:23:39 +0200161 {KE_KEY, 0x11, KEY_WLAN },
Matthew Garretta195dcd2008-08-19 12:13:20 +0100162 {KE_KEY, 0x12, KEY_PROG1 },
163 {KE_KEY, 0x13, KEY_MUTE },
164 {KE_KEY, 0x14, KEY_VOLUMEDOWN },
165 {KE_KEY, 0x15, KEY_VOLUMEUP },
Matthew Garrettb5f6f262009-01-20 16:17:46 +0100166 {KE_KEY, 0x1a, KEY_COFFEE },
167 {KE_KEY, 0x1b, KEY_ZOOM },
168 {KE_KEY, 0x1c, KEY_PROG2 },
169 {KE_KEY, 0x1d, KEY_PROG3 },
Darren Salt64b86b62009-04-27 09:23:38 +0200170 {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
171 {KE_KEY, NOTIFY_BRN_MIN + 2, KEY_BRIGHTNESSUP },
Matthew Garretta195dcd2008-08-19 12:13:20 +0100172 {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
173 {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
174 {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
175 {KE_END, 0},
176};
177
Eric Coopere59f8792008-03-13 12:55:46 +0100178/*
179 * The hotkey driver declaration
180 */
181static int eeepc_hotk_add(struct acpi_device *device);
182static int eeepc_hotk_remove(struct acpi_device *device, int type);
Bjorn Helgaasd9b9bd72009-04-30 09:36:03 -0600183static void eeepc_hotk_notify(struct acpi_device *device, u32 event);
Eric Coopere59f8792008-03-13 12:55:46 +0100184
185static const struct acpi_device_id eeepc_device_ids[] = {
186 {EEEPC_HOTK_HID, 0},
187 {"", 0},
188};
189MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
190
191static struct acpi_driver eeepc_hotk_driver = {
192 .name = EEEPC_HOTK_NAME,
193 .class = EEEPC_HOTK_CLASS,
194 .ids = eeepc_device_ids,
Bjorn Helgaasd9b9bd72009-04-30 09:36:03 -0600195 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
Eric Coopere59f8792008-03-13 12:55:46 +0100196 .ops = {
197 .add = eeepc_hotk_add,
198 .remove = eeepc_hotk_remove,
Bjorn Helgaasd9b9bd72009-04-30 09:36:03 -0600199 .notify = eeepc_hotk_notify,
Eric Coopere59f8792008-03-13 12:55:46 +0100200 },
201};
202
Corentin Charya5fa4292008-03-13 12:56:37 +0100203/* The backlight device /sys/class/backlight */
204static struct backlight_device *eeepc_backlight_device;
205
Corentin Charye1faa9d2008-03-13 12:57:18 +0100206/* The hwmon device */
207static struct device *eeepc_hwmon_device;
208
Corentin Charya5fa4292008-03-13 12:56:37 +0100209/*
210 * The backlight class declaration
211 */
212static int read_brightness(struct backlight_device *bd);
213static int update_bl_status(struct backlight_device *bd);
214static struct backlight_ops eeepcbl_ops = {
215 .get_brightness = read_brightness,
216 .update_status = update_bl_status,
217};
218
Eric Coopere59f8792008-03-13 12:55:46 +0100219MODULE_AUTHOR("Corentin Chary, Eric Cooper");
220MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
221MODULE_LICENSE("GPL");
222
223/*
224 * ACPI Helpers
225 */
226static int write_acpi_int(acpi_handle handle, const char *method, int val,
227 struct acpi_buffer *output)
228{
229 struct acpi_object_list params;
230 union acpi_object in_obj;
231 acpi_status status;
232
233 params.count = 1;
234 params.pointer = &in_obj;
235 in_obj.type = ACPI_TYPE_INTEGER;
236 in_obj.integer.value = val;
237
238 status = acpi_evaluate_object(handle, (char *)method, &params, output);
239 return (status == AE_OK ? 0 : -1);
240}
241
242static int read_acpi_int(acpi_handle handle, const char *method, int *val)
243{
244 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400245 unsigned long long result;
Eric Coopere59f8792008-03-13 12:55:46 +0100246
247 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
248 if (ACPI_FAILURE(status)) {
249 *val = -1;
250 return -1;
251 } else {
252 *val = result;
253 return 0;
254 }
255}
256
257static int set_acpi(int cm, int value)
258{
259 if (ehotk->cm_supported & (0x1 << cm)) {
260 const char *method = cm_setv[cm];
261 if (method == NULL)
262 return -ENODEV;
263 if (write_acpi_int(ehotk->handle, method, value, NULL))
264 printk(EEEPC_WARNING "Error writing %s\n", method);
265 }
266 return 0;
267}
268
269static int get_acpi(int cm)
270{
271 int value = -1;
272 if ((ehotk->cm_supported & (0x1 << cm))) {
273 const char *method = cm_getv[cm];
274 if (method == NULL)
275 return -ENODEV;
276 if (read_acpi_int(ehotk->handle, method, &value))
277 printk(EEEPC_WARNING "Error reading %s\n", method);
278 }
279 return value;
280}
281
282/*
Corentin Charya5fa4292008-03-13 12:56:37 +0100283 * Backlight
284 */
285static int read_brightness(struct backlight_device *bd)
286{
287 return get_acpi(CM_ASL_PANELBRIGHT);
288}
289
290static int set_brightness(struct backlight_device *bd, int value)
291{
292 value = max(0, min(15, value));
293 return set_acpi(CM_ASL_PANELBRIGHT, value);
294}
295
296static int update_bl_status(struct backlight_device *bd)
297{
298 return set_brightness(bd, bd->props.brightness);
299}
300
301/*
Matthew Garretta195dcd2008-08-19 12:13:20 +0100302 * Rfkill helpers
303 */
304
305static int eeepc_wlan_rfkill_set(void *data, enum rfkill_state state)
306{
307 if (state == RFKILL_STATE_SOFT_BLOCKED)
308 return set_acpi(CM_ASL_WLAN, 0);
309 else
310 return set_acpi(CM_ASL_WLAN, 1);
311}
312
313static int eeepc_wlan_rfkill_state(void *data, enum rfkill_state *state)
314{
315 if (get_acpi(CM_ASL_WLAN) == 1)
316 *state = RFKILL_STATE_UNBLOCKED;
317 else
318 *state = RFKILL_STATE_SOFT_BLOCKED;
319 return 0;
320}
321
322static int eeepc_bluetooth_rfkill_set(void *data, enum rfkill_state state)
323{
324 if (state == RFKILL_STATE_SOFT_BLOCKED)
325 return set_acpi(CM_ASL_BLUETOOTH, 0);
326 else
327 return set_acpi(CM_ASL_BLUETOOTH, 1);
328}
329
330static int eeepc_bluetooth_rfkill_state(void *data, enum rfkill_state *state)
331{
332 if (get_acpi(CM_ASL_BLUETOOTH) == 1)
333 *state = RFKILL_STATE_UNBLOCKED;
334 else
335 *state = RFKILL_STATE_SOFT_BLOCKED;
336 return 0;
337}
338
339/*
Eric Coopere59f8792008-03-13 12:55:46 +0100340 * Sys helpers
341 */
342static int parse_arg(const char *buf, unsigned long count, int *val)
343{
344 if (!count)
345 return 0;
346 if (sscanf(buf, "%i", val) != 1)
347 return -EINVAL;
348 return count;
349}
350
351static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
352{
353 int rv, value;
354
355 rv = parse_arg(buf, count, &value);
356 if (rv > 0)
357 set_acpi(cm, value);
358 return rv;
359}
360
361static ssize_t show_sys_acpi(int cm, char *buf)
362{
363 return sprintf(buf, "%d\n", get_acpi(cm));
364}
365
366#define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
367 static ssize_t show_##_name(struct device *dev, \
368 struct device_attribute *attr, \
369 char *buf) \
370 { \
371 return show_sys_acpi(_cm, buf); \
372 } \
373 static ssize_t store_##_name(struct device *dev, \
374 struct device_attribute *attr, \
375 const char *buf, size_t count) \
376 { \
377 return store_sys_acpi(_cm, buf, count); \
378 } \
379 static struct device_attribute dev_attr_##_name = { \
380 .attr = { \
381 .name = __stringify(_name), \
382 .mode = 0644 }, \
383 .show = show_##_name, \
384 .store = store_##_name, \
385 }
386
387EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
388EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
389EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
Grigori Goronzy158ca1d2009-04-27 09:23:40 +0200390EEEPC_CREATE_DEVICE_ATTR(cpufv, CM_ASL_CPUFV);
Eric Coopere59f8792008-03-13 12:55:46 +0100391
392static struct attribute *platform_attributes[] = {
393 &dev_attr_camera.attr,
394 &dev_attr_cardr.attr,
395 &dev_attr_disp.attr,
Grigori Goronzy158ca1d2009-04-27 09:23:40 +0200396 &dev_attr_cpufv.attr,
Eric Coopere59f8792008-03-13 12:55:46 +0100397 NULL
398};
399
400static struct attribute_group platform_attribute_group = {
401 .attrs = platform_attributes
402};
403
404/*
405 * Hotkey functions
406 */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100407static struct key_entry *eepc_get_entry_by_scancode(int code)
408{
409 struct key_entry *key;
410
411 for (key = eeepc_keymap; key->type != KE_END; key++)
412 if (code == key->code)
413 return key;
414
415 return NULL;
416}
417
418static struct key_entry *eepc_get_entry_by_keycode(int code)
419{
420 struct key_entry *key;
421
422 for (key = eeepc_keymap; key->type != KE_END; key++)
423 if (code == key->keycode && key->type == KE_KEY)
424 return key;
425
426 return NULL;
427}
428
429static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
430{
431 struct key_entry *key = eepc_get_entry_by_scancode(scancode);
432
433 if (key && key->type == KE_KEY) {
434 *keycode = key->keycode;
435 return 0;
436 }
437
438 return -EINVAL;
439}
440
441static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
442{
443 struct key_entry *key;
444 int old_keycode;
445
446 if (keycode < 0 || keycode > KEY_MAX)
447 return -EINVAL;
448
449 key = eepc_get_entry_by_scancode(scancode);
450 if (key && key->type == KE_KEY) {
451 old_keycode = key->keycode;
452 key->keycode = keycode;
453 set_bit(keycode, dev->keybit);
454 if (!eepc_get_entry_by_keycode(old_keycode))
455 clear_bit(old_keycode, dev->keybit);
456 return 0;
457 }
458
459 return -EINVAL;
460}
461
Eric Coopere59f8792008-03-13 12:55:46 +0100462static int eeepc_hotk_check(void)
463{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100464 const struct key_entry *key;
Eric Coopere59f8792008-03-13 12:55:46 +0100465 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
466 int result;
467
468 result = acpi_bus_get_status(ehotk->device);
469 if (result)
470 return result;
471 if (ehotk->device->status.present) {
472 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
473 &buffer)) {
474 printk(EEEPC_ERR "Hotkey initialization failed\n");
475 return -ENODEV;
476 } else {
477 printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
478 ehotk->init_flag);
479 }
480 /* get control methods supported */
481 if (read_acpi_int(ehotk->handle, "CMSG"
482 , &ehotk->cm_supported)) {
483 printk(EEEPC_ERR
484 "Get control methods supported failed\n");
485 return -ENODEV;
486 } else {
487 printk(EEEPC_INFO
488 "Get control methods supported: 0x%x\n",
489 ehotk->cm_supported);
490 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100491 ehotk->inputdev = input_allocate_device();
492 if (!ehotk->inputdev) {
493 printk(EEEPC_INFO "Unable to allocate input device\n");
494 return 0;
495 }
496 ehotk->inputdev->name = "Asus EeePC extra buttons";
497 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
498 ehotk->inputdev->id.bustype = BUS_HOST;
499 ehotk->inputdev->getkeycode = eeepc_getkeycode;
500 ehotk->inputdev->setkeycode = eeepc_setkeycode;
501
502 for (key = eeepc_keymap; key->type != KE_END; key++) {
503 switch (key->type) {
504 case KE_KEY:
505 set_bit(EV_KEY, ehotk->inputdev->evbit);
506 set_bit(key->keycode, ehotk->inputdev->keybit);
507 break;
508 }
509 }
510 result = input_register_device(ehotk->inputdev);
511 if (result) {
512 printk(EEEPC_INFO "Unable to register input device\n");
513 input_free_device(ehotk->inputdev);
514 return 0;
515 }
Eric Coopere59f8792008-03-13 12:55:46 +0100516 } else {
517 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
518 return -EINVAL;
519 }
520 return 0;
521}
522
Darren Salt64b86b62009-04-27 09:23:38 +0200523static int notify_brn(void)
Corentin Charya5fa4292008-03-13 12:56:37 +0100524{
Darren Salt64b86b62009-04-27 09:23:38 +0200525 /* returns the *previous* brightness, or -1 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100526 struct backlight_device *bd = eeepc_backlight_device;
Darren Salt64b86b62009-04-27 09:23:38 +0200527 if (bd) {
528 int old = bd->props.brightness;
Darren Salt7695fb02009-02-07 01:02:07 -0500529 bd->props.brightness = read_brightness(bd);
Darren Salt64b86b62009-04-27 09:23:38 +0200530 return old;
531 }
532 return -1;
Corentin Charya5fa4292008-03-13 12:56:37 +0100533}
534
Matthew Garrett57402942009-01-20 16:17:48 +0100535static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
536{
Alan Jenkins978605c2009-04-27 09:23:39 +0200537 enum rfkill_state state;
Matthew Garrett57402942009-01-20 16:17:48 +0100538 struct pci_dev *dev;
539 struct pci_bus *bus = pci_find_bus(0, 1);
540
541 if (event != ACPI_NOTIFY_BUS_CHECK)
542 return;
543
544 if (!bus) {
545 printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
546 return;
547 }
548
Alan Jenkins978605c2009-04-27 09:23:39 +0200549 eeepc_wlan_rfkill_state(ehotk->eeepc_wlan_rfkill, &state);
550
551 if (state == RFKILL_STATE_UNBLOCKED) {
Matthew Garrett57402942009-01-20 16:17:48 +0100552 dev = pci_get_slot(bus, 0);
553 if (dev) {
554 /* Device already present */
555 pci_dev_put(dev);
556 return;
557 }
558 dev = pci_scan_single_device(bus, 0);
559 if (dev) {
560 pci_bus_assign_resources(bus);
561 if (pci_bus_add_device(dev))
562 printk(EEEPC_ERR "Unable to hotplug wifi\n");
563 }
564 } else {
565 dev = pci_get_slot(bus, 0);
566 if (dev) {
567 pci_remove_bus_device(dev);
568 pci_dev_put(dev);
569 }
570 }
Alan Jenkins978605c2009-04-27 09:23:39 +0200571
572 rfkill_force_state(ehotk->eeepc_wlan_rfkill, state);
Matthew Garrett57402942009-01-20 16:17:48 +0100573}
574
Bjorn Helgaasd9b9bd72009-04-30 09:36:03 -0600575static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
Eric Coopere59f8792008-03-13 12:55:46 +0100576{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100577 static struct key_entry *key;
Corentin Chary7950b712009-02-15 19:30:20 +0100578 u16 count;
Darren Salt64b86b62009-04-27 09:23:38 +0200579 int brn = -ENODEV;
Corentin Chary7950b712009-02-15 19:30:20 +0100580
Eric Coopere59f8792008-03-13 12:55:46 +0100581 if (!ehotk)
582 return;
Bjorn Helgaasd9b9bd72009-04-30 09:36:03 -0600583 if (event > ACPI_MAX_SYS_NOTIFY)
584 return;
Corentin Charya5fa4292008-03-13 12:56:37 +0100585 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
Darren Salt64b86b62009-04-27 09:23:38 +0200586 brn = notify_brn();
Corentin Chary7950b712009-02-15 19:30:20 +0100587 count = ehotk->event_count[event % 128]++;
588 acpi_bus_generate_proc_event(ehotk->device, event, count);
Corentin Chary2b25c9f2009-01-20 16:17:49 +0100589 acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
590 dev_name(&ehotk->device->dev), event,
Corentin Chary7950b712009-02-15 19:30:20 +0100591 count);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100592 if (ehotk->inputdev) {
Darren Salt64b86b62009-04-27 09:23:38 +0200593 if (brn != -ENODEV) {
594 /* brightness-change events need special
595 * handling for conversion to key events
596 */
597 if (brn < 0)
598 brn = event;
599 else
600 brn += NOTIFY_BRN_MIN;
601 if (event < brn)
602 event = NOTIFY_BRN_MIN; /* brightness down */
603 else if (event > brn)
604 event = NOTIFY_BRN_MIN + 2; /* ... up */
605 else
606 event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
607 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100608 key = eepc_get_entry_by_scancode(event);
609 if (key) {
610 switch (key->type) {
611 case KE_KEY:
612 input_report_key(ehotk->inputdev, key->keycode,
613 1);
614 input_sync(ehotk->inputdev);
615 input_report_key(ehotk->inputdev, key->keycode,
616 0);
617 input_sync(ehotk->inputdev);
618 break;
619 }
620 }
621 }
Eric Coopere59f8792008-03-13 12:55:46 +0100622}
623
Matthew Garrett57402942009-01-20 16:17:48 +0100624static int eeepc_register_rfkill_notifier(char *node)
625{
626 acpi_status status = AE_OK;
627 acpi_handle handle;
628
629 status = acpi_get_handle(NULL, node, &handle);
630
631 if (ACPI_SUCCESS(status)) {
632 status = acpi_install_notify_handler(handle,
633 ACPI_SYSTEM_NOTIFY,
634 eeepc_rfkill_notify,
635 NULL);
636 if (ACPI_FAILURE(status))
637 printk(EEEPC_WARNING
638 "Failed to register notify on %s\n", node);
639 } else
640 return -ENODEV;
641
642 return 0;
643}
644
645static void eeepc_unregister_rfkill_notifier(char *node)
646{
647 acpi_status status = AE_OK;
648 acpi_handle handle;
649
650 status = acpi_get_handle(NULL, node, &handle);
651
652 if (ACPI_SUCCESS(status)) {
653 status = acpi_remove_notify_handler(handle,
654 ACPI_SYSTEM_NOTIFY,
655 eeepc_rfkill_notify);
656 if (ACPI_FAILURE(status))
657 printk(EEEPC_ERR
658 "Error removing rfkill notify handler %s\n",
659 node);
660 }
661}
662
Eric Coopere59f8792008-03-13 12:55:46 +0100663static int eeepc_hotk_add(struct acpi_device *device)
664{
Eric Coopere59f8792008-03-13 12:55:46 +0100665 int result;
666
667 if (!device)
668 return -EINVAL;
669 printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
670 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
671 if (!ehotk)
672 return -ENOMEM;
673 ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
674 ehotk->handle = device->handle;
675 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
676 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700677 device->driver_data = ehotk;
Eric Coopere59f8792008-03-13 12:55:46 +0100678 ehotk->device = device;
679 result = eeepc_hotk_check();
680 if (result)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100681 goto ehotk_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100682
Alan Jenkinsfbc97e42009-04-27 09:23:37 +0200683 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
684 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
685
Matthew Garretta195dcd2008-08-19 12:13:20 +0100686 if (get_acpi(CM_ASL_WLAN) != -1) {
687 ehotk->eeepc_wlan_rfkill = rfkill_allocate(&device->dev,
688 RFKILL_TYPE_WLAN);
689
690 if (!ehotk->eeepc_wlan_rfkill)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100691 goto wlan_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100692
693 ehotk->eeepc_wlan_rfkill->name = "eeepc-wlan";
694 ehotk->eeepc_wlan_rfkill->toggle_radio = eeepc_wlan_rfkill_set;
695 ehotk->eeepc_wlan_rfkill->get_state = eeepc_wlan_rfkill_state;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100696 if (get_acpi(CM_ASL_WLAN) == 1) {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100697 ehotk->eeepc_wlan_rfkill->state =
698 RFKILL_STATE_UNBLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100699 rfkill_set_default(RFKILL_TYPE_WLAN,
700 RFKILL_STATE_UNBLOCKED);
701 } else {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100702 ehotk->eeepc_wlan_rfkill->state =
703 RFKILL_STATE_SOFT_BLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100704 rfkill_set_default(RFKILL_TYPE_WLAN,
705 RFKILL_STATE_SOFT_BLOCKED);
706 }
707 result = rfkill_register(ehotk->eeepc_wlan_rfkill);
708 if (result)
709 goto wlan_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100710 }
711
712 if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
713 ehotk->eeepc_bluetooth_rfkill =
714 rfkill_allocate(&device->dev, RFKILL_TYPE_BLUETOOTH);
715
716 if (!ehotk->eeepc_bluetooth_rfkill)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100717 goto bluetooth_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100718
719 ehotk->eeepc_bluetooth_rfkill->name = "eeepc-bluetooth";
720 ehotk->eeepc_bluetooth_rfkill->toggle_radio =
721 eeepc_bluetooth_rfkill_set;
722 ehotk->eeepc_bluetooth_rfkill->get_state =
723 eeepc_bluetooth_rfkill_state;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100724 if (get_acpi(CM_ASL_BLUETOOTH) == 1) {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100725 ehotk->eeepc_bluetooth_rfkill->state =
726 RFKILL_STATE_UNBLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100727 rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
728 RFKILL_STATE_UNBLOCKED);
729 } else {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100730 ehotk->eeepc_bluetooth_rfkill->state =
731 RFKILL_STATE_SOFT_BLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100732 rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
733 RFKILL_STATE_SOFT_BLOCKED);
734 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100735
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100736 result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
737 if (result)
738 goto bluetooth_fail;
Eric Coopere59f8792008-03-13 12:55:46 +0100739 }
Matthew Garrett57402942009-01-20 16:17:48 +0100740
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100741 return 0;
742
743 bluetooth_fail:
744 if (ehotk->eeepc_bluetooth_rfkill)
745 rfkill_free(ehotk->eeepc_bluetooth_rfkill);
746 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
747 ehotk->eeepc_wlan_rfkill = NULL;
748 wlan_fail:
749 if (ehotk->eeepc_wlan_rfkill)
750 rfkill_free(ehotk->eeepc_wlan_rfkill);
Corentin Charybd320052009-04-27 09:23:43 +0200751 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
752 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100753 ehotk_fail:
754 kfree(ehotk);
755 ehotk = NULL;
756
Eric Coopere59f8792008-03-13 12:55:46 +0100757 return result;
758}
759
760static int eeepc_hotk_remove(struct acpi_device *device, int type)
761{
Eric Coopere59f8792008-03-13 12:55:46 +0100762 if (!device || !acpi_driver_data(device))
763 return -EINVAL;
Matthew Garrett57402942009-01-20 16:17:48 +0100764
765 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
766 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
767
Eric Coopere59f8792008-03-13 12:55:46 +0100768 kfree(ehotk);
769 return 0;
770}
771
772/*
Corentin Charye1faa9d2008-03-13 12:57:18 +0100773 * Hwmon
774 */
775static int eeepc_get_fan_pwm(void)
776{
777 int value = 0;
778
779 read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
Corentin Chary04dcd842008-10-09 15:33:57 +0200780 value = value * 255 / 100;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100781 return (value);
782}
783
784static void eeepc_set_fan_pwm(int value)
785{
Corentin Chary04dcd842008-10-09 15:33:57 +0200786 value = SENSORS_LIMIT(value, 0, 255);
787 value = value * 100 / 255;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100788 ec_write(EEEPC_EC_SC02, value);
789}
790
791static int eeepc_get_fan_rpm(void)
792{
793 int high = 0;
794 int low = 0;
795
796 read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
797 read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
798 return (high << 8 | low);
799}
800
801static int eeepc_get_fan_ctrl(void)
802{
803 int value = 0;
804
805 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
806 return ((value & 0x02 ? 1 : 0));
807}
808
809static void eeepc_set_fan_ctrl(int manual)
810{
811 int value = 0;
812
813 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
814 if (manual)
815 value |= 0x02;
816 else
817 value &= ~0x02;
818 ec_write(EEEPC_EC_SFB3, value);
819}
820
821static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
822{
823 int rv, value;
824
825 rv = parse_arg(buf, count, &value);
826 if (rv > 0)
827 set(value);
828 return rv;
829}
830
831static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
832{
833 return sprintf(buf, "%d\n", get());
834}
835
836#define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
837 static ssize_t show_##_name(struct device *dev, \
838 struct device_attribute *attr, \
839 char *buf) \
840 { \
841 return show_sys_hwmon(_set, buf); \
842 } \
843 static ssize_t store_##_name(struct device *dev, \
844 struct device_attribute *attr, \
845 const char *buf, size_t count) \
846 { \
847 return store_sys_hwmon(_get, buf, count); \
848 } \
849 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
850
851EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
Corentin Chary04dcd842008-10-09 15:33:57 +0200852EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100853 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
854EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
855 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
856
Corentin Chary04dcd842008-10-09 15:33:57 +0200857static ssize_t
858show_name(struct device *dev, struct device_attribute *attr, char *buf)
859{
860 return sprintf(buf, "eeepc\n");
861}
862static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
863
Corentin Charye1faa9d2008-03-13 12:57:18 +0100864static struct attribute *hwmon_attributes[] = {
Corentin Chary04dcd842008-10-09 15:33:57 +0200865 &sensor_dev_attr_pwm1.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100866 &sensor_dev_attr_fan1_input.dev_attr.attr,
867 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Corentin Chary04dcd842008-10-09 15:33:57 +0200868 &sensor_dev_attr_name.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100869 NULL
870};
871
872static struct attribute_group hwmon_attribute_group = {
873 .attrs = hwmon_attributes
874};
875
876/*
Eric Coopere59f8792008-03-13 12:55:46 +0100877 * exit/init
878 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100879static void eeepc_backlight_exit(void)
880{
881 if (eeepc_backlight_device)
882 backlight_device_unregister(eeepc_backlight_device);
Corentin Charya9df80c2009-01-20 16:17:40 +0100883 eeepc_backlight_device = NULL;
884}
885
886static void eeepc_rfkill_exit(void)
887{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100888 if (ehotk->eeepc_wlan_rfkill)
889 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
890 if (ehotk->eeepc_bluetooth_rfkill)
891 rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
Corentin Charya9df80c2009-01-20 16:17:40 +0100892}
893
894static void eeepc_input_exit(void)
895{
896 if (ehotk->inputdev)
897 input_unregister_device(ehotk->inputdev);
Corentin Charya5fa4292008-03-13 12:56:37 +0100898}
899
Corentin Charye1faa9d2008-03-13 12:57:18 +0100900static void eeepc_hwmon_exit(void)
901{
902 struct device *hwmon;
903
904 hwmon = eeepc_hwmon_device;
905 if (!hwmon)
906 return ;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100907 sysfs_remove_group(&hwmon->kobj,
908 &hwmon_attribute_group);
Matthew Garrettf1441312008-08-20 14:08:57 -0700909 hwmon_device_unregister(hwmon);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100910 eeepc_hwmon_device = NULL;
911}
912
Eric Coopere59f8792008-03-13 12:55:46 +0100913static void __exit eeepc_laptop_exit(void)
914{
Corentin Charya5fa4292008-03-13 12:56:37 +0100915 eeepc_backlight_exit();
Corentin Charya9df80c2009-01-20 16:17:40 +0100916 eeepc_rfkill_exit();
917 eeepc_input_exit();
Corentin Charye1faa9d2008-03-13 12:57:18 +0100918 eeepc_hwmon_exit();
Eric Coopere59f8792008-03-13 12:55:46 +0100919 acpi_bus_unregister_driver(&eeepc_hotk_driver);
920 sysfs_remove_group(&platform_device->dev.kobj,
921 &platform_attribute_group);
922 platform_device_unregister(platform_device);
923 platform_driver_unregister(&platform_driver);
924}
925
Corentin Charya5fa4292008-03-13 12:56:37 +0100926static int eeepc_backlight_init(struct device *dev)
927{
928 struct backlight_device *bd;
929
930 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
931 NULL, &eeepcbl_ops);
932 if (IS_ERR(bd)) {
933 printk(EEEPC_ERR
934 "Could not register eeepc backlight device\n");
935 eeepc_backlight_device = NULL;
936 return PTR_ERR(bd);
937 }
938 eeepc_backlight_device = bd;
939 bd->props.max_brightness = 15;
940 bd->props.brightness = read_brightness(NULL);
941 bd->props.power = FB_BLANK_UNBLANK;
942 backlight_update_status(bd);
943 return 0;
944}
945
Corentin Charye1faa9d2008-03-13 12:57:18 +0100946static int eeepc_hwmon_init(struct device *dev)
947{
948 struct device *hwmon;
949 int result;
950
951 hwmon = hwmon_device_register(dev);
952 if (IS_ERR(hwmon)) {
953 printk(EEEPC_ERR
954 "Could not register eeepc hwmon device\n");
955 eeepc_hwmon_device = NULL;
956 return PTR_ERR(hwmon);
957 }
958 eeepc_hwmon_device = hwmon;
959 result = sysfs_create_group(&hwmon->kobj,
960 &hwmon_attribute_group);
961 if (result)
962 eeepc_hwmon_exit();
963 return result;
964}
965
Eric Coopere59f8792008-03-13 12:55:46 +0100966static int __init eeepc_laptop_init(void)
967{
968 struct device *dev;
969 int result;
970
971 if (acpi_disabled)
972 return -ENODEV;
973 result = acpi_bus_register_driver(&eeepc_hotk_driver);
974 if (result < 0)
975 return result;
976 if (!ehotk) {
977 acpi_bus_unregister_driver(&eeepc_hotk_driver);
978 return -ENODEV;
979 }
980 dev = acpi_get_physical_device(ehotk->device->handle);
Thomas Renningera2bf8c02008-08-01 17:37:59 +0200981
982 if (!acpi_video_backlight_support()) {
983 result = eeepc_backlight_init(dev);
984 if (result)
985 goto fail_backlight;
986 } else
987 printk(EEEPC_INFO "Backlight controlled by ACPI video "
988 "driver\n");
989
Corentin Charye1faa9d2008-03-13 12:57:18 +0100990 result = eeepc_hwmon_init(dev);
991 if (result)
992 goto fail_hwmon;
Eric Coopere59f8792008-03-13 12:55:46 +0100993 /* Register platform stuff */
994 result = platform_driver_register(&platform_driver);
995 if (result)
996 goto fail_platform_driver;
997 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
998 if (!platform_device) {
999 result = -ENOMEM;
1000 goto fail_platform_device1;
1001 }
1002 result = platform_device_add(platform_device);
1003 if (result)
1004 goto fail_platform_device2;
1005 result = sysfs_create_group(&platform_device->dev.kobj,
1006 &platform_attribute_group);
1007 if (result)
1008 goto fail_sysfs;
1009 return 0;
1010fail_sysfs:
1011 platform_device_del(platform_device);
1012fail_platform_device2:
1013 platform_device_put(platform_device);
1014fail_platform_device1:
1015 platform_driver_unregister(&platform_driver);
1016fail_platform_driver:
Corentin Charye1faa9d2008-03-13 12:57:18 +01001017 eeepc_hwmon_exit();
1018fail_hwmon:
Corentin Charya5fa4292008-03-13 12:56:37 +01001019 eeepc_backlight_exit();
1020fail_backlight:
Corentin Charya9df80c2009-01-20 16:17:40 +01001021 eeepc_input_exit();
1022 eeepc_rfkill_exit();
Eric Coopere59f8792008-03-13 12:55:46 +01001023 return result;
1024}
1025
1026module_init(eeepc_laptop_init);
1027module_exit(eeepc_laptop_exit);