]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/platform/x86/eeepc-laptop.c
eeepc-laptop: code movement
[mirror_ubuntu-bionic-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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/types.h>
25 #include <linux/platform_device.h>
26 #include <linux/backlight.h>
27 #include <linux/fb.h>
28 #include <linux/hwmon.h>
29 #include <linux/hwmon-sysfs.h>
30 #include <acpi/acpi_drivers.h>
31 #include <acpi/acpi_bus.h>
32 #include <linux/uaccess.h>
33 #include <linux/input.h>
34 #include <linux/rfkill.h>
35 #include <linux/pci.h>
36 #include <linux/pci_hotplug.h>
37 #include <linux/leds.h>
38
39 #define EEEPC_LAPTOP_VERSION "0.1"
40
41 #define EEEPC_HOTK_NAME "Eee PC Hotkey Driver"
42 #define EEEPC_HOTK_FILE "eeepc"
43 #define EEEPC_HOTK_CLASS "hotkey"
44 #define EEEPC_HOTK_DEVICE_NAME "Hotkey"
45 #define EEEPC_HOTK_HID "ASUS010"
46
47 MODULE_AUTHOR("Corentin Chary, Eric Cooper");
48 MODULE_DESCRIPTION(EEEPC_HOTK_NAME);
49 MODULE_LICENSE("GPL");
50
51 /*
52 * Definitions for Asus EeePC
53 */
54 #define NOTIFY_WLAN_ON 0x10
55 #define NOTIFY_BRN_MIN 0x20
56 #define NOTIFY_BRN_MAX 0x2f
57
58 enum {
59 DISABLE_ASL_WLAN = 0x0001,
60 DISABLE_ASL_BLUETOOTH = 0x0002,
61 DISABLE_ASL_IRDA = 0x0004,
62 DISABLE_ASL_CAMERA = 0x0008,
63 DISABLE_ASL_TV = 0x0010,
64 DISABLE_ASL_GPS = 0x0020,
65 DISABLE_ASL_DISPLAYSWITCH = 0x0040,
66 DISABLE_ASL_MODEM = 0x0080,
67 DISABLE_ASL_CARDREADER = 0x0100,
68 DISABLE_ASL_3G = 0x0200,
69 DISABLE_ASL_WIMAX = 0x0400,
70 DISABLE_ASL_HWCF = 0x0800
71 };
72
73 enum {
74 CM_ASL_WLAN = 0,
75 CM_ASL_BLUETOOTH,
76 CM_ASL_IRDA,
77 CM_ASL_1394,
78 CM_ASL_CAMERA,
79 CM_ASL_TV,
80 CM_ASL_GPS,
81 CM_ASL_DVDROM,
82 CM_ASL_DISPLAYSWITCH,
83 CM_ASL_PANELBRIGHT,
84 CM_ASL_BIOSFLASH,
85 CM_ASL_ACPIFLASH,
86 CM_ASL_CPUFV,
87 CM_ASL_CPUTEMPERATURE,
88 CM_ASL_FANCPU,
89 CM_ASL_FANCHASSIS,
90 CM_ASL_USBPORT1,
91 CM_ASL_USBPORT2,
92 CM_ASL_USBPORT3,
93 CM_ASL_MODEM,
94 CM_ASL_CARDREADER,
95 CM_ASL_3G,
96 CM_ASL_WIMAX,
97 CM_ASL_HWCF,
98 CM_ASL_LID,
99 CM_ASL_TYPE,
100 CM_ASL_PANELPOWER, /*P901*/
101 CM_ASL_TPD
102 };
103
104 static const char *cm_getv[] = {
105 "WLDG", "BTHG", NULL, NULL,
106 "CAMG", NULL, NULL, NULL,
107 NULL, "PBLG", NULL, NULL,
108 "CFVG", NULL, NULL, NULL,
109 "USBG", NULL, NULL, "MODG",
110 "CRDG", "M3GG", "WIMG", "HWCF",
111 "LIDG", "TYPE", "PBPG", "TPDG"
112 };
113
114 static const char *cm_setv[] = {
115 "WLDS", "BTHS", NULL, NULL,
116 "CAMS", NULL, NULL, NULL,
117 "SDSP", "PBLS", "HDPS", NULL,
118 "CFVS", NULL, NULL, NULL,
119 "USBG", NULL, NULL, "MODS",
120 "CRDS", "M3GS", "WIMS", NULL,
121 NULL, NULL, "PBPS", "TPDS"
122 };
123
124 struct key_entry {
125 char type;
126 u8 code;
127 u16 keycode;
128 };
129
130 enum { KE_KEY, KE_END };
131
132 static struct key_entry eeepc_keymap[] = {
133 /* Sleep already handled via generic ACPI code */
134 {KE_KEY, 0x10, KEY_WLAN },
135 {KE_KEY, 0x11, KEY_WLAN },
136 {KE_KEY, 0x12, KEY_PROG1 },
137 {KE_KEY, 0x13, KEY_MUTE },
138 {KE_KEY, 0x14, KEY_VOLUMEDOWN },
139 {KE_KEY, 0x15, KEY_VOLUMEUP },
140 {KE_KEY, 0x1a, KEY_COFFEE },
141 {KE_KEY, 0x1b, KEY_ZOOM },
142 {KE_KEY, 0x1c, KEY_PROG2 },
143 {KE_KEY, 0x1d, KEY_PROG3 },
144 {KE_KEY, NOTIFY_BRN_MIN, KEY_BRIGHTNESSDOWN },
145 {KE_KEY, NOTIFY_BRN_MAX, KEY_BRIGHTNESSUP },
146 {KE_KEY, 0x30, KEY_SWITCHVIDEOMODE },
147 {KE_KEY, 0x31, KEY_SWITCHVIDEOMODE },
148 {KE_KEY, 0x32, KEY_SWITCHVIDEOMODE },
149 {KE_END, 0},
150 };
151
152
153 /*
154 * This is the main structure, we can use it to store useful information
155 * about the hotk device
156 */
157 struct eeepc_hotk {
158 struct acpi_device *device; /* the device we are in */
159 acpi_handle handle; /* the handle of the hotk device */
160 u32 cm_supported; /* the control methods supported
161 by this BIOS */
162 u16 event_count[128]; /* count for each event */
163 struct input_dev *inputdev;
164 u16 *keycode_map;
165 struct rfkill *wlan_rfkill;
166 struct rfkill *bluetooth_rfkill;
167 struct rfkill *wwan3g_rfkill;
168 struct rfkill *wimax_rfkill;
169 struct hotplug_slot *hotplug_slot;
170 struct mutex hotplug_lock;
171 };
172
173 /* The actual device the driver binds to */
174 static struct eeepc_hotk *ehotk;
175
176 /* The platform device */
177 static struct platform_device *platform_device;
178
179 /* The backlight device /sys/class/backlight */
180 static struct backlight_device *eeepc_backlight_device;
181
182 /* The hwmon device */
183 static struct device *eeepc_hwmon_device;
184
185
186 /*
187 * ACPI Helpers
188 */
189 static int write_acpi_int(acpi_handle handle, const char *method, int val)
190 {
191 struct acpi_object_list params;
192 union acpi_object in_obj;
193 acpi_status status;
194
195 params.count = 1;
196 params.pointer = &in_obj;
197 in_obj.type = ACPI_TYPE_INTEGER;
198 in_obj.integer.value = val;
199
200 status = acpi_evaluate_object(handle, (char *)method, &params, NULL);
201 return (status == AE_OK ? 0 : -1);
202 }
203
204 static int read_acpi_int(acpi_handle handle, const char *method, int *val)
205 {
206 acpi_status status;
207 unsigned long long result;
208
209 status = acpi_evaluate_integer(handle, (char *)method, NULL, &result);
210 if (ACPI_FAILURE(status)) {
211 *val = -1;
212 return -1;
213 } else {
214 *val = result;
215 return 0;
216 }
217 }
218
219 static int set_acpi(int cm, int value)
220 {
221 const char *method = cm_setv[cm];
222
223 if (method == NULL)
224 return -ENODEV;
225 if ((ehotk->cm_supported & (0x1 << cm)) == 0)
226 return -ENODEV;
227
228 if (write_acpi_int(ehotk->handle, method, value))
229 pr_warning("Error writing %s\n", method);
230 return 0;
231 }
232
233 static int get_acpi(int cm)
234 {
235 const char *method = cm_getv[cm];
236 int value;
237
238 if (method == NULL)
239 return -ENODEV;
240 if ((ehotk->cm_supported & (0x1 << cm)) == 0)
241 return -ENODEV;
242
243 if (read_acpi_int(ehotk->handle, method, &value))
244 pr_warning("Error reading %s\n", method);
245 return value;
246 }
247
248 /*
249 * Sys helpers
250 */
251 static int parse_arg(const char *buf, unsigned long count, int *val)
252 {
253 if (!count)
254 return 0;
255 if (sscanf(buf, "%i", val) != 1)
256 return -EINVAL;
257 return count;
258 }
259
260 static ssize_t store_sys_acpi(int cm, const char *buf, size_t count)
261 {
262 int rv, value;
263
264 rv = parse_arg(buf, count, &value);
265 if (rv > 0)
266 value = set_acpi(cm, value);
267 if (value < 0)
268 return -EIO;
269 return rv;
270 }
271
272 static ssize_t show_sys_acpi(int cm, char *buf)
273 {
274 int value = get_acpi(cm);
275
276 if (value < 0)
277 return -EIO;
278 return sprintf(buf, "%d\n", value);
279 }
280
281 #define EEEPC_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
282 static ssize_t show_##_name(struct device *dev, \
283 struct device_attribute *attr, \
284 char *buf) \
285 { \
286 return show_sys_acpi(_cm, buf); \
287 } \
288 static ssize_t store_##_name(struct device *dev, \
289 struct device_attribute *attr, \
290 const char *buf, size_t count) \
291 { \
292 return store_sys_acpi(_cm, buf, count); \
293 } \
294 static struct device_attribute dev_attr_##_name = { \
295 .attr = { \
296 .name = __stringify(_name), \
297 .mode = _mode }, \
298 .show = show_##_name, \
299 .store = store_##_name, \
300 }
301
302 EEEPC_CREATE_DEVICE_ATTR(camera, 0644, CM_ASL_CAMERA);
303 EEEPC_CREATE_DEVICE_ATTR(cardr, 0644, CM_ASL_CARDREADER);
304 EEEPC_CREATE_DEVICE_ATTR(disp, 0200, CM_ASL_DISPLAYSWITCH);
305
306 struct eeepc_cpufv {
307 int num;
308 int cur;
309 };
310
311 static int get_cpufv(struct eeepc_cpufv *c)
312 {
313 c->cur = get_acpi(CM_ASL_CPUFV);
314 c->num = (c->cur >> 8) & 0xff;
315 c->cur &= 0xff;
316 if (c->cur < 0 || c->num <= 0 || c->num > 12)
317 return -ENODEV;
318 return 0;
319 }
320
321 static ssize_t show_available_cpufv(struct device *dev,
322 struct device_attribute *attr,
323 char *buf)
324 {
325 struct eeepc_cpufv c;
326 int i;
327 ssize_t len = 0;
328
329 if (get_cpufv(&c))
330 return -ENODEV;
331 for (i = 0; i < c.num; i++)
332 len += sprintf(buf + len, "%d ", i);
333 len += sprintf(buf + len, "\n");
334 return len;
335 }
336
337 static ssize_t show_cpufv(struct device *dev,
338 struct device_attribute *attr,
339 char *buf)
340 {
341 struct eeepc_cpufv c;
342
343 if (get_cpufv(&c))
344 return -ENODEV;
345 return sprintf(buf, "%#x\n", (c.num << 8) | c.cur);
346 }
347
348 static ssize_t store_cpufv(struct device *dev,
349 struct device_attribute *attr,
350 const char *buf, size_t count)
351 {
352 struct eeepc_cpufv c;
353 int rv, value;
354
355 if (get_cpufv(&c))
356 return -ENODEV;
357 rv = parse_arg(buf, count, &value);
358 if (rv < 0)
359 return rv;
360 if (!rv || value < 0 || value >= c.num)
361 return -EINVAL;
362 set_acpi(CM_ASL_CPUFV, value);
363 return rv;
364 }
365
366 static struct device_attribute dev_attr_cpufv = {
367 .attr = {
368 .name = "cpufv",
369 .mode = 0644 },
370 .show = show_cpufv,
371 .store = store_cpufv
372 };
373
374 static struct device_attribute dev_attr_available_cpufv = {
375 .attr = {
376 .name = "available_cpufv",
377 .mode = 0444 },
378 .show = show_available_cpufv
379 };
380
381 static struct attribute *platform_attributes[] = {
382 &dev_attr_camera.attr,
383 &dev_attr_cardr.attr,
384 &dev_attr_disp.attr,
385 &dev_attr_cpufv.attr,
386 &dev_attr_available_cpufv.attr,
387 NULL
388 };
389
390 static struct attribute_group platform_attribute_group = {
391 .attrs = platform_attributes
392 };
393
394 static int eeepc_platform_init(void)
395 {
396 int result;
397
398 platform_device = platform_device_alloc(EEEPC_HOTK_FILE, -1);
399 if (!platform_device)
400 return -ENOMEM;
401
402 result = platform_device_add(platform_device);
403 if (result)
404 goto fail_platform_device;
405
406 result = sysfs_create_group(&platform_device->dev.kobj,
407 &platform_attribute_group);
408 if (result)
409 goto fail_sysfs;
410 return 0;
411
412 fail_sysfs:
413 platform_device_del(platform_device);
414 fail_platform_device:
415 platform_device_put(platform_device);
416 return result;
417 }
418
419 static void eeepc_platform_exit(void)
420 {
421 sysfs_remove_group(&platform_device->dev.kobj,
422 &platform_attribute_group);
423 platform_device_unregister(platform_device);
424 }
425
426 /*
427 * LEDs
428 */
429 /*
430 * These functions actually update the LED's, and are called from a
431 * workqueue. By doing this as separate work rather than when the LED
432 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
433 * potentially bad time, such as a timer interrupt.
434 */
435 static int tpd_led_wk;
436
437 static void tpd_led_update(struct work_struct *ignored)
438 {
439 int value = tpd_led_wk;
440 set_acpi(CM_ASL_TPD, value);
441 }
442
443 static struct workqueue_struct *led_workqueue;
444 static DECLARE_WORK(tpd_led_work, tpd_led_update);
445
446 static void tpd_led_set(struct led_classdev *led_cdev,
447 enum led_brightness value)
448 {
449 tpd_led_wk = (value > 0) ? 1 : 0;
450 queue_work(led_workqueue, &tpd_led_work);
451 }
452
453 static struct led_classdev tpd_led = {
454 .name = "eeepc::touchpad",
455 .brightness_set = tpd_led_set,
456 .max_brightness = 1
457 };
458
459 static int eeepc_led_init(struct device *dev)
460 {
461 int rv;
462
463 if (get_acpi(CM_ASL_TPD) == -ENODEV)
464 return 0;
465
466 led_workqueue = create_singlethread_workqueue("led_workqueue");
467 if (!led_workqueue)
468 return -ENOMEM;
469
470 rv = led_classdev_register(dev, &tpd_led);
471 if (rv) {
472 destroy_workqueue(led_workqueue);
473 return rv;
474 }
475
476 return 0;
477 }
478
479 static void eeepc_led_exit(void)
480 {
481 if (tpd_led.dev)
482 led_classdev_unregister(&tpd_led);
483 if (led_workqueue)
484 destroy_workqueue(led_workqueue);
485 }
486
487
488 /*
489 * PCI hotplug (for wlan rfkill)
490 */
491 static bool eeepc_wlan_rfkill_blocked(void)
492 {
493 if (get_acpi(CM_ASL_WLAN) == 1)
494 return false;
495 return true;
496 }
497
498 static void eeepc_rfkill_hotplug(void)
499 {
500 struct pci_dev *dev;
501 struct pci_bus *bus;
502 bool blocked = eeepc_wlan_rfkill_blocked();
503
504 if (ehotk->wlan_rfkill)
505 rfkill_set_sw_state(ehotk->wlan_rfkill, blocked);
506
507 mutex_lock(&ehotk->hotplug_lock);
508
509 if (ehotk->hotplug_slot) {
510 bus = pci_find_bus(0, 1);
511 if (!bus) {
512 pr_warning("Unable to find PCI bus 1?\n");
513 goto out_unlock;
514 }
515
516 if (!blocked) {
517 dev = pci_get_slot(bus, 0);
518 if (dev) {
519 /* Device already present */
520 pci_dev_put(dev);
521 goto out_unlock;
522 }
523 dev = pci_scan_single_device(bus, 0);
524 if (dev) {
525 pci_bus_assign_resources(bus);
526 if (pci_bus_add_device(dev))
527 pr_err("Unable to hotplug wifi\n");
528 }
529 } else {
530 dev = pci_get_slot(bus, 0);
531 if (dev) {
532 pci_remove_bus_device(dev);
533 pci_dev_put(dev);
534 }
535 }
536 }
537
538 out_unlock:
539 mutex_unlock(&ehotk->hotplug_lock);
540 }
541
542 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
543 {
544 if (event != ACPI_NOTIFY_BUS_CHECK)
545 return;
546
547 eeepc_rfkill_hotplug();
548 }
549
550 static int eeepc_register_rfkill_notifier(char *node)
551 {
552 acpi_status status = AE_OK;
553 acpi_handle handle;
554
555 status = acpi_get_handle(NULL, node, &handle);
556
557 if (ACPI_SUCCESS(status)) {
558 status = acpi_install_notify_handler(handle,
559 ACPI_SYSTEM_NOTIFY,
560 eeepc_rfkill_notify,
561 NULL);
562 if (ACPI_FAILURE(status))
563 pr_warning("Failed to register notify on %s\n", node);
564 } else
565 return -ENODEV;
566
567 return 0;
568 }
569
570 static void eeepc_unregister_rfkill_notifier(char *node)
571 {
572 acpi_status status = AE_OK;
573 acpi_handle handle;
574
575 status = acpi_get_handle(NULL, node, &handle);
576
577 if (ACPI_SUCCESS(status)) {
578 status = acpi_remove_notify_handler(handle,
579 ACPI_SYSTEM_NOTIFY,
580 eeepc_rfkill_notify);
581 if (ACPI_FAILURE(status))
582 pr_err("Error removing rfkill notify handler %s\n",
583 node);
584 }
585 }
586
587 static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
588 u8 *value)
589 {
590 int val = get_acpi(CM_ASL_WLAN);
591
592 if (val == 1 || val == 0)
593 *value = val;
594 else
595 return -EINVAL;
596
597 return 0;
598 }
599
600 static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
601 {
602 kfree(hotplug_slot->info);
603 kfree(hotplug_slot);
604 }
605
606 static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
607 .owner = THIS_MODULE,
608 .get_adapter_status = eeepc_get_adapter_status,
609 .get_power_status = eeepc_get_adapter_status,
610 };
611
612 static int eeepc_setup_pci_hotplug(void)
613 {
614 int ret = -ENOMEM;
615 struct pci_bus *bus = pci_find_bus(0, 1);
616
617 if (!bus) {
618 pr_err("Unable to find wifi PCI bus\n");
619 return -ENODEV;
620 }
621
622 ehotk->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
623 if (!ehotk->hotplug_slot)
624 goto error_slot;
625
626 ehotk->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
627 GFP_KERNEL);
628 if (!ehotk->hotplug_slot->info)
629 goto error_info;
630
631 ehotk->hotplug_slot->private = ehotk;
632 ehotk->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
633 ehotk->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
634 eeepc_get_adapter_status(ehotk->hotplug_slot,
635 &ehotk->hotplug_slot->info->adapter_status);
636
637 ret = pci_hp_register(ehotk->hotplug_slot, bus, 0, "eeepc-wifi");
638 if (ret) {
639 pr_err("Unable to register hotplug slot - %d\n", ret);
640 goto error_register;
641 }
642
643 return 0;
644
645 error_register:
646 kfree(ehotk->hotplug_slot->info);
647 error_info:
648 kfree(ehotk->hotplug_slot);
649 ehotk->hotplug_slot = NULL;
650 error_slot:
651 return ret;
652 }
653
654 /*
655 * Rfkill devices
656 */
657 static int eeepc_rfkill_set(void *data, bool blocked)
658 {
659 unsigned long asl = (unsigned long)data;
660 return set_acpi(asl, !blocked);
661 }
662
663 static const struct rfkill_ops eeepc_rfkill_ops = {
664 .set_block = eeepc_rfkill_set,
665 };
666
667 static int eeepc_new_rfkill(struct rfkill **rfkill,
668 const char *name, struct device *dev,
669 enum rfkill_type type, int cm)
670 {
671 int result;
672
673 result = get_acpi(cm);
674 if (result < 0)
675 return result;
676
677 *rfkill = rfkill_alloc(name, dev, type,
678 &eeepc_rfkill_ops, (void *)(unsigned long)cm);
679
680 if (!*rfkill)
681 return -EINVAL;
682
683 rfkill_init_sw_state(*rfkill, get_acpi(cm) != 1);
684 result = rfkill_register(*rfkill);
685 if (result) {
686 rfkill_destroy(*rfkill);
687 *rfkill = NULL;
688 return result;
689 }
690 return 0;
691 }
692
693 static void eeepc_rfkill_exit(void)
694 {
695 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P5");
696 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P6");
697 eeepc_unregister_rfkill_notifier("\\_SB.PCI0.P0P7");
698 if (ehotk->wlan_rfkill) {
699 rfkill_unregister(ehotk->wlan_rfkill);
700 rfkill_destroy(ehotk->wlan_rfkill);
701 ehotk->wlan_rfkill = NULL;
702 }
703 /*
704 * Refresh pci hotplug in case the rfkill state was changed after
705 * eeepc_unregister_rfkill_notifier()
706 */
707 eeepc_rfkill_hotplug();
708 if (ehotk->hotplug_slot)
709 pci_hp_deregister(ehotk->hotplug_slot);
710
711 if (ehotk->bluetooth_rfkill) {
712 rfkill_unregister(ehotk->bluetooth_rfkill);
713 rfkill_destroy(ehotk->bluetooth_rfkill);
714 ehotk->bluetooth_rfkill = NULL;
715 }
716 if (ehotk->wwan3g_rfkill) {
717 rfkill_unregister(ehotk->wwan3g_rfkill);
718 rfkill_destroy(ehotk->wwan3g_rfkill);
719 ehotk->wwan3g_rfkill = NULL;
720 }
721 if (ehotk->wimax_rfkill) {
722 rfkill_unregister(ehotk->wimax_rfkill);
723 rfkill_destroy(ehotk->wimax_rfkill);
724 ehotk->wimax_rfkill = NULL;
725 }
726 }
727
728 static int eeepc_rfkill_init(struct device *dev)
729 {
730 int result = 0;
731
732 mutex_init(&ehotk->hotplug_lock);
733
734 result = eeepc_new_rfkill(&ehotk->wlan_rfkill,
735 "eeepc-wlan", dev,
736 RFKILL_TYPE_WLAN, CM_ASL_WLAN);
737
738 if (result && result != -ENODEV)
739 goto exit;
740
741 result = eeepc_new_rfkill(&ehotk->bluetooth_rfkill,
742 "eeepc-bluetooth", dev,
743 RFKILL_TYPE_BLUETOOTH, CM_ASL_BLUETOOTH);
744
745 if (result && result != -ENODEV)
746 goto exit;
747
748 result = eeepc_new_rfkill(&ehotk->wwan3g_rfkill,
749 "eeepc-wwan3g", dev,
750 RFKILL_TYPE_WWAN, CM_ASL_3G);
751
752 if (result && result != -ENODEV)
753 goto exit;
754
755 result = eeepc_new_rfkill(&ehotk->wimax_rfkill,
756 "eeepc-wimax", dev,
757 RFKILL_TYPE_WIMAX, CM_ASL_WIMAX);
758
759 if (result && result != -ENODEV)
760 goto exit;
761
762 result = eeepc_setup_pci_hotplug();
763 /*
764 * If we get -EBUSY then something else is handling the PCI hotplug -
765 * don't fail in this case
766 */
767 if (result == -EBUSY)
768 result = 0;
769
770 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P5");
771 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P6");
772 eeepc_register_rfkill_notifier("\\_SB.PCI0.P0P7");
773 /*
774 * Refresh pci hotplug in case the rfkill state was changed during
775 * setup.
776 */
777 eeepc_rfkill_hotplug();
778
779 exit:
780 if (result && result != -ENODEV)
781 eeepc_rfkill_exit();
782 return result;
783 }
784
785 /*
786 * Platform driver - hibernate/resume callbacks
787 */
788 static int eeepc_hotk_thaw(struct device *device)
789 {
790 if (ehotk->wlan_rfkill) {
791 bool wlan;
792
793 /*
794 * Work around bios bug - acpi _PTS turns off the wireless led
795 * during suspend. Normally it restores it on resume, but
796 * we should kick it ourselves in case hibernation is aborted.
797 */
798 wlan = get_acpi(CM_ASL_WLAN);
799 set_acpi(CM_ASL_WLAN, wlan);
800 }
801
802 return 0;
803 }
804
805 static int eeepc_hotk_restore(struct device *device)
806 {
807 /* Refresh both wlan rfkill state and pci hotplug */
808 if (ehotk->wlan_rfkill)
809 eeepc_rfkill_hotplug();
810
811 if (ehotk->bluetooth_rfkill)
812 rfkill_set_sw_state(ehotk->bluetooth_rfkill,
813 get_acpi(CM_ASL_BLUETOOTH) != 1);
814 if (ehotk->wwan3g_rfkill)
815 rfkill_set_sw_state(ehotk->wwan3g_rfkill,
816 get_acpi(CM_ASL_3G) != 1);
817 if (ehotk->wimax_rfkill)
818 rfkill_set_sw_state(ehotk->wimax_rfkill,
819 get_acpi(CM_ASL_WIMAX) != 1);
820
821 return 0;
822 }
823
824 static struct dev_pm_ops eeepc_pm_ops = {
825 .thaw = eeepc_hotk_thaw,
826 .restore = eeepc_hotk_restore,
827 };
828
829 static struct platform_driver platform_driver = {
830 .driver = {
831 .name = EEEPC_HOTK_FILE,
832 .owner = THIS_MODULE,
833 .pm = &eeepc_pm_ops,
834 }
835 };
836
837 /*
838 * Hwmon device
839 */
840
841 #define EEEPC_EC_SC00 0x61
842 #define EEEPC_EC_FAN_PWM (EEEPC_EC_SC00 + 2) /* Fan PWM duty cycle (%) */
843 #define EEEPC_EC_FAN_HRPM (EEEPC_EC_SC00 + 5) /* High byte, fan speed (RPM) */
844 #define EEEPC_EC_FAN_LRPM (EEEPC_EC_SC00 + 6) /* Low byte, fan speed (RPM) */
845
846 #define EEEPC_EC_SFB0 0xD0
847 #define EEEPC_EC_FAN_CTRL (EEEPC_EC_SFB0 + 3) /* Byte containing SF25 */
848
849 static int eeepc_get_fan_pwm(void)
850 {
851 u8 value = 0;
852
853 ec_read(EEEPC_EC_FAN_PWM, &value);
854 return value * 255 / 100;
855 }
856
857 static void eeepc_set_fan_pwm(int value)
858 {
859 value = SENSORS_LIMIT(value, 0, 255);
860 value = value * 100 / 255;
861 ec_write(EEEPC_EC_FAN_PWM, value);
862 }
863
864 static int eeepc_get_fan_rpm(void)
865 {
866 u8 high = 0;
867 u8 low = 0;
868
869 ec_read(EEEPC_EC_FAN_HRPM, &high);
870 ec_read(EEEPC_EC_FAN_LRPM, &low);
871 return high << 8 | low;
872 }
873
874 static int eeepc_get_fan_ctrl(void)
875 {
876 u8 value = 0;
877
878 ec_read(EEEPC_EC_FAN_CTRL, &value);
879 if (value & 0x02)
880 return 1; /* manual */
881 else
882 return 2; /* automatic */
883 }
884
885 static void eeepc_set_fan_ctrl(int manual)
886 {
887 u8 value = 0;
888
889 ec_read(EEEPC_EC_FAN_CTRL, &value);
890 if (manual == 1)
891 value |= 0x02;
892 else
893 value &= ~0x02;
894 ec_write(EEEPC_EC_FAN_CTRL, value);
895 }
896
897 static ssize_t store_sys_hwmon(void (*set)(int), const char *buf, size_t count)
898 {
899 int rv, value;
900
901 rv = parse_arg(buf, count, &value);
902 if (rv > 0)
903 set(value);
904 return rv;
905 }
906
907 static ssize_t show_sys_hwmon(int (*get)(void), char *buf)
908 {
909 return sprintf(buf, "%d\n", get());
910 }
911
912 #define EEEPC_CREATE_SENSOR_ATTR(_name, _mode, _set, _get) \
913 static ssize_t show_##_name(struct device *dev, \
914 struct device_attribute *attr, \
915 char *buf) \
916 { \
917 return show_sys_hwmon(_set, buf); \
918 } \
919 static ssize_t store_##_name(struct device *dev, \
920 struct device_attribute *attr, \
921 const char *buf, size_t count) \
922 { \
923 return store_sys_hwmon(_get, buf, count); \
924 } \
925 static SENSOR_DEVICE_ATTR(_name, _mode, show_##_name, store_##_name, 0);
926
927 EEEPC_CREATE_SENSOR_ATTR(fan1_input, S_IRUGO, eeepc_get_fan_rpm, NULL);
928 EEEPC_CREATE_SENSOR_ATTR(pwm1, S_IRUGO | S_IWUSR,
929 eeepc_get_fan_pwm, eeepc_set_fan_pwm);
930 EEEPC_CREATE_SENSOR_ATTR(pwm1_enable, S_IRUGO | S_IWUSR,
931 eeepc_get_fan_ctrl, eeepc_set_fan_ctrl);
932
933 static ssize_t
934 show_name(struct device *dev, struct device_attribute *attr, char *buf)
935 {
936 return sprintf(buf, "eeepc\n");
937 }
938 static SENSOR_DEVICE_ATTR(name, S_IRUGO, show_name, NULL, 0);
939
940 static struct attribute *hwmon_attributes[] = {
941 &sensor_dev_attr_pwm1.dev_attr.attr,
942 &sensor_dev_attr_fan1_input.dev_attr.attr,
943 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
944 &sensor_dev_attr_name.dev_attr.attr,
945 NULL
946 };
947
948 static struct attribute_group hwmon_attribute_group = {
949 .attrs = hwmon_attributes
950 };
951
952 static void eeepc_hwmon_exit(void)
953 {
954 struct device *hwmon;
955
956 hwmon = eeepc_hwmon_device;
957 if (!hwmon)
958 return ;
959 sysfs_remove_group(&hwmon->kobj,
960 &hwmon_attribute_group);
961 hwmon_device_unregister(hwmon);
962 eeepc_hwmon_device = NULL;
963 }
964
965 static int eeepc_hwmon_init(struct device *dev)
966 {
967 struct device *hwmon;
968 int result;
969
970 hwmon = hwmon_device_register(dev);
971 if (IS_ERR(hwmon)) {
972 pr_err("Could not register eeepc hwmon device\n");
973 eeepc_hwmon_device = NULL;
974 return PTR_ERR(hwmon);
975 }
976 eeepc_hwmon_device = hwmon;
977 result = sysfs_create_group(&hwmon->kobj,
978 &hwmon_attribute_group);
979 if (result)
980 eeepc_hwmon_exit();
981 return result;
982 }
983
984 /*
985 * Backlight device
986 */
987 static int read_brightness(struct backlight_device *bd)
988 {
989 return get_acpi(CM_ASL_PANELBRIGHT);
990 }
991
992 static int set_brightness(struct backlight_device *bd, int value)
993 {
994 return set_acpi(CM_ASL_PANELBRIGHT, value);
995 }
996
997 static int update_bl_status(struct backlight_device *bd)
998 {
999 return set_brightness(bd, bd->props.brightness);
1000 }
1001
1002 static struct backlight_ops eeepcbl_ops = {
1003 .get_brightness = read_brightness,
1004 .update_status = update_bl_status,
1005 };
1006
1007 static int eeepc_backlight_notify(void)
1008 {
1009 struct backlight_device *bd = eeepc_backlight_device;
1010 int old = bd->props.brightness;
1011
1012 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1013
1014 return old;
1015 }
1016
1017 static int eeepc_backlight_init(struct device *dev)
1018 {
1019 struct backlight_device *bd;
1020
1021 bd = backlight_device_register(EEEPC_HOTK_FILE, dev,
1022 NULL, &eeepcbl_ops);
1023 if (IS_ERR(bd)) {
1024 pr_err("Could not register eeepc backlight device\n");
1025 eeepc_backlight_device = NULL;
1026 return PTR_ERR(bd);
1027 }
1028 eeepc_backlight_device = bd;
1029 bd->props.max_brightness = 15;
1030 bd->props.brightness = read_brightness(NULL);
1031 bd->props.power = FB_BLANK_UNBLANK;
1032 backlight_update_status(bd);
1033 return 0;
1034 }
1035
1036 static void eeepc_backlight_exit(void)
1037 {
1038 if (eeepc_backlight_device)
1039 backlight_device_unregister(eeepc_backlight_device);
1040 eeepc_backlight_device = NULL;
1041 }
1042
1043
1044 /*
1045 * Input device (i.e. hotkeys)
1046 */
1047 static struct key_entry *eeepc_get_entry_by_scancode(int code)
1048 {
1049 struct key_entry *key;
1050
1051 for (key = eeepc_keymap; key->type != KE_END; key++)
1052 if (code == key->code)
1053 return key;
1054
1055 return NULL;
1056 }
1057
1058 static void eeepc_input_notify(int event)
1059 {
1060 static struct key_entry *key;
1061
1062 key = eeepc_get_entry_by_scancode(event);
1063 if (key) {
1064 switch (key->type) {
1065 case KE_KEY:
1066 input_report_key(ehotk->inputdev, key->keycode,
1067 1);
1068 input_sync(ehotk->inputdev);
1069 input_report_key(ehotk->inputdev, key->keycode,
1070 0);
1071 input_sync(ehotk->inputdev);
1072 break;
1073 }
1074 }
1075 }
1076
1077 static struct key_entry *eepc_get_entry_by_keycode(int code)
1078 {
1079 struct key_entry *key;
1080
1081 for (key = eeepc_keymap; key->type != KE_END; key++)
1082 if (code == key->keycode && key->type == KE_KEY)
1083 return key;
1084
1085 return NULL;
1086 }
1087
1088 static int eeepc_getkeycode(struct input_dev *dev, int scancode, int *keycode)
1089 {
1090 struct key_entry *key = eeepc_get_entry_by_scancode(scancode);
1091
1092 if (key && key->type == KE_KEY) {
1093 *keycode = key->keycode;
1094 return 0;
1095 }
1096
1097 return -EINVAL;
1098 }
1099
1100 static int eeepc_setkeycode(struct input_dev *dev, int scancode, int keycode)
1101 {
1102 struct key_entry *key;
1103 int old_keycode;
1104
1105 if (keycode < 0 || keycode > KEY_MAX)
1106 return -EINVAL;
1107
1108 key = eeepc_get_entry_by_scancode(scancode);
1109 if (key && key->type == KE_KEY) {
1110 old_keycode = key->keycode;
1111 key->keycode = keycode;
1112 set_bit(keycode, dev->keybit);
1113 if (!eepc_get_entry_by_keycode(old_keycode))
1114 clear_bit(old_keycode, dev->keybit);
1115 return 0;
1116 }
1117
1118 return -EINVAL;
1119 }
1120
1121 static int eeepc_input_init(struct device *dev)
1122 {
1123 const struct key_entry *key;
1124 int result;
1125
1126 ehotk->inputdev = input_allocate_device();
1127 if (!ehotk->inputdev) {
1128 pr_info("Unable to allocate input device\n");
1129 return -ENOMEM;
1130 }
1131 ehotk->inputdev->name = "Asus EeePC extra buttons";
1132 ehotk->inputdev->dev.parent = dev;
1133 ehotk->inputdev->phys = EEEPC_HOTK_FILE "/input0";
1134 ehotk->inputdev->id.bustype = BUS_HOST;
1135 ehotk->inputdev->getkeycode = eeepc_getkeycode;
1136 ehotk->inputdev->setkeycode = eeepc_setkeycode;
1137
1138 for (key = eeepc_keymap; key->type != KE_END; key++) {
1139 switch (key->type) {
1140 case KE_KEY:
1141 set_bit(EV_KEY, ehotk->inputdev->evbit);
1142 set_bit(key->keycode, ehotk->inputdev->keybit);
1143 break;
1144 }
1145 }
1146 result = input_register_device(ehotk->inputdev);
1147 if (result) {
1148 pr_info("Unable to register input device\n");
1149 input_free_device(ehotk->inputdev);
1150 return result;
1151 }
1152 return 0;
1153 }
1154
1155 static void eeepc_input_exit(void)
1156 {
1157 if (ehotk->inputdev)
1158 input_unregister_device(ehotk->inputdev);
1159 }
1160
1161 /*
1162 * ACPI driver
1163 */
1164 static void eeepc_hotk_notify(struct acpi_device *device, u32 event)
1165 {
1166 u16 count;
1167
1168 if (event > ACPI_MAX_SYS_NOTIFY)
1169 return;
1170 count = ehotk->event_count[event % 128]++;
1171 acpi_bus_generate_proc_event(ehotk->device, event, count);
1172 acpi_bus_generate_netlink_event(ehotk->device->pnp.device_class,
1173 dev_name(&ehotk->device->dev), event,
1174 count);
1175
1176 if (event >= NOTIFY_BRN_MIN && event <= NOTIFY_BRN_MAX) {
1177 int old_brightness, new_brightness;
1178
1179 /* Update backlight device. */
1180 old_brightness = eeepc_backlight_notify();
1181
1182 /* Convert brightness event to keypress (obsolescent hack). */
1183 new_brightness = event - NOTIFY_BRN_MIN;
1184
1185 if (new_brightness < old_brightness) {
1186 event = NOTIFY_BRN_MIN; /* brightness down */
1187 } else if (new_brightness > old_brightness) {
1188 event = NOTIFY_BRN_MAX; /* brightness up */
1189 } else {
1190 /*
1191 * no change in brightness - already at min/max,
1192 * event will be desired value (or else ignored).
1193 */
1194 }
1195 }
1196 eeepc_input_notify(event);
1197 }
1198
1199 static void cmsg_quirk(int cm, const char *name)
1200 {
1201 int dummy;
1202
1203 /* Some BIOSes do not report cm although it is avaliable.
1204 Check if cm_getv[cm] works and, if yes, assume cm should be set. */
1205 if (!(ehotk->cm_supported & (1 << cm))
1206 && !read_acpi_int(ehotk->handle, cm_getv[cm], &dummy)) {
1207 pr_info("%s (%x) not reported by BIOS,"
1208 " enabling anyway\n", name, 1 << cm);
1209 ehotk->cm_supported |= 1 << cm;
1210 }
1211 }
1212
1213 static void cmsg_quirks(void)
1214 {
1215 cmsg_quirk(CM_ASL_LID, "LID");
1216 cmsg_quirk(CM_ASL_TYPE, "TYPE");
1217 cmsg_quirk(CM_ASL_PANELPOWER, "PANELPOWER");
1218 cmsg_quirk(CM_ASL_TPD, "TPD");
1219 }
1220
1221 static int eeepc_hotk_init(void)
1222 {
1223 unsigned int init_flags;
1224 int result;
1225
1226 result = acpi_bus_get_status(ehotk->device);
1227 if (result)
1228 return result;
1229 if (!ehotk->device->status.present) {
1230 pr_err("Hotkey device not present, aborting\n");
1231 return -ENODEV;
1232 }
1233
1234 init_flags = DISABLE_ASL_WLAN | DISABLE_ASL_DISPLAYSWITCH;
1235 pr_notice("Hotkey init flags 0x%x\n", init_flags);
1236
1237 if (write_acpi_int(ehotk->handle, "INIT", init_flags)) {
1238 pr_err("Hotkey initialization failed\n");
1239 return -ENODEV;
1240 }
1241
1242 /* get control methods supported */
1243 if (read_acpi_int(ehotk->handle, "CMSG", &ehotk->cm_supported)) {
1244 pr_err("Get control methods supported failed\n");
1245 return -ENODEV;
1246 }
1247 cmsg_quirks();
1248 pr_info("Get control methods supported: 0x%x\n", ehotk->cm_supported);
1249
1250 return 0;
1251 }
1252
1253 static void __devinit eeepc_enable_camera(void)
1254 {
1255 /*
1256 * If the following call to set_acpi() fails, it's because there's no
1257 * camera so we can ignore the error.
1258 */
1259 if (get_acpi(CM_ASL_CAMERA) == 0)
1260 set_acpi(CM_ASL_CAMERA, 1);
1261 }
1262
1263 static int __devinit eeepc_hotk_add(struct acpi_device *device)
1264 {
1265 struct device *dev;
1266 int result;
1267
1268 pr_notice(EEEPC_HOTK_NAME "\n");
1269 ehotk = kzalloc(sizeof(struct eeepc_hotk), GFP_KERNEL);
1270 if (!ehotk)
1271 return -ENOMEM;
1272 ehotk->handle = device->handle;
1273 strcpy(acpi_device_name(device), EEEPC_HOTK_DEVICE_NAME);
1274 strcpy(acpi_device_class(device), EEEPC_HOTK_CLASS);
1275 device->driver_data = ehotk;
1276 ehotk->device = device;
1277
1278 result = eeepc_hotk_init();
1279 if (result)
1280 goto fail_platform;
1281 eeepc_enable_camera();
1282
1283 result = eeepc_platform_init();
1284 if (result)
1285 goto fail_platform;
1286
1287 dev = &platform_device->dev;
1288
1289 if (!acpi_video_backlight_support()) {
1290 result = eeepc_backlight_init(dev);
1291 if (result)
1292 goto fail_backlight;
1293 } else
1294 pr_info("Backlight controlled by ACPI video driver\n");
1295
1296 result = eeepc_input_init(dev);
1297 if (result)
1298 goto fail_input;
1299
1300 result = eeepc_hwmon_init(dev);
1301 if (result)
1302 goto fail_hwmon;
1303
1304 result = eeepc_led_init(dev);
1305 if (result)
1306 goto fail_led;
1307
1308 result = eeepc_rfkill_init(dev);
1309 if (result)
1310 goto fail_rfkill;
1311
1312 return 0;
1313
1314 fail_rfkill:
1315 eeepc_led_exit();
1316 fail_led:
1317 eeepc_hwmon_exit();
1318 fail_hwmon:
1319 eeepc_input_exit();
1320 fail_input:
1321 eeepc_backlight_exit();
1322 fail_backlight:
1323 eeepc_platform_exit();
1324 fail_platform:
1325 kfree(ehotk);
1326
1327 return result;
1328 }
1329
1330 static int eeepc_hotk_remove(struct acpi_device *device, int type)
1331 {
1332 eeepc_backlight_exit();
1333 eeepc_rfkill_exit();
1334 eeepc_input_exit();
1335 eeepc_hwmon_exit();
1336 eeepc_led_exit();
1337 eeepc_platform_exit();
1338
1339 kfree(ehotk);
1340 return 0;
1341 }
1342
1343
1344 static const struct acpi_device_id eeepc_device_ids[] = {
1345 {EEEPC_HOTK_HID, 0},
1346 {"", 0},
1347 };
1348 MODULE_DEVICE_TABLE(acpi, eeepc_device_ids);
1349
1350 static struct acpi_driver eeepc_hotk_driver = {
1351 .name = EEEPC_HOTK_NAME,
1352 .class = EEEPC_HOTK_CLASS,
1353 .owner = THIS_MODULE,
1354 .ids = eeepc_device_ids,
1355 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1356 .ops = {
1357 .add = eeepc_hotk_add,
1358 .remove = eeepc_hotk_remove,
1359 .notify = eeepc_hotk_notify,
1360 },
1361 };
1362
1363
1364 static int __init eeepc_laptop_init(void)
1365 {
1366 int result;
1367
1368 result = platform_driver_register(&platform_driver);
1369 if (result < 0)
1370 return result;
1371
1372 result = acpi_bus_register_driver(&eeepc_hotk_driver);
1373 if (result < 0)
1374 goto fail_acpi_driver;
1375 if (!ehotk) {
1376 result = -ENODEV;
1377 goto fail_no_device;
1378 }
1379 return 0;
1380
1381 fail_no_device:
1382 acpi_bus_unregister_driver(&eeepc_hotk_driver);
1383 fail_acpi_driver:
1384 platform_driver_unregister(&platform_driver);
1385 return result;
1386 }
1387
1388 static void __exit eeepc_laptop_exit(void)
1389 {
1390 acpi_bus_unregister_driver(&eeepc_hotk_driver);
1391 platform_driver_unregister(&platform_driver);
1392 }
1393
1394 module_init(eeepc_laptop_init);
1395 module_exit(eeepc_laptop_exit);