blob: f54cfeac522193fd63fccb18b6cc20b361492679 [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 },
161 {KE_KEY, 0x12, KEY_PROG1 },
162 {KE_KEY, 0x13, KEY_MUTE },
163 {KE_KEY, 0x14, KEY_VOLUMEDOWN },
164 {KE_KEY, 0x15, KEY_VOLUMEUP },
Matthew Garrettb5f6f262009-01-20 16:17:46 +0100165 {KE_KEY, 0x1a, KEY_COFFEE },
166 {KE_KEY, 0x1b, KEY_ZOOM },
167 {KE_KEY, 0x1c, KEY_PROG2 },
168 {KE_KEY, 0x1d, KEY_PROG3 },
Darren Salt64b86b62009-04-27 09:23:38 +0200169 {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
170 {KE_KEY, NOTIFY_BRN_MIN + 2, KEY_BRIGHTNESSUP },
Matthew Garretta195dcd2008-08-19 12:13:20 +0100171 {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
172 {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
173 {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
174 {KE_END, 0},
175};
176
Eric Coopere59f8792008-03-13 12:55:46 +0100177/*
178 * The hotkey driver declaration
179 */
180static int eeepc_hotk_add(struct acpi_device *device);
181static int eeepc_hotk_remove(struct acpi_device *device, int type);
182
183static const struct acpi_device_id eeepc_device_ids[] = {
184 {EEEPC_HOTK_HID, 0},
185 {"", 0},
186};
187MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
188
189static struct acpi_driver eeepc_hotk_driver = {
190 .name = EEEPC_HOTK_NAME,
191 .class = EEEPC_HOTK_CLASS,
192 .ids = eeepc_device_ids,
193 .ops = {
194 .add = eeepc_hotk_add,
195 .remove = eeepc_hotk_remove,
196 },
197};
198
Corentin Charya5fa4292008-03-13 12:56:37 +0100199/* The backlight device /sys/class/backlight */
200static struct backlight_device *eeepc_backlight_device;
201
Corentin Charye1faa9d2008-03-13 12:57:18 +0100202/* The hwmon device */
203static struct device *eeepc_hwmon_device;
204
Corentin Charya5fa4292008-03-13 12:56:37 +0100205/*
206 * The backlight class declaration
207 */
208static int read_brightness(struct backlight_device *bd);
209static int update_bl_status(struct backlight_device *bd);
210static struct backlight_ops eeepcbl_ops = {
211 .get_brightness = read_brightness,
212 .update_status = update_bl_status,
213};
214
Eric Coopere59f8792008-03-13 12:55:46 +0100215MODULE_AUTHOR("Corentin Chary, Eric Cooper");
216MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
217MODULE_LICENSE("GPL");
218
219/*
220 * ACPI Helpers
221 */
222static int write_acpi_int(acpi_handle handle, const char *method, int val,
223 struct acpi_buffer *output)
224{
225 struct acpi_object_list params;
226 union acpi_object in_obj;
227 acpi_status status;
228
229 params.count = 1;
230 params.pointer = &in_obj;
231 in_obj.type = ACPI_TYPE_INTEGER;
232 in_obj.integer.value = val;
233
234 status = acpi_evaluate_object(handle, (char *)method, &params, output);
235 return (status == AE_OK ? 0 : -1);
236}
237
238static int read_acpi_int(acpi_handle handle, const char *method, int *val)
239{
240 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400241 unsigned long long result;
Eric Coopere59f8792008-03-13 12:55:46 +0100242
243 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
244 if (ACPI_FAILURE(status)) {
245 *val = -1;
246 return -1;
247 } else {
248 *val = result;
249 return 0;
250 }
251}
252
253static int set_acpi(int cm, int value)
254{
255 if (ehotk->cm_supported & (0x1 << cm)) {
256 const char *method = cm_setv[cm];
257 if (method == NULL)
258 return -ENODEV;
259 if (write_acpi_int(ehotk->handle, method, value, NULL))
260 printk(EEEPC_WARNING "Error writing %s\n", method);
261 }
262 return 0;
263}
264
265static int get_acpi(int cm)
266{
267 int value = -1;
268 if ((ehotk->cm_supported & (0x1 << cm))) {
269 const char *method = cm_getv[cm];
270 if (method == NULL)
271 return -ENODEV;
272 if (read_acpi_int(ehotk->handle, method, &value))
273 printk(EEEPC_WARNING "Error reading %s\n", method);
274 }
275 return value;
276}
277
278/*
Corentin Charya5fa4292008-03-13 12:56:37 +0100279 * Backlight
280 */
281static int read_brightness(struct backlight_device *bd)
282{
283 return get_acpi(CM_ASL_PANELBRIGHT);
284}
285
286static int set_brightness(struct backlight_device *bd, int value)
287{
288 value = max(0, min(15, value));
289 return set_acpi(CM_ASL_PANELBRIGHT, value);
290}
291
292static int update_bl_status(struct backlight_device *bd)
293{
294 return set_brightness(bd, bd->props.brightness);
295}
296
297/*
Matthew Garretta195dcd2008-08-19 12:13:20 +0100298 * Rfkill helpers
299 */
300
301static int eeepc_wlan_rfkill_set(void *data, enum rfkill_state state)
302{
303 if (state == RFKILL_STATE_SOFT_BLOCKED)
304 return set_acpi(CM_ASL_WLAN, 0);
305 else
306 return set_acpi(CM_ASL_WLAN, 1);
307}
308
309static int eeepc_wlan_rfkill_state(void *data, enum rfkill_state *state)
310{
311 if (get_acpi(CM_ASL_WLAN) == 1)
312 *state = RFKILL_STATE_UNBLOCKED;
313 else
314 *state = RFKILL_STATE_SOFT_BLOCKED;
315 return 0;
316}
317
318static int eeepc_bluetooth_rfkill_set(void *data, enum rfkill_state state)
319{
320 if (state == RFKILL_STATE_SOFT_BLOCKED)
321 return set_acpi(CM_ASL_BLUETOOTH, 0);
322 else
323 return set_acpi(CM_ASL_BLUETOOTH, 1);
324}
325
326static int eeepc_bluetooth_rfkill_state(void *data, enum rfkill_state *state)
327{
328 if (get_acpi(CM_ASL_BLUETOOTH) == 1)
329 *state = RFKILL_STATE_UNBLOCKED;
330 else
331 *state = RFKILL_STATE_SOFT_BLOCKED;
332 return 0;
333}
334
335/*
Eric Coopere59f8792008-03-13 12:55:46 +0100336 * Sys helpers
337 */
338static int parse_arg(const char *buf, unsigned long count, int *val)
339{
340 if (!count)
341 return 0;
342 if (sscanf(buf, "%i", val) != 1)
343 return -EINVAL;
344 return count;
345}
346
347static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
348{
349 int rv, value;
350
351 rv = parse_arg(buf, count, &value);
352 if (rv > 0)
353 set_acpi(cm, value);
354 return rv;
355}
356
357static ssize_t show_sys_acpi(int cm, char *buf)
358{
359 return sprintf(buf, "%d\n", get_acpi(cm));
360}
361
362#define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
363 static ssize_t show_##_name(struct device *dev, \
364 struct device_attribute *attr, \
365 char *buf) \
366 { \
367 return show_sys_acpi(_cm, buf); \
368 } \
369 static ssize_t store_##_name(struct device *dev, \
370 struct device_attribute *attr, \
371 const char *buf, size_t count) \
372 { \
373 return store_sys_acpi(_cm, buf, count); \
374 } \
375 static struct device_attribute dev_attr_##_name = { \
376 .attr = { \
377 .name = __stringify(_name), \
378 .mode = 0644 }, \
379 .show = show_##_name, \
380 .store = store_##_name, \
381 }
382
383EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
384EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
385EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
Eric Coopere59f8792008-03-13 12:55:46 +0100386
387static struct attribute *platform_attributes[] = {
388 &dev_attr_camera.attr,
389 &dev_attr_cardr.attr,
390 &dev_attr_disp.attr,
Eric Coopere59f8792008-03-13 12:55:46 +0100391 NULL
392};
393
394static struct attribute_group platform_attribute_group = {
395 .attrs = platform_attributes
396};
397
398/*
399 * Hotkey functions
400 */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100401static struct key_entry *eepc_get_entry_by_scancode(int code)
402{
403 struct key_entry *key;
404
405 for (key = eeepc_keymap; key->type != KE_END; key++)
406 if (code == key->code)
407 return key;
408
409 return NULL;
410}
411
412static struct key_entry *eepc_get_entry_by_keycode(int code)
413{
414 struct key_entry *key;
415
416 for (key = eeepc_keymap; key->type != KE_END; key++)
417 if (code == key->keycode && key->type == KE_KEY)
418 return key;
419
420 return NULL;
421}
422
423static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
424{
425 struct key_entry *key = eepc_get_entry_by_scancode(scancode);
426
427 if (key && key->type == KE_KEY) {
428 *keycode = key->keycode;
429 return 0;
430 }
431
432 return -EINVAL;
433}
434
435static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
436{
437 struct key_entry *key;
438 int old_keycode;
439
440 if (keycode < 0 || keycode > KEY_MAX)
441 return -EINVAL;
442
443 key = eepc_get_entry_by_scancode(scancode);
444 if (key && key->type == KE_KEY) {
445 old_keycode = key->keycode;
446 key->keycode = keycode;
447 set_bit(keycode, dev->keybit);
448 if (!eepc_get_entry_by_keycode(old_keycode))
449 clear_bit(old_keycode, dev->keybit);
450 return 0;
451 }
452
453 return -EINVAL;
454}
455
Eric Coopere59f8792008-03-13 12:55:46 +0100456static int eeepc_hotk_check(void)
457{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100458 const struct key_entry *key;
Eric Coopere59f8792008-03-13 12:55:46 +0100459 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
460 int result;
461
462 result = acpi_bus_get_status(ehotk->device);
463 if (result)
464 return result;
465 if (ehotk->device->status.present) {
466 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
467 &buffer)) {
468 printk(EEEPC_ERR "Hotkey initialization failed\n");
469 return -ENODEV;
470 } else {
471 printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
472 ehotk->init_flag);
473 }
474 /* get control methods supported */
475 if (read_acpi_int(ehotk->handle, "CMSG"
476 , &ehotk->cm_supported)) {
477 printk(EEEPC_ERR
478 "Get control methods supported failed\n");
479 return -ENODEV;
480 } else {
481 printk(EEEPC_INFO
482 "Get control methods supported: 0x%x\n",
483 ehotk->cm_supported);
484 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100485 ehotk->inputdev = input_allocate_device();
486 if (!ehotk->inputdev) {
487 printk(EEEPC_INFO "Unable to allocate input device\n");
488 return 0;
489 }
490 ehotk->inputdev->name = "Asus EeePC extra buttons";
491 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
492 ehotk->inputdev->id.bustype = BUS_HOST;
493 ehotk->inputdev->getkeycode = eeepc_getkeycode;
494 ehotk->inputdev->setkeycode = eeepc_setkeycode;
495
496 for (key = eeepc_keymap; key->type != KE_END; key++) {
497 switch (key->type) {
498 case KE_KEY:
499 set_bit(EV_KEY, ehotk->inputdev->evbit);
500 set_bit(key->keycode, ehotk->inputdev->keybit);
501 break;
502 }
503 }
504 result = input_register_device(ehotk->inputdev);
505 if (result) {
506 printk(EEEPC_INFO "Unable to register input device\n");
507 input_free_device(ehotk->inputdev);
508 return 0;
509 }
Eric Coopere59f8792008-03-13 12:55:46 +0100510 } else {
511 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
512 return -EINVAL;
513 }
514 return 0;
515}
516
Darren Salt64b86b62009-04-27 09:23:38 +0200517static int notify_brn(void)
Corentin Charya5fa4292008-03-13 12:56:37 +0100518{
Darren Salt64b86b62009-04-27 09:23:38 +0200519 /* returns the *previous* brightness, or -1 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100520 struct backlight_device *bd = eeepc_backlight_device;
Darren Salt64b86b62009-04-27 09:23:38 +0200521 if (bd) {
522 int old = bd->props.brightness;
Darren Salt7695fb02009-02-07 01:02:07 -0500523 bd->props.brightness = read_brightness(bd);
Darren Salt64b86b62009-04-27 09:23:38 +0200524 return old;
525 }
526 return -1;
Corentin Charya5fa4292008-03-13 12:56:37 +0100527}
528
Matthew Garrett57402942009-01-20 16:17:48 +0100529static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
530{
531 struct pci_dev *dev;
532 struct pci_bus *bus = pci_find_bus(0, 1);
533
534 if (event != ACPI_NOTIFY_BUS_CHECK)
535 return;
536
537 if (!bus) {
538 printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
539 return;
540 }
541
542 if (get_acpi(CM_ASL_WLAN) == 1) {
543 dev = pci_get_slot(bus, 0);
544 if (dev) {
545 /* Device already present */
546 pci_dev_put(dev);
547 return;
548 }
549 dev = pci_scan_single_device(bus, 0);
550 if (dev) {
551 pci_bus_assign_resources(bus);
552 if (pci_bus_add_device(dev))
553 printk(EEEPC_ERR "Unable to hotplug wifi\n");
554 }
555 } else {
556 dev = pci_get_slot(bus, 0);
557 if (dev) {
558 pci_remove_bus_device(dev);
559 pci_dev_put(dev);
560 }
561 }
562}
563
Eric Coopere59f8792008-03-13 12:55:46 +0100564static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
565{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100566 static struct key_entry *key;
Corentin Chary7950b712009-02-15 19:30:20 +0100567 u16 count;
Darren Salt64b86b62009-04-27 09:23:38 +0200568 int brn = -ENODEV;
Corentin Chary7950b712009-02-15 19:30:20 +0100569
Eric Coopere59f8792008-03-13 12:55:46 +0100570 if (!ehotk)
571 return;
Corentin Charya5fa4292008-03-13 12:56:37 +0100572 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
Darren Salt64b86b62009-04-27 09:23:38 +0200573 brn = notify_brn();
Corentin Chary7950b712009-02-15 19:30:20 +0100574 count = ehotk->event_count[event % 128]++;
575 acpi_bus_generate_proc_event(ehotk->device, event, count);
Corentin Chary2b25c9f2009-01-20 16:17:49 +0100576 acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
577 dev_name(&ehotk->device->dev), event,
Corentin Chary7950b712009-02-15 19:30:20 +0100578 count);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100579 if (ehotk->inputdev) {
Darren Salt64b86b62009-04-27 09:23:38 +0200580 if (brn != -ENODEV) {
581 /* brightness-change events need special
582 * handling for conversion to key events
583 */
584 if (brn < 0)
585 brn = event;
586 else
587 brn += NOTIFY_BRN_MIN;
588 if (event < brn)
589 event = NOTIFY_BRN_MIN; /* brightness down */
590 else if (event > brn)
591 event = NOTIFY_BRN_MIN + 2; /* ... up */
592 else
593 event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
594 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100595 key = eepc_get_entry_by_scancode(event);
596 if (key) {
597 switch (key->type) {
598 case KE_KEY:
599 input_report_key(ehotk->inputdev, key->keycode,
600 1);
601 input_sync(ehotk->inputdev);
602 input_report_key(ehotk->inputdev, key->keycode,
603 0);
604 input_sync(ehotk->inputdev);
605 break;
606 }
607 }
608 }
Eric Coopere59f8792008-03-13 12:55:46 +0100609}
610
Matthew Garrett57402942009-01-20 16:17:48 +0100611static int eeepc_register_rfkill_notifier(char *node)
612{
613 acpi_status status = AE_OK;
614 acpi_handle handle;
615
616 status = acpi_get_handle(NULL, node, &handle);
617
618 if (ACPI_SUCCESS(status)) {
619 status = acpi_install_notify_handler(handle,
620 ACPI_SYSTEM_NOTIFY,
621 eeepc_rfkill_notify,
622 NULL);
623 if (ACPI_FAILURE(status))
624 printk(EEEPC_WARNING
625 "Failed to register notify on %s\n", node);
626 } else
627 return -ENODEV;
628
629 return 0;
630}
631
632static void eeepc_unregister_rfkill_notifier(char *node)
633{
634 acpi_status status = AE_OK;
635 acpi_handle handle;
636
637 status = acpi_get_handle(NULL, node, &handle);
638
639 if (ACPI_SUCCESS(status)) {
640 status = acpi_remove_notify_handler(handle,
641 ACPI_SYSTEM_NOTIFY,
642 eeepc_rfkill_notify);
643 if (ACPI_FAILURE(status))
644 printk(EEEPC_ERR
645 "Error removing rfkill notify handler %s\n",
646 node);
647 }
648}
649
Eric Coopere59f8792008-03-13 12:55:46 +0100650static int eeepc_hotk_add(struct acpi_device *device)
651{
652 acpi_status status = AE_OK;
653 int result;
654
655 if (!device)
656 return -EINVAL;
657 printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
658 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
659 if (!ehotk)
660 return -ENOMEM;
661 ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
662 ehotk->handle = device->handle;
663 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
664 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700665 device->driver_data = ehotk;
Eric Coopere59f8792008-03-13 12:55:46 +0100666 ehotk->device = device;
667 result = eeepc_hotk_check();
668 if (result)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100669 goto ehotk_fail;
Eric Coopere59f8792008-03-13 12:55:46 +0100670 status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
671 eeepc_hotk_notify, ehotk);
672 if (ACPI_FAILURE(status))
673 printk(EEEPC_ERR "Error installing notify handler\n");
Matthew Garretta195dcd2008-08-19 12:13:20 +0100674
Alan Jenkinsfbc97e42009-04-27 09:23:37 +0200675 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
676 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
677
Matthew Garretta195dcd2008-08-19 12:13:20 +0100678 if (get_acpi(CM_ASL_WLAN) != -1) {
679 ehotk->eeepc_wlan_rfkill = rfkill_allocate(&device->dev,
680 RFKILL_TYPE_WLAN);
681
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
685 ehotk->eeepc_wlan_rfkill->name = "eeepc-wlan";
686 ehotk->eeepc_wlan_rfkill->toggle_radio = eeepc_wlan_rfkill_set;
687 ehotk->eeepc_wlan_rfkill->get_state = eeepc_wlan_rfkill_state;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100688 if (get_acpi(CM_ASL_WLAN) == 1) {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100689 ehotk->eeepc_wlan_rfkill->state =
690 RFKILL_STATE_UNBLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100691 rfkill_set_default(RFKILL_TYPE_WLAN,
692 RFKILL_STATE_UNBLOCKED);
693 } else {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100694 ehotk->eeepc_wlan_rfkill->state =
695 RFKILL_STATE_SOFT_BLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100696 rfkill_set_default(RFKILL_TYPE_WLAN,
697 RFKILL_STATE_SOFT_BLOCKED);
698 }
699 result = rfkill_register(ehotk->eeepc_wlan_rfkill);
700 if (result)
701 goto wlan_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100702 }
703
704 if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
705 ehotk->eeepc_bluetooth_rfkill =
706 rfkill_allocate(&device->dev, RFKILL_TYPE_BLUETOOTH);
707
708 if (!ehotk->eeepc_bluetooth_rfkill)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100709 goto bluetooth_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100710
711 ehotk->eeepc_bluetooth_rfkill->name = "eeepc-bluetooth";
712 ehotk->eeepc_bluetooth_rfkill->toggle_radio =
713 eeepc_bluetooth_rfkill_set;
714 ehotk->eeepc_bluetooth_rfkill->get_state =
715 eeepc_bluetooth_rfkill_state;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100716 if (get_acpi(CM_ASL_BLUETOOTH) == 1) {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100717 ehotk->eeepc_bluetooth_rfkill->state =
718 RFKILL_STATE_UNBLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100719 rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
720 RFKILL_STATE_UNBLOCKED);
721 } else {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100722 ehotk->eeepc_bluetooth_rfkill->state =
723 RFKILL_STATE_SOFT_BLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100724 rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
725 RFKILL_STATE_SOFT_BLOCKED);
726 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100727
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100728 result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
729 if (result)
730 goto bluetooth_fail;
Eric Coopere59f8792008-03-13 12:55:46 +0100731 }
Matthew Garrett57402942009-01-20 16:17:48 +0100732
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100733 return 0;
734
735 bluetooth_fail:
736 if (ehotk->eeepc_bluetooth_rfkill)
737 rfkill_free(ehotk->eeepc_bluetooth_rfkill);
738 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
739 ehotk->eeepc_wlan_rfkill = NULL;
740 wlan_fail:
741 if (ehotk->eeepc_wlan_rfkill)
742 rfkill_free(ehotk->eeepc_wlan_rfkill);
743 ehotk_fail:
744 kfree(ehotk);
745 ehotk = NULL;
746
Eric Coopere59f8792008-03-13 12:55:46 +0100747 return result;
748}
749
750static int eeepc_hotk_remove(struct acpi_device *device, int type)
751{
752 acpi_status status = 0;
753
754 if (!device || !acpi_driver_data(device))
755 return -EINVAL;
756 status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
757 eeepc_hotk_notify);
758 if (ACPI_FAILURE(status))
759 printk(EEEPC_ERR "Error removing notify handler\n");
Matthew Garrett57402942009-01-20 16:17:48 +0100760
761 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
762 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
763
Eric Coopere59f8792008-03-13 12:55:46 +0100764 kfree(ehotk);
765 return 0;
766}
767
768/*
Corentin Charye1faa9d2008-03-13 12:57:18 +0100769 * Hwmon
770 */
771static int eeepc_get_fan_pwm(void)
772{
773 int value = 0;
774
775 read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
Corentin Chary04dcd842008-10-09 15:33:57 +0200776 value = value * 255 / 100;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100777 return (value);
778}
779
780static void eeepc_set_fan_pwm(int value)
781{
Corentin Chary04dcd842008-10-09 15:33:57 +0200782 value = SENSORS_LIMIT(value, 0, 255);
783 value = value * 100 / 255;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100784 ec_write(EEEPC_EC_SC02, value);
785}
786
787static int eeepc_get_fan_rpm(void)
788{
789 int high = 0;
790 int low = 0;
791
792 read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
793 read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
794 return (high << 8 | low);
795}
796
797static int eeepc_get_fan_ctrl(void)
798{
799 int value = 0;
800
801 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
802 return ((value & 0x02 ? 1 : 0));
803}
804
805static void eeepc_set_fan_ctrl(int manual)
806{
807 int value = 0;
808
809 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
810 if (manual)
811 value |= 0x02;
812 else
813 value &= ~0x02;
814 ec_write(EEEPC_EC_SFB3, value);
815}
816
817static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
818{
819 int rv, value;
820
821 rv = parse_arg(buf, count, &value);
822 if (rv > 0)
823 set(value);
824 return rv;
825}
826
827static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
828{
829 return sprintf(buf, "%d\n", get());
830}
831
832#define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
833 static ssize_t show_##_name(struct device *dev, \
834 struct device_attribute *attr, \
835 char *buf) \
836 { \
837 return show_sys_hwmon(_set, buf); \
838 } \
839 static ssize_t store_##_name(struct device *dev, \
840 struct device_attribute *attr, \
841 const char *buf, size_t count) \
842 { \
843 return store_sys_hwmon(_get, buf, count); \
844 } \
845 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
846
847EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
Corentin Chary04dcd842008-10-09 15:33:57 +0200848EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100849 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
850EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
851 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
852
Corentin Chary04dcd842008-10-09 15:33:57 +0200853static ssize_t
854show_name(struct device *dev, struct device_attribute *attr, char *buf)
855{
856 return sprintf(buf, "eeepc\n");
857}
858static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
859
Corentin Charye1faa9d2008-03-13 12:57:18 +0100860static struct attribute *hwmon_attributes[] = {
Corentin Chary04dcd842008-10-09 15:33:57 +0200861 &sensor_dev_attr_pwm1.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100862 &sensor_dev_attr_fan1_input.dev_attr.attr,
863 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Corentin Chary04dcd842008-10-09 15:33:57 +0200864 &sensor_dev_attr_name.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100865 NULL
866};
867
868static struct attribute_group hwmon_attribute_group = {
869 .attrs = hwmon_attributes
870};
871
872/*
Eric Coopere59f8792008-03-13 12:55:46 +0100873 * exit/init
874 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100875static void eeepc_backlight_exit(void)
876{
877 if (eeepc_backlight_device)
878 backlight_device_unregister(eeepc_backlight_device);
Corentin Charya9df80c2009-01-20 16:17:40 +0100879 eeepc_backlight_device = NULL;
880}
881
882static void eeepc_rfkill_exit(void)
883{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100884 if (ehotk->eeepc_wlan_rfkill)
885 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
886 if (ehotk->eeepc_bluetooth_rfkill)
887 rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
Corentin Charya9df80c2009-01-20 16:17:40 +0100888}
889
890static void eeepc_input_exit(void)
891{
892 if (ehotk->inputdev)
893 input_unregister_device(ehotk->inputdev);
Corentin Charya5fa4292008-03-13 12:56:37 +0100894}
895
Corentin Charye1faa9d2008-03-13 12:57:18 +0100896static void eeepc_hwmon_exit(void)
897{
898 struct device *hwmon;
899
900 hwmon = eeepc_hwmon_device;
901 if (!hwmon)
902 return ;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100903 sysfs_remove_group(&hwmon->kobj,
904 &hwmon_attribute_group);
Matthew Garrettf1441312008-08-20 14:08:57 -0700905 hwmon_device_unregister(hwmon);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100906 eeepc_hwmon_device = NULL;
907}
908
Eric Coopere59f8792008-03-13 12:55:46 +0100909static void __exit eeepc_laptop_exit(void)
910{
Corentin Charya5fa4292008-03-13 12:56:37 +0100911 eeepc_backlight_exit();
Corentin Charya9df80c2009-01-20 16:17:40 +0100912 eeepc_rfkill_exit();
913 eeepc_input_exit();
Corentin Charye1faa9d2008-03-13 12:57:18 +0100914 eeepc_hwmon_exit();
Eric Coopere59f8792008-03-13 12:55:46 +0100915 acpi_bus_unregister_driver(&eeepc_hotk_driver);
916 sysfs_remove_group(&platform_device->dev.kobj,
917 &platform_attribute_group);
918 platform_device_unregister(platform_device);
919 platform_driver_unregister(&platform_driver);
920}
921
Corentin Charya5fa4292008-03-13 12:56:37 +0100922static int eeepc_backlight_init(struct device *dev)
923{
924 struct backlight_device *bd;
925
926 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
927 NULL, &eeepcbl_ops);
928 if (IS_ERR(bd)) {
929 printk(EEEPC_ERR
930 "Could not register eeepc backlight device\n");
931 eeepc_backlight_device = NULL;
932 return PTR_ERR(bd);
933 }
934 eeepc_backlight_device = bd;
935 bd->props.max_brightness = 15;
936 bd->props.brightness = read_brightness(NULL);
937 bd->props.power = FB_BLANK_UNBLANK;
938 backlight_update_status(bd);
939 return 0;
940}
941
Corentin Charye1faa9d2008-03-13 12:57:18 +0100942static int eeepc_hwmon_init(struct device *dev)
943{
944 struct device *hwmon;
945 int result;
946
947 hwmon = hwmon_device_register(dev);
948 if (IS_ERR(hwmon)) {
949 printk(EEEPC_ERR
950 "Could not register eeepc hwmon device\n");
951 eeepc_hwmon_device = NULL;
952 return PTR_ERR(hwmon);
953 }
954 eeepc_hwmon_device = hwmon;
955 result = sysfs_create_group(&hwmon->kobj,
956 &hwmon_attribute_group);
957 if (result)
958 eeepc_hwmon_exit();
959 return result;
960}
961
Eric Coopere59f8792008-03-13 12:55:46 +0100962static int __init eeepc_laptop_init(void)
963{
964 struct device *dev;
965 int result;
966
967 if (acpi_disabled)
968 return -ENODEV;
969 result = acpi_bus_register_driver(&eeepc_hotk_driver);
970 if (result < 0)
971 return result;
972 if (!ehotk) {
973 acpi_bus_unregister_driver(&eeepc_hotk_driver);
974 return -ENODEV;
975 }
976 dev = acpi_get_physical_device(ehotk->device->handle);
Thomas Renningera2bf8c02008-08-01 17:37:59 +0200977
978 if (!acpi_video_backlight_support()) {
979 result = eeepc_backlight_init(dev);
980 if (result)
981 goto fail_backlight;
982 } else
983 printk(EEEPC_INFO "Backlight controlled by ACPI video "
984 "driver\n");
985
Corentin Charye1faa9d2008-03-13 12:57:18 +0100986 result = eeepc_hwmon_init(dev);
987 if (result)
988 goto fail_hwmon;
Eric Coopere59f8792008-03-13 12:55:46 +0100989 /* Register platform stuff */
990 result = platform_driver_register(&platform_driver);
991 if (result)
992 goto fail_platform_driver;
993 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
994 if (!platform_device) {
995 result = -ENOMEM;
996 goto fail_platform_device1;
997 }
998 result = platform_device_add(platform_device);
999 if (result)
1000 goto fail_platform_device2;
1001 result = sysfs_create_group(&platform_device->dev.kobj,
1002 &platform_attribute_group);
1003 if (result)
1004 goto fail_sysfs;
1005 return 0;
1006fail_sysfs:
1007 platform_device_del(platform_device);
1008fail_platform_device2:
1009 platform_device_put(platform_device);
1010fail_platform_device1:
1011 platform_driver_unregister(&platform_driver);
1012fail_platform_driver:
Corentin Charye1faa9d2008-03-13 12:57:18 +01001013 eeepc_hwmon_exit();
1014fail_hwmon:
Corentin Charya5fa4292008-03-13 12:56:37 +01001015 eeepc_backlight_exit();
1016fail_backlight:
Corentin Charya9df80c2009-01-20 16:17:40 +01001017 eeepc_input_exit();
1018 eeepc_rfkill_exit();
Eric Coopere59f8792008-03-13 12:55:46 +01001019 return result;
1020}
1021
1022module_init(eeepc_laptop_init);
1023module_exit(eeepc_laptop_exit);