blob: 66655d2b4ce9043a814bccc4c7fa94429130a7aa [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 },
Matthew Garretta195dcd2008-08-19 12:13:20 +0100169 {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
170 {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
171 {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
172 {KE_END, 0},
173};
174
Eric Coopere59f8792008-03-13 12:55:46 +0100175/*
176 * The hotkey driver declaration
177 */
178static int eeepc_hotk_add(struct acpi_device *device);
179static int eeepc_hotk_remove(struct acpi_device *device, int type);
180
181static const struct acpi_device_id eeepc_device_ids[] = {
182 {EEEPC_HOTK_HID, 0},
183 {"", 0},
184};
185MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
186
187static struct acpi_driver eeepc_hotk_driver = {
188 .name = EEEPC_HOTK_NAME,
189 .class = EEEPC_HOTK_CLASS,
190 .ids = eeepc_device_ids,
191 .ops = {
192 .add = eeepc_hotk_add,
193 .remove = eeepc_hotk_remove,
194 },
195};
196
Corentin Charya5fa4292008-03-13 12:56:37 +0100197/* The backlight device /sys/class/backlight */
198static struct backlight_device *eeepc_backlight_device;
199
Corentin Charye1faa9d2008-03-13 12:57:18 +0100200/* The hwmon device */
201static struct device *eeepc_hwmon_device;
202
Corentin Charya5fa4292008-03-13 12:56:37 +0100203/*
204 * The backlight class declaration
205 */
206static int read_brightness(struct backlight_device *bd);
207static int update_bl_status(struct backlight_device *bd);
208static struct backlight_ops eeepcbl_ops = {
209 .get_brightness = read_brightness,
210 .update_status = update_bl_status,
211};
212
Eric Coopere59f8792008-03-13 12:55:46 +0100213MODULE_AUTHOR("Corentin Chary, Eric Cooper");
214MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
215MODULE_LICENSE("GPL");
216
217/*
218 * ACPI Helpers
219 */
220static int write_acpi_int(acpi_handle handle, const char *method, int val,
221 struct acpi_buffer *output)
222{
223 struct acpi_object_list params;
224 union acpi_object in_obj;
225 acpi_status status;
226
227 params.count = 1;
228 params.pointer = &in_obj;
229 in_obj.type = ACPI_TYPE_INTEGER;
230 in_obj.integer.value = val;
231
232 status = acpi_evaluate_object(handle, (char *)method, &params, output);
233 return (status == AE_OK ? 0 : -1);
234}
235
236static int read_acpi_int(acpi_handle handle, const char *method, int *val)
237{
238 acpi_status status;
Matthew Wilcox27663c52008-10-10 02:22:59 -0400239 unsigned long long result;
Eric Coopere59f8792008-03-13 12:55:46 +0100240
241 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
242 if (ACPI_FAILURE(status)) {
243 *val = -1;
244 return -1;
245 } else {
246 *val = result;
247 return 0;
248 }
249}
250
251static int set_acpi(int cm, int value)
252{
253 if (ehotk->cm_supported & (0x1 << cm)) {
254 const char *method = cm_setv[cm];
255 if (method == NULL)
256 return -ENODEV;
257 if (write_acpi_int(ehotk->handle, method, value, NULL))
258 printk(EEEPC_WARNING "Error writing %s\n", method);
259 }
260 return 0;
261}
262
263static int get_acpi(int cm)
264{
265 int value = -1;
266 if ((ehotk->cm_supported & (0x1 << cm))) {
267 const char *method = cm_getv[cm];
268 if (method == NULL)
269 return -ENODEV;
270 if (read_acpi_int(ehotk->handle, method, &value))
271 printk(EEEPC_WARNING "Error reading %s\n", method);
272 }
273 return value;
274}
275
276/*
Corentin Charya5fa4292008-03-13 12:56:37 +0100277 * Backlight
278 */
279static int read_brightness(struct backlight_device *bd)
280{
281 return get_acpi(CM_ASL_PANELBRIGHT);
282}
283
284static int set_brightness(struct backlight_device *bd, int value)
285{
286 value = max(0, min(15, value));
287 return set_acpi(CM_ASL_PANELBRIGHT, value);
288}
289
290static int update_bl_status(struct backlight_device *bd)
291{
292 return set_brightness(bd, bd->props.brightness);
293}
294
295/*
Matthew Garretta195dcd2008-08-19 12:13:20 +0100296 * Rfkill helpers
297 */
298
299static int eeepc_wlan_rfkill_set(void *data, enum rfkill_state state)
300{
301 if (state == RFKILL_STATE_SOFT_BLOCKED)
302 return set_acpi(CM_ASL_WLAN, 0);
303 else
304 return set_acpi(CM_ASL_WLAN, 1);
305}
306
307static int eeepc_wlan_rfkill_state(void *data, enum rfkill_state *state)
308{
309 if (get_acpi(CM_ASL_WLAN) == 1)
310 *state = RFKILL_STATE_UNBLOCKED;
311 else
312 *state = RFKILL_STATE_SOFT_BLOCKED;
313 return 0;
314}
315
316static int eeepc_bluetooth_rfkill_set(void *data, enum rfkill_state state)
317{
318 if (state == RFKILL_STATE_SOFT_BLOCKED)
319 return set_acpi(CM_ASL_BLUETOOTH, 0);
320 else
321 return set_acpi(CM_ASL_BLUETOOTH, 1);
322}
323
324static int eeepc_bluetooth_rfkill_state(void *data, enum rfkill_state *state)
325{
326 if (get_acpi(CM_ASL_BLUETOOTH) == 1)
327 *state = RFKILL_STATE_UNBLOCKED;
328 else
329 *state = RFKILL_STATE_SOFT_BLOCKED;
330 return 0;
331}
332
333/*
Eric Coopere59f8792008-03-13 12:55:46 +0100334 * Sys helpers
335 */
336static int parse_arg(const char *buf, unsigned long count, int *val)
337{
338 if (!count)
339 return 0;
340 if (sscanf(buf, "%i", val) != 1)
341 return -EINVAL;
342 return count;
343}
344
345static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
346{
347 int rv, value;
348
349 rv = parse_arg(buf, count, &value);
350 if (rv > 0)
351 set_acpi(cm, value);
352 return rv;
353}
354
355static ssize_t show_sys_acpi(int cm, char *buf)
356{
357 return sprintf(buf, "%d\n", get_acpi(cm));
358}
359
360#define EEEPC_CREATE_DEVICE_ATTR(_name, _cm) \
361 static ssize_t show_##_name(struct device *dev, \
362 struct device_attribute *attr, \
363 char *buf) \
364 { \
365 return show_sys_acpi(_cm, buf); \
366 } \
367 static ssize_t store_##_name(struct device *dev, \
368 struct device_attribute *attr, \
369 const char *buf, size_t count) \
370 { \
371 return store_sys_acpi(_cm, buf, count); \
372 } \
373 static struct device_attribute dev_attr_##_name = { \
374 .attr = { \
375 .name = __stringify(_name), \
376 .mode = 0644 }, \
377 .show = show_##_name, \
378 .store = store_##_name, \
379 }
380
381EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
382EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
383EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
Eric Coopere59f8792008-03-13 12:55:46 +0100384
385static struct attribute *platform_attributes[] = {
386 &dev_attr_camera.attr,
387 &dev_attr_cardr.attr,
388 &dev_attr_disp.attr,
Eric Coopere59f8792008-03-13 12:55:46 +0100389 NULL
390};
391
392static struct attribute_group platform_attribute_group = {
393 .attrs = platform_attributes
394};
395
396/*
397 * Hotkey functions
398 */
Matthew Garretta195dcd2008-08-19 12:13:20 +0100399static struct key_entry *eepc_get_entry_by_scancode(int code)
400{
401 struct key_entry *key;
402
403 for (key = eeepc_keymap; key->type != KE_END; key++)
404 if (code == key->code)
405 return key;
406
407 return NULL;
408}
409
410static struct key_entry *eepc_get_entry_by_keycode(int code)
411{
412 struct key_entry *key;
413
414 for (key = eeepc_keymap; key->type != KE_END; key++)
415 if (code == key->keycode && key->type == KE_KEY)
416 return key;
417
418 return NULL;
419}
420
421static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
422{
423 struct key_entry *key = eepc_get_entry_by_scancode(scancode);
424
425 if (key && key->type == KE_KEY) {
426 *keycode = key->keycode;
427 return 0;
428 }
429
430 return -EINVAL;
431}
432
433static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
434{
435 struct key_entry *key;
436 int old_keycode;
437
438 if (keycode < 0 || keycode > KEY_MAX)
439 return -EINVAL;
440
441 key = eepc_get_entry_by_scancode(scancode);
442 if (key && key->type == KE_KEY) {
443 old_keycode = key->keycode;
444 key->keycode = keycode;
445 set_bit(keycode, dev->keybit);
446 if (!eepc_get_entry_by_keycode(old_keycode))
447 clear_bit(old_keycode, dev->keybit);
448 return 0;
449 }
450
451 return -EINVAL;
452}
453
Eric Coopere59f8792008-03-13 12:55:46 +0100454static int eeepc_hotk_check(void)
455{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100456 const struct key_entry *key;
Eric Coopere59f8792008-03-13 12:55:46 +0100457 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
458 int result;
459
460 result = acpi_bus_get_status(ehotk->device);
461 if (result)
462 return result;
463 if (ehotk->device->status.present) {
464 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
465 &buffer)) {
466 printk(EEEPC_ERR "Hotkey initialization failed\n");
467 return -ENODEV;
468 } else {
469 printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
470 ehotk->init_flag);
471 }
472 /* get control methods supported */
473 if (read_acpi_int(ehotk->handle, "CMSG"
474 , &ehotk->cm_supported)) {
475 printk(EEEPC_ERR
476 "Get control methods supported failed\n");
477 return -ENODEV;
478 } else {
479 printk(EEEPC_INFO
480 "Get control methods supported: 0x%x\n",
481 ehotk->cm_supported);
482 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100483 ehotk->inputdev = input_allocate_device();
484 if (!ehotk->inputdev) {
485 printk(EEEPC_INFO "Unable to allocate input device\n");
486 return 0;
487 }
488 ehotk->inputdev->name = "Asus EeePC extra buttons";
489 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
490 ehotk->inputdev->id.bustype = BUS_HOST;
491 ehotk->inputdev->getkeycode = eeepc_getkeycode;
492 ehotk->inputdev->setkeycode = eeepc_setkeycode;
493
494 for (key = eeepc_keymap; key->type != KE_END; key++) {
495 switch (key->type) {
496 case KE_KEY:
497 set_bit(EV_KEY, ehotk->inputdev->evbit);
498 set_bit(key->keycode, ehotk->inputdev->keybit);
499 break;
500 }
501 }
502 result = input_register_device(ehotk->inputdev);
503 if (result) {
504 printk(EEEPC_INFO "Unable to register input device\n");
505 input_free_device(ehotk->inputdev);
506 return 0;
507 }
Eric Coopere59f8792008-03-13 12:55:46 +0100508 } else {
509 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
510 return -EINVAL;
511 }
512 return 0;
513}
514
Corentin Charya5fa4292008-03-13 12:56:37 +0100515static void notify_brn(void)
516{
517 struct backlight_device *bd = eeepc_backlight_device;
518 bd->props.brightness = read_brightness(bd);
519}
520
Matthew Garrett57402942009-01-20 16:17:48 +0100521static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
522{
523 struct pci_dev *dev;
524 struct pci_bus *bus = pci_find_bus(0, 1);
525
526 if (event != ACPI_NOTIFY_BUS_CHECK)
527 return;
528
529 if (!bus) {
530 printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
531 return;
532 }
533
534 if (get_acpi(CM_ASL_WLAN) == 1) {
535 dev = pci_get_slot(bus, 0);
536 if (dev) {
537 /* Device already present */
538 pci_dev_put(dev);
539 return;
540 }
541 dev = pci_scan_single_device(bus, 0);
542 if (dev) {
543 pci_bus_assign_resources(bus);
544 if (pci_bus_add_device(dev))
545 printk(EEEPC_ERR "Unable to hotplug wifi\n");
546 }
547 } else {
548 dev = pci_get_slot(bus, 0);
549 if (dev) {
550 pci_remove_bus_device(dev);
551 pci_dev_put(dev);
552 }
553 }
554}
555
Eric Coopere59f8792008-03-13 12:55:46 +0100556static void eeepc_hotk_notify(acpi_handle handle, u32 event, void *data)
557{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100558 static struct key_entry *key;
Eric Coopere59f8792008-03-13 12:55:46 +0100559 if (!ehotk)
560 return;
Corentin Charya5fa4292008-03-13 12:56:37 +0100561 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
562 notify_brn();
Eric Coopere59f8792008-03-13 12:55:46 +0100563 acpi_bus_generate_proc_event(ehotk->device, event,
564 ehotk->event_count[event % 128]++);
Matthew Garretta195dcd2008-08-19 12:13:20 +0100565 if (ehotk->inputdev) {
566 key = eepc_get_entry_by_scancode(event);
567 if (key) {
568 switch (key->type) {
569 case KE_KEY:
570 input_report_key(ehotk->inputdev, key->keycode,
571 1);
572 input_sync(ehotk->inputdev);
573 input_report_key(ehotk->inputdev, key->keycode,
574 0);
575 input_sync(ehotk->inputdev);
576 break;
577 }
578 }
579 }
Eric Coopere59f8792008-03-13 12:55:46 +0100580}
581
Matthew Garrett57402942009-01-20 16:17:48 +0100582static int eeepc_register_rfkill_notifier(char *node)
583{
584 acpi_status status = AE_OK;
585 acpi_handle handle;
586
587 status = acpi_get_handle(NULL, node, &handle);
588
589 if (ACPI_SUCCESS(status)) {
590 status = acpi_install_notify_handler(handle,
591 ACPI_SYSTEM_NOTIFY,
592 eeepc_rfkill_notify,
593 NULL);
594 if (ACPI_FAILURE(status))
595 printk(EEEPC_WARNING
596 "Failed to register notify on %s\n", node);
597 } else
598 return -ENODEV;
599
600 return 0;
601}
602
603static void eeepc_unregister_rfkill_notifier(char *node)
604{
605 acpi_status status = AE_OK;
606 acpi_handle handle;
607
608 status = acpi_get_handle(NULL, node, &handle);
609
610 if (ACPI_SUCCESS(status)) {
611 status = acpi_remove_notify_handler(handle,
612 ACPI_SYSTEM_NOTIFY,
613 eeepc_rfkill_notify);
614 if (ACPI_FAILURE(status))
615 printk(EEEPC_ERR
616 "Error removing rfkill notify handler %s\n",
617 node);
618 }
619}
620
Eric Coopere59f8792008-03-13 12:55:46 +0100621static int eeepc_hotk_add(struct acpi_device *device)
622{
623 acpi_status status = AE_OK;
624 int result;
625
626 if (!device)
627 return -EINVAL;
628 printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
629 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
630 if (!ehotk)
631 return -ENOMEM;
632 ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
633 ehotk->handle = device->handle;
634 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
635 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700636 device->driver_data = ehotk;
Eric Coopere59f8792008-03-13 12:55:46 +0100637 ehotk->device = device;
638 result = eeepc_hotk_check();
639 if (result)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100640 goto ehotk_fail;
Eric Coopere59f8792008-03-13 12:55:46 +0100641 status = acpi_install_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
642 eeepc_hotk_notify, ehotk);
643 if (ACPI_FAILURE(status))
644 printk(EEEPC_ERR "Error installing notify handler\n");
Matthew Garretta195dcd2008-08-19 12:13:20 +0100645
646 if (get_acpi(CM_ASL_WLAN) != -1) {
647 ehotk->eeepc_wlan_rfkill = rfkill_allocate(&device->dev,
648 RFKILL_TYPE_WLAN);
649
650 if (!ehotk->eeepc_wlan_rfkill)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100651 goto wlan_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100652
653 ehotk->eeepc_wlan_rfkill->name = "eeepc-wlan";
654 ehotk->eeepc_wlan_rfkill->toggle_radio = eeepc_wlan_rfkill_set;
655 ehotk->eeepc_wlan_rfkill->get_state = eeepc_wlan_rfkill_state;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100656 if (get_acpi(CM_ASL_WLAN) == 1) {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100657 ehotk->eeepc_wlan_rfkill->state =
658 RFKILL_STATE_UNBLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100659 rfkill_set_default(RFKILL_TYPE_WLAN,
660 RFKILL_STATE_UNBLOCKED);
661 } else {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100662 ehotk->eeepc_wlan_rfkill->state =
663 RFKILL_STATE_SOFT_BLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100664 rfkill_set_default(RFKILL_TYPE_WLAN,
665 RFKILL_STATE_SOFT_BLOCKED);
666 }
667 result = rfkill_register(ehotk->eeepc_wlan_rfkill);
668 if (result)
669 goto wlan_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100670 }
671
672 if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
673 ehotk->eeepc_bluetooth_rfkill =
674 rfkill_allocate(&device->dev, RFKILL_TYPE_BLUETOOTH);
675
676 if (!ehotk->eeepc_bluetooth_rfkill)
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100677 goto bluetooth_fail;
Matthew Garretta195dcd2008-08-19 12:13:20 +0100678
679 ehotk->eeepc_bluetooth_rfkill->name = "eeepc-bluetooth";
680 ehotk->eeepc_bluetooth_rfkill->toggle_radio =
681 eeepc_bluetooth_rfkill_set;
682 ehotk->eeepc_bluetooth_rfkill->get_state =
683 eeepc_bluetooth_rfkill_state;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100684 if (get_acpi(CM_ASL_BLUETOOTH) == 1) {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100685 ehotk->eeepc_bluetooth_rfkill->state =
686 RFKILL_STATE_UNBLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100687 rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
688 RFKILL_STATE_UNBLOCKED);
689 } else {
Matthew Garretta195dcd2008-08-19 12:13:20 +0100690 ehotk->eeepc_bluetooth_rfkill->state =
691 RFKILL_STATE_SOFT_BLOCKED;
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100692 rfkill_set_default(RFKILL_TYPE_BLUETOOTH,
693 RFKILL_STATE_SOFT_BLOCKED);
694 }
Matthew Garretta195dcd2008-08-19 12:13:20 +0100695
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100696 result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
697 if (result)
698 goto bluetooth_fail;
Eric Coopere59f8792008-03-13 12:55:46 +0100699 }
Matthew Garrett57402942009-01-20 16:17:48 +0100700
701 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
702 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
703
Matthew Garrettc9ddf8f2009-01-20 16:17:47 +0100704 return 0;
705
706 bluetooth_fail:
707 if (ehotk->eeepc_bluetooth_rfkill)
708 rfkill_free(ehotk->eeepc_bluetooth_rfkill);
709 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
710 ehotk->eeepc_wlan_rfkill = NULL;
711 wlan_fail:
712 if (ehotk->eeepc_wlan_rfkill)
713 rfkill_free(ehotk->eeepc_wlan_rfkill);
714 ehotk_fail:
715 kfree(ehotk);
716 ehotk = NULL;
717
Eric Coopere59f8792008-03-13 12:55:46 +0100718 return result;
719}
720
721static int eeepc_hotk_remove(struct acpi_device *device, int type)
722{
723 acpi_status status = 0;
724
725 if (!device || !acpi_driver_data(device))
726 return -EINVAL;
727 status = acpi_remove_notify_handler(ehotk->handle, ACPI_SYSTEM_NOTIFY,
728 eeepc_hotk_notify);
729 if (ACPI_FAILURE(status))
730 printk(EEEPC_ERR "Error removing notify handler\n");
Matthew Garrett57402942009-01-20 16:17:48 +0100731
732 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
733 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
734
Eric Coopere59f8792008-03-13 12:55:46 +0100735 kfree(ehotk);
736 return 0;
737}
738
739/*
Corentin Charye1faa9d2008-03-13 12:57:18 +0100740 * Hwmon
741 */
742static int eeepc_get_fan_pwm(void)
743{
744 int value = 0;
745
746 read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
Corentin Chary04dcd842008-10-09 15:33:57 +0200747 value = value * 255 / 100;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100748 return (value);
749}
750
751static void eeepc_set_fan_pwm(int value)
752{
Corentin Chary04dcd842008-10-09 15:33:57 +0200753 value = SENSORS_LIMIT(value, 0, 255);
754 value = value * 100 / 255;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100755 ec_write(EEEPC_EC_SC02, value);
756}
757
758static int eeepc_get_fan_rpm(void)
759{
760 int high = 0;
761 int low = 0;
762
763 read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
764 read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
765 return (high << 8 | low);
766}
767
768static int eeepc_get_fan_ctrl(void)
769{
770 int value = 0;
771
772 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
773 return ((value & 0x02 ? 1 : 0));
774}
775
776static void eeepc_set_fan_ctrl(int manual)
777{
778 int value = 0;
779
780 read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
781 if (manual)
782 value |= 0x02;
783 else
784 value &= ~0x02;
785 ec_write(EEEPC_EC_SFB3, value);
786}
787
788static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
789{
790 int rv, value;
791
792 rv = parse_arg(buf, count, &value);
793 if (rv > 0)
794 set(value);
795 return rv;
796}
797
798static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
799{
800 return sprintf(buf, "%d\n", get());
801}
802
803#define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
804 static ssize_t show_##_name(struct device *dev, \
805 struct device_attribute *attr, \
806 char *buf) \
807 { \
808 return show_sys_hwmon(_set, buf); \
809 } \
810 static ssize_t store_##_name(struct device *dev, \
811 struct device_attribute *attr, \
812 const char *buf, size_t count) \
813 { \
814 return store_sys_hwmon(_get, buf, count); \
815 } \
816 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
817
818EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
Corentin Chary04dcd842008-10-09 15:33:57 +0200819EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100820 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
821EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
822 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
823
Corentin Chary04dcd842008-10-09 15:33:57 +0200824static ssize_t
825show_name(struct device *dev, struct device_attribute *attr, char *buf)
826{
827 return sprintf(buf, "eeepc\n");
828}
829static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
830
Corentin Charye1faa9d2008-03-13 12:57:18 +0100831static struct attribute *hwmon_attributes[] = {
Corentin Chary04dcd842008-10-09 15:33:57 +0200832 &sensor_dev_attr_pwm1.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100833 &sensor_dev_attr_fan1_input.dev_attr.attr,
834 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Corentin Chary04dcd842008-10-09 15:33:57 +0200835 &sensor_dev_attr_name.dev_attr.attr,
Corentin Charye1faa9d2008-03-13 12:57:18 +0100836 NULL
837};
838
839static struct attribute_group hwmon_attribute_group = {
840 .attrs = hwmon_attributes
841};
842
843/*
Eric Coopere59f8792008-03-13 12:55:46 +0100844 * exit/init
845 */
Corentin Charya5fa4292008-03-13 12:56:37 +0100846static void eeepc_backlight_exit(void)
847{
848 if (eeepc_backlight_device)
849 backlight_device_unregister(eeepc_backlight_device);
Corentin Charya9df80c2009-01-20 16:17:40 +0100850 eeepc_backlight_device = NULL;
851}
852
853static void eeepc_rfkill_exit(void)
854{
Matthew Garretta195dcd2008-08-19 12:13:20 +0100855 if (ehotk->eeepc_wlan_rfkill)
856 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
857 if (ehotk->eeepc_bluetooth_rfkill)
858 rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
Corentin Charya9df80c2009-01-20 16:17:40 +0100859}
860
861static void eeepc_input_exit(void)
862{
863 if (ehotk->inputdev)
864 input_unregister_device(ehotk->inputdev);
Corentin Charya5fa4292008-03-13 12:56:37 +0100865}
866
Corentin Charye1faa9d2008-03-13 12:57:18 +0100867static void eeepc_hwmon_exit(void)
868{
869 struct device *hwmon;
870
871 hwmon = eeepc_hwmon_device;
872 if (!hwmon)
873 return ;
Corentin Charye1faa9d2008-03-13 12:57:18 +0100874 sysfs_remove_group(&hwmon->kobj,
875 &hwmon_attribute_group);
Matthew Garrettf1441312008-08-20 14:08:57 -0700876 hwmon_device_unregister(hwmon);
Corentin Charye1faa9d2008-03-13 12:57:18 +0100877 eeepc_hwmon_device = NULL;
878}
879
Eric Coopere59f8792008-03-13 12:55:46 +0100880static void __exit eeepc_laptop_exit(void)
881{
Corentin Charya5fa4292008-03-13 12:56:37 +0100882 eeepc_backlight_exit();
Corentin Charya9df80c2009-01-20 16:17:40 +0100883 eeepc_rfkill_exit();
884 eeepc_input_exit();
Corentin Charye1faa9d2008-03-13 12:57:18 +0100885 eeepc_hwmon_exit();
Eric Coopere59f8792008-03-13 12:55:46 +0100886 acpi_bus_unregister_driver(&eeepc_hotk_driver);
887 sysfs_remove_group(&platform_device->dev.kobj,
888 &platform_attribute_group);
889 platform_device_unregister(platform_device);
890 platform_driver_unregister(&platform_driver);
891}
892
Corentin Charya5fa4292008-03-13 12:56:37 +0100893static int eeepc_backlight_init(struct device *dev)
894{
895 struct backlight_device *bd;
896
897 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
898 NULL, &eeepcbl_ops);
899 if (IS_ERR(bd)) {
900 printk(EEEPC_ERR
901 "Could not register eeepc backlight device\n");
902 eeepc_backlight_device = NULL;
903 return PTR_ERR(bd);
904 }
905 eeepc_backlight_device = bd;
906 bd->props.max_brightness = 15;
907 bd->props.brightness = read_brightness(NULL);
908 bd->props.power = FB_BLANK_UNBLANK;
909 backlight_update_status(bd);
910 return 0;
911}
912
Corentin Charye1faa9d2008-03-13 12:57:18 +0100913static int eeepc_hwmon_init(struct device *dev)
914{
915 struct device *hwmon;
916 int result;
917
918 hwmon = hwmon_device_register(dev);
919 if (IS_ERR(hwmon)) {
920 printk(EEEPC_ERR
921 "Could not register eeepc hwmon device\n");
922 eeepc_hwmon_device = NULL;
923 return PTR_ERR(hwmon);
924 }
925 eeepc_hwmon_device = hwmon;
926 result = sysfs_create_group(&hwmon->kobj,
927 &hwmon_attribute_group);
928 if (result)
929 eeepc_hwmon_exit();
930 return result;
931}
932
Eric Coopere59f8792008-03-13 12:55:46 +0100933static int __init eeepc_laptop_init(void)
934{
935 struct device *dev;
936 int result;
937
938 if (acpi_disabled)
939 return -ENODEV;
940 result = acpi_bus_register_driver(&eeepc_hotk_driver);
941 if (result < 0)
942 return result;
943 if (!ehotk) {
944 acpi_bus_unregister_driver(&eeepc_hotk_driver);
945 return -ENODEV;
946 }
947 dev = acpi_get_physical_device(ehotk->device->handle);
Thomas Renningera2bf8c02008-08-01 17:37:59 +0200948
949 if (!acpi_video_backlight_support()) {
950 result = eeepc_backlight_init(dev);
951 if (result)
952 goto fail_backlight;
953 } else
954 printk(EEEPC_INFO "Backlight controlled by ACPI video "
955 "driver\n");
956
Corentin Charye1faa9d2008-03-13 12:57:18 +0100957 result = eeepc_hwmon_init(dev);
958 if (result)
959 goto fail_hwmon;
Eric Coopere59f8792008-03-13 12:55:46 +0100960 /* Register platform stuff */
961 result = platform_driver_register(&platform_driver);
962 if (result)
963 goto fail_platform_driver;
964 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
965 if (!platform_device) {
966 result = -ENOMEM;
967 goto fail_platform_device1;
968 }
969 result = platform_device_add(platform_device);
970 if (result)
971 goto fail_platform_device2;
972 result = sysfs_create_group(&platform_device->dev.kobj,
973 &platform_attribute_group);
974 if (result)
975 goto fail_sysfs;
976 return 0;
977fail_sysfs:
978 platform_device_del(platform_device);
979fail_platform_device2:
980 platform_device_put(platform_device);
981fail_platform_device1:
982 platform_driver_unregister(&platform_driver);
983fail_platform_driver:
Corentin Charye1faa9d2008-03-13 12:57:18 +0100984 eeepc_hwmon_exit();
985fail_hwmon:
Corentin Charya5fa4292008-03-13 12:56:37 +0100986 eeepc_backlight_exit();
987fail_backlight:
Corentin Charya9df80c2009-01-20 16:17:40 +0100988 eeepc_input_exit();
989 eeepc_rfkill_exit();
Eric Coopere59f8792008-03-13 12:55:46 +0100990 return result;
991}
992
993module_init(eeepc_laptop_init);
994module_exit(eeepc_laptop_exit);