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