blob: 8153b3e5918967bb1662c44523d1ab7eab233d69 [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);
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100183static int eeepc_hotk_resume(struct acpi_device *device);
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,
195 .ops = {
196 .add = eeepc_hotk_add,
197 .remove = eeepc_hotk_remove,
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100198 .resume = eeepc_hotk_resume,
Eric Coopere59f8792008-03-13 12:55:46 +0100199 },
200};
201
Corentin Charya5fa4292008-03-13 12:56:37 +0100202/* The backlight device /sys/class/backlight */
203static struct backlight_device *eeepc_backlight_device;
204
Corentin Charye1faa9d2008-03-13 12:57:18 +0100205/* The hwmon device */
206static struct device *eeepc_hwmon_device;
207
Corentin Charya5fa4292008-03-13 12:56:37 +0100208/*
209 * The backlight class declaration
210 */
211static int read_brightness(struct backlight_device *bd);
212static int update_bl_status(struct backlight_device *bd);
213static struct backlight_ops eeepcbl_ops = {
214 .get_brightness = read_brightness,
215 .update_status = update_bl_status,
216};
217
Eric Coopere59f8792008-03-13 12:55:46 +0100218MODULE_AUTHOR("Corentin Chary, Eric Cooper");
219MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
220MODULE_LICENSE("GPL");
221
222/*
223 * ACPI Helpers
224 */
225static int write_acpi_int(acpi_handle handle, const char *method, int val,
226 struct acpi_buffer *output)
227{
228 struct acpi_object_list params;
229 union acpi_object in_obj;
230 acpi_status status;
231
232 params.count = 1;
233 params.pointer = &in_obj;
234 in_obj.type = ACPI_TYPE_INTEGER;
235 in_obj.integer.value = val;
236
237 status = acpi_evaluate_object(handle, (char *)method, &params, output);
238 return (status == AE_OK ? 0 : -1);
239}
240
241static int read_acpi_int(acpi_handle handle, const char *method, int *val)
242{
243 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400244 unsigned long long result;
Eric Coopere59f8792008-03-13 12:55:46 +0100245
246 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
247 if (ACPI_FAILURE(status)) {
248 *val = -1;
249 return -1;
250 } else {
251 *val = result;
252 return 0;
253 }
254}
255
256static int set_acpi(int cm, int value)
257{
258 if (ehotk->cm_supported & (0x1 << cm)) {
259 const char *method = cm_setv[cm];
260 if (method == NULL)
261 return -ENODEV;
262 if (write_acpi_int(ehotk->handle, method, value, NULL))
263 printk(EEEPC_WARNING "Error writing %s\n", method);
264 }
265 return 0;
266}
267
268static int get_acpi(int cm)
269{
270 int value = -1;
271 if ((ehotk->cm_supported & (0x1 << cm))) {
272 const char *method = cm_getv[cm];
273 if (method == NULL)
274 return -ENODEV;
275 if (read_acpi_int(ehotk->handle, method, &value))
276 printk(EEEPC_WARNING "Error reading %s\n", method);
277 }
278 return value;
279}
280
281/*
Corentin Charya5fa4292008-03-13 12:56:37 +0100282 * Backlight
283 */
284static int read_brightness(struct backlight_device *bd)
285{
286 return get_acpi(CM_ASL_PANELBRIGHT);
287}
288
289static int set_brightness(struct backlight_device *bd, int value)
290{
291 value = max(0, min(15, value));
292 return set_acpi(CM_ASL_PANELBRIGHT, value);
293}
294
295static int update_bl_status(struct backlight_device *bd)
296{
297 return set_brightness(bd, bd->props.brightness);
298}
299
300/*
Matthew Garretta195dcd2008-08-19 12:13:20 +0100301 * Rfkill helpers
302 */
303
Johannes Berg19d337d2009-06-02 13:01:37 +0200304static bool eeepc_wlan_rfkill_blocked(void)
Matthew Garretta195dcd2008-08-19 12:13:20 +0100305{
306 if (get_acpi(CM_ASL_WLAN) == 1)
Johannes Berg19d337d2009-06-02 13:01:37 +0200307 return false;
308 return true;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100309}
310
Johannes Berg19d337d2009-06-02 13:01:37 +0200311static int eeepc_rfkill_set(void *data, bool blocked)
Matthew Garretta195dcd2008-08-19 12:13:20 +0100312{
Johannes Berg19d337d2009-06-02 13:01:37 +0200313 unsigned long asl = (unsigned long)data;
314 return set_acpi(asl, !blocked);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100315}
316
Johannes Berg19d337d2009-06-02 13:01:37 +0200317static const struct rfkill_ops eeepc_rfkill_ops = {
318 .set_block = eeepc_rfkill_set,
319};
Matthew Garretta195dcd2008-08-19 12:13:20 +0100320
321/*
Eric Coopere59f8792008-03-13 12:55:46 +0100322 * Sys helpers
323 */
324static int parse_arg(const char *buf, unsigned long count, int *val)
325{
326 if (!count)
327 return 0;
328 if (sscanf(buf, "%i", val) != 1)
329 return -EINVAL;
330 return count;
331}
332
333static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
334{
335 int rv, value;
336
337 rv = parse_arg(buf, count, &value);
338 if (rv > 0)
339 set_acpi(cm, value);
340 return rv;
341}
342
343static ssize_t show_sys_acpi(int cm, char *buf)
344{
345 return sprintf(buf, "%d\n", get_acpi(cm));
346}
347
348#define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
349 static ssize_t show_##_name(struct device *dev, \
350 struct device_attribute *attr, \
351 char *buf) \
352 { \
353 return show_sys_acpi(_cm, buf); \
354 } \
355 static ssize_t store_##_name(struct device *dev, \
356 struct device_attribute *attr, \
357 const char *buf, size_t count) \
358 { \
359 return store_sys_acpi(_cm, buf, count); \
360 } \
361 static struct device_attribute dev_attr_##_name = { \
362 .attr = { \
363 .name = __stringify(_name), \
364 .mode = 0644 }, \
365 .show = show_##_name, \
366 .store = store_##_name, \
367 }
368
369EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
370EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
371EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
Grigori Goronzy158ca1d2009-04-27 09:23:40 +0200372EEEPC_CREATE_DEVICE_ATTR(cpufv, CM_ASL_CPUFV);
Eric Coopere59f8792008-03-13 12:55:46 +0100373
374static struct attribute *platform_attributes[] = {
375 &dev_attr_camera.attr,
376 &dev_attr_cardr.attr,
377 &dev_attr_disp.attr,
Grigori Goronzy158ca1d2009-04-27 09:23:40 +0200378 &dev_attr_cpufv.attr,
Eric Coopere59f8792008-03-13 12:55:46 +0100379 NULL
380};
381
382static struct attribute_group platform_attribute_group = {
383 .attrs = platform_attributes
384};
385
386/*
387 * Hotkey functions
388 */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100389static struct key_entry *eepc_get_entry_by_scancode(int code)
390{
391 struct key_entry *key;
392
393 for (key = eeepc_keymap; key->type != KE_END; key++)
394 if (code == key->code)
395 return key;
396
397 return NULL;
398}
399
400static struct key_entry *eepc_get_entry_by_keycode(int code)
401{
402 struct key_entry *key;
403
404 for (key = eeepc_keymap; key->type != KE_END; key++)
405 if (code == key->keycode && key->type == KE_KEY)
406 return key;
407
408 return NULL;
409}
410
411static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
412{
413 struct key_entry *key = eepc_get_entry_by_scancode(scancode);
414
415 if (key && key->type == KE_KEY) {
416 *keycode = key->keycode;
417 return 0;
418 }
419
420 return -EINVAL;
421}
422
423static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
424{
425 struct key_entry *key;
426 int old_keycode;
427
428 if (keycode < 0 || keycode > KEY_MAX)
429 return -EINVAL;
430
431 key = eepc_get_entry_by_scancode(scancode);
432 if (key && key->type == KE_KEY) {
433 old_keycode = key->keycode;
434 key->keycode = keycode;
435 set_bit(keycode, dev->keybit);
436 if (!eepc_get_entry_by_keycode(old_keycode))
437 clear_bit(old_keycode, dev->keybit);
438 return 0;
439 }
440
441 return -EINVAL;
442}
443
Eric Coopere59f8792008-03-13 12:55:46 +0100444static int eeepc_hotk_check(void)
445{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100446 const struct key_entry *key;
Eric Coopere59f8792008-03-13 12:55:46 +0100447 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
448 int result;
449
450 result = acpi_bus_get_status(ehotk->device);
451 if (result)
452 return result;
453 if (ehotk->device->status.present) {
454 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
455 &buffer)) {
456 printk(EEEPC_ERR "Hotkey initialization failed\n");
457 return -ENODEV;
458 } else {
459 printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
460 ehotk->init_flag);
461 }
462 /* get control methods supported */
463 if (read_acpi_int(ehotk->handle, "CMSG"
464 , &ehotk->cm_supported)) {
465 printk(EEEPC_ERR
466 "Get control methods supported failed\n");
467 return -ENODEV;
468 } else {
469 printk(EEEPC_INFO
470 "Get control methods supported: 0x%x\n",
471 ehotk->cm_supported);
472 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100473 ehotk->inputdev = input_allocate_device();
474 if (!ehotk->inputdev) {
475 printk(EEEPC_INFO "Unable to allocate input device\n");
476 return 0;
477 }
478 ehotk->inputdev->name = "Asus EeePC extra buttons";
479 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
480 ehotk->inputdev->id.bustype = BUS_HOST;
481 ehotk->inputdev->getkeycode = eeepc_getkeycode;
482 ehotk->inputdev->setkeycode = eeepc_setkeycode;
483
484 for (key = eeepc_keymap; key->type != KE_END; key++) {
485 switch (key->type) {
486 case KE_KEY:
487 set_bit(EV_KEY, ehotk->inputdev->evbit);
488 set_bit(key->keycode, ehotk->inputdev->keybit);
489 break;
490 }
491 }
492 result = input_register_device(ehotk->inputdev);
493 if (result) {
494 printk(EEEPC_INFO "Unable to register input device\n");
495 input_free_device(ehotk->inputdev);
496 return 0;
497 }
Eric Coopere59f8792008-03-13 12:55:46 +0100498 } else {
499 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
500 return -EINVAL;
501 }
502 return 0;
503}
504
Darren Salt64b86b62009-04-27 09:23:38 +0200505static int notify_brn(void)
Corentin Charya5fa4292008-03-13 12:56:37 +0100506{
Darren Salt64b86b62009-04-27 09:23:38 +0200507 /* returns the *previous* brightness, or -1 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100508 struct backlight_device *bd = eeepc_backlight_device;
Darren Salt64b86b62009-04-27 09:23:38 +0200509 if (bd) {
510 int old = bd->props.brightness;
Darren Salt7695fb02009-02-07 01:02:07 -0500511 bd->props.brightness = read_brightness(bd);
Darren Salt64b86b62009-04-27 09:23:38 +0200512 return old;
513 }
514 return -1;
Corentin Charya5fa4292008-03-13 12:56:37 +0100515}
516
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100517static void eeepc_rfkill_hotplug(void)
Matthew Garrett57402942009-01-20 16:17:48 +0100518{
519 struct pci_dev *dev;
520 struct pci_bus *bus = pci_find_bus(0, 1);
Johannes Berg19d337d2009-06-02 13:01:37 +0200521 bool blocked;
Matthew Garrett57402942009-01-20 16:17:48 +0100522
Matthew Garrett57402942009-01-20 16:17:48 +0100523 if (!bus) {
524 printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
525 return;
526 }
527
Johannes Berg19d337d2009-06-02 13:01:37 +0200528 blocked = eeepc_wlan_rfkill_blocked();
529 if (!blocked) {
Matthew Garrett57402942009-01-20 16:17:48 +0100530 dev = pci_get_slot(bus, 0);
531 if (dev) {
532 /* Device already present */
533 pci_dev_put(dev);
534 return;
535 }
536 dev = pci_scan_single_device(bus, 0);
537 if (dev) {
538 pci_bus_assign_resources(bus);
539 if (pci_bus_add_device(dev))
540 printk(EEEPC_ERR "Unable to hotplug wifi\n");
541 }
542 } else {
543 dev = pci_get_slot(bus, 0);
544 if (dev) {
545 pci_remove_bus_device(dev);
546 pci_dev_put(dev);
547 }
548 }
Alan Jenkins978605c2009-04-27 09:23:39 +0200549
Johannes Berg19d337d2009-06-02 13:01:37 +0200550 rfkill_set_sw_state(ehotk->eeepc_wlan_rfkill, blocked);
Matthew Garrett57402942009-01-20 16:17:48 +0100551}
552
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100553static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
554{
555 if (event != ACPI_NOTIFY_BUS_CHECK)
556 return;
557
558 eeepc_rfkill_hotplug();
559}
560
Eric Coopere59f8792008-03-13 12:55:46 +0100561static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
562{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100563 static struct key_entry *key;
Corentin Chary7950b712009-02-15 19:30:20 +0100564 u16 count;
Darren Salt64b86b62009-04-27 09:23:38 +0200565 int brn = -ENODEV;
Corentin Chary7950b712009-02-15 19:30:20 +0100566
Eric Coopere59f8792008-03-13 12:55:46 +0100567 if (!ehotk)
568 return;
Corentin Charya5fa4292008-03-13 12:56:37 +0100569 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
Darren Salt64b86b62009-04-27 09:23:38 +0200570 brn = notify_brn();
Corentin Chary7950b712009-02-15 19:30:20 +0100571 count = ehotk->event_count[event % 128]++;
572 acpi_bus_generate_proc_event(ehotk->device, event, count);
Corentin Chary2b25c9f2009-01-20 16:17:49 +0100573 acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
574 dev_name(&ehotk->device->dev), event,
Corentin Chary7950b712009-02-15 19:30:20 +0100575 count);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100576 if (ehotk->inputdev) {
Darren Salt64b86b62009-04-27 09:23:38 +0200577 if (brn != -ENODEV) {
578 /* brightness-change events need special
579 * handling for conversion to key events
580 */
581 if (brn < 0)
582 brn = event;
583 else
584 brn += NOTIFY_BRN_MIN;
585 if (event < brn)
586 event = NOTIFY_BRN_MIN; /* brightness down */
587 else if (event > brn)
588 event = NOTIFY_BRN_MIN + 2; /* ... up */
589 else
590 event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
591 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100592 key = eepc_get_entry_by_scancode(event);
593 if (key) {
594 switch (key->type) {
595 case KE_KEY:
596 input_report_key(ehotk->inputdev, key->keycode,
597 1);
598 input_sync(ehotk->inputdev);
599 input_report_key(ehotk->inputdev, key->keycode,
600 0);
601 input_sync(ehotk->inputdev);
602 break;
603 }
604 }
605 }
Eric Coopere59f8792008-03-13 12:55:46 +0100606}
607
Matthew Garrett57402942009-01-20 16:17:48 +0100608static int eeepc_register_rfkill_notifier(char *node)
609{
610 acpi_status status = AE_OK;
611 acpi_handle handle;
612
613 status = acpi_get_handle(NULL, node, &handle);
614
615 if (ACPI_SUCCESS(status)) {
616 status = acpi_install_notify_handler(handle,
617 ACPI_SYSTEM_NOTIFY,
618 eeepc_rfkill_notify,
619 NULL);
620 if (ACPI_FAILURE(status))
621 printk(EEEPC_WARNING
622 "Failed to register notify on %s\n", node);
623 } else
624 return -ENODEV;
625
626 return 0;
627}
628
629static void eeepc_unregister_rfkill_notifier(char *node)
630{
631 acpi_status status = AE_OK;
632 acpi_handle handle;
633
634 status = acpi_get_handle(NULL, node, &handle);
635
636 if (ACPI_SUCCESS(status)) {
637 status = acpi_remove_notify_handler(handle,
638 ACPI_SYSTEM_NOTIFY,
639 eeepc_rfkill_notify);
640 if (ACPI_FAILURE(status))
641 printk(EEEPC_ERR
642 "Error removing rfkill notify handler %s\n",
643 node);
644 }
645}
646
Eric Coopere59f8792008-03-13 12:55:46 +0100647static int eeepc_hotk_add(struct acpi_device *device)
648{
649 acpi_status status = AE_OK;
650 int result;
651
652 if (!device)
653 return -EINVAL;
654 printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
655 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
656 if (!ehotk)
657 return -ENOMEM;
658 ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
659 ehotk->handle = device->handle;
660 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
661 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700662 device->driver_data = ehotk;
Eric Coopere59f8792008-03-13 12:55:46 +0100663 ehotk->device = device;
664 result = eeepc_hotk_check();
665 if (result)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100666 goto ehotk_fail;
Eric Coopere59f8792008-03-13 12:55:46 +0100667 status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
668 eeepc_hotk_notify, ehotk);
669 if (ACPI_FAILURE(status))
670 printk(EEEPC_ERR "Error installing notify handler\n");
Matthew Garretta195dcd2008-08-19 12:13:20 +0100671
Alan Jenkinsfbc97e42009-04-27 09:23:37 +0200672 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
673 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
674
Matthew Garretta195dcd2008-08-19 12:13:20 +0100675 if (get_acpi(CM_ASL_WLAN) != -1) {
Johannes Berg19d337d2009-06-02 13:01:37 +0200676 ehotk->eeepc_wlan_rfkill = rfkill_alloc("eeepc-wlan",
677 &device->dev,
678 RFKILL_TYPE_WLAN,
679 &eeepc_rfkill_ops,
680 (void *)CM_ASL_WLAN);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100681
682 if (!ehotk->eeepc_wlan_rfkill)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100683 goto wlan_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100684
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100685 rfkill_init_sw_state(ehotk->eeepc_wlan_rfkill,
686 get_acpi(CM_ASL_WLAN) != 1);
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100687 result = rfkill_register(ehotk->eeepc_wlan_rfkill);
688 if (result)
689 goto wlan_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100690 }
691
692 if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
693 ehotk->eeepc_bluetooth_rfkill =
Johannes Berg19d337d2009-06-02 13:01:37 +0200694 rfkill_alloc("eeepc-bluetooth",
695 &device->dev,
696 RFKILL_TYPE_BLUETOOTH,
697 &eeepc_rfkill_ops,
698 (void *)CM_ASL_BLUETOOTH);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100699
700 if (!ehotk->eeepc_bluetooth_rfkill)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100701 goto bluetooth_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100702
Alan Jenkins06d5caf2009-06-16 15:39:51 +0100703 rfkill_init_sw_state(ehotk->eeepc_bluetooth_rfkill,
704 get_acpi(CM_ASL_BLUETOOTH) != 1);
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100705 result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
706 if (result)
707 goto bluetooth_fail;
Eric Coopere59f8792008-03-13 12:55:46 +0100708 }
Matthew Garrett57402942009-01-20 16:17:48 +0100709
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100710 return 0;
711
712 bluetooth_fail:
Johannes Berg19d337d2009-06-02 13:01:37 +0200713 rfkill_destroy(ehotk->eeepc_bluetooth_rfkill);
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100714 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100715 wlan_fail:
Johannes Berg19d337d2009-06-02 13:01:37 +0200716 rfkill_destroy(ehotk->eeepc_wlan_rfkill);
Corentin Charybd320052009-04-27 09:23:43 +0200717 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
718 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100719 ehotk_fail:
720 kfree(ehotk);
721 ehotk = NULL;
722
Eric Coopere59f8792008-03-13 12:55:46 +0100723 return result;
724}
725
726static int eeepc_hotk_remove(struct acpi_device *device, int type)
727{
728 acpi_status status = 0;
729
730 if (!device || !acpi_driver_data(device))
731 return -EINVAL;
732 status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
733 eeepc_hotk_notify);
734 if (ACPI_FAILURE(status))
735 printk(EEEPC_ERR "Error removing notify handler\n");
Matthew Garrett57402942009-01-20 16:17:48 +0100736
737 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
738 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
739
Eric Coopere59f8792008-03-13 12:55:46 +0100740 kfree(ehotk);
741 return 0;
742}
743
Alan Jenkins96e9cfe2009-06-16 14:53:52 +0100744static int eeepc_hotk_resume(struct acpi_device *device)
745{
746 if (ehotk->eeepc_wlan_rfkill) {
747 bool wlan;
748
749 /* Workaround - it seems that _PTS disables the wireless
750 without notification or changing the value read by WLAN.
751 Normally this is fine because the correct value is restored
752 from the non-volatile storage on resume, but we need to do
753 it ourself if case suspend is aborted, or we lose wireless.
754 */
755 wlan = get_acpi(CM_ASL_WLAN);
756 set_acpi(CM_ASL_WLAN, wlan);
757
758 rfkill_set_sw_state(ehotk->eeepc_wlan_rfkill,
759 wlan != 1);
760
761 eeepc_rfkill_hotplug();
762 }
763
764 if (ehotk->eeepc_bluetooth_rfkill)
765 rfkill_set_sw_state(ehotk->eeepc_bluetooth_rfkill,
766 get_acpi(CM_ASL_BLUETOOTH) != 1);
767
768 return 0;
769}
770
Eric Coopere59f8792008-03-13 12:55:46 +0100771/*
Corentin Charye1faa9d2008-03-13 12:57:18 +0100772 * Hwmon
773 */
774static int eeepc_get_fan_pwm(void)
775{
776 int value = 0;
777
778 read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
Corentin Chary04dcd842008-10-09 15:33:57 +0200779 value = value * 255 / 100;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100780 return (value);
781}
782
783static void eeepc_set_fan_pwm(int value)
784{
Corentin Chary04dcd842008-10-09 15:33:57 +0200785 value = SENSORS_LIMIT(value, 0, 255);
786 value = value * 100 / 255;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100787 ec_write(EEEPC_EC_SC02, value);
788}
789
790static int eeepc_get_fan_rpm(void)
791{
792 int high = 0;
793 int low = 0;
794
795 read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
796 read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
797 return (high << 8 | low);
798}
799
800static int eeepc_get_fan_ctrl(void)
801{
802 int value = 0;
803
804 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
805 return ((value & 0x02 ? 1 : 0));
806}
807
808static void eeepc_set_fan_ctrl(int manual)
809{
810 int value = 0;
811
812 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
813 if (manual)
814 value |= 0x02;
815 else
816 value &= ~0x02;
817 ec_write(EEEPC_EC_SFB3, value);
818}
819
820static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
821{
822 int rv, value;
823
824 rv = parse_arg(buf, count, &value);
825 if (rv > 0)
826 set(value);
827 return rv;
828}
829
830static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
831{
832 return sprintf(buf, "%d\n", get());
833}
834
835#define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
836 static ssize_t show_##_name(struct device *dev, \
837 struct device_attribute *attr, \
838 char *buf) \
839 { \
840 return show_sys_hwmon(_set, buf); \
841 } \
842 static ssize_t store_##_name(struct device *dev, \
843 struct device_attribute *attr, \
844 const char *buf, size_t count) \
845 { \
846 return store_sys_hwmon(_get, buf, count); \
847 } \
848 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
849
850EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
Corentin Chary04dcd842008-10-09 15:33:57 +0200851EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100852 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
853EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
854 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
855
Corentin Chary04dcd842008-10-09 15:33:57 +0200856static ssize_t
857show_name(struct device *dev, struct device_attribute *attr, char *buf)
858{
859 return sprintf(buf, "eeepc\n");
860}
861static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
862
Corentin Charye1faa9d2008-03-13 12:57:18 +0100863static struct attribute *hwmon_attributes[] = {
Corentin Chary04dcd842008-10-09 15:33:57 +0200864 &sensor_dev_attr_pwm1.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100865 &sensor_dev_attr_fan1_input.dev_attr.attr,
866 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Corentin Chary04dcd842008-10-09 15:33:57 +0200867 &sensor_dev_attr_name.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100868 NULL
869};
870
871static struct attribute_group hwmon_attribute_group = {
872 .attrs = hwmon_attributes
873};
874
875/*
Eric Coopere59f8792008-03-13 12:55:46 +0100876 * exit/init
877 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100878static void eeepc_backlight_exit(void)
879{
880 if (eeepc_backlight_device)
881 backlight_device_unregister(eeepc_backlight_device);
Corentin Charya9df80c2009-01-20 16:17:40 +0100882 eeepc_backlight_device = NULL;
883}
884
885static void eeepc_rfkill_exit(void)
886{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100887 if (ehotk->eeepc_wlan_rfkill)
888 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
889 if (ehotk->eeepc_bluetooth_rfkill)
890 rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
Corentin Charya9df80c2009-01-20 16:17:40 +0100891}
892
893static void eeepc_input_exit(void)
894{
895 if (ehotk->inputdev)
896 input_unregister_device(ehotk->inputdev);
Corentin Charya5fa4292008-03-13 12:56:37 +0100897}
898
Corentin Charye1faa9d2008-03-13 12:57:18 +0100899static void eeepc_hwmon_exit(void)
900{
901 struct device *hwmon;
902
903 hwmon = eeepc_hwmon_device;
904 if (!hwmon)
905 return ;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100906 sysfs_remove_group(&hwmon->kobj,
907 &hwmon_attribute_group);
Matthew Garrettf1441312008-08-20 14:08:57 -0700908 hwmon_device_unregister(hwmon);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100909 eeepc_hwmon_device = NULL;
910}
911
Eric Coopere59f8792008-03-13 12:55:46 +0100912static void __exit eeepc_laptop_exit(void)
913{
Corentin Charya5fa4292008-03-13 12:56:37 +0100914 eeepc_backlight_exit();
Corentin Charya9df80c2009-01-20 16:17:40 +0100915 eeepc_rfkill_exit();
916 eeepc_input_exit();
Corentin Charye1faa9d2008-03-13 12:57:18 +0100917 eeepc_hwmon_exit();
Eric Coopere59f8792008-03-13 12:55:46 +0100918 acpi_bus_unregister_driver(&eeepc_hotk_driver);
919 sysfs_remove_group(&platform_device->dev.kobj,
920 &platform_attribute_group);
921 platform_device_unregister(platform_device);
922 platform_driver_unregister(&platform_driver);
923}
924
Corentin Charya5fa4292008-03-13 12:56:37 +0100925static int eeepc_backlight_init(struct device *dev)
926{
927 struct backlight_device *bd;
928
929 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
930 NULL, &eeepcbl_ops);
931 if (IS_ERR(bd)) {
932 printk(EEEPC_ERR
933 "Could not register eeepc backlight device\n");
934 eeepc_backlight_device = NULL;
935 return PTR_ERR(bd);
936 }
937 eeepc_backlight_device = bd;
938 bd->props.max_brightness = 15;
939 bd->props.brightness = read_brightness(NULL);
940 bd->props.power = FB_BLANK_UNBLANK;
941 backlight_update_status(bd);
942 return 0;
943}
944
Corentin Charye1faa9d2008-03-13 12:57:18 +0100945static int eeepc_hwmon_init(struct device *dev)
946{
947 struct device *hwmon;
948 int result;
949
950 hwmon = hwmon_device_register(dev);
951 if (IS_ERR(hwmon)) {
952 printk(EEEPC_ERR
953 "Could not register eeepc hwmon device\n");
954 eeepc_hwmon_device = NULL;
955 return PTR_ERR(hwmon);
956 }
957 eeepc_hwmon_device = hwmon;
958 result = sysfs_create_group(&hwmon->kobj,
959 &hwmon_attribute_group);
960 if (result)
961 eeepc_hwmon_exit();
962 return result;
963}
964
Eric Coopere59f8792008-03-13 12:55:46 +0100965static int __init eeepc_laptop_init(void)
966{
967 struct device *dev;
968 int result;
969
970 if (acpi_disabled)
971 return -ENODEV;
972 result = acpi_bus_register_driver(&eeepc_hotk_driver);
973 if (result < 0)
974 return result;
975 if (!ehotk) {
976 acpi_bus_unregister_driver(&eeepc_hotk_driver);
977 return -ENODEV;
978 }
979 dev = acpi_get_physical_device(ehotk->device->handle);
Thomas Renningera2bf8c02008-08-01 17:37:59 +0200980
981 if (!acpi_video_backlight_support()) {
982 result = eeepc_backlight_init(dev);
983 if (result)
984 goto fail_backlight;
985 } else
986 printk(EEEPC_INFO "Backlight controlled by ACPI video "
987 "driver\n");
988
Corentin Charye1faa9d2008-03-13 12:57:18 +0100989 result = eeepc_hwmon_init(dev);
990 if (result)
991 goto fail_hwmon;
Eric Coopere59f8792008-03-13 12:55:46 +0100992 /* Register platform stuff */
993 result = platform_driver_register(&platform_driver);
994 if (result)
995 goto fail_platform_driver;
996 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
997 if (!platform_device) {
998 result = -ENOMEM;
999 goto fail_platform_device1;
1000 }
1001 result = platform_device_add(platform_device);
1002 if (result)
1003 goto fail_platform_device2;
1004 result = sysfs_create_group(&platform_device->dev.kobj,
1005 &platform_attribute_group);
1006 if (result)
1007 goto fail_sysfs;
1008 return 0;
1009fail_sysfs:
1010 platform_device_del(platform_device);
1011fail_platform_device2:
1012 platform_device_put(platform_device);
1013fail_platform_device1:
1014 platform_driver_unregister(&platform_driver);
1015fail_platform_driver:
Corentin Charye1faa9d2008-03-13 12:57:18 +01001016 eeepc_hwmon_exit();
1017fail_hwmon:
Corentin Charya5fa4292008-03-13 12:56:37 +01001018 eeepc_backlight_exit();
1019fail_backlight:
Corentin Charya9df80c2009-01-20 16:17:40 +01001020 eeepc_input_exit();
1021 eeepc_rfkill_exit();
Eric Coopere59f8792008-03-13 12:55:46 +01001022 return result;
1023}
1024
1025module_init(eeepc_laptop_init);
1026module_exit(eeepc_laptop_exit);