Merge branch 'bjorn-notify' into release
[linux-2.6] / drivers / platform / x86 / eeepc-laptop.c
1 /*
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>
24 #include <linux/backlight.h>
25 #include <linux/fb.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <acpi/acpi_drivers.h>
29 #include <acpi/acpi_bus.h>
30 #include <linux/uaccess.h>
31 #include <linux/input.h>
32 #include <linux/rfkill.h>
33 #include <linux/pci.h>
34
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
53 #define NOTIFY_BRN_MIN  0x20
54 #define NOTIFY_BRN_MAX  0x2f
55
56 enum {
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
68 enum {
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
93 static const char *cm_getv[] = {
94         "WLDG", "BTHG", NULL, NULL,
95         "CAMG", NULL, NULL, NULL,
96         NULL, "PBLG", NULL, NULL,
97         "CFVG", NULL, NULL, NULL,
98         "USBG", NULL, NULL, "MODG",
99         "CRDG", "LIDG"
100 };
101
102 static const char *cm_setv[] = {
103         "WLDS", "BTHS", NULL, NULL,
104         "CAMS", NULL, NULL, NULL,
105         "SDSP", "PBLS", "HDPS", NULL,
106         "CFVS", NULL, NULL, NULL,
107         "USBG", NULL, NULL, "MODS",
108         "CRDS", NULL
109 };
110
111 #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
120 /*
121  * This is the main structure, we can use it to store useful information
122  * about the hotk device
123  */
124 struct 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 */
131         struct input_dev *inputdev;
132         u16 *keycode_map;
133         struct rfkill *eeepc_wlan_rfkill;
134         struct rfkill *eeepc_bluetooth_rfkill;
135 };
136
137 /* The actual device the driver binds to */
138 static struct eeepc_hotk *ehotk;
139
140 /* Platform device/driver */
141 static struct platform_driver platform_driver = {
142         .driver = {
143                 .name = EEEPC_HOTK_FILE,
144                 .owner = THIS_MODULE,
145         }
146 };
147
148 static struct platform_device *platform_device;
149
150 struct key_entry {
151         char type;
152         u8 code;
153         u16 keycode;
154 };
155
156 enum { KE_KEY, KE_END };
157
158 static struct key_entry eeepc_keymap[] = {
159         /* Sleep already handled via generic ACPI code */
160         {KE_KEY, 0x10, KEY_WLAN },
161         {KE_KEY, 0x11, KEY_WLAN },
162         {KE_KEY, 0x12, KEY_PROG1 },
163         {KE_KEY, 0x13, KEY_MUTE },
164         {KE_KEY, 0x14, KEY_VOLUMEDOWN },
165         {KE_KEY, 0x15, KEY_VOLUMEUP },
166         {KE_KEY, 0x1a, KEY_COFFEE },
167         {KE_KEY, 0x1b, KEY_ZOOM },
168         {KE_KEY, 0x1c, KEY_PROG2 },
169         {KE_KEY, 0x1d, KEY_PROG3 },
170         {KE_KEY, NOTIFY_BRN_MIN,     KEY_BRIGHTNESSDOWN },
171         {KE_KEY, NOTIFY_BRN_MIN + 2, KEY_BRIGHTNESSUP },
172         {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
173         {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
174         {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
175         {KE_END, 0},
176 };
177
178 /*
179  * The hotkey driver declaration
180  */
181 static int eeepc_hotk_add(struct acpi_device *device);
182 static int eeepc_hotk_remove(struct acpi_device *device, int type);
183 static int eeepc_hotk_resume(struct acpi_device *device);
184 static void eeepc_hotk_notify(struct acpi_device *device, u32 event);
185
186 static const struct acpi_device_id eeepc_device_ids[] = {
187         {EEEPC_HOTK_HID, 0},
188         {"", 0},
189 };
190 MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
191
192 static struct acpi_driver eeepc_hotk_driver = {
193         .name = EEEPC_HOTK_NAME,
194         .class = EEEPC_HOTK_CLASS,
195         .ids = eeepc_device_ids,
196         .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
197         .ops = {
198                 .add = eeepc_hotk_add,
199                 .remove = eeepc_hotk_remove,
200                 .resume = eeepc_hotk_resume,
201                 .notify = eeepc_hotk_notify,
202         },
203 };
204
205 /* The backlight device /sys/class/backlight */
206 static struct backlight_device *eeepc_backlight_device;
207
208 /* The hwmon device */
209 static struct device *eeepc_hwmon_device;
210
211 /*
212  * The backlight class declaration
213  */
214 static int read_brightness(struct backlight_device *bd);
215 static int update_bl_status(struct backlight_device *bd);
216 static struct backlight_ops eeepcbl_ops = {
217         .get_brightness = read_brightness,
218         .update_status = update_bl_status,
219 };
220
221 MODULE_AUTHOR("Corentin Chary, Eric Cooper");
222 MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
223 MODULE_LICENSE("GPL");
224
225 /*
226  * ACPI Helpers
227  */
228 static int write_acpi_int(acpi_handle handle, const char *method, int val,
229                           struct acpi_buffer *output)
230 {
231         struct acpi_object_list params;
232         union acpi_object in_obj;
233         acpi_status status;
234
235         params.count = 1;
236         params.pointer = &in_obj;
237         in_obj.type = ACPI_TYPE_INTEGER;
238         in_obj.integer.value = val;
239
240         status = acpi_evaluate_object(handle, (char *)method, &params, output);
241         return (status == AE_OK ? 0 : -1);
242 }
243
244 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
245 {
246         acpi_status status;
247         unsigned long long result;
248
249         status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
250         if (ACPI_FAILURE(status)) {
251                 *val = -1;
252                 return -1;
253         } else {
254                 *val = result;
255                 return 0;
256         }
257 }
258
259 static int set_acpi(int cm, int value)
260 {
261         if (ehotk->cm_supported & (0x1 << cm)) {
262                 const char *method = cm_setv[cm];
263                 if (method == NULL)
264                         return -ENODEV;
265                 if (write_acpi_int(ehotk->handle, method, value, NULL))
266                         printk(EEEPC_WARNING "Error writing %s\n", method);
267         }
268         return 0;
269 }
270
271 static int get_acpi(int cm)
272 {
273         int value = -1;
274         if ((ehotk->cm_supported & (0x1 << cm))) {
275                 const char *method = cm_getv[cm];
276                 if (method == NULL)
277                         return -ENODEV;
278                 if (read_acpi_int(ehotk->handle, method, &value))
279                         printk(EEEPC_WARNING "Error reading %s\n", method);
280         }
281         return value;
282 }
283
284 /*
285  * Backlight
286  */
287 static int read_brightness(struct backlight_device *bd)
288 {
289         return get_acpi(CM_ASL_PANELBRIGHT);
290 }
291
292 static int set_brightness(struct backlight_device *bd, int value)
293 {
294         value = max(0, min(15, value));
295         return set_acpi(CM_ASL_PANELBRIGHT, value);
296 }
297
298 static int update_bl_status(struct backlight_device *bd)
299 {
300         return set_brightness(bd, bd->props.brightness);
301 }
302
303 /*
304  * Rfkill helpers
305  */
306
307 static bool eeepc_wlan_rfkill_blocked(void)
308 {
309         if (get_acpi(CM_ASL_WLAN) == 1)
310                 return false;
311         return true;
312 }
313
314 static int eeepc_rfkill_set(void *data, bool blocked)
315 {
316         unsigned long asl = (unsigned long)data;
317         return set_acpi(asl, !blocked);
318 }
319
320 static const struct rfkill_ops eeepc_rfkill_ops = {
321         .set_block = eeepc_rfkill_set,
322 };
323
324 /*
325  * Sys helpers
326  */
327 static int parse_arg(const char *buf, unsigned long count, int *val)
328 {
329         if (!count)
330                 return 0;
331         if (sscanf(buf, "%i", val) != 1)
332                 return -EINVAL;
333         return count;
334 }
335
336 static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
337 {
338         int rv, value;
339
340         rv = parse_arg(buf, count, &value);
341         if (rv > 0)
342                 set_acpi(cm, value);
343         return rv;
344 }
345
346 static ssize_t show_sys_acpi(int cm, char *buf)
347 {
348         return sprintf(buf, "%d\n", get_acpi(cm));
349 }
350
351 #define EEEPC_CREATE_DEVICE_ATTR(_name, _cm)                            \
352         static ssize_t show_##_name(struct device *dev,                 \
353                                     struct device_attribute *attr,      \
354                                     char *buf)                          \
355         {                                                               \
356                 return show_sys_acpi(_cm, buf);                         \
357         }                                                               \
358         static ssize_t store_##_name(struct device *dev,                \
359                                      struct device_attribute *attr,     \
360                                      const char *buf, size_t count)     \
361         {                                                               \
362                 return store_sys_acpi(_cm, buf, count);                 \
363         }                                                               \
364         static struct device_attribute dev_attr_##_name = {             \
365                 .attr = {                                               \
366                         .name = __stringify(_name),                     \
367                         .mode = 0644 },                                 \
368                 .show   = show_##_name,                                 \
369                 .store  = store_##_name,                                \
370         }
371
372 EEEPC_CREATE_DEVICE_ATTR(camera, CM_ASL_CAMERA);
373 EEEPC_CREATE_DEVICE_ATTR(cardr, CM_ASL_CARDREADER);
374 EEEPC_CREATE_DEVICE_ATTR(disp, CM_ASL_DISPLAYSWITCH);
375 EEEPC_CREATE_DEVICE_ATTR(cpufv, CM_ASL_CPUFV);
376
377 static struct attribute *platform_attributes[] = {
378         &dev_attr_camera.attr,
379         &dev_attr_cardr.attr,
380         &dev_attr_disp.attr,
381         &dev_attr_cpufv.attr,
382         NULL
383 };
384
385 static struct attribute_group platform_attribute_group = {
386         .attrs = platform_attributes
387 };
388
389 /*
390  * Hotkey functions
391  */
392 static struct key_entry *eepc_get_entry_by_scancode(int code)
393 {
394         struct key_entry *key;
395
396         for (key = eeepc_keymap; key->type != KE_END; key++)
397                 if (code == key->code)
398                         return key;
399
400         return NULL;
401 }
402
403 static struct key_entry *eepc_get_entry_by_keycode(int code)
404 {
405         struct key_entry *key;
406
407         for (key = eeepc_keymap; key->type != KE_END; key++)
408                 if (code == key->keycode && key->type == KE_KEY)
409                         return key;
410
411         return NULL;
412 }
413
414 static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
415 {
416         struct key_entry *key = eepc_get_entry_by_scancode(scancode);
417
418         if (key && key->type == KE_KEY) {
419                 *keycode = key->keycode;
420                 return 0;
421         }
422
423         return -EINVAL;
424 }
425
426 static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
427 {
428         struct key_entry *key;
429         int old_keycode;
430
431         if (keycode < 0 || keycode > KEY_MAX)
432                 return -EINVAL;
433
434         key = eepc_get_entry_by_scancode(scancode);
435         if (key && key->type == KE_KEY) {
436                 old_keycode = key->keycode;
437                 key->keycode = keycode;
438                 set_bit(keycode, dev->keybit);
439                 if (!eepc_get_entry_by_keycode(old_keycode))
440                         clear_bit(old_keycode, dev->keybit);
441                 return 0;
442         }
443
444         return -EINVAL;
445 }
446
447 static int eeepc_hotk_check(void)
448 {
449         const struct key_entry *key;
450         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
451         int result;
452
453         result = acpi_bus_get_status(ehotk->device);
454         if (result)
455                 return result;
456         if (ehotk->device->status.present) {
457                 if (write_acpi_int(ehotk->handle, "INIT", ehotk->init_flag,
458                                     &buffer)) {
459                         printk(EEEPC_ERR "Hotkey initialization failed\n");
460                         return -ENODEV;
461                 } else {
462                         printk(EEEPC_NOTICE "Hotkey init flags 0x%x\n",
463                                ehotk->init_flag);
464                 }
465                 /* get control methods supported */
466                 if (read_acpi_int(ehotk->handle, "CMSG"
467                                    , &ehotk->cm_supported)) {
468                         printk(EEEPC_ERR
469                                "Get control methods supported failed\n");
470                         return -ENODEV;
471                 } else {
472                         printk(EEEPC_INFO
473                                "Get control methods supported: 0x%x\n",
474                                ehotk->cm_supported);
475                 }
476                 ehotk->inputdev = input_allocate_device();
477                 if (!ehotk->inputdev) {
478                         printk(EEEPC_INFO "Unable to allocate input device\n");
479                         return 0;
480                 }
481                 ehotk->inputdev->name = "Asus EeePC extra buttons";
482                 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
483                 ehotk->inputdev->id.bustype = BUS_HOST;
484                 ehotk->inputdev->getkeycode = eeepc_getkeycode;
485                 ehotk->inputdev->setkeycode = eeepc_setkeycode;
486
487                 for (key = eeepc_keymap; key->type != KE_END; key++) {
488                         switch (key->type) {
489                         case KE_KEY:
490                                 set_bit(EV_KEY, ehotk->inputdev->evbit);
491                                 set_bit(key->keycode, ehotk->inputdev->keybit);
492                                 break;
493                         }
494                 }
495                 result = input_register_device(ehotk->inputdev);
496                 if (result) {
497                         printk(EEEPC_INFO "Unable to register input device\n");
498                         input_free_device(ehotk->inputdev);
499                         return 0;
500                 }
501         } else {
502                 printk(EEEPC_ERR "Hotkey device not present, aborting\n");
503                 return -EINVAL;
504         }
505         return 0;
506 }
507
508 static int notify_brn(void)
509 {
510         /* returns the *previous* brightness, or -1 */
511         struct backlight_device *bd = eeepc_backlight_device;
512         if (bd) {
513                 int old = bd->props.brightness;
514                 bd->props.brightness = read_brightness(bd);
515                 return old;
516         }
517         return -1;
518 }
519
520 static void eeepc_rfkill_hotplug(void)
521 {
522         struct pci_dev *dev;
523         struct pci_bus *bus = pci_find_bus(0, 1);
524         bool blocked;
525
526         if (!bus) {
527                 printk(EEEPC_WARNING "Unable to find PCI bus 1?\n");
528                 return;
529         }
530
531         blocked = eeepc_wlan_rfkill_blocked();
532         if (!blocked) {
533                 dev = pci_get_slot(bus, 0);
534                 if (dev) {
535                         /* Device already present */
536                         pci_dev_put(dev);
537                         return;
538                 }
539                 dev = pci_scan_single_device(bus, 0);
540                 if (dev) {
541                         pci_bus_assign_resources(bus);
542                         if (pci_bus_add_device(dev))
543                                 printk(EEEPC_ERR "Unable to hotplug wifi\n");
544                 }
545         } else {
546                 dev = pci_get_slot(bus, 0);
547                 if (dev) {
548                         pci_remove_bus_device(dev);
549                         pci_dev_put(dev);
550                 }
551         }
552
553         rfkill_set_sw_state(ehotk->eeepc_wlan_rfkill, blocked);
554 }
555
556 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
557 {
558         if (event != ACPI_NOTIFY_BUS_CHECK)
559                 return;
560
561         eeepc_rfkill_hotplug();
562 }
563
564 static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
565 {
566         static struct key_entry *key;
567         u16 count;
568         int brn = -ENODEV;
569
570         if (!ehotk)
571                 return;
572         if (event > ACPI_MAX_SYS_NOTIFY)
573                 return;
574         if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX)
575                 brn = notify_brn();
576         count = ehotk->event_count[event % 128]++;
577         acpi_bus_generate_proc_event(ehotk->device, event, count);
578         acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
579                                         dev_name(&ehotk->device->dev), event,
580                                         count);
581         if (ehotk->inputdev) {
582                 if (brn != -ENODEV) {
583                         /* brightness-change events need special
584                          * handling for conversion to key events
585                          */
586                         if (brn < 0)
587                                 brn = event;
588                         else
589                                 brn += NOTIFY_BRN_MIN;
590                         if (event < brn)
591                                 event = NOTIFY_BRN_MIN; /* brightness down */
592                         else if (event > brn)
593                                 event = NOTIFY_BRN_MIN + 2; /* ... up */
594                         else
595                                 event = NOTIFY_BRN_MIN + 1; /* ... unchanged */
596                 }
597                 key = eepc_get_entry_by_scancode(event);
598                 if (key) {
599                         switch (key->type) {
600                         case KE_KEY:
601                                 input_report_key(ehotk->inputdev, key->keycode,
602                                                  1);
603                                 input_sync(ehotk->inputdev);
604                                 input_report_key(ehotk->inputdev, key->keycode,
605                                                  0);
606                                 input_sync(ehotk->inputdev);
607                                 break;
608                         }
609                 }
610         }
611 }
612
613 static int eeepc_register_rfkill_notifier(char *node)
614 {
615         acpi_status status = AE_OK;
616         acpi_handle handle;
617
618         status = acpi_get_handle(NULL, node, &handle);
619
620         if (ACPI_SUCCESS(status)) {
621                 status = acpi_install_notify_handler(handle,
622                                                      ACPI_SYSTEM_NOTIFY,
623                                                      eeepc_rfkill_notify,
624                                                      NULL);
625                 if (ACPI_FAILURE(status))
626                         printk(EEEPC_WARNING
627                                "Failed to register notify on %s\n", node);
628         } else
629                 return -ENODEV;
630
631         return 0;
632 }
633
634 static void eeepc_unregister_rfkill_notifier(char *node)
635 {
636         acpi_status status = AE_OK;
637         acpi_handle handle;
638
639         status = acpi_get_handle(NULL, node, &handle);
640
641         if (ACPI_SUCCESS(status)) {
642                 status = acpi_remove_notify_handler(handle,
643                                                      ACPI_SYSTEM_NOTIFY,
644                                                      eeepc_rfkill_notify);
645                 if (ACPI_FAILURE(status))
646                         printk(EEEPC_ERR
647                                "Error removing rfkill notify handler %s\n",
648                                 node);
649         }
650 }
651
652 static int eeepc_hotk_add(struct acpi_device *device)
653 {
654         int result;
655
656         if (!device)
657                  return -EINVAL;
658         printk(EEEPC_NOTICE EEEPC_HOTK_NAME "\n");
659         ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
660         if (!ehotk)
661                 return -ENOMEM;
662         ehotk->init_flag = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
663         ehotk->handle = device->handle;
664         strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
665         strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
666         device->driver_data = ehotk;
667         ehotk->device = device;
668         result = eeepc_hotk_check();
669         if (result)
670                 goto ehotk_fail;
671
672         eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
673         eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
674
675         if (get_acpi(CM_ASL_WLAN) != -1) {
676                 ehotk->eeepc_wlan_rfkill = rfkill_alloc("eeepc-wlan",
677                                                         &device->dev,
678                                                         RFKILL_TYPE_WLAN,
679                                                         &eeepc_rfkill_ops,
680                                                         (void *)CM_ASL_WLAN);
681
682                 if (!ehotk->eeepc_wlan_rfkill)
683                         goto wlan_fail;
684
685                 rfkill_init_sw_state(ehotk->eeepc_wlan_rfkill,
686                                      get_acpi(CM_ASL_WLAN) != 1);
687                 result = rfkill_register(ehotk->eeepc_wlan_rfkill);
688                 if (result)
689                         goto wlan_fail;
690         }
691
692         if (get_acpi(CM_ASL_BLUETOOTH) != -1) {
693                 ehotk->eeepc_bluetooth_rfkill =
694                         rfkill_alloc("eeepc-bluetooth",
695                                      &device->dev,
696                                      RFKILL_TYPE_BLUETOOTH,
697                                      &eeepc_rfkill_ops,
698                                      (void *)CM_ASL_BLUETOOTH);
699
700                 if (!ehotk->eeepc_bluetooth_rfkill)
701                         goto bluetooth_fail;
702
703                 rfkill_init_sw_state(ehotk->eeepc_bluetooth_rfkill,
704                                      get_acpi(CM_ASL_BLUETOOTH) != 1);
705                 result = rfkill_register(ehotk->eeepc_bluetooth_rfkill);
706                 if (result)
707                         goto bluetooth_fail;
708         }
709
710         return 0;
711
712  bluetooth_fail:
713         rfkill_destroy(ehotk->eeepc_bluetooth_rfkill);
714         rfkill_unregister(ehotk->eeepc_wlan_rfkill);
715  wlan_fail:
716         rfkill_destroy(ehotk->eeepc_wlan_rfkill);
717         eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
718         eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
719  ehotk_fail:
720         kfree(ehotk);
721         ehotk = NULL;
722
723         return result;
724 }
725
726 static int eeepc_hotk_remove(struct acpi_device *device, int type)
727 {
728         if (!device || !acpi_driver_data(device))
729                  return -EINVAL;
730
731         eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
732         eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
733
734         kfree(ehotk);
735         return 0;
736 }
737
738 static int eeepc_hotk_resume(struct acpi_device *device)
739 {
740         if (ehotk->eeepc_wlan_rfkill) {
741                 bool wlan;
742
743                 /* Workaround - it seems that _PTS disables the wireless
744                    without notification or changing the value read by WLAN.
745                    Normally this is fine because the correct value is restored
746                    from the non-volatile storage on resume, but we need to do
747                    it ourself if case suspend is aborted, or we lose wireless.
748                  */
749                 wlan = get_acpi(CM_ASL_WLAN);
750                 set_acpi(CM_ASL_WLAN, wlan);
751
752                 rfkill_set_sw_state(ehotk->eeepc_wlan_rfkill,
753                                     wlan != 1);
754
755                 eeepc_rfkill_hotplug();
756         }
757
758         if (ehotk->eeepc_bluetooth_rfkill)
759                 rfkill_set_sw_state(ehotk->eeepc_bluetooth_rfkill,
760                                     get_acpi(CM_ASL_BLUETOOTH) != 1);
761
762         return 0;
763 }
764
765 /*
766  * Hwmon
767  */
768 static int eeepc_get_fan_pwm(void)
769 {
770         int value = 0;
771
772         read_acpi_int(NULL, EEEPC_EC_FAN_PWM, &value);
773         value = value * 255 / 100;
774         return (value);
775 }
776
777 static void eeepc_set_fan_pwm(int value)
778 {
779         value = SENSORS_LIMIT(value, 0, 255);
780         value = value * 100 / 255;
781         ec_write(EEEPC_EC_SC02, value);
782 }
783
784 static int eeepc_get_fan_rpm(void)
785 {
786         int high = 0;
787         int low = 0;
788
789         read_acpi_int(NULL, EEEPC_EC_FAN_HRPM, &high);
790         read_acpi_int(NULL, EEEPC_EC_FAN_LRPM, &low);
791         return (high << 8 | low);
792 }
793
794 static int eeepc_get_fan_ctrl(void)
795 {
796         int value = 0;
797
798         read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
799         return ((value & 0x02 ? 1 : 0));
800 }
801
802 static void eeepc_set_fan_ctrl(int manual)
803 {
804         int value = 0;
805
806         read_acpi_int(NULL, EEEPC_EC_FAN_CTRL, &value);
807         if (manual)
808                 value |= 0x02;
809         else
810                 value &= ~0x02;
811         ec_write(EEEPC_EC_SFB3, value);
812 }
813
814 static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
815 {
816         int rv, value;
817
818         rv = parse_arg(buf, count, &value);
819         if (rv > 0)
820                 set(value);
821         return rv;
822 }
823
824 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
825 {
826         return sprintf(buf, "%d\n", get());
827 }
828
829 #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get)              \
830         static ssize_t show_##_name(struct device *dev,                 \
831                                     struct device_attribute *attr,      \
832                                     char *buf)                          \
833         {                                                               \
834                 return show_sys_hwmon(_set, buf);                       \
835         }                                                               \
836         static ssize_t store_##_name(struct device *dev,                \
837                                      struct device_attribute *attr,     \
838                                      const char *buf, size_t count)     \
839         {                                                               \
840                 return store_sys_hwmon(_get, buf, count);               \
841         }                                                               \
842         static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
843
844 EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
845 EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
846                          eeepc_get_fan_pwm, eeepc_set_fan_pwm);
847 EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
848                          eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
849
850 static ssize_t
851 show_name(struct device *dev, struct device_attribute *attr, char *buf)
852 {
853         return sprintf(buf, "eeepc\n");
854 }
855 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
856
857 static struct attribute *hwmon_attributes[] = {
858         &sensor_dev_attr_pwm1.dev_attr.attr,
859         &sensor_dev_attr_fan1_input.dev_attr.attr,
860         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
861         &sensor_dev_attr_name.dev_attr.attr,
862         NULL
863 };
864
865 static struct attribute_group hwmon_attribute_group = {
866         .attrs = hwmon_attributes
867 };
868
869 /*
870  * exit/init
871  */
872 static void eeepc_backlight_exit(void)
873 {
874         if (eeepc_backlight_device)
875                 backlight_device_unregister(eeepc_backlight_device);
876         eeepc_backlight_device = NULL;
877 }
878
879 static void eeepc_rfkill_exit(void)
880 {
881         if (ehotk->eeepc_wlan_rfkill)
882                 rfkill_unregister(ehotk->eeepc_wlan_rfkill);
883         if (ehotk->eeepc_bluetooth_rfkill)
884                 rfkill_unregister(ehotk->eeepc_bluetooth_rfkill);
885 }
886
887 static void eeepc_input_exit(void)
888 {
889         if (ehotk->inputdev)
890                 input_unregister_device(ehotk->inputdev);
891 }
892
893 static void eeepc_hwmon_exit(void)
894 {
895         struct device *hwmon;
896
897         hwmon = eeepc_hwmon_device;
898         if (!hwmon)
899                 return ;
900         sysfs_remove_group(&hwmon->kobj,
901                            &hwmon_attribute_group);
902         hwmon_device_unregister(hwmon);
903         eeepc_hwmon_device = NULL;
904 }
905
906 static void __exit eeepc_laptop_exit(void)
907 {
908         eeepc_backlight_exit();
909         eeepc_rfkill_exit();
910         eeepc_input_exit();
911         eeepc_hwmon_exit();
912         acpi_bus_unregister_driver(&eeepc_hotk_driver);
913         sysfs_remove_group(&platform_device->dev.kobj,
914                            &platform_attribute_group);
915         platform_device_unregister(platform_device);
916         platform_driver_unregister(&platform_driver);
917 }
918
919 static int eeepc_backlight_init(struct device *dev)
920 {
921         struct backlight_device *bd;
922
923         bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
924                                        NULL, &eeepcbl_ops);
925         if (IS_ERR(bd)) {
926                 printk(EEEPC_ERR
927                        "Could not register eeepc backlight device\n");
928                 eeepc_backlight_device = NULL;
929                 return PTR_ERR(bd);
930         }
931         eeepc_backlight_device = bd;
932         bd->props.max_brightness = 15;
933         bd->props.brightness = read_brightness(NULL);
934         bd->props.power = FB_BLANK_UNBLANK;
935         backlight_update_status(bd);
936         return 0;
937 }
938
939 static int eeepc_hwmon_init(struct device *dev)
940 {
941         struct device *hwmon;
942         int result;
943
944         hwmon = hwmon_device_register(dev);
945         if (IS_ERR(hwmon)) {
946                 printk(EEEPC_ERR
947                        "Could not register eeepc hwmon device\n");
948                 eeepc_hwmon_device = NULL;
949                 return PTR_ERR(hwmon);
950         }
951         eeepc_hwmon_device = hwmon;
952         result = sysfs_create_group(&hwmon->kobj,
953                                     &hwmon_attribute_group);
954         if (result)
955                 eeepc_hwmon_exit();
956         return result;
957 }
958
959 static int __init eeepc_laptop_init(void)
960 {
961         struct device *dev;
962         int result;
963
964         if (acpi_disabled)
965                 return -ENODEV;
966         result = acpi_bus_register_driver(&eeepc_hotk_driver);
967         if (result < 0)
968                 return result;
969         if (!ehotk) {
970                 acpi_bus_unregister_driver(&eeepc_hotk_driver);
971                 return -ENODEV;
972         }
973         dev = acpi_get_physical_device(ehotk->device->handle);
974
975         if (!acpi_video_backlight_support()) {
976                 result = eeepc_backlight_init(dev);
977                 if (result)
978                         goto fail_backlight;
979         } else
980                 printk(EEEPC_INFO "Backlight controlled by ACPI video "
981                        "driver\n");
982
983         result = eeepc_hwmon_init(dev);
984         if (result)
985                 goto fail_hwmon;
986         /* Register platform stuff */
987         result = platform_driver_register(&platform_driver);
988         if (result)
989                 goto fail_platform_driver;
990         platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
991         if (!platform_device) {
992                 result = -ENOMEM;
993                 goto fail_platform_device1;
994         }
995         result = platform_device_add(platform_device);
996         if (result)
997                 goto fail_platform_device2;
998         result = sysfs_create_group(&platform_device->dev.kobj,
999                                     &platform_attribute_group);
1000         if (result)
1001                 goto fail_sysfs;
1002         return 0;
1003 fail_sysfs:
1004         platform_device_del(platform_device);
1005 fail_platform_device2:
1006         platform_device_put(platform_device);
1007 fail_platform_device1:
1008         platform_driver_unregister(&platform_driver);
1009 fail_platform_driver:
1010         eeepc_hwmon_exit();
1011 fail_hwmon:
1012         eeepc_backlight_exit();
1013 fail_backlight:
1014         eeepc_input_exit();
1015         eeepc_rfkill_exit();
1016         return result;
1017 }
1018
1019 module_init(eeepc_laptop_init);
1020 module_exit(eeepc_laptop_exit);